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
omega-mv / tests / fake_m16_native.h
| 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 for the host test of the Blackwell GPU engine backend |
| 19 | * (tests/test_omega_blackwell_engine.c). NOT_RUN: written in cut A3b2 and never |
| 20 | * compiled or executed by its author. |
| 21 | * |
| 22 | * It replaces seven symbols of the real driver stack, so the REAL backend |
| 23 | * (src/omega_blackwell_engine.c) and the REAL vector wrapper |
| 24 | * (src/omega_blackwell_submit.c) run unchanged on a machine with no GPU: |
| 25 | * m16_native_open, m16_native_create_channel, m16_native_submit_methods, |
| 26 | * m16_native_wait_marker, m16_native_close, nvrm_alloc, nvrm_free. |
| 27 | * The signatures come from physics/m16/m16_native.h and physics/nvrm/nvrm.h at |
| 28 | * the physics pin; the fake file includes those headers, so a mismatch is a |
| 29 | * compile error. |
| 30 | * |
| 31 | * What it models (every point is a MODEL, UNVERIFIED on the chip, confidence |
| 32 | * about 70 percent that it matches the real driver): |
| 33 | * - "device" memory is host memory, GPU address equals CPU address, dirty |
| 34 | * (0x5a fill) so a missing zeroing step shows up; |
| 35 | * - the submitted pushbuffer is parsed and executed in order: DMA uploads, |
| 36 | * the two inline QMDs, the semaphore initial value, the release packets and |
| 37 | * the L2 flush memory operation (method 0x28, operation 0x10 << 27); |
| 38 | * - the vector kernel is a simple loop (c[i] = a[i] + b[i] for the threads the |
| 39 | * QMD asks for) and only runs if the program bytes in device memory are the |
| 40 | * bytes the test expects; |
| 41 | * - the second marker release is only written if an L2 flush operation |
| 42 | * appears between the two releases (modelling choice, see the doc); |
| 43 | * - m16_native_wait_marker is the real ">=" serial compare with no loop; a |
| 44 | * timeout advances the fake clock by the timeout so waited_ms is exact; |
| 45 | * - nvrm_free of a zeroed NvrmMem is a no-op that counts nothing, a real free |
| 46 | * zeroes the NvrmMem and clobbers errno (like a munmap or ioctl might). |
| 47 | */ |
| 48 | #ifndef FAKE_M16_NATIVE_H |
| 49 | #define FAKE_M16_NATIVE_H |
| 50 | |
| 51 | #include <stddef.h> |
| 52 | #include <stdint.h> |
| 53 | |
| 54 | #define FK_TEXT "fake driver says no" |
| 55 | #define FK_EV_MAX 512 |
| 56 | #define FK_SNAP_WORDS 256 |
| 57 | #define FK_PB_MAX 16384 /* the whole 64 KiB session ring (omega_gpu_cmdlist.c RING_WORDS), so a near-full command list fits */ |
| 58 | |
| 59 | /* Event kinds in the log (fk.ev). */ |
| 60 | enum { |
| 61 | FK_EV_OPEN = 1, |
| 62 | FK_EV_CHANNEL, |
| 63 | FK_EV_ALLOC, /* a = bytes asked */ |
| 64 | FK_EV_FREE, /* a = handle */ |
| 65 | FK_EV_SUBMIT, /* a = word count */ |
| 66 | FK_EV_WAIT, /* a = FK_W_*, b = expected value, c = timeout ms */ |
| 67 | FK_EV_CLOSE, /* a = live allocations at the moment of close */ |
| 68 | FK_EV_PB_CBANK, /* a = destination, b = words (224) */ |
| 69 | FK_EV_PB_ARGS, /* a = destination, b = words (7) */ |
| 70 | FK_EV_PB_SEM_INIT, /* a = destination, b = value */ |
| 71 | FK_EV_PB_QMD, /* a = QMD address, b = 1 if it is the kernel QMD */ |
| 72 | FK_EV_PB_RELEASE, /* a = address, b = payload, c = flags word */ |
| 73 | FK_EV_PB_FLUSH, /* a = operation word (last word of the packet) */ |
| 74 | FK_EV_KERNEL /* a = threads, b = element count */ |
| 75 | }; |
| 76 | |
| 77 | /* Which coherent word a wait looked at. */ |
| 78 | enum { FK_W_UNKNOWN = 0, FK_W_MARKER1, FK_W_MARKER2, FK_W_SEM, FK_W_COUNT }; |
| 79 | |
| 80 | enum { FK_ALLOC_FAIL = 0, FK_ALLOC_NO_CPU, FK_ALLOC_SHORT, FK_ALLOC_MISALIGNED }; |
| 81 | enum { FK_SYNC_NORMAL = 0, FK_SYNC_NEVER, FK_SYNC_OVERSHOOT }; |
| 82 | /* FK_KERNEL_SKIP: run no kernel at all and only release the completion semaphore (for tests that count |
| 83 | * driver calls with kernels the fake does not model, such as the matmul and attention launches). */ |
| 84 | enum { FK_KERNEL_OK = 0, FK_KERNEL_NO_WRITE, FK_KERNEL_HALF, FK_KERNEL_WRONG, FK_KERNEL_SKIP }; |
| 85 | |
| 86 | #define FK_SIZE_LOG 8192 |
| 87 | #define FK_LIVE_MAX 128 /* live driver buffers the fake can track at once */ |
| 88 | |
| 89 | typedef struct { |
| 90 | int kind; |
| 91 | uint64_t a; |
| 92 | uint64_t b; |
| 93 | uint64_t c; |
| 94 | } FakeEvent; |
| 95 | |
| 96 | typedef struct { |
| 97 | /* ---- faults and behaviour, set by the test after fake_reset ---- */ |
| 98 | int open_fail; |
| 99 | int channel_fail; |
| 100 | int submit_fail; |
| 101 | int submit_faults_device; /* with submit_fail: the failed submit also sets rm.faulted, as a channel error does */ |
| 102 | int close_fail; |
| 103 | uint64_t alloc_max; /* largest nvrm_alloc the fake grants, 0 = the default 1 MiB */ |
| 104 | int alloc_fail_nth; /* 1-based nvrm_alloc call that misbehaves, 0 = none */ |
| 105 | int alloc_mode; /* FK_ALLOC_* for that call */ |
| 106 | int free_fail_nth; /* 1-based real nvrm_free call that fails, 0 = none */ |
| 107 | int marker_mode; /* FK_SYNC_*: first marker release */ |
| 108 | int marker2_mode; /* FK_SYNC_*: second marker release */ |
| 109 | int sem_mode; /* FK_SYNC_*: kernel completion semaphore */ |
| 110 | int kernel_mode; /* FK_KERNEL_* */ |
| 111 | const void *prog_expect; /* program bytes the device memory must hold, NULL = do not check */ |
| 112 | size_t prog_len; |
| 113 | |
| 114 | /* ---- observations ---- */ |
| 115 | uint64_t clock_ms; /* the fake millisecond clock (fake_now_ms) */ |
| 116 | int open_calls; |
| 117 | int channel_calls; |
| 118 | int alloc_calls; |
| 119 | int uncached_calls; /* allocations made through nvrm_alloc_gpu_uncached (also counted in alloc_calls) */ |
| 120 | int free_calls; /* real frees only (a zeroed NvrmMem is not counted) */ |
| 121 | int close_calls; |
| 122 | int submit_calls; |
| 123 | int wait_calls; |
| 124 | int double_free; /* frees of a handle that was not live */ |
| 125 | int live; /* live backend allocations right now (not the channel's own 4 KiB buffer) */ |
| 126 | int live_at_close; /* live allocations when m16_native_close was called */ |
| 127 | int device_open; |
| 128 | int channel_up; |
| 129 | uint64_t pb_mem_size; /* ctx->pb_mem.size when the pushbuffer was submitted */ |
| 130 | int pb_parse_error; /* the pushbuffer had a packet the fake could not parse */ |
| 131 | uint32_t pb[FK_PB_MAX]; |
| 132 | size_t pb_len; |
| 133 | |
| 134 | uint64_t marker1_va; /* first release address, from the pushbuffer */ |
| 135 | uint64_t marker2_va; /* second release address, 0 with NO_C3 */ |
| 136 | uint64_t sem_va; /* destination of the semaphore initial value DMA */ |
| 137 | uint32_t sem_init_value; |
| 138 | uint32_t pre_marker1; /* values the GPU would see at the doorbell */ |
| 139 | uint32_t pre_marker2; |
| 140 | uint32_t pre_sem; |
| 141 | int pre_valid; /* the three pre_* values were captured */ |
| 142 | int kernel_ran; |
| 143 | int prog_matched; |
| 144 | uint64_t kernel_threads; |
| 145 | uint64_t kernel_n; |
| 146 | uint32_t out_before[FK_SNAP_WORDS]; /* device output words just before the kernel ran */ |
| 147 | size_t snap_n; |
| 148 | int poison_collisions; /* snapshot words equal to the correct sum (a bad poison word) */ |
| 149 | uint32_t release_flags[2]; |
| 150 | uint64_t alloc_va[16]; /* GPU address of the nth nvrm_alloc (1-based nth stored at [nth - 1]) */ |
| 151 | uint64_t arg_a; /* kernel argument addresses the device saw in the constant bank */ |
| 152 | uint64_t arg_b; |
| 153 | uint64_t arg_c; |
| 154 | uint64_t prog_va_seen; /* program address from QMD1 */ |
| 155 | uint64_t cbank_va_seen;/* constant bank address from QMD1 */ |
| 156 | |
| 157 | int wait_n[FK_W_COUNT]; |
| 158 | uint64_t wait_ms[FK_W_COUNT]; |
| 159 | uint32_t wait_expected[FK_W_COUNT]; |
| 160 | |
| 161 | uint64_t alloc_size_log[FK_SIZE_LOG]; /* size asked by the nth granted nvrm_alloc (0-based), first FK_SIZE_LOG only */ |
| 162 | int alloc_size_n; |
| 163 | uint64_t free_size_log[FK_SIZE_LOG]; /* size of the nth real nvrm_free */ |
| 164 | int free_size_n; |
| 165 | |
| 166 | FakeEvent ev[FK_EV_MAX]; |
| 167 | int nev; |
| 168 | int ev_overflow; |
| 169 | } FakeDriver; |
| 170 | |
| 171 | extern FakeDriver fk; |
| 172 | |
| 173 | /* Frees every host allocation the fake still holds (the test's own teardown, not a driver call). */ |
| 174 | void fake_release_all(void); |
| 175 | /* fake_release_all, then zero the whole state. Call at the start of every case. */ |
| 176 | void fake_reset(void); |
| 177 | /* Millisecond clock for omega_gpu_engine_test_set_clock. */ |
| 178 | uint64_t fake_now_ms(void); |
| 179 | /* Number of events of this kind in the log. */ |
| 180 | int fake_count(int kind); |
| 181 | /* Index of the nth (1-based) event of this kind, or -1. */ |
| 182 | int fake_nth(int kind, int nth); |
| 183 | |
| 184 | #endif |