machine_kexec_64.c 13 KB

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