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