kexec.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #ifdef CONFIG_KEXEC_CORE
  18. #include <linux/list.h>
  19. #include <linux/compat.h>
  20. #include <linux/ioport.h>
  21. #include <linux/module.h>
  22. #include <asm/kexec.h>
  23. /* Verify architecture specific macros are defined */
  24. #ifndef KEXEC_SOURCE_MEMORY_LIMIT
  25. #error KEXEC_SOURCE_MEMORY_LIMIT not defined
  26. #endif
  27. #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
  28. #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
  29. #endif
  30. #ifndef KEXEC_CONTROL_MEMORY_LIMIT
  31. #error KEXEC_CONTROL_MEMORY_LIMIT not defined
  32. #endif
  33. #ifndef KEXEC_CONTROL_MEMORY_GFP
  34. #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
  35. #endif
  36. #ifndef KEXEC_CONTROL_PAGE_SIZE
  37. #error KEXEC_CONTROL_PAGE_SIZE not defined
  38. #endif
  39. #ifndef KEXEC_ARCH
  40. #error KEXEC_ARCH not defined
  41. #endif
  42. #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
  43. #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
  44. #endif
  45. #ifndef KEXEC_CRASH_MEM_ALIGN
  46. #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
  47. #endif
  48. #define KEXEC_CORE_NOTE_NAME CRASH_CORE_NOTE_NAME
  49. /*
  50. * This structure is used to hold the arguments that are used when loading
  51. * kernel binaries.
  52. */
  53. typedef unsigned long kimage_entry_t;
  54. struct kexec_segment {
  55. /*
  56. * This pointer can point to user memory if kexec_load() system
  57. * call is used or will point to kernel memory if
  58. * kexec_file_load() system call is used.
  59. *
  60. * Use ->buf when expecting to deal with user memory and use ->kbuf
  61. * when expecting to deal with kernel memory.
  62. */
  63. union {
  64. void __user *buf;
  65. void *kbuf;
  66. };
  67. size_t bufsz;
  68. unsigned long mem;
  69. size_t memsz;
  70. };
  71. #ifdef CONFIG_COMPAT
  72. struct compat_kexec_segment {
  73. compat_uptr_t buf;
  74. compat_size_t bufsz;
  75. compat_ulong_t mem; /* User space sees this as a (void *) ... */
  76. compat_size_t memsz;
  77. };
  78. #endif
  79. #ifdef CONFIG_KEXEC_FILE
  80. struct purgatory_info {
  81. /*
  82. * Pointer to elf header at the beginning of kexec_purgatory.
  83. * Note: kexec_purgatory is read only
  84. */
  85. const Elf_Ehdr *ehdr;
  86. /*
  87. * Temporary, modifiable buffer for sechdrs used for relocation.
  88. * This memory can be freed post image load.
  89. */
  90. Elf_Shdr *sechdrs;
  91. /*
  92. * Temporary, modifiable buffer for stripped purgatory used for
  93. * relocation. This memory can be freed post image load.
  94. */
  95. void *purgatory_buf;
  96. };
  97. struct kimage;
  98. typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
  99. typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
  100. unsigned long kernel_len, char *initrd,
  101. unsigned long initrd_len, char *cmdline,
  102. unsigned long cmdline_len);
  103. typedef int (kexec_cleanup_t)(void *loader_data);
  104. #ifdef CONFIG_KEXEC_SIG
  105. typedef int (kexec_verify_sig_t)(const char *kernel_buf,
  106. unsigned long kernel_len);
  107. #endif
  108. struct kexec_file_ops {
  109. kexec_probe_t *probe;
  110. kexec_load_t *load;
  111. kexec_cleanup_t *cleanup;
  112. #ifdef CONFIG_KEXEC_SIG
  113. kexec_verify_sig_t *verify_sig;
  114. #endif
  115. };
  116. extern const struct kexec_file_ops * const kexec_file_loaders[];
  117. int kexec_image_probe_default(struct kimage *image, void *buf,
  118. unsigned long buf_len);
  119. int kexec_image_post_load_cleanup_default(struct kimage *image);
  120. /*
  121. * If kexec_buf.mem is set to this value, kexec_locate_mem_hole()
  122. * will try to allocate free memory. Arch may overwrite it.
  123. */
  124. #ifndef KEXEC_BUF_MEM_UNKNOWN
  125. #define KEXEC_BUF_MEM_UNKNOWN 0
  126. #endif
  127. /**
  128. * struct kexec_buf - parameters for finding a place for a buffer in memory
  129. * @image: kexec image in which memory to search.
  130. * @buffer: Contents which will be copied to the allocated memory.
  131. * @bufsz: Size of @buffer.
  132. * @mem: On return will have address of the buffer in memory.
  133. * @memsz: Size for the buffer in memory.
  134. * @buf_align: Minimum alignment needed.
  135. * @buf_min: The buffer can't be placed below this address.
  136. * @buf_max: The buffer can't be placed above this address.
  137. * @top_down: Allocate from top of memory.
  138. */
  139. struct kexec_buf {
  140. struct kimage *image;
  141. void *buffer;
  142. unsigned long bufsz;
  143. unsigned long mem;
  144. unsigned long memsz;
  145. unsigned long buf_align;
  146. unsigned long buf_min;
  147. unsigned long buf_max;
  148. bool top_down;
  149. };
  150. int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
  151. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  152. void *buf, unsigned int size,
  153. bool get_value);
  154. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
  155. /* Architectures may override the below functions */
  156. int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
  157. unsigned long buf_len);
  158. void *arch_kexec_kernel_image_load(struct kimage *image);
  159. int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
  160. Elf_Shdr *section,
  161. const Elf_Shdr *relsec,
  162. const Elf_Shdr *symtab);
  163. int arch_kexec_apply_relocations(struct purgatory_info *pi,
  164. Elf_Shdr *section,
  165. const Elf_Shdr *relsec,
  166. const Elf_Shdr *symtab);
  167. int arch_kimage_file_post_load_cleanup(struct kimage *image);
  168. #ifdef CONFIG_KEXEC_SIG
  169. int arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
  170. unsigned long buf_len);
  171. #endif
  172. int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf);
  173. extern int kexec_add_buffer(struct kexec_buf *kbuf);
  174. int kexec_locate_mem_hole(struct kexec_buf *kbuf);
  175. /* Alignment required for elf header segment */
  176. #define ELF_CORE_HEADER_ALIGN 4096
  177. struct crash_mem_range {
  178. u64 start, end;
  179. };
  180. struct crash_mem {
  181. unsigned int max_nr_ranges;
  182. unsigned int nr_ranges;
  183. struct crash_mem_range ranges[];
  184. };
  185. extern int crash_exclude_mem_range(struct crash_mem *mem,
  186. unsigned long long mstart,
  187. unsigned long long mend);
  188. extern int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
  189. void **addr, unsigned long *sz);
  190. #endif /* CONFIG_KEXEC_FILE */
  191. #ifdef CONFIG_KEXEC_ELF
  192. struct kexec_elf_info {
  193. /*
  194. * Where the ELF binary contents are kept.
  195. * Memory managed by the user of the struct.
  196. */
  197. const char *buffer;
  198. const struct elfhdr *ehdr;
  199. const struct elf_phdr *proghdrs;
  200. };
  201. int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
  202. struct kexec_elf_info *elf_info);
  203. int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
  204. struct kexec_elf_info *elf_info,
  205. struct kexec_buf *kbuf,
  206. unsigned long *lowest_load_addr);
  207. void kexec_free_elf_info(struct kexec_elf_info *elf_info);
  208. int kexec_elf_probe(const char *buf, unsigned long len);
  209. #endif
  210. struct kimage {
  211. kimage_entry_t head;
  212. kimage_entry_t *entry;
  213. kimage_entry_t *last_entry;
  214. unsigned long start;
  215. struct page *control_code_page;
  216. struct page *swap_page;
  217. void *vmcoreinfo_data_copy; /* locates in the crash memory */
  218. unsigned long nr_segments;
  219. struct kexec_segment segment[KEXEC_SEGMENT_MAX];
  220. struct list_head control_pages;
  221. struct list_head dest_pages;
  222. struct list_head unusable_pages;
  223. /* Address of next control page to allocate for crash kernels. */
  224. unsigned long control_page;
  225. /* Flags to indicate special processing */
  226. unsigned int type : 1;
  227. #define KEXEC_TYPE_DEFAULT 0
  228. #define KEXEC_TYPE_CRASH 1
  229. unsigned int preserve_context : 1;
  230. /* If set, we are using file mode kexec syscall */
  231. unsigned int file_mode:1;
  232. #ifdef ARCH_HAS_KIMAGE_ARCH
  233. struct kimage_arch arch;
  234. #endif
  235. #ifdef CONFIG_KEXEC_FILE
  236. /* Additional fields for file based kexec syscall */
  237. void *kernel_buf;
  238. unsigned long kernel_buf_len;
  239. void *initrd_buf;
  240. unsigned long initrd_buf_len;
  241. char *cmdline_buf;
  242. unsigned long cmdline_buf_len;
  243. /* File operations provided by image loader */
  244. const struct kexec_file_ops *fops;
  245. /* Image loader handling the kernel can store a pointer here */
  246. void *image_loader_data;
  247. /* Information for loading purgatory */
  248. struct purgatory_info purgatory_info;
  249. #endif
  250. #ifdef CONFIG_IMA_KEXEC
  251. /* Virtual address of IMA measurement buffer for kexec syscall */
  252. void *ima_buffer;
  253. #endif
  254. };
  255. /* kexec interface functions */
  256. extern void machine_kexec(struct kimage *image);
  257. extern int machine_kexec_prepare(struct kimage *image);
  258. extern void machine_kexec_cleanup(struct kimage *image);
  259. extern int kernel_kexec(void);
  260. extern struct page *kimage_alloc_control_pages(struct kimage *image,
  261. unsigned int order);
  262. extern void __crash_kexec(struct pt_regs *);
  263. extern void crash_kexec(struct pt_regs *);
  264. int kexec_should_crash(struct task_struct *);
  265. int kexec_crash_loaded(void);
  266. void crash_save_cpu(struct pt_regs *regs, int cpu);
  267. extern int kimage_crash_copy_vmcoreinfo(struct kimage *image);
  268. extern struct kimage *kexec_image;
  269. extern struct kimage *kexec_crash_image;
  270. extern int kexec_load_disabled;
  271. #ifndef kexec_flush_icache_page
  272. #define kexec_flush_icache_page(page)
  273. #endif
  274. /* List of defined/legal kexec flags */
  275. #ifndef CONFIG_KEXEC_JUMP
  276. #define KEXEC_FLAGS KEXEC_ON_CRASH
  277. #else
  278. #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT)
  279. #endif
  280. /* List of defined/legal kexec file flags */
  281. #define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
  282. KEXEC_FILE_NO_INITRAMFS)
  283. /* Location of a reserved region to hold the crash kernel.
  284. */
  285. extern struct resource crashk_res;
  286. extern struct resource crashk_low_res;
  287. extern note_buf_t __percpu *crash_notes;
  288. /* flag to track if kexec reboot is in progress */
  289. extern bool kexec_in_progress;
  290. int crash_shrink_memory(unsigned long new_size);
  291. size_t crash_get_memory_size(void);
  292. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
  293. void arch_kexec_protect_crashkres(void);
  294. void arch_kexec_unprotect_crashkres(void);
  295. #ifndef page_to_boot_pfn
  296. static inline unsigned long page_to_boot_pfn(struct page *page)
  297. {
  298. return page_to_pfn(page);
  299. }
  300. #endif
  301. #ifndef boot_pfn_to_page
  302. static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
  303. {
  304. return pfn_to_page(boot_pfn);
  305. }
  306. #endif
  307. #ifndef phys_to_boot_phys
  308. static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
  309. {
  310. return phys;
  311. }
  312. #endif
  313. #ifndef boot_phys_to_phys
  314. static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
  315. {
  316. return boot_phys;
  317. }
  318. #endif
  319. static inline unsigned long virt_to_boot_phys(void *addr)
  320. {
  321. return phys_to_boot_phys(__pa((unsigned long)addr));
  322. }
  323. static inline void *boot_phys_to_virt(unsigned long entry)
  324. {
  325. return phys_to_virt(boot_phys_to_phys(entry));
  326. }
  327. #ifndef arch_kexec_post_alloc_pages
  328. static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
  329. #endif
  330. #ifndef arch_kexec_pre_free_pages
  331. static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
  332. #endif
  333. #else /* !CONFIG_KEXEC_CORE */
  334. struct pt_regs;
  335. struct task_struct;
  336. static inline void __crash_kexec(struct pt_regs *regs) { }
  337. static inline void crash_kexec(struct pt_regs *regs) { }
  338. static inline int kexec_should_crash(struct task_struct *p) { return 0; }
  339. static inline int kexec_crash_loaded(void) { return 0; }
  340. #define kexec_in_progress false
  341. #endif /* CONFIG_KEXEC_CORE */
  342. #endif /* !defined(__ASSEBMLY__) */
  343. #endif /* LINUX_KEXEC_H */