]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/relocate_kernel_64.S
x86/speculation: Consolidate CPU whitelists
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / relocate_kernel_64.S
CommitLineData
5234f5eb
EB
1/*
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>
0341c14d 10#include <asm/page_types.h>
4bfaaef0 11#include <asm/kexec.h>
fd3af531 12#include <asm/processor-flags.h>
0341c14d 13#include <asm/pgtable_types.h>
526764bb 14#include <asm/nospec-branch.h>
5234f5eb 15
4bfaaef0
MD
16/*
17 * Must be relocatable PIC code callable as a C function
18 */
19
20#define PTR(x) (x << 3)
366932de 21#define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
4bfaaef0 22
fee7b0d8
HY
23/*
24 * control_page + KEXEC_CONTROL_CODE_MAX_SIZE
25 * ~ control_page + PAGE_SIZE are used as data storage and stack for
26 * jumping back
27 */
28#define DATA(offset) (KEXEC_CONTROL_CODE_MAX_SIZE+(offset))
29
30/* Minimal CPU state */
31#define RSP DATA(0x0)
32#define CR0 DATA(0x8)
33#define CR3 DATA(0x10)
34#define CR4 DATA(0x18)
35
36/* other data */
37#define CP_PA_TABLE_PAGE DATA(0x20)
38#define CP_PA_SWAP_PAGE DATA(0x28)
39#define CP_PA_BACKUP_PAGES_MAP DATA(0x30)
40
4bfaaef0 41 .text
288621e3 42 .align PAGE_SIZE
5234f5eb 43 .code64
4bfaaef0
MD
44 .globl relocate_kernel
45relocate_kernel:
fef3a7a1
HY
46 /*
47 * %rdi indirection_page
4bfaaef0
MD
48 * %rsi page_list
49 * %rdx start address
fee7b0d8 50 * %rcx preserve_context
4e237903 51 * %r8 sme_active
4bfaaef0
MD
52 */
53
fee7b0d8
HY
54 /* Save the CPU context, used for jumping back */
55 pushq %rbx
56 pushq %rbp
57 pushq %r12
58 pushq %r13
59 pushq %r14
60 pushq %r15
61 pushf
62
63 movq PTR(VA_CONTROL_PAGE)(%rsi), %r11
64 movq %rsp, RSP(%r11)
65 movq %cr0, %rax
66 movq %rax, CR0(%r11)
67 movq %cr3, %rax
68 movq %rax, CR3(%r11)
69 movq %cr4, %rax
70 movq %rax, CR4(%r11)
71
0f4d9c7e
KS
72 /* Save CR4. Required to enable the right paging mode later. */
73 movq %rax, %r13
74
5234f5eb
EB
75 /* zero out flags, and disable interrupts */
76 pushq $0
77 popfq
78
4e237903
TL
79 /* Save SME active flag */
80 movq %r8, %r12
81
fef3a7a1
HY
82 /*
83 * get physical address of control page now
84 * this is impossible after page table switch
85 */
4bfaaef0
MD
86 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
87
88 /* get physical address of page table now too */
fee7b0d8
HY
89 movq PTR(PA_TABLE_PAGE)(%rsi), %r9
90
91 /* get physical address of swap page now */
92 movq PTR(PA_SWAP_PAGE)(%rsi), %r10
93
94 /* save some information for jumping back */
95 movq %r9, CP_PA_TABLE_PAGE(%r11)
96 movq %r10, CP_PA_SWAP_PAGE(%r11)
97 movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11)
5234f5eb 98
f5deb796 99 /* Switch to the identity mapped page tables */
fee7b0d8 100 movq %r9, %cr3
4bfaaef0
MD
101
102 /* setup a new stack at the end of the physical control page */
a7bba17b 103 lea PAGE_SIZE(%r8), %rsp
4bfaaef0
MD
104
105 /* jump to identity mapped page */
106 addq $(identity_mapped - relocate_kernel), %r8
107 pushq %r8
108 ret
109
110identity_mapped:
050438ed
HY
111 /* set return address to 0 if not preserving context */
112 pushq $0
4bfaaef0
MD
113 /* store the start address on the stack */
114 pushq %rdx
5234f5eb 115
fef3a7a1
HY
116 /*
117 * Set cr0 to a known state:
fd3af531 118 * - Paging enabled
119 * - Alignment check disabled
120 * - Write protect disabled
121 * - No task switch
122 * - Don't do FP software emulation.
123 * - Proctected mode enabled
5234f5eb
EB
124 */
125 movq %cr0, %rax
fd3af531 126 andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
127 orl $(X86_CR0_PG | X86_CR0_PE), %eax
5234f5eb
EB
128 movq %rax, %cr0
129
fef3a7a1
HY
130 /*
131 * Set cr4 to a known state:
fd3af531 132 * - physical address extension enabled
0f4d9c7e 133 * - 5-level paging, if it was enabled before
5234f5eb 134 */
a734b4a2 135 movl $X86_CR4_PAE, %eax
0f4d9c7e
KS
136 testq $X86_CR4_LA57, %r13
137 jz 1f
138 orl $X86_CR4_LA57, %eax
1391:
5234f5eb
EB
140 movq %rax, %cr4
141
142 jmp 1f
1431:
144
f5deb796 145 /* Flush the TLB (needed?) */
fee7b0d8
HY
146 movq %r9, %cr3
147
4e237903
TL
148 /*
149 * If SME is active, there could be old encrypted cache line
150 * entries that will conflict with the now unencrypted memory
151 * used by kexec. Flush the caches before copying the kernel.
152 */
153 testq %r12, %r12
154 jz 1f
155 wbinvd
1561:
157
fee7b0d8
HY
158 movq %rcx, %r11
159 call swap_pages
160
161 /*
162 * To be certain of avoiding problems with self-modifying code
163 * I need to execute a serializing instruction here.
164 * So I flush the TLB by reloading %cr3 here, it's handy,
165 * and not processor dependent.
166 */
167 movq %cr3, %rax
168 movq %rax, %cr3
169
170 /*
171 * set all of the registers to known values
172 * leave %rsp alone
173 */
174
175 testq %r11, %r11
176 jnz 1f
f037e416
PA
177 xorl %eax, %eax
178 xorl %ebx, %ebx
179 xorl %ecx, %ecx
180 xorl %edx, %edx
181 xorl %esi, %esi
182 xorl %edi, %edi
183 xorl %ebp, %ebp
184 xorl %r8d, %r8d
185 xorl %r9d, %r9d
186 xorl %r10d, %r10d
187 xorl %r11d, %r11d
188 xorl %r12d, %r12d
189 xorl %r13d, %r13d
190 xorl %r14d, %r14d
191 xorl %r15d, %r15d
fee7b0d8
HY
192
193 ret
194
1951:
196 popq %rdx
197 leaq PAGE_SIZE(%r10), %rsp
526764bb 198 ANNOTATE_RETPOLINE_SAFE
fee7b0d8
HY
199 call *%rdx
200
201 /* get the re-entry point of the peer system */
202 movq 0(%rsp), %rbp
203 call 1f
2041:
205 popq %r8
206 subq $(1b - relocate_kernel), %r8
207 movq CP_PA_SWAP_PAGE(%r8), %r10
208 movq CP_PA_BACKUP_PAGES_MAP(%r8), %rdi
209 movq CP_PA_TABLE_PAGE(%r8), %rax
210 movq %rax, %cr3
211 lea PAGE_SIZE(%r8), %rsp
212 call swap_pages
213 movq $virtual_mapped, %rax
214 pushq %rax
215 ret
216
217virtual_mapped:
218 movq RSP(%r8), %rsp
219 movq CR4(%r8), %rax
220 movq %rax, %cr4
221 movq CR3(%r8), %rax
222 movq CR0(%r8), %r8
223 movq %rax, %cr3
224 movq %r8, %cr0
225 movq %rbp, %rax
226
227 popf
228 popq %r15
229 popq %r14
230 popq %r13
231 popq %r12
232 popq %rbp
233 popq %rbx
234 ret
5234f5eb
EB
235
236 /* Do the copies */
fee7b0d8 237swap_pages:
5234f5eb 238 movq %rdi, %rcx /* Put the page_list in %rcx */
f037e416
PA
239 xorl %edi, %edi
240 xorl %esi, %esi
5234f5eb
EB
241 jmp 1f
242
2430: /* top, read another word for the indirection page */
244
245 movq (%rbx), %rcx
246 addq $8, %rbx
2471:
3e1aa7cb 248 testb $0x1, %cl /* is it a destination page? */
5234f5eb
EB
249 jz 2f
250 movq %rcx, %rdi
251 andq $0xfffffffffffff000, %rdi
252 jmp 0b
2532:
3e1aa7cb 254 testb $0x2, %cl /* is it an indirection page? */
5234f5eb
EB
255 jz 2f
256 movq %rcx, %rbx
257 andq $0xfffffffffffff000, %rbx
258 jmp 0b
2592:
3e1aa7cb 260 testb $0x4, %cl /* is it the done indicator? */
5234f5eb
EB
261 jz 2f
262 jmp 3f
2632:
3e1aa7cb 264 testb $0x8, %cl /* is it the source indicator? */
5234f5eb
EB
265 jz 0b /* Ignore it otherwise */
266 movq %rcx, %rsi /* For ever source page do a copy */
267 andq $0xfffffffffffff000, %rsi
268
fee7b0d8
HY
269 movq %rdi, %rdx
270 movq %rsi, %rax
271
272 movq %r10, %rdi
a734b4a2 273 movl $512, %ecx
5234f5eb 274 rep ; movsq
5234f5eb 275
fee7b0d8
HY
276 movq %rax, %rdi
277 movq %rdx, %rsi
a734b4a2 278 movl $512, %ecx
fee7b0d8 279 rep ; movsq
5234f5eb 280
fee7b0d8
HY
281 movq %rdx, %rdi
282 movq %r10, %rsi
a734b4a2 283 movl $512, %ecx
fee7b0d8 284 rep ; movsq
5234f5eb 285
fee7b0d8
HY
286 lea PAGE_SIZE(%rax), %rsi
287 jmp 0b
2883:
5234f5eb 289 ret
fee7b0d8
HY
290
291 .globl kexec_control_code_size
292.set kexec_control_code_size, . - relocate_kernel