machine_kexec_64.c 8.3 KB

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