1 // SPDX-License-Identifier: GPL-2.0-only
3 * handle transition of Linux booting another kernel
4 * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
7 #define pr_fmt(fmt) "kexec: " fmt
10 #include <linux/kexec.h>
11 #include <linux/string.h>
12 #include <linux/gfp.h>
13 #include <linux/reboot.h>
14 #include <linux/numa.h>
15 #include <linux/ftrace.h>
17 #include <linux/suspend.h>
18 #include <linux/vmalloc.h>
21 #include <asm/pgtable.h>
22 #include <asm/tlbflush.h>
23 #include <asm/mmu_context.h>
24 #include <asm/io_apic.h>
25 #include <asm/debugreg.h>
26 #include <asm/kexec-bzimage64.h>
27 #include <asm/setup.h>
28 #include <asm/set_memory.h>
30 #ifdef CONFIG_KEXEC_FILE
31 const struct kexec_file_ops * const kexec_file_loaders[] = {
37 static void free_transition_pgtable(struct kimage *image)
39 free_page((unsigned long)image->arch.p4d);
40 image->arch.p4d = NULL;
41 free_page((unsigned long)image->arch.pud);
42 image->arch.pud = NULL;
43 free_page((unsigned long)image->arch.pmd);
44 image->arch.pmd = NULL;
45 free_page((unsigned long)image->arch.pte);
46 image->arch.pte = NULL;
49 static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
55 unsigned long vaddr, paddr;
58 vaddr = (unsigned long)relocate_kernel;
59 paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE);
60 pgd += pgd_index(vaddr);
61 if (!pgd_present(*pgd)) {
62 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
65 image->arch.p4d = p4d;
66 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE));
68 p4d = p4d_offset(pgd, vaddr);
69 if (!p4d_present(*p4d)) {
70 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
73 image->arch.pud = pud;
74 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
76 pud = pud_offset(p4d, vaddr);
77 if (!pud_present(*pud)) {
78 pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
81 image->arch.pmd = pmd;
82 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
84 pmd = pmd_offset(pud, vaddr);
85 if (!pmd_present(*pmd)) {
86 pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
89 image->arch.pte = pte;
90 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
92 pte = pte_offset_kernel(pmd, vaddr);
93 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC_NOENC));
99 static void *alloc_pgt_page(void *data)
101 struct kimage *image = (struct kimage *)data;
105 page = kimage_alloc_control_pages(image, 0);
107 p = page_address(page);
114 static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
116 struct x86_mapping_info info = {
117 .alloc_pgt_page = alloc_pgt_page,
119 .page_flag = __PAGE_KERNEL_LARGE_EXEC,
120 .kernpg_flag = _KERNPG_TABLE_NOENC,
122 unsigned long mstart, mend;
127 level4p = (pgd_t *)__va(start_pgtable);
131 info.direct_gbpages = true;
133 for (i = 0; i < nr_pfn_mapped; i++) {
134 mstart = pfn_mapped[i].start << PAGE_SHIFT;
135 mend = pfn_mapped[i].end << PAGE_SHIFT;
137 result = kernel_ident_mapping_init(&info,
138 level4p, mstart, mend);
144 * segments's mem ranges could be outside 0 ~ max_pfn,
145 * for example when jump back to original kernel from kexeced kernel.
146 * or first kernel is booted with user mem map, and second kernel
147 * could be loaded out of that range.
149 for (i = 0; i < image->nr_segments; i++) {
150 mstart = image->segment[i].mem;
151 mend = mstart + image->segment[i].memsz;
153 result = kernel_ident_mapping_init(&info,
154 level4p, mstart, mend);
160 return init_transition_pgtable(image, level4p);
163 static void set_idt(void *newidt, u16 limit)
165 struct desc_ptr curidt;
167 /* x86-64 supports unaliged loads & stores */
169 curidt.address = (unsigned long)newidt;
171 __asm__ __volatile__ (
178 static void set_gdt(void *newgdt, u16 limit)
180 struct desc_ptr curgdt;
182 /* x86-64 supports unaligned loads & stores */
184 curgdt.address = (unsigned long)newgdt;
186 __asm__ __volatile__ (
192 static void load_segments(void)
194 __asm__ __volatile__ (
200 : : "a" (__KERNEL_DS) : "memory"
204 #ifdef CONFIG_KEXEC_FILE
205 /* Update purgatory as needed after various image segments have been prepared */
206 static int arch_update_purgatory(struct kimage *image)
210 if (!image->file_mode)
213 /* Setup copying of backup region */
214 if (image->type == KEXEC_TYPE_CRASH) {
215 ret = kexec_purgatory_get_set_symbol(image,
216 "purgatory_backup_dest",
217 &image->arch.backup_load_addr,
218 sizeof(image->arch.backup_load_addr), 0);
222 ret = kexec_purgatory_get_set_symbol(image,
223 "purgatory_backup_src",
224 &image->arch.backup_src_start,
225 sizeof(image->arch.backup_src_start), 0);
229 ret = kexec_purgatory_get_set_symbol(image,
230 "purgatory_backup_sz",
231 &image->arch.backup_src_sz,
232 sizeof(image->arch.backup_src_sz), 0);
239 #else /* !CONFIG_KEXEC_FILE */
240 static inline int arch_update_purgatory(struct kimage *image)
244 #endif /* CONFIG_KEXEC_FILE */
246 int machine_kexec_prepare(struct kimage *image)
248 unsigned long start_pgtable;
251 /* Calculate the offsets */
252 start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
254 /* Setup the identity mapped 64bit page table */
255 result = init_pgtable(image, start_pgtable);
259 /* update purgatory as needed */
260 result = arch_update_purgatory(image);
267 void machine_kexec_cleanup(struct kimage *image)
269 free_transition_pgtable(image);
273 * Do not allocate memory (or fail in any way) in machine_kexec().
274 * We are past the point of no return, committed to rebooting now.
276 void machine_kexec(struct kimage *image)
278 unsigned long page_list[PAGES_NR];
280 int save_ftrace_enabled;
282 #ifdef CONFIG_KEXEC_JUMP
283 if (image->preserve_context)
284 save_processor_state();
287 save_ftrace_enabled = __ftrace_enabled_save();
289 /* Interrupts aren't acceptable while we reboot */
291 hw_breakpoint_disable();
293 if (image->preserve_context) {
294 #ifdef CONFIG_X86_IO_APIC
296 * We need to put APICs in legacy mode so that we can
297 * get timer interrupts in second kernel. kexec/kdump
298 * paths already have calls to restore_boot_irq_mode()
299 * in one form or other. kexec jump path also need one.
302 restore_boot_irq_mode();
306 control_page = page_address(image->control_code_page) + PAGE_SIZE;
307 memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
309 page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page);
310 page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
311 page_list[PA_TABLE_PAGE] =
312 (unsigned long)__pa(page_address(image->control_code_page));
314 if (image->type == KEXEC_TYPE_DEFAULT)
315 page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
319 * The segment registers are funny things, they have both a
320 * visible and an invisible part. Whenever the visible part is
321 * set to a specific selector, the invisible part is loaded
322 * with from a table in memory. At no other time is the
323 * descriptor table in memory accessed.
325 * I take advantage of this here by force loading the
326 * segments, before I zap the gdt with an invalid value.
330 * The gdt & idt are now invalid.
331 * If you want to load them you must set up your own idt & gdt.
333 set_gdt(phys_to_virt(0), 0);
334 set_idt(phys_to_virt(0), 0);
337 image->start = relocate_kernel((unsigned long)image->head,
338 (unsigned long)page_list,
340 image->preserve_context,
343 #ifdef CONFIG_KEXEC_JUMP
344 if (image->preserve_context)
345 restore_processor_state();
348 __ftrace_enabled_restore(save_ftrace_enabled);
351 void arch_crash_save_vmcoreinfo(void)
353 u64 sme_mask = sme_me_mask;
355 VMCOREINFO_NUMBER(phys_base);
356 VMCOREINFO_SYMBOL(init_top_pgt);
357 vmcoreinfo_append_str("NUMBER(pgtable_l5_enabled)=%d\n",
358 pgtable_l5_enabled());
361 VMCOREINFO_SYMBOL(node_data);
362 VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
364 vmcoreinfo_append_str("KERNELOFFSET=%lx\n",
366 VMCOREINFO_NUMBER(KERNEL_IMAGE_SIZE);
367 VMCOREINFO_NUMBER(sme_mask);
370 /* arch-dependent functionality related to kexec file-based syscall */
372 #ifdef CONFIG_KEXEC_FILE
373 void *arch_kexec_kernel_image_load(struct kimage *image)
375 vfree(image->arch.elf_headers);
376 image->arch.elf_headers = NULL;
378 if (!image->fops || !image->fops->load)
379 return ERR_PTR(-ENOEXEC);
381 return image->fops->load(image, image->kernel_buf,
382 image->kernel_buf_len, image->initrd_buf,
383 image->initrd_buf_len, image->cmdline_buf,
384 image->cmdline_buf_len);
388 * Apply purgatory relocations.
390 * @pi: Purgatory to be relocated.
391 * @section: Section relocations applying to.
392 * @relsec: Section containing RELAs.
393 * @symtabsec: Corresponding symtab.
395 * TODO: Some of the code belongs to generic code. Move that in kexec.c.
397 int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
398 Elf_Shdr *section, const Elf_Shdr *relsec,
399 const Elf_Shdr *symtabsec)
405 unsigned long address, sec_base, value;
406 const char *strtab, *name, *shstrtab;
407 const Elf_Shdr *sechdrs;
409 /* String & section header string table */
410 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
411 strtab = (char *)pi->ehdr + sechdrs[symtabsec->sh_link].sh_offset;
412 shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset;
414 rel = (void *)pi->ehdr + relsec->sh_offset;
416 pr_debug("Applying relocate section %s to %u\n",
417 shstrtab + relsec->sh_name, relsec->sh_info);
419 for (i = 0; i < relsec->sh_size / sizeof(*rel); i++) {
422 * rel[i].r_offset contains byte offset from beginning
423 * of section to the storage unit affected.
425 * This is location to update. This is temporary buffer
426 * where section is currently loaded. This will finally be
427 * loaded to a different address later, pointed to by
428 * ->sh_addr. kexec takes care of moving it
429 * (kexec_load_segment()).
431 location = pi->purgatory_buf;
432 location += section->sh_offset;
433 location += rel[i].r_offset;
435 /* Final address of the location */
436 address = section->sh_addr + rel[i].r_offset;
439 * rel[i].r_info contains information about symbol table index
440 * w.r.t which relocation must be made and type of relocation
441 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
442 * these respectively.
444 sym = (void *)pi->ehdr + symtabsec->sh_offset;
445 sym += ELF64_R_SYM(rel[i].r_info);
448 name = strtab + sym->st_name;
450 name = shstrtab + sechdrs[sym->st_shndx].sh_name;
452 pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
453 name, sym->st_info, sym->st_shndx, sym->st_value,
456 if (sym->st_shndx == SHN_UNDEF) {
457 pr_err("Undefined symbol: %s\n", name);
461 if (sym->st_shndx == SHN_COMMON) {
462 pr_err("symbol '%s' in common section\n", name);
466 if (sym->st_shndx == SHN_ABS)
468 else if (sym->st_shndx >= pi->ehdr->e_shnum) {
469 pr_err("Invalid section %d for symbol %s\n",
470 sym->st_shndx, name);
473 sec_base = pi->sechdrs[sym->st_shndx].sh_addr;
475 value = sym->st_value;
477 value += rel[i].r_addend;
479 switch (ELF64_R_TYPE(rel[i].r_info)) {
483 *(u64 *)location = value;
486 *(u32 *)location = value;
487 if (value != *(u32 *)location)
491 *(s32 *)location = value;
492 if ((s64)value != *(s32 *)location)
497 value -= (u64)address;
498 *(u32 *)location = value;
501 pr_err("Unknown rela relocation: %llu\n",
502 ELF64_R_TYPE(rel[i].r_info));
509 pr_err("Overflow in relocation type %d value 0x%lx\n",
510 (int)ELF64_R_TYPE(rel[i].r_info), value);
513 #endif /* CONFIG_KEXEC_FILE */
516 kexec_mark_range(unsigned long start, unsigned long end, bool protect)
519 unsigned int nr_pages;
522 * For physical range: [start, end]. We must skip the unassigned
523 * crashk resource with zero-valued "end" member.
525 if (!end || start > end)
528 page = pfn_to_page(start >> PAGE_SHIFT);
529 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
531 return set_pages_ro(page, nr_pages);
533 return set_pages_rw(page, nr_pages);
536 static void kexec_mark_crashkres(bool protect)
538 unsigned long control;
540 kexec_mark_range(crashk_low_res.start, crashk_low_res.end, protect);
542 /* Don't touch the control code page used in crash_kexec().*/
543 control = PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page));
544 /* Control code page is located in the 2nd page. */
545 kexec_mark_range(crashk_res.start, control + PAGE_SIZE - 1, protect);
546 control += KEXEC_CONTROL_PAGE_SIZE;
547 kexec_mark_range(control, crashk_res.end, protect);
550 void arch_kexec_protect_crashkres(void)
552 kexec_mark_crashkres(true);
555 void arch_kexec_unprotect_crashkres(void)
557 kexec_mark_crashkres(false);
560 int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
563 * If SME is active we need to be sure that kexec pages are
564 * not encrypted because when we boot to the new kernel the
565 * pages won't be accessed encrypted (initially).
567 return set_memory_decrypted((unsigned long)vaddr, pages);
570 void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages)
573 * If SME is active we need to reset the pages back to being
574 * an encrypted mapping before freeing them.
576 set_memory_encrypted((unsigned long)vaddr, pages);