relocate_kernel_64.S 6.4 KB

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