relocate_kernel_32.S 5.5 KB

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