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>
10 #include <asm/page_types.h>
11 #include <asm/kexec.h>
12 #include <asm/processor-flags.h>
13 #include <asm/pgtable_types.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)
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 */
32 #define CR3 DATA(0x10)
33 #define CR4 DATA(0x18)
36 #define CP_PA_TABLE_PAGE DATA(0x20)
37 #define CP_PA_SWAP_PAGE DATA(0x28)
38 #define CP_PA_BACKUP_PAGES_MAP DATA(0x30)
43 .globl relocate_kernel
46 * %rdi indirection_page
49 * %rcx preserve_context
52 /* Save the CPU context, used for jumping back */
61 movq PTR(VA_CONTROL_PAGE)(%rsi), %r11
70 /* zero out flags, and disable interrupts */
75 * get physical address of control page now
76 * this is impossible after page table switch
78 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
80 /* get physical address of page table now too */
81 movq PTR(PA_TABLE_PAGE)(%rsi), %r9
83 /* get physical address of swap page now */
84 movq PTR(PA_SWAP_PAGE)(%rsi), %r10
86 /* save some information for jumping back */
87 movq %r9, CP_PA_TABLE_PAGE(%r11)
88 movq %r10, CP_PA_SWAP_PAGE(%r11)
89 movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11)
91 /* Switch to the identity mapped page tables */
94 /* setup a new stack at the end of the physical control page */
95 lea PAGE_SIZE(%r8), %rsp
97 /* jump to identity mapped page */
98 addq $(identity_mapped - relocate_kernel), %r8
103 /* store the start address on the stack */
107 * Set cr0 to a known state:
109 * - Alignment check disabled
110 * - Write protect disabled
112 * - Don't do FP software emulation.
113 * - Proctected mode enabled
116 andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
117 orl $(X86_CR0_PG | X86_CR0_PE), %eax
121 * Set cr4 to a known state:
122 * - physical address extension enabled
124 movq $X86_CR4_PAE, %rax
130 /* Flush the TLB (needed?) */
137 * To be certain of avoiding problems with self-modifying code
138 * I need to execute a serializing instruction here.
139 * So I flush the TLB by reloading %cr3 here, it's handy,
140 * and not processor dependent.
146 * set all of the registers to known values
172 leaq PAGE_SIZE(%r10), %rsp
175 /* get the re-entry point of the peer system */
180 subq $(1b - relocate_kernel), %r8
181 movq CP_PA_SWAP_PAGE(%r8), %r10
182 movq CP_PA_BACKUP_PAGES_MAP(%r8), %rdi
183 movq CP_PA_TABLE_PAGE(%r8), %rax
185 lea PAGE_SIZE(%r8), %rsp
187 movq $virtual_mapped, %rax
212 movq %rdi, %rcx /* Put the page_list in %rcx */
217 0: /* top, read another word for the indirection page */
222 testq $0x1, %rcx /* is it a destination page? */
225 andq $0xfffffffffffff000, %rdi
228 testq $0x2, %rcx /* is it an indirection page? */
231 andq $0xfffffffffffff000, %rbx
234 testq $0x4, %rcx /* is it the done indicator? */
238 testq $0x8, %rcx /* is it the source indicator? */
239 jz 0b /* Ignore it otherwise */
240 movq %rcx, %rsi /* For ever source page do a copy */
241 andq $0xfffffffffffff000, %rsi
260 lea PAGE_SIZE(%rax), %rsi
265 .globl kexec_control_code_size
266 .set kexec_control_code_size, . - relocate_kernel