Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef LINUX_KEXEC_H
0003 #define LINUX_KEXEC_H
0004 
0005 #define IND_DESTINATION_BIT 0
0006 #define IND_INDIRECTION_BIT 1
0007 #define IND_DONE_BIT        2
0008 #define IND_SOURCE_BIT      3
0009 
0010 #define IND_DESTINATION  (1 << IND_DESTINATION_BIT)
0011 #define IND_INDIRECTION  (1 << IND_INDIRECTION_BIT)
0012 #define IND_DONE         (1 << IND_DONE_BIT)
0013 #define IND_SOURCE       (1 << IND_SOURCE_BIT)
0014 #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
0015 
0016 #if !defined(__ASSEMBLY__)
0017 
0018 #include <linux/crash_core.h>
0019 #include <asm/io.h>
0020 
0021 #include <uapi/linux/kexec.h>
0022 #include <linux/verification.h>
0023 
0024 /* Location of a reserved region to hold the crash kernel.
0025  */
0026 extern struct resource crashk_res;
0027 extern struct resource crashk_low_res;
0028 extern note_buf_t __percpu *crash_notes;
0029 
0030 #ifdef CONFIG_KEXEC_CORE
0031 #include <linux/list.h>
0032 #include <linux/compat.h>
0033 #include <linux/ioport.h>
0034 #include <linux/module.h>
0035 #include <asm/kexec.h>
0036 
0037 /* Verify architecture specific macros are defined */
0038 
0039 #ifndef KEXEC_SOURCE_MEMORY_LIMIT
0040 #error KEXEC_SOURCE_MEMORY_LIMIT not defined
0041 #endif
0042 
0043 #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
0044 #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
0045 #endif
0046 
0047 #ifndef KEXEC_CONTROL_MEMORY_LIMIT
0048 #error KEXEC_CONTROL_MEMORY_LIMIT not defined
0049 #endif
0050 
0051 #ifndef KEXEC_CONTROL_MEMORY_GFP
0052 #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
0053 #endif
0054 
0055 #ifndef KEXEC_CONTROL_PAGE_SIZE
0056 #error KEXEC_CONTROL_PAGE_SIZE not defined
0057 #endif
0058 
0059 #ifndef KEXEC_ARCH
0060 #error KEXEC_ARCH not defined
0061 #endif
0062 
0063 #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
0064 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
0065 #endif
0066 
0067 #ifndef KEXEC_CRASH_MEM_ALIGN
0068 #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
0069 #endif
0070 
0071 #define KEXEC_CORE_NOTE_NAME    CRASH_CORE_NOTE_NAME
0072 
0073 /*
0074  * This structure is used to hold the arguments that are used when loading
0075  * kernel binaries.
0076  */
0077 
0078 typedef unsigned long kimage_entry_t;
0079 
0080 struct kexec_segment {
0081     /*
0082      * This pointer can point to user memory if kexec_load() system
0083      * call is used or will point to kernel memory if
0084      * kexec_file_load() system call is used.
0085      *
0086      * Use ->buf when expecting to deal with user memory and use ->kbuf
0087      * when expecting to deal with kernel memory.
0088      */
0089     union {
0090         void __user *buf;
0091         void *kbuf;
0092     };
0093     size_t bufsz;
0094     unsigned long mem;
0095     size_t memsz;
0096 };
0097 
0098 #ifdef CONFIG_COMPAT
0099 struct compat_kexec_segment {
0100     compat_uptr_t buf;
0101     compat_size_t bufsz;
0102     compat_ulong_t mem; /* User space sees this as a (void *) ... */
0103     compat_size_t memsz;
0104 };
0105 #endif
0106 
0107 #ifdef CONFIG_KEXEC_FILE
0108 struct purgatory_info {
0109     /*
0110      * Pointer to elf header at the beginning of kexec_purgatory.
0111      * Note: kexec_purgatory is read only
0112      */
0113     const Elf_Ehdr *ehdr;
0114     /*
0115      * Temporary, modifiable buffer for sechdrs used for relocation.
0116      * This memory can be freed post image load.
0117      */
0118     Elf_Shdr *sechdrs;
0119     /*
0120      * Temporary, modifiable buffer for stripped purgatory used for
0121      * relocation. This memory can be freed post image load.
0122      */
0123     void *purgatory_buf;
0124 };
0125 
0126 struct kimage;
0127 
0128 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
0129 typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
0130                  unsigned long kernel_len, char *initrd,
0131                  unsigned long initrd_len, char *cmdline,
0132                  unsigned long cmdline_len);
0133 typedef int (kexec_cleanup_t)(void *loader_data);
0134 
0135 #ifdef CONFIG_KEXEC_SIG
0136 typedef int (kexec_verify_sig_t)(const char *kernel_buf,
0137                  unsigned long kernel_len);
0138 #endif
0139 
0140 struct kexec_file_ops {
0141     kexec_probe_t *probe;
0142     kexec_load_t *load;
0143     kexec_cleanup_t *cleanup;
0144 #ifdef CONFIG_KEXEC_SIG
0145     kexec_verify_sig_t *verify_sig;
0146 #endif
0147 };
0148 
0149 extern const struct kexec_file_ops * const kexec_file_loaders[];
0150 
0151 int kexec_image_probe_default(struct kimage *image, void *buf,
0152                   unsigned long buf_len);
0153 int kexec_image_post_load_cleanup_default(struct kimage *image);
0154 
0155 /*
0156  * If kexec_buf.mem is set to this value, kexec_locate_mem_hole()
0157  * will try to allocate free memory. Arch may overwrite it.
0158  */
0159 #ifndef KEXEC_BUF_MEM_UNKNOWN
0160 #define KEXEC_BUF_MEM_UNKNOWN 0
0161 #endif
0162 
0163 /**
0164  * struct kexec_buf - parameters for finding a place for a buffer in memory
0165  * @image:  kexec image in which memory to search.
0166  * @buffer: Contents which will be copied to the allocated memory.
0167  * @bufsz:  Size of @buffer.
0168  * @mem:    On return will have address of the buffer in memory.
0169  * @memsz:  Size for the buffer in memory.
0170  * @buf_align:  Minimum alignment needed.
0171  * @buf_min:    The buffer can't be placed below this address.
0172  * @buf_max:    The buffer can't be placed above this address.
0173  * @top_down:   Allocate from top of memory.
0174  */
0175 struct kexec_buf {
0176     struct kimage *image;
0177     void *buffer;
0178     unsigned long bufsz;
0179     unsigned long mem;
0180     unsigned long memsz;
0181     unsigned long buf_align;
0182     unsigned long buf_min;
0183     unsigned long buf_max;
0184     bool top_down;
0185 };
0186 
0187 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
0188 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
0189                    void *buf, unsigned int size,
0190                    bool get_value);
0191 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
0192 void *kexec_image_load_default(struct kimage *image);
0193 
0194 #ifndef arch_kexec_kernel_image_probe
0195 static inline int
0196 arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len)
0197 {
0198     return kexec_image_probe_default(image, buf, buf_len);
0199 }
0200 #endif
0201 
0202 #ifndef arch_kimage_file_post_load_cleanup
0203 static inline int arch_kimage_file_post_load_cleanup(struct kimage *image)
0204 {
0205     return kexec_image_post_load_cleanup_default(image);
0206 }
0207 #endif
0208 
0209 #ifndef arch_kexec_kernel_image_load
0210 static inline void *arch_kexec_kernel_image_load(struct kimage *image)
0211 {
0212     return kexec_image_load_default(image);
0213 }
0214 #endif
0215 
0216 #ifdef CONFIG_KEXEC_SIG
0217 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
0218 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
0219 #endif
0220 #endif
0221 
0222 extern int kexec_add_buffer(struct kexec_buf *kbuf);
0223 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
0224 
0225 #ifndef arch_kexec_locate_mem_hole
0226 /**
0227  * arch_kexec_locate_mem_hole - Find free memory to place the segments.
0228  * @kbuf:                       Parameters for the memory search.
0229  *
0230  * On success, kbuf->mem will have the start address of the memory region found.
0231  *
0232  * Return: 0 on success, negative errno on error.
0233  */
0234 static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
0235 {
0236     return kexec_locate_mem_hole(kbuf);
0237 }
0238 #endif
0239 
0240 /* Alignment required for elf header segment */
0241 #define ELF_CORE_HEADER_ALIGN   4096
0242 
0243 struct crash_mem_range {
0244     u64 start, end;
0245 };
0246 
0247 struct crash_mem {
0248     unsigned int max_nr_ranges;
0249     unsigned int nr_ranges;
0250     struct crash_mem_range ranges[];
0251 };
0252 
0253 extern int crash_exclude_mem_range(struct crash_mem *mem,
0254                    unsigned long long mstart,
0255                    unsigned long long mend);
0256 extern int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
0257                        void **addr, unsigned long *sz);
0258 
0259 #ifndef arch_kexec_apply_relocations_add
0260 /*
0261  * arch_kexec_apply_relocations_add - apply relocations of type RELA
0262  * @pi:     Purgatory to be relocated.
0263  * @section:    Section relocations applying to.
0264  * @relsec: Section containing RELAs.
0265  * @symtab: Corresponding symtab.
0266  *
0267  * Return: 0 on success, negative errno on error.
0268  */
0269 static inline int
0270 arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
0271                  const Elf_Shdr *relsec, const Elf_Shdr *symtab)
0272 {
0273     pr_err("RELA relocation unsupported.\n");
0274     return -ENOEXEC;
0275 }
0276 #endif
0277 
0278 #ifndef arch_kexec_apply_relocations
0279 /*
0280  * arch_kexec_apply_relocations - apply relocations of type REL
0281  * @pi:     Purgatory to be relocated.
0282  * @section:    Section relocations applying to.
0283  * @relsec: Section containing RELs.
0284  * @symtab: Corresponding symtab.
0285  *
0286  * Return: 0 on success, negative errno on error.
0287  */
0288 static inline int
0289 arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
0290                  const Elf_Shdr *relsec, const Elf_Shdr *symtab)
0291 {
0292     pr_err("REL relocation unsupported.\n");
0293     return -ENOEXEC;
0294 }
0295 #endif
0296 #endif /* CONFIG_KEXEC_FILE */
0297 
0298 #ifdef CONFIG_KEXEC_ELF
0299 struct kexec_elf_info {
0300     /*
0301      * Where the ELF binary contents are kept.
0302      * Memory managed by the user of the struct.
0303      */
0304     const char *buffer;
0305 
0306     const struct elfhdr *ehdr;
0307     const struct elf_phdr *proghdrs;
0308 };
0309 
0310 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
0311                    struct kexec_elf_info *elf_info);
0312 
0313 int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
0314              struct kexec_elf_info *elf_info,
0315              struct kexec_buf *kbuf,
0316              unsigned long *lowest_load_addr);
0317 
0318 void kexec_free_elf_info(struct kexec_elf_info *elf_info);
0319 int kexec_elf_probe(const char *buf, unsigned long len);
0320 #endif
0321 struct kimage {
0322     kimage_entry_t head;
0323     kimage_entry_t *entry;
0324     kimage_entry_t *last_entry;
0325 
0326     unsigned long start;
0327     struct page *control_code_page;
0328     struct page *swap_page;
0329     void *vmcoreinfo_data_copy; /* locates in the crash memory */
0330 
0331     unsigned long nr_segments;
0332     struct kexec_segment segment[KEXEC_SEGMENT_MAX];
0333 
0334     struct list_head control_pages;
0335     struct list_head dest_pages;
0336     struct list_head unusable_pages;
0337 
0338     /* Address of next control page to allocate for crash kernels. */
0339     unsigned long control_page;
0340 
0341     /* Flags to indicate special processing */
0342     unsigned int type : 1;
0343 #define KEXEC_TYPE_DEFAULT 0
0344 #define KEXEC_TYPE_CRASH   1
0345     unsigned int preserve_context : 1;
0346     /* If set, we are using file mode kexec syscall */
0347     unsigned int file_mode:1;
0348 
0349 #ifdef ARCH_HAS_KIMAGE_ARCH
0350     struct kimage_arch arch;
0351 #endif
0352 
0353 #ifdef CONFIG_KEXEC_FILE
0354     /* Additional fields for file based kexec syscall */
0355     void *kernel_buf;
0356     unsigned long kernel_buf_len;
0357 
0358     void *initrd_buf;
0359     unsigned long initrd_buf_len;
0360 
0361     char *cmdline_buf;
0362     unsigned long cmdline_buf_len;
0363 
0364     /* File operations provided by image loader */
0365     const struct kexec_file_ops *fops;
0366 
0367     /* Image loader handling the kernel can store a pointer here */
0368     void *image_loader_data;
0369 
0370     /* Information for loading purgatory */
0371     struct purgatory_info purgatory_info;
0372 #endif
0373 
0374 #ifdef CONFIG_IMA_KEXEC
0375     /* Virtual address of IMA measurement buffer for kexec syscall */
0376     void *ima_buffer;
0377 
0378     phys_addr_t ima_buffer_addr;
0379     size_t ima_buffer_size;
0380 #endif
0381 
0382     /* Core ELF header buffer */
0383     void *elf_headers;
0384     unsigned long elf_headers_sz;
0385     unsigned long elf_load_addr;
0386 };
0387 
0388 /* kexec interface functions */
0389 extern void machine_kexec(struct kimage *image);
0390 extern int machine_kexec_prepare(struct kimage *image);
0391 extern void machine_kexec_cleanup(struct kimage *image);
0392 extern int kernel_kexec(void);
0393 extern struct page *kimage_alloc_control_pages(struct kimage *image,
0394                         unsigned int order);
0395 
0396 #ifndef machine_kexec_post_load
0397 static inline int machine_kexec_post_load(struct kimage *image) { return 0; }
0398 #endif
0399 
0400 extern void __crash_kexec(struct pt_regs *);
0401 extern void crash_kexec(struct pt_regs *);
0402 int kexec_should_crash(struct task_struct *);
0403 int kexec_crash_loaded(void);
0404 void crash_save_cpu(struct pt_regs *regs, int cpu);
0405 extern int kimage_crash_copy_vmcoreinfo(struct kimage *image);
0406 
0407 extern struct kimage *kexec_image;
0408 extern struct kimage *kexec_crash_image;
0409 extern int kexec_load_disabled;
0410 
0411 #ifndef kexec_flush_icache_page
0412 #define kexec_flush_icache_page(page)
0413 #endif
0414 
0415 /* List of defined/legal kexec flags */
0416 #ifndef CONFIG_KEXEC_JUMP
0417 #define KEXEC_FLAGS    KEXEC_ON_CRASH
0418 #else
0419 #define KEXEC_FLAGS    (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT)
0420 #endif
0421 
0422 /* List of defined/legal kexec file flags */
0423 #define KEXEC_FILE_FLAGS    (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
0424                  KEXEC_FILE_NO_INITRAMFS)
0425 
0426 /* flag to track if kexec reboot is in progress */
0427 extern bool kexec_in_progress;
0428 
0429 int crash_shrink_memory(unsigned long new_size);
0430 size_t crash_get_memory_size(void);
0431 
0432 #ifndef arch_kexec_protect_crashkres
0433 /*
0434  * Protection mechanism for crashkernel reserved memory after
0435  * the kdump kernel is loaded.
0436  *
0437  * Provide an empty default implementation here -- architecture
0438  * code may override this
0439  */
0440 static inline void arch_kexec_protect_crashkres(void) { }
0441 #endif
0442 
0443 #ifndef arch_kexec_unprotect_crashkres
0444 static inline void arch_kexec_unprotect_crashkres(void) { }
0445 #endif
0446 
0447 #ifndef page_to_boot_pfn
0448 static inline unsigned long page_to_boot_pfn(struct page *page)
0449 {
0450     return page_to_pfn(page);
0451 }
0452 #endif
0453 
0454 #ifndef boot_pfn_to_page
0455 static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
0456 {
0457     return pfn_to_page(boot_pfn);
0458 }
0459 #endif
0460 
0461 #ifndef phys_to_boot_phys
0462 static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
0463 {
0464     return phys;
0465 }
0466 #endif
0467 
0468 #ifndef boot_phys_to_phys
0469 static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
0470 {
0471     return boot_phys;
0472 }
0473 #endif
0474 
0475 #ifndef crash_free_reserved_phys_range
0476 static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
0477 {
0478     unsigned long addr;
0479 
0480     for (addr = begin; addr < end; addr += PAGE_SIZE)
0481         free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
0482 }
0483 #endif
0484 
0485 static inline unsigned long virt_to_boot_phys(void *addr)
0486 {
0487     return phys_to_boot_phys(__pa((unsigned long)addr));
0488 }
0489 
0490 static inline void *boot_phys_to_virt(unsigned long entry)
0491 {
0492     return phys_to_virt(boot_phys_to_phys(entry));
0493 }
0494 
0495 #ifndef arch_kexec_post_alloc_pages
0496 static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
0497 #endif
0498 
0499 #ifndef arch_kexec_pre_free_pages
0500 static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
0501 #endif
0502 
0503 #else /* !CONFIG_KEXEC_CORE */
0504 struct pt_regs;
0505 struct task_struct;
0506 static inline void __crash_kexec(struct pt_regs *regs) { }
0507 static inline void crash_kexec(struct pt_regs *regs) { }
0508 static inline int kexec_should_crash(struct task_struct *p) { return 0; }
0509 static inline int kexec_crash_loaded(void) { return 0; }
0510 #define kexec_in_progress false
0511 #endif /* CONFIG_KEXEC_CORE */
0512 
0513 #ifdef CONFIG_KEXEC_SIG
0514 void set_kexec_sig_enforced(void);
0515 #else
0516 static inline void set_kexec_sig_enforced(void) {}
0517 #endif
0518 
0519 #endif /* !defined(__ASSEBMLY__) */
0520 
0521 #endif /* LINUX_KEXEC_H */