relocate_kernel_32.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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-2004 Eric Biederman <[email protected]>
  5. */
  6. #include <linux/linkage.h>
  7. #include <asm/page_types.h>
  8. #include <asm/kexec.h>
  9. #include <asm/nospec-branch.h>
  10. #include <asm/processor-flags.h>
  11. /*
  12. * Must be relocatable PIC code callable as a C function, in particular
  13. * there must be a plain RET and not jump to return thunk.
  14. */
  15. #define PTR(x) (x << 2)
  16. /*
  17. * control_page + KEXEC_CONTROL_CODE_MAX_SIZE
  18. * ~ control_page + PAGE_SIZE are used as data storage and stack for
  19. * jumping back
  20. */
  21. #define DATA(offset) (KEXEC_CONTROL_CODE_MAX_SIZE+(offset))
  22. /* Minimal CPU state */
  23. #define ESP DATA(0x0)
  24. #define CR0 DATA(0x4)
  25. #define CR3 DATA(0x8)
  26. #define CR4 DATA(0xc)
  27. /* other data */
  28. #define CP_VA_CONTROL_PAGE DATA(0x10)
  29. #define CP_PA_PGD DATA(0x14)
  30. #define CP_PA_SWAP_PAGE DATA(0x18)
  31. #define CP_PA_BACKUP_PAGES_MAP DATA(0x1c)
  32. .text
  33. SYM_CODE_START_NOALIGN(relocate_kernel)
  34. /* Save the CPU context, used for jumping back */
  35. pushl %ebx
  36. pushl %esi
  37. pushl %edi
  38. pushl %ebp
  39. pushf
  40. movl 20+8(%esp), %ebp /* list of pages */
  41. movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
  42. movl %esp, ESP(%edi)
  43. movl %cr0, %eax
  44. movl %eax, CR0(%edi)
  45. movl %cr3, %eax
  46. movl %eax, CR3(%edi)
  47. movl %cr4, %eax
  48. movl %eax, CR4(%edi)
  49. /* read the arguments and say goodbye to the stack */
  50. movl 20+4(%esp), %ebx /* page_list */
  51. movl 20+8(%esp), %ebp /* list of pages */
  52. movl 20+12(%esp), %edx /* start address */
  53. movl 20+16(%esp), %ecx /* cpu_has_pae */
  54. movl 20+20(%esp), %esi /* preserve_context */
  55. /* zero out flags, and disable interrupts */
  56. pushl $0
  57. popfl
  58. /* save some information for jumping back */
  59. movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
  60. movl %edi, CP_VA_CONTROL_PAGE(%edi)
  61. movl PTR(PA_PGD)(%ebp), %eax
  62. movl %eax, CP_PA_PGD(%edi)
  63. movl PTR(PA_SWAP_PAGE)(%ebp), %eax
  64. movl %eax, CP_PA_SWAP_PAGE(%edi)
  65. movl %ebx, CP_PA_BACKUP_PAGES_MAP(%edi)
  66. /*
  67. * get physical address of control page now
  68. * this is impossible after page table switch
  69. */
  70. movl PTR(PA_CONTROL_PAGE)(%ebp), %edi
  71. /* switch to new set of page tables */
  72. movl PTR(PA_PGD)(%ebp), %eax
  73. movl %eax, %cr3
  74. /* setup a new stack at the end of the physical control page */
  75. lea PAGE_SIZE(%edi), %esp
  76. /* jump to identity mapped page */
  77. movl %edi, %eax
  78. addl $(identity_mapped - relocate_kernel), %eax
  79. pushl %eax
  80. ANNOTATE_UNRET_SAFE
  81. ret
  82. int3
  83. SYM_CODE_END(relocate_kernel)
  84. SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
  85. /* set return address to 0 if not preserving context */
  86. pushl $0
  87. /* store the start address on the stack */
  88. pushl %edx
  89. /*
  90. * Set cr0 to a known state:
  91. * - Paging disabled
  92. * - Alignment check disabled
  93. * - Write protect disabled
  94. * - No task switch
  95. * - Don't do FP software emulation.
  96. * - Protected mode enabled
  97. */
  98. movl %cr0, %eax
  99. andl $~(X86_CR0_PG | X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %eax
  100. orl $(X86_CR0_PE), %eax
  101. movl %eax, %cr0
  102. /* clear cr4 if applicable */
  103. testl %ecx, %ecx
  104. jz 1f
  105. /*
  106. * Set cr4 to a known state:
  107. * Setting everything to zero seems safe.
  108. */
  109. xorl %eax, %eax
  110. movl %eax, %cr4
  111. jmp 1f
  112. 1:
  113. /* Flush the TLB (needed?) */
  114. xorl %eax, %eax
  115. movl %eax, %cr3
  116. movl CP_PA_SWAP_PAGE(%edi), %eax
  117. pushl %eax
  118. pushl %ebx
  119. call swap_pages
  120. addl $8, %esp
  121. /*
  122. * To be certain of avoiding problems with self-modifying code
  123. * I need to execute a serializing instruction here.
  124. * So I flush the TLB, it's handy, and not processor dependent.
  125. */
  126. xorl %eax, %eax
  127. movl %eax, %cr3
  128. /*
  129. * set all of the registers to known values
  130. * leave %esp alone
  131. */
  132. testl %esi, %esi
  133. jnz 1f
  134. xorl %edi, %edi
  135. xorl %eax, %eax
  136. xorl %ebx, %ebx
  137. xorl %ecx, %ecx
  138. xorl %edx, %edx
  139. xorl %esi, %esi
  140. xorl %ebp, %ebp
  141. ANNOTATE_UNRET_SAFE
  142. ret
  143. int3
  144. 1:
  145. popl %edx
  146. movl CP_PA_SWAP_PAGE(%edi), %esp
  147. addl $PAGE_SIZE, %esp
  148. 2:
  149. ANNOTATE_RETPOLINE_SAFE
  150. call *%edx
  151. /* get the re-entry point of the peer system */
  152. movl 0(%esp), %ebp
  153. call 1f
  154. 1:
  155. popl %ebx
  156. subl $(1b - relocate_kernel), %ebx
  157. movl CP_VA_CONTROL_PAGE(%ebx), %edi
  158. lea PAGE_SIZE(%ebx), %esp
  159. movl CP_PA_SWAP_PAGE(%ebx), %eax
  160. movl CP_PA_BACKUP_PAGES_MAP(%ebx), %edx
  161. pushl %eax
  162. pushl %edx
  163. call swap_pages
  164. addl $8, %esp
  165. movl CP_PA_PGD(%ebx), %eax
  166. movl %eax, %cr3
  167. movl %cr0, %eax
  168. orl $X86_CR0_PG, %eax
  169. movl %eax, %cr0
  170. lea PAGE_SIZE(%edi), %esp
  171. movl %edi, %eax
  172. addl $(virtual_mapped - relocate_kernel), %eax
  173. pushl %eax
  174. ANNOTATE_UNRET_SAFE
  175. ret
  176. int3
  177. SYM_CODE_END(identity_mapped)
  178. SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped)
  179. movl CR4(%edi), %eax
  180. movl %eax, %cr4
  181. movl CR3(%edi), %eax
  182. movl %eax, %cr3
  183. movl CR0(%edi), %eax
  184. movl %eax, %cr0
  185. movl ESP(%edi), %esp
  186. movl %ebp, %eax
  187. popf
  188. popl %ebp
  189. popl %edi
  190. popl %esi
  191. popl %ebx
  192. ANNOTATE_UNRET_SAFE
  193. ret
  194. int3
  195. SYM_CODE_END(virtual_mapped)
  196. /* Do the copies */
  197. SYM_CODE_START_LOCAL_NOALIGN(swap_pages)
  198. movl 8(%esp), %edx
  199. movl 4(%esp), %ecx
  200. pushl %ebp
  201. pushl %ebx
  202. pushl %edi
  203. pushl %esi
  204. movl %ecx, %ebx
  205. jmp 1f
  206. 0: /* top, read another word from the indirection page */
  207. movl (%ebx), %ecx
  208. addl $4, %ebx
  209. 1:
  210. testb $0x1, %cl /* is it a destination page */
  211. jz 2f
  212. movl %ecx, %edi
  213. andl $0xfffff000, %edi
  214. jmp 0b
  215. 2:
  216. testb $0x2, %cl /* is it an indirection page */
  217. jz 2f
  218. movl %ecx, %ebx
  219. andl $0xfffff000, %ebx
  220. jmp 0b
  221. 2:
  222. testb $0x4, %cl /* is it the done indicator */
  223. jz 2f
  224. jmp 3f
  225. 2:
  226. testb $0x8, %cl /* is it the source indicator */
  227. jz 0b /* Ignore it otherwise */
  228. movl %ecx, %esi /* For every source page do a copy */
  229. andl $0xfffff000, %esi
  230. movl %edi, %eax
  231. movl %esi, %ebp
  232. movl %edx, %edi
  233. movl $1024, %ecx
  234. rep ; movsl
  235. movl %ebp, %edi
  236. movl %eax, %esi
  237. movl $1024, %ecx
  238. rep ; movsl
  239. movl %eax, %edi
  240. movl %edx, %esi
  241. movl $1024, %ecx
  242. rep ; movsl
  243. lea PAGE_SIZE(%ebp), %esi
  244. jmp 0b
  245. 3:
  246. popl %esi
  247. popl %edi
  248. popl %ebx
  249. popl %ebp
  250. ANNOTATE_UNRET_SAFE
  251. ret
  252. int3
  253. SYM_CODE_END(swap_pages)
  254. .globl kexec_control_code_size
  255. .set kexec_control_code_size, . - relocate_kernel