Files
nvdriver/tests/vm/rmapi_user_test.c
Fardid021 0a9a7cbf67 tests/vm: GF100_ZBC_CLEAR (0x9096) constructs since #266
Next to #419's check of a class without a constructor. It shares a CHECK with
0x90e6, so that the recorded reports keep their number of checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 01:47:44 +11:00

696 lines
44 KiB
C

// SPDX-License-Identifier: MIT
/* Guest userspace test of the Rust RM API nodes: the client-level slice and the device,
* subdevice, BinaryApi and confidential-compute classes driven end to end through the
* character devices, with the QEMU EDU function as the one bound GPU (the test module
* gives it the PMC_BOOT_0 of a GV100). Expected values come from the original driver's
* behaviour as recorded by tools/check_rmapi_binary.py (status codes, handle values,
* reply layouts). */
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <poll.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
#define NODE "/dev/nvidia-rs-rmapi-ctl"
#define GPU_NODE "/dev/nvidia-rs-rmapi-gpu0"
#define ESC(nr, size) _IOC(_IOC_READ | _IOC_WRITE, 'F', (nr), (size))
static unsigned checks;
#define CHECK(x) do { if (!(x)) { printf("NV_RS_VM_RMAPI_FAIL line %d errno %d\n", __LINE__, errno); return 1; } checks++; } while (0)
struct nvos64 { uint32_t root, parent, object, klass; uint64_t params, rights; uint32_t size, flags, status, pad; };
struct nvos21 { uint32_t root, parent, object, klass; uint64_t params; uint32_t size, status; };
struct nvos54 { uint32_t client, object, cmd, flags; uint64_t params; uint32_t size, status; };
struct nvos00 { uint32_t root, parent, object, status; };
struct card { uint8_t valid, pad[3]; uint32_t domain; uint8_t bus, slot, function, pad2; uint16_t vendor, device; uint32_t gpu_id; uint16_t irq, pad3; uint64_t reg, reg_size, fb, fb_size; uint32_t minor; uint8_t name[10], pad4[2]; };
struct version { uint32_t cmd, reply; char text[64]; };
struct xfer { uint32_t cmd, size; uint64_t pointer; };
/* NV_MEMORY_ALLOCATION_PARAMS, NVOS33 with the descriptor, NVOS34. */
struct memory { uint32_t owner, type, flags, width, height, pitch, attr, attr2, format, compr, zcull, pad; uint64_t range_lo, range_hi, size, alignment, offset, limit, address; uint32_t ctag, space, internal, tag, node, pad2; };
struct nvos33 { uint32_t client, device, memory, pad; uint64_t offset, length, address; uint32_t status, flags; int32_t fd; uint32_t pad2; };
struct nvos34 { uint32_t client, device, memory, pad; uint64_t address; uint32_t status, flags; };
static int control(int fd, uint32_t client, uint32_t object, uint32_t cmd, void *params, uint32_t size, uint32_t *status)
{
struct nvos54 c = { client, object, cmd, 0, (uint64_t)(uintptr_t)params, size, 0xdddddddd };
int r = ioctl(fd, ESC(0x2a, sizeof c), &c);
*status = c.status;
return r;
}
static uint32_t make(int fd, uint32_t client, uint32_t parent, uint32_t handle, uint32_t klass, void *params)
{
struct nvos64 a = { client, parent, handle, klass, (uint64_t)(uintptr_t)params, 0, 0, 0, 0xdddddddd, 0 };
if (ioctl(fd, ESC(0x2b, sizeof a), &a) != 0)
return 0xffffffff;
return a.status;
}
/* NV_ESC_RM_MAP_MEMORY of [offset, offset + length) with the mapping context on `target`. */
static uint32_t map(int fd, uint32_t client, uint32_t device, uint32_t memory, uint64_t offset, uint64_t length, int target, struct nvos33 *m)
{
memset(m, 0, sizeof *m);
m->client = client; m->device = device; m->memory = memory; m->offset = offset; m->length = length;
m->status = 0xdddddddd; m->fd = target;
if (ioctl(fd, ESC(0x4e, sizeof *m), m) != 0)
return 0xffffffff;
return m->status;
}
static uint32_t unmap(int fd, uint32_t client, uint32_t device, uint32_t memory, uint64_t address)
{
struct nvos34 u = { client, device, memory, 0, address, 0xdddddddd, 0 };
if (ioctl(fd, ESC(0x4f, sizeof u), &u) != 0)
return 0xffffffff;
return u.status;
}
/* The workload's system memory request (flags 0xc001, attr 0x3a000000). */
static uint32_t sysmem(int fd, uint32_t client, uint32_t device, uint32_t handle, uint32_t owner, uint32_t type, uint64_t size, struct memory *m)
{
memset(m, 0, sizeof *m);
m->owner = owner; m->type = type; m->flags = 0xc001; m->attr = 0x3a000000; m->size = size;
return make(fd, client, device, handle, 0x3e, m);
}
static uint8_t pattern(unsigned page) { return (uint8_t)(page * 7 + 1); }
static int root(int fd, uint32_t *handle, uint32_t *status)
{
struct nvos64 a = { 0 };
int r;
a.status = 0xdddddddd;
r = ioctl(fd, ESC(0x2b, sizeof a), &a);
*handle = a.object; *status = a.status;
return r == 0 && a.klass == 0x41 && a.root == 0 ? 0 : -1;
}
int main(void)
{
static uint8_t block[16384];
static uint32_t list[201];
struct card cards[32];
struct version v;
struct nvos64 a;
struct nvos21 legacy;
struct nvos54 c;
struct nvos00 f;
struct xfer x;
uint32_t client, other, status, words[96], gpu;
uint64_t memblock;
char strings[3][64];
struct { uint32_t size, pad; uint64_t pointers[3]; uint32_t changelist[2]; } build;
void *unmapped;
struct { uint32_t device, share, target_client, target_device, flags, pad; uint64_t size, start, limit; uint32_t mode, pad2; } dev;
struct pollfd wait;
const uint32_t hdev = 0x5c000001, hsub = 0x5c000002, hbin = 0x5c000003, hcc = 0x5c000004;
const uint32_t hnote = 0x5c000020, hbig = 0x5c000021, hvirt = 0x5c000022;
struct memory mem;
struct nvos33 m, m2;
volatile uint8_t *big, *part, *note, *last;
int fd, fd2, gfd, gfd2, mfd, mfd2, mfd3, i, child;
pid_t pid;
CHECK(sizeof(struct nvos64) == 48 && sizeof(struct nvos21) == 32 && sizeof(struct nvos54) == 32);
CHECK(sizeof(struct card) == 72 && sizeof(struct version) == 72 && sizeof build == 40);
CHECK(sizeof(struct memory) == 128 && sizeof(struct nvos33) == 56 && sizeof(struct nvos34) == 32);
fd = open(NODE, O_RDWR | O_CLOEXEC);
CHECK(fd >= 0);
/* NV_ESC_CHECK_VERSION_STR. */
memset(&v, 0, sizeof v); strcpy(v.text, "580.178.04");
CHECK(ioctl(fd, ESC(0xd2, sizeof v), &v) == 0 && v.reply == 1);
memset(&v, 0, sizeof v); strcpy(v.text, "570.00");
CHECK(ioctl(fd, ESC(0xd2, sizeof v), &v) == -1 && errno == EINVAL);
CHECK(!strcmp(v.text, "580.178.04"));
/* NV_ESC_SYS_PARAMS: the first value is kept. */
memblock = 0x80000000ull;
CHECK(ioctl(fd, ESC(0xd6, 8), &memblock) == 0 && memblock == 0x80000000ull);
CHECK(ioctl(fd, ESC(0xd6, 8), &memblock) == 0);
memblock = 0x40000000ull;
CHECK(ioctl(fd, ESC(0xd6, 8), &memblock) == -1 && errno == EBUSY);
CHECK(ioctl(fd, ESC(0xd6, 4), &memblock) == -1 && errno == EINVAL);
/* NV_ESC_CARD_INFO: exactly the EDU function the test module registered. */
memset(cards, 0xa5, sizeof cards);
CHECK(ioctl(fd, ESC(0xc8, sizeof cards), cards) == 0);
CHECK(cards[0].valid == 1 && cards[1].valid == 0);
CHECK(cards[0].vendor == 0x1234 && cards[0].device == 0x11e8);
CHECK(cards[0].domain == 0 && cards[0].bus == 0 && cards[0].slot == 1 && cards[0].function == 0);
gpu = cards[0].gpu_id;
CHECK(gpu == 0x0001 && cards[0].minor == 0);
CHECK(cards[0].reg != 0 && cards[0].reg_size == 0x100000 && cards[0].fb == 0 && cards[0].fb_size == 0);
for (i = 0; i < 10; i++) CHECK(cards[0].name[i] == 0);
CHECK(ioctl(fd, ESC(0xc8, 71), cards) == -1 && errno == EINVAL);
/* Client: generated handle, class rewritten to NV01_ROOT_CLIENT. */
CHECK(root(fd, &client, &status) == 0 && status == 0 && client == 0xc1d00000);
memset(&legacy, 0, sizeof legacy); legacy.status = 0xdddddddd;
CHECK(ioctl(fd, ESC(0x2b, sizeof legacy), &legacy) == 0 && legacy.status == 0 && legacy.object == 0xc1d00001 && legacy.klass == 0x41);
memset(&a, 0, sizeof a); a.root = 0x1234; words[0] = 0; a.params = (uintptr_t)words;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0 && a.object == 0x1234 && words[0] == 0x1234);
a.object = 0x999;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0x19 && a.object == 0x999);
CHECK(ioctl(fd, ESC(0x2b, 0x28), &a) == -1 && errno == EINVAL);
/* 0x101 build version (deprecated converter): three user buffers. */
memset(strings, 0xa5, sizeof strings); memset(&build, 0, sizeof build);
build.size = 0x50;
for (i = 0; i < 3; i++) build.pointers[i] = (uintptr_t)strings[i];
CHECK(control(fd, client, client, 0x101, &build, sizeof build, &status) == 0 && status == 0);
CHECK(!strcmp(strings[0], "580.178.04") && !strcmp(strings[1], "rel/gpu_drv/r580/r582_73-627"));
CHECK(!strcmp(strings[2], "Private r582_73 rel/gpu_drv/r580/r582_73-627 unknown"));
CHECK((uint8_t)strings[0][11] == 0xa5 && build.changelist[0] == 0x024ae753 && build.changelist[1] == 0x024ae753);
build.size = 52; build.changelist[0] = 0;
CHECK(control(fd, client, client, 0x101, &build, sizeof build, &status) == 0 && status == 0x3a && build.changelist[0] == 0);
build.pointers[1] = 0;
CHECK(control(fd, client, client, 0x101, &build, sizeof build, &status) == 0 && status == 0 && build.size == 53);
unmapped = mmap(NULL, 4096, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
CHECK(unmapped != MAP_FAILED);
build.size = 0x50; build.pointers[1] = (uintptr_t)unmapped;
CHECK(control(fd, client, client, 0x101, &build, sizeof build, &status) == 0 && status == 0x1e);
/* The two share-policy requests of the workload, then the system queries. */
words[0] = client; words[1] = 1; words[2] = 4;
CHECK(control(fd, client, client, 0xd04, words, 12, &status) == 0 && status == 0);
words[1] = 4; words[2] = 0x00040004;
CHECK(control(fd, client, client, 0xd04, words, 12, &status) == 0 && status == 0);
words[2] = 8;
CHECK(control(fd, client, client, 0xd04, words, 12, &status) == 0 && status == 0x1f);
words[0] = 0xa5a5a5a5;
CHECK(control(fd, client, client, 0x1f0, words, 4, &status) == 0 && status == 0 && words[0] == 1);
memset(words, 0xa5, sizeof words);
CHECK(control(fd, client, client, 0x214, words, 384, &status) == 0 && status == 0);
CHECK(words[0] == gpu && words[1] == 0xffffffff && words[31] == 0xffffffff);
CHECK(words[32] == 0xffffffff && words[63] == 0xffffffff && words[64] == 0 && words[65] == 0xa5a5a5a5);
memset(block, 0xa5, sizeof block);
CHECK(control(fd, client, client, 0xa04, block, 2244, &status) == 0 && status == 0);
CHECK(!memcmp(block, "\0\0\0\0\xa5", 5));
memset(words, 0xff, sizeof words); words[0] = gpu; words[32] = 0;
CHECK(control(fd, client, client, 0x215, words, 132, &status) == 0 && status == 0);
words[1] = 0x7777;
CHECK(control(fd, client, client, 0x215, words, 132, &status) == 0 && status == 0x1f);
words[0] = 0xffff;
CHECK(control(fd, client, client, 0x215, words, 132, &status) == 0 && status == 0);
memset(block, 0xa5, sizeof block);
CHECK(control(fd, client, client, 0x288, block, 3076, &status) == 0 && status == 0);
memcpy(words, block, 16);
CHECK(words[0] == 1 && words[1] == gpu && words[2] == 0xffffffff && words[3] == 0xffffffff && block[16] == 0xa5);
/* The GPU is probed but its adapter is not initialized: it is not attached. */
memset(words, 0xa5, sizeof words);
CHECK(control(fd, client, client, 0x201, words, 128, &status) == 0 && status == 0);
for (i = 0; i < 32; i++) CHECK(words[i] == 0xffffffff);
words[0] = gpu; words[1] = 0xa5a5a5a5;
CHECK(control(fd, client, client, 0x205, words, 32, &status) == 0 && status == 0x1f && words[1] == 0xa5a5a5a5);
CHECK(control(fd, client, client, 0x202, words, 40, &status) == 0 && status == 0x1f);
memset(block, 0xa5, sizeof block);
CHECK(control(fd, client, client, 0x13e, block, 1032, &status) == 0 && status == 0);
CHECK(!strcmp((char *)block, "580.178.04") && !strcmp((char *)block + 512, "r582_73") && block[255] == 0);
/* Entry checks of the control path, in the original's order. */
CHECK(control(fd, 0xbad, 0xbad, 0x20800000, NULL, 0, &status) == 0 && status == 0);
CHECK(control(fd, 0xbad, client, 0x214, words, 384, &status) == 0 && status == 0x23);
CHECK(control(fd, client, 0xbad, 0x214, words, 384, &status) == 0 && status == 0x57);
CHECK(control(fd, client, 0xbad, 0x214, words, 380, &status) == 0 && status == 0x1f);
CHECK(control(fd, client, client, 0x214, NULL, 384, &status) == 0 && status == 0x1f);
CHECK(control(fd, client, client, 0x2ff, words, 4, &status) == 0 && status == 0x56);
CHECK(control(fd, client, client, 0x20800102, block, 564, &status) == 0 && status == 0x56);
/* 0xd01 names an object of the client: none yet, and never the client itself. */
words[0] = 0xbad; words[1] = 0;
CHECK(control(fd, client, client, 0xd01, words, 12, &status) == 0 && status == 0x57);
words[0] = client;
CHECK(control(fd, client, client, 0xd01, words, 12, &status) == 0 && status == 0x31);
words[0] = 0xa5a5a5a5;
CHECK(control(fd, client, client, 0x27b, words, 4, &status) == 0 && status == 0 && words[0] == 0);
CHECK(control(fd, client, client, 0x136, words, 4, &status) == 0 && status == 0 && words[0] == 1);
CHECK(control(fd, client, client, 0x214, unmapped, 384, &status) == 0 && status == 0x1e);
memset(&c, 0, sizeof c); c.client = c.object = client; c.cmd = 0x214; c.flags = 1; c.params = (uintptr_t)words; c.size = 384;
CHECK(ioctl(fd, ESC(0x2a, sizeof c), &c) == 0 && c.status == 0x1f);
c.flags = 4;
CHECK(ioctl(fd, ESC(0x2a, sizeof c), &c) == 0 && c.status == 0x6a);
CHECK(ioctl(fd, ESC(0x2a, 0x1c), &c) == -1 && errno == EINVAL);
/* The XFER envelope carries the same request. */
c.flags = 0; c.status = 0xdddddddd; memset(words, 0xa5, sizeof words);
x.cmd = 0x2a; x.size = sizeof c; x.pointer = (uintptr_t)&c;
CHECK(ioctl(fd, ESC(0xd3, sizeof x), &x) == 0 && c.status == 0 && words[0] == gpu);
/* Objects. The GPU's device node is not open, so the GPU is not attached and a device
* cannot be made on it; the handle is assigned first. */
memset(&a, 0, sizeof a); a.root = a.parent = client; a.klass = 0x80; memset(block, 0, 56); a.params = (uintptr_t)block;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0x40 && a.object == 0xcaf00000);
a.object = 0; a.parent = 0xdead;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0x57 && a.object == 0);
a.parent = client; a.klass = 0x2080; a.params = 0;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0x36);
a.klass = 0x12345;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0x22);
a.klass = 0xc36f;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0x1f);
a.klass = 0x80; a.params = (uintptr_t)unmapped;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0x1e);
/* Device node: opening it attaches the GPU (without engines). */
CHECK(sizeof dev == 56);
gfd = open(GPU_NODE, O_RDWR | O_CLOEXEC);
CHECK(gfd >= 0);
memset(words, 0xa5, sizeof words);
CHECK(control(fd, client, client, 0x201, words, 128, &status) == 0 && status == 0);
CHECK(words[0] == gpu && words[1] == 0xffffffff);
memset(words, 0xa5, sizeof words); words[0] = gpu;
CHECK(control(fd, client, client, 0x205, words, 32, &status) == 0 && status == 0);
CHECK(words[1] == 0 && words[2] == 0 && words[3] == 0 && words[4] == 0 && words[5] == gpu && words[6] == 0 && words[7] == 0xffffffff);
memset(words, 0xff, sizeof words); words[0] = gpu;
CHECK(control(fd, client, client, 0x216, words, 128, &status) == 0 && status == 0);
words[0] = 0x7777;
CHECK(control(fd, client, client, 0x216, words, 128, &status) == 0 && status == 0x1f);
/* Escapes on the device node: object escapes belong to the control node. */
CHECK(ioctl(gfd, ESC(0x2b, sizeof a), &a) == -1 && errno == EINVAL);
CHECK(ioctl(gfd, ESC(0x2a, sizeof c), &c) == -1 && errno == EINVAL);
CHECK(ioctl(gfd, ESC(0xc8, sizeof cards), cards) == -1 && errno == EINVAL);
/* NV_ESC_REGISTER_FD: one control file per file, never itself, never a device file. */
i = fd;
CHECK(ioctl(gfd, ESC(0xc9, 4), &i) == 0 && i == fd);
CHECK(ioctl(gfd, ESC(0xc9, 4), &i) == -1 && errno == EINVAL);
gfd2 = open(GPU_NODE, O_RDWR | O_CLOEXEC);
CHECK(gfd2 >= 0);
i = gfd;
CHECK(ioctl(gfd2, ESC(0xc9, 4), &i) == -1 && errno == EINVAL);
i = 9999;
CHECK(ioctl(gfd2, ESC(0xc9, 4), &i) == -1 && errno == EINVAL);
i = fd;
CHECK(ioctl(gfd2, ESC(0xc9, 8), &memblock) == -1 && errno == EINVAL);
CHECK(ioctl(gfd2, ESC(0xc9, 4), &i) == 0);
CHECK(ioctl(fd, ESC(0xc9, 4), &i) == -1 && errno == EINVAL);
/* mmap and poll exist; nothing can be mapped and no event can be pending yet. */
CHECK(mmap(NULL, 4096, PROT_READ, MAP_SHARED, gfd, 0) == MAP_FAILED && errno == EINVAL);
CHECK(mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0) == MAP_FAILED && errno == EINVAL);
wait.fd = gfd; wait.events = POLLIN | POLLPRI; wait.revents = 0;
CHECK(poll(&wait, 1, 0) == 0 && wait.revents == 0);
/* Device: the workload's parameters; rules of the original's constructor. */
memset(&dev, 0, sizeof dev); dev.share = client; dev.mode = 2;
dev.device = 1;
CHECK(make(fd, client, client, hdev, 0x80, &dev) == 0x22);
dev.device = 32;
CHECK(make(fd, client, client, hdev, 0x80, &dev) == 0x1f);
dev.device = 0; dev.share = 0x12345;
CHECK(make(fd, client, client, hdev, 0x80, &dev) == 0x33);
dev.share = client; dev.flags = 8;
CHECK(make(fd, client, client, hdev, 0x80, &dev) == 0x1f);
dev.flags = 0;
CHECK(make(fd, client, client, hdev, 0x80, NULL) == 0x1f);
/* A process that does not hold the device node open is refused. */
pid = fork();
CHECK(pid >= 0);
if (pid == 0) {
close(gfd); close(gfd2);
_exit(make(fd, client, client, hdev, 0x80, &dev) == 0x1b ? 0 : 1);
}
CHECK(waitpid(pid, &child, 0) == pid && WIFEXITED(child) && WEXITSTATUS(child) == 0);
CHECK(make(fd, client, client, hdev, 0x80, &dev) == 0);
CHECK(make(fd, client, client, hdev, 0x80, &dev) == 0x19);
words[0] = 8;
CHECK(make(fd, client, hdev, hsub, 0x2080, words) == 0x22);
words[0] = 0;
CHECK(make(fd, client, hdev, hsub, 0x2080, words) == 0);
CHECK(make(fd, client, hdev, 0, 0x2080, words) == 0x1a);
CHECK(make(fd, client, hdev, hbin, 0x2081, NULL) == 0x36);
CHECK(make(fd, client, hsub, hbin, 0x2081, NULL) == 0);
CHECK(make(fd, client, client, hcc, 0xcb33, NULL) == 0);
/* A class without a constructor: GK110_SUBDEVICE_GRAPHICS (0xa0e0) is Kepler-only, so
* the original refuses it here too (unsupported class -> NOT_SUPPORTED, 0x56). */
CHECK(make(fd, client, hsub, 0, 0xa0e0, NULL) == 0x56);
CHECK(make(fd, client, hsub, 0x5c000027, 0x90e7, NULL) == 0);
CHECK(make(fd, client, hsub, 0x5c000028, 0xc361, NULL) == 0);
CHECK(make(fd, client, hsub, 0x5c000029, 0x90e6, NULL) == 0 &&
make(fd, client, hsub, 0x5c00002a, 0x9096, NULL) == 0);
memset(words, 0xa5, 16);
CHECK(control(fd, client, 0x5c000029, 0x90e60101, words, 16, &status) == 0 && status == 0);
CHECK(words[0] == 0x100 && words[1] == 0x02003000 && words[2] == 0x02003000 && words[3] == 0x00400000);
words[0] = 0x5c000028; words[1] = 0; words[2] = 0xa5a5a5a5;
CHECK(control(fd, client, client, 0xd01, words, 12, &status) == 0 && status == 0 && words[2] == 3);
/* Slice 6, channel classes. This node has no engine side, so after their parameter
* checks they are unavailable (0x56) and nothing is constructed. */
{
uint32_t tsg[5] = { 0, 0, 0, 1, 0 }, share[3] = { 0, 1, 0 }, engine[4] = { 0, 0, 0, 0 }, first;
static uint32_t chan[92];
struct { uint32_t client, device, fd, status; } ev = { client, hsub, (uint32_t)gfd, 0xdddddddd };
struct { uint32_t parent, source, klass, index; uint64_t data; } evp;
struct { uint32_t count, pad; uint64_t handles, ids; } listing = { 0, 0, (uintptr_t)words, (uintptr_t)words };
const uint32_t htsg = 0x5c000030, hev = 0x5c000040;
CHECK(sizeof chan == 368 && sizeof evp == 24 && sizeof ev == 16 && sizeof listing == 24);
CHECK(make(fd, client, hdev, htsg, 0xa06c, tsg) == 0x56);
tsg[3] = 2;
CHECK(make(fd, client, hdev, htsg, 0xa06c, tsg) == 0x1f);
/* A handle of another class as the address space: 0x33 in the original's record
* (target/rmapi/original-rmapi-channel.txt, case chan-group). */
tsg[3] = 1; tsg[2] = hsub;
CHECK(make(fd, client, hdev, htsg, 0xa06c, tsg) == 0x33);
tsg[2] = 0;
CHECK(make(fd, client, hsub, htsg, 0xa06c, tsg) == 0x36);
CHECK(make(fd, client, hdev, htsg, 0xa06c, NULL) == 0x1f);
CHECK(make(fd, client, hdev, htsg + 1, 0x9067, share) == 0x36);
/* Failed allocations do not copy out parameters (cid at +0x84 stays 0). */
CHECK(make(fd, client, hdev, htsg + 2, 0xc36f, chan) == 0x56 && chan[33] == 0);
CHECK(make(fd, client, hdev, htsg + 2, 0xc36f, chan) == 0x56 && chan[33] == 0);
chan[5] = 1;
CHECK(make(fd, client, hdev, htsg + 2, 0xc36f, chan) == 0x56 && chan[33] == 0);
chan[5] = 0; chan[6] = 1; chan[7] = 1;
CHECK(make(fd, client, hdev, htsg + 2, 0xc36f, chan) == 0x1f && chan[33] == 0);
(void)first;
CHECK(make(fd, client, hdev, htsg + 3, 0xc3c0, engine) == 0x36);
CHECK(make(fd, client, hdev, htsg + 3, 0xc3b5, engine) == 0x36);
/* Controls that read channel state: no channel can exist. */
CHECK(control(fd, client, hdev, 0x80170d, &listing, 24, &status) == 0 && status == 0x1f);
words[0] = 0x1234;
listing.count = 1; listing.handles = (uintptr_t)words;
CHECK(control(fd, client, hdev, 0x80170d, &listing, 24, &status) == 0 && status == 0 && words[0] == 0xffffffff);
words[0] = 0x1234; words[1] = 0; words[2] = 0; words[3] = 0;
CHECK(control(fd, client, hsub, 0x20801218, words, 16, &status) == 0 && status == 0x57);
CHECK(control(fd, client, hdev, 0xa06c0101, words, 3, &status) == 0 && status == 0x56);
/* OS events and event objects through the device file that registered `fd`. */
CHECK(ioctl(gfd, ESC(0xce, sizeof ev), &ev) == 0 && ev.status == 0);
ev.status = 0xdddddddd;
CHECK(ioctl(gfd, ESC(0xce, sizeof ev), &ev) == 0 && ev.status == 0x1a);
CHECK(ioctl(gfd, ESC(0xce, 12), &ev) == -1 && errno == EINVAL);
evp.parent = client; evp.source = 0; evp.klass = 0x79; evp.index = 0x3800000c; evp.data = (uint64_t)gfd;
CHECK(make(gfd, client, hsub, hev, 0x79, &evp) == 0 && evp.source == hsub);
CHECK(evp.data != 0 && evp.data != (uint64_t)gfd && evp.index == 0x3800000c);
evp.data = 9999; evp.source = 0;
CHECK(make(gfd, client, hsub, hev + 1, 0x79, &evp) == 0x57 && evp.source == hsub && evp.data == 9999);
evp.data = (uint64_t)gfd; evp.index = 0x38000001;
CHECK(make(gfd, client, hsub, hev + 1, 0x79, &evp) == 0x56);
evp.data = (uint64_t)gfd; evp.index = 0x3000000c;
CHECK(make(gfd, client, hdev, hev + 1, 0x79, &evp) == 0x31 && evp.source == hdev);
/* Class 0x05 names the class in its parameters; the control node takes it too. */
evp.data = (uint64_t)gfd; evp.index = 0x38000019;
CHECK(make(fd, client, hsub, hev + 1, 0x05, &evp) == 0 && evp.data != (uint64_t)gfd);
wait.fd = gfd; wait.events = POLLIN | POLLPRI; wait.revents = 0;
CHECK(poll(&wait, 1, 0) == 0 && wait.revents == 0);
ev.status = 0xdddddddd;
CHECK(ioctl(gfd, ESC(0xcf, sizeof ev), &ev) == 0 && ev.status == 0);
ev.status = 0xdddddddd;
CHECK(ioctl(gfd, ESC(0xcf, sizeof ev), &ev) == 0 && ev.status == 0x28);
evp.data = (uint64_t)gfd; evp.index = 0x3800000c;
CHECK(make(gfd, client, hsub, hev + 2, 0x79, &evp) == 0x57);
f.root = client; f.parent = 0; f.object = hev; f.status = 0xdddddddd;
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0);
/* The other event and a second OS event record are left for the close paths. */
ev.fd = 77; ev.status = 0xdddddddd;
CHECK(ioctl(gfd, ESC(0xce, sizeof ev), &ev) == 0 && ev.status == 0);
}
/* Information controls that need no engine state. */
memset(block, 0xa5, 8);
CHECK(control(fd, client, hcc, 0xcb330101, block, 6, &status) == 0 && status == 0 && !memcmp(block, "\0\0\0\0\0\0\xa5", 7));
words[0] = 0xa5a5a5a5;
CHECK(control(fd, client, hdev, 0x800280, words, 4, &status) == 0 && status == 0 && words[0] == 1);
memset(words, 0xa5, 8);
CHECK(control(fd, client, hdev, 0x800289, words, 8, &status) == 0 && status == 0 && words[0] == 0 && words[1] == 0xa5a5a500);
memset(words, 0xa5, 16);
CHECK(control(fd, client, hsub, 0x20801701, words, 16, &status) == 0 && status == 0);
CHECK(words[0] == 0x140 && words[1] == 0 && words[2] == 0xa1 && words[3] == 0);
memset(words, 0xa5, 16);
CHECK(control(fd, client, hsub, 0x20801801, words, 16, &status) == 0 && status == 0);
CHECK(words[0] == 0x11e81234 && words[3] == 0x11e8 && words[2] < 0x100);
words[0] = 0xa5a5a5a5;
CHECK(control(fd, client, hsub, 0x20800119, words, 4, &status) == 0 && status == 0 && words[0] == 0);
words[0] = 0xa5a5a5a5;
CHECK(control(fd, client, hsub, 0x20800131, words, 4, &status) == 0 && status == 0 && words[0] == 0);
CHECK(control(fd, client, hsub, 0x20800146, NULL, 0, &status) == 0 && status == 0x63);
CHECK(control(fd, client, hsub, 0x20800145, NULL, 0, &status) == 0 && status == 0);
CHECK(control(fd, client, hsub, 0x20800146, NULL, 0, &status) == 0 && status == 0);
memset(words, 0xa5, 332); words[0] = 2; words[1] = 0; words[3] = 0x28;
CHECK(control(fd, client, hsub, 0x20808159, words, 332, &status) == 0 && status == 0);
CHECK(words[2] == 0 && words[4] == 0 && words[5] == 0xa5a5a5a5);
words[3] = 0x29; words[4] = 0xa5a5a5a5;
CHECK(control(fd, client, hsub, 0x20808159, words, 332, &status) == 0 && status == 0x1f && words[4] == 0xa5a5a5a5);
block[0] = 0xa5;
CHECK(control(fd, client, hsub, 0x20808162, block, 1, &status) == 0 && status == 0 && block[0] == 0);
memset(block, 0xa5, 72);
CHECK(control(fd, client, hsub, 0x20803601, block, 72, &status) == 0 && status == 0);
for (i = 0; i < 72; i++) CHECK(block[i] == 0);
memset(block, 0xa5, 28);
CHECK(control(fd, client, hsub, 0x2080182b, block, 28, &status) == 0 && status == 0);
for (i = 0; i < 28; i++) CHECK(block[i] == 0);
memset(block, 0xa5, 992);
CHECK(control(fd, client, hbin, 0x20810108, block, 992, &status) == 0 && status == 0);
CHECK(block[0] == 0 && block[1] == 0xa5 && block[0x84] == 0 && block[0x87] == 0 && block[0x88] == 0xa5 && block[0x3d8] == 0);
/* Slice 3. The class list after the missing-engine sweep: the fixture's display fuse
* word says "no display", so the nine display classes are gone (99 of 108). */
memset(list, 0xa5, sizeof list);
CHECK(control(fd, client, hdev, 0x800292, list, 804, &status) == 0 && status == 0);
CHECK(list[0] == 99 && list[1] == 0xf && list[99] == 0xc361 && list[100] == 0xa5a5a5a5);
for (i = 1, status = 0; i <= 99; i++) status |= list[i] == 0x73 || (list[i] & 0xfff0) == 0xc370;
CHECK(status == 0);
CHECK(control(fd, client, hsub, 0x800292, list, 804, &status) == 0 && status == 0x56);
/* Host capabilities (no NVIDIA host bridge), PCIe atomics words, RC watchdog requests. */
memset(block, 0xa5, 4);
CHECK(control(fd, client, hdev, 0x801402, block, 3, &status) == 0 && status == 0 && !memcmp(block, "\0\0\0\xa5", 4));
memset(block, 0, 112);
CHECK(control(fd, client, hsub, 0x2080182a, block, 112, &status) == 0 && status == 0);
block[0] = 1;
CHECK(control(fd, client, hsub, 0x2080182a, block, 112, &status) == 0 && status == 0x56);
CHECK(control(fd, client, hsub, 0x20802210, NULL, 0, &status) == 0 && status == 0);
CHECK(control(fd, client, hsub, 0x20802210, NULL, 0, &status) == 0 && status == 0);
CHECK(control(fd, client, hsub, 0x2080220c, NULL, 0, &status) == 0 && status == 0);
CHECK(control(fd, client, hdev, 0x20802210, NULL, 0, &status) == 0 && status == 0x56);
/* Slice 3c. The engine list behind the copy-engine class update: with the fixture's
* PCE-LCE words the ninth copy engine is stubbed (15 engines, COPY8 = 0x11 absent). */
memset(list, 0xa5, sizeof list);
CHECK(control(fd, client, hsub, 0x20800170, list, 340, &status) == 0 && status == 0);
CHECK(list[0] == 15 && list[1] == 1 && list[2] == 9 && list[9] == 0x10 && list[10] == 0x13 && list[15] == 0x26 && list[16] == 0xa5a5a5a5);
CHECK(control(fd, client, hdev, 0x20800170, list, 340, &status) == 0 && status == 0x56);
/* Slice 4. The two address spaces of the workload: externally owned with page faulting
* at 0x1000, and RM-managed at 0x5000000. */
memset(list, 0, 56); list[1] = 0x48; list[10] = 0x1000;
CHECK(make(fd, client, hdev, 0x5c000010, 0x90f1, list) == 0);
CHECK(list[2] == 0xfffff000 && list[3] == 0x1ffff && list[10] == 0x1000 && list[1] == 0x48);
memset(list, 0, 56); list[10] = 0x5000000;
CHECK(make(fd, client, hdev, 0x5c000011, 0x90f1, list) == 0 && list[2] == 0xfb000000 && list[3] == 0x1ffff);
memset(list, 0, 56); list[1] = 0x40;
CHECK(make(fd, client, hdev, 0x5c000013, 0x90f1, list) == 0x1f);
memset(list, 0, 56); list[0] = 3;
CHECK(make(fd, client, hdev, 0x5c000013, 0x90f1, list) == 0x40);
CHECK(make(fd, client, client, 0x5c000013, 0x90f1, list) == 0x36);
memset(list, 0xa5, 192); list[41] = 0x5c000010;
CHECK(control(fd, client, hdev, 0x801806, list, 192, &status) == 0 && status == 0);
CHECK(list[0] == 49 && list[3] == 0x10000 && list[4] == 0x10000 && list[7] == 0x100 && list[11] == 0xa5a5a5a5 && list[42] == 0x1000 && list[43] == 0 && list[46] == 0x211000);
list[41] = 0;
CHECK(control(fd, client, hdev, 0x801806, list, 192, &status) == 0 && status == 0x40);
list[41] = hsub;
CHECK(control(fd, client, hdev, 0x801806, list, 192, &status) == 0 && status == 0x33);
/* ThirdPartyP2P and its VA-space registration; the object is invalidated when a
* registered space goes away. */
words[0] = 1;
CHECK(make(fd, client, hsub, 0x5c000012, 0x503c, words) == 0);
CHECK(make(fd, client, hsub, 0x5c000013, 0x503c, words) == 0x63);
memset(words, 0xa5, 16); words[0] = 0x5c000010;
CHECK(control(fd, client, 0x5c000012, 0x503c0102, words, 16, &status) == 0 && status == 0);
CHECK(words[1] == 0xa5a5a5a5 && words[2] == 0xfe00 && words[3] == 0);
CHECK(control(fd, client, 0x5c000012, 0x503c0102, words, 16, &status) == 0 && status == 0x19);
f.root = client; f.parent = 0; f.object = 0x5c000010; f.status = 0xdddddddd;
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0);
words[0] = 0x5c000011;
CHECK(control(fd, client, 0x5c000012, 0x503c0102, words, 16, &status) == 0 && status == 0x6e);
f.object = 0x5c000012; f.status = 0xdddddddd;
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0);
CHECK(control(fd, client, 0x5c000012, 0x503c0102, words, 16, &status) == 0 && status == 0x57);
/* Control cache: the stored reply of 0x20801701 answers on the device handle, where its
* handler is not exported; an object without a GPU attribute takes the normal path. */
memset(words, 0xa5, 16);
CHECK(control(fd, client, hdev, 0x20801701, words, 16, &status) == 0 && status == 0 && words[0] == 0x140 && words[2] == 0xa1);
memset(words, 0xa5, 16);
CHECK(control(fd, client, hbin, 0x20801701, words, 16, &status) == 0 && status == 0x56 && words[0] == 0xa5a5a5a5);
CHECK(control(fd, client, hsub, 0x20801701, words, 12, &status) == 0 && status == 0x1f);
/* Priority 2B information controls. */
memset(block, 0, 564); block[0] = 1; block[4] = 0x11;
CHECK(control(fd, client, hsub, 0x20800102, block, 564, &status) == 0 && status == 0 && block[8] == 1);
memset(block, 0, 420); block[0] = 1; block[4] = 0;
CHECK(control(fd, client, hsub, 0x20801823, block, 420, &status) == 0 && status == 0 && block[8] == 3);
memset(block, 0, 48);
CHECK(control(fd, client, hsub, 0x20801227, block, 48, &status) == 0 && status == 0 && block[0] == 0xb0 && block[0x28] == 1);
memset(block, 0, 9240);
CHECK(control(fd, client, hsub, 0x2080121b, block, 9240, &status) == 0 && status == 0 && block[0x2400] == 80 && block[0x2402] == 40);
{
uint32_t gr_list[4] = { 0x0c, 0, 0x08, 0 };
struct { uint32_t count, pad; uint64_t ptr; uint32_t pad2[4]; } gri = { 2, 0, (uintptr_t)gr_list, { 0 } };
CHECK(control(fd, client, hsub, 0x20801201, &gri, 32, &status) == 0 && status == 0 && gr_list[1] == 0x700 && gr_list[3] == 80);
}
memset(block, 0xa5, 3);
CHECK(control(fd, client, hdev, 0x801307, block, 3, &status) == 0 && status == 0 && block[0] == 0x0b && block[1] == 0x01 && block[2] == 0x12);
memset(words, 0xa5, 200);
CHECK(control(fd, client, hsub, 0x20801803, words, 200, &status) == 0 && status == 0 && words[0] == 4);
memset(block, 0, 132);
CHECK(control(fd, client, hsub, 0x20800110, block, 132, &status) == 0 && status == 0 && block[4] != 0);
memset(block, 0, 64);
CHECK(control(fd, client, hsub, 0x20800111, block, 64, &status) == 0 && status == 0 && !strcmp((char *)block, "GV100GL-A"));
memset(words, 0, 24);
CHECK(control(fd, client, hsub, 0x2080122a, words, 24, &status) == 0 && status == 0 && words[4] == 0x3f);
memset(words, 0, 24); words[4] = 3;
CHECK(control(fd, client, hsub, 0x2080122b, words, 24, &status) == 0 && status == 0 && words[5] == 0x7e);
memset(block, 0, 1928); block[0] = 1; block[8] = 2; block[16] = 3;
CHECK(control(fd, client, hsub, 0x20803801, block, 1928, &status) == 0 && status == 0 && block[12] == 0 && block[20] == 3);
/* A subdevice command that is not cacheable is not exported by the device. */
CHECK(control(fd, client, hdev, 0x20800131, words, 4, &status) == 0 && status == 0x56);
/* Slice 5. Memory objects. System memory (0x3e) with the workload's parameters: a
* 4 KiB notifier (type 13, which also gets a kernel mapping) and 2 MiB. */
CHECK(sysmem(fd, client, hdev, hnote, hdev, 13, 0x1000, &mem) == 0);
CHECK(mem.attr == 0x2a800000 && mem.attr2 == 9 && mem.alignment == 0x1000 && mem.size == 0x1000 && mem.offset == 0 && mem.limit == 0xfff);
CHECK(sysmem(fd, client, hdev, hbig, client, 0, 0x200000, &mem) == 0);
CHECK(mem.attr == 0x2a800000 && mem.attr2 == 9 && mem.alignment == 0x1000 && mem.size == 0x200000 && mem.offset == 0 && mem.limit == 0x1fffff);
words[0] = hbig; words[1] = 0; words[2] = 0xa5a5a5a5;
CHECK(control(fd, client, client, 0xd01, words, 12, &status) == 0 && status == 0 && words[2] == 1);
/* The node's page budget is 256 MiB: three more objects of 64 MiB fit, a fourth does not. */
for (i = 0; i < 3; i++)
CHECK(sysmem(fd, client, hdev, 0x5c000030 + i, client, 0, 0x4000000, &mem) == 0 && mem.limit == 0x3ffffff);
CHECK(sysmem(fd, client, hdev, 0x5c000033, client, 0, 0x4000000, &mem) == 0x51);
for (i = 0; i < 3; i++) {
f.root = client; f.parent = 0; f.object = 0x5c000030 + i; f.status = 0xdddddddd;
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0);
}
CHECK(sysmem(fd, client, hdev, 0x5c000033, client, 0, 0x4000000, &mem) == 0);
f.object = 0x5c000033; f.status = 0xdddddddd;
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0);
/* NV_ESC_RM_MAP_MEMORY: the mapping context goes to a control file of this driver. */
mfd = open(NODE, O_RDWR | O_CLOEXEC);
mfd2 = open(NODE, O_RDWR | O_CLOEXEC);
mfd3 = open(NODE, O_RDWR | O_CLOEXEC);
CHECK(mfd >= 0 && mfd2 >= 0 && mfd3 >= 0);
CHECK(map(fd, client, hdev, hbig, 0, 0x200000, 9999, &m) == 0x1f);
CHECK(map(fd, client, hdev, hbig, 0, 0x200000, gfd, &m) == 0x1f);
CHECK(map(fd, client, hdev, hbig, 0, 0x200000, 0, &m) == 0x1f);
CHECK(map(fd, client, hdev, hbig, 0x200000, 0x1000, mfd, &m) == 0x20);
CHECK(map(fd, client, hdev, hbig, 0x1ff000, 0x2000, mfd, &m) == 0x2e);
/* None of the refused requests left a context behind. */
CHECK(mmap(NULL, 4096, PROT_READ, MAP_SHARED, mfd, 0) == MAP_FAILED && errno == EINVAL);
CHECK(map(fd, client, hdev, hbig, 0, 0x200000, mfd, &m) == 0);
CHECK(m.address != 0 && (m.address & 0xfff) == 0 && m.flags == 0x03008000);
/* One context per file, for the life of the file. */
CHECK(map(fd, client, hdev, hbig, 0, 0x1000, mfd, &m2) == 0x63);
CHECK(map(fd, client, hdev, hnote, 0, 0x1000, mfd, &m2) == 0x63);
/* mmap: offset 0 only, inside the allocation, shared. */
CHECK(mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 4096) == MAP_FAILED && errno == EINVAL);
CHECK(mmap(NULL, 0x201000, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0) == MAP_FAILED && errno == ERANGE);
CHECK(mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, mfd, 0) == MAP_FAILED && errno == EINVAL);
big = mmap(NULL, 0x200000, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
CHECK(big != MAP_FAILED);
for (i = 0, status = 0; i < 0x200000; i += 0x400) status |= big[i];
CHECK(status == 0);
for (i = 0; i < 512; i++) big[i * 4096 + i] = pattern(i);
/* A second mapping of the same object, from its fourth page, with an offset in the page;
* the address comes back with that offset. */
CHECK(map(fd, client, hdev, hbig, 0x3234, 0x2000, mfd2, &m2) == 0);
CHECK((m2.address & 0xfff) == 0x234 && m2.address != m.address);
CHECK(mmap(NULL, 510 * 4096, PROT_READ, MAP_SHARED, mfd2, 0) == MAP_FAILED && errno == ERANGE);
part = mmap(NULL, 0x3000, PROT_READ | PROT_WRITE, MAP_SHARED, mfd2, 0);
CHECK(part != MAP_FAILED);
CHECK(close(mfd2) == 0);
for (i = 0; i < 3; i++) CHECK(part[i * 4096 + 3 + i] == pattern(3 + i));
part[0x234] = 0x77;
CHECK(big[0x3234] == 0x77);
/* The context can be mapped again; a shorter mapping starts at the same page. */
last = mmap(NULL, 4096, PROT_READ, MAP_SHARED, mfd, 0);
CHECK(last != MAP_FAILED && last[0] == pattern(0) && munmap((void *)last, 4096) == 0);
/* The notifier object. */
CHECK(map(fd, client, hsub, hnote, 0, 0x1000, mfd3, &m2) == 0 && (m2.address & 0xfff) == 0);
CHECK(mmap(NULL, 0x2000, PROT_READ | PROT_WRITE, MAP_SHARED, mfd3, 0) == MAP_FAILED && errno == ERANGE);
note = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mfd3, 0);
CHECK(note != MAP_FAILED && note[5] == 0);
CHECK(close(mfd3) == 0);
note[5] = 0x5a;
/* NV_ESC_RM_UNMAP_MEMORY names the mapping by its address; the process's own mapping
* is not touched. */
CHECK(unmap(fd, client, hdev, hbig, m.address + 0x1000) == 0x57);
CHECK(unmap(fd, client, hdev, hbig, m.address) == 0);
CHECK(unmap(fd, client, hdev, hbig, m.address) == 0x57);
CHECK(big[511 * 4096 + 511] == pattern(511));
/* Free with live mappings: the pages stay while they are mapped. */
f.root = client; f.parent = 0; f.object = hbig; f.status = 0xdddddddd;
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0);
CHECK(map(fd, client, hdev, hbig, 0, 0x1000, mfd3, &m2) == 0x57);
CHECK(big[100 * 4096 + 100] == pattern(100) && part[0x234] == 0x77);
big[7] = 0x42;
/* The file still carries its context of the freed object. */
last = mmap(NULL, 4096, PROT_READ, MAP_SHARED, mfd, 0);
CHECK(last != MAP_FAILED && last[7] == 0x42 && munmap((void *)last, 4096) == 0);
CHECK(munmap((void *)big, 0x200000) == 0 && close(mfd) == 0);
CHECK(part[4096 + 4] == pattern(4));
/* Virtual memory (0x50a0) in the RM-managed address space, the workload's request. */
memset(&mem, 0, sizeof mem); mem.owner = client; mem.flags = 0x8c415; mem.size = 0xfb000000; mem.offset = 0x5000000; mem.space = 0x5c000011;
CHECK(make(fd, client, hdev, hvirt, 0x50a0, &mem) == 0);
CHECK(mem.attr == 0x16000000 && mem.attr2 == 0x100005 && mem.range_lo == 0x5000000 && mem.range_hi == 0xffffffff);
CHECK(mem.size == 0xfb000000 && mem.alignment == 0x200000 && mem.offset == 0x5000000 && mem.limit == 0xfaffffff);
memset(&mem, 0, sizeof mem); mem.owner = client; mem.flags = 0x8c415; mem.size = 0xfb000000; mem.offset = 0x5000000; mem.space = 0x5c000011;
CHECK(make(fd, client, hdev, 0x5c000023, 0x50a0, &mem) == 0x1a);
words[0] = hvirt; words[1] = 0; words[2] = 0xa5a5a5a5;
CHECK(control(fd, client, client, 0xd01, words, 12, &status) == 0 && status == 0x1f);
CHECK(map(fd, client, hdev, hvirt, 0, 0x1000, mfd3, &m2) == 0x56);
/* Video memory (0x40): this driver's first open builds no framebuffer heap. */
memset(&mem, 0, sizeof mem); mem.owner = client; mem.flags = 0x1c101; mem.attr = 0x18000000; mem.size = 0x280000; mem.alignment = 0x200000;
CHECK(make(fd, client, hdev, 0x5c000024, 0x40, &mem) == 0x56);
/* The last close of the device node detaches the GPU and frees its devices, and with
* them the notifier object; its mapping stays readable. */
CHECK(close(gfd2) == 0);
CHECK(control(fd, client, hsub, 0x20800131, words, 4, &status) == 0 && status == 0);
CHECK(close(gfd) == 0);
CHECK(note[5] == 0x5a);
words[0] = hnote; words[1] = 0;
CHECK(control(fd, client, client, 0xd01, words, 12, &status) == 0 && status == 0x57);
CHECK(control(fd, client, hsub, 0x20800131, words, 4, &status) == 0 && status == 0x57);
CHECK(control(fd, client, hcc, 0xcb330101, block, 6, &status) == 0 && status == 0);
memset(words, 0xa5, sizeof words);
CHECK(control(fd, client, client, 0x201, words, 128, &status) == 0 && status == 0 && words[0] == 0xffffffff);
CHECK(make(fd, client, client, hdev, 0x80, &dev) == 0x40);
/* A second file: clients belong to the file that allocated them. */
fd2 = open(NODE, O_RDWR | O_CLOEXEC);
CHECK(fd2 >= 0);
CHECK(control(fd2, client, client, 0x1f0, words, 4, &status) == 0 && status == 0x23);
memset(&f, 0, sizeof f); f.root = f.object = client; f.status = 0xdddddddd;
CHECK(ioctl(fd2, ESC(0x29, sizeof f), &f) == 0 && f.status == 0x23);
CHECK(root(fd2, &other, &status) == 0 && status == 0 && other == 0xc1d00002);
words[0] = other; words[1] = 1; words[2] = 3;
CHECK(control(fd, client, client, 0xd04, words, 12, &status) == 0 && status == 0);
CHECK(close(fd2) == 0);
CHECK(control(fd, client, client, 0xd04, words, 12, &status) == 0 && status == 0x33);
/* Free: a handle that names nothing succeeds; the parent field is ignored. */
f.root = client; f.parent = 0xdeadbeef; f.object = 0x5c000002; f.status = 0xdddddddd;
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0);
f.object = client;
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0);
CHECK(ioctl(fd, ESC(0x29, sizeof f), &f) == 0 && f.status == 0x33);
CHECK(control(fd, client, client, 0x1f0, words, 4, &status) == 0 && status == 0x23);
CHECK(root(fd, &other, &status) == 0 && status == 0 && other == 0xc1d00003);
/* Escapes outside the slice. */
memset(block, 0, sizeof block);
CHECK(ioctl(fd, ESC(0x2c, 0x20), block) == -1 && errno == EINVAL);
CHECK(ioctl(fd, ESC(0x4e, 0x38), block) == 0 && block[40] == 0x23);
CHECK(ioctl(fd, ESC(0x27, 0x38), block) == -1 && errno == EINVAL);
/* The clients still open (0xc1d00001, 0x1234, 0xc1d00003) go with the file. */
CHECK(close(fd) == 0);
fd = open(NODE, O_RDWR | O_CLOEXEC);
CHECK(fd >= 0);
memset(&a, 0, sizeof a); a.root = 0x1234;
CHECK(ioctl(fd, ESC(0x2b, sizeof a), &a) == 0 && a.status == 0 && a.object == 0x1234);
CHECK(close(fd) == 0);
/* A control file closed with a live memory object and a live mapping of it: the client
* and the object go with the file, the mapping keeps its pages until the process ends. */
fd = open(NODE, O_RDWR | O_CLOEXEC);
gfd = open(GPU_NODE, O_RDWR | O_CLOEXEC);
mfd = open(NODE, O_RDWR | O_CLOEXEC);
CHECK(fd >= 0 && gfd >= 0 && mfd >= 0);
CHECK(root(fd, &client, &status) == 0 && status == 0);
memset(&dev, 0, sizeof dev); dev.share = client; dev.mode = 2;
CHECK(make(fd, client, client, hdev, 0x80, &dev) == 0);
CHECK(sysmem(fd, client, hdev, hbig, client, 0, 0x2000, &mem) == 0 && mem.limit == 0x1fff);
CHECK(sysmem(fd, client, hdev, hnote, hdev, 13, 0x1000, &mem) == 0);
CHECK(map(fd, client, hdev, hbig, 0x1000, 0x1000, mfd, &m) == 0);
big = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
CHECK(big != MAP_FAILED && big[0] == 0);
big[0] = 0x33;
CHECK(close(mfd) == 0 && close(fd) == 0 && close(gfd) == 0);
CHECK(big[0] == 0x33 && part[4096 + 4] == pattern(4) && note[5] == 0x5a);
printf("NV_RS_VM_RMAPI_PASS {\"checks\":%u,\"gpus\":1,\"gpu_id\":%u}\n", checks, gpu);
return 0;
}