machine_kexec_64.c 6.0 KB

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