1 /* SPDX-License-Identifier: GPL-2.0 */
5 #define IND_DESTINATION_BIT 0
6 #define IND_INDIRECTION_BIT 1
8 #define IND_SOURCE_BIT 3
10 #define IND_DESTINATION (1 << IND_DESTINATION_BIT)
11 #define IND_INDIRECTION (1 << IND_INDIRECTION_BIT)
12 #define IND_DONE (1 << IND_DONE_BIT)
13 #define IND_SOURCE (1 << IND_SOURCE_BIT)
14 #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
16 #if !defined(__ASSEMBLY__)
18 #include <linux/vmcore_info.h>
19 #include <linux/crash_reserve.h>
21 #include <linux/range.h>
23 #include <uapi/linux/kexec.h>
24 #include <linux/verification.h>
26 extern note_buf_t __percpu *crash_notes;
28 #ifdef CONFIG_CRASH_DUMP
29 #include <linux/prandom.h>
32 #ifdef CONFIG_KEXEC_CORE
33 #include <linux/list.h>
34 #include <linux/compat.h>
35 #include <linux/ioport.h>
36 #include <linux/module.h>
37 #include <linux/highmem.h>
38 #include <asm/kexec.h>
39 #include <linux/crash_core.h>
41 /* Verify architecture specific macros are defined */
43 #ifndef KEXEC_SOURCE_MEMORY_LIMIT
44 #error KEXEC_SOURCE_MEMORY_LIMIT not defined
47 #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
48 #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
51 #ifndef KEXEC_CONTROL_MEMORY_LIMIT
52 #error KEXEC_CONTROL_MEMORY_LIMIT not defined
55 #ifndef KEXEC_CONTROL_MEMORY_GFP
56 #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
59 #ifndef KEXEC_CONTROL_PAGE_SIZE
60 #error KEXEC_CONTROL_PAGE_SIZE not defined
64 #error KEXEC_ARCH not defined
67 #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
68 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
71 #ifndef KEXEC_CRASH_MEM_ALIGN
72 #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
76 * This structure is used to hold the arguments that are used when loading
80 typedef unsigned long kimage_entry_t;
82 struct kexec_segment {
84 * This pointer can point to user memory if kexec_load() system
85 * call is used or will point to kernel memory if
86 * kexec_file_load() system call is used.
88 * Use ->buf when expecting to deal with user memory and use ->kbuf
89 * when expecting to deal with kernel memory.
101 struct compat_kexec_segment {
104 compat_ulong_t mem; /* User space sees this as a (void *) ... */
109 #ifdef CONFIG_KEXEC_FILE
110 struct purgatory_info {
112 * Pointer to elf header at the beginning of kexec_purgatory.
113 * Note: kexec_purgatory is read only
115 const Elf_Ehdr *ehdr;
117 * Temporary, modifiable buffer for sechdrs used for relocation.
118 * This memory can be freed post image load.
122 * Temporary, modifiable buffer for stripped purgatory used for
123 * relocation. This memory can be freed post image load.
130 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
131 typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
132 unsigned long kernel_len, char *initrd,
133 unsigned long initrd_len, char *cmdline,
134 unsigned long cmdline_len);
135 typedef int (kexec_cleanup_t)(void *loader_data);
137 #ifdef CONFIG_KEXEC_SIG
138 typedef int (kexec_verify_sig_t)(const char *kernel_buf,
139 unsigned long kernel_len);
142 struct kexec_file_ops {
143 kexec_probe_t *probe;
145 kexec_cleanup_t *cleanup;
146 #ifdef CONFIG_KEXEC_SIG
147 kexec_verify_sig_t *verify_sig;
151 extern const struct kexec_file_ops * const kexec_file_loaders[];
153 int kexec_image_probe_default(struct kimage *image, void *buf,
154 unsigned long buf_len);
155 int kexec_image_post_load_cleanup_default(struct kimage *image);
158 * If kexec_buf.mem is set to this value, kexec_locate_mem_hole()
159 * will try to allocate free memory. Arch may overwrite it.
161 #ifndef KEXEC_BUF_MEM_UNKNOWN
162 #define KEXEC_BUF_MEM_UNKNOWN 0
166 * struct kexec_buf - parameters for finding a place for a buffer in memory
167 * @image: kexec image in which memory to search.
168 * @buffer: Contents which will be copied to the allocated memory.
169 * @bufsz: Size of @buffer.
170 * @mem: On return will have address of the buffer in memory.
171 * @memsz: Size for the buffer in memory.
172 * @buf_align: Minimum alignment needed.
173 * @buf_min: The buffer can't be placed below this address.
174 * @buf_max: The buffer can't be placed above this address.
175 * @top_down: Allocate from top of memory.
176 * @random: Place the buffer at a random position.
179 struct kimage *image;
184 unsigned long buf_align;
185 unsigned long buf_min;
186 unsigned long buf_max;
188 #ifdef CONFIG_CRASH_DUMP
194 #ifdef CONFIG_CRASH_DUMP
195 static inline void kexec_random_range_start(unsigned long start,
197 struct kexec_buf *kbuf,
198 unsigned long *temp_start)
203 get_random_bytes(&i, sizeof(unsigned short));
204 *temp_start = start + (end - start) / USHRT_MAX * i;
208 static inline void kexec_random_range_start(unsigned long start,
210 struct kexec_buf *kbuf,
211 unsigned long *temp_start)
215 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
216 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
217 void *buf, unsigned int size,
219 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
221 #ifndef arch_kexec_kernel_image_probe
223 arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len)
225 return kexec_image_probe_default(image, buf, buf_len);
229 #ifndef arch_kimage_file_post_load_cleanup
230 static inline int arch_kimage_file_post_load_cleanup(struct kimage *image)
232 return kexec_image_post_load_cleanup_default(image);
236 #ifndef arch_check_excluded_range
237 static inline int arch_check_excluded_range(struct kimage *image,
245 #ifdef CONFIG_KEXEC_SIG
246 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
247 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
251 extern int kexec_add_buffer(struct kexec_buf *kbuf);
252 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
254 #ifndef arch_kexec_locate_mem_hole
256 * arch_kexec_locate_mem_hole - Find free memory to place the segments.
257 * @kbuf: Parameters for the memory search.
259 * On success, kbuf->mem will have the start address of the memory region found.
261 * Return: 0 on success, negative errno on error.
263 static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
265 return kexec_locate_mem_hole(kbuf);
269 #ifndef arch_kexec_apply_relocations_add
271 * arch_kexec_apply_relocations_add - apply relocations of type RELA
272 * @pi: Purgatory to be relocated.
273 * @section: Section relocations applying to.
274 * @relsec: Section containing RELAs.
275 * @symtab: Corresponding symtab.
277 * Return: 0 on success, negative errno on error.
280 arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
281 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
283 pr_err("RELA relocation unsupported.\n");
288 #ifndef arch_kexec_apply_relocations
290 * arch_kexec_apply_relocations - apply relocations of type REL
291 * @pi: Purgatory to be relocated.
292 * @section: Section relocations applying to.
293 * @relsec: Section containing RELs.
294 * @symtab: Corresponding symtab.
296 * Return: 0 on success, negative errno on error.
299 arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
300 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
302 pr_err("REL relocation unsupported.\n");
306 #endif /* CONFIG_KEXEC_FILE */
308 #ifdef CONFIG_KEXEC_ELF
309 struct kexec_elf_info {
311 * Where the ELF binary contents are kept.
312 * Memory managed by the user of the struct.
316 const struct elfhdr *ehdr;
317 const struct elf_phdr *proghdrs;
320 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
321 struct kexec_elf_info *elf_info);
323 int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
324 struct kexec_elf_info *elf_info,
325 struct kexec_buf *kbuf,
326 unsigned long *lowest_load_addr);
328 void kexec_free_elf_info(struct kexec_elf_info *elf_info);
329 int kexec_elf_probe(const char *buf, unsigned long len);
333 kimage_entry_t *entry;
334 kimage_entry_t *last_entry;
337 struct page *control_code_page;
338 struct page *swap_page;
339 void *vmcoreinfo_data_copy; /* locates in the crash memory */
341 unsigned long nr_segments;
342 struct kexec_segment segment[KEXEC_SEGMENT_MAX];
344 struct list_head control_pages;
345 struct list_head dest_pages;
346 struct list_head unusable_pages;
348 /* Address of next control page to allocate for crash kernels. */
349 unsigned long control_page;
351 /* Flags to indicate special processing */
352 unsigned int type : 1;
353 #define KEXEC_TYPE_DEFAULT 0
354 #define KEXEC_TYPE_CRASH 1
355 unsigned int preserve_context : 1;
356 /* If set, we are using file mode kexec syscall */
357 unsigned int file_mode:1;
358 #ifdef CONFIG_CRASH_HOTPLUG
359 /* If set, it is safe to update kexec segments that are
360 * excluded from SHA calculation.
362 unsigned int hotplug_support:1;
365 #ifdef ARCH_HAS_KIMAGE_ARCH
366 struct kimage_arch arch;
369 #ifdef CONFIG_KEXEC_FILE
370 /* Additional fields for file based kexec syscall */
372 unsigned long kernel_buf_len;
375 unsigned long initrd_buf_len;
378 unsigned long cmdline_buf_len;
380 /* File operations provided by image loader */
381 const struct kexec_file_ops *fops;
383 /* Image loader handling the kernel can store a pointer here */
384 void *image_loader_data;
386 /* Information for loading purgatory */
387 struct purgatory_info purgatory_info;
390 #ifdef CONFIG_CRASH_HOTPLUG
392 int elfcorehdr_index;
393 bool elfcorehdr_updated;
396 #ifdef CONFIG_IMA_KEXEC
397 /* Virtual address of IMA measurement buffer for kexec syscall */
400 phys_addr_t ima_buffer_addr;
401 size_t ima_buffer_size;
403 unsigned long ima_segment_index;
404 bool is_ima_segment_index_set;
408 struct kexec_segment *scratch;
412 /* Core ELF header buffer */
414 unsigned long elf_headers_sz;
415 unsigned long elf_load_addr;
417 /* dm crypt keys buffer */
418 unsigned long dm_crypt_keys_addr;
419 unsigned long dm_crypt_keys_sz;
422 /* kexec interface functions */
423 extern void machine_kexec(struct kimage *image);
424 extern int machine_kexec_prepare(struct kimage *image);
425 extern void machine_kexec_cleanup(struct kimage *image);
426 extern int kernel_kexec(void);
427 extern struct page *kimage_alloc_control_pages(struct kimage *image,
430 #ifndef machine_kexec_post_load
431 static inline int machine_kexec_post_load(struct kimage *image) { return 0; }
434 extern struct kimage *kexec_image;
435 extern struct kimage *kexec_crash_image;
437 bool kexec_load_permitted(int kexec_image_type);
439 #ifndef kexec_flush_icache_page
440 #define kexec_flush_icache_page(page)
443 /* List of defined/legal kexec flags */
444 #ifndef CONFIG_KEXEC_JUMP
445 #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_UPDATE_ELFCOREHDR | KEXEC_CRASH_HOTPLUG_SUPPORT)
447 #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR | \
448 KEXEC_CRASH_HOTPLUG_SUPPORT)
451 /* List of defined/legal kexec file flags */
452 #define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
453 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
455 /* flag to track if kexec reboot is in progress */
456 extern bool kexec_in_progress;
458 #ifndef page_to_boot_pfn
459 static inline unsigned long page_to_boot_pfn(struct page *page)
461 return page_to_pfn(page);
465 #ifndef boot_pfn_to_page
466 static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
468 return pfn_to_page(boot_pfn);
472 #ifndef phys_to_boot_phys
473 static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
479 #ifndef boot_phys_to_phys
480 static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
486 #ifndef crash_free_reserved_phys_range
487 static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
491 for (addr = begin; addr < end; addr += PAGE_SIZE)
492 free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
496 static inline unsigned long virt_to_boot_phys(void *addr)
498 return phys_to_boot_phys(__pa((unsigned long)addr));
501 static inline void *boot_phys_to_virt(unsigned long entry)
503 return phys_to_virt(boot_phys_to_phys(entry));
506 #ifndef arch_kexec_post_alloc_pages
507 static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
510 #ifndef arch_kexec_pre_free_pages
511 static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
514 extern bool kexec_file_dbg_print;
516 #define kexec_dprintk(fmt, arg...) \
517 do { if (kexec_file_dbg_print) pr_info(fmt, ##arg); } while (0)
519 extern void *kimage_map_segment(struct kimage *image, unsigned long addr, unsigned long size);
520 extern void kimage_unmap_segment(void *buffer);
521 #else /* !CONFIG_KEXEC_CORE */
525 static inline void __crash_kexec(struct pt_regs *regs) { }
526 static inline void crash_kexec(struct pt_regs *regs) { }
527 static inline int kexec_should_crash(struct task_struct *p) { return 0; }
528 static inline int kexec_crash_loaded(void) { return 0; }
529 static inline void *kimage_map_segment(struct kimage *image, unsigned long addr, unsigned long size)
531 static inline void kimage_unmap_segment(void *buffer) { }
532 #define kexec_in_progress false
533 #endif /* CONFIG_KEXEC_CORE */
535 #ifdef CONFIG_KEXEC_SIG
536 void set_kexec_sig_enforced(void);
538 static inline void set_kexec_sig_enforced(void) {}
541 #endif /* !defined(__ASSEBMLY__) */
543 #endif /* LINUX_KEXEC_H */