Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/core/osc_task.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / core / osc_task.c` 383 lines ยท 16217 bytes Raw Blame History 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 /* osc_task.c -- launch an admitted OSC unit as an EL0 task. See osc_task.h and docs/osc-launch.md. 18 * 19 * Address space of the task (all user VAs are base + offset, base = a free L0 slot of the kernel root, so 20 * kernel mappings stay EL1-only; every user page is non-global): 21 * 0x001000 OscRt table EL0 read-only, never executable (13 pointers into the runtime stubs) 22 * 0x002000 runtime stubs EL0 read, execute, never writable (`svc #i`; the only SVCs a unit may reach) 23 * 0x010000 unit code EL0 read, execute, never writable 24 * 0x200000 input window EL0 read-only, never executable (the `bytes` arguments) 25 * 0x210000 workspace EL0 read-write, never executable (the `cells` arguments, caller-owned, 0..32 pages) 26 * ..0x400000 stack EL0 read-write, never executable (top down; the page below it is unmapped) 27 * Writable and executable never coincide (W^X). Everything the task can reach is private to the slot 28 * except the caller's workspace. */ 29 #include "arch.h" 30 #include "ck_internal.h" 31 #include "fmt.h" 32 #include "osc_admit.h" 33 #include "osc_task.h" 34 #include "pt.h" 35 #include "sha256.h" 36 37 38 /* ---- vector hooks (arch/exception.c) and asm (arch/m3.S) ---- */ 39 extern struct ck_frame *(*ck_tick_switch)(struct ck_frame *f); 40 extern int (*ck_lower_sync)(struct ck_frame *f, uint64_t esr); 41 void ck_el0_enter(uint64_t entry, uint64_t user_sp, const uint64_t regs[31]); 42 void ck_el0_resume(void); 43 44 #define PTE_TABLE 3ull 45 #define PTE_PAGE 3ull 46 #define PTE_AP_EL0_RW (1ull << 6) 47 #define PTE_AP_EL0_RO (3ull << 6) 48 #define PTE_NG (1ull << 11) 49 50 #define OFF_RT_TAB 0x001000ull 51 #define OFF_RT_CODE 0x002000ull 52 #define OFF_CODE 0x010000ull 53 #define OFF_IN 0x200000ull 54 #define OFF_WS 0x210000ull 55 #define OFF_STACK_TOP 0x400000ull 56 57 struct slot { 58 uint64_t l0[512], l1[512], l2[512], l3a[512], l3b[512]; 59 uint8_t rt_tab[4096], rt_code[4096], in[4096]; 60 uint8_t code[CK_OSC_CODE_MAX_BYTES]; 61 uint8_t stack[CK_OSC_STACK_MAX_BYTES]; 62 int used; 63 } __attribute__((aligned(4096))); 64 static struct slot slot0; 65 66 static uint64_t base_va; 67 68 static uint64_t get_base(void) 69 { 70 if (base_va) 71 return base_va; 72 const uint64_t *l0 = (const uint64_t *)(uintptr_t)ck_mm_report()->root; 73 for (unsigned i = 1; i < 256; i++) 74 if (!l0[i]) { 75 base_va = (uint64_t)i << 39; 76 break; 77 } 78 return base_va; 79 } 80 81 uint64_t ck_osc_va_in(void) { return get_base() ? base_va + OFF_IN : 0; } 82 uint64_t ck_osc_va_ws(void) { return get_base() ? base_va + OFF_WS : 0; } 83 84 static void clean_range(const void *p, size_t len, int icache) 85 { 86 uint64_t ctr = ck_rd(ctr_el0); 87 uint64_t dline = 4ull << ((ctr >> 16) & 0xf), iline = 4ull << (ctr & 0xf); 88 uint64_t a = (uint64_t)(uintptr_t)p, end = a + len; 89 for (uint64_t x = a & ~(dline - 1); x < end; x += dline) 90 __asm__ volatile("dc civac, %0" ::"r"(x) : "memory"); 91 ck_dsb_ish(); 92 if (icache) { 93 for (uint64_t x = a & ~(iline - 1); x < end; x += iline) 94 __asm__ volatile("ic ivau, %0" ::"r"(x) : "memory"); 95 ck_dsb_ish(); 96 } 97 ck_isb(); 98 } 99 100 static void swap_ttbr0(uint64_t ttbr0) 101 { 102 ck_dsb_ish(); 103 ck_wr(ttbr0_el1, ttbr0); 104 ck_isb(); 105 __asm__ volatile("tlbi vmalle1" ::: "memory"); 106 ck_dsb_ish(); 107 ck_isb(); 108 } 109 110 /* ---- page attributes: the W^X rules live here ---- */ 111 #define COMMON (CK_PTE_ATTRIDX(CK_MAIR_IDX_NORMAL) | CK_PTE_SH_INNER | CK_PTE_AF | PTE_NG | CK_PTE_PXN) 112 #define DATA_XN CK_PTE_UXN /* data, stack and input are never executable at EL0 */ 113 #define ATTR_RX (PTE_PAGE | PTE_AP_EL0_RO | COMMON) /* read + execute */ 114 #define ATTR_RO (PTE_PAGE | PTE_AP_EL0_RO | COMMON | DATA_XN) /* read only, no execute */ 115 #define ATTR_RW (PTE_PAGE | PTE_AP_EL0_RW | COMMON | DATA_XN) /* read + write, no execute */ 116 117 static void map(uint64_t *l3, unsigned idx, const void *page, uint64_t attr) 118 { 119 l3[idx] = (uint64_t)(uintptr_t)page | attr; 120 } 121 122 static unsigned count_mapped(const struct slot *s) 123 { 124 unsigned n = 0; 125 for (unsigned i = 0; i < 512; i++) 126 n += (unsigned)((s->l3a[i] & 1) + (s->l3b[i] & 1)); 127 return n; 128 } 129 130 /* ---- the run ---- */ 131 static struct { 132 int active, done; 133 uint64_t ticks, budget, rt_code_va; 134 struct osc_result res; 135 } run; 136 137 static void end_task(struct ck_frame *f, const struct osc_event *e) 138 { 139 osc_launch_classify(e, run.rt_code_va, &run.res); 140 run.done = 1; 141 f->elr = (uint64_t)(uintptr_t)ck_el0_resume; 142 f->spsr = 0x3c5; /* EL1h, DAIF masked */ 143 } 144 145 static int launch_sync(struct ck_frame *f, uint64_t esr) 146 { 147 if (!run.active || run.done || (f->spsr & 0xf) != 0) 148 return -1; 149 struct osc_event e = { ((esr >> 26) & 0x3f) == 0x15 ? OSC_EV_SVC : OSC_EV_SYNC, esr, f->elr, f->x[0], 150 f->x[1] }; 151 end_task(f, &e); 152 return 0; 153 } 154 155 static struct ck_frame *launch_tick(struct ck_frame *f) 156 { 157 if (!run.active || run.done || (f->spsr & 0xf) != 0) 158 return f; 159 run.ticks++; 160 if (osc_launch_budget_expired(run.ticks, run.budget)) { 161 struct osc_event e = { OSC_EV_BUDGET, 0, f->elr, 0, 0 }; 162 end_task(f, &e); 163 } 164 return f; 165 } 166 167 static void refuse(struct osc_result *res, unsigned code) 168 { 169 memset(res, 0, sizeof *res); 170 res->cls = OSC_RES_REFUSED; 171 res->refused_code = (uint8_t)code; 172 } 173 174 void ck_osc_launch(const uint8_t *unit, size_t unit_len, const struct osc_accept *a, const uint8_t *rcpt, 175 size_t rcpt_len, const struct ck_osc_launch_req *rq, struct osc_result *res, 176 struct ck_osc_launch_info *info) 177 { 178 struct ck_osc_launch_info local; 179 if (!info) 180 info = &local; 181 memset(info, 0, sizeof *info); 182 info->slot_free_after = 1; 183 info->tables_zeroed = 1; /* a refusal built no tables */ 184 refuse(res, OSC_RESOURCE_UNAVAILABLE); 185 /* aienos#383: no launch without a valid realization receipt for exactly this unit's program and code, under 186 * the kernel's own policy (never the caller's). Checked before the name lookup, so a refused receipt runs and 187 * maps nothing. The code is still hashed again below before it is made executable. */ 188 { 189 struct osc_policy pol; 190 ck_osc_policy(&pol); 191 unsigned rc = osc_receipt_verify(rcpt, rcpt_len, a, &pol, &info->rcpt); 192 if (rc != 0) { 193 refuse(res, rc); 194 return; 195 } 196 } 197 int fi = osc_unit_lookup(a, rq->name, rq->name_len); 198 if (fi < 0) { 199 refuse(res, OSC_LAUNCH_BAD_ENTRY); 200 return; 201 } 202 const struct osc_entry *e = &a->entry[fi]; 203 /* spec 8.2 step 16: a signer-declared budget above a launcher hard maximum is LIMIT_EXCEEDED, before any 204 * reservation. max_stack_bytes is the only declared budget the launcher bounds: cpu_ticks is only lowered by 205 * the caller cap (max_ticks), and pool_slots is not bounded because no pool service is provided (a call to 206 * one ends OUTCOME_UNKNOWN). Real reservation failures below keep 30. */ 207 if (!osc_launch_stack_ok(a->max_stack_bytes, CK_OSC_STACK_MAX_BYTES)) { 208 refuse(res, OSC_LIMIT_EXCEEDED); 209 return; 210 } 211 uint64_t in_va = ck_osc_va_in(), ws_va = ck_osc_va_ws(); 212 /* The workspace is mapped read-write into EL0, so it must be memory the caller owns: refuse (30, nothing 213 * mapped) a range over the cap, null, not page aligned, or overlapping the launcher's own state (slot0: page 214 * tables, code copy, runtime, stack) or the caller's input buffer. The kernel has no allowed region for 215 * workspaces, so ownership beyond that is the caller's promise (osc_task.h). */ 216 if (rq->ws && rq->ws->pages && osc_launch_ws_pages_ok(rq->ws->pages) && rq->ws->mem && 217 (!osc_launch_disjoint((uintptr_t)rq->ws->mem, (uint64_t)rq->ws->pages * CK_OSC_PAGE, (uintptr_t)&slot0, sizeof slot0) || 218 !osc_launch_disjoint((uintptr_t)rq->ws->mem, (uint64_t)rq->ws->pages * CK_OSC_PAGE, (uintptr_t)rq->in, rq->in_len))) { 219 refuse(res, OSC_RESOURCE_UNAVAILABLE); 220 return; 221 } 222 if (rq->ws && (!osc_launch_ws_pages_ok(rq->ws->pages) || (rq->ws->pages && (!rq->ws->mem || ((uintptr_t)rq->ws->mem & 4095))))) { 223 refuse(res, OSC_RESOURCE_UNAVAILABLE); /* workspace over the cap or unusable: nothing ran */ 224 return; 225 } 226 uint64_t ws_len = rq->ws ? (uint64_t)rq->ws->pages * CK_OSC_PAGE : 0; 227 if (rq->nargs > 6 || !osc_launch_args_ok(e->reg_kind, e->nregs, rq->args, rq->nargs) || 228 !osc_launch_ranges_owned(e->reg_kind, e->nregs, rq->args, in_va, rq->in_len, ws_va, ws_len)) { 229 refuse(res, OSC_LAUNCH_ARG_SHAPE); 230 return; 231 } 232 /* reservation: code size, input size, busy slot, no base: RESOURCE_UNAVAILABLE and nothing runs */ 233 uint64_t base = get_base(); 234 uint32_t stack = a->max_stack_bytes; 235 if (!base || slot0.used || run.active || a->code_len > CK_OSC_CODE_MAX_BYTES || 236 rq->in_len > CK_OSC_IN_MAX_BYTES || (rq->in_len && !rq->in) || (uint64_t)a->code_off + a->code_len > unit_len || 237 e->code_offset >= a->code_len || (e->code_offset & 3)) 238 return; 239 struct slot *s = &slot0; 240 s->used = 1; 241 /* private copy of the code, hashed again before it is made executable (spec 8.2) */ 242 memset(s->code, 0, sizeof s->code); 243 memcpy(s->code, unit + a->code_off, a->code_len); 244 uint8_t dig[32]; 245 sha256_hash(s->code, a->code_len, dig); 246 if (memcmp(dig, a->code_sha256, 32) != 0) { 247 memset(s->code, 0, sizeof s->code); 248 s->used = 0; 249 refuse(res, OSC_CODE_HASH_MISMATCH); 250 return; 251 } 252 /* tables: kernel root copied (EL1-only), one free L0 slot chained to two L3 tables */ 253 memcpy(s->l0, (const void *)(uintptr_t)ck_mm_report()->root, sizeof s->l0); 254 unsigned idx = (unsigned)(base >> 39); 255 memset(s->l1, 0, sizeof s->l1); 256 memset(s->l2, 0, sizeof s->l2); 257 memset(s->l3a, 0, sizeof s->l3a); 258 memset(s->l3b, 0, sizeof s->l3b); 259 s->l0[idx] = (uint64_t)(uintptr_t)s->l1 | PTE_TABLE; 260 s->l1[0] = (uint64_t)(uintptr_t)s->l2 | PTE_TABLE; 261 s->l2[0] = (uint64_t)(uintptr_t)s->l3a | PTE_TABLE; 262 s->l2[1] = (uint64_t)(uintptr_t)s->l3b | PTE_TABLE; 263 uint64_t rt_code_va = base + OFF_RT_CODE, code_va = base + OFF_CODE, stack_top = base + OFF_STACK_TOP; 264 /* runtime: OscRt table (13 pointers) and the stubs `svc #i` */ 265 memset(s->rt_tab, 0, sizeof s->rt_tab); 266 memset(s->rt_code, 0, sizeof s->rt_code); 267 for (unsigned i = 0; i < OSC_RT_SERVICES; i++) 268 ((uint64_t *)(void *)s->rt_tab)[i] = rt_code_va + (uint64_t)i * OSC_RT_STUB_BYTES; 269 for (unsigned i = 0; i < OSC_RT_STUBS; i++) 270 ((uint32_t *)(void *)s->rt_code)[2 * i] = 0xd4000001u | (i << 5); /* svc #i */ 271 memset(s->in, 0, sizeof s->in); 272 if (rq->in_len) 273 memcpy(s->in, rq->in, rq->in_len); 274 memset(s->stack, 0, sizeof s->stack); 275 unsigned spages = (stack + CK_OSC_PAGE - 1) / CK_OSC_PAGE, cpages = (a->code_len + CK_OSC_PAGE - 1) / CK_OSC_PAGE; 276 map(s->l3a, 1, s->rt_tab, ATTR_RO); 277 map(s->l3a, 2, s->rt_code, ATTR_RX); 278 for (unsigned i = 0; i < cpages; i++) 279 map(s->l3a, 16 + i, s->code + i * CK_OSC_PAGE, ATTR_RX); 280 map(s->l3b, 0, s->in, ATTR_RO); 281 if (rq->ws) 282 for (unsigned i = 0; i < rq->ws->pages; i++) 283 map(s->l3b, 16 + i, rq->ws->mem + i * CK_OSC_PAGE, ATTR_RW); 284 for (unsigned i = 0; i < spages; i++) 285 map(s->l3b, 512 - spages + i, s->stack + (CK_OSC_STACK_MAX_BYTES / CK_OSC_PAGE - spages + i) * CK_OSC_PAGE, 286 ATTR_RW); 287 info->pages_mapped = count_mapped(s); 288 clean_range(s, 5 * 4096, 0); 289 clean_range(s->rt_tab, sizeof s->rt_tab, 0); 290 clean_range(s->rt_code, sizeof s->rt_code, 1); 291 clean_range(s->in, sizeof s->in, 0); 292 clean_range(s->code, sizeof s->code, 1); 293 clean_range(s->stack, sizeof s->stack, 0); 294 if (rq->ws) 295 clean_range(rq->ws->mem, (size_t)rq->ws->pages * CK_OSC_PAGE, 0); 296 297 /* registers: arguments in x0..x5, OscRt in x7, return stub in x30; nothing else */ 298 uint64_t regs[31] = { 0 }; 299 for (unsigned i = 0; i < e->nregs; i++) 300 regs[i] = rq->args[i]; 301 regs[7] = base + OFF_RT_TAB; 302 regs[30] = rt_code_va + (uint64_t)OSC_SVC_RETURN * OSC_RT_STUB_BYTES; 303 info->budget = osc_launch_budget(a->cpu_ticks, rq->max_ticks); 304 305 int (*old_sync)(struct ck_frame *, uint64_t) = ck_lower_sync; 306 struct ck_frame *(*old_tick)(struct ck_frame *) = ck_tick_switch; 307 uint64_t kttbr = ck_rd(ttbr0_el1), cpacr = ck_rd(cpacr_el1), cntkctl = ck_rd(cntkctl_el1); 308 memset(&run, 0, sizeof run); 309 run.budget = info->budget; 310 run.rt_code_va = rt_code_va; 311 ck_lower_sync = launch_sync; 312 ck_tick_switch = launch_tick; 313 uint64_t hz = ck_rd(cntfrq_el0); 314 ck_wr(cpacr_el1, (cpacr & ~(3ull << 20)) | (1ull << 20)); /* FP/SIMD traps at EL0 */ 315 /* EL0 must not touch the timers or counters: the budget timer is the kernel's, whatever firmware left in 316 * CNTKCTL_EL1. Clear EL0PCTEN/EL0VCTEN (bits 0,1: counter reads), EVNTEN/EVNTDIR (2,3: event stream) and 317 * EL0VTEN/EL0PTEN (8,9: timer registers); the loader clears only 8 and 9 (artifact_loader.c). */ 318 ck_wr(cntkctl_el1, cntkctl & ~0x30Full); 319 swap_ttbr0((uint64_t)(uintptr_t)s->l0); 320 ck_wr(cntp_cval_el0, ck_rd(cntpct_el0) + (hz ? hz / 100 : 625000)); 321 ck_wr(cntp_ctl_el0, 1); 322 ck_isb(); 323 run.active = 1; 324 ck_el0_enter(code_va + e->code_offset, stack_top, regs); 325 run.active = 0; 326 ck_wr(cntp_ctl_el0, 0); 327 ck_isb(); 328 swap_ttbr0(kttbr); 329 ck_wr(cpacr_el1, cpacr); 330 ck_wr(cntkctl_el1, cntkctl); 331 ck_isb(); 332 ck_lower_sync = old_sync; 333 ck_tick_switch = old_tick; 334 if (run.done) { 335 *res = run.res; 336 } else { /* the task came back without an event the kernel saw */ 337 struct osc_event lost = { OSC_EV_LOST, 0, 0, 0, 0 }; 338 osc_launch_classify(&lost, rt_code_va, res); 339 } 340 if (res->cls == OSC_RES_RETURNED && e->ret_kind == 0) 341 res->value = 0; /* a void function reports 0 (spec 9.3) */ 342 res->ticks = run.ticks; 343 344 /* teardown: tables, runtime, input, code and stack are cleared; the caller's workspace stays */ 345 memset(s->l0, 0, sizeof s->l0); 346 memset(s->l1, 0, sizeof s->l1); 347 memset(s->l2, 0, sizeof s->l2); 348 memset(s->l3a, 0, sizeof s->l3a); 349 memset(s->l3b, 0, sizeof s->l3b); 350 memset(s->rt_tab, 0, sizeof s->rt_tab); 351 memset(s->rt_code, 0, sizeof s->rt_code); 352 memset(s->in, 0, sizeof s->in); 353 memset(s->code, 0, sizeof s->code); 354 memset(s->stack, 0, sizeof s->stack); 355 clean_range(s, sizeof *s, 1); 356 info->tables_zeroed = count_mapped(s) + (unsigned)(s->l0[idx] != 0) == 0; 357 s->used = 0; 358 info->slot_free_after = !s->used; 359 } 360 361 int ck_osc_result_str(const struct osc_result *r, char *buf, size_t n) 362 { 363 switch (r->cls) { 364 case OSC_RES_RETURNED: 365 return ck_snprintf(buf, n, "RETURNED value=%llu", (unsigned long long)r->value); 366 case OSC_RES_TRAPPED: 367 return ck_snprintf(buf, n, "TRAPPED trap_code=%u", (unsigned)r->trap_code); 368 case OSC_RES_UNKNOWN: 369 return ck_snprintf(buf, n, "OUTCOME_UNKNOWN unknown_reason=%u", (unsigned)r->unknown_reason); 370 case OSC_RES_REFUSED: 371 return ck_snprintf(buf, n, "REFUSED_AT_ADMISSION code=%u name=%s", (unsigned)r->refused_code, 372 osc_code_name(r->refused_code)); 373 default: 374 return ck_snprintf(buf, n, "UNCLASSIFIED"); 375 } 376 } 377 378 /* Where the launcher keeps its own state, so the selftest can aim a workspace at it. */ 379 void ck_osc_state_range(uint64_t *addr, uint64_t *len) 380 { 381 *addr = (uint64_t)(uintptr_t)&slot0; 382 *len = sizeof slot0; 383 }