]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/locore.S
zfs: merge openzfs/zfs@6c3c5fcfb (zfs-2.1-release) into stable/13
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / locore.S
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.inc"
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 #include <machine/vm.h>
38 #include <machine/vmparam.h>
39
40 #define VIRT_BITS       48
41
42         .globl  kernbase
43         .set    kernbase, KERNBASE
44
45 /*
46  * We assume:
47  *  MMU      on with an identity map, or off
48  *  D-Cache: off
49  *  I-Cache: on or off
50  *  We are loaded at a 2MiB aligned address
51  */
52
53 ENTRY(_start)
54         /* Drop to EL1 */
55         bl      drop_to_el1
56
57         /*
58          * Disable the MMU. We may have entered the kernel with it on and
59          * will need to update the tables later. If this has been set up
60          * with anything other than a VA == PA map then this will fail,
61          * but in this case the code to find where we are running from
62          * would have also failed.
63          */
64         dsb     sy
65         mrs     x2, sctlr_el1
66         bic     x2, x2, SCTLR_M
67         msr     sctlr_el1, x2
68         isb
69
70         /* Set the context id */
71         msr     contextidr_el1, xzr
72
73         /* Get the virt -> phys offset */
74         bl      get_virt_delta
75
76         /*
77          * At this point:
78          * x29 = PA - VA
79          * x28 = Our physical load address
80          */
81
82         /* Create the page tables */
83         bl      create_pagetables
84
85         /*
86          * At this point:
87          * x27 = TTBR0 table
88          * x26 = Kernel L1 table
89          * x24 = TTBR1 table
90          */
91
92         /* Enable the mmu */
93         bl      start_mmu
94
95         /* Load the new ttbr0 pagetable */
96         adrp    x27, pagetable_l0_ttbr0
97         add     x27, x27, :lo12:pagetable_l0_ttbr0
98
99         /* Jump to the virtual address space */
100         ldr     x15, .Lvirtdone
101         br      x15
102
103 virtdone:
104         /* Set up the stack */
105         adrp    x25, initstack_end
106         add     x25, x25, :lo12:initstack_end
107         mov     sp, x25
108         sub     sp, sp, #PCB_SIZE
109
110         /* Zero the BSS */
111         ldr     x15, .Lbss
112         ldr     x14, .Lend
113 1:
114         str     xzr, [x15], #8
115         cmp     x15, x14
116         b.lo    1b
117
118 #if defined(PERTHREAD_SSP)
119         /* Set sp_el0 to the boot canary for early per-thread SSP to work */
120         adrp    x15, boot_canary
121         add     x15, x15, :lo12:boot_canary
122         msr     sp_el0, x15
123 #endif
124
125         /* Backup the module pointer */
126         mov     x1, x0
127
128         /* Make the page table base a virtual address */
129         sub     x26, x26, x29
130         sub     x24, x24, x29
131
132         sub     sp, sp, #BOOTPARAMS_SIZE
133         mov     x0, sp
134
135         /* Degate the delda so it is VA -> PA */
136         neg     x29, x29
137
138         str     x1,  [x0, #BP_MODULEP]
139         str     x26, [x0, #BP_KERN_L1PT]
140         str     x29, [x0, #BP_KERN_DELTA]
141         adrp    x25, initstack
142         add     x25, x25, :lo12:initstack
143         str     x25, [x0, #BP_KERN_STACK]
144         str     x24, [x0, #BP_KERN_L0PT]
145         str     x27, [x0, #BP_KERN_TTBR0]
146         str     x23, [x0, #BP_BOOT_EL]
147
148         /* trace back starts here */
149         mov     fp, #0
150         /* Branch to C code */
151         bl      initarm
152         /* We are done with the boot params */
153         add     sp, sp, #BOOTPARAMS_SIZE
154         bl      mi_startup
155
156         /* We should not get here */
157         brk     0
158
159         .align 3
160 .Lvirtdone:
161         .quad   virtdone
162 .Lbss:
163         .quad   __bss_start
164 .Lend:
165         .quad   __bss_end
166 END(_start)
167
168 #ifdef SMP
169 /*
170  * mpentry(unsigned long)
171  *
172  * Called by a core when it is being brought online.
173  * The data in x0 is passed straight to init_secondary.
174  */
175 ENTRY(mpentry)
176         /* Disable interrupts */
177         msr     daifset, #DAIF_INTR
178
179         /* Drop to EL1 */
180         bl      drop_to_el1
181
182         /* Set the context id */
183         msr     contextidr_el1, xzr
184
185         /* Load the kernel page table */
186         adrp    x24, pagetable_l0_ttbr1
187         add     x24, x24, :lo12:pagetable_l0_ttbr1
188         /* Load the identity page table */
189         adrp    x27, pagetable_l0_ttbr0_boostrap
190         add     x27, x27, :lo12:pagetable_l0_ttbr0_boostrap
191
192         /* Enable the mmu */
193         bl      start_mmu
194
195         /* Load the new ttbr0 pagetable */
196         adrp    x27, pagetable_l0_ttbr0
197         add     x27, x27, :lo12:pagetable_l0_ttbr0
198
199         /* Jump to the virtual address space */
200         ldr     x15, =mp_virtdone
201         br      x15
202
203 mp_virtdone:
204         /* Start using the AP boot stack */
205         ldr     x4, =bootstack
206         ldr     x4, [x4]
207         mov     sp, x4
208
209 #if defined(PERTHREAD_SSP)
210         /* Set sp_el0 to the boot canary for early per-thread SSP to work */
211         adrp    x15, boot_canary
212         add     x15, x15, :lo12:boot_canary
213         msr     sp_el0, x15
214 #endif
215
216         /* Load the kernel ttbr0 pagetable */
217         msr     ttbr0_el1, x27
218         isb
219
220         /* Invalidate the TLB */
221         tlbi    vmalle1
222         dsb     sy
223         isb
224
225         b       init_secondary
226 END(mpentry)
227 #endif
228
229 /*
230  * If we are started in EL2, configure the required hypervisor
231  * registers and drop to EL1.
232  */
233 LENTRY(drop_to_el1)
234         mrs     x23, CurrentEL
235         lsr     x23, x23, #2
236         cmp     x23, #0x2
237         b.eq    1f
238         ret
239 1:
240         /* Configure the Hypervisor */
241         mov     x2, #(HCR_RW)
242         msr     hcr_el2, x2
243
244         /* Load the Virtualization Process ID Register */
245         mrs     x2, midr_el1
246         msr     vpidr_el2, x2
247
248         /* Load the Virtualization Multiprocess ID Register */
249         mrs     x2, mpidr_el1
250         msr     vmpidr_el2, x2
251
252         /* Set the bits that need to be 1 in sctlr_el1 */
253         ldr     x2, .Lsctlr_res1
254         msr     sctlr_el1, x2
255
256         /* Don't trap to EL2 for exceptions */
257         mov     x2, #CPTR_RES1
258         msr     cptr_el2, x2
259
260         /* Don't trap to EL2 for CP15 traps */
261         msr     hstr_el2, xzr
262
263         /* Enable access to the physical timers at EL1 */
264         mrs     x2, cnthctl_el2
265         orr     x2, x2, #(CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN)
266         msr     cnthctl_el2, x2
267
268         /* Set the counter offset to a known value */
269         msr     cntvoff_el2, xzr
270
271         /* Hypervisor trap functions */
272         adrp    x2, hyp_vectors
273         add     x2, x2, :lo12:hyp_vectors
274         msr     vbar_el2, x2
275
276         mov     x2, #(PSR_F | PSR_I | PSR_A | PSR_D | PSR_M_EL1h)
277         msr     spsr_el2, x2
278
279         /* Configure GICv3 CPU interface */
280         mrs     x2, id_aa64pfr0_el1
281         /* Extract GIC bits from the register */
282         ubfx    x2, x2, #ID_AA64PFR0_GIC_SHIFT, #ID_AA64PFR0_GIC_BITS
283         /* GIC[3:0] == 0001 - GIC CPU interface via special regs. supported */
284         cmp     x2, #(ID_AA64PFR0_GIC_CPUIF_EN >> ID_AA64PFR0_GIC_SHIFT)
285         b.ne    2f
286
287         mrs     x2, icc_sre_el2
288         orr     x2, x2, #ICC_SRE_EL2_EN /* Enable access from insecure EL1 */
289         orr     x2, x2, #ICC_SRE_EL2_SRE        /* Enable system registers */
290         msr     icc_sre_el2, x2
291 2:
292
293         /* Set the address to return to our return address */
294         msr     elr_el2, x30
295         isb
296
297         eret
298
299         .align 3
300 .Lsctlr_res1:
301         .quad SCTLR_RES1
302 LEND(drop_to_el1)
303
304 #define VECT_EMPTY      \
305         .align 7;       \
306         1:      b       1b
307
308         .align 11
309 hyp_vectors:
310         VECT_EMPTY      /* Synchronous EL2t */
311         VECT_EMPTY      /* IRQ EL2t */
312         VECT_EMPTY      /* FIQ EL2t */
313         VECT_EMPTY      /* Error EL2t */
314
315         VECT_EMPTY      /* Synchronous EL2h */
316         VECT_EMPTY      /* IRQ EL2h */
317         VECT_EMPTY      /* FIQ EL2h */
318         VECT_EMPTY      /* Error EL2h */
319
320         VECT_EMPTY      /* Synchronous 64-bit EL1 */
321         VECT_EMPTY      /* IRQ 64-bit EL1 */
322         VECT_EMPTY      /* FIQ 64-bit EL1 */
323         VECT_EMPTY      /* Error 64-bit EL1 */
324
325         VECT_EMPTY      /* Synchronous 32-bit EL1 */
326         VECT_EMPTY      /* IRQ 32-bit EL1 */
327         VECT_EMPTY      /* FIQ 32-bit EL1 */
328         VECT_EMPTY      /* Error 32-bit EL1 */
329
330 /*
331  * Get the delta between the physical address we were loaded to and the
332  * virtual address we expect to run from. This is used when building the
333  * initial page table.
334  */
335 LENTRY(get_virt_delta)
336         /* Load the physical address of virt_map */
337         adrp    x29, virt_map
338         add     x29, x29, :lo12:virt_map
339         /* Load the virtual address of virt_map stored in virt_map */
340         ldr     x28, [x29]
341         /* Find PA - VA as PA' = VA' - VA + PA = VA' + (PA - VA) = VA' + x29 */
342         sub     x29, x29, x28
343         /* Find the load address for the kernel */
344         mov     x28, #(KERNBASE)
345         add     x28, x28, x29
346         ret
347
348         .align 3
349 virt_map:
350         .quad   virt_map
351 LEND(get_virt_delta)
352
353 /*
354  * This builds the page tables containing the identity map, and the kernel
355  * virtual map.
356  *
357  * It relys on:
358  *  We were loaded to an address that is on a 2MiB boundary
359  *  All the memory must not cross a 1GiB boundaty
360  *  x28 contains the physical address we were loaded from
361  *
362  * TODO: This is out of date.
363  *  There are at least 5 pages before that address for the page tables
364  *   The pages used are:
365  *    - The Kernel L2 table
366  *    - The Kernel L1 table
367  *    - The Kernel L0 table             (TTBR1)
368  *    - The identity (PA = VA) L1 table
369  *    - The identity (PA = VA) L0 table (TTBR0)
370  */
371 LENTRY(create_pagetables)
372         /* Save the Link register */
373         mov     x5, x30
374
375         /* Clean the page table */
376         adrp    x6, pagetable
377         add     x6, x6, :lo12:pagetable
378         mov     x26, x6
379         adrp    x27, pagetable_end
380         add     x27, x27, :lo12:pagetable_end
381 1:
382         stp     xzr, xzr, [x6], #16
383         stp     xzr, xzr, [x6], #16
384         stp     xzr, xzr, [x6], #16
385         stp     xzr, xzr, [x6], #16
386         cmp     x6, x27
387         b.lo    1b
388
389         /*
390          * Build the TTBR1 maps.
391          */
392
393         /* Find the size of the kernel */
394         mov     x6, #(KERNBASE)
395
396 #if defined(LINUX_BOOT_ABI)
397         /* X19 is used as 'map FDT data' flag */
398         mov     x19, xzr
399
400         /* No modules or FDT pointer ? */
401         cbz     x0, booti_no_fdt
402
403         /*
404          * Test if x0 points to modules descriptor(virtual address) or
405          * to FDT (physical address)
406          */
407         cmp     x0, x6          /* x6 is #(KERNBASE) */
408         b.lo    booti_fdt
409 #endif
410
411         /* Booted with modules pointer */
412         /* Find modulep - begin */
413         sub     x8, x0, x6
414         /* Add two 2MiB pages for the module data and round up */
415         ldr     x7, =(3 * L2_SIZE - 1)
416         add     x8, x8, x7
417         b       common
418
419 #if defined(LINUX_BOOT_ABI)
420 booti_fdt:
421         /* Booted by U-Boot booti with FDT data */
422         /* Set 'map FDT data' flag */
423         mov     x19, #1
424
425 booti_no_fdt:
426         /* Booted by U-Boot booti without FTD data */
427         /* Find the end - begin */
428         ldr     x7, .Lend
429         sub     x8, x7, x6
430
431         /*
432          * Add one 2MiB page for copy of FDT data (maximum FDT size),
433          * one for metadata and round up
434          */
435         ldr     x7, =(3 * L2_SIZE - 1)
436         add     x8, x8, x7
437 #endif
438
439 common:
440         /* Get the number of l2 pages to allocate, rounded down */
441         lsr     x10, x8, #(L2_SHIFT)
442
443         /* Create the kernel space L2 table */
444         mov     x6, x26
445         mov     x7, #VM_MEMATTR_WRITE_BACK
446         mov     x8, #(KERNBASE & L2_BLOCK_MASK)
447         mov     x9, x28
448         bl      build_l2_block_pagetable
449
450         /* Move to the l1 table */
451         add     x26, x26, #PAGE_SIZE
452
453         /* Link the l1 -> l2 table */
454         mov     x9, x6
455         mov     x6, x26
456         bl      link_l1_pagetable
457
458         /* Move to the l0 table */
459         add     x24, x26, #PAGE_SIZE
460
461         /* Link the l0 -> l1 table */
462         mov     x9, x6
463         mov     x6, x24
464         mov     x10, #1
465         bl      link_l0_pagetable
466
467         /*
468          * Build the TTBR0 maps.  As TTBR0 maps, they must specify ATTR_S1_nG.
469          * They are only needed early on, so the VA = PA map is uncached.
470          */
471         add     x27, x24, #PAGE_SIZE
472
473         mov     x6, x27         /* The initial page table */
474 #if defined(SOCDEV_PA) && defined(SOCDEV_VA)
475         /* Create a table for the UART */
476         mov     x7, #(ATTR_S1_nG | ATTR_S1_IDX(VM_MEMATTR_DEVICE))
477         mov     x8, #(SOCDEV_VA)        /* VA start */
478         mov     x9, #(SOCDEV_PA)        /* PA start */
479         mov     x10, #1
480         bl      build_l1_block_pagetable
481 #endif
482
483 #if defined(LINUX_BOOT_ABI)
484         /* Map FDT data ? */
485         cbz     x19, 1f
486
487         /* Create the identity mapping for FDT data (2 MiB max) */
488         mov     x7, #(ATTR_S1_nG | ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK))
489         mov     x9, x0
490         mov     x8, x0          /* VA start (== PA start) */
491         mov     x10, #1
492         bl      build_l1_block_pagetable
493
494 1:
495 #endif
496
497         /* Create the VA = PA map */
498         mov     x7, #(ATTR_S1_nG | ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK))
499         mov     x9, x28
500         mov     x8, x9          /* VA start (== PA start) */
501         mov     x10, #1
502         bl      build_l1_block_pagetable
503
504         /* Move to the l0 table */
505         add     x27, x27, #PAGE_SIZE
506
507         /* Link the l0 -> l1 table */
508         mov     x9, x6
509         mov     x6, x27
510         mov     x10, #1
511         bl      link_l0_pagetable
512
513         /* Restore the Link register */
514         mov     x30, x5
515         ret
516 LEND(create_pagetables)
517
518 /*
519  * Builds an L0 -> L1 table descriptor
520  *
521  * This is a link for a 512GiB block of memory with up to 1GiB regions mapped
522  * within it by build_l1_block_pagetable.
523  *
524  *  x6  = L0 table
525  *  x8  = Virtual Address
526  *  x9  = L1 PA (trashed)
527  *  x10 = Entry count
528  *  x11, x12 and x13 are trashed
529  */
530 LENTRY(link_l0_pagetable)
531         /*
532          * Link an L0 -> L1 table entry.
533          */
534         /* Find the table index */
535         lsr     x11, x8, #L0_SHIFT
536         and     x11, x11, #L0_ADDR_MASK
537
538         /* Build the L0 block entry */
539         mov     x12, #L0_TABLE
540         orr     x12, x12, #(TATTR_UXN_TABLE | TATTR_AP_TABLE_NO_EL0)
541
542         /* Only use the output address bits */
543         lsr     x9, x9, #PAGE_SHIFT
544 1:      orr     x13, x12, x9, lsl #PAGE_SHIFT
545
546         /* Store the entry */
547         str     x13, [x6, x11, lsl #3]
548
549         sub     x10, x10, #1
550         add     x11, x11, #1
551         add     x9, x9, #1
552         cbnz    x10, 1b
553
554         ret
555 LEND(link_l0_pagetable)
556
557 /*
558  * Builds an L1 -> L2 table descriptor
559  *
560  * This is a link for a 1GiB block of memory with up to 2MiB regions mapped
561  * within it by build_l2_block_pagetable.
562  *
563  *  x6  = L1 table
564  *  x8  = Virtual Address
565  *  x9  = L2 PA (trashed)
566  *  x11, x12 and x13 are trashed
567  */
568 LENTRY(link_l1_pagetable)
569         /*
570          * Link an L1 -> L2 table entry.
571          */
572         /* Find the table index */
573         lsr     x11, x8, #L1_SHIFT
574         and     x11, x11, #Ln_ADDR_MASK
575
576         /* Build the L1 block entry */
577         mov     x12, #L1_TABLE
578
579         /* Only use the output address bits */
580         lsr     x9, x9, #PAGE_SHIFT
581         orr     x13, x12, x9, lsl #PAGE_SHIFT
582
583         /* Store the entry */
584         str     x13, [x6, x11, lsl #3]
585
586         ret
587 LEND(link_l1_pagetable)
588
589 /*
590  * Builds count 1 GiB page table entry
591  *  x6  = L1 table
592  *  x7  = Variable lower block attributes
593  *  x8  = VA start
594  *  x9  = PA start (trashed)
595  *  x10 = Entry count
596  *  x11, x12 and x13 are trashed
597  */
598 LENTRY(build_l1_block_pagetable)
599         /*
600          * Build the L1 table entry.
601          */
602         /* Find the table index */
603         lsr     x11, x8, #L1_SHIFT
604         and     x11, x11, #Ln_ADDR_MASK
605
606         /* Build the L1 block entry */
607         orr     x12, x7, #L1_BLOCK
608         orr     x12, x12, #(ATTR_DEFAULT)
609
610         /* Only use the output address bits */
611         lsr     x9, x9, #L1_SHIFT
612
613         /* Set the physical address for this virtual address */
614 1:      orr     x13, x12, x9, lsl #L1_SHIFT
615
616         /* Store the entry */
617         str     x13, [x6, x11, lsl #3]
618
619         sub     x10, x10, #1
620         add     x11, x11, #1
621         add     x9, x9, #1
622         cbnz    x10, 1b
623
624         ret
625 LEND(build_l1_block_pagetable)
626
627 /*
628  * Builds count 2 MiB page table entry
629  *  x6  = L2 table
630  *  x7  = Type (0 = Device, 1 = Normal)
631  *  x8  = VA start
632  *  x9  = PA start (trashed)
633  *  x10 = Entry count
634  *  x11, x12 and x13 are trashed
635  */
636 LENTRY(build_l2_block_pagetable)
637         /*
638          * Build the L2 table entry.
639          */
640         /* Find the table index */
641         lsr     x11, x8, #L2_SHIFT
642         and     x11, x11, #Ln_ADDR_MASK
643
644         /* Build the L2 block entry */
645         lsl     x12, x7, #2
646         orr     x12, x12, #L2_BLOCK
647         orr     x12, x12, #(ATTR_DEFAULT)
648         orr     x12, x12, #(ATTR_S1_UXN)
649
650         /* Only use the output address bits */
651         lsr     x9, x9, #L2_SHIFT
652
653         /* Set the physical address for this virtual address */
654 1:      orr     x13, x12, x9, lsl #L2_SHIFT
655
656         /* Store the entry */
657         str     x13, [x6, x11, lsl #3]
658
659         sub     x10, x10, #1
660         add     x11, x11, #1
661         add     x9, x9, #1
662         cbnz    x10, 1b
663
664         ret
665 LEND(build_l2_block_pagetable)
666
667 LENTRY(start_mmu)
668         dsb     sy
669
670         /* Load the exception vectors */
671         ldr     x2, =exception_vectors
672         msr     vbar_el1, x2
673
674         /* Load ttbr0 and ttbr1 */
675         msr     ttbr0_el1, x27
676         msr     ttbr1_el1, x24
677         isb
678
679         /* Clear the Monitor Debug System control register */
680         msr     mdscr_el1, xzr
681
682         /* Invalidate the TLB */
683         tlbi    vmalle1is
684         dsb     ish
685         isb
686
687         ldr     x2, mair
688         msr     mair_el1, x2
689
690         /*
691          * Setup TCR according to the PARange and ASIDBits fields
692          * from ID_AA64MMFR0_EL1 and the HAFDBS field from the
693          * ID_AA64MMFR1_EL1.  More precisely, set TCR_EL1.AS
694          * to 1 only if the ASIDBits field equals 0b0010.
695          */
696         ldr     x2, tcr
697         mrs     x3, id_aa64mmfr0_el1
698
699         /* Copy the bottom 3 bits from id_aa64mmfr0_el1 into TCR.IPS */
700         bfi     x2, x3, #(TCR_IPS_SHIFT), #(TCR_IPS_WIDTH)
701         and     x3, x3, #(ID_AA64MMFR0_ASIDBits_MASK)
702
703         /* Check if the HW supports 16 bit ASIDS */
704         cmp     x3, #(ID_AA64MMFR0_ASIDBits_16)
705         /* If so x3 == 1, else x3 == 0 */
706         cset    x3, eq
707         /* Set TCR.AS with x3 */
708         bfi     x2, x3, #(TCR_ASID_SHIFT), #(TCR_ASID_WIDTH)
709
710         /*
711          * Check if the HW supports access flag and dirty state updates,
712          * and set TCR_EL1.HA and TCR_EL1.HD accordingly.
713          */
714         mrs     x3, id_aa64mmfr1_el1
715         and     x3, x3, #(ID_AA64MMFR1_HAFDBS_MASK)
716         cmp     x3, #1
717         b.ne    1f
718         orr     x2, x2, #(TCR_HA)
719         b       2f
720 1:
721         cmp     x3, #2
722         b.ne    2f
723         orr     x2, x2, #(TCR_HA | TCR_HD)
724 2:
725         msr     tcr_el1, x2
726
727         /*
728          * Setup SCTLR.
729          */
730         ldr     x2, sctlr_set
731         ldr     x3, sctlr_clear
732         mrs     x1, sctlr_el1
733         bic     x1, x1, x3      /* Clear the required bits */
734         orr     x1, x1, x2      /* Set the required bits */
735         msr     sctlr_el1, x1
736         isb
737
738         ret
739
740         .align 3
741 mair:
742         .quad   MAIR_ATTR(MAIR_DEVICE_nGnRnE, VM_MEMATTR_DEVICE_nGnRnE) | \
743                 MAIR_ATTR(MAIR_NORMAL_NC, VM_MEMATTR_UNCACHEABLE)   |   \
744                 MAIR_ATTR(MAIR_NORMAL_WB, VM_MEMATTR_WRITE_BACK)    |   \
745                 MAIR_ATTR(MAIR_NORMAL_WT, VM_MEMATTR_WRITE_THROUGH) |   \
746                 MAIR_ATTR(MAIR_DEVICE_nGnRE, VM_MEMATTR_DEVICE_nGnRE)
747 tcr:
748         .quad (TCR_TxSZ(64 - VIRT_BITS) | TCR_TG1_4K | TCR_TG0_4K | \
749             TCR_CACHE_ATTRS | TCR_SMP_ATTRS)
750 sctlr_set:
751         /* Bits to set */
752         .quad (SCTLR_LSMAOE | SCTLR_nTLSMD | SCTLR_UCI | SCTLR_SPAN | \
753             SCTLR_nTWE | SCTLR_nTWI | SCTLR_UCT | SCTLR_DZE | \
754             SCTLR_I | SCTLR_SED | SCTLR_SA0 | SCTLR_SA | SCTLR_C | \
755             SCTLR_M | SCTLR_CP15BEN)
756 sctlr_clear:
757         /* Bits to clear */
758         .quad (SCTLR_EE | SCTLR_E0E | SCTLR_IESB | SCTLR_WXN | SCTLR_UMA | \
759             SCTLR_ITD | SCTLR_A)
760 LEND(start_mmu)
761
762 ENTRY(abort)
763         b abort
764 END(abort)
765
766         .section .init_pagetable, "aw", %nobits
767         .align PAGE_SHIFT
768         /*
769          * 6 initial tables (in the following order):
770          *           L2 for kernel (High addresses)
771          *           L1 for kernel
772          *           L0 for kernel
773          *           L1 bootstrap for user   (Low addresses)
774          *           L0 bootstrap for user
775          *           L0 for user
776          */
777         .globl pagetable_l0_ttbr1
778 pagetable:
779         .space  PAGE_SIZE
780 pagetable_l1_ttbr1:
781         .space  PAGE_SIZE
782 pagetable_l0_ttbr1:
783         .space  PAGE_SIZE
784 pagetable_l1_ttbr0_bootstrap:
785         .space  PAGE_SIZE
786 pagetable_l0_ttbr0_boostrap:
787         .space  PAGE_SIZE
788 pagetable_l0_ttbr0:
789         .space  PAGE_SIZE
790
791 pagetable_end:
792
793 el2_pagetable:
794         .space  PAGE_SIZE
795
796         .align  4
797 initstack:
798         .space  (PAGE_SIZE * KSTACK_PAGES)
799 initstack_end:
800
801
802 .text
803 EENTRY(aarch32_sigcode)
804         .word 0xe1a0000d        // mov r0, sp
805         .word 0xe2800040        // add r0, r0, #SIGF_UC
806         .word 0xe59f700c        // ldr r7, [pc, #12]
807         .word 0xef000000        // swi #0
808         .word 0xe59f7008        // ldr r7, [pc, #8]
809         .word 0xef000000        // swi #0
810         .word 0xeafffffa        // b . - 16
811 EEND(aarch32_sigcode)
812         .word SYS_sigreturn
813         .word SYS_exit
814         .align  3
815 aarch32_esigcode:
816         .data
817         .global sz_aarch32_sigcode
818 sz_aarch32_sigcode:
819         .quad aarch32_esigcode - aarch32_sigcode