#!/usr/bin/env python3 """Run the original producers of the GV100 Graphics StateLoad inputs and write an oracle. Executed original code on the emulator of check_gpu_graphics_load_binary.py: the class binders and initializers, the GraphicsManager and Graphics constructors, Graphics PreInitLocked, GraphicsManager InitLocked and Graphics InitLocked (_nv033663rm -> _nv033662rm -> _nv033193rm, _nv033212rm -> _nv033211rm), and the OBJBUS geometry query _nv018541rm that fills the GraphicsManager record. Register values, the fuse object (GPU+1e78 functions +100 and +118) and the registry are case inputs. The Graphics InitLocked register accesses fall in a lost-event gap of the physical transcript (lines 138651-138667), so their order here is instruction-emulation evidence only. """ import hashlib import json from pathlib import Path import struct import sys sys.path.insert(0, str(Path(__file__).resolve().parent)) ARGUMENTS = sys.argv[1:] sys.argv = sys.argv[:1] import check_gpu_graphics_load_binary as h # noqa: E402 ROOT = h.ROOT rows = [] FAULTS = [] CONSTANTS = (0x14, 0x16, 0x17, 0x1c, 0x1e, 0x2a) RECORD = (0, 4, 8, 0xc, 0x14, 0x18, 0x1c, 0x20, 0x24, 0x28, 0x30, 0x34, 0x38) GRINFO = (0x544, 0x548, 0x54c, 0x550, 0x554, 0x558, 0x55c, 0x560, 0x564, 0x5e8, 0x540, 0x528) GR_BYTES = (0x160c, 0x15a2, 0x15c9, 0x15ca, 0x15e5, 0x18d4, 0x50) def default_case(): case = h.default_case() case["reads"][0x1070] = [0] case["reads"][0x40960c] = [0] case["reads"][0x500920] = [0] return case def produced(): """The state InitLocked leaves that the load reads, and the neighbouring fields.""" block = h.grinfo() records = h.q(block + 0x570) count = h.d(h.q(h.GRMGR + 0x2b0) + 0xc) capacity = h.d(block + 0x540) tile = bytes(h.cpu.mem_read(h.q(block + 0x538), capacity)).hex() if h.q(block + 0x538) else "" # The SM-id table at GRinfo+578 (_nv033220rm): 0x24 bytes per SM, two SMs per TPC that # differ in the SM index (+8) only. One "gpc:tpc:physical" entry per TPC. table, sms, entries = h.q(block + 0x578), 2 * h.d(block + 0x550), [] for number in range(sms if table else 0): gpc, tpc, sm, order, physical = (h.d(table + 0x24 * number + offset) for offset in (0, 4, 8, 0xc, 0x18)) assert (sm, order) == (number % 2, number // 2), (number, sm, order) if number % 2: assert entries[-1] == f"{gpc:x}:{tpc:x}:{physical:x}" else: entries.append(f"{gpc:x}:{tpc:x}:{physical:x}") return [",".join(":".join(f"{h.d(records + 0x1f0 * gpc + offset):x}" for offset in RECORD) for gpc in range(count)) if records else "", " ".join(f"{h.d(block + offset):x}" for offset in GRINFO), tile or "-", " ".join(f"{h.b(h.GR + offset):x}" for offset in GR_BYTES) + " %x %x" % (h.d(h.GR + 0x138), h.d(h.GR + 0x188c)), " ".join(f"{h.b(h.GRMGR + offset):x}" for offset in h.PROPERTIES) + " %x" % h.b(h.LPWR + 0x272), ",".join(entries) or "-"] def init(kind="N", **changes): case = default_case() case.update(changes) case["reads"] = {**default_case()["reads"], **changes.get("reads", {})} try: stages = h.construct(case) except (RuntimeError, AssertionError) as error: # The original faults (a CPU exception in emulation, a kernel fault on a machine). where = str(error).split("\n")[0] or "emulation stopped outside the return address" FAULTS.append({"bus": list(case["bus"]), "tpc_words": [f"{case['reads'][0x502608 + 0x8000 * gpc][0]:#x}" for gpc in range(6)], "fault": where}) return None, None, None, None events = h.state["events"] start = events.index("stage:_nv033663rm") assert not any(status for name, status in stages.items() if name != "_nv033663rm"), stages status = stages["_nv033663rm"] kept = [event for event in events[start + 1:] if event[0] not in "eAS" or event[1] != ":"] query = case["fuse_query"].get(0xff000034, (0, 0)) fields = [" ".join(f"{value:x}" for value in case["bus"]), "%x %x" % query] state = produced() if status == 0 else ["", "", "-", "", "", "-"] rows.append(f"{kind};" + ";".join(fields) + f";{status:x};" + ";".join(state) + ";" + " ".join(kept)) return status, state, kept, list(h.state["keys"]) def words(tpc, second=4): return {0x502608 + 0x8000 * gpc: [count | second << 16] for gpc, count in enumerate(tpc)} def main(): h.state["real"] = True # K: the constants the GPU object's function at +420 returns, and the divisor table. status, baseline, events, keys = init("V") assert status == 0 constants = [h.call(h.q(h.GPU + 0x420), h.GPU, index) & 0xffffffff for index in CONSTANTS] primes = struct.unpack_from("<16I", h.sections[".rodata"], 0x4ce8a88) rows.insert(0, "K;" + " ".join(f"{value:x}" for value in constants) + ";" + " ".join(f"{value:x}" for value in primes)) # B: OBJBUS+300 _nv018541rm, which fills the GraphicsManager record. assert h.function_at(h.q(h.BUS + 0x300)) == "_nv018541rm" random = h.Random(0x33193) for number in range(24): values = ((0x100001, 6, 6, 8, 7) if number == 0 else tuple(random.word() for unused in range(5))) h.state["case"]["reads"] = {offset: [value] for offset, value in zip( (0x120070, 0x120078, 0x22430, 0x22438, 0x22434), values)} h.state["events"].clear() h.call("_nv018541rm", h.GPU, h.BUS) record = h.q(h.GRMGR + 0x2b0) rows.append("B;" + " ".join(f"{h.d(record + offset):x}" for offset in (0xc, 0x10, 0x14, 0x18)) + " %x %x;" % (h.b(record + 0x1c), h.d(h.BUS + 0x664)) + " ".join(h.state["events"])) # Registry: every key the constructors and InitLocked read, set one at a time. registry_effects = {} for key in sorted(set(keys)): for value in (1, 5): status, state, unused, unused = init("N", registry={key: value}) if status is None: registry_effects.setdefault(key, []).append({"value": value, "fault": True}) elif status != 0 or state != baseline: registry_effects.setdefault(key, []).append( {"value": value, "status": f"{status:#x}", "fields_changed": [name for name, new, old in zip( ("records", "grinfo", "tile map", "graphics bytes", "manager bytes", "SM-id table"), state, baseline) if new != old]}) rows.pop() # Geometry: TPC counts, the second count, fuse words and the GPC fuse query. for tpc in ((7,) * 6, (6,) * 6, (7, 5, 7, 7, 4, 7), (1,) * 6, (8,) * 6, (0, 7, 7, 7, 7, 7), (0,) * 6, (7, 7, 7, 0, 0, 0), (3, 1, 4, 1, 5, 2), (2, 2, 2, 2, 2, 1), (8, 7, 6, 5, 4, 3), (1, 0, 0, 0, 0, 0), (5,) * 6, (7, 6, 7, 6, 7, 6)): for second in (4, 0, 7, 0x1f): init(reads=words(tpc, second)) for mask in (0, 1, 0x3e, 0x2a, 0x15, 0x3f, 0xffffffc0, 0x20, 0xffffffff): for query_status in (0, 0x56): init(fuse_query={0xff000034: (query_status, mask)}) for fuse in ({}, {0xc38 + 4 * gpc: 1 for gpc in range(6)}, {0xc3c: 3, 0xc48: 0x7f}, {0xc38: 0xffffffff, 0xc4c: 0x80}, {0xc40: 0x55}): for mask in (0, 0x9, 0x30): init(fuse=dict(fuse), fuse_query={0xff000034: (0, mask)}) # The bus record: GPC count, the count the fuse masks use, TPCs per GPC. for bus in ((6, 6, 7, 42), (1, 6, 7, 42), (4, 6, 7, 42), (6, 4, 7, 28), (6, 6, 8, 48), (6, 6, 5, 30), (3, 3, 7, 21), (2, 6, 3, 18), (6, 0, 7, 0), (5, 6, 7, 42), (6, 6, 1, 6), (6, 31, 7, 217), (6, 6, 31, 186)): for tpc in ((7,) * 6, (3, 1, 4, 1, 5, 2)): init(bus=bus, reads=words(tpc)) # PPC words and the three single reads. for number in range(40): reads = words(tuple(random.word() % 9 for unused in range(6)), random.pick((4, 4, 2, 0))) for gpc in range(6): for unit in range(3): reads[0x500c30 + 0x8000 * gpc + 4 * unit] = [random.pick( (0, 1, 0x49, 0x12, 0x24, 0x7f, 0x80, 0x100, random.word()))] for offset in (0x1070, 0x40960c, 0x500920): reads[offset] = [random.word()] init(reads=reads, fuse={0xc38 + 4 * gpc: random.word() & 0x7f for gpc in range(6)}, fuse_query={0xff000034: (0, random.pick((0, 0, 0, 1, 8, 0x21)))}) out = ROOT / "target/gpu-stage" oracle = "\n".join(rows) + "\n" (out / "original-graphics-init.txt").write_text(oracle) kinds = {} for row in rows: kinds[row.split(";", 1)[0]] = kinds.get(row.split(";", 1)[0], 0) + 1 failed = sum(1 for row in rows if row[0] == "N" and row.split(";")[3] != "0") report = { "original_sha256": h.DIGEST, "chip_hal": "0x23", "rm_hal": 2, "entry": "_nv033663rm on objects prepared by the binders, _nv033946rm, _nv034073rm, " "_nv031719rm, _nv033667rm and _nv034082rm; _nv018541rm separately", "init_locked_events_of_the_v100_case": events, "registry_keys_read": sorted(set(keys)), "registry_keys_that_change_the_produced_state": registry_effects, "inputs_on_which_the_original_faults": FAULTS, "gpu_420_constants": {f"{index:#x}": value for index, value in zip(CONSTANTS, constants)}, "prime_table_rodata_0x4ce8a88": list(primes), "fixture_supplied": ["register values", "the fuse object functions +100 and +118", "the registry", "_nv000260rm, _nv019166rm (services, recorded)", "the bus record through _nv034073rm in N rows"], "cases": len(rows), "cases_by_kind": kinds, "init_cases_with_nonzero_status": failed, "kinds": {"K": "GPU+420 constants and the prime table", "V": "the V100 geometry", "B": "_nv018541rm", "N": "_nv033663rm"}, "oracle": "target/gpu-stage/original-graphics-init.txt", "oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(), "scope": "Original GV100 Graphics InitLocked and the OBJBUS geometry query. The order " "of the InitLocked register accesses has instruction-emulation evidence only " "(lost events in the transcript). No hardware executed."} (ROOT / "reports/gpu-graphics-init-original.json").write_text(json.dumps(report, indent=1) + "\n") print(f"{len(rows)} original Graphics init rows {kinds}; {failed} with a non-zero status; " f"registry keys with an effect: {sorted(registry_effects)}") print(report["oracle_sha256"]) if __name__ == "__main__": main()