machine_kexec_64.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/string.h>
  11. #include <linux/reboot.h>
  12. #include <linux/numa.h>
  13. #include <linux/ftrace.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/io.h>
  18. #define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
  19. static u64 kexec_pgd[512] PAGE_ALIGNED;
  20. static u64 kexec_pud0[512] PAGE_ALIGNED;
  21. static u64 kexec_pmd0[512] PAGE_ALIGNED;
  22. static u64 kexec_pte0[512] PAGE_ALIGNED;
  23. static u64 kexec_pud1[512] PAGE_ALIGNED;
  24. static u64 kexec_pmd1[512] PAGE_ALIGNED;
  25. static u64 kexec_pte1[512] PAGE_ALIGNED;
  26. static void init_level2_page(pmd_t *level2p, unsigned long addr)
  27. {
  28. unsigned long end_addr;
  29. addr &= PAGE_MASK;
  30. end_addr = addr + PUD_SIZE;
  31. while (addr < end_addr) {
  32. set_pmd(level2p++, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
  33. addr += PMD_SIZE;
  34. }
  35. }
  36. static int init_level3_page(struct kimage *image, pud_t *level3p,
  37. unsigned long addr, unsigned long last_addr)
  38. {
  39. unsigned long end_addr;
  40. int result;
  41. result = 0;
  42. addr &= PAGE_MASK;
  43. end_addr = addr + PGDIR_SIZE;
  44. while ((addr < last_addr) && (addr < end_addr)) {
  45. struct page *page;
  46. pmd_t *level2p;
  47. page = kimage_alloc_control_pages(image, 0);
  48. if (!page) {
  49. result = -ENOMEM;
  50. goto out;
  51. }
  52. level2p = (pmd_t *)page_address(page);
  53. init_level2_page(level2p, addr);
  54. set_pud(level3p++, __pud(__pa(level2p) | _KERNPG_TABLE));
  55. addr += PUD_SIZE;
  56. }
  57. /* clear the unused entries */
  58. while (addr < end_addr) {
  59. pud_clear(level3p++);
  60. addr += PUD_SIZE;
  61. }
  62. out:
  63. return result;
  64. }
  65. static int init_level4_page(struct kimage *image, pgd_t *level4p,
  66. unsigned long addr, unsigned long last_addr)
  67. {
  68. unsigned long end_addr;
  69. int result;
  70. result = 0;
  71. addr &= PAGE_MASK;
  72. end_addr = addr + (PTRS_PER_PGD * PGDIR_SIZE);
  73. while ((addr < last_addr) && (addr < end_addr)) {
  74. struct page *page;
  75. pud_t *level3p;
  76. page = kimage_alloc_control_pages(image, 0);
  77. if (!page) {
  78. result = -ENOMEM;
  79. goto out;
  80. }
  81. level3p = (pud_t *)page_address(page);
  82. result = init_level3_page(image, level3p, addr, last_addr);
  83. if (result) {
  84. goto out;
  85. }
  86. set_pgd(level4p++, __pgd(__pa(level3p) | _KERNPG_TABLE));
  87. addr += PGDIR_SIZE;
  88. }
  89. /* clear the unused entries */
  90. while (addr < end_addr) {
  91. pgd_clear(level4p++);
  92. addr += PGDIR_SIZE;
  93. }
  94. out:
  95. return result;
  96. }
  97. static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
  98. {
  99. pgd_t *level4p;
  100. level4p = (pgd_t *)__va(start_pgtable);
  101. return init_level4_page(image, level4p, 0, max_pfn << PAGE_SHIFT);
  102. }
  103. static void set_idt(void *newidt, u16 limit)
  104. {
  105. struct desc_ptr curidt;
  106. /* x86-64 supports unaliged loads & stores */
  107. curidt.size = limit;
  108. curidt.address = (unsigned long)newidt;
  109. __asm__ __volatile__ (
  110. "lidtq %0\n"
  111. : : "m" (curidt)
  112. );
  113. };
  114. static void set_gdt(void *newgdt, u16 limit)
  115. {
  116. struct desc_ptr curgdt;
  117. /* x86-64 supports unaligned loads & stores */
  118. curgdt.size = limit;
  119. curgdt.address = (unsigned long)newgdt;
  120. __asm__ __volatile__ (
  121. "lgdtq %0\n"
  122. : : "m" (curgdt)
  123. );
  124. };
  125. static void load_segments(void)
  126. {
  127. __asm__ __volatile__ (
  128. "\tmovl %0,%%ds\n"
  129. "\tmovl %0,%%es\n"
  130. "\tmovl %0,%%ss\n"
  131. "\tmovl %0,%%fs\n"
  132. "\tmovl %0,%%gs\n"
  133. : : "a" (__KERNEL_DS) : "memory"
  134. );
  135. }
  136. int machine_kexec_prepare(struct kimage *image)
  137. {
  138. unsigned long start_pgtable;
  139. int result;
  140. /* Calculate the offsets */
  141. start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  142. /* Setup the identity mapped 64bit page table */
  143. result = init_pgtable(image, start_pgtable);
  144. if (result)
  145. return result;
  146. return 0;
  147. }
  148. void machine_kexec_cleanup(struct kimage *image)
  149. {
  150. return;
  151. }
  152. /*
  153. * Do not allocate memory (or fail in any way) in machine_kexec().
  154. * We are past the point of no return, committed to rebooting now.
  155. */
  156. void machine_kexec(struct kimage *image)
  157. {
  158. unsigned long page_list[PAGES_NR];
  159. void *control_page;
  160. tracer_disable();
  161. /* Interrupts aren't acceptable while we reboot */
  162. local_irq_disable();
  163. control_page = page_address(image->control_code_page) + PAGE_SIZE;
  164. memcpy(control_page, relocate_kernel, PAGE_SIZE);
  165. page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page);
  166. page_list[VA_CONTROL_PAGE] = (unsigned long)relocate_kernel;
  167. page_list[PA_PGD] = virt_to_phys(&kexec_pgd);
  168. page_list[VA_PGD] = (unsigned long)kexec_pgd;
  169. page_list[PA_PUD_0] = virt_to_phys(&kexec_pud0);
  170. page_list[VA_PUD_0] = (unsigned long)kexec_pud0;
  171. page_list[PA_PMD_0] = virt_to_phys(&kexec_pmd0);
  172. page_list[VA_PMD_0] = (unsigned long)kexec_pmd0;
  173. page_list[PA_PTE_0] = virt_to_phys(&kexec_pte0);
  174. page_list[VA_PTE_0] = (unsigned long)kexec_pte0;
  175. page_list[PA_PUD_1] = virt_to_phys(&kexec_pud1);
  176. page_list[VA_PUD_1] = (unsigned long)kexec_pud1;
  177. page_list[PA_PMD_1] = virt_to_phys(&kexec_pmd1);
  178. page_list[VA_PMD_1] = (unsigned long)kexec_pmd1;
  179. page_list[PA_PTE_1] = virt_to_phys(&kexec_pte1);
  180. page_list[VA_PTE_1] = (unsigned long)kexec_pte1;
  181. page_list[PA_TABLE_PAGE] =
  182. (unsigned long)__pa(page_address(image->control_code_page));
  183. /* The segment registers are funny things, they have both a
  184. * visible and an invisible part. Whenever the visible part is
  185. * set to a specific selector, the invisible part is loaded
  186. * with from a table in memory. At no other time is the
  187. * descriptor table in memory accessed.
  188. *
  189. * I take advantage of this here by force loading the
  190. * segments, before I zap the gdt with an invalid value.
  191. */
  192. load_segments();
  193. /* The gdt & idt are now invalid.
  194. * If you want to load them you must set up your own idt & gdt.
  195. */
  196. set_gdt(phys_to_virt(0),0);
  197. set_idt(phys_to_virt(0),0);
  198. /* now call it */
  199. relocate_kernel((unsigned long)image->head, (unsigned long)page_list,
  200. image->start);
  201. }
  202. void arch_crash_save_vmcoreinfo(void)
  203. {
  204. VMCOREINFO_SYMBOL(phys_base);
  205. VMCOREINFO_SYMBOL(init_level4_pgt);
  206. #ifdef CONFIG_NUMA
  207. VMCOREINFO_SYMBOL(node_data);
  208. VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
  209. #endif
  210. }