relocate_kernel_64.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /* store the start address on the stack */
  86. pushq %rdx
  87. /*
  88. * Set cr0 to a known state:
  89. * - Paging enabled
  90. * - Alignment check disabled
  91. * - Write protect disabled
  92. * - No task switch
  93. * - Don't do FP software emulation.
  94. * - Proctected mode enabled
  95. */
  96. movq %cr0, %rax
  97. andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
  98. orl $(X86_CR0_PG | X86_CR0_PE), %eax
  99. movq %rax, %cr0
  100. /*
  101. * Set cr4 to a known state:
  102. * - physical address extension enabled
  103. */
  104. movq $X86_CR4_PAE, %rax
  105. movq %rax, %cr4
  106. jmp 1f
  107. 1:
  108. /* Flush the TLB (needed?) */
  109. movq %r9, %cr3
  110. movq %rcx, %r11
  111. call swap_pages
  112. /*
  113. * To be certain of avoiding problems with self-modifying code
  114. * I need to execute a serializing instruction here.
  115. * So I flush the TLB by reloading %cr3 here, it's handy,
  116. * and not processor dependent.
  117. */
  118. movq %cr3, %rax
  119. movq %rax, %cr3
  120. /*
  121. * set all of the registers to known values
  122. * leave %rsp alone
  123. */
  124. testq %r11, %r11
  125. jnz 1f
  126. xorq %rax, %rax
  127. xorq %rbx, %rbx
  128. xorq %rcx, %rcx
  129. xorq %rdx, %rdx
  130. xorq %rsi, %rsi
  131. xorq %rdi, %rdi
  132. xorq %rbp, %rbp
  133. xorq %r8, %r8
  134. xorq %r9, %r9
  135. xorq %r10, %r9
  136. xorq %r11, %r11
  137. xorq %r12, %r12
  138. xorq %r13, %r13
  139. xorq %r14, %r14
  140. xorq %r15, %r15
  141. ret
  142. 1:
  143. popq %rdx
  144. leaq PAGE_SIZE(%r10), %rsp
  145. call *%rdx
  146. /* get the re-entry point of the peer system */
  147. movq 0(%rsp), %rbp
  148. call 1f
  149. 1:
  150. popq %r8
  151. subq $(1b - relocate_kernel), %r8
  152. movq CP_PA_SWAP_PAGE(%r8), %r10
  153. movq CP_PA_BACKUP_PAGES_MAP(%r8), %rdi
  154. movq CP_PA_TABLE_PAGE(%r8), %rax
  155. movq %rax, %cr3
  156. lea PAGE_SIZE(%r8), %rsp
  157. call swap_pages
  158. movq $virtual_mapped, %rax
  159. pushq %rax
  160. ret
  161. virtual_mapped:
  162. movq RSP(%r8), %rsp
  163. movq CR4(%r8), %rax
  164. movq %rax, %cr4
  165. movq CR3(%r8), %rax
  166. movq CR0(%r8), %r8
  167. movq %rax, %cr3
  168. movq %r8, %cr0
  169. movq %rbp, %rax
  170. popf
  171. popq %r15
  172. popq %r14
  173. popq %r13
  174. popq %r12
  175. popq %rbp
  176. popq %rbx
  177. ret
  178. /* Do the copies */
  179. swap_pages:
  180. movq %rdi, %rcx /* Put the page_list in %rcx */
  181. xorq %rdi, %rdi
  182. xorq %rsi, %rsi
  183. jmp 1f
  184. 0: /* top, read another word for the indirection page */
  185. movq (%rbx), %rcx
  186. addq $8, %rbx
  187. 1:
  188. testq $0x1, %rcx /* is it a destination page? */
  189. jz 2f
  190. movq %rcx, %rdi
  191. andq $0xfffffffffffff000, %rdi
  192. jmp 0b
  193. 2:
  194. testq $0x2, %rcx /* is it an indirection page? */
  195. jz 2f
  196. movq %rcx, %rbx
  197. andq $0xfffffffffffff000, %rbx
  198. jmp 0b
  199. 2:
  200. testq $0x4, %rcx /* is it the done indicator? */
  201. jz 2f
  202. jmp 3f
  203. 2:
  204. testq $0x8, %rcx /* is it the source indicator? */
  205. jz 0b /* Ignore it otherwise */
  206. movq %rcx, %rsi /* For ever source page do a copy */
  207. andq $0xfffffffffffff000, %rsi
  208. movq %rdi, %rdx
  209. movq %rsi, %rax
  210. movq %r10, %rdi
  211. movq $512, %rcx
  212. rep ; movsq
  213. movq %rax, %rdi
  214. movq %rdx, %rsi
  215. movq $512, %rcx
  216. rep ; movsq
  217. movq %rdx, %rdi
  218. movq %r10, %rsi
  219. movq $512, %rcx
  220. rep ; movsq
  221. lea PAGE_SIZE(%rax), %rsi
  222. jmp 0b
  223. 3:
  224. ret
  225. .globl kexec_control_code_size
  226. .set kexec_control_code_size, . - relocate_kernel