Files
nvdriver/tools/probe_original_software_controls.py

93 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""Bounded original-driver CPU client/control parity; no GPU resources allocated."""
import ctypes as C, fcntl, os, struct, json, pathlib, time
def _read_module_version(node: str) -> str:
module_name = 'nvidia_rs' if node == '/dev/nvidia-rsctl' else 'nvidia'
try:
return (pathlib.Path('/sys/module') / module_name / 'version').read_text(encoding='utf-8').strip()
except OSError:
return 'unloaded'
def main():
node = os.environ.get('NV_RS_NODE', '/dev/nvidiactl')
records = []
clients = []
fds = []
def request(fd, label, n, top, payload=None):
errno = 0
try:
fcntl.ioctl(fd, 0xc0004600 | (len(top) << 16) | n, top, True)
except OSError as e:
errno = e.errno
offset = {0x2b: 40 if len(top) == 48 else 28, 0x2a: 28, 0x29: 12}[n]
row = {'label': label, 'linux_errno': errno, 'status': struct.unpack_from('<I', top, offset)[0], 'response': top.hex()}
if payload is not None:
row['payload'] = bytes(payload).hex()
records.append(row)
return row
try:
fds.append(os.open(node, os.O_RDWR))
fds.append(os.open(node, os.O_RDWR))
def alloc(label, legacy=False, root=0, parent=0, new=0, cls=0x41):
top = bytearray(32 if legacy else 48)
struct.pack_into('<IIII', top, 0, root, parent, new, cls)
row = request(fds[0], label, 0x2b, top)
if not row['linux_errno'] and row['status'] == 0:
handle = struct.unpack_from('<I', top, 8)[0]
clients.append(handle)
return handle
return 0
root = alloc('root64')
if not root:
raise RuntimeError('root64 allocation failed')
cases = [('root_parent', root, 1), ('root_class', root, 2), ('invalid_index', root, 0), ('unknown_index', root, 3), ('missing_object', 0xfffffff0, 2), ('missing_object_invalid_index', 0xfffffff0, 0)]
for label, obj, index in cases:
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', obj, index, 0xa5a5a5a5a5a5a5a5))
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, C.addressof(data), 16, 0))
request(fds[0], label, 0x2a, top, data)
for size in [0, 8, 15, 17]:
data = (C.c_ubyte * 32).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5) + bytes([0xa5]) * 16)
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, C.addressof(data), size, 0))
request(fds[0], f'handle_size_{size}', 0x2a, top, data)
for target in [0, 0xfffffff0, root]:
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIII', root, 2, 0xa5a5a5a5, 0xa5a5a5a5))
top = bytearray(struct.pack('<IIIIQII', root, target, 0xd02, 0, C.addressof(data), 16, 0))
request(fds[0], f'control_target_{target:#x}', 0x2a, top, data)
for obj in [root, 0, 0xfffffff0]:
data = (C.c_ubyte * 12).from_buffer_copy(struct.pack('<III', obj, 0, 0xa5a5a5a5))
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd01, 0, C.addressof(data), 12, 0))
request(fds[0], f'address_space_{obj:#x}', 0x2a, top, data)
for size in [8, 16]:
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIII', root, 0, 0xa5a5a5a5, 0xa5a5a5a5))
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd01, 0, C.addressof(data), size, 0))
request(fds[0], f'address_space_size_{size}', 0x2a, top, data)
for invalidclient, size in [(0, 16), (0, 15), (0xfffffff0, 16), (0xfffffff0, 15)]:
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5))
top = bytearray(struct.pack('<IIIIQII', invalidclient, root, 0xd02, 0, C.addressof(data), size, 0))
request(fds[0], f'client_{invalidclient:#x}_size_{size}', 0x2a, top, data)
for flag in [1, 2]:
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5))
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, flag, C.addressof(data), 16, 0))
request(fds[0], f'control_flags_{flag}', 0x2a, top, data)
for label, legacy, r, p, n in [('root21', True, 0, 0, 0), ('requested_root21', True, 0, 0, 0x24680001), ('requested_root64', False, 0, 0, 0x24680002), ('nonzero_root_field', False, root, 0, 0), ('nonzero_parent_field', False, 0, root, 0)]:
alloc(label, legacy, r, p, n)
finally:
if fds:
for h in clients:
request(fds[0], 'cleanup', 0x29, bytearray(struct.pack('<IIII', h, 0, h, 0)))
for fd in fds:
os.close(fd)
print(json.dumps({'scope': 'Software roots and root controls only; no GPU objects, GPU memory or hardware operations requested', 'node': node, 'driver_version': _read_module_version(node), 'records': records, 'time_ns': time.time_ns()}, indent=2))
return 0
if __name__ == '__main__':
raise SystemExit(main())