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 / visor / visor_machine.c

317 lines · 14506 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 * visor_machine.c -- Omega Visor V1, lane 5: machine view (read-only).
19 * See visor_machine.h. No physics headers, no authority, fixed storage.
20 */
21#include "visor_machine.h"
22#include "visor.h"
23#include "aarch64_target.h"
24
25#include <inttypes.h>
26#include <stdarg.h>
27#include <stdio.h>
28#include <string.h>
29#include <sys/utsname.h>
30
31/* Weak reference: resolves only when omega_blackwell_realize.o is linked.
32 * The Visor never submits GPU work; the id is shown as a declared target. */
33extern int omega_blackwell_get_machine_id(SemanticId *out_machine_id) __attribute__((weak));
34
35int visor_machine_blackwell_id(SemanticId *out_id) {
36 if (!out_id) return -1;
37 if (!omega_blackwell_get_machine_id) return -1;
38 return omega_blackwell_get_machine_id(out_id) == 0 ? 0 : -1;
39}
40
41const char *visor_machine_unit_name(ComputeUnitType t) {
42 switch (t) {
43 case UNIT_ALU: return "ALU";
44 case UNIT_BRANCH: return "BRANCH";
45 case UNIT_MULTIPLIER: return "MULTIPLIER";
46 case UNIT_DIVIDER: return "DIVIDER";
47 case UNIT_LOAD_STORE: return "LOAD_STORE";
48 case UNIT_VECTOR: return "VECTOR";
49 case UNIT_ACCELERATOR_PORT: return "ACCELERATOR_PORT";
50 }
51 return "UNKNOWN";
52}
53
54/* ---- host facts ---------------------------------------------------------- */
55
56static void read_first_line(const char *path, char *out, size_t n) {
57 out[0] = '\0';
58 FILE *f = fopen(path, "r");
59 if (!f) return;
60 if (fgets(out, (int)n, f)) {
61 size_t l = strlen(out);
62 while (l > 0 && (out[l - 1] == '\n' || out[l - 1] == '\r' || out[l - 1] == ' ')) out[--l] = '\0';
63 }
64 fclose(f);
65}
66
67static void collect_cpu_parts(char *out, size_t n) {
68 char parts[16][12];
69 size_t count = 0;
70 out[0] = '\0';
71 FILE *f = fopen("/proc/cpuinfo", "r");
72 if (!f) return;
73 char line[256];
74 while (fgets(line, sizeof(line), f)) {
75 if (strncmp(line, "CPU part", 8) != 0) continue;
76 const char *c = strchr(line, ':');
77 if (!c) continue;
78 c++;
79 while (*c == ' ' || *c == '\t') c++;
80 char val[12];
81 size_t k = 0;
82 while (*c && *c != '\n' && *c != ' ' && k + 1 < sizeof(val)) val[k++] = *c++;
83 val[k] = '\0';
84 bool dup = false;
85 for (size_t i = 0; i < count; ++i) if (strcmp(parts[i], val) == 0) dup = true;
86 if (!dup && count < 16) { memcpy(parts[count], val, sizeof(val)); count++; }
87 }
88 fclose(f);
89 /* sort for determinism */
90 for (size_t i = 0; i < count; ++i)
91 for (size_t j = i + 1; j < count; ++j)
92 if (strcmp(parts[j], parts[i]) < 0) {
93 char t[12]; memcpy(t, parts[i], 12); memcpy(parts[i], parts[j], 12); memcpy(parts[j], t, 12);
94 }
95 size_t pos = 0;
96 for (size_t i = 0; i < count; ++i) {
97 int w = snprintf(out + pos, n - pos, "%s%s", i ? "," : "", parts[i]);
98 if (w < 0 || (size_t)w >= n - pos) break;
99 pos += (size_t)w;
100 }
101}
102
103int visor_machine_host_facts(VisorHostFacts *out) {
104 if (!out) return -1;
105 memset(out, 0, sizeof(*out));
106 struct utsname u;
107 if (uname(&u) == 0) snprintf(out->uname_machine, sizeof(out->uname_machine), "%.31s", u.machine);
108 read_first_line("/sys/devices/virtual/dmi/id/product_name", out->dmi_product, sizeof(out->dmi_product));
109 collect_cpu_parts(out->cpu_parts, sizeof(out->cpu_parts));
110 return 0;
111}
112
113/* ---- view ---------------------------------------------------------------- */
114
115static bool seal_is_constant(const uint8_t seal[32]) {
116 for (size_t i = 1; i < 32; ++i) if (seal[i] != seal[0]) return false;
117 return true;
118}
119
120static void fill_targets(const OmegaMachineGraph *mg, VisorMachineView *v) {
121 size_t t = 0;
122 VisorMachineTarget *a = &v->targets[t++];
123 snprintf(a->name, sizeof(a->name), "%s", VISOR_TARGET_AARCH64);
124 visor_format_id(&mg->machine_id, a->machine_id);
125 a->linked = true;
126#if defined(__aarch64__)
127 a->runnable = true;
128 a->note = "pure AArch64 realizations run natively on this host via omega_exec_native_f3";
129#else
130 a->runnable = false;
131 a->note = "host is not aarch64; run refused";
132#endif
133
134 VisorMachineTarget *b = &v->targets[t++];
135 snprintf(b->name, sizeof(b->name), "%s", VISOR_TARGET_BLACKWELL);
136 SemanticId bw;
137 if (visor_machine_blackwell_id(&bw) == 0) {
138 visor_format_id(&bw, b->machine_id);
139 b->linked = true;
140 b->note = "declared target (omega_blackwell_get_machine_id); run refused: requires GPU submit authority; not linked in Visor V1";
141 } else {
142 snprintf(b->machine_id, sizeof(b->machine_id), "none");
143 b->linked = false;
144 b->note = "declared target, Blackwell realize objects not linked into this binary; run refused";
145 }
146 b->runnable = false;
147 v->target_count = t;
148}
149
150int visor_machine_view(const OmegaMachineGraph *mg, VisorMachineView *out) {
151 if (!mg || !out) return -1;
152 memset(out, 0, sizeof(*out));
153 snprintf(out->name, sizeof(out->name), "%s", mg->name);
154 visor_format_id(&mg->machine_id, out->id_text);
155 out->target_profile = mg->target_profile;
156 out->observed = false;
157 snprintf(out->provenance, sizeof(out->provenance),
158 "assumed: machine graph as given; no physics descriptor ingested (V1)");
159 out->issue_width = mg->pipeline.issue_width;
160 out->max_in_flight = mg->pipeline.max_in_flight;
161 out->out_of_order = mg->pipeline.out_of_order;
162 out->unit_count = mg->pipeline.unit_count > OMEGA_MACHINE_MAX_UNITS ? OMEGA_MACHINE_MAX_UNITS
163 : mg->pipeline.unit_count;
164 for (size_t i = 0; i < out->unit_count; ++i) {
165 const MachineComputeUnit *u = &mg->pipeline.units[i];
166 out->units[i].type_name = visor_machine_unit_name(u->type);
167 out->units[i].count = u->count;
168 out->units[i].latency = u->latency_cycles;
169 out->units[i].throughput = u->throughput_per_cycle;
170 }
171 out->gpr_count = mg->registers.gpr_count;
172 out->gpr_width = mg->registers.gpr_width_bits;
173 out->vec_count = mg->registers.vector_count;
174 out->vec_width = mg->registers.vector_width_bits;
175 out->cache_count = mg->cache_count > OMEGA_MACHINE_MAX_CACHES ? OMEGA_MACHINE_MAX_CACHES : mg->cache_count;
176 for (size_t i = 0; i < out->cache_count; ++i) {
177 const MachineCacheLevel *c = &mg->caches[i];
178 out->caches[i] = (VisorMachineCache){ c->level, c->is_instruction, c->size_bytes,
179 c->line_size_bytes, c->associativity, c->latency_cycles };
180 }
181 out->dram_base = mg->dram_base;
182 out->dram_size = mg->dram_size;
183 out->physics_authorized = mg->is_physics_authorized;
184 out->physics_seal_is_placeholder = seal_is_constant(mg->physics_receipt_seal);
185 char err[96] = {0};
186 out->topology_valid = omega_machine_validate_topology(mg, err, sizeof(err)) == 0;
187 if (!out->topology_valid) snprintf(out->topology_error, sizeof(out->topology_error), "%s", err);
188 fill_targets(mg, out);
189 return 0;
190}
191
192int visor_machine_from_facts(const VisorHostFacts *f, OmegaMachineGraph *out_mg, VisorMachineView *out_view) {
193 if (!f || !out_mg || !out_view) return -1;
194 bool aarch64 = strcmp(f->uname_machine, "aarch64") == 0;
195 bool dmi_spark = strstr(f->dmi_product, "DGX_Spark") || strstr(f->dmi_product, "DGX Spark");
196 bool nv2 = strstr(f->cpu_parts, "0xd4f") != NULL;
197 bool spark = aarch64 && (dmi_spark || nv2);
198 int rc = spark ? omega_machine_build_dgx_spark(out_mg) : omega_machine_build_qemu_virt(out_mg);
199 if (rc != 0) return -1;
200 if (visor_machine_view(out_mg, out_view) != 0) return -1;
201 const char *fit;
202 if (!spark) fit = "fallback profile";
203 else if (nv2) fit = "host cores include Neoverse-V2 (0xd4f)";
204 else fit = "profile labels Neoverse-V2 4-wide; host cores do NOT match (no 0xd4f)";
205 snprintf(out_view->provenance, sizeof(out_view->provenance),
206 "assumed: omega_machine_build_%s(); host uname=%.31s dmi=%.63s cpu_parts=%.63s; %s; "
207 "no physics descriptor ingested; seal is builder constant",
208 spark ? "dgx_spark" : "qemu_virt",
209 f->uname_machine[0] ? f->uname_machine : "?",
210 f->dmi_product[0] ? f->dmi_product : "?",
211 f->cpu_parts[0] ? f->cpu_parts : "?", fit);
212 out_view->observed = false;
213 return 0;
214}
215
216int visor_machine_current(OmegaMachineGraph *out_mg, VisorMachineView *out_view) {
217 VisorHostFacts f;
218 if (visor_machine_host_facts(&f) != 0) return -1;
219 return visor_machine_from_facts(&f, out_mg, out_view);
220}
221
222/* ---- formatting ---------------------------------------------------------- */
223
224typedef struct { char *buf; size_t n, pos; bool trunc; } Out;
225
226static void outf(Out *o, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
227static void outf(Out *o, const char *fmt, ...) {
228 if (o->trunc) return;
229 va_list ap;
230 va_start(ap, fmt);
231 int w = vsnprintf(o->buf + o->pos, o->n - o->pos, fmt, ap);
232 va_end(ap);
233 if (w < 0 || (size_t)w >= o->n - o->pos) { o->trunc = true; o->buf[o->n - 1] = '\0'; return; }
234 o->pos += (size_t)w;
235}
236
237static void json_str(Out *o, const char *s) {
238 outf(o, "\"");
239 for (; s && *s; ++s) {
240 unsigned char c = (unsigned char)*s;
241 if (c == '"' || c == '\\') outf(o, "\\%c", c);
242 else if (c < 0x20) outf(o, "\\u%04x", c);
243 else outf(o, "%c", c);
244 }
245 outf(o, "\"");
246}
247
248int visor_machine_format_text(const VisorMachineView *v, char *out, size_t n) {
249 if (!v || !out || n == 0) return -1;
250 Out o = { out, n, 0, false };
251 out[0] = '\0';
252 outf(&o, "machine %s\n", v->name);
253 outf(&o, " id %s\n", v->id_text);
254 outf(&o, " profile 0x%02x\n", v->target_profile);
255 outf(&o, " observed %s\n", v->observed ? "yes" : "no (assumed canonical profile)");
256 outf(&o, " provenance %s\n", v->provenance);
257 outf(&o, " pipeline issue=%u in_flight=%u %s\n", v->issue_width, v->max_in_flight,
258 v->out_of_order ? "out-of-order" : "in-order");
259 for (size_t i = 0; i < v->unit_count; ++i)
260 outf(&o, " unit %-16s count=%u latency=%u throughput=%u\n", v->units[i].type_name,
261 v->units[i].count, v->units[i].latency, v->units[i].throughput);
262 outf(&o, " registers gpr=%ux%u vec=%ux%u\n", v->gpr_count, v->gpr_width, v->vec_count, v->vec_width);
263 for (size_t i = 0; i < v->cache_count; ++i)
264 outf(&o, " cache L%u%s size=%" PRIu64 " line=%u assoc=%u latency=%u\n", v->caches[i].level,
265 v->caches[i].is_instruction ? "I" : (v->caches[i].level == 1 ? "D" : " "), v->caches[i].size, v->caches[i].line,
266 v->caches[i].assoc, v->caches[i].latency);
267 outf(&o, " dram base=0x%" PRIx64 " size=%" PRIu64 "\n", v->dram_base, v->dram_size);
268 outf(&o, " physics flag=%s seal=%s\n", v->physics_authorized ? "set" : "unset",
269 v->physics_seal_is_placeholder ? "builder-constant placeholder (not a receipt)" : "non-constant");
270 outf(&o, " topology %s%s\n", v->topology_valid ? "valid" : "INVALID: ",
271 v->topology_valid ? "" : v->topology_error);
272 for (size_t i = 0; i < v->target_count; ++i)
273 outf(&o, " target %-22s linked=%s runnable=%s id=%s\n note: %s\n", v->targets[i].name,
274 v->targets[i].linked ? "yes" : "no", v->targets[i].runnable ? "yes" : "no",
275 v->targets[i].machine_id, v->targets[i].note ? v->targets[i].note : "");
276 return o.trunc ? -1 : (int)o.pos;
277}
278
279int visor_machine_format_json(const VisorMachineView *v, char *out, size_t n) {
280 if (!v || !out || n == 0) return -1;
281 Out o = { out, n, 0, false };
282 out[0] = '\0';
283 outf(&o, "{\"name\":"); json_str(&o, v->name);
284 outf(&o, ",\"id\":"); json_str(&o, v->id_text);
285 outf(&o, ",\"target_profile\":%u,\"observed\":%s,\"provenance\":", v->target_profile,
286 v->observed ? "true" : "false");
287 json_str(&o, v->provenance);
288 outf(&o, ",\"pipeline\":{\"issue_width\":%u,\"max_in_flight\":%u,\"out_of_order\":%s,\"units\":[",
289 v->issue_width, v->max_in_flight, v->out_of_order ? "true" : "false");
290 for (size_t i = 0; i < v->unit_count; ++i)
291 outf(&o, "%s{\"type\":\"%s\",\"count\":%u,\"latency\":%u,\"throughput\":%u}", i ? "," : "",
292 v->units[i].type_name, v->units[i].count, v->units[i].latency, v->units[i].throughput);
293 outf(&o, "]},\"registers\":{\"gpr_count\":%u,\"gpr_width\":%u,\"vec_count\":%u,\"vec_width\":%u},\"caches\":[",
294 v->gpr_count, v->gpr_width, v->vec_count, v->vec_width);
295 for (size_t i = 0; i < v->cache_count; ++i)
296 outf(&o, "%s{\"level\":%u,\"instruction\":%s,\"size\":%" PRIu64 ",\"line\":%u,\"assoc\":%u,\"latency\":%u}",
297 i ? "," : "", v->caches[i].level, v->caches[i].is_instruction ? "true" : "false",
298 v->caches[i].size, v->caches[i].line, v->caches[i].assoc, v->caches[i].latency);
299 outf(&o, "],\"dram_base\":%" PRIu64 ",\"dram_size\":%" PRIu64
300 ",\"physics_authorized\":%s,\"physics_seal_is_placeholder\":%s,\"topology_valid\":%s,\"topology_error\":",
301 v->dram_base, v->dram_size, v->physics_authorized ? "true" : "false",
302 v->physics_seal_is_placeholder ? "true" : "false", v->topology_valid ? "true" : "false");
303 json_str(&o, v->topology_error);
304 outf(&o, ",\"targets\":[");
305 for (size_t i = 0; i < v->target_count; ++i) {
306 outf(&o, "%s{\"name\":", i ? "," : "");
307 json_str(&o, v->targets[i].name);
308 outf(&o, ",\"machine_id\":");
309 json_str(&o, v->targets[i].machine_id);
310 outf(&o, ",\"linked\":%s,\"runnable\":%s,\"note\":", v->targets[i].linked ? "true" : "false",
311 v->targets[i].runnable ? "true" : "false");
312 json_str(&o, v->targets[i].note);
313 outf(&o, "}");
314 }
315 outf(&o, "]}");
316 return o.trunc ? -1 : (int)o.pos;
317}