aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / arch / vectors.S
| 1 | /* vectors.S -- exception vectors, EL2 -> EL1h entry, recoverable probe. |
| 2 | * ck_vectors serves VBAR_EL1 only; ck_vectors_el2 (fatal-only) serves VBAR_EL2 |
| 3 | * before the drop to EL1. |
| 4 | * TPIDR_EL1 and TPIDRRO_EL0 are scratch registers in the entry path; every |
| 5 | * exit clears TPIDRRO_EL0 so no kernel value is readable from EL0 (the M3 |
| 6 | * EL0 tasks, core/m3.c). Frame layout: struct ck_frame in arch.h. */ |
| 7 | #define FRAME 272 |
| 8 | |
| 9 | .text |
| 10 | |
| 11 | .macro VENTRY idx |
| 12 | .balign 0x80 |
| 13 | msr tpidr_el1, x0 |
| 14 | msr tpidrro_el0, x1 |
| 15 | mov x1, #\idx |
| 16 | b ck_vec_common |
| 17 | .endm |
| 18 | |
| 19 | .balign 2048 |
| 20 | .global ck_vectors |
| 21 | ck_vectors: |
| 22 | VENTRY 0 |
| 23 | VENTRY 1 |
| 24 | VENTRY 2 |
| 25 | VENTRY 3 |
| 26 | VENTRY 4 |
| 27 | VENTRY 5 |
| 28 | VENTRY 6 |
| 29 | VENTRY 7 |
| 30 | VENTRY 8 |
| 31 | VENTRY 9 |
| 32 | VENTRY 10 |
| 33 | VENTRY 11 |
| 34 | VENTRY 12 |
| 35 | VENTRY 13 |
| 36 | VENTRY 14 |
| 37 | VENTRY 15 |
| 38 | |
| 39 | ck_vec_common: |
| 40 | /* Stack guard: if SP is within one frame of the floor (overflow into the |
| 41 | * guard page), continue on the emergency stack so the fault can be |
| 42 | * reported instead of recursing. */ |
| 43 | adrp x0, ck_stack_floor |
| 44 | ldr x0, [x0, :lo12:ck_stack_floor] |
| 45 | add x0, x0, #FRAME |
| 46 | cmp sp, x0 |
| 47 | b.hs 1f |
| 48 | adrp x0, ck_emergency_stack_top |
| 49 | add x0, x0, :lo12:ck_emergency_stack_top |
| 50 | mov sp, x0 |
| 51 | 1: sub sp, sp, #FRAME |
| 52 | stp x2, x3, [sp, #16] |
| 53 | stp x4, x5, [sp, #32] |
| 54 | stp x6, x7, [sp, #48] |
| 55 | stp x8, x9, [sp, #64] |
| 56 | stp x10, x11, [sp, #80] |
| 57 | stp x12, x13, [sp, #96] |
| 58 | stp x14, x15, [sp, #112] |
| 59 | stp x16, x17, [sp, #128] |
| 60 | stp x18, x19, [sp, #144] |
| 61 | stp x20, x21, [sp, #160] |
| 62 | stp x22, x23, [sp, #176] |
| 63 | stp x24, x25, [sp, #192] |
| 64 | stp x26, x27, [sp, #208] |
| 65 | stp x28, x29, [sp, #224] |
| 66 | mrs x2, tpidr_el1 |
| 67 | mrs x3, tpidrro_el0 |
| 68 | stp x2, x3, [sp, #0] |
| 69 | mrs x2, elr_el1 |
| 70 | stp x30, x2, [sp, #240] |
| 71 | mrs x2, spsr_el1 |
| 72 | stp x2, x1, [sp, #256] |
| 73 | mov x0, sp |
| 74 | cmp x1, #5 |
| 75 | b.eq 4f |
| 76 | cmp x1, #9 |
| 77 | b.eq 4f |
| 78 | bl ck_exception |
| 79 | b 3f |
| 80 | /* IRQ: the dispatcher returns the frame to resume. It is the same frame |
| 81 | * unless the M3 preemption check switches to another task's frame, which |
| 82 | * sits at the top of that task's stack, so SP moves with it. */ |
| 83 | 4: bl ck_irq_dispatch |
| 84 | mov sp, x0 |
| 85 | 3: msr tpidrro_el0, xzr /* scratch never leaks to EL0 */ |
| 86 | ldp x30, x2, [sp, #240] |
| 87 | msr elr_el1, x2 |
| 88 | ldr x2, [sp, #256] |
| 89 | msr spsr_el1, x2 |
| 90 | ldp x0, x1, [sp, #0] |
| 91 | ldp x2, x3, [sp, #16] |
| 92 | ldp x4, x5, [sp, #32] |
| 93 | ldp x6, x7, [sp, #48] |
| 94 | ldp x8, x9, [sp, #64] |
| 95 | ldp x10, x11, [sp, #80] |
| 96 | ldp x12, x13, [sp, #96] |
| 97 | ldp x14, x15, [sp, #112] |
| 98 | ldp x16, x17, [sp, #128] |
| 99 | ldp x18, x19, [sp, #144] |
| 100 | ldp x20, x21, [sp, #160] |
| 101 | ldp x22, x23, [sp, #176] |
| 102 | ldp x24, x25, [sp, #192] |
| 103 | ldp x26, x27, [sp, #208] |
| 104 | ldp x28, x29, [sp, #224] |
| 105 | add sp, sp, #FRAME |
| 106 | eret |
| 107 | |
| 108 | /* EL2 vectors, live only between the kernel entry at EL2 and the drop to EL1. |
| 109 | * Every EL2 exception is fatal: mask everything, move to the emergency stack |
| 110 | * (the firmware stack may be the cause) and report ESR_EL2/FAR_EL2/ELR_EL2, |
| 111 | * then reset. Nothing here touches the EL1 state or returns. */ |
| 112 | .macro VENTRY_EL2 idx |
| 113 | .balign 0x80 |
| 114 | msr daifset, #0xf |
| 115 | mov x0, #\idx |
| 116 | b ck_vec_el2_common |
| 117 | .endm |
| 118 | |
| 119 | .balign 2048 |
| 120 | .global ck_vectors_el2 |
| 121 | ck_vectors_el2: |
| 122 | VENTRY_EL2 0 |
| 123 | VENTRY_EL2 1 |
| 124 | VENTRY_EL2 2 |
| 125 | VENTRY_EL2 3 |
| 126 | VENTRY_EL2 4 |
| 127 | VENTRY_EL2 5 |
| 128 | VENTRY_EL2 6 |
| 129 | VENTRY_EL2 7 |
| 130 | VENTRY_EL2 8 |
| 131 | VENTRY_EL2 9 |
| 132 | VENTRY_EL2 10 |
| 133 | VENTRY_EL2 11 |
| 134 | VENTRY_EL2 12 |
| 135 | VENTRY_EL2 13 |
| 136 | VENTRY_EL2 14 |
| 137 | VENTRY_EL2 15 |
| 138 | |
| 139 | ck_vec_el2_common: |
| 140 | adrp x1, ck_emergency_stack_top |
| 141 | add x1, x1, :lo12:ck_emergency_stack_top |
| 142 | mov sp, x1 |
| 143 | mov x29, xzr |
| 144 | mov x30, xzr |
| 145 | bl ck_el2_fatal |
| 146 | 1: wfi |
| 147 | b 1b |
| 148 | |
| 149 | /* int ck_probe_read(const volatile void *addr) */ |
| 150 | .global ck_probe_read |
| 151 | .type ck_probe_read, %function |
| 152 | ck_probe_read: |
| 153 | adrp x1, ck_probe_state |
| 154 | add x1, x1, :lo12:ck_probe_state |
| 155 | adr x2, 0f |
| 156 | str x2, [x1, #8] |
| 157 | adr x2, 1f |
| 158 | str x2, [x1, #0] |
| 159 | dsb sy |
| 160 | isb |
| 161 | 0: ldr x3, [x0] |
| 162 | str xzr, [x1, #0] |
| 163 | mov x0, #0 |
| 164 | ret |
| 165 | 1: mov x0, #1 |
| 166 | ret |
| 167 | .size ck_probe_read, . - ck_probe_read |
| 168 | |
| 169 | /* ck_enter_el1(root x0, mair x1, tcr x2, sctlr x3, sp x4, entry x5, arg x6) |
| 170 | * ADR 0009: known EL2 state, EL1 timer and FP access, GIC system registers |
| 171 | * for EL1, then eret to EL1h (SPSR 0x3c5: EL1h, DAIF masked) with the EL1 |
| 172 | * MMU already on our tables. */ |
| 173 | .global ck_enter_el1 |
| 174 | .type ck_enter_el1, %function |
| 175 | ck_enter_el1: |
| 176 | msr daifset, #0xf |
| 177 | mrs x9, midr_el1 |
| 178 | msr vpidr_el2, x9 |
| 179 | mrs x9, mpidr_el1 |
| 180 | msr vmpidr_el2, x9 |
| 181 | mov x9, #(1 << 31) /* HCR_EL2.RW: EL1 is AArch64; no traps, no stage 2 */ |
| 182 | msr hcr_el2, x9 |
| 183 | mov x9, #0x33ff /* CPTR_EL2: RES1 bits, no FP/SIMD traps */ |
| 184 | msr cptr_el2, x9 |
| 185 | msr hstr_el2, xzr |
| 186 | msr vttbr_el2, xzr |
| 187 | mov x9, #3 /* CNTHCTL_EL2: EL1PCTEN | EL1PCEN */ |
| 188 | msr cnthctl_el2, x9 |
| 189 | msr cntvoff_el2, xzr |
| 190 | mrs x9, id_aa64pfr0_el1 /* GIC system registers implemented? */ |
| 191 | ubfx x9, x9, #24, #4 |
| 192 | cbz x9, 1f |
| 193 | mrs x9, S3_4_C12_C9_5 /* ICC_SRE_EL2 */ |
| 194 | mov x10, #0x9 /* SRE | Enable (EL1 may use ICC_SRE_EL1) */ |
| 195 | orr x9, x9, x10 |
| 196 | msr S3_4_C12_C9_5, x9 |
| 197 | isb |
| 198 | msr S3_4_C12_C11_0, xzr /* ICH_HCR_EL2: no virtual CPU interface */ |
| 199 | 1: mov x9, #(3 << 20) /* CPACR_EL1.FPEN: no FP/SIMD traps */ |
| 200 | msr cpacr_el1, x9 |
| 201 | msr mair_el1, x1 |
| 202 | msr tcr_el1, x2 |
| 203 | msr ttbr0_el1, x0 |
| 204 | msr ttbr1_el1, xzr |
| 205 | dsb ish |
| 206 | tlbi vmalle1 |
| 207 | dsb ish |
| 208 | ic iallu |
| 209 | dsb ish |
| 210 | isb |
| 211 | msr sctlr_el1, x3 |
| 212 | isb |
| 213 | msr sp_el1, x4 |
| 214 | adrp x9, ck_vectors |
| 215 | add x9, x9, :lo12:ck_vectors |
| 216 | msr vbar_el1, x9 |
| 217 | mov x9, #0x3c5 |
| 218 | msr spsr_el2, x9 |
| 219 | msr elr_el2, x5 |
| 220 | mov x0, x6 |
| 221 | mov x30, xzr |
| 222 | mov x29, xzr |
| 223 | isb |
| 224 | eret |
| 225 | .size ck_enter_el1, . - ck_enter_el1 |
| 226 | |
| 227 | /* ck_switch_el1: firmware handed over at EL1 already. Turn the MMU off, |
| 228 | * install our tables, turn it on, switch stacks. Code runs identity mapped |
| 229 | * in both regimes, so the PC stays valid. */ |
| 230 | .global ck_switch_el1 |
| 231 | .type ck_switch_el1, %function |
| 232 | ck_switch_el1: |
| 233 | msr daifset, #0xf |
| 234 | mrs x9, sctlr_el1 |
| 235 | bic x9, x9, #1 |
| 236 | msr sctlr_el1, x9 |
| 237 | isb |
| 238 | msr mair_el1, x1 |
| 239 | msr tcr_el1, x2 |
| 240 | msr ttbr0_el1, x0 |
| 241 | msr ttbr1_el1, xzr |
| 242 | dsb ish |
| 243 | tlbi vmalle1 |
| 244 | dsb ish |
| 245 | ic iallu |
| 246 | dsb ish |
| 247 | isb |
| 248 | msr sctlr_el1, x3 |
| 249 | isb |
| 250 | mov sp, x4 |
| 251 | adrp x9, ck_vectors |
| 252 | add x9, x9, :lo12:ck_vectors |
| 253 | msr vbar_el1, x9 |
| 254 | isb |
| 255 | mov x0, x6 |
| 256 | mov x29, xzr |
| 257 | mov x30, xzr |
| 258 | br x5 |
| 259 | .size ck_switch_el1, . - ck_switch_el1 |
| 260 | |
| 261 | .bss |
| 262 | .balign 16 |
| 263 | ck_emergency_stack: |
| 264 | .space 16384 |
| 265 | ck_emergency_stack_top: |