1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * handle transition of Linux booting another kernel 4 * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com> 5 */ 6 7 #define pr_fmt(fmt) "kexec: " fmt 8 9 #include <linux/mm.h> 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> 16 #include <linux/io.h> 17 #include <linux/suspend.h> 18 #include <linux/vmalloc.h> 19 #include <linux/efi.h> 20 #include <linux/cc_platform.h> 21 22 #include <asm/init.h> 23 #include <asm/tlbflush.h> 24 #include <asm/mmu_context.h> 25 #include <asm/io_apic.h> 26 #include <asm/debugreg.h> 27 #include <asm/kexec-bzimage64.h> 28 #include <asm/setup.h> 29 #include <asm/set_memory.h> 30 #include <asm/cpu.h> 31 #include <asm/efi.h> 32 33 #ifdef CONFIG_ACPI 34 /* 35 * Used while adding mapping for ACPI tables. 36 * Can be reused when other iomem regions need be mapped 37 */ 38 struct init_pgtable_data { 39 struct x86_mapping_info *info; 40 pgd_t *level4p; 41 }; 42 43 static int mem_region_callback(struct resource *res, void *arg) 44 { 45 struct init_pgtable_data *data = arg; 46 47 return kernel_ident_mapping_init(data->info, data->level4p, 48 res->start, res->end + 1); 49 } 50 51 static int 52 map_acpi_tables(struct x86_mapping_info *info, pgd_t *level4p) 53 { 54 struct init_pgtable_data data; 55 unsigned long flags; 56 int ret; 57 58 data.info = info; 59 data.level4p = level4p; 60 flags = IORESOURCE_MEM | IORESOURCE_BUSY; 61 62 ret = walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1, 63 &data, mem_region_callback); 64 if (ret && ret != -EINVAL) 65 return ret; 66 67 /* ACPI tables could be located in ACPI Non-volatile Storage region */ 68 ret = walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, 69 &data, mem_region_callback); 70 if (ret && ret != -EINVAL) 71 return ret; 72 73 return 0; 74 } 75 #else 76 static int map_acpi_tables(struct x86_mapping_info *info, pgd_t *level4p) { return 0; } 77 #endif 78 79 static int map_mmio_serial(struct x86_mapping_info *info, pgd_t *level4p) 80 { 81 unsigned long mstart, mend; 82 83 if (!kexec_debug_8250_mmio32) 84 return 0; 85 86 mstart = kexec_debug_8250_mmio32 & PAGE_MASK; 87 mend = (kexec_debug_8250_mmio32 + PAGE_SIZE + 23) & PAGE_MASK; 88 pr_info("Map PCI serial at %lx - %lx\n", mstart, mend); 89 return kernel_ident_mapping_init(info, level4p, mstart, mend); 90 } 91 92 #ifdef CONFIG_KEXEC_FILE 93 const struct kexec_file_ops * const kexec_file_loaders[] = { 94 &kexec_bzImage64_ops, 95 NULL 96 }; 97 #endif 98 99 static int 100 map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p) 101 { 102 #ifdef CONFIG_EFI 103 unsigned long mstart, mend; 104 void *kaddr; 105 int ret; 106 107 if (!efi_enabled(EFI_BOOT)) 108 return 0; 109 110 mstart = (boot_params.efi_info.efi_systab | 111 ((u64)boot_params.efi_info.efi_systab_hi<<32)); 112 113 if (efi_enabled(EFI_64BIT)) 114 mend = mstart + sizeof(efi_system_table_64_t); 115 else 116 mend = mstart + sizeof(efi_system_table_32_t); 117 118 if (!mstart) 119 return 0; 120 121 ret = kernel_ident_mapping_init(info, level4p, mstart, mend); 122 if (ret) 123 return ret; 124 125 kaddr = memremap(mstart, mend - mstart, MEMREMAP_WB); 126 if (!kaddr) { 127 pr_err("Could not map UEFI system table\n"); 128 return -ENOMEM; 129 } 130 131 mstart = efi_config_table; 132 133 if (efi_enabled(EFI_64BIT)) { 134 efi_system_table_64_t *stbl = (efi_system_table_64_t *)kaddr; 135 136 mend = mstart + sizeof(efi_config_table_64_t) * stbl->nr_tables; 137 } else { 138 efi_system_table_32_t *stbl = (efi_system_table_32_t *)kaddr; 139 140 mend = mstart + sizeof(efi_config_table_32_t) * stbl->nr_tables; 141 } 142 143 memunmap(kaddr); 144 145 return kernel_ident_mapping_init(info, level4p, mstart, mend); 146 #endif 147 return 0; 148 } 149 150 static void free_transition_pgtable(struct kimage *image) 151 { 152 free_page((unsigned long)image->arch.p4d); 153 image->arch.p4d = NULL; 154 free_page((unsigned long)image->arch.pud); 155 image->arch.pud = NULL; 156 free_page((unsigned long)image->arch.pmd); 157 image->arch.pmd = NULL; 158 free_page((unsigned long)image->arch.pte); 159 image->arch.pte = NULL; 160 } 161 162 static int init_transition_pgtable(struct kimage *image, pgd_t *pgd, 163 unsigned long control_page) 164 { 165 pgprot_t prot = PAGE_KERNEL_EXEC_NOENC; 166 unsigned long vaddr, paddr; 167 int result = -ENOMEM; 168 p4d_t *p4d; 169 pud_t *pud; 170 pmd_t *pmd; 171 pte_t *pte; 172 173 /* 174 * For the transition to the identity mapped page tables, the control 175 * code page also needs to be mapped at the virtual address it starts 176 * off running from. 177 */ 178 vaddr = (unsigned long)__va(control_page); 179 paddr = control_page; 180 pgd += pgd_index(vaddr); 181 if (!pgd_present(*pgd)) { 182 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL); 183 if (!p4d) 184 goto err; 185 image->arch.p4d = p4d; 186 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE)); 187 } 188 p4d = p4d_offset(pgd, vaddr); 189 if (!p4d_present(*p4d)) { 190 pud = (pud_t *)get_zeroed_page(GFP_KERNEL); 191 if (!pud) 192 goto err; 193 image->arch.pud = pud; 194 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE)); 195 } 196 pud = pud_offset(p4d, vaddr); 197 if (!pud_present(*pud)) { 198 pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL); 199 if (!pmd) 200 goto err; 201 image->arch.pmd = pmd; 202 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); 203 } 204 pmd = pmd_offset(pud, vaddr); 205 if (!pmd_present(*pmd)) { 206 pte = (pte_t *)get_zeroed_page(GFP_KERNEL); 207 if (!pte) 208 goto err; 209 image->arch.pte = pte; 210 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE)); 211 } 212 pte = pte_offset_kernel(pmd, vaddr); 213 214 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) 215 prot = PAGE_KERNEL_EXEC; 216 217 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot)); 218 return 0; 219 err: 220 return result; 221 } 222 223 static void *alloc_pgt_page(void *data) 224 { 225 struct kimage *image = (struct kimage *)data; 226 struct page *page; 227 void *p = NULL; 228 229 page = kimage_alloc_control_pages(image, 0); 230 if (page) { 231 p = page_address(page); 232 clear_page(p); 233 } 234 235 return p; 236 } 237 238 static int init_pgtable(struct kimage *image, unsigned long control_page) 239 { 240 struct x86_mapping_info info = { 241 .alloc_pgt_page = alloc_pgt_page, 242 .context = image, 243 .page_flag = __PAGE_KERNEL_LARGE_EXEC, 244 .kernpg_flag = _KERNPG_TABLE_NOENC, 245 }; 246 unsigned long mstart, mend; 247 int result; 248 int i; 249 250 image->arch.pgd = alloc_pgt_page(image); 251 if (!image->arch.pgd) 252 return -ENOMEM; 253 254 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { 255 info.page_flag |= _PAGE_ENC; 256 info.kernpg_flag |= _PAGE_ENC; 257 } 258 259 if (direct_gbpages) 260 info.direct_gbpages = true; 261 262 for (i = 0; i < nr_pfn_mapped; i++) { 263 mstart = pfn_mapped[i].start << PAGE_SHIFT; 264 mend = pfn_mapped[i].end << PAGE_SHIFT; 265 266 result = kernel_ident_mapping_init(&info, image->arch.pgd, 267 mstart, mend); 268 if (result) 269 return result; 270 } 271 272 /* 273 * segments's mem ranges could be outside 0 ~ max_pfn, 274 * for example when jump back to original kernel from kexeced kernel. 275 * or first kernel is booted with user mem map, and second kernel 276 * could be loaded out of that range. 277 */ 278 for (i = 0; i < image->nr_segments; i++) { 279 mstart = image->segment[i].mem; 280 mend = mstart + image->segment[i].memsz; 281 282 result = kernel_ident_mapping_init(&info, image->arch.pgd, 283 mstart, mend); 284 285 if (result) 286 return result; 287 } 288 289 /* 290 * Prepare EFI systab and ACPI tables for kexec kernel since they are 291 * not covered by pfn_mapped. 292 */ 293 result = map_efi_systab(&info, image->arch.pgd); 294 if (result) 295 return result; 296 297 result = map_acpi_tables(&info, image->arch.pgd); 298 if (result) 299 return result; 300 301 result = map_mmio_serial(&info, image->arch.pgd); 302 if (result) 303 return result; 304 305 /* 306 * This must be last because the intermediate page table pages it 307 * allocates will not be control pages and may overlap the image. 308 */ 309 return init_transition_pgtable(image, image->arch.pgd, control_page); 310 } 311 312 static void load_segments(void) 313 { 314 __asm__ __volatile__ ( 315 "\tmovl %0,%%ds\n" 316 "\tmovl %0,%%es\n" 317 "\tmovl %0,%%ss\n" 318 "\tmovl %0,%%fs\n" 319 "\tmovl %0,%%gs\n" 320 : : "a" (__KERNEL_DS) : "memory" 321 ); 322 } 323 324 static void prepare_debug_idt(unsigned long control_page, unsigned long vec_ofs) 325 { 326 gate_desc idtentry = { 0 }; 327 int i; 328 329 idtentry.bits.p = 1; 330 idtentry.bits.type = GATE_TRAP; 331 idtentry.segment = __KERNEL_CS; 332 idtentry.offset_low = (control_page & 0xFFFF) + vec_ofs; 333 idtentry.offset_middle = (control_page >> 16) & 0xFFFF; 334 idtentry.offset_high = control_page >> 32; 335 336 for (i = 0; i < 16; i++) { 337 kexec_debug_idt[i] = idtentry; 338 idtentry.offset_low += KEXEC_DEBUG_EXC_HANDLER_SIZE; 339 } 340 } 341 342 int machine_kexec_prepare(struct kimage *image) 343 { 344 void *control_page = page_address(image->control_code_page); 345 unsigned long reloc_start = (unsigned long)__relocate_kernel_start; 346 unsigned long reloc_end = (unsigned long)__relocate_kernel_end; 347 int result; 348 349 /* Setup the identity mapped 64bit page table */ 350 result = init_pgtable(image, __pa(control_page)); 351 if (result) 352 return result; 353 kexec_va_control_page = (unsigned long)control_page; 354 kexec_pa_table_page = (unsigned long)__pa(image->arch.pgd); 355 356 if (image->type == KEXEC_TYPE_DEFAULT) 357 kexec_pa_swap_page = page_to_pfn(image->swap_page) << PAGE_SHIFT; 358 359 prepare_debug_idt((unsigned long)__pa(control_page), 360 (unsigned long)kexec_debug_exc_vectors - reloc_start); 361 362 __memcpy(control_page, __relocate_kernel_start, reloc_end - reloc_start); 363 364 set_memory_rox((unsigned long)control_page, 1); 365 366 return 0; 367 } 368 369 void machine_kexec_cleanup(struct kimage *image) 370 { 371 void *control_page = page_address(image->control_code_page); 372 373 set_memory_nx((unsigned long)control_page, 1); 374 set_memory_rw((unsigned long)control_page, 1); 375 376 free_transition_pgtable(image); 377 } 378 379 /* 380 * Do not allocate memory (or fail in any way) in machine_kexec(). 381 * We are past the point of no return, committed to rebooting now. 382 */ 383 void __nocfi machine_kexec(struct kimage *image) 384 { 385 unsigned long reloc_start = (unsigned long)__relocate_kernel_start; 386 relocate_kernel_fn *relocate_kernel_ptr; 387 unsigned int host_mem_enc_active; 388 int save_ftrace_enabled; 389 void *control_page; 390 391 /* 392 * This must be done before load_segments() since if call depth tracking 393 * is used then GS must be valid to make any function calls. 394 */ 395 host_mem_enc_active = cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT); 396 397 #ifdef CONFIG_KEXEC_JUMP 398 if (image->preserve_context) 399 save_processor_state(); 400 #endif 401 402 save_ftrace_enabled = __ftrace_enabled_save(); 403 404 /* Interrupts aren't acceptable while we reboot */ 405 local_irq_disable(); 406 hw_breakpoint_disable(); 407 cet_disable(); 408 409 if (image->preserve_context) { 410 #ifdef CONFIG_X86_IO_APIC 411 /* 412 * We need to put APICs in legacy mode so that we can 413 * get timer interrupts in second kernel. kexec/kdump 414 * paths already have calls to restore_boot_irq_mode() 415 * in one form or other. kexec jump path also need one. 416 */ 417 clear_IO_APIC(); 418 restore_boot_irq_mode(); 419 #endif 420 } 421 422 control_page = page_address(image->control_code_page); 423 424 /* 425 * Allow for the possibility that relocate_kernel might not be at 426 * the very start of the page. 427 */ 428 relocate_kernel_ptr = control_page + (unsigned long)relocate_kernel - reloc_start; 429 430 /* 431 * The segment registers are funny things, they have both a 432 * visible and an invisible part. Whenever the visible part is 433 * set to a specific selector, the invisible part is loaded 434 * with from a table in memory. At no other time is the 435 * descriptor table in memory accessed. 436 * 437 * Take advantage of this here by force loading the segments, 438 * before the GDT is zapped with an invalid value. 439 */ 440 load_segments(); 441 442 /* now call it */ 443 image->start = relocate_kernel_ptr((unsigned long)image->head, 444 virt_to_phys(control_page), 445 image->start, 446 image->preserve_context, 447 host_mem_enc_active); 448 449 #ifdef CONFIG_KEXEC_JUMP 450 if (image->preserve_context) 451 restore_processor_state(); 452 #endif 453 454 __ftrace_enabled_restore(save_ftrace_enabled); 455 } 456 457 /* arch-dependent functionality related to kexec file-based syscall */ 458 459 #ifdef CONFIG_KEXEC_FILE 460 /* 461 * Apply purgatory relocations. 462 * 463 * @pi: Purgatory to be relocated. 464 * @section: Section relocations applying to. 465 * @relsec: Section containing RELAs. 466 * @symtabsec: Corresponding symtab. 467 * 468 * TODO: Some of the code belongs to generic code. Move that in kexec.c. 469 */ 470 int arch_kexec_apply_relocations_add(struct purgatory_info *pi, 471 Elf_Shdr *section, const Elf_Shdr *relsec, 472 const Elf_Shdr *symtabsec) 473 { 474 unsigned int i; 475 Elf64_Rela *rel; 476 Elf64_Sym *sym; 477 void *location; 478 unsigned long address, sec_base, value; 479 const char *strtab, *name, *shstrtab; 480 const Elf_Shdr *sechdrs; 481 482 /* String & section header string table */ 483 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff; 484 strtab = (char *)pi->ehdr + sechdrs[symtabsec->sh_link].sh_offset; 485 shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset; 486 487 rel = (void *)pi->ehdr + relsec->sh_offset; 488 489 pr_debug("Applying relocate section %s to %u\n", 490 shstrtab + relsec->sh_name, relsec->sh_info); 491 492 for (i = 0; i < relsec->sh_size / sizeof(*rel); i++) { 493 494 /* 495 * rel[i].r_offset contains byte offset from beginning 496 * of section to the storage unit affected. 497 * 498 * This is location to update. This is temporary buffer 499 * where section is currently loaded. This will finally be 500 * loaded to a different address later, pointed to by 501 * ->sh_addr. kexec takes care of moving it 502 * (kexec_load_segment()). 503 */ 504 location = pi->purgatory_buf; 505 location += section->sh_offset; 506 location += rel[i].r_offset; 507 508 /* Final address of the location */ 509 address = section->sh_addr + rel[i].r_offset; 510 511 /* 512 * rel[i].r_info contains information about symbol table index 513 * w.r.t which relocation must be made and type of relocation 514 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get 515 * these respectively. 516 */ 517 sym = (void *)pi->ehdr + symtabsec->sh_offset; 518 sym += ELF64_R_SYM(rel[i].r_info); 519 520 if (sym->st_name) 521 name = strtab + sym->st_name; 522 else 523 name = shstrtab + sechdrs[sym->st_shndx].sh_name; 524 525 pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n", 526 name, sym->st_info, sym->st_shndx, sym->st_value, 527 sym->st_size); 528 529 if (sym->st_shndx == SHN_UNDEF) { 530 pr_err("Undefined symbol: %s\n", name); 531 return -ENOEXEC; 532 } 533 534 if (sym->st_shndx == SHN_COMMON) { 535 pr_err("symbol '%s' in common section\n", name); 536 return -ENOEXEC; 537 } 538 539 if (sym->st_shndx == SHN_ABS) 540 sec_base = 0; 541 else if (sym->st_shndx >= pi->ehdr->e_shnum) { 542 pr_err("Invalid section %d for symbol %s\n", 543 sym->st_shndx, name); 544 return -ENOEXEC; 545 } else 546 sec_base = pi->sechdrs[sym->st_shndx].sh_addr; 547 548 value = sym->st_value; 549 value += sec_base; 550 value += rel[i].r_addend; 551 552 switch (ELF64_R_TYPE(rel[i].r_info)) { 553 case R_X86_64_NONE: 554 break; 555 case R_X86_64_64: 556 *(u64 *)location = value; 557 break; 558 case R_X86_64_32: 559 *(u32 *)location = value; 560 if (value != *(u32 *)location) 561 goto overflow; 562 break; 563 case R_X86_64_32S: 564 *(s32 *)location = value; 565 if ((s64)value != *(s32 *)location) 566 goto overflow; 567 break; 568 case R_X86_64_PC32: 569 case R_X86_64_PLT32: 570 value -= (u64)address; 571 *(u32 *)location = value; 572 break; 573 default: 574 pr_err("Unknown rela relocation: %llu\n", 575 ELF64_R_TYPE(rel[i].r_info)); 576 return -ENOEXEC; 577 } 578 } 579 return 0; 580 581 overflow: 582 pr_err("Overflow in relocation type %d value 0x%lx\n", 583 (int)ELF64_R_TYPE(rel[i].r_info), value); 584 return -ENOEXEC; 585 } 586 587 int arch_kimage_file_post_load_cleanup(struct kimage *image) 588 { 589 vfree(image->elf_headers); 590 image->elf_headers = NULL; 591 image->elf_headers_sz = 0; 592 593 return kexec_image_post_load_cleanup_default(image); 594 } 595 #endif /* CONFIG_KEXEC_FILE */ 596 597 #ifdef CONFIG_CRASH_DUMP 598 599 static int 600 kexec_mark_range(unsigned long start, unsigned long end, bool protect) 601 { 602 struct page *page; 603 unsigned int nr_pages; 604 605 /* 606 * For physical range: [start, end]. We must skip the unassigned 607 * crashk resource with zero-valued "end" member. 608 */ 609 if (!end || start > end) 610 return 0; 611 612 page = pfn_to_page(start >> PAGE_SHIFT); 613 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; 614 if (protect) 615 return set_pages_ro(page, nr_pages); 616 else 617 return set_pages_rw(page, nr_pages); 618 } 619 620 static void kexec_mark_crashkres(bool protect) 621 { 622 unsigned long control; 623 624 kexec_mark_range(crashk_low_res.start, crashk_low_res.end, protect); 625 626 /* Don't touch the control code page used in crash_kexec().*/ 627 control = PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page)); 628 kexec_mark_range(crashk_res.start, control - 1, protect); 629 control += KEXEC_CONTROL_PAGE_SIZE; 630 kexec_mark_range(control, crashk_res.end, protect); 631 } 632 633 /* make the memory storing dm crypt keys in/accessible */ 634 static void kexec_mark_dm_crypt_keys(bool protect) 635 { 636 unsigned long start_paddr, end_paddr; 637 unsigned int nr_pages; 638 639 if (kexec_crash_image->dm_crypt_keys_addr) { 640 start_paddr = kexec_crash_image->dm_crypt_keys_addr; 641 end_paddr = start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1; 642 nr_pages = (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE_SIZE; 643 if (protect) 644 set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages); 645 else 646 __set_memory_prot( 647 (unsigned long)phys_to_virt(start_paddr), 648 nr_pages, 649 __pgprot(_PAGE_PRESENT | _PAGE_NX | _PAGE_RW)); 650 } 651 } 652 653 void arch_kexec_protect_crashkres(void) 654 { 655 kexec_mark_crashkres(true); 656 kexec_mark_dm_crypt_keys(true); 657 } 658 659 void arch_kexec_unprotect_crashkres(void) 660 { 661 kexec_mark_dm_crypt_keys(false); 662 kexec_mark_crashkres(false); 663 } 664 #endif 665 666 /* 667 * During a traditional boot under SME, SME will encrypt the kernel, 668 * so the SME kexec kernel also needs to be un-encrypted in order to 669 * replicate a normal SME boot. 670 * 671 * During a traditional boot under SEV, the kernel has already been 672 * loaded encrypted, so the SEV kexec kernel needs to be encrypted in 673 * order to replicate a normal SEV boot. 674 */ 675 int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) 676 { 677 if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) 678 return 0; 679 680 /* 681 * If host memory encryption is active we need to be sure that kexec 682 * pages are not encrypted because when we boot to the new kernel the 683 * pages won't be accessed encrypted (initially). 684 */ 685 return set_memory_decrypted((unsigned long)vaddr, pages); 686 } 687 688 void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) 689 { 690 if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) 691 return; 692 693 /* 694 * If host memory encryption is active we need to reset the pages back 695 * to being an encrypted mapping before freeing them. 696 */ 697 set_memory_encrypted((unsigned long)vaddr, pages); 698 } 699