machine_kexec.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. unsigned char curidt[10];
  111. /* x86-64 supports unaliged loads & stores */
  112. (*(u16 *)(curidt)) = limit;
  113. (*(u64 *)(curidt +2)) = (unsigned long)(newidt);
  114. __asm__ __volatile__ (
  115. "lidt %0\n"
  116. : "=m" (curidt)
  117. );
  118. };
  119. static void set_gdt(void *newgdt, u16 limit)
  120. {
  121. unsigned char curgdt[10];
  122. /* x86-64 supports unaligned loads & stores */
  123. (*(u16 *)(curgdt)) = limit;
  124. (*(u64 *)(curgdt +2)) = (unsigned long)(newgdt);
  125. __asm__ __volatile__ (
  126. "lgdt %0\n"
  127. : "=m" (curgdt)
  128. );
  129. };
  130. static void load_segments(void)
  131. {
  132. __asm__ __volatile__ (
  133. "\tmovl $"STR(__KERNEL_DS)",%eax\n"
  134. "\tmovl %eax,%ds\n"
  135. "\tmovl %eax,%es\n"
  136. "\tmovl %eax,%ss\n"
  137. "\tmovl %eax,%fs\n"
  138. "\tmovl %eax,%gs\n"
  139. );
  140. #undef STR
  141. #undef __STR
  142. }
  143. typedef NORET_TYPE void (*relocate_new_kernel_t)(unsigned long indirection_page,
  144. unsigned long control_code_buffer,
  145. unsigned long start_address,
  146. unsigned long pgtable) ATTRIB_NORET;
  147. const extern unsigned char relocate_new_kernel[];
  148. const extern unsigned long relocate_new_kernel_size;
  149. int machine_kexec_prepare(struct kimage *image)
  150. {
  151. unsigned long start_pgtable, control_code_buffer;
  152. int result;
  153. /* Calculate the offsets */
  154. start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  155. control_code_buffer = start_pgtable + 4096UL;
  156. /* Setup the identity mapped 64bit page table */
  157. result = init_pgtable(image, start_pgtable);
  158. if (result)
  159. return result;
  160. /* Place the code in the reboot code buffer */
  161. memcpy(__va(control_code_buffer), relocate_new_kernel,
  162. relocate_new_kernel_size);
  163. return 0;
  164. }
  165. void machine_kexec_cleanup(struct kimage *image)
  166. {
  167. return;
  168. }
  169. /*
  170. * Do not allocate memory (or fail in any way) in machine_kexec().
  171. * We are past the point of no return, committed to rebooting now.
  172. */
  173. NORET_TYPE void machine_kexec(struct kimage *image)
  174. {
  175. unsigned long page_list;
  176. unsigned long control_code_buffer;
  177. unsigned long start_pgtable;
  178. relocate_new_kernel_t rnk;
  179. /* Interrupts aren't acceptable while we reboot */
  180. local_irq_disable();
  181. /* Calculate the offsets */
  182. page_list = image->head;
  183. start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  184. control_code_buffer = start_pgtable + 4096UL;
  185. /* Set the low half of the page table to my identity mapped
  186. * page table for kexec. Leave the high half pointing at the
  187. * kernel pages. Don't bother to flush the global pages
  188. * as that will happen when I fully switch to my identity mapped
  189. * page table anyway.
  190. */
  191. memcpy(__va(read_cr3()), __va(start_pgtable), PAGE_SIZE/2);
  192. __flush_tlb();
  193. /* The segment registers are funny things, they are
  194. * automatically loaded from a table, in memory wherever you
  195. * set them to a specific selector, but this table is never
  196. * accessed again unless you set the segment to a different selector.
  197. *
  198. * The more common model are caches where the behide
  199. * the scenes work is done, but is also dropped at arbitrary
  200. * times.
  201. *
  202. * I take advantage of this here by force loading the
  203. * segments, before I zap the gdt with an invalid value.
  204. */
  205. load_segments();
  206. /* The gdt & idt are now invalid.
  207. * If you want to load them you must set up your own idt & gdt.
  208. */
  209. set_gdt(phys_to_virt(0),0);
  210. set_idt(phys_to_virt(0),0);
  211. /* now call it */
  212. rnk = (relocate_new_kernel_t) control_code_buffer;
  213. (*rnk)(page_list, control_code_buffer, image->start, start_pgtable);
  214. }