#!/usr/bin/env python3 """Run the original producers of the NVDEC HS run's inputs and write an oracle. The NVDEC HS run (core gpu::nvdec_hs_run, tools/check_gpu_nvdec_hs_run_binary.py) takes several answers from its fixture. This harness executes the original code that produces four of them, so the kernel can supply them without constants borrowed from a test: X KernelFifo+1b8 (_nv040256rm with _nv040255rm) over the 16 engine records the original constructor builds from the physical V100 device-info table (reports/engine-cache-binary.json, physical_gv100.table_bytes): every input type 0..14, 16 and 17 with every value a record holds in that column plus a missing one, and every output type 0..14. The NVDEC run asks (0, 8) for the engine descriptor and (8, 6) for the OBJMC engine index. B The OBJBSP object as its object-create routine _nv006624rm builds it: the creator _nv005713rm and the constructor _nv003356rm on zeroed storage, with the interface constructor _nv003365rm, which stores the interface byte +10 (OBJBSP+f8) and tail-calls the Falcon object creation _nv028792rm, and the property initializer _nv005811rm. C The NVDEC engine constructor _nv046468rm (OBJBSP+1c8): the registry values RmDisableBsp, RmDisableBspLoad and RMForceNvdecUcodeEncryption, the engine descriptor and the bytes it stores. S _nv031478rm (gpu, 0, 0, begin, name) with _nv031447rm and _nv031395rm: whether it touches the thread state, by GPU+cac and the manager's device. Fixture-supplied: diagnostics, the registry read _nv049786rm, the Falcon object creation _nv028792rm (recorded, returns zero), the Falcon configuration _nv028412rm (recorded), the base constructors _nv003389rm and _nv037808rm (recorded, return zero), the system object with its manager and device table, the device's mask _nv031338rm, the thread state _nv059477rm and the KernelFifo function at +170. No hardware is involved. """ import bisect import hashlib import itertools import json from pathlib import Path import struct from elftools.elf.elffile import ELFFile from unicorn import UC_ARCH_X86, UC_HOOK_CODE, UC_HOOK_MEM_WRITE, UC_MODE_64, Uc from unicorn.x86_const import (UC_X86_REG_R8, UC_X86_REG_R9, UC_X86_REG_RAX, 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" TEXT, RODATA, DATA, STACK, STOP = 0x10000000, 0x20000000, 0x30000000, 0x40000000, 0x50000000 OTHER = 0x60000000 (GPU, KFIFO, TABLE, NVDEC, FLCN, CHIP, RM, THREAD, OUT, SYSTEM, MANAGER, DEVICE) = ( DATA, DATA + 0x10000, DATA + 0x12000, DATA + 0x14000, DATA + 0x16000, DATA + 0x18000, DATA + 0x19000, DATA + 0x1a000, DATA + 0x1b000, DATA + 0x1c000, DATA + 0x1d000, DATA + 0x1f000) INDIRECT, KFIFO_170 = STOP + 0x100, STOP + 0x200 OBJECT_BYTES = 0x4f8 ROOTS = ("_nv040256rm", "_nv005713rm", "_nv003356rm", "_nv046468rm", "_nv031478rm") HOOKED = ("_nv047869rm", "_nv046745rm", "_nv046747rm", "_nv049786rm", "_nv028792rm", "_nv028412rm", "_nv003389rm", "_nv037808rm", "_nv031338rm", "_nv059477rm", "_nv047868rm") KEYS = ("RmDisableBsp", "RmDisableBspLoad", "RMForceNvdecUcodeEncryption") assert hashlib.sha256(ORIGINAL.read_bytes()).hexdigest() == DIGEST physical = json.loads((ROOT / "reports/engine-cache-binary.json").read_text()) assert physical["artifact_sha256"] == DIGEST physical = physical["physical_gv100"] table = bytes(physical["table_bytes"]) assert len(table) == 100 * physical["record_count"] == 1600 bindings = json.loads((ROOT / "reports/gv100-engine-hal-bindings.json").read_text()) assert bindings["original_sha256"] == DIGEST nvdec_class = next(entry for entry in bindings["classes"] if entry["class_label"] == "OBJBSP") assert nvdec_class["object_creators"] == ["_nv005713rm"] # Since the 2026-11-19 correction the report also lists the interface # constructors _nv003365rm and _nv003371rm, which section B runs here itself. assert "_nv005811rm" in nvdec_class["property_binders"] assert nvdec_class["argument_conventions"]["_nv005811rm"]["order"] == "chip, RM" assert {offset: nvdec_class["hal_properties"][offset]["value"] for offset in ("0xf8", "0xfa", "0xfb")} == {"0xf8": "0x1", "0xfa": "0x0", "0xfb": "0x1"} 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} text = bytearray(elf.get_section_by_name(".text").data()) rodata = elf.get_section_by_name(".rodata").data() text_section = elf.get_section_index(".text") rodata_section = elf.get_section_index(".rodata") starts = sorted((symbol["st_value"], symbol["st_size"], symbol.name) for symbol in symbols if symbol["st_shndx"] == text_section and symbol["st_size"]) relocations = sorted((relocation["r_offset"], relocation["r_info_type"], relocation["r_info_sym"], relocation["r_addend"]) for relocation in elf.get_section_by_name(".rela.text").iter_relocations()) def other(symbol): section = symbol["st_shndx"] return OTHER + ((section if isinstance(section, int) else 0xff) << 24) + symbol["st_value"] offsets = [entry[0] for entry in relocations] entries = {symbol[0]: symbol[2] for symbol in starts} bounds = [symbol[0] for symbol in starts] executed, pending = [], list(ROOTS) while pending: name = pending.pop() if name in executed or name in HOOKED: continue executed.append(name) start, size = by_name[name]["st_value"], by_name[name]["st_size"] for offset, kind, index, addend in relocations[bisect.bisect_left(offsets, start): bisect.bisect_left(offsets, start + size)]: target = symbols[index] if target.name == "__x86_indirect_thunk_rax": address = INDIRECT elif target["st_shndx"] == rodata_section: address = RODATA + target["st_value"] elif target["st_shndx"] != text_section: # Data of other sections is not mapped, except the page of the # system-object pointer; any other access would stop the run. address = other(target) else: address = TEXT + target["st_value"] # Follow direct calls and tail calls only, not stored pointers. if kind in (2, 4) and text[offset - 1] in (0xe8, 0xe9): callee = entries.get(target["st_value"] + addend + 4) if callee and callee not in HOOKED: pending.append(callee) value = address + addend if kind in (2, 4): value -= TEXT + offset else: assert kind == 11, kind struct.pack_into(" 0x1f or mask is None: assert trace == [] and word(THREAD + 0x3c) == depth, (cac, trace) sections += 1 out = ROOT / "target/gpu-stage" out.mkdir(parents=True, exist_ok=True) oracle = "\n".join(rows) + "\n" (out / "original-nvdec-hs-inputs.txt").write_text(oracle) counts = {kind: sum(1 for row in rows if row[0] == kind) for kind in "XBCS"} report = { "original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "executed": sorted(executed), "fixture_supplied": list(HOOKED) + ["KernelFifo function at +170"], "cases": counts, "total_cases": sum(counts.values()), "oracle": "target/gpu-stage/original-nvdec-hs-inputs.txt", "oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(), "engine_lookup": { "function": "KernelFifo+1b8 = _nv040256rm, body _nv040255rm", "table": "reports/engine-cache-binary.json physical_gv100.table_bytes (the original " "constructor's 16 records for 0000:b3:00.0)", "physical_answers": physical_answers}, "object_creation": { "chain": "_nv006624rm clears 0x4f8 bytes and calls _nv005713rm then _nv003356rm; " "_nv003356rm calls _nv003365rm on OBJBSP+e8, which stores byte +10 = 1 for " "every chip and tail-calls _nv028792rm (Falcon object creation, which stores " "the Falcon at interface +18); _nv003356rm ends with _nv005811rm", "passes": constructed}, "engine_constructor": { "function": "_nv046468rm", "registry_keys": list(KEYS), "effect": "RmDisableBsp read as 1 stores OBJBSP+50 = 1; RmDisableBspLoad read as 1 " "stores +420 = 1; RMForceNvdecUcodeEncryption read stores +421 = (value != 0); " "Falcon+5ec = 0x8f99e100 | instance, Falcon+57c = instance + 0xe, " "Falcon+3a2 = OBJBSP+fb; then _nv028412rm; +448 = 0; status 0"}, "section": { "function": "_nv031478rm with _nv031447rm and _nv031395rm", "effect": "_nv031395rm returns zero for GPU+cac above 0x1f or an empty device slot " "of the manager (system object +1e0, table at +970), otherwise the device's " "mask (_nv031338rm). With at most one bit set _nv031478rm returns without " "touching the thread state; otherwise it increments or decrements the dword " "at +3c of the thread state _nv059477rm returns."}, "scope": "Original producers of four inputs of the NVDEC HS run. Registry answers, the " "manager's device table and the thread state are fixture-supplied; no hardware " "executed."} (ROOT / "reports/gpu-nvdec-hs-inputs-original.json").write_text(json.dumps(report, indent=2) + "\n") print(f"{sum(counts.values())} original cases {counts}; physical answers {physical_answers}; " f"executed {len(executed)} functions")