mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
186 lines
6.3 KiB
Python
186 lines
6.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Original DCB legacy-offset source priority and raw LE word getter in isolated RAM."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
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_rom_bit_dcb_elf import (
|
|
DIGEST,
|
|
ROOT,
|
|
discover_rm_object,
|
|
link_rom_functions,
|
|
parse_elf64_rm_object,
|
|
require,
|
|
require_unicorn,
|
|
write_or_check_json,
|
|
write_or_check_text,
|
|
)
|
|
|
|
TEXT = 0x10000000
|
|
DATA = 0x30000000
|
|
STACK = 0x40000000
|
|
RODATA = 0x50000000
|
|
STOP = 0x60000000
|
|
NAMES = ["_nv021800rm", "_nv021799rm", "_nv000056rm", "_nv000062rm", "_nv000069rm", "_nv000070rm"]
|
|
|
|
|
|
def build_parser() -> argparse.ArgumentParser:
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument(
|
|
"--check",
|
|
action="store_true",
|
|
help="Verify Rust vectors and JSON report without modifying files.",
|
|
)
|
|
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:
|
|
options = build_parser().parse_args(argv)
|
|
unicorn, x86 = require_unicorn()
|
|
|
|
rm_object = discover_rm_object(options.rm_object)
|
|
parsed = parse_elf64_rm_object(rm_object)
|
|
syms = parsed["defined"]
|
|
text, rodata = link_rom_functions(
|
|
parsed, NAMES, text_base=TEXT, rodata_base=RODATA, thunk_rax_addr=STOP + 0x100
|
|
)
|
|
|
|
uc = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_64)
|
|
uc.mem_map(TEXT, (len(text) + 4095) & ~4095)
|
|
uc.mem_write(TEXT, bytes(text))
|
|
uc.mem_map(RODATA, (len(rodata) + 4095) & ~4095)
|
|
uc.mem_write(RODATA, rodata)
|
|
uc.mem_map(DATA, 0x400000)
|
|
uc.mem_map(STACK, 0x10000)
|
|
uc.mem_map(STOP, 4096)
|
|
|
|
def address(n: str) -> int:
|
|
return TEXT + syms[n]["st_value"]
|
|
|
|
def w32(a: int, v: int) -> None:
|
|
uc.mem_write(a, struct.pack("<I", v))
|
|
|
|
def w64(a: int, v: int) -> None:
|
|
uc.mem_write(a, struct.pack("<Q", v))
|
|
|
|
def ret(value: int) -> None:
|
|
rsp = uc.reg_read(x86.UC_X86_REG_RSP)
|
|
rip = struct.unpack("<Q", uc.mem_read(rsp, 8))[0]
|
|
uc.reg_write(x86.UC_X86_REG_RAX, value)
|
|
uc.reg_write(x86.UC_X86_REG_RSP, rsp + 8)
|
|
uc.reg_write(x86.UC_X86_REG_RIP, rip)
|
|
|
|
addr_gpu_ret = (address("_nv049086rm"), address("_nv005245rm"))
|
|
addr_memset = address("_nv052705rm")
|
|
addr_logs = (
|
|
address("_nv047869rm"),
|
|
address("_nv047868rm"),
|
|
address("_nv046745rm"),
|
|
address("_nv046747rm"),
|
|
address("_nv046744rm"),
|
|
)
|
|
|
|
def hook(emu, pc, _size, _user):
|
|
if pc in addr_gpu_ret:
|
|
ret(DATA)
|
|
elif pc == addr_memset:
|
|
dst = emu.reg_read(x86.UC_X86_REG_RDI)
|
|
value = emu.reg_read(x86.UC_X86_REG_RSI) & 255
|
|
count = emu.reg_read(x86.UC_X86_REG_RDX)
|
|
emu.mem_write(dst, bytes([value]) * count)
|
|
ret(dst)
|
|
elif pc in addr_logs:
|
|
ret(0)
|
|
|
|
uc.hook_add(unicorn.UC_HOOK_CODE, hook)
|
|
rows = []
|
|
for primary in range(3):
|
|
for alternate in range(3):
|
|
for value in (0, 0x1234, 0xFFFF):
|
|
uc.mem_write(DATA, bytes(0x40000))
|
|
p = DATA + 0x20000
|
|
a = DATA + 0x30000
|
|
w64(DATA + 0x20C8, 0 if primary == 0 else p)
|
|
w64(DATA + 0x20D0, 0 if alternate == 0 else a)
|
|
w64(p + 0x2E8, DATA + 0x100000 if primary == 2 else 0)
|
|
w64(a + 0x130, DATA + 0x110000 if alternate == 2 else 0)
|
|
uc.mem_write(DATA + 0x100000 + 0x36, struct.pack("<H", value))
|
|
uc.mem_write(DATA + 0x110000 + 0x36, struct.pack("<H", value ^ 0xFFFF))
|
|
w32(DATA + 0x100, 0xDEADBEEF)
|
|
uc.reg_write(x86.UC_X86_REG_RDI, DATA)
|
|
uc.reg_write(x86.UC_X86_REG_RSI, p)
|
|
uc.reg_write(x86.UC_X86_REG_RDX, DATA + 0x100)
|
|
uc.reg_write(x86.UC_X86_REG_RSP, STACK + 0xFFE8)
|
|
uc.reg_write(x86.UC_X86_REG_RBP, STACK + 0x8000)
|
|
w64(STACK + 0xFFE8, STOP)
|
|
uc.emu_start(address("_nv021800rm"), STOP, count=20000)
|
|
require(uc.reg_read(x86.UC_X86_REG_RIP) == STOP, "_nv021800rm did not reach STOP")
|
|
rows.append(
|
|
{
|
|
"primary": primary,
|
|
"alternate": alternate,
|
|
"value": value,
|
|
"status": uc.reg_read(x86.UC_X86_REG_RAX) & 0xFFFFFFFF,
|
|
"offset": struct.unpack("<I", uc.mem_read(DATA + 0x100, 4))[0],
|
|
}
|
|
)
|
|
|
|
uc.reg_write(x86.UC_X86_REG_RSP, STACK + 0xFFE8)
|
|
w64(STACK + 0xFFE8, STOP)
|
|
uc.emu_start(address("_nv021799rm"), STOP, count=20000)
|
|
require(uc.reg_read(x86.UC_X86_REG_RAX) == 0x40, "unexpected _nv021799rm status")
|
|
|
|
lines = [
|
|
"// Actual original legacy-offset source priority and raw LEword getter.",
|
|
"const LEGACY_CASES: &[LegacyCase] = &[",
|
|
]
|
|
for c in rows:
|
|
lines.append(
|
|
f"LegacyCase {{ primary: {c['primary']}, alternate: {c['alternate']}, "
|
|
f"value: {c['value']}, status: {c['status']}, offset: {c['offset']} }},"
|
|
)
|
|
lines.append("];")
|
|
write_or_check_text(
|
|
ROOT / "src/nvidia/src/gpu/rom_dcb_offset_vectors.rs",
|
|
"\n".join(lines) + "\n",
|
|
options.check,
|
|
"PF ROM DCB offset Rust vectors",
|
|
)
|
|
report = {
|
|
"artifact_sha256": DIGEST,
|
|
"functions": NAMES,
|
|
"cases": len(rows),
|
|
"bit108_status": 0x40,
|
|
"rows": rows,
|
|
"interceptions": ["pure formatted-read output zero", "logs"],
|
|
"limits": [
|
|
"No native/hardware access",
|
|
"Rawword backed even if declaredview length unset: legacy doesnot consult declaredlength",
|
|
"Objectpresent/nullimage differs from missingobject and mustnotfall back",
|
|
],
|
|
}
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-pf-rom-dcb-offset-oracle.json",
|
|
report,
|
|
options.check,
|
|
"PF ROM DCB offset JSON report",
|
|
)
|
|
print(len(rows), "original DCB legacy priority cases")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|