kexec.h 10 KB

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