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 / omega_accelerator.c

159 lines · 6156 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#include "omega_accelerator.h"
18#include "sha256.h"
19#include <string.h>
20#include <stdio.h>
21
22int omega_accel_port_init(OmegaAccelPort *port,
23 const OmegaAccelCapability *cap) {
24 if (!port || !cap) return -1;
25 memset(port, 0, sizeof(*port));
26
27 port->capability = *cap;
28 port->is_bound_to_physics = true;
29 memset(port->expected_seal, 0, sizeof(port->expected_seal));
30 return 0;
31}
32
33int omega_accel_port_build_dma_intent(const OmegaAccelPort *port,
34 uint64_t iova_base,
35 uint64_t phys_base,
36 uint64_t size_bytes,
37 uint32_t permissions,
38 OmegaEffectIntent *out_intent) {
39 if (!port || !out_intent || size_bytes == 0) return -1;
40
41 /* Check bounds against capability */
42 if (iova_base < port->capability.iova_bound_base ||
43 (iova_base + size_bytes) > (port->capability.iova_bound_base + port->capability.iova_bound_size)) {
44 return -2;
45 }
46
47 memset(out_intent, 0, sizeof(*out_intent));
48 out_intent->version = 1;
49 out_intent->length = sizeof(OmegaEffectIntent);
50 out_intent->request_id = 1000 + port->requests_issued + 1;
51 out_intent->principal_id = port->capability.principal_id;
52 out_intent->capability_slot = port->capability.slot;
53 out_intent->capability_generation = port->capability.generation;
54 out_intent->resource_type = RES_ACCELERATOR;
55 out_intent->operation = ACCEL_OP_MAP_DMA;
56 out_intent->target_base = iova_base;
57 out_intent->target_size = size_bytes;
58 out_intent->param0 = phys_base;
59 (void)permissions;
60
61 return 0;
62}
63
64int omega_accel_port_build_submit_intent(const OmegaAccelPort *port,
65 uint32_t queue_id,
66 uint64_t cmd_iova,
67 uint64_t cmd_len,
68 OmegaEffectIntent *out_intent) {
69 if (!port || !out_intent || cmd_len == 0) return -1;
70
71 memset(out_intent, 0, sizeof(*out_intent));
72 out_intent->version = 1;
73 out_intent->length = sizeof(OmegaEffectIntent);
74 out_intent->request_id = 2000 + port->requests_issued + 1;
75 out_intent->principal_id = port->capability.principal_id;
76 out_intent->capability_slot = port->capability.slot;
77 out_intent->capability_generation = port->capability.generation;
78 out_intent->resource_type = RES_ACCELERATOR;
79 out_intent->operation = ACCEL_OP_SUBMIT;
80 out_intent->target_base = queue_id;
81 out_intent->target_size = cmd_len;
82 out_intent->param0 = cmd_iova;
83
84 return 0;
85}
86
87int omega_accel_port_verify_receipt(OmegaAccelPort *port,
88 const OmegaEffectIntent *intent,
89 const OmegaEffectReceipt *receipt) {
90 if (!port || !receipt) return -1;
91
92 if (receipt->decision != DEC_ADMITTED) {
93 return -2; /* Effect refused or rejected by Physics */
94 }
95
96 /* Verify intent digest */
97 if (intent) {
98 uint8_t expected_intent_digest[32];
99 sha256_hash((const uint8_t *)intent, sizeof(OmegaEffectIntent), expected_intent_digest);
100 if (memcmp(expected_intent_digest, receipt->intent_digest, 32) != 0) {
101 return -3; /* Intent digest mismatch */
102 }
103 }
104
105 /* Verify previous receipt seal binding */
106 if (memcmp(port->expected_seal, receipt->previous_receipt_digest, 32) != 0) {
107 return -4; /* Seal chain discontinuity */
108 }
109
110 /* Verify unkeyed receipt digest over bytes 0..127 + previous_receipt_digest */
111 uint8_t hash_input[160];
112 memcpy(hash_input, receipt, 128);
113 memcpy(hash_input + 128, receipt->previous_receipt_digest, 32);
114
115 uint8_t expected_receipt_digest[32];
116 sha256_hash(hash_input, 160, expected_receipt_digest);
117
118 if (memcmp(expected_receipt_digest, receipt->receipt_digest, 32) != 0) {
119 return -5; /* Digest mismatch (does not authenticate the producer) */
120 }
121
122 /* Update rolling expected seal and advance counter */
123 memcpy(port->expected_seal, receipt->receipt_digest, 32);
124 port->receipts_validated++;
125
126 /* If this was a successful DMA grant, track the window */
127 if (intent && intent->operation == ACCEL_OP_MAP_DMA) {
128 if (port->active_window_count < OMEGA_ACCEL_MAX_WINDOWS) {
129 OmegaDmaWindow *w = &port->windows[port->active_window_count++];
130 w->iova_base = intent->target_base;
131 w->phys_base = receipt->actual_effect;
132 w->size_bytes = intent->target_size;
133 w->permissions = DMA_PERM_READ | DMA_PERM_WRITE | DMA_PERM_COHERENT;
134 w->window_id = port->active_window_count;
135 }
136 }
137
138 return 0;
139}
140
141int omega_accel_port_bind_machine_graph(const OmegaAccelPort *port,
142 OmegaMachineGraph *mg) {
143 if (!port || !mg) return -1;
144
145 /* Add UNIT_ACCELERATOR_PORT to pipeline compute units */
146 if (mg->pipeline.unit_count < OMEGA_MACHINE_MAX_UNITS) {
147 MachineComputeUnit *unit = &mg->pipeline.units[mg->pipeline.unit_count++];
148 unit->type = UNIT_ACCELERATOR_PORT;
149 unit->count = 1;
150 unit->latency_cycles = 10;
151 unit->throughput_per_cycle = 4;
152 }
153
154 mg->is_physics_authorized = true;
155 memcpy(mg->physics_receipt_seal, port->expected_seal, 32);
156
157 /* Recompute canonical machine ID */
158 return omega_machine_compute_id(mg);
159}