2 * relocate_kernel.S - put the kernel image in place to boot
3 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
9 #include <linux/linkage.h>
11 #include <asm/kexec.h>
12 #include <asm/processor-flags.h>
13 #include <asm/pgtable.h>
16 * Must be relocatable PIC code callable as a C function
19 #define PTR(x) (x << 2)
20 #define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
21 #define PAE_PGD_ATTR (_PAGE_PRESENT)
23 /* control_page + KEXEC_CONTROL_CODE_MAX_SIZE
24 * ~ control_page + PAGE_SIZE are used as data storage and stack for
27 #define DATA(offset) (KEXEC_CONTROL_CODE_MAX_SIZE+(offset))
29 /* Minimal CPU state */
36 #define CP_VA_CONTROL_PAGE DATA(0x10)
37 #define CP_PA_PGD DATA(0x14)
38 #define CP_PA_SWAP_PAGE DATA(0x18)
39 #define CP_PA_BACKUP_PAGES_MAP DATA(0x1c)
43 .globl relocate_kernel
45 /* Save the CPU context, used for jumping back */
53 movl 20+8(%esp), %ebp /* list of pages */
54 movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
64 /* map the control page at its virtual address */
66 movl PTR(VA_PGD)(%ebp), %edi
67 movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
68 andl $0xc0000000, %eax
72 movl PTR(PA_PMD_0)(%ebp), %edx
73 orl $PAE_PGD_ATTR, %edx
76 movl PTR(VA_PMD_0)(%ebp), %edi
77 movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
78 andl $0x3fe00000, %eax
82 movl PTR(PA_PTE_0)(%ebp), %edx
86 movl PTR(VA_PTE_0)(%ebp), %edi
87 movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
88 andl $0x001ff000, %eax
92 movl PTR(PA_CONTROL_PAGE)(%ebp), %edx
96 /* identity map the control page at its physical address */
98 movl PTR(VA_PGD)(%ebp), %edi
99 movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
100 andl $0xc0000000, %eax
104 movl PTR(PA_PMD_1)(%ebp), %edx
105 orl $PAE_PGD_ATTR, %edx
108 movl PTR(VA_PMD_1)(%ebp), %edi
109 movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
110 andl $0x3fe00000, %eax
114 movl PTR(PA_PTE_1)(%ebp), %edx
118 movl PTR(VA_PTE_1)(%ebp), %edi
119 movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
120 andl $0x001ff000, %eax
124 movl PTR(PA_CONTROL_PAGE)(%ebp), %edx
128 /* map the control page at its virtual address */
130 movl PTR(VA_PGD)(%ebp), %edi
131 movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
132 andl $0xffc00000, %eax
136 movl PTR(PA_PTE_0)(%ebp), %edx
140 movl PTR(VA_PTE_0)(%ebp), %edi
141 movl PTR(VA_CONTROL_PAGE)(%ebp), %eax
142 andl $0x003ff000, %eax
146 movl PTR(PA_CONTROL_PAGE)(%ebp), %edx
150 /* identity map the control page at its physical address */
152 movl PTR(VA_PGD)(%ebp), %edi
153 movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
154 andl $0xffc00000, %eax
158 movl PTR(PA_PTE_1)(%ebp), %edx
162 movl PTR(VA_PTE_1)(%ebp), %edi
163 movl PTR(PA_CONTROL_PAGE)(%ebp), %eax
164 andl $0x003ff000, %eax
168 movl PTR(PA_CONTROL_PAGE)(%ebp), %edx
174 /* read the arguments and say goodbye to the stack */
175 movl 20+4(%esp), %ebx /* page_list */
176 movl 20+8(%esp), %ebp /* list of pages */
177 movl 20+12(%esp), %edx /* start address */
178 movl 20+16(%esp), %ecx /* cpu_has_pae */
179 movl 20+20(%esp), %esi /* preserve_context */
181 /* zero out flags, and disable interrupts */
185 /* save some information for jumping back */
186 movl PTR(VA_CONTROL_PAGE)(%ebp), %edi
187 movl %edi, CP_VA_CONTROL_PAGE(%edi)
188 movl PTR(PA_PGD)(%ebp), %eax
189 movl %eax, CP_PA_PGD(%edi)
190 movl PTR(PA_SWAP_PAGE)(%ebp), %eax
191 movl %eax, CP_PA_SWAP_PAGE(%edi)
192 movl %ebx, CP_PA_BACKUP_PAGES_MAP(%edi)
194 /* get physical address of control page now */
195 /* this is impossible after page table switch */
196 movl PTR(PA_CONTROL_PAGE)(%ebp), %edi
198 /* switch to new set of page tables */
199 movl PTR(PA_PGD)(%ebp), %eax
202 /* setup a new stack at the end of the physical control page */
203 lea PAGE_SIZE(%edi), %esp
205 /* jump to identity mapped page */
207 addl $(identity_mapped - relocate_kernel), %eax
212 /* store the start address on the stack */
215 /* Set cr0 to a known state:
217 * - Alignment check disabled
218 * - Write protect disabled
220 * - Don't do FP software emulation.
221 * - Proctected mode enabled
224 andl $~(X86_CR0_PG | X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %eax
225 orl $(X86_CR0_PE), %eax
228 /* clear cr4 if applicable */
231 /* Set cr4 to a known state:
232 * Setting everything to zero seems safe.
240 /* Flush the TLB (needed?) */
244 movl CP_PA_SWAP_PAGE(%edi), %eax
250 /* To be certain of avoiding problems with self-modifying code
251 * I need to execute a serializing instruction here.
252 * So I flush the TLB, it's handy, and not processor dependent.
257 /* set all of the registers to known values */
258 /* leave %esp alone */
272 movl CP_PA_SWAP_PAGE(%edi), %esp
273 addl $PAGE_SIZE, %esp
277 /* get the re-entry point of the peer system */
282 subl $(1b - relocate_kernel), %ebx
283 movl CP_VA_CONTROL_PAGE(%ebx), %edi
284 lea PAGE_SIZE(%ebx), %esp
285 movl CP_PA_SWAP_PAGE(%ebx), %eax
286 movl CP_PA_BACKUP_PAGES_MAP(%ebx), %edx
291 movl CP_PA_PGD(%ebx), %eax
296 lea PAGE_SIZE(%edi), %esp
298 addl $(virtual_mapped - relocate_kernel), %eax
330 0: /* top, read another word from the indirection page */
334 testl $0x1, %ecx /* is it a destination page */
337 andl $0xfffff000, %edi
340 testl $0x2, %ecx /* is it an indirection page */
343 andl $0xfffff000, %ebx
346 testl $0x4, %ecx /* is it the done indicator */
350 testl $0x8, %ecx /* is it the source indicator */
351 jz 0b /* Ignore it otherwise */
352 movl %ecx, %esi /* For every source page do a copy */
353 andl $0xfffff000, %esi
372 lea PAGE_SIZE(%ebp), %esi
381 .globl kexec_control_code_size
382 .set kexec_control_code_size, . - relocate_kernel