kexec.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_VERIFY_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_VERIFY_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. /**
  120. * struct kexec_buf - parameters for finding a place for a buffer in memory
  121. * @image: kexec image in which memory to search.
  122. * @buffer: Contents which will be copied to the allocated memory.
  123. * @bufsz: Size of @buffer.
  124. * @mem: On return will have address of the buffer in memory.
  125. * @memsz: Size for the buffer in memory.
  126. * @buf_align: Minimum alignment needed.
  127. * @buf_min: The buffer can't be placed below this address.
  128. * @buf_max: The buffer can't be placed above this address.
  129. * @top_down: Allocate from top of memory.
  130. */
  131. struct kexec_buf {
  132. struct kimage *image;
  133. void *buffer;
  134. unsigned long bufsz;
  135. unsigned long mem;
  136. unsigned long memsz;
  137. unsigned long buf_align;
  138. unsigned long buf_min;
  139. unsigned long buf_max;
  140. bool top_down;
  141. };
  142. int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
  143. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  144. void *buf, unsigned int size,
  145. bool get_value);
  146. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
  147. int __weak arch_kexec_apply_relocations_add(struct purgatory_info *pi,
  148. Elf_Shdr *section,
  149. const Elf_Shdr *relsec,
  150. const Elf_Shdr *symtab);
  151. int __weak arch_kexec_apply_relocations(struct purgatory_info *pi,
  152. Elf_Shdr *section,
  153. const Elf_Shdr *relsec,
  154. const Elf_Shdr *symtab);
  155. int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
  156. int (*func)(struct resource *, void *));
  157. extern int kexec_add_buffer(struct kexec_buf *kbuf);
  158. int kexec_locate_mem_hole(struct kexec_buf *kbuf);
  159. /* Alignment required for elf header segment */
  160. #define ELF_CORE_HEADER_ALIGN 4096
  161. struct crash_mem_range {
  162. u64 start, end;
  163. };
  164. struct crash_mem {
  165. unsigned int max_nr_ranges;
  166. unsigned int nr_ranges;
  167. struct crash_mem_range ranges[0];
  168. };
  169. extern int crash_exclude_mem_range(struct crash_mem *mem,
  170. unsigned long long mstart,
  171. unsigned long long mend);
  172. extern int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
  173. void **addr, unsigned long *sz);
  174. #endif /* CONFIG_KEXEC_FILE */
  175. struct kimage {
  176. kimage_entry_t head;
  177. kimage_entry_t *entry;
  178. kimage_entry_t *last_entry;
  179. unsigned long start;
  180. struct page *control_code_page;
  181. struct page *swap_page;
  182. void *vmcoreinfo_data_copy; /* locates in the crash memory */
  183. unsigned long nr_segments;
  184. struct kexec_segment segment[KEXEC_SEGMENT_MAX];
  185. struct list_head control_pages;
  186. struct list_head dest_pages;
  187. struct list_head unusable_pages;
  188. /* Address of next control page to allocate for crash kernels. */
  189. unsigned long control_page;
  190. /* Flags to indicate special processing */
  191. unsigned int type : 1;
  192. #define KEXEC_TYPE_DEFAULT 0
  193. #define KEXEC_TYPE_CRASH 1
  194. unsigned int preserve_context : 1;
  195. /* If set, we are using file mode kexec syscall */
  196. unsigned int file_mode:1;
  197. #ifdef ARCH_HAS_KIMAGE_ARCH
  198. struct kimage_arch arch;
  199. #endif
  200. #ifdef CONFIG_KEXEC_FILE
  201. /* Additional fields for file based kexec syscall */
  202. void *kernel_buf;
  203. unsigned long kernel_buf_len;
  204. void *initrd_buf;
  205. unsigned long initrd_buf_len;
  206. char *cmdline_buf;
  207. unsigned long cmdline_buf_len;
  208. /* File operations provided by image loader */
  209. const struct kexec_file_ops *fops;
  210. /* Image loader handling the kernel can store a pointer here */
  211. void *image_loader_data;
  212. /* Information for loading purgatory */
  213. struct purgatory_info purgatory_info;
  214. #endif
  215. };
  216. /* kexec interface functions */
  217. extern void machine_kexec(struct kimage *image);
  218. extern int machine_kexec_prepare(struct kimage *image);
  219. extern void machine_kexec_cleanup(struct kimage *image);
  220. extern int kernel_kexec(void);
  221. extern struct page *kimage_alloc_control_pages(struct kimage *image,
  222. unsigned int order);
  223. extern void __crash_kexec(struct pt_regs *);
  224. extern void crash_kexec(struct pt_regs *);
  225. int kexec_should_crash(struct task_struct *);
  226. int kexec_crash_loaded(void);
  227. void crash_save_cpu(struct pt_regs *regs, int cpu);
  228. extern int kimage_crash_copy_vmcoreinfo(struct kimage *image);
  229. extern struct kimage *kexec_image;
  230. extern struct kimage *kexec_crash_image;
  231. extern int kexec_load_disabled;
  232. #ifndef kexec_flush_icache_page
  233. #define kexec_flush_icache_page(page)
  234. #endif
  235. /* List of defined/legal kexec flags */
  236. #ifndef CONFIG_KEXEC_JUMP
  237. #define KEXEC_FLAGS KEXEC_ON_CRASH
  238. #else
  239. #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT)
  240. #endif
  241. /* List of defined/legal kexec file flags */
  242. #define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
  243. KEXEC_FILE_NO_INITRAMFS)
  244. /* Location of a reserved region to hold the crash kernel.
  245. */
  246. extern struct resource crashk_res;
  247. extern struct resource crashk_low_res;
  248. extern note_buf_t __percpu *crash_notes;
  249. /* flag to track if kexec reboot is in progress */
  250. extern bool kexec_in_progress;
  251. int crash_shrink_memory(unsigned long new_size);
  252. size_t crash_get_memory_size(void);
  253. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
  254. void arch_kexec_protect_crashkres(void);
  255. void arch_kexec_unprotect_crashkres(void);
  256. #ifndef page_to_boot_pfn
  257. static inline unsigned long page_to_boot_pfn(struct page *page)
  258. {
  259. return page_to_pfn(page);
  260. }
  261. #endif
  262. #ifndef boot_pfn_to_page
  263. static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
  264. {
  265. return pfn_to_page(boot_pfn);
  266. }
  267. #endif
  268. #ifndef phys_to_boot_phys
  269. static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
  270. {
  271. return phys;
  272. }
  273. #endif
  274. #ifndef boot_phys_to_phys
  275. static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
  276. {
  277. return boot_phys;
  278. }
  279. #endif
  280. static inline unsigned long virt_to_boot_phys(void *addr)
  281. {
  282. return phys_to_boot_phys(__pa((unsigned long)addr));
  283. }
  284. static inline void *boot_phys_to_virt(unsigned long entry)
  285. {
  286. return phys_to_virt(boot_phys_to_phys(entry));
  287. }
  288. #ifndef arch_kexec_post_alloc_pages
  289. static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
  290. #endif
  291. #ifndef arch_kexec_pre_free_pages
  292. static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
  293. #endif
  294. #else /* !CONFIG_KEXEC_CORE */
  295. struct pt_regs;
  296. struct task_struct;
  297. static inline void __crash_kexec(struct pt_regs *regs) { }
  298. static inline void crash_kexec(struct pt_regs *regs) { }
  299. static inline int kexec_should_crash(struct task_struct *p) { return 0; }
  300. static inline int kexec_crash_loaded(void) { return 0; }
  301. #define kexec_in_progress false
  302. #endif /* CONFIG_KEXEC_CORE */
  303. #endif /* !defined(__ASSEBMLY__) */
  304. #endif /* LINUX_KEXEC_H */