add basic exception model and context switching for aarch64
[lunaix-os.git] / lunaix-os / arch / aarch64 / boot / init.c
1 #include <lunaix/boot_generic.h>
2 #include <asm/aa64.h>
3
4 #include "init.h"
5
6 static inline void
7 setup_pstate()
8 {
9     /*
10         SCTRL_EL1
11             EE=0, E0E=0     // all little endian
12             WXN=1           // write implie exec never
13             SA0=1, SA=1     // alignment check on SP
14             A=1             // alignment check on memref
15             NMI=1           // mask interrupt
16             M=1             // enable mmu
17     */
18
19    unsigned long sctrl = 0;
20
21    sctrl |= SCTRL_NMI;
22    sctrl |= SCTRL_WXN | SCTRL_nAA;
23    sctrl |= SCTRL_SA | SCTRL_SA0;
24    sctrl |= SCTRL_A | SCTRL_M;
25
26    set_sysreg(TCR_EL1, sctrl);
27    set_sysreg(SPSel, 1);
28 }
29
30 extern void aa64_vbase();
31
32 static inline void
33 setup_evbar()
34 {
35     set_sysreg(VBAR_EL1, aa64_vbase);
36 }
37
38 static inline void
39 setup_ttbr()
40 {
41     /*
42         
43         TCR_EL1
44             SH0=3           // Inner sharable
45             ORGN0=0         // Normal memory, Outer Non-cacheable.
46             IRGN0=1         // Normal memory, Inner Write-Back Read-Allocate Write-Allocate Cacheable.
47             A1=0            // TTBR0 define ASID
48             EPD1=0
49             T1SZ=0
50             EPD0=1
51             T0SZ=16         // disable TTBR1, Use TTBR0 for all translation
52             TG0=0           // VA48, 256T, 4K Granule
53             TBI1=0,
54             TBI0=0          // Ignore top bits
55             AS=1            // 16bits asid
56             HA=1
57             HD=1            // Hardware managed dirty and access
58
59
60         We may use the follow practice later
61             TTBR0:  Translation for user-land (lowmem)
62             TTBR1:  Translation for kernel-land (highmem)
63     */
64
65     unsigned long tcr = 0;
66     ptr_t ttb;
67
68     tcr |= TCR_T1SZ(0) | TCR_T0SZ(16);
69     tcr |= TCR_TG0(TCR_G4K);
70     tcr |= TCR_AS | TCR_HA | TCR_HD;
71     tcr |= TCR_EPD0;
72
73     ttb = kremap();
74
75     set_sysreg(TTBR0_EL1, ttb);
76     set_sysreg(TCR_EL1, tcr);
77 }
78
79 static inline void
80 extract_dtb_bootinfo(ptr_t dtb, struct boot_handoff* handoff)
81 {
82     handoff->kexec.dtb_pa = dtb;
83     
84     // TODO extract /memory, /reserved-memories from dtb
85 }
86
87 struct boot_handoff*
88 aarch64_init(ptr_t dtb)
89 {
90     setup_evbar();
91     setup_ttbr();
92     setup_pstate();
93
94     struct boot_handoff* handoff;
95     
96     handoff = bootmem_alloc(sizeof(*handoff));
97
98     extract_dtb_bootinfo(dtb, handoff);
99
100     return handoff;
101 }