kexec.h 9.4 KB

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