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