blob: 3fd2c693e4752d01e071de68ef57e2db8b47b605 [file] [log] [blame]
Eric W. Biederman5234f5e2005-06-25 14:58:02 -07001/*
2 * relocate_kernel.S - put the kernel image in place to boot
3 * Copyright (C) 2002-2005 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
9#include <linux/linkage.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080010#include <asm/page_types.h>
Magnus Damm4bfaaef2006-09-26 10:52:38 +020011#include <asm/kexec.h>
gorcunov@gmail.comfd3af532008-03-23 00:00:08 +030012#include <asm/processor-flags.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080013#include <asm/pgtable_types.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070014
Magnus Damm4bfaaef2006-09-26 10:52:38 +020015/*
16 * Must be relocatable PIC code callable as a C function
17 */
18
19#define PTR(x) (x << 3)
gorcunov@gmail.com366932d2008-03-23 00:00:09 +030020#define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
Magnus Damm4bfaaef2006-09-26 10:52:38 +020021
Huang Yingfee7b0d2009-03-10 10:57:16 +080022/*
23 * control_page + KEXEC_CONTROL_CODE_MAX_SIZE
24 * ~ control_page + PAGE_SIZE are used as data storage and stack for
25 * jumping back
26 */
27#define DATA(offset) (KEXEC_CONTROL_CODE_MAX_SIZE+(offset))
28
29/* Minimal CPU state */
30#define RSP DATA(0x0)
31#define CR0 DATA(0x8)
32#define CR3 DATA(0x10)
33#define CR4 DATA(0x18)
34
35/* other data */
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)
39
Magnus Damm4bfaaef2006-09-26 10:52:38 +020040 .text
Cyrill Gorcunov288621e2008-03-21 23:12:14 +030041 .align PAGE_SIZE
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070042 .code64
Magnus Damm4bfaaef2006-09-26 10:52:38 +020043 .globl relocate_kernel
44relocate_kernel:
Huang Yingfef3a7a2009-03-10 10:56:57 +080045 /*
46 * %rdi indirection_page
Magnus Damm4bfaaef2006-09-26 10:52:38 +020047 * %rsi page_list
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070048 * %rdx start address
Huang Yingfee7b0d2009-03-10 10:57:16 +080049 * %rcx preserve_context
Magnus Damm4bfaaef2006-09-26 10:52:38 +020050 */
51
Huang Yingfee7b0d2009-03-10 10:57:16 +080052 /* Save the CPU context, used for jumping back */
53 pushq %rbx
54 pushq %rbp
55 pushq %r12
56 pushq %r13
57 pushq %r14
58 pushq %r15
59 pushf
60
61 movq PTR(VA_CONTROL_PAGE)(%rsi), %r11
62 movq %rsp, RSP(%r11)
63 movq %cr0, %rax
64 movq %rax, CR0(%r11)
65 movq %cr3, %rax
66 movq %rax, CR3(%r11)
67 movq %cr4, %rax
68 movq %rax, CR4(%r11)
69
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070070 /* zero out flags, and disable interrupts */
71 pushq $0
72 popfq
73
Huang Yingfef3a7a2009-03-10 10:56:57 +080074 /*
75 * get physical address of control page now
76 * this is impossible after page table switch
77 */
Magnus Damm4bfaaef2006-09-26 10:52:38 +020078 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070079
Magnus Damm4bfaaef2006-09-26 10:52:38 +020080 /* get physical address of page table now too */
Huang Yingfee7b0d2009-03-10 10:57:16 +080081 movq PTR(PA_TABLE_PAGE)(%rsi), %r9
82
83 /* get physical address of swap page now */
84 movq PTR(PA_SWAP_PAGE)(%rsi), %r10
85
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)
Magnus Damm4bfaaef2006-09-26 10:52:38 +020090
Huang Yingf5deb792009-02-03 14:22:48 +080091 /* Switch to the identity mapped page tables */
Huang Yingfee7b0d2009-03-10 10:57:16 +080092 movq %r9, %cr3
Magnus Damm4bfaaef2006-09-26 10:52:38 +020093
94 /* setup a new stack at the end of the physical control page */
gorcunov@gmail.coma7bba172008-03-23 00:00:07 +030095 lea PAGE_SIZE(%r8), %rsp
Magnus Damm4bfaaef2006-09-26 10:52:38 +020096
97 /* jump to identity mapped page */
98 addq $(identity_mapped - relocate_kernel), %r8
99 pushq %r8
100 ret
101
102identity_mapped:
Huang Ying050438e2011-07-14 09:34:37 +0800103 /* set return address to 0 if not preserving context */
104 pushq $0
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200105 /* store the start address on the stack */
106 pushq %rdx
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700107
Huang Yingfef3a7a2009-03-10 10:56:57 +0800108 /*
109 * Set cr0 to a known state:
gorcunov@gmail.comfd3af532008-03-23 00:00:08 +0300110 * - Paging enabled
111 * - Alignment check disabled
112 * - Write protect disabled
113 * - No task switch
114 * - Don't do FP software emulation.
115 * - Proctected mode enabled
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700116 */
117 movq %cr0, %rax
gorcunov@gmail.comfd3af532008-03-23 00:00:08 +0300118 andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
119 orl $(X86_CR0_PG | X86_CR0_PE), %eax
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700120 movq %rax, %cr0
121
Huang Yingfef3a7a2009-03-10 10:56:57 +0800122 /*
123 * Set cr4 to a known state:
gorcunov@gmail.comfd3af532008-03-23 00:00:08 +0300124 * - physical address extension enabled
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700125 */
gorcunov@gmail.comfd3af532008-03-23 00:00:08 +0300126 movq $X86_CR4_PAE, %rax
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700127 movq %rax, %cr4
128
129 jmp 1f
1301:
131
Huang Yingf5deb792009-02-03 14:22:48 +0800132 /* Flush the TLB (needed?) */
Huang Yingfee7b0d2009-03-10 10:57:16 +0800133 movq %r9, %cr3
134
135 movq %rcx, %r11
136 call swap_pages
137
138 /*
139 * To be certain of avoiding problems with self-modifying code
140 * I need to execute a serializing instruction here.
141 * So I flush the TLB by reloading %cr3 here, it's handy,
142 * and not processor dependent.
143 */
144 movq %cr3, %rax
145 movq %rax, %cr3
146
147 /*
148 * set all of the registers to known values
149 * leave %rsp alone
150 */
151
152 testq %r11, %r11
153 jnz 1f
H. Peter Anvinf037e412013-06-20 21:16:00 -0700154 xorl %eax, %eax
155 xorl %ebx, %ebx
156 xorl %ecx, %ecx
157 xorl %edx, %edx
158 xorl %esi, %esi
159 xorl %edi, %edi
160 xorl %ebp, %ebp
161 xorl %r8d, %r8d
162 xorl %r9d, %r9d
163 xorl %r10d, %r10d
164 xorl %r11d, %r11d
165 xorl %r12d, %r12d
166 xorl %r13d, %r13d
167 xorl %r14d, %r14d
168 xorl %r15d, %r15d
Huang Yingfee7b0d2009-03-10 10:57:16 +0800169
170 ret
171
1721:
173 popq %rdx
174 leaq PAGE_SIZE(%r10), %rsp
175 call *%rdx
176
177 /* get the re-entry point of the peer system */
178 movq 0(%rsp), %rbp
179 call 1f
1801:
181 popq %r8
182 subq $(1b - relocate_kernel), %r8
183 movq CP_PA_SWAP_PAGE(%r8), %r10
184 movq CP_PA_BACKUP_PAGES_MAP(%r8), %rdi
185 movq CP_PA_TABLE_PAGE(%r8), %rax
186 movq %rax, %cr3
187 lea PAGE_SIZE(%r8), %rsp
188 call swap_pages
189 movq $virtual_mapped, %rax
190 pushq %rax
191 ret
192
193virtual_mapped:
194 movq RSP(%r8), %rsp
195 movq CR4(%r8), %rax
196 movq %rax, %cr4
197 movq CR3(%r8), %rax
198 movq CR0(%r8), %r8
199 movq %rax, %cr3
200 movq %r8, %cr0
201 movq %rbp, %rax
202
203 popf
204 popq %r15
205 popq %r14
206 popq %r13
207 popq %r12
208 popq %rbp
209 popq %rbx
210 ret
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700211
212 /* Do the copies */
Huang Yingfee7b0d2009-03-10 10:57:16 +0800213swap_pages:
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700214 movq %rdi, %rcx /* Put the page_list in %rcx */
H. Peter Anvinf037e412013-06-20 21:16:00 -0700215 xorl %edi, %edi
216 xorl %esi, %esi
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700217 jmp 1f
218
2190: /* top, read another word for the indirection page */
220
221 movq (%rbx), %rcx
222 addq $8, %rbx
2231:
224 testq $0x1, %rcx /* is it a destination page? */
225 jz 2f
226 movq %rcx, %rdi
227 andq $0xfffffffffffff000, %rdi
228 jmp 0b
2292:
230 testq $0x2, %rcx /* is it an indirection page? */
231 jz 2f
232 movq %rcx, %rbx
233 andq $0xfffffffffffff000, %rbx
234 jmp 0b
2352:
236 testq $0x4, %rcx /* is it the done indicator? */
237 jz 2f
238 jmp 3f
2392:
240 testq $0x8, %rcx /* is it the source indicator? */
241 jz 0b /* Ignore it otherwise */
242 movq %rcx, %rsi /* For ever source page do a copy */
243 andq $0xfffffffffffff000, %rsi
244
Huang Yingfee7b0d2009-03-10 10:57:16 +0800245 movq %rdi, %rdx
246 movq %rsi, %rax
247
248 movq %r10, %rdi
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700249 movq $512, %rcx
250 rep ; movsq
Huang Yingfee7b0d2009-03-10 10:57:16 +0800251
252 movq %rax, %rdi
253 movq %rdx, %rsi
254 movq $512, %rcx
255 rep ; movsq
256
257 movq %rdx, %rdi
258 movq %r10, %rsi
259 movq $512, %rcx
260 rep ; movsq
261
262 lea PAGE_SIZE(%rax), %rsi
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700263 jmp 0b
2643:
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700265 ret
Huang Yingfee7b0d2009-03-10 10:57:16 +0800266
267 .globl kexec_control_code_size
268.set kexec_control_code_size, . - relocate_kernel