interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / runtime / rx_coherent.c

922 lines · 35070 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/*
18 * rx_coherent.c -- physical projection of the canonical reaction object.
19 *
20 * RxObject is the only identity. The bytes in the coherent image are the
21 * frozen 32-byte OMEGA_SHARED_WORLD_V1 record plus the object's field window.
22 * This file does not allocate a second id or a second generation. The
23 * permission word in the frozen record is written as zero and is never read
24 * as authority. The graphics-processor worker is not started here.
25 *
26 * The frozen header's object table holds 64 records. This image holds one
27 * 32-byte slot per host object. That slot is a projection, not an identity.
28 * A live object may have no window. The 32-byte record and the 128-byte
29 * descriptor are unchanged. The physics m20 sources are not edited.
30 */
31#include "rx_world.h"
32#include "omega_native.h"
33
34#include <stdatomic.h>
35#include <stdlib.h>
36#include <string.h>
37
38#define ALIGN_UP(x, a) (((x) + ((uint64_t)(a) - 1u)) & ~((uint64_t)(a) - 1u))
39
40typedef struct {
41 uint32_t count;
42 uint32_t world_epoch;
43 uint8_t _pad[OMEGA_SW_CACHELINE - 8u];
44 OmegaSharedWorldObject objects[RX_MAX_OBJECTS];
45} RxProjectedTable;
46
47_Static_assert(sizeof(OmegaSharedWorldObject) == 32, "physical record must stay 32 bytes");
48_Static_assert(sizeof(OmegaSharedWorldDesc) == 128, "descriptor must stay 128 bytes");
49_Static_assert(offsetof(RxProjectedTable, objects) == OMEGA_SW_CACHELINE,
50 "projection table entries follow one cache line");
51_Static_assert(RX_OBJECT_WINDOW == RX_MAX_FIELDS * sizeof(uint64_t),
52 "window holds the field values and nothing else");
53
54static uint64_t off_header(void) { return 0; }
55static uint64_t off_c2g(void) {
56 return ALIGN_UP(sizeof(OmegaSharedWorldHeader), OMEGA_SW_CACHELINE);
57}
58static uint64_t off_g2c(void) {
59 return ALIGN_UP(off_c2g() + sizeof(OmegaSharedWorldRing), OMEGA_SW_CACHELINE);
60}
61static uint64_t off_fault(void) {
62 return ALIGN_UP(off_g2c() + sizeof(OmegaSharedWorldRing), OMEGA_SW_CACHELINE);
63}
64static uint64_t off_objtbl(void) {
65 return ALIGN_UP(off_fault() + sizeof(OmegaSharedWorldFaultMailbox), OMEGA_SW_CACHELINE);
66}
67static uint64_t off_payload(void) {
68 return ALIGN_UP(off_objtbl() + sizeof(RxProjectedTable), OMEGA_SW_CACHELINE);
69}
70static uint64_t off_heartbeat(void) {
71 return ALIGN_UP(off_payload() + (uint64_t)RX_PHYS_WINDOWS * RX_OBJECT_WINDOW,
72 OMEGA_SW_CACHELINE);
73}
74static uint64_t image_bytes(void) {
75 return off_heartbeat() + OMEGA_SW_CACHELINE;
76}
77
78uint64_t rx_world_off_c2g(void) { return off_c2g(); }
79uint64_t rx_world_off_g2c(void) { return off_g2c(); }
80uint64_t rx_world_off_fault(void) { return off_fault(); }
81uint64_t rx_world_off_object_table(void) { return off_objtbl(); }
82uint64_t rx_world_off_heartbeat(void) { return off_heartbeat(); }
83
84static uint64_t window_offset(uint32_t win) {
85 return off_payload() + (uint64_t)win * RX_OBJECT_WINDOW;
86}
87
88uint64_t rx_world_physical_table_offset(void) {
89 return off_objtbl() + offsetof(RxProjectedTable, objects);
90}
91
92static uint64_t load_acquire_u64(volatile uint64_t *p) {
93 return atomic_load_explicit((_Atomic uint64_t *)p, memory_order_acquire);
94}
95static uint64_t load_relaxed_u64(volatile uint64_t *p) {
96 return atomic_load_explicit((_Atomic uint64_t *)p, memory_order_relaxed);
97}
98static void store_release_u64(volatile uint64_t *p, uint64_t v) {
99 atomic_store_explicit((_Atomic uint64_t *)p, v, memory_order_release);
100}
101static void store_release_u32(volatile uint32_t *p, uint32_t v) {
102 atomic_store_explicit((_Atomic uint32_t *)p, v, memory_order_release);
103}
104
105static uint32_t crc32c(const uint8_t *data, size_t len) {
106 uint64_t r = 0;
107 (void)omega2_rx_coh_crc32c(&r, data, len, (uint64_t)len);
108 return (uint32_t)r;
109}
110
111static uint32_t desc_checksum(const OmegaSharedWorldDesc *desc) {
112 uint8_t buf[0x3C + 64];
113 memcpy(buf, desc, 0x3C);
114 memcpy(buf + 0x3C, desc->payload, 64);
115 return crc32c(buf, sizeof(buf));
116}
117
118void rx_world_seal_descriptor(OmegaSharedWorldDesc *desc) {
119 desc->magic = OMEGA_SW_MAGIC;
120 desc->abi_version = OMEGA_SW_ABI_VERSION;
121 desc->flags = (uint16_t)(desc->flags | OMEGA_SW_FLAG_CHECKSUM | OMEGA_SW_FLAG_OBJECT_REF);
122 desc->checksum = 0;
123 desc->checksum = desc_checksum(desc);
124}
125
126static OmegaSharedWorldHeader *hdr_of(RxWorld *w) {
127 return (OmegaSharedWorldHeader *)(w->coherent + off_header());
128}
129static OmegaSharedWorldRing *pub_ring(RxWorld *w) {
130 return (OmegaSharedWorldRing *)(w->coherent + off_c2g());
131}
132static OmegaSharedWorldFaultMailbox *fault_of(RxWorld *w) {
133 return (OmegaSharedWorldFaultMailbox *)(w->coherent + off_fault());
134}
135static RxProjectedTable *table_of(RxWorld *w) {
136 return (RxProjectedTable *)(w->coherent + off_objtbl());
137}
138
139static void put_u64(uint8_t *p, uint64_t v) {
140 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i));
141}
142static uint64_t get_u64(const uint8_t *p) {
143 uint64_t v = 0;
144 for (int i = 0; i < 8; i++) v |= (uint64_t)p[i] << (8 * i);
145 return v;
146}
147static void put_u32(uint8_t *p, uint32_t v) {
148 p[0] = (uint8_t)v;
149 p[1] = (uint8_t)(v >> 8);
150 p[2] = (uint8_t)(v >> 16);
151 p[3] = (uint8_t)(v >> 24);
152}
153static uint32_t get_u32(const uint8_t *p) {
154 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
155}
156
157static int ring_msg(uint16_t t) {
158 uint64_t r = 0;
159 (void)omega2_rx_coh_ring_msg(&r, (uint64_t)t);
160 return r != 0;
161}
162
163/* A claim names the object the seat reads; the object it writes is named in
164 * the payload and its write right is checked at publication. */
165static uint32_t msg_rights(uint16_t t) {
166 uint64_t r = 0;
167 (void)omega2_rx_coh_msg_rights(&r, (uint64_t)t);
168 return (uint32_t)r;
169}
170
171static void raise_fault(RxWorld *w, uint32_t code, uint64_t sequence, uint32_t a, uint32_t b) {
172 OmegaSharedWorldFaultMailbox *m = fault_of(w);
173 m->world_epoch = w->world_epoch;
174 m->fault_sequence = sequence;
175 m->detail_a = a;
176 m->detail_b = b;
177 store_release_u32(&m->fault_code, code);
178}
179
180static int fault_err(uint32_t fault) {
181 uint64_t r = 0;
182 (void)omega2_rx_coh_fault_err(&r, (uint64_t)fault);
183 return (int)(int64_t)r;
184}
185
186int rx_coherent_format(RxWorld *w) {
187 uint64_t bytes = image_bytes();
188 void *mem = NULL;
189 if (posix_memalign(&mem, OMEGA_SW_CACHELINE, (size_t)bytes) != 0) return RX_ERR_FULL;
190 memset(mem, 0, (size_t)bytes);
191 w->coherent = mem;
192 w->coherent_bytes = bytes;
193 w->world_epoch = 1;
194
195 OmegaSharedWorldRing *c2g = pub_ring(w);
196 OmegaSharedWorldRing *g2c = (OmegaSharedWorldRing *)(w->coherent + off_g2c());
197 for (int which = 0; which < 2; which++) {
198 OmegaSharedWorldRing *r = which ? g2c : c2g;
199 r->capacity = OMEGA_SW_RING_CAPACITY;
200 r->mask = OMEGA_SW_RING_MASK;
201 r->world_epoch = w->world_epoch;
202 r->ring_generation = 1;
203 }
204 fault_of(w)->world_epoch = w->world_epoch;
205
206 RxProjectedTable *t = table_of(w);
207 t->count = RX_MAX_OBJECTS;
208 t->world_epoch = w->world_epoch;
209 for (uint32_t i = 0; i < RX_MAX_OBJECTS; i++) {
210 w->objects[i].id = i;
211 w->objects[i].generation = 1;
212 w->objects[i].placed = false;
213 w->objects[i].window = 0;
214 w->objects[i].region_offset = 0;
215 w->objects[i].size_bytes = 0;
216 w->objects[i].placement = 0;
217 w->objects[i].locality = 0;
218 w->objects[i].coherency = 0;
219 rx_coherent_project(w, i);
220 }
221
222 OmegaSharedWorldHeader *h = hdr_of(w);
223 h->abi_version = OMEGA_SW_ABI_VERSION;
224 h->header_bytes = (uint16_t)sizeof(OmegaSharedWorldHeader);
225 h->world_epoch = w->world_epoch;
226 h->world_generation = 1;
227 h->region_bytes = bytes;
228 h->ring_capacity = OMEGA_SW_RING_CAPACITY;
229 h->desc_bytes = (uint32_t)sizeof(OmegaSharedWorldDesc);
230 h->off_cpu_to_gpu_ring = off_c2g();
231 h->off_gpu_to_cpu_ring = off_g2c();
232 h->off_fault_mailbox = off_fault();
233 h->off_object_table = off_objtbl();
234 atomic_thread_fence(memory_order_release);
235 store_release_u32(&h->magic, OMEGA_SW_MAGIC);
236 return RX_OK;
237}
238
239void rx_coherent_free(RxWorld *w) {
240 if (!w->coherent_borrowed) free(w->coherent);
241 w->coherent = NULL;
242 w->coherent_borrowed = false;
243}
244
245void rx_coherent_project(RxWorld *w, uint32_t id) {
246 if (!w->coherent || id >= RX_MAX_OBJECTS) return;
247 RxObject *o = &w->objects[id];
248 OmegaSharedWorldObject *p = &table_of(w)->objects[id];
249 /* Exactly the entry members written here, not the whole entry. */
250 w->stats.proj_bytes += sizeof p->object_id + sizeof p->generation + sizeof p->permissions +
251 sizeof p->state + sizeof p->region_offset + sizeof p->size_bytes;
252 p->object_id = o->id;
253 p->generation = o->generation;
254 p->permissions = 0;
255 if (!o->placed) {
256 p->state = OMEGA_SW_OBJ_REVOKED;
257 p->region_offset = 0;
258 p->size_bytes = 0;
259 return;
260 }
261 p->state = o->live ? OMEGA_SW_OBJ_ACTIVE : OMEGA_SW_OBJ_REVOKED;
262 p->region_offset = o->region_offset;
263 p->size_bytes = o->size_bytes;
264 if (o->region_offset < off_payload() || o->region_offset > w->coherent_bytes ||
265 o->size_bytes > w->coherent_bytes - o->region_offset)
266 return;
267 uint8_t *win = w->coherent + o->region_offset;
268 for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) put_u64(win + f * 8u, o->field[f]);
269 w->stats.proj_bytes += RX_MAX_FIELDS * 8u;
270}
271
272static bool overlap(uint64_t a, uint64_t asz, uint64_t b, uint64_t bsz) {
273 if (asz == 0 || bsz == 0) return false;
274 if (a > UINT64_MAX - asz || b > UINT64_MAX - bsz) return true;
275 return a < (b + bsz) && b < (a + asz);
276}
277
278static int check_locked(RxWorld *w, const OmegaSharedWorldDesc *desc,
279 uint64_t expected_sequence, uint32_t *out_fault) {
280 uint32_t fault = OMEGA_SW_FAULT_NONE;
281 if (!w->coherent || !desc) fault = OMEGA_SW_FAULT_BAD_MAGIC;
282 else if (desc->magic != OMEGA_SW_MAGIC) fault = OMEGA_SW_FAULT_BAD_MAGIC;
283 else if (desc->abi_version != OMEGA_SW_ABI_VERSION) fault = OMEGA_SW_FAULT_BAD_VERSION;
284 else if (desc->world_epoch != w->world_epoch) fault = OMEGA_SW_FAULT_STALE_EPOCH;
285 else if (desc->sequence != expected_sequence) fault = OMEGA_SW_FAULT_BAD_SEQUENCE;
286 else if (!ring_msg(desc->msg_type)) fault = OMEGA_SW_FAULT_BAD_MSGTYPE;
287 else if ((desc->flags & OMEGA_SW_FLAG_CHECKSUM) == 0) fault = OMEGA_SW_FAULT_BAD_CHECKSUM;
288 else {
289 OmegaSharedWorldDesc tmp = *desc;
290 uint32_t want = tmp.checksum;
291 tmp.checksum = 0;
292 if (desc_checksum(&tmp) != want) fault = OMEGA_SW_FAULT_BAD_CHECKSUM;
293 }
294
295 if (fault == OMEGA_SW_FAULT_NONE) {
296 if (desc->object_id >= RX_MAX_OBJECTS) fault = OMEGA_SW_FAULT_OOB_OBJECT;
297 else {
298 const RxObject *o = &w->objects[desc->object_id];
299 const OmegaSharedWorldObject *p = &table_of(w)->objects[desc->object_id];
300 if (desc->object_generation == 0 || desc->object_generation != o->generation)
301 fault = OMEGA_SW_FAULT_STALE_GEN;
302 else if (p->object_id != o->id || p->generation != o->generation)
303 fault = RX_FAULT_DIVERGED;
304 else if (!o->placed) {
305 fault = (p->state == OMEGA_SW_OBJ_ACTIVE || p->size_bytes != 0)
306 ? RX_FAULT_DIVERGED
307 : RX_FAULT_UNPLACED;
308 } else if (p->region_offset != o->region_offset || p->size_bytes != o->size_bytes ||
309 (o->live ? p->state != OMEGA_SW_OBJ_ACTIVE : p->state != OMEGA_SW_OBJ_REVOKED))
310 fault = RX_FAULT_DIVERGED;
311 else if (!o->live && desc->msg_type != RX_RING_FAULT)
312 fault = OMEGA_SW_FAULT_OOB_OBJECT;
313 else if (desc->object_length == 0 ||
314 (uint64_t)desc->object_offset > o->size_bytes ||
315 (uint64_t)desc->object_length > o->size_bytes - (uint64_t)desc->object_offset)
316 fault = RX_FAULT_BOUNDS;
317 else if (o->region_offset < off_payload() ||
318 o->region_offset > w->coherent_bytes ||
319 o->size_bytes > w->coherent_bytes - o->region_offset)
320 fault = RX_FAULT_BOUNDS;
321 else {
322 uint64_t abs = o->region_offset + desc->object_offset;
323 if (abs > w->coherent_bytes ||
324 (uint64_t)desc->object_length > w->coherent_bytes - abs)
325 fault = RX_FAULT_BOUNDS;
326 }
327 if (fault == OMEGA_SW_FAULT_NONE) {
328 for (uint32_t i = 0; i < RX_MAX_OBJECTS; i++) {
329 if (i == o->id || !w->objects[i].live) continue;
330 if (overlap(o->region_offset, o->size_bytes,
331 w->objects[i].region_offset, w->objects[i].size_bytes)) {
332 fault = RX_FAULT_OVERLAP;
333 break;
334 }
335 }
336 }
337 if (fault == OMEGA_SW_FAULT_NONE && o->live) {
338 const uint8_t *win = w->coherent + o->region_offset;
339 for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) {
340 if (get_u64(win + f * 8u) != o->field[f]) {
341 fault = RX_FAULT_TORN_PUB;
342 break;
343 }
344 }
345 }
346 if (fault == OMEGA_SW_FAULT_NONE && desc->msg_type == RX_RING_PUBLISH) {
347 if (desc->payload_len < 24) fault = RX_FAULT_TORN_PUB;
348 else if (get_u64(desc->payload + 8) != o->version) fault = RX_FAULT_TORN_PUB;
349 }
350 if (fault == OMEGA_SW_FAULT_NONE) {
351 if (desc->payload_len < RX_CAP_PAYLOAD) fault = RX_FAULT_CAP;
352 else {
353 RxCapRef cap = { get_u32(desc->payload),
354 get_u32(desc->payload + 4) |
355 (uint64_t)get_u32(desc->payload + RX_CAP_GEN_HI_A) << 32 };
356 if (cap.cap_id != o->cap.cap_id || cap.generation != o->cap.generation)
357 fault = RX_FAULT_CAP;
358 else {
359 RxCapEntry ent;
360 int irc = rx_world_inspect_cap(w, cap, &ent);
361 int vrc = irc == RX_CAP_OK
362 ? rx_world_validate_cap(w, cap, ent.subject, o->resource,
363 msg_rights(desc->msg_type), NULL)
364 : irc;
365 if (vrc != RX_CAP_OK) fault = RX_FAULT_CAP;
366 else if (desc->msg_type == RX_RING_PUBLISH) {
367 uint64_t cid = get_u64(desc->payload + 16);
368 const RxCrumb *k = (cid == 0 || cid > w->n_crumbs) ? NULL : &w->crumbs[cid - 1];
369 bool named = false;
370 if (k) {
371 for (uint32_t n = 0; n < k->n_outputs; n++)
372 if (k->outputs[n].obj.id == o->id &&
373 k->outputs[n].obj.generation == o->generation)
374 named = true;
375 for (uint32_t n = 0; n < k->n_inputs; n++)
376 if (k->inputs[n].obj.id == o->id &&
377 k->inputs[n].obj.generation == o->generation)
378 named = true;
379 }
380 if (!named) fault = RX_FAULT_TORN_PUB;
381 }
382 }
383 }
384 }
385 }
386 }
387 if (out_fault) *out_fault = fault;
388 return fault == OMEGA_SW_FAULT_NONE ? RX_OK : fault_err(fault);
389}
390
391int rx_world_check_descriptor(RxWorld *w, const OmegaSharedWorldDesc *desc,
392 uint64_t expected_sequence, uint32_t *out_fault) {
393 if (!w) return RX_ERR_ARG;
394 pthread_mutex_lock(&w->mu);
395 int rc = check_locked(w, desc, expected_sequence, out_fault);
396 pthread_mutex_unlock(&w->mu);
397 return rc;
398}
399
400void rx_coherent_publish(RxWorld *w, uint32_t id, uint64_t crumb_id) {
401 if (!w->coherent || id >= RX_MAX_OBJECTS) return;
402 RxObject *o = &w->objects[id];
403 if (!o->placed) return;
404 if (o->cap.cap_id == 0 && o->cap.generation == 0) return;
405 rx_coherent_project(w, id);
406 OmegaSharedWorldRing *ring = pub_ring(w);
407 uint64_t t = load_relaxed_u64(&ring->tail);
408 uint64_t h = load_acquire_u64(&ring->head);
409 if (t - h >= ring->capacity) {
410 w->stats.desc_rejected++;
411 return;
412 }
413 OmegaSharedWorldDesc d;
414 memset(&d, 0, sizeof(d));
415 d.msg_type = RX_RING_PUBLISH;
416 d.sequence = t;
417 d.world_epoch = w->world_epoch;
418 d.object_id = o->id;
419 d.object_generation = o->generation;
420 d.object_offset = 0;
421 d.object_length = (uint32_t)o->size_bytes;
422 d.payload_len = RX_CAP_PAYLOAD;
423 put_u32(d.payload, o->cap.cap_id);
424 put_u32(d.payload + 4, (uint32_t)o->cap.generation);
425 put_u32(d.payload + RX_CAP_GEN_HI_A, (uint32_t)(o->cap.generation >> 32));
426 put_u64(d.payload + 8, o->version);
427 put_u64(d.payload + 16, crumb_id);
428 rx_world_seal_descriptor(&d);
429 memcpy(&ring->slots[t & ring->mask], &d, sizeof(d));
430 store_release_u64(&ring->tail, t + 1);
431 w->stats.c2g_write_bytes += sizeof(d);
432 w->stats.desc_published++;
433}
434
435int rx_world_take_publication(RxWorld *w, OmegaSharedWorldDesc *out, uint32_t *out_fault) {
436 if (!w || !w->coherent) return RX_ERR_ARG;
437 pthread_mutex_lock(&w->mu);
438 OmegaSharedWorldRing *ring = pub_ring(w);
439 uint64_t h = load_relaxed_u64(&ring->head);
440 uint64_t t = load_acquire_u64(&ring->tail);
441 if (h >= t) {
442 pthread_mutex_unlock(&w->mu);
443 return RX_ERR_NOT_FOUND;
444 }
445 OmegaSharedWorldDesc d;
446 memcpy(&d, &ring->slots[h & ring->mask], sizeof(d));
447 w->stats.c2g_read_bytes += sizeof(d);
448 uint32_t fault = OMEGA_SW_FAULT_NONE;
449 int rc = check_locked(w, &d, h, &fault);
450 store_release_u64(&ring->head, h + 1);
451 if (rc != RX_OK) {
452 raise_fault(w, fault, h, d.msg_type, (uint32_t)d.sequence);
453 w->stats.desc_rejected++;
454 }
455 if (out_fault) *out_fault = fault;
456 if (out && rc == RX_OK) *out = d;
457 pthread_mutex_unlock(&w->mu);
458 return rc;
459}
460
461int rx_world_inject_descriptor(RxWorld *w, const OmegaSharedWorldDesc *desc) {
462 if (!w || !desc || !w->coherent) return RX_ERR_ARG;
463 pthread_mutex_lock(&w->mu);
464 OmegaSharedWorldRing *ring = pub_ring(w);
465 uint64_t t = load_relaxed_u64(&ring->tail);
466 uint64_t h = load_acquire_u64(&ring->head);
467 if (t - h >= ring->capacity) {
468 pthread_mutex_unlock(&w->mu);
469 return RX_ERR_FULL;
470 }
471 memcpy(&ring->slots[t & ring->mask], desc, sizeof(*desc));
472 store_release_u64(&ring->tail, t + 1);
473 w->stats.c2g_write_bytes += sizeof(*desc);
474 pthread_mutex_unlock(&w->mu);
475 return RX_OK;
476}
477
478uint64_t rx_world_publication_tail(RxWorld *w) {
479 if (!w || !w->coherent) return 0;
480 pthread_mutex_lock(&w->mu);
481 uint64_t t = load_relaxed_u64(&pub_ring(w)->tail);
482 pthread_mutex_unlock(&w->mu);
483 return t;
484}
485
486int rx_world_bind_capability(RxWorld *w, RxObjRef ref, RxCapRef cap) {
487 if (!w) return RX_ERR_ARG;
488 pthread_mutex_lock(&w->mu);
489 int rc = RX_OK;
490 if (ref.id >= RX_MAX_OBJECTS || !w->objects[ref.id].live ||
491 w->objects[ref.id].generation != ref.generation) {
492 rc = RX_ERR_STALE_GEN;
493 goto out;
494 }
495 RxCapEntry ent;
496 if (rx_world_inspect_cap(w, cap, &ent) != RX_CAP_OK ||
497 rx_world_validate_cap(w, cap, ent.subject, w->objects[ref.id].resource,
498 RX_RIGHT_READ, NULL) != RX_CAP_OK) {
499 rc = RX_ERR_AUTHORITY;
500 goto out;
501 }
502 w->objects[ref.id].cap = cap;
503out:
504 pthread_mutex_unlock(&w->mu);
505 return rc;
506}
507
508int rx_world_physical(RxWorld *w, RxObjRef ref, OmegaSharedWorldObject *out) {
509 if (!w || !out || !w->coherent) return RX_ERR_ARG;
510 pthread_mutex_lock(&w->mu);
511 int rc = RX_OK;
512 if (ref.id >= RX_MAX_OBJECTS || w->objects[ref.id].generation != ref.generation) {
513 rc = RX_ERR_STALE_GEN;
514 } else if (!w->objects[ref.id].placed) {
515 rc = RX_ERR_UNPLACED;
516 } else {
517 *out = table_of(w)->objects[ref.id];
518 }
519 pthread_mutex_unlock(&w->mu);
520 return rc;
521}
522
523int rx_world_overwrite_physical(RxWorld *w, uint32_t id, const OmegaSharedWorldObject *src) {
524 if (!w || !src || !w->coherent || id >= RX_MAX_OBJECTS) return RX_ERR_ARG;
525 pthread_mutex_lock(&w->mu);
526 table_of(w)->objects[id] = *src;
527 pthread_mutex_unlock(&w->mu);
528 return RX_OK;
529}
530
531static int window_busy(const RxWorld *w, uint32_t win, uint32_t except) {
532 for (uint32_t i = 0; i < RX_MAX_OBJECTS; i++) {
533 if (i == except || !w->objects[i].placed) continue;
534 if (w->objects[i].window == win) return 1;
535 }
536 return 0;
537}
538
539static int window_for_offset(uint64_t offset, uint64_t length, uint32_t *out_win) {
540 if (length != RX_OBJECT_WINDOW) return RX_ERR_BOUNDS;
541 if (offset > UINT64_MAX - length) return RX_ERR_BOUNDS;
542 uint64_t payload = off_payload();
543 uint64_t limit = payload + (uint64_t)RX_PHYS_WINDOWS * RX_OBJECT_WINDOW;
544 if (offset < payload || offset + length > limit) return RX_ERR_BOUNDS;
545 uint64_t rel = offset - payload;
546 if (rel % RX_OBJECT_WINDOW != 0) return RX_ERR_BOUNDS;
547 *out_win = (uint32_t)(rel / RX_OBJECT_WINDOW);
548 return RX_OK;
549}
550
551static void adopt_window(RxWorld *w, RxObject *o, uint32_t win) {
552 o->placed = true;
553 o->window = win;
554 o->region_offset = window_offset(win);
555 o->size_bytes = RX_OBJECT_WINDOW;
556 o->placement = RX_PLACE_COHERENT;
557 o->locality = RX_LOCALITY_MACHINE;
558 o->coherency = RX_COHERENCY_HOST;
559 rx_coherent_project(w, o->id);
560}
561
562static int current_object(RxWorld *w, RxObjRef ref, RxObject **out) {
563 if (ref.id >= RX_MAX_OBJECTS || w->objects[ref.id].generation != ref.generation ||
564 !w->objects[ref.id].live)
565 return RX_ERR_STALE_GEN;
566 *out = &w->objects[ref.id];
567 return RX_OK;
568}
569
570int rx_world_attach_physical(RxWorld *w, RxObjRef ref) {
571 if (!w || !w->coherent) return RX_ERR_ARG;
572 pthread_mutex_lock(&w->mu);
573 RxObject *o = NULL;
574 int rc = current_object(w, ref, &o);
575 if (rc != RX_OK) goto out;
576 if (o->placed) {
577 rc = RX_ERR_EXISTS;
578 goto out;
579 }
580 uint32_t win = 0;
581 int found = 0;
582 for (uint32_t i = 0; i < RX_PHYS_WINDOWS; i++) {
583 if (!window_busy(w, i, o->id)) {
584 win = i;
585 found = 1;
586 break;
587 }
588 }
589 if (!found) {
590 rc = RX_ERR_FULL;
591 goto out;
592 }
593 adopt_window(w, o, win);
594 rc = RX_OK;
595out:
596 pthread_mutex_unlock(&w->mu);
597 return rc;
598}
599
600int rx_world_detach_physical(RxWorld *w, RxObjRef ref) {
601 if (!w || !w->coherent) return RX_ERR_ARG;
602 pthread_mutex_lock(&w->mu);
603 RxObject *o = NULL;
604 int rc = current_object(w, ref, &o);
605 if (rc != RX_OK) goto out;
606 if (!o->placed) {
607 rc = RX_ERR_UNPLACED;
608 goto out;
609 }
610 o->placed = false;
611 o->window = 0;
612 o->region_offset = 0;
613 o->size_bytes = 0;
614 o->placement = 0;
615 o->locality = 0;
616 o->coherency = 0;
617 rx_coherent_project(w, o->id);
618 rc = RX_OK;
619out:
620 pthread_mutex_unlock(&w->mu);
621 return rc;
622}
623
624int rx_world_place_physical(RxWorld *w, RxObjRef ref, uint64_t offset, uint64_t length) {
625 if (!w || !w->coherent) return RX_ERR_ARG;
626 pthread_mutex_lock(&w->mu);
627 RxObject *o = NULL;
628 int rc = current_object(w, ref, &o);
629 if (rc != RX_OK) goto out;
630 uint32_t win = 0;
631 rc = window_for_offset(offset, length, &win);
632 if (rc != RX_OK) goto out;
633 if (window_busy(w, win, o->id)) {
634 rc = RX_ERR_EXISTS;
635 goto out;
636 }
637 if (o->placed && o->window != win) {
638 uint8_t saved[RX_OBJECT_WINDOW];
639 memcpy(saved, w->coherent + o->region_offset, RX_OBJECT_WINDOW);
640 adopt_window(w, o, win);
641 memcpy(w->coherent + o->region_offset, saved, RX_OBJECT_WINDOW);
642 w->stats.window_move_bytes += 2u * RX_OBJECT_WINDOW;
643 } else {
644 adopt_window(w, o, win);
645 }
646 rc = RX_OK;
647out:
648 pthread_mutex_unlock(&w->mu);
649 return rc;
650}
651
652int rx_world_relocate_physical(RxWorld *w, RxObjRef ref) {
653 if (!w || !w->coherent) return RX_ERR_ARG;
654 pthread_mutex_lock(&w->mu);
655 RxObject *o = NULL;
656 int rc = current_object(w, ref, &o);
657 if (rc != RX_OK) goto out;
658 if (!o->placed) {
659 rc = RX_ERR_UNPLACED;
660 goto out;
661 }
662 uint32_t win = 0;
663 int found = 0;
664 for (uint32_t i = 0; i < RX_PHYS_WINDOWS; i++) {
665 if (i == o->window || window_busy(w, i, o->id)) continue;
666 win = i;
667 found = 1;
668 break;
669 }
670 if (!found) {
671 rc = RX_ERR_FULL;
672 goto out;
673 }
674 uint8_t saved[RX_OBJECT_WINDOW];
675 uint8_t digest[32];
676 uint32_t generation = o->generation;
677 uint64_t version = o->version;
678 memcpy(saved, w->coherent + o->region_offset, RX_OBJECT_WINDOW);
679 memcpy(digest, o->digest, 32);
680 adopt_window(w, o, win);
681 memcpy(w->coherent + o->region_offset, saved, RX_OBJECT_WINDOW);
682 w->stats.window_move_bytes += 2u * RX_OBJECT_WINDOW;
683 if (o->generation != generation || o->version != version ||
684 memcmp(o->digest, digest, 32) != 0) {
685 rc = RX_ERR_BAD_DESC;
686 goto out;
687 }
688 rc = RX_OK;
689out:
690 pthread_mutex_unlock(&w->mu);
691 return rc;
692}
693
694/* ---- resident seat ------------------------------------------------------ */
695
696static OmegaSharedWorldRing *g2c_ring(RxWorld *w) {
697 return (OmegaSharedWorldRing *)(w->coherent + off_g2c());
698}
699
700static int notice_intact(const OmegaSharedWorldDesc *d, uint32_t epoch) {
701 if (!d || d->magic != OMEGA_SW_MAGIC || d->abi_version != OMEGA_SW_ABI_VERSION)
702 return RX_ERR_BAD_DESC;
703 if (d->world_epoch != epoch) return RX_ERR_STALE_GEN;
704 if ((d->flags & OMEGA_SW_FLAG_CHECKSUM) == 0) return RX_ERR_TORN;
705 OmegaSharedWorldDesc tmp = *d;
706 uint32_t got = tmp.checksum;
707 tmp.checksum = 0;
708 if (desc_checksum(&tmp) != got) return RX_ERR_TORN;
709 return RX_OK;
710}
711
712static int post_on(RxWorld *w, OmegaSharedWorldRing *ring, OmegaSharedWorldDesc *d) {
713 uint64_t t = load_relaxed_u64(&ring->tail);
714 uint64_t h = load_acquire_u64(&ring->head);
715 if (t - h >= ring->capacity) return RX_ERR_FULL;
716 d->sequence = t;
717 d->world_epoch = w->world_epoch;
718 rx_world_seal_descriptor(d);
719 memcpy(&ring->slots[t & ring->mask], d, sizeof(*d));
720 atomic_thread_fence(memory_order_release);
721 store_release_u64(&ring->tail, t + 1);
722 if (ring == g2c_ring(w)) w->stats.g2c_write_bytes += sizeof(*d);
723 else w->stats.c2g_write_bytes += sizeof(*d);
724 return RX_OK;
725}
726
727int rx_world_enable_resident(RxWorld *w) {
728 if (!w) return RX_ERR_ARG;
729 pthread_mutex_lock(&w->mu);
730 w->resident_enabled = true;
731 if (w->seat_generation == 0) w->seat_generation = 1;
732 pthread_mutex_unlock(&w->mu);
733 return RX_OK;
734}
735
736/* Claim payload, little-endian:
737 * 0 in capability id 4 in capability generation
738 * 8 in object version 16 parent crumb
739 * 24 out object id 28 out object generation
740 * 32 out capability id 36 out capability generation */
741#define CLAIM_PAYLOAD RX_CAP_PAYLOAD
742
743int rx_resident_post_claim(RxWorld *w, uint32_t in, uint32_t out, uint64_t parent,
744 uint64_t *seq_out) {
745 if (!w || !w->coherent || in >= RX_MAX_OBJECTS || out >= RX_MAX_OBJECTS || in == out)
746 return RX_ERR_ARG;
747 RxObject *a = &w->objects[in];
748 RxObject *b = &w->objects[out];
749 if (!a->live || !a->placed || !b->live || !b->placed) return RX_ERR_UNPLACED;
750 OmegaSharedWorldRing *ring = pub_ring(w);
751 uint64_t t = load_relaxed_u64(&ring->tail);
752 OmegaSharedWorldDesc d;
753 memset(&d, 0, sizeof(d));
754 d.msg_type = RX_RING_CLAIM;
755 d.object_id = a->id;
756 d.object_generation = a->generation;
757 d.object_offset = 0;
758 d.object_length = (uint32_t)a->size_bytes;
759 d.payload_len = CLAIM_PAYLOAD;
760 d.arg_b = (uint32_t)t;
761 d.producer_generation = w->seat_generation;
762 put_u32(d.payload, a->cap.cap_id);
763 put_u32(d.payload + 4, (uint32_t)a->cap.generation);
764 put_u64(d.payload + 8, a->version);
765 put_u64(d.payload + 16, parent);
766 put_u32(d.payload + 24, b->id);
767 put_u32(d.payload + 28, b->generation);
768 put_u32(d.payload + 32, b->cap.cap_id);
769 put_u32(d.payload + 36, (uint32_t)b->cap.generation);
770 put_u32(d.payload + RX_CAP_GEN_HI_A, (uint32_t)(a->cap.generation >> 32));
771 put_u32(d.payload + RX_CAP_GEN_HI_B, (uint32_t)(b->cap.generation >> 32));
772 int rc = post_on(w, ring, &d);
773 if (rc == RX_OK) {
774 w->stats.resident_claims++;
775 if (seq_out) *seq_out = t;
776 }
777 return rc;
778}
779
780/* The seat's one operation, as the stand-in performs it: the qualified
781 * 32-bit integer add of the input's field 0 and field 1, into the output's
782 * field 0. The high word is written as zero, as the chip writes it. */
783static void apply_add(RxWorld *w, const RxObject *a, const RxObject *b) {
784 const uint8_t *in = w->coherent + a->region_offset;
785 uint8_t *outw = w->coherent + b->region_offset;
786 uint32_t sum = (uint32_t)get_u64(in) + (uint32_t)get_u64(in + 8);
787 put_u64(outw, (uint64_t)sum);
788}
789
790/* Stand-in check of the output named in a claim. */
791static int claim_out_ok(RxWorld *w, const OmegaSharedWorldDesc *d, uint32_t *out_id) {
792 if (d->payload_len < CLAIM_PAYLOAD) return 0;
793 uint32_t id = get_u32(d->payload + 24);
794 uint32_t gen = get_u32(d->payload + 28);
795 if (id >= RX_MAX_OBJECTS || id == d->object_id) return 0;
796 const RxObject *b = &w->objects[id];
797 const OmegaSharedWorldObject *p = &table_of(w)->objects[id];
798 if (!b->live || !b->placed || gen == 0 || gen != b->generation) return 0;
799 if (p->generation != gen || p->state != OMEGA_SW_OBJ_ACTIVE ||
800 p->region_offset != b->region_offset)
801 return 0;
802 *out_id = id;
803 return 1;
804}
805
806static int post_result(RxWorld *w, uint16_t msg, const OmegaSharedWorldDesc *claim) {
807 OmegaSharedWorldDesc d;
808 memset(&d, 0, sizeof(d));
809 d.msg_type = msg;
810 d.object_id = claim->object_id;
811 d.object_generation = claim->object_generation;
812 d.object_offset = 0;
813 d.object_length = claim->object_length ? claim->object_length : RX_OBJECT_WINDOW;
814 d.payload_len = claim->payload_len;
815 d.arg_b = (uint32_t)claim->sequence;
816 d.producer_generation = claim->producer_generation; /* echoed, as the chip does */
817 memcpy(d.payload, claim->payload, sizeof d.payload);
818 return post_on(w, g2c_ring(w), &d);
819}
820
821int rx_resident_seat_step(RxWorld *w) {
822 if (!w || !w->coherent) return RX_ERR_ARG;
823 pthread_mutex_lock(&w->mu);
824 int rc = 0;
825 OmegaSharedWorldRing *ring = pub_ring(w);
826 for (;;) {
827 uint64_t h = load_relaxed_u64(&ring->head);
828 uint64_t t = load_acquire_u64(&ring->tail);
829 if (h >= t) break;
830 OmegaSharedWorldDesc d;
831 memcpy(&d, &ring->slots[h & ring->mask], sizeof(d));
832 w->stats.c2g_read_bytes += sizeof(d);
833 if (d.msg_type == RX_RING_SHUTDOWN && notice_intact(&d, w->world_epoch) == RX_OK) {
834 store_release_u64(&ring->head, h + 1);
835 w->resident_stopped = true;
836 rc = 2;
837 break;
838 }
839 if (d.msg_type != RX_RING_CLAIM) {
840 store_release_u64(&ring->head, h + 1);
841 continue;
842 }
843 uint32_t fault = OMEGA_SW_FAULT_NONE;
844 int chk = check_locked(w, &d, h, &fault);
845 if (chk != RX_OK) {
846 raise_fault(w, fault, h, d.msg_type, d.object_id);
847 post_result(w, RX_RING_FAULT, &d);
848 store_release_u64(&ring->head, h + 1);
849 w->stats.desc_rejected++;
850 rc = chk;
851 break;
852 }
853 uint32_t out_id = 0;
854 /* A claim from an earlier seat generation is refused, as on the chip. */
855 if (d.producer_generation != w->seat_generation || !claim_out_ok(w, &d, &out_id)) {
856 raise_fault(w, OMEGA_SW_FAULT_STALE_GEN, h, d.msg_type, d.object_id);
857 post_result(w, RX_RING_FAULT, &d);
858 store_release_u64(&ring->head, h + 1);
859 w->stats.desc_rejected++;
860 rc = RX_ERR_STALE_GEN;
861 break;
862 }
863 apply_add(w, &w->objects[d.object_id], &w->objects[out_id]);
864 if (post_result(w, RX_RING_PUBLISH, &d) != RX_OK) {
865 rc = RX_ERR_FULL;
866 break;
867 }
868 store_release_u64(&ring->head, h + 1);
869 rc = 1;
870 break;
871 }
872 pthread_mutex_unlock(&w->mu);
873 return rc;
874}
875
876int rx_resident_take_result(RxWorld *w, OmegaSharedWorldDesc *out) {
877 if (!w || !w->coherent || !out) return RX_ERR_ARG;
878 OmegaSharedWorldRing *ring = g2c_ring(w);
879 uint64_t h = load_relaxed_u64(&ring->head);
880 uint64_t t = load_acquire_u64(&ring->tail);
881 if (h >= t) { w->stats.gpu_polls_empty++; return RX_ERR_NOT_FOUND; }
882 memcpy(out, &ring->slots[h & ring->mask], sizeof(*out));
883 store_release_u64(&ring->head, h + 1);
884 w->stats.g2c_read_bytes += sizeof(*out);
885 w->stats.gpu_results_taken++;
886 return notice_intact(out, w->world_epoch);
887}
888
889int rx_resident_reset(RxWorld *w) {
890 if (!w) return RX_ERR_ARG;
891 pthread_mutex_lock(&w->mu);
892 w->resident_stopped = false;
893 pthread_mutex_unlock(&w->mu);
894 return RX_OK;
895}
896
897int rx_resident_shutdown(RxWorld *w) {
898 if (!w || !w->coherent) return RX_ERR_ARG;
899 pthread_mutex_lock(&w->mu);
900 OmegaSharedWorldDesc d;
901 memset(&d, 0, sizeof(d));
902 d.msg_type = RX_RING_SHUTDOWN;
903 d.object_id = 0;
904 d.object_generation = w->objects[0].generation ? w->objects[0].generation : 1;
905 d.object_length = RX_OBJECT_WINDOW;
906 d.payload_len = 8;
907 int rc = post_on(w, pub_ring(w), &d);
908 pthread_mutex_unlock(&w->mu);
909 return rc;
910}
911
912int rx_world_bind_coherent(RxWorld *w, void *mem, uint64_t bytes, int borrowed) {
913 if (!w || !mem || !w->coherent || bytes != w->coherent_bytes) return RX_ERR_ARG;
914 pthread_mutex_lock(&w->mu);
915 memcpy(mem, w->coherent, (size_t)bytes);
916 w->stats.setup_copy_bytes += bytes;
917 if (!w->coherent_borrowed) free(w->coherent);
918 w->coherent = mem;
919 w->coherent_borrowed = borrowed ? true : false;
920 pthread_mutex_unlock(&w->mu);
921 return RX_OK;
922}