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 /* Location of a reserved region to hold the crash kernel.
27 extern struct resource crashk_res;
28 extern struct resource crashk_low_res;
29 extern note_buf_t __percpu *crash_notes;
31 #ifdef CONFIG_KEXEC_CORE
32 #include <linux/list.h>
33 #include <linux/compat.h>
34 #include <linux/ioport.h>
35 #include <linux/module.h>
36 #include <linux/highmem.h>
37 #include <asm/kexec.h>
39 /* Verify architecture specific macros are defined */
41 #ifndef KEXEC_SOURCE_MEMORY_LIMIT
42 #error KEXEC_SOURCE_MEMORY_LIMIT not defined
45 #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
46 #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
49 #ifndef KEXEC_CONTROL_MEMORY_LIMIT
50 #error KEXEC_CONTROL_MEMORY_LIMIT not defined
53 #ifndef KEXEC_CONTROL_MEMORY_GFP
54 #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
57 #ifndef KEXEC_CONTROL_PAGE_SIZE
58 #error KEXEC_CONTROL_PAGE_SIZE not defined
62 #error KEXEC_ARCH not defined
65 #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
66 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
69 #ifndef KEXEC_CRASH_MEM_ALIGN
70 #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
73 #define KEXEC_CORE_NOTE_NAME CRASH_CORE_NOTE_NAME
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.
178 struct kimage *image;
183 unsigned long buf_align;
184 unsigned long buf_min;
185 unsigned long buf_max;
189 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
190 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
191 void *buf, unsigned int size,
193 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
195 #ifndef arch_kexec_kernel_image_probe
197 arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len)
199 return kexec_image_probe_default(image, buf, buf_len);
203 #ifndef arch_kimage_file_post_load_cleanup
204 static inline int arch_kimage_file_post_load_cleanup(struct kimage *image)
206 return kexec_image_post_load_cleanup_default(image);
210 #ifdef CONFIG_KEXEC_SIG
211 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
212 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
216 extern int kexec_add_buffer(struct kexec_buf *kbuf);
217 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
219 #ifndef arch_kexec_locate_mem_hole
221 * arch_kexec_locate_mem_hole - Find free memory to place the segments.
222 * @kbuf: Parameters for the memory search.
224 * On success, kbuf->mem will have the start address of the memory region found.
226 * Return: 0 on success, negative errno on error.
228 static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
230 return kexec_locate_mem_hole(kbuf);
234 #ifndef arch_kexec_apply_relocations_add
236 * arch_kexec_apply_relocations_add - apply relocations of type RELA
237 * @pi: Purgatory to be relocated.
238 * @section: Section relocations applying to.
239 * @relsec: Section containing RELAs.
240 * @symtab: Corresponding symtab.
242 * Return: 0 on success, negative errno on error.
245 arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
246 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
248 pr_err("RELA relocation unsupported.\n");
253 #ifndef arch_kexec_apply_relocations
255 * arch_kexec_apply_relocations - apply relocations of type REL
256 * @pi: Purgatory to be relocated.
257 * @section: Section relocations applying to.
258 * @relsec: Section containing RELs.
259 * @symtab: Corresponding symtab.
261 * Return: 0 on success, negative errno on error.
264 arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
265 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
267 pr_err("REL relocation unsupported.\n");
271 #endif /* CONFIG_KEXEC_FILE */
273 #ifdef CONFIG_KEXEC_ELF
274 struct kexec_elf_info {
276 * Where the ELF binary contents are kept.
277 * Memory managed by the user of the struct.
281 const struct elfhdr *ehdr;
282 const struct elf_phdr *proghdrs;
285 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
286 struct kexec_elf_info *elf_info);
288 int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
289 struct kexec_elf_info *elf_info,
290 struct kexec_buf *kbuf,
291 unsigned long *lowest_load_addr);
293 void kexec_free_elf_info(struct kexec_elf_info *elf_info);
294 int kexec_elf_probe(const char *buf, unsigned long len);
298 kimage_entry_t *entry;
299 kimage_entry_t *last_entry;
302 struct page *control_code_page;
303 struct page *swap_page;
304 void *vmcoreinfo_data_copy; /* locates in the crash memory */
306 unsigned long nr_segments;
307 struct kexec_segment segment[KEXEC_SEGMENT_MAX];
309 struct list_head control_pages;
310 struct list_head dest_pages;
311 struct list_head unusable_pages;
313 /* Address of next control page to allocate for crash kernels. */
314 unsigned long control_page;
316 /* Flags to indicate special processing */
317 unsigned int type : 1;
318 #define KEXEC_TYPE_DEFAULT 0
319 #define KEXEC_TYPE_CRASH 1
320 unsigned int preserve_context : 1;
321 /* If set, we are using file mode kexec syscall */
322 unsigned int file_mode:1;
323 #ifdef CONFIG_CRASH_HOTPLUG
324 /* If set, allow changes to elfcorehdr of kexec_load'd image */
325 unsigned int update_elfcorehdr:1;
328 #ifdef ARCH_HAS_KIMAGE_ARCH
329 struct kimage_arch arch;
332 #ifdef CONFIG_KEXEC_FILE
333 /* Additional fields for file based kexec syscall */
335 unsigned long kernel_buf_len;
338 unsigned long initrd_buf_len;
341 unsigned long cmdline_buf_len;
343 /* File operations provided by image loader */
344 const struct kexec_file_ops *fops;
346 /* Image loader handling the kernel can store a pointer here */
347 void *image_loader_data;
349 /* Information for loading purgatory */
350 struct purgatory_info purgatory_info;
353 #ifdef CONFIG_CRASH_HOTPLUG
355 int elfcorehdr_index;
356 bool elfcorehdr_updated;
359 #ifdef CONFIG_IMA_KEXEC
360 /* Virtual address of IMA measurement buffer for kexec syscall */
363 phys_addr_t ima_buffer_addr;
364 size_t ima_buffer_size;
367 /* Core ELF header buffer */
369 unsigned long elf_headers_sz;
370 unsigned long elf_load_addr;
373 /* kexec interface functions */
374 extern void machine_kexec(struct kimage *image);
375 extern int machine_kexec_prepare(struct kimage *image);
376 extern void machine_kexec_cleanup(struct kimage *image);
377 extern int kernel_kexec(void);
378 extern struct page *kimage_alloc_control_pages(struct kimage *image,
381 #ifndef machine_kexec_post_load
382 static inline int machine_kexec_post_load(struct kimage *image) { return 0; }
385 extern void __crash_kexec(struct pt_regs *);
386 extern void crash_kexec(struct pt_regs *);
387 int kexec_should_crash(struct task_struct *);
388 int kexec_crash_loaded(void);
389 void crash_save_cpu(struct pt_regs *regs, int cpu);
390 extern int kimage_crash_copy_vmcoreinfo(struct kimage *image);
392 extern struct kimage *kexec_image;
393 extern struct kimage *kexec_crash_image;
395 bool kexec_load_permitted(int kexec_image_type);
397 #ifndef kexec_flush_icache_page
398 #define kexec_flush_icache_page(page)
401 /* List of defined/legal kexec flags */
402 #ifndef CONFIG_KEXEC_JUMP
403 #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_UPDATE_ELFCOREHDR)
405 #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR)
408 /* List of defined/legal kexec file flags */
409 #define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
410 KEXEC_FILE_NO_INITRAMFS)
412 /* flag to track if kexec reboot is in progress */
413 extern bool kexec_in_progress;
415 int crash_shrink_memory(unsigned long new_size);
416 ssize_t crash_get_memory_size(void);
418 #ifndef arch_kexec_protect_crashkres
420 * Protection mechanism for crashkernel reserved memory after
421 * the kdump kernel is loaded.
423 * Provide an empty default implementation here -- architecture
424 * code may override this
426 static inline void arch_kexec_protect_crashkres(void) { }
429 #ifndef arch_kexec_unprotect_crashkres
430 static inline void arch_kexec_unprotect_crashkres(void) { }
433 #ifndef page_to_boot_pfn
434 static inline unsigned long page_to_boot_pfn(struct page *page)
436 return page_to_pfn(page);
440 #ifndef boot_pfn_to_page
441 static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
443 return pfn_to_page(boot_pfn);
447 #ifndef phys_to_boot_phys
448 static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
454 #ifndef boot_phys_to_phys
455 static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
461 #ifndef crash_free_reserved_phys_range
462 static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
466 for (addr = begin; addr < end; addr += PAGE_SIZE)
467 free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
471 static inline unsigned long virt_to_boot_phys(void *addr)
473 return phys_to_boot_phys(__pa((unsigned long)addr));
476 static inline void *boot_phys_to_virt(unsigned long entry)
478 return phys_to_virt(boot_phys_to_phys(entry));
481 #ifndef arch_kexec_post_alloc_pages
482 static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
485 #ifndef arch_kexec_pre_free_pages
486 static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
489 #ifndef arch_crash_handle_hotplug_event
490 static inline void arch_crash_handle_hotplug_event(struct kimage *image) { }
493 int crash_check_update_elfcorehdr(void);
495 #ifndef crash_hotplug_cpu_support
496 static inline int crash_hotplug_cpu_support(void) { return 0; }
499 #ifndef crash_hotplug_memory_support
500 static inline int crash_hotplug_memory_support(void) { return 0; }
503 #ifndef crash_get_elfcorehdr_size
504 static inline unsigned int crash_get_elfcorehdr_size(void) { return 0; }
507 #else /* !CONFIG_KEXEC_CORE */
510 static inline void __crash_kexec(struct pt_regs *regs) { }
511 static inline void crash_kexec(struct pt_regs *regs) { }
512 static inline int kexec_should_crash(struct task_struct *p) { return 0; }
513 static inline int kexec_crash_loaded(void) { return 0; }
514 #define kexec_in_progress false
515 #endif /* CONFIG_KEXEC_CORE */
517 #ifdef CONFIG_KEXEC_SIG
518 void set_kexec_sig_enforced(void);
520 static inline void set_kexec_sig_enforced(void) {}
523 #endif /* !defined(__ASSEBMLY__) */
525 #endif /* LINUX_KEXEC_H */