machine_kexec_64.c 15 KB

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