machine_kexec_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. #define pr_fmt(fmt) "kexec: " fmt
  7. #include <linux/mm.h>
  8. #include <linux/kexec.h>
  9. #include <linux/string.h>
  10. #include <linux/gfp.h>
  11. #include <linux/reboot.h>
  12. #include <linux/numa.h>
  13. #include <linux/ftrace.h>
  14. #include <linux/io.h>
  15. #include <linux/suspend.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/efi.h>
  18. #include <linux/cc_platform.h>
  19. #include <asm/init.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/io_apic.h>
  23. #include <asm/debugreg.h>
  24. #include <asm/kexec-bzimage64.h>
  25. #include <asm/setup.h>
  26. #include <asm/set_memory.h>
  27. #include <asm/cpu.h>
  28. #include <asm/efi.h>
  29. #ifdef CONFIG_ACPI
  30. /*
  31. * Used while adding mapping for ACPI tables.
  32. * Can be reused when other iomem regions need be mapped
  33. */
  34. struct init_pgtable_data {
  35. struct x86_mapping_info *info;
  36. pgd_t *level4p;
  37. };
  38. static int mem_region_callback(struct resource *res, void *arg)
  39. {
  40. struct init_pgtable_data *data = arg;
  41. return kernel_ident_mapping_init(data->info, data->level4p,
  42. res->start, res->end + 1);
  43. }
  44. static int
  45. map_acpi_tables(struct x86_mapping_info *info, pgd_t *level4p)
  46. {
  47. struct init_pgtable_data data;
  48. unsigned long flags;
  49. int ret;
  50. data.info = info;
  51. data.level4p = level4p;
  52. flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  53. ret = walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1,
  54. &data, mem_region_callback);
  55. if (ret && ret != -EINVAL)
  56. return ret;
  57. /* ACPI tables could be located in ACPI Non-volatile Storage region */
  58. ret = walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1,
  59. &data, mem_region_callback);
  60. if (ret && ret != -EINVAL)
  61. return ret;
  62. return 0;
  63. }
  64. #else
  65. static int map_acpi_tables(struct x86_mapping_info *info, pgd_t *level4p) { return 0; }
  66. #endif
  67. #ifdef CONFIG_KEXEC_FILE
  68. const struct kexec_file_ops * const kexec_file_loaders[] = {
  69. &kexec_bzImage64_ops,
  70. NULL
  71. };
  72. #endif
  73. static int
  74. map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
  75. {
  76. #ifdef CONFIG_EFI
  77. unsigned long mstart, mend;
  78. void *kaddr;
  79. int ret;
  80. if (!efi_enabled(EFI_BOOT))
  81. return 0;
  82. mstart = (boot_params.efi_info.efi_systab |
  83. ((u64)boot_params.efi_info.efi_systab_hi<<32));
  84. if (efi_enabled(EFI_64BIT))
  85. mend = mstart + sizeof(efi_system_table_64_t);
  86. else
  87. mend = mstart + sizeof(efi_system_table_32_t);
  88. if (!mstart)
  89. return 0;
  90. ret = kernel_ident_mapping_init(info, level4p, mstart, mend);
  91. if (ret)
  92. return ret;
  93. kaddr = memremap(mstart, mend - mstart, MEMREMAP_WB);
  94. if (!kaddr) {
  95. pr_err("Could not map UEFI system table\n");
  96. return -ENOMEM;
  97. }
  98. mstart = efi_config_table;
  99. if (efi_enabled(EFI_64BIT)) {
  100. efi_system_table_64_t *stbl = (efi_system_table_64_t *)kaddr;
  101. mend = mstart + sizeof(efi_config_table_64_t) * stbl->nr_tables;
  102. } else {
  103. efi_system_table_32_t *stbl = (efi_system_table_32_t *)kaddr;
  104. mend = mstart + sizeof(efi_config_table_32_t) * stbl->nr_tables;
  105. }
  106. memunmap(kaddr);
  107. return kernel_ident_mapping_init(info, level4p, mstart, mend);
  108. #endif
  109. return 0;
  110. }
  111. static void free_transition_pgtable(struct kimage *image)
  112. {
  113. free_page((unsigned long)image->arch.p4d);
  114. image->arch.p4d = NULL;
  115. free_page((unsigned long)image->arch.pud);
  116. image->arch.pud = NULL;
  117. free_page((unsigned long)image->arch.pmd);
  118. image->arch.pmd = NULL;
  119. free_page((unsigned long)image->arch.pte);
  120. image->arch.pte = NULL;
  121. }
  122. static int init_transition_pgtable(struct kimage *image, pgd_t *pgd,
  123. unsigned long control_page)
  124. {
  125. pgprot_t prot = PAGE_KERNEL_EXEC_NOENC;
  126. unsigned long vaddr, paddr;
  127. int result = -ENOMEM;
  128. p4d_t *p4d;
  129. pud_t *pud;
  130. pmd_t *pmd;
  131. pte_t *pte;
  132. vaddr = (unsigned long)relocate_kernel;
  133. paddr = control_page;
  134. pgd += pgd_index(vaddr);
  135. if (!pgd_present(*pgd)) {
  136. p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
  137. if (!p4d)
  138. goto err;
  139. image->arch.p4d = p4d;
  140. set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE));
  141. }
  142. p4d = p4d_offset(pgd, vaddr);
  143. if (!p4d_present(*p4d)) {
  144. pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
  145. if (!pud)
  146. goto err;
  147. image->arch.pud = pud;
  148. set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
  149. }
  150. pud = pud_offset(p4d, vaddr);
  151. if (!pud_present(*pud)) {
  152. pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  153. if (!pmd)
  154. goto err;
  155. image->arch.pmd = pmd;
  156. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  157. }
  158. pmd = pmd_offset(pud, vaddr);
  159. if (!pmd_present(*pmd)) {
  160. pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
  161. if (!pte)
  162. goto err;
  163. image->arch.pte = pte;
  164. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
  165. }
  166. pte = pte_offset_kernel(pmd, vaddr);
  167. if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
  168. prot = PAGE_KERNEL_EXEC;
  169. set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
  170. return 0;
  171. err:
  172. return result;
  173. }
  174. static void *alloc_pgt_page(void *data)
  175. {
  176. struct kimage *image = (struct kimage *)data;
  177. struct page *page;
  178. void *p = NULL;
  179. page = kimage_alloc_control_pages(image, 0);
  180. if (page) {
  181. p = page_address(page);
  182. clear_page(p);
  183. }
  184. return p;
  185. }
  186. static int init_pgtable(struct kimage *image, unsigned long control_page)
  187. {
  188. struct x86_mapping_info info = {
  189. .alloc_pgt_page = alloc_pgt_page,
  190. .context = image,
  191. .page_flag = __PAGE_KERNEL_LARGE_EXEC,
  192. .kernpg_flag = _KERNPG_TABLE_NOENC,
  193. };
  194. unsigned long mstart, mend;
  195. int result;
  196. int i;
  197. image->arch.pgd = alloc_pgt_page(image);
  198. if (!image->arch.pgd)
  199. return -ENOMEM;
  200. if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
  201. info.page_flag |= _PAGE_ENC;
  202. info.kernpg_flag |= _PAGE_ENC;
  203. }
  204. if (direct_gbpages)
  205. info.direct_gbpages = true;
  206. for (i = 0; i < nr_pfn_mapped; i++) {
  207. mstart = pfn_mapped[i].start << PAGE_SHIFT;
  208. mend = pfn_mapped[i].end << PAGE_SHIFT;
  209. result = kernel_ident_mapping_init(&info, image->arch.pgd,
  210. mstart, mend);
  211. if (result)
  212. return result;
  213. }
  214. /*
  215. * segments's mem ranges could be outside 0 ~ max_pfn,
  216. * for example when jump back to original kernel from kexeced kernel.
  217. * or first kernel is booted with user mem map, and second kernel
  218. * could be loaded out of that range.
  219. */
  220. for (i = 0; i < image->nr_segments; i++) {
  221. mstart = image->segment[i].mem;
  222. mend = mstart + image->segment[i].memsz;
  223. result = kernel_ident_mapping_init(&info, image->arch.pgd,
  224. mstart, mend);
  225. if (result)
  226. return result;
  227. }
  228. /*
  229. * Prepare EFI systab and ACPI tables for kexec kernel since they are
  230. * not covered by pfn_mapped.
  231. */
  232. result = map_efi_systab(&info, image->arch.pgd);
  233. if (result)
  234. return result;
  235. result = map_acpi_tables(&info, image->arch.pgd);
  236. if (result)
  237. return result;
  238. /*
  239. * This must be last because the intermediate page table pages it
  240. * allocates will not be control pages and may overlap the image.
  241. */
  242. return init_transition_pgtable(image, image->arch.pgd, control_page);
  243. }
  244. static void load_segments(void)
  245. {
  246. __asm__ __volatile__ (
  247. "\tmovl %0,%%ds\n"
  248. "\tmovl %0,%%es\n"
  249. "\tmovl %0,%%ss\n"
  250. "\tmovl %0,%%fs\n"
  251. "\tmovl %0,%%gs\n"
  252. : : "a" (__KERNEL_DS) : "memory"
  253. );
  254. }
  255. int machine_kexec_prepare(struct kimage *image)
  256. {
  257. unsigned long control_page;
  258. int result;
  259. /* Calculate the offsets */
  260. control_page = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  261. /* Setup the identity mapped 64bit page table */
  262. result = init_pgtable(image, control_page);
  263. if (result)
  264. return result;
  265. return 0;
  266. }
  267. void machine_kexec_cleanup(struct kimage *image)
  268. {
  269. free_transition_pgtable(image);
  270. }
  271. /*
  272. * Do not allocate memory (or fail in any way) in machine_kexec().
  273. * We are past the point of no return, committed to rebooting now.
  274. */
  275. void machine_kexec(struct kimage *image)
  276. {
  277. unsigned long page_list[PAGES_NR];
  278. unsigned int host_mem_enc_active;
  279. int save_ftrace_enabled;
  280. void *control_page;
  281. /*
  282. * This must be done before load_segments() since if call depth tracking
  283. * is used then GS must be valid to make any function calls.
  284. */
  285. host_mem_enc_active = cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT);
  286. #ifdef CONFIG_KEXEC_JUMP
  287. if (image->preserve_context)
  288. save_processor_state();
  289. #endif
  290. save_ftrace_enabled = __ftrace_enabled_save();
  291. /* Interrupts aren't acceptable while we reboot */
  292. local_irq_disable();
  293. hw_breakpoint_disable();
  294. cet_disable();
  295. if (image->preserve_context) {
  296. #ifdef CONFIG_X86_IO_APIC
  297. /*
  298. * We need to put APICs in legacy mode so that we can
  299. * get timer interrupts in second kernel. kexec/kdump
  300. * paths already have calls to restore_boot_irq_mode()
  301. * in one form or other. kexec jump path also need one.
  302. */
  303. clear_IO_APIC();
  304. restore_boot_irq_mode();
  305. #endif
  306. }
  307. control_page = page_address(image->control_code_page);
  308. __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] = (unsigned long)__pa(image->arch.pgd);
  312. if (image->type == KEXEC_TYPE_DEFAULT)
  313. page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
  314. << PAGE_SHIFT);
  315. /*
  316. * The segment registers are funny things, they have both a
  317. * visible and an invisible part. Whenever the visible part is
  318. * set to a specific selector, the invisible part is loaded
  319. * with from a table in memory. At no other time is the
  320. * descriptor table in memory accessed.
  321. *
  322. * I take advantage of this here by force loading the
  323. * segments, before I zap the gdt with an invalid value.
  324. */
  325. load_segments();
  326. /*
  327. * The gdt & idt are now invalid.
  328. * If you want to load them you must set up your own idt & gdt.
  329. */
  330. native_idt_invalidate();
  331. native_gdt_invalidate();
  332. /* now call it */
  333. image->start = relocate_kernel((unsigned long)image->head,
  334. (unsigned long)page_list,
  335. image->start,
  336. image->preserve_context,
  337. host_mem_enc_active);
  338. #ifdef CONFIG_KEXEC_JUMP
  339. if (image->preserve_context)
  340. restore_processor_state();
  341. #endif
  342. __ftrace_enabled_restore(save_ftrace_enabled);
  343. }
  344. /* arch-dependent functionality related to kexec file-based syscall */
  345. #ifdef CONFIG_KEXEC_FILE
  346. /*
  347. * Apply purgatory relocations.
  348. *
  349. * @pi: Purgatory to be relocated.
  350. * @section: Section relocations applying to.
  351. * @relsec: Section containing RELAs.
  352. * @symtabsec: Corresponding symtab.
  353. *
  354. * TODO: Some of the code belongs to generic code. Move that in kexec.c.
  355. */
  356. int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
  357. Elf_Shdr *section, const Elf_Shdr *relsec,
  358. const Elf_Shdr *symtabsec)
  359. {
  360. unsigned int i;
  361. Elf64_Rela *rel;
  362. Elf64_Sym *sym;
  363. void *location;
  364. unsigned long address, sec_base, value;
  365. const char *strtab, *name, *shstrtab;
  366. const Elf_Shdr *sechdrs;
  367. /* String & section header string table */
  368. sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
  369. strtab = (char *)pi->ehdr + sechdrs[symtabsec->sh_link].sh_offset;
  370. shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset;
  371. rel = (void *)pi->ehdr + relsec->sh_offset;
  372. pr_debug("Applying relocate section %s to %u\n",
  373. shstrtab + relsec->sh_name, relsec->sh_info);
  374. for (i = 0; i < relsec->sh_size / sizeof(*rel); i++) {
  375. /*
  376. * rel[i].r_offset contains byte offset from beginning
  377. * of section to the storage unit affected.
  378. *
  379. * This is location to update. This is temporary buffer
  380. * where section is currently loaded. This will finally be
  381. * loaded to a different address later, pointed to by
  382. * ->sh_addr. kexec takes care of moving it
  383. * (kexec_load_segment()).
  384. */
  385. location = pi->purgatory_buf;
  386. location += section->sh_offset;
  387. location += rel[i].r_offset;
  388. /* Final address of the location */
  389. address = section->sh_addr + rel[i].r_offset;
  390. /*
  391. * rel[i].r_info contains information about symbol table index
  392. * w.r.t which relocation must be made and type of relocation
  393. * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
  394. * these respectively.
  395. */
  396. sym = (void *)pi->ehdr + symtabsec->sh_offset;
  397. sym += ELF64_R_SYM(rel[i].r_info);
  398. if (sym->st_name)
  399. name = strtab + sym->st_name;
  400. else
  401. name = shstrtab + sechdrs[sym->st_shndx].sh_name;
  402. pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
  403. name, sym->st_info, sym->st_shndx, sym->st_value,
  404. sym->st_size);
  405. if (sym->st_shndx == SHN_UNDEF) {
  406. pr_err("Undefined symbol: %s\n", name);
  407. return -ENOEXEC;
  408. }
  409. if (sym->st_shndx == SHN_COMMON) {
  410. pr_err("symbol '%s' in common section\n", name);
  411. return -ENOEXEC;
  412. }
  413. if (sym->st_shndx == SHN_ABS)
  414. sec_base = 0;
  415. else if (sym->st_shndx >= pi->ehdr->e_shnum) {
  416. pr_err("Invalid section %d for symbol %s\n",
  417. sym->st_shndx, name);
  418. return -ENOEXEC;
  419. } else
  420. sec_base = pi->sechdrs[sym->st_shndx].sh_addr;
  421. value = sym->st_value;
  422. value += sec_base;
  423. value += rel[i].r_addend;
  424. switch (ELF64_R_TYPE(rel[i].r_info)) {
  425. case R_X86_64_NONE:
  426. break;
  427. case R_X86_64_64:
  428. *(u64 *)location = value;
  429. break;
  430. case R_X86_64_32:
  431. *(u32 *)location = value;
  432. if (value != *(u32 *)location)
  433. goto overflow;
  434. break;
  435. case R_X86_64_32S:
  436. *(s32 *)location = value;
  437. if ((s64)value != *(s32 *)location)
  438. goto overflow;
  439. break;
  440. case R_X86_64_PC32:
  441. case R_X86_64_PLT32:
  442. value -= (u64)address;
  443. *(u32 *)location = value;
  444. break;
  445. default:
  446. pr_err("Unknown rela relocation: %llu\n",
  447. ELF64_R_TYPE(rel[i].r_info));
  448. return -ENOEXEC;
  449. }
  450. }
  451. return 0;
  452. overflow:
  453. pr_err("Overflow in relocation type %d value 0x%lx\n",
  454. (int)ELF64_R_TYPE(rel[i].r_info), value);
  455. return -ENOEXEC;
  456. }
  457. int arch_kimage_file_post_load_cleanup(struct kimage *image)
  458. {
  459. vfree(image->elf_headers);
  460. image->elf_headers = NULL;
  461. image->elf_headers_sz = 0;
  462. return kexec_image_post_load_cleanup_default(image);
  463. }
  464. #endif /* CONFIG_KEXEC_FILE */
  465. #ifdef CONFIG_CRASH_DUMP
  466. static int
  467. kexec_mark_range(unsigned long start, unsigned long end, bool protect)
  468. {
  469. struct page *page;
  470. unsigned int nr_pages;
  471. /*
  472. * For physical range: [start, end]. We must skip the unassigned
  473. * crashk resource with zero-valued "end" member.
  474. */
  475. if (!end || start > end)
  476. return 0;
  477. page = pfn_to_page(start >> PAGE_SHIFT);
  478. nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
  479. if (protect)
  480. return set_pages_ro(page, nr_pages);
  481. else
  482. return set_pages_rw(page, nr_pages);
  483. }
  484. static void kexec_mark_crashkres(bool protect)
  485. {
  486. unsigned long control;
  487. kexec_mark_range(crashk_low_res.start, crashk_low_res.end, protect);
  488. /* Don't touch the control code page used in crash_kexec().*/
  489. control = PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page));
  490. kexec_mark_range(crashk_res.start, control - 1, protect);
  491. control += KEXEC_CONTROL_PAGE_SIZE;
  492. kexec_mark_range(control, crashk_res.end, protect);
  493. }
  494. void arch_kexec_protect_crashkres(void)
  495. {
  496. kexec_mark_crashkres(true);
  497. }
  498. void arch_kexec_unprotect_crashkres(void)
  499. {
  500. kexec_mark_crashkres(false);
  501. }
  502. #endif
  503. /*
  504. * During a traditional boot under SME, SME will encrypt the kernel,
  505. * so the SME kexec kernel also needs to be un-encrypted in order to
  506. * replicate a normal SME boot.
  507. *
  508. * During a traditional boot under SEV, the kernel has already been
  509. * loaded encrypted, so the SEV kexec kernel needs to be encrypted in
  510. * order to replicate a normal SEV boot.
  511. */
  512. int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
  513. {
  514. if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  515. return 0;
  516. /*
  517. * If host memory encryption is active we need to be sure that kexec
  518. * pages are not encrypted because when we boot to the new kernel the
  519. * pages won't be accessed encrypted (initially).
  520. */
  521. return set_memory_decrypted((unsigned long)vaddr, pages);
  522. }
  523. void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages)
  524. {
  525. if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  526. return;
  527. /*
  528. * If host memory encryption is active we need to reset the pages back
  529. * to being an encrypted mapping before freeing them.
  530. */
  531. set_memory_encrypted((unsigned long)vaddr, pages);
  532. }