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/vmcore_info.h> 19 #include <linux/crash_reserve.h> 20 #include <asm/io.h> 21 #include <linux/range.h> 22 23 #include <uapi/linux/kexec.h> 24 #include <linux/verification.h> 25 26 extern note_buf_t __percpu *crash_notes; 27 28 #ifdef CONFIG_KEXEC_CORE 29 #include <linux/list.h> 30 #include <linux/compat.h> 31 #include <linux/ioport.h> 32 #include <linux/module.h> 33 #include <linux/highmem.h> 34 #include <asm/kexec.h> 35 #include <linux/crash_core.h> 36 37 /* Verify architecture specific macros are defined */ 38 39 #ifndef KEXEC_SOURCE_MEMORY_LIMIT 40 #error KEXEC_SOURCE_MEMORY_LIMIT not defined 41 #endif 42 43 #ifndef KEXEC_DESTINATION_MEMORY_LIMIT 44 #error KEXEC_DESTINATION_MEMORY_LIMIT not defined 45 #endif 46 47 #ifndef KEXEC_CONTROL_MEMORY_LIMIT 48 #error KEXEC_CONTROL_MEMORY_LIMIT not defined 49 #endif 50 51 #ifndef KEXEC_CONTROL_MEMORY_GFP 52 #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY) 53 #endif 54 55 #ifndef KEXEC_CONTROL_PAGE_SIZE 56 #error KEXEC_CONTROL_PAGE_SIZE not defined 57 #endif 58 59 #ifndef KEXEC_ARCH 60 #error KEXEC_ARCH not defined 61 #endif 62 63 #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT 64 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT 65 #endif 66 67 #ifndef KEXEC_CRASH_MEM_ALIGN 68 #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE 69 #endif 70 71 /* 72 * This structure is used to hold the arguments that are used when loading 73 * kernel binaries. 74 */ 75 76 typedef unsigned long kimage_entry_t; 77 78 struct 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 97 struct 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 106 struct 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 124 struct kimage; 125 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); 132 133 #ifdef CONFIG_KEXEC_SIG 134 typedef int (kexec_verify_sig_t)(const char *kernel_buf, 135 unsigned long kernel_len); 136 #endif 137 138 struct 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 147 extern const struct kexec_file_ops * const kexec_file_loaders[]; 148 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); 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 */ 173 struct 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 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, 188 bool get_value); 189 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name); 190 191 #ifndef arch_kexec_kernel_image_probe 192 static inline int 193 arch_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 200 static 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 208 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len); 209 #endif 210 #endif 211 212 extern int kexec_add_buffer(struct kexec_buf *kbuf); 213 int 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 */ 224 static 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 */ 240 static inline int 241 arch_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 */ 259 static inline int 260 arch_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 270 struct 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 281 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr, 282 struct kexec_elf_info *elf_info); 283 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); 288 289 void kexec_free_elf_info(struct kexec_elf_info *elf_info); 290 int kexec_elf_probe(const char *buf, unsigned long len); 291 #endif 292 struct 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, it is safe to update kexec segments that are 321 * excluded from SHA calculation. 322 */ 323 unsigned int hotplug_support:1; 324 #endif 325 326 #ifdef ARCH_HAS_KIMAGE_ARCH 327 struct kimage_arch arch; 328 #endif 329 330 #ifdef CONFIG_KEXEC_FILE 331 /* Additional fields for file based kexec syscall */ 332 void *kernel_buf; 333 unsigned long kernel_buf_len; 334 335 void *initrd_buf; 336 unsigned long initrd_buf_len; 337 338 char *cmdline_buf; 339 unsigned long cmdline_buf_len; 340 341 /* File operations provided by image loader */ 342 const struct kexec_file_ops *fops; 343 344 /* Image loader handling the kernel can store a pointer here */ 345 void *image_loader_data; 346 347 /* Information for loading purgatory */ 348 struct purgatory_info purgatory_info; 349 #endif 350 351 #ifdef CONFIG_CRASH_HOTPLUG 352 int hp_action; 353 int elfcorehdr_index; 354 bool elfcorehdr_updated; 355 #endif 356 357 #ifdef CONFIG_IMA_KEXEC 358 /* Virtual address of IMA measurement buffer for kexec syscall */ 359 void *ima_buffer; 360 361 phys_addr_t ima_buffer_addr; 362 size_t ima_buffer_size; 363 #endif 364 365 /* Core ELF header buffer */ 366 void *elf_headers; 367 unsigned long elf_headers_sz; 368 unsigned long elf_load_addr; 369 }; 370 371 /* kexec interface functions */ 372 extern void machine_kexec(struct kimage *image); 373 extern int machine_kexec_prepare(struct kimage *image); 374 extern void machine_kexec_cleanup(struct kimage *image); 375 extern int kernel_kexec(void); 376 extern struct page *kimage_alloc_control_pages(struct kimage *image, 377 unsigned int order); 378 379 #ifndef machine_kexec_post_load 380 static inline int machine_kexec_post_load(struct kimage *image) { return 0; } 381 #endif 382 383 extern struct kimage *kexec_image; 384 extern struct kimage *kexec_crash_image; 385 386 bool kexec_load_permitted(int kexec_image_type); 387 388 #ifndef kexec_flush_icache_page 389 #define kexec_flush_icache_page(page) 390 #endif 391 392 /* List of defined/legal kexec flags */ 393 #ifndef CONFIG_KEXEC_JUMP 394 #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_UPDATE_ELFCOREHDR | KEXEC_CRASH_HOTPLUG_SUPPORT) 395 #else 396 #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR | \ 397 KEXEC_CRASH_HOTPLUG_SUPPORT) 398 #endif 399 400 /* List of defined/legal kexec file flags */ 401 #define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \ 402 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG) 403 404 /* flag to track if kexec reboot is in progress */ 405 extern bool kexec_in_progress; 406 407 #ifndef page_to_boot_pfn 408 static inline unsigned long page_to_boot_pfn(struct page *page) 409 { 410 return page_to_pfn(page); 411 } 412 #endif 413 414 #ifndef boot_pfn_to_page 415 static inline struct page *boot_pfn_to_page(unsigned long boot_pfn) 416 { 417 return pfn_to_page(boot_pfn); 418 } 419 #endif 420 421 #ifndef phys_to_boot_phys 422 static inline unsigned long phys_to_boot_phys(phys_addr_t phys) 423 { 424 return phys; 425 } 426 #endif 427 428 #ifndef boot_phys_to_phys 429 static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys) 430 { 431 return boot_phys; 432 } 433 #endif 434 435 #ifndef crash_free_reserved_phys_range 436 static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end) 437 { 438 unsigned long addr; 439 440 for (addr = begin; addr < end; addr += PAGE_SIZE) 441 free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT)); 442 } 443 #endif 444 445 static inline unsigned long virt_to_boot_phys(void *addr) 446 { 447 return phys_to_boot_phys(__pa((unsigned long)addr)); 448 } 449 450 static inline void *boot_phys_to_virt(unsigned long entry) 451 { 452 return phys_to_virt(boot_phys_to_phys(entry)); 453 } 454 455 #ifndef arch_kexec_post_alloc_pages 456 static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; } 457 #endif 458 459 #ifndef arch_kexec_pre_free_pages 460 static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { } 461 #endif 462 463 extern bool kexec_file_dbg_print; 464 465 #define kexec_dprintk(fmt, arg...) \ 466 do { if (kexec_file_dbg_print) pr_info(fmt, ##arg); } while (0) 467 468 #else /* !CONFIG_KEXEC_CORE */ 469 struct pt_regs; 470 struct task_struct; 471 static inline void __crash_kexec(struct pt_regs *regs) { } 472 static inline void crash_kexec(struct pt_regs *regs) { } 473 static inline int kexec_should_crash(struct task_struct *p) { return 0; } 474 static inline int kexec_crash_loaded(void) { return 0; } 475 #define kexec_in_progress false 476 #endif /* CONFIG_KEXEC_CORE */ 477 478 #ifdef CONFIG_KEXEC_SIG 479 void set_kexec_sig_enforced(void); 480 #else 481 static inline void set_kexec_sig_enforced(void) {} 482 #endif 483 484 #endif /* !defined(__ASSEBMLY__) */ 485 486 #endif /* LINUX_KEXEC_H */ 487