machine_kexec_64.c 15 KB

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