machine_kexec_64.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/pgtable.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/debugreg.h>
  21. static int init_one_level2_page(struct kimage *image, pgd_t *pgd,
  22. unsigned long addr)
  23. {
  24. pud_t *pud;
  25. pmd_t *pmd;
  26. struct page *page;
  27. int result = -ENOMEM;
  28. addr &= PMD_MASK;
  29. pgd += pgd_index(addr);
  30. if (!pgd_present(*pgd)) {
  31. page = kimage_alloc_control_pages(image, 0);
  32. if (!page)
  33. goto out;
  34. pud = (pud_t *)page_address(page);
  35. clear_page(pud);
  36. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
  37. }
  38. pud = pud_offset(pgd, addr);
  39. if (!pud_present(*pud)) {
  40. page = kimage_alloc_control_pages(image, 0);
  41. if (!page)
  42. goto out;
  43. pmd = (pmd_t *)page_address(page);
  44. clear_page(pmd);
  45. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  46. }
  47. pmd = pmd_offset(pud, addr);
  48. if (!pmd_present(*pmd))
  49. set_pmd(pmd, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
  50. result = 0;
  51. out:
  52. return result;
  53. }
  54. static void init_level2_page(pmd_t *level2p, unsigned long addr)
  55. {
  56. unsigned long end_addr;
  57. addr &= PAGE_MASK;
  58. end_addr = addr + PUD_SIZE;
  59. while (addr < end_addr) {
  60. set_pmd(level2p++, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
  61. addr += PMD_SIZE;
  62. }
  63. }
  64. static int init_level3_page(struct kimage *image, pud_t *level3p,
  65. unsigned long addr, unsigned long last_addr)
  66. {
  67. unsigned long end_addr;
  68. int result;
  69. result = 0;
  70. addr &= PAGE_MASK;
  71. end_addr = addr + PGDIR_SIZE;
  72. while ((addr < last_addr) && (addr < end_addr)) {
  73. struct page *page;
  74. pmd_t *level2p;
  75. page = kimage_alloc_control_pages(image, 0);
  76. if (!page) {
  77. result = -ENOMEM;
  78. goto out;
  79. }
  80. level2p = (pmd_t *)page_address(page);
  81. init_level2_page(level2p, addr);
  82. set_pud(level3p++, __pud(__pa(level2p) | _KERNPG_TABLE));
  83. addr += PUD_SIZE;
  84. }
  85. /* clear the unused entries */
  86. while (addr < end_addr) {
  87. pud_clear(level3p++);
  88. addr += PUD_SIZE;
  89. }
  90. out:
  91. return result;
  92. }
  93. static int init_level4_page(struct kimage *image, pgd_t *level4p,
  94. unsigned long addr, unsigned long last_addr)
  95. {
  96. unsigned long end_addr;
  97. int result;
  98. result = 0;
  99. addr &= PAGE_MASK;
  100. end_addr = addr + (PTRS_PER_PGD * PGDIR_SIZE);
  101. while ((addr < last_addr) && (addr < end_addr)) {
  102. struct page *page;
  103. pud_t *level3p;
  104. page = kimage_alloc_control_pages(image, 0);
  105. if (!page) {
  106. result = -ENOMEM;
  107. goto out;
  108. }
  109. level3p = (pud_t *)page_address(page);
  110. result = init_level3_page(image, level3p, addr, last_addr);
  111. if (result)
  112. goto out;
  113. set_pgd(level4p++, __pgd(__pa(level3p) | _KERNPG_TABLE));
  114. addr += PGDIR_SIZE;
  115. }
  116. /* clear the unused entries */
  117. while (addr < end_addr) {
  118. pgd_clear(level4p++);
  119. addr += PGDIR_SIZE;
  120. }
  121. out:
  122. return result;
  123. }
  124. static void free_transition_pgtable(struct kimage *image)
  125. {
  126. free_page((unsigned long)image->arch.pud);
  127. free_page((unsigned long)image->arch.pmd);
  128. free_page((unsigned long)image->arch.pte);
  129. }
  130. static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
  131. {
  132. pud_t *pud;
  133. pmd_t *pmd;
  134. pte_t *pte;
  135. unsigned long vaddr, paddr;
  136. int result = -ENOMEM;
  137. vaddr = (unsigned long)relocate_kernel;
  138. paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE);
  139. pgd += pgd_index(vaddr);
  140. if (!pgd_present(*pgd)) {
  141. pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
  142. if (!pud)
  143. goto err;
  144. image->arch.pud = pud;
  145. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
  146. }
  147. pud = pud_offset(pgd, vaddr);
  148. if (!pud_present(*pud)) {
  149. pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  150. if (!pmd)
  151. goto err;
  152. image->arch.pmd = pmd;
  153. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  154. }
  155. pmd = pmd_offset(pud, vaddr);
  156. if (!pmd_present(*pmd)) {
  157. pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
  158. if (!pte)
  159. goto err;
  160. image->arch.pte = pte;
  161. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
  162. }
  163. pte = pte_offset_kernel(pmd, vaddr);
  164. set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
  165. return 0;
  166. err:
  167. free_transition_pgtable(image);
  168. return result;
  169. }
  170. static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
  171. {
  172. pgd_t *level4p;
  173. int result;
  174. level4p = (pgd_t *)__va(start_pgtable);
  175. result = init_level4_page(image, level4p, 0, max_pfn << PAGE_SHIFT);
  176. if (result)
  177. return result;
  178. /*
  179. * image->start may be outside 0 ~ max_pfn, for example when
  180. * jump back to original kernel from kexeced kernel
  181. */
  182. result = init_one_level2_page(image, level4p, image->start);
  183. if (result)
  184. return result;
  185. return init_transition_pgtable(image, level4p);
  186. }
  187. static void set_idt(void *newidt, u16 limit)
  188. {
  189. struct desc_ptr curidt;
  190. /* x86-64 supports unaliged loads & stores */
  191. curidt.size = limit;
  192. curidt.address = (unsigned long)newidt;
  193. __asm__ __volatile__ (
  194. "lidtq %0\n"
  195. : : "m" (curidt)
  196. );
  197. };
  198. static void set_gdt(void *newgdt, u16 limit)
  199. {
  200. struct desc_ptr curgdt;
  201. /* x86-64 supports unaligned loads & stores */
  202. curgdt.size = limit;
  203. curgdt.address = (unsigned long)newgdt;
  204. __asm__ __volatile__ (
  205. "lgdtq %0\n"
  206. : : "m" (curgdt)
  207. );
  208. };
  209. static void load_segments(void)
  210. {
  211. __asm__ __volatile__ (
  212. "\tmovl %0,%%ds\n"
  213. "\tmovl %0,%%es\n"
  214. "\tmovl %0,%%ss\n"
  215. "\tmovl %0,%%fs\n"
  216. "\tmovl %0,%%gs\n"
  217. : : "a" (__KERNEL_DS) : "memory"
  218. );
  219. }
  220. int machine_kexec_prepare(struct kimage *image)
  221. {
  222. unsigned long start_pgtable;
  223. int result;
  224. /* Calculate the offsets */
  225. start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  226. /* Setup the identity mapped 64bit page table */
  227. result = init_pgtable(image, start_pgtable);
  228. if (result)
  229. return result;
  230. return 0;
  231. }
  232. void machine_kexec_cleanup(struct kimage *image)
  233. {
  234. free_transition_pgtable(image);
  235. }
  236. /*
  237. * Do not allocate memory (or fail in any way) in machine_kexec().
  238. * We are past the point of no return, committed to rebooting now.
  239. */
  240. void machine_kexec(struct kimage *image)
  241. {
  242. unsigned long page_list[PAGES_NR];
  243. void *control_page;
  244. int save_ftrace_enabled;
  245. #ifdef CONFIG_KEXEC_JUMP
  246. if (image->preserve_context)
  247. save_processor_state();
  248. #endif
  249. save_ftrace_enabled = __ftrace_enabled_save();
  250. /* Interrupts aren't acceptable while we reboot */
  251. local_irq_disable();
  252. hw_breakpoint_disable();
  253. if (image->preserve_context) {
  254. #ifdef CONFIG_X86_IO_APIC
  255. /*
  256. * We need to put APICs in legacy mode so that we can
  257. * get timer interrupts in second kernel. kexec/kdump
  258. * paths already have calls to disable_IO_APIC() in
  259. * one form or other. kexec jump path also need
  260. * one.
  261. */
  262. disable_IO_APIC();
  263. #endif
  264. }
  265. control_page = page_address(image->control_code_page) + PAGE_SIZE;
  266. memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
  267. page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page);
  268. page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
  269. page_list[PA_TABLE_PAGE] =
  270. (unsigned long)__pa(page_address(image->control_code_page));
  271. if (image->type == KEXEC_TYPE_DEFAULT)
  272. page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
  273. << PAGE_SHIFT);
  274. /*
  275. * The segment registers are funny things, they have both a
  276. * visible and an invisible part. Whenever the visible part is
  277. * set to a specific selector, the invisible part is loaded
  278. * with from a table in memory. At no other time is the
  279. * descriptor table in memory accessed.
  280. *
  281. * I take advantage of this here by force loading the
  282. * segments, before I zap the gdt with an invalid value.
  283. */
  284. load_segments();
  285. /*
  286. * The gdt & idt are now invalid.
  287. * If you want to load them you must set up your own idt & gdt.
  288. */
  289. set_gdt(phys_to_virt(0), 0);
  290. set_idt(phys_to_virt(0), 0);
  291. /* now call it */
  292. image->start = relocate_kernel((unsigned long)image->head,
  293. (unsigned long)page_list,
  294. image->start,
  295. image->preserve_context);
  296. #ifdef CONFIG_KEXEC_JUMP
  297. if (image->preserve_context)
  298. restore_processor_state();
  299. #endif
  300. __ftrace_enabled_restore(save_ftrace_enabled);
  301. }
  302. void arch_crash_save_vmcoreinfo(void)
  303. {
  304. VMCOREINFO_SYMBOL(phys_base);
  305. VMCOREINFO_SYMBOL(init_level4_pgt);
  306. #ifdef CONFIG_NUMA
  307. VMCOREINFO_SYMBOL(node_data);
  308. VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
  309. #endif
  310. }