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 / m3.S
| 1 | /* m3.S -- M3 isolation primitives for core/m3.c (port of the global_asm in |
| 2 | * crates/aienos-kernel/src/thread.rs and user.rs): |
| 3 | * ck_ctx_switch cooperative switch of x19-x30 and sp |
| 4 | * ck_thread_start first frame of a cooperative thread |
| 5 | * ck_el0_enter eret to EL0 with every general register chosen |
| 6 | * ck_el0_resume kernel landing point after an EL0 task exits |
| 7 | * ck_el0_demo_* position-independent EL0 program of the el0 check |
| 8 | * ck_ipc_prog_* position-independent EL0 program of the ipc check |
| 9 | * The kernel is built -mgeneral-regs-only, so no kernel code keeps live state |
| 10 | * in FP/SIMD registers and the context holds no d8-d15 (the Rust context |
| 11 | * does). EL0 FP/SIMD use traps (CPACR_EL1.FPEN = 01 while EL0 runs). */ |
| 12 | |
| 13 | .text |
| 14 | |
| 15 | /* void ck_ctx_switch(struct ck_ctx *from x0, const struct ck_ctx *to x1) |
| 16 | * struct ck_ctx { uint64_t x19_x30[12]; uint64_t sp; } */ |
| 17 | .global ck_ctx_switch |
| 18 | .type ck_ctx_switch, %function |
| 19 | ck_ctx_switch: |
| 20 | stp x19, x20, [x0, #0] |
| 21 | stp x21, x22, [x0, #16] |
| 22 | stp x23, x24, [x0, #32] |
| 23 | stp x25, x26, [x0, #48] |
| 24 | stp x27, x28, [x0, #64] |
| 25 | stp x29, x30, [x0, #80] |
| 26 | mov x9, sp |
| 27 | str x9, [x0, #96] |
| 28 | ldp x19, x20, [x1, #0] |
| 29 | ldp x21, x22, [x1, #16] |
| 30 | ldp x23, x24, [x1, #32] |
| 31 | ldp x25, x26, [x1, #48] |
| 32 | ldp x27, x28, [x1, #64] |
| 33 | ldp x29, x30, [x1, #80] |
| 34 | ldr x9, [x1, #96] |
| 35 | mov sp, x9 |
| 36 | ret |
| 37 | .size ck_ctx_switch, . - ck_ctx_switch |
| 38 | |
| 39 | /* Entered by the first ck_ctx_switch into a prepared context: x19 = arg, |
| 40 | * x20 = entry. Never returns: ck_thread_exit parks the thread. */ |
| 41 | .global ck_thread_start |
| 42 | .type ck_thread_start, %function |
| 43 | ck_thread_start: |
| 44 | mov x0, x19 |
| 45 | blr x20 |
| 46 | bl ck_thread_exit |
| 47 | 1: b 1b |
| 48 | .size ck_thread_start, . - ck_thread_start |
| 49 | |
| 50 | /* void ck_el0_enter(uint64_t entry x0, uint64_t user_sp x1, const uint64_t regs[31] x2) |
| 51 | * Saves the kernel callee-saved registers on SP_EL1, records SP_EL1 in |
| 52 | * ck_el0_kernel_sp, loads x0-x30 from regs (no kernel value reaches EL0) and |
| 53 | * erets to EL0t with interrupts unmasked. Returns when the EL0 task exits |
| 54 | * (the syscall handler redirects to ck_el0_resume). */ |
| 55 | .global ck_el0_enter |
| 56 | .type ck_el0_enter, %function |
| 57 | ck_el0_enter: |
| 58 | sub sp, sp, #96 |
| 59 | stp x19, x20, [sp, #0] |
| 60 | stp x21, x22, [sp, #16] |
| 61 | stp x23, x24, [sp, #32] |
| 62 | stp x25, x26, [sp, #48] |
| 63 | stp x27, x28, [sp, #64] |
| 64 | stp x29, x30, [sp, #80] |
| 65 | adrp x9, ck_el0_kernel_sp |
| 66 | mov x10, sp |
| 67 | str x10, [x9, :lo12:ck_el0_kernel_sp] |
| 68 | msr sp_el0, x1 |
| 69 | msr elr_el1, x0 |
| 70 | msr spsr_el1, xzr /* EL0t, DAIF clear */ |
| 71 | msr tpidrro_el0, xzr /* vectors.S scratch: never visible to EL0 */ |
| 72 | msr tpidr_el0, xzr |
| 73 | mov x30, x2 |
| 74 | ldp x0, x1, [x30, #0] |
| 75 | ldp x2, x3, [x30, #16] |
| 76 | ldp x4, x5, [x30, #32] |
| 77 | ldp x6, x7, [x30, #48] |
| 78 | ldp x8, x9, [x30, #64] |
| 79 | ldp x10, x11, [x30, #80] |
| 80 | ldp x12, x13, [x30, #96] |
| 81 | ldp x14, x15, [x30, #112] |
| 82 | ldp x16, x17, [x30, #128] |
| 83 | ldp x18, x19, [x30, #144] |
| 84 | ldp x20, x21, [x30, #160] |
| 85 | ldp x22, x23, [x30, #176] |
| 86 | ldp x24, x25, [x30, #192] |
| 87 | ldp x26, x27, [x30, #208] |
| 88 | ldp x28, x29, [x30, #224] |
| 89 | ldr x30, [x30, #240] |
| 90 | isb |
| 91 | eret |
| 92 | .size ck_el0_enter, . - ck_el0_enter |
| 93 | |
| 94 | /* Reached by eret (SPSR = EL1h, DAIF masked) after the EL0 task exits. */ |
| 95 | .global ck_el0_resume |
| 96 | .type ck_el0_resume, %function |
| 97 | ck_el0_resume: |
| 98 | adrp x9, ck_el0_kernel_sp |
| 99 | ldr x9, [x9, :lo12:ck_el0_kernel_sp] |
| 100 | mov sp, x9 |
| 101 | ldp x19, x20, [sp, #0] |
| 102 | ldp x21, x22, [sp, #16] |
| 103 | ldp x23, x24, [sp, #32] |
| 104 | ldp x25, x26, [sp, #48] |
| 105 | ldp x27, x28, [sp, #64] |
| 106 | ldp x29, x30, [sp, #80] |
| 107 | add sp, sp, #96 |
| 108 | ret |
| 109 | .size ck_el0_resume, . - ck_el0_resume |
| 110 | |
| 111 | /* ---- EL0 programs: copied into a user code page, never run in place ---- */ |
| 112 | .section .rodata.ck_el0, "a" |
| 113 | .balign 4 |
| 114 | /* el0 check (user.rs aienos_user_code): x0 = console handle, x3 = a kernel |
| 115 | * address. write granted, write with a forged handle, read the kernel |
| 116 | * address (must fault and be contained), exit 0. */ |
| 117 | .global ck_el0_demo_start, ck_el0_demo_end |
| 118 | ck_el0_demo_start: |
| 119 | mov x19, x0 |
| 120 | mov x0, x19 |
| 121 | adr x1, 2f |
| 122 | mov x2, #16 |
| 123 | mov x8, #1 |
| 124 | svc #0 |
| 125 | mov x0, xzr |
| 126 | adr x1, 2f |
| 127 | mov x2, #16 |
| 128 | mov x8, #1 |
| 129 | svc #0 |
| 130 | ldr x21, [x3] |
| 131 | mov x0, xzr |
| 132 | mov x8, #2 |
| 133 | svc #0 |
| 134 | 1: b 1b |
| 135 | .balign 4 |
| 136 | 2: .ascii "el0 user: hello\n" |
| 137 | ck_el0_demo_end: |
| 138 | |
| 139 | /* ipc check (user.rs aienos_ipc_code): x20 = handle0, x21 = handle1, |
| 140 | * x22 = role (1 = A, 2 = B phase 0, bit 8 = B phase 1). Exit code is a |
| 141 | * bitmask of failed expectations (0 = all as expected). */ |
| 142 | .balign 4 |
| 143 | .global ck_ipc_prog_start, ck_ipc_prog_end |
| 144 | ck_ipc_prog_start: |
| 145 | mov x23, xzr |
| 146 | cmp x22, #1 |
| 147 | b.eq 10f |
| 148 | tbnz x22, #8, 50f |
| 149 | b 20f |
| 150 | 10: mov x0, x20 /* A: send kind 7 on the channel */ |
| 151 | mov x1, #7 |
| 152 | mov x2, #0x1111 |
| 153 | mov x3, #0x2222 |
| 154 | mov x4, #0x3333 |
| 155 | mov x5, #0xa |
| 156 | mov x6, #0x1000 |
| 157 | mov x7, #2 |
| 158 | mov x8, #3 |
| 159 | svc #0 |
| 160 | cmp x0, #0 |
| 161 | b.eq 15f |
| 162 | orr x23, x23, #32 |
| 163 | 15: mov x0, x21 /* A: delegate READ on the object to B */ |
| 164 | mov x1, #1 |
| 165 | mov x2, #1 |
| 166 | mov x8, #5 |
| 167 | svc #0 |
| 168 | cmp x0, #0 |
| 169 | b.eq 16f |
| 170 | orr x23, x23, #64 |
| 171 | 16: mov x0, x23 |
| 172 | mov x8, #2 |
| 173 | svc #0 |
| 174 | 70: b 70b |
| 175 | 20: mov x0, x20 /* B0: receive */ |
| 176 | mov x8, #4 |
| 177 | svc #0 |
| 178 | cmp x0, #0 |
| 179 | b.eq 21f |
| 180 | orr x23, x23, #1 |
| 181 | 21: cmp x1, #7 |
| 182 | b.eq 22f |
| 183 | orr x23, x23, #1 |
| 184 | 22: movz x5, #0x1111 |
| 185 | cmp x2, x5 |
| 186 | b.eq 23f |
| 187 | orr x23, x23, #1 |
| 188 | 23: movz x5, #0x2222 |
| 189 | cmp x3, x5 |
| 190 | b.eq 24f |
| 191 | orr x23, x23, #1 |
| 192 | 24: mov x0, x21 /* B0: read object word 0 */ |
| 193 | mov x1, #0 |
| 194 | mov x8, #6 |
| 195 | svc #0 |
| 196 | movz x5, #0xee01 |
| 197 | movk x5, #0xc0ff, lsl #16 |
| 198 | cmp x0, x5 |
| 199 | b.eq 25f |
| 200 | orr x23, x23, #2 |
| 201 | 25: mov x0, x21 /* B0: write must be refused (READ only) */ |
| 202 | mov x1, #0 |
| 203 | mov x2, #0xbad |
| 204 | mov x8, #7 |
| 205 | svc #0 |
| 206 | cmp x0, #0 |
| 207 | b.ne 26f |
| 208 | orr x23, x23, #4 |
| 209 | 26: mov x6, #1 /* B0: forged handle must be refused */ |
| 210 | lsl x6, x6, #32 |
| 211 | eor x0, x21, x6 |
| 212 | mov x1, #0 |
| 213 | mov x8, #6 |
| 214 | svc #0 |
| 215 | cmp x0, #0 |
| 216 | b.ne 27f |
| 217 | orr x23, x23, #8 |
| 218 | 27: mov x0, x23 |
| 219 | mov x8, #2 |
| 220 | svc #0 |
| 221 | 71: b 71b |
| 222 | 50: mov x0, x21 /* B1: revoked handle must be refused */ |
| 223 | mov x1, #0 |
| 224 | mov x8, #6 |
| 225 | svc #0 |
| 226 | cmp x0, #0 |
| 227 | b.ne 51f |
| 228 | orr x23, x23, #16 |
| 229 | 51: mov x0, x23 |
| 230 | mov x8, #2 |
| 231 | svc #0 |
| 232 | 72: b 72b |
| 233 | ck_ipc_prog_end: |
| 234 | |
| 235 | .bss |
| 236 | .balign 8 |
| 237 | .global ck_el0_kernel_sp |
| 238 | ck_el0_kernel_sp: |
| 239 | .space 8 |