machine_kexec_32.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * handle transition of Linux booting another kernel
  4. * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/kexec.h>
  8. #include <linux/delay.h>
  9. #include <linux/numa.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/suspend.h>
  12. #include <linux/gfp.h>
  13. #include <linux/io.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/apic.h>
  18. #include <asm/io_apic.h>
  19. #include <asm/cpufeature.h>
  20. #include <asm/desc.h>
  21. #include <asm/set_memory.h>
  22. #include <asm/debugreg.h>
  23. static void set_gdt(void *newgdt, __u16 limit)
  24. {
  25. struct desc_ptr curgdt;
  26. /* ia32 supports unaligned loads & stores */
  27. curgdt.size = limit;
  28. curgdt.address = (unsigned long)newgdt;
  29. load_gdt(&curgdt);
  30. }
  31. static void load_segments(void)
  32. {
  33. #define __STR(X) #X
  34. #define STR(X) __STR(X)
  35. __asm__ __volatile__ (
  36. "\tljmp $"STR(__KERNEL_CS)",$1f\n"
  37. "\t1:\n"
  38. "\tmovl $"STR(__KERNEL_DS)",%%eax\n"
  39. "\tmovl %%eax,%%ds\n"
  40. "\tmovl %%eax,%%es\n"
  41. "\tmovl %%eax,%%ss\n"
  42. : : : "eax", "memory");
  43. #undef STR
  44. #undef __STR
  45. }
  46. static void machine_kexec_free_page_tables(struct kimage *image)
  47. {
  48. free_pages((unsigned long)image->arch.pgd, PGD_ALLOCATION_ORDER);
  49. image->arch.pgd = NULL;
  50. #ifdef CONFIG_X86_PAE
  51. free_page((unsigned long)image->arch.pmd0);
  52. image->arch.pmd0 = NULL;
  53. free_page((unsigned long)image->arch.pmd1);
  54. image->arch.pmd1 = NULL;
  55. #endif
  56. free_page((unsigned long)image->arch.pte0);
  57. image->arch.pte0 = NULL;
  58. free_page((unsigned long)image->arch.pte1);
  59. image->arch.pte1 = NULL;
  60. }
  61. static int machine_kexec_alloc_page_tables(struct kimage *image)
  62. {
  63. image->arch.pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  64. PGD_ALLOCATION_ORDER);
  65. #ifdef CONFIG_X86_PAE
  66. image->arch.pmd0 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  67. image->arch.pmd1 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  68. #endif
  69. image->arch.pte0 = (pte_t *)get_zeroed_page(GFP_KERNEL);
  70. image->arch.pte1 = (pte_t *)get_zeroed_page(GFP_KERNEL);
  71. if (!image->arch.pgd ||
  72. #ifdef CONFIG_X86_PAE
  73. !image->arch.pmd0 || !image->arch.pmd1 ||
  74. #endif
  75. !image->arch.pte0 || !image->arch.pte1) {
  76. return -ENOMEM;
  77. }
  78. return 0;
  79. }
  80. static void machine_kexec_page_table_set_one(
  81. pgd_t *pgd, pmd_t *pmd, pte_t *pte,
  82. unsigned long vaddr, unsigned long paddr)
  83. {
  84. p4d_t *p4d;
  85. pud_t *pud;
  86. pgd += pgd_index(vaddr);
  87. #ifdef CONFIG_X86_PAE
  88. if (!(pgd_val(*pgd) & _PAGE_PRESENT))
  89. set_pgd(pgd, __pgd(__pa(pmd) | _PAGE_PRESENT));
  90. #endif
  91. p4d = p4d_offset(pgd, vaddr);
  92. pud = pud_offset(p4d, vaddr);
  93. pmd = pmd_offset(pud, vaddr);
  94. if (!(pmd_val(*pmd) & _PAGE_PRESENT))
  95. set_pmd(pmd, __pmd(__pa(pte) | _PAGE_TABLE));
  96. pte = pte_offset_kernel(pmd, vaddr);
  97. set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
  98. }
  99. static void machine_kexec_prepare_page_tables(struct kimage *image)
  100. {
  101. void *control_page;
  102. pmd_t *pmd = NULL;
  103. control_page = page_address(image->control_code_page);
  104. #ifdef CONFIG_X86_PAE
  105. pmd = image->arch.pmd0;
  106. #endif
  107. machine_kexec_page_table_set_one(
  108. image->arch.pgd, pmd, image->arch.pte0,
  109. (unsigned long)control_page, __pa(control_page));
  110. #ifdef CONFIG_X86_PAE
  111. pmd = image->arch.pmd1;
  112. #endif
  113. machine_kexec_page_table_set_one(
  114. image->arch.pgd, pmd, image->arch.pte1,
  115. __pa(control_page), __pa(control_page));
  116. }
  117. /*
  118. * A architecture hook called to validate the
  119. * proposed image and prepare the control pages
  120. * as needed. The pages for KEXEC_CONTROL_PAGE_SIZE
  121. * have been allocated, but the segments have yet
  122. * been copied into the kernel.
  123. *
  124. * Do what every setup is needed on image and the
  125. * reboot code buffer to allow us to avoid allocations
  126. * later.
  127. *
  128. * - Make control page executable.
  129. * - Allocate page tables
  130. * - Setup page tables
  131. */
  132. int machine_kexec_prepare(struct kimage *image)
  133. {
  134. int error;
  135. set_memory_x((unsigned long)page_address(image->control_code_page), 1);
  136. error = machine_kexec_alloc_page_tables(image);
  137. if (error)
  138. return error;
  139. machine_kexec_prepare_page_tables(image);
  140. return 0;
  141. }
  142. /*
  143. * Undo anything leftover by machine_kexec_prepare
  144. * when an image is freed.
  145. */
  146. void machine_kexec_cleanup(struct kimage *image)
  147. {
  148. set_memory_nx((unsigned long)page_address(image->control_code_page), 1);
  149. machine_kexec_free_page_tables(image);
  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. void machine_kexec(struct kimage *image)
  156. {
  157. unsigned long page_list[PAGES_NR];
  158. void *control_page;
  159. int save_ftrace_enabled;
  160. asmlinkage unsigned long
  161. (*relocate_kernel_ptr)(unsigned long indirection_page,
  162. unsigned long control_page,
  163. unsigned long start_address,
  164. unsigned int has_pae,
  165. unsigned int preserve_context);
  166. #ifdef CONFIG_KEXEC_JUMP
  167. if (image->preserve_context)
  168. save_processor_state();
  169. #endif
  170. save_ftrace_enabled = __ftrace_enabled_save();
  171. /* Interrupts aren't acceptable while we reboot */
  172. local_irq_disable();
  173. hw_breakpoint_disable();
  174. if (image->preserve_context) {
  175. #ifdef CONFIG_X86_IO_APIC
  176. /*
  177. * We need to put APICs in legacy mode so that we can
  178. * get timer interrupts in second kernel. kexec/kdump
  179. * paths already have calls to restore_boot_irq_mode()
  180. * in one form or other. kexec jump path also need one.
  181. */
  182. clear_IO_APIC();
  183. restore_boot_irq_mode();
  184. #endif
  185. }
  186. control_page = page_address(image->control_code_page);
  187. memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
  188. relocate_kernel_ptr = control_page;
  189. page_list[PA_CONTROL_PAGE] = __pa(control_page);
  190. page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
  191. page_list[PA_PGD] = __pa(image->arch.pgd);
  192. if (image->type == KEXEC_TYPE_DEFAULT)
  193. page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
  194. << PAGE_SHIFT);
  195. /*
  196. * The segment registers are funny things, they have both a
  197. * visible and an invisible part. Whenever the visible part is
  198. * set to a specific selector, the invisible part is loaded
  199. * with from a table in memory. At no other time is the
  200. * descriptor table in memory accessed.
  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. /*
  207. * The gdt & idt are now invalid.
  208. * If you want to load them you must set up your own idt & gdt.
  209. */
  210. idt_invalidate(phys_to_virt(0));
  211. set_gdt(phys_to_virt(0), 0);
  212. /* now call it */
  213. image->start = relocate_kernel_ptr((unsigned long)image->head,
  214. (unsigned long)page_list,
  215. image->start,
  216. boot_cpu_has(X86_FEATURE_PAE),
  217. image->preserve_context);
  218. #ifdef CONFIG_KEXEC_JUMP
  219. if (image->preserve_context)
  220. restore_processor_state();
  221. #endif
  222. __ftrace_enabled_restore(save_ftrace_enabled);
  223. }