03f85ad03025ba735c574e1c416ed87a6dc1762a
[linux-2.6-block.git] / include / linux / kexec.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef LINUX_KEXEC_H
3 #define LINUX_KEXEC_H
4
5 #define IND_DESTINATION_BIT 0
6 #define IND_INDIRECTION_BIT 1
7 #define IND_DONE_BIT        2
8 #define IND_SOURCE_BIT      3
9
10 #define IND_DESTINATION  (1 << IND_DESTINATION_BIT)
11 #define IND_INDIRECTION  (1 << IND_INDIRECTION_BIT)
12 #define IND_DONE         (1 << IND_DONE_BIT)
13 #define IND_SOURCE       (1 << IND_SOURCE_BIT)
14 #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
15
16 #if !defined(__ASSEMBLY__)
17
18 #include <linux/vmcore_info.h>
19 #include <linux/crash_reserve.h>
20 #include <asm/io.h>
21 #include <linux/range.h>
22
23 #include <uapi/linux/kexec.h>
24 #include <linux/verification.h>
25
26 extern note_buf_t __percpu *crash_notes;
27
28 #ifdef CONFIG_CRASH_DUMP
29 #include <linux/prandom.h>
30 #endif
31
32 #ifdef CONFIG_KEXEC_CORE
33 #include <linux/list.h>
34 #include <linux/compat.h>
35 #include <linux/ioport.h>
36 #include <linux/module.h>
37 #include <linux/highmem.h>
38 #include <asm/kexec.h>
39 #include <linux/crash_core.h>
40
41 /* Verify architecture specific macros are defined */
42
43 #ifndef KEXEC_SOURCE_MEMORY_LIMIT
44 #error KEXEC_SOURCE_MEMORY_LIMIT not defined
45 #endif
46
47 #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
48 #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
49 #endif
50
51 #ifndef KEXEC_CONTROL_MEMORY_LIMIT
52 #error KEXEC_CONTROL_MEMORY_LIMIT not defined
53 #endif
54
55 #ifndef KEXEC_CONTROL_MEMORY_GFP
56 #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
57 #endif
58
59 #ifndef KEXEC_CONTROL_PAGE_SIZE
60 #error KEXEC_CONTROL_PAGE_SIZE not defined
61 #endif
62
63 #ifndef KEXEC_ARCH
64 #error KEXEC_ARCH not defined
65 #endif
66
67 #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
68 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
69 #endif
70
71 #ifndef KEXEC_CRASH_MEM_ALIGN
72 #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
73 #endif
74
75 /*
76  * This structure is used to hold the arguments that are used when loading
77  * kernel binaries.
78  */
79
80 typedef unsigned long kimage_entry_t;
81
82 struct kexec_segment {
83         /*
84          * This pointer can point to user memory if kexec_load() system
85          * call is used or will point to kernel memory if
86          * kexec_file_load() system call is used.
87          *
88          * Use ->buf when expecting to deal with user memory and use ->kbuf
89          * when expecting to deal with kernel memory.
90          */
91         union {
92                 void __user *buf;
93                 void *kbuf;
94         };
95         size_t bufsz;
96         unsigned long mem;
97         size_t memsz;
98 };
99
100 #ifdef CONFIG_COMPAT
101 struct compat_kexec_segment {
102         compat_uptr_t buf;
103         compat_size_t bufsz;
104         compat_ulong_t mem;     /* User space sees this as a (void *) ... */
105         compat_size_t memsz;
106 };
107 #endif
108
109 #ifdef CONFIG_KEXEC_FILE
110 struct purgatory_info {
111         /*
112          * Pointer to elf header at the beginning of kexec_purgatory.
113          * Note: kexec_purgatory is read only
114          */
115         const Elf_Ehdr *ehdr;
116         /*
117          * Temporary, modifiable buffer for sechdrs used for relocation.
118          * This memory can be freed post image load.
119          */
120         Elf_Shdr *sechdrs;
121         /*
122          * Temporary, modifiable buffer for stripped purgatory used for
123          * relocation. This memory can be freed post image load.
124          */
125         void *purgatory_buf;
126 };
127
128 struct kimage;
129
130 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
131 typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
132                              unsigned long kernel_len, char *initrd,
133                              unsigned long initrd_len, char *cmdline,
134                              unsigned long cmdline_len);
135 typedef int (kexec_cleanup_t)(void *loader_data);
136
137 #ifdef CONFIG_KEXEC_SIG
138 typedef int (kexec_verify_sig_t)(const char *kernel_buf,
139                                  unsigned long kernel_len);
140 #endif
141
142 struct kexec_file_ops {
143         kexec_probe_t *probe;
144         kexec_load_t *load;
145         kexec_cleanup_t *cleanup;
146 #ifdef CONFIG_KEXEC_SIG
147         kexec_verify_sig_t *verify_sig;
148 #endif
149 };
150
151 extern const struct kexec_file_ops * const kexec_file_loaders[];
152
153 int kexec_image_probe_default(struct kimage *image, void *buf,
154                               unsigned long buf_len);
155 int kexec_image_post_load_cleanup_default(struct kimage *image);
156
157 /*
158  * If kexec_buf.mem is set to this value, kexec_locate_mem_hole()
159  * will try to allocate free memory. Arch may overwrite it.
160  */
161 #ifndef KEXEC_BUF_MEM_UNKNOWN
162 #define KEXEC_BUF_MEM_UNKNOWN 0
163 #endif
164
165 /**
166  * struct kexec_buf - parameters for finding a place for a buffer in memory
167  * @image:      kexec image in which memory to search.
168  * @buffer:     Contents which will be copied to the allocated memory.
169  * @bufsz:      Size of @buffer.
170  * @mem:        On return will have address of the buffer in memory.
171  * @memsz:      Size for the buffer in memory.
172  * @buf_align:  Minimum alignment needed.
173  * @buf_min:    The buffer can't be placed below this address.
174  * @buf_max:    The buffer can't be placed above this address.
175  * @top_down:   Allocate from top of memory.
176  * @random:     Place the buffer at a random position.
177  */
178 struct kexec_buf {
179         struct kimage *image;
180         void *buffer;
181         unsigned long bufsz;
182         unsigned long mem;
183         unsigned long memsz;
184         unsigned long buf_align;
185         unsigned long buf_min;
186         unsigned long buf_max;
187         bool top_down;
188 #ifdef CONFIG_CRASH_DUMP
189         bool random;
190 #endif
191 };
192
193
194 #ifdef CONFIG_CRASH_DUMP
195 static inline void kexec_random_range_start(unsigned long start,
196                                             unsigned long end,
197                                             struct kexec_buf *kbuf,
198                                             unsigned long *temp_start)
199 {
200         unsigned short i;
201
202         if (kbuf->random) {
203                 get_random_bytes(&i, sizeof(unsigned short));
204                 *temp_start = start + (end - start) / USHRT_MAX * i;
205         }
206 }
207 #else
208 static inline void kexec_random_range_start(unsigned long start,
209                                             unsigned long end,
210                                             struct kexec_buf *kbuf,
211                                             unsigned long *temp_start)
212 {}
213 #endif
214
215 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
216 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
217                                    void *buf, unsigned int size,
218                                    bool get_value);
219 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
220
221 #ifndef arch_kexec_kernel_image_probe
222 static inline int
223 arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len)
224 {
225         return kexec_image_probe_default(image, buf, buf_len);
226 }
227 #endif
228
229 #ifndef arch_kimage_file_post_load_cleanup
230 static inline int arch_kimage_file_post_load_cleanup(struct kimage *image)
231 {
232         return kexec_image_post_load_cleanup_default(image);
233 }
234 #endif
235
236 #ifndef arch_check_excluded_range
237 static inline int arch_check_excluded_range(struct kimage *image,
238                                             unsigned long start,
239                                             unsigned long end)
240 {
241         return 0;
242 }
243 #endif
244
245 #ifdef CONFIG_KEXEC_SIG
246 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
247 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
248 #endif
249 #endif
250
251 extern int kexec_add_buffer(struct kexec_buf *kbuf);
252 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
253
254 #ifndef arch_kexec_locate_mem_hole
255 /**
256  * arch_kexec_locate_mem_hole - Find free memory to place the segments.
257  * @kbuf:                       Parameters for the memory search.
258  *
259  * On success, kbuf->mem will have the start address of the memory region found.
260  *
261  * Return: 0 on success, negative errno on error.
262  */
263 static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
264 {
265         return kexec_locate_mem_hole(kbuf);
266 }
267 #endif
268
269 #ifndef arch_kexec_apply_relocations_add
270 /*
271  * arch_kexec_apply_relocations_add - apply relocations of type RELA
272  * @pi:         Purgatory to be relocated.
273  * @section:    Section relocations applying to.
274  * @relsec:     Section containing RELAs.
275  * @symtab:     Corresponding symtab.
276  *
277  * Return: 0 on success, negative errno on error.
278  */
279 static inline int
280 arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
281                                  const Elf_Shdr *relsec, const Elf_Shdr *symtab)
282 {
283         pr_err("RELA relocation unsupported.\n");
284         return -ENOEXEC;
285 }
286 #endif
287
288 #ifndef arch_kexec_apply_relocations
289 /*
290  * arch_kexec_apply_relocations - apply relocations of type REL
291  * @pi:         Purgatory to be relocated.
292  * @section:    Section relocations applying to.
293  * @relsec:     Section containing RELs.
294  * @symtab:     Corresponding symtab.
295  *
296  * Return: 0 on success, negative errno on error.
297  */
298 static inline int
299 arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
300                              const Elf_Shdr *relsec, const Elf_Shdr *symtab)
301 {
302         pr_err("REL relocation unsupported.\n");
303         return -ENOEXEC;
304 }
305 #endif
306 #endif /* CONFIG_KEXEC_FILE */
307
308 #ifdef CONFIG_KEXEC_ELF
309 struct kexec_elf_info {
310         /*
311          * Where the ELF binary contents are kept.
312          * Memory managed by the user of the struct.
313          */
314         const char *buffer;
315
316         const struct elfhdr *ehdr;
317         const struct elf_phdr *proghdrs;
318 };
319
320 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
321                                struct kexec_elf_info *elf_info);
322
323 int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
324                          struct kexec_elf_info *elf_info,
325                          struct kexec_buf *kbuf,
326                          unsigned long *lowest_load_addr);
327
328 void kexec_free_elf_info(struct kexec_elf_info *elf_info);
329 int kexec_elf_probe(const char *buf, unsigned long len);
330 #endif
331 struct kimage {
332         kimage_entry_t head;
333         kimage_entry_t *entry;
334         kimage_entry_t *last_entry;
335
336         unsigned long start;
337         struct page *control_code_page;
338         struct page *swap_page;
339         void *vmcoreinfo_data_copy; /* locates in the crash memory */
340
341         unsigned long nr_segments;
342         struct kexec_segment segment[KEXEC_SEGMENT_MAX];
343
344         struct list_head control_pages;
345         struct list_head dest_pages;
346         struct list_head unusable_pages;
347
348         /* Address of next control page to allocate for crash kernels. */
349         unsigned long control_page;
350
351         /* Flags to indicate special processing */
352         unsigned int type : 1;
353 #define KEXEC_TYPE_DEFAULT 0
354 #define KEXEC_TYPE_CRASH   1
355         unsigned int preserve_context : 1;
356         /* If set, we are using file mode kexec syscall */
357         unsigned int file_mode:1;
358 #ifdef CONFIG_CRASH_HOTPLUG
359         /* If set, it is safe to update kexec segments that are
360          * excluded from SHA calculation.
361          */
362         unsigned int hotplug_support:1;
363 #endif
364
365 #ifdef ARCH_HAS_KIMAGE_ARCH
366         struct kimage_arch arch;
367 #endif
368
369 #ifdef CONFIG_KEXEC_FILE
370         /* Additional fields for file based kexec syscall */
371         void *kernel_buf;
372         unsigned long kernel_buf_len;
373
374         void *initrd_buf;
375         unsigned long initrd_buf_len;
376
377         char *cmdline_buf;
378         unsigned long cmdline_buf_len;
379
380         /* File operations provided by image loader */
381         const struct kexec_file_ops *fops;
382
383         /* Image loader handling the kernel can store a pointer here */
384         void *image_loader_data;
385
386         /* Information for loading purgatory */
387         struct purgatory_info purgatory_info;
388 #endif
389
390 #ifdef CONFIG_CRASH_HOTPLUG
391         int hp_action;
392         int elfcorehdr_index;
393         bool elfcorehdr_updated;
394 #endif
395
396 #ifdef CONFIG_IMA_KEXEC
397         /* Virtual address of IMA measurement buffer for kexec syscall */
398         void *ima_buffer;
399
400         phys_addr_t ima_buffer_addr;
401         size_t ima_buffer_size;
402
403         unsigned long ima_segment_index;
404         bool is_ima_segment_index_set;
405 #endif
406
407         struct {
408                 struct kexec_segment *scratch;
409                 phys_addr_t fdt;
410         } kho;
411
412         /* Core ELF header buffer */
413         void *elf_headers;
414         unsigned long elf_headers_sz;
415         unsigned long elf_load_addr;
416
417         /* dm crypt keys buffer */
418         unsigned long dm_crypt_keys_addr;
419         unsigned long dm_crypt_keys_sz;
420 };
421
422 /* kexec interface functions */
423 extern void machine_kexec(struct kimage *image);
424 extern int machine_kexec_prepare(struct kimage *image);
425 extern void machine_kexec_cleanup(struct kimage *image);
426 extern int kernel_kexec(void);
427 extern struct page *kimage_alloc_control_pages(struct kimage *image,
428                                                 unsigned int order);
429
430 #ifndef machine_kexec_post_load
431 static inline int machine_kexec_post_load(struct kimage *image) { return 0; }
432 #endif
433
434 extern struct kimage *kexec_image;
435 extern struct kimage *kexec_crash_image;
436
437 bool kexec_load_permitted(int kexec_image_type);
438
439 #ifndef kexec_flush_icache_page
440 #define kexec_flush_icache_page(page)
441 #endif
442
443 /* List of defined/legal kexec flags */
444 #ifndef CONFIG_KEXEC_JUMP
445 #define KEXEC_FLAGS    (KEXEC_ON_CRASH | KEXEC_UPDATE_ELFCOREHDR | KEXEC_CRASH_HOTPLUG_SUPPORT)
446 #else
447 #define KEXEC_FLAGS    (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR | \
448                         KEXEC_CRASH_HOTPLUG_SUPPORT)
449 #endif
450
451 /* List of defined/legal kexec file flags */
452 #define KEXEC_FILE_FLAGS        (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
453                                  KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
454
455 /* flag to track if kexec reboot is in progress */
456 extern bool kexec_in_progress;
457
458 #ifndef page_to_boot_pfn
459 static inline unsigned long page_to_boot_pfn(struct page *page)
460 {
461         return page_to_pfn(page);
462 }
463 #endif
464
465 #ifndef boot_pfn_to_page
466 static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
467 {
468         return pfn_to_page(boot_pfn);
469 }
470 #endif
471
472 #ifndef phys_to_boot_phys
473 static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
474 {
475         return phys;
476 }
477 #endif
478
479 #ifndef boot_phys_to_phys
480 static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
481 {
482         return boot_phys;
483 }
484 #endif
485
486 #ifndef crash_free_reserved_phys_range
487 static inline void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
488 {
489         unsigned long addr;
490
491         for (addr = begin; addr < end; addr += PAGE_SIZE)
492                 free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
493 }
494 #endif
495
496 static inline unsigned long virt_to_boot_phys(void *addr)
497 {
498         return phys_to_boot_phys(__pa((unsigned long)addr));
499 }
500
501 static inline void *boot_phys_to_virt(unsigned long entry)
502 {
503         return phys_to_virt(boot_phys_to_phys(entry));
504 }
505
506 #ifndef arch_kexec_post_alloc_pages
507 static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
508 #endif
509
510 #ifndef arch_kexec_pre_free_pages
511 static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
512 #endif
513
514 extern bool kexec_file_dbg_print;
515
516 #define kexec_dprintk(fmt, arg...) \
517         do { if (kexec_file_dbg_print) pr_info(fmt, ##arg); } while (0)
518
519 extern void *kimage_map_segment(struct kimage *image, unsigned long addr, unsigned long size);
520 extern void kimage_unmap_segment(void *buffer);
521 #else /* !CONFIG_KEXEC_CORE */
522 struct pt_regs;
523 struct task_struct;
524 struct kimage;
525 static inline void __crash_kexec(struct pt_regs *regs) { }
526 static inline void crash_kexec(struct pt_regs *regs) { }
527 static inline int kexec_should_crash(struct task_struct *p) { return 0; }
528 static inline int kexec_crash_loaded(void) { return 0; }
529 static inline void *kimage_map_segment(struct kimage *image, unsigned long addr, unsigned long size)
530 { return NULL; }
531 static inline void kimage_unmap_segment(void *buffer) { }
532 #define kexec_in_progress false
533 #endif /* CONFIG_KEXEC_CORE */
534
535 #ifdef CONFIG_KEXEC_SIG
536 void set_kexec_sig_enforced(void);
537 #else
538 static inline void set_kexec_sig_enforced(void) {}
539 #endif
540
541 #endif /* !defined(__ASSEBMLY__) */
542
543 #endif /* LINUX_KEXEC_H */