#!/usr/bin/env python3 """Run the original GV100 OBJFIFO StateLoad and write an oracle. Executed original code. The whole object file is loaded and relocated, so no callee is selected by hand; the KernelFifo, OBJFIFO, OBJMC and GPU-object function tables come from running their original binders and property initializers with chip HAL 0x23 and RM HAL 2. preparation the engine table constructor behind KernelFifo+1a8 (_nv040242rm -> OBJFIFO+2f0 _nv027760rm) on the device-info registers of a physical V100; the channel-id manager _nv040227rm; the OBJFIFO constructor _nv027761rm (registry keys are case inputs) with _nv028163rm; the scheduler manager object (_nv006686rm) and the OBJFIFO InitLocked step _nv028169rm -> _nv055508rm, which creates one owner object per runlist (_nv055507rm, class selection _nv055499rm, object creation _nv006057rm, owner initialization through vtable +30) and allocates its two buffers (_nv015974rm -> _nv040400rm, size and alignment _nv040339rm). load OBJFIFO StateLoad _nv028309rm -> _nv028308rm: +710, the reset _nv028145rm -> _nv028143rm -> _nv043611rm -> OBJMC+130 _nv043609rm (+138, OBJBIF+608/+610 through the configuration accessors GPU+208/+210/+218), the channel-id manager loop (_nv040234rm, +608), OBJBIF+318 _nv017752rm, the MIGManager function at +168, _nv028025rm (+660 _nv027982rm with +630 _nv027874rm, +668, +1d8 _nv027979rm, +1d0, +128 _nv027662rm -> _nv030171rm, +540, +118 _nv027681rm -> _nv030976rm and GPU+5c8 _nv030977rm with the table getters GPU+740/+748/+750/+768, +150, +140), +160 _nv027753rm (_nv027770rm -> _nv027771rm per engine: busy check _nv027871rm with +3e8 _nv027923rm and +400 _nv027859rm, restriction _nv040361rm, +2d8 _nv028277rm with _nv027655rm, _nv028181rm with the owner lookup _nv055498rm, the owner's submit _nv004422rm -> _nv055557rm -> _nv015976rm and _nv015977rm -> +3b0 _nv028192rm -> _nv028190rm -> +3b8 _nv028203rm, and the preempt _nv028101rm with +1e8 _nv028063rm and +1e0 _nv028344rm; then the 0x2a0c write and _nv027803rm -> _nv027804rm), +640 _nv027660rm, +508 _nv027665rm, +518 _nv027667rm, +500, +510, +780. after KernelFifo+208 _nv040417rm with the arguments (1, 0) the early caller _nv028064rm passes; its own argument variations. Fixture-supplied: register access (_nv053907rm, _nv053912rm), the delay _nv049598rm, timeout start and check (_nv059509rm, _nv059505rm), diagnostics, the allocator, fill and copy, locks, registry lookups, the multi-GPU helpers, the object-model ancestor lookup, the framebuffer allocation _nv043691rm of the runlist buffers (the address is a case input; the request is recorded), and the cross-object services: the timer function at +180 of the GPU+1f60 object, the bus function at +220 of the GPU+1f78 object (it has its own oracle in tools/check_gpu_bus_stage_binary.py), the clock-gating feature query and record (_nv031034rm, _nv031133rm), the PMU mutex of the LPWR reference (_nv031518rm, _nv031522rm) with the engine disable _nv042848rm, the recovery service _nv037737rm of the preempt wait, the timer registrations of the scheduler manager and the write-combine flush. No hardware is involved. """ import bisect from _binary_path import resolve_580_binary import hashlib import json from pathlib import Path import struct import sys import warnings warnings.filterwarnings("ignore", category=UserWarning) from elftools.elf.elffile import ELFFile # noqa: E402 from unicorn import UC_ARCH_X86, UC_HOOK_CODE, UC_MODE_64, Uc, UcError # noqa: E402 from unicorn.x86_const import (UC_X86_REG_R8, UC_X86_REG_R9, UC_X86_REG_R10, # noqa: E402 UC_X86_REG_R11, UC_X86_REG_R12, UC_X86_REG_R13, UC_X86_REG_R14, UC_X86_REG_R15, UC_X86_REG_RAX, UC_X86_REG_RBP, UC_X86_REG_RBX, 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" TRANSCRIPT = ROOT / "target/trace/b3-bar0.txt" FIRST_LINE, LAST_LINE = 140621, 140934 BASES = {".text": 0x10000000, ".data": 0x20000000, ".rodata": 0x30000000, ".bss": 0x38000000} EXT, HEAP, OBJ, STACK, STOP = 0x08000000, 0x40000000, 0x48000000, 0x4c000000, 0x4d000000 HEAP_SIZE, OBJ_SIZE = 0x400000, 0x120000 (GPU, KFIFO, OFIFO, MC, BIF, KBIF, OBUS, TMR, INTR, LPWR, CHIP, RM, MIG, PGCTRL, CHILD, TABLE, MEMMGR) = (OBJ + 0x10000 * index for index in range(17)) CONTINUE, TIMER_SLOT, BUS_220, LOCK = STOP + 0x100, STOP + 0x200, STOP + 0x210, STOP + 0x800 ARGS = (UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX, UC_X86_REG_RCX, UC_X86_REG_R8, UC_X86_REG_R9) THUNKS = {"rax": UC_X86_REG_RAX, "rbx": UC_X86_REG_RBX, "rcx": UC_X86_REG_RCX, "rdx": UC_X86_REG_RDX, "rsi": UC_X86_REG_RSI, "rdi": UC_X86_REG_RDI, "r8": UC_X86_REG_R8, "r9": UC_X86_REG_R9, "r10": UC_X86_REG_R10, "r11": UC_X86_REG_R11, "r12": UC_X86_REG_R12, "r13": UC_X86_REG_R13, "r14": UC_X86_REG_R14, "r15": UC_X86_REG_R15} BINDERS = (("_nv005912rm", KFIFO), ("_nv005792rm", KFIFO), ("_nv005970rm", OFIFO), ("_nv005971rm", OFIFO), ("_nv005972rm", OFIFO), ("_nv005820rm", OFIFO), ("_nv006001rm", MC)) GPU_BINDERS = ("_nv005978rm", "_nv005980rm", "_nv005982rm", "_nv005984rm") # Slots the load dispatches through, checked against the bindings report. EXPECTED = { "OBJFIFO": {"0x118": "_nv027681rm", "0x128": "_nv027662rm", "0x140": "_nv028171rm", "0x150": "_nv028320rm", "0x160": "_nv027753rm", "0x1d0": "_nv027977rm", "0x1d8": "_nv027979rm", "0x1e0": "_nv028344rm", "0x1e8": "_nv028063rm", "0x2d8": "_nv028277rm", "0x2f0": "_nv027760rm", "0x3a8": "_nv027888rm", "0x3b0": "_nv028192rm", "0x3b8": "_nv028203rm", "0x3e8": "_nv027923rm", "0x3f0": "_nv028082rm", "0x3f8": "_nv028084rm", "0x400": "_nv027859rm", "0x500": "_nv027657rm", "0x508": "_nv027665rm", "0x510": "_nv028161rm", "0x518": "_nv027667rm", "0x540": "_nv027981rm", "0x5c0": "_nv027678rm", "0x5c8": "_nv027674rm", "0x5d0": "_nv027672rm", "0x5d8": "_nv027676rm", "0x608": "_nv028104rm", "0x630": "_nv027874rm", "0x640": "_nv027660rm", "0x660": "_nv027982rm", "0x668": "_nv027972rm", "0x710": "_nv028136rm", "0x780": "_nv028288rm"}, "KernelFifo": {"0x100": "_nv040425rm", "0x118": "_nv040413rm", "0x1a8": "_nv040242rm", "0x1b8": "_nv040256rm", "0x1f0": "_nv040410rm", "0x1f8": "_nv040407rm", "0x200": "_nv040404rm", "0x208": "_nv040417rm", "0x288": "_nv040335rm"}, "OBJMC": {"0x130": "_nv043609rm", "0x138": "_nv043605rm"}, } # Stored by the harness from the bindings report: only these functions of the objects are used. FOREIGN = (("OBJBIF", BIF, 0x318, "_nv017752rm"), ("OBJBIF", BIF, 0x608, "_nv017686rm"), ("OBJBIF", BIF, 0x610, "_nv017682rm"), ("MIGManager", MIG, 0x168, "_nv046163rm")) GPU_SLOTS = {0x208: "_nv031264rm", 0x210: "_nv031094rm", 0x218: "_nv031092rm", 0x5c8: "_nv030977rm", 0x740: "_nv030423rm", 0x748: "_nv030728rm", 0x750: "_nv030519rm", 0x768: "_nv030894rm"} SERVICES = {"OBJTMR": ("0x180", "_nv059553rm"), "OBJBUS": ("0x220", "_nv018640rm")} raw = ORIGINAL.read_bytes() assert hashlib.sha256(raw).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"]} for label, slots in EXPECTED.items(): for slot, function in slots.items(): assert tables[label][slot]["function"] == function, (label, slot) for label, base, 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) snapshot = json.loads((ROOT / "reports/hardware-inventory-original-b3.json").read_text())["inventory"] DEVICE_INFO = {int(key, 16): int(value, 16) for key, value in snapshot.items() if int(key, 16) == 0x2004 or 0x2390 <= int(key, 16) < 0x23d0 or 0x22700 <= int(key, 16) < 0x22800} with ORIGINAL.open("rb") as stream: elf = ELFFile(stream) index = {section.name: number for number, section in enumerate(elf.iter_sections())} sections = {name: bytearray(elf.get_section_by_name(name).data()) if name != ".bss" else bytearray(elf.get_section_by_name(name)["sh_size"]) for name in BASES} symbol_bytes = elf.get_section_by_name(".symtab").data() strings = elf.get_section_by_name(".strtab").data() relocations = {name: elf.get_section_by_name(".rela" + name).data() for name in (".text", ".data", ".rodata")} section_base = {index[name]: base for name, base in BASES.items()} symbols, by_name, externals, functions = [], {}, {}, [] for name_offset, info, other, section, value, size in struct.iter_unpack("> 32] if target is None: continue if kind == 1: struct.pack_into("= 0: start, size, name = functions[position] if start <= address < start + max(size, 1): return name return None cpu = Uc(UC_ARCH_X86, UC_MODE_64) for name, base in BASES.items(): cpu.mem_map(base, (len(sections[name]) + 0xfff) & ~0xfff) cpu.mem_write(base, bytes(sections[name])) for base, size in ((EXT, 0x10000), (HEAP, HEAP_SIZE), (OBJ, OBJ_SIZE), (STACK, 0x100000), (STOP, 0x1000)): cpu.mem_map(base, size) executed, calls = set(), [] state = {} def q(address): return struct.unpack(" 1 else values[0] else: value = state["case"]["default_read"](offset) if state["phase"] == "init" and offset != 0x2008 and "DEBUG" in sys.argv: print("init read", hex(offset), calls[-10:]) emit(f"r:{offset:x}:{value:x}") return value def write_register(a): assert (a[0], a[1] & 0xffffffff, a[2] & 0xffffffff) == (GPU + 0x4120, 0, 0) emit(f"w:{a[3] & 0xffffffff:x}:{a[4] & 0xffffffff:x}") return 0 def diagnostic(a): emit(f"e:{a[0] & 0xffffffff:x}:{a[1] & 0xffffffff:x}") return 0 def registry(a): key = text(a[1]) state["keys"].append(key) value = state["case"]["registry"].get(key) if value is None: return 0x57 wd(a[2], value) return 0 def timeout_start(a): assert a[0] == GPU + 0x1e08 emit(f"ts:{a[2] & 0xffffffff:x}:{a[3] & 0xffffffff:x}") return 0 def timeout_check(a): assert a[0] == GPU + 0x1e08 status = pop("check", 0) state["checks"] = state.get("checks", 0) + 1 assert state["checks"] < 2000, "the case never ends its wait" emit(f"tc:{a[2] & 0xffffffff:x}:{status:x}") return status def framebuffer_allocate(a): """The runlist buffers of OBJFIFO InitLocked: the request is recorded and the descriptor is described at the case's address with the original _nv043698rm.""" descriptor = a[0] request = (q(descriptor + 0x30), q(descriptor + 0x28), d(descriptor + 0x70), d(descriptor + 0xa0), b(descriptor + 0x58) if False else 0) state["requests"].append(request[:4]) address = state["case"]["buffers"].pop(0) nested("_nv043698rm", [descriptor, 2, address, q(descriptor + 0x30)], lambda unused: 0) def feature_state(a): value = pop("feature", 0) emit(f"F:{a[1] & 0xffffffff:x}:{a[2] & 0xffffffff:x}:{value:x}") return value def record_feature(a): emit(f"G:{a[1] & 0xffffffff:x}:{a[2] & 0xffffffff:x}:{a[3] & 0xff:x}") return 0 def mutex_acquire(a): """_nv031518rm(mutex manager, 6, &token, 0).""" assert a[0] == q(GPU + 0x1f50) and a[1] & 0xffffffff == 6 status = pop("mutex", 0) emit(f"ma:{d(a[2]):x}:{status:x}") if status == 0: wd(a[2], 0x55) return status def mutex_release(a): assert a[0] == q(GPU + 0x1f50) and a[1] & 0xffffffff == 6 emit("mr") return 0 def engine_disable(a): emit(f"pd:{a[2] & 0xffffffff:x}") return 0 HANDLERS = { "_nv053907rm": read_register, "_nv053912rm": write_register, "_nv047869rm": diagnostic, "_nv047868rm": diagnostic, "_nv047867rm": diagnostic, "_nv046745rm": lambda a: emit(f"A:{a[0] & 0xffffffff:x}") or 0, "_nv046747rm": lambda a: emit(f"S:{a[0] & 0xffffffff:x}:{a[1] & 0xffffffff:x}") or 0, "_nv046744rm": lambda a: emit(f"A:{a[0] & 0xffffffff:x}") or 0, "_nv052687rm": lambda a: allocate(a[0], 0xcc), "_nv052701rm": lambda a: 0, "_nv052705rm": lambda a: cpu.mem_write(a[0], bytes([a[1] & 0xff]) * (a[2] & 0xffffffff)) or a[0], "_nv052695rm": lambda a: cpu.mem_write(a[0], bytes(cpu.mem_read(a[2], a[3] & 0xffffffff))) or a[0], "_nv052744rm": lambda a: LOCK, "_nv052743rm": lambda a: 0, "_nv052747rm": lambda a: 0, "_nv049786rm": registry, "_nv049086rm": lambda a: GPU, "_nv005245rm": lambda a: a[0] if OBJ <= a[0] < OBJ + OBJ_SIZE else None, "_nv031390rm": lambda a: 0, "_nv031395rm": lambda a: 1, "_nv031426rm": lambda a: GPU, "_nv031478rm": lambda a: 0, "_nv031482rm": lambda a: 0, "_nv049598rm": lambda a: emit(f"t:{a[0] & 0xffffffff:x}") or 0, "_nv059509rm": timeout_start, "_nv059505rm": timeout_check, "_nv037737rm": lambda a: emit("sv") or 0, "_nv043691rm": framebuffer_allocate, "_nv031034rm": feature_state, "_nv031133rm": record_feature, "_nv031518rm": mutex_acquire, "_nv031522rm": mutex_release, "_nv042848rm": engine_disable, "_nv059643rm": lambda a: 0, "os_is_vgx_hyper": lambda a: 0, "os_flush_cpu_write_combine_buffer": lambda a: emit("f") or 0, } def install(address, handler): def run(emu, pc, size, unused): value = handler([emu.reg_read(register) for register in ARGS]) if value is not None: leave(value) cpu.hook_add(UC_HOOK_CODE, run, begin=address, end=address) for name, handler in HANDLERS.items(): install(externals[name] if name in externals else by_name[name], handler) for name, address in externals.items(): if name.startswith("__x86_indirect_thunk_"): register = THUNKS[name.rsplit("_", 1)[1]] cpu.hook_add(UC_HOOK_CODE, lambda emu, pc, size, register: emu.reg_write( UC_X86_REG_RIP, emu.reg_read(register)), register, begin=address, end=address) def resume(emu, pc, size, unused): value = state["continuations"].pop()(emu.reg_read(UC_X86_REG_RAX)) if value is not None: leave(value) cpu.hook_add(UC_HOOK_CODE, resume, begin=CONTINUE, end=CONTINUE) install(TIMER_SLOT, lambda a: emit(f"d:{a[2] & 0xffffffff:x}") or 0) install(BUS_220, lambda a: emit("b220") or 0) TIMER_OTHER = STOP + 0x230 install(TIMER_OTHER, lambda a: 0) def record_function(emu, address, size, unused): name = function_at(address) if name and address == by_name.get(name): executed.add(name) calls.append(name) del calls[:-64] def unexpected(emu, address, size, unused): for name, target in externals.items(): if target == address and name not in HANDLERS and not name.startswith("__x86"): raise AssertionError(f"unexpected external {name} after {calls[-8:]}") cpu.hook_add(UC_HOOK_CODE, unexpected, begin=EXT, end=EXT + 0x10000) PRISTINE = {name: bytes(sections[name]) for name in (".data", ".bss")} TRACED_BUFFERS = [0x7fdb70000 - 0x20000 * number for number in range(26)] # Register values of the traced V100 (transcript lines 140621-140934) where they matter. TRACED_READS = {0x88004: [0x100506], 0x200: [0x1fecdff1], 0x88014: [0xc], 0x88018: [0x39a0], 0x2a04: [0x800f4240]} def default_case(): return dict(reads={key: list(value) for key, value in TRACED_READS.items()}, default_read=lambda offset: 0xeffffeff if offset & 0xfff == 0x13c else 0, registry={}, check=[], feature=[], mutex=[], buffers=list(TRACED_BUFFERS), channels=0x1000, flags=0, gpu={0x911: 1}, lpwr=None, mc=True, mig=True, overrides=[], fifo={}, drop_owner=None, restricted=[], gpu_1e0c=0x7a120, gpu_1e28=0) def construct(case): """Objects as creation, the binders, the constructors and InitLocked leave them.""" cpu.mem_write(OBJ, bytes(OBJ_SIZE)) for name, content in PRISTINE.items(): cpu.mem_write(BASES[name], content) cpu.mem_write(CHIP + 0x10, struct.pack("> 32 def pick(self, values): return values[self.word() % len(values)] def main(): hook = cpu.hook_add(UC_HOOK_CODE, record_function, begin=BASES[".text"], end=BASES[".text"] + len(sections[".text"])) # The physical V100: the Lpwr object as its constructor leaves it (reports/ # gpu-clock-gating-providers-validation.json), no LPWR engine supported yet. transcript = None v100 = dict(lpwr={}) if TRANSCRIPT.exists(): reads, tokens = transcript_case() bus = [token for token in tokens if token.split(":")[1] in ("1100", "1140", "9080", "88714")] v100["reads"] = {offset: values for offset, values in reads.items() if offset not in (0x1100, 0x9080, 0x88714)} case, status, physical, stored, requests = run("V", **v100) cpu.hook_del(hook) load_executed = set(executed) table = bytes(cpu.mem_read(q(KFIFO + 0x348), 1600)) assert status == 0 and stored == (1, 0, 0x7fffffff, 0, 0), (status, stored) assert set(requests) == {(0x20000, 0x1000, 2, 0x86)} and len(requests) == 26 assert not any(token[0] in "eAS" for token in physical) state["events"].clear() call(q(KFIFO + 0x208), GPU, KFIFO, 1, 0) assert state["events"] == ["w:2254:10000000"] if TRANSCRIPT.exists(): registers = [] for token in physical: if token == "b220": registers += bus elif token[0] in "rw" and token[1] == ":": registers.append(token) registers += state["events"] assert registers == tokens, "transcript" transcript = {"lines": f"{FIRST_LINE}-{LAST_LINE}", "register_accesses": len(tokens), "equal": True, "bus_function_accesses_taken_from_the_transcript": bus, "last_line_is": "KernelFifo+208 _nv040417rm(gpu, fifo, 1, 0), called by " "_nv028064rm after the load"} rows.insert(0, "T;" + table.hex()) random = Random(0x28309) # Load flags, missing objects, GPU bytes. for flags in (0, 1, 2, 3, 0xfffffffe, 0xffffffff): for lpwr in (None, {}): run("L", flags=flags, lpwr=lpwr) for mc in (True, False): for mig in (True, False): run("L", mc=mc, mig=mig, lpwr={}) for gpu_911 in (0, 1, 0x80): for gpu_93b in (0, 1, 0x80): run("L", gpu={0x911: gpu_911, 0x93b: gpu_93b}, lpwr={}) # The Lpwr object, its two children and the feature queries of the table loads. for lpwr in (None, dict(child=False), dict(pgctrl=False), dict(byte_183=1), dict(byte_184=1), dict(byte_183=1, byte_184=1), dict(byte_271=0, mask=4), dict(mask=4), dict(mask=1), dict(mask=1, policy=2), dict(mask=1, policy=0xfffffffd), dict(mask=5, policy=2), dict(mask=0xfffffffa, policy=2), dict(mask=2)): for feature in ([], [1, 1], [1, 0], [0, 1], [2, 3], [3, 2]): run("L", lpwr=lpwr, feature=feature) # The LPWR reference of the scheduling switch and of the submit: mutex outcomes. for lpwr in (dict(mask=4), dict(mask=1, policy=2), dict(mask=5, policy=2)): for mutex in ([], [99], [99, 99, 0], [99] * 19 + [0], [99] * 20, [0x40], [99, 0x40], [0, 99, 0, 0x65], [0x65] * 6): run("L", lpwr=lpwr, mutex=mutex) # Configuration: context-switch timeout, PBDMA timeout, the forced timeout byte. for value, enable in ((0x3fffff, 1), (0, 0), (1, 1), (0x7fffffff, 1), (0, 1), (0x80000000, 0), (0x80000000, 1), (0xffffffff, 1), (0x12345, 0), (0x7fffffff, 2)): for debug in (0, 1): run("L", lpwr={}, fifo={0x808: (4, value), 0x80c: (1, enable), 0x80d: (1, debug)}) for pbdma in (0, 1, 2, 0xffff, 0x10000, 0xffffffff): for forced in (0, 1): run("L", lpwr={}, fifo={0x814: (4, pbdma), 0x7c7: (1, forced)}) for wait in (0, 1, 0x7a120, 0xffffffff): run("L", lpwr={}, fifo={0x810: (4, wait)}) # Register words: PCI configuration, PMC enable, PBDMA interrupt enables, scheduling # disables, status words. for number in range(60): reads = {0x88004: [random.word()], 0x200: [random.word()], 0x88014: [random.word()], 0x88018: [random.word()], 0x2a04: [random.word()], 0x2630: [random.word() for unused in range(random.pick((1, 3, 30)))]} for pbdma in range(14): reads[0x4013c + 0x2000 * pbdma] = [random.word()] reads[0x3080 + 4 * pbdma] = [random.word()] for engine in range(15): reads[0x2640 + 8 * engine] = [random.word()] run("L", lpwr={}, reads=reads) for word in (0, 0x1000, 0x2000, 0xa000, 0xc000, 0xe000, 0x1000a000, 0x10005000, 0x80000000, 0xffffffff, 0x10021002, 0xfff1fff): reads = {0x3080 + 4 * pbdma: [word] for pbdma in range(14)} reads.update({0x2640 + 8 * engine: [word] for engine in range(15)}) run("L", lpwr={}, reads=reads) # The preempt wait: pending bits and the timeout check. for pending in ([0xffffffff, 0], [0xffffffff, 0xffffffff, 0], [1, 0], [0x1ffe, 0], [0xffffffff] * 5 + [0]): for check in ([], [0x65], [0, 0x65], [0x10, 0], [0, 0, 0, 0x65], [3, 0x65]): run("L", lpwr={}, reads={0x2638: pending}, check=check) for row in (0, 1, 3, 7, 14): # The row's own poll stays pending and times out. pending = [0] * row + [0xffffffff] for gpu in ({}, {0x917: 1}, {0x918: 1}, {0x919: 1}, {0x926: 1}): run("L", lpwr={}, reads={0x2638: pending}, check=[0x65] * 16, gpu={0x911: 1, **gpu}) for gpu in ({0x917: 1}, {0x918: 1}, {0x919: 1}, {0x926: 1}, {0x52f3: 1}, {0x52f2: 1}, {0x52f2: 1, 0x52f3: 1}, {0x917: 1, 0x52f3: 1}): run("L", lpwr={}, gpu={0x911: 1, **gpu}) # A runlist without an owner object. for runlist in range(13): run("L", lpwr={}, drop_owner=runlist) run("L", lpwr={}, drop_owner=6, gpu={0x911: 1, 0x917: 1}) # Register override table of the GPU object. for overrides in ([(0x2630, 0xff, 0x100)], [(0x1fff, 1, 2), (0x2000, 0xffffffff, 0)], [(0x3fff, 0, 0x55), (0x4000, 0, 1)], [(0xf0002100, 0xf, 0x30)], [(0x2140, 0x7fffffff, 0), (0x2a0c, 0, 1), (0x40108, 1, 1), (0x3ffc, 3, 3)]): run("L", lpwr={}, overrides=overrides, reads={0x2630: [0xa5a5a5a5]}) # Buffer addresses and channel counts (the request the owners make at InitLocked). # Below 2^40: the original keeps 28 bits of the page number; core refuses the rest. for base in (0x20000, 0xffff000000, 0x12345000, 0xff00000000, 0x1ffffff000): run("L", lpwr={}, buffers=[(base + 0x20000 * number) & (1 << 64) - 1 for number in range(26)]) for channels in (0x1000, 0x800, 0x200, 0x80, 1, 0x1001, 0x2000): case, status, events, stored, requests = run("L", lpwr={}, channels=channels) assert len(set(requests)) == 1 and len(requests) == 26 rows.append("B;%x;%x %x %x %x;%x" % (channels, *requests[0], len(requests))) # The OBJFIFO constructor's fields the load reads. for registry, gpu_1e0c, gpu_1e28 in ( ({}, 0x7a120, 0), ({}, 0x1e8480, 0x20000005), ({}, 0, 0x20ffffff), ({"RmEngineContextSwitchTimeoutUs": 0}, 0x7a120, 0), ({"RmEngineContextSwitchTimeoutUs": 1}, 0x7a120, 0), ({"RmEngineContextSwitchTimeoutUs": 0x80000000}, 0x7a120, 0), ({"RmEngineContextSwitchTimeoutUs": 0x80000123}, 0x7a120, 0), ({"RmEngineContextSwitchTimeoutUs": 0xffffffff}, 0x7a120, 0x20000002), ({"RmEngineContextSwitchTimeoutUs": 0x3e8}, 0x7a120, 0x30000003), ({"RmEngineContextSwitchTimeoutUs": 0}, 0x7a120, 0x20400000), ({"RmPbdmaTimeout": 0}, 0x7a120, 0), ({"RmPbdmaTimeout": 1}, 0x7a120, 0), ({"RmPbdmaTimeout": 0x12345678}, 0x7a120, 0), ({"RmPbdmaTimeout": 7, "RmEngineContextSwitchTimeoutUs": 0x7fffffff}, 5, 0)): case = default_case() case.update(registry=dict(registry), gpu_1e0c=gpu_1e0c, gpu_1e28=gpu_1e28, lpwr={}) prepare(case) config = [int.from_bytes(cpu.mem_read(OFIFO + offset, width), "little") for offset, width in FIFO_FIELDS] rows.append("C;%s;%s;%x %x;%s;%s" % ( "-" if "RmPbdmaTimeout" not in registry else "%x" % registry["RmPbdmaTimeout"], "-" if "RmEngineContextSwitchTimeoutUs" not in registry else "%x" % registry["RmEngineContextSwitchTimeoutUs"], gpu_1e0c, gpu_1e28, " ".join(f"{value:x}" for value in config), " ".join(state["constructor"]))) assert state["keys"] == REGISTRY_KEYS, state["keys"] # OBJFIFO StateUnload _nv028312rm -> _nv028311rm after a load, as the early caller's # teardown _nv028321rm runs it (flags 0): the table unload +120 _nv027819rm -> # _nv030368rm, the pending-interrupt service +1c8 _nv028265rm -> +190 _nv028230rm # (+188, +130, +688 _nv028258rm -> _nv028263rm -> the owner's function at vtable +b8, # +170) and the interrupt disable +6b8 _nv027774rm. unload_executed = set() def unload(pending, enabled=(0x61810101,), events=(0x1fff,), gpu=None, lpwr=None, drop_owner=None, more=None): case = default_case() case.update(lpwr={} if lpwr is None else lpwr) prepare(case) assert call("_nv028309rm", GPU, OFIFO, 0) & 0xffffffff == 0 assert b(GPU + 0x99f) == 1 # State changed after the load. case["gpu"] = {0x911: 1, **(gpu or {})} for offset, value in case["gpu"].items(): wb(GPU + offset, value) if drop_owner is not None: manager = q(OFIFO + 0x7e0) wq(manager + 0x58, q(manager + 0x58) & ~(1 << drop_owner)) # The MemoryManager object, with no object at +498. wq(GPU + 0x1ed0, MEMMGR) state["events"].clear() executed.clear() reads = {0x2100: list(pending), 0x2140: list(enabled), 0x2a00: list(events)} reads.update(more or {}) case["reads"].clear() case["reads"].update({key: list(value) for key, value in reads.items()}) hook = cpu.hook_add(UC_HOOK_CODE, record_function, begin=BASES[".text"], end=BASES[".text"] + len(sections[".text"])) status = call("_nv028312rm", GPU, OFIFO, 0) & 0xffffffff cpu.hook_del(hook) unload_executed.update(executed) lpwr_text = " ".join(f"{value:x}" for value in ( int(case["lpwr"].get("pgctrl", True)), case["lpwr"].get("mask", 0), case["lpwr"].get("policy", 0), case["lpwr"].get("byte_271", 1), int(case["lpwr"].get("child", True)), case["lpwr"].get("byte_183", 0), case["lpwr"].get("byte_184", 0))) if lpwr != "absent" else "-" rows.append("X;%s;%s;%s;%s;%x;%x;%s" % ( " ".join(f"{case['gpu'].get(offset, 0):x}" for offset in GPU_BYTES), lpwr_text, "-" if drop_owner is None else f"{drop_owner:x}", ",".join(f"{offset:x}:{listed(values)}" for offset, values in sorted(reads.items())), status, b(GPU + 0x99f), " ".join(state["events"]))) return state["events"] traced_unload = unload([0x40000000, 0]) for pending in ([0, 0], [0x40000000, 0], [0x40000000, 0x40000000], [0x800000, 0], [0x40800000, 0x1], [0x1, 0], [0x10000, 0], [0x1000000, 0], [0x41810001, 0], [0xffffffff, 0]): for enabled in (0x61810101, 0x7fffffff, 0, 0x40000000, 0x41810001, 0x00800000): if pending[0] & enabled & 0xa0000100: # PBDMA service, the function at +718 and _nv026827rm are not executed. continue for events in (0x1fff, 0, 1, 0x1040, 0x2000, 0x3fff, 0x80000001): unload(pending, (enabled,), (events,), more={0x252c: [0x11], 0x256c: [0x22], 0x258c: [0x33]}) for gpu in ({0x917: 1}, {0x918: 1}, {0x919: 1}, {0x926: 1}, {0x93b: 1}): unload([0x40000000, 0], gpu=gpu) for runlist in (0, 6, 12): for events in (0x1fff, 1 << runlist, 0x1fff & ~(1 << runlist)): unload([0x40000000, 0], events=(events,), drop_owner=runlist) for lpwr in ({"child": False}, {"pgctrl": False}, {"byte_184": 1}, {"byte_183": 1}, {"mask": 5, "policy": 2}): unload([0x40000000, 0], lpwr=lpwr) # KernelFifo+208 _nv040417rm(gpu, fifo, enable, offset). prepare(dict(default_case(), lpwr={})) for enable in (0, 1, 2, 0x100, 0x101, 0xff): for offset in (0, 0xfff, 0x1000, 0x7ffdf0000, 0xffffffffff000, 0xfffffffffffff000): state["events"].clear() call(q(KFIFO + 0x208), GPU, KFIFO, enable, offset) rows.append(f"U;{enable:x};{offset:x};" + " ".join(state["events"])) out = ROOT / "target/gpu-stage" out.mkdir(parents=True, exist_ok=True) oracle = "\n".join(rows) + "\n" (out / "original-fifo-load.txt").write_text(oracle) kinds = {} for row in rows: kinds[row.split(";", 1)[0]] = kinds.get(row.split(";", 1)[0], 0) + 1 loads = [row.split(";") for row in rows if row[0] in "LV"] failed = sum(1 for fields in loads if fields[14] != "0") hooked = sorted(HANDLERS) report = { "original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "entry": "_nv028309rm (OBJFIFO StateLoad) on objects built by the engine table " "constructor, _nv040227rm, _nv027761rm and _nv028169rm; _nv040417rm", "executed_on_the_v100_case": sorted(load_executed - set(hooked)), "executed_count": len(load_executed - set(hooked)), "fixture_supplied": hooked + ["OBJTMR+0x180", "OBJBUS+0x220", "OBJTMR+0x1a0/1a8/1f8/200"], "slots_checked_against_bindings_report": EXPECTED, "foreign_slots_stored_from_the_bindings_report": [ [label, f"{slot:#x}", function] for label, base, slot, function in FOREIGN], "gpu_object_slots": {f"{slot:#x}": function for slot, function in GPU_SLOTS.items()}, "cases": len(rows), "cases_by_kind": kinds, "load_cases_with_nonzero_status": failed, "kinds": {"T": "engine table of the physical V100 (16 records of 100 bytes)", "V": "the traced V100 load", "L": "load cases", "B": "runlist buffer request of the owners at InitLocked per channel count", "C": "OBJFIFO constructor fields", "U": "_nv040417rm", "X": "OBJFIFO StateUnload after a load"}, "v100_runlist_buffer_requests": { "count": 26, "bytes": "0x20000", "alignment": "0x1000", "address_space": 2, "descriptor_tag": "0x86", "order": "two per owner, owners created in engine " "table order of their first engine; the load submits the first of each pair"}, "unload": { "entry": "_nv028312rm with flags 0 after a load", "executed": sorted(unload_executed - set(hooked)), "traced_case_events": traced_unload, "transcript_lines_141005_141012": "W 26ec 1fffe, R 2100 40000000, R 2140 61810101, " "R 2a00 1fff, W 2a00 1fff, R 2100 0, W 2140 0, W 2528 0"}, "v100_events": len(physical), "transcript_comparison": transcript, "registry_keys_read_by_the_constructor": REGISTRY_KEYS, "oracle": "target/gpu-stage/original-fifo-load.txt", "oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(), "scope": "Original GV100 OBJFIFO StateLoad with binder-selected tables on original " "InitLocked state. Register values, timeouts, delays, memory addresses, the " "timer and bus functions, the clock-gating feature state, the LPWR mutex and " "the recovery service are fixture-supplied; no hardware executed."} (ROOT / "reports/gpu-fifo-load-original.json").write_text(json.dumps(report, indent=2) + "\n") print(f"{len(rows)} original FIFO load rows ({failed} loads with a nonzero status); executed " f"{len(load_executed - set(hooked))} original functions" + ("; transcript lines %s equal" % transcript["lines"] if transcript else "; transcript absent")) print(report["oracle_sha256"]) REGISTRY_KEYS = ["RmPbdmaTimeout", "RmDefaultPbTimeslice", "RmReplayableFaultsDefault", "RMHostBug1748740War", "RMEnableQoSRunlistIntr", "RmAllowChannelCreationOnPendingReset", "RmPbdmaAcquireTimeout", "RmPpbmaLbPbbufThrottle0", "RmPpbmaLbPbbufThrottle1", "RMOverrideVfsConfig", "RmEngineContextSwitchTimeoutUs", "RmCtxswTimeoutDebugDump"] if __name__ == "__main__": main()