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/osh_op.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 / osh_op.c` 369 lines ยท 14265 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 /* osh_op.c -- see osh_op.h. The adapter side of OSH on AIENOS: request record in, one capability-checked platform 18 * operation out. No lexing, parsing or expansion happens here. No libc calls (host and kernel builds share it). */ 19 #include "osh_op.h" 20 #include "fmt.h" 21 #include "sha256.h" 22 23 #if defined(CK_OSH_OP_MUTANT_SKIP_PERM) && !__STDC_HOSTED__ 24 #error "CK_OSH_OP_MUTANT_SKIP_PERM is a host-test-only mutant" 25 #endif 26 27 /* section 7.1 layout */ 28 #define REQ_MAGIC 0x4F524551ull 29 #define REQ_HDR 8u 30 #define CMD_CELLS 164u 31 #define MAX_CMDS 8u 32 #define MAX_ARGV 32u 33 #define MAX_ASSIGN 16u 34 #define MAX_REDIR 8u 35 #define LINE_MAX_BYTES 200u /* longest text after the prefix; over it is LIMIT before any effect */ 36 37 static const char PREFIX[] = "osh_op_out: "; 38 39 static void zero(void *p, size_t n) 40 { 41 uint8_t *b = p; 42 for (size_t i = 0; i < n; i++) 43 b[i] = 0; 44 } 45 46 __attribute__((unused)) static int same(const void *a, const void *b, size_t n) 47 { 48 const uint8_t *x = a, *y = b; 49 for (size_t i = 0; i < n; i++) 50 if (x[i] != y[i]) 51 return 0; 52 return 1; 53 } 54 55 const char *osh_err_name(unsigned e) 56 { 57 return osh_err_label(e); /* osh_err.h; section 8.6: an unknown code is IO, never OK */ 58 } 59 60 const char *osh_outcome_name(unsigned o) 61 { 62 static const char *const n[] = { "NOT_STARTED", "COMPLETED", "FAILED_NO_EFFECT", "CANCELLED", "OUTCOME_UNKNOWN" }; 63 return o < sizeof n / sizeof n[0] ? n[o] : "OUTCOME_UNKNOWN"; 64 } 65 66 static void hex32(const uint8_t d[32], char o[65]) 67 { 68 static const char x[] = "`0123456789abcdef`"; 69 for (unsigned i = 0; i < 32; i++) { 70 o[2 * i] = x[d[i] >> 4]; 71 o[2 * i + 1] = x[d[i] & 15]; 72 } 73 o[64] = 0; 74 } 75 76 void osh_session_init(struct osh_session *s, unsigned grant_rights) 77 { 78 zero(s, sizeof *s); 79 ck_cap_init(&s->caps, 0x4f534800u, 4); 80 sha256_hash((const uint8_t *)"aienos-osh-test-session", 23, s->principal); 81 if (grant_rights && ck_cap_insert(&s->caps, OSH_RES_CONSOLE_LOG, grant_rights, &s->console_cap) == CK_CAP_OK) 82 for (unsigned i = 0; i < 32; i++) 83 s->cap_holder[i] = s->principal[i]; 84 } 85 86 int osh_session_close(struct osh_session *s) 87 { 88 if (s->console_cap.generation) 89 (void)ck_cap_remove(&s->caps, s->console_cap); 90 for (unsigned i = 0; i < CK_CAP_SLOTS; i++) 91 if (s->caps.slots[i].live) 92 return 0; 93 return 1; 94 } 95 96 void osh_session_restart(struct osh_session *fresh, const struct osh_session *old) 97 { 98 osh_session_init(fresh, 0); 99 for (unsigned i = 0; i < OSH_INTENT_MAX; i++) 100 fresh->intents[i] = old->intents[i]; 101 fresh->pipelines = old->pipelines; 102 } 103 104 static void note(const struct osh_env *env, const char *fmt, uint32_t seq, const char *what, const uint8_t dg[32]) 105 { 106 char d[65], line[200]; 107 hex32(dg, d); 108 ck_snprintf(line, sizeof line, fmt, (unsigned)seq, what, d); 109 env->log(env->ctx, line); 110 } 111 112 /* Mandatory security receipt (section 13): every use of a grant, every denial and every refusal at the effect boundary. 113 * Names principal, capability domain/index/generation, resource class, operation and the request digest. */ 114 static void receipt(const struct osh_env *env, const struct osh_result *r, const struct osh_binding *b, const char *result, 115 const char *effect) 116 { 117 char d[65], p[65], line[420]; 118 hex32(r->digest, d); 119 hex32(b->principal_id, p); 120 ck_snprintf(line, sizeof line, 121 "osh_op_receipt: seq=%u op=console_write result=%s request_digest=%s effect=%s principal=%s cap_domain=%u " 122 "cap_index=%u cap_generation=%llu resource_class=%u operation=%u", 123 (unsigned)r->seq, result, d, effect, p, (unsigned)b->domain, (unsigned)b->cap_index, 124 (unsigned long long)b->cap_generation, (unsigned)b->resource_class, (unsigned)b->operation); 125 env->log(env->ctx, line); 126 } 127 128 unsigned osh_op_recover(struct osh_session *s, const struct osh_env *env) 129 { 130 unsigned n = 0; 131 for (unsigned i = 0; i < OSH_INTENT_MAX; i++) { 132 struct osh_intent *t = &s->intents[i]; 133 if (!t->used || t->closed) 134 continue; 135 t->closed = 1; 136 t->outcome = OSH_O_OUTCOME_UNKNOWN; 137 t->err = OSH_E_OUTCOME_UNKNOWN; 138 note(env, "osh_op_record: seq=%u outcome=%s request_digest=%s replayed=0", t->seq, "OUTCOME_UNKNOWN", t->digest); 139 n++; 140 } 141 return n; 142 } 143 144 /* section 9 rule 2 and 3: validated at the moment of the effect, against the table that holds the capability. The 145 * REVOKED / STALE split is this adapter's reading of the draft: a handle whose slot is now empty (or a tombstone) and 146 * whose generation was issued is REVOKED; a slot reused under a newer generation is STALE; anything never issued is DENIED. */ 147 unsigned osh_op_perm_check(const struct osh_session *s, const struct osh_binding *b) 148 { 149 #ifdef CK_OSH_OP_MUTANT_SKIP_PERM 150 (void)s; 151 (void)b; 152 return OSH_E_OK; 153 #else 154 if (b->domain == 2) 155 return OSH_E_CAP_DOMAIN_MISMATCH; /* the console resource lives in the kernel IPC table (domain 1) */ 156 if (b->domain != 1) 157 return OSH_E_INVALID_ARG; 158 if (b->cap_generation > 0xffffffffull) 159 return OSH_E_CAP_GEN_NARROW; 160 if (b->resource_class != OSH_RC_CONSOLE_LOG || b->operation != OSH_OP_WRITE) 161 return OSH_E_DENIED; 162 if (!same(b->principal_id, s->cap_holder, 32)) /* the capability belongs to the principal it was granted to */ 163 return OSH_E_DENIED; 164 uint32_t gen = (uint32_t)b->cap_generation; 165 if (gen == 0 || b->cap_index >= s->caps.n) 166 return OSH_E_DENIED; 167 const struct ck_cap_slot *sl = &s->caps.slots[b->cap_index]; 168 struct ck_handle h = { b->cap_index, gen }; 169 if (sl->live && !sl->tombstone && sl->generation == gen) { 170 uint32_t res = 0; 171 int r = ck_cap_lookup(&s->caps, h, CK_R_WRITE, &res); 172 return r == CK_CAP_OK && res == OSH_RES_CONSOLE_LOG ? OSH_E_OK : OSH_E_DENIED; 173 } 174 if (sl->live && !sl->tombstone && sl->generation > gen) 175 return OSH_E_STALE; 176 if ((sl->live && sl->tombstone && sl->generation == gen) || (!sl->live && s->caps.generations[b->cap_index] == gen)) 177 return OSH_E_REVOKED; 178 return OSH_E_DENIED; 179 #endif 180 } 181 182 static int status_for(unsigned err) 183 { 184 if (err == OSH_E_OK) 185 return 0; 186 switch (err) { 187 case OSH_E_NOT_FOUND: return 127; 188 case OSH_E_DENIED: case OSH_E_REVOKED: case OSH_E_STALE: case OSH_E_CAP_DOMAIN_MISMATCH: case OSH_E_CAP_GEN_NARROW: 189 case OSH_E_NOT_SUPPORTED: return 126; /* found but not run: the adapter's choice, UNVERIFIED against the reference shell */ 190 case OSH_E_INTERRUPTED: return 130; 191 default: return 70; /* IO, INVALID_ARG, LIMIT, unknown: an internal-style failure */ 192 } 193 } 194 195 static void finish(struct osh_result *r, unsigned err, unsigned outcome) 196 { 197 r->error = err; 198 r->outcome = outcome; 199 r->status = status_for(err); 200 } 201 202 static void digest_request(const uint64_t *req, unsigned ncmds, const uint64_t *out, uint64_t out_used, uint8_t d[32]) 203 { 204 sha256_ctx c; 205 uint8_t b[8]; 206 size_t cells = REQ_HDR + (size_t)ncmds * CMD_CELLS; 207 sha256_init(&c); 208 for (size_t i = 0; i < cells; i++) { 209 for (unsigned k = 0; k < 8; k++) 210 b[k] = (uint8_t)(req[i] >> (8 * k)); 211 sha256_update(&c, b, 8); 212 } 213 for (uint64_t i = 0; i < out_used; i++) { 214 b[0] = (uint8_t)out[i]; 215 sha256_update(&c, b, 1); 216 } 217 sha256_final(&c, d); 218 } 219 220 static struct osh_intent *close_intent(struct osh_intent *t, unsigned outcome, unsigned err, int status) 221 { 222 t->closed = 1; 223 t->outcome = (uint8_t)outcome; 224 t->err = (uint8_t)err; 225 t->status = (uint32_t)status; 226 return t; 227 } 228 229 void osh_op_exec(struct osh_session *s, const struct osh_env *env, const uint64_t *req, const uint64_t *out, size_t n_out, 230 struct osh_result *r) 231 { 232 zero(r, sizeof *r); 233 r->seq = ++s->pipelines; 234 235 /* 1. the record is the expander's output, but it sits in memory the unit could write: check it before use */ 236 uint64_t ncmds = req[1], out_used = req[4]; 237 if (req[0] != REQ_MAGIC || req[5] != 1 || (req[3] & ~1ull) || req[6] || req[7] || ncmds < 1 || ncmds > MAX_CMDS || 238 out_used > n_out) { 239 finish(r, OSH_E_INVALID_ARG, OSH_O_NOT_STARTED); 240 return; 241 } 242 digest_request(req, (unsigned)ncmds, out, out_used, r->digest); 243 const uint64_t *c0 = req + REQ_HDR; 244 uint64_t nargv = c0[0], nassign = c0[1], nredir = c0[2], builtin = c0[3]; 245 if (nargv > MAX_ARGV || nassign > MAX_ASSIGN || nredir > MAX_REDIR) { 246 finish(r, OSH_E_INVALID_ARG, OSH_O_NOT_STARTED); 247 return; 248 } 249 for (uint64_t k = 0; k < nargv; k++) { 250 uint64_t off = c0[4 + 2 * k], len = c0[5 + 2 * k]; 251 if (off > out_used || len > out_used - off) { 252 finish(r, OSH_E_INVALID_ARG, OSH_O_NOT_STARTED); 253 return; 254 } 255 for (uint64_t i = 0; i < len; i++) 256 if (out[off + i] > 255) { 257 finish(r, OSH_E_INVALID_ARG, OSH_O_NOT_STARTED); 258 return; 259 } 260 } 261 262 /* 2. RESOLVE (section 8.3): a name is a lookup key only. Class 1, a compiled operation the adapter hosts. */ 263 if (nargv == 0 || c0[4 + 1] != 4 || out[c0[4]] != 'k' || out[c0[4] + 1] != 'l' || out[c0[4] + 2] != 'o' || 264 out[c0[4] + 3] != 'g') { 265 finish(r, OSH_E_NOT_FOUND, OSH_O_NOT_STARTED); 266 return; 267 } 268 /* what this layer cannot do yet is refused before any effect, never approximated */ 269 if (ncmds != 1 || nassign || nredir || builtin) { 270 finish(r, OSH_E_NOT_SUPPORTED, OSH_O_NOT_STARTED); /* no pipes, redirections, assignments in this slice */ 271 return; 272 } 273 274 /* 3. the text to write: argv[1..] joined by one space; bytes outside 0x20..0x7e become '.' so an argument can 275 * never forge a record line */ 276 char line[sizeof PREFIX + LINE_MAX_BYTES + 3]; 277 size_t n = 0; 278 for (; PREFIX[n]; n++) 279 line[n] = PREFIX[n]; 280 size_t body = 0; 281 for (uint64_t k = 1; k < nargv; k++) { 282 uint64_t off = c0[4 + 2 * k], len = c0[5 + 2 * k]; 283 if (body + (k > 1) + len > LINE_MAX_BYTES) { 284 finish(r, OSH_E_LIMIT, OSH_O_NOT_STARTED); 285 return; 286 } 287 if (k > 1) 288 line[n + body++] = ' '; 289 for (uint64_t i = 0; i < len; i++) { 290 uint8_t ch = (uint8_t)out[off + i]; 291 line[n + body++] = ch >= 0x20 && ch <= 0x7e ? (char)ch : '.'; 292 } 293 } 294 n += body; 295 line[n++] = '\n'; 296 line[n] = 0; /* the sink may print it as a C string */ 297 298 /* 4. the binding this effect carries, built from what the task holds. Nothing here grants anything. */ 299 struct osh_binding b; 300 zero(&b, sizeof b); 301 for (unsigned i = 0; i < 32; i++) 302 b.principal_id[i] = s->principal[i]; 303 b.domain = 1; 304 b.cap_index = s->console_cap.index; 305 b.cap_generation = s->console_cap.generation; 306 b.resource_class = OSH_RC_CONSOLE_LOG; 307 b.operation = OSH_OP_WRITE; 308 309 /* 5. the intent record, before the effect (section 10). A slot is free when never used or when its outcome is 310 * already written (and logged); only OPEN intents count against the limit. */ 311 struct osh_intent *t = 0; 312 for (unsigned i = 0; i < OSH_INTENT_MAX && !t; i++) 313 if (!s->intents[i].used) 314 t = &s->intents[i]; 315 for (unsigned i = 0; i < OSH_INTENT_MAX && !t; i++) 316 if (s->intents[i].closed) 317 t = &s->intents[i]; 318 if (!t) { 319 finish(r, OSH_E_LIMIT, OSH_O_NOT_STARTED); 320 receipt(env, r, &b, osh_err_name(OSH_E_LIMIT), "none"); 321 return; 322 } 323 t->used = 1; 324 t->closed = 0; 325 t->seq = r->seq; 326 for (unsigned i = 0; i < 32; i++) 327 t->digest[i] = r->digest[i]; 328 329 if (s->hooks.revoke_before_perm_at == r->seq && s->console_cap.generation) 330 (void)ck_cap_remove(&s->caps, s->console_cap); /* revoked between building the request and using it */ 331 332 /* 6. the effect boundary: validated now, not at parse time */ 333 unsigned pc = osh_op_perm_check(s, &b); 334 if (pc != OSH_E_OK) { 335 close_intent(t, OSH_O_FAILED_NO_EFFECT, pc, status_for(pc)); 336 finish(r, pc, OSH_O_FAILED_NO_EFFECT); 337 receipt(env, r, &b, osh_err_name(pc), "none"); 338 return; 339 } 340 if (s->hooks.interrupt_before_effect_at == r->seq) { 341 close_intent(t, OSH_O_CANCELLED, OSH_E_INTERRUPTED, 130); 342 finish(r, OSH_E_INTERRUPTED, OSH_O_CANCELLED); 343 r->stop = 1; 344 note(env, "osh_op_record: seq=%u outcome=%s request_digest=%s effect=none", r->seq, "CANCELLED", r->digest); 345 return; 346 } 347 348 /* 7. the effect */ 349 int w = s->hooks.sink_fail_at == r->seq ? -1 : env->out(env->ctx, line, n); 350 if (w != 0) { 351 /* a failed write-class effect may have partly happened: unknown, not "no effect" */ 352 close_intent(t, OSH_O_OUTCOME_UNKNOWN, OSH_E_IO, 70); 353 finish(r, OSH_E_IO, OSH_O_OUTCOME_UNKNOWN); 354 note(env, "osh_op_record: seq=%u outcome=%s request_digest=%s effect=unknown", r->seq, "OUTCOME_UNKNOWN", r->digest); 355 return; 356 } 357 /* the grant was used: a security receipt, always (section 13), written even if the adapter is lost right after */ 358 receipt(env, r, &b, "OK", "console_write"); 359 if (s->hooks.crash_after_effect_at == r->seq) { 360 r->crashed = 1; /* the adapter is lost here: the effect happened, the outcome is never written */ 361 r->stop = 1; 362 r->error = OSH_E_OUTCOME_UNKNOWN; 363 r->outcome = OSH_O_OUTCOME_UNKNOWN; 364 r->status = 70; 365 return; 366 } 367 close_intent(t, OSH_O_COMPLETED, OSH_E_OK, 0); 368 finish(r, OSH_E_OK, OSH_O_COMPLETED); 369 }