machine_kexec_32.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/delay.h>
  11. #include <linux/numa.h>
  12. #include <linux/ftrace.h>
  13. #include <linux/suspend.h>
  14. #include <linux/gfp.h>
  15. #include <linux/io.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/pgalloc.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/apic.h>
  21. #include <asm/io_apic.h>
  22. #include <asm/cpufeature.h>
  23. #include <asm/desc.h>
  24. #include <asm/set_memory.h>
  25. #include <asm/debugreg.h>
  26. static void set_gdt(void *newgdt, __u16 limit)
  27. {
  28. struct desc_ptr curgdt;
  29. /* ia32 supports unaligned loads & stores */
  30. curgdt.size = limit;
  31. curgdt.address = (unsigned long)newgdt;
  32. load_gdt(&curgdt);
  33. }
  34. static void load_segments(void)
  35. {
  36. #define __STR(X) #X
  37. #define STR(X) __STR(X)
  38. __asm__ __volatile__ (
  39. "\tljmp $"STR(__KERNEL_CS)",$1f\n"
  40. "\t1:\n"
  41. "\tmovl $"STR(__KERNEL_DS)",%%eax\n"
  42. "\tmovl %%eax,%%ds\n"
  43. "\tmovl %%eax,%%es\n"
  44. "\tmovl %%eax,%%ss\n"
  45. : : : "eax", "memory");
  46. #undef STR
  47. #undef __STR
  48. }
  49. static void machine_kexec_free_page_tables(struct kimage *image)
  50. {
  51. free_page((unsigned long)image->arch.pgd);
  52. #ifdef CONFIG_X86_PAE
  53. free_page((unsigned long)image->arch.pmd0);
  54. free_page((unsigned long)image->arch.pmd1);
  55. #endif
  56. free_page((unsigned long)image->arch.pte0);
  57. free_page((unsigned long)image->arch.pte1);
  58. }
  59. static int machine_kexec_alloc_page_tables(struct kimage *image)
  60. {
  61. image->arch.pgd = (pgd_t *)get_zeroed_page(GFP_KERNEL);
  62. #ifdef CONFIG_X86_PAE
  63. image->arch.pmd0 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  64. image->arch.pmd1 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  65. #endif
  66. image->arch.pte0 = (pte_t *)get_zeroed_page(GFP_KERNEL);
  67. image->arch.pte1 = (pte_t *)get_zeroed_page(GFP_KERNEL);
  68. if (!image->arch.pgd ||
  69. #ifdef CONFIG_X86_PAE
  70. !image->arch.pmd0 || !image->arch.pmd1 ||
  71. #endif
  72. !image->arch.pte0 || !image->arch.pte1) {
  73. machine_kexec_free_page_tables(image);
  74. return -ENOMEM;
  75. }
  76. return 0;
  77. }
  78. static void machine_kexec_page_table_set_one(
  79. pgd_t *pgd, pmd_t *pmd, pte_t *pte,
  80. unsigned long vaddr, unsigned long paddr)
  81. {
  82. p4d_t *p4d;
  83. pud_t *pud;
  84. pgd += pgd_index(vaddr);
  85. #ifdef CONFIG_X86_PAE
  86. if (!(pgd_val(*pgd) & _PAGE_PRESENT))
  87. set_pgd(pgd, __pgd(__pa(pmd) | _PAGE_PRESENT));
  88. #endif
  89. p4d = p4d_offset(pgd, vaddr);
  90. pud = pud_offset(p4d, vaddr);
  91. pmd = pmd_offset(pud, vaddr);
  92. if (!(pmd_val(*pmd) & _PAGE_PRESENT))
  93. set_pmd(pmd, __pmd(__pa(pte) | _PAGE_TABLE));
  94. pte = pte_offset_kernel(pmd, vaddr);
  95. set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
  96. }
  97. static void machine_kexec_prepare_page_tables(struct kimage *image)
  98. {
  99. void *control_page;
  100. pmd_t *pmd = NULL;
  101. control_page = page_address(image->control_code_page);
  102. #ifdef CONFIG_X86_PAE
  103. pmd = image->arch.pmd0;
  104. #endif
  105. machine_kexec_page_table_set_one(
  106. image->arch.pgd, pmd, image->arch.pte0,
  107. (unsigned long)control_page, __pa(control_page));
  108. #ifdef CONFIG_X86_PAE
  109. pmd = image->arch.pmd1;
  110. #endif
  111. machine_kexec_page_table_set_one(
  112. image->arch.pgd, pmd, image->arch.pte1,
  113. __pa(control_page), __pa(control_page));
  114. }
  115. /*
  116. * A architecture hook called to validate the
  117. * proposed image and prepare the control pages
  118. * as needed. The pages for KEXEC_CONTROL_PAGE_SIZE
  119. * have been allocated, but the segments have yet
  120. * been copied into the kernel.
  121. *
  122. * Do what every setup is needed on image and the
  123. * reboot code buffer to allow us to avoid allocations
  124. * later.
  125. *
  126. * - Make control page executable.
  127. * - Allocate page tables
  128. * - Setup page tables
  129. */
  130. int machine_kexec_prepare(struct kimage *image)
  131. {
  132. int error;
  133. set_pages_x(image->control_code_page, 1);
  134. error = machine_kexec_alloc_page_tables(image);
  135. if (error)
  136. return error;
  137. machine_kexec_prepare_page_tables(image);
  138. return 0;
  139. }
  140. /*
  141. * Undo anything leftover by machine_kexec_prepare
  142. * when an image is freed.
  143. */
  144. void machine_kexec_cleanup(struct kimage *image)
  145. {
  146. set_pages_nx(image->control_code_page, 1);
  147. machine_kexec_free_page_tables(image);
  148. }
  149. /*
  150. * Do not allocate memory (or fail in any way) in machine_kexec().
  151. * We are past the point of no return, committed to rebooting now.
  152. */
  153. void machine_kexec(struct kimage *image)
  154. {
  155. unsigned long page_list[PAGES_NR];
  156. void *control_page;
  157. int save_ftrace_enabled;
  158. asmlinkage unsigned long
  159. (*relocate_kernel_ptr)(unsigned long indirection_page,
  160. unsigned long control_page,
  161. unsigned long start_address,
  162. unsigned int has_pae,
  163. unsigned int preserve_context);
  164. #ifdef CONFIG_KEXEC_JUMP
  165. if (image->preserve_context)
  166. save_processor_state();
  167. #endif
  168. save_ftrace_enabled = __ftrace_enabled_save();
  169. /* Interrupts aren't acceptable while we reboot */
  170. local_irq_disable();
  171. hw_breakpoint_disable();
  172. if (image->preserve_context) {
  173. #ifdef CONFIG_X86_IO_APIC
  174. /*
  175. * We need to put APICs in legacy mode so that we can
  176. * get timer interrupts in second kernel. kexec/kdump
  177. * paths already have calls to disable_IO_APIC() in
  178. * one form or other. kexec jump path also need
  179. * one.
  180. */
  181. disable_IO_APIC();
  182. #endif
  183. }
  184. control_page = page_address(image->control_code_page);
  185. memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
  186. relocate_kernel_ptr = control_page;
  187. page_list[PA_CONTROL_PAGE] = __pa(control_page);
  188. page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
  189. page_list[PA_PGD] = __pa(image->arch.pgd);
  190. if (image->type == KEXEC_TYPE_DEFAULT)
  191. page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
  192. << PAGE_SHIFT);
  193. /*
  194. * The segment registers are funny things, they have both a
  195. * visible and an invisible part. Whenever the visible part is
  196. * set to a specific selector, the invisible part is loaded
  197. * with from a table in memory. At no other time is the
  198. * descriptor table in memory accessed.
  199. *
  200. * I take advantage of this here by force loading the
  201. * segments, before I zap the gdt with an invalid value.
  202. */
  203. load_segments();
  204. /*
  205. * The gdt & idt are now invalid.
  206. * If you want to load them you must set up your own idt & gdt.
  207. */
  208. idt_invalidate(phys_to_virt(0));
  209. set_gdt(phys_to_virt(0), 0);
  210. /* now call it */
  211. image->start = relocate_kernel_ptr((unsigned long)image->head,
  212. (unsigned long)page_list,
  213. image->start,
  214. boot_cpu_has(X86_FEATURE_PAE),
  215. image->preserve_context);
  216. #ifdef CONFIG_KEXEC_JUMP
  217. if (image->preserve_context)
  218. restore_processor_state();
  219. #endif
  220. __ftrace_enabled_restore(save_ftrace_enabled);
  221. }
  222. void arch_crash_save_vmcoreinfo(void)
  223. {
  224. #ifdef CONFIG_NUMA
  225. VMCOREINFO_SYMBOL(node_data);
  226. VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
  227. #endif
  228. #ifdef CONFIG_X86_PAE
  229. VMCOREINFO_CONFIG(X86_PAE);
  230. #endif
  231. }