machine_kexec_64.c 14 KB

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