machine_kexec.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <linux/string.h>
  12. #include <linux/reboot.h>
  13. #include <asm/pda.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/io.h>
  19. #include <asm/apic.h>
  20. #include <asm/cpufeature.h>
  21. #include <asm/hw_irq.h>
  22. #define LEVEL0_SIZE (1UL << 12UL)
  23. #define LEVEL1_SIZE (1UL << 21UL)
  24. #define LEVEL2_SIZE (1UL << 30UL)
  25. #define LEVEL3_SIZE (1UL << 39UL)
  26. #define LEVEL4_SIZE (1UL << 48UL)
  27. #define L0_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  28. #define L1_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE)
  29. #define L2_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  30. #define L3_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  31. static void init_level2_page(u64 *level2p, unsigned long addr)
  32. {
  33. unsigned long end_addr;
  34. addr &= PAGE_MASK;
  35. end_addr = addr + LEVEL2_SIZE;
  36. while (addr < end_addr) {
  37. *(level2p++) = addr | L1_ATTR;
  38. addr += LEVEL1_SIZE;
  39. }
  40. }
  41. static int init_level3_page(struct kimage *image, u64 *level3p,
  42. unsigned long addr, unsigned long last_addr)
  43. {
  44. unsigned long end_addr;
  45. int result;
  46. result = 0;
  47. addr &= PAGE_MASK;
  48. end_addr = addr + LEVEL3_SIZE;
  49. while ((addr < last_addr) && (addr < end_addr)) {
  50. struct page *page;
  51. u64 *level2p;
  52. page = kimage_alloc_control_pages(image, 0);
  53. if (!page) {
  54. result = -ENOMEM;
  55. goto out;
  56. }
  57. level2p = (u64 *)page_address(page);
  58. init_level2_page(level2p, addr);
  59. *(level3p++) = __pa(level2p) | L2_ATTR;
  60. addr += LEVEL2_SIZE;
  61. }
  62. /* clear the unused entries */
  63. while (addr < end_addr) {
  64. *(level3p++) = 0;
  65. addr += LEVEL2_SIZE;
  66. }
  67. out:
  68. return result;
  69. }
  70. static int init_level4_page(struct kimage *image, u64 *level4p,
  71. unsigned long addr, unsigned long last_addr)
  72. {
  73. unsigned long end_addr;
  74. int result;
  75. result = 0;
  76. addr &= PAGE_MASK;
  77. end_addr = addr + LEVEL4_SIZE;
  78. while ((addr < last_addr) && (addr < end_addr)) {
  79. struct page *page;
  80. u64 *level3p;
  81. page = kimage_alloc_control_pages(image, 0);
  82. if (!page) {
  83. result = -ENOMEM;
  84. goto out;
  85. }
  86. level3p = (u64 *)page_address(page);
  87. result = init_level3_page(image, level3p, addr, last_addr);
  88. if (result) {
  89. goto out;
  90. }
  91. *(level4p++) = __pa(level3p) | L3_ATTR;
  92. addr += LEVEL3_SIZE;
  93. }
  94. /* clear the unused entries */
  95. while (addr < end_addr) {
  96. *(level4p++) = 0;
  97. addr += LEVEL3_SIZE;
  98. }
  99. out:
  100. return result;
  101. }
  102. static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
  103. {
  104. u64 *level4p;
  105. level4p = (u64 *)__va(start_pgtable);
  106. return init_level4_page(image, level4p, 0, end_pfn << PAGE_SHIFT);
  107. }
  108. static void set_idt(void *newidt, u16 limit)
  109. {
  110. struct desc_ptr curidt;
  111. /* x86-64 supports unaliged loads & stores */
  112. curidt.size = limit;
  113. curidt.address = (unsigned long)newidt;
  114. __asm__ __volatile__ (
  115. "lidtq %0\n"
  116. : : "m" (curidt)
  117. );
  118. };
  119. static void set_gdt(void *newgdt, u16 limit)
  120. {
  121. struct desc_ptr curgdt;
  122. /* x86-64 supports unaligned loads & stores */
  123. curgdt.size = limit;
  124. curgdt.address = (unsigned long)newgdt;
  125. __asm__ __volatile__ (
  126. "lgdtq %0\n"
  127. : : "m" (curgdt)
  128. );
  129. };
  130. static void load_segments(void)
  131. {
  132. __asm__ __volatile__ (
  133. "\tmovl %0,%%ds\n"
  134. "\tmovl %0,%%es\n"
  135. "\tmovl %0,%%ss\n"
  136. "\tmovl %0,%%fs\n"
  137. "\tmovl %0,%%gs\n"
  138. : : "a" (__KERNEL_DS)
  139. );
  140. }
  141. typedef NORET_TYPE void (*relocate_new_kernel_t)(unsigned long indirection_page,
  142. unsigned long control_code_buffer,
  143. unsigned long start_address,
  144. unsigned long pgtable) ATTRIB_NORET;
  145. const extern unsigned char relocate_new_kernel[];
  146. const extern unsigned long relocate_new_kernel_size;
  147. int machine_kexec_prepare(struct kimage *image)
  148. {
  149. unsigned long start_pgtable, control_code_buffer;
  150. int result;
  151. /* Calculate the offsets */
  152. start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  153. control_code_buffer = start_pgtable + 4096UL;
  154. /* Setup the identity mapped 64bit page table */
  155. result = init_pgtable(image, start_pgtable);
  156. if (result)
  157. return result;
  158. /* Place the code in the reboot code buffer */
  159. memcpy(__va(control_code_buffer), relocate_new_kernel,
  160. relocate_new_kernel_size);
  161. return 0;
  162. }
  163. void machine_kexec_cleanup(struct kimage *image)
  164. {
  165. return;
  166. }
  167. /*
  168. * Do not allocate memory (or fail in any way) in machine_kexec().
  169. * We are past the point of no return, committed to rebooting now.
  170. */
  171. NORET_TYPE void machine_kexec(struct kimage *image)
  172. {
  173. unsigned long page_list;
  174. unsigned long control_code_buffer;
  175. unsigned long start_pgtable;
  176. relocate_new_kernel_t rnk;
  177. /* Interrupts aren't acceptable while we reboot */
  178. local_irq_disable();
  179. /* Calculate the offsets */
  180. page_list = image->head;
  181. start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  182. control_code_buffer = start_pgtable + 4096UL;
  183. /* Set the low half of the page table to my identity mapped
  184. * page table for kexec. Leave the high half pointing at the
  185. * kernel pages. Don't bother to flush the global pages
  186. * as that will happen when I fully switch to my identity mapped
  187. * page table anyway.
  188. */
  189. memcpy(__va(read_cr3()), __va(start_pgtable), PAGE_SIZE/2);
  190. __flush_tlb();
  191. /* The segment registers are funny things, they are
  192. * automatically loaded from a table, in memory wherever you
  193. * set them to a specific selector, but this table is never
  194. * accessed again unless you set the segment to a different selector.
  195. *
  196. * The more common model are caches where the behide
  197. * the scenes work is done, but is also dropped at arbitrary
  198. * times.
  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. /* The gdt & idt are now invalid.
  205. * If you want to load them you must set up your own idt & gdt.
  206. */
  207. set_gdt(phys_to_virt(0),0);
  208. set_idt(phys_to_virt(0),0);
  209. /* now call it */
  210. rnk = (relocate_new_kernel_t) control_code_buffer;
  211. (*rnk)(page_list, control_code_buffer, image->start, start_pgtable);
  212. }