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 / core / m3.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* m3.c -- M3 isolation checks in the C kernel core (port of the Rust kernel's |
| 18 | * thread.rs, user.rs and ipc.rs demos as driven by aienos-boot handoff.rs). |
| 19 | * Prints, in the Rust order: |
| 20 | * threads: two cooperative EL1 threads on the core/sched.c run queue |
| 21 | * el0: an EL0 task in its own address space: capability-checked write, |
| 22 | * forged handle refused, kernel-address read faults and is contained |
| 23 | * preempt: two spinning EL1 workers switched only by the timer IRQ |
| 24 | * placement: workers placed on cores from the MADT efficiency classes |
| 25 | * (pure computation: neither kernel starts a secondary core) |
| 26 | * ipc: two EL0 principals, typed message, attenuated delegation, |
| 27 | * forged handle refused, revoked handle refused |
| 28 | * Single core, EL1h; runs with IRQs masked except inside the preempt check |
| 29 | * and while EL0 runs. */ |
| 30 | #include "acpi.h" |
| 31 | #include "arch.h" |
| 32 | #include "ck_internal.h" |
| 33 | #include "ipc.h" |
| 34 | #include "pt.h" |
| 35 | #include "sched.h" |
| 36 | |
| 37 | #define DENIED 0xffffffffffffffffull |
| 38 | |
| 39 | /* ---- vector hooks (arch/exception.c) ---- */ |
| 40 | extern struct ck_frame *(*ck_tick_switch)(struct ck_frame *f); |
| 41 | extern int (*ck_lower_sync)(struct ck_frame *f, uint64_t esr); |
| 42 | |
| 43 | /* ---- asm (arch/m3.S) ---- */ |
| 44 | struct ck_ctx { |
| 45 | uint64_t x19_x30[12]; |
| 46 | uint64_t sp; |
| 47 | }; |
| 48 | void ck_ctx_switch(struct ck_ctx *from, const struct ck_ctx *to); |
| 49 | void ck_thread_start(void); |
| 50 | void ck_thread_exit(void); |
| 51 | void ck_el0_enter(uint64_t entry, uint64_t user_sp, const uint64_t regs[31]); |
| 52 | void ck_el0_resume(void); |
| 53 | extern const char ck_el0_demo_start[], ck_el0_demo_end[]; |
| 54 | extern const char ck_ipc_prog_start[], ck_ipc_prog_end[]; |
| 55 | |
| 56 | /* ===================== cooperative threads (thread.rs) ===================== */ |
| 57 | #define TMAX 4 |
| 58 | static struct ck_ctx contexts[TMAX], main_ctx; |
| 59 | static int runnable[TMAX]; |
| 60 | static unsigned current = TMAX; |
| 61 | static struct ck_sched sched; |
| 62 | static uint8_t thread_stack[2][8192] __attribute__((aligned(16))); |
| 63 | static char trace[10]; |
| 64 | static unsigned trace_len; |
| 65 | static uint64_t thread_floor[TMAX]; /* stack base per thread, for the vector stack guard */ |
| 66 | static uint64_t kernel_floor; |
| 67 | |
| 68 | static void yield_now(void) |
| 69 | { |
| 70 | unsigned from = current, target = TMAX; |
| 71 | for (unsigned i = 0; i < TMAX; i++) { |
| 72 | uint32_t id; |
| 73 | if (ck_sched_tick(&sched, 0, &id) == 1 && id < TMAX && runnable[id] && id != from) { |
| 74 | target = id; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | if (target == from) |
| 79 | return; |
| 80 | current = target; |
| 81 | /* keep the vector stack guard on the stack that will run */ |
| 82 | ck_stack_floor = target == TMAX ? kernel_floor : thread_floor[target]; |
| 83 | ck_ctx_switch(from == TMAX ? &main_ctx : &contexts[from], |
| 84 | target == TMAX ? &main_ctx : &contexts[target]); |
| 85 | } |
| 86 | |
| 87 | void ck_thread_exit(void) |
| 88 | { |
| 89 | runnable[current] = 0; |
| 90 | ck_sched_remove(&sched, current); |
| 91 | for (;;) |
| 92 | yield_now(); |
| 93 | } |
| 94 | |
| 95 | static int spawn(uint8_t *stack, size_t len, void (*entry)(uint64_t), uint64_t arg) |
| 96 | { |
| 97 | for (unsigned slot = 0; slot < TMAX; slot++) { |
| 98 | if (runnable[slot] || ck_sched_task(&sched, slot)) |
| 99 | continue; |
| 100 | uint64_t sp = (((uint64_t)(uintptr_t)stack + len) & ~15ull) - 16; |
| 101 | contexts[slot] = (struct ck_ctx){ .sp = sp }; |
| 102 | thread_floor[slot] = (uint64_t)(uintptr_t)stack; |
| 103 | contexts[slot].x19_x30[0] = arg; |
| 104 | contexts[slot].x19_x30[1] = (uint64_t)(uintptr_t)entry; |
| 105 | contexts[slot].x19_x30[11] = (uint64_t)(uintptr_t)ck_thread_start; |
| 106 | if (ck_sched_enqueue(&sched, slot, CK_PRIO_NORMAL) < 0) |
| 107 | return -1; |
| 108 | runnable[slot] = 1; |
| 109 | return (int)slot; |
| 110 | } |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | static void thread_worker(uint64_t tag) |
| 115 | { |
| 116 | for (int i = 0; i < 5; i++) { |
| 117 | if (trace_len < sizeof trace) |
| 118 | trace[trace_len++] = tag == 0 ? 'A' : 'B'; |
| 119 | yield_now(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void check_threads(void) |
| 124 | { |
| 125 | static const uint8_t one_class[1] = { 0 }; |
| 126 | ck_sched_init(&sched, 1, one_class, TMAX, 8); |
| 127 | trace_len = 0; |
| 128 | current = TMAX; |
| 129 | int ok = spawn(thread_stack[0], sizeof thread_stack[0], thread_worker, 0) >= 0 && |
| 130 | spawn(thread_stack[1], sizeof thread_stack[1], thread_worker, 1) >= 0; |
| 131 | while (ok && (runnable[0] || runnable[1] || runnable[2] || runnable[3])) |
| 132 | yield_now(); |
| 133 | char seen[sizeof trace + 1]; |
| 134 | memcpy(seen, trace, trace_len); |
| 135 | seen[trace_len] = 0; |
| 136 | ck_printf("threads: %s interleave=%s\n", |
| 137 | trace_len == 10 && !memcmp(seen, "ABABABABAB", 10) ? "ok" : "unexpected", seen); |
| 138 | } |
| 139 | |
| 140 | /* ======================= timer preemption (thread.rs) ====================== */ |
| 141 | static uint8_t preempt_stack[2][16384] __attribute__((aligned(16))); |
| 142 | static volatile uint64_t counter_a, counter_b; |
| 143 | static volatile int preempt_active, preempt_done; |
| 144 | static uint64_t preempt_switches; |
| 145 | static struct ck_frame *worker_frame[2], *main_frame; |
| 146 | static unsigned preempt_cur; |
| 147 | |
| 148 | static void preempt_worker(uint64_t which) |
| 149 | { |
| 150 | for (;;) { |
| 151 | if (which) |
| 152 | counter_b = counter_b + 1; |
| 153 | else |
| 154 | counter_a = counter_a + 1; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* A worker function never returns; if one did, its x30 lands here. */ |
| 159 | static void preempt_worker_exit(void) |
| 160 | { |
| 161 | ck_panic("preempt: worker returned"); |
| 162 | } |
| 163 | |
| 164 | static struct ck_frame *prepare_frame(uint8_t *stack, size_t len, void (*entry)(uint64_t), |
| 165 | uint64_t arg) |
| 166 | { |
| 167 | uint64_t top = ((uint64_t)(uintptr_t)stack + len) & ~15ull; |
| 168 | struct ck_frame *f = (struct ck_frame *)(uintptr_t)(top - sizeof(struct ck_frame)); |
| 169 | memset(f, 0, sizeof *f); |
| 170 | f->x[0] = arg; |
| 171 | f->x[30] = (uint64_t)(uintptr_t)preempt_worker_exit; |
| 172 | f->elr = (uint64_t)(uintptr_t)entry; |
| 173 | f->spsr = 0x5; /* EL1h, DAIF clear: the next tick preempts it */ |
| 174 | f->vector = 5; |
| 175 | return f; |
| 176 | } |
| 177 | |
| 178 | /* Called by ck_irq_dispatch after the timer tick was re-armed and EOId. |
| 179 | * Returns the frame the vector exit restores (SP moves onto that stack). */ |
| 180 | static struct ck_frame *preempt_tick(struct ck_frame *f) |
| 181 | { |
| 182 | if (!preempt_active) |
| 183 | return f; |
| 184 | if (!main_frame) { |
| 185 | main_frame = f; |
| 186 | preempt_cur = 0; |
| 187 | ck_stack_floor = (uint64_t)(uintptr_t)preempt_stack[0]; |
| 188 | return worker_frame[0]; |
| 189 | } |
| 190 | worker_frame[preempt_cur] = f; |
| 191 | if (preempt_switches >= 4 && counter_a > 0 && counter_b > 0) { |
| 192 | preempt_active = 0; |
| 193 | preempt_done = 1; |
| 194 | struct ck_frame *t = main_frame; |
| 195 | main_frame = 0; |
| 196 | ck_stack_floor = kernel_floor; |
| 197 | return t; |
| 198 | } |
| 199 | preempt_cur ^= 1; |
| 200 | preempt_switches++; |
| 201 | ck_stack_floor = (uint64_t)(uintptr_t)preempt_stack[preempt_cur]; |
| 202 | return worker_frame[preempt_cur]; |
| 203 | } |
| 204 | |
| 205 | static void check_preempt(void) |
| 206 | { |
| 207 | counter_a = counter_b = 0; |
| 208 | preempt_switches = 0; |
| 209 | main_frame = 0; |
| 210 | preempt_done = 0; |
| 211 | worker_frame[0] = prepare_frame(preempt_stack[0], sizeof preempt_stack[0], preempt_worker, 0); |
| 212 | worker_frame[1] = prepare_frame(preempt_stack[1], sizeof preempt_stack[1], preempt_worker, 1); |
| 213 | uint64_t hz = ck_rd(cntfrq_el0); |
| 214 | int timed_out = 0; |
| 215 | if (hz) { |
| 216 | preempt_active = 1; |
| 217 | ck_tick_switch = preempt_tick; |
| 218 | uint64_t start = ck_rd(cntpct_el0); |
| 219 | ck_wr(cntp_cval_el0, start + hz / 100); |
| 220 | ck_wr(cntp_ctl_el0, 1); |
| 221 | ck_isb(); |
| 222 | ck_irq_on(); |
| 223 | ck_isb(); |
| 224 | /* Main resumes only when the dispatcher hands its frame back. The |
| 225 | * timeout covers a timer that never fires (main never left). */ |
| 226 | while (!preempt_done) { |
| 227 | if (ck_rd(cntpct_el0) - start > hz * 2) { |
| 228 | timed_out = 1; |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | ck_irq_off(); |
| 233 | ck_isb(); |
| 234 | preempt_active = 0; |
| 235 | ck_tick_switch = 0; |
| 236 | ck_wr(cntp_ctl_el0, 0); |
| 237 | ck_isb(); |
| 238 | ck_stack_floor = kernel_floor; |
| 239 | } |
| 240 | uint64_t a = counter_a, b = counter_b; |
| 241 | int ok = !timed_out && a > 0 && b > 0 && preempt_switches >= 4; |
| 242 | ck_printf("preempt: %s a=%llu b=%llu switches=%llu\n", ok ? "ok" : "failed", |
| 243 | (unsigned long long)a, (unsigned long long)b, (unsigned long long)preempt_switches); |
| 244 | } |
| 245 | |
| 246 | /* ============================ placement (MADT) ============================= */ |
| 247 | static void check_placement(void) |
| 248 | { |
| 249 | const void *madt = ck_acpi_find("APIC"); |
| 250 | struct ck_cpu_topology topo; |
| 251 | if (!madt || ck_madt_cpu_topology(madt, &topo)) { |
| 252 | ck_puts("placement: unavailable\n"); |
| 253 | return; |
| 254 | } |
| 255 | char line[128]; |
| 256 | size_t n = 0; |
| 257 | static const char *const names[2] = { "worker0", "worker1" }; |
| 258 | n += (size_t)ck_snprintf(line, sizeof line, "placement:"); |
| 259 | for (uint32_t i = 0; i < 2; i++) { |
| 260 | uint8_t cls; |
| 261 | uint32_t core; |
| 262 | if (ck_place_task(&topo, i, &cls, &core) == 0) |
| 263 | n += (size_t)ck_snprintf(line + n, sizeof line - n, " %s=class%u/core%u", names[i], |
| 264 | (unsigned)cls, (unsigned)core); |
| 265 | else |
| 266 | n += (size_t)ck_snprintf(line + n, sizeof line - n, " %s=unplaced", names[i]); |
| 267 | } |
| 268 | ck_printf("%s\n", line); |
| 269 | } |
| 270 | |
| 271 | /* ===================== EL0 address spaces (user.rs) ======================== */ |
| 272 | struct el0_window { |
| 273 | uint64_t l0[512], l1[512], l2[512], l3[512]; |
| 274 | uint8_t code[4096]; |
| 275 | uint8_t stack[4096]; |
| 276 | } __attribute__((aligned(4096))); |
| 277 | static struct el0_window demo_window, ipc_window[2]; |
| 278 | |
| 279 | #define PTE_TABLE 3ull |
| 280 | #define PTE_PAGE 3ull |
| 281 | #define PTE_AP_EL0_RW (1ull << 6) |
| 282 | #define PTE_AP_EL0_RO (3ull << 6) |
| 283 | #define PTE_NG (1ull << 11) |
| 284 | |
| 285 | static void clean_range(const void *p, size_t len, int icache) |
| 286 | { |
| 287 | uint64_t ctr = ck_rd(ctr_el0); |
| 288 | uint64_t dline = 4ull << ((ctr >> 16) & 0xf), iline = 4ull << (ctr & 0xf); |
| 289 | uint64_t a = (uint64_t)(uintptr_t)p, end = a + len; |
| 290 | for (uint64_t x = a & ~(dline - 1); x < end; x += dline) |
| 291 | __asm__ volatile("dc civac, %0" ::"r"(x) : "memory"); |
| 292 | ck_dsb_ish(); |
| 293 | if (icache) { |
| 294 | for (uint64_t x = a & ~(iline - 1); x < end; x += iline) |
| 295 | __asm__ volatile("ic ivau, %0" ::"r"(x) : "memory"); |
| 296 | ck_dsb_ish(); |
| 297 | } |
| 298 | ck_isb(); |
| 299 | } |
| 300 | |
| 301 | /* Kernel L0 copied (kernel mappings stay EL1-only: AP[1] = 0), one free |
| 302 | * L0 slot in the lower half chained to: page 0 = code (EL0 read-only, |
| 303 | * executable at EL0, never at EL1), page 1 = stack (EL0 read-write, never |
| 304 | * executable). nG: the user pages are not global. Returns the user base, 0 if |
| 305 | * no L0 slot is free or the image does not fit. */ |
| 306 | static uint64_t build_window(struct el0_window *w, const char *img, size_t len, uint64_t kroot) |
| 307 | { |
| 308 | if (len > sizeof w->code) |
| 309 | return 0; |
| 310 | memcpy(w->l0, (const void *)(uintptr_t)kroot, sizeof w->l0); |
| 311 | unsigned idx = 0; |
| 312 | for (unsigned i = 1; i < 256 && !idx; i++) |
| 313 | if (!w->l0[i]) |
| 314 | idx = i; |
| 315 | if (!idx) |
| 316 | return 0; |
| 317 | memset(w->l1, 0, sizeof w->l1); |
| 318 | memset(w->l2, 0, sizeof w->l2); |
| 319 | memset(w->l3, 0, sizeof w->l3); |
| 320 | w->l0[idx] = (uint64_t)(uintptr_t)w->l1 | PTE_TABLE; |
| 321 | w->l1[0] = (uint64_t)(uintptr_t)w->l2 | PTE_TABLE; |
| 322 | w->l2[0] = (uint64_t)(uintptr_t)w->l3 | PTE_TABLE; |
| 323 | uint64_t common = CK_PTE_ATTRIDX(CK_MAIR_IDX_NORMAL) | CK_PTE_SH_INNER | CK_PTE_AF | PTE_NG | |
| 324 | CK_PTE_PXN; |
| 325 | w->l3[0] = (uint64_t)(uintptr_t)w->code | PTE_PAGE | PTE_AP_EL0_RO | common; |
| 326 | w->l3[1] = (uint64_t)(uintptr_t)w->stack | PTE_PAGE | PTE_AP_EL0_RW | common | CK_PTE_UXN; |
| 327 | memset(w->code, 0, sizeof w->code); |
| 328 | memcpy(w->code, img, len); |
| 329 | memset(w->stack, 0, sizeof w->stack); |
| 330 | clean_range(w->l0, 4 * 4096, 0); |
| 331 | clean_range(w->code, sizeof w->code, 1); |
| 332 | clean_range(w->stack, sizeof w->stack, 0); |
| 333 | return (uint64_t)idx << 39; |
| 334 | } |
| 335 | |
| 336 | static void swap_ttbr0(uint64_t ttbr0) |
| 337 | { |
| 338 | ck_dsb_ish(); |
| 339 | ck_wr(ttbr0_el1, ttbr0); |
| 340 | ck_isb(); |
| 341 | __asm__ volatile("tlbi vmalle1" ::: "memory"); |
| 342 | ck_dsb_ish(); |
| 343 | ck_isb(); |
| 344 | } |
| 345 | |
| 346 | enum { EL0_NONE, EL0_DEMO, EL0_IPC }; |
| 347 | static int el0_mode; |
| 348 | static uint64_t el0_exit; |
| 349 | static uint64_t el0_code_base; /* user VA of the running task's code page */ |
| 350 | static const uint8_t *el0_code; /* kernel view of that page */ |
| 351 | |
| 352 | static void el0_to_kernel(struct ck_frame *f, uint64_t code) |
| 353 | { |
| 354 | el0_exit = code; |
| 355 | f->elr = (uint64_t)(uintptr_t)ck_el0_resume; |
| 356 | f->spsr = 0x3c5; /* EL1h, DAIF masked (the Rust kernel resumes with 0x5) */ |
| 357 | } |
| 358 | |
| 359 | /* Runs one EL0 task to its exit on the given address space. */ |
| 360 | static uint64_t run_el0(uint64_t root, uint64_t base, const struct el0_window *w, int mode, |
| 361 | const uint64_t regs[31]) |
| 362 | { |
| 363 | uint64_t kttbr = ck_rd(ttbr0_el1), cpacr = ck_rd(cpacr_el1); |
| 364 | el0_mode = mode; |
| 365 | el0_exit = DENIED; |
| 366 | el0_code_base = base; |
| 367 | el0_code = w->code; |
| 368 | ck_wr(cpacr_el1, (cpacr & ~(3ull << 20)) | (1ull << 20)); /* FP/SIMD traps at EL0 */ |
| 369 | swap_ttbr0(root); |
| 370 | ck_el0_enter(base, base + 0x2000, regs); |
| 371 | swap_ttbr0(kttbr); |
| 372 | ck_wr(cpacr_el1, cpacr); |
| 373 | ck_isb(); |
| 374 | el0_mode = EL0_NONE; |
| 375 | return el0_exit; |
| 376 | } |
| 377 | |
| 378 | /* ---- el0 check (user.rs run_demo) ---- */ |
| 379 | #define CONSOLE_RESOURCE 1u |
| 380 | static struct ck_cap_table user_caps; |
| 381 | static uint64_t authorized_raw; |
| 382 | static int write_granted, forged_denied, fault_contained; |
| 383 | static uint64_t el0_probe_addr; /* the one kernel address the demo task is expected to fault on */ |
| 384 | #define EL0_FAULT_KILLED 0xfa17ull /* exit code of a task killed by an unexpected fault */ |
| 385 | |
| 386 | static int user_bytes(uint64_t addr, uint64_t len, const uint8_t **out) |
| 387 | { |
| 388 | if (addr < el0_code_base || len > 4096 || addr - el0_code_base > 4096 - len) |
| 389 | return -1; |
| 390 | *out = el0_code + (addr - el0_code_base); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static void demo_write(struct ck_frame *f) |
| 395 | { |
| 396 | uint64_t raw = f->x[0]; |
| 397 | struct ck_handle h; |
| 398 | if (ck_handle_from_raw(raw, &h)) { |
| 399 | forged_denied = 1; |
| 400 | f->x[0] = DENIED; |
| 401 | return; |
| 402 | } |
| 403 | uint32_t res = 0; |
| 404 | int allowed = ck_cap_lookup(&user_caps, h, CK_R_WRITE, &res) == 0 && res == CONSOLE_RESOURCE; |
| 405 | const uint8_t *bytes; |
| 406 | if (allowed && user_bytes(f->x[1], f->x[2], &bytes) == 0) { |
| 407 | char chunk[65]; |
| 408 | for (uint64_t at = 0; at < f->x[2]; at += 64) { |
| 409 | uint64_t n = f->x[2] - at < 64 ? f->x[2] - at : 64; |
| 410 | memcpy(chunk, bytes + at, n); |
| 411 | chunk[n] = 0; |
| 412 | ck_puts(chunk); |
| 413 | } |
| 414 | write_granted = 1; |
| 415 | f->x[0] = 0; |
| 416 | return; |
| 417 | } |
| 418 | if (raw != authorized_raw) |
| 419 | forged_denied = 1; |
| 420 | f->x[0] = DENIED; |
| 421 | } |
| 422 | |
| 423 | static void check_el0(uint64_t kroot) |
| 424 | { |
| 425 | write_granted = forged_denied = fault_contained = 0; |
| 426 | ck_cap_init(&user_caps, 0x454c3001u, 4); |
| 427 | struct ck_handle h; |
| 428 | uint64_t base = build_window(&demo_window, ck_el0_demo_start, |
| 429 | (size_t)(ck_el0_demo_end - ck_el0_demo_start), kroot); |
| 430 | if (ck_cap_insert(&user_caps, CONSOLE_RESOURCE, CK_R_WRITE, &h) || !base) { |
| 431 | ck_puts("el0: failed setup\n"); |
| 432 | return; |
| 433 | } |
| 434 | authorized_raw = ck_handle_raw(h); |
| 435 | uint64_t regs[31] = { 0 }; |
| 436 | regs[0] = authorized_raw; |
| 437 | regs[3] = (uint64_t)(uintptr_t)ck_el0_enter; /* kernel text: EL1-only */ |
| 438 | el0_probe_addr = regs[3]; |
| 439 | uint64_t code = run_el0((uint64_t)(uintptr_t)demo_window.l0, base, &demo_window, EL0_DEMO, regs); |
| 440 | int ok = write_granted && forged_denied && fault_contained && code == 0; |
| 441 | ck_printf("el0: %s write=%s forged=%s fault=%s exit=%llu\n", ok ? "ok" : "failed", |
| 442 | write_granted ? "granted" : "denied", forged_denied ? "denied" : "accepted", |
| 443 | fault_contained ? "contained" : "uncontained", (unsigned long long)code); |
| 444 | } |
| 445 | |
| 446 | /* ---- ipc check (user.rs run_ipc_demo) ---- */ |
| 447 | static struct ck_ipc ipc; |
| 448 | static unsigned ipc_principal, ipc_phase; |
| 449 | static int msg_delivered, cap_delegated, rights_attenuated, ipc_forged, ipc_revoked; |
| 450 | static uint64_t sent_kind, sent_p0, sent_p1, sent_obj, child_raw; |
| 451 | |
| 452 | static void ipc_handle_refused(void) |
| 453 | { |
| 454 | if (ipc_phase) |
| 455 | ipc_revoked = 1; |
| 456 | else |
| 457 | ipc_forged = 1; |
| 458 | } |
| 459 | |
| 460 | static void dispatch_ipc(struct ck_frame *f) |
| 461 | { |
| 462 | struct ck_handle h; |
| 463 | uint64_t nr = f->x[8]; |
| 464 | if (nr == 2) { /* exit */ |
| 465 | el0_to_kernel(f, f->x[0]); |
| 466 | return; |
| 467 | } |
| 468 | if (nr < 3 || nr > 7) { |
| 469 | f->x[0] = DENIED; |
| 470 | return; |
| 471 | } |
| 472 | if (ck_handle_from_raw(f->x[0], &h)) { |
| 473 | if (nr == 6) |
| 474 | ipc_handle_refused(); |
| 475 | f->x[0] = DENIED; |
| 476 | return; |
| 477 | } |
| 478 | switch (nr) { |
| 479 | case 3: { /* channel send */ |
| 480 | struct ck_msg m = { 0 }; |
| 481 | m.kind = (uint32_t)f->x[1]; |
| 482 | m.payload[0] = f->x[2]; |
| 483 | m.payload[1] = f->x[3]; |
| 484 | m.payload[2] = f->x[4]; |
| 485 | m.region_base = f->x[5]; |
| 486 | m.region_pages = (uint32_t)f->x[6]; |
| 487 | m.object = f->x[7]; |
| 488 | if (ck_ipc_send(&ipc, ipc_principal, h, &m) == 0) { |
| 489 | sent_kind = f->x[1]; |
| 490 | sent_p0 = f->x[2]; |
| 491 | sent_p1 = f->x[3]; |
| 492 | sent_obj = f->x[7]; |
| 493 | f->x[0] = 0; |
| 494 | } else { |
| 495 | f->x[0] = DENIED; |
| 496 | } |
| 497 | break; |
| 498 | } |
| 499 | case 4: { /* channel receive */ |
| 500 | struct ck_msg m; |
| 501 | if (ck_ipc_recv(&ipc, ipc_principal, h, &m)) { |
| 502 | f->x[0] = DENIED; |
| 503 | break; |
| 504 | } |
| 505 | if (m.kind == sent_kind && m.payload[0] == sent_p0 && m.payload[1] == sent_p1 && |
| 506 | m.object == sent_obj) |
| 507 | msg_delivered = 1; |
| 508 | f->x[0] = 0; |
| 509 | f->x[1] = m.kind; |
| 510 | f->x[2] = m.payload[0]; |
| 511 | f->x[3] = m.payload[1]; |
| 512 | f->x[4] = m.payload[2]; |
| 513 | f->x[5] = m.object; |
| 514 | break; |
| 515 | } |
| 516 | case 5: { /* delegate: x1 = target principal index, x2 = wire rights */ |
| 517 | struct ck_handle child; |
| 518 | if ((f->x[2] & ~(uint64_t)CK_R_ALL) || f->x[1] > 1 || |
| 519 | ck_ipc_delegate(&ipc, ipc_principal, h, (unsigned)f->x[1], (unsigned)f->x[2], &child)) { |
| 520 | f->x[0] = DENIED; |
| 521 | break; |
| 522 | } |
| 523 | cap_delegated = 1; |
| 524 | child_raw = ck_handle_raw(child); |
| 525 | f->x[0] = 0; |
| 526 | break; |
| 527 | } |
| 528 | case 6: { /* object read */ |
| 529 | uint64_t v; |
| 530 | int r = ck_ipc_object_read(&ipc, ipc_principal, h, f->x[1], &v); |
| 531 | if (r == 0) { |
| 532 | f->x[0] = v; |
| 533 | } else { |
| 534 | if (r == CK_IPC_INVALID_HANDLE) |
| 535 | ipc_handle_refused(); |
| 536 | f->x[0] = DENIED; |
| 537 | } |
| 538 | break; |
| 539 | } |
| 540 | case 7: { /* object write */ |
| 541 | int r = ck_ipc_object_write(&ipc, ipc_principal, h, f->x[1], f->x[2]); |
| 542 | if (r == CK_IPC_MISSING_RIGHTS) |
| 543 | rights_attenuated = 1; |
| 544 | f->x[0] = r ? DENIED : 0; |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | static int lower_sync(struct ck_frame *f, uint64_t esr) |
| 551 | { |
| 552 | if (el0_mode == EL0_NONE || (f->spsr & 0xf) != 0) |
| 553 | return -1; /* not from an M3 EL0 task: fatal path */ |
| 554 | uint64_t ec = (esr >> 26) & 0x3f; |
| 555 | if (ec == 0x15 && (esr & 0xffff) == 0) { |
| 556 | if (el0_mode == EL0_IPC) |
| 557 | dispatch_ipc(f); |
| 558 | else if (f->x[8] == 1) |
| 559 | demo_write(f); |
| 560 | else if (f->x[8] == 2) |
| 561 | el0_to_kernel(f, f->x[0]); |
| 562 | else |
| 563 | f->x[0] = DENIED; |
| 564 | } else if (ec == 0x24) { /* data abort from EL0 */ |
| 565 | uint64_t far; |
| 566 | __asm__ volatile("mrs %0, far_el1" : "=r"(far)); |
| 567 | if (el0_mode == EL0_DEMO && !fault_contained && far == el0_probe_addr) { |
| 568 | /* the demo's one expected probe of kernel memory: contained, |
| 569 | * the load is skipped and the task continues to its exit */ |
| 570 | fault_contained = 1; |
| 571 | f->elr += 4; |
| 572 | } else { |
| 573 | /* any other EL0 fault kills the task; the kernel keeps running */ |
| 574 | el0_to_kernel(f, EL0_FAULT_KILLED); |
| 575 | } |
| 576 | } else { |
| 577 | el0_to_kernel(f, DENIED); |
| 578 | } |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static void check_ipc(uint64_t kroot) |
| 583 | { |
| 584 | msg_delivered = cap_delegated = rights_attenuated = ipc_forged = ipc_revoked = 0; |
| 585 | child_raw = 0; |
| 586 | struct ck_handle a_send, a_obj, b_recv; |
| 587 | ck_ipc_reset(&ipc, &a_send, &a_obj, &b_recv); |
| 588 | size_t len = (size_t)(ck_ipc_prog_end - ck_ipc_prog_start); |
| 589 | uint64_t base_a = build_window(&ipc_window[0], ck_ipc_prog_start, len, kroot); |
| 590 | uint64_t base_b = build_window(&ipc_window[1], ck_ipc_prog_start, len, kroot); |
| 591 | if (!base_a || !base_b) { |
| 592 | ck_puts("ipc: failed setup\n"); |
| 593 | return; |
| 594 | } |
| 595 | uint64_t root_a = (uint64_t)(uintptr_t)ipc_window[0].l0; |
| 596 | uint64_t root_b = (uint64_t)(uintptr_t)ipc_window[1].l0; |
| 597 | uint64_t regs[31] = { 0 }; |
| 598 | |
| 599 | /* principal A: send the typed message, delegate an attenuated copy */ |
| 600 | ipc_principal = 0; |
| 601 | ipc_phase = 0; |
| 602 | regs[0] = regs[22] = 1; |
| 603 | regs[20] = ck_handle_raw(a_send); |
| 604 | regs[21] = ck_handle_raw(a_obj); |
| 605 | uint64_t exit_a = run_el0(root_a, base_a, &ipc_window[0], EL0_IPC, regs); |
| 606 | |
| 607 | /* principal B, phase 0: receive, read, attempt write, forge */ |
| 608 | ipc_principal = 1; |
| 609 | regs[0] = regs[22] = 2; |
| 610 | regs[20] = ck_handle_raw(b_recv); |
| 611 | regs[21] = child_raw; |
| 612 | uint64_t exit_b0 = run_el0(root_b, base_b, &ipc_window[1], EL0_IPC, regs); |
| 613 | |
| 614 | /* the kernel revokes A's ancestor; B's derived handle must die */ |
| 615 | int revoke = ck_ipc_revoke(&ipc, 0, a_obj); |
| 616 | |
| 617 | /* principal B, phase 1: the derived handle no longer resolves */ |
| 618 | ipc_phase = 1; |
| 619 | regs[0] = regs[22] = 0x102; |
| 620 | uint64_t exit_b1 = run_el0(root_b, base_b, &ipc_window[1], EL0_IPC, regs); |
| 621 | |
| 622 | int ok = msg_delivered && cap_delegated && rights_attenuated && ipc_forged && ipc_revoked && |
| 623 | revoke == 0 && exit_a == 0 && exit_b0 == 0 && exit_b1 == 0; |
| 624 | ck_printf("ipc: %s message=%s cap=%s rights=%s forged=%s revoked=%s\n", ok ? "ok" : "failed", |
| 625 | msg_delivered ? "delivered" : "missed", cap_delegated ? "delegated" : "failed", |
| 626 | rights_attenuated ? "attenuated" : "escalated", ipc_forged ? "denied" : "accepted", |
| 627 | ipc_revoked ? "denied" : "accepted"); |
| 628 | ck_printf("ipc_detail: exit_a=0x%llx exit_b0=0x%llx exit_b1=0x%llx child=0x%llx revoke=%d\n", |
| 629 | (unsigned long long)exit_a, (unsigned long long)exit_b0, |
| 630 | (unsigned long long)exit_b1, (unsigned long long)child_raw, revoke); |
| 631 | } |
| 632 | |
| 633 | void ck_m3_run(void) |
| 634 | { |
| 635 | kernel_floor = ck_stack_floor; |
| 636 | uint64_t kroot = ck_mm_report()->root; |
| 637 | ck_lower_sync = lower_sync; |
| 638 | ck_set_stage("m3_threads"); |
| 639 | check_threads(); |
| 640 | ck_set_stage("m3_el0"); |
| 641 | check_el0(kroot); |
| 642 | ck_set_stage("m3_preempt"); |
| 643 | check_preempt(); |
| 644 | ck_set_stage("m3_placement"); |
| 645 | check_placement(); |
| 646 | ck_set_stage("m3_ipc"); |
| 647 | check_ipc(kroot); |
| 648 | ck_lower_sync = 0; |
| 649 | } |