xref: /freebsd/sys/arm64/arm64/locore.S (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
1/*-
2 * Copyright (c) 2012-2014 Andrew Turner
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include "assym.s"
30#include "opt_kstack_pages.h"
31#include <sys/syscall.h>
32#include <machine/asm.h>
33#include <machine/armreg.h>
34#include <machine/hypervisor.h>
35#include <machine/param.h>
36#include <machine/pte.h>
37
38#define	VIRT_BITS	39
39
40	.globl	kernbase
41	.set	kernbase, KERNBASE
42
43#define	DEVICE_MEM	0
44#define	NORMAL_UNCACHED	1
45#define	NORMAL_MEM	2
46
47/*
48 * We assume:
49 *  MMU      on with an identity map, or off
50 *  D-Cache: off
51 *  I-Cache: on or off
52 *  We are loaded at a 2MiB aligned address
53 */
54
55	.text
56	.globl _start
57_start:
58	/* Drop to EL1 */
59	bl	drop_to_el1
60
61	/*
62	 * Disable the MMU. We may have entered the kernel with it on and
63	 * will need to update the tables later. If this has been set up
64	 * with anything other than a VA == PA map then this will fail,
65	 * but in this case the code to find where we are running from
66	 * would have also failed.
67	 */
68	dsb	sy
69	mrs	x2, sctlr_el1
70	bic	x2, x2, SCTLR_M
71	msr	sctlr_el1, x2
72	isb
73
74	/* Set the context id */
75	msr	contextidr_el1, xzr
76
77	/* Get the virt -> phys offset */
78	bl	get_virt_delta
79
80	/*
81	 * At this point:
82	 * x29 = PA - VA
83	 * x28 = Our physical load address
84	 */
85
86	/* Create the page tables */
87	bl	create_pagetables
88
89	/*
90	 * At this point:
91	 * x27 = TTBR0 table
92	 * x26 = TTBR1 table
93	 */
94
95	/* Enable the mmu */
96	bl	start_mmu
97
98	/* Jump to the virtual address space */
99	ldr	x15, .Lvirtdone
100	br	x15
101
102virtdone:
103	/* Set up the stack */
104	adr	x25, initstack_end
105	mov	sp, x25
106	sub	sp, sp, #PCB_SIZE
107
108	/* Zero the BSS */
109	ldr	x15, .Lbss
110	ldr	x14, .Lend
1111:
112	str	xzr, [x15], #8
113	cmp	x15, x14
114	b.lo	1b
115
116	/* Backup the module pointer */
117	mov	x1, x0
118
119	/* Make the page table base a virtual address */
120	sub	x26, x26, x29
121
122	sub	sp, sp, #(64 * 4)
123	mov	x0, sp
124
125	/* Degate the delda so it is VA -> PA */
126	neg	x29, x29
127
128	str	x1,  [x0]	/* modulep */
129	str	x26, [x0, 8]	/* kern_l1pt */
130	str	x29, [x0, 16]	/* kern_delta */
131	str	x25, [x0, 24]	/* kern_stack */
132
133	/* trace back starts here */
134	mov	fp, #0
135	/* Branch to C code */
136	bl	initarm
137	bl	mi_startup
138
139	/* We should not get here */
140	brk	0
141
142	.align 3
143.Lvirtdone:
144	.quad	virtdone
145.Lbss:
146	.quad	__bss_start
147.Lend:
148	.quad	_end
149
150#ifdef SMP
151/*
152 * mpentry(unsigned long)
153 *
154 * Called by a core when it is being brought online.
155 * The data in x0 is passed straight to init_secondary.
156 */
157ENTRY(mpentry)
158	/* Disable interrupts */
159	msr	daifset, #2
160
161	/* Drop to EL1 */
162	bl	drop_to_el1
163
164	/* Set the context id */
165	msr	contextidr_el1, x1
166
167	/* Load the kernel page table */
168	adr	x26, pagetable_l1_ttbr1
169	/* Load the identity page table */
170	adr	x27, pagetable_l1_ttbr0
171
172	/* Enable the mmu */
173	bl	start_mmu
174
175	/* Jump to the virtual address space */
176	ldr	x15, =mp_virtdone
177	br	x15
178
179mp_virtdone:
180	ldr	x4, =secondary_stacks
181	mov	x5, #(PAGE_SIZE * KSTACK_PAGES)
182	mul	x5, x0, x5
183	add	sp, x4, x5
184
185	b	init_secondary
186END(mpentry)
187#endif
188
189/*
190 * If we are started in EL2, configure the required hypervisor
191 * registers and drop to EL1.
192 */
193drop_to_el1:
194	mrs	x1, CurrentEL
195	lsr	x1, x1, #2
196	cmp	x1, #0x2
197	b.eq	1f
198	ret
1991:
200	/* Configure the Hypervisor */
201	mov	x2, #(HCR_RW)
202	msr	hcr_el2, x2
203
204	/* Load the Virtualization Process ID Register */
205	mrs	x2, midr_el1
206	msr	vpidr_el2, x2
207
208	/* Load the Virtualization Multiprocess ID Register */
209	mrs	x2, mpidr_el1
210	msr	vmpidr_el2, x2
211
212	/* Set the bits that need to be 1 in sctlr_el1 */
213	ldr	x2, .Lsctlr_res1
214	msr	sctlr_el1, x2
215
216	/* Don't trap to EL2 for exceptions */
217	mov	x2, #CPTR_RES1
218	msr	cptr_el2, x2
219
220	/* Don't trap to EL2 for CP15 traps */
221	msr	hstr_el2, xzr
222
223	/* Enable access to the physical timers at EL1 */
224	mrs	x2, cnthctl_el2
225	orr	x2, x2, #(CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN)
226	msr	cnthctl_el2, x2
227
228	/* Set the counter offset to a known value */
229	msr	cntvoff_el2, xzr
230
231	/* Hypervisor trap functions */
232	adr	x2, hyp_vectors
233	msr	vbar_el2, x2
234
235	mov	x2, #(PSR_F | PSR_I | PSR_A | PSR_D | PSR_M_EL1h)
236	msr	spsr_el2, x2
237
238	/* Configure GICv3 CPU interface */
239	mrs	x2, id_aa64pfr0_el1
240	/* Extract GIC bits from the register */
241	ubfx	x2, x2, #ID_AA64PFR0_GIC_SHIFT, #ID_AA64PFR0_GIC_BITS
242	/* GIC[3:0] == 0001 - GIC CPU interface via special regs. supported */
243	cmp	x2, #(ID_AA64PFR0_GIC_CPUIF_EN >> ID_AA64PFR0_GIC_SHIFT)
244	b.ne	2f
245
246	mrs	x2, icc_sre_el2
247	orr	x2, x2, #ICC_SRE_EL2_EN	/* Enable access from insecure EL1 */
248	msr	icc_sre_el2, x2
2492:
250
251	/* Set the address to return to our return address */
252	msr	elr_el2, x30
253	isb
254
255	eret
256
257	.align 3
258.Lsctlr_res1:
259	.quad SCTLR_RES1
260
261#define	VECT_EMPTY	\
262	.align 7;	\
263	1:	b	1b
264
265	.align 11
266hyp_vectors:
267	VECT_EMPTY	/* Synchronous EL2t */
268	VECT_EMPTY	/* IRQ EL2t */
269	VECT_EMPTY	/* FIQ EL2t */
270	VECT_EMPTY	/* Error EL2t */
271
272	VECT_EMPTY	/* Synchronous EL2h */
273	VECT_EMPTY	/* IRQ EL2h */
274	VECT_EMPTY	/* FIQ EL2h */
275	VECT_EMPTY	/* Error EL2h */
276
277	VECT_EMPTY	/* Synchronous 64-bit EL1 */
278	VECT_EMPTY	/* IRQ 64-bit EL1 */
279	VECT_EMPTY	/* FIQ 64-bit EL1 */
280	VECT_EMPTY	/* Error 64-bit EL1 */
281
282	VECT_EMPTY	/* Synchronous 32-bit EL1 */
283	VECT_EMPTY	/* IRQ 32-bit EL1 */
284	VECT_EMPTY	/* FIQ 32-bit EL1 */
285	VECT_EMPTY	/* Error 32-bit EL1 */
286
287/*
288 * Get the delta between the physical address we were loaded to and the
289 * virtual address we expect to run from. This is used when building the
290 * initial page table.
291 */
292get_virt_delta:
293	/* Load the physical address of virt_map */
294	adr	x29, virt_map
295	/* Load the virtual address of virt_map stored in virt_map */
296	ldr	x28, [x29]
297	/* Find PA - VA as PA' = VA' - VA + PA = VA' + (PA - VA) = VA' + x29 */
298	sub	x29, x29, x28
299	/* Find the load address for the kernel */
300	mov	x28, #(KERNBASE)
301	add	x28, x28, x29
302	ret
303
304	.align 3
305virt_map:
306	.quad	virt_map
307
308/*
309 * This builds the page tables containing the identity map, and the kernel
310 * virtual map.
311 *
312 * It relys on:
313 *  We were loaded to an address that is on a 2MiB boundary
314 *  All the memory must not cross a 1GiB boundaty
315 *  x28 contains the physical address we were loaded from
316 *
317 * TODO: This is out of date.
318 *  There are at least 5 pages before that address for the page tables
319 *   The pages used are:
320 *    - The identity (PA = VA) table (TTBR0)
321 *    - The Kernel L1 table          (TTBR1)(not yet)
322 *    -  The PA != VA L2 table to jump into (not yet)
323 *    -  The FDT L2 table                   (not yet)
324 */
325create_pagetables:
326	/* Save the Link register */
327	mov	x5, x30
328
329	/* Clean the page table */
330	adr	x6, pagetable
331	mov	x26, x6
332	adr	x27, pagetable_end
3331:
334	stp	xzr, xzr, [x6], #16
335	stp	xzr, xzr, [x6], #16
336	stp	xzr, xzr, [x6], #16
337	stp	xzr, xzr, [x6], #16
338	cmp	x6, x27
339	b.lo	1b
340
341	/*
342	 * Build the TTBR1 maps.
343	 */
344
345	/* Find the size of the kernel */
346	mov	x6, #(KERNBASE)
347	ldr	x7, .Lend
348	/* Find the end - begin */
349	sub	x8, x7, x6
350	/* Get the number of l2 pages to allocate, rounded down */
351	lsr	x10, x8, #(L2_SHIFT)
352	/* Add 4 MiB for any rounding above and the module data */
353	add	x10, x10, #2
354
355	/* Create the kernel space L2 table */
356	mov	x6, x26
357	mov	x7, #NORMAL_MEM
358	mov	x8, #(KERNBASE & L2_BLOCK_MASK)
359	mov	x9, x28
360	bl	build_block_pagetable
361
362	/* Move to the l1 table */
363	add	x26, x26, #PAGE_SIZE
364
365	/* Link the l1 -> l2 table */
366	mov	x9, x6
367	mov	x6, x26
368	bl	link_l1_pagetable
369
370
371	/*
372	 * Build the TTBR0 maps.
373	 */
374	add	x27, x26, #PAGE_SIZE
375
376#if defined(SOCDEV_PA) && defined(SOCDEV_VA)
377	/* Create a table for the UART */
378	mov	x6, x27		/* The initial page table */
379	mov	x7, #DEVICE_MEM
380	mov	x8, #(SOCDEV_VA)	/* VA start */
381	mov	x9, #(SOCDEV_PA)	/* PA start */
382	bl	build_section_pagetable
383#endif
384
385	/* Create the VA = PA map */
386	mov	x6, x27		/* The initial page table */
387	mov	x7, #NORMAL_UNCACHED /* Uncached as it's only needed early on */
388	mov	x9, x27
389	mov	x8, x9		/* VA start (== PA start) */
390	bl	build_section_pagetable
391
392	/* Restore the Link register */
393	mov	x30, x5
394	ret
395
396/*
397 * Builds a 1 GiB page table entry
398 *  x6 = L1 table
399 *  x7 = Type (0 = Device, 1 = Normal)
400 *  x8 = VA start
401 *  x9 = PA start (trashed)
402 *  x11, x12 and x13 are trashed
403 */
404build_section_pagetable:
405	/*
406	 * Build the L1 table entry.
407	 */
408	/* Find the table index */
409	lsr	x11, x8, #L1_SHIFT
410	and	x11, x11, #Ln_ADDR_MASK
411
412	/* Build the L1 block entry */
413	lsl	x12, x7, #2
414	orr	x12, x12, #L1_BLOCK
415	orr	x12, x12, #(ATTR_AF)
416
417	/* Only use the output address bits */
418	lsr	x9, x9, #L1_SHIFT
419	orr	x12, x12, x9, lsl #L1_SHIFT
420
421	/* Store the entry */
422	str	x12, [x6, x11, lsl #3]
423
424	ret
425
426/*
427 * Builds an L1 -> L2 table descriptor
428 *
429 * This is a link for a 1GiB block of memory with up to 2MiB regions mapped
430 * within it by build_block_pagetable.
431 *
432 *  x6  = L1 table
433 *  x8  = Virtual Address
434 *  x9  = L2 PA (trashed)
435 *  x11, x12 and x13 are trashed
436 */
437link_l1_pagetable:
438	/*
439	 * Link an L1 -> L2 table entry.
440	 */
441	/* Find the table index */
442	lsr	x11, x8, #L1_SHIFT
443	and	x11, x11, #Ln_ADDR_MASK
444
445	/* Build the L1 block entry */
446	mov	x12, #L1_TABLE
447
448	/* Only use the output address bits */
449	lsr	x9, x9, #12
450	orr	x12, x12, x9, lsl #12
451
452	/* Store the entry */
453	str	x12, [x6, x11, lsl #3]
454
455	ret
456
457/*
458 * Builds count 2 MiB page table entry
459 *  x6  = L2 table
460 *  x7  = Type (0 = Device, 1 = Normal)
461 *  x8  = VA start
462 *  x9  = PA start (trashed)
463 *  x10 = Entry count (TODO)
464 *  x11, x12 and x13 are trashed
465 */
466build_block_pagetable:
467	/*
468	 * Build the L2 table entry.
469	 */
470	/* Find the table index */
471	lsr	x11, x8, #L2_SHIFT
472	and	x11, x11, #Ln_ADDR_MASK
473
474	/* Build the L2 block entry */
475	lsl	x12, x7, #2
476	orr	x12, x12, #L2_BLOCK
477	orr	x12, x12, #(ATTR_AF)
478#ifdef SMP
479	orr	x12, x12, ATTR_SH(ATTR_SH_IS)
480#endif
481
482	/* Only use the output address bits */
483	lsr	x9, x9, #L2_SHIFT
484
485	/* Set the physical address for this virtual address */
4861:	orr	x12, x12, x9, lsl #L2_SHIFT
487
488	/* Store the entry */
489	str	x12, [x6, x11, lsl #3]
490
491	/* Clear the address bits */
492	and	x12, x12, #ATTR_MASK_L
493
494	sub	x10, x10, #1
495	add	x11, x11, #1
496	add	x9, x9, #1
497	cbnz	x10, 1b
498
4992:	ret
500
501start_mmu:
502	dsb	sy
503
504	/* Load the exception vectors */
505	ldr	x2, =exception_vectors
506	msr	vbar_el1, x2
507
508	/* Load ttbr0 and ttbr1 */
509	msr	ttbr0_el1, x27
510	msr	ttbr1_el1, x26
511	isb
512
513	/* Clear the Monitor Debug System control register */
514	msr	mdscr_el1, xzr
515
516	/* Invalidate the TLB */
517	tlbi	vmalle1is
518
519	ldr	x2, mair
520	msr	mair_el1, x2
521
522	/* Setup TCR according to PARange bits from ID_AA64MMFR0_EL1 */
523	ldr	x2, tcr
524	mrs	x3, id_aa64mmfr0_el1
525	bfi	x2, x3, #32, #3
526	msr	tcr_el1, x2
527
528	/* Setup SCTLR */
529	ldr	x2, sctlr_set
530	ldr	x3, sctlr_clear
531	mrs	x1, sctlr_el1
532	bic	x1, x1, x3	/* Clear the required bits */
533	orr	x1, x1, x2	/* Set the required bits */
534	msr	sctlr_el1, x1
535	isb
536
537	ret
538
539	.align 3
540mair:
541		/* Device            Normal, no cache     Normal, write-back */
542	.quad	MAIR_ATTR(0x00, 0) | MAIR_ATTR(0x44, 1) | MAIR_ATTR(0xff, 2)
543tcr:
544	.quad (TCR_TxSZ(64 - VIRT_BITS) | TCR_ASID_16 | TCR_TG1_4K | \
545	    TCR_CACHE_ATTRS | TCR_SMP_ATTRS)
546sctlr_set:
547	/* Bits to set */
548	.quad (SCTLR_UCI | SCTLR_nTWE | SCTLR_nTWI | SCTLR_UCT | SCTLR_DZE | \
549	    SCTLR_I | SCTLR_SED | SCTLR_C | SCTLR_M)
550sctlr_clear:
551	/* Bits to clear */
552	.quad (SCTLR_EE | SCTLR_EOE | SCTLR_WXN | SCTLR_UMA | SCTLR_ITD | \
553	    SCTLR_THEE | SCTLR_CP15BEN | SCTLR_SA0 | SCTLR_SA | SCTLR_A)
554
555	.globl abort
556abort:
557	b abort
558
559	//.section .init_pagetable
560	.align 12 /* 4KiB aligned */
561	/*
562	 * 3 initial tables (in the following order):
563	 *           L2 for kernel (High addresses)
564	 *           L1 for kernel
565	 *           L1 for user   (Low addresses)
566	 */
567pagetable:
568	.space	PAGE_SIZE
569pagetable_l1_ttbr1:
570	.space	PAGE_SIZE
571pagetable_l1_ttbr0:
572	.space	PAGE_SIZE
573pagetable_end:
574
575el2_pagetable:
576	.space	PAGE_SIZE
577
578	.globl init_pt_va
579init_pt_va:
580	.quad pagetable		/* XXX: Keep page tables VA */
581
582	.align	4
583initstack:
584	.space	(PAGE_SIZE * KSTACK_PAGES)
585initstack_end:
586
587
588ENTRY(sigcode)
589	mov	x0, sp
590	add	x0, x0, #SF_UC
591
5921:
593	mov	x8, #SYS_sigreturn
594	svc	0
595
596	/* sigreturn failed, exit */
597	mov	x8, #SYS_exit
598	svc	0
599
600	b	1b
601END(sigcode)
602	/* This may be copied to the stack, keep it 16-byte aligned */
603	.align	3
604esigcode:
605
606	.data
607	.align	3
608	.global	szsigcode
609szsigcode:
610	.quad	esigcode - sigcode
611