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