kexec.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #ifndef LINUX_KEXEC_H
  2. #define LINUX_KEXEC_H
  3. #define IND_DESTINATION_BIT 0
  4. #define IND_INDIRECTION_BIT 1
  5. #define IND_DONE_BIT 2
  6. #define IND_SOURCE_BIT 3
  7. #define IND_DESTINATION (1 << IND_DESTINATION_BIT)
  8. #define IND_INDIRECTION (1 << IND_INDIRECTION_BIT)
  9. #define IND_DONE (1 << IND_DONE_BIT)
  10. #define IND_SOURCE (1 << IND_SOURCE_BIT)
  11. #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
  12. #if !defined(__ASSEMBLY__)
  13. #include <asm/io.h>
  14. #include <uapi/linux/kexec.h>
  15. #ifdef CONFIG_KEXEC_CORE
  16. #include <linux/list.h>
  17. #include <linux/linkage.h>
  18. #include <linux/compat.h>
  19. #include <linux/ioport.h>
  20. #include <linux/elfcore.h>
  21. #include <linux/elf.h>
  22. #include <linux/module.h>
  23. #include <asm/kexec.h>
  24. /* Verify architecture specific macros are defined */
  25. #ifndef KEXEC_SOURCE_MEMORY_LIMIT
  26. #error KEXEC_SOURCE_MEMORY_LIMIT not defined
  27. #endif
  28. #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
  29. #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
  30. #endif
  31. #ifndef KEXEC_CONTROL_MEMORY_LIMIT
  32. #error KEXEC_CONTROL_MEMORY_LIMIT not defined
  33. #endif
  34. #ifndef KEXEC_CONTROL_MEMORY_GFP
  35. #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
  36. #endif
  37. #ifndef KEXEC_CONTROL_PAGE_SIZE
  38. #error KEXEC_CONTROL_PAGE_SIZE not defined
  39. #endif
  40. #ifndef KEXEC_ARCH
  41. #error KEXEC_ARCH not defined
  42. #endif
  43. #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
  44. #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
  45. #endif
  46. #ifndef KEXEC_CRASH_MEM_ALIGN
  47. #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
  48. #endif
  49. #define KEXEC_NOTE_HEAD_BYTES ALIGN(sizeof(struct elf_note), 4)
  50. #define KEXEC_CORE_NOTE_NAME "CORE"
  51. #define KEXEC_CORE_NOTE_NAME_BYTES ALIGN(sizeof(KEXEC_CORE_NOTE_NAME), 4)
  52. #define KEXEC_CORE_NOTE_DESC_BYTES ALIGN(sizeof(struct elf_prstatus), 4)
  53. /*
  54. * The per-cpu notes area is a list of notes terminated by a "NULL"
  55. * note header. For kdump, the code in vmcore.c runs in the context
  56. * of the second kernel to combine them into one note.
  57. */
  58. #ifndef KEXEC_NOTE_BYTES
  59. #define KEXEC_NOTE_BYTES ( (KEXEC_NOTE_HEAD_BYTES * 2) + \
  60. KEXEC_CORE_NOTE_NAME_BYTES + \
  61. KEXEC_CORE_NOTE_DESC_BYTES )
  62. #endif
  63. /*
  64. * This structure is used to hold the arguments that are used when loading
  65. * kernel binaries.
  66. */
  67. typedef unsigned long kimage_entry_t;
  68. struct kexec_segment {
  69. /*
  70. * This pointer can point to user memory if kexec_load() system
  71. * call is used or will point to kernel memory if
  72. * kexec_file_load() system call is used.
  73. *
  74. * Use ->buf when expecting to deal with user memory and use ->kbuf
  75. * when expecting to deal with kernel memory.
  76. */
  77. union {
  78. void __user *buf;
  79. void *kbuf;
  80. };
  81. size_t bufsz;
  82. unsigned long mem;
  83. size_t memsz;
  84. };
  85. #ifdef CONFIG_COMPAT
  86. struct compat_kexec_segment {
  87. compat_uptr_t buf;
  88. compat_size_t bufsz;
  89. compat_ulong_t mem; /* User space sees this as a (void *) ... */
  90. compat_size_t memsz;
  91. };
  92. #endif
  93. #ifdef CONFIG_KEXEC_FILE
  94. struct purgatory_info {
  95. /* Pointer to elf header of read only purgatory */
  96. Elf_Ehdr *ehdr;
  97. /* Pointer to purgatory sechdrs which are modifiable */
  98. Elf_Shdr *sechdrs;
  99. /*
  100. * Temporary buffer location where purgatory is loaded and relocated
  101. * This memory can be freed post image load
  102. */
  103. void *purgatory_buf;
  104. /* Address where purgatory is finally loaded and is executed from */
  105. unsigned long purgatory_load_addr;
  106. };
  107. typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
  108. typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
  109. unsigned long kernel_len, char *initrd,
  110. unsigned long initrd_len, char *cmdline,
  111. unsigned long cmdline_len);
  112. typedef int (kexec_cleanup_t)(void *loader_data);
  113. #ifdef CONFIG_KEXEC_VERIFY_SIG
  114. typedef int (kexec_verify_sig_t)(const char *kernel_buf,
  115. unsigned long kernel_len);
  116. #endif
  117. struct kexec_file_ops {
  118. kexec_probe_t *probe;
  119. kexec_load_t *load;
  120. kexec_cleanup_t *cleanup;
  121. #ifdef CONFIG_KEXEC_VERIFY_SIG
  122. kexec_verify_sig_t *verify_sig;
  123. #endif
  124. };
  125. #endif
  126. struct kimage {
  127. kimage_entry_t head;
  128. kimage_entry_t *entry;
  129. kimage_entry_t *last_entry;
  130. unsigned long start;
  131. struct page *control_code_page;
  132. struct page *swap_page;
  133. unsigned long nr_segments;
  134. struct kexec_segment segment[KEXEC_SEGMENT_MAX];
  135. struct list_head control_pages;
  136. struct list_head dest_pages;
  137. struct list_head unusable_pages;
  138. /* Address of next control page to allocate for crash kernels. */
  139. unsigned long control_page;
  140. /* Flags to indicate special processing */
  141. unsigned int type : 1;
  142. #define KEXEC_TYPE_DEFAULT 0
  143. #define KEXEC_TYPE_CRASH 1
  144. unsigned int preserve_context : 1;
  145. /* If set, we are using file mode kexec syscall */
  146. unsigned int file_mode:1;
  147. #ifdef ARCH_HAS_KIMAGE_ARCH
  148. struct kimage_arch arch;
  149. #endif
  150. #ifdef CONFIG_KEXEC_FILE
  151. /* Additional fields for file based kexec syscall */
  152. void *kernel_buf;
  153. unsigned long kernel_buf_len;
  154. void *initrd_buf;
  155. unsigned long initrd_buf_len;
  156. char *cmdline_buf;
  157. unsigned long cmdline_buf_len;
  158. /* File operations provided by image loader */
  159. struct kexec_file_ops *fops;
  160. /* Image loader handling the kernel can store a pointer here */
  161. void *image_loader_data;
  162. /* Information for loading purgatory */
  163. struct purgatory_info purgatory_info;
  164. #endif
  165. };
  166. /* kexec interface functions */
  167. extern void machine_kexec(struct kimage *image);
  168. extern int machine_kexec_prepare(struct kimage *image);
  169. extern void machine_kexec_cleanup(struct kimage *image);
  170. extern asmlinkage long sys_kexec_load(unsigned long entry,
  171. unsigned long nr_segments,
  172. struct kexec_segment __user *segments,
  173. unsigned long flags);
  174. extern int kernel_kexec(void);
  175. extern int kexec_add_buffer(struct kimage *image, char *buffer,
  176. unsigned long bufsz, unsigned long memsz,
  177. unsigned long buf_align, unsigned long buf_min,
  178. unsigned long buf_max, bool top_down,
  179. unsigned long *load_addr);
  180. extern struct page *kimage_alloc_control_pages(struct kimage *image,
  181. unsigned int order);
  182. extern int kexec_load_purgatory(struct kimage *image, unsigned long min,
  183. unsigned long max, int top_down,
  184. unsigned long *load_addr);
  185. extern int kexec_purgatory_get_set_symbol(struct kimage *image,
  186. const char *name, void *buf,
  187. unsigned int size, bool get_value);
  188. extern void *kexec_purgatory_get_symbol_addr(struct kimage *image,
  189. const char *name);
  190. extern void __crash_kexec(struct pt_regs *);
  191. extern void crash_kexec(struct pt_regs *);
  192. int kexec_should_crash(struct task_struct *);
  193. int kexec_crash_loaded(void);
  194. void crash_save_cpu(struct pt_regs *regs, int cpu);
  195. void crash_save_vmcoreinfo(void);
  196. void arch_crash_save_vmcoreinfo(void);
  197. __printf(1, 2)
  198. void vmcoreinfo_append_str(const char *fmt, ...);
  199. phys_addr_t paddr_vmcoreinfo_note(void);
  200. #define VMCOREINFO_OSRELEASE(value) \
  201. vmcoreinfo_append_str("OSRELEASE=%s\n", value)
  202. #define VMCOREINFO_PAGESIZE(value) \
  203. vmcoreinfo_append_str("PAGESIZE=%ld\n", value)
  204. #define VMCOREINFO_SYMBOL(name) \
  205. vmcoreinfo_append_str("SYMBOL(%s)=%lx\n", #name, (unsigned long)&name)
  206. #define VMCOREINFO_SIZE(name) \
  207. vmcoreinfo_append_str("SIZE(%s)=%lu\n", #name, \
  208. (unsigned long)sizeof(name))
  209. #define VMCOREINFO_STRUCT_SIZE(name) \
  210. vmcoreinfo_append_str("SIZE(%s)=%lu\n", #name, \
  211. (unsigned long)sizeof(struct name))
  212. #define VMCOREINFO_OFFSET(name, field) \
  213. vmcoreinfo_append_str("OFFSET(%s.%s)=%lu\n", #name, #field, \
  214. (unsigned long)offsetof(struct name, field))
  215. #define VMCOREINFO_LENGTH(name, value) \
  216. vmcoreinfo_append_str("LENGTH(%s)=%lu\n", #name, (unsigned long)value)
  217. #define VMCOREINFO_NUMBER(name) \
  218. vmcoreinfo_append_str("NUMBER(%s)=%ld\n", #name, (long)name)
  219. #define VMCOREINFO_CONFIG(name) \
  220. vmcoreinfo_append_str("CONFIG_%s=y\n", #name)
  221. #define VMCOREINFO_PAGE_OFFSET(value) \
  222. vmcoreinfo_append_str("PAGE_OFFSET=%lx\n", (unsigned long)value)
  223. #define VMCOREINFO_VMALLOC_START(value) \
  224. vmcoreinfo_append_str("VMALLOC_START=%lx\n", (unsigned long)value)
  225. #define VMCOREINFO_VMEMMAP_START(value) \
  226. vmcoreinfo_append_str("VMEMMAP_START=%lx\n", (unsigned long)value)
  227. extern struct kimage *kexec_image;
  228. extern struct kimage *kexec_crash_image;
  229. extern int kexec_load_disabled;
  230. #ifndef kexec_flush_icache_page
  231. #define kexec_flush_icache_page(page)
  232. #endif
  233. /* List of defined/legal kexec flags */
  234. #ifndef CONFIG_KEXEC_JUMP
  235. #define KEXEC_FLAGS KEXEC_ON_CRASH
  236. #else
  237. #define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT)
  238. #endif
  239. /* List of defined/legal kexec file flags */
  240. #define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
  241. KEXEC_FILE_NO_INITRAMFS)
  242. #define VMCOREINFO_BYTES (4096)
  243. #define VMCOREINFO_NOTE_NAME "VMCOREINFO"
  244. #define VMCOREINFO_NOTE_NAME_BYTES ALIGN(sizeof(VMCOREINFO_NOTE_NAME), 4)
  245. #define VMCOREINFO_NOTE_SIZE (KEXEC_NOTE_HEAD_BYTES*2 + VMCOREINFO_BYTES \
  246. + VMCOREINFO_NOTE_NAME_BYTES)
  247. /* Location of a reserved region to hold the crash kernel.
  248. */
  249. extern struct resource crashk_res;
  250. extern struct resource crashk_low_res;
  251. typedef u32 note_buf_t[KEXEC_NOTE_BYTES/4];
  252. extern note_buf_t __percpu *crash_notes;
  253. extern u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
  254. extern size_t vmcoreinfo_size;
  255. extern size_t vmcoreinfo_max_size;
  256. /* flag to track if kexec reboot is in progress */
  257. extern bool kexec_in_progress;
  258. int __init parse_crashkernel(char *cmdline, unsigned long long system_ram,
  259. unsigned long long *crash_size, unsigned long long *crash_base);
  260. int parse_crashkernel_high(char *cmdline, unsigned long long system_ram,
  261. unsigned long long *crash_size, unsigned long long *crash_base);
  262. int parse_crashkernel_low(char *cmdline, unsigned long long system_ram,
  263. unsigned long long *crash_size, unsigned long long *crash_base);
  264. int crash_shrink_memory(unsigned long new_size);
  265. size_t crash_get_memory_size(void);
  266. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
  267. int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
  268. unsigned long buf_len);
  269. void * __weak arch_kexec_kernel_image_load(struct kimage *image);
  270. int __weak arch_kimage_file_post_load_cleanup(struct kimage *image);
  271. int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
  272. unsigned long buf_len);
  273. int __weak arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr,
  274. Elf_Shdr *sechdrs, unsigned int relsec);
  275. int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  276. unsigned int relsec);
  277. void arch_kexec_protect_crashkres(void);
  278. void arch_kexec_unprotect_crashkres(void);
  279. #ifndef page_to_boot_pfn
  280. static inline unsigned long page_to_boot_pfn(struct page *page)
  281. {
  282. return page_to_pfn(page);
  283. }
  284. #endif
  285. #ifndef boot_pfn_to_page
  286. static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
  287. {
  288. return pfn_to_page(boot_pfn);
  289. }
  290. #endif
  291. #ifndef phys_to_boot_phys
  292. static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
  293. {
  294. return phys;
  295. }
  296. #endif
  297. #ifndef boot_phys_to_phys
  298. static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
  299. {
  300. return boot_phys;
  301. }
  302. #endif
  303. static inline unsigned long virt_to_boot_phys(void *addr)
  304. {
  305. return phys_to_boot_phys(__pa((unsigned long)addr));
  306. }
  307. static inline void *boot_phys_to_virt(unsigned long entry)
  308. {
  309. return phys_to_virt(boot_phys_to_phys(entry));
  310. }
  311. #else /* !CONFIG_KEXEC_CORE */
  312. struct pt_regs;
  313. struct task_struct;
  314. static inline void __crash_kexec(struct pt_regs *regs) { }
  315. static inline void crash_kexec(struct pt_regs *regs) { }
  316. static inline int kexec_should_crash(struct task_struct *p) { return 0; }
  317. static inline int kexec_crash_loaded(void) { return 0; }
  318. #define kexec_in_progress false
  319. #endif /* CONFIG_KEXEC_CORE */
  320. #endif /* !defined(__ASSEBMLY__) */
  321. #endif /* LINUX_KEXEC_H */