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>
14 * Must be relocatable PIC code callable as a C function
17 #define PTR(x) (x << 3)
18 #define PAGE_ALIGNED (1 << PAGE_SHIFT)
19 #define PAGE_ATTR 0x63 /* _PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY */
24 .globl relocate_kernel
26 /* %rdi indirection_page
31 /* map the control page at its virtual address */
33 movq $0x0000ff8000000000, %r10 /* mask */
34 mov $(39 - 3), %cl /* bits to shift */
35 movq PTR(VA_CONTROL_PAGE)(%rsi), %r11 /* address to map */
41 movq PTR(VA_PGD)(%rsi), %r8
43 movq PTR(PA_PUD_0)(%rsi), %r8
54 movq PTR(VA_PUD_0)(%rsi), %r8
56 movq PTR(PA_PMD_0)(%rsi), %r8
67 movq PTR(VA_PMD_0)(%rsi), %r8
69 movq PTR(PA_PTE_0)(%rsi), %r8
80 movq PTR(VA_PTE_0)(%rsi), %r8
82 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
86 /* identity map the control page at its physical address */
88 movq $0x0000ff8000000000, %r10 /* mask */
89 mov $(39 - 3), %cl /* bits to shift */
90 movq PTR(PA_CONTROL_PAGE)(%rsi), %r11 /* address to map */
96 movq PTR(VA_PGD)(%rsi), %r8
98 movq PTR(PA_PUD_1)(%rsi), %r8
109 movq PTR(VA_PUD_1)(%rsi), %r8
111 movq PTR(PA_PMD_1)(%rsi), %r8
122 movq PTR(VA_PMD_1)(%rsi), %r8
124 movq PTR(PA_PTE_1)(%rsi), %r8
135 movq PTR(VA_PTE_1)(%rsi), %r8
137 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
142 /* %rdi indirection_page
147 /* zero out flags, and disable interrupts */
151 /* get physical address of control page now */
152 /* this is impossible after page table switch */
153 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
155 /* get physical address of page table now too */
156 movq PTR(PA_TABLE_PAGE)(%rsi), %rcx
158 /* switch to new set of page tables */
159 movq PTR(PA_PGD)(%rsi), %r9
162 /* setup a new stack at the end of the physical control page */
165 /* jump to identity mapped page */
166 addq $(identity_mapped - relocate_kernel), %r8
171 /* store the start address on the stack */
174 /* Set cr0 to a known state:
175 * 31 1 == Paging enabled
176 * 18 0 == Alignment check disabled
177 * 16 0 == Write protect disabled
178 * 3 0 == No task switch
179 * 2 0 == Don't do FP software emulation.
180 * 0 1 == Proctected mode enabled
183 andq $~((1<<18)|(1<<16)|(1<<3)|(1<<2)), %rax
184 orl $((1<<31)|(1<<0)), %eax
187 /* Set cr4 to a known state:
188 * 10 0 == xmm exceptions disabled
189 * 9 0 == xmm registers instructions disabled
190 * 8 0 == performance monitoring counter disabled
191 * 7 0 == page global disabled
192 * 6 0 == machine check exceptions disabled
193 * 5 1 == physical address extension enabled
194 * 4 0 == page size extensions disabled
195 * 3 0 == Debug extensions disabled
196 * 2 0 == Time stamp disable (disabled)
197 * 1 0 == Protected mode virtual interrupts disabled
198 * 0 0 == VME disabled
207 /* Switch to the identity mapped page tables,
213 movq %rdi, %rcx /* Put the page_list in %rcx */
218 0: /* top, read another word for the indirection page */
223 testq $0x1, %rcx /* is it a destination page? */
226 andq $0xfffffffffffff000, %rdi
229 testq $0x2, %rcx /* is it an indirection page? */
232 andq $0xfffffffffffff000, %rbx
235 testq $0x4, %rcx /* is it the done indicator? */
239 testq $0x8, %rcx /* is it the source indicator? */
240 jz 0b /* Ignore it otherwise */
241 movq %rcx, %rsi /* For ever source page do a copy */
242 andq $0xfffffffffffff000, %rsi
249 /* To be certain of avoiding problems with self-modifying code
250 * I need to execute a serializing instruction here.
251 * So I flush the TLB by reloading %cr3 here, it's handy,
252 * and not processor dependent.
257 /* set all of the registers to known values */
258 /* leave %rsp alone */