relocate_kernel_32.S 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * relocate_kernel.S - put the kernel image in place to boot
  3. * Copyright (C) 2002-2004 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. /*
  13. * Must be relocatable PIC code callable as a C function
  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. .globl relocate_kernel
  34. relocate_kernel:
  35. /* Save the CPU context, used for jumping back */
  36. pushl %ebx
  37. pushl %esi
  38. pushl %edi
  39. pushl %ebp
  40. pushf
  41. movl 20+8(%esp), %ebp /* list of pages */
  42. movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
  43. movl %esp, ESP(%edi)
  44. movl %cr0, %eax
  45. movl %eax, CR0(%edi)
  46. movl %cr3, %eax
  47. movl %eax, CR3(%edi)
  48. movl %cr4, %eax
  49. movl %eax, CR4(%edi)
  50. /* read the arguments and say goodbye to the stack */
  51. movl 20+4(%esp), %ebx /* page_list */
  52. movl 20+8(%esp), %ebp /* list of pages */
  53. movl 20+12(%esp), %edx /* start address */
  54. movl 20+16(%esp), %ecx /* cpu_has_pae */
  55. movl 20+20(%esp), %esi /* preserve_context */
  56. /* zero out flags, and disable interrupts */
  57. pushl $0
  58. popfl
  59. /* save some information for jumping back */
  60. movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
  61. movl %edi, CP_VA_CONTROL_PAGE(%edi)
  62. movl PTR(PA_PGD)(%ebp), %eax
  63. movl %eax, CP_PA_PGD(%edi)
  64. movl PTR(PA_SWAP_PAGE)(%ebp), %eax
  65. movl %eax, CP_PA_SWAP_PAGE(%edi)
  66. movl %ebx, CP_PA_BACKUP_PAGES_MAP(%edi)
  67. /*
  68. * get physical address of control page now
  69. * this is impossible after page table switch
  70. */
  71. movl PTR(PA_CONTROL_PAGE)(%ebp), %edi
  72. /* switch to new set of page tables */
  73. movl PTR(PA_PGD)(%ebp), %eax
  74. movl %eax, %cr3
  75. /* setup a new stack at the end of the physical control page */
  76. lea PAGE_SIZE(%edi), %esp
  77. /* jump to identity mapped page */
  78. movl %edi, %eax
  79. addl $(identity_mapped - relocate_kernel), %eax
  80. pushl %eax
  81. ret
  82. identity_mapped:
  83. /* set return address to 0 if not preserving context */
  84. pushl $0
  85. /* store the start address on the stack */
  86. pushl %edx
  87. /*
  88. * Set cr0 to a known state:
  89. * - Paging disabled
  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. movl %cr0, %eax
  97. andl $~(X86_CR0_PG | X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %eax
  98. orl $(X86_CR0_PE), %eax
  99. movl %eax, %cr0
  100. /* clear cr4 if applicable */
  101. testl %ecx, %ecx
  102. jz 1f
  103. /*
  104. * Set cr4 to a known state:
  105. * Setting everything to zero seems safe.
  106. */
  107. xorl %eax, %eax
  108. movl %eax, %cr4
  109. jmp 1f
  110. 1:
  111. /* Flush the TLB (needed?) */
  112. xorl %eax, %eax
  113. movl %eax, %cr3
  114. movl CP_PA_SWAP_PAGE(%edi), %eax
  115. pushl %eax
  116. pushl %ebx
  117. call swap_pages
  118. addl $8, %esp
  119. /*
  120. * To be certain of avoiding problems with self-modifying code
  121. * I need to execute a serializing instruction here.
  122. * So I flush the TLB, it's handy, and not processor dependent.
  123. */
  124. xorl %eax, %eax
  125. movl %eax, %cr3
  126. /*
  127. * set all of the registers to known values
  128. * leave %esp alone
  129. */
  130. testl %esi, %esi
  131. jnz 1f
  132. xorl %edi, %edi
  133. xorl %eax, %eax
  134. xorl %ebx, %ebx
  135. xorl %ecx, %ecx
  136. xorl %edx, %edx
  137. xorl %esi, %esi
  138. xorl %ebp, %ebp
  139. ret
  140. 1:
  141. popl %edx
  142. movl CP_PA_SWAP_PAGE(%edi), %esp
  143. addl $PAGE_SIZE, %esp
  144. 2:
  145. call *%edx
  146. /* get the re-entry point of the peer system */
  147. movl 0(%esp), %ebp
  148. call 1f
  149. 1:
  150. popl %ebx
  151. subl $(1b - relocate_kernel), %ebx
  152. movl CP_VA_CONTROL_PAGE(%ebx), %edi
  153. lea PAGE_SIZE(%ebx), %esp
  154. movl CP_PA_SWAP_PAGE(%ebx), %eax
  155. movl CP_PA_BACKUP_PAGES_MAP(%ebx), %edx
  156. pushl %eax
  157. pushl %edx
  158. call swap_pages
  159. addl $8, %esp
  160. movl CP_PA_PGD(%ebx), %eax
  161. movl %eax, %cr3
  162. movl %cr0, %eax
  163. orl $X86_CR0_PG, %eax
  164. movl %eax, %cr0
  165. lea PAGE_SIZE(%edi), %esp
  166. movl %edi, %eax
  167. addl $(virtual_mapped - relocate_kernel), %eax
  168. pushl %eax
  169. ret
  170. virtual_mapped:
  171. movl CR4(%edi), %eax
  172. movl %eax, %cr4
  173. movl CR3(%edi), %eax
  174. movl %eax, %cr3
  175. movl CR0(%edi), %eax
  176. movl %eax, %cr0
  177. movl ESP(%edi), %esp
  178. movl %ebp, %eax
  179. popf
  180. popl %ebp
  181. popl %edi
  182. popl %esi
  183. popl %ebx
  184. ret
  185. /* Do the copies */
  186. swap_pages:
  187. movl 8(%esp), %edx
  188. movl 4(%esp), %ecx
  189. pushl %ebp
  190. pushl %ebx
  191. pushl %edi
  192. pushl %esi
  193. movl %ecx, %ebx
  194. jmp 1f
  195. 0: /* top, read another word from the indirection page */
  196. movl (%ebx), %ecx
  197. addl $4, %ebx
  198. 1:
  199. testb $0x1, %cl /* is it a destination page */
  200. jz 2f
  201. movl %ecx, %edi
  202. andl $0xfffff000, %edi
  203. jmp 0b
  204. 2:
  205. testb $0x2, %cl /* is it an indirection page */
  206. jz 2f
  207. movl %ecx, %ebx
  208. andl $0xfffff000, %ebx
  209. jmp 0b
  210. 2:
  211. testb $0x4, %cl /* is it the done indicator */
  212. jz 2f
  213. jmp 3f
  214. 2:
  215. testb $0x8, %cl /* is it the source indicator */
  216. jz 0b /* Ignore it otherwise */
  217. movl %ecx, %esi /* For every source page do a copy */
  218. andl $0xfffff000, %esi
  219. movl %edi, %eax
  220. movl %esi, %ebp
  221. movl %edx, %edi
  222. movl $1024, %ecx
  223. rep ; movsl
  224. movl %ebp, %edi
  225. movl %eax, %esi
  226. movl $1024, %ecx
  227. rep ; movsl
  228. movl %eax, %edi
  229. movl %edx, %esi
  230. movl $1024, %ecx
  231. rep ; movsl
  232. lea PAGE_SIZE(%ebp), %esi
  233. jmp 0b
  234. 3:
  235. popl %esi
  236. popl %edi
  237. popl %ebx
  238. popl %ebp
  239. ret
  240. .globl kexec_control_code_size
  241. .set kexec_control_code_size, . - relocate_kernel