relocate_kernel_32.S 7.9 KB

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