Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / linux / kexec.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
dc009d92
EB
2#ifndef LINUX_KEXEC_H
3#define LINUX_KEXEC_H
4
cf2df639
GL
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)
b28c2ee8 14#define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
cf2df639
GL
15
16#if !defined(__ASSEMBLY__)
17
443cbaf9 18#include <linux/vmcore_info.h>
85fcde40 19#include <linux/crash_reserve.h>
43546d86 20#include <asm/io.h>
cade589f 21#include <linux/range.h>
43546d86 22
607ca46e 23#include <uapi/linux/kexec.h>
c903dae8 24#include <linux/verification.h>
29a5c67e 25
f05fa109
JZ
26extern note_buf_t __percpu *crash_notes;
27
bf454ec3
CX
28#ifdef CONFIG_CRASH_DUMP
29#include <linux/prandom.h>
30#endif
31
2965faa5 32#ifdef CONFIG_KEXEC_CORE
dc009d92 33#include <linux/list.h>
dc009d92 34#include <linux/compat.h>
9c15e852 35#include <linux/ioport.h>
12db5562 36#include <linux/module.h>
24726275 37#include <linux/highmem.h>
dc009d92 38#include <asm/kexec.h>
02aff848 39#include <linux/crash_core.h>
dc009d92
EB
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
7e01b5ac 55#ifndef KEXEC_CONTROL_MEMORY_GFP
dc5cccac 56#define KEXEC_CONTROL_MEMORY_GFP (GFP_KERNEL | __GFP_NORETRY)
7e01b5ac
MS
57#endif
58
163f6876
HY
59#ifndef KEXEC_CONTROL_PAGE_SIZE
60#error KEXEC_CONTROL_PAGE_SIZE not defined
dc009d92
EB
61#endif
62
63#ifndef KEXEC_ARCH
64#error KEXEC_ARCH not defined
65#endif
66
3d214fae
MH
67#ifndef KEXEC_CRASH_CONTROL_MEMORY_LIMIT
68#define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
69#endif
70
558df720
MH
71#ifndef KEXEC_CRASH_MEM_ALIGN
72#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
73#endif
74
dc009d92
EB
75/*
76 * This structure is used to hold the arguments that are used when loading
77 * kernel binaries.
78 */
79
80typedef unsigned long kimage_entry_t;
dc009d92 81
dc009d92 82struct kexec_segment {
815d5704
VG
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 };
dc009d92 95 size_t bufsz;
29a5c67e 96 unsigned long mem;
dc009d92
EB
97 size_t memsz;
98};
99
100#ifdef CONFIG_COMPAT
101struct 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
978e30c9 109#ifdef CONFIG_KEXEC_FILE
12db5562 110struct purgatory_info {
65c225d3
PR
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 */
12db5562
VG
120 Elf_Shdr *sechdrs;
121 /*
65c225d3
PR
122 * Temporary, modifiable buffer for stripped purgatory used for
123 * relocation. This memory can be freed post image load.
12db5562
VG
124 */
125 void *purgatory_buf;
12db5562
VG
126};
127
ee6ebeda
PR
128struct kimage;
129
978e30c9
XP
130typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
131typedef 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);
135typedef int (kexec_cleanup_t)(void *loader_data);
136
99d5cadf 137#ifdef CONFIG_KEXEC_SIG
978e30c9
XP
138typedef int (kexec_verify_sig_t)(const char *kernel_buf,
139 unsigned long kernel_len);
140#endif
141
142struct kexec_file_ops {
143 kexec_probe_t *probe;
144 kexec_load_t *load;
145 kexec_cleanup_t *cleanup;
99d5cadf 146#ifdef CONFIG_KEXEC_SIG
978e30c9
XP
147 kexec_verify_sig_t *verify_sig;
148#endif
149};
60fe3910 150
9ec4ecef
AT
151extern const struct kexec_file_ops * const kexec_file_loaders[];
152
153int kexec_image_probe_default(struct kimage *image, void *buf,
154 unsigned long buf_len);
92a98a2b 155int kexec_image_post_load_cleanup_default(struct kimage *image);
9ec4ecef 156
b6664ba4
AT
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
60fe3910
TJB
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.
bf454ec3 176 * @random: Place the buffer at a random position.
60fe3910
TJB
177 */
178struct kexec_buf {
179 struct kimage *image;
ec2b9bfa 180 void *buffer;
60fe3910
TJB
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;
bf454ec3
CX
188#ifdef CONFIG_CRASH_DUMP
189 bool random;
190#endif
60fe3910
TJB
191};
192
bf454ec3
CX
193
194#ifdef CONFIG_CRASH_DUMP
195static 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
208static 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
3be3f61d
PR
215int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
216int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
217 void *buf, unsigned int size,
218 bool get_value);
219void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name);
65d9a9a6
NR
220
221#ifndef arch_kexec_kernel_image_probe
222static inline int
223arch_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
230static 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
9f0552c9
SJ
236#ifndef arch_check_excluded_range
237static 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
c903dae8
CX
245#ifdef CONFIG_KEXEC_SIG
246#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
247int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
248#endif
249#endif
250
ec2b9bfa 251extern int kexec_add_buffer(struct kexec_buf *kbuf);
e2e806f9 252int kexec_locate_mem_hole(struct kexec_buf *kbuf);
babac4a8 253
65d9a9a6
NR
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 */
263static inline int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
264{
265 return kexec_locate_mem_hole(kbuf);
266}
267#endif
268
3e35142e
NR
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 */
279static inline int
280arch_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 */
298static inline int
299arch_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
60fe3910 306#endif /* CONFIG_KEXEC_FILE */
978e30c9 307
175fca3b
SS
308#ifdef CONFIG_KEXEC_ELF
309struct 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;
175fca3b
SS
318};
319
320int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
321 struct kexec_elf_info *elf_info);
322
323int 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
328void kexec_free_elf_info(struct kexec_elf_info *elf_info);
329int kexec_elf_probe(const char *buf, unsigned long len);
330#endif
dc009d92
EB
331struct kimage {
332 kimage_entry_t head;
333 kimage_entry_t *entry;
334 kimage_entry_t *last_entry;
335
dc009d92
EB
336 unsigned long start;
337 struct page *control_code_page;
3ab83521 338 struct page *swap_page;
1229384f 339 void *vmcoreinfo_data_copy; /* locates in the crash memory */
dc009d92
EB
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;
7d3e2bca 346 struct list_head unusable_pages;
dc009d92
EB
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
3ab83521 355 unsigned int preserve_context : 1;
cb105258
VG
356 /* If set, we are using file mode kexec syscall */
357 unsigned int file_mode:1;
a72bbec7 358#ifdef CONFIG_CRASH_HOTPLUG
79365026
SJ
359 /* If set, it is safe to update kexec segments that are
360 * excluded from SHA calculation.
361 */
362 unsigned int hotplug_support:1;
a72bbec7 363#endif
92be3d6b
HY
364
365#ifdef ARCH_HAS_KIMAGE_ARCH
366 struct kimage_arch arch;
367#endif
cb105258 368
978e30c9 369#ifdef CONFIG_KEXEC_FILE
cb105258
VG
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 */
9ec4ecef 381 const struct kexec_file_ops *fops;
cb105258
VG
382
383 /* Image loader handling the kernel can store a pointer here */
384 void *image_loader_data;
12db5562
VG
385
386 /* Information for loading purgatory */
387 struct purgatory_info purgatory_info;
978e30c9 388#endif
f31e3386 389
24726275
ED
390#ifdef CONFIG_CRASH_HOTPLUG
391 int hp_action;
392 int elfcorehdr_index;
393 bool elfcorehdr_updated;
394#endif
395
f31e3386
LR
396#ifdef CONFIG_IMA_KEXEC
397 /* Virtual address of IMA measurement buffer for kexec syscall */
398 void *ima_buffer;
0c605158
LR
399
400 phys_addr_t ima_buffer_addr;
401 size_t ima_buffer_size;
9ee8888a
SC
402
403 unsigned long ima_segment_index;
404 bool is_ima_segment_index_set;
f31e3386 405#endif
9336a5f6 406
3bdecc3c
AG
407 struct {
408 struct kexec_segment *scratch;
409 phys_addr_t fdt;
410 } kho;
411
9336a5f6
LR
412 /* Core ELF header buffer */
413 void *elf_headers;
414 unsigned long elf_headers_sz;
415 unsigned long elf_load_addr;
479e5854
CX
416
417 /* dm crypt keys buffer */
418 unsigned long dm_crypt_keys_addr;
419 unsigned long dm_crypt_keys_sz;
cb105258 420};
dc009d92
EB
421
422/* kexec interface functions */
3ab83521 423extern void machine_kexec(struct kimage *image);
dc009d92
EB
424extern int machine_kexec_prepare(struct kimage *image);
425extern void machine_kexec_cleanup(struct kimage *image);
3ab83521 426extern int kernel_kexec(void);
72414d3f
MS
427extern struct page *kimage_alloc_control_pages(struct kimage *image,
428 unsigned int order);
0738eceb
NR
429
430#ifndef machine_kexec_post_load
431static inline int machine_kexec_post_load(struct kimage *image) { return 0; }
432#endif
2596b6ae 433
dc009d92 434extern struct kimage *kexec_image;
c330dda9 435extern struct kimage *kexec_crash_image;
7e99f8b6 436
a42aaad2 437bool kexec_load_permitted(int kexec_image_type);
dc009d92 438
a7956113
ZN
439#ifndef kexec_flush_icache_page
440#define kexec_flush_icache_page(page)
441#endif
442
3ab83521
HY
443/* List of defined/legal kexec flags */
444#ifndef CONFIG_KEXEC_JUMP
79365026 445#define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_UPDATE_ELFCOREHDR | KEXEC_CRASH_HOTPLUG_SUPPORT)
3ab83521 446#else
79365026
SJ
447#define KEXEC_FLAGS (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR | \
448 KEXEC_CRASH_HOTPLUG_SUPPORT)
3ab83521 449#endif
dc009d92 450
cb105258
VG
451/* List of defined/legal kexec file flags */
452#define KEXEC_FILE_FLAGS (KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
cbc2fe9d 453 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
cb105258 454
4fc9bbf9
KA
455/* flag to track if kexec reboot is in progress */
456extern bool kexec_in_progress;
457
43546d86
RK
458#ifndef page_to_boot_pfn
459static 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
466static 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
473static 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
480static inline phys_addr_t boot_phys_to_phys(unsigned long boot_phys)
481{
482 return boot_phys;
483}
484#endif
485
0738eceb
NR
486#ifndef crash_free_reserved_phys_range
487static 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
43546d86
RK
496static inline unsigned long virt_to_boot_phys(void *addr)
497{
498 return phys_to_boot_phys(__pa((unsigned long)addr));
499}
500
501static inline void *boot_phys_to_virt(unsigned long entry)
502{
503 return phys_to_virt(boot_phys_to_phys(entry));
504}
505
bba4ed01
TL
506#ifndef arch_kexec_post_alloc_pages
507static 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
511static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { }
512#endif
513
cbc2fe9d
BH
514extern bool kexec_file_dbg_print;
515
f4af41bf
BH
516#define kexec_dprintk(fmt, arg...) \
517 do { if (kexec_file_dbg_print) pr_info(fmt, ##arg); } while (0)
cbc2fe9d 518
0091d924
SC
519extern void *kimage_map_segment(struct kimage *image, unsigned long addr, unsigned long size);
520extern void kimage_unmap_segment(void *buffer);
2965faa5 521#else /* !CONFIG_KEXEC_CORE */
6e274d14
AN
522struct pt_regs;
523struct task_struct;
0091d924 524struct kimage;
7bbee5ca 525static inline void __crash_kexec(struct pt_regs *regs) { }
6e274d14
AN
526static inline void crash_kexec(struct pt_regs *regs) { }
527static inline int kexec_should_crash(struct task_struct *p) { return 0; }
21db79e8 528static inline int kexec_crash_loaded(void) { return 0; }
0091d924
SC
529static inline void *kimage_map_segment(struct kimage *image, unsigned long addr, unsigned long size)
530{ return NULL; }
531static inline void kimage_unmap_segment(void *buffer) { }
2b94ed24 532#define kexec_in_progress false
2965faa5 533#endif /* CONFIG_KEXEC_CORE */
cf2df639 534
af16df54
CX
535#ifdef CONFIG_KEXEC_SIG
536void set_kexec_sig_enforced(void);
537#else
538static inline void set_kexec_sig_enforced(void) {}
539#endif
540
cf2df639
GL
541#endif /* !defined(__ASSEBMLY__) */
542
dc009d92 543#endif /* LINUX_KEXEC_H */