xref: /kvm-unit-tests/arm/cstart64.S (revision e97e1c827fadc972c4efc8fc0650984b6fcc74e8)
1/*
2 * Boot entry point and assembler functions for aarch64 tests.
3 *
4 * Copyright (C) 2017, Red Hat Inc, Andrew Jones <drjones@redhat.com>
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2.
7 */
8#define __ASSEMBLY__
9#include <auxinfo.h>
10#include <asm/asm-offsets.h>
11#include <asm/assembler.h>
12#include <asm/ptrace.h>
13#include <asm/page.h>
14#include <asm/pgtable-hwdef.h>
15#include <asm/thread_info.h>
16#include <asm/sysreg.h>
17
18.macro zero_range, tmp1, tmp2
199998:	cmp	\tmp1, \tmp2
20	b.eq	9997f
21	stp	xzr, xzr, [\tmp1], #16
22	b	9998b
239997:
24.endm
25
26.section .init
27
28/*
29 * Bootloader params are in x0-x3. See kernel doc
30 * Documentation/arm64/booting.txt
31 */
32.globl start
33start:
34	/* get our base address */
35	adrp	x4, start
36	add     x4, x4, :lo12:start
37
38	/*
39	 * Update all R_AARCH64_RELATIVE relocations using the table
40	 * of Elf64_Rela entries between reloc_start/end. The build
41	 * will not emit other relocation types.
42	 *
43	 * struct Elf64_Rela {
44	 * 	uint64_t r_offset;
45	 * 	uint64_t r_info;
46	 * 	int64_t  r_addend;
47	 * }
48	 */
49	adrp	x5, reloc_start
50	add     x5, x5, :lo12:reloc_start
51	adrp	x6, reloc_end
52	add     x6, x6, :lo12:reloc_end
531:
54	cmp	x5, x6
55	b.hs	1f
56	ldr	x7, [x5]			// r_offset
57	ldr	x8, [x5, #16]			// r_addend
58	add	x8, x8, x4			// val = base + r_addend
59	str	x8, [x4, x7]			// base[r_offset] = val
60	add	x5, x5, #24
61	b	1b
62
631:
64	/* zero BSS */
65	adrp	x4, bss
66	add	x4, x4, :lo12:bss
67	adrp    x5, ebss
68	add     x5, x5, :lo12:ebss
69	zero_range x4, x5
70
71	/* zero and set up stack */
72	adrp    x5, stacktop
73	add     x5, x5, :lo12:stacktop
74	sub	x4, x5, #THREAD_SIZE
75	zero_range x4, x5
76
77	/* set SCTLR_EL1 to a known value */
78	ldr	x4, =INIT_SCTLR_EL1_MMU_OFF
79	msr	sctlr_el1, x4
80	isb
81
82	mov	x4, #1
83	msr	spsel, x4
84	adrp    x4, stackptr
85	add     sp, x4, :lo12:stackptr
86
87	/* enable FP/ASIMD */
88	mov	x4, #(3 << 20)
89	msr	cpacr_el1, x4
90
91	/* set up exception handling */
92	bl	exceptions_init
93
94	/* complete setup */
95	bl	setup				// x0 is the addr of the dtb
96
97	/* run the test */
98	adrp	x0, __argc
99	ldr	w0, [x0, :lo12:__argc]
100	adrp	x1, __argv
101	add	x1, x1, :lo12:__argv
102	adrp	x2, __environ
103	add	x2, x2, :lo12:__environ
104	bl	main
105	bl	exit
106	b	halt
107
108.text
109
110get_mmu_off:
111	adrp	x0, auxinfo
112	ldr	x0, [x0, :lo12:auxinfo + 8]
113	and	x0, x0, #AUXINFO_MMU_OFF
114	ret
115
116.globl secondary_entry
117secondary_entry:
118	/* Enable FP/ASIMD */
119	mov	x0, #(3 << 20)
120	msr	cpacr_el1, x0
121
122	/* set up exception handling */
123	bl	exceptions_init
124
125	/* enable the MMU unless requested off */
126	bl	get_mmu_off
127	cbnz	x0, 1f
128	adrp	x0, mmu_idmap
129	ldr	x0, [x0, :lo12:mmu_idmap]
130	bl	asm_mmu_enable
131
1321:
133	/* set the stack */
134	adrp	x0, secondary_data
135	ldr	x0, [x0, :lo12:secondary_data]
136	mov	sp, x0
137
138	/* finish init in C code */
139	bl	secondary_cinit
140
141	/* x0 is now the entry function, run it */
142	blr	x0
143	b	do_idle
144
145.globl halt
146halt:
1471:	wfi
148	b	1b
149
150/*
151 * asm_mmu_enable
152 *   Inputs:
153 *     x0 is the base address of the translation table
154 *   Outputs: none
155 *
156 * Adapted from
157 *   arch/arm64/kernel/head.S
158 *   arch/arm64/mm/proc.S
159 */
160
161/*
162 * Memory region attributes for LPAE:
163 *
164 *   n = AttrIndx[2:0]
165 *                      n       MAIR
166 *   DEVICE_nGnRnE      000     00000000
167 *   DEVICE_nGnRE       001     00000100
168 *   DEVICE_GRE         010     00001100
169 *   NORMAL_NC          011     01000100
170 *   NORMAL             100     11111111
171 *   NORMAL_WT          101     10111011
172 *   DEVICE_nGRE        110     00001000
173 */
174#define MAIR(attr, mt) ((attr) << ((mt) * 8))
175
176#if PAGE_SIZE == SZ_64K
177#define TCR_TG_FLAGS	TCR_TG0_64K | TCR_TG1_64K
178#elif PAGE_SIZE == SZ_16K
179#define TCR_TG_FLAGS	TCR_TG0_16K | TCR_TG1_16K
180#elif PAGE_SIZE == SZ_4K
181#define TCR_TG_FLAGS	TCR_TG0_4K | TCR_TG1_4K
182#endif
183
184.globl asm_mmu_enable
185asm_mmu_enable:
186	tlbi	vmalle1			// invalidate I + D TLBs
187	dsb	nsh
188
189	/* TCR */
190	ldr	x1, =TCR_TxSZ(VA_BITS) |		\
191		     TCR_TG_FLAGS  |			\
192		     TCR_IRGN_WBWA | TCR_ORGN_WBWA |	\
193		     TCR_SHARED |			\
194		     TCR_EPD1
195	mrs	x2, id_aa64mmfr0_el1
196	bfi	x1, x2, #32, #3
197	msr	tcr_el1, x1
198
199	/* MAIR */
200	ldr	x1, =MAIR(0x00, MT_DEVICE_nGnRnE) |	\
201		     MAIR(0x04, MT_DEVICE_nGnRE) |	\
202		     MAIR(0x0c, MT_DEVICE_GRE) |	\
203		     MAIR(0x44, MT_NORMAL_NC) |		\
204		     MAIR(0xff, MT_NORMAL) |	        \
205		     MAIR(0xbb, MT_NORMAL_WT) |         \
206		     MAIR(0x08, MT_DEVICE_nGRE)
207	msr	mair_el1, x1
208
209	/* TTBR0 */
210	msr	ttbr0_el1, x0
211	isb
212
213	/* SCTLR */
214	mrs	x1, sctlr_el1
215	orr	x1, x1, SCTLR_EL1_C
216	orr	x1, x1, SCTLR_EL1_I
217	orr	x1, x1, SCTLR_EL1_M
218	msr	sctlr_el1, x1
219	isb
220
221	ret
222
223.globl asm_mmu_disable
224asm_mmu_disable:
225	mrs	x0, sctlr_el1
226	bic	x0, x0, SCTLR_EL1_M
227	msr	sctlr_el1, x0
228	isb
229
230	/* Clean + invalidate the entire memory */
231	adrp	x0, __phys_offset
232	ldr	x0, [x0, :lo12:__phys_offset]
233	adrp	x1, __phys_end
234	ldr	x1, [x1, :lo12:__phys_end]
235	sub	x1, x1, x0
236	dcache_by_line_op civac, sy, x0, x1, x2, x3
237
238	ret
239
240/*
241 * Vectors
242 */
243
244exceptions_init:
245	adrp	x4, vector_table
246	add	x4, x4, :lo12:vector_table
247	msr	vbar_el1, x4
248	isb
249	ret
250
251/*
252 * Vector stubs
253 * Adapted from arch/arm64/kernel/entry.S
254 */
255.macro vector_stub, name, vec
256\name:
257	stp	 x0,  x1, [sp, #-S_FRAME_SIZE]!
258	stp	 x2,  x3, [sp,  #16]
259	stp	 x4,  x5, [sp,  #32]
260	stp	 x6,  x7, [sp,  #48]
261	stp	 x8,  x9, [sp,  #64]
262	stp	x10, x11, [sp,  #80]
263	stp	x12, x13, [sp,  #96]
264	stp	x14, x15, [sp, #112]
265	stp	x16, x17, [sp, #128]
266	stp	x18, x19, [sp, #144]
267	stp	x20, x21, [sp, #160]
268	stp	x22, x23, [sp, #176]
269	stp	x24, x25, [sp, #192]
270	stp	x26, x27, [sp, #208]
271	stp	x28, x29, [sp, #224]
272
273	str	x30, [sp, #S_LR]
274
275	.if \vec >= 8
276	mrs	x1, sp_el0
277	.else
278	add	x1, sp, #S_FRAME_SIZE
279	.endif
280	str	x1, [sp, #S_SP]
281
282	mrs	x1, elr_el1
283	mrs	x2, spsr_el1
284	stp	x1, x2, [sp, #S_PC]
285
286	mov	x0, \vec
287	mov	x1, sp
288	mrs	x2, esr_el1
289	bl	do_handle_exception
290
291	ldp	x1, x2, [sp, #S_PC]
292	msr	spsr_el1, x2
293	msr	elr_el1, x1
294
295	.if \vec >= 8
296	ldr	x1, [sp, #S_SP]
297	msr	sp_el0, x1
298	.endif
299
300	ldr	x30, [sp, #S_LR]
301
302	ldp	x28, x29, [sp, #224]
303	ldp	x26, x27, [sp, #208]
304	ldp	x24, x25, [sp, #192]
305	ldp	x22, x23, [sp, #176]
306	ldp	x20, x21, [sp, #160]
307	ldp	x18, x19, [sp, #144]
308	ldp	x16, x17, [sp, #128]
309	ldp	x14, x15, [sp, #112]
310	ldp	x12, x13, [sp,  #96]
311	ldp	x10, x11, [sp,  #80]
312	ldp	 x8,  x9, [sp,  #64]
313	ldp	 x6,  x7, [sp,  #48]
314	ldp	 x4,  x5, [sp,  #32]
315	ldp	 x2,  x3, [sp,  #16]
316	ldp	 x0,  x1, [sp], #S_FRAME_SIZE
317
318	eret
319.endm
320
321vector_stub	el1t_sync,     0
322vector_stub	el1t_irq,      1
323vector_stub	el1t_fiq,      2
324vector_stub	el1t_error,    3
325
326vector_stub	el1h_sync,     4
327vector_stub	el1h_irq,      5
328vector_stub	el1h_fiq,      6
329vector_stub	el1h_error,    7
330
331vector_stub	el0_sync_64,   8
332vector_stub	el0_irq_64,    9
333vector_stub	el0_fiq_64,   10
334vector_stub	el0_error_64, 11
335
336vector_stub	el0_sync_32,  12
337vector_stub	el0_irq_32,   13
338vector_stub	el0_fiq_32,   14
339vector_stub	el0_error_32, 15
340
341.section .text.ex
342
343.macro ventry, label
344.align 7
345	b	\label
346.endm
347
348.align 11
349vector_table:
350	ventry	el1t_sync			// Synchronous EL1t
351	ventry	el1t_irq			// IRQ EL1t
352	ventry	el1t_fiq			// FIQ EL1t
353	ventry	el1t_error			// Error EL1t
354
355	ventry	el1h_sync			// Synchronous EL1h
356	ventry	el1h_irq			// IRQ EL1h
357	ventry	el1h_fiq			// FIQ EL1h
358	ventry	el1h_error			// Error EL1h
359
360	ventry	el0_sync_64			// Synchronous 64-bit EL0
361	ventry	el0_irq_64			// IRQ 64-bit EL0
362	ventry	el0_fiq_64			// FIQ 64-bit EL0
363	ventry	el0_error_64			// Error 64-bit EL0
364
365	ventry	el0_sync_32			// Synchronous 32-bit EL0
366	ventry	el0_irq_32			// IRQ 32-bit EL0
367	ventry	el0_fiq_32			// FIQ 32-bit EL0
368	ventry	el0_error_32			// Error 32-bit EL0
369