#!/usr/bin/env python3 """Run the original GV100 PMU message and command transport over framebuffer queues. The environment is that of tools/check_gpu_pmu_ls_start_binary.py, executed unchanged through its traced V100 Load case: the PMU class is constructed, initialised and started, and the 0x207a00-byte surface is backed by emulator memory, so every byte the original copies to or from it is observed. Executed original code (chip HAL 0x23, RM HAL 2): receive the transport's message function _nv052433rm (vtable +68): the INIT branch (tail read, 60-byte copy from the message queue, _nv051847rm with _nv052268rm, _nv052416rm and its three _nv028635rm queue constructions, _nv052431rm, _nv028435rm, _nv051824rm) and the reply branch (_nv028644rm with the queue functions _nv013398rm, _nv013399rm, _nv013410rm, _nv013403rm, _nv013384rm, the classification, the sequence lookup, the copy of the reply out of the command element, _nv028473rm). post _nv052461rm and the transport's post function _nv052441rm (vtable +60) with the sequence allocation _nv028648rm, the command memory wait _nv052635rm/_nv055424rm, the element build, the queue write _nv028630rm (_nv013401rm) and the reply wait _nv052635rm/_nv055427rm. requests _nv043191rm -> _nv016743rm (the light-secure manager's Falcon reset): the region command and the bootstrap command, with _nv016932rm and _nv043198rm. Fixture-supplied: as in the start harness. The interrupt service pass _nv037739rm delivers a case's reply by writing a message element and the reply bytes into the surface and continuing in the original _nv052433rm; the PMU's own interrupt entry above it is not executed. The queue position registers 0x10a4cc and 0x10a804 read back what was written; 0x10a4c8 (the message head the PMU advances) is set by the case. The firmware side is a fixture: the message contents are what this harness writes, built to satisfy the checks the original applies. No hardware. """ import hashlib import json from pathlib import Path import sys HERE = Path(__file__).resolve() START = HERE.with_name("check_gpu_pmu_ls_start_binary.py") _text = START.read_text() _begin, _end = "HERE = Path(__file__).resolve()", "# ------------------------------------------------------------------ variations" assert _begin in _text and _end in _text __file__ = str(START) exec(compile(_text[_text.index(_begin):_text.index(_end)], str(START), "exec"), globals()) # noqa: S102 __file__ = str(HERE) SURFACE = V100_PMU["surface"] SURF = next(backing for backing, size, base in state["ranges"] if base == SURFACE) LPQ, MSGQ, ELEMENT, MESSAGE = 0x8200, 0x10200, 0x800, 0x40 READ_BACK.update((0x10a4cc, 0x10a804)) STARTED = pmu_snapshot() rows, counts = [], {} QUEUE_OBJECTS = {} def init_message(status=0, offset=0x100, size=0x200, falcon=0x1234, unit=0xf, kind=0): m = bytearray(MESSAGE) m[0], m[1], m[6] = unit, 0x3c, kind m[0x20:0x22] = offset.to_bytes(2, "little") m[0x22:0x24] = size.to_bytes(2, "little") m[0x24:0x26] = falcon.to_bytes(2, "little") m[0x26] = status return bytes(m) def fbq_message(seq, number, flags=0, b5=0x0f, b6=4, unit=0x0f, size=0x18, bad=0): """A reply message element: header, classification bytes, the 16-bit length and message number of the element, a 16-bit checksum word.""" m = bytearray(MESSAGE) m[0], m[1], m[3], m[5], m[6], m[0x14], m[0x15] = unit, size, seq, b5, b6, seq, flags m[0xa:0xc] = (size - 4).to_bytes(2, "little") m[0xc:0xe] = number.to_bytes(2, "little") total = sum(int.from_bytes(m[i:i + 2], "little") for i in range(0, size + 3 & ~3, 2)) m[0xe:0x10] = ((bad - total) & 0xffff).to_bytes(2, "little") return bytes(m) def element(index): return bytes(cpu.mem_read(SURF + LPQ + ELEMENT * index, ELEMENT)) def deliver(spec, position): """spec: number:flags:status:bad:b6 -- written at message index `position`.""" number, flags, status, bad, b6 = spec def action(): head = state["reads"][0x10a804][0] index = (head - 1) % 16 raw = bytearray(element(index)) seq = raw[0xb] raw[0x14 + 4], raw[0x14 + 5], raw[0x14 + 0x10] = 0, status, 0x77 cpu.mem_write(SURF + LPQ + ELEMENT * index, bytes(raw)) cpu.mem_write(SURF + MSGQ + MESSAGE * position, fbq_message(seq, number, flags=flags, bad=bad, b6=b6)) state["reads"][0x10a4c8] = [(position + 1) % 16] return action def run(label, name, *arguments): try: return call(name, *arguments) except Exception: print("FAILED", label, function_at(cpu.reg_read(UC_X86_REG_RIP)), calls[-6:], trace[-34:], [hex(c) for f, c in diag[-8:]], file=sys.stderr) raise def begin(kind, tail=0): pmu_restore(STARTED) del trace[:], diag[:], asserts[:] state.update(reads={0x10a4cc: [tail], 0x10a4c8: [tail], 0x10a804: [0], 0x10ac08: [0], 0x10a800: [0], 0x10a820: [0], 0x10a824: [0]}, checks=[], counts={}, fail={}) irq["pending"] = [] def finish(kind, label, status, extra=""): counts[kind] = counts.get(kind, 0) + 1 rows.append("|".join((kind, label, f"{status:x}", extra, " ".join(trace)))) del trace[:] def init_case(tail=0, **message): begin("I", tail) cpu.mem_write(SURF + MSGQ + MESSAGE * tail, init_message(**message)) status = call("_nv052433rm", GPU, TRANSPORT, 0, 0) kernel = pointer(PMU + 0xb08) - 8 extra = (f"ready={byte(F_PMU + 0x583):x} value={half(F_PMU + 0x5d0):x} " f"state={word(kernel + 0x80):x}:{word(kernel + 0x84):x}") label = f"tail={tail:x} " + " ".join(f"{key}={value:x}" for key, value in message.items()) finish("I", label.strip(), status, extra) return status def schedule(replies): """replies: list of (pass, spec); passes count interrupt service passes.""" pending = [] for number, spec, position in replies: while len(pending) < number - 1: pending.append(None) pending.append(deliver(spec, position)) irq["pending"] = pending def describe(replies): return ",".join(f"{number}:{position}:" + ":".join(f"{value:x}" for value in spec) for number, spec, position in replies) or "-" def request_case(scenario, falcon, flags, replies, checks=()): """_nv043191rm(gpu, lsfm, falcon, flags, 0) in the scenario's current state.""" head = state["reads"][0x10a804][0] state["checks"] = list(checks) schedule(replies) status = run((scenario, falcon, flags, describe(replies)), "_nv043191rm", GPU, LSFM, falcon, flags, 0) left = len(state["checks"]) used = list(checks)[:len(checks) - left] posted = (state["reads"][0x10a804][0] - head) % 16 elements = ",".join(element((head + index) % 16)[:0x40].hex() for index in range(posted)) digest = ",".join(hashlib.sha256(element((head + index) % 16)).hexdigest()[:16] for index in range(posted)) finish("R", f"s={scenario} falcon={falcon:x} flags={flags:x} replies={describe(replies)} " f"checks={','.join(f'{value:x}' for value in used) or '-'}", status, f"a37d={byte(ACR + 0x37d):x} elements={elements or '-'} sha={digest or '-'}") return status def rpc_case(scenario, payload, replies, checks=()): """_nv052461rm(gpu, queue object, rpc, bytes, 0, 0, 1).""" head = state["reads"][0x10a804][0] state["checks"] = list(checks) schedule(replies) cpu.mem_write(SCRATCH + 0x1000, payload) q(STACK + 0xfff00 + 8, 1) # seventh argument: the low-priority command queue status = run((scenario, len(payload)), "_nv052461rm", GPU, TRANSPORT + 8, SCRATCH + 0x1000, len(payload), 0, 0, 1) left = len(state["checks"]) used = list(checks)[:len(checks) - left] posted = (state["reads"][0x10a804][0] - head) % 16 after = bytes(cpu.mem_read(SCRATCH + 0x1000, len(payload))) digest = ",".join(hashlib.sha256(element((head + index) % 16)).hexdigest()[:16] for index in range(posted)) finish("P", f"s={scenario} rpc={payload.hex()} replies={describe(replies)} " f"checks={','.join(f'{value:x}' for value in used) or '-'}", status, f"after={after.hex()} sha={digest or '-'}") return status def ready(tail=0): begin("S", tail) cpu.mem_write(SURF + MSGQ + MESSAGE * tail, init_message()) assert call("_nv052433rm", GPU, TRANSPORT, 0, 0) == 0 del trace[:] state["reads"][0x10a4c8] = [(tail + 1) % 16] OK = (1, 0, 0, 0, 4) # ------------------------------------------------------------------- INIT assert init_case() == 0 QUEUES = pointer(pointer(F_PMU + 0x610) + 0x48) for index in range(3): base = QUEUES + 0xd0 * index QUEUE_OBJECTS[index] = {"logical": word(base + 0x88), "physical": word(base + 0x8c), "elements": word(base + 0x90), "element_bytes": word(base + 0xa4), "kind": byte(base + 0xb8), "surface_offset": pointer(base + 0xa8) - SURF, "next_message_number": half(base + 0xc8)} assert [(entry["surface_offset"], entry["element_bytes"], entry["elements"]) for entry in QUEUE_OBJECTS.values()] == [(0x200, 0x800, 16), (0x8200, 0x800, 16), (0x10200, 0x40, 16)], QUEUE_OBJECTS INFORMATION = bytes(cpu.mem_read(pointer(F_PMU + 0x610), 0x40)).hex() for changes in (dict(status=1), dict(status=3), dict(status=0xff), dict(unit=0xe), dict(kind=1), dict(offset=0, size=0x20), dict(offset=0x400, size=0x1000), dict(falcon=0xffff), dict(offset=0x100, size=0)): init_case(**changes) for tail in (1, 5, 15): init_case(tail=tail) # -------------------------------------------------------- the three requests # First open: region command and SEC2, then FECS, then GPCCS (transcript # 164966-165083 and 166380-167028 show two, one and one exchanges). ready() assert request_case(1, 7, 0, [(2, (1, 0, 0, 0, 4), 1), (4, (2, 0, 0, 0, 4), 2)], checks=(0,) * 8) == 0 assert request_case(1, 2, 0, [(1, (3, 0, 0, 0, 4), 3)], checks=(0,) * 8) == 0 assert request_case(1, 3, 0, [(3, (4, 0, 0, 0, 4), 4)], checks=(0,) * 8) == 0 for flags in (1, 7): request_case(1, 2, flags, [(1, (5 + (flags == 7), 0, 0, 0, 4), 5 + (flags == 7))], checks=(0,) * 4) request_case(1, 2, 8, []) # flags out of range request_case(1, 0x1f, 0, []) # Falcon number out of range request_case(1, 7, 1, []) # flags for a Falcon that takes none request_case(1, 0, 0, []) # the PMU itself # Failure and timeout branches, each from a fresh ready state. ready() request_case(2, 7, 0, [], checks=(0, 0, 0x65)) # no reply to the region command ready() request_case(3, 7, 0, [(1, (1, 0, 3, 0, 4), 1)], checks=(0,) * 4) # firmware status 3 ready() request_case(4, 7, 0, [(1, OK, 1)], checks=(0, 0, 0, 0x65)) # bootstrap command unanswered ready() request_case(5, 7, 0, [(1, OK, 1), (2, (2, 0, 0x2a, 0, 4), 2)], checks=(0,) * 6) ready() request_case(6, 7, 0, [(1, (9, 0, 0, 0, 4), 1), (2, (10, 0, 0, 0, 4), 2)], checks=(0,) * 6) ready() request_case(7, 7, 0, [(1, (1, 0, 0, 1, 4), 1), (2, (2, 0, 0, 0, 4), 2)], checks=(0,) * 6) ready() request_case(8, 7, 0, [(1, (1, 2, 0, 0, 4), 1), (2, (2, 2, 0, 0, 4), 2)], checks=(0,) * 6) ready() # fourteen exchanges, then the positions wrap for number in range(1, 15): assert rpc_case(9, bytes([0, 0x0a, 2, 0]) + bytes([number] * 0x14), [(1, (number, 0, 0, 0, 4), number)], checks=(0,) * 4) == 0 assert request_case(9, 7, 0, [(1, (15, 0, 0, 0, 4), 15), (2, (16, 0, 0, 0, 4), 0)], checks=(0,) * 6) == 0 assert request_case(9, 3, 0, [(2, (17, 0, 0, 0, 4), 1)], checks=(0,) * 6) == 0 assert state["reads"][0x10a804] == [1] and state["reads"][0x10a4cc] == [2] # ------------------------------------------------------------ raw requests ready() number = 1 for size in (0x10, 0x11, 0x18, 0x1c, 0x40, 0x100, 0x7ec, 0x7f0): payload = bytes([0, 0x0a, 2, 0]) + bytes((7 * index + size) & 0xff for index in range(size - 4)) status = rpc_case(10, payload, [(1, (number, 0, 0, 0, 4), number)], checks=(0,) * 4) number += status == 0 or state["reads"][0x10a4cc][0] == (number + 1) % 16 rpc_case(10, bytes([0, 0x0a, 2, 0]) + bytes(0x7f1 - 4), []) # too large rpc_case(10, bytes([1, 0x0a, 2, 0]) + bytes(0x14), []) # callback form, no callback begin("S") rpc_case(11, bytes([0, 0x0a, 2, 0]) + bytes(0x14), []) # before INIT out = ROOT / "target/gpu-stage" oracle = "\n".join(rows) + "\n" (out / "original-pmu-fbq.txt").write_text(oracle) report = { "original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "environment": "tools/check_gpu_pmu_ls_start_binary.py through its traced Load", "queue_objects_after_init": QUEUE_OBJECTS, "queue_information_bytes": INFORMATION, "cases": len(rows), "cases_by_kind": counts, "oracle": "target/gpu-stage/original-pmu-fbq.txt", "oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(), "scope": "Original GV100 PMU INIT handling, command post, reply receive and the " "light-secure manager's Falcon requests over the framebuffer queues. The " "firmware side (message bytes) is a fixture; no hardware executed."} (ROOT / "reports/gpu-pmu-fbq-original.json").write_text(json.dumps(report, indent=2) + "\n") print(f"{len(rows)} original PMU queue cases {counts}; oracle sha256 {report['oracle_sha256']}")