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

288 lines · 15652 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/* test_omega_gpu_platform_linux: the Linux REFERENCE platform (src/omega_gpu_platform_linux.c, reference only) on the
18 * simulated driver (tests/fake_m16_native.c). Host program, hosted build, opens no device.
19 *
20 * What it checks: the reference claims exactly the session service set (no MMIO, no IRQ: polling is never claimed as
21 * an interrupt) and passes the contract check; the hosted session installs it by default; the session makes the same
22 * driver calls in the same order as before P07 cut C (channel, then the four session buffers through
23 * nvrm_alloc_gpu_uncached, one submit with the C3 tail, both marker waits, close); the reference refuses a GPU-cached
24 * buffer, a poll of a buffer that is not HOST_POLLED, freeing the live queue ring, an oversize submit and every handle
25 * of a closed device; the driver's own failure text survives an open failure; a marker that never lands latches the
26 * session, which then frees nothing and closes nothing. Each case runs in its own child process.
27 */
28#include "fake_m16_native.h"
29#include "nvrm.h"
30#include "omega_blackwell_submit.h"
31#include "omega_gpu_cmdlist.h"
32#include "omega_gpu_mem_cached.h"
33#include "omega_gpu_platform_linux.h"
34#include "omega_gpu_session.h"
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/wait.h>
40#include <unistd.h>
41
42/* The three driver symbols the session path needs that the fake (written for the vector engine) lacks. */
43static int g_open_fail;
44int nvrm_open(Nvrm *rm) {
45 if (g_open_fail) { snprintf(rm->err, sizeof rm->err, "fake RM: open refused"); return -1; }
46 fk.device_open = 1; return 0;
47}
48int nvrm_close(Nvrm *rm) { (void)rm; return 0; }
49static int g_retires;
50void nvrm_retire(Nvrm *rm, uint32_t upto) { (void)rm; (void)upto; g_retires++; }
51
52static int case_ok;
53#define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0)
54
55static const uint32_t ARGS[4] = {1, 2, 3, 4};
56static OmegaGpuLaunch launch_desc(uint64_t timeout_ms) {
57 OmegaGpuLaunch L = { .code_va = 0x200000000ull, .gpr_count = 64, .threads_x = 32, .grid_x = 1, .grid_y = 1,
58 .num_elements = 32, .args = ARGS, .n_args = 4, .timeout_ms = timeout_ms };
59 return L;
60}
61
62static void c_caps(void) {
63 OmegaGpuPlatformCaps caps;
64 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&caps);
65 EXPECT(p != NULL);
66 EXPECT(caps.services == OMEGA_GPU_SVC_REQUIRED_SESSION);
67 EXPECT(!(caps.services & OMEGA_GPU_SVC_IRQ) && !(caps.services & OMEGA_GPU_SVC_MMIO));
68 EXPECT(p->irq_wait == NULL && p->mmio_read32 == NULL && p->mmio_write32 == NULL);
69 EXPECT(omega_gpu_platform_check_for(p, &caps, OMEGA_GPU_SVC_REQUIRED_SESSION) == OMEGA_GPU_PLAT_OK);
70 EXPECT(caps.abi_major == 1 && caps.abi_minor == 2);
71}
72
73static void c_default_install(void) {
74 fake_reset();
75 EXPECT(omega_gpu_session_platform() == omega_gpu_platform_linux(NULL)); /* hosted: the reference by default */
76 EXPECT(omega_gpu_session_completion_mode() == OMEGA_GPU_COMPLETION_POLLING);
77}
78
79static void c_same_driver_calls(void) {
80 fake_reset();
81 fk.kernel_mode = FK_KERNEL_SKIP;
82 omega_gpu_session_lock();
83 OmegaGpuSession *s = omega_gpu_session_open();
84 EXPECT(s != NULL);
85 if (!s) { printf(" %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); return; }
86 EXPECT(fk.channel_calls == 1 && fk.uncached_calls == 4 && fk.alloc_calls == 4 && fk.live == 4);
87 const int ch = fake_nth(FK_EV_CHANNEL, 1), a1 = fake_nth(FK_EV_ALLOC, 1);
88 EXPECT(ch >= 0 && a1 > ch); /* channel first, then the session buffers */
89 EXPECT(fk.ev[fake_nth(FK_EV_ALLOC, 1)].a == 0x10000 && fk.ev[fake_nth(FK_EV_ALLOC, 2)].a == 0x1000 &&
90 fk.ev[fake_nth(FK_EV_ALLOC, 3)].a == 0x1000 && fk.ev[fake_nth(FK_EV_ALLOC, 4)].a == 0x10000);
91 OmegaGpuLaunch L = launch_desc(1000);
92 uint32_t mk = 0;
93 EXPECT(omega_gpu_session_launch(&L, NULL, &mk) == 0);
94 EXPECT(fk.submit_calls == 1 && fk.pb_mem_size == 0x10000);
95 EXPECT(fake_count(FK_EV_PB_RELEASE) == 2 && fake_count(FK_EV_PB_FLUSH) == 1); /* the C3 tail */
96 EXPECT(fk.marker1_va == s->marker.va && fk.marker2_va == s->marker.va + 0x10);
97 EXPECT(fk.sem_va == s->qmd.va + 0x2000 && fk.sem_init_value == OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT);
98 EXPECT(mk == OMEGA_BW_MARKER_COMPLETION_PAYLOAD && g_retires == 1);
99 omega_gpu_session_unlock();
100 omega_gpu_session_close();
101 EXPECT(fk.free_calls == 4 && fk.close_calls == 1 && fk.live_at_close == 0 && fk.double_free == 0);
102}
103
104static void c_reference_refusals(void) {
105 fake_reset();
106 OmegaGpuPlatformCaps caps;
107 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&caps);
108 OmegaGpuDeviceHandle d;
109 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_OK);
110 OmegaGpuDeviceHandle d2;
111 EXPECT(p->device_open(p->ctx, 0, &d2) == OMEGA_GPU_PLAT_REFUSED_LIMIT); /* one device context */
112 OmegaGpuBufferDesc cached = {.kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE,
113 .gpu_cache = OMEGA_GPU_CACHE_GPU_CACHED, .bytes = 4096};
114 OmegaGpuBufferHandle h; OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus;
115 const int allocs0 = fk.alloc_calls;
116 EXPECT(p->dma_alloc(p->ctx, d, &cached, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY);
117 cached.rights |= OMEGA_GPU_RIGHT_HOST_POLLED; /* polled and cached: the contract refuses it too */
118 EXPECT(p->dma_alloc(p->ctx, d, &cached, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY);
119 EXPECT(fk.alloc_calls == allocs0); /* refused before the driver */
120 OmegaGpuBufferDesc data = {.kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE,
121 .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096};
122 EXPECT(p->dma_alloc(p->ctx, d, &data, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK);
123 EXPECT(cpu.ptr != NULL && cpu.len >= 4096 && bus.iova == 0);
124 uint32_t seen = 0;
125 EXPECT(p->poll_word(p->ctx, h, 0, 1, 1, &seen) == OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS); /* not HOST_POLLED */
126 OmegaGpuBufferDesc ring = {.kind = OMEGA_GPU_HANDLE_BUF_QUEUE, .rights = OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ,
127 .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096};
128 OmegaGpuBufferHandle rh; OmegaGpuQueueHandle q;
129 EXPECT(p->dma_alloc(p->ctx, d, &ring, &rh, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK);
130 EXPECT(p->queue_open(p->ctx, d, h, &q) == OMEGA_GPU_PLAT_REFUSED_BAD_KIND); /* a DATA buffer is not a ring */
131 EXPECT(p->queue_open(p->ctx, d, rh, &q) == OMEGA_GPU_PLAT_OK);
132 EXPECT(p->dma_free(p->ctx, rh) == OMEGA_GPU_PLAT_REFUSED_BUSY); /* the open queue still uses it */
133 static uint32_t big[2048];
134 EXPECT(p->queue_submit(p->ctx, q, big, 2048) == OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH); /* 8 KiB into a 4 KiB ring */
135 EXPECT(fk.submit_calls == 0);
136 const int frees0 = fk.free_calls;
137 EXPECT(p->device_close(p->ctx, d) == OMEGA_GPU_PLAT_OK);
138 /* every handle of the closed device is stale */
139 EXPECT(p->dma_free(p->ctx, h) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE);
140 EXPECT(p->sync_cpu_to_gpu(p->ctx, h, 0, 4) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE);
141 EXPECT(p->queue_submit(p->ctx, q, big, 4) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE);
142 EXPECT(p->dma_alloc(p->ctx, d, &data, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE);
143 EXPECT(fk.free_calls == frees0);
144 EXPECT(strstr(p->diag_text(p->ctx), "STALE") != NULL);
145}
146
147static void c_open_failure_text(void) {
148 fake_reset();
149 fk.channel_fail = 1;
150 omega_gpu_session_lock();
151 EXPECT(omega_gpu_session_open() == NULL);
152 EXPECT(strncmp(omega_gpu_session_last_error(), "m16_native_create_channel: ", 27) == 0);
153 EXPECT(strstr(omega_gpu_session_last_error(), FK_TEXT) != NULL); /* the driver's own text survives */
154 fk.channel_fail = 0;
155 g_open_fail = 1;
156 EXPECT(omega_gpu_session_open() == NULL);
157 EXPECT(strcmp(omega_gpu_session_last_error(), "nvrm_open: fake RM: open refused") == 0);
158 g_open_fail = 0;
159 EXPECT(omega_gpu_session_open() != NULL && !omega_gpu_session_is_blocked());
160 omega_gpu_session_unlock();
161 omega_gpu_session_close();
162}
163
164static void c_marker_never_latches(void) {
165 fake_reset();
166 fk.kernel_mode = FK_KERNEL_SKIP;
167 fk.marker_mode = FK_SYNC_NEVER;
168 omega_gpu_session_lock();
169 EXPECT(omega_gpu_session_open() != NULL);
170 OmegaGpuLaunch L = launch_desc(5);
171 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1);
172 EXPECT(omega_gpu_session_is_blocked());
173 EXPECT(fk.wait_calls == 5 && fk.clock_ms == 5); /* five 1 ms poll slices, then uncertain */
174 EXPECT(g_retires == 0);
175 omega_gpu_session_unlock();
176 omega_gpu_session_close();
177 EXPECT(fk.free_calls == 0 && fk.close_calls == 0 && fk.live == 4); /* parked: nothing released, nothing closed */
178 OmegaGpuParkStats ps; omega_gpu_session_parked(&ps);
179 EXPECT(ps.buffers == 4);
180}
181
182/* omega #457: the L2_SYSMEM_NCOH_INVALIDATE prefix, MEM_OP_A..D = 0, 0, 0, 0x11 << 27 (clc96f.h:30-73). The fake logs
183 * every MEM_OP as FK_EV_PB_FLUSH with a = the operation word. */
184#define L2_INVAL_OP (0x11u << 27)
185static int count_l2_inval(void) {
186 int n = 0;
187 for (int i = 0; i < fk.nev; i++) n += fk.ev[i].kind == FK_EV_PB_FLUSH && fk.ev[i].a == L2_INVAL_OP;
188 return n;
189}
190
191static void c_l2_inval_prefix_once(void) {
192 fake_reset();
193 fk.kernel_mode = FK_KERNEL_SKIP;
194 OmegaGpuPlatformCaps caps;
195 (void)omega_gpu_platform_linux(&caps);
196 EXPECT(caps.submit_prefix_words == 5u); /* the cost the platform publishes is the prefix it writes */
197 omega_gpu_session_lock();
198 OmegaGpuSession *s = omega_gpu_session_open();
199 EXPECT(s != NULL);
200 if (!s) { omega_gpu_session_unlock(); return; }
201 const OmegaGpuPlatform *p = omega_gpu_session_platform();
202 OmegaGpuMem cached;
203 EXPECT(omega_gpu_session_alloc_cached(4096, &cached) == 0);
204 OmegaGpuLaunch L = launch_desc(1000);
205 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
206 EXPECT(count_l2_inval() == 0); /* no host write into a GPU-cached buffer yet: no prefix */
207 const size_t plain = fk.pb_len;
208 EXPECT(p->sync_cpu_to_gpu(p->ctx, s->marker.handle, 0, 4) == OMEGA_GPU_PLAT_OK); /* an uncached buffer: no prefix */
209 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
210 EXPECT(count_l2_inval() == 0 && fk.pb_len == plain);
211 EXPECT(p->sync_cpu_to_gpu(p->ctx, cached.handle, 0, 4096) == OMEGA_GPU_PLAT_OK);
212 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
213 EXPECT(count_l2_inval() == 1); /* exactly once, at the head of the next submission */
214 EXPECT(fk.pb_len == plain + 5u);
215 EXPECT(fk.pb[0] == ((2u << 28) | (4u << 16) | (0x0028u >> 2)) && fk.pb[1] == 0 && fk.pb[2] == 0 && fk.pb[3] == 0 &&
216 fk.pb[4] == L2_INVAL_OP);
217 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
218 EXPECT(count_l2_inval() == 1 && fk.pb_len == plain); /* consumed: the launch after it has none */
219 EXPECT(p->sync_gpu_to_cpu(p->ctx, cached.handle, 0, 4096) == OMEGA_GPU_PLAT_OK); /* GPU to CPU is no host write */
220 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
221 EXPECT(count_l2_inval() == 1 && fk.pb_len == plain);
222 EXPECT(fk.pb_parse_error == 0 && fk.ev_overflow == 0);
223 omega_gpu_session_unlock();
224}
225
226/* Fable review of omega #459: a stamped standalone list of >= 1167 launches filled one submission to 16381 words; with
227 * the 5-word prefix pending that is 16386 > 16384 ring words, refused BAD_LENGTH on every retry. The command list must
228 * budget the prefix the platform publishes (caps.submit_prefix_words). */
229/* 2048 launches (about 14 ring words each) fill the 16384-word ring so the list splits into exactly 2 submissions.
230 * OMEGA_GPU_CMDLIST_MAX_LAUNCHES is 4096 since L312, which would split into 4, so the count is pinned here. */
231#define NEAR_CAP_LAUNCHES 2048u
232static void c_cmdlist_near_cap_with_l2_inval_pending(void) {
233 fake_reset();
234 fk.kernel_mode = FK_KERNEL_SKIP;
235 fk.alloc_max = 4u << 20; /* the 2048-launch bank and QMD buffers are 2 MiB each */
236 OmegaGpuCmdList *cl = NULL;
237 EXPECT(omega_gpu_cmdlist_create(NEAR_CAP_LAUNCHES, &cl) == 0);
238 if (!cl) { printf(" %s\n", omega_gpu_session_last_error()); return; }
239 omega_gpu_session_lock();
240 OmegaGpuLaunch L = launch_desc(1000);
241 for (uint32_t i = 0; i < NEAR_CAP_LAUNCHES; i++) EXPECT(omega_gpu_cmdlist_add(cl, &L) == (int)i);
242 const OmegaGpuPlatform *p = omega_gpu_session_platform();
243 OmegaGpuMem cached;
244 EXPECT(omega_gpu_session_alloc_cached(4096, &cached) == 0);
245 EXPECT(p->sync_cpu_to_gpu(p->ctx, cached.handle, 0, 4096) == OMEGA_GPU_PLAT_OK); /* the prefix is now pending */
246 const int submits0 = fk.submit_calls;
247 OmegaGpuCmdListOpts o = { .mode = OMEGA_GPU_CMDLIST_PCAS, .barrier = OMEGA_GPU_CMDLIST_BARRIER_WFI, .timeout_ms = 1000, .stamps = 1 };
248 OmegaGpuCmdListStats st;
249 const int rc = omega_gpu_cmdlist_submit(cl, &o, &st);
250 if (rc != 0) printf(" cmdlist submit: %s (platform: %s)\n", omega_gpu_session_last_error(), p->diag_text(p->ctx));
251 EXPECT(rc == 0);
252 EXPECT(st.sems_done == NEAR_CAP_LAUNCHES && st.first_missing == -1);
253 EXPECT(st.submits == 2 && fk.submit_calls - submits0 == 2);
254 EXPECT(count_l2_inval() == 1); /* the first submission only */
255 for (int i = 0; i < fk.nev; i++)
256 if (fk.ev[i].kind == FK_EV_SUBMIT) EXPECT(fk.ev[i].a * 4u <= fk.pb_mem_size); /* prefix included, inside the ring */
257 EXPECT(fk.pb_parse_error == 0 && fk.ev_overflow == 0);
258 EXPECT(!omega_gpu_session_is_blocked() && !omega_gpu_cmdlist_is_latched());
259 omega_gpu_session_unlock();
260}
261
262typedef struct { const char *id; void (*fn)(void); } Case;
263static const Case CASES[] = {
264 {"LINUX_REF_CAPS_NO_MMIO_NO_IRQ", c_caps},
265 {"HOSTED_DEFAULT_IS_LINUX_REFERENCE", c_default_install},
266 {"SAME_DRIVER_CALLS_AND_ORDER_AS_BEFORE", c_same_driver_calls},
267 {"REFERENCE_REFUSALS_CACHED_POLLED_RING_STALE", c_reference_refusals},
268 {"OPEN_FAILURE_KEEPS_DRIVER_TEXT", c_open_failure_text},
269 {"MARKER_NEVER_LATCHES_AND_PARKS", c_marker_never_latches},
270 {"L2_INVAL_PREFIX_ONCE_AFTER_CACHED_HOST_WRITE", c_l2_inval_prefix_once},
271 {"CMDLIST_NEAR_CAP_SUBMITS_WITH_L2_INVAL_PENDING", c_cmdlist_near_cap_with_l2_inval_pending},
272};
273
274int main(void) {
275 int pass = 0, fail = 0;
276 for (size_t i = 0; i < sizeof CASES / sizeof CASES[0]; i++) {
277 fflush(stdout);
278 pid_t pid = fork();
279 if (pid == 0) { case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); }
280 int st = 0;
281 waitpid(pid, &st, 0);
282 const int ok = WIFEXITED(st) && WEXITSTATUS(st) == 0;
283 printf("%s %s\n", ok ? "PASS" : "FAIL", CASES[i].id);
284 if (ok) pass++; else fail++;
285 }
286 printf("test_omega_gpu_platform_linux (reference only, simulated driver): %d passed, %d failed\n", pass, fail);
287 return fail ? 1 : 0;
288}