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 / native / nvml / nvml.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note nvml.om: state, transport requests, error policy, nvmlInit_v2 and nvmlShutdown for compat/nvml. |
| 3 | note Pure unit nvml_step (nvml.om, query.om, step.om). The generated library (native/nvml/api.om, lowered |
| 4 | note to compat/nvml) owns no policy: it calls omega_nvml_step, performs the one action the step asks for |
| 5 | note through omega2_nvrm_open_file, omega2_nvrm_close_file or omega2_nvrm_ioctl (native/nvrm/transport.om), |
| 6 | note writes the result back and calls the step again until the step reports done. |
| 7 | note RM layouts: open-gpu-kernel-modules 580.173.02 (commit 20e4e6e1), src/common/sdk/nvidia/inc. |
| 8 | note Constants: evidence/NVML-2026-11-06/wolfram.txt. |
| 9 | note |
| 10 | note s (ulong 128), written by C: 0 op, 1..4 arguments, 14 action result, 15 omega2 status. |
| 11 | note written by the step: 5 scalar output, 6 nvmlReturn_t, 7 bytes of b at OO to copy out, |
| 12 | note 8 action (0 done, 1 ioctl, 2 open the path at PO of length 13, 3 close), 9 fd, |
| 13 | note 10 ioctl nr, 11 header bytes at b 0, 12 params bytes at b PO, 27 store the scalar (1). |
| 14 | note state: 16 init refcount, 17 phase (1 live), 18 control fd+1, 19 root client, 20 next handle, |
| 15 | note 21 GPU count, 22 resume point, 23 loop index, 24 pending error, 25 cursor, 26 scratch. |
| 16 | note GPU k (at most 8) at 32+8k: 0 fd+1, 1 minor, 2 gpuId, 3 deviceInstance, 4 device handle, |
| 17 | note 5 subdevice handle, 6 compute class. |
| 18 | note b (uchar 83296): header 0..4607, params PO..PO+55887, output OO..OO+22799. |
| 19 | |
| 20 | const SN ulong 128 |
| 21 | const BN ulong 83296 |
| 22 | const PO ulong 4608 |
| 23 | const OO ulong 60496 |
| 24 | |
| 25 | fn g32 b edit uchar BN off ulong to ulong |
| 26 | v = ulong 0 |
| 27 | for i ulong 4 |
| 28 | v = or v shl ulong get b add off i mul i 8 |
| 29 | end |
| 30 | ret v |
| 31 | end |
| 32 | |
| 33 | fn g64 b edit uchar BN off ulong to ulong |
| 34 | v = ulong 0 |
| 35 | for i ulong 8 |
| 36 | v = or v shl ulong get b add off i mul i 8 |
| 37 | end |
| 38 | ret v |
| 39 | end |
| 40 | |
| 41 | fn p32 b edit uchar BN off ulong v ulong to void |
| 42 | for i ulong 4 |
| 43 | put b add off i uchar and shr v mul i 8 255 |
| 44 | end |
| 45 | end |
| 46 | |
| 47 | fn p64 b edit uchar BN off ulong v ulong to void |
| 48 | for i ulong 8 |
| 49 | put b add off i uchar and shr v mul i 8 255 |
| 50 | end |
| 51 | end |
| 52 | |
| 53 | fn bzero b edit uchar BN off ulong n ulong to void |
| 54 | for i ulong n |
| 55 | put b add off i 0 |
| 56 | end |
| 57 | end |
| 58 | |
| 59 | fn dv k ulong f ulong to ulong |
| 60 | ret add 32 add mul k 8 f |
| 61 | end |
| 62 | |
| 63 | fn done s edit ulong SN code ulong to void |
| 64 | put s 6 code |
| 65 | put s 8 0 |
| 66 | put s 22 0 |
| 67 | end |
| 68 | |
| 69 | fn act s edit ulong SN k ulong fd ulong nr ulong hl ulong pl ulong to void |
| 70 | put s 8 k |
| 71 | put s 9 fd |
| 72 | put s 10 nr |
| 73 | put s 11 hl |
| 74 | put s 12 pl |
| 75 | end |
| 76 | |
| 77 | fn cfd s edit ulong SN to ulong |
| 78 | ret sub get s 18 1 |
| 79 | end |
| 80 | |
| 81 | note NV_ESC_RM_CONTROL (nr 42), NVOS54 32-byte header: hClient 0, hObject 4, cmd 8, status 28. |
| 82 | fn ctl s edit ulong SN b edit uchar BN obj ulong cmd ulong n ulong to void |
| 83 | bzero b 0 32 |
| 84 | p32 b 0 get s 19 |
| 85 | p32 b 4 obj |
| 86 | p32 b 8 cmd |
| 87 | act s 1 cfd s 42 32 n |
| 88 | end |
| 89 | |
| 90 | note NV_ESC_RM_ALLOC (nr 43), NVOS21 32-byte header: hRoot 0, parent 4, hObjectNew 8, hClass 12, |
| 91 | note status 28. The root client (class 65, NV01_ROOT_CLIENT) takes its handle from RM. |
| 92 | fn alc s edit ulong SN b edit uchar BN par ulong cls ulong n ulong to void |
| 93 | bzero b 0 32 |
| 94 | p32 b 0 get s 19 |
| 95 | p32 b 4 par |
| 96 | if ne cls 65 |
| 97 | p32 b 8 get s 20 |
| 98 | put s 20 add get s 20 1 |
| 99 | end |
| 100 | p32 b 12 cls |
| 101 | act s 1 cfd s 43 32 n |
| 102 | end |
| 103 | |
| 104 | note NV_ESC_RM_FREE (nr 41), NVOS00 16-byte header: hRoot 0, parent 4, hObjectOld 8, status 12. |
| 105 | fn rmfree s edit ulong SN b edit uchar BN root ulong par ulong obj ulong to void |
| 106 | bzero b 0 16 |
| 107 | p32 b 0 root |
| 108 | p32 b 4 par |
| 109 | p32 b 8 obj |
| 110 | act s 1 cfd s 41 16 0 |
| 111 | end |
| 112 | |
| 113 | note The device path /dev/nvidia<minor> or /dev/nvidiactl at PO; result its length. |
| 114 | fn path b edit uchar BN minor ulong isctl ulong to ulong |
| 115 | p64 b PO 7599382571363165231 |
| 116 | if eq isctl 1 |
| 117 | p64 b add PO 8 119247139334500 |
| 118 | ret 14 |
| 119 | end |
| 120 | p32 b add PO 8 6383972 |
| 121 | n = add PO 11 |
| 122 | if ge minor 100 |
| 123 | put b n uchar add 48 div minor 100 |
| 124 | n = add n 1 |
| 125 | end |
| 126 | if ge minor 10 |
| 127 | put b n uchar add 48 rem div minor 10 10 |
| 128 | n = add n 1 |
| 129 | end |
| 130 | put b n uchar add 48 rem minor 10 |
| 131 | ret sub add n 1 PO |
| 132 | end |
| 133 | |
| 134 | note Transport results are a long carried as its two's-complement bits in a ulong; -1 is |
| 135 | note 18446744073709551615 (2^64-1), -2 ...614, -6 ...610, -13 ...603, -19 ...597. |
| 136 | note An ioctl result: a nonzero omega2 status or an errno other than EPERM/EACCES (NO_PERMISSION 4) |
| 137 | note is UNKNOWN (999). |
| 138 | fn ioerr s edit ulong SN to ulong |
| 139 | if ne get s 15 0 |
| 140 | ret 999 |
| 141 | end |
| 142 | x = get s 14 |
| 143 | if eq x 0 |
| 144 | ret 0 |
| 145 | end |
| 146 | if or eq x 18446744073709551615 eq x 18446744073709551603 |
| 147 | ret 4 |
| 148 | end |
| 149 | ret 999 |
| 150 | end |
| 151 | |
| 152 | note An open result: ENOENT, ENXIO or ENODEV is DRIVER_NOT_LOADED (9), EPERM or EACCES NO_PERMISSION. |
| 153 | note A non-negative result (below 2^63) is the descriptor. |
| 154 | fn operr s edit ulong SN to ulong |
| 155 | if ne get s 15 0 |
| 156 | ret 999 |
| 157 | end |
| 158 | x = get s 14 |
| 159 | if lt x 9223372036854775808 |
| 160 | ret 0 |
| 161 | end |
| 162 | if or or eq x 18446744073709551614 eq x 18446744073709551610 eq x 18446744073709551597 |
| 163 | ret 9 |
| 164 | end |
| 165 | if or eq x 18446744073709551615 eq x 18446744073709551603 |
| 166 | ret 4 |
| 167 | end |
| 168 | ret 999 |
| 169 | end |
| 170 | |
| 171 | note RM status (nvstatuscodes.h): 0x1B INSUFFICIENT_PERMISSIONS -> 4, 0x0F GPU_IS_LOST -> 15, |
| 172 | note 0x56 NOT_SUPPORTED -> 3, anything else UNKNOWN. |
| 173 | fn rmerr v ulong to ulong |
| 174 | if eq v 0 |
| 175 | ret 0 |
| 176 | end |
| 177 | if eq v 27 |
| 178 | ret 4 |
| 179 | end |
| 180 | if eq v 15 |
| 181 | ret 15 |
| 182 | end |
| 183 | if eq v 86 |
| 184 | ret 3 |
| 185 | end |
| 186 | ret 999 |
| 187 | end |
| 188 | |
| 189 | fn chk s edit ulong SN b edit uchar BN off ulong to ulong |
| 190 | e = ioerr s |
| 191 | if ne e 0 |
| 192 | ret e |
| 193 | end |
| 194 | ret rmerr g32 b off |
| 195 | end |
| 196 | |
| 197 | note Teardown, resume points 100 and 101: per GPU free the subdevice, free the device, close its fd; |
| 198 | note then free the root client and close the control fd. Cursor s 25 walks items 0..25. |
| 199 | fn tdtry s edit ulong SN b edit uchar BN t ulong to bool |
| 200 | if lt t 24 |
| 201 | d = div t 3 |
| 202 | r = rem t 3 |
| 203 | if eq r 0 |
| 204 | x = get s dv d 5 |
| 205 | if ne x 0 |
| 206 | put s dv d 5 0 |
| 207 | rmfree s b get s 19 get s dv d 4 x |
| 208 | ret true |
| 209 | end |
| 210 | ret false |
| 211 | end |
| 212 | if eq r 1 |
| 213 | x = get s dv d 4 |
| 214 | if ne x 0 |
| 215 | put s dv d 4 0 |
| 216 | rmfree s b get s 19 get s 19 x |
| 217 | ret true |
| 218 | end |
| 219 | ret false |
| 220 | end |
| 221 | x = get s dv d 0 |
| 222 | if ne x 0 |
| 223 | put s dv d 0 0 |
| 224 | act s 3 sub x 1 0 0 0 |
| 225 | ret true |
| 226 | end |
| 227 | ret false |
| 228 | end |
| 229 | if eq t 24 |
| 230 | x = get s 19 |
| 231 | if ne x 0 |
| 232 | rmfree s b x x x |
| 233 | put s 19 0 |
| 234 | ret true |
| 235 | end |
| 236 | ret false |
| 237 | end |
| 238 | x = get s 18 |
| 239 | if ne x 0 |
| 240 | act s 3 sub x 1 0 0 0 |
| 241 | put s 18 0 |
| 242 | ret true |
| 243 | end |
| 244 | ret false |
| 245 | end |
| 246 | |
| 247 | fn tdstep s edit ulong SN b edit uchar BN to void |
| 248 | for t ulong 26 |
| 249 | if ge t get s 25 |
| 250 | if tdtry s b t |
| 251 | put s 25 add t 1 |
| 252 | put s 22 101 |
| 253 | ret |
| 254 | end |
| 255 | end |
| 256 | end |
| 257 | e = get s 24 |
| 258 | if eq e 0 |
| 259 | e = get s 26 |
| 260 | end |
| 261 | for i ulong 96 |
| 262 | put s add 16 i 0 |
| 263 | end |
| 264 | done s e |
| 265 | end |
| 266 | |
| 267 | fn tdres s edit ulong SN b edit uchar BN to void |
| 268 | e = ulong 0 |
| 269 | if eq get s 8 1 |
| 270 | e = chk s b 12 |
| 271 | else |
| 272 | e = ioerr s |
| 273 | end |
| 274 | if ne e 0 |
| 275 | put s 26 999 |
| 276 | end |
| 277 | tdstep s b |
| 278 | end |
| 279 | |
| 280 | fn fail s edit ulong SN b edit uchar BN code ulong to void |
| 281 | put s 24 code |
| 282 | put s 25 0 |
| 283 | put s 26 0 |
| 284 | tdstep s b |
| 285 | end |
| 286 | |
| 287 | note Next GPU: NV0000_CTRL_CMD_GPU_GET_ID_INFO_V2 (0x205, ctrl0000gpu.h:172-185) gives deviceInstance. |
| 288 | fn devnext s edit ulong SN b edit uchar BN to void |
| 289 | k = get s 23 |
| 290 | if ge k get s 21 |
| 291 | put s 17 1 |
| 292 | put s 16 1 |
| 293 | done s 0 |
| 294 | ret |
| 295 | end |
| 296 | bzero b PO 32 |
| 297 | p32 b PO get s dv k 2 |
| 298 | ctl s b get s 19 517 32 |
| 299 | put s 22 5 |
| 300 | end |
| 301 | |
| 302 | note NV_ESC_CARD_INFO (nr 200): 64 entries of 72 bytes, valid 0, gpu_id 16, minor_number 56. |
| 303 | fn cards s edit ulong SN b edit uchar BN to void |
| 304 | e = ioerr s |
| 305 | if ne e 0 |
| 306 | fail s b e |
| 307 | ret |
| 308 | end |
| 309 | n = ulong 0 |
| 310 | for i ulong 64 |
| 311 | o = mul i 72 |
| 312 | if ne get b o 0 |
| 313 | if ge n 8 |
| 314 | fail s b 999 |
| 315 | ret |
| 316 | end |
| 317 | m = g32 b add o 56 |
| 318 | if gt m 255 |
| 319 | fail s b 999 |
| 320 | ret |
| 321 | end |
| 322 | put s dv n 1 m |
| 323 | put s dv n 2 g32 b add o 16 |
| 324 | n = add n 1 |
| 325 | end |
| 326 | end |
| 327 | put s 21 n |
| 328 | put s 23 0 |
| 329 | devnext s b |
| 330 | end |
| 331 | |
| 332 | note NV0080_CTRL_CMD_GPU_GET_CLASSLIST_V2 (0x800292, ctrl0080gpu.h:506): numClasses 0, classList 4. |
| 333 | note 52928 is BLACKWELL_COMPUTE_B (0xCEC0), the class GET_PIDS filters on. |
| 334 | fn clist s edit ulong SN b edit uchar BN k ulong to void |
| 335 | n = g32 b PO |
| 336 | if gt n 200 |
| 337 | fail s b 999 |
| 338 | ret |
| 339 | end |
| 340 | c = ulong 0 |
| 341 | for i ulong n |
| 342 | if eq g32 b add add PO 4 mul i 4 52928 |
| 343 | c = 52928 |
| 344 | end |
| 345 | end |
| 346 | put s dv k 6 c |
| 347 | put s 23 add k 1 |
| 348 | devnext s b |
| 349 | end |
| 350 | |
| 351 | fn devstep s edit ulong SN b edit uchar BN pc ulong to void |
| 352 | k = get s 23 |
| 353 | if eq pc 6 |
| 354 | e = operr s |
| 355 | if ne e 0 |
| 356 | fail s b e |
| 357 | ret |
| 358 | end |
| 359 | put s dv k 0 add get s 14 1 |
| 360 | bzero b 0 4 |
| 361 | p32 b 0 cfd s |
| 362 | act s 1 get s 14 201 4 0 |
| 363 | put s 22 7 |
| 364 | ret |
| 365 | end |
| 366 | e = ulong 0 |
| 367 | if eq pc 7 |
| 368 | e = ioerr s |
| 369 | else |
| 370 | e = chk s b 28 |
| 371 | end |
| 372 | if ne e 0 |
| 373 | fail s b e |
| 374 | ret |
| 375 | end |
| 376 | if eq pc 5 |
| 377 | put s dv k 3 g32 b add PO 8 |
| 378 | n = path b get s dv k 1 0 |
| 379 | act s 2 0 0 0 0 |
| 380 | put s 13 n |
| 381 | put s 22 6 |
| 382 | ret |
| 383 | end |
| 384 | if eq pc 7 |
| 385 | bzero b PO 56 |
| 386 | p32 b PO get s dv k 3 |
| 387 | p32 b add PO 4 get s 19 |
| 388 | alc s b get s 19 128 56 |
| 389 | put s 22 8 |
| 390 | ret |
| 391 | end |
| 392 | if eq pc 8 |
| 393 | put s dv k 4 g32 b 8 |
| 394 | bzero b PO 4 |
| 395 | alc s b get s dv k 4 8320 4 |
| 396 | put s 22 9 |
| 397 | ret |
| 398 | end |
| 399 | if eq pc 9 |
| 400 | put s dv k 5 g32 b 8 |
| 401 | bzero b PO 804 |
| 402 | ctl s b get s dv k 4 8389266 804 |
| 403 | put s 22 10 |
| 404 | ret |
| 405 | end |
| 406 | clist s b k |
| 407 | end |
| 408 | |
| 409 | note nvmlInit_v2: a live library only counts the reference. Otherwise open /dev/nvidiactl, allocate |
| 410 | note the root client, read the card table, then per GPU: id info, open and register its device |
| 411 | note node, allocate NV01_DEVICE_0 (128) and NV20_SUBDEVICE_0 (8320), read the class list. |
| 412 | note Any failure tears everything down and returns its code with the refcount still 0. |
| 413 | fn init s edit ulong SN b edit uchar BN to void |
| 414 | pc = get s 22 |
| 415 | if ge pc 100 |
| 416 | tdres s b |
| 417 | ret |
| 418 | end |
| 419 | if eq pc 0 |
| 420 | if gt get s 16 0 |
| 421 | put s 16 add get s 16 1 |
| 422 | done s 0 |
| 423 | ret |
| 424 | end |
| 425 | for i ulong 96 |
| 426 | put s add 16 i 0 |
| 427 | end |
| 428 | put s 20 3472883713 |
| 429 | n = path b 0 1 |
| 430 | act s 2 0 0 0 0 |
| 431 | put s 13 n |
| 432 | put s 22 1 |
| 433 | ret |
| 434 | end |
| 435 | if eq pc 1 |
| 436 | e = operr s |
| 437 | if ne e 0 |
| 438 | done s e |
| 439 | ret |
| 440 | end |
| 441 | put s 18 add get s 14 1 |
| 442 | bzero b PO 4 |
| 443 | alc s b 0 65 0 |
| 444 | put s 22 2 |
| 445 | ret |
| 446 | end |
| 447 | if eq pc 2 |
| 448 | e = chk s b 28 |
| 449 | if ne e 0 |
| 450 | fail s b e |
| 451 | ret |
| 452 | end |
| 453 | x = g32 b 8 |
| 454 | if eq x 0 |
| 455 | fail s b 999 |
| 456 | ret |
| 457 | end |
| 458 | put s 19 x |
| 459 | bzero b 0 4608 |
| 460 | act s 1 cfd s 200 4608 0 |
| 461 | put s 22 3 |
| 462 | ret |
| 463 | end |
| 464 | if eq pc 3 |
| 465 | cards s b |
| 466 | ret |
| 467 | end |
| 468 | devstep s b pc |
| 469 | end |
| 470 | |
| 471 | note nvmlShutdown: UNINITIALIZED (1) without a live library; the last reference tears down and |
| 472 | note reports UNKNOWN if any free or close failed (the library is closed either way). |
| 473 | fn shut s edit ulong SN b edit uchar BN to void |
| 474 | pc = get s 22 |
| 475 | if ge pc 100 |
| 476 | tdres s b |
| 477 | ret |
| 478 | end |
| 479 | if eq get s 16 0 |
| 480 | done s 1 |
| 481 | ret |
| 482 | end |
| 483 | if gt get s 16 1 |
| 484 | put s 16 sub get s 16 1 |
| 485 | done s 0 |
| 486 | ret |
| 487 | end |
| 488 | put s 24 0 |
| 489 | put s 25 0 |
| 490 | put s 26 0 |
| 491 | tdstep s b |
| 492 | end |