Files
nvdriver/tools/check_gpu_nvdec_hs_run_binary.py
nvidia-driver-rs 4526e6a816 Baseline: validated 30/30 tree with passing Level 7 hardware regression
- Updated tools/post_reboot.sh to check kernel oops taint bit and stuck driver tasks instead of <86400s uptime
- Level 7 hardware regression passed on 0000:b3:00.0 (module 37e6fa430835eee2b426019db3ad70eeb7a9e9070c09c84bd9b973cf56cbe529, 107 inventory words, 0 static mismatches, CUDA baseline verified before and after)
- Updated .gitignore to exclude build archives (*.a, *.rlib), __pycache__, and raw trace dumps
2026-11-23 23:48:38 +00:00

803 lines
35 KiB
Python

#!/usr/bin/env python3
"""Run the original GV100 NVDEC HS Falcon step of MemorySystem Load and write an oracle.
Executed original code (chip HAL 0x23, RM HAL 2), with the object's real
relocations for .text, .rodata and .data:
MemorySystem +cb8 _nv045404rm (predicate) with +ca8 _nv045506rm, +cb0
_nv045392rm, +ca0 _nv044833rm, +cc0 _nv045387rm;
+c78 _nv045174rm (the run) with +dc8 _nv044493rm, +c80
_nv046026rm, +c88 _nv045113rm; +e98 _nv044383rm.
OBJBSP _nv018167rm with the bundle getters at +260/+268/+270, the
descriptor helpers _nv017872rm, _nv017874rm, _nv017879rm,
_nv017881rm and the unpacker beneath them, +3d8 _nv046969rm,
+3c8 _nv046934rm, +3d0 _nv046477rm, +1e0 _nv046984rm, +238
_nv046979rm, and +1c8 _nv046468rm, which configures the Falcon.
OBJACR _nv016965rm (signature placement), +370 _nv016945rm.
PMU class +3d8 _nv052308rm.
Falcon +228 _nv028570rm, +220 _nv028574rm, +128 _nv028679rm with
_nv028678rm and _nv028467rm, _nv028539rm, _nv028746rm,
_nv028484rm, +1a0 _nv028785rm, _nv028445rm, _nv028523rm, +1e0
_nv028457rm with _nv028456rm, +1c8 _nv028764rm, _nv028782rm,
the register helpers _nv028672rm and _nv028674rm, and the
attach and configuration functions _nv021629rm, _nv028533rm,
_nv028412rm.
OBJMC +158 _nv043619rm, +150 _nv043634rm, +188 _nv043603rm, +190
_nv043639rm with _nv041568rm, and the list helpers.
GPU object _nv030976rm and +5c8 _nv030977rm (clock-gating tables) with
the functions at +740 to +768, +3b0 _nv030929rm.
Other _nv059506rm (wait on a condition), _nv043190rm.
The function tables are filled by the original binders. The NVDEC object's
vtable and interface pointers are stored by this harness exactly as the creator
_nv005713rm stores them, and the Falcon is attached with _nv021629rm.
Fixture-supplied: register read _nv053907rm and write _nv053912rm, the delay
_nv049598rm, the time functions _nv059509rm, _nv059505rm and _nv059492rm,
diagnostics (_nv047869rm, _nv047868rm, _nv046745rm, _nv046747rm, the state
assertion _nv049765rm), the allocator (_nv052687rm, _nv052701rm, _nv059714rm),
memory fill and copy performed as such, the registry lookup _nv049786rm (no
value found), the clock-gating feature services _nv031034rm and _nv031133rm,
the FIFO lookup _nv030608rm with its function at +1b8, the multi-GPU section
_nv031478rm, the interface function _nv028817rm that finds this Falcon on a
GPU, the function at +270 of the GPU+20c8 object, the function at +168 of the
GPU+1e78 object, the GPU function at +3b8 (_nv030210rm) and the address
function _nv043721rm of the MemorySystem+4070 allocation. No hardware is
involved.
"""
import bisect
import hashlib
import itertools
import json
from pathlib import Path
import struct
import warnings
warnings.simplefilter("ignore")
from elftools.elf.elffile import ELFFile # noqa: E402
from unicorn import UC_ARCH_X86, UC_HOOK_BLOCK, UC_HOOK_CODE, UC_MODE_64, Uc # noqa: E402
from unicorn.x86_const import (UC_X86_REG_R8, UC_X86_REG_R9, UC_X86_REG_RAX, # noqa: E402
UC_X86_REG_RBP, UC_X86_REG_RCX, UC_X86_REG_RDI, UC_X86_REG_RDX,
UC_X86_REG_RIP, UC_X86_REG_RSI, UC_X86_REG_RSP)
ROOT = Path(__file__).resolve().parents[1]
ORIGINAL = Path("/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/"
"nv-kernel.o_binary")
DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b"
TRANSCRIPT = ROOT / "target/trace/b3-bar0.txt"
TEXT, RODATA, DATAS, BSS, DATA, STACK, STOP, HEAP = (
0x10000000, 0x20000000, 0x28000000, 0x2c000000, 0x30000000, 0x40000000, 0x50000000,
0x60000000)
INDIRECT, EXTERNAL, SERVICE = STOP + 0x100, STOP + 0x200, STOP + 0xc000
(GPU, MEMSYS, NVDEC, FLCN, CHIP, RM, VBIOS, ACR, LSFM, SEC2, FUSE, PMU, MC, FIFO, SCRATCH,
ALLOCATION) = (DATA + 0x20000 * index for index in range(16))
OWNER = NVDEC + 0xe8
ARGUMENTS = (UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX, UC_X86_REG_RCX, UC_X86_REG_R8,
UC_X86_REG_R9)
# Class vtables and self pointers the creator _nv005713rm stores in an OBJBSP.
NVDEC_VTABLES = ((0, 0x4d94840), (0x10, 0x4d94870), (8, 0x4d94858), (0xe8, 0x4d948f8),
(0x108, 0x4d94a58), (0x120, 0x4d94a78), (0x180, 0x4d94ad0))
NVDEC_SELF = ((0x190, 0x10), (0x1a8, 0x108), (0x198, 8), (0x1a0, 0xe8), (0x1b0, 0x120),
(0x1b8, 0x180), (0x1c0, 0))
BOUND = {
"MemorySystem": (MEMSYS, {0xc68: "_nv045171rm", 0xc78: "_nv045174rm", 0xc80: "_nv046026rm",
0xc88: "_nv045113rm", 0xca0: "_nv044833rm", 0xca8: "_nv045506rm",
0xcb0: "_nv045392rm", 0xcb8: "_nv045404rm", 0xcc0: "_nv045387rm",
0xdc8: "_nv044493rm", 0xe98: "_nv044383rm"}),
"OBJBSP": (NVDEC, {0x1c8: "_nv046468rm", 0x1e0: "_nv046984rm", 0x238: "_nv046979rm",
0x260: "_nv018230rm", 0x268: "_nv018227rm", 0x270: "_nv018224rm",
0x3c8: "_nv046934rm", 0x3d0: "_nv046477rm", 0x3d8: "_nv046969rm"}),
"Falcon": (FLCN, {0x128: "_nv028679rm", 0x1a0: "_nv028785rm", 0x1c8: "_nv028764rm",
0x1d0: "_nv028761rm", 0x1d8: "_nv028461rm", 0x1e0: "_nv028457rm",
0x220: "_nv028574rm", 0x228: "_nv028570rm", 0x230: "_nv028567rm",
0x2e8: "_nv028579rm", 0x2f0: "_nv028577rm", 0x370: "_nv028766rm",
0x378: "_nv028612rm", 0x388: "_nv028734rm"}),
"OBJMC": (MC, {0x150: "_nv043634rm", 0x158: "_nv043619rm", 0x188: "_nv043603rm",
0x190: "_nv043639rm"}),
"OBJACR": (ACR, {0x370: "_nv016945rm"}),
"OBJSEC2": (SEC2, {0x438: "_nv055811rm"}),
"PMU class": (PMU, {0x3d8: "_nv052308rm"}),
"GPU": (GPU, {0x3b0: "_nv030929rm", 0x3b8: "_nv030210rm", 0x5c8: "_nv030977rm",
0x740: "_nv030423rm", 0x750: "_nv030519rm", 0x768: "_nv030894rm"}),
}
DIAGNOSTICS = ("_nv047869rm", "_nv047868rm", "_nv046745rm", "_nv046747rm", "_nv049765rm")
HOOKED = ("_nv053907rm", "_nv053912rm", "_nv049598rm", "_nv059509rm", "_nv059505rm",
"_nv059492rm", "_nv052687rm", "_nv052701rm", "_nv059714rm", "_nv052705rm",
"_nv049786rm", "_nv031034rm", "_nv031133rm", "_nv030608rm", "_nv031478rm",
"_nv028817rm", "_nv043721rm") + DIAGNOSTICS
assert hashlib.sha256(ORIGINAL.read_bytes()).hexdigest() == DIGEST
with ORIGINAL.open("rb") as stream:
elf = ELFFile(stream)
symbols = list(elf.get_section_by_name(".symtab").iter_symbols())
by_name = {symbol.name: symbol for symbol in symbols}
sections = {}
for name, base in ((".text", TEXT), (".rodata", RODATA), (".data", DATAS)):
sections[elf.get_section_index(name)] = (base, bytearray(
elf.get_section_by_name(name).data()), name)
bss_index, bss_size = elf.get_section_index(".bss"), elf.get_section_by_name(".bss")["sh_size"]
text_index = elf.get_section_index(".text")
starts = sorted((symbol["st_value"], symbol["st_size"], symbol.name) for symbol in symbols
if symbol["st_shndx"] == text_index and symbol["st_size"])
relocations = {name: [(entry["r_offset"], entry["r_info_type"], entry["r_info_sym"],
entry["r_addend"])
for entry in elf.get_section_by_name(".rela" + name).iter_relocations()]
for name in (".text", ".rodata", ".data")}
entries = {start: name for start, size, name in starts}
external = {}
def address_of(symbol):
index = symbol["st_shndx"]
if index in sections:
return sections[index][0] + symbol["st_value"]
if index == bss_index:
return BSS + symbol["st_value"]
if symbol.name == "__x86_indirect_thunk_rax":
return INDIRECT
return external.setdefault(symbol.name, EXTERNAL + 8 * len(external))
for base, data, name in sections.values():
for offset, kind, index, addend in relocations[name]:
value = address_of(symbols[index]) + addend
assert len(external) < (SERVICE - EXTERNAL) // 8
if kind in (2, 4):
struct.pack_into("<I", data, offset, (value - (base + offset)) & 0xffffffff)
elif kind in (10, 11):
struct.pack_into("<I", data, offset, value & 0xffffffff)
else:
assert kind == 1, kind
struct.pack_into("<Q", data, offset, value & 0xffffffffffffffff)
cpu = Uc(UC_ARCH_X86, UC_MODE_64)
for base, data, name in sections.values():
cpu.mem_map(base, (len(data) + 4095) & ~4095)
cpu.mem_write(base, bytes(data))
cpu.mem_map(BSS, (bss_size + 4095) & ~4095)
for base, size in ((DATA, 0x400000), (STACK, 0x100000), (STOP, 0x10000), (HEAP, 0x400000)):
cpu.mem_map(base, size)
case, trace, hooks, executed, heap, diagnostics = {}, [], {}, set(), [HEAP], [0]
def q(address, value):
cpu.mem_write(address, struct.pack("<Q", value))
def d(address, value):
cpu.mem_write(address, struct.pack("<I", value))
def b(address, value):
cpu.mem_write(address, bytes([value]))
def byte(address):
return cpu.mem_read(address, 1)[0]
def word(address):
return struct.unpack("<I", cpu.mem_read(address, 4))[0]
def pointer(address):
return struct.unpack("<Q", cpu.mem_read(address, 8))[0]
def text(name):
return TEXT + by_name[name]["st_value"]
def leave(emu, value):
rsp = emu.reg_read(UC_X86_REG_RSP)
emu.reg_write(UC_X86_REG_RSP, rsp + 8)
emu.reg_write(UC_X86_REG_RAX, value & 0xffffffffffffffff)
emu.reg_write(UC_X86_REG_RIP, pointer(rsp))
def hook(*names):
def register(function):
for name in names:
hooks[text(name) if name.startswith("_nv") else name] = (name, function)
return function
return register
def service(name):
def register(function):
address = SERVICE + 16 * sum(1 for key in hooks if isinstance(key, int)
and key >= SERVICE)
hooks[address] = (name, function)
return address
return register
def next_read(offset):
values = case["reads"][offset]
return values.pop(0) if len(values) > 1 else values[0]
@hook("_nv053907rm")
def register_read(a, low):
assert (a[0], low[1], low[2]) == (GPU + 0x4120, 0, 0), a
value = next_read(low[3])
trace.append(f"r:{low[3]:x}:{value:x}")
return value
@hook("_nv053912rm")
def register_write(a, low):
assert (a[0], low[1], low[2]) == (GPU + 0x4120, 0, 0), a
trace.append(f"w:{low[3]:x}:{low[4]:x}")
return 0
@hook("_nv049598rm")
def delay(a, low):
trace.append(f"t:{low[0]:x}")
return 0
@hook("_nv059509rm")
def timeout_start(a, low):
assert a[0] == GPU + 0x1e08
trace.append(f"ts:{low[2]:x}:{low[3]:x}")
return 0
@hook("_nv059505rm")
def timeout_check(a, low):
assert a[0] == GPU + 0x1e08
status = case["checks"].pop(0) if case["checks"] else 0
trace.append(f"tc:{low[2]:x}:{status:x}")
return status
@hook("_nv059492rm")
def timeout_reset(a, low):
assert a[0] == GPU
trace.append("tr")
return 0
@hook(*DIAGNOSTICS)
def diagnostic(a, low):
diagnostics[0] += 1
return 0
@hook("_nv052687rm")
def allocate(a, low):
address = heap[0]
heap[0] += (low[0] + 0x3f) & ~0x1f
assert heap[0] < HEAP + 0x400000
return address
@hook("_nv052701rm", "_nv059714rm")
def release(a, low):
return 0
@hook("_nv052705rm")
def fill(a, low):
cpu.mem_write(a[0], bytes([low[1] & 0xff]) * low[2])
return a[0]
@hook("_nv049786rm")
def registry(a, low):
return 0x56
@hook("_nv031034rm")
def feature_state(a, low):
assert a[0] == GPU
state = case["inputs"]["fs7" if low[1] == 7 else "fs13"]
trace.append(f"fs:{low[1]:x}:{low[2]:x}:{state:x}")
return state
@hook("_nv031133rm")
def record_feature(a, low):
assert a[0] == GPU
trace.append(f"rf:{low[1]:x}:{low[2]:x}:{low[3] & 0xff:x}")
return 0
@hook("_nv030608rm")
def fifo_object(a, low):
assert a[0] == GPU
return FIFO
@hook("_nv031478rm")
def gpu_section(a, low):
assert (a[0], low[1], low[2]) == (GPU, 0, 0), a
trace.append(f"gs:{low[3]:x}")
return 0
@hook("_nv028817rm")
def falcon_on_gpu(a, low):
assert (a[0], a[1]) == (GPU, OWNER)
return FLCN
@hook("_nv043721rm")
def allocation_address(a, low):
assert (a[0], low[1]) == (ALLOCATION, 1)
return (case["inputs"]["first"] + a[2]) & 0xffffffffffffffff
def copy(a, low):
cpu.mem_write(a[0], bytes(cpu.mem_read(a[1], low[2])))
return a[0]
hooks[address_of(by_name["os_mem_copy"])] = ("os_mem_copy", copy)
@service("FIFO+1b8")
def fifo_lookup(a, low):
assert (a[0], a[1]) == (GPU, FIFO)
inputs = case["inputs"]
status, value = ((inputs["ia"], inputs["ix"]) if (low[2], low[4]) == (0, 8)
else (inputs["ba"], inputs["bx"]))
assert (low[2], low[4]) in ((0, 8), (8, 6)), low
if status == 0:
d(a[5], value)
trace.append(f"fi:{low[2]:x}:{low[3]:x}:{low[4]:x}:{status:x}:{value if not status else 0:x}")
return status
@service("GPU+20c8 object +270")
def vbios_270(a, low):
assert (a[0], a[1]) == (GPU, VBIOS)
trace.append(f"vb:{case['inputs']['vb']:x}")
return case["inputs"]["vb"]
@service("GPU+1e78 object +168")
def fuse_168(a, low):
assert (a[0], a[1], low[2]) == (GPU, FUSE, 0x67c)
trace.append(f"fu:{case['inputs']['fu']:x}")
return case["inputs"]["fu"]
@service("GPU+3b8")
def gpu_3b8(a, low):
assert a[0] == GPU
trace.append(f"g3:{case['inputs']['g3']:x}")
return case["inputs"]["g3"]
@service("synthetic bundle getter")
def bundle_getter(a, low):
return SCRATCH
def block(emu, address, size, unused):
if address in hooks:
a = [emu.reg_read(register) for register in ARGUMENTS]
leave(emu, hooks[address][1](a, [value & 0xffffffff for value in a]))
elif address - TEXT in entries:
executed.add(entries[address - TEXT])
else:
assert not EXTERNAL <= address < SERVICE, [name for name, value in external.items()
if value == address]
cpu.hook_add(UC_HOOK_BLOCK, block)
cpu.hook_add(UC_HOOK_CODE, lambda emu, address, size, unused: emu.reg_write(
UC_X86_REG_RIP, emu.reg_read(UC_X86_REG_RAX)), begin=INDIRECT, end=INDIRECT)
def call(name, *arguments):
for register, argument in zip(ARGUMENTS, arguments):
cpu.reg_write(register, argument)
rsp = STACK + 0xfffc8
cpu.reg_write(UC_X86_REG_RSP, rsp)
cpu.reg_write(UC_X86_REG_RBP, STACK + 0x80000)
q(rsp, STOP)
executed.add(name)
cpu.emu_start(text(name), STOP, count=80000000)
assert cpu.reg_read(UC_X86_REG_RIP) == STOP, name
return cpu.reg_read(UC_X86_REG_RAX) & 0xffffffff
# Function tables by the original binders.
cpu.mem_write(CHIP + 0x10, struct.pack("<H", 0x23))
cpu.mem_write(RM + 0x10, struct.pack("<H", 2))
q(GPU + 0x80, CHIP)
q(GPU + 0x88, RM)
case.update(inputs={}, reads={}, checks=[])
for binder, target in (("_nv005936rm", MEMSYS), ("_nv005951rm", NVDEC), ("_nv005952rm", NVDEC),
# The parent-class binders receive sub-objects: OBJBSP+120
# for _nv005997rm, PMU+108 for _nv006034rm
# (reports/gv100-engine-hal-bindings.json, argument_conventions).
("_nv005997rm", NVDEC + 0x120), ("_nv005811rm", NVDEC), ("_nv005876rm", FLCN),
("_nv006010rm", SEC2), ("_nv006011rm", SEC2), ("_nv005947rm", ACR),
("_nv006034rm", PMU + 0x108), ("_nv006035rm", PMU), ("_nv006001rm", MC)):
call(binder, target, CHIP, RM)
call("_nv005980rm", GPU)
call("_nv005982rm", GPU)
binders = sorted(executed)
bound = {}
for label, (base, slots) in BOUND.items():
for slot, function in slots.items():
assert entries.get(pointer(base + slot) - TEXT) == function, (label, hex(slot))
bound[f"{label}+{slot:#x}"] = function
properties = {"OBJBSP+0x50": byte(NVDEC + 0x50), "OBJBSP+0x423": byte(NVDEC + 0x423)}
assert properties == {"OBJBSP+0x50": 0, "OBJBSP+0x423": 0}, properties
# The NVDEC object as its creator lays it out, and its child pointers.
for offset, table in NVDEC_VTABLES:
q(NVDEC + offset, RODATA + table)
for offset, target in NVDEC_SELF:
q(NVDEC + offset, NVDEC + target)
q(NVDEC + 0x100, FLCN)
# Interface byte +10: the configuration function at +60 of the interface
# vtable (the NVDEC function at +3c8) is asked only when it is set.
b(OWNER + 0x10, 1)
for offset, target in ((0x1ea0, MC), (0x1ec0, MEMSYS), (0x2080, NVDEC), (0x20c8, VBIOS),
(0x2120, PMU), (0x2308, SEC2), (0x2310, LSFM), (0x2318, ACR),
(0x1e78, FUSE), (0x1e20, GPU)):
q(GPU + offset, target)
BOOT0 = 0x140000a1
d(GPU + 0xde0, BOOT0)
d(GPU + 0xcac, 0x20)
q(FIFO + 0x1b8, fifo_lookup)
q(VBIOS + 0x270, vbios_270)
q(FUSE + 0x168, fuse_168)
assert call("_nv021629rm", FLCN, OWNER) == 0
assert call("_nv046468rm", GPU, NVDEC) == 0
configured = {"Falcon+0x544": byte(FLCN + 0x544), "Falcon+0x548": word(FLCN + 0x548),
"Falcon+0x550": word(FLCN + 0x550), "Falcon+0x55c": word(FLCN + 0x55c),
"Falcon+0x558": word(FLCN + 0x558), "Falcon+0x581": byte(FLCN + 0x581),
"Falcon+0x5ec": word(FLCN + 0x5ec), "Falcon+0x608": word(FLCN + 0x608),
"Falcon+0x621": byte(FLCN + 0x621), "Falcon+0x650": word(FLCN + 0x650),
"Falcon+0x3a2": byte(FLCN + 0x3a2), "Falcon+0x3af": byte(FLCN + 0x3af),
"Falcon+0x5d8": "OBJBSP+0xe8" if pointer(FLCN + 0x5d8) == OWNER else "other"}
assert configured == {
"Falcon+0x544": 1, "Falcon+0x548": 0x84000, "Falcon+0x550": 0x84600,
"Falcon+0x55c": BOOT0, "Falcon+0x558": 0, "Falcon+0x581": 1, "Falcon+0x5ec": 0x8f99e100,
"Falcon+0x608": 0, "Falcon+0x621": 0, "Falcon+0x650": 1, "Falcon+0x3a2": 0,
"Falcon+0x3af": 0, "Falcon+0x5d8": "OBJBSP+0xe8"}, configured
SETUP = sorted(executed)
OBJECTS = (GPU, MEMSYS, NVDEC, FLCN, VBIOS, ACR, LSFM, SEC2, FUSE, PMU, MC, FIFO, SCRATCH,
ALLOCATION)
SNAPSHOT = [bytes(cpu.mem_read(base, 0x20000)) for base in OBJECTS]
BUNDLE_260 = RODATA + 0x29850
BUNDLE_COUNT = word(BUNDLE_260)
BUNDLE = {word(BUNDLE_260 + 8 + 16 * index): pointer(BUNDLE_260 + 16 + 16 * index)
for index in range(BUNDLE_COUNT)}
assert sorted(BUNDLE) == [0xd, 0xe, 0x18, 0x23, 0x31, 0x32], BUNDLE
rows, counts = [], {}
FALCON_STATE = (("3a0", 0x3a0), ("3a2", 0x3a2), ("3a5", 0x3a5), ("3a7", 0x3a7), ("3ab", 0x3ab),
("583", 0x583), ("5d4", 0x5d4), ("5d5", 0x5d5))
def descriptor(address, data):
"""A stored (not packed) descriptor in the original layout."""
cpu.mem_write(address + 0x100, data)
cpu.mem_write(address, struct.pack("<IIQI", len(data), len(data), address + 0x100, 0xa))
return address
def variant(name):
"""Synthetic bundles built from the original descriptors of bundle +260."""
chosen = dict(BUNDLE)
if name.startswith("drop"):
del chosen[int(name[4:], 16)]
elif name == "prod":
chosen[0x26], chosen[0x1a] = chosen[0x23], chosen[0x18]
elif name == "debug":
chosen[0x24], chosen[0x19] = chosen[0x23], chosen[0x18]
elif name == "multi":
# Two signatures per record, selected through tags 0x2e and 0x3c.
chosen[0xd] = descriptor(SCRATCH + 0x1000, bytes(range(0x10, 0x30)))
chosen[0xe] = descriptor(SCRATCH + 0x1400, bytes(range(0x50, 0x70)))
chosen[0x2e] = descriptor(SCRATCH + 0x1800, struct.pack("<I", 2))
chosen[0x3c] = descriptor(SCRATCH + 0x1c00, struct.pack("<3I", 3, 0, 0))
else:
raise AssertionError(name)
d(SCRATCH, len(chosen))
for index, (tag, target) in enumerate(chosen.items()):
d(SCRATCH + 8 + 16 * index, tag)
q(SCRATCH + 16 + 16 * index, target)
q(NVDEC + 0x260, bundle_getter)
def prepare(inputs, reads, checks):
for base, saved in zip(OBJECTS, SNAPSHOT):
cpu.mem_write(base, saved)
heap[0] = HEAP
diagnostics[0] = 0
trace.clear()
case.update(inputs=inputs, reads={offset: list(values) for offset, values in reads.items()},
checks=list(checks))
for name, offset in (("g915", 0x915), ("g917", 0x917), ("g918", 0x918), ("g919", 0x919),
("g926", 0x926), ("g90e", 0x90e), ("g914", 0x914), ("g91f", 0x91f)):
b(GPU + offset, inputs.get(name, 0))
cpu.mem_write(GPU + 0x4de4, struct.pack("<H", inputs.get("g4de4", 0)))
if inputs.get("g91f"):
q(GPU + 0x3b8, gpu_3b8)
def finish(kind, inputs, reads, checks, status, state):
assert all(len(values) == 1 for values in case["reads"].values()), (kind, inputs,
case["reads"])
assert not case["checks"], (kind, inputs, case["checks"])
counts[kind] = counts.get(kind, 0) + 1
rows.append("|".join((
kind, " ".join(f"{key}={value}" if isinstance(value, str) else f"{key}={value:x}"
for key, value in inputs.items()),
" ".join(f"{offset:x}=" + ",".join(f"{value:x}" for value in values)
for offset, values in reads.items()),
",".join(f"{value:x}" for value in checks), f"{status:x}",
" ".join(f"{key}={value:x}" for key, value in state.items()), f"{diagnostics[0]:x}",
" ".join(trace))))
return list(trace)
FULL = dict(nvdec=1, n50=0, n423=0, vb=0, lsfm=0, flags=0, g915=1, g917=0, g918=0, g919=0,
g926=0, f3a6=0, f3b4=0, eng=0x8f99e100, fs7=0, fs13=0, ia=0, ix=0x41, ba=0, bx=15,
m8cc5=0, bundle="real")
# Register values of the traced V100. Lists are consumed in order; the last
# value repeats.
READS = {0x1180fc: [0x247fffff], 0x21644: [1], 0x10ac08: [0], 0x200: [0x1fecdff1, 0x1fecdff1,
0x1fec5ff1, 0x1fec5ff1,
0x1fec5ff1, 0x1fecdff1],
0x84048: [4], 0x8412c: [0x996b1136], 0x84040: [0], 0x8410c: [3, 1], 0x84624: [0x110],
0x84100: [0x10] + [0] * 21 + [0x10], 0x100cd0: [0x1ffff000, 1, 0x2a], 0x841c4: [0]}
def falcon_setup(inputs):
b(FLCN + 0x3a6, inputs["f3a6"])
b(FLCN + 0x3b4, inputs["f3b4"])
d(FLCN + 0x5ec, inputs["eng"])
d(LSFM + 0x198, inputs["lsfm"] << word(FLCN + 0x608))
def falcon_state():
state = {name: byte(FLCN + offset) for name, offset in FALCON_STATE}
state["582"] = struct.unpack("<H", cpu.mem_read(FLCN + 0x582, 2))[0]
return state
def full(reads=None, checks=(), **changes):
inputs = dict(FULL, **changes)
reads = {offset: list(values) for offset, values in {**READS, **(reads or {})}.items()}
prepare(inputs, reads, checks)
falcon_setup(inputs)
q(GPU + 0x2080, NVDEC if inputs["nvdec"] else 0)
b(NVDEC + 0x50, inputs["n50"])
b(NVDEC + 0x423, inputs["n423"])
b(MEMSYS + 0x8cc5, inputs["m8cc5"])
if inputs["bundle"] != "real":
variant(inputs["bundle"])
status = call("_nv045174rm", GPU, MEMSYS, inputs["flags"])
# Unused read values of a path that ended early are dropped from the row.
reads = {offset: values[:len(values) - len(case["reads"][offset]) + 1]
for offset, values in reads.items()}
case["reads"] = {offset: values[-1:] for offset, values in reads.items()}
case["checks"] = []
state = falcon_state()
state.update({"8cc5": byte(MEMSYS + 0x8cc5), "444": byte(NVDEC + 0x444),
"498": byte(NVDEC + 0x498)})
return finish("F", inputs, reads, checks[:len(checks)], status, state)
def slot_220(after, reads=None, checks=(), **changes):
inputs = dict(FULL, **changes)
inputs = {key: inputs[key] for key in ("lsfm", "g917", "g918", "g919", "g926", "f3a6", "f3b4",
"eng", "fs7", "fs13", "ia", "ix", "ba", "bx")}
inputs["after"] = after
reads = {offset: list(values) for offset, values in {
0x200: [0x1fecdff1], 0x84048: [4], 0x8412c: [0x996b1136], 0x84040: [0],
0x8410c: [3, 1], **(reads or {})}.items()}
prepare(inputs, reads, checks)
falcon_setup(inputs)
status = call("_nv028574rm", GPU, FLCN, after)
reads = {offset: values[:len(values) - len(case["reads"][offset]) + 1]
for offset, values in reads.items()}
case["reads"] = {offset: values[-1:] for offset, values in reads.items()}
assert not case["checks"], case["checks"]
return finish("S", inputs, reads, checks, status, falcon_state())
def predicate(reads=None, **changes):
inputs = dict(nvdec=1, n50=0, sec2=1, s50=0, g915=1, g918=0, g90e=0, g914=0, g91f=0,
g4de4=0, g3=0, fu=0x21000)
inputs.update(changes)
reads = {offset: list(values) for offset, values in {
0x1180fc: [0x247fffff], 0x1180f4: [0], 0x100cd0: [0x1ffff000, 1, 0x2a],
0x21000: [1], **(reads or {})}.items()}
prepare(inputs, reads, ())
q(GPU + 0x2080, NVDEC if inputs["nvdec"] else 0)
q(GPU + 0x2308, SEC2 if inputs["sec2"] else 0)
b(NVDEC + 0x50, inputs["n50"])
b(SEC2 + 0x50, inputs["s50"])
result = call("_nv045404rm", GPU, MEMSYS, 0) & 0xff
reads = {offset: values[:len(values) - len(case["reads"][offset]) + 1]
for offset, values in reads.items()}
case["reads"] = {offset: values[-1:] for offset, values in reads.items()}
state = {"valid": byte(MEMSYS + 0x8be0), "start": pointer(MEMSYS + 0x8be8),
"end": pointer(MEMSYS + 0x8bf0)}
return finish("P", inputs, reads, (), result, state)
def vpr_write(allocation, bytes_30=0, first=0, value=0):
inputs = dict(alloc=allocation, bytes=bytes_30, first=first, value=value)
prepare(inputs, {}, ())
q(MEMSYS + 0x4070, ALLOCATION if allocation else 0)
q(ALLOCATION + 0x30, bytes_30)
q(MEMSYS + 0x4080, value)
status = call("_nv044383rm", GPU, MEMSYS)
return finish("V", inputs, {}, (), status, {})
# The traced V100: transcript lines 139267 to 140316 are the predicate, the
# run and the two range writes that follow it in the same Load.
physical = (predicate() + full() + vpr_write(0))
physical = [entry for entry in physical if entry[:2] in ("r:", "w:")]
transcript = [line.split() for line in TRANSCRIPT.read_text().split("\n")[139266:140316]]
expected = [f"{kind.lower()}:{int(offset, 16):x}:{int(value, 16):x}"
for kind, offset, value, unused in transcript]
assert physical == expected, next((index, a, e) for index, (a, e) in enumerate(
itertools.zip_longest(physical, expected)) if a != e)
PHYSICAL_SHA256 = hashlib.sha256("\n".join(expected).encode()).hexdigest()
# Predicate.
for nvdec, n50, sec2, s50 in itertools.product((0, 1), (0, 1), (0, 1), (0, 1)):
if nvdec and not n50 and not sec2:
continue # The original calls through the null GPU+2308 pointer.
for g914, vpr in itertools.product((0, 1), (0x2a, 0x3a)):
if vpr == 0x3a and not sec2:
continue # Same null call on the VPR-enabled branch.
predicate(nvdec=nvdec, n50=n50, sec2=sec2, s50=s50, g914=g914,
reads={0x100cd0: [0x1ffff000, 1, vpr]})
for g4de4, g90e in ((1, 0), (0x100, 0), (0, 1)):
predicate(g4de4=g4de4, g90e=g90e)
for g915, g918 in ((0, 0), (1, 1), (0, 1)):
predicate(g915=g915, g918=g918, reads={0x100cd0: [0]})
# VPR start above its end, with and without GPU+914; fuse bit clear.
for g914, fuse in itertools.product((0, 1), (0, 1)):
predicate(g914=g914, reads={0x100cd0: [0xfffffff0, 1, 0x12], 0x21000: [fuse]})
predicate(g914=g914, fu=0x21004 + 4 * fuse, reads={0x100cd0: [0x10, 0x21, 0x12],
0x21004 + 4 * fuse: [fuse]})
# The index field reads back late.
predicate(reads={0x100cd0: [1, 2, 0x1ffff000, 0, 3, 0x11, 0, 1, 0x2a]})
# The +8be0 range: field of 0x1180fc, size word, GPU+91f with the GPU function at +3b8.
for first, size, g91f, g3 in itertools.product(
(0x247fffff, 0x24600000, 0x24700123, 0x24f12345, 0xffffffff, 0x00600000),
(0, 1, 0xffff, 0xffffffff, 0x10000), (0, 1), (0, 1)):
if g3 and not g91f:
continue
predicate(g91f=g91f, g3=g3, reads={0x1180fc: [first, 0x24712345], 0x1180f4: [size]})
# Falcon+220 with argument 1.
for lsfm in (0, 1):
slot_220(1, lsfm=lsfm)
# Falcon+220 with argument 0: every MC engine index, with the FIFO failing or not.
for index in itertools.chain(range(0xb8), (0xffff, 0xffffffff)):
if index in (0xd, 0x31):
continue # Mapped through OBJMC+148 and +160; not Falcon engines.
for ba, bx in ((0, 15), (0x56, 15), (0, 0x2f)):
if ba == 0 and bx == 15 or index in (9, 0xc, 0x23, 0x2f, 0x41, 0x54) \
or 0xf <= index <= 0x17 or 0x26 <= index <= 0x28:
slot_220(0, ix=index, ba=ba, bx=bx, reads={0x200: [0xffffffff, 0xffffffff,
0x7fffffff, 0, 0x5a5a5a5a]})
for ia in (0x56, 0xffff):
slot_220(0, ia=ia)
# Engine not enabled, enabled; interrupt setup variants.
for enable, f3a6, f3b4, eng, itfen in itertools.product(
(0x1fecdff1, 0x1fec5ff1, 0, 0xffffffff), (0, 1), (0, 1),
(0x8f99e100, 0x11ce1000, 0x11ce10ff, 0x8f99e107), (0, 1, 2, 3, 4, 0xffffffff)):
if not enable & 0x8000 and (f3a6 or f3b4 or itfen):
continue
slot_220(0, f3a6=f3a6, f3b4=f3b4, eng=eng, reads={0x200: [enable], 0x84048: [itfen]})
# HWCFG1 fields, GPU state bytes, the scrub wait and its timeout, feature states.
for hwcfg in (0, 0x30, 0x10, 0x40000000, 0x400000f0, 0x996b1136, 0xffffffff, 0xc1, 0x7f):
slot_220(0, reads={0x8412c: [hwcfg, hwcfg ^ 0xff]})
for g917, g918, g919, g926 in ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1),
(1, 1, 1, 1)):
slot_220(0, g917=g917, g918=g918, g919=g919, g926=g926)
for dmactl, checks in (([0], ()), ([2, 4, 6, 0], (0, 0, 0)), ([6, 6], (0, 0x65)),
([6, 6, 1], (0, 0x65)), ([6, 6], (0x56,)), ([2, 2, 0], (0x65,)),
([7, 0xfffffff9], (0,))):
slot_220(0, reads={0x8410c: dmactl}, checks=checks)
for fs7, fs13 in itertools.product((0, 1, 2), repeat=2):
slot_220(0, fs7=fs7, fs13=fs13)
# The run. Missing or disabled engine object, both bundle choices, tag choices.
full(nvdec=0)
full(n50=1)
for revision in range(0, 0x20, 3):
full(reads={0x1180fc: [revision << 20 | 0x24000123]})
full(m8cc5=1)
full(vb=1)
full(vb=1, n423=1)
for bundle, mode in itertools.product(("real", "prod", "debug"), (0, 1)):
full(bundle=bundle, reads={0x21644: [mode]})
for tag in sorted(BUNDLE):
full(bundle=f"drop{tag:x}")
full(bundle="drop23", reads={0x21644: [0]})
# Signature choice: PMU fuse bit 20, and two signatures per record.
for fuse in (0, 0x100000, 0xffefffff, 0xffffffff):
full(reads={0x10ac08: [fuse]})
full(bundle="multi", reads={0x10ac08: [fuse]})
# Start through CPUCTL or its alias; halt immediately, late, never. The first
# time check of each list belongs to the scrub wait of the reset.
for cpuctl, checks in (([0x10, 0x10], ()), ([0x50, 0x10], ()), ([0x40, 0x10], ()),
([0, 0, 0x10], (0, 0)), ([0x10] + [0] * 5 + [0xffffffff], (0,) * 6),
([0x10, 0], (0, 0, 0, 0x65)), ([0x10, 0], (0, 0x65)),
([0x10, 0, 0, 0, 0x10], (0, 0x56, 0, 0x57)),
([0x10, 0, 0, 0, 0x10], (0, 0x56, 0x57, 0)),
([0x10, 0, 0x10], (0, 0x56)), ([0x10, 0, 0x10], (0x65, 0x56)),
([0x10, 0], (0x56, 0x65))):
full(reads={0x84100: cpuctl}, checks=checks)
# VPR state after the run, the Load flag that skips the check, mailbox value.
for flags, after, mailbox in itertools.product((0, 0x20, 0xffffffdf, 0xffffffff),
(0x2a, 0x3a, 0x12), (0, 0xdeadbeef)):
full(flags=flags, reads={0x100cd0: [0x1ffff000, 1, after], 0x84040: [0, mailbox]})
for g915, g918 in ((0, 0), (1, 1)):
full(g915=g915, g918=g918)
# Service variants seen through the whole run.
for changes in (dict(lsfm=1), dict(fs7=1), dict(fs13=1), dict(ia=0x56), dict(ba=0x56),
dict(ix=0x2f, bx=3), dict(f3a6=1), dict(f3b4=1), dict(g917=1)):
full(**changes)
full(reads={0x84624: [0xffffff7f]})
full(reads={0x200: [0x1fec5ff1]})
# The range writes.
vpr_write(0, value=0x123456789abcdef)
vpr_write(0, value=0xffffffffffffffff)
for bytes_30, first, value in itertools.product(
(0, 1, 0x1000, 0x100000, 0x123456789), (0, 0x1ffff00000, 0xfffffffffffff000, 0x1234567),
(0, 0x15, 0x8000000012345678)):
vpr_write(1, bytes_30, first, value)
out = ROOT / "target/gpu-stage"
out.mkdir(parents=True, exist_ok=True)
oracle = "\n".join(rows) + "\n"
(out / "original-nvdec-hs-run.txt").write_text(oracle)
report = {
"original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2,
"binders_executed": binders,
"executed": sorted(executed - set(binders)),
"setup_functions": sorted(set(SETUP) - set(binders)),
"fixture_supplied": list(HOOKED) + ["os_mem_copy"] + sorted(
name for key, (name, unused) in hooks.items() if isinstance(key, int) and key >= SERVICE),
"bindings": bound, "properties": properties,
"falcon_after_original_configuration": {
key: (value if isinstance(value, str) else f"{value:#x}")
for key, value in configured.items()},
"stored_by_harness": {
"OBJBSP vtables (as creator _nv005713rm)": {f"{offset:#x}": f".rodata+{table:#x}"
for offset, table in NVDEC_VTABLES},
"OBJBSP interface pointers (as creator _nv005713rm)": {
f"{offset:#x}": f"OBJBSP+{target:#x}" for offset, target in NVDEC_SELF},
"OBJBSP+0x100": "Falcon object",
"OBJBSP+0xf8": "1 (interface byte +10; inferred, see the validation report)",
"GPU+0xde0": f"{BOOT0:#x}", "GPU+0xcac": "0x20", "GPU+0x1e20": "GPU",
"GPU children": "1ea0 OBJMC, 1ec0 MemorySystem, 2080 OBJBSP, 20c8, 2120 PMU class, "
"2308 OBJSEC2, 2310, 2318 OBJACR, 1e78"},
"cases": len(rows), "cases_by_kind": {
"predicate (P)": counts["P"], "falcon +220 (S)": counts["S"], "run (F)": counts["F"],
"range write (V)": counts["V"]},
"oracle": "target/gpu-stage/original-nvdec-hs-run.txt",
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
"physical_transcript": {
"file": "target/trace/b3-bar0.txt", "lines": [139267, 140316],
"accesses": len(expected), "sha256_of_normalised_lines": PHYSICAL_SHA256,
"reproduced_by_original_instructions": True},
"scope": "Original GV100 NVDEC HS Falcon step with binder-selected function tables. "
"Register values, time, the FIFO translation and the other listed services are "
"fixture-supplied; no hardware executed."}
(ROOT / "reports/gpu-nvdec-hs-run-original.json").write_text(json.dumps(report, indent=2) + "\n")
print(f"{len(rows)} original NVDEC HS run cases " + str(counts) + f"; executed "
f"{len(executed)} functions; physical lines 139267-140316 reproduced; "
f"oracle sha256 {report['oracle_sha256']}")