at v6.7 14 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef LINUX_KEXEC_H 3#define LINUX_KEXEC_H 4 5#define IND_DESTINATION_BIT 0 6#define IND_INDIRECTION_BIT 1 7#define IND_DONE_BIT 2 8#define IND_SOURCE_BIT 3 9 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) 15 16#if !defined(__ASSEMBLY__) 17 18#include <linux/crash_core.h> 19#include <asm/io.h> 20#include <linux/range.h> 21 22#include <uapi/linux/kexec.h> 23#include <linux/verification.h> 24 25extern note_buf_t __percpu *crash_notes; 26 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> 34 35/* Verify architecture specific macros are defined */ 36 37#ifndef KEXEC_SOURCE_MEMORY_LIMIT 38#error KEXEC_SOURCE_MEMORY_LIMIT not defined 39#endif 40 41#ifndef KEXEC_DESTINATION_MEMORY_LIMIT 42#error KEXEC_DESTINATION_MEMORY_LIMIT not defined 43#endif 44 45#ifndef KEXEC_CONTROL_MEMORY_LIMIT 46#error KEXEC_CONTROL_MEMORY_LIMIT not defined 47#endif 48 49#ifndef KEXEC_CONTROL_MEMORY_GFP 50#define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY) 51#endif 52 53#ifndef KEXEC_CONTROL_PAGE_SIZE 54#error KEXEC_CONTROL_PAGE_SIZE not defined 55#endif 56 57#ifndef KEXEC_ARCH 58#error KEXEC_ARCH not defined 59#endif 60 61#ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT 62#define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT 63#endif 64 65#ifndef KEXEC_CRASH_MEM_ALIGN 66#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE 67#endif 68 69#define KEXEC_CORE_NOTE_NAME CRASH_CORE_NOTE_NAME 70 71/* 72 * This structure is used to hold the arguments that are used when loading 73 * kernel binaries. 74 */ 75 76typedef unsigned long kimage_entry_t; 77 78struct kexec_segment { 79 /* 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. 83 * 84 * Use ->buf when expecting to deal with user memory and use ->kbuf 85 * when expecting to deal with kernel memory. 86 */ 87 union { 88 void __user *buf; 89 void *kbuf; 90 }; 91 size_t bufsz; 92 unsigned long mem; 93 size_t memsz; 94}; 95 96#ifdef CONFIG_COMPAT 97struct compat_kexec_segment { 98 compat_uptr_t buf; 99 compat_size_t bufsz; 100 compat_ulong_t mem; /* User space sees this as a (void *) ... */ 101 compat_size_t memsz; 102}; 103#endif 104 105#ifdef CONFIG_KEXEC_FILE 106struct purgatory_info { 107 /* 108 * Pointer to elf header at the beginning of kexec_purgatory. 109 * Note: kexec_purgatory is read only 110 */ 111 const Elf_Ehdr *ehdr; 112 /* 113 * Temporary, modifiable buffer for sechdrs used for relocation. 114 * This memory can be freed post image load. 115 */ 116 Elf_Shdr *sechdrs; 117 /* 118 * Temporary, modifiable buffer for stripped purgatory used for 119 * relocation. This memory can be freed post image load. 120 */ 121 void *purgatory_buf; 122}; 123 124struct kimage; 125 126typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size); 127typedef 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); 131typedef int (kexec_cleanup_t)(void *loader_data); 132 133#ifdef CONFIG_KEXEC_SIG 134typedef int (kexec_verify_sig_t)(const char *kernel_buf, 135 unsigned long kernel_len); 136#endif 137 138struct kexec_file_ops { 139 kexec_probe_t *probe; 140 kexec_load_t *load; 141 kexec_cleanup_t *cleanup; 142#ifdef CONFIG_KEXEC_SIG 143 kexec_verify_sig_t *verify_sig; 144#endif 145}; 146 147extern const struct kexec_file_ops * const kexec_file_loaders[]; 148 149int kexec_image_probe_default(struct kimage *image, void *buf, 150 unsigned long buf_len); 151int kexec_image_post_load_cleanup_default(struct kimage *image); 152 153/* 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. 156 */ 157#ifndef KEXEC_BUF_MEM_UNKNOWN 158#define KEXEC_BUF_MEM_UNKNOWN 0 159#endif 160 161/** 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. 172 */ 173struct kexec_buf { 174 struct kimage *image; 175 void *buffer; 176 unsigned long bufsz; 177 unsigned long mem; 178 unsigned long memsz; 179 unsigned long buf_align; 180 unsigned long buf_min; 181 unsigned long buf_max; 182 bool top_down; 183}; 184 185int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf); 186int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name, 187 void *buf, unsigned int size, 188 bool get_value); 189void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name); 190 191#ifndef arch_kexec_kernel_image_probe 192static inline int 193arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len) 194{ 195 return kexec_image_probe_default(image, buf, buf_len); 196} 197#endif 198 199#ifndef arch_kimage_file_post_load_cleanup 200static inline int arch_kimage_file_post_load_cleanup(struct kimage *image) 201{ 202 return kexec_image_post_load_cleanup_default(image); 203} 204#endif 205 206#ifdef CONFIG_KEXEC_SIG 207#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION 208int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len); 209#endif 210#endif 211 212extern int kexec_add_buffer(struct kexec_buf *kbuf); 213int kexec_locate_mem_hole(struct kexec_buf *kbuf); 214 215#ifndef arch_kexec_locate_mem_hole 216/** 217 * arch_kexec_locate_mem_hole - Find free memory to place the segments. 218 * @kbuf: Parameters for the memory search. 219 * 220 * On success, kbuf->mem will have the start address of the memory region found. 221 * 222 * Return: 0 on success, negative errno on error. 223 */ 224static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf) 225{ 226 return kexec_locate_mem_hole(kbuf); 227} 228#endif 229 230#ifndef arch_kexec_apply_relocations_add 231/* 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. 237 * 238 * Return: 0 on success, negative errno on error. 239 */ 240static inline int 241arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section, 242 const Elf_Shdr *relsec, const Elf_Shdr *symtab) 243{ 244 pr_err("RELA relocation unsupported.\n"); 245 return -ENOEXEC; 246} 247#endif 248 249#ifndef arch_kexec_apply_relocations 250/* 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. 256 * 257 * Return: 0 on success, negative errno on error. 258 */ 259static inline int 260arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section, 261 const Elf_Shdr *relsec, const Elf_Shdr *symtab) 262{ 263 pr_err("REL relocation unsupported.\n"); 264 return -ENOEXEC; 265} 266#endif 267#endif /* CONFIG_KEXEC_FILE */ 268 269#ifdef CONFIG_KEXEC_ELF 270struct kexec_elf_info { 271 /* 272 * Where the ELF binary contents are kept. 273 * Memory managed by the user of the struct. 274 */ 275 const char *buffer; 276 277 const struct elfhdr *ehdr; 278 const struct elf_phdr *proghdrs; 279}; 280 281int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr, 282 struct kexec_elf_info *elf_info); 283 284int 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); 288 289void kexec_free_elf_info(struct kexec_elf_info *elf_info); 290int kexec_elf_probe(const char *buf, unsigned long len); 291#endif 292struct kimage { 293 kimage_entry_t head; 294 kimage_entry_t *entry; 295 kimage_entry_t *last_entry; 296 297 unsigned long start; 298 struct page *control_code_page; 299 struct page *swap_page; 300 void *vmcoreinfo_data_copy; /* locates in the crash memory */ 301 302 unsigned long nr_segments; 303 struct kexec_segment segment[KEXEC_SEGMENT_MAX]; 304 305 struct list_head control_pages; 306 struct list_head dest_pages; 307 struct list_head unusable_pages; 308 309 /* Address of next control page to allocate for crash kernels. */ 310 unsigned long control_page; 311 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; 322#endif 323 324#ifdef ARCH_HAS_KIMAGE_ARCH 325 struct kimage_arch arch; 326#endif 327 328#ifdef CONFIG_KEXEC_FILE 329 /* Additional fields for file based kexec syscall */ 330 void *kernel_buf; 331 unsigned long kernel_buf_len; 332 333 void *initrd_buf; 334 unsigned long initrd_buf_len; 335 336 char *cmdline_buf; 337 unsigned long cmdline_buf_len; 338 339 /* File operations provided by image loader */ 340 const struct kexec_file_ops *fops; 341 342 /* Image loader handling the kernel can store a pointer here */ 343 void *image_loader_data; 344 345 /* Information for loading purgatory */ 346 struct purgatory_info purgatory_info; 347#endif 348 349#ifdef CONFIG_CRASH_HOTPLUG 350 int hp_action; 351 int elfcorehdr_index; 352 bool elfcorehdr_updated; 353#endif 354 355#ifdef CONFIG_IMA_KEXEC 356 /* Virtual address of IMA measurement buffer for kexec syscall */ 357 void *ima_buffer; 358 359 phys_addr_t ima_buffer_addr; 360 size_t ima_buffer_size; 361#endif 362 363 /* Core ELF header buffer */ 364 void *elf_headers; 365 unsigned long elf_headers_sz; 366 unsigned long elf_load_addr; 367}; 368 369/* kexec interface functions */ 370extern void machine_kexec(struct kimage *image); 371extern int machine_kexec_prepare(struct kimage *image); 372extern void machine_kexec_cleanup(struct kimage *image); 373extern int kernel_kexec(void); 374extern struct page *kimage_alloc_control_pages(struct kimage *image, 375 unsigned int order); 376 377#ifndef machine_kexec_post_load 378static inline int machine_kexec_post_load(struct kimage *image) { return 0; } 379#endif 380 381extern void __crash_kexec(struct pt_regs *); 382extern void crash_kexec(struct pt_regs *); 383int kexec_should_crash(struct task_struct *); 384int kexec_crash_loaded(void); 385void crash_save_cpu(struct pt_regs *regs, int cpu); 386extern int kimage_crash_copy_vmcoreinfo(struct kimage *image); 387 388extern struct kimage *kexec_image; 389extern struct kimage *kexec_crash_image; 390 391bool kexec_load_permitted(int kexec_image_type); 392 393#ifndef kexec_flush_icache_page 394#define kexec_flush_icache_page(page) 395#endif 396 397/* List of defined/legal kexec flags */ 398#ifndef CONFIG_KEXEC_JUMP 399#define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_UPDATE_ELFCOREHDR) 400#else 401#define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR) 402#endif 403 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) 407 408/* flag to track if kexec reboot is in progress */ 409extern bool kexec_in_progress; 410 411int crash_shrink_memory(unsigned long new_size); 412ssize_t crash_get_memory_size(void); 413 414#ifndef arch_kexec_protect_crashkres 415/* 416 * Protection mechanism for crashkernel reserved memory after 417 * the kdump kernel is loaded. 418 * 419 * Provide an empty default implementation here -- architecture 420 * code may override this 421 */ 422static inline void arch_kexec_protect_crashkres(void) { } 423#endif 424 425#ifndef arch_kexec_unprotect_crashkres 426static inline void arch_kexec_unprotect_crashkres(void) { } 427#endif 428 429#ifndef page_to_boot_pfn 430static inline unsigned long page_to_boot_pfn(struct page *page) 431{ 432 return page_to_pfn(page); 433} 434#endif 435 436#ifndef boot_pfn_to_page 437static inline struct page *boot_pfn_to_page(unsigned long boot_pfn) 438{ 439 return pfn_to_page(boot_pfn); 440} 441#endif 442 443#ifndef phys_to_boot_phys 444static inline unsigned long phys_to_boot_phys(phys_addr_t phys) 445{ 446 return phys; 447} 448#endif 449 450#ifndef boot_phys_to_phys 451static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys) 452{ 453 return boot_phys; 454} 455#endif 456 457#ifndef crash_free_reserved_phys_range 458static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end) 459{ 460 unsigned long addr; 461 462 for (addr = begin; addr < end; addr += PAGE_SIZE) 463 free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT)); 464} 465#endif 466 467static inline unsigned long virt_to_boot_phys(void *addr) 468{ 469 return phys_to_boot_phys(__pa((unsigned long)addr)); 470} 471 472static inline void *boot_phys_to_virt(unsigned long entry) 473{ 474 return phys_to_virt(boot_phys_to_phys(entry)); 475} 476 477#ifndef arch_kexec_post_alloc_pages 478static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; } 479#endif 480 481#ifndef arch_kexec_pre_free_pages 482static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { } 483#endif 484 485#ifndef arch_crash_handle_hotplug_event 486static inline void arch_crash_handle_hotplug_event(struct kimage *image) { } 487#endif 488 489int crash_check_update_elfcorehdr(void); 490 491#ifndef crash_hotplug_cpu_support 492static inline int crash_hotplug_cpu_support(void) { return 0; } 493#endif 494 495#ifndef crash_hotplug_memory_support 496static inline int crash_hotplug_memory_support(void) { return 0; } 497#endif 498 499#ifndef crash_get_elfcorehdr_size 500static inline unsigned int crash_get_elfcorehdr_size(void) { return 0; } 501#endif 502 503#else /* !CONFIG_KEXEC_CORE */ 504struct pt_regs; 505struct task_struct; 506static inline void __crash_kexec(struct pt_regs *regs) { } 507static inline void crash_kexec(struct pt_regs *regs) { } 508static inline int kexec_should_crash(struct task_struct *p) { return 0; } 509static inline int kexec_crash_loaded(void) { return 0; } 510#define kexec_in_progress false 511#endif /* CONFIG_KEXEC_CORE */ 512 513#ifdef CONFIG_KEXEC_SIG 514void set_kexec_sig_enforced(void); 515#else 516static inline void set_kexec_sig_enforced(void) {} 517#endif 518 519#endif /* !defined(__ASSEBMLY__) */ 520 521#endif /* LINUX_KEXEC_H */