#!/usr/bin/env python3 """Run the original GV100 OBJMC engine reset (HAL slot 0x130) and write an oracle. Executed original code: the reset body _nv043609rm, the GV100 HAL binder _nv006001rm that populates the OBJMC function table, the OBJMC functions the reset reaches through that table (_nv043605rm at +138, _nv043583rm at +140, _nv043623rm at +148), the engine-list helpers _nv017899rm and _nv017901rm, _nv017895rm which builds the all-engines list the first-open wrapper passes, the interrupt-enable snapshot _nv037727rm with the GV100 Intr functions at +3a0 (_nv037574rm) and +150 (_nv037527rm), the GV100 OBJBIF functions at +608 (_nv017686rm) and +610 (_nv017682rm), and the GPU-object configuration accessors they call at GPU+208 (_nv031264rm), GPU+210 (_nv031094rm) and GPU+218 (_nv031092rm). Only the OBJMC binder is run. The function pointers of the other objects are stored by this harness and checked against reports/gv100-engine-hal-bindings.json. Fixture-supplied services: register read _nv053907rm and write _nv053912rm, the delay _nv049598rm, diagnostics, the FIFO lookup _nv030608rm with the FIFO function at +1b8 that translates a CE engine descriptor to its reset bit, the timer function at +180 of the GPU+1f60 object (GV100 _nv059553rm) and the bus function at +220 of the GPU+1f78 object (GV100 _nv018640rm, which has its own oracle in check_gpu_bus_stage_binary.py). No hardware is involved. """ import bisect from _binary_path import resolve_580_binary 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_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 = resolve_580_binary() DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" TEXT, DATA, STACK, STOP = 0x10000000, 0x30000000, 0x40000000, 0x50000000 (GPU, MC, CHILD, CHIP, RM, BIF, KERNEL_BIF, BUS, TIMER, INTR, PMU, FIFO, LIST) = ( DATA, DATA + 0x10000, DATA + 0x20000, DATA + 0x30000, DATA + 0x31000, DATA + 0x32000, DATA + 0x33000, DATA + 0x36000, DATA + 0x37000, DATA + 0x38000, DATA + 0x3a000, DATA + 0x3b000, DATA + 0x3c000) INDIRECT, FIFO_1B8, TIMER_180, BUS_220 = STOP + 0x100, STOP + 0x200, STOP + 0x210, STOP + 0x220 FILL = 0x77777777 BITS = 0xb3 # The binder only stores pointers, so the functions it selects are listed. ROOTS = ("_nv043609rm", "_nv006001rm", "_nv043605rm", "_nv043583rm", "_nv043623rm", "_nv017895rm", "_nv017686rm", "_nv017682rm", "_nv031264rm", "_nv031094rm", "_nv031092rm", "_nv037574rm", "_nv037527rm") HOOKED = ("_nv053907rm", "_nv053912rm", "_nv049598rm", "_nv047869rm", "_nv046747rm", "_nv046745rm", "_nv030608rm", "_nv052705rm") # Object, slot, function: stored by the harness, taken from the bindings report. FOREIGN = (("gpu_object", GPU, 0x208, "_nv031264rm"), ("gpu_object", GPU, 0x210, "_nv031094rm"), ("gpu_object", GPU, 0x218, "_nv031092rm"), ("OBJBIF", BIF, 0x608, "_nv017686rm"), ("OBJBIF", BIF, 0x610, "_nv017682rm"), ("Intr", INTR, 0x3a0, "_nv037574rm"), ("Intr", INTR, 0x150, "_nv037527rm")) SERVICES = {"OBJTMR": ("0x180", "_nv059553rm"), "OBJBUS": ("0x220", "_nv018640rm")} assert hashlib.sha256(ORIGINAL.read_bytes()).hexdigest() == DIGEST bindings = json.loads((ROOT / "reports/gv100-engine-hal-bindings.json").read_text()) assert bindings["original_sha256"] == DIGEST tables = {entry["class_label"]: entry["hal_slots"] for entry in bindings["classes"]} tables["gpu_object"] = bindings["gpu_object"]["hal_slots"] for label, unused, slot, function in FOREIGN: assert tables[label][f"{slot:#x}"]["function"] == function, (label, slot) for label, (slot, function) in SERVICES.items(): assert tables[label][slot]["function"] == function, (label, slot) 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()) text_section = elf.get_section_index(".text") 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()) offsets = [entry[0] for entry in relocations] entries = {symbol[0]: symbol[2] 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 else: assert target["st_shndx"] == text_section, (name, target.name) 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("> 64 & (1 << 64) - 1, value >> 128) # Physical first-open sequence of the original driver on a V100 (BAR0 offsets). PHYSICAL = ["r:88004:100506", "w:88004:100502", "r:200:42001120", "w:200:42000020", "w:200:42001120", "r:200:42001120", "r:140:0", "w:200:bfffffff", "r:200:1fecdff1", "w:88004:100506"] observed = run(ALL, reads=(0x100506, 0x42001120, 0x42001120, 0, 0x1fecdff1)) assert [entry for entry in observed if entry[0] in "rw"] == PHYSICAL, observed assert observed[-1] == "b220" # Full reset: every one of the 0xb3 engine bits is set. CHILDREN = ((0, 0), (1, 0), (1, 1)) VALUES = (0x42001120, 0, 0xffffffff, 0x1fecdff1, 0x00101100) for (child, byte_190a), gpu_52fc, gpu_6d29, (pmu, byte_7f9) in itertools.product( CHILDREN, (0, 1), (0, 1), CHILDREN): for first, second, third in itertools.product(VALUES, (0x42001120, 0xffefeeff), (0x1fecdff1, 0x5fecdff1)): run(ALL, delay=7, child=child, byte_190a=byte_190a, gpu_52fc=gpu_52fc, gpu_6d29=gpu_6d29, pmu=pmu, byte_7f9=byte_7f9, gpu_96c=first & 1, reads=(0x100506, first, second, 0x200, third)) # The interrupt-enable snapshot; GPU+69e4 equal to 3 skips the register read. for gpu_69e4, gpu_dec, enable in itertools.product( (0, 2, 3, 4), (0, 0x200, 0x7fffffff, 0x80000000, 0xffffffff), (0, 1, 0x200, 0x7fffffff, 0x80000000, 0x80000200, 0xffffffff)): skipped = gpu_69e4 == 3 run(ALL, gpu_69e4=gpu_69e4, gpu_dec=gpu_dec, pmu=1, byte_7f9=1, reads=(0x100506, 0x42001120, 0x42001120) + (() if skipped else (enable,)) + (0x1fecdff1,)) # Saved configuration word, ignored service results, bits above the list length. for command, service_status, high in itertools.product( (0, 4, 0x100502, 0x100506, 0xffffffff), (0, 0x56), (0, 1)): engines = (ALL[0], ALL[1], ALL[2] | (0x1fff << 51 if high else 0)) run(engines, child=1, service_status=service_status, reads=(command, 0x42001120, 0x42001120, 0, 0x1fecdff1)) # Partial reset: at least one of the 0xb3 bits is clear. for selected in itertools.product((0, 1), repeat=4): chosen = [engine for engine, wanted in zip((0x54, 4, 2, 0xd), selected) if wanted] for (child, byte_190a), first, delay in itertools.product( CHILDREN, (0x42001120, 0xffffffff, 0, 0x1fecdff1), (0, 3, 0x00418938)): run(bits(*chosen), delay=delay, child=child, byte_190a=byte_190a, gpu_96c=delay & 1, reads=(0x100506, first)) # CE engines 0xf to 0x22 ask the FIFO for their reset bit. for instance, (base, step), first in itertools.product( range(20), ((0, 1), (0x25, 3), (0xffffffe0, 1)), (0xffffffff, 0)): run(bits(0xf + instance), fifo_base=base, fifo_step=step, reads=(0x100506, first)) for fail, status, first in itertools.product((0, 1, 7, 19), (0x56, 0xffff), (0xffffffff, 0, 0x42001120)): run(bits(*range(0xf, 0x23)), fifo_fail=fail, fifo_status=status, fifo_base=1, reads=(0x100506, first)) run(bits(2, 4, 0xd, 0x54, 0x10, 0x11 + fail), child=1, fifo_fail=0x11 + fail - 0xf, fifo_status=status, fifo_base=9, fifo_step=5, reads=(0x100506, first)) # Lists that miss a single engine take the partial path with everything else. for missing, (child, byte_190a), first, service_status in itertools.product( (0, 2, 4, 0xd, 0xe, 0xf, 0x22, 0x23, 0x54, 0x80, 0xb2), CHILDREN, (0x42001120, 0xffffffff, 0), (0, 0x56)): engines = tuple(value & ~mask for value, mask in zip(ALL, bits(missing))) run(engines, delay=1, child=child, byte_190a=byte_190a, fifo_base=3, fifo_step=7, service_status=service_status, reads=(0x100506, first)) # Engines the routine does not look at, and the empty list. for engines in (bits(), bits(0), bits(1), bits(3), bits(0xe), bits(0x23), bits(0x53), bits(0x55), bits(0xb2), (0, 0, 0x1fff << 51)): for first in (0x42001120, 0xffffffff): run(engines, delay=2, child=1, reads=(0x100506, first)) assert paths["full"] and paths["partial"], paths out = ROOT / "target/gpu-stage" out.mkdir(parents=True, exist_ok=True) oracle = "\n".join(rows) + "\n" (out / "original-mc-reset.txt").write_text(oracle) report = { "original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "executed": sorted(executed), "fixture_supplied": list(HOOKED) + [ "FIFO function at +1b8 (CE engine descriptor to reset bit)", "timer function at +180 of the GPU+1f60 object (GV100 _nv059553rm)", "bus function at +220 of the GPU+1f78 object (GV100 _nv018640rm)", "memory fill is performed as such"], "bindings": bound, "pointers_stored_by_harness": {f"{label}+{slot:#x}": function for label, unused, slot, function in FOREIGN}, "cases": len(rows), "full_reset_cases": paths["full"], "partial_reset_cases": paths["partial"], "oracle": "target/gpu-stage/original-mc-reset.txt", "oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(), "physical_sequence_reproduced": PHYSICAL, "scope": "Original GV100 OBJMC engine reset with the binder-selected function table. " "Register values, delays, the FIFO translation, the timer delay, the bus " "function and diagnostics are fixture-supplied; no hardware executed."} (ROOT / "reports/gpu-mc-reset-original.json").write_text(json.dumps(report, indent=2) + "\n") print(f"{len(rows)} original MC reset cases ({paths['full']} full, {paths['partial']} partial); " f"executed {len(executed)} functions")