machine_kexec_64.c 13 KB

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