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/crash_core.h>
20 #include <linux/range.h>
22 #include <uapi/linux/kexec.h>
23 #include <linux/verification.h>
25 extern note_buf_t __percpu *crash_notes;
27 #ifdef CONFIG_KEXEC_CORE
28 #include <linux/list.h>
29 #include <linux/compat.h>
30 #include <linux/ioport.h>
31 #include <linux/module.h>
32 #include <linux/highmem.h>
33 #include <asm/kexec.h>
35 /* Verify architecture specific macros are defined */
37 #ifndef KEXEC_SOURCE_MEMORY_LIMIT
38 #error KEXEC_SOURCE_MEMORY_LIMIT not defined
41 #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
42 #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
45 #ifndef KEXEC_CONTROL_MEMORY_LIMIT
46 #error KEXEC_CONTROL_MEMORY_LIMIT not defined
49 #ifndef KEXEC_CONTROL_MEMORY_GFP
50 #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
53 #ifndef KEXEC_CONTROL_PAGE_SIZE
54 #error KEXEC_CONTROL_PAGE_SIZE not defined
58 #error KEXEC_ARCH not defined
61 #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
62 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
65 #ifndef KEXEC_CRASH_MEM_ALIGN
66 #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
69 #define KEXEC_CORE_NOTE_NAME CRASH_CORE_NOTE_NAME
72 * This structure is used to hold the arguments that are used when loading
76 typedef unsigned long kimage_entry_t;
78 struct kexec_segment {
80 * This pointer can point to user memory if kexec_load() system
81 * call is used or will point to kernel memory if
82 * kexec_file_load() system call is used.
84 * Use ->buf when expecting to deal with user memory and use ->kbuf
85 * when expecting to deal with kernel memory.
97 struct compat_kexec_segment {
100 compat_ulong_t mem; /* User space sees this as a (void *) ... */
105 #ifdef CONFIG_KEXEC_FILE
106 struct purgatory_info {
108 * Pointer to elf header at the beginning of kexec_purgatory.
109 * Note: kexec_purgatory is read only
111 const Elf_Ehdr *ehdr;
113 * Temporary, modifiable buffer for sechdrs used for relocation.
114 * This memory can be freed post image load.
118 * Temporary, modifiable buffer for stripped purgatory used for
119 * relocation. This memory can be freed post image load.
126 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
127 typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
128 unsigned long kernel_len, char *initrd,
129 unsigned long initrd_len, char *cmdline,
130 unsigned long cmdline_len);
131 typedef int (kexec_cleanup_t)(void *loader_data);
133 #ifdef CONFIG_KEXEC_SIG
134 typedef int (kexec_verify_sig_t)(const char *kernel_buf,
135 unsigned long kernel_len);
138 struct kexec_file_ops {
139 kexec_probe_t *probe;
141 kexec_cleanup_t *cleanup;
142 #ifdef CONFIG_KEXEC_SIG
143 kexec_verify_sig_t *verify_sig;
147 extern const struct kexec_file_ops * const kexec_file_loaders[];
149 int kexec_image_probe_default(struct kimage *image, void *buf,
150 unsigned long buf_len);
151 int kexec_image_post_load_cleanup_default(struct kimage *image);
154 * If kexec_buf.mem is set to this value, kexec_locate_mem_hole()
155 * will try to allocate free memory. Arch may overwrite it.
157 #ifndef KEXEC_BUF_MEM_UNKNOWN
158 #define KEXEC_BUF_MEM_UNKNOWN 0
162 * struct kexec_buf - parameters for finding a place for a buffer in memory
163 * @image: kexec image in which memory to search.
164 * @buffer: Contents which will be copied to the allocated memory.
165 * @bufsz: Size of @buffer.
166 * @mem: On return will have address of the buffer in memory.
167 * @memsz: Size for the buffer in memory.
168 * @buf_align: Minimum alignment needed.
169 * @buf_min: The buffer can't be placed below this address.
170 * @buf_max: The buffer can't be placed above this address.
171 * @top_down: Allocate from top of memory.
174 struct kimage *image;
179 unsigned long buf_align;
180 unsigned long buf_min;
181 unsigned long buf_max;
185 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
186 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
187 void *buf, unsigned int size,
189 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
191 #ifndef arch_kexec_kernel_image_probe
193 arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len)
195 return kexec_image_probe_default(image, buf, buf_len);
199 #ifndef arch_kimage_file_post_load_cleanup
200 static inline int arch_kimage_file_post_load_cleanup(struct kimage *image)
202 return kexec_image_post_load_cleanup_default(image);
206 #ifdef CONFIG_KEXEC_SIG
207 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
208 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
212 extern int kexec_add_buffer(struct kexec_buf *kbuf);
213 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
215 #ifndef arch_kexec_locate_mem_hole
217 * arch_kexec_locate_mem_hole - Find free memory to place the segments.
218 * @kbuf: Parameters for the memory search.
220 * On success, kbuf->mem will have the start address of the memory region found.
222 * Return: 0 on success, negative errno on error.
224 static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
226 return kexec_locate_mem_hole(kbuf);
230 #ifndef arch_kexec_apply_relocations_add
232 * arch_kexec_apply_relocations_add - apply relocations of type RELA
233 * @pi: Purgatory to be relocated.
234 * @section: Section relocations applying to.
235 * @relsec: Section containing RELAs.
236 * @symtab: Corresponding symtab.
238 * Return: 0 on success, negative errno on error.
241 arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
242 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
244 pr_err("RELA relocation unsupported.\n");
249 #ifndef arch_kexec_apply_relocations
251 * arch_kexec_apply_relocations - apply relocations of type REL
252 * @pi: Purgatory to be relocated.
253 * @section: Section relocations applying to.
254 * @relsec: Section containing RELs.
255 * @symtab: Corresponding symtab.
257 * Return: 0 on success, negative errno on error.
260 arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
261 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
263 pr_err("REL relocation unsupported.\n");
267 #endif /* CONFIG_KEXEC_FILE */
269 #ifdef CONFIG_KEXEC_ELF
270 struct kexec_elf_info {
272 * Where the ELF binary contents are kept.
273 * Memory managed by the user of the struct.
277 const struct elfhdr *ehdr;
278 const struct elf_phdr *proghdrs;
281 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
282 struct kexec_elf_info *elf_info);
284 int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
285 struct kexec_elf_info *elf_info,
286 struct kexec_buf *kbuf,
287 unsigned long *lowest_load_addr);
289 void kexec_free_elf_info(struct kexec_elf_info *elf_info);
290 int kexec_elf_probe(const char *buf, unsigned long len);
294 kimage_entry_t *entry;
295 kimage_entry_t *last_entry;
298 struct page *control_code_page;
299 struct page *swap_page;
300 void *vmcoreinfo_data_copy; /* locates in the crash memory */
302 unsigned long nr_segments;
303 struct kexec_segment segment[KEXEC_SEGMENT_MAX];
305 struct list_head control_pages;
306 struct list_head dest_pages;
307 struct list_head unusable_pages;
309 /* Address of next control page to allocate for crash kernels. */
310 unsigned long control_page;
312 /* Flags to indicate special processing */
313 unsigned int type : 1;
314 #define KEXEC_TYPE_DEFAULT 0
315 #define KEXEC_TYPE_CRASH 1
316 unsigned int preserve_context : 1;
317 /* If set, we are using file mode kexec syscall */
318 unsigned int file_mode:1;
319 #ifdef CONFIG_CRASH_HOTPLUG
320 /* If set, allow changes to elfcorehdr of kexec_load'd image */
321 unsigned int update_elfcorehdr:1;
324 #ifdef ARCH_HAS_KIMAGE_ARCH
325 struct kimage_arch arch;
328 #ifdef CONFIG_KEXEC_FILE
329 /* Additional fields for file based kexec syscall */
331 unsigned long kernel_buf_len;
334 unsigned long initrd_buf_len;
337 unsigned long cmdline_buf_len;
339 /* File operations provided by image loader */
340 const struct kexec_file_ops *fops;
342 /* Image loader handling the kernel can store a pointer here */
343 void *image_loader_data;
345 /* Information for loading purgatory */
346 struct purgatory_info purgatory_info;
349 #ifdef CONFIG_CRASH_HOTPLUG
351 int elfcorehdr_index;
352 bool elfcorehdr_updated;
355 #ifdef CONFIG_IMA_KEXEC
356 /* Virtual address of IMA measurement buffer for kexec syscall */
359 phys_addr_t ima_buffer_addr;
360 size_t ima_buffer_size;
363 /* Core ELF header buffer */
365 unsigned long elf_headers_sz;
366 unsigned long elf_load_addr;
369 /* kexec interface functions */
370 extern void machine_kexec(struct kimage *image);
371 extern int machine_kexec_prepare(struct kimage *image);
372 extern void machine_kexec_cleanup(struct kimage *image);
373 extern int kernel_kexec(void);
374 extern struct page *kimage_alloc_control_pages(struct kimage *image,
377 #ifndef machine_kexec_post_load
378 static inline int machine_kexec_post_load(struct kimage *image) { return 0; }
381 extern void __crash_kexec(struct pt_regs *);
382 extern void crash_kexec(struct pt_regs *);
383 int kexec_should_crash(struct task_struct *);
384 int kexec_crash_loaded(void);
385 void crash_save_cpu(struct pt_regs *regs, int cpu);
386 extern int kimage_crash_copy_vmcoreinfo(struct kimage *image);
388 extern struct kimage *kexec_image;
389 extern struct kimage *kexec_crash_image;
391 bool kexec_load_permitted(int kexec_image_type);
393 #ifndef kexec_flush_icache_page
394 #define kexec_flush_icache_page(page)
397 /* List of defined/legal kexec flags */
398 #ifndef CONFIG_KEXEC_JUMP
399 #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_UPDATE_ELFCOREHDR)
401 #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR)
404 /* List of defined/legal kexec file flags */
405 #define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
406 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
408 /* flag to track if kexec reboot is in progress */
409 extern bool kexec_in_progress;
411 int crash_shrink_memory(unsigned long new_size);
412 ssize_t crash_get_memory_size(void);
414 #ifndef arch_kexec_protect_crashkres
416 * Protection mechanism for crashkernel reserved memory after
417 * the kdump kernel is loaded.
419 * Provide an empty default implementation here -- architecture
420 * code may override this
422 static inline void arch_kexec_protect_crashkres(void) { }
425 #ifndef arch_kexec_unprotect_crashkres
426 static inline void arch_kexec_unprotect_crashkres(void) { }
429 #ifndef page_to_boot_pfn
430 static inline unsigned long page_to_boot_pfn(struct page *page)
432 return page_to_pfn(page);
436 #ifndef boot_pfn_to_page
437 static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
439 return pfn_to_page(boot_pfn);
443 #ifndef phys_to_boot_phys
444 static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
450 #ifndef boot_phys_to_phys
451 static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
457 #ifndef crash_free_reserved_phys_range
458 static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
462 for (addr = begin; addr < end; addr += PAGE_SIZE)
463 free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
467 static inline unsigned long virt_to_boot_phys(void *addr)
469 return phys_to_boot_phys(__pa((unsigned long)addr));
472 static inline void *boot_phys_to_virt(unsigned long entry)
474 return phys_to_virt(boot_phys_to_phys(entry));
477 #ifndef arch_kexec_post_alloc_pages
478 static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
481 #ifndef arch_kexec_pre_free_pages
482 static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
485 #ifndef arch_crash_handle_hotplug_event
486 static inline void arch_crash_handle_hotplug_event(struct kimage *image) { }
489 int crash_check_update_elfcorehdr(void);
491 #ifndef crash_hotplug_cpu_support
492 static inline int crash_hotplug_cpu_support(void) { return 0; }
495 #ifndef crash_hotplug_memory_support
496 static inline int crash_hotplug_memory_support(void) { return 0; }
499 #ifndef crash_get_elfcorehdr_size
500 static inline unsigned int crash_get_elfcorehdr_size(void) { return 0; }
503 extern bool kexec_file_dbg_print;
505 #define kexec_dprintk(fmt, ...) \
507 kexec_file_dbg_print ? KERN_INFO : KERN_DEBUG, \
510 #else /* !CONFIG_KEXEC_CORE */
513 static inline void __crash_kexec(struct pt_regs *regs) { }
514 static inline void crash_kexec(struct pt_regs *regs) { }
515 static inline int kexec_should_crash(struct task_struct *p) { return 0; }
516 static inline int kexec_crash_loaded(void) { return 0; }
517 #define kexec_in_progress false
518 #endif /* CONFIG_KEXEC_CORE */
520 #ifdef CONFIG_KEXEC_SIG
521 void set_kexec_sig_enforced(void);
523 static inline void set_kexec_sig_enforced(void) {}
526 #endif /* !defined(__ASSEBMLY__) */
528 #endif /* LINUX_KEXEC_H */