omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / runtime / rx_world.h
| 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 | /* |
| 18 | * rx_world.h -- first heartbeat of the resident reaction runtime (ADR 0016). |
| 19 | * |
| 20 | * One shared object world. Reactions declare what wakes them, what they read, |
| 21 | * what they write and which capabilities they need. A dependency index maps a |
| 22 | * (object, field-mask) change to the interested reactions only; nothing scans |
| 23 | * the graph. Worker threads run whatever is READY, in admission order; they |
| 24 | * never decide the semantic sequence. A reaction computes against a versioned |
| 25 | * snapshot, then publishes atomically after re-validating its inputs and its |
| 26 | * capabilities. Every outcome leaves a content-addressed causal crumb. |
| 27 | * |
| 28 | * This is the simple human reference (ADR 0016 §56 step 2): one world mutex, |
| 29 | * bounded fixed-size tables. It is deliberately not optimized. |
| 30 | * |
| 31 | * Covered here: one canonical object (semantic record and physical placement |
| 32 | * are two aspects of that same object), CPU checks of the pointer-free |
| 33 | * cross-engine descriptor, R3 reaction core, R4 causal crumbs, R5 host |
| 34 | * admission, R6 host stability. The receipt is what claims a gate. This file |
| 35 | * does not claim a graphics-processor run. A native authority view can be |
| 36 | * installed beside the Linux oracle; installing it is not, by itself, the |
| 37 | * R7 claim. Faculty labels in tests are not the real AIEN, Omega, or AEGIS. |
| 38 | */ |
| 39 | #ifndef RX_WORLD_H |
| 40 | #define RX_WORLD_H |
| 41 | |
| 42 | #include "rx_caproot.h" |
| 43 | #include "rx_caller.h" |
| 44 | #include "omega_shared_world_abi.h" |
| 45 | |
| 46 | #include <pthread.h> |
| 47 | #include <stdbool.h> |
| 48 | #include <stdint.h> |
| 49 | |
| 50 | #define RX_MAX_OBJECTS 256u |
| 51 | #define RX_MAX_FIELDS 8u |
| 52 | #define RX_MAX_REACTIONS 1024u |
| 53 | #define RX_MAX_DEPS 8u |
| 54 | #define RX_MAX_WRITES 8u |
| 55 | #define RX_MAX_CAPS 8u |
| 56 | #define RX_MAX_MUTATIONS 16u |
| 57 | #define RX_MAX_SUBS 1024u /* subscriptions per object */ |
| 58 | /* Causal-log capacity for long living-body episodes: 2^25 records (about |
| 59 | * 38 GB of reserved address space at 1128 B each), backed only as written. */ |
| 60 | #define RX_CRUMBS_LONG_EPISODE (1ull << 25) |
| 61 | #define RX_MAX_WORKERS 16u |
| 62 | #define RX_MAX_BINDERS 4u /* commit binders per World (COMPOSITION-2) */ |
| 63 | #define RX_MAX_PARENTS (RX_MAX_DEPS * RX_MAX_FIELDS + 1u) |
| 64 | #define RX_PRIORITY_CLASSES 7u |
| 65 | #define RX_DEFERRED_INITIAL 64u |
| 66 | |
| 67 | #define RX_ALL_FIELDS ((uint64_t)((1u << RX_MAX_FIELDS) - 1u)) |
| 68 | #define RX_FIELD(i) ((uint64_t)1u << (i)) |
| 69 | |
| 70 | /* Faculties (logical write domains, not memory owners). */ |
| 71 | enum { RX_FACULTY_EXTERNAL = 0, RX_FACULTY_AIEN, RX_FACULTY_OMEGA, RX_FACULTY_AEGIS, |
| 72 | RX_FACULTY_ROOT }; /* the capability root installing what it minted (R8) */ |
| 73 | |
| 74 | /* Priority classes (ADR 0016 §9), highest first. */ |
| 75 | enum { |
| 76 | RX_PRIO_CRITICAL = 0, RX_PRIO_INTERACTIVE, RX_PRIO_FOREGROUND, RX_PRIO_LEARNING, |
| 77 | RX_PRIO_MAINTENANCE, RX_PRIO_SPECULATIVE, RX_PRIO_BACKGROUND |
| 78 | }; |
| 79 | |
| 80 | /* How a self-fed cycle should be read. Ordinary alternating output is an |
| 81 | * unstable oscillation. A declared periodic loop may alternate and still be |
| 82 | * useful control. A run that stops changing is convergent. A run that keeps |
| 83 | * spending work without a new value is livelock. */ |
| 84 | enum { RX_LOOP_ORDINARY = 0, RX_LOOP_PERIODIC = 1 }; |
| 85 | |
| 86 | /* Persistence class (ADR 0016 §19; ADR 0015 §2 mapping in ADR 0016 Part I). */ |
| 87 | typedef enum { RX_PERSIST_EPHEMERAL = 0, RX_PERSIST_RESIDENT, RX_PERSIST_DURABLE } RxPersist; |
| 88 | |
| 89 | /* Reaction lifecycle (ADR 0016 §6). Every activation ends back in DORMANT. */ |
| 90 | typedef enum { |
| 91 | RX_DORMANT = 0, |
| 92 | RX_READY, |
| 93 | RX_BLOCKED_AUTHORITY, |
| 94 | RX_BLOCKED_RESOURCE, |
| 95 | RX_RUNNING, |
| 96 | RX_PUBLISHING, |
| 97 | RX_COMMITTED, |
| 98 | RX_INVALIDATED, |
| 99 | RX_FAILED, |
| 100 | RX_CANCELLED, |
| 101 | RX_CONFLICT, |
| 102 | RX_REJECTED, |
| 103 | RX_STATE_COUNT |
| 104 | } RxState; |
| 105 | |
| 106 | /* Causal crumb kinds. */ |
| 107 | typedef enum { |
| 108 | RX_CRUMB_CREATE = 1, |
| 109 | RX_CRUMB_EXTERNAL, |
| 110 | RX_CRUMB_COMMIT, |
| 111 | RX_CRUMB_INVALIDATED, |
| 112 | RX_CRUMB_BLOCKED_AUTHORITY, |
| 113 | RX_CRUMB_REJECTED, |
| 114 | RX_CRUMB_FAILED, |
| 115 | RX_CRUMB_NOOP, /* ran, proposed no change: fixed point */ |
| 116 | RX_CRUMB_RETIRE, |
| 117 | RX_CRUMB_QUARANTINE, /* an R6 limit stopped this reaction; reason names it */ |
| 118 | RX_CRUMB_CANCELLED, /* HD-09: ran past its declared deadline; nothing |
| 119 | published; reason RX_ERR_DEADLINE, or |
| 120 | RX_ERR_HALTED when an operator stop refused it */ |
| 121 | /* R16 G6 operator emergency control (spec/r16-operator-emergency-stop.md): |
| 122 | * reaction UINT32_MAX, caps[0] the operator's control capability. */ |
| 123 | RX_CRUMB_OPERATOR_STOP, /* a stop took effect; reason = the operator's code */ |
| 124 | RX_CRUMB_OPERATOR_RESUME /* a stopped world resumed; parent = its stop */ |
| 125 | } RxCrumbKind; |
| 126 | |
| 127 | /* Which R6 limit engaged (reason of a QUARANTINE crumb). */ |
| 128 | enum { RX_CONTAIN_BUDGET = 1, RX_CONTAIN_OSCILLATION, RX_CONTAIN_LIVELOCK, |
| 129 | RX_CONTAIN_CONFLICT }; |
| 130 | |
| 131 | /* Publication errors. */ |
| 132 | #define RX_OK 0 |
| 133 | #define RX_ERR_ARG -1 |
| 134 | #define RX_ERR_FULL -2 |
| 135 | #define RX_ERR_STALE_GEN -3 |
| 136 | #define RX_ERR_AUTHORITY -4 |
| 137 | #define RX_ERR_WRITE_SET -5 |
| 138 | #define RX_ERR_NOT_FOUND -6 |
| 139 | #define RX_ERR_TIMEOUT -7 |
| 140 | #define RX_ERR_BOUNDS -20 |
| 141 | #define RX_ERR_REPLAY -21 |
| 142 | #define RX_ERR_TORN -22 |
| 143 | #define RX_ERR_BAD_DESC -23 |
| 144 | #define RX_ERR_UNPLACED -24 |
| 145 | #define RX_ERR_EXISTS -25 |
| 146 | #define RX_ERR_SEAT_LOST -26 /* the graphics seat died holding the claim */ |
| 147 | #define RX_ERR_IDENTITY -27 /* caller credential absent, forged, stale or revoked */ |
| 148 | #define RX_ERR_BINDING -28 /* a bound field's external reference was refused (COMPOSITION-2) */ |
| 149 | #define RX_ERR_BUSY -29 /* the reaction still has work pending (rx_world_remove_reaction) */ |
| 150 | #define RX_ERR_DEADLINE -30 /* the logical tick passed the declared deadline (RX_CRUMB_CANCELLED) */ |
| 151 | #define RX_ERR_HALTED -31 /* operator emergency stop in force (RX_CRUMB_CANCELLED) */ |
| 152 | #define RX_ERR_IO -32 /* an operator-stop mark could not be read or retired */ |
| 153 | |
| 154 | /* Reaction notices carried in the frozen 128-byte descriptor. |
| 155 | * The older transform request/result values stay in the frozen layout and |
| 156 | * are rejected here: a ring slot is a wake, a claim, a publication, a |
| 157 | * completion, a fault, or a shutdown. It is not a call to another subsystem. |
| 158 | */ |
| 159 | enum { |
| 160 | RX_RING_WAKE = 0x0010u, |
| 161 | RX_RING_CLAIM = 0x0011u, |
| 162 | RX_RING_PUBLISH = 0x0012u, |
| 163 | RX_RING_COMPLETE = 0x0013u, |
| 164 | RX_RING_FAULT = 0x0014u, |
| 165 | RX_RING_KEEPALIVE = 0x0003u, /* frozen NOP: ordering only, not a call */ |
| 166 | RX_RING_SHUTDOWN = 0x0015u |
| 167 | }; |
| 168 | |
| 169 | /* Host-side descriptor faults. They do not change the frozen mailbox enum. */ |
| 170 | #define RX_FAULT_BOUNDS 0x0101u |
| 171 | #define RX_FAULT_CAP 0x0102u |
| 172 | #define RX_FAULT_TORN_PUB 0x0103u |
| 173 | #define RX_FAULT_OVERLAP 0x0104u |
| 174 | #define RX_FAULT_DIVERGED 0x0105u |
| 175 | #define RX_FAULT_UNPLACED 0x0106u |
| 176 | |
| 177 | /* Capability references in publication and claim payloads. Bytes 0-3 are |
| 178 | * the input object's cap id and 4-7 the low half of its 64-bit generation; |
| 179 | * a claim also carries the output object's id (24), object generation (28), |
| 180 | * cap id (32) and cap generation low half (36). The high halves of the two |
| 181 | * cap generations sit at 40 and 44, past every field the seat reads; the |
| 182 | * seat copies the whole descriptor into its notice, so they come back. */ |
| 183 | #define RX_CAP_GEN_HI_A 40u |
| 184 | #define RX_CAP_GEN_HI_B 44u |
| 185 | #define RX_CAP_PAYLOAD 48u |
| 186 | |
| 187 | /* Physical aspect. These name where the bytes sit. They are not the object's name. |
| 188 | * A live object may have no placement. One spare window exists so a placed |
| 189 | * object can move without borrowing another object's bytes. */ |
| 190 | #define RX_PLACE_COHERENT 1u |
| 191 | #define RX_LOCALITY_MACHINE 1u |
| 192 | #define RX_COHERENCY_HOST 1u /* CPU image of the shared layout; no graphics processor claimed */ |
| 193 | #define RX_COHERENCY_SEAT 2u /* same window, graphics seat participated */ |
| 194 | /* Bit in RxResourceNeed.accelerator_features. Admission already requires the |
| 195 | * budget to offer every bit a reaction asks for. This bit selects the resident |
| 196 | * graphics seat. It is not a call from the processor to a graphics function. */ |
| 197 | #define RX_ACCEL_BLACKWELL 0x2u |
| 198 | #define RX_SEAT_BLACKWELL 0x100u |
| 199 | #define RX_SEQ_WORKER 0x200u /* crumb worker of the R15 sequential reference */ |
| 200 | #define RX_OBJECT_WINDOW 64u /* eight field values, little-endian */ |
| 201 | #define RX_PHYS_WINDOWS (RX_MAX_OBJECTS + 1u) |
| 202 | |
| 203 | typedef struct { uint32_t id; uint32_t generation; } RxObjRef; |
| 204 | |
| 205 | typedef struct { |
| 206 | RxObjRef obj; |
| 207 | uint64_t mask; /* fields of interest */ |
| 208 | } RxDep; |
| 209 | |
| 210 | typedef struct { |
| 211 | RxCapRef ref; |
| 212 | uint64_t resource; |
| 213 | uint32_t rights; |
| 214 | } RxCapNeed; |
| 215 | |
| 216 | typedef struct { |
| 217 | RxObjRef obj; |
| 218 | uint32_t field; |
| 219 | uint64_t value; |
| 220 | } RxMutation; |
| 221 | |
| 222 | /* Versioned snapshot of one read dependency. */ |
| 223 | typedef struct { |
| 224 | RxObjRef obj; |
| 225 | uint64_t mask; |
| 226 | uint64_t version; |
| 227 | uint64_t field[RX_MAX_FIELDS]; |
| 228 | uint64_t field_version[RX_MAX_FIELDS]; |
| 229 | uint64_t field_writer[RX_MAX_FIELDS]; /* causal id of the last writer */ |
| 230 | } RxSnapshotDep; |
| 231 | |
| 232 | typedef struct RxCtx { |
| 233 | uint32_t n_in; |
| 234 | RxSnapshotDep in[RX_MAX_DEPS]; |
| 235 | uint32_t n_out; |
| 236 | RxMutation out[RX_MAX_MUTATIONS]; |
| 237 | void *user; |
| 238 | uint32_t worker; |
| 239 | } RxCtx; |
| 240 | |
| 241 | /* A reaction function may return RX_FN_DEFER when it has handed a physical |
| 242 | * effect it decided (an R9 store write) to a durable executor and has |
| 243 | * nothing to publish yet. The activation keeps its admission and its place |
| 244 | * (READY/RUNNING is not left, quiescence waits for it) but frees the worker. |
| 245 | * When the executor calls rx_world_resume the same activation runs again on a |
| 246 | * fresh snapshot, on an ordinary worker, and publishes through the usual |
| 247 | * checks. No crumb is written for the deferring run. Never returned in a |
| 248 | * sequential reference world (it is treated as a failure there). */ |
| 249 | #define RX_FN_DEFER 0x44454652 |
| 250 | |
| 251 | /* Pure transformation: read ctx->in, append proposals to ctx->out. |
| 252 | * Return 0 to publish (possibly nothing), negative to fail. */ |
| 253 | typedef int (*RxFn)(RxCtx *ctx); |
| 254 | |
| 255 | /* What a reaction costs to be admitted. Zero means "none of this kind". |
| 256 | * Priority stays on the descriptor; this does not choose meaning, only fit. */ |
| 257 | typedef struct { |
| 258 | uint32_t compute_class; /* 0 = any; else bit (class & 31) must be offered */ |
| 259 | uint32_t locality; /* 0 = anywhere; else required bits */ |
| 260 | uint32_t accelerator_features; /* 0 = none; else required bits */ |
| 261 | uint32_t latency_class; /* recorded; admission uses priority */ |
| 262 | uint64_t memory_bytes; |
| 263 | uint64_t deadline; /* 0 = none, compared with the world tick */ |
| 264 | uint64_t energy_cost; /* 0 = free */ |
| 265 | } RxResourceNeed; |
| 266 | |
| 267 | /* Physical budget. The setter is exact: zero means that resource is |
| 268 | * unavailable. rx_world_init installs permissive defaults for callers that |
| 269 | * never set a budget. starvation_bound 0 disables bounded fairness (strict |
| 270 | * priority); a positive value bounds how long a waiting lower class may be |
| 271 | * skipped by more urgent work. */ |
| 272 | typedef struct { |
| 273 | uint32_t slots; |
| 274 | uint32_t starvation_bound; |
| 275 | uint32_t offered_locality; |
| 276 | uint32_t offered_accel; |
| 277 | uint32_t compute_mask; |
| 278 | uint64_t memory_bytes; |
| 279 | uint64_t energy_budget; |
| 280 | uint64_t logical_tick; |
| 281 | } RxResourceBudget; |
| 282 | |
| 283 | /* Stability limits. Zero disables that limit. */ |
| 284 | typedef struct { |
| 285 | uint32_t activation_budget; /* per reaction */ |
| 286 | uint32_t fanout_limit; /* wakes from one publication */ |
| 287 | uint32_t oscillation_limit; /* A-B-A flips before quarantine */ |
| 288 | uint32_t livelock_limit; /* consecutive no-progress runs */ |
| 289 | uint32_t conflict_limit; /* consecutive invalidations/rejections */ |
| 290 | } RxStabilityBudget; |
| 291 | |
| 292 | typedef struct { |
| 293 | const char *name; |
| 294 | uint32_t faculty; |
| 295 | uint32_t subject; /* principal the capabilities must be bound to */ |
| 296 | /* R16 C5: in a world with bound callers, the runtime-issued credential |
| 297 | * for `subject` (rx_caller.h). Checked at registration; the stored copy |
| 298 | * keeps only the generation, and activation and commit re-check that the |
| 299 | * enrollment is still live at it. Ignored by an unbound world. */ |
| 300 | RxCallerCred caller; |
| 301 | uint32_t priority; |
| 302 | RxResourceNeed need; |
| 303 | uint32_t n_triggers; |
| 304 | RxDep triggers[RX_MAX_DEPS]; |
| 305 | uint32_t n_reads; /* triggers are read implicitly */ |
| 306 | RxDep reads[RX_MAX_DEPS]; |
| 307 | uint32_t n_writes; |
| 308 | RxDep writes[RX_MAX_WRITES]; |
| 309 | uint32_t n_caps; |
| 310 | RxCapNeed caps[RX_MAX_CAPS]; |
| 311 | /* R8. When cap_slotted[i] is set, the reference for caps[i] is read at |
| 312 | * every check from fields 0 (cap id) and 1 (generation) of the slot |
| 313 | * object, not from caps[i].ref. Holding a slot is still not permission: |
| 314 | * the authority validates whatever reference it holds. A slot that is |
| 315 | * retired or of another generation yields no reference. */ |
| 316 | bool cap_slotted[RX_MAX_CAPS]; |
| 317 | RxObjRef cap_slot[RX_MAX_CAPS]; |
| 318 | /* R8. Every field this reaction proposes is stamped as written by its |
| 319 | * commit, even when the value did not change (only changed fields wake |
| 320 | * anyone). For records whose authorship is checked field by field: a |
| 321 | * value left over from an earlier writer would otherwise keep that |
| 322 | * writer's name. */ |
| 323 | bool stamp_proposed; |
| 324 | uint32_t loop_kind; /* RX_LOOP_ORDINARY or RX_LOOP_PERIODIC */ |
| 325 | RxFn fn; |
| 326 | void *user; |
| 327 | } RxReactionDesc; |
| 328 | |
| 329 | typedef struct { |
| 330 | uint32_t id; |
| 331 | uint32_t generation; |
| 332 | uint32_t type; |
| 333 | bool live; |
| 334 | RxPersist persist; |
| 335 | uint64_t resource; /* capability resource that guards this object */ |
| 336 | uint64_t version; |
| 337 | uint64_t field[RX_MAX_FIELDS]; |
| 338 | uint64_t field_version[RX_MAX_FIELDS]; |
| 339 | uint64_t field_writer[RX_MAX_FIELDS]; |
| 340 | uint8_t digest[32]; /* content identity of (type, fields) */ |
| 341 | /* Capability reference. Holding this pair is not permission; the root still checks it. */ |
| 342 | RxCapRef cap; |
| 343 | /* Physical aspect of this same object. Absent until attached. |
| 344 | * Not a second object and not a second generation. */ |
| 345 | bool placed; |
| 346 | uint32_t window; |
| 347 | uint64_t region_offset; |
| 348 | uint64_t size_bytes; |
| 349 | uint32_t placement; |
| 350 | uint32_t locality; |
| 351 | uint32_t coherency; |
| 352 | } RxObject; |
| 353 | |
| 354 | typedef struct { uint32_t reaction; uint32_t generation; uint64_t mask; } RxSub; |
| 355 | |
| 356 | typedef struct { |
| 357 | uint64_t id; /* 1-based, dense; 0 = none */ |
| 358 | RxCrumbKind kind; |
| 359 | uint32_t reaction; /* UINT32_MAX for external/create */ |
| 360 | uint32_t faculty; |
| 361 | uint32_t worker; |
| 362 | uint64_t wake_cause; /* crumb that last woke this activation */ |
| 363 | uint64_t coalesced_wakes; /* extra wakes folded into this activation */ |
| 364 | uint32_t n_inputs; |
| 365 | struct { RxObjRef obj; uint64_t version; uint64_t mask; } inputs[RX_MAX_DEPS]; |
| 366 | uint32_t n_caps; |
| 367 | RxCapRef caps[RX_MAX_CAPS]; |
| 368 | uint32_t cap_issuer[RX_MAX_CAPS]; /* authority provenance; 0 if unknown */ |
| 369 | uint32_t n_outputs; |
| 370 | struct { RxObjRef obj; uint64_t version; uint64_t mask; } outputs[RX_MAX_WRITES]; |
| 371 | uint32_t n_parents; |
| 372 | uint64_t parents[RX_MAX_PARENTS]; |
| 373 | uint64_t t_start_ns; |
| 374 | uint64_t t_end_ns; |
| 375 | int32_t reason; |
| 376 | uint8_t digest[32]; /* SHA-256 over canonical crumb bytes + parent digests */ |
| 377 | /* Root of the causal episode: the outside publication this crumb's wake |
| 378 | * chain starts from (its own id for one), 0 when the chain starts at no |
| 379 | * outside publication. Derived from wake_cause, so not hashed; checked by |
| 380 | * rx_world_verify_crumbs. */ |
| 381 | uint64_t episode; |
| 382 | } RxCrumb; |
| 383 | |
| 384 | typedef struct { |
| 385 | RxReactionDesc desc; |
| 386 | RxState state; |
| 387 | bool rearm; /* woken while running: go READY again after */ |
| 388 | bool quarantined; |
| 389 | bool parked; /* yielded so other admitted work can run */ |
| 390 | bool holding; /* currently charged against the physical budget */ |
| 391 | uint64_t wake_cause; |
| 392 | uint64_t coalesced; |
| 393 | uint64_t activations; /* lifetime observability */ |
| 394 | uint64_t episode_activations; /* reset when a wake belongs to another causal episode */ |
| 395 | uint64_t episode; /* episode root episode_activations counts for */ |
| 396 | uint64_t last_crumb; /* this reaction's latest crumb */ |
| 397 | uint32_t contain_pending; /* RX_CONTAIN_* engaged in this activation, not yet recorded */ |
| 398 | uint64_t commits; |
| 399 | uint64_t suppressed; |
| 400 | uint64_t wait_seq; |
| 401 | uint64_t service; |
| 402 | uint64_t memory_held; |
| 403 | uint64_t energy_held; |
| 404 | uint32_t noop_streak; |
| 405 | uint32_t osc_streak; |
| 406 | uint32_t conflict_streak; |
| 407 | uint32_t yield_left; /* self-wakes to skip while other work is waiting */ |
| 408 | uint64_t last_out; |
| 409 | uint64_t prev_out; |
| 410 | bool have_last; |
| 411 | bool have_two; |
| 412 | bool resident_seat; /* handed to the graphics seat; fn is not called */ |
| 413 | bool deferred; /* fn returned RX_FN_DEFER; waits for rx_world_resume */ |
| 414 | bool resume_pending; /* resumed before the deferring run returned */ |
| 415 | /* I11: popped while a running writer of one of its inputs had not yet |
| 416 | * published. Admitted (charged, in in_flight), state READY, not on a |
| 417 | * ready ring; released when no such writer is running, so the writer's |
| 418 | * wake merges into this activation instead of causing a second one. */ |
| 419 | bool upstream_held; |
| 420 | /* Taken out by rx_world_remove_reaction: no subscriptions, never woken |
| 421 | * or run; the slot may be reused by a registration with the same subject |
| 422 | * and faculty. */ |
| 423 | bool removed; |
| 424 | /* R15 timing (only with a timing buffer). */ |
| 425 | uint64_t t_demand, t_ready, t_run, t_fn_end, sched_ns, sched_cpu_ns; |
| 426 | /* Sequential reference only (rx_seq_reference.c): the newest version of |
| 427 | * each trigger this stage has seen, and a re-run it still owes. */ |
| 428 | uint64_t seq_seen[RX_MAX_DEPS]; |
| 429 | bool seq_pending; |
| 430 | uint64_t resident_seq; /* ring sequence of the claim notice */ |
| 431 | uint64_t resident_parent; /* crumb that made this activation ready */ |
| 432 | } RxReaction; |
| 433 | |
| 434 | typedef struct { |
| 435 | uint64_t transitions[RX_STATE_COUNT][RX_STATE_COUNT]; |
| 436 | uint64_t illegal_transitions; |
| 437 | uint64_t wakes; |
| 438 | uint64_t coalesced_wakes; |
| 439 | uint64_t subscriptions_checked; |
| 440 | uint64_t commits; |
| 441 | uint64_t invalidations; |
| 442 | uint64_t blocked_authority; |
| 443 | uint64_t blocked_resource; |
| 444 | uint64_t rejected; |
| 445 | uint64_t failed; |
| 446 | uint64_t noops; |
| 447 | uint64_t crumb_overflow; |
| 448 | uint64_t suppressed_wakes; |
| 449 | uint64_t deferred_wakes; |
| 450 | uint64_t deferred_peak; |
| 451 | uint64_t quarantines; |
| 452 | uint64_t oscillation_trips; |
| 453 | uint64_t livelock_trips; |
| 454 | uint64_t backoffs; |
| 455 | uint64_t periodic_commits; |
| 456 | uint64_t useful_commits; |
| 457 | uint64_t churn; |
| 458 | uint64_t deadline_overdue; |
| 459 | uint64_t desc_published; |
| 460 | uint64_t desc_rejected; |
| 461 | uint64_t resident_claims; /* claims posted to a resident seat */ |
| 462 | uint64_t resident_closed; /* claims ended: committed, refused, or lost */ |
| 463 | uint64_t resident_halted; /* of those closed, results caught by an operator stop (re-run on resume) */ |
| 464 | uint64_t deferrals; /* activations that handed an effect to a durable executor */ |
| 465 | uint64_t seat_losses; /* times a seat was declared lost */ |
| 466 | /* R15 instrumentation. Counted, never inferred. */ |
| 467 | uint64_t activations; /* run_one entries (every activation, any outcome) */ |
| 468 | uint64_t externals; /* outside publications */ |
| 469 | /* Propagation (spec §6.3). `subscriptions_checked` counts subscriptions |
| 470 | * inspected and `wakes` counts wake attempts (demand calls, including |
| 471 | * the sequential reference's own admission). */ |
| 472 | uint64_t subscriptions_matched; /* inspected, generation and mask matched */ |
| 473 | uint64_t wakes_accepted; /* wake attempt took a DORMANT reaction to waiting */ |
| 474 | uint64_t ready_inserts; /* reactions placed on a ready ring */ |
| 475 | /* Copied bytes: bytes processor code writes into a destination as a copy |
| 476 | * or serialization of data that exists elsewhere. Every path is its own |
| 477 | * counter; none is inferred from architecture. The graphics chip's own |
| 478 | * ring and window accesses are not processor copies and are not here. */ |
| 479 | uint64_t snapshot_bytes; /* RxSnapshotDep entries filled into RxCtx */ |
| 480 | uint64_t stage_bytes; /* current fields copied into the publication |
| 481 | staging buffer, per object written */ |
| 482 | uint64_t crumb_bytes; /* RxCrumb records appended to the causal log */ |
| 483 | uint64_t proj_bytes; /* coherent object table entries + field windows */ |
| 484 | uint64_t c2g_write_bytes; /* descriptors written to the processor->seat ring |
| 485 | (publications, claims, shutdown, injected) */ |
| 486 | uint64_t c2g_read_bytes; /* descriptors copied out of that ring (publication |
| 487 | consumer, processor stand-in seat) */ |
| 488 | uint64_t g2c_write_bytes; /* result descriptors written by the stand-in seat */ |
| 489 | uint64_t g2c_read_bytes; /* result descriptors copied out of the seat ring */ |
| 490 | uint64_t window_move_bytes; /* object windows saved and restored on relocation */ |
| 491 | uint64_t setup_copy_bytes; /* whole-region copy when binding coherent memory */ |
| 492 | /* Scheduling time, only while a timing buffer is installed: wake, |
| 493 | * admission and ready-ring code (RES), readiness polling and admission |
| 494 | * (SEQ). Wall is CLOCK_MONOTONIC; CPU is the calling thread's |
| 495 | * CLOCK_THREAD_CPUTIME_ID. Each interval is read on one thread. */ |
| 496 | uint64_t sched_wall_ns; |
| 497 | uint64_t sched_cpu_ns; |
| 498 | uint64_t gpu_results_taken; /* completion notices taken from the chip ring */ |
| 499 | uint64_t gpu_polls_empty; /* completion-ring polls that found nothing */ |
| 500 | uint64_t gpu_host_waits; /* times a caller blocked for a GPU completion */ |
| 501 | uint64_t seq_pulses; /* sequential reference only: pulses run */ |
| 502 | uint64_t seq_polls; /* sequential reference only: readiness polls */ |
| 503 | uint64_t seq_runs; /* sequential reference only: stages run */ |
| 504 | /* HD-09 resource contract v0, first enforcement cut: activations whose |
| 505 | * function returned after the logical tick passed need.deadline (strictly |
| 506 | * greater). Each was cancelled before publishing; its charge was refunded |
| 507 | * once by end_activation. */ |
| 508 | uint64_t deadline_cancelled; |
| 509 | } RxStats; |
| 510 | |
| 511 | /* R15 timing sample, recorded only when a timing buffer is installed |
| 512 | * (rx_world_set_timing). Wall-clock ns, CLOCK_MONOTONIC. */ |
| 513 | typedef struct { |
| 514 | uint32_t reaction; |
| 515 | uint32_t outcome; /* RxCrumbKind of the activation */ |
| 516 | uint64_t cause; /* crumb that woke it */ |
| 517 | uint64_t t_demand; /* wake accepted (DORMANT -> waiting) */ |
| 518 | uint64_t t_ready; /* admitted: placed on a ready ring */ |
| 519 | uint64_t t_run; /* run_one entered */ |
| 520 | uint64_t t_fn_end; /* reaction function returned */ |
| 521 | uint64_t t_visible; /* publication committed and dependents woken */ |
| 522 | uint64_t sched_ns; /* wall time inside wake/admission/queue code for it */ |
| 523 | uint64_t sched_cpu_ns; /* thread CPU time inside the same code */ |
| 524 | } RxTiming; |
| 525 | |
| 526 | /* The most recent propagation wave (only while a timing buffer is installed). */ |
| 527 | typedef struct { |
| 528 | uint64_t wall_ns, cpu_ns; |
| 529 | uint64_t inspected, matched, wake_attempts, wakes_accepted; |
| 530 | uint64_t ready_inserts, coalesced, deferred, suppressed; |
| 531 | } RxPropWave; |
| 532 | |
| 533 | typedef int (*RxAuthValidateFn)(const void *ctx, RxCapRef ref, uint32_t subject, |
| 534 | uint64_t resource, uint32_t rights, RxCapEntry *out); |
| 535 | typedef int (*RxAuthInspectFn)(const void *ctx, RxCapRef ref, RxCapEntry *out); |
| 536 | |
| 537 | /* State of the R16 G6 operator emergency stop (rx_world_halt_status). */ |
| 538 | typedef struct { |
| 539 | bool halted; |
| 540 | bool restored; /* this stop was read back from the durable mark */ |
| 541 | uint64_t seq; /* stops taken (a restored stop keeps its number) */ |
| 542 | uint32_t subject; /* operator of the stop in force or last lifted */ |
| 543 | RxCapRef cap; /* the control capability that operator presented */ |
| 544 | int32_t reason; /* the operator's reason code */ |
| 545 | uint64_t crumb; /* its OPERATOR_STOP crumb (0 if the log was full) */ |
| 546 | uint64_t t_ns; /* CLOCK_REALTIME of the stop */ |
| 547 | int durable; /* 1 mark on disk; 0 no halt dir; < 0 -errno of the write */ |
| 548 | uint64_t refused; /* activations, publications, seat results, creates and retires refused */ |
| 549 | uint64_t cancelled; /* of those, activations and seat results caught by a stop, re-run on resume */ |
| 550 | } RxHaltStatus; |
| 551 | |
| 552 | /* Durable recorder (M20 Cortex). Called with the world mutex held, once per |
| 553 | * crumb, after the crumb is in the log; it reads the world and must not call |
| 554 | * back into it. It cannot veto or fail the commit it records. The one |
| 555 | * installed recorder is rx_cortex_record.c, the Cortex writer for World. */ |
| 556 | struct RxWorld; |
| 557 | typedef void (*RxRecordFn)(void *ctx, const struct RxWorld *w, const RxCrumb *k); |
| 558 | typedef void (*RxRecordReleaseFn)(void *ctx); |
| 559 | |
| 560 | /* Commit binder (COMPOSITION-2). A bound field holds a reference into a store |
| 561 | * outside the World (a J-Space branch). The binder is consulted with the world |
| 562 | * mutex held, after a publication has passed every other check (versions, |
| 563 | * authority, write set) and before anything in it becomes visible: |
| 564 | * |
| 565 | * check(each changed bound field) may refuse: nothing is visible, the |
| 566 | * activation ends REJECTED with |
| 567 | * RX_ERR_BINDING, abort() runs for every |
| 568 | * bound value the publication proposed |
| 569 | * bind(each changed bound field) the binder's point of no return (J-Space |
| 570 | * seals the branch). A failure here also |
| 571 | * refuses the publication; abort() is told |
| 572 | * which values were already bound |
| 573 | * then the commit is applied old values stay in the binder's care |
| 574 | * |
| 575 | * So a World value never names a reference its store did not accept, and a |
| 576 | * refused reference never becomes a World value. The binder must not call |
| 577 | * back into the World. `subject` is the publishing reaction's subject, or the |
| 578 | * World's external subject. Up to RX_MAX_BINDERS binders per World, each |
| 579 | * owning the fields it bound (a field has at most one owner); a publication's |
| 580 | * bound fields are each checked, bound and aborted by their own binder. */ |
| 581 | typedef int (*RxBindCheckFn)(void *ctx, RxObjRef obj, uint32_t field, uint64_t old_value, |
| 582 | uint64_t new_value, uint32_t subject); |
| 583 | typedef int (*RxBindFn)(void *ctx, RxObjRef obj, uint32_t field, uint64_t old_value, |
| 584 | uint64_t new_value, uint32_t subject); |
| 585 | typedef void (*RxBindAbortFn)(void *ctx, RxObjRef obj, uint32_t field, uint64_t value, |
| 586 | uint32_t subject, int was_bound); |
| 587 | |
| 588 | typedef struct RxWorld { |
| 589 | pthread_mutex_t mu; |
| 590 | pthread_cond_t work_cv; |
| 591 | pthread_cond_t idle_cv; |
| 592 | |
| 593 | RxCapRoot *root; |
| 594 | /* When set, validation reads this view instead of the Linux oracle. |
| 595 | * The view cannot mint. The Linux oracle stays available for comparison. */ |
| 596 | const void *auth_ctx; |
| 597 | RxAuthValidateFn auth_validate; |
| 598 | RxAuthInspectFn auth_inspect; |
| 599 | uint32_t external_subject; |
| 600 | |
| 601 | RxObject objects[RX_MAX_OBJECTS]; |
| 602 | RxSub *subs[RX_MAX_OBJECTS]; |
| 603 | uint32_t n_subs[RX_MAX_OBJECTS]; |
| 604 | |
| 605 | RxReaction reactions[RX_MAX_REACTIONS]; |
| 606 | uint32_t n_reactions; |
| 607 | |
| 608 | /* Physical admission: bounded per-priority FIFO rings. */ |
| 609 | uint32_t ready_q[RX_PRIORITY_CLASSES][RX_MAX_REACTIONS]; |
| 610 | uint32_t ready_head[RX_PRIORITY_CLASSES]; |
| 611 | uint32_t ready_len[RX_PRIORITY_CLASSES]; |
| 612 | uint32_t in_flight; /* READY + RUNNING + PUBLISHING */ |
| 613 | |
| 614 | RxResourceBudget budget; |
| 615 | RxStabilityBudget stability; |
| 616 | uint32_t used_slots; |
| 617 | uint64_t used_memory; |
| 618 | uint64_t used_energy; |
| 619 | uint32_t admit_debt[RX_PRIORITY_CLASSES]; |
| 620 | uint32_t run_debt[RX_PRIORITY_CLASSES]; |
| 621 | uint64_t admit_seq; |
| 622 | uint32_t peak_slots; |
| 623 | uint32_t peak_blocked; |
| 624 | |
| 625 | /* Fan-out limiting delays excess valid wakes; it never drops semantic work. */ |
| 626 | struct { uint32_t reaction; uint64_t cause; } *deferred; |
| 627 | uint32_t deferred_head; |
| 628 | uint32_t deferred_len; |
| 629 | uint32_t deferred_cap; |
| 630 | |
| 631 | RxCrumb *crumbs; |
| 632 | uint64_t n_crumbs; |
| 633 | uint64_t crumb_cap; |
| 634 | |
| 635 | pthread_t workers[RX_MAX_WORKERS]; |
| 636 | uint32_t n_workers; |
| 637 | bool stopping; |
| 638 | |
| 639 | /* CPU image of OMEGA_SHARED_WORLD_V1. The 32-byte records are a projection |
| 640 | * of objects[], not an allocator of their own. */ |
| 641 | uint8_t *coherent; |
| 642 | uint64_t coherent_bytes; |
| 643 | uint32_t world_epoch; |
| 644 | bool resident_enabled; /* a resident seat may take claims on this image */ |
| 645 | bool coherent_borrowed; /* image memory is not freed with the world */ |
| 646 | bool resident_stopped; |
| 647 | /* Incarnation of the resident seat. Every claim carries it. A seat takes |
| 648 | * only claims of its own incarnation, and the processor accepts only |
| 649 | * results of the current one. It moves when a seat is lost; object |
| 650 | * generations do not. */ |
| 651 | uint32_t seat_generation; |
| 652 | |
| 653 | RxStats stats; |
| 654 | uint32_t n_deferred; /* activations waiting for rx_world_resume */ |
| 655 | /* Signalled when a claim is posted to the resident seat, so a completion |
| 656 | * transport can sleep while no graphics work is outstanding. */ |
| 657 | pthread_cond_t claim_cv; |
| 658 | |
| 659 | /* R15 timing buffer (off when null). */ |
| 660 | RxTiming *timing; |
| 661 | uint64_t timing_cap, n_timing; /* n_timing counts attempts, even past cap */ |
| 662 | RxPropWave last_prop; /* most recent propagate() */ |
| 663 | uint32_t sched_nest; /* timed scheduling sections open (count once) */ |
| 664 | |
| 665 | /* Sequential reference (never production): no workers, no dependency |
| 666 | * wakes; rx_seq_pulse decides what runs. */ |
| 667 | bool sequential; |
| 668 | uint64_t seq_pulse_started, seq_idle_start; |
| 669 | |
| 670 | /* R16 C5 caller enrollments (rx_caller.h). Only digests are kept. Own |
| 671 | * lock: taken inside mu (activation) and alone (R9's check). */ |
| 672 | pthread_mutex_t callers_mu; |
| 673 | struct { |
| 674 | uint32_t subject; |
| 675 | bool live; |
| 676 | uint64_t generation; |
| 677 | uint8_t digest[32]; |
| 678 | } callers[RX_CALLER_MAX]; |
| 679 | uint32_t n_callers; |
| 680 | uint64_t caller_generation; /* last generation issued */ |
| 681 | bool callers_bound; /* one way: set once, never cleared */ |
| 682 | |
| 683 | /* R16 G6 operator emergency stop (spec/r16-operator-emergency-stop.md). |
| 684 | * Written under mu and callers_mu together, so either lock reads it. |
| 685 | * No worker takes work and nothing publishes while it is set. */ |
| 686 | bool halted; |
| 687 | RxHaltStatus halt; |
| 688 | char halt_dir[384]; /* durable mark directory; "" = none */ |
| 689 | |
| 690 | /* M20 durable recorder (rx_world_set_recorder); at most one. */ |
| 691 | RxRecordFn recorder; |
| 692 | RxRecordReleaseFn recorder_release; |
| 693 | void *recorder_ctx; |
| 694 | |
| 695 | /* COMPOSITION-2 commit binders (rx_world_set_binder); up to |
| 696 | * RX_MAX_BINDERS. bind_mask[slot]: bound fields of that object; |
| 697 | * bind_owner[slot]: index into binder[] of the binder owning them. */ |
| 698 | struct { |
| 699 | RxBindCheckFn check; |
| 700 | RxBindFn bind; |
| 701 | RxBindAbortFn abort_fn; |
| 702 | void *ctx; |
| 703 | } binder[RX_MAX_BINDERS]; |
| 704 | uint32_t n_binders; |
| 705 | uint8_t bind_mask[RX_MAX_OBJECTS]; |
| 706 | uint8_t bind_owner[RX_MAX_OBJECTS]; |
| 707 | } RxWorld; |
| 708 | |
| 709 | int rx_world_init(RxWorld *w, RxCapRoot *root, uint32_t n_workers, uint64_t crumb_cap); |
| 710 | /* `root` may be null when `validate` is set. The Linux oracle is unchanged |
| 711 | * when `validate` is null. */ |
| 712 | int rx_world_init_with_auth(RxWorld *w, RxCapRoot *root, const void *auth_ctx, |
| 713 | RxAuthValidateFn validate, RxAuthInspectFn inspect, |
| 714 | uint32_t n_workers, uint64_t crumb_cap); |
| 715 | int rx_world_validate_cap(const RxWorld *w, RxCapRef ref, uint32_t subject, |
| 716 | uint64_t resource, uint32_t rights, RxCapEntry *out); |
| 717 | int rx_world_inspect_cap(const RxWorld *w, RxCapRef ref, RxCapEntry *out); |
| 718 | struct AienosCapView; |
| 719 | int rx_world_init_native(RxWorld *w, const struct AienosCapView *view, |
| 720 | uint32_t n_workers, uint64_t crumb_cap); |
| 721 | void rx_world_destroy(RxWorld *w); |
| 722 | /* Install the durable recorder. With `replay`, every crumb already in the |
| 723 | * log is handed to it first, under the same lock, so nothing recorded before |
| 724 | * installation is missed. RX_ERR_EXISTS if one is installed. `release` (may |
| 725 | * be null) is called by rx_world_destroy if the recorder is still installed. */ |
| 726 | int rx_world_set_recorder(RxWorld *w, RxRecordFn fn, RxRecordReleaseFn release, void *ctx, |
| 727 | bool replay); |
| 728 | /* Remove the recorder installed with `ctx` (RX_ERR_NOT_FOUND otherwise). |
| 729 | * release is not called. */ |
| 730 | int rx_world_clear_recorder(RxWorld *w, void *ctx); |
| 731 | /* COMPOSITION-2: install a commit binder (RX_ERR_EXISTS if one with `ctx` is |
| 732 | * installed, RX_ERR_FULL past RX_MAX_BINDERS) and name the fields it owns. |
| 733 | * Binding a field of a stale object is RX_ERR_STALE_GEN; a field another |
| 734 | * binder owns is RX_ERR_EXISTS; without a binder for `ctx`, RX_ERR_NOT_FOUND. |
| 735 | * clear removes the binder of `ctx` and every field it bound. */ |
| 736 | int rx_world_set_binder(RxWorld *w, RxBindCheckFn check, RxBindFn bind, RxBindAbortFn abort_fn, |
| 737 | void *ctx); |
| 738 | int rx_world_bind_field(RxWorld *w, void *ctx, RxObjRef obj, uint32_t field); |
| 739 | int rx_world_clear_binder(RxWorld *w, void *ctx); |
| 740 | uint32_t rx_world_binder_count(RxWorld *w); |
| 741 | /* R15: record an RxTiming per activation into `buf` (cap entries; later ones |
| 742 | * are dropped and counted in n_timing beyond cap). Null turns it off. */ |
| 743 | void rx_world_set_timing(RxWorld *w, RxTiming *buf, uint64_t cap); |
| 744 | /* Samples stored and samples attempted since rx_world_set_timing. Returns |
| 745 | * RX_ERR_FULL when attempted > stored: statistics from that buffer are |
| 746 | * truncated and must not be reduced. */ |
| 747 | int rx_world_timing_status(RxWorld *w, uint64_t *stored, uint64_t *attempted); |
| 748 | /* 1 in production builds; 0 only in the R15 measurement build that skips |
| 749 | * causal digests (-DRX_MEASURE_NO_CAUSAL_DIGEST). */ |
| 750 | extern const int rx_world_causal_digest_enabled; |
| 751 | void rx_world_set_resources(RxWorld *w, const RxResourceBudget *budget); |
| 752 | void rx_world_set_stability(RxWorld *w, const RxStabilityBudget *stability); |
| 753 | |
| 754 | /* Object lifecycle. Creation writes a CREATE crumb. Retiring bumps the slot |
| 755 | * generation, so every outstanding RxObjRef to it becomes stale. */ |
| 756 | int rx_world_create(RxWorld *w, uint32_t type, RxPersist persist, uint64_t resource, |
| 757 | const uint64_t init[RX_MAX_FIELDS], RxObjRef *out); |
| 758 | int rx_world_retire(RxWorld *w, RxObjRef ref); |
| 759 | |
| 760 | /* Register a reaction; subscriptions go into the dependency index. In a |
| 761 | * world with bound callers, d->caller must be the credential issued for |
| 762 | * d->subject, or the call returns RX_ERR_IDENTITY and registers nothing. */ |
| 763 | int rx_world_add_reaction(RxWorld *w, const RxReactionDesc *d, uint32_t *out_id); |
| 764 | /* The same with d->caller taken from `keys` by d->subject (none when the |
| 765 | * keyring has no entry). The caller's descriptor is not modified. */ |
| 766 | int rx_world_add_reaction_keyed(RxWorld *w, const RxCallerKeyring *keys, |
| 767 | const RxReactionDesc *d, uint32_t *out_id); |
| 768 | /* Take a reaction out of the World. Allowed only while it is DORMANT with |
| 769 | * nothing pending (not re-armed, parked, deferred, resume-pending, holding |
| 770 | * resources, nor waiting in the fan-out backlog): RX_ERR_BUSY otherwise, |
| 771 | * nothing changed. Its subscriptions leave the dependency index, it is never |
| 772 | * woken or run again (demand ignores it), and its table slot is kept for |
| 773 | * reuse: rx_world_add_reaction fills a removed slot before growing the table, |
| 774 | * but only for a reaction with the same subject and faculty, so a crumb's |
| 775 | * reaction id keeps naming the principal that wrote it |
| 776 | * (rx_world_crumb_origin). RX_ERR_NOT_FOUND if out of range or removed. */ |
| 777 | int rx_world_remove_reaction(RxWorld *w, uint32_t rid); |
| 778 | |
| 779 | /* What the World holds right now, for leak checks (read under the lock). |
| 780 | * The causal crumb log is history (append-only by design) and is not here. */ |
| 781 | typedef struct { |
| 782 | uint32_t reaction_slots; /* n_reactions: table slots ever used */ |
| 783 | uint32_t reactions_active; /* registered, not removed */ |
| 784 | uint32_t reactions_removed; /* removed slots waiting for reuse */ |
| 785 | uint64_t subscriptions; /* entries in the dependency index */ |
| 786 | uint32_t objects_live; |
| 787 | uint32_t binders; |
| 788 | uint32_t bound_fields; |
| 789 | uint32_t in_flight; |
| 790 | uint32_t fanout_backlog; |
| 791 | uint32_t deferred; |
| 792 | uint32_t used_slots; |
| 793 | uint64_t used_memory, used_energy; |
| 794 | } RxFootprint; |
| 795 | void rx_world_footprint(RxWorld *w, RxFootprint *out); |
| 796 | |
| 797 | /* R16 C5 caller credentials (rx_caller.h). |
| 798 | * enroll: mint a credential for `subject` (RX_CALLER_ERR_EXISTS if it has |
| 799 | * one, RX_CALLER_ERR_CLOSED once bound, RX_CALLER_ERR_FULL past |
| 800 | * RX_CALLER_MAX). The secret is returned only here. |
| 801 | * bind: from now on every reaction registration, activation and commit |
| 802 | * checks the subject's credential. One way; enrollment closes. Refused |
| 803 | * (RX_ERR_IDENTITY, world left unbound) while any registered reaction was |
| 804 | * admitted without a credential (C7): before binding, a reaction that names |
| 805 | * a credential is fully checked, and one that names none is kept as |
| 806 | * unauthenticated and can never run in a bound world. |
| 807 | * revoke: retire `subject`'s enrollment; needs its current credential, so no |
| 808 | * caller can revoke another's identity. Its reactions are then blocked. |
| 809 | * Takes the world lock (C7), so it never lands between a commit's identity |
| 810 | * check and its publish: it precedes the check or follows the publish. |
| 811 | * check: RX_CALLER_OK or the RX_CALLER_ERR_* reason. Constant-time compare. |
| 812 | * check_fn: the same, shaped for rx_gen_bind_authority (ctx is the world), |
| 813 | * with `op` one of RX_CALLER_OP_CHECK, _HOLD (on success the enrollment |
| 814 | * table stays locked, so no revocation lands, until _RELEASE). */ |
| 815 | int rx_world_enroll_caller(RxWorld *w, uint32_t subject, RxCallerCred *out); |
| 816 | int rx_world_bind_callers(RxWorld *w); |
| 817 | int rx_world_revoke_caller(RxWorld *w, uint32_t subject, const RxCallerCred *cred); |
| 818 | int rx_world_check_caller(RxWorld *w, uint32_t subject, const RxCallerCred *cred); |
| 819 | int rx_world_caller_check_fn(void *world, uint32_t subject, const RxCallerCred *cred, int op); |
| 820 | |
| 821 | /* ---- R16 G6 operator emergency stop (spec/r16-operator-emergency-stop.md) ---- |
| 822 | * Authority: the caller credential of `subject` (rx_world_enroll_caller) AND a |
| 823 | * capability for `subject` on RX_WORLD_RES_CONTROL carrying RX_WORLD_RIGHT_HALT, |
| 824 | * validated by the world's authority. A subject any registered reaction runs |
| 825 | * under is refused: no reaction can stop or resume the world it runs in. |
| 826 | * Stop: no worker takes work, a computed activation is refused at its commit |
| 827 | * (CANCELLED, RX_ERR_HALTED) and re-run on resume, outside publications and |
| 828 | * seat results are refused, object create and retire are refused, and the |
| 829 | * caller check refuses so a bound generation store neither proposes nor |
| 830 | * promotes. A refused activation does not count toward the episode budget. Work already committing |
| 831 | * finishes first (the stop waits for the world lock). A durable mark is |
| 832 | * written when a halt directory is set; the stop takes effect even if that |
| 833 | * write fails (`durable` < 0 says so). Never call with mu held. */ |
| 834 | #define RX_WORLD_RES_CONTROL 0x906ull /* the world's control resource */ |
| 835 | #define RX_WORLD_RIGHT_HALT RX_RIGHT_EPOCH /* the right that may void every |
| 836 | capability may also pause the world */ |
| 837 | #define RX_HALT_ALREADY 1 /* stop: already stopped, nothing changed */ |
| 838 | #define RX_HALT_NOT_STOPPED 2 /* resume: not stopped, nothing changed */ |
| 839 | /* A dir that src/omega_path_check.h refuses (a ".." component, a byte outside |
| 840 | * its set, too long) is RX_ERR_ARG, and the world's halt directory is unchanged. */ |
| 841 | int rx_world_set_halt_dir(RxWorld *w, const char *dir); |
| 842 | int rx_world_emergency_stop(RxWorld *w, uint32_t subject, const RxCallerCred *cred, |
| 843 | RxCapRef cap, int32_t reason, RxHaltStatus *out); |
| 844 | int rx_world_emergency_resume(RxWorld *w, uint32_t subject, const RxCallerCred *cred, |
| 845 | RxCapRef cap, RxHaltStatus *out); |
| 846 | void rx_world_halt_status(RxWorld *w, RxHaltStatus *out); |
| 847 | /* The same authority check as stop and resume (credential, not a reaction |
| 848 | * subject, live control capability), without acting: RX_OK, RX_ERR_IDENTITY |
| 849 | * or RX_ERR_AUTHORITY. For operator requests that read or retire the |
| 850 | * operator's own authority (docs/r16-operator-control.md). Never call with mu held. */ |
| 851 | int rx_world_operator_authorize(RxWorld *w, uint32_t subject, const RxCallerCred *cred, RxCapRef cap); |
| 852 | |
| 853 | /* A stimulus from outside the organism (sensor, human input). Requires a |
| 854 | * capability for (external_subject, object resource, WRITE). Returns the |
| 855 | * causal id of the EXTERNAL crumb, or a negative error. */ |
| 856 | int64_t rx_world_publish_external(RxWorld *w, RxCapRef cap, |
| 857 | const RxMutation *muts, uint32_t n); |
| 858 | |
| 859 | /* Wait until no reaction is READY/RUNNING/PUBLISHING. Observation only. */ |
| 860 | int rx_world_wait_quiescent(RxWorld *w, int timeout_ms); |
| 861 | |
| 862 | /* Read-only inspection (takes the world lock). */ |
| 863 | int rx_world_read(RxWorld *w, RxObjRef ref, RxObject *out); |
| 864 | const RxCrumb *rx_world_crumb(const RxWorld *w, uint64_t id); |
| 865 | /* Who published crumb `id`: its reaction (UINT32_MAX for outside), that |
| 866 | * reaction's subject (the external subject for outside) and faculty. RX_OK |
| 867 | * for a commit or an outside publication, 1 for the object's creation (the |
| 868 | * field still holds its initial value), negative otherwise. Takes the world |
| 869 | * lock. */ |
| 870 | int rx_world_crumb_origin(RxWorld *w, uint64_t id, uint32_t *reaction, uint32_t *subject, |
| 871 | uint32_t *faculty); |
| 872 | /* Causal id of the publication that last wrote obj.field. */ |
| 873 | uint64_t rx_world_explain(RxWorld *w, RxObjRef ref, uint32_t field); |
| 874 | /* Recompute every crumb digest and check parent links; 0 on success. */ |
| 875 | int rx_world_verify_crumbs(RxWorld *w, uint64_t *out_checked); |
| 876 | /* Digest a crumb exactly as the log does (parents are read from w). Binds |
| 877 | * every capability reference in full, including all 64 generation bits. */ |
| 878 | void rx_world_crumb_digest(const RxWorld *w, const RxCrumb *k, uint8_t out[32]); |
| 879 | /* SHA-256 over all live objects' (id, generation, content digest). */ |
| 880 | void rx_world_digest(RxWorld *w, uint8_t out[32]); |
| 881 | |
| 882 | /* Record which capability reference speaks for this object. Does not grant rights. */ |
| 883 | int rx_world_bind_capability(RxWorld *w, RxObjRef ref, RxCapRef cap); |
| 884 | |
| 885 | /* Copy the 32-byte physical projection. Generation is the canonical one. |
| 886 | * RX_ERR_UNPLACED means this generation has no realization. */ |
| 887 | int rx_world_physical(RxWorld *w, RxObjRef ref, OmegaSharedWorldObject *out); |
| 888 | |
| 889 | /* Bind or drop the coherent window for this generation. Neither call |
| 890 | * allocates an id or advances the generation. A second attach is refused. |
| 891 | * A reference whose generation is not current is refused and changes nothing. */ |
| 892 | int rx_world_attach_physical(RxWorld *w, RxObjRef ref); |
| 893 | int rx_world_detach_physical(RxWorld *w, RxObjRef ref); |
| 894 | |
| 895 | /* Move the bytes of an attached object onto another free window. |
| 896 | * The id, generation, version, and content digest stay put. */ |
| 897 | int rx_world_relocate_physical(RxWorld *w, RxObjRef ref); |
| 898 | |
| 899 | /* Place the object at an exact region offset. Used to refuse a window that |
| 900 | * does not fit. A refused call leaves the object where it was. */ |
| 901 | int rx_world_place_physical(RxWorld *w, RxObjRef ref, uint64_t offset, uint64_t length); |
| 902 | |
| 903 | /* Hostile-writer seam: store bytes into the physical projection only. |
| 904 | * Does not change the canonical object, its generation, or its capability. */ |
| 905 | int rx_world_overwrite_physical(RxWorld *w, uint32_t id, const OmegaSharedWorldObject *src); |
| 906 | |
| 907 | /* Validate one descriptor against the canonical object and the capability root. |
| 908 | * expected_sequence is the next ring sequence this descriptor must carry. |
| 909 | * *out_fault receives a mailbox or RX_FAULT_* code. */ |
| 910 | int rx_world_check_descriptor(RxWorld *w, const OmegaSharedWorldDesc *desc, |
| 911 | uint64_t expected_sequence, uint32_t *out_fault); |
| 912 | |
| 913 | /* Copy the next publication-ring slot and validate it. A hostile slot is |
| 914 | * consumed and rejected so the ring still moves forward. */ |
| 915 | int rx_world_take_publication(RxWorld *w, OmegaSharedWorldDesc *out, uint32_t *out_fault); |
| 916 | |
| 917 | /* Place a raw descriptor on the publication ring without correcting it. |
| 918 | * The next take decides whether it is acceptable. */ |
| 919 | int rx_world_inject_descriptor(RxWorld *w, const OmegaSharedWorldDesc *desc); |
| 920 | |
| 921 | /* Next publication sequence a producer would stamp. */ |
| 922 | uint64_t rx_world_publication_tail(RxWorld *w); |
| 923 | |
| 924 | /* Byte offset of physical record 0 inside the coherent image. */ |
| 925 | uint64_t rx_world_physical_table_offset(void); |
| 926 | |
| 927 | /* Fill checksum and the frozen magic/version. Does not invent an identity. */ |
| 928 | void rx_world_seal_descriptor(OmegaSharedWorldDesc *desc); |
| 929 | |
| 930 | /* Allow a resident seat on this image. The seat's one operation is fixed: |
| 931 | * it adds field 0 and field 1 of the object it reads (low 32 bits, the same |
| 932 | * integer add the qualified vector add uses) and writes the sum into field 0 |
| 933 | * of a second object. The notice names both objects. It does not carry code. */ |
| 934 | int rx_world_enable_resident(RxWorld *w); |
| 935 | |
| 936 | /* Stand-in for the graphics seat, on the same image. One call consumes |
| 937 | * notices until it has handled one claim, a shutdown, or the ring is empty. |
| 938 | * Returns 1 if it wrote a result notice, 0 if there was nothing to do, |
| 939 | * 2 if it saw shutdown, negative on a refused claim (a fault notice was |
| 940 | * still published). */ |
| 941 | int rx_resident_seat_step(RxWorld *w); |
| 942 | |
| 943 | /* Caller already holds the world lock. Posts one claim: read placed object |
| 944 | * `in`, write placed object `out`. The notice names both objects, their |
| 945 | * generations, and their bound capabilities. It does not carry code. */ |
| 946 | int rx_resident_post_claim(RxWorld *w, uint32_t in, uint32_t out, uint64_t parent, |
| 947 | uint64_t *seq_out); |
| 948 | |
| 949 | /* Clear the seat's stopped flag. Does not move either ring. */ |
| 950 | int rx_resident_reset(RxWorld *w); |
| 951 | |
| 952 | /* The durable executor finished the effect reaction `reaction` deferred on |
| 953 | * (RX_FN_DEFER): run that activation again. Safe from any thread. Decides |
| 954 | * nothing; the reaction re-reads the world and the executor's result. */ |
| 955 | int rx_world_resume(RxWorld *w, uint32_t reaction); |
| 956 | |
| 957 | /* Take one result notice and, when it is valid, publish it into the canonical |
| 958 | * object. A dependent reaction wakes from that publication. */ |
| 959 | int rx_resident_accept(RxWorld *w); |
| 960 | |
| 961 | /* Block until at least one resident claim is outstanding (posted and not yet |
| 962 | * closed) or timeout_ms passes. Transport only: it decides nothing. Returns |
| 963 | * RX_OK when work is outstanding, RX_ERR_TIMEOUT otherwise. */ |
| 964 | int rx_resident_wait_outstanding(RxWorld *w, int timeout_ms); |
| 965 | |
| 966 | /* Ask the seat to leave. The object world stays. */ |
| 967 | int rx_resident_shutdown(RxWorld *w); |
| 968 | |
| 969 | /* The seat is gone without leaving: killed, or its channel reset. The caller |
| 970 | * guarantees the chip no longer writes the image. Claims the old seat never |
| 971 | * took and results nobody accepted are discarded, and the seat generation |
| 972 | * moves. Every claim the seat still held ends FAILED with RX_ERR_SEAT_LOST: |
| 973 | * the output window is restored from the canonical object, the resource |
| 974 | * charge is released, and a crumb records the loss. With retry, each of those |
| 975 | * activations is made ready again, caused by its failure crumb, and its new |
| 976 | * claim carries the new seat generation. Returns the number of claims lost. */ |
| 977 | int rx_resident_seat_lost(RxWorld *w, int retry); |
| 978 | /* Caller holds the world lock. Copies one graphics-to-processor result and |
| 979 | * moves that ring forward. */ |
| 980 | int rx_resident_take_result(RxWorld *w, OmegaSharedWorldDesc *out); |
| 981 | |
| 982 | /* Point the one image at caller memory of the same size. borrowed means the |
| 983 | * world will not free it. */ |
| 984 | int rx_world_bind_coherent(RxWorld *w, void *mem, uint64_t bytes, int borrowed); |
| 985 | |
| 986 | uint64_t rx_world_off_c2g(void); |
| 987 | uint64_t rx_world_off_g2c(void); |
| 988 | uint64_t rx_world_off_fault(void); |
| 989 | uint64_t rx_world_off_object_table(void); |
| 990 | uint64_t rx_world_off_heartbeat(void); |
| 991 | |
| 992 | /* Caller holds the world lock. Projection and publication of the same object. */ |
| 993 | int rx_coherent_format(RxWorld *w); |
| 994 | void rx_coherent_free(RxWorld *w); |
| 995 | void rx_coherent_project(RxWorld *w, uint32_t id); |
| 996 | void rx_coherent_publish(RxWorld *w, uint32_t id, uint64_t crumb_id); |
| 997 | |
| 998 | bool rx_state_transition_legal(RxState from, RxState to); |
| 999 | const char *rx_state_name(RxState s); |
| 1000 | const char *rx_crumb_kind_name(RxCrumbKind k); |
| 1001 | |
| 1002 | #endif /* RX_WORLD_H */ |