xref: /linux-6.15/include/linux/kexec.h (revision 66cd9d4e)
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/crash_core.h>
19 #include <asm/io.h>
20 
21 #include <uapi/linux/kexec.h>
22 
23 /* Location of a reserved region to hold the crash kernel.
24  */
25 extern struct resource crashk_res;
26 extern struct resource crashk_low_res;
27 extern note_buf_t __percpu *crash_notes;
28 
29 #ifdef CONFIG_KEXEC_CORE
30 #include <linux/list.h>
31 #include <linux/compat.h>
32 #include <linux/ioport.h>
33 #include <linux/module.h>
34 #include <asm/kexec.h>
35 
36 /* Verify architecture specific macros are defined */
37 
38 #ifndef KEXEC_SOURCE_MEMORY_LIMIT
39 #error KEXEC_SOURCE_MEMORY_LIMIT not defined
40 #endif
41 
42 #ifndef KEXEC_DESTINATION_MEMORY_LIMIT
43 #error KEXEC_DESTINATION_MEMORY_LIMIT not defined
44 #endif
45 
46 #ifndef KEXEC_CONTROL_MEMORY_LIMIT
47 #error KEXEC_CONTROL_MEMORY_LIMIT not defined
48 #endif
49 
50 #ifndef KEXEC_CONTROL_MEMORY_GFP
51 #define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
52 #endif
53 
54 #ifndef KEXEC_CONTROL_PAGE_SIZE
55 #error KEXEC_CONTROL_PAGE_SIZE not defined
56 #endif
57 
58 #ifndef KEXEC_ARCH
59 #error KEXEC_ARCH not defined
60 #endif
61 
62 #ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
63 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
64 #endif
65 
66 #ifndef KEXEC_CRASH_MEM_ALIGN
67 #define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
68 #endif
69 
70 #define KEXEC_CORE_NOTE_NAME	CRASH_CORE_NOTE_NAME
71 
72 /*
73  * This structure is used to hold the arguments that are used when loading
74  * kernel binaries.
75  */
76 
77 typedef unsigned long kimage_entry_t;
78 
79 struct kexec_segment {
80 	/*
81 	 * This pointer can point to user memory if kexec_load() system
82 	 * call is used or will point to kernel memory if
83 	 * kexec_file_load() system call is used.
84 	 *
85 	 * Use ->buf when expecting to deal with user memory and use ->kbuf
86 	 * when expecting to deal with kernel memory.
87 	 */
88 	union {
89 		void __user *buf;
90 		void *kbuf;
91 	};
92 	size_t bufsz;
93 	unsigned long mem;
94 	size_t memsz;
95 };
96 
97 #ifdef CONFIG_COMPAT
98 struct compat_kexec_segment {
99 	compat_uptr_t buf;
100 	compat_size_t bufsz;
101 	compat_ulong_t mem;	/* User space sees this as a (void *) ... */
102 	compat_size_t memsz;
103 };
104 #endif
105 
106 #ifdef CONFIG_KEXEC_FILE
107 struct purgatory_info {
108 	/*
109 	 * Pointer to elf header at the beginning of kexec_purgatory.
110 	 * Note: kexec_purgatory is read only
111 	 */
112 	const Elf_Ehdr *ehdr;
113 	/*
114 	 * Temporary, modifiable buffer for sechdrs used for relocation.
115 	 * This memory can be freed post image load.
116 	 */
117 	Elf_Shdr *sechdrs;
118 	/*
119 	 * Temporary, modifiable buffer for stripped purgatory used for
120 	 * relocation. This memory can be freed post image load.
121 	 */
122 	void *purgatory_buf;
123 };
124 
125 struct kimage;
126 
127 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
128 typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
129 			     unsigned long kernel_len, char *initrd,
130 			     unsigned long initrd_len, char *cmdline,
131 			     unsigned long cmdline_len);
132 typedef int (kexec_cleanup_t)(void *loader_data);
133 
134 #ifdef CONFIG_KEXEC_SIG
135 typedef int (kexec_verify_sig_t)(const char *kernel_buf,
136 				 unsigned long kernel_len);
137 #endif
138 
139 struct kexec_file_ops {
140 	kexec_probe_t *probe;
141 	kexec_load_t *load;
142 	kexec_cleanup_t *cleanup;
143 #ifdef CONFIG_KEXEC_SIG
144 	kexec_verify_sig_t *verify_sig;
145 #endif
146 };
147 
148 extern const struct kexec_file_ops * const kexec_file_loaders[];
149 
150 int kexec_image_probe_default(struct kimage *image, void *buf,
151 			      unsigned long buf_len);
152 int kexec_image_post_load_cleanup_default(struct kimage *image);
153 
154 /*
155  * If kexec_buf.mem is set to this value, kexec_locate_mem_hole()
156  * will try to allocate free memory. Arch may overwrite it.
157  */
158 #ifndef KEXEC_BUF_MEM_UNKNOWN
159 #define KEXEC_BUF_MEM_UNKNOWN 0
160 #endif
161 
162 /**
163  * struct kexec_buf - parameters for finding a place for a buffer in memory
164  * @image:	kexec image in which memory to search.
165  * @buffer:	Contents which will be copied to the allocated memory.
166  * @bufsz:	Size of @buffer.
167  * @mem:	On return will have address of the buffer in memory.
168  * @memsz:	Size for the buffer in memory.
169  * @buf_align:	Minimum alignment needed.
170  * @buf_min:	The buffer can't be placed below this address.
171  * @buf_max:	The buffer can't be placed above this address.
172  * @top_down:	Allocate from top of memory.
173  */
174 struct kexec_buf {
175 	struct kimage *image;
176 	void *buffer;
177 	unsigned long bufsz;
178 	unsigned long mem;
179 	unsigned long memsz;
180 	unsigned long buf_align;
181 	unsigned long buf_min;
182 	unsigned long buf_max;
183 	bool top_down;
184 };
185 
186 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
187 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
188 				   void *buf, unsigned int size,
189 				   bool get_value);
190 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
191 
192 /* Architectures may override the below functions */
193 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
194 				  unsigned long buf_len);
195 void *arch_kexec_kernel_image_load(struct kimage *image);
196 int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
197 				     Elf_Shdr *section,
198 				     const Elf_Shdr *relsec,
199 				     const Elf_Shdr *symtab);
200 int arch_kexec_apply_relocations(struct purgatory_info *pi,
201 				 Elf_Shdr *section,
202 				 const Elf_Shdr *relsec,
203 				 const Elf_Shdr *symtab);
204 int arch_kimage_file_post_load_cleanup(struct kimage *image);
205 #ifdef CONFIG_KEXEC_SIG
206 int arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
207 				 unsigned long buf_len);
208 #endif
209 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf);
210 
211 extern int kexec_add_buffer(struct kexec_buf *kbuf);
212 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
213 
214 /* Alignment required for elf header segment */
215 #define ELF_CORE_HEADER_ALIGN   4096
216 
217 struct crash_mem_range {
218 	u64 start, end;
219 };
220 
221 struct crash_mem {
222 	unsigned int max_nr_ranges;
223 	unsigned int nr_ranges;
224 	struct crash_mem_range ranges[];
225 };
226 
227 extern int crash_exclude_mem_range(struct crash_mem *mem,
228 				   unsigned long long mstart,
229 				   unsigned long long mend);
230 extern int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
231 				       void **addr, unsigned long *sz);
232 #endif /* CONFIG_KEXEC_FILE */
233 
234 #ifdef CONFIG_KEXEC_ELF
235 struct kexec_elf_info {
236 	/*
237 	 * Where the ELF binary contents are kept.
238 	 * Memory managed by the user of the struct.
239 	 */
240 	const char *buffer;
241 
242 	const struct elfhdr *ehdr;
243 	const struct elf_phdr *proghdrs;
244 };
245 
246 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
247 			       struct kexec_elf_info *elf_info);
248 
249 int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
250 			 struct kexec_elf_info *elf_info,
251 			 struct kexec_buf *kbuf,
252 			 unsigned long *lowest_load_addr);
253 
254 void kexec_free_elf_info(struct kexec_elf_info *elf_info);
255 int kexec_elf_probe(const char *buf, unsigned long len);
256 #endif
257 struct kimage {
258 	kimage_entry_t head;
259 	kimage_entry_t *entry;
260 	kimage_entry_t *last_entry;
261 
262 	unsigned long start;
263 	struct page *control_code_page;
264 	struct page *swap_page;
265 	void *vmcoreinfo_data_copy; /* locates in the crash memory */
266 
267 	unsigned long nr_segments;
268 	struct kexec_segment segment[KEXEC_SEGMENT_MAX];
269 
270 	struct list_head control_pages;
271 	struct list_head dest_pages;
272 	struct list_head unusable_pages;
273 
274 	/* Address of next control page to allocate for crash kernels. */
275 	unsigned long control_page;
276 
277 	/* Flags to indicate special processing */
278 	unsigned int type : 1;
279 #define KEXEC_TYPE_DEFAULT 0
280 #define KEXEC_TYPE_CRASH   1
281 	unsigned int preserve_context : 1;
282 	/* If set, we are using file mode kexec syscall */
283 	unsigned int file_mode:1;
284 
285 #ifdef ARCH_HAS_KIMAGE_ARCH
286 	struct kimage_arch arch;
287 #endif
288 
289 #ifdef CONFIG_KEXEC_FILE
290 	/* Additional fields for file based kexec syscall */
291 	void *kernel_buf;
292 	unsigned long kernel_buf_len;
293 
294 	void *initrd_buf;
295 	unsigned long initrd_buf_len;
296 
297 	char *cmdline_buf;
298 	unsigned long cmdline_buf_len;
299 
300 	/* File operations provided by image loader */
301 	const struct kexec_file_ops *fops;
302 
303 	/* Image loader handling the kernel can store a pointer here */
304 	void *image_loader_data;
305 
306 	/* Information for loading purgatory */
307 	struct purgatory_info purgatory_info;
308 #endif
309 
310 #ifdef CONFIG_IMA_KEXEC
311 	/* Virtual address of IMA measurement buffer for kexec syscall */
312 	void *ima_buffer;
313 
314 	phys_addr_t ima_buffer_addr;
315 	size_t ima_buffer_size;
316 #endif
317 
318 	/* Core ELF header buffer */
319 	void *elf_headers;
320 	unsigned long elf_headers_sz;
321 	unsigned long elf_load_addr;
322 };
323 
324 /* kexec interface functions */
325 extern void machine_kexec(struct kimage *image);
326 extern int machine_kexec_prepare(struct kimage *image);
327 extern void machine_kexec_cleanup(struct kimage *image);
328 extern int kernel_kexec(void);
329 extern struct page *kimage_alloc_control_pages(struct kimage *image,
330 						unsigned int order);
331 int machine_kexec_post_load(struct kimage *image);
332 
333 extern void __crash_kexec(struct pt_regs *);
334 extern void crash_kexec(struct pt_regs *);
335 int kexec_should_crash(struct task_struct *);
336 int kexec_crash_loaded(void);
337 void crash_save_cpu(struct pt_regs *regs, int cpu);
338 extern int kimage_crash_copy_vmcoreinfo(struct kimage *image);
339 
340 extern struct kimage *kexec_image;
341 extern struct kimage *kexec_crash_image;
342 extern int kexec_load_disabled;
343 
344 #ifndef kexec_flush_icache_page
345 #define kexec_flush_icache_page(page)
346 #endif
347 
348 /* List of defined/legal kexec flags */
349 #ifndef CONFIG_KEXEC_JUMP
350 #define KEXEC_FLAGS    KEXEC_ON_CRASH
351 #else
352 #define KEXEC_FLAGS    (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT)
353 #endif
354 
355 /* List of defined/legal kexec file flags */
356 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
357 				 KEXEC_FILE_NO_INITRAMFS)
358 
359 /* flag to track if kexec reboot is in progress */
360 extern bool kexec_in_progress;
361 
362 int crash_shrink_memory(unsigned long new_size);
363 size_t crash_get_memory_size(void);
364 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
365 
366 void arch_kexec_protect_crashkres(void);
367 void arch_kexec_unprotect_crashkres(void);
368 
369 #ifndef page_to_boot_pfn
370 static inline unsigned long page_to_boot_pfn(struct page *page)
371 {
372 	return page_to_pfn(page);
373 }
374 #endif
375 
376 #ifndef boot_pfn_to_page
377 static inline struct page *boot_pfn_to_page(unsigned long boot_pfn)
378 {
379 	return pfn_to_page(boot_pfn);
380 }
381 #endif
382 
383 #ifndef phys_to_boot_phys
384 static inline unsigned long phys_to_boot_phys(phys_addr_t phys)
385 {
386 	return phys;
387 }
388 #endif
389 
390 #ifndef boot_phys_to_phys
391 static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
392 {
393 	return boot_phys;
394 }
395 #endif
396 
397 static inline unsigned long virt_to_boot_phys(void *addr)
398 {
399 	return phys_to_boot_phys(__pa((unsigned long)addr));
400 }
401 
402 static inline void *boot_phys_to_virt(unsigned long entry)
403 {
404 	return phys_to_virt(boot_phys_to_phys(entry));
405 }
406 
407 #ifndef arch_kexec_post_alloc_pages
408 static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { return 0; }
409 #endif
410 
411 #ifndef arch_kexec_pre_free_pages
412 static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
413 #endif
414 
415 #else /* !CONFIG_KEXEC_CORE */
416 struct pt_regs;
417 struct task_struct;
418 static inline void __crash_kexec(struct pt_regs *regs) { }
419 static inline void crash_kexec(struct pt_regs *regs) { }
420 static inline int kexec_should_crash(struct task_struct *p) { return 0; }
421 static inline int kexec_crash_loaded(void) { return 0; }
422 #define kexec_in_progress false
423 #endif /* CONFIG_KEXEC_CORE */
424 
425 #endif /* !defined(__ASSEBMLY__) */
426 
427 #endif /* LINUX_KEXEC_H */
428