relocate_kernel_32.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. /* store the start address on the stack */
  84. pushl %edx
  85. /*
  86. * Set cr0 to a known state:
  87. * - Paging disabled
  88. * - Alignment check disabled
  89. * - Write protect disabled
  90. * - No task switch
  91. * - Don't do FP software emulation.
  92. * - Proctected mode enabled
  93. */
  94. movl %cr0, %eax
  95. andl $~(X86_CR0_PG | X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %eax
  96. orl $(X86_CR0_PE), %eax
  97. movl %eax, %cr0
  98. /* clear cr4 if applicable */
  99. testl %ecx, %ecx
  100. jz 1f
  101. /*
  102. * Set cr4 to a known state:
  103. * Setting everything to zero seems safe.
  104. */
  105. xorl %eax, %eax
  106. movl %eax, %cr4
  107. jmp 1f
  108. 1:
  109. /* Flush the TLB (needed?) */
  110. xorl %eax, %eax
  111. movl %eax, %cr3
  112. movl CP_PA_SWAP_PAGE(%edi), %eax
  113. pushl %eax
  114. pushl %ebx
  115. call swap_pages
  116. addl $8, %esp
  117. /*
  118. * To be certain of avoiding problems with self-modifying code
  119. * I need to execute a serializing instruction here.
  120. * So I flush the TLB, it's handy, and not processor dependent.
  121. */
  122. xorl %eax, %eax
  123. movl %eax, %cr3
  124. /*
  125. * set all of the registers to known values
  126. * leave %esp alone
  127. */
  128. testl %esi, %esi
  129. jnz 1f
  130. xorl %edi, %edi
  131. xorl %eax, %eax
  132. xorl %ebx, %ebx
  133. xorl %ecx, %ecx
  134. xorl %edx, %edx
  135. xorl %esi, %esi
  136. xorl %ebp, %ebp
  137. ret
  138. 1:
  139. popl %edx
  140. movl CP_PA_SWAP_PAGE(%edi), %esp
  141. addl $PAGE_SIZE, %esp
  142. 2:
  143. call *%edx
  144. /* get the re-entry point of the peer system */
  145. movl 0(%esp), %ebp
  146. call 1f
  147. 1:
  148. popl %ebx
  149. subl $(1b - relocate_kernel), %ebx
  150. movl CP_VA_CONTROL_PAGE(%ebx), %edi
  151. lea PAGE_SIZE(%ebx), %esp
  152. movl CP_PA_SWAP_PAGE(%ebx), %eax
  153. movl CP_PA_BACKUP_PAGES_MAP(%ebx), %edx
  154. pushl %eax
  155. pushl %edx
  156. call swap_pages
  157. addl $8, %esp
  158. movl CP_PA_PGD(%ebx), %eax
  159. movl %eax, %cr3
  160. movl %cr0, %eax
  161. orl $(1<<31), %eax
  162. movl %eax, %cr0
  163. lea PAGE_SIZE(%edi), %esp
  164. movl %edi, %eax
  165. addl $(virtual_mapped - relocate_kernel), %eax
  166. pushl %eax
  167. ret
  168. virtual_mapped:
  169. movl CR4(%edi), %eax
  170. movl %eax, %cr4
  171. movl CR3(%edi), %eax
  172. movl %eax, %cr3
  173. movl CR0(%edi), %eax
  174. movl %eax, %cr0
  175. movl ESP(%edi), %esp
  176. movl %ebp, %eax
  177. popf
  178. popl %ebp
  179. popl %edi
  180. popl %esi
  181. popl %ebx
  182. ret
  183. /* Do the copies */
  184. swap_pages:
  185. movl 8(%esp), %edx
  186. movl 4(%esp), %ecx
  187. pushl %ebp
  188. pushl %ebx
  189. pushl %edi
  190. pushl %esi
  191. movl %ecx, %ebx
  192. jmp 1f
  193. 0: /* top, read another word from the indirection page */
  194. movl (%ebx), %ecx
  195. addl $4, %ebx
  196. 1:
  197. testl $0x1, %ecx /* is it a destination page */
  198. jz 2f
  199. movl %ecx, %edi
  200. andl $0xfffff000, %edi
  201. jmp 0b
  202. 2:
  203. testl $0x2, %ecx /* is it an indirection page */
  204. jz 2f
  205. movl %ecx, %ebx
  206. andl $0xfffff000, %ebx
  207. jmp 0b
  208. 2:
  209. testl $0x4, %ecx /* is it the done indicator */
  210. jz 2f
  211. jmp 3f
  212. 2:
  213. testl $0x8, %ecx /* is it the source indicator */
  214. jz 0b /* Ignore it otherwise */
  215. movl %ecx, %esi /* For every source page do a copy */
  216. andl $0xfffff000, %esi
  217. movl %edi, %eax
  218. movl %esi, %ebp
  219. movl %edx, %edi
  220. movl $1024, %ecx
  221. rep ; movsl
  222. movl %ebp, %edi
  223. movl %eax, %esi
  224. movl $1024, %ecx
  225. rep ; movsl
  226. movl %eax, %edi
  227. movl %edx, %esi
  228. movl $1024, %ecx
  229. rep ; movsl
  230. lea PAGE_SIZE(%ebp), %esi
  231. jmp 0b
  232. 3:
  233. popl %esi
  234. popl %edi
  235. popl %ebx
  236. popl %ebp
  237. ret
  238. .globl kexec_control_code_size
  239. .set kexec_control_code_size, . - relocate_kernel