• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * relocate_kernel.S - put the kernel image in place to boot
4 * Copyright (C) 2002-2005 Eric Biederman  <ebiederm@xmission.com>
5 */
6
7#include <linux/linkage.h>
8#include <asm/page_types.h>
9#include <asm/kexec.h>
10#include <asm/processor-flags.h>
11#include <asm/pgtable_types.h>
12#include <asm/nospec-branch.h>
13#include <asm/unwind_hints.h>
14
15/*
16 * Must be relocatable PIC code callable as a C function, in particular
17 * there must be a plain RET and not jump to return thunk.
18 */
19
20#define PTR(x) (x << 3)
21#define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
22
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
41	.text
42	.align PAGE_SIZE
43	.code64
44SYM_CODE_START_NOALIGN(relocate_kernel)
45	UNWIND_HINT_EMPTY
46	/*
47	 * %rdi indirection_page
48	 * %rsi page_list
49	 * %rdx start address
50	 * %rcx preserve_context
51	 * %r8  sme_active
52	 */
53
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
72	/* Save CR4. Required to enable the right paging mode later. */
73	movq	%rax, %r13
74
75	/* zero out flags, and disable interrupts */
76	pushq $0
77	popfq
78
79	/* Save SME active flag */
80	movq	%r8, %r12
81
82	/*
83	 * get physical address of control page now
84	 * this is impossible after page table switch
85	 */
86	movq	PTR(PA_CONTROL_PAGE)(%rsi), %r8
87
88	/* get physical address of page table now too */
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)
98
99	/* Switch to the identity mapped page tables */
100	movq	%r9, %cr3
101
102	/* setup a new stack at the end of the physical control page */
103	lea	PAGE_SIZE(%r8), %rsp
104
105	/* jump to identity mapped page */
106	addq	$(identity_mapped - relocate_kernel), %r8
107	pushq	%r8
108	ANNOTATE_UNRET_SAFE
109	ret
110	int3
111SYM_CODE_END(relocate_kernel)
112
113SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
114	UNWIND_HINT_EMPTY
115	/* set return address to 0 if not preserving context */
116	pushq	$0
117	/* store the start address on the stack */
118	pushq   %rdx
119
120	/*
121	 * Set cr0 to a known state:
122	 *  - Paging enabled
123	 *  - Alignment check disabled
124	 *  - Write protect disabled
125	 *  - No task switch
126	 *  - Don't do FP software emulation.
127	 *  - Proctected mode enabled
128	 */
129	movq	%cr0, %rax
130	andq	$~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
131	orl	$(X86_CR0_PG | X86_CR0_PE), %eax
132	movq	%rax, %cr0
133
134	/*
135	 * Set cr4 to a known state:
136	 *  - physical address extension enabled
137	 *  - 5-level paging, if it was enabled before
138	 */
139	movl	$X86_CR4_PAE, %eax
140	testq	$X86_CR4_LA57, %r13
141	jz	1f
142	orl	$X86_CR4_LA57, %eax
1431:
144	movq	%rax, %cr4
145
146	jmp 1f
1471:
148
149	/* Flush the TLB (needed?) */
150	movq	%r9, %cr3
151
152	/*
153	 * If SME is active, there could be old encrypted cache line
154	 * entries that will conflict with the now unencrypted memory
155	 * used by kexec. Flush the caches before copying the kernel.
156	 */
157	testq	%r12, %r12
158	jz 1f
159	wbinvd
1601:
161
162	movq	%rcx, %r11
163	call	swap_pages
164
165	/*
166	 * To be certain of avoiding problems with self-modifying code
167	 * I need to execute a serializing instruction here.
168	 * So I flush the TLB by reloading %cr3 here, it's handy,
169	 * and not processor dependent.
170	 */
171	movq	%cr3, %rax
172	movq	%rax, %cr3
173
174	/*
175	 * set all of the registers to known values
176	 * leave %rsp alone
177	 */
178
179	testq	%r11, %r11
180	jnz 1f
181	xorl	%eax, %eax
182	xorl	%ebx, %ebx
183	xorl    %ecx, %ecx
184	xorl    %edx, %edx
185	xorl    %esi, %esi
186	xorl    %edi, %edi
187	xorl    %ebp, %ebp
188	xorl	%r8d, %r8d
189	xorl	%r9d, %r9d
190	xorl	%r10d, %r10d
191	xorl	%r11d, %r11d
192	xorl	%r12d, %r12d
193	xorl	%r13d, %r13d
194	xorl	%r14d, %r14d
195	xorl	%r15d, %r15d
196
197	ANNOTATE_UNRET_SAFE
198	ret
199	int3
200
2011:
202	popq	%rdx
203	leaq	PAGE_SIZE(%r10), %rsp
204	ANNOTATE_RETPOLINE_SAFE
205	call	*%rdx
206
207	/* get the re-entry point of the peer system */
208	movq	0(%rsp), %rbp
209	leaq	relocate_kernel(%rip), %r8
210	movq	CP_PA_SWAP_PAGE(%r8), %r10
211	movq	CP_PA_BACKUP_PAGES_MAP(%r8), %rdi
212	movq	CP_PA_TABLE_PAGE(%r8), %rax
213	movq	%rax, %cr3
214	lea	PAGE_SIZE(%r8), %rsp
215	call	swap_pages
216	movq	$virtual_mapped, %rax
217	pushq	%rax
218	ANNOTATE_UNRET_SAFE
219	ret
220	int3
221SYM_CODE_END(identity_mapped)
222
223SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped)
224	UNWIND_HINT_EMPTY
225	movq	RSP(%r8), %rsp
226	movq	CR4(%r8), %rax
227	movq	%rax, %cr4
228	movq	CR3(%r8), %rax
229	movq	CR0(%r8), %r8
230	movq	%rax, %cr3
231	movq	%r8, %cr0
232	movq	%rbp, %rax
233
234	popf
235	popq	%r15
236	popq	%r14
237	popq	%r13
238	popq	%r12
239	popq	%rbp
240	popq	%rbx
241	ANNOTATE_UNRET_SAFE
242	ret
243	int3
244SYM_CODE_END(virtual_mapped)
245
246	/* Do the copies */
247SYM_CODE_START_LOCAL_NOALIGN(swap_pages)
248	UNWIND_HINT_EMPTY
249	movq	%rdi, %rcx 	/* Put the page_list in %rcx */
250	xorl	%edi, %edi
251	xorl	%esi, %esi
252	jmp	1f
253
2540:	/* top, read another word for the indirection page */
255
256	movq	(%rbx), %rcx
257	addq	$8,	%rbx
2581:
259	testb	$0x1,	%cl   /* is it a destination page? */
260	jz	2f
261	movq	%rcx,	%rdi
262	andq	$0xfffffffffffff000, %rdi
263	jmp	0b
2642:
265	testb	$0x2,	%cl   /* is it an indirection page? */
266	jz	2f
267	movq	%rcx,   %rbx
268	andq	$0xfffffffffffff000, %rbx
269	jmp	0b
2702:
271	testb	$0x4,	%cl   /* is it the done indicator? */
272	jz	2f
273	jmp	3f
2742:
275	testb	$0x8,	%cl   /* is it the source indicator? */
276	jz	0b	      /* Ignore it otherwise */
277	movq	%rcx,   %rsi  /* For ever source page do a copy */
278	andq	$0xfffffffffffff000, %rsi
279
280	movq	%rdi, %rdx
281	movq	%rsi, %rax
282
283	movq	%r10, %rdi
284	movl	$512, %ecx
285	rep ; movsq
286
287	movq	%rax, %rdi
288	movq	%rdx, %rsi
289	movl	$512, %ecx
290	rep ; movsq
291
292	movq	%rdx, %rdi
293	movq	%r10, %rsi
294	movl	$512, %ecx
295	rep ; movsq
296
297	lea	PAGE_SIZE(%rax), %rsi
298	jmp	0b
2993:
300	ANNOTATE_UNRET_SAFE
301	ret
302	int3
303SYM_CODE_END(swap_pages)
304
305	.globl kexec_control_code_size
306.set kexec_control_code_size, . - relocate_kernel
307