machine_kexec_32.c 6.9 KB

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