machine_kexec_64.c 15 KB

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