machine_kexec_64.c 13 KB

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