machine_kexec_64.c 6.5 KB

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