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_gpu_platform_aien_fx.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_gpu_platform_aien_fx.c` 281 lines · 13358 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* test_omega_gpu_platform_aien_fx: the aien-gpu platform's REAL effects (no OmegaGpuAienSys replacement) against a 3 * fake device file. Host program, opens no device and needs no module. 4 * 5 * The fake device is a memfd named "aien-fx"; CUDAPP_AIEN_GPU_DEV=/proc/self/fd/N makes the platform open it as it 6 * would open /dev/aien-gpu, so open(2), mmap(2) of objects and of the doorbell page, munmap(2) and close(2) are the 7 * kernel's. Only the ioctls are emulated: a seccomp filter turns every ioctl of type 'A' into SIGSYS, and the handler 8 * answers AGPU_INFO / MEM_ALLOC / MEM_FREE / CHAN_CREATE / CHAN_DESTROY like aien-gpu.ko would (objects at bump 9 * offsets inside the memfd, the usermode page at offset 0), records the request bytes in and out, and returns. 10 * 11 * Built twice (mk/omega-gpu-engine.mk test-gpu-platform-aien-effects): against native/aiengpu/effects.om through the 12 * generated bridge library (CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS=0) and against the C system calls 13 * (=1). The trace written to descriptor 3 when AIEN_FX_TRACE_FD=3 (every ioctl's bytes in and out, then every nonzero 32-byte line of the 14 * fake device: pushbuffer, ring, USERD GPPut, doorbell word) must be byte-identical between the two builds. 15 * It also checks: every object freed, the channel destroyed, and no mapping of the device left in /proc/self/maps 16 * after device_close; in the Omega build, an operator request (AGPU_BOOT) is refused by the unit with -22 and never 17 * reaches the kernel. 18 */ 19 #ifndef CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS 20 #define CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS 0 /* as src/omega_gpu_platform_aien.c on AArch64 */ 21 #endif 22 #include "omega_gpu_platform_aien.h" 23 #include "aien_gpu_uapi.h" 24 #if !CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS 25 #include "cudapp_aiengpu_native.h" 26 #endif 27 28 #include 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 #include 37 #include 38 #include 39 #include 40 #include 41 #include 42 #include 43 #include 44 #include 45 46 #if !defined(__aarch64__) 47 #error "the aien-gpu effects fixture reads the AArch64 SIGSYS register frame" 48 #endif 49 50 #define FX_BYTES (8u << 20) 51 #define FX_DOORBELL_OFF 0ull 52 #define FX_FIRST_OBJ 0x10000ull 53 #define FX_TOKEN 0x00010007u 54 #define FX_OBJS 16 55 56 static int ok = 1; 57 #define EXPECT(c) do { if (!(c)) { ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0) 58 59 typedef struct { uint64_t off, size; uint32_t flags; int live; } FxObj; 60 static struct { 61 int memfd; 62 ino_t ino; 63 uint64_t bump; 64 FxObj obj[FX_OBJS]; 65 uint32_t nobj, traps, chan_live, chan_creates, chan_destroys, frees; 66 } fx; 67 68 /* ---- trace: built in a static buffer by the signal handler (no stdio there), written out at the end ---- */ 69 static char tbuf[1u << 20]; 70 static size_t tlen; 71 static void t_str(const char *s) { while (*s && tlen < sizeof tbuf) tbuf[tlen++] = *s++; } 72 static void t_hex(const void *p, size_t n) { 73 static const char hx[] = "`0123456789abcdef`"; 74 const uint8_t *b = p; 75 for (size_t i = 0; i < n && tlen + 2 < sizeof tbuf; i++) { tbuf[tlen++] = hx[b[i] >> 4]; tbuf[tlen++] = hx[b[i] & 15u]; } 76 } 77 static void t_u64(uint64_t v) { 78 uint8_t be[8]; 79 for (int i = 0; i < 8; i++) be[i] = (uint8_t)(v >> (56 - 8 * i)); 80 t_hex(be, 8); 81 } 82 83 static FxObj *obj_of(uint32_t h) { return (h >= 1 && h <= fx.nobj && fx.obj[h - 1].live) ? &fx.obj[h - 1] : NULL; } 84 85 /* aien-gpu.ko's answer to one request; 0 or -errno as the syscall would return it. */ 86 static long fx_ioctl(unsigned long req, void *arg) { 87 if (req == AGPU_INFO) { 88 struct agpu_info *i = arg; 89 memset(i, 0, sizeof *i); 90 i->abi = AGPU_ABI_VERSION; i->state = AGPU_STATE_RM_READY; 91 return 0; 92 } 93 if (req == AGPU_MEM_ALLOC) { 94 struct agpu_mem_alloc *a = arg; 95 if (a->size == 0 || (a->size & 4095u) || (a->va & 4095u) || fx.nobj >= FX_OBJS || 96 fx.bump + a->size > FX_BYTES) { a->st.verdict = AGPU_RM_E_ARG; return 0; } 97 FxObj *o = &fx.obj[fx.nobj++]; 98 *o = (FxObj){ .off = fx.bump, .size = a->size, .flags = a->flags, .live = 1 }; 99 fx.bump = (fx.bump + a->size + 0xffffu) & ~0xffffull; 100 a->handle = fx.nobj; a->mmap_off = o->off; a->st.verdict = AGPU_RM_OK; 101 return 0; 102 } 103 if (req == AGPU_MEM_FREE) { 104 struct agpu_mem_free *f = arg; 105 FxObj *o = obj_of(f->handle); 106 if (!o) { f->st.verdict = AGPU_RM_E_ARG; return 0; } 107 o->live = 0; fx.frees++; f->st.verdict = AGPU_RM_OK; 108 return 0; 109 } 110 if (req == AGPU_CHAN_CREATE) { 111 struct agpu_chan_create *c = arg; 112 if (fx.chan_live || !obj_of(c->ring_handle) || !obj_of(c->userd_handle) || !obj_of(c->err_handle)) { 113 c->st.verdict = AGPU_RM_E_ARG; return 0; 114 } 115 c->chid = 7; c->token = FX_TOKEN; c->doorbell_off = FX_DOORBELL_OFF; c->channel = 77; c->st.verdict = AGPU_RM_OK; 116 fx.chan_live = 1; fx.chan_creates++; 117 return 0; 118 } 119 if (req == AGPU_CHAN_DESTROY) { 120 struct agpu_chan_destroy *d = arg; 121 if (!fx.chan_live || d->channel != 77) { d->st.verdict = AGPU_RM_E_ARG; return 0; } 122 fx.chan_live = 0; fx.chan_destroys++; d->st.verdict = AGPU_RM_OK; 123 return 0; 124 } 125 return -ENOTTY; 126 } 127 128 static void on_sigsys(int sig, siginfo_t *si, void *uc_) { 129 (void)sig; 130 ucontext_t *uc = uc_; 131 if (si->si_syscall != __NR_ioctl) { uc->uc_mcontext.regs[0] = (uint64_t)-ENOSYS; return; } 132 const int fd = (int)uc->uc_mcontext.regs[0]; 133 const unsigned long req = (unsigned long)(uint32_t)uc->uc_mcontext.regs[1]; 134 void *arg = (void *)(uintptr_t)uc->uc_mcontext.regs[2]; 135 struct stat st; 136 fx.traps++; 137 long rc; 138 if (fstat(fd, &st) != 0 || st.st_ino != fx.ino) rc = -EBADF; 139 else { 140 t_str("ioctl "); t_u64(req); t_str(" in "); t_hex(arg, _IOC_SIZE(req)); 141 rc = fx_ioctl(req, arg); 142 t_str(" rc "); t_u64((uint64_t)rc); t_str(" out "); t_hex(arg, _IOC_SIZE(req)); t_str("\n"); 143 } 144 uc->uc_mcontext.regs[0] = (uint64_t)rc; 145 } 146 147 static void trap_aien_ioctls(void) { 148 static uint8_t altstack[1u << 16]; 149 stack_t ss = { .ss_sp = altstack, .ss_size = sizeof altstack }; 150 struct sigaction sa; 151 memset(&sa, 0, sizeof sa); 152 sa.sa_sigaction = on_sigsys; 153 sa.sa_flags = SA_SIGINFO | SA_ONSTACK; 154 if (sigaltstack(&ss, NULL) != 0 || sigaction(SIGSYS, &sa, NULL) != 0) { perror("sigaction"); exit(2); } 155 struct sock_filter f[] = { 156 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)), 157 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_ioctl, 0, 4), 158 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, args[1])), /* low word, little endian */ 159 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xff00u), 160 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, (uint32_t)'A' << 8, 0, 1), 161 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP), 162 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), 163 }; 164 struct sock_fprog prog = { .len = (unsigned short)(sizeof f / sizeof f[0]), .filter = f }; 165 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0 || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) != 0) { 166 perror("seccomp"); exit(2); 167 } 168 } 169 170 /* Every nonzero 32-byte line of the fake device, by file offset. */ 171 static void trace_device(void) { 172 uint8_t line[32]; 173 static const uint8_t zero[32]; 174 for (uint64_t off = 0; off < fx.bump; off += sizeof line) { 175 if (pread(fx.memfd, line, sizeof line, (off_t)off) != (ssize_t)sizeof line) { ok = 0; return; } 176 if (memcmp(line, zero, sizeof line) == 0) continue; 177 t_str("dev "); t_u64(off); t_str(" "); t_hex(line, sizeof line); t_str("\n"); 178 } 179 } 180 181 static int device_still_mapped(void) { 182 FILE *m = fopen("/proc/self/maps", "r"); 183 char l[512]; 184 int n = 0; 185 if (!m) return -1; 186 while (fgets(l, sizeof l, m)) n += strstr(l, "memfd:aien-fx") != NULL; 187 fclose(m); 188 return n; 189 } 190 191 static OmegaGpuBufferDesc desc(uint32_t kind, uint32_t rights, uint32_t cache, uint64_t bytes) { 192 OmegaGpuBufferDesc d = { .kind = kind, .rights = rights, .gpu_cache = cache, .align = 0, .bytes = bytes }; 193 return d; 194 } 195 #define RW (OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE) 196 197 static int run(void) { 198 fx.memfd = memfd_create("aien-fx", MFD_CLOEXEC); 199 struct stat st; 200 if (fx.memfd < 0 || ftruncate(fx.memfd, FX_BYTES) != 0 || fstat(fx.memfd, &st) != 0) { perror("memfd"); return 2; } 201 fx.ino = st.st_ino; 202 fx.bump = FX_FIRST_OBJ; /* [0, 64 KiB): the usermode (doorbell) page */ 203 char path[64]; 204 snprintf(path, sizeof path, "/proc/self/fd/%d", fx.memfd); 205 setenv("CUDAPP_AIEN_GPU_DEV", path, 1); 206 setenv("CUDAPP_MEM_RESERVE_MIB", "0", 1); 207 trap_aien_ioctls(); 208 209 const OmegaGpuPlatform *p = omega_gpu_platform_aien(NULL); 210 OmegaGpuDeviceHandle dev; 211 OmegaGpuBufferHandle ring, marker, cached; 212 OmegaCpuView ring_cpu, marker_cpu, cached_cpu; 213 OmegaGpuVa ring_va, marker_va, cached_va; 214 OmegaGpuQueueHandle q; 215 OmegaGpuBufferDesc dr = desc(OMEGA_GPU_HANDLE_BUF_QUEUE, OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ, 216 OMEGA_GPU_CACHE_GPU_UNCACHED, 0x10000); 217 OmegaGpuBufferDesc dm = desc(OMEGA_GPU_HANDLE_BUF_DATA, RW | OMEGA_GPU_RIGHT_HOST_POLLED, OMEGA_GPU_CACHE_GPU_UNCACHED, 4096); 218 OmegaGpuBufferDesc dc = desc(OMEGA_GPU_HANDLE_BUF_DATA, OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ, 219 OMEGA_GPU_CACHE_GPU_CACHED, 4096); 220 int r = p->device_open(p->ctx, 0, &dev); 221 if (r != OMEGA_GPU_PLAT_OK) { printf(" device_open %d: %s\n", r, p->diag_text(p->ctx)); return 1; } 222 EXPECT(p->dma_alloc(p->ctx, dev, &dr, &ring, &ring_cpu, &ring_va, NULL) == OMEGA_GPU_PLAT_OK); 223 EXPECT(p->dma_alloc(p->ctx, dev, &dm, &marker, &marker_cpu, &marker_va, NULL) == OMEGA_GPU_PLAT_OK); 224 EXPECT(p->dma_alloc(p->ctx, dev, &dc, &cached, &cached_cpu, &cached_va, NULL) == OMEGA_GPU_PLAT_OK); 225 r = p->queue_open(p->ctx, dev, ring, &q); 226 if (r != OMEGA_GPU_PLAT_OK) { printf(" queue_open %d: %s\n", r, p->diag_text(p->ctx)); return 1; } 227 /* the CPU view is the device file: a store through it is in the memfd */ 228 if (marker_cpu.ptr) *(volatile uint32_t *)marker_cpu.ptr = 0x5eedf00du; 229 const uint32_t w[8] = { 0x2005005cu, (uint32_t)marker_va.va, (uint32_t)(marker_va.va >> 32), 1u, 0u, 0x00100001u, 230 0x20010040u, 0x0000a5a5u }; 231 EXPECT(p->queue_submit(p->ctx, q, w, 6) == OMEGA_GPU_PLAT_OK); 232 t_str("--- after submit 1\n"); trace_device(); 233 EXPECT(p->sync_cpu_to_gpu(p->ctx, cached, 0, 4096) == OMEGA_GPU_PLAT_OK); /* GPU-cached: the next submit has the prefix */ 234 EXPECT(p->queue_submit(p->ctx, q, w, 8) == OMEGA_GPU_PLAT_OK); 235 t_str("--- after submit 2 (L2 prefix)\n"); trace_device(); 236 EXPECT(p->queue_submit(p->ctx, q, w + 6, 2) == OMEGA_GPU_PLAT_OK); 237 t_str("--- after submit 3\n"); trace_device(); 238 EXPECT(p->queue_retire(p->ctx, q) == OMEGA_GPU_PLAT_OK); 239 EXPECT(p->dma_free(p->ctx, cached) == OMEGA_GPU_PLAT_OK); 240 EXPECT(p->queue_close(p->ctx, q) == OMEGA_GPU_PLAT_OK); 241 EXPECT(p->dma_free(p->ctx, marker) == OMEGA_GPU_PLAT_OK); 242 EXPECT(p->dma_free(p->ctx, ring) == OMEGA_GPU_PLAT_OK); 243 EXPECT(p->device_close(p->ctx, dev) == OMEGA_GPU_PLAT_OK); 244 uint32_t live = 0; 245 for (uint32_t i = 0; i < fx.nobj; i++) live += (uint32_t)fx.obj[i].live; 246 EXPECT(fx.nobj == 5 && live == 0 && fx.frees == 5); /* 3 buffers, the GPFIFO buffer, the notifier */ 247 EXPECT(fx.chan_creates == 1 && fx.chan_destroys == 1 && fx.chan_live == 0); 248 EXPECT(device_still_mapped() == 0); 249 #if !CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS 250 { 251 /* An operator request is not in the unit's allowlist: refused with -22, no ioctl reaches the kernel. */ 252 uint8_t blk[_IOC_SIZE(AGPU_BOOT)]; 253 uint64_t res = 0; 254 memset(blk, 0, sizeof blk); 255 const int dfd = open(path, O_RDWR | O_CLOEXEC); 256 const uint32_t traps = fx.traps; 257 EXPECT(dfd >= 0 && omega2_aiengpu_ioctl(&res, (uint64_t)dfd, AGPU_BOOT, blk, sizeof blk) == 0 && 258 (int64_t)res == -EINVAL && fx.traps == traps); 259 if (dfd >= 0) close(dfd); 260 } 261 #endif 262 t_str("--- closed\n"); trace_device(); 263 const char *tp = getenv("AIEN_FX_TRACE_FD"); 264 if (tp) { 265 if (strcmp(tp, "3") != 0 || write(3, tbuf, tlen) != (ssize_t)tlen) { perror("trace"); return 2; } 266 } 267 return ok ? 0 : 1; 268 } 269 270 int main(void) { 271 setvbuf(stdout, NULL, _IONBF, 0); 272 /* the seccomp filter cannot be removed, so the case runs in a child */ 273 const pid_t pid = fork(); 274 if (pid == 0) _exit(run()); 275 int status = 0; 276 if (pid < 0 || waitpid(pid, &status, 0) != pid) { perror("fork"); return 2; } 277 const int rc = WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status); 278 printf("%s aien-gpu effects fixture (%s): rc %d\n", rc == 0 ? "PASS" : "FAIL", 279 CUDAPP_MIGRATION_PENDING_C_PLATFORM_AIEN_EFFECTS ? "C system calls" : "native/aiengpu/effects.om", rc); 280 return rc; 281 }