2 * relocate_kernel.S - put the kernel image in place to boot
3 * Copyright (C) 2002-2005 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 << 3)
20 #define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
25 .globl relocate_kernel
27 /* %rdi indirection_page
32 /* map the control page at its virtual address */
34 movq $0x0000ff8000000000, %r10 /* mask */
35 mov $(39 - 3), %cl /* bits to shift */
36 movq PTR(VA_CONTROL_PAGE)(%rsi), %r11 /* address to map */
42 movq PTR(VA_PGD)(%rsi), %r8
44 movq PTR(PA_PUD_0)(%rsi), %r8
55 movq PTR(VA_PUD_0)(%rsi), %r8
57 movq PTR(PA_PMD_0)(%rsi), %r8
68 movq PTR(VA_PMD_0)(%rsi), %r8
70 movq PTR(PA_PTE_0)(%rsi), %r8
81 movq PTR(VA_PTE_0)(%rsi), %r8
83 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
87 /* identity map the control page at its physical address */
89 movq $0x0000ff8000000000, %r10 /* mask */
90 mov $(39 - 3), %cl /* bits to shift */
91 movq PTR(PA_CONTROL_PAGE)(%rsi), %r11 /* address to map */
97 movq PTR(VA_PGD)(%rsi), %r8
99 movq PTR(PA_PUD_1)(%rsi), %r8
110 movq PTR(VA_PUD_1)(%rsi), %r8
112 movq PTR(PA_PMD_1)(%rsi), %r8
123 movq PTR(VA_PMD_1)(%rsi), %r8
125 movq PTR(PA_PTE_1)(%rsi), %r8
136 movq PTR(VA_PTE_1)(%rsi), %r8
138 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
143 /* %rdi indirection_page
148 /* zero out flags, and disable interrupts */
152 /* get physical address of control page now */
153 /* this is impossible after page table switch */
154 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
156 /* get physical address of page table now too */
157 movq PTR(PA_TABLE_PAGE)(%rsi), %rcx
159 /* switch to new set of page tables */
160 movq PTR(PA_PGD)(%rsi), %r9
163 /* setup a new stack at the end of the physical control page */
164 lea PAGE_SIZE(%r8), %rsp
166 /* jump to identity mapped page */
167 addq $(identity_mapped - relocate_kernel), %r8
172 /* store the start address on the stack */
175 /* Set cr0 to a known state:
177 * - Alignment check disabled
178 * - Write protect disabled
180 * - Don't do FP software emulation.
181 * - Proctected mode enabled
184 andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
185 orl $(X86_CR0_PG | X86_CR0_PE), %eax
188 /* Set cr4 to a known state:
189 * - physical address extension enabled
191 movq $X86_CR4_PAE, %rax
197 /* Switch to the identity mapped page tables,
203 movq %rdi, %rcx /* Put the page_list in %rcx */
208 0: /* top, read another word for the indirection page */
213 testq $0x1, %rcx /* is it a destination page? */
216 andq $0xfffffffffffff000, %rdi
219 testq $0x2, %rcx /* is it an indirection page? */
222 andq $0xfffffffffffff000, %rbx
225 testq $0x4, %rcx /* is it the done indicator? */
229 testq $0x8, %rcx /* is it the source indicator? */
230 jz 0b /* Ignore it otherwise */
231 movq %rcx, %rsi /* For ever source page do a copy */
232 andq $0xfffffffffffff000, %rsi
239 /* To be certain of avoiding problems with self-modifying code
240 * I need to execute a serializing instruction here.
241 * So I flush the TLB by reloading %cr3 here, it's handy,
242 * and not processor dependent.
247 /* set all of the registers to known values */
248 /* leave %rsp alone */