relocate_kernel_64.S 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * relocate_kernel.S - put the kernel image in place to boot
  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/linkage.h>
  9. #include <asm/page_types.h>
  10. #include <asm/kexec.h>
  11. #include <asm/processor-flags.h>
  12. #include <asm/pgtable_types.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. .globl relocate_kernel
  37. relocate_kernel:
  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. identity_mapped:
  90. /* set return address to 0 if not preserving context */
  91. pushq $0
  92. /* store the start address on the stack */
  93. pushq %rdx
  94. /*
  95. * Set cr0 to a known state:
  96. * - Paging enabled
  97. * - Alignment check disabled
  98. * - Write protect disabled
  99. * - No task switch
  100. * - Don't do FP software emulation.
  101. * - Proctected mode enabled
  102. */
  103. movq %cr0, %rax
  104. andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
  105. orl $(X86_CR0_PG | X86_CR0_PE), %eax
  106. movq %rax, %cr0
  107. /*
  108. * Set cr4 to a known state:
  109. * - physical address extension enabled
  110. * - 5-level paging, if it was enabled before
  111. */
  112. movl $X86_CR4_PAE, %eax
  113. testq $X86_CR4_LA57, %r13
  114. jz 1f
  115. orl $X86_CR4_LA57, %eax
  116. 1:
  117. movq %rax, %cr4
  118. jmp 1f
  119. 1:
  120. /* Flush the TLB (needed?) */
  121. movq %r9, %cr3
  122. /*
  123. * If SME is active, there could be old encrypted cache line
  124. * entries that will conflict with the now unencrypted memory
  125. * used by kexec. Flush the caches before copying the kernel.
  126. */
  127. testq %r12, %r12
  128. jz 1f
  129. wbinvd
  130. 1:
  131. movq %rcx, %r11
  132. call swap_pages
  133. /*
  134. * To be certain of avoiding problems with self-modifying code
  135. * I need to execute a serializing instruction here.
  136. * So I flush the TLB by reloading %cr3 here, it's handy,
  137. * and not processor dependent.
  138. */
  139. movq %cr3, %rax
  140. movq %rax, %cr3
  141. /*
  142. * set all of the registers to known values
  143. * leave %rsp alone
  144. */
  145. testq %r11, %r11
  146. jnz 1f
  147. xorl %eax, %eax
  148. xorl %ebx, %ebx
  149. xorl %ecx, %ecx
  150. xorl %edx, %edx
  151. xorl %esi, %esi
  152. xorl %edi, %edi
  153. xorl %ebp, %ebp
  154. xorl %r8d, %r8d
  155. xorl %r9d, %r9d
  156. xorl %r10d, %r10d
  157. xorl %r11d, %r11d
  158. xorl %r12d, %r12d
  159. xorl %r13d, %r13d
  160. xorl %r14d, %r14d
  161. xorl %r15d, %r15d
  162. ret
  163. 1:
  164. popq %rdx
  165. leaq PAGE_SIZE(%r10), %rsp
  166. call *%rdx
  167. /* get the re-entry point of the peer system */
  168. movq 0(%rsp), %rbp
  169. call 1f
  170. 1:
  171. popq %r8
  172. subq $(1b - relocate_kernel), %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. virtual_mapped:
  183. movq RSP(%r8), %rsp
  184. movq CR4(%r8), %rax
  185. movq %rax, %cr4
  186. movq CR3(%r8), %rax
  187. movq CR0(%r8), %r8
  188. movq %rax, %cr3
  189. movq %r8, %cr0
  190. movq %rbp, %rax
  191. popf
  192. popq %r15
  193. popq %r14
  194. popq %r13
  195. popq %r12
  196. popq %rbp
  197. popq %rbx
  198. ret
  199. /* Do the copies */
  200. swap_pages:
  201. movq %rdi, %rcx /* Put the page_list in %rcx */
  202. xorl %edi, %edi
  203. xorl %esi, %esi
  204. jmp 1f
  205. 0: /* top, read another word for the indirection page */
  206. movq (%rbx), %rcx
  207. addq $8, %rbx
  208. 1:
  209. testb $0x1, %cl /* is it a destination page? */
  210. jz 2f
  211. movq %rcx, %rdi
  212. andq $0xfffffffffffff000, %rdi
  213. jmp 0b
  214. 2:
  215. testb $0x2, %cl /* is it an indirection page? */
  216. jz 2f
  217. movq %rcx, %rbx
  218. andq $0xfffffffffffff000, %rbx
  219. jmp 0b
  220. 2:
  221. testb $0x4, %cl /* is it the done indicator? */
  222. jz 2f
  223. jmp 3f
  224. 2:
  225. testb $0x8, %cl /* is it the source indicator? */
  226. jz 0b /* Ignore it otherwise */
  227. movq %rcx, %rsi /* For ever source page do a copy */
  228. andq $0xfffffffffffff000, %rsi
  229. movq %rdi, %rdx
  230. movq %rsi, %rax
  231. movq %r10, %rdi
  232. movl $512, %ecx
  233. rep ; movsq
  234. movq %rax, %rdi
  235. movq %rdx, %rsi
  236. movl $512, %ecx
  237. rep ; movsq
  238. movq %rdx, %rdi
  239. movq %r10, %rsi
  240. movl $512, %ecx
  241. rep ; movsq
  242. lea PAGE_SIZE(%rax), %rsi
  243. jmp 0b
  244. 3:
  245. ret
  246. .globl kexec_control_code_size
  247. .set kexec_control_code_size, . - relocate_kernel