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