aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
| 1 | # OSC unit task launch (C3-3a) |
| 2 | |
| 3 | Label for everything below: **QEMU (aarch64 virt), TEST signer, not physical.** Nothing here has run on a |
| 4 | Spark or any real machine. Contract: `OSC_UNIT_ARTIFACT.md` section 9 (aien-protocols PR #17). Gate: |
| 5 | `scripts/qemu_ck_osc_launch_test.sh` (`OSC_LAUNCH` row in `scripts/ck_gates.sh`, notes in |
| 6 | `native/kernel/GATES.md`). |
| 7 | |
| 8 | ## What runs |
| 9 | |
| 10 | An admitted unit (`struct osc_accept` from `osc_unit_admit`) is run as one function call in an EL0 task with |
| 11 | its own address space, by `ck_osc_launch` (`core/osc_task.c`): verify the unit's realization receipt (aienos#383, |
| 12 | `docs/osc-receipts.md`; no valid receipt, no launch), look up the function by exact name, check the |
| 13 | arguments, copy and re-hash the code, map, enter EL0, wait, classify, tear down. One call at a time, one slot. |
| 14 | |
| 15 | ## The call signature (what an entry gets) |
| 16 | |
| 17 | - **x0..x5:** the arguments in `reg_kind` order. A slice takes two registers: pointer, then length (`bytes` |
| 18 | length in bytes, `cells` length in 8-byte cells). Scalars are checked for their kind first (bool 0/1, |
| 19 | narrow ints in range, signed ints canonically sign-extended). |
| 20 | - **x7:** `OscRt *`, a unit-space address (base + 0x1000). It points at the 13-pointer vtable of |
| 21 | `runtime_abi_version` 1 (offsets 0..96). The page is **read-only and never executable** to the unit; the |
| 22 | pointers lead to read-execute stubs (base + 0x2000), each `svc #i`. Only the `trap` stub (offset 16) and the |
| 23 | return stub are serviced. |
| 24 | - **x30:** the return stub. The entry function's `ret` reaches it and the kernel reads **x0** as the result. |
| 25 | Every other register is zero. **sp** is the top of a stack of the declared `max_stack_bytes`. |
| 26 | - **Slices:** `bytes` pointers must lie in the input window (read-only to the unit, at most 4096 bytes copied |
| 27 | from the caller, address from `ck_osc_va_in()`). `cells` pointers must be 8-byte aligned and lie in the |
| 28 | caller's workspace (read-write, caller-sized 0 to 32 pages, `ck_osc_va_ws()`). A zero-length slice may carry a null pointer and |
| 29 | is never touched. A pointer anywhere else is refused with 41 before the first instruction. |
| 30 | - **Return value:** one u64 in x0 (`RETURNED.value`); a void function (`ret_kind` 0) reports 0, as the spec says (not exercised by the gate: no |
| 31 | void function in the test units). Narrow return kinds are not re-canonicalized. |
| 32 | |
| 33 | ## The ticks budget |
| 34 | |
| 35 | The unit is **timer ticks**: one tick is one EL1 physical-timer interrupt, `CNTFRQ/100` counter counts = |
| 36 | **10 ms** (the C kernel's scheduler tick, `arch/timer.c`). The budget is the unit's declared `cpu_ticks`, |
| 37 | lowered (never raised) by `max_ticks` in the request. The timer is armed only for the duration of the task; |
| 38 | the Nth tick that arrives while EL0 is running ends the task. A task shorter than one tick is never charged |
| 39 | (`ticks=0`); the limit is accurate to one tick. |
| 40 | |
| 41 | `OUTCOME_UNKNOWN` reaches the caller as `struct osc_result`: `cls == OSC_RES_UNKNOWN (4)` and |
| 42 | `unknown_reason`: 1 FAULT, 2 TICK_OVERRUN, 3 LAUNCH_LOST, 4 TRAP_CODE_UNKNOWN, 255 OTHER (the spec's values). |
| 43 | The other classes: `RETURNED (1)` with `value`, `TRAPPED (2)` with `trap_code` 1..14, `REFUSED (3)` with |
| 44 | `refused_code` (40, 41, 30, 16 or 14). The reason is diagnostic and never changes the class. |
| 45 | |
| 46 | ## Re-entry: yes, with a caller-owned workspace |
| 47 | |
| 48 | A unit can be called again and see state left in a **caller-owned `cells` workspace**: pass the same |
| 49 | `struct ck_osc_ws` (`{ mem, pages }`, page aligned, `pages` chosen by the caller, 0 to 32) in `ck_osc_launch_req.ws`. The kernel maps the caller's pages read-write (never executable) |
| 50 | and does not clear them at teardown; the unit's own stack and registers are not kept, nothing else persists. |
| 51 | The gate calls `counter(ws, 1)` three times on one workspace (1, 2, 3), once on a fresh one (1 again), and twice on a |
| 52 | 23-page workspace (the OSH resumable size, 91,648 bytes) touching its very last cell: 1, then 2, state preserved. |
| 53 | Section 9 allows this: borrows last for one call and the runtime keeps no reference. An OSH step that returns |
| 54 | NEED_MORE_INPUT and is called again therefore works if all its state lives in that workspace. Not provided: |
| 55 | `alloc`, arenas and pools (the runtime services behind vtable offsets 0 and 24..96), so a step that allocates |
| 56 | through `OscRt` ends `OUTCOME_UNKNOWN(OTHER)`. |
| 57 | |
| 58 | ## What is enforced |
| 59 | |
| 60 | - **W^X:** code and runtime stubs are read-execute and never writable; data, stack, input window, OscRt table |
| 61 | and workspace are never executable (UXN) and PXN is set on every user page. Kernel mappings are EL1-only. |
| 62 | - **OscRt and kernel memory are not writable by the unit:** a write to the table, to code or to any kernel |
| 63 | address is a contained fault. The stack has an unmapped page below it: overflow is a fault. |
| 64 | - **Code integrity:** the code is copied into private pages and hashed again against `code_sha256` before it |
| 65 | is mapped (spec 8.2); a mismatch is refused 16. |
| 66 | - **No system calls from unit context (9.1.1 item 1):** an SVC anywhere but a runtime stub (with the stub's own |
| 67 | immediate) ends the task as `OUTCOME_UNKNOWN(FAULT)`. Admission already refuses every SVC word (8.4). |
| 68 | - **Arguments before the first instruction:** 9.2 exactly (verified against the spec's C reference) plus |
| 69 | ownership (9.1.1 item 4). Unknown name: 40. Bad shape or range: 41. |
| 70 | - **Results:** RETURNED only through the return stub; TRAPPED only through the trap stub with a code in |
| 71 | 1..14; a `brk` reached directly, any CPU fault, a trap code outside 1..14 and a budget overrun are |
| 72 | `OUTCOME_UNKNOWN`, never RETURNED or TRAPPED. |
| 73 | - **Timers:** EL0 access to the timers, counters and event stream is switched off for the task (CNTKCTL_EL1 bits 0-3, 8, 9 cleared, restored after), whatever firmware left there. |
| 74 | - **Teardown:** tables, runtime pages, input, code and stack are zeroed and unmapped; `page_tables_zeroed=1` (every page-table entry and the L0 slot are zero; this says nothing about frame contents, which are cleared by memset) and the |
| 75 | slot is free for the next launch. Caches are cleaned and the TLB invalidated. |
| 76 | |
| 77 | ## Decisions (simplest honest option, with reasons) |
| 78 | |
| 79 | 1. **A bare `brk` is a fault, not TRAPPED.** The brief said "TRAPPED with the BRK immediate for BRK 1..14". |
| 80 | Spec 8.4 says a unit that reaches a `brk` has faulted and the platform reports `OUTCOME_UNKNOWN(FAULT)`, |
| 81 | "never TRAPPED"; 9.3 says a launcher never maps a fault to TRAPPED. The spec wins. TRAPPED comes from the |
| 82 | real compiler sequence: the unit calls the runtime trap service with the code (`ldr x16,[rt,#16]; blr x16`, |
| 83 | code in x1), served by the `trap` stub. The gate shows TRAPPED 3 and 14 that way, and `brk` reached |
| 84 | directly as FAULT. |
| 85 | 2. **The ticks budget uses the existing 10 ms timer interrupt**, armed only while a task runs, with no |
| 86 | preemption between tasks. This keeps the C kernel's scheduler untouched. Cost: 10 ms granularity. |
| 87 | 3. **A caller cap on the budget** (`max_ticks`), because the declared maximum of a signer is up to 1e9 ticks |
| 88 | (a loop would run 115 days). The cap only lowers. |
| 89 | 4. **Launcher hard maxima are stricter than the container's.** Spec 8.2 step 16: a signer-declared budget |
| 90 | above a hard maximum is refused `LIMIT_EXCEEDED` (14) before any reservation, nothing runs. The only declared |
| 91 | budget the launcher bounds is `max_stack_bytes`, at 64 KiB: a stack of 65552 (65536 + 16, the smallest legal value above the maximum: the container needs a multiple of 16) gives 14 (gate line, unit |
| 92 | `l02_stack_over`), and so does `a06_valid_limits_at_max` (1 MiB). `cpu_ticks` is never refused here; the caller |
| 93 | cap `max_ticks` only lowers it (decision 3). `pool_slots` is not bounded: no pool service exists, a call to one |
| 94 | ends `OUTCOME_UNKNOWN(OTHER)` (decision 6). Real reservation failures keep 30 (`RESOURCE_UNAVAILABLE`): |
| 95 | code larger than 64 KiB, input over 4 KiB, task slot busy, no free address-space slot. The container has no |
| 96 | workspace field (spec section 7), so there is no declared workspace to refuse. |
| 97 | `cpu_ticks` and `pool_slots` have no launcher hard maximum, so they never get 14 from the launcher: the |
| 98 | container already bounds them at admission (cpu_ticks up to 1e9, pool_slots up to 64), a lower tick limit is |
| 99 | the caller's `max_ticks` (decision 3), and pool_slots reserves nothing because no pool service exists. |
| 100 | 4a. **The workspace is caller-sized, capped at 32 pages (128 KiB, `OSC_WS_MAX_PAGES`).** The only workspace bound |
| 101 | is the caller's. OSH needs 23 pages. 0 pages means no workspace. The cap bounds the user pages, page-table |
| 102 | slots and kernel bss; it is the kernel's choice, never read from the container. A request over the cap, with |
| 103 | a null or non-page-aligned `mem`, or overlapping the launcher's own state or the input buffer, is refused |
| 104 | before anything is mapped with 30 (`RESOURCE_UNAVAILABLE`), the code spec 8.3 gives to a failed reservation |
| 105 | of pages. The workspace is mapped read-write into EL0, so it MUST be memory the caller owns. The kernel has no |
| 106 | region it limits workspaces to; beyond those checks it trusts the caller (also stated in `osc_task.h`). The |
| 107 | mapping is never executable, is not cleared at teardown, and `cells` pointer checks use the actual size. |
| 108 | 5. **Ownership failures use code 41.** The spec gives no separate code for 9.1.1 item 4. |
| 109 | 6. **Unprovided runtime services end the task as `OUTCOME_UNKNOWN(OTHER)`** instead of being faked. |
| 110 | 7. **Hand-assembled test unit.** The extra gate units (trap, spin, faults, counter) are written in assembly |
| 111 | (`tests/fixtures/osc_unit/launch/l01.S`) and signed with the spec's own generator helpers; `oscc` emits |
| 112 | only digests, and OSC has no way to loop forever or fault. The IR section is `min.ir`, so the IR does not |
| 113 | describe that code (attested, never checked: spec section 10). |
| 114 | |
| 115 | ## Not done (later cuts) |
| 116 | |
| 117 | - **Capability authority:** the capability table delivered to the task is empty. CAPS requests are admitted but |
| 118 | not granted or enforced; admission still treats every pinned generation as stale. |
| 119 | - **Generation** (`required_generation`, the 32/64-bit bridge, conflict C1). |
| 120 | - **IPC and console input**, and any runtime service behind `OscRt` other than trap: alloc, arenas, pools. |
| 121 | - **Concurrency:** one task slot, one call at a time, no preemption between tasks, no SMP use. |
| 122 | - **Return canonicalization:** x0 is reported as-is for every kind except void (reported as 0). |
| 123 | - Kernel entry for launches outside the gate: the self-test driver (`CK_OSC_LAUNCH_TEST`, qualification |
| 124 | builds only) is the only caller. No Store-driven or shell-driven launch exists yet. |
| 125 | - **Nothing is physical.** No Spark, no hardware, no release signer; the only key is the spec's throwaway |
| 126 | TEST key. |