machine_kexec_32.c 6.2 KB

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