mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
682 lines
25 KiB
Python
682 lines
25 KiB
Python
#!/usr/bin/env python3
|
|
"""Recover ordered selected GPU metadata stores by executing original setters.
|
|
Instruction execution is isolated private RAM; production uses generated Rust
|
|
metadata, not proprietary executable code or a captured runtime heap.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
|
|
if str(Path(__file__).resolve().parent) not in sys.path:
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
from _pf_hal_channel_elf import (
|
|
ROOT,
|
|
SHN_ABS,
|
|
SHN_UNDEF,
|
|
STT_FUNC,
|
|
discover_rm_object,
|
|
parse_elf64_rm_object,
|
|
require,
|
|
require_unicorn,
|
|
write_or_check_json,
|
|
write_or_check_text,
|
|
)
|
|
|
|
TEXT = 0x10000000
|
|
RO = 0x18000000
|
|
DATA = 0x21000000
|
|
BSS = 0x22000000
|
|
GPU = 0x24000000
|
|
STACK = 0x40000000
|
|
STUB = 0x50000000
|
|
STOP = 0x60000000
|
|
|
|
|
|
def build_parser() -> argparse.ArgumentParser:
|
|
p = argparse.ArgumentParser(
|
|
description="Recover/verify 580.178.04 GPU PF constructor metadata and binary oracle vectors."
|
|
)
|
|
p.add_argument(
|
|
"--check",
|
|
action="store_true",
|
|
help="Verify all generated Rust vector files and JSON reports without writing.",
|
|
)
|
|
p.add_argument(
|
|
"--rm-object",
|
|
default=None,
|
|
help="Path to 580.178.04 nv-kernel.o_binary (overrides auto-discovery).",
|
|
)
|
|
return p
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
opt = build_parser().parse_args(argv)
|
|
unicorn, x86 = require_unicorn()
|
|
|
|
rm_object = discover_rm_object(opt.rm_object)
|
|
parsed = parse_elf64_rm_object(rm_object)
|
|
digest = parsed["digest"]
|
|
symbols = parsed["symbols"]
|
|
defined = parsed["defined"]
|
|
|
|
text_idx = parsed["section_index"](".text")
|
|
bases = {
|
|
text_idx: TEXT,
|
|
parsed["section_index"](".rodata"): RO,
|
|
parsed["section_index"](".data"): DATA,
|
|
parsed["section_index"](".bss"): BSS,
|
|
}
|
|
buffers = {name: bytearray(parsed["section_bytes"](name)) for name in (".text", ".rodata", ".data")}
|
|
sizes = {name: len(buf) for name, buf in buffers.items()}
|
|
undefined: dict[str, int] = {}
|
|
hooks: dict[int, str] = {}
|
|
targets: dict[int, int] = {}
|
|
funcs = {
|
|
TEXT + s["st_value"]: s["name"]
|
|
for s in symbols
|
|
if s["st_shndx"] == text_idx and s["st_type"] == STT_FUNC and s["name"]
|
|
}
|
|
|
|
for name, buf in buffers.items():
|
|
base = bases[parsed["section_index"](name)]
|
|
for off, index, kind, addend in parsed["iter_relocations"](".rela" + name):
|
|
if index not in targets:
|
|
symbol = symbols[index]
|
|
shndx = symbol["st_shndx"]
|
|
target = bases.get(shndx)
|
|
if target is not None:
|
|
target += symbol["st_value"]
|
|
elif shndx == SHN_UNDEF:
|
|
target = undefined.setdefault(
|
|
symbol["name"], STUB + 0x100 + len(undefined) * 16
|
|
)
|
|
hooks[target] = symbol["name"]
|
|
elif shndx == SHN_ABS:
|
|
target = symbol["st_value"]
|
|
else:
|
|
raise RuntimeError(f"Unsupported symbol section {(symbol['name'], shndx)}")
|
|
targets[index] = target
|
|
target = targets[index]
|
|
value = target + addend - (base + off) if kind in (2, 4) else target + addend
|
|
if kind == 1:
|
|
struct.pack_into("<Q", buf, off, value & 0xFFFFFFFFFFFFFFFF)
|
|
elif kind in (2, 4, 10, 11):
|
|
struct.pack_into("<I", buf, off, value & 0xFFFFFFFF)
|
|
else:
|
|
raise RuntimeError(f"Unsupported relocation kind {kind}")
|
|
|
|
uc = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_64)
|
|
for name, buf in buffers.items():
|
|
base = bases[parsed["section_index"](name)]
|
|
uc.mem_map(base, (len(buf) + 4095) & ~4095)
|
|
uc.mem_write(base, bytes(buf))
|
|
uc.mem_map(BSS, 0x200000)
|
|
uc.mem_map(GPU, 0x10000)
|
|
uc.mem_map(STACK, 0x10000)
|
|
uc.mem_map(STACK + 0x10000, 4096)
|
|
uc.mem_map(STUB, 0x10000)
|
|
uc.mem_map(STOP, 4096)
|
|
uc.mem_map(GPU + 0x10000, 0x20000)
|
|
|
|
visited: set[str] = set()
|
|
state = {
|
|
"stores": [],
|
|
"allocations": 0,
|
|
"alloc_fail": 99,
|
|
"alloc_next": GPU + 0x10000,
|
|
"mmio": [],
|
|
"read_pattern": 0,
|
|
}
|
|
|
|
hooks[TEXT + defined["_nv052687rm"]["st_value"]] = "allocate_leaf"
|
|
for name in ("_nv047869rm", "_nv046745rm"):
|
|
hooks[TEXT + defined[name]["st_value"]] = "diagnostic_leaf"
|
|
|
|
def ret(value: int = 0) -> None:
|
|
sp = uc.reg_read(x86.UC_X86_REG_RSP)
|
|
target = int.from_bytes(uc.mem_read(sp, 8), "little")
|
|
uc.reg_write(x86.UC_X86_REG_RAX, value)
|
|
uc.reg_write(x86.UC_X86_REG_RSP, sp + 8)
|
|
uc.reg_write(x86.UC_X86_REG_RIP, target)
|
|
|
|
def original_code(emu, address, _size, _user):
|
|
if address == STOP:
|
|
emu.emu_stop()
|
|
return
|
|
if address in funcs:
|
|
visited.add(funcs[address])
|
|
name = hooks.get(address)
|
|
if name is None:
|
|
return
|
|
if name.startswith("__x86_indirect_thunk_"):
|
|
reg_id = getattr(x86, "UC_X86_REG_" + name.split("_")[-1].upper())
|
|
emu.reg_write(x86.UC_X86_REG_RIP, emu.reg_read(reg_id))
|
|
return
|
|
if name == "diagnostic_leaf":
|
|
ret()
|
|
return
|
|
if name == "allocate_leaf":
|
|
index = state["allocations"]
|
|
state["allocations"] += 1
|
|
if index == state["alloc_fail"]:
|
|
ret()
|
|
return
|
|
result = state["alloc_next"]
|
|
state["alloc_next"] += (emu.reg_read(x86.UC_X86_REG_RDI) + 15) & ~15
|
|
ret(result)
|
|
return
|
|
if name == "os_mem_set":
|
|
addr = emu.reg_read(x86.UC_X86_REG_RDI)
|
|
emu.mem_write(
|
|
addr,
|
|
bytes([emu.reg_read(x86.UC_X86_REG_RSI) & 255]) * emu.reg_read(x86.UC_X86_REG_RDX),
|
|
)
|
|
ret(addr)
|
|
return
|
|
if name == "os_mem_copy":
|
|
addr = emu.reg_read(x86.UC_X86_REG_RDI)
|
|
emu.mem_write(
|
|
addr,
|
|
bytes(emu.mem_read(emu.reg_read(x86.UC_X86_REG_RSI), emu.reg_read(x86.UC_X86_REG_RDX))),
|
|
)
|
|
ret(addr)
|
|
return
|
|
if name in ("os_log", "os_dbg_breakpoint", "os_debug_breakpoint"):
|
|
ret()
|
|
return
|
|
raise RuntimeError(f"unexpected external service {(name, hex(address))}")
|
|
|
|
def write(_emu, _access, address, size, value, _user):
|
|
if GPU <= address < GPU + 0x6E70:
|
|
state["stores"].append((address - GPU, size, value & ((1 << (size * 8)) - 1)))
|
|
|
|
def invalid(emu, _access, address, _size, _value, _user):
|
|
pc = emu.reg_read(x86.UC_X86_REG_RIP)
|
|
nearest = max((a for a in funcs if a <= pc), default=0)
|
|
print(
|
|
"INVALID",
|
|
hex(address),
|
|
"PC",
|
|
hex(pc),
|
|
funcs.get(nearest),
|
|
hex(pc - nearest),
|
|
"RBP",
|
|
hex(emu.reg_read(x86.UC_X86_REG_RBP)),
|
|
flush=True,
|
|
)
|
|
return False
|
|
|
|
code_hook = uc.hook_add(unicorn.UC_HOOK_CODE, original_code)
|
|
uc.hook_add(unicorn.UC_HOOK_MEM_WRITE, write)
|
|
uc.hook_add(unicorn.UC_HOOK_MEM_INVALID, invalid)
|
|
|
|
zero_regs = [
|
|
x86.UC_X86_REG_RAX,
|
|
x86.UC_X86_REG_RBX,
|
|
x86.UC_X86_REG_RCX,
|
|
x86.UC_X86_REG_RDX,
|
|
x86.UC_X86_REG_R8,
|
|
x86.UC_X86_REG_R9,
|
|
x86.UC_X86_REG_R10,
|
|
x86.UC_X86_REG_R11,
|
|
x86.UC_X86_REG_R12,
|
|
x86.UC_X86_REG_R13,
|
|
x86.UC_X86_REG_R14,
|
|
x86.UC_X86_REG_R15,
|
|
]
|
|
arg_regs = [
|
|
x86.UC_X86_REG_RDI,
|
|
x86.UC_X86_REG_RSI,
|
|
x86.UC_X86_REG_RDX,
|
|
x86.UC_X86_REG_RCX,
|
|
x86.UC_X86_REG_R8,
|
|
x86.UC_X86_REG_R9,
|
|
]
|
|
|
|
def call(name: str, args: list[int]) -> None:
|
|
for reg in zero_regs:
|
|
uc.reg_write(reg, 0)
|
|
for reg, value in zip(arg_regs, args):
|
|
uc.reg_write(reg, value)
|
|
uc.mem_write(STACK, bytes(0x10000))
|
|
uc.reg_write(x86.UC_X86_REG_RBP, STACK + 0x8000)
|
|
uc.reg_write(x86.UC_X86_REG_RSP, STACK + 0xFFF8)
|
|
uc.mem_write(STACK + 0xFFF8, struct.pack("<Q", STOP))
|
|
for i, value in enumerate(args[6:]):
|
|
uc.mem_write(STACK + 0x10000 + i * 8, struct.pack("<Q", value))
|
|
uc.emu_start(TEXT + defined[name]["st_value"], STOP, count=1000000)
|
|
require(uc.reg_read(x86.UC_X86_REG_RIP) == STOP, f"{name} did not reach STOP")
|
|
|
|
def normalized_image(address: int, size: int) -> bytes:
|
|
image = bytearray(uc.mem_read(address, size))
|
|
for off in range(0, len(image) - 7, 8):
|
|
value = struct.unpack_from("<Q", image, off)[0]
|
|
if TEXT <= value < TEXT + sizes[".text"]:
|
|
value = value - TEXT + 0x0616F810
|
|
elif RO <= value < RO + sizes[".rodata"]:
|
|
value = value - RO + 0x0111D660
|
|
elif DATA <= value < DATA + sizes[".data"]:
|
|
value = value - DATA + 0x00F7E890
|
|
elif BSS <= value < BSS + 0x19D688:
|
|
value = value - BSS + 0x05FD2180
|
|
struct.pack_into("<Q", image, off, value)
|
|
return bytes(image)
|
|
|
|
outputs = []
|
|
binding_len = 0
|
|
for pattern in (0, 0xA5):
|
|
uc.mem_write(GPU, bytes([pattern]) * 0x6E70)
|
|
state["stores"] = []
|
|
call("_nv005722rm", [GPU, 0x14, 0, 0, 0, 3, 0])
|
|
binding_len = len(state["stores"])
|
|
call("_nv005825rm", [GPU])
|
|
outputs.append(state["stores"].copy())
|
|
require(outputs[0] == outputs[1], "Metadata unexpectedly depends on prior image bytes")
|
|
|
|
rows = []
|
|
for off, size, value in outputs[0]:
|
|
if GPU <= value < GPU + 0x6E70:
|
|
kind = "Gpu"
|
|
value -= GPU
|
|
elif TEXT <= value < TEXT + sizes[".text"]:
|
|
kind = "Constant"
|
|
value = value - TEXT + 0x0616F810
|
|
elif RO <= value < RO + sizes[".rodata"]:
|
|
kind = "Constant"
|
|
value = value - RO + 0x0111D660
|
|
elif DATA <= value < DATA + sizes[".data"]:
|
|
kind = "Constant"
|
|
value = value - DATA + 0x00F7E890
|
|
elif BSS <= value < BSS + 0x19D688:
|
|
raise RuntimeError("unexpected BSS metadata")
|
|
else:
|
|
kind = "Constant"
|
|
rows.append((off, size * 8, kind, value))
|
|
|
|
code = (
|
|
"// Actual original005722 +005825 instruction-store trace; chipHAL23/runtime3.\n"
|
|
"#[rustfmt::skip]\n"
|
|
"const GV100_PF_METADATA:&[Store]=&[\n"
|
|
+ "".join(
|
|
f"Store{{offset:{off},width:{width},value:Value::{kind}({value})}},\n"
|
|
for off, width, kind, value in rows
|
|
)
|
|
+ "];\n"
|
|
)
|
|
write_or_check_text(
|
|
ROOT / "src/nvidia/src/gpu/pf_constructor_metadata.rs",
|
|
code,
|
|
opt.check,
|
|
"Original constructor metadata",
|
|
)
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-pf-constructor-metadata.json",
|
|
{
|
|
"artifact_sha256": digest,
|
|
"ordered_stores": len(rows),
|
|
"binding_stores": binding_len,
|
|
"field_initializer_stores": len(rows) - binding_len,
|
|
"chip_hal": 0x23,
|
|
"runtime": 3,
|
|
"rm_hal": 2,
|
|
"architecture": 0x14,
|
|
"implementation": 0,
|
|
"soc": 0,
|
|
"firmware": 0,
|
|
"initial_patterns": [0, 0xA5],
|
|
"executed_functions": sorted(visited),
|
|
"physical_gpu_access": False,
|
|
},
|
|
opt.check,
|
|
"PF constructor metadata report",
|
|
)
|
|
print(len(rows), "actual ordered metadata stores", len(visited), "original functions executed")
|
|
|
|
ctor_rows = []
|
|
for case in range(64):
|
|
pattern = 0xA5 if case & 1 else 0
|
|
state["alloc_fail"] = [0, 1, 99, 99][case % 4]
|
|
state["alloc_next"] = GPU + 0x10000
|
|
state["allocations"] = 0
|
|
uc.mem_write(GPU, bytes(0x10000))
|
|
uc.mem_write(GPU + 0x10000, bytes([pattern]) * 0x10000)
|
|
uuid = bytes((case + i) & 255 for i in range(16))
|
|
uc.mem_write(GPU + 0x20000, uuid)
|
|
call("_nv005722rm", [GPU, 0x14, 0, 0, 0, 3, 0])
|
|
call("_nv003368rm", [GPU, case, case * 17, GPU + 0x20000 if case & 2 else 0, GPU + 0x30000])
|
|
result = uc.reg_read(x86.UC_X86_REG_RAX) & 0xFFFFFFFF
|
|
image = normalized_image(GPU, 0x6E70) + bytes(uc.mem_read(GPU + 0x10000, 0xCC))
|
|
if not opt.check:
|
|
(ROOT / "target/pf-software").mkdir(parents=True, exist_ok=True)
|
|
(ROOT / "target/pf-software" / f"original-{case}.bin").write_bytes(image)
|
|
fnv = 14695981039346656037
|
|
for byte in image:
|
|
fnv = ((fnv ^ byte) * 1099511628211) & 0xFFFFFFFFFFFFFFFF
|
|
ctor_rows.append([case, fnv, result, state["allocations"]])
|
|
|
|
code = (
|
|
"// Actual original005722 +003368/005825/030237/030235 full CPU constructors.\n"
|
|
"#[rustfmt::skip]\n"
|
|
"const ORIGINAL_CONSTRUCTOR_VECTORS:&[(u32,u64,u32,u32)]=&[\n"
|
|
+ "".join(
|
|
f"({case},{h},{status},{allocs}),\n" for case, h, status, allocs in ctor_rows
|
|
)
|
|
+ "];\n"
|
|
)
|
|
write_or_check_text(
|
|
ROOT / "src/nvidia/src/gpu/pf_constructor_original_vectors.rs",
|
|
code,
|
|
opt.check,
|
|
"Original full constructor vectors",
|
|
)
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-pf-constructor-binary.json",
|
|
{
|
|
"artifact_sha256": digest,
|
|
"full_original_constructor_cases": len(ctor_rows),
|
|
"executed_functions": sorted(visited - {"_nv052687rm", "_nv047869rm", "_nv046745rm"}),
|
|
"entered_but_scripted_leaves": ["_nv052687rm", "_nv047869rm", "_nv046745rm"],
|
|
"scripted_external_leaves": ["OS allocate", "memset", "memcpy", "diagnostics"],
|
|
"physical_gpu_access": False,
|
|
"cases": ctor_rows,
|
|
},
|
|
opt.check,
|
|
"PF constructor binary report",
|
|
)
|
|
print(len(ctor_rows), "full actual original CPU constructor cases")
|
|
|
|
arch_addr = GPU + 0x30000
|
|
output_addr = GPU + 0x40000
|
|
uc.mem_map(arch_addr, 0x2000)
|
|
uc.mem_map(output_addr, 0x1000)
|
|
architecture_images = []
|
|
for pattern in (0, 0xA5):
|
|
uc.mem_write(arch_addr, bytes([pattern]) * 0x98)
|
|
uc.mem_write(output_addr, bytes(8))
|
|
state["alloc_next"] = arch_addr
|
|
state["alloc_fail"] = 99
|
|
state["allocations"] = 0
|
|
call("_nv006506rm", [output_addr, 0, 0, 0x14, 0, 0, 0, 0x14, 0, 0, 0])
|
|
require(uc.reg_read(x86.UC_X86_REG_RAX) == 0, "GpuArch constructor failed")
|
|
require(
|
|
int.from_bytes(uc.mem_read(output_addr, 8), "little") == arch_addr
|
|
and state["allocations"] == 1,
|
|
"GpuArch allocation mismatch",
|
|
)
|
|
architecture_images.append(normalized_image(arch_addr, 0x98))
|
|
require(architecture_images[0] == architecture_images[1], "GpuArch depends on prior memory")
|
|
|
|
code = (
|
|
"// Actual full006506/005671/005882/003322/031300 root GpuArch construction.\n"
|
|
"const ORIGINAL_ARCHITECTURE:[u8;0x98]=["
|
|
+ ",".join(map(str, architecture_images[0]))
|
|
+ "];\n"
|
|
)
|
|
write_or_check_text(
|
|
ROOT / "src/nvidia/src/gpu/gpu_arch_original.rs",
|
|
code,
|
|
opt.check,
|
|
"Original architecture constructor",
|
|
)
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-architecture-binary.json",
|
|
{
|
|
"artifact_sha256": digest,
|
|
"actual_root_architecture_constructions": 2,
|
|
"logical_base": arch_addr,
|
|
"initial_patterns": [0, 0xA5],
|
|
"canonical_image_sha256": hashlib.sha256(architecture_images[0]).hexdigest(),
|
|
"selected_factors": [0x14, 0, 0, 0],
|
|
"physical_gpu_access": False,
|
|
},
|
|
opt.check,
|
|
"GPU architecture binary report",
|
|
)
|
|
print("2 full actual GpuArch constructions")
|
|
|
|
sys_addr = GPU + 0x50000
|
|
mgr_addr = GPU + 0x60000
|
|
hal_addr = GPU + 0x70000
|
|
ifaces_addr = GPU + 0x80000
|
|
for address in (sys_addr, mgr_addr, hal_addr, ifaces_addr):
|
|
uc.mem_map(address, 4096)
|
|
uc.mem_write(output_addr, bytes(8))
|
|
state["alloc_next"] = hal_addr
|
|
state["alloc_fail"] = 99
|
|
state["allocations"] = 0
|
|
call("_nv006656rm", [output_addr, 0, 0])
|
|
require(
|
|
uc.reg_read(x86.UC_X86_REG_RAX) == 0 and state["allocations"] == 1,
|
|
"OBJHAL root construction failed",
|
|
)
|
|
uc.mem_write(hal_addr + 0x48, struct.pack("<Q", RO + 0x0113B3F0 - 0x0111D660))
|
|
hal_image = normalized_image(hal_addr, 0x50)
|
|
call("_nv054818rm", [ifaces_addr])
|
|
iface_image = normalized_image(ifaces_addr, 205 * 8)
|
|
uc.mem_write(BSS + defined["_nv029075rm"]["st_value"], struct.pack("<Q", sys_addr))
|
|
uc.mem_write(sys_addr + 0x258, struct.pack("<Q", mgr_addr))
|
|
uc.mem_write(mgr_addr, bytes(4096))
|
|
uc.mem_write(mgr_addr + 0x48 + 35 * 8, struct.pack("<Q", hal_addr))
|
|
|
|
boot_rows = []
|
|
for boot42 in (0, 0x14000000, 0x14100000, 0x16000000, 0xFFFFFFFF):
|
|
for boot0 in (0, 0x140000A1, 0x23A0, 0x2300):
|
|
for soc in (0, 0x23A0):
|
|
for pattern in (0, 0xA5):
|
|
uc.mem_write(GPU, bytes([pattern]) * 0x6E70)
|
|
call("_nv030179rm", [GPU, boot0, boot42, soc])
|
|
status = uc.reg_read(x86.UC_X86_REG_RAX) & 0xFFFFFFFF
|
|
boot_rows.append(
|
|
[
|
|
boot0,
|
|
boot42,
|
|
soc,
|
|
pattern,
|
|
status,
|
|
int.from_bytes(uc.mem_read(GPU + 0x9E8, 4), "little"),
|
|
int.from_bytes(uc.mem_read(GPU + 0x1E68, 8), "little"),
|
|
]
|
|
)
|
|
|
|
code = (
|
|
"// Original root006656 +053989 setup assignment,054818 and030179/034616/034617.\n"
|
|
"const ORIGINAL_HAL:[u8;0x50]=["
|
|
+ ",".join(map(str, hal_image))
|
|
+ "];\n#[rustfmt::skip]\nconst ORIGINAL_INTERFACES:[u64;205]=["
|
|
+ ",".join(str(v[0]) for v in struct.iter_unpack("<Q", iface_image))
|
|
+ "];\n#[rustfmt::skip]\nconst ORIGINAL_BOOT_ROWS:&[(u32,u32,u32,u8,u32,u32,u64)]=&[\n"
|
|
+ "".join("(" + ",".join(map(str, row)) + "),\n" for row in boot_rows)
|
|
+ "];\n"
|
|
)
|
|
write_or_check_text(
|
|
ROOT / "src/nvidia/src/gpu/legacy_hal_original.rs",
|
|
code,
|
|
opt.check,
|
|
"Legacy HAL original vectors",
|
|
)
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-legacy-hal-binary.json",
|
|
{
|
|
"artifact_sha256": digest,
|
|
"actual_root_006656_constructions": 1,
|
|
"setup_publication": "original053989 direct assignment reproduced; its parent manager not emulated",
|
|
"actual_054818_entries": 205,
|
|
"actual_030179_bindings": len(boot_rows),
|
|
"physical_gpu_access": False,
|
|
"cases": boot_rows,
|
|
},
|
|
opt.check,
|
|
"Legacy HAL binary report",
|
|
)
|
|
print(len(boot_rows), "actual BOOT/HAL bindings;205 actual interface copies")
|
|
|
|
aperture_addr = GPU + 0x90000
|
|
uc.mem_map(aperture_addr, 4096)
|
|
prefix_rows = []
|
|
aperture_image = None
|
|
for length in (0x1000, 0x200000, 0xFFFFFFFF):
|
|
for failed in (False, True):
|
|
uc.mem_write(GPU, bytes(0x6E70))
|
|
uc.mem_write(aperture_addr, bytes([0xA5]) * 0x88)
|
|
uc.mem_write(GPU + 0xE30, struct.pack("<Q", 0xF0000000))
|
|
uc.mem_write(GPU + 0xE40, struct.pack("<I", length))
|
|
state["alloc_next"] = aperture_addr
|
|
state["alloc_fail"] = 0 if failed else 99
|
|
state["allocations"] = 0
|
|
call("_nv053897rm", [GPU + 0x4120, GPU])
|
|
status = uc.reg_read(x86.UC_X86_REG_RAX) & 0xFFFFFFFF
|
|
prefix_rows.append(
|
|
[
|
|
length,
|
|
failed,
|
|
status,
|
|
int.from_bytes(uc.mem_read(GPU + 0x4120, 8), "little"),
|
|
int.from_bytes(uc.mem_read(GPU + 0x1D98, 8), "little"),
|
|
int.from_bytes(uc.mem_read(GPU + 0x1D30, 8), "little"),
|
|
]
|
|
)
|
|
if length == 0x200000 and not failed:
|
|
aperture_image = normalized_image(aperture_addr, 0x88)
|
|
require(aperture_image is not None, "missing canonical aperture image")
|
|
|
|
code = (
|
|
"// Actual053897/030477/006535/003332/037840, allocation leaf scripted.\n"
|
|
"const ORIGINAL_APERTURE:[u8;0x88]=["
|
|
+ ",".join(map(str, aperture_image))
|
|
+ "];\n"
|
|
)
|
|
write_or_check_text(
|
|
ROOT / "src/nvidia/src/gpu/register_aperture_original.rs",
|
|
code,
|
|
opt.check,
|
|
"Original aperture prefix",
|
|
)
|
|
code = (
|
|
"// Original053897 complete normal-PCI allocation prefixes.\n"
|
|
"#[rustfmt::skip]\n"
|
|
"const ORIGINAL_APERTURE_PREFIX:&[(u32,bool,u32,u64,u64,u64)]=&[\n"
|
|
+ "".join("(" + ",".join(str(v).lower() for v in row) + "),\n" for row in prefix_rows)
|
|
+ "];\n"
|
|
)
|
|
write_or_check_text(
|
|
ROOT / "tests/pf-software/aperture_vectors.rs",
|
|
code,
|
|
opt.check,
|
|
"Original aperture host vectors",
|
|
)
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-register-aperture-binary.json",
|
|
{
|
|
"artifact_sha256": digest,
|
|
"actual_053897_prefix_cases": len(prefix_rows),
|
|
"actual_functions": [
|
|
"053897",
|
|
"030477",
|
|
"006535",
|
|
"005671",
|
|
"003332",
|
|
"037840",
|
|
"005757",
|
|
"005738",
|
|
"003389",
|
|
"003413",
|
|
"052705",
|
|
],
|
|
"scripted_leaves": ["CPUallocation", "memset", "diagnostics"],
|
|
"physical_gpu_access": False,
|
|
"cases": prefix_rows,
|
|
},
|
|
opt.check,
|
|
"Register aperture binary report",
|
|
)
|
|
print(len(prefix_rows), "complete original normalPCI register-access prefixes")
|
|
|
|
call("_nv006032rm", [ifaces_addr, arch_addr + 0x38])
|
|
selected_reset = int.from_bytes(uc.mem_read(ifaces_addr + 0x78, 8), "little")
|
|
require(
|
|
selected_reset == TEXT + defined["_nv052252rm"]["st_value"],
|
|
"unexpected selected PMU reset handler",
|
|
)
|
|
hooks[TEXT + defined["_nv053912rm"]["st_value"]] = "reset_mmio_write"
|
|
hooks[TEXT + defined["_nv053907rm"]["st_value"]] = "reset_mmio_read"
|
|
|
|
def reset_code(emu, address, size, user):
|
|
name = hooks.get(address)
|
|
if name == "reset_mmio_write":
|
|
offset = emu.reg_read(x86.UC_X86_REG_RCX)
|
|
value = emu.reg_read(x86.UC_X86_REG_R8) & 0xFFFFFFFF
|
|
require(offset == 0x10A3C0, f"unexpected reset write offset {offset:#x}")
|
|
state["mmio"].append((1, offset, value))
|
|
ret()
|
|
return
|
|
if name == "reset_mmio_read":
|
|
offset = emu.reg_read(x86.UC_X86_REG_RCX)
|
|
require(offset == 0x10A3C0, f"unexpected reset read offset {offset:#x}")
|
|
state["mmio"].append((2, offset, state["read_pattern"]))
|
|
ret(state["read_pattern"])
|
|
return
|
|
original_code(emu, address, size, user)
|
|
|
|
uc.hook_del(code_hook)
|
|
uc.hook_add(unicorn.UC_HOOK_CODE, reset_code)
|
|
|
|
reset_rows = []
|
|
for argument in (0, 1, 2, 255):
|
|
for rp in (0, 0xFFFFFFFF, 0x12345678, 0x80000001):
|
|
state["read_pattern"] = rp
|
|
state["mmio"] = []
|
|
call("_nv052252rm", [GPU, 0, argument])
|
|
require(uc.reg_read(x86.UC_X86_REG_RAX) == 0, "PMU reset returned non-zero")
|
|
require(
|
|
state["mmio"] == [(1, 0x10A3C0, int(argument != 0))] + [(2, 0x10A3C0, rp)] * 10,
|
|
"unexpected PMU reset MMIO trace",
|
|
)
|
|
h = 14695981039346656037
|
|
for event in state["mmio"]:
|
|
for byte in struct.pack("<III", *event):
|
|
h = ((h ^ byte) * 1099511628211) & 0xFFFFFFFFFFFFFFFF
|
|
reset_rows.append([argument, rp, h])
|
|
|
|
code_text = (
|
|
"// Original006032 chipHAL23 selection and052252 instructions.\n"
|
|
"#[rustfmt::skip]\n"
|
|
"const RESET_VECTORS:&[(u8,u32,u64)]=&[\n"
|
|
+ "".join("(" + ",".join(map(str, row)) + "),\n" for row in reset_rows)
|
|
+ "];\n"
|
|
)
|
|
write_or_check_text(
|
|
ROOT / "src/nvidia/src/gpu/pmu_reset_original.rs",
|
|
code_text,
|
|
opt.check,
|
|
"Original selected PMU reset vectors",
|
|
)
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-pmu-reset-binary.json",
|
|
{
|
|
"artifact_sha256": digest,
|
|
"actual_selected_setter": "006032",
|
|
"chip_hal": 35,
|
|
"method_offset": 0x78,
|
|
"selected_body": "052252",
|
|
"actual_reset_executions": len(reset_rows),
|
|
"scripted_leaves": ["checked register read", "checked register write"],
|
|
"register": 0x10A3C0,
|
|
"physical_gpu_access": False,
|
|
"cases": reset_rows,
|
|
},
|
|
opt.check,
|
|
"PMU reset binary report",
|
|
)
|
|
print(len(reset_rows), "actual selected PMU reset transcripts")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|