machine_kexec_64.c 14 KB

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