mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
180 lines
8.4 KiB
Python
180 lines
8.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Run the original GV100 OBJMC StateLoad and write a comparison oracle.
|
|
|
|
Executed original code: the class body _nv043630rm, the GV100 HAL binder
|
|
_nv006001rm that populates the object's function table, and everything they
|
|
reach: the HAL load, the engine-to-enable-mask translation, the reset leaf and
|
|
the bit-vector helpers. Register access, the delay and diagnostics are
|
|
fixture-supplied. No hardware is involved.
|
|
"""
|
|
import hashlib
|
|
from _binary_path import resolve_580_binary
|
|
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_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,
|
|
UC_X86_REG_R8)
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
ORIGINAL = resolve_580_binary()
|
|
DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b"
|
|
TEXT, DATA, STACK, STOP = 0x10000000, 0x30000000, 0x40000000, 0x50000000
|
|
GPU, MC, CHILD, CHIP, RM, KERNEL_MC = (DATA, DATA + 0x10000, DATA + 0x20000, DATA + 0x30000,
|
|
DATA + 0x31000, DATA + 0x32000)
|
|
INDIRECT = STOP + 0x100
|
|
# The binder only stores pointers, so the functions it selects are listed.
|
|
ROOTS = ("_nv043630rm", "_nv006001rm", "_nv043573rm", "_nv043634rm", "_nv043619rm",
|
|
"_nv043639rm")
|
|
HOOKED = ("_nv053907rm", "_nv053912rm", "_nv049598rm", "_nv047869rm", "_nv046747rm",
|
|
"_nv046745rm", "_nv030608rm", "_nv052695rm", "_nv052705rm")
|
|
|
|
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())
|
|
text_section = elf.get_section_index(".text")
|
|
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())
|
|
import bisect # noqa: E402
|
|
|
|
offsets = [entry[0] for entry in relocations]
|
|
executed, pending = [], 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
|
|
else:
|
|
assert target["st_shndx"] == text_section, (name, target.name)
|
|
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):
|
|
entry = target["st_value"] + addend + 4
|
|
callee = next((symbol for symbol in starts if symbol[0] == entry), None)
|
|
if callee and callee[2] not in HOOKED:
|
|
pending.append(callee[2])
|
|
value = address + addend
|
|
if kind in (2, 4):
|
|
value -= TEXT + offset
|
|
else:
|
|
assert kind == 11, kind
|
|
struct.pack_into("<i", text, offset, value)
|
|
|
|
cpu = Uc(UC_ARCH_X86, UC_MODE_64)
|
|
for address, size in ((TEXT, (len(text) + 4095) & ~4095), (DATA, 0x40000), (STACK, 0x10000),
|
|
(STOP, 0x1000)):
|
|
cpu.mem_map(address, size)
|
|
cpu.mem_write(TEXT, bytes(text))
|
|
case = {}
|
|
trace = []
|
|
|
|
|
|
def leave(emu, value=0):
|
|
rsp = emu.reg_read(UC_X86_REG_RSP)
|
|
emu.reg_write(UC_X86_REG_RSP, rsp + 8)
|
|
emu.reg_write(UC_X86_REG_RAX, value)
|
|
emu.reg_write(UC_X86_REG_RIP, struct.unpack("<Q", emu.mem_read(rsp, 8))[0])
|
|
|
|
|
|
def leaf(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))
|
|
value = 0
|
|
if name == "_nv053907rm":
|
|
assert (a, b & 0xffffffff, c & 0xffffffff) == (GPU + 0x4120, 0, 0)
|
|
value = case["reads"].pop(0)
|
|
trace.append(f"r:{d & 0xffffffff:x}:{value:x}")
|
|
elif name == "_nv053912rm":
|
|
assert (a, b & 0xffffffff, c & 0xffffffff) == (GPU + 0x4120, 0, 0)
|
|
trace.append(f"w:{d & 0xffffffff:x}:{e & 0xffffffff:x}")
|
|
elif name == "_nv049598rm":
|
|
trace.append(f"t:{a & 0xffffffff:x}")
|
|
elif name == "_nv052695rm":
|
|
emu.mem_write(a, bytes(emu.mem_read(c, d & 0xffffffff)))
|
|
value = a
|
|
elif name == "_nv052705rm":
|
|
emu.mem_write(a, bytes([b & 0xff]) * (c & 0xffffffff))
|
|
value = a
|
|
elif name == "_nv030608rm":
|
|
raise AssertionError("engine 2 must not query the FIFO")
|
|
else:
|
|
trace.append(f"log:{name}")
|
|
leave(emu, value)
|
|
return run
|
|
|
|
|
|
cpu.hook_add(UC_HOOK_CODE, lambda emu, address, size, unused: emu.reg_write(
|
|
UC_X86_REG_RIP, emu.reg_read(UC_X86_REG_RAX)), begin=INDIRECT, end=INDIRECT)
|
|
for hooked in HOOKED:
|
|
entry = TEXT + by_name[hooked]["st_value"]
|
|
cpu.hook_add(UC_HOOK_CODE, leaf(hooked), begin=entry, end=entry)
|
|
|
|
|
|
def call(name, *arguments):
|
|
for register, argument in zip((UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX), arguments):
|
|
cpu.reg_write(register, argument)
|
|
rsp = STACK + 0xffc8
|
|
cpu.reg_write(UC_X86_REG_RSP, rsp)
|
|
cpu.reg_write(UC_X86_REG_RBP, STACK + 0x8000)
|
|
cpu.mem_write(rsp, struct.pack("<Q", STOP))
|
|
cpu.emu_start(TEXT + by_name[name]["st_value"], STOP, count=200000)
|
|
assert cpu.reg_read(UC_X86_REG_RIP) == STOP, name
|
|
return cpu.reg_read(UC_X86_REG_RAX) & 0xffffffff
|
|
|
|
|
|
cpu.mem_write(CHIP + 0x10, struct.pack("<H", 0x23))
|
|
cpu.mem_write(RM + 0x10, struct.pack("<H", 2))
|
|
call("_nv006001rm", MC, CHIP, RM)
|
|
bindings = {slot: struct.unpack("<Q", cpu.mem_read(MC + slot, 8))[0] - TEXT
|
|
for slot in (0x150, 0x158, 0x180, 0x190)}
|
|
names = {symbol[0]: symbol[2] for symbol in starts}
|
|
bound = {f"{slot:#x}": names[offset] for slot, offset in bindings.items()}
|
|
assert bound == {"0x150": "_nv043634rm", "0x158": "_nv043619rm", "0x180": "_nv043573rm",
|
|
"0x190": "_nv043639rm"}, bound
|
|
cpu.mem_write(GPU + 0x1ea8, struct.pack("<Q", KERNEL_MC))
|
|
rows = []
|
|
VALUES = (0x42001120, 0, 1, 0xffffffff, 0x1fecdff1)
|
|
for child, byte_190a, gpu_52fc, gpu_6d29 in itertools.product((0, 1), (0, 1), (0, 1), (0, 1)):
|
|
for first, second in itertools.product(VALUES, (0x5fecdff1, 0x1fecdff1)):
|
|
case["reads"] = [first, second]
|
|
trace.clear()
|
|
cpu.mem_write(GPU + 0x1f70, struct.pack("<Q", CHILD if child else 0))
|
|
cpu.mem_write(CHILD + 0x190a, bytes([byte_190a]))
|
|
cpu.mem_write(GPU + 0x52fc, bytes([gpu_52fc]))
|
|
cpu.mem_write(GPU + 0x6d29, bytes([gpu_6d29]))
|
|
status = call("_nv043630rm", GPU, MC)
|
|
assert not case["reads"]
|
|
rows.append(" ".join(f"{value:x}" for value in (child, byte_190a, gpu_52fc, gpu_6d29,
|
|
first, second))
|
|
+ f";{status:x};" + " ".join(trace))
|
|
out = ROOT / "target/gpu-stage"
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
oracle = "\n".join(rows) + "\n"
|
|
(out / "original-mc-load.txt").write_text(oracle)
|
|
report = {"original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "executed": sorted(executed),
|
|
"fixture_supplied": list(HOOKED) + ["memory copy and fill are performed as such"], "bindings": bound, "cases": len(rows),
|
|
"oracle": "target/gpu-stage/original-mc-load.txt",
|
|
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
|
|
"scope": "Original GV100 OBJMC StateLoad register sequence with the binder-selected "
|
|
"function table. Register values, the delay and diagnostics are "
|
|
"fixture-supplied; no hardware executed."}
|
|
(ROOT / "reports/gpu-mc-load-original.json").write_text(json.dumps(report, indent=2) + "\n")
|
|
print(f"{len(rows)} original MC load cases; executed {len(executed)} functions")
|