machine_kexec.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * machine_kexec.c - 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 <asm/pgtable.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/tlbflush.h>
  14. #include <asm/mmu_context.h>
  15. #include <asm/io.h>
  16. #include <asm/apic.h>
  17. #include <asm/cpufeature.h>
  18. #include <asm/desc.h>
  19. #include <asm/system.h>
  20. #define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
  21. #define L0_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  22. #define L1_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  23. #define L2_ATTR (_PAGE_PRESENT)
  24. #define LEVEL0_SIZE (1UL << 12UL)
  25. #ifndef CONFIG_X86_PAE
  26. #define LEVEL1_SIZE (1UL << 22UL)
  27. static u32 pgtable_level1[1024] PAGE_ALIGNED;
  28. static void identity_map_page(unsigned long address)
  29. {
  30. unsigned long level1_index, level2_index;
  31. u32 *pgtable_level2;
  32. /* Find the current page table */
  33. pgtable_level2 = __va(read_cr3());
  34. /* Find the indexes of the physical address to identity map */
  35. level1_index = (address % LEVEL1_SIZE)/LEVEL0_SIZE;
  36. level2_index = address / LEVEL1_SIZE;
  37. /* Identity map the page table entry */
  38. pgtable_level1[level1_index] = address | L0_ATTR;
  39. pgtable_level2[level2_index] = __pa(pgtable_level1) | L1_ATTR;
  40. /* Flush the tlb so the new mapping takes effect.
  41. * Global tlb entries are not flushed but that is not an issue.
  42. */
  43. load_cr3(pgtable_level2);
  44. }
  45. #else
  46. #define LEVEL1_SIZE (1UL << 21UL)
  47. #define LEVEL2_SIZE (1UL << 30UL)
  48. static u64 pgtable_level1[512] PAGE_ALIGNED;
  49. static u64 pgtable_level2[512] PAGE_ALIGNED;
  50. static void identity_map_page(unsigned long address)
  51. {
  52. unsigned long level1_index, level2_index, level3_index;
  53. u64 *pgtable_level3;
  54. /* Find the current page table */
  55. pgtable_level3 = __va(read_cr3());
  56. /* Find the indexes of the physical address to identity map */
  57. level1_index = (address % LEVEL1_SIZE)/LEVEL0_SIZE;
  58. level2_index = (address % LEVEL2_SIZE)/LEVEL1_SIZE;
  59. level3_index = address / LEVEL2_SIZE;
  60. /* Identity map the page table entry */
  61. pgtable_level1[level1_index] = address | L0_ATTR;
  62. pgtable_level2[level2_index] = __pa(pgtable_level1) | L1_ATTR;
  63. set_64bit(&pgtable_level3[level3_index],
  64. __pa(pgtable_level2) | L2_ATTR);
  65. /* Flush the tlb so the new mapping takes effect.
  66. * Global tlb entries are not flushed but that is not an issue.
  67. */
  68. load_cr3(pgtable_level3);
  69. }
  70. #endif
  71. static void set_idt(void *newidt, __u16 limit)
  72. {
  73. struct Xgt_desc_struct curidt;
  74. /* ia32 supports unaliged loads & stores */
  75. curidt.size = limit;
  76. curidt.address = (unsigned long)newidt;
  77. load_idt(&curidt);
  78. };
  79. static void set_gdt(void *newgdt, __u16 limit)
  80. {
  81. struct Xgt_desc_struct curgdt;
  82. /* ia32 supports unaligned loads & stores */
  83. curgdt.size = limit;
  84. curgdt.address = (unsigned long)newgdt;
  85. load_gdt(&curgdt);
  86. };
  87. static void load_segments(void)
  88. {
  89. #define __STR(X) #X
  90. #define STR(X) __STR(X)
  91. __asm__ __volatile__ (
  92. "\tljmp $"STR(__KERNEL_CS)",$1f\n"
  93. "\t1:\n"
  94. "\tmovl $"STR(__KERNEL_DS)",%eax\n"
  95. "\tmovl %eax,%ds\n"
  96. "\tmovl %eax,%es\n"
  97. "\tmovl %eax,%fs\n"
  98. "\tmovl %eax,%gs\n"
  99. "\tmovl %eax,%ss\n"
  100. );
  101. #undef STR
  102. #undef __STR
  103. }
  104. typedef asmlinkage NORET_TYPE void (*relocate_new_kernel_t)(
  105. unsigned long indirection_page,
  106. unsigned long reboot_code_buffer,
  107. unsigned long start_address,
  108. unsigned int has_pae) ATTRIB_NORET;
  109. const extern unsigned char relocate_new_kernel[];
  110. extern void relocate_new_kernel_end(void);
  111. const extern unsigned int relocate_new_kernel_size;
  112. /*
  113. * A architecture hook called to validate the
  114. * proposed image and prepare the control pages
  115. * as needed. The pages for KEXEC_CONTROL_CODE_SIZE
  116. * have been allocated, but the segments have yet
  117. * been copied into the kernel.
  118. *
  119. * Do what every setup is needed on image and the
  120. * reboot code buffer to allow us to avoid allocations
  121. * later.
  122. *
  123. * Currently nothing.
  124. */
  125. int machine_kexec_prepare(struct kimage *image)
  126. {
  127. return 0;
  128. }
  129. /*
  130. * Undo anything leftover by machine_kexec_prepare
  131. * when an image is freed.
  132. */
  133. void machine_kexec_cleanup(struct kimage *image)
  134. {
  135. }
  136. /*
  137. * Do not allocate memory (or fail in any way) in machine_kexec().
  138. * We are past the point of no return, committed to rebooting now.
  139. */
  140. NORET_TYPE void machine_kexec(struct kimage *image)
  141. {
  142. unsigned long page_list;
  143. unsigned long reboot_code_buffer;
  144. relocate_new_kernel_t rnk;
  145. /* Interrupts aren't acceptable while we reboot */
  146. local_irq_disable();
  147. /* Compute some offsets */
  148. reboot_code_buffer = page_to_pfn(image->control_code_page)
  149. << PAGE_SHIFT;
  150. page_list = image->head;
  151. /* Set up an identity mapping for the reboot_code_buffer */
  152. identity_map_page(reboot_code_buffer);
  153. /* copy it out */
  154. memcpy((void *)reboot_code_buffer, relocate_new_kernel,
  155. relocate_new_kernel_size);
  156. /* The segment registers are funny things, they are
  157. * automatically loaded from a table, in memory wherever you
  158. * set them to a specific selector, but this table is never
  159. * accessed again you set the segment to a different selector.
  160. *
  161. * The more common model is are caches where the behide
  162. * the scenes work is done, but is also dropped at arbitrary
  163. * times.
  164. *
  165. * I take advantage of this here by force loading the
  166. * segments, before I zap the gdt with an invalid value.
  167. */
  168. load_segments();
  169. /* The gdt & idt are now invalid.
  170. * If you want to load them you must set up your own idt & gdt.
  171. */
  172. set_gdt(phys_to_virt(0),0);
  173. set_idt(phys_to_virt(0),0);
  174. /* now call it */
  175. rnk = (relocate_new_kernel_t) reboot_code_buffer;
  176. (*rnk)(page_list, reboot_code_buffer, image->start, cpu_has_pae);
  177. }