1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * relocate_kernel.S - put the kernel image in place to boot
4 * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
7 #include <linux/linkage.h>
8 #include <linux/stringify.h>
9 #include <asm/alternative.h>
10 #include <asm/page_types.h>
11 #include <asm/kexec.h>
12 #include <asm/processor-flags.h>
13 #include <asm/pgtable_types.h>
14 #include <asm/nospec-branch.h>
15 #include <asm/unwind_hints.h>
18 * Must be relocatable PIC code callable as a C function, in particular
19 * there must be a plain RET and not jump to return thunk.
22 #define PTR(x) (x << 3)
23 #define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
26 * control_page + KEXEC_CONTROL_CODE_MAX_SIZE
27 * ~ control_page + PAGE_SIZE are used as data storage and stack for
30 #define DATA(offset) (KEXEC_CONTROL_CODE_MAX_SIZE+(offset))
32 /* Minimal CPU state */
35 #define CR3 DATA(0x10)
36 #define CR4 DATA(0x18)
39 #define CP_PA_TABLE_PAGE DATA(0x20)
40 #define CP_PA_SWAP_PAGE DATA(0x28)
41 #define CP_PA_BACKUP_PAGES_MAP DATA(0x30)
46 SYM_CODE_START_NOALIGN(relocate_range)
47 SYM_CODE_START_NOALIGN(relocate_kernel)
48 UNWIND_HINT_END_OF_STACK
51 * %rdi indirection_page
54 * %rcx preserve_context
55 * %r8 host_mem_enc_active
58 /* Save the CPU context, used for jumping back */
67 movq PTR(VA_CONTROL_PAGE)(%rsi), %r11
76 /* Save CR4. Required to enable the right paging mode later. */
79 /* zero out flags, and disable interrupts */
83 /* Save SME active flag */
87 * get physical address of control page now
88 * this is impossible after page table switch
90 movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
92 /* get physical address of page table now too */
93 movq PTR(PA_TABLE_PAGE)(%rsi), %r9
95 /* get physical address of swap page now */
96 movq PTR(PA_SWAP_PAGE)(%rsi), %r10
98 /* save some information for jumping back */
99 movq %r9, CP_PA_TABLE_PAGE(%r11)
100 movq %r10, CP_PA_SWAP_PAGE(%r11)
101 movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11)
103 /* Switch to the identity mapped page tables */
106 /* setup a new stack at the end of the physical control page */
107 lea PAGE_SIZE(%r8), %rsp
109 /* jump to identity mapped page */
110 addq $(identity_mapped - relocate_kernel), %r8
115 SYM_CODE_END(relocate_kernel)
117 SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
118 UNWIND_HINT_END_OF_STACK
119 /* set return address to 0 if not preserving context */
121 /* store the start address on the stack */
125 * Clear X86_CR4_CET (if it was set) such that we can clear CR0_WP
129 andq $~(X86_CR4_CET), %rax
133 * Set cr0 to a known state:
135 * - Alignment check disabled
136 * - Write protect disabled
138 * - Don't do FP software emulation.
139 * - Protected mode enabled
142 andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
143 orl $(X86_CR0_PG | X86_CR0_PE), %eax
147 * Set cr4 to a known state:
148 * - physical address extension enabled
149 * - 5-level paging, if it was enabled before
150 * - Machine check exception on TDX guest, if it was enabled before.
151 * Clearing MCE might not be allowed in TDX guests, depending on setup.
153 * Use R13 that contains the original CR4 value, read in relocate_kernel().
154 * PAE is always set in the original CR4.
156 andl $(X86_CR4_PAE | X86_CR4_LA57), %r13d
157 ALTERNATIVE "", __stringify(orl $X86_CR4_MCE, %r13d), X86_FEATURE_TDX_GUEST
160 /* Flush the TLB (needed?) */
164 * If SME is active, there could be old encrypted cache line
165 * entries that will conflict with the now unencrypted memory
166 * used by kexec. Flush the caches before copying the kernel.
173 /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */
178 * To be certain of avoiding problems with self-modifying code
179 * I need to execute a serializing instruction here.
180 * So I flush the TLB by reloading %cr3 here, it's handy,
181 * and not processor dependent.
187 * set all of the registers to known values
215 leaq PAGE_SIZE(%r10), %rsp
216 ANNOTATE_RETPOLINE_SAFE
219 /* get the re-entry point of the peer system */
221 leaq relocate_kernel(%rip), %r8
222 movq CP_PA_SWAP_PAGE(%r8), %r10
223 movq CP_PA_BACKUP_PAGES_MAP(%r8), %rdi
224 movq CP_PA_TABLE_PAGE(%r8), %rax
226 lea PAGE_SIZE(%r8), %rsp
228 movq $virtual_mapped, %rax
233 SYM_CODE_END(identity_mapped)
235 SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped)
236 UNWIND_HINT_END_OF_STACK
237 ANNOTATE_NOENDBR // RET target, above
257 SYM_CODE_END(virtual_mapped)
260 SYM_CODE_START_LOCAL_NOALIGN(swap_pages)
261 UNWIND_HINT_END_OF_STACK
262 movq %rdi, %rcx /* Put the indirection_page in %rcx */
267 0: /* top, read another word for the indirection page */
272 testb $0x1, %cl /* is it a destination page? */
275 andq $0xfffffffffffff000, %rdi
278 testb $0x2, %cl /* is it an indirection page? */
281 andq $0xfffffffffffff000, %rbx
284 testb $0x4, %cl /* is it the done indicator? */
288 testb $0x8, %cl /* is it the source indicator? */
289 jz 0b /* Ignore it otherwise */
290 movq %rcx, %rsi /* For ever source page do a copy */
291 andq $0xfffffffffffff000, %rsi
293 movq %rdi, %rdx /* Save destination page to %rdx */
294 movq %rsi, %rax /* Save source page to %rax */
296 /* copy source page to swap page */
301 /* copy destination page to source page */
307 /* copy swap page to destination page */
313 lea PAGE_SIZE(%rax), %rsi
319 SYM_CODE_END(swap_pages)
321 .skip KEXEC_CONTROL_CODE_MAX_SIZE - (. - relocate_kernel), 0xcc
322 SYM_CODE_END(relocate_range);