machine_kexec_64.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #include <linux/mm.h>
  9. #include <linux/kexec.h>
  10. #include <linux/string.h>
  11. #include <linux/gfp.h>
  12. #include <linux/reboot.h>
  13. #include <linux/numa.h>
  14. #include <linux/ftrace.h>
  15. #include <linux/io.h>
  16. #include <linux/suspend.h>
  17. #include <asm/init.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/mmu_context.h>
  21. #include <asm/debugreg.h>
  22. static void free_transition_pgtable(struct kimage *image)
  23. {
  24. free_page((unsigned long)image->arch.pud);
  25. free_page((unsigned long)image->arch.pmd);
  26. free_page((unsigned long)image->arch.pte);
  27. }
  28. static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
  29. {
  30. pud_t *pud;
  31. pmd_t *pmd;
  32. pte_t *pte;
  33. unsigned long vaddr, paddr;
  34. int result = -ENOMEM;
  35. vaddr = (unsigned long)relocate_kernel;
  36. paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE);
  37. pgd += pgd_index(vaddr);
  38. if (!pgd_present(*pgd)) {
  39. pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
  40. if (!pud)
  41. goto err;
  42. image->arch.pud = pud;
  43. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
  44. }
  45. pud = pud_offset(pgd, vaddr);
  46. if (!pud_present(*pud)) {
  47. pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  48. if (!pmd)
  49. goto err;
  50. image->arch.pmd = pmd;
  51. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  52. }
  53. pmd = pmd_offset(pud, vaddr);
  54. if (!pmd_present(*pmd)) {
  55. pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
  56. if (!pte)
  57. goto err;
  58. image->arch.pte = pte;
  59. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
  60. }
  61. pte = pte_offset_kernel(pmd, vaddr);
  62. set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
  63. return 0;
  64. err:
  65. free_transition_pgtable(image);
  66. return result;
  67. }
  68. static void *alloc_pgt_page(void *data)
  69. {
  70. struct kimage *image = (struct kimage *)data;
  71. struct page *page;
  72. void *p = NULL;
  73. page = kimage_alloc_control_pages(image, 0);
  74. if (page) {
  75. p = page_address(page);
  76. clear_page(p);
  77. }
  78. return p;
  79. }
  80. static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
  81. {
  82. struct x86_mapping_info info = {
  83. .alloc_pgt_page = alloc_pgt_page,
  84. .context = image,
  85. .pmd_flag = __PAGE_KERNEL_LARGE_EXEC,
  86. };
  87. unsigned long mstart, mend;
  88. pgd_t *level4p;
  89. int result;
  90. int i;
  91. level4p = (pgd_t *)__va(start_pgtable);
  92. clear_page(level4p);
  93. for (i = 0; i < nr_pfn_mapped; i++) {
  94. mstart = pfn_mapped[i].start << PAGE_SHIFT;
  95. mend = pfn_mapped[i].end << PAGE_SHIFT;
  96. result = kernel_ident_mapping_init(&info,
  97. level4p, mstart, mend);
  98. if (result)
  99. return result;
  100. }
  101. /*
  102. * segments's mem ranges could be outside 0 ~ max_pfn,
  103. * for example when jump back to original kernel from kexeced kernel.
  104. * or first kernel is booted with user mem map, and second kernel
  105. * could be loaded out of that range.
  106. */
  107. for (i = 0; i < image->nr_segments; i++) {
  108. mstart = image->segment[i].mem;
  109. mend = mstart + image->segment[i].memsz;
  110. result = kernel_ident_mapping_init(&info,
  111. level4p, mstart, mend);
  112. if (result)
  113. return result;
  114. }
  115. return init_transition_pgtable(image, level4p);
  116. }
  117. static void set_idt(void *newidt, u16 limit)
  118. {
  119. struct desc_ptr curidt;
  120. /* x86-64 supports unaliged loads & stores */
  121. curidt.size = limit;
  122. curidt.address = (unsigned long)newidt;
  123. __asm__ __volatile__ (
  124. "lidtq %0\n"
  125. : : "m" (curidt)
  126. );
  127. };
  128. static void set_gdt(void *newgdt, u16 limit)
  129. {
  130. struct desc_ptr curgdt;
  131. /* x86-64 supports unaligned loads & stores */
  132. curgdt.size = limit;
  133. curgdt.address = (unsigned long)newgdt;
  134. __asm__ __volatile__ (
  135. "lgdtq %0\n"
  136. : : "m" (curgdt)
  137. );
  138. };
  139. static void load_segments(void)
  140. {
  141. __asm__ __volatile__ (
  142. "\tmovl %0,%%ds\n"
  143. "\tmovl %0,%%es\n"
  144. "\tmovl %0,%%ss\n"
  145. "\tmovl %0,%%fs\n"
  146. "\tmovl %0,%%gs\n"
  147. : : "a" (__KERNEL_DS) : "memory"
  148. );
  149. }
  150. int machine_kexec_prepare(struct kimage *image)
  151. {
  152. unsigned long start_pgtable;
  153. int result;
  154. /* Calculate the offsets */
  155. start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  156. /* Setup the identity mapped 64bit page table */
  157. result = init_pgtable(image, start_pgtable);
  158. if (result)
  159. return result;
  160. return 0;
  161. }
  162. void machine_kexec_cleanup(struct kimage *image)
  163. {
  164. free_transition_pgtable(image);
  165. }
  166. /*
  167. * Do not allocate memory (or fail in any way) in machine_kexec().
  168. * We are past the point of no return, committed to rebooting now.
  169. */
  170. void machine_kexec(struct kimage *image)
  171. {
  172. unsigned long page_list[PAGES_NR];
  173. void *control_page;
  174. int save_ftrace_enabled;
  175. #ifdef CONFIG_KEXEC_JUMP
  176. if (image->preserve_context)
  177. save_processor_state();
  178. #endif
  179. save_ftrace_enabled = __ftrace_enabled_save();
  180. /* Interrupts aren't acceptable while we reboot */
  181. local_irq_disable();
  182. hw_breakpoint_disable();
  183. if (image->preserve_context) {
  184. #ifdef CONFIG_X86_IO_APIC
  185. /*
  186. * We need to put APICs in legacy mode so that we can
  187. * get timer interrupts in second kernel. kexec/kdump
  188. * paths already have calls to disable_IO_APIC() in
  189. * one form or other. kexec jump path also need
  190. * one.
  191. */
  192. disable_IO_APIC();
  193. #endif
  194. }
  195. control_page = page_address(image->control_code_page) + PAGE_SIZE;
  196. memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
  197. page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page);
  198. page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
  199. page_list[PA_TABLE_PAGE] =
  200. (unsigned long)__pa(page_address(image->control_code_page));
  201. if (image->type == KEXEC_TYPE_DEFAULT)
  202. page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
  203. << PAGE_SHIFT);
  204. /*
  205. * The segment registers are funny things, they have both a
  206. * visible and an invisible part. Whenever the visible part is
  207. * set to a specific selector, the invisible part is loaded
  208. * with from a table in memory. At no other time is the
  209. * descriptor table in memory accessed.
  210. *
  211. * I take advantage of this here by force loading the
  212. * segments, before I zap the gdt with an invalid value.
  213. */
  214. load_segments();
  215. /*
  216. * The gdt & idt are now invalid.
  217. * If you want to load them you must set up your own idt & gdt.
  218. */
  219. set_gdt(phys_to_virt(0), 0);
  220. set_idt(phys_to_virt(0), 0);
  221. /* now call it */
  222. image->start = relocate_kernel((unsigned long)image->head,
  223. (unsigned long)page_list,
  224. image->start,
  225. image->preserve_context);
  226. #ifdef CONFIG_KEXEC_JUMP
  227. if (image->preserve_context)
  228. restore_processor_state();
  229. #endif
  230. __ftrace_enabled_restore(save_ftrace_enabled);
  231. }
  232. void arch_crash_save_vmcoreinfo(void)
  233. {
  234. VMCOREINFO_SYMBOL(phys_base);
  235. VMCOREINFO_SYMBOL(init_level4_pgt);
  236. #ifdef CONFIG_NUMA
  237. VMCOREINFO_SYMBOL(node_data);
  238. VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
  239. #endif
  240. }