kexec.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LINUX_KEXEC_H
  3. #define LINUX_KEXEC_H
  4. #define IND_DESTINATION_BIT 0
  5. #define IND_INDIRECTION_BIT 1
  6. #define IND_DONE_BIT 2
  7. #define IND_SOURCE_BIT 3
  8. #define IND_DESTINATION (1 << IND_DESTINATION_BIT)
  9. #define IND_INDIRECTION (1 << IND_INDIRECTION_BIT)
  10. #define IND_DONE (1 << IND_DONE_BIT)
  11. #define IND_SOURCE (1 << IND_SOURCE_BIT)
  12. #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
  13. #if !defined(__ASSEMBLY__)
  14. #include <linux/crash_core.h>
  15. #include <asm/io.h>
  16. #include <uapi/linux/kexec.h>
  17. #include <linux/verification.h>
  18. /* Location of a reserved region to hold the crash kernel.
  19. */
  20. extern struct resource crashk_res;
  21. extern struct resource crashk_low_res;
  22. extern note_buf_t __percpu *crash_notes;
  23. #ifdef CONFIG_KEXEC_CORE
  24. #include <linux/list.h>
  25. #include <linux/compat.h>
  26. #include <linux/ioport.h>
  27. #include <linux/module.h>
  28. #include <asm/kexec.h>
  29. /* Verify architecture specific macros are defined */
  30. #ifndef KEXEC_SOURCE_MEMORY_LIMIT
  31. #error KEXEC_SOURCE_MEMORY_LIMIT not defined
  32. #endif
  33. #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
  34. #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
  35. #endif
  36. #ifndef KEXEC_CONTROL_MEMORY_LIMIT
  37. #error KEXEC_CONTROL_MEMORY_LIMIT not defined
  38. #endif
  39. #ifndef KEXEC_CONTROL_MEMORY_GFP
  40. #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
  41. #endif
  42. #ifndef KEXEC_CONTROL_PAGE_SIZE
  43. #error KEXEC_CONTROL_PAGE_SIZE not defined
  44. #endif
  45. #ifndef KEXEC_ARCH
  46. #error KEXEC_ARCH not defined
  47. #endif
  48. #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
  49. #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
  50. #endif
  51. #ifndef KEXEC_CRASH_MEM_ALIGN
  52. #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
  53. #endif
  54. #define KEXEC_CORE_NOTE_NAME CRASH_CORE_NOTE_NAME
  55. /*
  56. * This structure is used to hold the arguments that are used when loading
  57. * kernel binaries.
  58. */
  59. typedef unsigned long kimage_entry_t;
  60. struct kexec_segment {
  61. /*
  62. * This pointer can point to user memory if kexec_load() system
  63. * call is used or will point to kernel memory if
  64. * kexec_file_load() system call is used.
  65. *
  66. * Use ->buf when expecting to deal with user memory and use ->kbuf
  67. * when expecting to deal with kernel memory.
  68. */
  69. union {
  70. void __user *buf;
  71. void *kbuf;
  72. };
  73. size_t bufsz;
  74. unsigned long mem;
  75. size_t memsz;
  76. };
  77. #ifdef CONFIG_COMPAT
  78. struct compat_kexec_segment {
  79. compat_uptr_t buf;
  80. compat_size_t bufsz;
  81. compat_ulong_t mem; /* User space sees this as a (void *) ... */
  82. compat_size_t memsz;
  83. };
  84. #endif
  85. #ifdef CONFIG_KEXEC_FILE
  86. struct purgatory_info {
  87. /*
  88. * Pointer to elf header at the beginning of kexec_purgatory.
  89. * Note: kexec_purgatory is read only
  90. */
  91. const Elf_Ehdr *ehdr;
  92. /*
  93. * Temporary, modifiable buffer for sechdrs used for relocation.
  94. * This memory can be freed post image load.
  95. */
  96. Elf_Shdr *sechdrs;
  97. /*
  98. * Temporary, modifiable buffer for stripped purgatory used for
  99. * relocation. This memory can be freed post image load.
  100. */
  101. void *purgatory_buf;
  102. };
  103. struct kimage;
  104. typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
  105. typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
  106. unsigned long kernel_len, char *initrd,
  107. unsigned long initrd_len, char *cmdline,
  108. unsigned long cmdline_len);
  109. typedef int (kexec_cleanup_t)(void *loader_data);
  110. #ifdef CONFIG_KEXEC_SIG
  111. typedef int (kexec_verify_sig_t)(const char *kernel_buf,
  112. unsigned long kernel_len);
  113. #endif
  114. struct kexec_file_ops {
  115. kexec_probe_t *probe;
  116. kexec_load_t *load;
  117. kexec_cleanup_t *cleanup;
  118. #ifdef CONFIG_KEXEC_SIG
  119. kexec_verify_sig_t *verify_sig;
  120. #endif
  121. };
  122. extern const struct kexec_file_ops * const kexec_file_loaders[];
  123. int kexec_image_probe_default(struct kimage *image, void *buf,
  124. unsigned long buf_len);
  125. int kexec_image_post_load_cleanup_default(struct kimage *image);
  126. /*
  127. * If kexec_buf.mem is set to this value, kexec_locate_mem_hole()
  128. * will try to allocate free memory. Arch may overwrite it.
  129. */
  130. #ifndef KEXEC_BUF_MEM_UNKNOWN
  131. #define KEXEC_BUF_MEM_UNKNOWN 0
  132. #endif
  133. /**
  134. * struct kexec_buf - parameters for finding a place for a buffer in memory
  135. * @image: kexec image in which memory to search.
  136. * @buffer: Contents which will be copied to the allocated memory.
  137. * @bufsz: Size of @buffer.
  138. * @mem: On return will have address of the buffer in memory.
  139. * @memsz: Size for the buffer in memory.
  140. * @buf_align: Minimum alignment needed.
  141. * @buf_min: The buffer can't be placed below this address.
  142. * @buf_max: The buffer can't be placed above this address.
  143. * @top_down: Allocate from top of memory.
  144. */
  145. struct kexec_buf {
  146. struct kimage *image;
  147. void *buffer;
  148. unsigned long bufsz;
  149. unsigned long mem;
  150. unsigned long memsz;
  151. unsigned long buf_align;
  152. unsigned long buf_min;
  153. unsigned long buf_max;
  154. bool top_down;
  155. };
  156. int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
  157. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  158. void *buf, unsigned int size,
  159. bool get_value);
  160. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
  161. void *kexec_image_load_default(struct kimage *image);
  162. #ifndef arch_kexec_kernel_image_probe
  163. static inline int
  164. arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len)
  165. {
  166. return kexec_image_probe_default(image, buf, buf_len);
  167. }
  168. #endif
  169. #ifndef arch_kimage_file_post_load_cleanup
  170. static inline int arch_kimage_file_post_load_cleanup(struct kimage *image)
  171. {
  172. return kexec_image_post_load_cleanup_default(image);
  173. }
  174. #endif
  175. #ifndef arch_kexec_kernel_image_load
  176. static inline void *arch_kexec_kernel_image_load(struct kimage *image)
  177. {
  178. return kexec_image_load_default(image);
  179. }
  180. #endif
  181. #ifdef CONFIG_KEXEC_SIG
  182. #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
  183. int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
  184. #endif
  185. #endif
  186. extern int kexec_add_buffer(struct kexec_buf *kbuf);
  187. int kexec_locate_mem_hole(struct kexec_buf *kbuf);
  188. #ifndef arch_kexec_locate_mem_hole
  189. /**
  190. * arch_kexec_locate_mem_hole - Find free memory to place the segments.
  191. * @kbuf: Parameters for the memory search.
  192. *
  193. * On success, kbuf->mem will have the start address of the memory region found.
  194. *
  195. * Return: 0 on success, negative errno on error.
  196. */
  197. static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
  198. {
  199. return kexec_locate_mem_hole(kbuf);
  200. }
  201. #endif
  202. /* Alignment required for elf header segment */
  203. #define ELF_CORE_HEADER_ALIGN 4096
  204. struct crash_mem_range {
  205. u64 start, end;
  206. };
  207. struct crash_mem {
  208. unsigned int max_nr_ranges;
  209. unsigned int nr_ranges;
  210. struct crash_mem_range ranges[];
  211. };
  212. extern int crash_exclude_mem_range(struct crash_mem *mem,
  213. unsigned long long mstart,
  214. unsigned long long mend);
  215. extern int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
  216. void **addr, unsigned long *sz);
  217. #ifndef arch_kexec_apply_relocations_add
  218. /*
  219. * arch_kexec_apply_relocations_add - apply relocations of type RELA
  220. * @pi: Purgatory to be relocated.
  221. * @section: Section relocations applying to.
  222. * @relsec: Section containing RELAs.
  223. * @symtab: Corresponding symtab.
  224. *
  225. * Return: 0 on success, negative errno on error.
  226. */
  227. static inline int
  228. arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
  229. const Elf_Shdr *relsec, const Elf_Shdr *symtab)
  230. {
  231. pr_err("RELA relocation unsupported.\n");
  232. return -ENOEXEC;
  233. }
  234. #endif
  235. #ifndef arch_kexec_apply_relocations
  236. /*
  237. * arch_kexec_apply_relocations - apply relocations of type REL
  238. * @pi: Purgatory to be relocated.
  239. * @section: Section relocations applying to.
  240. * @relsec: Section containing RELs.
  241. * @symtab: Corresponding symtab.
  242. *
  243. * Return: 0 on success, negative errno on error.
  244. */
  245. static inline int
  246. arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
  247. const Elf_Shdr *relsec, const Elf_Shdr *symtab)
  248. {
  249. pr_err("REL relocation unsupported.\n");
  250. return -ENOEXEC;
  251. }
  252. #endif
  253. #endif /* CONFIG_KEXEC_FILE */
  254. #ifdef CONFIG_KEXEC_ELF
  255. struct kexec_elf_info {
  256. /*
  257. * Where the ELF binary contents are kept.
  258. * Memory managed by the user of the struct.
  259. */
  260. const char *buffer;
  261. const struct elfhdr *ehdr;
  262. const struct elf_phdr *proghdrs;
  263. };
  264. int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
  265. struct kexec_elf_info *elf_info);
  266. int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
  267. struct kexec_elf_info *elf_info,
  268. struct kexec_buf *kbuf,
  269. unsigned long *lowest_load_addr);
  270. void kexec_free_elf_info(struct kexec_elf_info *elf_info);
  271. int kexec_elf_probe(const char *buf, unsigned long len);
  272. #endif
  273. struct kimage {
  274. kimage_entry_t head;
  275. kimage_entry_t *entry;
  276. kimage_entry_t *last_entry;
  277. unsigned long start;
  278. struct page *control_code_page;
  279. struct page *swap_page;
  280. void *vmcoreinfo_data_copy; /* locates in the crash memory */
  281. unsigned long nr_segments;
  282. struct kexec_segment segment[KEXEC_SEGMENT_MAX];
  283. struct list_head control_pages;
  284. struct list_head dest_pages;
  285. struct list_head unusable_pages;
  286. /* Address of next control page to allocate for crash kernels. */
  287. unsigned long control_page;
  288. /* Flags to indicate special processing */
  289. unsigned int type : 1;
  290. #define KEXEC_TYPE_DEFAULT 0
  291. #define KEXEC_TYPE_CRASH 1
  292. unsigned int preserve_context : 1;
  293. /* If set, we are using file mode kexec syscall */
  294. unsigned int file_mode:1;
  295. #ifdef ARCH_HAS_KIMAGE_ARCH
  296. struct kimage_arch arch;
  297. #endif
  298. #ifdef CONFIG_KEXEC_FILE
  299. /* Additional fields for file based kexec syscall */
  300. void *kernel_buf;
  301. unsigned long kernel_buf_len;
  302. void *initrd_buf;
  303. unsigned long initrd_buf_len;
  304. char *cmdline_buf;
  305. unsigned long cmdline_buf_len;
  306. /* File operations provided by image loader */
  307. const struct kexec_file_ops *fops;
  308. /* Image loader handling the kernel can store a pointer here */
  309. void *image_loader_data;
  310. /* Information for loading purgatory */
  311. struct purgatory_info purgatory_info;
  312. #endif
  313. #ifdef CONFIG_IMA_KEXEC
  314. /* Virtual address of IMA measurement buffer for kexec syscall */
  315. void *ima_buffer;
  316. phys_addr_t ima_buffer_addr;
  317. size_t ima_buffer_size;
  318. #endif
  319. /* Core ELF header buffer */
  320. void *elf_headers;
  321. unsigned long elf_headers_sz;
  322. unsigned long elf_load_addr;
  323. };
  324. /* kexec interface functions */
  325. extern void machine_kexec(struct kimage *image);
  326. extern int machine_kexec_prepare(struct kimage *image);
  327. extern void machine_kexec_cleanup(struct kimage *image);
  328. extern int kernel_kexec(void);
  329. extern struct page *kimage_alloc_control_pages(struct kimage *image,
  330. unsigned int order);
  331. #ifndef machine_kexec_post_load
  332. static inline int machine_kexec_post_load(struct kimage *image) { return 0; }
  333. #endif
  334. extern void __crash_kexec(struct pt_regs *);
  335. extern void crash_kexec(struct pt_regs *);
  336. int kexec_should_crash(struct task_struct *);
  337. int kexec_crash_loaded(void);
  338. void crash_save_cpu(struct pt_regs *regs, int cpu);
  339. extern int kimage_crash_copy_vmcoreinfo(struct kimage *image);
  340. extern struct kimage *kexec_image;
  341. extern struct kimage *kexec_crash_image;
  342. extern int kexec_load_disabled;
  343. #ifndef kexec_flush_icache_page
  344. #define kexec_flush_icache_page(page)
  345. #endif
  346. /* List of defined/legal kexec flags */
  347. #ifndef CONFIG_KEXEC_JUMP
  348. #define KEXEC_FLAGS KEXEC_ON_CRASH
  349. #else
  350. #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT)
  351. #endif
  352. /* List of defined/legal kexec file flags */
  353. #define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
  354. KEXEC_FILE_NO_INITRAMFS)
  355. /* flag to track if kexec reboot is in progress */
  356. extern bool kexec_in_progress;
  357. int crash_shrink_memory(unsigned long new_size);
  358. ssize_t crash_get_memory_size(void);
  359. #ifndef arch_kexec_protect_crashkres
  360. /*
  361. * Protection mechanism for crashkernel reserved memory after
  362. * the kdump kernel is loaded.
  363. *
  364. * Provide an empty default implementation here -- architecture
  365. * code may override this
  366. */
  367. static inline void arch_kexec_protect_crashkres(void) { }
  368. #endif
  369. #ifndef arch_kexec_unprotect_crashkres
  370. static inline void arch_kexec_unprotect_crashkres(void) { }
  371. #endif
  372. #ifndef page_to_boot_pfn
  373. static inline unsigned long page_to_boot_pfn(struct page *page)
  374. {
  375. return page_to_pfn(page);
  376. }
  377. #endif
  378. #ifndef boot_pfn_to_page
  379. static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
  380. {
  381. return pfn_to_page(boot_pfn);
  382. }
  383. #endif
  384. #ifndef phys_to_boot_phys
  385. static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
  386. {
  387. return phys;
  388. }
  389. #endif
  390. #ifndef boot_phys_to_phys
  391. static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
  392. {
  393. return boot_phys;
  394. }
  395. #endif
  396. #ifndef crash_free_reserved_phys_range
  397. static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  398. {
  399. unsigned long addr;
  400. for (addr = begin; addr < end; addr += PAGE_SIZE)
  401. free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
  402. }
  403. #endif
  404. static inline unsigned long virt_to_boot_phys(void *addr)
  405. {
  406. return phys_to_boot_phys(__pa((unsigned long)addr));
  407. }
  408. static inline void *boot_phys_to_virt(unsigned long entry)
  409. {
  410. return phys_to_virt(boot_phys_to_phys(entry));
  411. }
  412. #ifndef arch_kexec_post_alloc_pages
  413. static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
  414. #endif
  415. #ifndef arch_kexec_pre_free_pages
  416. static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
  417. #endif
  418. #else /* !CONFIG_KEXEC_CORE */
  419. struct pt_regs;
  420. struct task_struct;
  421. static inline void __crash_kexec(struct pt_regs *regs) { }
  422. static inline void crash_kexec(struct pt_regs *regs) { }
  423. static inline int kexec_should_crash(struct task_struct *p) { return 0; }
  424. static inline int kexec_crash_loaded(void) { return 0; }
  425. #define kexec_in_progress false
  426. #endif /* CONFIG_KEXEC_CORE */
  427. #ifdef CONFIG_KEXEC_SIG
  428. void set_kexec_sig_enforced(void);
  429. #else
  430. static inline void set_kexec_sig_enforced(void) {}
  431. #endif
  432. #endif /* !defined(__ASSEBMLY__) */
  433. #endif /* LINUX_KEXEC_H */