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_chip_admission.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_chip_admission.c` 288 lines · 15086 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Library GPU admission (developer bars D2, D6): om/admission/chip_admit.om through src/omega_gpu_chip.c, and 3 * its two call sites in nvrm_open. HOST_ONLY: nvrm.c is compiled into this file with open/ioctl/close/mmap 4 * replaced by a simulated driver, so no /dev/nvidia node is ever opened; every path nvrm_open tries to 5 * open is recorded instead. Fake PCI trees are built under a temporary folder. 6 * 1. the pure decision against a C host reference built from the doctor's chip table (omega_gpu_chip_find) 7 * and the prepared Ampere ids of tests/ampere/doctor.py, over every device id and four classes; 8 * 2. nvrm_open on an unknown chip, an empty tree, an unreadable tree and an unmatched CUDAPP_GPU_BDF: 9 * refused with the sentence and zero device-node opens (the negative controls); 10 * 3. nvrm_open on a GB10 tree: the guard passes and the driver's card is checked again, first with the 11 * matching card (reaches the usermode allocation, which the simulation fails), then a different card 12 * (refused before /dev/nvidia0), then a card admitted as sm_86 whose classes select sm_121 (refused). */ 13 #ifndef _GNU_SOURCE 14 #define _GNU_SOURCE 15 #endif 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 #include 25 #include 26 #include 27 #include 28 #include 29 static int sim_ioctl(int, unsigned long, ...); 30 static int sim_open(const char *, int, ...); 31 static int sim_close(int); 32 static void *sim_mmap(void *, size_t, int, int, int, off_t); 33 #define ioctl sim_ioctl 34 #define open sim_open 35 #define close sim_close 36 #define mmap sim_mmap 37 #ifndef NVRM_C 38 #define NVRM_C "../nvrm/nvrm.c" 39 #endif 40 #include NVRM_C 41 #undef ioctl 42 #undef open 43 #undef close 44 #undef mmap 45 #include "cudapp_native.h" /* omega_chip_admit, the generated entry of om/admission/chip_admit.om */ 46 47 #define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); exit(1); } } while (0) 48 49 /* ---- simulated driver ---- */ 50 static char opened[16][32]; 51 static unsigned n_opened; 52 static nv_pci_info_t g_card; 53 static uint32_t g_sm_version; 54 static const uint32_t g_bw_classes[] = { BLACKWELL_COMPUTE_B, BLACKWELL_CHANNEL_GPFIFO_A, HOPPER_USERMODE_A }; 55 56 static int sim_open(const char *path, int flags, ...) { 57 (void)flags; 58 if (n_opened < 16) snprintf(opened[n_opened], sizeof opened[0], "%s", path); 59 n_opened++; 60 /* Control nodes and the GPU node get a fake descriptor; nothing reaches the OS. */ 61 if (!strcmp(path, "/dev/nvidiactl") || !strcmp(path, "/dev/nvidia-uvm") || !strcmp(path, "/dev/nvidia0")) return 700; 62 errno = ENOENT; 63 return -1; 64 } 65 static int sim_close(int fd) { CHECK(fd == 700); return 0; } 66 static void *sim_mmap(void *va, size_t size, int prot, int flags, int fd, off_t offset) { 67 (void)va; (void)size; (void)prot; (void)flags; (void)fd; (void)offset; 68 CHECK(!"no mapping is reached in these tests"); 69 return MAP_FAILED; 70 } 71 static int sim_ioctl(int fd, unsigned long request, ...) { 72 CHECK(fd == 700); 73 va_list ap; va_start(ap, request); void *arg = va_arg(ap, void *); va_end(ap); 74 if (request == UVM_INITIALIZE) { ((UVM_INITIALIZE_PARAMS *)arg)->rmStatus = NV_OK; return 0; } 75 if (request == UVM_MM_INITIALIZE) return 0; 76 CHECK(_IOC_TYPE(request) == NV_IOCTL_MAGIC); 77 switch (_IOC_NR(request)) { 78 case NV_ESC_CARD_INFO: { 79 nv_ioctl_card_info_t *cards = arg; 80 cards[0].valid = 1; cards[0].pci_info = g_card; cards[0].gpu_id = 0x100; cards[0].minor_number = 0; 81 return 0; 82 } 83 case NV_ESC_REGISTER_FD: return 0; 84 case NV_ESC_RM_ALLOC: { 85 NVOS21_PARAMETERS *p = arg; 86 if (p->hClass == NV01_ROOT_CLIENT) p->hObjectNew = 0xc1d00001u; 87 p->status = p->hClass == HOPPER_USERMODE_A ? NV_ERR_INVALID_STATE : NV_OK; /* stop after admission */ 88 return 0; 89 } 90 case NV_ESC_RM_FREE: return 0; 91 case NV_ESC_RM_CONTROL: { 92 NVOS54_PARAMETERS *p = arg; 93 void *params = (void *)(uintptr_t)p->params; 94 p->status = NV_OK; 95 if (p->cmd == NV2080_CTRL_CMD_GR_GET_INFO) { 96 NV2080_CTRL_GR_GET_INFO_PARAMS *g = params; 97 ((NV2080_CTRL_GR_INFO *)(uintptr_t)g->grInfoList)->data = g_sm_version; 98 } else if (p->cmd == NV0080_CTRL_CMD_GPU_GET_CLASSLIST) { 99 NV0080_CTRL_GPU_GET_CLASSLIST_PARAMS *c = params; 100 if (c->classList) memcpy((void *)(uintptr_t)c->classList, g_bw_classes, sizeof g_bw_classes); 101 c->numClasses = 3; 102 } 103 return 0; 104 } 105 default: CHECK(!"unexpected simulated driver operation"); return -1; 106 } 107 } 108 109 /* ---- fake PCI trees ---- */ 110 static char g_tmp[256]; 111 static void put(const char *root, const char *bdf, const char *file, const char *value) { 112 char p[512]; 113 snprintf(p, sizeof p, "%s/sys/bus/pci/devices/%s", root, bdf); 114 char cmd[640]; 115 snprintf(cmd, sizeof cmd, "mkdir -p '%s'", p); 116 CHECK(system(cmd) == 0); 117 snprintf(p, sizeof p, "%s/sys/bus/pci/devices/%s/%s", root, bdf, file); 118 int fd = open(p, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); 119 FILE *f = fd < 0 ? NULL : fdopen(fd, "w"); 120 if (!f && fd >= 0) close(fd); 121 CHECK(f != NULL); 122 fprintf(f, "%s\n", value); 123 fclose(f); 124 } 125 static void gpu(const char *root, const char *bdf, const char *vendor, const char *device, const char *cls) { 126 put(root, bdf, "vendor", vendor); put(root, bdf, "device", device); put(root, bdf, "class", cls); 127 } 128 static const char *tree(const char *name) { 129 static char p[320]; 130 snprintf(p, sizeof p, "%s/%s", g_tmp, name); 131 char cmd[400]; 132 snprintf(cmd, sizeof cmd, "mkdir -p '%s/sys/bus/pci/devices'", p); 133 CHECK(system(cmd) == 0); 134 return p; 135 } 136 137 /* ---- host reference ---- */ 138 static const uint16_t k_ampere[] = { 0x2204u, 0x2486u, 0x2489u }; /* tests/ampere/doctor.py CARDS */ 139 static uint32_t ref_target(uint16_t vendor, uint16_t device, uint32_t cls) { 140 if (vendor != OMEGA_GPU_PCI_VENDOR_NVIDIA || (cls >> 16) != 0x03u) return 0; 141 const OmegaGpuChipRow *r = omega_gpu_chip_find(vendor, device); 142 if (r) return (uint32_t)strtoul(r->sm + 3, NULL, 10); /* "sm_121" */ 143 for (size_t i = 0; i < sizeof k_ampere / sizeof k_ampere[0]; i++) if (k_ampere[i] == device) return 86; 144 return 0; 145 } 146 147 static void differential(void) { 148 static const uint32_t classes[] = { 0x030000u, 0x030200u, 0x040300u, 0x060400u }; 149 static const uint16_t vendors[] = { 0x10deu, 0x1002u }; 150 unsigned same = 0, admitted = 0; 151 for (size_t v = 0; v < 2; v++) 152 for (size_t c = 0; c < 4; c++) 153 for (uint32_t dev = 0; dev <= 0xffffu; dev++) { 154 OmegaGpuPciDev d = { .vendor = vendors[v], .device = (uint16_t)dev, .class_code = classes[c] }; 155 snprintf(d.bdf, sizeof d.bdf, "0000:01:00.0"); 156 OmegaGpuChipAdmission a; 157 int st = omega_gpu_chip_admit(&d, 1, NULL, &a); 158 uint32_t want = ref_target(d.vendor, d.device, d.class_code); 159 int want_st = want ? OMEGA_GPU_ADMIT_OK 160 : (d.vendor == 0x10deu && (d.class_code >> 16) == 3u) ? OMEGA_GPU_ADMIT_REFUSED 161 : OMEGA_GPU_ADMIT_NO_GPU; 162 if (st != want_st || a.target_sm != want || (st == OMEGA_GPU_ADMIT_REFUSED && a.device != dev)) { 163 fprintf(stderr, "CHIP ADMIT DIFF vendor 0x%04x device 0x%04x class 0x%06x: omega status %d sm %u," 164 " reference status %d sm %u\n", d.vendor, dev, d.class_code, st, a.target_sm, want_st, want); 165 exit(1); 166 } 167 same++; 168 admitted += st == OMEGA_GPU_ADMIT_OK; 169 } 170 CHECK(admitted == 2 * 4); /* GB10 and three Ampere ids, each in two display classes */ 171 printf("chip admission: .om decision equals the C host reference on %u single-GPU inputs (%u admitted)\n", same, 172 admitted); 173 } 174 175 static void decisions(void) { 176 OmegaGpuPciDev d[18]; 177 memset(d, 0, sizeof d); 178 OmegaGpuChipAdmission a; 179 CHECK(omega_gpu_chip_admit(d, 0, NULL, &a) == OMEGA_GPU_ADMIT_NO_GPU); 180 CHECK(omega_gpu_chip_admit(d, 0, "000f:01:00.0", &a) == OMEGA_GPU_ADMIT_NO_BDF); 181 snprintf(d[0].bdf, 64, "000f:00:00.0"); d[0].vendor = 0x10de; d[0].device = 0x22d1; d[0].class_code = 0x060400; 182 snprintf(d[1].bdf, 64, "000f:01:00.0"); d[1].vendor = 0x10de; d[1].device = 0x2e12; d[1].class_code = 0x030000; 183 snprintf(d[2].bdf, 64, "000f:01:00.1"); d[2].vendor = 0x10de; d[2].device = 0x22ce; d[2].class_code = 0x040300; 184 CHECK(omega_gpu_chip_admit(d, 3, NULL, &a) == OMEGA_GPU_ADMIT_OK && a.target_sm == 121 && a.index == 1); 185 CHECK(omega_gpu_chip_admit(d, 3, "000f:01:00.0", &a) == OMEGA_GPU_ADMIT_OK && a.target_sm == 121); 186 CHECK(omega_gpu_chip_admit(d, 3, "000f:01:00.1", &a) == OMEGA_GPU_ADMIT_NO_BDF && a.target_sm == 0); 187 /* A second, unknown GPU: refused without an address, ignored with the GB10's address. */ 188 snprintf(d[3].bdf, 64, "0001:01:00.0"); d[3].vendor = 0x10de; d[3].device = 0x2b85; d[3].class_code = 0x030000; 189 CHECK(omega_gpu_chip_admit(d, 4, NULL, &a) == OMEGA_GPU_ADMIT_REFUSED && a.index == 3 && a.device == 0x2b85 && 190 a.target_sm == 0); 191 CHECK(omega_gpu_chip_admit(d, 4, "000f:01:00.0", &a) == OMEGA_GPU_ADMIT_OK && a.target_sm == 121); 192 CHECK(omega_gpu_chip_admit(d, 4, "0001:01:00.0", &a) == OMEGA_GPU_ADMIT_REFUSED && a.device == 0x2b85); 193 /* An address matching two entries and more than 16 GPUs are bad input. */ 194 snprintf(d[3].bdf, 64, "000f:01:00.0"); 195 CHECK(omega_gpu_chip_admit(d, 4, "000f:01:00.0", &a) == OMEGA_GPU_ADMIT_BAD_INPUT && a.target_sm == 0); 196 CHECK(omega_gpu_chip_admit(d, 17, NULL, &a) == OMEGA_GPU_ADMIT_BAD_INPUT); 197 CHECK(omega_gpu_chip_admit(d, -1, NULL, &a) == OMEGA_GPU_ADMIT_BAD_INPUT); 198 { /* The unit's own input contract, called directly (the C wrapper only ever passes 0 or 1 as the match word): 199 * a match word above 1 or a want above 1 is bad input; the same entry with match word 1 is admitted. */ 200 uint64_t w[4 * OMEGA_GPU_ADMIT_MAX] = { 0x10de, 0x2e12, 0x030000, 2 }, o[4] = { 0 }, st = 99; 201 CHECK(omega_chip_admit(&st, w, 4 * OMEGA_GPU_ADMIT_MAX, 1, 1, o, 4) == OMEGA_OK && st == 4 && o[0] == 4 && o[2] == 0); 202 w[3] = 1; 203 CHECK(omega_chip_admit(&st, w, 4 * OMEGA_GPU_ADMIT_MAX, 1, 1, o, 4) == OMEGA_OK && st == 0 && o[2] == 121); 204 CHECK(omega_chip_admit(&st, w, 4 * OMEGA_GPU_ADMIT_MAX, 1, 2, o, 4) == OMEGA_OK && st == 4 && o[2] == 0); 205 } 206 /* Ampere ids admitted as sm_86. */ 207 for (size_t i = 0; i < 3; i++) { 208 d[1].device = k_ampere[i]; 209 CHECK(omega_gpu_chip_admit(d, 3, NULL, &a) == OMEGA_GPU_ADMIT_OK && a.target_sm == 86); 210 } 211 puts("chip admission: decision cases (bridge, audio, two GPUs, address, duplicates, more than 16, Ampere) pass"); 212 } 213 214 static void open_refused(const char *root, const char *bdf, const char *expect) { 215 static Nvrm rm; 216 n_opened = 0; 217 omega_gpu_chip_set_root(root); 218 if (bdf) setenv("CUDAPP_GPU_BDF", bdf, 1); else unsetenv("CUDAPP_GPU_BDF"); 219 CHECK(nvrm_open(&rm) == -1); 220 if (!strstr(rm.err, expect) || n_opened != 0 || rm.va_slot_base != 0 || strlen(rm.err) >= 240) { 221 fprintf(stderr, "FAIL refusal: err \"%s\" (%zu bytes), %u device opens, expected \"%s\"\n", rm.err, 222 strlen(rm.err), n_opened, expect); 223 exit(1); 224 } 225 (void)nvrm_close(&rm); 226 printf("refused, 0 device opens: %s\n", rm.err); 227 } 228 229 static void open_admitted(uint16_t card_device, uint32_t sm_version, const char *expect, const char *last_open) { 230 static Nvrm rm; 231 n_opened = 0; 232 g_card = (nv_pci_info_t){ .domain = 0xf, .bus = 1, .slot = 0, .function = 0, .vendor_id = 0x10de, 233 .device_id = card_device }; 234 g_sm_version = sm_version; 235 CHECK(nvrm_open(&rm) == -1); 236 if (!strstr(rm.err, expect) || n_opened == 0 || n_opened > 16 || strcmp(opened[n_opened - 1], last_open) != 0) { 237 fprintf(stderr, "FAIL admitted path: err \"%s\", %u opens, last \"%s\", expected \"%s\" after \"%s\"\n", 238 rm.err, n_opened, n_opened ? opened[n_opened - 1] : "", expect, last_open); 239 exit(1); 240 } 241 CHECK(strcmp(opened[0], "/dev/nvidiactl") == 0); 242 (void)nvrm_close(&rm); 243 printf("guard passed, %u simulated opens (last %s): %s\n", n_opened, last_open, rm.err); 244 } 245 246 int main(void) { 247 snprintf(g_tmp, sizeof g_tmp, "%s/cudapp-chip-admission-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); 248 CHECK(mkdtemp(g_tmp) != NULL); 249 decisions(); 250 differential(); 251 252 /* Negative controls: refused with the sentence before any device node. */ 253 const char *unknown = tree("unknown"); 254 gpu(unknown, "0000:01:00.0", "0x10de", "0x2b85", "0x030000"); 255 gpu(unknown, "0000:01:00.1", "0x10de", "0x22e8", "0x040300"); 256 open_refused(unknown, NULL, "GPU 0000:01:00.0: NVIDIA device 0x2b85 is not one CUDA++ has qualified, so the library " 257 "opened no /dev/nvidia node. Run `make doctor`"); 258 open_refused(unknown, "0000:01:00.0", "NVIDIA device 0x2b85 is not one CUDA++ has qualified"); 259 open_refused(tree("empty"), NULL, "no NVIDIA GPU under /sys/bus/pci/devices, so the library opened no /dev/nvidia"); 260 char missing[320]; 261 snprintf(missing, sizeof missing, "%s/absent", g_tmp); 262 open_refused(missing, NULL, "to name the GPU, so the library opened no /dev/nvidia node"); 263 const char *gb10 = tree("gb10"); 264 gpu(gb10, "000f:00:00.0", "0x10de", "0x22d1", "0x060400"); 265 gpu(gb10, "000f:01:00.0", "0x10de", "0x2e12", "0x030000"); 266 open_refused(gb10, "0000:02:00.0", "CUDAPP_GPU_BDF=0000:02:00.0 names no NVIDIA GPU"); 267 open_refused(gb10, "000f:00:00.0", "CUDAPP_GPU_BDF=000f:00:00.0 names no NVIDIA GPU"); 268 269 /* Admitted: the guard passes; the driver's card is checked again. */ 270 unsetenv("CUDAPP_GPU_BDF"); 271 omega_gpu_chip_set_root(gb10); 272 /* The physical Spark reports raw RM SM 10.04 for 10de:2e12 (tests/test_ampere_runtime.c). */ 273 open_admitted(0x2e12, NV2080_CTRL_GR_INFO_SM_VERSION_10_04, "RM_ALLOC class 0xc661 status", "/dev/nvidia0"); 274 open_admitted(0x2e13, NV2080_CTRL_GR_INFO_SM_VERSION_12_01, 275 "GPU 000f:01:00.0: the driver reports NVIDIA device 0x2e13, which is not one CUDA++ has qualified", 276 "/dev/nvidia-uvm"); 277 open_admitted(0x2204, NV2080_CTRL_GR_INFO_SM_VERSION_12_01, 278 "GPU admission admitted sm_86 but the driver classes select sm_121; refused", "/dev/nvidia0"); 279 setenv("CUDAPP_GPU_BDF", "000f:01:00.0", 1); 280 open_admitted(0x2e12, NV2080_CTRL_GR_INFO_SM_VERSION_10_04, "RM_ALLOC class 0xc661 status", "/dev/nvidia0"); 281 omega_gpu_chip_set_root(NULL); 282 283 char cmd[320]; 284 snprintf(cmd, sizeof cmd, "rm -rf '%s'", g_tmp); 285 CHECK(system(cmd) == 0); 286 puts("chip admission tests passed (HOST_ONLY: simulated driver, fake PCI trees; chip NOT_RUN)"); 287 return 0; 288 }