#!/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("= 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("= 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']}")