#!/usr/bin/env python3 """Run the original GV100 MemorySystem ZBC service (MemorySystem+fa8, _nv045299rm) and write an oracle. Executed original code. This reuses the emulator of check_gpu_graphics_load_binary.py (whole object relocated, original binders and constructors) and replaces its ZBC fixture with the original body. On top of the load preparation it runs the original _nv044445rm (the LTC register objects at MemorySystem+8d00) and _nv045302rm (the table allocation InitLocked performs). Byte MemorySystem+8cb1 is the registry value RMDisableZBCDefaultLoad that PreInitUnlocked _nv045897rm stores; that body is not executed here, the byte is a case input. """ import hashlib import json from pathlib import Path import struct import sys sys.path.insert(0, str(Path(__file__).resolve().parent)) from _gpu_graphics_oracle_check import require, write_or_check_json, write_or_check_text # noqa: E402 ARGUMENTS = list(sys.argv[1:]) _saved_argv = list(sys.argv) sys.argv = sys.argv[:1] try: import check_gpu_graphics_load_binary as h # noqa: E402 finally: sys.argv = _saved_argv ROOT = h.ROOT SYS, VGPU, RC = (h.OBJ + 0x10000 * index for index in (23, 24, 25)) M = h.MEMSYS ZBC_FIRST, ZBC_LAST = h.ZBC_LINES COUNTERS = (0x16a8, 0x16ac, 0x16b0, 0x16b4, 0x16b8) # Bodies the service reaches that the load harness hooks or lacks. h.hook("_nv060484rm", lambda a: 0x57) # the vGPU manager has no record of this GPU def compare(a): left = bytes(h.cpu.mem_read(a[0], a[2] & 0xffffffff)) right = bytes(h.cpu.mem_read(a[1], a[2] & 0xffffffff)) return 0 if left == right else (1 if left > right else 0xffffffff) h.hook("os_mem_cmp", compare) rows = [] def table(): return h.q(M + 0x1408) def dump(): """The state of class index 0 and the depth and stencil tables.""" base = table() colours = ",".join(":".join(f"{h.d(base + 0x28 * index + 4 * word):x}" for word in range(10)) for index in range(16)) depth = ",".join(":".join(f"{h.d(M + 0x1528 + 0xc * index + 4 * word):x}" for word in range(3)) for index in range(16)) stencil = ",".join(":".join(f"{h.d(M + 0x15e8 + 0xc * index + 4 * word):x}" for word in range(3)) for index in range(16)) scalars = "%x %x %x " % (h.q(M + 0x1410), h.d(M + 0x1418), h.q(M + 0x1520)) scalars += " ".join(f"{h.d(M + offset):x}" for offset in COUNTERS) return f"{scalars};{colours};{depth};{stencil}" def prepare(case): """Load preparation, then the MemorySystem state InitLocked leaves for ZBC.""" block = h.prepare(case) require(h.function_at(h.by_name["_nv045299rm"]) == "_nv045299rm", "unexpected _nv045299rm") h.wq(M + 0xfa8, h.by_name["_nv045299rm"]) h.wq(h.by_name["_nv029075rm"], SYS) h.wq(SYS + 0x1f0, VGPU) h.wq(h.GPU + 0x2100, RC) h.cpu.mem_write(RC, bytes(0x1000)) h.wd(RC + 0x164, case.get("rc", (0, 0))[0]) h.wd(RC + 0x168, case.get("rc", (0, 0))[1]) require(h.call("_nv044445rm", h.GPU, M) & 0xffffffff == 0, "_nv044445rm failed") require(h.call("_nv045302rm", h.GPU, M) & 0xffffffff == 0, "_nv045302rm failed") require(not [event for event in h.state["events"] if event[0] in "rw"], "unexpected rw events") for offset, value in case.get("memsys", {}).items(): h.wb(M + offset, value) h.state["events"].clear() return block def poke(case): """State changes of a case: dwords of the tables, counters and the masks.""" base = table() for (index, word), value in case.get("colours", {}).items(): h.wd(base + 0x28 * index + 4 * word, value) for (index, word), value in case.get("depth", {}).items(): h.wd(M + 0x1528 + 0xc * index + 4 * word, value) for (index, word), value in case.get("stencil", {}).items(): h.wd(M + 0x15e8 + 0xc * index + 4 * word, value) for offset, value in case.get("counters", {}).items(): h.wd(M + offset, value) if "mask" in case: h.wq(M + 0x1410, case["mask"]) if "count" in case: h.wd(M + 0x1418, case["count"]) if "global_mask" in case: h.wq(M + 0x1520, case["global_mask"]) def configuration(case): return "%x %x %x %x %x %x %x" % (h.b(M + 0x8cb1), h.b(M + 0x8cb2), h.b(M + 0x8cd0), h.b(M + 0x1339), h.d(RC + 0x164), h.d(RC + 0x168), h.b(h.GR + 0x1593)) def kept(events): return [event for event in events if event[0] not in "eAS" or event[1] != ":"] def service(kind="Z", runs=1, **changes): """_nv045299rm(gpu, memory system, 0) with Graphics+1593 set as the load sets it.""" case = h.default_load() case.update(changes) case["reads"] = {**h.default_load()["reads"], **changes.get("reads", {})} case["gpu"] = {**h.default_load()["gpu"], **changes.get("gpu", {})} block = prepare(case) fields = h.inputs_of(case, block) h.wb(h.GR + 0x1593, 1) for number in range(runs): if number == runs - 1: poke(case) before = dump() setup = configuration(case) h.state["events"].clear() status = h.call("_nv045299rm", h.GPU, M, 0) & 0xffffffff require(not h.state["continuations"], "unexpected pending continuations") events = list(h.state["events"]) rows.append(f"{kind};" + ";".join(fields) + f";{setup};{before};{status:x};{dump()};" + " ".join(kept(events))) return status, events def load(kind, **changes): """_nv033665rm with the original ZBC service inside.""" case = h.default_load() case.update(changes, entry="_nv033665rm") case["reads"] = {**h.default_load()["reads"], **changes.get("reads", {})} case["gpu"] = {**h.default_load()["gpu"], **changes.get("gpu", {})} block = prepare(case) fields = h.inputs_of(case, block) poke(case) before, setup = dump(), configuration(case) h.state["events"].clear() status = h.call("_nv033665rm", h.GPU, h.GR, case["flags"]) & 0xffffffff require(not h.state["continuations"], "unexpected pending continuations") events = list(h.state["events"]) info = h.q(h.GRMGR + 0x2b0) outputs = ([f"{h.b(info + offset):x}" for offset in h.FLAGS] + [f"{h.b(h.KGR + 0x1a9):x}", f"{h.d(block + 0x608):x}", f"{h.b(block + 0x614):x}", f"{h.b(h.GR + 0x1593):x}", f"{h.d(h.GR + 0x1620):x}", f"{h.b(h.GR + 0x1591):x}"]) rows.append(f"{kind};" + ";".join(fields) + f";{setup};{before};{status:x};{dump()};" + " ".join(outputs) + ";" + " ".join(kept(events))) return status, events def registers(events): return [event for event in events if event[0] in "rw" and event[1] == ":"] def main(check=False): rows.clear() # The traced V100: the whole StateLoad, every body original except time, diagnostics, # the VBIOS table and the LPWR query. reads, tokens = h.transcript_range(h.FIRST_LINE, h.LAST_LINE) h.trace_functions(True) h.executed.clear() status, physical = load("V", real=True, reads={**h.default_case()["reads"], **reads}) h.trace_functions(False) got = registers(physical) for number, (left, right) in enumerate(zip(got, tokens)): require(left == right, (number, left, right, status, physical[number - 6:number + 30])) require(status == 0 and got == tokens, ("transcript", status, len(got))) require( not any(event[0] in "eAS" and event[1] == ":" for event in physical), "unexpected error/assert events in physical trace", ) executed = sorted(name for name in h.executed if name.startswith("_nv04") or name in ("_nv013328rm", "_nv013330rm", "_nv013332rm", "_nv000902rm", "_nv033614rm", "_nv033722rm", "_nv033729rm")) # The service alone on the lines the transcript shows for it. zbc_reads, zbc_tokens = h.transcript_range(ZBC_FIRST, ZBC_LAST) status, alone = service("T", real=True, reads=zbc_reads) require(status == 0 and registers(alone) == zbc_tokens, "service transcript") # The default tables are constants of the object: read where the three loaders read them. data = h.sections[".rodata"] defaults = {"colour": [list(struct.unpack_from("<9I", data, 0x4c53e60 + 0x24 * entry)) for entry in range(1, 3)], "depth": list(struct.unpack_from("> 3 & 1} if number % 16 >= 12 else {}, default_read=h.hashed(random.word()), reads={0x409b00: [0], 0x12006c: [0x10]}, **case) # L: the whole StateLoad with the original service, Falcon and clock-gating children as # the load harness's fixtures. for changes in (dict(), dict(flags=3), dict(gr={0x160c: 0}), dict(mc=False), dict(memsys={0x8cb1: 1}), dict(memsys={0x8cb2: 0}), dict(gpu={0x911: 0}), dict(lpwr=False), dict(base_38=0), dict(done_mask=1), dict(reads={0x409b00: [0] * 3 + [0x80000000] * 40, 0x40060c: [1]}, check=[0x65] * 40)): load("L", **changes) for number in range(8): load("L", default_read=h.hashed(random.word()), check=[0x65] * 600, reads={0x409660: [random.pick((0, 0x3f100, 0x7000, 0x21000))], 0x409b00: [0]}) out = ROOT / "target/gpu-stage" oracle = "\n".join(rows) + "\n" write_or_check_text(out / "original-graphics-zbc.txt", oracle, check=check) 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] == "Z" and row.split(";")[17] != "0") report = { "original_sha256": h.DIGEST, "chip_hal": "0x23", "rm_hal": 2, "entry": "_nv045299rm(gpu, MemorySystem, 0) on the objects of " "check_gpu_graphics_load_binary.py plus _nv044445rm and _nv045302rm", "executed_in_the_traced_load": executed, "fixture_supplied": ["register values", "time (_nv059509rm, _nv059505rm, _nv049819rm)", "_nv042917rm (LPWR query)", "_nv060484rm (vGPU record lookup, " "returns not found)", "MemorySystem+8cb1 (registry)", "the OBJRC words GPU+2100 +164/+168", "in Z and L rows: _nv027871rm; in L rows also _nv033505rm, " "_nv027641rm, _nv029884rm"], "default_tables": {"source": ".rodata of the object, read by _nv045294rm (+0x4c53e64, " "stride 0x24: four LTC words, four PGRAPH words, format), " "_nv045295rm (+0x4c53e48) and _nv045297rm (+0x4c54068); " "the first entry of each table is immediate operands", "rodata_words": defaults}, "transcript": {"load_lines": f"{h.FIRST_LINE}-{h.LAST_LINE}", "load_equal": True, "load_register_events": len(registers(physical)), "service_lines": f"{ZBC_FIRST}-{ZBC_LAST}", "service_equal": True, "service_register_events": len(registers(alone))}, "cases": len(rows), "cases_by_kind": kinds, "service_cases_with_nonzero_status": failed, "kinds": {"V": "the traced V100 StateLoad with the original service, every event", "T": "the service alone on transcript lines 157715-158241", "C": "the default-table words of .rodata", "Z": "_nv045299rm", "L": "_nv033665rm with the original service"}, "oracle": "target/gpu-stage/original-graphics-zbc.txt", "oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(), "scope": "Original GV100 MemorySystem ZBC service outside PM resume (OBJMC+1ba clear, " "GPU+90c clear, no vGPU guest, MemorySystem+8cf8 zero), with MemorySystem+1339 " "and +8cd0 clear as the GV100 initializer leaves them and Graphics not yet " "loaded (+1591 clear). No hardware executed."} write_or_check_json(ROOT / "reports/gpu-graphics-zbc-original.json", report, check=check) print(f"{len(rows)} original ZBC rows {kinds}; transcript lines {ZBC_FIRST}-{ZBC_LAST} and " f"{h.FIRST_LINE}-{h.LAST_LINE} equal") print(report["oracle_sha256"]) if __name__ == "__main__": if "--disassemble" in ARGUMENTS: for name in ARGUMENTS[ARGUMENTS.index("--disassemble") + 1:]: print(f"== {name}") print(h.disassemble(name)) else: main(check="--check" in ARGUMENTS)