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