at v6.15 13 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/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 26extern 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 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#ifndef arch_check_excluded_range 207static inline int arch_check_excluded_range(struct kimage *image, 208 unsigned long start, 209 unsigned long end) 210{ 211 return 0; 212} 213#endif 214 215#ifdef CONFIG_KEXEC_SIG 216#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION 217int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len); 218#endif 219#endif 220 221extern int kexec_add_buffer(struct kexec_buf *kbuf); 222int kexec_locate_mem_hole(struct kexec_buf *kbuf); 223 224#ifndef arch_kexec_locate_mem_hole 225/** 226 * arch_kexec_locate_mem_hole - Find free memory to place the segments. 227 * @kbuf: Parameters for the memory search. 228 * 229 * On success, kbuf->mem will have the start address of the memory region found. 230 * 231 * Return: 0 on success, negative errno on error. 232 */ 233static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf) 234{ 235 return kexec_locate_mem_hole(kbuf); 236} 237#endif 238 239#ifndef arch_kexec_apply_relocations_add 240/* 241 * arch_kexec_apply_relocations_add - apply relocations of type RELA 242 * @pi: Purgatory to be relocated. 243 * @section: Section relocations applying to. 244 * @relsec: Section containing RELAs. 245 * @symtab: Corresponding symtab. 246 * 247 * Return: 0 on success, negative errno on error. 248 */ 249static inline int 250arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section, 251 const Elf_Shdr *relsec, const Elf_Shdr *symtab) 252{ 253 pr_err("RELA relocation unsupported.\n"); 254 return -ENOEXEC; 255} 256#endif 257 258#ifndef arch_kexec_apply_relocations 259/* 260 * arch_kexec_apply_relocations - apply relocations of type REL 261 * @pi: Purgatory to be relocated. 262 * @section: Section relocations applying to. 263 * @relsec: Section containing RELs. 264 * @symtab: Corresponding symtab. 265 * 266 * Return: 0 on success, negative errno on error. 267 */ 268static inline int 269arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section, 270 const Elf_Shdr *relsec, const Elf_Shdr *symtab) 271{ 272 pr_err("REL relocation unsupported.\n"); 273 return -ENOEXEC; 274} 275#endif 276#endif /* CONFIG_KEXEC_FILE */ 277 278#ifdef CONFIG_KEXEC_ELF 279struct kexec_elf_info { 280 /* 281 * Where the ELF binary contents are kept. 282 * Memory managed by the user of the struct. 283 */ 284 const char *buffer; 285 286 const struct elfhdr *ehdr; 287 const struct elf_phdr *proghdrs; 288}; 289 290int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr, 291 struct kexec_elf_info *elf_info); 292 293int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr, 294 struct kexec_elf_info *elf_info, 295 struct kexec_buf *kbuf, 296 unsigned long *lowest_load_addr); 297 298void kexec_free_elf_info(struct kexec_elf_info *elf_info); 299int kexec_elf_probe(const char *buf, unsigned long len); 300#endif 301struct kimage { 302 kimage_entry_t head; 303 kimage_entry_t *entry; 304 kimage_entry_t *last_entry; 305 306 unsigned long start; 307 struct page *control_code_page; 308 struct page *swap_page; 309 void *vmcoreinfo_data_copy; /* locates in the crash memory */ 310 311 unsigned long nr_segments; 312 struct kexec_segment segment[KEXEC_SEGMENT_MAX]; 313 314 struct list_head control_pages; 315 struct list_head dest_pages; 316 struct list_head unusable_pages; 317 318 /* Address of next control page to allocate for crash kernels. */ 319 unsigned long control_page; 320 321 /* Flags to indicate special processing */ 322 unsigned int type : 1; 323#define KEXEC_TYPE_DEFAULT 0 324#define KEXEC_TYPE_CRASH 1 325 unsigned int preserve_context : 1; 326 /* If set, we are using file mode kexec syscall */ 327 unsigned int file_mode:1; 328#ifdef CONFIG_CRASH_HOTPLUG 329 /* If set, it is safe to update kexec segments that are 330 * excluded from SHA calculation. 331 */ 332 unsigned int hotplug_support:1; 333#endif 334 335#ifdef ARCH_HAS_KIMAGE_ARCH 336 struct kimage_arch arch; 337#endif 338 339#ifdef CONFIG_KEXEC_FILE 340 /* Additional fields for file based kexec syscall */ 341 void *kernel_buf; 342 unsigned long kernel_buf_len; 343 344 void *initrd_buf; 345 unsigned long initrd_buf_len; 346 347 char *cmdline_buf; 348 unsigned long cmdline_buf_len; 349 350 /* File operations provided by image loader */ 351 const struct kexec_file_ops *fops; 352 353 /* Image loader handling the kernel can store a pointer here */ 354 void *image_loader_data; 355 356 /* Information for loading purgatory */ 357 struct purgatory_info purgatory_info; 358#endif 359 360#ifdef CONFIG_CRASH_HOTPLUG 361 int hp_action; 362 int elfcorehdr_index; 363 bool elfcorehdr_updated; 364#endif 365 366#ifdef CONFIG_IMA_KEXEC 367 /* Virtual address of IMA measurement buffer for kexec syscall */ 368 void *ima_buffer; 369 370 phys_addr_t ima_buffer_addr; 371 size_t ima_buffer_size; 372#endif 373 374 /* Core ELF header buffer */ 375 void *elf_headers; 376 unsigned long elf_headers_sz; 377 unsigned long elf_load_addr; 378}; 379 380/* kexec interface functions */ 381extern void machine_kexec(struct kimage *image); 382extern int machine_kexec_prepare(struct kimage *image); 383extern void machine_kexec_cleanup(struct kimage *image); 384extern int kernel_kexec(void); 385extern struct page *kimage_alloc_control_pages(struct kimage *image, 386 unsigned int order); 387 388#ifndef machine_kexec_post_load 389static inline int machine_kexec_post_load(struct kimage *image) { return 0; } 390#endif 391 392extern struct kimage *kexec_image; 393extern struct kimage *kexec_crash_image; 394 395bool 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 | KEXEC_CRASH_HOTPLUG_SUPPORT) 404#else 405#define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR | \ 406 KEXEC_CRASH_HOTPLUG_SUPPORT) 407#endif 408 409/* List of defined/legal kexec file flags */ 410#define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \ 411 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG) 412 413/* flag to track if kexec reboot is in progress */ 414extern bool kexec_in_progress; 415 416#ifndef page_to_boot_pfn 417static inline unsigned long page_to_boot_pfn(struct page *page) 418{ 419 return page_to_pfn(page); 420} 421#endif 422 423#ifndef boot_pfn_to_page 424static inline struct page *boot_pfn_to_page(unsigned long boot_pfn) 425{ 426 return pfn_to_page(boot_pfn); 427} 428#endif 429 430#ifndef phys_to_boot_phys 431static inline unsigned long phys_to_boot_phys(phys_addr_t phys) 432{ 433 return phys; 434} 435#endif 436 437#ifndef boot_phys_to_phys 438static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys) 439{ 440 return boot_phys; 441} 442#endif 443 444#ifndef crash_free_reserved_phys_range 445static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end) 446{ 447 unsigned long addr; 448 449 for (addr = begin; addr < end; addr += PAGE_SIZE) 450 free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT)); 451} 452#endif 453 454static inline unsigned long virt_to_boot_phys(void *addr) 455{ 456 return phys_to_boot_phys(__pa((unsigned long)addr)); 457} 458 459static inline void *boot_phys_to_virt(unsigned long entry) 460{ 461 return phys_to_virt(boot_phys_to_phys(entry)); 462} 463 464#ifndef arch_kexec_post_alloc_pages 465static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; } 466#endif 467 468#ifndef arch_kexec_pre_free_pages 469static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { } 470#endif 471 472extern bool kexec_file_dbg_print; 473 474#define kexec_dprintk(fmt, arg...) \ 475 do { if (kexec_file_dbg_print) pr_info(fmt, ##arg); } while (0) 476 477#else /* !CONFIG_KEXEC_CORE */ 478struct pt_regs; 479struct task_struct; 480static inline void __crash_kexec(struct pt_regs *regs) { } 481static inline void crash_kexec(struct pt_regs *regs) { } 482static inline int kexec_should_crash(struct task_struct *p) { return 0; } 483static inline int kexec_crash_loaded(void) { return 0; } 484#define kexec_in_progress false 485#endif /* CONFIG_KEXEC_CORE */ 486 487#ifdef CONFIG_KEXEC_SIG 488void set_kexec_sig_enforced(void); 489#else 490static inline void set_kexec_sig_enforced(void) {} 491#endif 492 493#endif /* !defined(__ASSEBMLY__) */ 494 495#endif /* LINUX_KEXEC_H */