machine_kexec_32.c 5.4 KB

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