Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/test_omega_blackwell_engine.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / test_omega_blackwell_engine.c` 1090 lines ยท 49466 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 * Host test of the REAL Blackwell backend (src/omega_blackwell_engine.c), the 19 * REAL engine core (src/omega_gpu_engine.c) and the REAL vector wrapper 20 * omega_blackwell_execute_vector (src/omega_blackwell_submit.c), driven through 21 * a SIMULATED driver (tests/fake_m16_native.c). Cut A3b2. NOT_RUN: this file has 22 * never been compiled or executed by its author (Phase A rule). 23 * 24 * No GPU, no device node, no physics C sources: the seven driver symbols the 25 * backend calls are replaced by the fake. Every check prints "[PASS] id" or 26 * "[FAIL] id"; the last line is "SUMMARY passed=N failed=M"; the exit status is 27 * nonzero on any failure. docs/numeric/OMEGA_GPU_ENGINE.md section 14.1 lists 28 * every id and the mutants that need it. 29 * 30 * Techniques: 31 * - A tracing copy of the real backend table logs every callback (so the call 32 * order the core and the backend produce together can be asserted) and 33 * forwards to the original. The real table is never edited. 34 * - The fake parses and executes the pushbuffer, so the descriptors, the 35 * release packets and the L2 flush are checked as the device would see them. 36 * - A retained run (uncertain completion) leaves the backend busy by design. 37 * The test asserts that, then drains the backend through its own table 38 * (free every role, close), exactly what a process exit would do. 39 * 40 * Failure names that no driver fault can reach through the real backend, and so 41 * are covered by the fake-backend test (tests/test_omega_gpu_engine.c) only: 42 * PREPARE (copy-in and build refuse only on arguments the core already 43 * validated, and alloc_checked catches bad buffers first as ALLOC), 44 * VISIBILITY_WAIT (the barrier always returns 0, copy_out refuses only on bad 45 * arguments), 46 * INTERNAL_INVARIANT (the table is complete). 47 * The CPU barrier (dsb sy) has no effect a host can observe; it is chip only. 48 */ 49 #include "omega_gpu_engine.h" 50 #include "omega_blackwell_engine.h" 51 #include "omega_blackwell_submit.h" 52 #include "omega_blackwell_encoder.h" 53 #include "omega_vector.h" 54 #include "fake_m16_native.h" 55 56 #include 57 #include 58 #include 59 #include 60 #include 61 62 #define N 100u /* not a multiple of 64: a grid rounded down leaves words unwritten */ 63 #define TMO_MARKER 111u 64 #define TMO_MARKER2 222u 65 #define TMO_RELEASE 333u 66 #define TMO_VISIBILITY 444u 67 68 #define MARKER1_PAYLOAD 0x44444444u 69 #define MARKER2_PAYLOAD 0x46464646u 70 #define SEM_INIT 5u 71 #define SEM_DONE 6u 72 #define RELEASE_FLAGS 0x00100001u /* RELEASE | WFI */ 73 #define FLUSH_OP (0x10u << 27) /* L2_FLUSH_DIRTY */ 74 75 static int n_pass; 76 static int n_fail; 77 78 static void check(const char *id, int ok) 79 { 80 if (ok) { 81 n_pass++; 82 printf("[PASS] %s\n", id); 83 } else { 84 n_fail++; 85 printf("[FAIL] %s\n", id); 86 } 87 } 88 89 /* ---- test data -------------------------------------------------------------- */ 90 91 static uint32_t ina[N]; 92 static uint32_t inb[N]; 93 static uint32_t expect_out[N]; 94 static uint32_t poison[N]; 95 static uint32_t outbuf[N]; 96 static uint8_t prog[512]; 97 static uint8_t prog_other[512]; 98 static uint8_t vec_code[0x1000]; 99 static size_t vec_len; 100 static uint8_t bigbytes[0x2000]; 101 static uint32_t bigwords[2048]; 102 103 static void init_data(void) 104 { 105 uint32_t i; 106 for (i = 0; i < N; i++) { 107 uint32_t s; 108 ina[i] = i * 0x01000193u + 0x01234567u; 109 inb[i] = i * 0x9e3779b1u + 7u; 110 s = ina[i] + inb[i]; 111 /* a wrong-by-one result must never look like the poison word */ 112 if (s == 0x7fffffffu || s == 0xffffffffu) inb[i] += 0x10u; 113 } 114 /* the old fixed poison value as a legitimate sum */ 115 ina[7] = 0xdeadbeefu - 5u; 116 inb[7] = 5u; 117 for (i = 0; i < N; i++) { 118 expect_out[i] = ina[i] + inb[i]; 119 poison[i] = ~expect_out[i]; 120 } 121 for (i = 0; i < sizeof prog; i++) { 122 prog[i] = (uint8_t)(i * 7u + 3u); 123 prog_other[i] = (uint8_t)(i * 11u + 5u); 124 } 125 } 126 127 static int output_correct(const uint32_t *o) 128 { 129 uint32_t i; 130 for (i = 0; i < N; i++) 131 if (o[i] != expect_out[i]) return 0; 132 return 1; 133 } 134 135 /* ---- tracing copy of the real backend table ---------------------------------- */ 136 137 enum { 138 T_OPEN = 1, T_CHANNEL, T_ALLOC, T_COPY_IN, T_POISON, T_BUILD, T_SUBMIT, T_WAIT1, 139 T_WAIT2, T_WAITSEM, T_BARRIER, T_COPY_OUT, T_FREE, T_CLOSE 140 }; 141 #define TLOG_MAX 128 142 143 static const OmegaGpuBackend *g_real; 144 static OmegaGpuBackend g_wrap; 145 static int g_tlog[TLOG_MAX]; 146 static int g_targ[TLOG_MAX]; 147 static int g_tn; 148 149 static void tlog(int kind, int arg) 150 { 151 if (g_tn < TLOG_MAX) { 152 g_tlog[g_tn] = kind; 153 g_targ[g_tn] = arg; 154 g_tn++; 155 } 156 } 157 158 static int w_open(void *ctx) { tlog(T_OPEN, 0); return g_real->open_device(ctx); } 159 static int w_channel(void *ctx) { tlog(T_CHANNEL, 0); return g_real->create_channel(ctx); } 160 static int w_alloc(void *ctx, int role, size_t bytes) { tlog(T_ALLOC, role); return g_real->alloc(ctx, role, bytes); } 161 static int w_copy_in(void *ctx, int role, const void *src, size_t bytes) { tlog(T_COPY_IN, role); return g_real->copy_in(ctx, role, src, bytes); } 162 static int w_poison(void *ctx, const uint32_t *words, size_t count) { tlog(T_POISON, 0); return g_real->fill_poison(ctx, words, count); } 163 static int w_build(void *ctx, const OmegaGpuJob *job) { tlog(T_BUILD, 0); return g_real->build(ctx, job); } 164 static int w_submit(void *ctx) { tlog(T_SUBMIT, 0); return g_real->submit(ctx); } 165 static int w_wait1(void *ctx, uint64_t ms) { tlog(T_WAIT1, 0); return g_real->wait_marker(ctx, ms); } 166 static int w_wait2(void *ctx, uint64_t ms) { tlog(T_WAIT2, 0); return g_real->wait_marker2(ctx, ms); } 167 static int w_waitsem(void *ctx, uint64_t ms) { tlog(T_WAITSEM, 0); return g_real->wait_release_semaphore(ctx, ms); } 168 static int w_barrier(void *ctx, uint64_t ms) { tlog(T_BARRIER, 0); return g_real->barrier(ctx, ms); } 169 static int w_copy_out(void *ctx, void *dst, size_t bytes) { tlog(T_COPY_OUT, 0); return g_real->copy_out(ctx, dst, bytes); } 170 static void w_diag(void *ctx, OmegaGpuBackendDiag *d) { g_real->diagnostics(ctx, d); } 171 static int w_free(void *ctx, int role) { tlog(T_FREE, role); return g_real->free_buf(ctx, role); } 172 static int w_close(void *ctx) { tlog(T_CLOSE, 0); return g_real->close(ctx); } 173 174 static void wrap_init(void) 175 { 176 g_real = omega_blackwell_engine_backend(); 177 g_wrap = *g_real; 178 g_wrap.open_device = w_open; 179 g_wrap.create_channel = w_channel; 180 g_wrap.alloc = w_alloc; 181 g_wrap.copy_in = w_copy_in; 182 g_wrap.fill_poison = w_poison; 183 g_wrap.build = w_build; 184 g_wrap.submit = w_submit; 185 g_wrap.wait_marker = w_wait1; 186 g_wrap.wait_marker2 = w_wait2; 187 g_wrap.wait_release_semaphore = w_waitsem; 188 g_wrap.barrier = w_barrier; 189 g_wrap.copy_out = w_copy_out; 190 g_wrap.diagnostics = w_diag; 191 g_wrap.free_buf = w_free; 192 g_wrap.close = w_close; 193 } 194 195 static int tcount(int kind) 196 { 197 int i; 198 int n = 0; 199 for (i = 0; i < g_tn; i++) 200 if (g_tlog[i] == kind) n++; 201 return n; 202 } 203 204 /* ---- case plumbing ---------------------------------------------------------- */ 205 206 static OmegaGpuJob job; 207 static OmegaGpuResult res; 208 209 static void make_job(OmegaGpuJob *j) 210 { 211 memset(j, 0, sizeof *j); 212 j->program = prog; 213 j->program_len = sizeof prog; 214 j->layout = OMEGA_GPU_LAYOUT_VECTOR_1D; 215 j->element_count = N; 216 j->input_a = ina; 217 j->input_a_len = N * 4u; 218 j->input_b = inb; 219 j->input_b_len = N * 4u; 220 j->output = outbuf; 221 j->output_len = N * 4u; 222 j->poison = poison; 223 j->poison_count = N; 224 j->timeouts.marker_ms = TMO_MARKER; 225 j->timeouts.marker2_ms = TMO_MARKER2; 226 j->timeouts.release_semaphore_ms = TMO_RELEASE; 227 j->timeouts.visibility_ms = TMO_VISIBILITY; 228 j->flags = 0; 229 } 230 231 static void begin_case(void) 232 { 233 (void)omega_blackwell_engine_shutdown(); /* no device carried between cases */ 234 fake_reset(); 235 omega_gpu_engine_test_reset_block(); 236 omega_gpu_engine_set_backend(&g_wrap); 237 g_tn = 0; 238 memset(outbuf, 0, sizeof outbuf); 239 fk.prog_expect = prog; 240 fk.prog_len = sizeof prog; 241 make_job(&job); 242 } 243 244 static int run(void) 245 { 246 return omega_gpu_execute(&job, &res); 247 } 248 249 static int res_is(int failure, int step, int state) 250 { 251 return res.failure == failure && res.failed_step == step && res.last_state == state; 252 } 253 254 /* Everything the backend allocated was returned, the device was closed once, and 255 * the number of real driver frees is as expected. */ 256 static int balanced(int frees) 257 { 258 return fk.live_at_close == 0 && fk.double_free == 0 && fk.close_calls == 1 && 259 fk.free_calls == frees && fk.device_open == 0; 260 } 261 262 static int has(const char *hay, const char *needle) 263 { 264 return strstr(hay, needle) != NULL; 265 } 266 267 /* Drains a retained backend through its own table, the way process exit would: 268 * free every role (all of them are held), then close. Returns 0 if all went well. */ 269 static int drain(void) 270 { 271 int bad = 0; 272 int role; 273 for (role = (int)OMEGA_GPU_BUF_ROLE_COUNT - 1; role >= 0; role--) 274 if (g_real->free_buf(g_real->ctx, role) != 0) bad = 1; 275 if (g_real->close(g_real->ctx) != 0) bad = 1; 276 return bad; 277 } 278 279 static void idf(char *buf, size_t cap, const char *tag, const char *what) 280 { 281 (void)snprintf(buf, cap, "sim_%s_%s", tag, what); 282 } 283 284 /* ---- cases ------------------------------------------------------------------ */ 285 286 static void case_success(void) 287 { 288 static const int k_order[24] = { 289 T_OPEN, T_CHANNEL, T_ALLOC, T_ALLOC, T_ALLOC, T_ALLOC, T_ALLOC, T_COPY_IN, T_COPY_IN, 290 T_COPY_IN, T_POISON, T_BUILD, T_SUBMIT, T_WAIT1, T_WAIT2, T_WAITSEM, T_BARRIER, 291 T_COPY_OUT, T_FREE, T_FREE, T_FREE, T_FREE, T_FREE, T_CLOSE 292 }; 293 static const int k_args[24] = { 294 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 4, 3, 2, 1, 0, 0 295 }; 296 static const uint64_t k_sizes[8] = { 297 0x10000u, 0x1000u, 0x1000u, 0x10000u, 0x1000u, 0x1000u, 0x1000u, 0x1000u 298 }; 299 OmegaBlackwellEngineRunInfo info; 300 int i; 301 int ok; 302 int i_cb, i_ar, i_q0, i_si, i_q1, i_k, i_r1, i_fl, i_r2; 303 304 begin_case(); 305 check("sim_success_returns_ok", run() == OMEGA_GPU_ENGINE_OK); 306 check("sim_success_state", res.last_state == OMEGA_GPU_ENGINE_STATE_SUCCESS && 307 res.failed_step == OMEGA_GPU_ENGINE_STEP_NONE); 308 check("sim_success_result_flags_clear", 309 res.retained == 0 && res.cleanup_failed == 0 && res.wait == OMEGA_GPU_ENGINE_WAIT_NONE); 310 check("sim_success_not_blocked", omega_gpu_engine_is_blocked() == 0); 311 check("sim_success_output_values", output_correct(outbuf)); 312 check("sim_success_unchanged_zero", res.n_outputs == 1u && res.output_unchanged_words[0] == 0u); 313 314 ok = g_tn == 24; 315 for (i = 0; ok && i < 24; i++) 316 if (g_tlog[i] != k_order[i] || g_targ[i] != k_args[i]) ok = 0; 317 check("sim_success_call_order", ok); 318 319 check("sim_success_driver_counts", 320 fk.open_calls == 1 && fk.channel_calls == 1 && fk.alloc_calls == 8 && 321 fk.submit_calls == 1 && fk.close_calls == 1 && fk.wait_calls == 3); 322 check("sim_success_all_allocs_gpu_uncached", fk.uncached_calls == fk.alloc_calls && fk.uncached_calls == 8); 323 ok = fake_count(FK_EV_ALLOC) == 8; 324 for (i = 0; ok && i < 8; i++) 325 if (fk.ev[fake_nth(FK_EV_ALLOC, i + 1)].a != k_sizes[i]) ok = 0; 326 check("sim_success_alloc_sizes", ok); 327 check("sim_success_balanced", balanced(8)); 328 check("sim_success_large_pushbuffer_ring", fk.pb_mem_size == 0x10000u); 329 check("sim_success_pushbuffer_parses", fk.pb_parse_error == 0 && fk.ev_overflow == 0); 330 331 /* The state the GPU sees at the doorbell: the sync words were zeroed first. */ 332 check("sim_success_doorbell_state_captured", fk.pre_valid == 1); 333 check("sim_success_marker1_zeroed_before_doorbell", fk.pre_marker1 == 0u); 334 check("sim_success_marker2_zeroed_before_doorbell", fk.pre_marker2 == 0u); 335 check("sim_success_semaphore_zeroed_before_doorbell", fk.pre_sem == 0u); 336 check("sim_success_semaphore_init_value", fk.sem_init_value == SEM_INIT); 337 338 /* The pushbuffer, as the device executes it. */ 339 i_cb = fake_nth(FK_EV_PB_CBANK, 1); 340 i_ar = fake_nth(FK_EV_PB_ARGS, 1); 341 i_q0 = fake_nth(FK_EV_PB_QMD, 1); 342 i_si = fake_nth(FK_EV_PB_SEM_INIT, 1); 343 i_q1 = fake_nth(FK_EV_PB_QMD, 2); 344 i_k = fake_nth(FK_EV_KERNEL, 1); 345 i_r1 = fake_nth(FK_EV_PB_RELEASE, 1); 346 i_fl = fake_nth(FK_EV_PB_FLUSH, 1); 347 i_r2 = fake_nth(FK_EV_PB_RELEASE, 2); 348 check("sim_success_pushbuffer_order", 349 i_cb >= 0 && i_cb < i_ar && i_ar < i_q0 && i_q0 < i_si && i_si < i_q1 && i_q1 < i_k && 350 i_k < i_r1 && i_r1 < i_fl && i_fl < i_r2); 351 check("sim_success_one_flush_two_releases", 352 fake_count(FK_EV_PB_FLUSH) == 1 && fake_count(FK_EV_PB_RELEASE) == 2); 353 if (i_cb >= 0 && i_ar >= 0 && i_q0 >= 0 && i_q1 >= 0 && i_fl >= 0 && i_r1 >= 0 && i_r2 >= 0) { 354 check("sim_success_cbank_upload_words_and_place", 355 fk.ev[i_cb].b == 224u && fk.ev[i_cb].a == fk.alloc_va[1]); 356 check("sim_success_args_upload_words_and_place", 357 fk.ev[i_ar].b == 7u && fk.ev[i_ar].a == fk.ev[i_cb].a + 0x380u); 358 check("sim_success_qmd0_is_not_the_kernel_and_place", 359 fk.ev[i_q0].b == 0u && fk.ev[i_q0].a == fk.alloc_va[3]); 360 check("sim_success_qmd1_is_the_kernel_and_place", 361 fk.ev[i_q1].b == 1u && fk.ev[i_q1].a == fk.alloc_va[3] + 0x1000u); 362 check("sim_success_flush_operation", fk.ev[i_fl].a == (uint64_t)FLUSH_OP); 363 check("sim_success_release1_address_payload_flags", 364 fk.ev[i_r1].a == fk.marker1_va && fk.ev[i_r1].b == MARKER1_PAYLOAD && 365 fk.ev[i_r1].c == RELEASE_FLAGS); 366 check("sim_success_release2_is_marker_page_plus_0x10", 367 fk.ev[i_r2].a == fk.ev[i_r1].a + 0x10u); 368 check("sim_success_release2_payload_and_flags", 369 fk.ev[i_r2].b == MARKER2_PAYLOAD && fk.ev[i_r2].c == RELEASE_FLAGS); 370 } else { 371 check("sim_success_cbank_upload_words_and_place", 0); 372 check("sim_success_args_upload_words_and_place", 0); 373 check("sim_success_qmd0_is_not_the_kernel_and_place", 0); 374 check("sim_success_qmd1_is_the_kernel_and_place", 0); 375 check("sim_success_flush_operation", 0); 376 check("sim_success_release1_address_payload_flags", 0); 377 check("sim_success_release2_is_marker_page_plus_0x10", 0); 378 check("sim_success_release2_payload_and_flags", 0); 379 } 380 381 /* The kernel launch the device saw. */ 382 check("sim_success_kernel_ran_once_with_right_program", fk.kernel_ran == 1 && fk.prog_matched == 1); 383 check("sim_success_kernel_threads_and_count", fk.kernel_threads == 128u && fk.kernel_n == N); 384 check("sim_success_kernel_arguments_are_a_b_output", 385 fk.arg_a == fk.alloc_va[5] && fk.arg_b == fk.alloc_va[6] && fk.arg_c == fk.alloc_va[7]); 386 check("sim_success_kernel_program_and_cbank_addresses", 387 fk.prog_va_seen == fk.alloc_va[4] && fk.cbank_va_seen == fk.alloc_va[1]); 388 check("sim_success_marker_and_semaphore_places", 389 fk.marker1_va == fk.alloc_va[2] && fk.sem_va == fk.alloc_va[3] + 0x2000u); 390 ok = fk.snap_n == N; 391 for (i = 0; ok && i < (int)N; i++) 392 if (fk.out_before[i] != poison[i]) ok = 0; 393 check("sim_success_poison_reached_device_output", ok); 394 check("sim_success_poison_never_equals_a_sum", fk.poison_collisions == 0); 395 396 /* The waits, by word, expected value and timeout. */ 397 check("sim_success_wait_counts", 398 fk.wait_n[FK_W_MARKER1] == 1 && fk.wait_n[FK_W_MARKER2] == 1 && 399 fk.wait_n[FK_W_SEM] == 1 && fk.wait_n[FK_W_UNKNOWN] == 0); 400 check("sim_success_wait_expected_values", 401 fk.wait_expected[FK_W_MARKER1] == MARKER1_PAYLOAD && 402 fk.wait_expected[FK_W_MARKER2] == MARKER2_PAYLOAD && 403 fk.wait_expected[FK_W_SEM] == SEM_DONE); 404 check("sim_success_wait_timeouts_come_from_the_job", 405 fk.wait_ms[FK_W_MARKER1] == TMO_MARKER && fk.wait_ms[FK_W_MARKER2] == TMO_MARKER2 && 406 fk.wait_ms[FK_W_SEM] == TMO_RELEASE); 407 i = fake_nth(FK_EV_WAIT, 1); 408 ok = i >= 0 && fk.ev[i].a == (uint64_t)FK_W_MARKER1; 409 i = fake_nth(FK_EV_WAIT, 2); 410 ok = ok && i >= 0 && fk.ev[i].a == (uint64_t)FK_W_MARKER2; 411 i = fake_nth(FK_EV_WAIT, 3); 412 ok = ok && i >= 0 && fk.ev[i].a == (uint64_t)FK_W_SEM; 413 check("sim_success_wait_order_marker_marker2_semaphore", ok); 414 415 /* What the backend observed, for the vector wrapper. */ 416 memset(&info, 0, sizeof info); 417 omega_blackwell_engine_run_info(&info); 418 check("sim_success_run_info_words", 419 info.marker == MARKER1_PAYLOAD && info.marker2 == MARKER2_PAYLOAD && info.semaphore == SEM_DONE); 420 check("sim_success_run_info_launch_time_set", info.launch_ns != 0u); 421 check("sim_success_run_info_done_time_after_launch", 422 info.marker_done_ns != 0u && info.marker_done_ns >= info.launch_ns); 423 } 424 425 static void case_no_c3(void) 426 { 427 OmegaBlackwellEngineRunInfo info; 428 begin_case(); 429 job.flags = OMEGA_GPU_ENGINE_FLAG_NO_C3; 430 check("sim_noc3_returns_ok", run() == OMEGA_GPU_ENGINE_OK); 431 check("sim_noc3_output_values", output_correct(outbuf)); 432 check("sim_noc3_no_flush_in_pushbuffer", fake_count(FK_EV_PB_FLUSH) == 0); 433 check("sim_noc3_one_release_only", fake_count(FK_EV_PB_RELEASE) == 1 && fk.marker2_va == 0u); 434 check("sim_noc3_marker2_never_waited", 435 fk.wait_n[FK_W_MARKER2] == 0 && tcount(T_WAIT2) == 0 && fk.wait_calls == 2); 436 check("sim_noc3_marker1_and_semaphore_waited", 437 fk.wait_n[FK_W_MARKER1] == 1 && fk.wait_n[FK_W_SEM] == 1); 438 memset(&info, 0, sizeof info); 439 omega_blackwell_engine_run_info(&info); 440 check("sim_noc3_run_info_marker2_not_reported", info.marker2 == 0u && info.marker == MARKER1_PAYLOAD); 441 check("sim_noc3_balanced", balanced(8)); 442 } 443 444 static void case_back_to_back(void) 445 { 446 int rc1; 447 int rc2; 448 int ok; 449 begin_case(); 450 rc1 = run(); 451 ok = output_correct(outbuf); 452 memset(outbuf, 0, sizeof outbuf); 453 rc2 = run(); 454 check("sim_twice_first_ok", rc1 == OMEGA_GPU_ENGINE_OK && ok); 455 check("sim_twice_second_ok_busy_flag_released", rc2 == OMEGA_GPU_ENGINE_OK && output_correct(outbuf)); 456 check("sim_twice_driver_opened_and_closed_twice", fk.open_calls == 2 && fk.close_calls == 2); 457 } 458 459 static void case_open_fail(void) 460 { 461 OmegaBlackwellEngineRunInfo info; 462 int rc; 463 begin_case(); 464 (void)run(); /* leave a dirty run snapshot behind */ 465 begin_case(); 466 fk.open_fail = 1; 467 rc = run(); 468 check("sim_open_fail_name_step_state", 469 rc == OMEGA_GPU_ENGINE_DEVICE_OPEN && 470 res_is(OMEGA_GPU_ENGINE_DEVICE_OPEN, OMEGA_GPU_ENGINE_STEP_OPEN_DEVICE, 471 OMEGA_GPU_ENGINE_STATE_INITIAL)); 472 check("sim_open_fail_errno_and_driver_code", res.err_no == EIO && res.drv_rc == -1); 473 check("sim_open_fail_driver_text_reported", strcmp(res.drv_text, FK_TEXT) == 0); 474 check("sim_open_fail_nothing_else_touched", 475 fk.channel_calls == 0 && fk.alloc_calls == 0 && fk.close_calls == 0 && fk.free_calls == 0); 476 check("sim_open_fail_not_retained_not_blocked", 477 res.retained == 0 && omega_gpu_engine_is_blocked() == 0); 478 memset(&info, 0, sizeof info); 479 omega_blackwell_engine_run_info(&info); 480 check("sim_open_fail_run_info_reset_at_open", 481 info.marker == 0u && info.semaphore == 0u && info.launch_ns == 0u && info.marker_done_ns == 0u); 482 begin_case(); 483 check("sim_open_fail_then_next_run_ok", run() == OMEGA_GPU_ENGINE_OK); 484 } 485 486 static void case_channel_fail(void) 487 { 488 int rc; 489 begin_case(); 490 fk.channel_fail = 1; 491 rc = run(); 492 check("sim_channel_fail_name_step_state", 493 rc == OMEGA_GPU_ENGINE_CHANNEL_CREATE && 494 res_is(OMEGA_GPU_ENGINE_CHANNEL_CREATE, OMEGA_GPU_ENGINE_STEP_CREATE_CHANNEL, 495 OMEGA_GPU_ENGINE_STATE_INITIAL)); 496 check("sim_channel_fail_errno_text_code", 497 res.err_no == EIO && res.drv_rc == -1 && strcmp(res.drv_text, FK_TEXT) == 0); 498 check("sim_channel_fail_closed_nothing_allocated", fk.alloc_calls == 0 && balanced(0)); 499 begin_case(); 500 check("sim_channel_fail_then_next_run_ok", run() == OMEGA_GPU_ENGINE_OK); 501 } 502 503 static void case_alloc_faults(void) 504 { 505 int nth; 506 char id[64]; 507 for (nth = 1; nth <= 8; nth++) { 508 int rc; 509 begin_case(); 510 fk.alloc_fail_nth = nth; 511 fk.alloc_mode = FK_ALLOC_FAIL; 512 rc = run(); 513 (void)snprintf(id, sizeof id, "sim_alloc%d_name_step_state", nth); 514 check(id, rc == OMEGA_GPU_ENGINE_ALLOC && 515 res_is(OMEGA_GPU_ENGINE_ALLOC, OMEGA_GPU_ENGINE_STEP_ALLOC, 516 OMEGA_GPU_ENGINE_STATE_INITIAL)); 517 (void)snprintf(id, sizeof id, "sim_alloc%d_errno_code_text", nth); 518 check(id, res.err_no == EIO && res.drv_rc == -1 && strcmp(res.drv_text, FK_TEXT) == 0); 519 (void)snprintf(id, sizeof id, "sim_alloc%d_stops_at_the_failure", nth); 520 check(id, fk.alloc_calls == nth); 521 /* nothing is leaked: every part that was allocated is freed (nth - 1 of them) */ 522 (void)snprintf(id, sizeof id, "sim_alloc%d_balanced", nth); 523 check(id, balanced(nth - 1)); 524 } 525 } 526 527 static void case_alloc_own_checks(void) 528 { 529 static const struct { int mode; int nth; int frees; const char *tag; } t[] = { 530 { FK_ALLOC_SHORT, 1, 1, "short_pb" }, 531 { FK_ALLOC_NO_CPU, 3, 3, "nocpu_marker" }, 532 { FK_ALLOC_MISALIGNED, 4, 4, "misaligned_qmd" }, 533 { FK_ALLOC_SHORT, 6, 6, "short_input_a" }, 534 { FK_ALLOC_NO_CPU, 6, 6, "nocpu_input_a" }, 535 { FK_ALLOC_MISALIGNED, 6, 6, "misaligned_input_a" }, 536 { FK_ALLOC_NO_CPU, 8, 8, "nocpu_output" } 537 }; 538 size_t k; 539 char id[64]; 540 for (k = 0; k < sizeof t / sizeof t[0]; k++) { 541 int rc; 542 begin_case(); 543 fk.alloc_fail_nth = t[k].nth; 544 fk.alloc_mode = t[k].mode; 545 rc = run(); 546 idf(id, sizeof id, t[k].tag, "refused_as_alloc"); 547 check(id, rc == OMEGA_GPU_ENGINE_ALLOC && 548 res_is(OMEGA_GPU_ENGINE_ALLOC, OMEGA_GPU_ENGINE_STEP_ALLOC, 549 OMEGA_GPU_ENGINE_STATE_INITIAL)); 550 idf(id, sizeof id, t[k].tag, "own_check_text"); 551 check(id, has(res.drv_text, "own checks")); 552 idf(id, sizeof id, t[k].tag, "balanced"); 553 check(id, balanced(t[k].frees)); 554 } 555 } 556 557 static void case_submit_fail(void) 558 { 559 int rc; 560 begin_case(); 561 fk.submit_fail = 1; 562 rc = run(); 563 check("sim_submit_fail_name_step_state", 564 rc == OMEGA_GPU_ENGINE_SUBMIT && 565 res_is(OMEGA_GPU_ENGINE_SUBMIT, OMEGA_GPU_ENGINE_STEP_SUBMIT, OMEGA_GPU_ENGINE_STATE_PREPARED)); 566 check("sim_submit_fail_errno_code_text", 567 res.err_no == EIO && res.drv_rc == -1 && strcmp(res.drv_text, FK_TEXT) == 0); 568 check("sim_submit_fail_is_not_uncertain", 569 res.retained == 0 && omega_gpu_engine_is_blocked() == 0 && fk.kernel_ran == 0); 570 check("sim_submit_fail_cleaned_up", balanced(8)); 571 } 572 573 /* One uncertain completion: the engine keeps everything and blocks the process. 574 * Asserts that, asserts the next job is refused without touching the driver, asserts 575 * a second open of the backend is refused, drains, and proves the backend works again. */ 576 static void uncertain_case(const char *tag, int mk, int mk2, int sem, uint32_t flags, 577 int failure, int step, int wait, uint64_t limit, uint64_t waited, 578 uint32_t want_marker, uint32_t want_marker2, uint32_t want_sem, 579 uint32_t want_sync, const char *want_text, int want_drv_rc) 580 { 581 char id[96]; 582 int rc; 583 int bad; 584 int open_before; 585 OmegaBlackwellEngineRunInfo info; 586 587 begin_case(); 588 fk.marker_mode = mk; 589 fk.marker2_mode = mk2; 590 fk.sem_mode = sem; 591 job.flags = flags; 592 rc = run(); 593 594 idf(id, sizeof id, tag, "name_step_wait"); 595 check(id, rc == failure && res.failure == failure && res.failed_step == step && res.wait == wait); 596 idf(id, sizeof id, tag, "last_state_submitted"); 597 check(id, res.last_state == OMEGA_GPU_ENGINE_STATE_SUBMITTED); 598 idf(id, sizeof id, tag, "limit_and_waited_ms"); 599 check(id, res.wait_timeout_ms == limit && res.waited_ms == waited); 600 idf(id, sizeof id, tag, "observed_words"); 601 check(id, res.marker == want_marker && res.marker2 == want_marker2 && 602 res.semaphore[0] == want_sem); 603 idf(id, sizeof id, tag, "sync_valid_mask"); 604 check(id, res.sync_valid == want_sync); 605 if (want_text != NULL) { 606 idf(id, sizeof id, tag, "driver_text"); 607 check(id, has(res.drv_text, want_text)); 608 } 609 idf(id, sizeof id, tag, "retained_and_blocked"); 610 check(id, res.retained == 1 && omega_gpu_engine_is_blocked() == 1); 611 idf(id, sizeof id, tag, "nothing_freed_nothing_closed"); 612 check(id, fk.free_calls == 0 && fk.close_calls == 0 && fk.live == 8 && fk.device_open == 1 && 613 tcount(T_FREE) == 0 && tcount(T_CLOSE) == 0); 614 615 idf(id, sizeof id, tag, "driver_code"); 616 check(id, res.drv_rc == want_drv_rc); 617 memset(&info, 0, sizeof info); 618 omega_blackwell_engine_run_info(&info); 619 idf(id, sizeof id, tag, "run_info_marker"); 620 check(id, info.marker == want_marker && info.launch_ns != 0u); 621 idf(id, sizeof id, tag, "run_info_done_time_only_if_marker_succeeded"); 622 check(id, (step == OMEGA_GPU_ENGINE_STEP_WAIT_MARKER) == (info.marker_done_ns == 0u)); 623 idf(id, sizeof id, tag, "run_info_marker2_when_it_was_the_failed_wait"); 624 check(id, step != OMEGA_GPU_ENGINE_STEP_WAIT_MARKER2 || info.marker2 == want_marker2); 625 idf(id, sizeof id, tag, "run_info_semaphore_when_it_was_the_failed_wait"); 626 check(id, step != OMEGA_GPU_ENGINE_STEP_WAIT_RELEASE_SEMAPHORE || info.semaphore == want_sem); 627 idf(id, sizeof id, tag, "no_marker2_wait_with_noc3"); 628 check(id, (flags & OMEGA_GPU_ENGINE_FLAG_NO_C3) == 0u || (fk.wait_n[FK_W_MARKER2] == 0 && tcount(T_WAIT2) == 0)); 629 open_before = fk.open_calls; 630 rc = run(); 631 idf(id, sizeof id, tag, "next_job_blocked"); 632 check(id, rc == OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED && 633 res.failed_step == OMEGA_GPU_ENGINE_STEP_BLOCKED && fk.open_calls == open_before); 634 idf(id, sizeof id, tag, "second_open_refused_by_backend"); 635 check(id, g_real->open_device(g_real->ctx) != 0 && fk.open_calls == open_before); 636 idf(id, sizeof id, tag, "still_retained_after_refusals"); 637 check(id, fk.live == 8 && fk.device_open == 1 && fk.close_calls == 0); 638 639 bad = drain(); 640 idf(id, sizeof id, tag, "drained_through_the_table"); 641 check(id, bad == 0 && fk.close_calls == 1 && fk.live_at_close == 0 && fk.double_free == 0 && 642 fk.free_calls == 8); 643 omega_gpu_engine_test_reset_block(); 644 make_job(&job); 645 /* The simulated driver still has the hang or overshoot of this case armed: restore a healthy chip. */ 646 fk.marker_mode = FK_SYNC_NORMAL; 647 fk.marker2_mode = FK_SYNC_NORMAL; 648 fk.sem_mode = FK_SYNC_NORMAL; 649 idf(id, sizeof id, tag, "backend_works_again_after_drain"); 650 check(id, run() == OMEGA_GPU_ENGINE_OK && output_correct(outbuf)); 651 } 652 653 static void case_uncertain(void) 654 { 655 const uint32_t all = OMEGA_GPU_SYNC_MARKER | OMEGA_GPU_SYNC_MARKER2 | OMEGA_GPU_SYNC_SEMAPHORE; 656 657 uncertain_case("unc_marker_hang", FK_SYNC_NEVER, FK_SYNC_NORMAL, FK_SYNC_NORMAL, 0u, 658 OMEGA_GPU_ENGINE_COMPLETION_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_MARKER, 659 OMEGA_GPU_ENGINE_WAIT_MARKER, TMO_MARKER, TMO_MARKER, 660 0u, MARKER2_PAYLOAD, SEM_DONE, all, NULL, -1); 661 uncertain_case("unc_marker2_hang", FK_SYNC_NORMAL, FK_SYNC_NEVER, FK_SYNC_NORMAL, 0u, 662 OMEGA_GPU_ENGINE_COMPLETION_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_MARKER2, 663 OMEGA_GPU_ENGINE_WAIT_MARKER2, TMO_MARKER2, TMO_MARKER2, 664 MARKER1_PAYLOAD, 0u, SEM_DONE, all, NULL, -1); 665 uncertain_case("unc_semaphore_hang", FK_SYNC_NORMAL, FK_SYNC_NORMAL, FK_SYNC_NEVER, 0u, 666 OMEGA_GPU_ENGINE_RELEASE_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_RELEASE_SEMAPHORE, 667 OMEGA_GPU_ENGINE_WAIT_RELEASE_SEMAPHORE, TMO_RELEASE, TMO_RELEASE, 668 MARKER1_PAYLOAD, MARKER2_PAYLOAD, SEM_INIT, all, NULL, -1); 669 uncertain_case("unc_marker_overshoot", FK_SYNC_OVERSHOOT, FK_SYNC_NORMAL, FK_SYNC_NORMAL, 0u, 670 OMEGA_GPU_ENGINE_COMPLETION_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_MARKER, 671 OMEGA_GPU_ENGINE_WAIT_MARKER, TMO_MARKER, 0u, 672 MARKER1_PAYLOAD + 1u, MARKER2_PAYLOAD, SEM_DONE, all, "observed 0x44444445", 0); 673 uncertain_case("unc_marker2_overshoot", FK_SYNC_NORMAL, FK_SYNC_OVERSHOOT, FK_SYNC_NORMAL, 0u, 674 OMEGA_GPU_ENGINE_COMPLETION_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_MARKER2, 675 OMEGA_GPU_ENGINE_WAIT_MARKER2, TMO_MARKER2, 0u, 676 MARKER1_PAYLOAD, MARKER2_PAYLOAD + 1u, SEM_DONE, all, "observed 0x46464647", 0); 677 uncertain_case("unc_semaphore_overshoot", FK_SYNC_NORMAL, FK_SYNC_NORMAL, FK_SYNC_OVERSHOOT, 0u, 678 OMEGA_GPU_ENGINE_RELEASE_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_RELEASE_SEMAPHORE, 679 OMEGA_GPU_ENGINE_WAIT_RELEASE_SEMAPHORE, TMO_RELEASE, 0u, 680 MARKER1_PAYLOAD, MARKER2_PAYLOAD, SEM_DONE + 1u, all, "observed 0x00000007", 0); 681 /* NO_C3: the second marker does not exist, so it is never reported either. */ 682 uncertain_case("unc_noc3_semaphore_hang", FK_SYNC_NORMAL, FK_SYNC_NORMAL, FK_SYNC_NEVER, 683 OMEGA_GPU_ENGINE_FLAG_NO_C3, 684 OMEGA_GPU_ENGINE_RELEASE_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_RELEASE_SEMAPHORE, 685 OMEGA_GPU_ENGINE_WAIT_RELEASE_SEMAPHORE, TMO_RELEASE, TMO_RELEASE, 686 MARKER1_PAYLOAD, 0u, SEM_INIT, OMEGA_GPU_SYNC_MARKER | OMEGA_GPU_SYNC_SEMAPHORE, NULL, -1); 687 } 688 689 static void case_cleanup_faults(void) 690 { 691 int rc; 692 693 /* a free of an ordinary buffer fails: every other free still runs */ 694 begin_case(); 695 fk.free_fail_nth = 1; 696 rc = run(); 697 check("sim_cleanup_free_role_name_step_state", 698 rc == OMEGA_GPU_ENGINE_CLEANUP && 699 res_is(OMEGA_GPU_ENGINE_CLEANUP, OMEGA_GPU_ENGINE_STEP_FREE, 700 OMEGA_GPU_ENGINE_STATE_OUTPUT_PRODUCED)); 701 check("sim_cleanup_free_role_flag_errno_text", 702 res.cleanup_failed == 1 && res.err_no == EIO && strcmp(res.drv_text, FK_TEXT) == 0); 703 check("sim_cleanup_free_role_every_free_attempted_and_closed", 704 fk.free_calls == 8 && fk.close_calls == 1 && fk.live_at_close == 1); 705 check("sim_cleanup_free_role_words_still_mapped_at_capture", 706 res.sync_valid == (OMEGA_GPU_SYNC_MARKER | OMEGA_GPU_SYNC_MARKER2 | OMEGA_GPU_SYNC_SEMAPHORE) && 707 res.marker == MARKER1_PAYLOAD && res.marker2 == MARKER2_PAYLOAD && res.semaphore[0] == SEM_DONE); 708 check("sim_cleanup_free_role_not_uncertain", res.retained == 0 && omega_gpu_engine_is_blocked() == 0); 709 710 /* a free of one SCRATCH part fails (the marker page, sixth real free): the 711 * other scratch parts are still freed and the sync words are unmapped first */ 712 begin_case(); 713 fk.free_fail_nth = 6; 714 rc = run(); 715 check("sim_cleanup_free_scratch_name_step_state", 716 rc == OMEGA_GPU_ENGINE_CLEANUP && 717 res_is(OMEGA_GPU_ENGINE_CLEANUP, OMEGA_GPU_ENGINE_STEP_FREE, 718 OMEGA_GPU_ENGINE_STATE_OUTPUT_PRODUCED)); 719 check("sim_cleanup_free_scratch_code_and_text", 720 res.cleanup_failed == 1 && res.drv_rc == -1 && strcmp(res.drv_text, FK_TEXT) == 0); 721 check("sim_cleanup_free_scratch_all_four_parts_attempted", 722 fk.free_calls == 8 && fk.close_calls == 1 && fk.live_at_close == 1); 723 check("sim_cleanup_free_scratch_sync_words_unmapped_before_free", res.sync_valid == 0u); 724 725 /* close fails */ 726 begin_case(); 727 fk.close_fail = 1; 728 rc = run(); 729 check("sim_cleanup_close_name_step_state", 730 rc == OMEGA_GPU_ENGINE_CLEANUP && 731 res_is(OMEGA_GPU_ENGINE_CLEANUP, OMEGA_GPU_ENGINE_STEP_CLOSE, 732 OMEGA_GPU_ENGINE_STATE_OUTPUT_PRODUCED)); 733 check("sim_cleanup_close_flag_errno_text", 734 res.cleanup_failed == 1 && res.err_no == EIO && strcmp(res.drv_text, FK_TEXT) == 0); 735 check("sim_cleanup_close_all_freed_first", fk.free_calls == 8 && fk.live_at_close == 0); 736 begin_case(); 737 check("sim_cleanup_close_failure_still_releases_busy_flag", run() == OMEGA_GPU_ENGINE_OK); 738 } 739 740 static void case_unchanged(void) 741 { 742 int rc; 743 744 begin_case(); 745 fk.kernel_mode = FK_KERNEL_NO_WRITE; 746 rc = run(); 747 check("sim_unchanged_nowrite_name_step_state", 748 rc == OMEGA_GPU_ENGINE_OUTPUT_UNCHANGED && 749 res_is(OMEGA_GPU_ENGINE_OUTPUT_UNCHANGED, OMEGA_GPU_ENGINE_STEP_SCAN, 750 OMEGA_GPU_ENGINE_STATE_OUTPUT_VISIBLE)); 751 check("sim_unchanged_nowrite_counts_every_word", 752 res.n_outputs == 1u && res.output_unchanged_words[0] == N); 753 check("sim_unchanged_nowrite_completion_was_seen", 754 fk.kernel_ran == 1 && fk.wait_n[FK_W_MARKER1] == 1 && fk.wait_n[FK_W_SEM] == 1); 755 check("sim_unchanged_nowrite_cleaned_up_not_retained", 756 balanced(8) && res.retained == 0 && omega_gpu_engine_is_blocked() == 0); 757 758 begin_case(); 759 fk.kernel_mode = FK_KERNEL_HALF; 760 rc = run(); 761 check("sim_unchanged_half_counts_the_unwritten_half", 762 rc == OMEGA_GPU_ENGINE_OUTPUT_UNCHANGED && res.output_unchanged_words[0] == N / 2u); 763 764 /* the device runs different program bytes than the job supplied: it writes nothing */ 765 begin_case(); 766 fk.prog_expect = prog_other; 767 rc = run(); 768 check("sim_unchanged_wrong_program_bytes_detected", 769 rc == OMEGA_GPU_ENGINE_OUTPUT_UNCHANGED && fk.kernel_ran == 1 && fk.prog_matched == 0 && 770 res.output_unchanged_words[0] == N); 771 } 772 773 static void case_table_guards(void) 774 { 775 const OmegaGpuBackend *be = omega_blackwell_engine_backend(); 776 void *ctx = be->ctx; 777 OmegaGpuJob j; 778 int role; 779 int bad = 0; 780 781 begin_case(); 782 memset(&j, 0, sizeof j); 783 j.layout = OMEGA_GPU_LAYOUT_VECTOR_1D; 784 j.element_count = N; 785 786 check("sim_guard_open_ok", be->open_device(ctx) == 0); 787 check("sim_guard_channel_ok", be->create_channel(ctx) == 0); 788 check("sim_guard_alloc_zero_bytes_refused", be->alloc(ctx, (int)OMEGA_GPU_BUF_OUTPUT, 0u) != 0); 789 check("sim_guard_alloc_unknown_role_refused", be->alloc(ctx, 99, 4096u) != 0); 790 check("sim_guard_alloc_rounds_up_to_whole_pages", 791 be->alloc(ctx, (int)OMEGA_GPU_BUF_OUTPUT, 5000u) == 0 && fake_count(FK_EV_ALLOC) == 1 && 792 fk.ev[fake_nth(FK_EV_ALLOC, 1)].a == 0x2000u); 793 check("sim_guard_free_after_rounding_ok", be->free_buf(ctx, (int)OMEGA_GPU_BUF_OUTPUT) == 0); 794 check("sim_guard_wait_before_build_refused_without_driver_call", 795 be->wait_marker(ctx, 5u) != 0 && be->wait_marker2(ctx, 5u) != 0 && 796 be->wait_release_semaphore(ctx, 5u) != 0 && fk.wait_calls == 0); 797 check("sim_guard_submit_before_build_refused", be->submit(ctx) != 0); 798 check("sim_guard_build_without_buffers_refused", be->build(ctx, &j) != 0); 799 800 check("sim_guard_alloc_scratch_ok", be->alloc(ctx, (int)OMEGA_GPU_BUF_SCRATCH, 0u) == 0); 801 check("sim_guard_alloc_program_ok", be->alloc(ctx, (int)OMEGA_GPU_BUF_PROGRAM, sizeof prog) == 0); 802 check("sim_guard_alloc_a_ok", be->alloc(ctx, (int)OMEGA_GPU_BUF_INPUT_A, N * 4u) == 0); 803 check("sim_guard_alloc_b_ok", be->alloc(ctx, (int)OMEGA_GPU_BUF_INPUT_B, N * 4u) == 0); 804 check("sim_guard_alloc_output_ok", be->alloc(ctx, (int)OMEGA_GPU_BUF_OUTPUT, N * 4u) == 0); 805 check("sim_guard_copy_in_negative_role_refused", be->copy_in(ctx, -1, prog, sizeof prog) != 0); 806 check("sim_guard_free_negative_role_refused", be->free_buf(ctx, -1) != 0); 807 check("sim_guard_build_null_job_refused", be->build(ctx, NULL) != 0); 808 j.layout = 7; 809 check("sim_guard_build_unknown_layout_refused", be->build(ctx, &j) != 0); 810 j.layout = (int)OMEGA_GPU_LAYOUT_VECTOR_1D; 811 j.element_count = 0u; 812 check("sim_guard_build_zero_count_refused", be->build(ctx, &j) != 0); 813 j.element_count = 2000u; 814 check("sim_guard_build_count_beyond_the_buffers_refused", be->build(ctx, &j) != 0); 815 j.element_count = N; 816 817 check("sim_guard_copy_in_too_long_refused", 818 be->copy_in(ctx, (int)OMEGA_GPU_BUF_PROGRAM, bigbytes, 0x1000u + 8u) != 0); 819 check("sim_guard_copy_in_unknown_role_refused", be->copy_in(ctx, 99, prog, sizeof prog) != 0); 820 check("sim_guard_copy_in_null_source_refused", 821 be->copy_in(ctx, (int)OMEGA_GPU_BUF_PROGRAM, NULL, sizeof prog) != 0); 822 check("sim_guard_fill_poison_too_long_refused", 823 be->fill_poison(ctx, bigwords, 0x1000u / 4u + 1u) != 0); 824 check("sim_guard_fill_poison_null_refused", be->fill_poison(ctx, NULL, N) != 0); 825 check("sim_guard_copy_out_too_long_refused", 826 be->copy_out(ctx, bigbytes, 0x1000u + 8u) != 0); 827 check("sim_guard_copy_out_null_refused", be->copy_out(ctx, NULL, N * 4u) != 0); 828 check("sim_guard_free_unknown_role_refused", be->free_buf(ctx, 99) != 0); 829 830 check("sim_guard_valid_copy_in_ok", be->copy_in(ctx, (int)OMEGA_GPU_BUF_PROGRAM, prog, sizeof prog) == 0); 831 check("sim_guard_valid_fill_poison_ok", be->fill_poison(ctx, poison, N) == 0); 832 memset(bigbytes, 0, sizeof bigbytes); 833 check("sim_guard_valid_copy_out_returns_the_poison", 834 be->copy_out(ctx, bigbytes, N * 4u) == 0 && memcmp(bigbytes, poison, N * 4u) == 0); 835 836 for (role = (int)OMEGA_GPU_BUF_ROLE_COUNT - 1; role >= 0; role--) 837 if (be->free_buf(ctx, role) != 0) bad = 1; 838 if (be->close(ctx) != 0) bad = 1; 839 check("sim_guard_drain_ok", bad == 0); 840 check("sim_guard_nothing_leaked", fk.live_at_close == 0 && fk.double_free == 0 && fk.close_calls == 1); 841 } 842 843 /* A close without the SCRATCH free (a backend that is closed with its words still 844 * mapped) must stop reading the sync words: the memory is gone. */ 845 static void case_close_unmaps_sync_words(void) 846 { 847 const OmegaGpuBackend *be = omega_blackwell_engine_backend(); 848 void *ctx = be->ctx; 849 OmegaGpuBackendDiag d; 850 OmegaGpuJob j; 851 int ok; 852 853 begin_case(); 854 make_job(&j); 855 ok = be->open_device(ctx) == 0 && be->create_channel(ctx) == 0 && 856 be->alloc(ctx, (int)OMEGA_GPU_BUF_SCRATCH, 0u) == 0 && 857 be->alloc(ctx, (int)OMEGA_GPU_BUF_PROGRAM, sizeof prog) == 0 && 858 be->alloc(ctx, (int)OMEGA_GPU_BUF_INPUT_A, N * 4u) == 0 && 859 be->alloc(ctx, (int)OMEGA_GPU_BUF_INPUT_B, N * 4u) == 0 && 860 be->alloc(ctx, (int)OMEGA_GPU_BUF_OUTPUT, N * 4u) == 0 && 861 be->build(ctx, &j) == 0; 862 check("sim_close_setup_through_the_table", ok); 863 memset(&d, 0, sizeof d); 864 be->diagnostics(ctx, &d); 865 check("sim_close_words_mapped_after_build", 866 d.sync_valid == (OMEGA_GPU_SYNC_MARKER | OMEGA_GPU_SYNC_MARKER2 | OMEGA_GPU_SYNC_SEMAPHORE)); 867 check("sim_close_returns_zero", be->close(ctx) == 0); 868 memset(&d, 0, sizeof d); 869 be->diagnostics(ctx, &d); 870 check("sim_close_unmaps_every_sync_word", d.sync_valid == 0u); 871 check("sim_close_released_everything", fk.close_calls == 1 && fk.live == 0); 872 } 873 874 /* Like begin_case, for the vector wrapper: the device must see the encoder's real 875 * program bytes, and no backend is installed, so the wrapper has to install its own. */ 876 static void begin_vec(void) 877 { 878 begin_case(); 879 omega_gpu_engine_set_backend(NULL); 880 fk.prog_expect = vec_code; 881 fk.prog_len = vec_len; 882 } 883 884 static void case_vector(void) 885 { 886 OmegaVectorSpec spec; 887 OmegaBlackwellRealization real; 888 OmegaBlackwellVectorExecution info; 889 uint32_t c[N]; 890 uint32_t i; 891 int rc; 892 int ok; 893 uint64_t want_elapsed; 894 895 memset(&real, 0, sizeof real); 896 real.sm_architecture = OMEGA_BW_SM_ARCH_121; 897 check("sim_vec_spec_init", omega_vector_spec_init(&spec, "engine_sim", N) == 0); 898 899 /* success */ 900 begin_vec(); 901 memset(c, 0, sizeof c); 902 memset(&info, 0xff, sizeof info); 903 rc = omega_blackwell_execute_vector(&spec, &real, ina, inb, c, &info); 904 check("sim_vec_success_returns_zero", rc == 0); 905 check("sim_vec_success_output_values", output_correct(c)); 906 check("sim_vec_success_parity_verified", info.parity_verified == true); 907 check("sim_vec_success_completion_marker_and_semaphore", 908 info.completion_marker == MARKER1_PAYLOAD && info.intermediate_semaphore == SEM_DONE); 909 check("sim_vec_success_shape_fields", 910 info.element_count == N && info.sm_architecture == (uint32_t)OMEGA_BW_SM_ARCH_121 && 911 info.target_chip[0] != '\0'); 912 want_elapsed = info.completion_timestamp_ns > info.launch_timestamp_ns ? 913 info.completion_timestamp_ns - info.launch_timestamp_ns : 0u; 914 check("sim_vec_success_timestamps", 915 info.launch_timestamp_ns != 0u && info.completion_timestamp_ns != 0u && 916 info.elapsed_ns == want_elapsed); 917 check("sim_vec_success_timeouts_are_5000_each", 918 fk.wait_ms[FK_W_MARKER1] == 5000u && fk.wait_ms[FK_W_MARKER2] == 5000u && 919 fk.wait_ms[FK_W_SEM] == 5000u); 920 check("sim_vec_success_all_protections_on", 921 fake_count(FK_EV_PB_FLUSH) == 1 && fake_count(FK_EV_PB_RELEASE) == 2 && 922 fk.wait_n[FK_W_MARKER2] == 1); 923 check("sim_vec_success_encoded_program_reached_the_device", 924 fk.kernel_ran == 1 && fk.prog_matched == 1); 925 ok = fk.snap_n == N; 926 for (i = 0; ok && i < N; i++) 927 if (fk.out_before[i] != (uint32_t)~expect_out[i]) ok = 0; 928 check("sim_vec_success_poison_is_the_complement_of_the_sum", ok); 929 check("sim_vec_success_poison_never_collides_even_for_0xdeadbeef", fk.poison_collisions == 0); 930 check("sim_vec_success_balanced", balanced(8)); 931 932 /* wrong math on the device: the engine passes it (it only checks poison), the wrapper must not */ 933 begin_vec(); 934 fk.kernel_mode = FK_KERNEL_WRONG; 935 memset(c, 0, sizeof c); 936 memset(&info, 0, sizeof info); 937 rc = omega_blackwell_execute_vector(&spec, &real, ina, inb, c, &info); 938 check("sim_vec_wrong_math_returns_nonzero", rc != 0); 939 check("sim_vec_wrong_math_parity_not_verified", info.parity_verified == false); 940 941 /* a cleanup fault after a good run is still a failure */ 942 begin_vec(); 943 fk.free_fail_nth = 1; 944 memset(c, 0, sizeof c); 945 rc = omega_blackwell_execute_vector(&spec, &real, ina, inb, c, &info); 946 check("sim_vec_cleanup_fault_returns_nonzero", rc != 0); 947 948 /* the device wrote nothing */ 949 begin_vec(); 950 fk.kernel_mode = FK_KERNEL_NO_WRITE; 951 memset(c, 0, sizeof c); 952 rc = omega_blackwell_execute_vector(&spec, &real, ina, inb, c, &info); 953 check("sim_vec_unwritten_output_returns_nonzero", rc != 0); 954 955 /* a hung first marker: failure, process blocked, backend retained */ 956 begin_vec(); 957 fk.marker_mode = FK_SYNC_NEVER; 958 memset(c, 0, sizeof c); 959 rc = omega_blackwell_execute_vector(&spec, &real, ina, inb, c, &info); 960 check("sim_vec_hang_returns_nonzero", rc != 0); 961 check("sim_vec_hang_blocks_the_process_and_retains", 962 omega_gpu_engine_is_blocked() == 1 && fk.free_calls == 0 && fk.close_calls == 0); 963 check("sim_vec_hang_drains", drain() == 0 && fk.live_at_close == 0); 964 omega_gpu_engine_test_reset_block(); 965 966 /* refusals before any driver call */ 967 begin_vec(); 968 check("sim_vec_null_spec_refused", omega_blackwell_execute_vector(NULL, &real, ina, inb, c, &info) != 0); 969 check("sim_vec_null_output_refused", omega_blackwell_execute_vector(&spec, &real, ina, inb, NULL, &info) != 0); 970 check("sim_vec_null_input_refused", omega_blackwell_execute_vector(&spec, &real, NULL, inb, c, &info) != 0); 971 spec.element_count = 0u; 972 check("sim_vec_zero_count_refused", omega_blackwell_execute_vector(&spec, &real, ina, inb, c, &info) != 0); 973 check("sim_vec_refusals_never_touch_the_driver", fk.open_calls == 0 && fk.alloc_calls == 0); 974 spec.element_count = N; 975 976 /* the wrapper can be used again afterwards (busy flag, block, tracing table) */ 977 begin_vec(); 978 memset(c, 0, sizeof c); 979 rc = omega_blackwell_execute_vector(&spec, &real, ina, inb, c, &info); 980 check("sim_vec_works_again", rc == 0 && output_correct(c)); 981 } 982 983 /* Packets in the last submitted pushbuffer that invalidate all shader caches 984 * (subchannel 1, method 0x021c, one data word = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL). 985 * Walks packet headers like the fake's parser, so a data word never counts as a header. */ 986 static int pb_shader_invalidates(void) 987 { 988 size_t i = 0; 989 int hits = 0; 990 while (i < fk.pb_len) { 991 uint32_t h = fk.pb[i++]; 992 uint32_t cnt = (h >> 16) & 0x1fffu; 993 uint32_t subc = (h >> 13) & 7u; 994 uint32_t mthd = (h & 0xfffu) << 2; 995 if (i + cnt > fk.pb_len) return -1; 996 if (subc == 1u && mthd == OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES && cnt == 1u && 997 fk.pb[i] == OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL) 998 hits++; 999 i += cnt; 1000 } 1001 return hits; 1002 } 1003 1004 /* omega-mv#575: the device and channel stay open between jobs (default). Measured 1005 * on the Spark: open 7.5 ms + channel 19.2 ms + close 11.3 ms of a 38.8 ms job. */ 1006 static void case_persistent_device(void) 1007 { 1008 int rc1, rc2, rc3, ok1, ok2; 1009 unsetenv("OMEGA_GPU_ENGINE_NO_PERSIST"); 1010 begin_case(); 1011 rc1 = run(); 1012 ok1 = output_correct(outbuf); 1013 memset(outbuf, 0, sizeof outbuf); 1014 rc2 = run(); 1015 ok2 = output_correct(outbuf); 1016 check("sim_persist_two_jobs_ok", rc1 == OMEGA_GPU_ENGINE_OK && ok1 && rc2 == OMEGA_GPU_ENGINE_OK && ok2); 1017 check("sim_persist_one_open_one_channel_no_close", 1018 fk.open_calls == 1 && fk.channel_calls == 1 && fk.close_calls == 0 && fk.device_open == 1); 1019 check("sim_persist_device_reported_open", omega_blackwell_engine_device_is_open() == 1); 1020 /* the PROGRAM buffer of the second job can reuse the first job's address on the kept 1021 * device, so every launch must invalidate the shader caches (the chip ran stale code 1022 * without it, 2026-10-19) */ 1023 check("sim_persist_pushbuffer_invalidates_shader_caches", pb_shader_invalidates() == 1); 1024 check("sim_persist_per_job_buffers_still_freed", fk.free_calls == 2 * 8 && fk.double_free == 0); 1025 check("sim_persist_shutdown_closes", omega_blackwell_engine_shutdown() == 0 && fk.close_calls == 1 && 1026 fk.device_open == 0 && omega_blackwell_engine_device_is_open() == 0); 1027 check("sim_persist_shutdown_idempotent", omega_blackwell_engine_shutdown() == 0 && fk.close_calls == 1); 1028 memset(outbuf, 0, sizeof outbuf); 1029 rc3 = run(); 1030 check("sim_persist_reopens_after_shutdown", rc3 == OMEGA_GPU_ENGINE_OK && output_correct(outbuf) && 1031 fk.open_calls == 2 && fk.channel_calls == 2); 1032 /* a job that fails at submit leaves the next job able to run */ 1033 fk.submit_fail = 1; 1034 rc3 = run(); 1035 check("sim_persist_fault_run_fails", rc3 != OMEGA_GPU_ENGINE_OK); 1036 fk.submit_fail = 0; 1037 memset(outbuf, 0, sizeof outbuf); 1038 rc3 = run(); 1039 check("sim_persist_recovers_after_fault", rc3 == OMEGA_GPU_ENGINE_OK && output_correct(outbuf)); 1040 /* a job whose submit faults the driver closes the device; the next job opens a fresh one */ 1041 fk.submit_fail = 1; 1042 fk.submit_faults_device = 1; 1043 rc3 = run(); 1044 check("sim_persist_driver_fault_closes_device", rc3 != OMEGA_GPU_ENGINE_OK && fk.close_calls == 2 && 1045 fk.device_open == 0 && omega_blackwell_engine_device_is_open() == 0); 1046 fk.submit_fail = 0; 1047 fk.submit_faults_device = 0; 1048 memset(outbuf, 0, sizeof outbuf); 1049 rc3 = run(); 1050 check("sim_persist_fresh_open_after_driver_fault", rc3 == OMEGA_GPU_ENGINE_OK && output_correct(outbuf) && 1051 fk.open_calls == 3 && fk.channel_calls == 3); 1052 (void)omega_blackwell_engine_shutdown(); 1053 setenv("OMEGA_GPU_ENGINE_NO_PERSIST", "1", 1); 1054 } 1055 1056 int main(void) 1057 { 1058 /* The cases above case_persistent_device() check the one-open-per-job lifecycle 1059 * (opened and closed twice, balanced frees, close unmaps the sync words). They run 1060 * with persistence off; case_persistent_device() then checks the default. */ 1061 setenv("OMEGA_GPU_ENGINE_NO_PERSIST", "1", 1); 1062 init_data(); 1063 wrap_init(); 1064 omega_gpu_engine_test_set_clock(fake_now_ms); 1065 check("sim_setup_encoder_ok", 1066 omega_blackwell_encode_vecadd(vec_code, sizeof vec_code, &vec_len) == 0 && vec_len > 0u); 1067 1068 case_success(); 1069 case_no_c3(); 1070 case_back_to_back(); 1071 case_open_fail(); 1072 case_channel_fail(); 1073 case_alloc_faults(); 1074 case_alloc_own_checks(); 1075 case_submit_fail(); 1076 case_uncertain(); 1077 case_cleanup_faults(); 1078 case_unchanged(); 1079 case_table_guards(); 1080 case_close_unmaps_sync_words(); 1081 case_vector(); 1082 case_persistent_device(); 1083 1084 omega_gpu_engine_set_backend(NULL); 1085 omega_gpu_engine_test_reset_block(); 1086 omega_gpu_engine_test_set_clock(NULL); 1087 fake_reset(); 1088 printf("SUMMARY passed=%d failed=%d\n", n_pass, n_fail); 1089 return n_fail ? 1 : 0; 1090 }