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 / tests / fake_m16_native.c

500 lines · 17158 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 * Simulated GB10 driver. See tests/fake_m16_native.h for what it models.
19 * NOT_RUN: written in cut A3b2, never compiled or executed by its author.
20 *
21 * Linked INSTEAD of physics/m16/m16_native.c and physics/nvrm/nvrm.c. The
22 * physics headers are still included, so the types (Nvrm, NvrmMem,
23 * M16NativeContext) and the prototypes are the real ones.
24 */
25#include "fake_m16_native.h"
26#include "m16_native.h"
27
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33
34FakeDriver fk;
35
36typedef struct {
37 uint32_t handle;
38 void *ptr;
39 uint64_t size;
40} LiveAlloc;
41
42static LiveAlloc g_live[FK_LIVE_MAX];
43static uint32_t g_next_handle;
44static void *g_chan_pb; /* the channel's own 4 KiB pushbuffer, not counted in fk.live */
45
46/* ---- test-side helpers ---------------------------------------------------- */
47
48void fake_release_all(void)
49{
50 size_t i;
51 for (i = 0; i < FK_LIVE_MAX; i++) {
52 if (g_live[i].handle != 0) {
53 free(g_live[i].ptr);
54 g_live[i].handle = 0;
55 g_live[i].ptr = NULL;
56 }
57 }
58 fk.live = 0;
59 free(g_chan_pb);
60 g_chan_pb = NULL;
61}
62
63void fake_reset(void)
64{
65 fake_release_all();
66 memset(&fk, 0, sizeof fk);
67 g_next_handle = 0;
68}
69
70uint64_t fake_now_ms(void)
71{
72 return fk.clock_ms;
73}
74
75int fake_count(int kind)
76{
77 int i;
78 int n = 0;
79 for (i = 0; i < fk.nev; i++)
80 if (fk.ev[i].kind == kind) n++;
81 return n;
82}
83
84int fake_nth(int kind, int nth)
85{
86 int i;
87 int n = 0;
88 for (i = 0; i < fk.nev; i++) {
89 if (fk.ev[i].kind != kind) continue;
90 n++;
91 if (n == nth) return i;
92 }
93 return -1;
94}
95
96/* ---- internal ------------------------------------------------------------- */
97
98static void logev(int kind, uint64_t a, uint64_t b, uint64_t c)
99{
100 if (fk.nev >= FK_EV_MAX) {
101 fk.ev_overflow = 1;
102 return;
103 }
104 fk.ev[fk.nev].kind = kind;
105 fk.ev[fk.nev].a = a;
106 fk.ev[fk.nev].b = b;
107 fk.ev[fk.nev].c = c;
108 fk.nev++;
109}
110
111/* A failed driver call: errno and the driver's own text, like the real nvrm does. */
112static int inject(Nvrm *rm, int rc)
113{
114 errno = EIO;
115 if (rm) (void)snprintf(rm->err, sizeof rm->err, "%s", FK_TEXT);
116 return rc;
117}
118
119static void live_add(uint32_t handle, void *ptr, uint64_t size)
120{
121 size_t i;
122 for (i = 0; i < FK_LIVE_MAX; i++) {
123 if (g_live[i].handle == 0) {
124 g_live[i].handle = handle;
125 g_live[i].ptr = ptr;
126 g_live[i].size = size;
127 fk.live++;
128 return;
129 }
130 }
131 free(ptr); /* the table is full: more live buffers than any test needs */
132}
133
134/* Returns 1 if the handle was live and is now released, else 0. */
135static int live_release(uint32_t handle)
136{
137 size_t i;
138 for (i = 0; i < FK_LIVE_MAX; i++) {
139 if (g_live[i].handle == handle) {
140 free(g_live[i].ptr);
141 g_live[i].handle = 0;
142 g_live[i].ptr = NULL;
143 fk.live--;
144 return 1;
145 }
146 }
147 return 0;
148}
149
150static volatile uint32_t *word_at(uint64_t va)
151{
152 return (volatile uint32_t *)(uintptr_t)va;
153}
154
155/* ---- the simulated GPU: pushbuffer parser and kernel ---------------------- */
156
157/* The vector kernel: QMD1 words give the program address (32, 33), the thread
158 * count (34 and 39), the constant bank (42, 43) and the completion semaphore
159 * (15, 16, 17), as written by src/omega_blackwell_qmd.c. The arguments are read
160 * from constant bank + 0x380: a, b, c addresses (lo, hi) and the element count. */
161static void run_kernel(const uint32_t *q)
162{
163 uint64_t prog = ((uint64_t)(q[33] & 0x1fffffu) << 36) | ((uint64_t)q[32] << 4);
164 uint64_t cb = ((uint64_t)(q[43] & 0x7ffffu) << 38) | ((uint64_t)q[42] << 6);
165 uint64_t threads = (uint64_t)(q[34] & 0xffffu) * (uint64_t)q[39];
166 const uint32_t *args = (const uint32_t *)(uintptr_t)(cb + 0x380u);
167 uint32_t *a = (uint32_t *)(uintptr_t)(((uint64_t)args[1] << 32) | args[0]);
168 uint32_t *b = (uint32_t *)(uintptr_t)(((uint64_t)args[3] << 32) | args[2]);
169 uint32_t *c = (uint32_t *)(uintptr_t)(((uint64_t)args[5] << 32) | args[4]);
170 uint32_t n = args[6];
171 uint64_t sem = ((uint64_t)q[16] << 32) | q[15];
172
173 if (fk.kernel_mode == FK_KERNEL_SKIP) { /* the kernel is not modelled: count the launch, release the semaphore */
174 fk.kernel_ran++;
175 if (fk.sem_mode != FK_SYNC_NEVER) *word_at(sem) = q[17] + (fk.sem_mode == FK_SYNC_OVERSHOOT ? 1u : 0u);
176 return;
177 }
178 uint64_t count = n < threads ? n : threads;
179 uint32_t i;
180 int ok = 1;
181
182 fk.kernel_ran++;
183 fk.kernel_threads = threads;
184 fk.arg_a = (uint64_t)(uintptr_t)a;
185 fk.arg_b = (uint64_t)(uintptr_t)b;
186 fk.arg_c = (uint64_t)(uintptr_t)c;
187 fk.prog_va_seen = prog;
188 fk.cbank_va_seen = cb;
189 fk.kernel_n = n;
190 if (fk.prog_expect != NULL && fk.prog_len != 0)
191 ok = memcmp((const void *)(uintptr_t)prog, fk.prog_expect, fk.prog_len) == 0;
192 fk.prog_matched = ok;
193 logev(FK_EV_KERNEL, threads, n, 0);
194
195 /* What the device output holds just before the kernel runs, and whether the
196 * poison words collide with the correct sums. */
197 fk.snap_n = n < FK_SNAP_WORDS ? n : FK_SNAP_WORDS;
198 for (i = 0; i < fk.snap_n; i++) {
199 fk.out_before[i] = c[i];
200 if (c[i] == (uint32_t)(a[i] + b[i])) fk.poison_collisions++;
201 }
202
203 if (ok && fk.kernel_mode != FK_KERNEL_NO_WRITE) {
204 if (fk.kernel_mode == FK_KERNEL_HALF) count /= 2u;
205 for (i = 0; i < count; i++) {
206 c[i] = (uint32_t)(a[i] + b[i]);
207 if (fk.kernel_mode == FK_KERNEL_WRONG) c[i] += 1u;
208 }
209 }
210
211 /* Completion semaphore (RELEASE_SEMAPHORE0 of the QMD): value q[17]. */
212 if (fk.sem_mode != FK_SYNC_NEVER)
213 *word_at(sem) = q[17] + (fk.sem_mode == FK_SYNC_OVERSHOOT ? 1u : 0u);
214}
215
216/* SEND_PCAS_A carries the QMD address >> 8 in 32 bits (a 40-bit GPU address). The fake's GPU address is the host
217 * pointer, which can be wider: find the live allocation whose low 40 bits match. NULL if none does. */
218static const uint32_t *qmd_from_shifted8(uint32_t s8)
219{
220 const uint64_t low = (uint64_t)s8 << 8, mask40 = (1ull << 40) - 1u;
221 size_t i;
222 for (i = 0; i < FK_LIVE_MAX; i++) {
223 uint64_t base = (uint64_t)(uintptr_t)g_live[i].ptr, a = (base & ~mask40) | low;
224 if (g_live[i].handle == 0) continue;
225 if (a < base) a += 1ull << 40;
226 if (a >= base && a + 96u * 4u <= base + g_live[i].size) return (const uint32_t *)(uintptr_t)a;
227 }
228 return NULL;
229}
230
231/* Walks the submitted pushbuffer. exec == 0 only collects addresses (so the
232 * state at the doorbell can be captured first); exec == 1 runs it. Every packet
233 * is a header (type 2 or 6 in bits 31:28, count in 28:16, subchannel in 15:13,
234 * method in 11:0 times 4) followed by count data words. */
235static void walk(const uint32_t *w, size_t n, int exec)
236{
237 size_t i = 0;
238 uint64_t dst = 0;
239 uint32_t line_bytes = 0;
240 uint32_t lines = 0;
241 int nrel = 0;
242 int flushed = 0;
243
244 while (i < n) {
245 uint32_t h = w[i++];
246 uint32_t type = h >> 28;
247 uint32_t cnt = (h >> 16) & 0x1fffu;
248 uint32_t subc = (h >> 13) & 7u;
249 uint32_t mthd = (h & 0xfffu) << 2;
250 const uint32_t *d;
251
252 if ((type != 2u && type != 6u) || i + cnt > n) {
253 fk.pb_parse_error = 1;
254 return;
255 }
256 d = &w[i];
257 i += cnt;
258
259 if (subc == 1u && mthd == 0x0188u && cnt == 2u) {
260 dst = ((uint64_t)d[0] << 32) | d[1];
261 } else if (subc == 1u && mthd == 0x0180u && cnt == 2u) {
262 line_bytes = d[0];
263 lines = d[1];
264 } else if (subc == 1u && mthd == 0x01b4u) {
265 if (dst == 0 || (uint64_t)cnt * 4u != (uint64_t)line_bytes * lines) {
266 fk.pb_parse_error = 1;
267 continue;
268 }
269 if (!exec) {
270 if (cnt == 1u) {
271 fk.sem_va = dst;
272 fk.sem_init_value = d[0];
273 }
274 } else {
275 memcpy((void *)(uintptr_t)dst, d, (size_t)cnt * 4u);
276 if (cnt == 224u) logev(FK_EV_PB_CBANK, dst, cnt, 0);
277 else if (cnt == 7u) logev(FK_EV_PB_ARGS, dst, cnt, 0);
278 else if (cnt == 1u) logev(FK_EV_PB_SEM_INIT, dst, d[0], 0);
279 }
280 } else if (subc == 1u && mthd == 0x0318u && cnt == 98u) {
281 uint64_t qaddr = ((uint64_t)(d[0] & 0x1ffu) << 40) | ((uint64_t)d[1] << 8);
282 const uint32_t *q = d + 2;
283 int launch = ((d[0] >> 30) & 1u) != 0;
284 if (exec) {
285 if (qaddr == 0) {
286 fk.pb_parse_error = 1;
287 continue;
288 }
289 memcpy((void *)(uintptr_t)qaddr, q, 96u * 4u);
290 logev(FK_EV_PB_QMD, qaddr, (launch && (q[9] & 1u) != 0) ? 1u : 0u, 0);
291 if (launch && (q[9] & 1u) != 0) run_kernel(q);
292 }
293 } else if (subc == 1u && mthd == 0x02b4u && cnt == 1u) {
294 /* SEND_PCAS_A (clcec0.h, QMD address >> 8): a standalone QMD the command list (src/omega_gpu_cmdlist.c)
295 * wrote to memory. Modelled only with FK_KERNEL_SKIP: the launch releases its completion semaphore. */
296 const uint32_t *q = exec && fk.kernel_mode == FK_KERNEL_SKIP ? qmd_from_shifted8(d[0]) : NULL;
297 if (q && (q[9] & 1u) != 0) run_kernel(q);
298 } else if (subc == 0u && mthd == 0x005cu && cnt == 5u) {
299 uint64_t addr = ((uint64_t)d[1] << 32) | d[0];
300 uint32_t payload = d[2];
301 uint32_t flags = d[4];
302 nrel++;
303 if (!exec) {
304 if (nrel == 1) fk.marker1_va = addr;
305 else if (nrel == 2) fk.marker2_va = addr;
306 } else {
307 int mode = nrel == 1 ? fk.marker_mode : fk.marker2_mode;
308 int writes = (flags & 1u) != 0;
309 logev(FK_EV_PB_RELEASE, addr, payload, flags);
310 if (nrel <= 2) fk.release_flags[nrel - 1] = flags;
311 /* Modelling choice: the second release only lands if an L2 flush
312 * memory operation came between the two releases. */
313 if (nrel == 2 && !flushed) writes = 0;
314 if (mode == FK_SYNC_NEVER) writes = 0;
315 if (writes && addr != 0)
316 *word_at(addr) = payload + (mode == FK_SYNC_OVERSHOOT ? 1u : 0u);
317 flushed = 0;
318 }
319 } else if (subc == 0u && mthd == 0x0028u && cnt == 4u) {
320 if (exec) {
321 logev(FK_EV_PB_FLUSH, d[3], 0, 0);
322 if (d[3] == (0x10u << 27)) flushed = 1;
323 }
324 }
325 /* every other packet is a setup method: ignored */
326 }
327}
328
329/* ---- the seven faked symbols ---------------------------------------------- */
330
331int m16_native_open(M16NativeContext *ctx)
332{
333 if (!ctx) return -1;
334 memset(ctx, 0, sizeof *ctx);
335 fk.open_calls++;
336 logev(FK_EV_OPEN, 0, 0, 0);
337 if (fk.open_fail) return inject(&ctx->rm, -1);
338 fk.device_open = 1;
339 return 0;
340}
341
342int m16_native_create_channel(M16NativeContext *ctx)
343{
344 void *p = NULL;
345 if (!ctx) return -1;
346 fk.channel_calls++;
347 logev(FK_EV_CHANNEL, 0, 0, 0);
348 if (fk.channel_fail) return inject(&ctx->rm, -1);
349 if (posix_memalign(&p, 4096, 0x1000) != 0) return -1;
350 memset(p, 0, 0x1000);
351 ctx->pb_mem.handle = ++g_next_handle;
352 ctx->pb_mem.size = 0x1000;
353 ctx->pb_mem.cpu = p;
354 ctx->pb_mem.va = (uint64_t)(uintptr_t)p;
355 g_chan_pb = p;
356 fk.channel_up = 1;
357 return 0;
358}
359
360int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out)
361{
362 void *p = NULL;
363 int mode = -1;
364 fk.alloc_calls++;
365 logev(FK_EV_ALLOC, size, 0, 0);
366 if (!out || size == 0 || size > (fk.alloc_max ? fk.alloc_max : 0x100000u)) return -1;
367 if (fk.alloc_fail_nth != 0 && fk.alloc_fail_nth == fk.alloc_calls) {
368 mode = fk.alloc_mode;
369 if (mode == FK_ALLOC_FAIL) return inject(rm, -1);
370 }
371 if (posix_memalign(&p, 4096, (size_t)size) != 0) return -1;
372 memset(p, 0x5a, (size_t)size); /* dirty memory */
373 out->handle = ++g_next_handle;
374 out->size = size;
375 out->cpu = p;
376 out->va = (uint64_t)(uintptr_t)p;
377 live_add(out->handle, p, size);
378 if (fk.alloc_size_n < FK_SIZE_LOG) fk.alloc_size_log[fk.alloc_size_n] = size;
379 fk.alloc_size_n++;
380 if (fk.alloc_calls >= 1 && fk.alloc_calls <= 16) fk.alloc_va[fk.alloc_calls - 1] = out->va;
381 if (mode == FK_ALLOC_NO_CPU) out->cpu = NULL;
382 else if (mode == FK_ALLOC_SHORT) out->size = size / 2u;
383 else if (mode == FK_ALLOC_MISALIGNED) out->va += 0x10u;
384 return 0;
385}
386
387/* Same as nvrm_alloc (the fake has no GPU cache); only counts the uncached request. */
388int nvrm_alloc_gpu_uncached(Nvrm *rm, uint64_t size, NvrmMem *out)
389{
390 fk.uncached_calls++;
391 return nvrm_alloc(rm, size, out);
392}
393
394int nvrm_free(Nvrm *rm, NvrmMem *m)
395{
396 if (!m || m->handle == 0) return 0; /* a zeroed NvrmMem is a no-op (nvrm.h) */
397 fk.free_calls++;
398 logev(FK_EV_FREE, m->handle, 0, 0);
399 if (fk.free_fail_nth != 0 && fk.free_fail_nth == fk.free_calls) return inject(rm, -1);
400 if (fk.free_size_n < FK_SIZE_LOG) fk.free_size_log[fk.free_size_n] = m->size;
401 fk.free_size_n++;
402 if (!live_release(m->handle)) {
403 fk.double_free++;
404 errno = EINVAL;
405 return -1;
406 }
407 memset(m, 0, sizeof *m);
408 errno = EBADF; /* a real free runs munmap and ioctl calls, which may change errno */
409 return 0;
410}
411
412int m16_native_submit_methods(M16NativeContext *ctx, const uint32_t *methods, size_t count)
413{
414 if (!ctx || !methods || count == 0) return -1;
415 fk.submit_calls++;
416 logev(FK_EV_SUBMIT, count, 0, 0);
417 if (fk.submit_fail && fk.submit_faults_device) ctx->rm.faulted = 1;
418 if (!fk.channel_up || fk.submit_fail) return inject(&ctx->rm, -1);
419 /* Like m16_native_enqueue_methods: the words must fit the ring buffer. */
420 if (count > ctx->pb_mem.size / sizeof(uint32_t) || count > FK_PB_MAX) return inject(&ctx->rm, -1);
421 fk.pb_mem_size = ctx->pb_mem.size;
422 memcpy(fk.pb, methods, count * sizeof(uint32_t));
423 fk.pb_len = count;
424
425 walk(fk.pb, count, 0);
426 if (fk.marker1_va != 0 && fk.sem_va != 0) {
427 fk.pre_marker1 = *word_at(fk.marker1_va);
428 fk.pre_marker2 = fk.marker2_va != 0 ? *word_at(fk.marker2_va) : 0u;
429 fk.pre_sem = *word_at(fk.sem_va);
430 fk.pre_valid = 1;
431 }
432 walk(fk.pb, count, 1);
433 return 0;
434}
435
436static int which_word(volatile uint32_t *p)
437{
438 uint64_t a = (uint64_t)(uintptr_t)p;
439 if (p == NULL) return FK_W_UNKNOWN;
440 if (fk.marker1_va != 0 && a == fk.marker1_va) return FK_W_MARKER1;
441 if (fk.marker2_va != 0 && a == fk.marker2_va) return FK_W_MARKER2;
442 if (fk.sem_va != 0 && a == fk.sem_va) return FK_W_SEM;
443 return FK_W_UNKNOWN;
444}
445
446/* The real wait is a ">=" serial compare in a loop with a deadline. The fake
447 * checks once and, if it is not satisfied, "waits" the whole timeout by
448 * advancing the fake clock. */
449int m16_native_wait_marker(volatile uint32_t *marker, uint32_t expected, uint64_t timeout_ms)
450{
451 int w = which_word(marker);
452 fk.wait_calls++;
453 fk.wait_n[w]++;
454 fk.wait_ms[w] = timeout_ms;
455 fk.wait_expected[w] = expected;
456 logev(FK_EV_WAIT, (uint64_t)w, expected, timeout_ms);
457 if (!marker) return -1;
458 if ((int32_t)(*marker - expected) >= 0) return 0;
459 fk.clock_ms += timeout_ms;
460 return -1;
461}
462
463int m16_native_close(M16NativeContext *ctx)
464{
465 int rc = 0;
466 if (!ctx) return -1;
467 fk.close_calls++;
468 fk.live_at_close = fk.live;
469 logev(FK_EV_CLOSE, (uint64_t)fk.live, 0, 0);
470 /* Like nvrm_close: whatever is still allocated is released here. */
471 fake_release_all();
472 fk.device_open = 0;
473 if (fk.close_fail) rc = inject(&ctx->rm, -1);
474 return rc;
475}
476
477/* L136: the simulated driver has one channel. Extra channels (AIEN_OMEGA_GPU_CHANNELS > 1) are refused here, so the
478 * platform's device_open refuses with stage "nvrm_channel_n"; the multi-channel path is proven on the chip only. */
479int nvrm_channel_n(Nvrm *rm, uint32_t extra)
480{
481 (void)rm; (void)extra;
482 return -1;
483}
484
485int nvrm_chan_submit(Nvrm *rm, uint32_t ch, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords)
486{
487 (void)rm; (void)ch; (void)pb; (void)off_bytes; (void)nwords;
488 return -1;
489}
490
491void nvrm_chan_retire(Nvrm *rm, uint32_t ch, uint32_t upto)
492{
493 (void)rm; (void)ch; (void)upto;
494}
495
496uint32_t nvrm_chan_put(const Nvrm *rm, uint32_t ch)
497{
498 (void)rm; (void)ch;
499 return 0;
500}