relocate_kernel_64.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.h>
  10. #include <asm/kexec.h>
  11. /*
  12. * Must be relocatable PIC code callable as a C function
  13. */
  14. #define PTR(x) (x << 3)
  15. #define PAGE_ATTR 0x63 /* _PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY */
  16. .text
  17. .align PAGE_SIZE
  18. .code64
  19. .globl relocate_kernel
  20. relocate_kernel:
  21. /* %rdi indirection_page
  22. * %rsi page_list
  23. * %rdx start address
  24. */
  25. /* map the control page at its virtual address */
  26. movq $0x0000ff8000000000, %r10 /* mask */
  27. mov $(39 - 3), %cl /* bits to shift */
  28. movq PTR(VA_CONTROL_PAGE)(%rsi), %r11 /* address to map */
  29. movq %r11, %r9
  30. andq %r10, %r9
  31. shrq %cl, %r9
  32. movq PTR(VA_PGD)(%rsi), %r8
  33. addq %r8, %r9
  34. movq PTR(PA_PUD_0)(%rsi), %r8
  35. orq $PAGE_ATTR, %r8
  36. movq %r8, (%r9)
  37. shrq $9, %r10
  38. sub $9, %cl
  39. movq %r11, %r9
  40. andq %r10, %r9
  41. shrq %cl, %r9
  42. movq PTR(VA_PUD_0)(%rsi), %r8
  43. addq %r8, %r9
  44. movq PTR(PA_PMD_0)(%rsi), %r8
  45. orq $PAGE_ATTR, %r8
  46. movq %r8, (%r9)
  47. shrq $9, %r10
  48. sub $9, %cl
  49. movq %r11, %r9
  50. andq %r10, %r9
  51. shrq %cl, %r9
  52. movq PTR(VA_PMD_0)(%rsi), %r8
  53. addq %r8, %r9
  54. movq PTR(PA_PTE_0)(%rsi), %r8
  55. orq $PAGE_ATTR, %r8
  56. movq %r8, (%r9)
  57. shrq $9, %r10
  58. sub $9, %cl
  59. movq %r11, %r9
  60. andq %r10, %r9
  61. shrq %cl, %r9
  62. movq PTR(VA_PTE_0)(%rsi), %r8
  63. addq %r8, %r9
  64. movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
  65. orq $PAGE_ATTR, %r8
  66. movq %r8, (%r9)
  67. /* identity map the control page at its physical address */
  68. movq $0x0000ff8000000000, %r10 /* mask */
  69. mov $(39 - 3), %cl /* bits to shift */
  70. movq PTR(PA_CONTROL_PAGE)(%rsi), %r11 /* address to map */
  71. movq %r11, %r9
  72. andq %r10, %r9
  73. shrq %cl, %r9
  74. movq PTR(VA_PGD)(%rsi), %r8
  75. addq %r8, %r9
  76. movq PTR(PA_PUD_1)(%rsi), %r8
  77. orq $PAGE_ATTR, %r8
  78. movq %r8, (%r9)
  79. shrq $9, %r10
  80. sub $9, %cl
  81. movq %r11, %r9
  82. andq %r10, %r9
  83. shrq %cl, %r9
  84. movq PTR(VA_PUD_1)(%rsi), %r8
  85. addq %r8, %r9
  86. movq PTR(PA_PMD_1)(%rsi), %r8
  87. orq $PAGE_ATTR, %r8
  88. movq %r8, (%r9)
  89. shrq $9, %r10
  90. sub $9, %cl
  91. movq %r11, %r9
  92. andq %r10, %r9
  93. shrq %cl, %r9
  94. movq PTR(VA_PMD_1)(%rsi), %r8
  95. addq %r8, %r9
  96. movq PTR(PA_PTE_1)(%rsi), %r8
  97. orq $PAGE_ATTR, %r8
  98. movq %r8, (%r9)
  99. shrq $9, %r10
  100. sub $9, %cl
  101. movq %r11, %r9
  102. andq %r10, %r9
  103. shrq %cl, %r9
  104. movq PTR(VA_PTE_1)(%rsi), %r8
  105. addq %r8, %r9
  106. movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
  107. orq $PAGE_ATTR, %r8
  108. movq %r8, (%r9)
  109. relocate_new_kernel:
  110. /* %rdi indirection_page
  111. * %rsi page_list
  112. * %rdx start address
  113. */
  114. /* zero out flags, and disable interrupts */
  115. pushq $0
  116. popfq
  117. /* get physical address of control page now */
  118. /* this is impossible after page table switch */
  119. movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
  120. /* get physical address of page table now too */
  121. movq PTR(PA_TABLE_PAGE)(%rsi), %rcx
  122. /* switch to new set of page tables */
  123. movq PTR(PA_PGD)(%rsi), %r9
  124. movq %r9, %cr3
  125. /* setup a new stack at the end of the physical control page */
  126. lea PAGE_SIZE(%r8), %rsp
  127. /* jump to identity mapped page */
  128. addq $(identity_mapped - relocate_kernel), %r8
  129. pushq %r8
  130. ret
  131. identity_mapped:
  132. /* store the start address on the stack */
  133. pushq %rdx
  134. /* Set cr0 to a known state:
  135. * 31 1 == Paging enabled
  136. * 18 0 == Alignment check disabled
  137. * 16 0 == Write protect disabled
  138. * 3 0 == No task switch
  139. * 2 0 == Don't do FP software emulation.
  140. * 0 1 == Proctected mode enabled
  141. */
  142. movq %cr0, %rax
  143. andq $~((1<<18)|(1<<16)|(1<<3)|(1<<2)), %rax
  144. orl $((1<<31)|(1<<0)), %eax
  145. movq %rax, %cr0
  146. /* Set cr4 to a known state:
  147. * 10 0 == xmm exceptions disabled
  148. * 9 0 == xmm registers instructions disabled
  149. * 8 0 == performance monitoring counter disabled
  150. * 7 0 == page global disabled
  151. * 6 0 == machine check exceptions disabled
  152. * 5 1 == physical address extension enabled
  153. * 4 0 == page size extensions disabled
  154. * 3 0 == Debug extensions disabled
  155. * 2 0 == Time stamp disable (disabled)
  156. * 1 0 == Protected mode virtual interrupts disabled
  157. * 0 0 == VME disabled
  158. */
  159. movq $((1<<5)), %rax
  160. movq %rax, %cr4
  161. jmp 1f
  162. 1:
  163. /* Switch to the identity mapped page tables,
  164. * and flush the TLB.
  165. */
  166. movq %rcx, %cr3
  167. /* Do the copies */
  168. movq %rdi, %rcx /* Put the page_list in %rcx */
  169. xorq %rdi, %rdi
  170. xorq %rsi, %rsi
  171. jmp 1f
  172. 0: /* top, read another word for the indirection page */
  173. movq (%rbx), %rcx
  174. addq $8, %rbx
  175. 1:
  176. testq $0x1, %rcx /* is it a destination page? */
  177. jz 2f
  178. movq %rcx, %rdi
  179. andq $0xfffffffffffff000, %rdi
  180. jmp 0b
  181. 2:
  182. testq $0x2, %rcx /* is it an indirection page? */
  183. jz 2f
  184. movq %rcx, %rbx
  185. andq $0xfffffffffffff000, %rbx
  186. jmp 0b
  187. 2:
  188. testq $0x4, %rcx /* is it the done indicator? */
  189. jz 2f
  190. jmp 3f
  191. 2:
  192. testq $0x8, %rcx /* is it the source indicator? */
  193. jz 0b /* Ignore it otherwise */
  194. movq %rcx, %rsi /* For ever source page do a copy */
  195. andq $0xfffffffffffff000, %rsi
  196. movq $512, %rcx
  197. rep ; movsq
  198. jmp 0b
  199. 3:
  200. /* To be certain of avoiding problems with self-modifying code
  201. * I need to execute a serializing instruction here.
  202. * So I flush the TLB by reloading %cr3 here, it's handy,
  203. * and not processor dependent.
  204. */
  205. movq %cr3, %rax
  206. movq %rax, %cr3
  207. /* set all of the registers to known values */
  208. /* leave %rsp alone */
  209. xorq %rax, %rax
  210. xorq %rbx, %rbx
  211. xorq %rcx, %rcx
  212. xorq %rdx, %rdx
  213. xorq %rsi, %rsi
  214. xorq %rdi, %rdi
  215. xorq %rbp, %rbp
  216. xorq %r8, %r8
  217. xorq %r9, %r9
  218. xorq %r10, %r9
  219. xorq %r11, %r11
  220. xorq %r12, %r12
  221. xorq %r13, %r13
  222. xorq %r14, %r14
  223. xorq %r15, %r15
  224. ret