relocate_kernel_32.S 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #include <asm/pgtable.h>
  13. /*
  14. * Must be relocatable PIC code callable as a C function
  15. */
  16. #define PTR(x) (x << 2)
  17. #define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  18. #define PAE_PGD_ATTR (_PAGE_PRESENT)
  19. /* control_page + PAGE_SIZE/2 ~ control_page + PAGE_SIZE * 3/4 are
  20. * used to save some data for jumping back
  21. */
  22. #define DATA(offset) (PAGE_SIZE/2+(offset))
  23. /* Minimal CPU state */
  24. #define ESP DATA(0x0)
  25. #define CR0 DATA(0x4)
  26. #define CR3 DATA(0x8)
  27. #define CR4 DATA(0xc)
  28. /* other data */
  29. #define CP_VA_CONTROL_PAGE DATA(0x10)
  30. #define CP_PA_PGD DATA(0x14)
  31. #define CP_PA_SWAP_PAGE DATA(0x18)
  32. #define CP_PA_BACKUP_PAGES_MAP DATA(0x1c)
  33. .text
  34. .align PAGE_SIZE
  35. .globl relocate_kernel
  36. relocate_kernel:
  37. /* Save the CPU context, used for jumping back */
  38. pushl %ebx
  39. pushl %esi
  40. pushl %edi
  41. pushl %ebp
  42. pushf
  43. movl 20+8(%esp), %ebp /* list of pages */
  44. movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
  45. movl %esp, ESP(%edi)
  46. movl %cr0, %eax
  47. movl %eax, CR0(%edi)
  48. movl %cr3, %eax
  49. movl %eax, CR3(%edi)
  50. movl %cr4, %eax
  51. movl %eax, CR4(%edi)
  52. #ifdef CONFIG_X86_PAE
  53. /* map the control page at its virtual address */
  54. movl PTR(VA_PGD)(%ebp), %edi
  55. movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
  56. andl $0xc0000000, %eax
  57. shrl $27, %eax
  58. addl %edi, %eax
  59. movl PTR(PA_PMD_0)(%ebp), %edx
  60. orl $PAE_PGD_ATTR, %edx
  61. movl %edx, (%eax)
  62. movl PTR(VA_PMD_0)(%ebp), %edi
  63. movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
  64. andl $0x3fe00000, %eax
  65. shrl $18, %eax
  66. addl %edi, %eax
  67. movl PTR(PA_PTE_0)(%ebp), %edx
  68. orl $PAGE_ATTR, %edx
  69. movl %edx, (%eax)
  70. movl PTR(VA_PTE_0)(%ebp), %edi
  71. movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
  72. andl $0x001ff000, %eax
  73. shrl $9, %eax
  74. addl %edi, %eax
  75. movl PTR(PA_CONTROL_PAGE)(%ebp), %edx
  76. orl $PAGE_ATTR, %edx
  77. movl %edx, (%eax)
  78. /* identity map the control page at its physical address */
  79. movl PTR(VA_PGD)(%ebp), %edi
  80. movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
  81. andl $0xc0000000, %eax
  82. shrl $27, %eax
  83. addl %edi, %eax
  84. movl PTR(PA_PMD_1)(%ebp), %edx
  85. orl $PAE_PGD_ATTR, %edx
  86. movl %edx, (%eax)
  87. movl PTR(VA_PMD_1)(%ebp), %edi
  88. movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
  89. andl $0x3fe00000, %eax
  90. shrl $18, %eax
  91. addl %edi, %eax
  92. movl PTR(PA_PTE_1)(%ebp), %edx
  93. orl $PAGE_ATTR, %edx
  94. movl %edx, (%eax)
  95. movl PTR(VA_PTE_1)(%ebp), %edi
  96. movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
  97. andl $0x001ff000, %eax
  98. shrl $9, %eax
  99. addl %edi, %eax
  100. movl PTR(PA_CONTROL_PAGE)(%ebp), %edx
  101. orl $PAGE_ATTR, %edx
  102. movl %edx, (%eax)
  103. #else
  104. /* map the control page at its virtual address */
  105. movl PTR(VA_PGD)(%ebp), %edi
  106. movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
  107. andl $0xffc00000, %eax
  108. shrl $20, %eax
  109. addl %edi, %eax
  110. movl PTR(PA_PTE_0)(%ebp), %edx
  111. orl $PAGE_ATTR, %edx
  112. movl %edx, (%eax)
  113. movl PTR(VA_PTE_0)(%ebp), %edi
  114. movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
  115. andl $0x003ff000, %eax
  116. shrl $10, %eax
  117. addl %edi, %eax
  118. movl PTR(PA_CONTROL_PAGE)(%ebp), %edx
  119. orl $PAGE_ATTR, %edx
  120. movl %edx, (%eax)
  121. /* identity map the control page at its physical address */
  122. movl PTR(VA_PGD)(%ebp), %edi
  123. movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
  124. andl $0xffc00000, %eax
  125. shrl $20, %eax
  126. addl %edi, %eax
  127. movl PTR(PA_PTE_1)(%ebp), %edx
  128. orl $PAGE_ATTR, %edx
  129. movl %edx, (%eax)
  130. movl PTR(VA_PTE_1)(%ebp), %edi
  131. movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
  132. andl $0x003ff000, %eax
  133. shrl $10, %eax
  134. addl %edi, %eax
  135. movl PTR(PA_CONTROL_PAGE)(%ebp), %edx
  136. orl $PAGE_ATTR, %edx
  137. movl %edx, (%eax)
  138. #endif
  139. relocate_new_kernel:
  140. /* read the arguments and say goodbye to the stack */
  141. movl 20+4(%esp), %ebx /* page_list */
  142. movl 20+8(%esp), %ebp /* list of pages */
  143. movl 20+12(%esp), %edx /* start address */
  144. movl 20+16(%esp), %ecx /* cpu_has_pae */
  145. movl 20+20(%esp), %esi /* preserve_context */
  146. /* zero out flags, and disable interrupts */
  147. pushl $0
  148. popfl
  149. /* save some information for jumping back */
  150. movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
  151. movl %edi, CP_VA_CONTROL_PAGE(%edi)
  152. movl PTR(PA_PGD)(%ebp), %eax
  153. movl %eax, CP_PA_PGD(%edi)
  154. movl PTR(PA_SWAP_PAGE)(%ebp), %eax
  155. movl %eax, CP_PA_SWAP_PAGE(%edi)
  156. movl %ebx, CP_PA_BACKUP_PAGES_MAP(%edi)
  157. /* get physical address of control page now */
  158. /* this is impossible after page table switch */
  159. movl PTR(PA_CONTROL_PAGE)(%ebp), %edi
  160. /* switch to new set of page tables */
  161. movl PTR(PA_PGD)(%ebp), %eax
  162. movl %eax, %cr3
  163. /* setup a new stack at the end of the physical control page */
  164. lea PAGE_SIZE(%edi), %esp
  165. /* jump to identity mapped page */
  166. movl %edi, %eax
  167. addl $(identity_mapped - relocate_kernel), %eax
  168. pushl %eax
  169. ret
  170. identity_mapped:
  171. /* store the start address on the stack */
  172. pushl %edx
  173. /* Set cr0 to a known state:
  174. * - Paging disabled
  175. * - Alignment check disabled
  176. * - Write protect disabled
  177. * - No task switch
  178. * - Don't do FP software emulation.
  179. * - Proctected mode enabled
  180. */
  181. movl %cr0, %eax
  182. andl $~(X86_CR0_PG | X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %eax
  183. orl $(X86_CR0_PE), %eax
  184. movl %eax, %cr0
  185. /* clear cr4 if applicable */
  186. testl %ecx, %ecx
  187. jz 1f
  188. /* Set cr4 to a known state:
  189. * Setting everything to zero seems safe.
  190. */
  191. xorl %eax, %eax
  192. movl %eax, %cr4
  193. jmp 1f
  194. 1:
  195. /* Flush the TLB (needed?) */
  196. xorl %eax, %eax
  197. movl %eax, %cr3
  198. movl CP_PA_SWAP_PAGE(%edi), %eax
  199. pushl %eax
  200. pushl %ebx
  201. call swap_pages
  202. addl $8, %esp
  203. /* To be certain of avoiding problems with self-modifying code
  204. * I need to execute a serializing instruction here.
  205. * So I flush the TLB, it's handy, and not processor dependent.
  206. */
  207. xorl %eax, %eax
  208. movl %eax, %cr3
  209. /* set all of the registers to known values */
  210. /* leave %esp alone */
  211. testl %esi, %esi
  212. jnz 1f
  213. xorl %edi, %edi
  214. xorl %eax, %eax
  215. xorl %ebx, %ebx
  216. xorl %ecx, %ecx
  217. xorl %edx, %edx
  218. xorl %esi, %esi
  219. xorl %ebp, %ebp
  220. ret
  221. 1:
  222. popl %edx
  223. movl CP_PA_SWAP_PAGE(%edi), %esp
  224. addl $PAGE_SIZE, %esp
  225. 2:
  226. call *%edx
  227. /* get the re-entry point of the peer system */
  228. movl 0(%esp), %ebp
  229. call 1f
  230. 1:
  231. popl %ebx
  232. subl $(1b - relocate_kernel), %ebx
  233. movl CP_VA_CONTROL_PAGE(%ebx), %edi
  234. lea PAGE_SIZE(%ebx), %esp
  235. movl CP_PA_SWAP_PAGE(%ebx), %eax
  236. movl CP_PA_BACKUP_PAGES_MAP(%ebx), %edx
  237. pushl %eax
  238. pushl %edx
  239. call swap_pages
  240. addl $8, %esp
  241. movl CP_PA_PGD(%ebx), %eax
  242. movl %eax, %cr3
  243. movl %cr0, %eax
  244. orl $(1<<31), %eax
  245. movl %eax, %cr0
  246. lea PAGE_SIZE(%edi), %esp
  247. movl %edi, %eax
  248. addl $(virtual_mapped - relocate_kernel), %eax
  249. pushl %eax
  250. ret
  251. virtual_mapped:
  252. movl CR4(%edi), %eax
  253. movl %eax, %cr4
  254. movl CR3(%edi), %eax
  255. movl %eax, %cr3
  256. movl CR0(%edi), %eax
  257. movl %eax, %cr0
  258. movl ESP(%edi), %esp
  259. movl %ebp, %eax
  260. popf
  261. popl %ebp
  262. popl %edi
  263. popl %esi
  264. popl %ebx
  265. ret
  266. /* Do the copies */
  267. swap_pages:
  268. movl 8(%esp), %edx
  269. movl 4(%esp), %ecx
  270. pushl %ebp
  271. pushl %ebx
  272. pushl %edi
  273. pushl %esi
  274. movl %ecx, %ebx
  275. jmp 1f
  276. 0: /* top, read another word from the indirection page */
  277. movl (%ebx), %ecx
  278. addl $4, %ebx
  279. 1:
  280. testl $0x1, %ecx /* is it a destination page */
  281. jz 2f
  282. movl %ecx, %edi
  283. andl $0xfffff000, %edi
  284. jmp 0b
  285. 2:
  286. testl $0x2, %ecx /* is it an indirection page */
  287. jz 2f
  288. movl %ecx, %ebx
  289. andl $0xfffff000, %ebx
  290. jmp 0b
  291. 2:
  292. testl $0x4, %ecx /* is it the done indicator */
  293. jz 2f
  294. jmp 3f
  295. 2:
  296. testl $0x8, %ecx /* is it the source indicator */
  297. jz 0b /* Ignore it otherwise */
  298. movl %ecx, %esi /* For every source page do a copy */
  299. andl $0xfffff000, %esi
  300. movl %edi, %eax
  301. movl %esi, %ebp
  302. movl %edx, %edi
  303. movl $1024, %ecx
  304. rep ; movsl
  305. movl %ebp, %edi
  306. movl %eax, %esi
  307. movl $1024, %ecx
  308. rep ; movsl
  309. movl %eax, %edi
  310. movl %edx, %esi
  311. movl $1024, %ecx
  312. rep ; movsl
  313. lea PAGE_SIZE(%ebp), %esi
  314. jmp 0b
  315. 3:
  316. popl %esi
  317. popl %edi
  318. popl %ebx
  319. popl %ebp
  320. ret