interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / nvrm / submit_test.c

119 lines · 6970 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* HOST_ONLY fake device for the GPFIFO submit stores (C4 streams, CUDApp#115).
3 * Two memfd pages stand in for the GPFIFO+USERD allocation and the usermode
4 * doorbell page; both are mapped through the native client exactly as
5 * nvrm_alloc_gpu_uncached / the usermode map do, so the Omega path stores
6 * through its owned handles. Built twice: CUDAPP_MIGRATION_PENDING_C_SUBMIT=0
7 * (Omega, native/streams/submit.om) and =1 (C oracle). Each build writes the
8 * final bytes of both pages plus the put counters to argv[1]; the make rule
9 * compares the two images byte for byte. Never nvrm_open(), never a GPU. */
10#include "nvrm.c"
11#include <assert.h>
12
13enum { PAGE = 0x10000, USERD = 0x8000 };
14
15static void map_fifo(Nvrm *rm, int fd, void *at, NvrmMem *mem, uint32_t slot) {
16 assert(nvrm_cpu_reserve(rm, (uintptr_t)at, PAGE) == at);
17 assert(nvrm_cpu_map(rm, fd, PAGE, at) == at);
18 rm->live[slot] = (NvrmLiveAlloc){.va = (uintptr_t)at, .size = PAGE, .cpu = at, .cpu_live = 1};
19 nvrm_mapping_transfer(rm, &rm->live[slot]);
20 rm->live_count = slot + 1;
21 assert(NVRM_C_SUBMIT || rm->live[slot].omega_mapping);
22 *mem = (NvrmMem){.va = (uintptr_t)at, .size = PAGE, .cpu = at};
23}
24
25static int fresh(size_t n) {
26 int fd = memfd_create("cudapp-streams-submit-fixture", MFD_CLOEXEC);
27 assert(fd >= 0 && ftruncate(fd, (off_t)n) == 0);
28 return fd;
29}
30
31int main(int argc, char **argv) {
32 assert(argc == 2);
33 int ffd = fresh(PAGE), cfd = fresh(PAGE), ufd = fresh(PAGE);
34 static Nvrm rm;
35 map_fifo(&rm, ffd, (void *)(uintptr_t)UINT64_C(0x7000000000), &rm.fifo, 0);
36 rm.usermode_cpu = nvrm_cpu_map(&rm, ufd, PAGE, NULL);
37 assert(rm.usermode_cpu != MAP_FAILED);
38 rm.doorbell = (volatile uint32_t *)((uint8_t *)rm.usermode_cpu + NVC361_NOTIFY_CHANNEL_PENDING);
39 rm.gpfifo = 1; rm.entries = 1024; rm.userd_off = USERD; rm.token = 0x1234abcd;
40 rm.n_extra = 1;
41 NvrmChan *c = &rm.extra[0];
42 map_fifo(&rm, cfd, (void *)(uintptr_t)UINT64_C(0x7000100000), &c->fifo, 1);
43 /* Non power of two ring and a uint32 put about to wrap: GPPut must be
44 * ((put + 1) mod 2^32) mod entries, as the C counter arithmetic gives. */
45 c->gpfifo = 1; c->entries = 6; c->userd_off = USERD; c->token = 0x5678;
46 c->put = c->retired = UINT32_MAX - 2;
47 NvrmMem pb = {.va = UINT64_C(0x123456789ab3), .size = 0x100000}; /* low bits set: the GP entry masks them */
48 volatile uint32_t *chan_gpput = (volatile uint32_t *)((uint8_t *)c->fifo.cpu + USERD + offsetof(Nvc96fControl, GPPut));
49
50 for (uint32_t i = 0; i < 2000; i++) {
51 uint32_t off = (i * 52u) & 0xffffcu, n = 1u + (i * 7919u) % 0x1fffu;
52 if (i == 7) n = 0x1fffff, off = 0, pb.size = 0x800000;
53 assert(nvrm_submit(&rm, &pb, off, n) == 0 && !rm.faulted);
54 nvrm_retire(&rm, rm.put);
55 assert(nvrm_chan_submit(&rm, 1, &pb, off, n) == 0);
56 assert(*chan_gpput == c->put % c->entries); /* every step, including the 2^32 wrap */
57 nvrm_chan_retire(&rm, 1, c->put);
58 pb.size = 0x100000;
59 }
60 /* Refusals before any store, both builds: zero words, unaligned span, full ring. */
61 assert(nvrm_enqueue(&rm, &pb, 0, 0) == -1);
62 rm.faulted = 0;
63 assert(nvrm_chan_submit(&rm, 1, &pb, 2, 1) == -1);
64 rm.faulted = 0;
65 assert(nvrm_chan_submit(&rm, 1, &pb, 0, 1) == 0 && nvrm_chan_submit(&rm, 1, &pb, 0, 2) == 0);
66 assert(nvrm_chan_submit(&rm, 1, &pb, 0, 3) == 0 && nvrm_chan_submit(&rm, 1, &pb, 0, 4) == 0);
67 assert(nvrm_chan_submit(&rm, 1, &pb, 0, 5) == 0);
68 assert(nvrm_chan_submit(&rm, 1, &pb, 0, 6) == -1);
69 rm.faulted = 0;
70
71#if !NVRM_C_SUBMIT
72 /* Omega argument refusals: no store issued, the page is unchanged. */
73 unsigned char before[PAGE], after[PAGE];
74 memcpy(before, rm.fifo.cpu, PAGE);
75 uint64_t r = 0, h = rm.live[0].omega_mapping;
76 assert(omega2_ctx_streams_gp_publish(rm.omega_context, &r, h, 1, 0, USERD, 0, 1) == 0 && (int64_t)r == -EINVAL);
77 assert(omega2_ctx_streams_gp_publish(rm.omega_context, &r, h, 8, 0, USERD + 2, 0, 1) == 0 && (int64_t)r == -EINVAL);
78 assert(omega2_ctx_streams_gp_publish(rm.omega_context, &r, h, 8, 0, PAGE, 0, 1) == 0 && (int64_t)r == -EINVAL);
79 assert(omega2_ctx_streams_gp_publish(rm.omega_context, &r, h, 8, 0, USERD, 0, 0) == 0 && (int64_t)r == -EINVAL);
80 assert(omega2_ctx_streams_gp_publish(rm.omega_context, &r, h, 8, 0, USERD, 0, 0x200000) == 0 && (int64_t)r == -EINVAL);
81 assert(omega2_ctx_streams_ring(rm.omega_context, &r, rm.omega_usermode_mapping, 0x91, 1) == 0 && (int64_t)r == -EINVAL);
82 assert(omega2_ctx_streams_ring(rm.omega_context, &r, rm.omega_usermode_mapping, 0x90, UINT64_C(0x100000000)) == 0 && (int64_t)r == -EINVAL);
83 /* nvrm_live_token: one owner and its offset; outside/straddling/no-owner/quarantined refused. */
84 uint64_t tk = 0, of = 0;
85 assert(nvrm_live_token(&rm, (uint8_t *)c->fifo.cpu + 0x40, 8, &tk, &of) == 0 && tk == rm.live[1].omega_mapping && of == 0x40);
86 assert(nvrm_live_token(&rm, (uint8_t *)c->fifo.cpu + PAGE - 4, 8, &tk, &of) == -1 && errno == EFAULT);
87 assert(nvrm_live_token(&rm, &tk, 8, &tk, &of) == -1 && errno == EFAULT);
88 assert(nvrm_live_token(&rm, c->fifo.cpu, 0, &tk, &of) == -1 && errno == EINVAL);
89 rm.live[1].quarantined = 1;
90 assert(nvrm_live_token(&rm, c->fifo.cpu, 8, &tk, &of) == -1 && errno == EFAULT);
91 rm.live[1].quarantined = 0;
92 /* A foreign handle is refused by the context, never dereferenced. */
93 assert(omega2_ctx_streams_gp_publish(rm.omega_context, &r, h ^ 0x40000, 8, 0, USERD, 0, 1) != 0 || (int64_t)r < 0);
94 /* Without an owned handle the enqueue refuses rather than storing through the pointer. */
95 rm.live[0].omega_mapping = 0;
96 uint32_t put_before = rm.put;
97 assert(nvrm_enqueue(&rm, &pb, 0, 1) == -1 && rm.put == put_before);
98 rm.live[0].omega_mapping = h; rm.faulted = 0;
99 memcpy(after, rm.fifo.cpu, PAGE);
100 assert(!memcmp(before, after, PAGE));
101#endif
102
103 int out_fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
104 FILE *out = out_fd < 0 ? NULL : fdopen(out_fd, "wb");
105 if (!out && out_fd >= 0) close(out_fd);
106 assert(out);
107 assert(fwrite(rm.fifo.cpu, 1, PAGE, out) == PAGE);
108 assert(fwrite(c->fifo.cpu, 1, PAGE, out) == PAGE);
109 assert(fwrite(rm.usermode_cpu, 1, PAGE, out) == PAGE);
110 assert(fwrite(&rm.put, sizeof rm.put, 1, out) == 1 && fwrite(&c->put, sizeof c->put, 1, out) == 1);
111 assert(fclose(out) == 0);
112 /* Spot checks independent of the other build: last GP entry and GPPut. */
113 uint32_t gpput = *(volatile uint32_t *)((uint8_t *)c->fifo.cpu + USERD + offsetof(Nvc96fControl, GPPut));
114 assert(gpput == c->put % c->entries);
115 assert(*rm.doorbell == c->token);
116 printf("STREAMS SUBMIT FIXTURE %s: put %u/%u, chan GPPut %u, doorbell 0x%x (HOST_ONLY, live tier NOT_RUN)\n",
117 NVRM_C_SUBMIT ? "C" : "OMEGA", rm.put, c->put, gpput, *rm.doorbell);
118 return 0;
119}