#!/usr/bin/env python3 """Run the original GV100 host interrupt table builder and write an oracle. Executed original code: the builder _nv037657rm (Intr slot 0x310), the slot functions it calls -- _nv037672rm (0x318), _nv037647rm (0x330) and _nv037665rm (0x338), which reaches _nv037659rm through slot 0x308 -- the binders _nv005896rm, _nv005897rm and _nv005787rm that populate the object, the vector helpers of the table at Intr+0x430, the GPU accessors _nv030544rm and _nv030608rm and, in the physical case, the KernelFifo engine lookup _nv040256rm over the original constructor's 16 records. Allocation, the engine count, the engine lookup of the modelled cases, engine objects and their class cast, the per-engine vector providers (_nv013840rm), the Intr slots 0x2e0 and 0x2e8, slot 0x190 of the object at GPU+0x1ea0 and diagnostics are fixture-supplied. 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, RODATA, DATA, STACK, STOP = 0x10000000, 0x20000000, 0x30000000, 0x40000000, 0x50000000 OTHER, HEAP = 0x60000000, 0x70000000 GPU, INTR, KFIFO, KERNEL = DATA, DATA + 0x10000, DATA + 0x13000, DATA + 0x14000 OBJECTS, SERVICES, CHIP, RM = DATA + 0x15000, DATA + 0x16000, DATA + 0x3c000, DATA + 0x3d000 ENGINE_TABLE = DATA + 0x18000 INDIRECT, INDIRECT_R8, ALLOCATOR = STOP + 0x100, STOP + 0x180, STOP + 0x400 SLOT_190, LOOKUP, ROUTE_2E0, ROUTE_2E8, SUBCONTEXTS = (STOP + 0x200 + 0x10 * n for n in range(5)) TABLE, SERVICE_VECTOR = INTR + 0x430, INTR + 0x15e8 MODULE_CONTAINER = 0xef90c6e FIXED = (0x4d3bf60, 0x4d3c050) NONE = 0xffffffff CONTAINER = ("_nv060217rm", "_nv060218rm", "_nv060220rm", "_nv060221rm", "_nv060222rm", "_nv060223rm", "_nv060225rm", "_nv060226rm", "_nv060228rm", "_nv016421rm") # The binders only store pointers, so the functions they select are listed. ROOTS = ("_nv005896rm", "_nv005897rm", "_nv005787rm", "_nv037657rm", "_nv037672rm", "_nv037647rm", "_nv037665rm", "_nv037659rm", "_nv040256rm") + CONTAINER HOOKED = ("_nv052691rm", "_nv015312rm", "_nv015317rm", "_nv031426rm", "_nv040331rm", "_nv052687rm", "_nv052701rm", "_nv052705rm", "_nv052695rm", "_nv030522rm", "_nv005245rm", "_nv013840rm", "_nv047869rm", "_nv046745rm", "_nv046747rm") # Entered and traced, then executed. OBSERVED = ("_nv060222rm", "_nv060217rm", "_nv060221rm", "_nv060228rm", "_nv040256rm") EXPECTED_SLOTS = {0x308: "_nv037659rm", 0x310: "_nv037657rm", 0x318: "_nv037672rm", 0x330: "_nv037647rm", 0x338: "_nv037665rm"} PROPERTIES = (0x3f1, 0x3f2, 0x3f3, 0x1569) # Descriptor classes whose GV100 stall provider reads column 7 of the # engine's own record (reports/gv100-stall-provider-validation.json). PROVIDER_CLASSES = (0xd334df, 0x793ceb, 0xe97b6c, 0x8f99e1, 0x28c408) 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} text = bytearray(elf.get_section_by_name(".text").data()) rodata = elf.get_section_by_name(".rodata").data() text_section, rodata_section = (elf.get_section_index(name) for name in (".text", ".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()) offsets = [entry[0] for entry in relocations] names = {symbol[0]: symbol[2] for symbol in starts} executed, pending, fixed_bounds = [], 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.name == "__x86_indirect_thunk_r8": address = INDIRECT_R8 elif target["st_shndx"] == rodata_section: address = RODATA + target["st_value"] if name == "_nv037647rm": fixed_bounds.append(target["st_value"] + addend) elif target["st_shndx"] == text_section: 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 = names.get(target["st_value"] + addend + 4) if callee and callee not in HOOKED: pending.append(callee) else: # Class descriptors are only passed to the hooked class cast. address = OTHER + target["st_value"] value = address + addend if kind in (2, 4): value -= TEXT + offset else: assert kind == 11, kind struct.pack_into("> 16:x}:{shown}") elif name == "_nv046745rm": if not from_container(emu): trace.append(f"a:{low_a:x}") elif name == "_nv046747rm": trace.append(f"as:{low_b:x}:{low_a:x}") else: raise AssertionError(f"{name} must not be reached") leave(emu, value) return run def observe(name): def run(emu, address, size, unused): a, b, c, d, e = (emu.reg_read(register) for register in ( UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX, UC_X86_REG_RCX, UC_X86_REG_R8)) if name == "_nv060222rm": assert (a, b) == (TABLE, ALLOCATOR) trace.append(f"ti:{c & 0xffffffff:x}:{d & 0xffffffff:x}") if case["init"]: leave(emu, case["init"]) elif name == "_nv060217rm": if a == SERVICE_VECTOR: handle = (struct.unpack("> 8 in PROVIDER_CLASSES for record in records[:15]) physical_engines = tuple((0, 0, 0, 0, 2, 0, 0, 0, 0, 1, record[7]) for record in records[:15]) for flags in (1, 2, 3): status, result = run("V", flags, engines=physical_engines, table=physical_table) assert status == 0 if flags == 3: physical_rows = [{"index": index, "engine": result[4 * index] & 0xffff, "pmc_mask": f"{result[4 * index + 1]:#010x}", "nonstall_vector": f"{result[4 * index + 2]:#x}", "stall_vector": f"{result[4 * index + 3]:#x}", "source": "fixed" if index < 15 else bytes(physical_table[(index - 15) * 100 + 84:(index - 14) * 100]) .split(b"\0")[0].decode()} for index in range(len(result) // 4)] assert all(result[4 * (15 + index)] == records[index][8] for index in range(15)) missing = sorted(set(MARKS.values()) - set(branches)) assert not missing, missing out = ROOT / "target/gpu-stage" out.mkdir(parents=True, exist_ok=True) oracle = "\n".join(rows) + "\n" (out / "original-intr-table.txt").write_text(oracle) kinds = {"T": "builder preconditions, original fixed rows", "R": "fixed rows replaced in the emulated .rodata", "D": "modelled engines", "V": "physical V100 engine records"} report = {"original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "executed": sorted(executed), "fixture_supplied": list(HOOKED) + [ "GPU+0x1ea0 slot 0x190", "KernelFifo+0x1b8 in the modelled cases", "Intr+0x2e0", "Intr+0x2e8", "memory copy and fill are performed as such"], "observed_then_executed": list(OBSERVED), "bindings": bound, "hal_properties": properties, "fixed_table_rodata": [hex(offset) for offset in FIXED], "fixed_rows": [list(row) for row in struct.iter_unpack("