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
CUDApp / tests / platform_linux_live_marker.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* The Linux reference's marker poll through omega_gpu_live_marker (src/omega_gpu_platform_linux.c ref_poll_word), |
| 3 | * on the simulated driver (tests/fake_m16_native.c). Host program, opens no device. |
| 4 | * |
| 5 | * The fake normally hands out memfd pages, which the platform binds itself (plat_bind_fd), so the lookup is never |
| 6 | * reached. Here memfd_create is replaced by an O_TMPFILE file: the platform's mmap capture skips it, the buffer has |
| 7 | * no platform token, and the poll must take the lookup. This file's omega_gpu_live_marker stands in for nvrm.c's: it |
| 8 | * answers only for the Nvrm the platform opened, binds the page in its own context (not the platform's), and returns |
| 9 | * that context. The poll must then read the word through the returned context at the returned offset, with |
| 10 | * the platform's Nvrm. Mutants in mk/platform-linux.mk: polling plat_wait_ctx instead, passing NULL for rm and |
| 11 | * dropping the offset are each caught. */ |
| 12 | #include "fake_m16_native.h" |
| 13 | #include "nvrm.h" |
| 14 | #include "omega_gpu_platform_linux.h" |
| 15 | #include "omega_gpu_session.h" |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/mman.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | uint32_t plat_wt_context_create(uint64_t capacity, struct omega_native_context **out); |
| 26 | uint32_t omega2_ctx_plat_wait_hold(struct omega_native_context *context, uint64_t *result, uint64_t o_addr, uint64_t o_n); |
| 27 | uint32_t omega2_ctx_plat_wait_bind(struct omega_native_context *context, uint64_t *result, uint64_t o_handle, uint64_t o_fd); |
| 28 | |
| 29 | static const Nvrm *g_open_rm; |
| 30 | int nvrm_open(Nvrm *rm) { rm->target_sm = 121; fk.device_open = 1; g_open_rm = rm; return 0; } |
| 31 | int nvrm_close(Nvrm *rm) { (void)rm; return 0; } |
| 32 | void nvrm_retire(Nvrm *rm, uint32_t upto) { (void)rm; (void)upto; } |
| 33 | |
| 34 | /* Not a memfd: readlink gives "/tmp/#N (deleted)", so the platform's mmap hook does not bind it. */ |
| 35 | static int g_last_fd = -1; |
| 36 | int memfd_create(const char *name, unsigned int flags) { |
| 37 | (void)name; |
| 38 | int fd = open("/tmp", O_TMPFILE | O_RDWR | ((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0), 0600); |
| 39 | if (fd >= 0) g_last_fd = fd; |
| 40 | return fd; |
| 41 | } |
| 42 | |
| 43 | static const uint8_t *g_base; |
| 44 | static struct omega_native_context *g_live_ctx; |
| 45 | static uint64_t g_live_token; |
| 46 | static int g_calls, g_bad_rm; |
| 47 | int omega_gpu_live_marker(const Nvrm *rm, const void *cpu, uint64_t *mapping, uint64_t *offset, |
| 48 | struct omega_native_context **ctx) { |
| 49 | g_calls++; |
| 50 | if (!ctx || !mapping || !offset) { errno = EINVAL; return -1; } |
| 51 | *ctx = NULL; |
| 52 | if (!rm || rm != g_open_rm) { g_bad_rm++; errno = EINVAL; return -1; } |
| 53 | const uint8_t *p = cpu; |
| 54 | if (!g_base || p < g_base || p + 4 > g_base + 4096) { errno = EFAULT; return -1; } |
| 55 | if (!g_live_ctx) { |
| 56 | uint64_t held = 0, r = 0; |
| 57 | if (plat_wt_context_create(8, &g_live_ctx) != 0 || !g_live_ctx) return -1; |
| 58 | if (omega2_ctx_plat_wait_hold(g_live_ctx, &held, 536870912ull, 4096) != 0 || (int64_t)held <= 0) return -1; |
| 59 | if (omega2_ctx_plat_wait_bind(g_live_ctx, &r, held, (uint64_t)dup(g_last_fd)) != 0 || (int64_t)r != 0) return -1; |
| 60 | g_live_token = held; |
| 61 | } |
| 62 | *mapping = g_live_token; |
| 63 | *offset = (uint64_t)(p - g_base); |
| 64 | *ctx = g_live_ctx; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | #define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0) |
| 69 | |
| 70 | int main(void) { |
| 71 | int fails = 0; |
| 72 | fake_reset(); |
| 73 | OmegaGpuPlatformCaps caps; |
| 74 | const OmegaGpuPlatform *p = omega_gpu_platform_linux(&caps); |
| 75 | OmegaGpuDeviceHandle d; |
| 76 | CHECK(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_OK); |
| 77 | OmegaGpuBufferDesc polled = {.kind = OMEGA_GPU_HANDLE_BUF_DATA, |
| 78 | .rights = OMEGA_GPU_RIGHT_HOST_POLLED | OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE, |
| 79 | .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096}; |
| 80 | OmegaGpuBufferHandle h; OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus; |
| 81 | CHECK(p->dma_alloc(p->ctx, d, &polled, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK); |
| 82 | if (fails) return 1; |
| 83 | g_base = cpu.ptr; |
| 84 | memset(cpu.ptr, 0, 4096); |
| 85 | /* The poll is wrap-aware >= (plat_wait.om: ready when (got - want) mod 2^32 < 2^31). WolframLanguageEvaluator: |
| 86 | * {16^^5a5a5a5a + 16^^10000000, Mod[0 - 16^^6a5a5a5a, 2^32] >= 2^31, Mod[16^^6a5a5a5a - 16^^6a5a5a5b, 2^32] >= 2^31, |
| 87 | * 512*1024*1024} -> {1784306266 (0x6a5a5a5a), True, True, 536870912}. Word 0 stays 0, which is behind the marker, |
| 88 | * so a poll that drops the offset never sees it; one past the marker is not ready. */ |
| 89 | ((volatile uint32_t *)cpu.ptr)[2] = 0x6a5a5a5au; |
| 90 | uint32_t seen = 0; |
| 91 | CHECK(p->poll_word(p->ctx, h, 8, 0x6a5a5a5au, 5, &seen) == OMEGA_GPU_PLAT_OK && seen == 0x6a5a5a5au); |
| 92 | CHECK(g_calls >= 1 && g_bad_rm == 0 && g_live_ctx != NULL); |
| 93 | /* One past the marker is not ready: the poll times out rather than reporting success. */ |
| 94 | CHECK(p->poll_word(p->ctx, h, 8, 0x6a5a5a5bu, 2, &seen) == OMEGA_GPU_POLL_TIMED_OUT); |
| 95 | CHECK(g_bad_rm == 0); |
| 96 | if (fails) return 1; |
| 97 | printf("PLATFORM LINUX LIVE MARKER PASS (poll through the returned context at the returned offset with the platform's Nvrm; one-past timed out; HOST_ONLY)\n"); |
| 98 | return 0; |
| 99 | } |