relocate_kernel_64.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. */
  44. /* Save the CPU context, used for jumping back */
  45. pushq %rbx
  46. pushq %rbp
  47. pushq %r12
  48. pushq %r13
  49. pushq %r14
  50. pushq %r15
  51. pushf
  52. movq PTR(VA_CONTROL_PAGE)(%rsi), %r11
  53. movq %rsp, RSP(%r11)
  54. movq %cr0, %rax
  55. movq %rax, CR0(%r11)
  56. movq %cr3, %rax
  57. movq %rax, CR3(%r11)
  58. movq %cr4, %rax
  59. movq %rax, CR4(%r11)
  60. /* zero out flags, and disable interrupts */
  61. pushq $0
  62. popfq
  63. /*
  64. * get physical address of control page now
  65. * this is impossible after page table switch
  66. */
  67. movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
  68. /* get physical address of page table now too */
  69. movq PTR(PA_TABLE_PAGE)(%rsi), %r9
  70. /* get physical address of swap page now */
  71. movq PTR(PA_SWAP_PAGE)(%rsi), %r10
  72. /* save some information for jumping back */
  73. movq %r9, CP_PA_TABLE_PAGE(%r11)
  74. movq %r10, CP_PA_SWAP_PAGE(%r11)
  75. movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11)
  76. /* Switch to the identity mapped page tables */
  77. movq %r9, %cr3
  78. /* setup a new stack at the end of the physical control page */
  79. lea PAGE_SIZE(%r8), %rsp
  80. /* jump to identity mapped page */
  81. addq $(identity_mapped - relocate_kernel), %r8
  82. pushq %r8
  83. ret
  84. identity_mapped:
  85. /* set return address to 0 if not preserving context */
  86. pushq $0
  87. /* store the start address on the stack */
  88. pushq %rdx
  89. /*
  90. * Set cr0 to a known state:
  91. * - Paging enabled
  92. * - Alignment check disabled
  93. * - Write protect disabled
  94. * - No task switch
  95. * - Don't do FP software emulation.
  96. * - Proctected mode enabled
  97. */
  98. movq %cr0, %rax
  99. andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
  100. orl $(X86_CR0_PG | X86_CR0_PE), %eax
  101. movq %rax, %cr0
  102. /*
  103. * Set cr4 to a known state:
  104. * - physical address extension enabled
  105. */
  106. movl $X86_CR4_PAE, %eax
  107. movq %rax, %cr4
  108. jmp 1f
  109. 1:
  110. /* Flush the TLB (needed?) */
  111. movq %r9, %cr3
  112. movq %rcx, %r11
  113. call swap_pages
  114. /*
  115. * To be certain of avoiding problems with self-modifying code
  116. * I need to execute a serializing instruction here.
  117. * So I flush the TLB by reloading %cr3 here, it's handy,
  118. * and not processor dependent.
  119. */
  120. movq %cr3, %rax
  121. movq %rax, %cr3
  122. /*
  123. * set all of the registers to known values
  124. * leave %rsp alone
  125. */
  126. testq %r11, %r11
  127. jnz 1f
  128. xorl %eax, %eax
  129. xorl %ebx, %ebx
  130. xorl %ecx, %ecx
  131. xorl %edx, %edx
  132. xorl %esi, %esi
  133. xorl %edi, %edi
  134. xorl %ebp, %ebp
  135. xorl %r8d, %r8d
  136. xorl %r9d, %r9d
  137. xorl %r10d, %r10d
  138. xorl %r11d, %r11d
  139. xorl %r12d, %r12d
  140. xorl %r13d, %r13d
  141. xorl %r14d, %r14d
  142. xorl %r15d, %r15d
  143. ret
  144. 1:
  145. popq %rdx
  146. leaq PAGE_SIZE(%r10), %rsp
  147. call *%rdx
  148. /* get the re-entry point of the peer system */
  149. movq 0(%rsp), %rbp
  150. call 1f
  151. 1:
  152. popq %r8
  153. subq $(1b - relocate_kernel), %r8
  154. movq CP_PA_SWAP_PAGE(%r8), %r10
  155. movq CP_PA_BACKUP_PAGES_MAP(%r8), %rdi
  156. movq CP_PA_TABLE_PAGE(%r8), %rax
  157. movq %rax, %cr3
  158. lea PAGE_SIZE(%r8), %rsp
  159. call swap_pages
  160. movq $virtual_mapped, %rax
  161. pushq %rax
  162. ret
  163. virtual_mapped:
  164. movq RSP(%r8), %rsp
  165. movq CR4(%r8), %rax
  166. movq %rax, %cr4
  167. movq CR3(%r8), %rax
  168. movq CR0(%r8), %r8
  169. movq %rax, %cr3
  170. movq %r8, %cr0
  171. movq %rbp, %rax
  172. popf
  173. popq %r15
  174. popq %r14
  175. popq %r13
  176. popq %r12
  177. popq %rbp
  178. popq %rbx
  179. ret
  180. /* Do the copies */
  181. swap_pages:
  182. movq %rdi, %rcx /* Put the page_list in %rcx */
  183. xorl %edi, %edi
  184. xorl %esi, %esi
  185. jmp 1f
  186. 0: /* top, read another word for the indirection page */
  187. movq (%rbx), %rcx
  188. addq $8, %rbx
  189. 1:
  190. testb $0x1, %cl /* is it a destination page? */
  191. jz 2f
  192. movq %rcx, %rdi
  193. andq $0xfffffffffffff000, %rdi
  194. jmp 0b
  195. 2:
  196. testb $0x2, %cl /* is it an indirection page? */
  197. jz 2f
  198. movq %rcx, %rbx
  199. andq $0xfffffffffffff000, %rbx
  200. jmp 0b
  201. 2:
  202. testb $0x4, %cl /* is it the done indicator? */
  203. jz 2f
  204. jmp 3f
  205. 2:
  206. testb $0x8, %cl /* is it the source indicator? */
  207. jz 0b /* Ignore it otherwise */
  208. movq %rcx, %rsi /* For ever source page do a copy */
  209. andq $0xfffffffffffff000, %rsi
  210. movq %rdi, %rdx
  211. movq %rsi, %rax
  212. movq %r10, %rdi
  213. movl $512, %ecx
  214. rep ; movsq
  215. movq %rax, %rdi
  216. movq %rdx, %rsi
  217. movl $512, %ecx
  218. rep ; movsq
  219. movq %rdx, %rdi
  220. movq %r10, %rsi
  221. movl $512, %ecx
  222. rep ; movsq
  223. lea PAGE_SIZE(%rax), %rsi
  224. jmp 0b
  225. 3:
  226. ret
  227. .globl kexec_control_code_size
  228. .set kexec_control_code_size, . - relocate_kernel