relocate_kernel_64.S 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * relocate_kernel.S - put the kernel image in place to boot
  4. * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
  5. */
  6. #include <linux/linkage.h>
  7. #include <asm/page_types.h>
  8. #include <asm/kexec.h>
  9. #include <asm/processor-flags.h>
  10. #include <asm/pgtable_types.h>
  11. #include <asm/nospec-branch.h>
  12. #include <asm/unwind_hints.h>
  13. /*
  14. * Must be relocatable PIC code callable as a C function
  15. */
  16. #define PTR(x) (x << 3)
  17. #define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  18. /*
  19. * control_page + KEXEC_CONTROL_CODE_MAX_SIZE
  20. * ~ control_page + PAGE_SIZE are used as data storage and stack for
  21. * jumping back
  22. */
  23. #define DATA(offset) (KEXEC_CONTROL_CODE_MAX_SIZE+(offset))
  24. /* Minimal CPU state */
  25. #define RSP DATA(0x0)
  26. #define CR0 DATA(0x8)
  27. #define CR3 DATA(0x10)
  28. #define CR4 DATA(0x18)
  29. /* other data */
  30. #define CP_PA_TABLE_PAGE DATA(0x20)
  31. #define CP_PA_SWAP_PAGE DATA(0x28)
  32. #define CP_PA_BACKUP_PAGES_MAP DATA(0x30)
  33. .text
  34. .align PAGE_SIZE
  35. .code64
  36. SYM_CODE_START_NOALIGN(relocate_kernel)
  37. UNWIND_HINT_EMPTY
  38. /*
  39. * %rdi indirection_page
  40. * %rsi page_list
  41. * %rdx start address
  42. * %rcx preserve_context
  43. * %r8 sme_active
  44. */
  45. /* Save the CPU context, used for jumping back */
  46. pushq %rbx
  47. pushq %rbp
  48. pushq %r12
  49. pushq %r13
  50. pushq %r14
  51. pushq %r15
  52. pushf
  53. movq PTR(VA_CONTROL_PAGE)(%rsi), %r11
  54. movq %rsp, RSP(%r11)
  55. movq %cr0, %rax
  56. movq %rax, CR0(%r11)
  57. movq %cr3, %rax
  58. movq %rax, CR3(%r11)
  59. movq %cr4, %rax
  60. movq %rax, CR4(%r11)
  61. /* Save CR4. Required to enable the right paging mode later. */
  62. movq %rax, %r13
  63. /* zero out flags, and disable interrupts */
  64. pushq $0
  65. popfq
  66. /* Save SME active flag */
  67. movq %r8, %r12
  68. /*
  69. * get physical address of control page now
  70. * this is impossible after page table switch
  71. */
  72. movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
  73. /* get physical address of page table now too */
  74. movq PTR(PA_TABLE_PAGE)(%rsi), %r9
  75. /* get physical address of swap page now */
  76. movq PTR(PA_SWAP_PAGE)(%rsi), %r10
  77. /* save some information for jumping back */
  78. movq %r9, CP_PA_TABLE_PAGE(%r11)
  79. movq %r10, CP_PA_SWAP_PAGE(%r11)
  80. movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11)
  81. /* Switch to the identity mapped page tables */
  82. movq %r9, %cr3
  83. /* setup a new stack at the end of the physical control page */
  84. lea PAGE_SIZE(%r8), %rsp
  85. /* jump to identity mapped page */
  86. addq $(identity_mapped - relocate_kernel), %r8
  87. pushq %r8
  88. ret
  89. SYM_CODE_END(relocate_kernel)
  90. SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
  91. UNWIND_HINT_EMPTY
  92. /* set return address to 0 if not preserving context */
  93. pushq $0
  94. /* store the start address on the stack */
  95. pushq %rdx
  96. /*
  97. * Set cr0 to a known state:
  98. * - Paging enabled
  99. * - Alignment check disabled
  100. * - Write protect disabled
  101. * - No task switch
  102. * - Don't do FP software emulation.
  103. * - Proctected mode enabled
  104. */
  105. movq %cr0, %rax
  106. andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
  107. orl $(X86_CR0_PG | X86_CR0_PE), %eax
  108. movq %rax, %cr0
  109. /*
  110. * Set cr4 to a known state:
  111. * - physical address extension enabled
  112. * - 5-level paging, if it was enabled before
  113. */
  114. movl $X86_CR4_PAE, %eax
  115. testq $X86_CR4_LA57, %r13
  116. jz 1f
  117. orl $X86_CR4_LA57, %eax
  118. 1:
  119. movq %rax, %cr4
  120. jmp 1f
  121. 1:
  122. /* Flush the TLB (needed?) */
  123. movq %r9, %cr3
  124. /*
  125. * If SME is active, there could be old encrypted cache line
  126. * entries that will conflict with the now unencrypted memory
  127. * used by kexec. Flush the caches before copying the kernel.
  128. */
  129. testq %r12, %r12
  130. jz 1f
  131. wbinvd
  132. 1:
  133. movq %rcx, %r11
  134. call swap_pages
  135. /*
  136. * To be certain of avoiding problems with self-modifying code
  137. * I need to execute a serializing instruction here.
  138. * So I flush the TLB by reloading %cr3 here, it's handy,
  139. * and not processor dependent.
  140. */
  141. movq %cr3, %rax
  142. movq %rax, %cr3
  143. /*
  144. * set all of the registers to known values
  145. * leave %rsp alone
  146. */
  147. testq %r11, %r11
  148. jnz 1f
  149. xorl %eax, %eax
  150. xorl %ebx, %ebx
  151. xorl %ecx, %ecx
  152. xorl %edx, %edx
  153. xorl %esi, %esi
  154. xorl %edi, %edi
  155. xorl %ebp, %ebp
  156. xorl %r8d, %r8d
  157. xorl %r9d, %r9d
  158. xorl %r10d, %r10d
  159. xorl %r11d, %r11d
  160. xorl %r12d, %r12d
  161. xorl %r13d, %r13d
  162. xorl %r14d, %r14d
  163. xorl %r15d, %r15d
  164. ret
  165. 1:
  166. popq %rdx
  167. leaq PAGE_SIZE(%r10), %rsp
  168. ANNOTATE_RETPOLINE_SAFE
  169. call *%rdx
  170. /* get the re-entry point of the peer system */
  171. movq 0(%rsp), %rbp
  172. leaq relocate_kernel(%rip), %r8
  173. movq CP_PA_SWAP_PAGE(%r8), %r10
  174. movq CP_PA_BACKUP_PAGES_MAP(%r8), %rdi
  175. movq CP_PA_TABLE_PAGE(%r8), %rax
  176. movq %rax, %cr3
  177. lea PAGE_SIZE(%r8), %rsp
  178. call swap_pages
  179. movq $virtual_mapped, %rax
  180. pushq %rax
  181. ret
  182. SYM_CODE_END(identity_mapped)
  183. SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped)
  184. UNWIND_HINT_EMPTY
  185. movq RSP(%r8), %rsp
  186. movq CR4(%r8), %rax
  187. movq %rax, %cr4
  188. movq CR3(%r8), %rax
  189. movq CR0(%r8), %r8
  190. movq %rax, %cr3
  191. movq %r8, %cr0
  192. movq %rbp, %rax
  193. popf
  194. popq %r15
  195. popq %r14
  196. popq %r13
  197. popq %r12
  198. popq %rbp
  199. popq %rbx
  200. ret
  201. SYM_CODE_END(virtual_mapped)
  202. /* Do the copies */
  203. SYM_CODE_START_LOCAL_NOALIGN(swap_pages)
  204. UNWIND_HINT_EMPTY
  205. movq %rdi, %rcx /* Put the page_list in %rcx */
  206. xorl %edi, %edi
  207. xorl %esi, %esi
  208. jmp 1f
  209. 0: /* top, read another word for the indirection page */
  210. movq (%rbx), %rcx
  211. addq $8, %rbx
  212. 1:
  213. testb $0x1, %cl /* is it a destination page? */
  214. jz 2f
  215. movq %rcx, %rdi
  216. andq $0xfffffffffffff000, %rdi
  217. jmp 0b
  218. 2:
  219. testb $0x2, %cl /* is it an indirection page? */
  220. jz 2f
  221. movq %rcx, %rbx
  222. andq $0xfffffffffffff000, %rbx
  223. jmp 0b
  224. 2:
  225. testb $0x4, %cl /* is it the done indicator? */
  226. jz 2f
  227. jmp 3f
  228. 2:
  229. testb $0x8, %cl /* is it the source indicator? */
  230. jz 0b /* Ignore it otherwise */
  231. movq %rcx, %rsi /* For ever source page do a copy */
  232. andq $0xfffffffffffff000, %rsi
  233. movq %rdi, %rdx
  234. movq %rsi, %rax
  235. movq %r10, %rdi
  236. movl $512, %ecx
  237. rep ; movsq
  238. movq %rax, %rdi
  239. movq %rdx, %rsi
  240. movl $512, %ecx
  241. rep ; movsq
  242. movq %rdx, %rdi
  243. movq %r10, %rsi
  244. movl $512, %ecx
  245. rep ; movsq
  246. lea PAGE_SIZE(%rax), %rsi
  247. jmp 0b
  248. 3:
  249. ret
  250. SYM_CODE_END(swap_pages)
  251. .globl kexec_control_code_size
  252. .set kexec_control_code_size, . - relocate_kernel