Files
nvdriver/tools/generate_pf_rom_source_oracle.py
morebrackets 1aa634f64e Keep initialized as u8 integer in generate_pf_rom_source_oracle.py and test live --check
- Emit integer initialized ({c['initialized']}) matching Case.initialized: u8
  in src/nvidia/src/gpu/rom_source_vectors.rs instead of Rust bool.
- Add test_live_check_mode_all_five_generators_match_checked_in_artifacts
  running all five generators with python3 -O --check when nv-kernel.o_binary
  and unicorn are installed.
2026-12-02 16:28:07 -08:00

268 lines
8.6 KiB
Python

#!/usr/bin/env python3
"""Original source-policy 060180 and caller state commit 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_source_acpi_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
BSS = 0x70000000
NAMES = [
"_nv059933rm",
"_nv060180rm",
"_nv060173rm",
"_nv006018rm",
"_nv005963rm",
"_nv006019rm",
"_nv060357rm",
"_nv060341rm",
"_nv060376rm",
"_nv060374rm",
"_nv060344rm",
"_nv024179rm",
"_nv024177rm",
"_nv024175rm",
"_nv023283rm",
"_nv023282rm",
"_nv023281rm",
"_nv023280rm",
"_nv023279rm",
]
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:
args = build_parser().parse_args(argv)
unicorn, x86 = require_unicorn()
rm_object = discover_rm_object(args.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,
bss_base=BSS,
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)
uc.mem_map(BSS, 0x10000)
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)
vbios = DATA + 0x20000
display = DATA + 0x30000
child = DATA + 0x40000
chip = DATA + 0x50000
rm = DATA + 0x51000
runtime = DATA + 0x60000
interface = DATA + 0x61000
vtable = DATA + 0x62000
st = {"trace": [], "current": None}
stop_pc = TEXT + (0x63CCFC9 - 0x616F810)
addr_rd32 = address("_nv053907rm")
def hook(emu, pc, _size, _user):
if pc == STOP + 0x100:
emu.reg_write(x86.UC_X86_REG_RIP, emu.reg_read(x86.UC_X86_REG_RAX))
elif pc == addr_rd32:
offset = emu.reg_read(x86.UC_X86_REG_RCX)
require(offset == 0x1580, f"unexpected strap read {offset:#x}")
st["trace"].append([1, offset])
ret(st["current"][6])
elif pc == STOP + 0x200:
offset = emu.reg_read(x86.UC_X86_REG_RSI)
st["trace"].append([2, offset])
require(offset in (0x80, 0x625480, 0x610490, 0x610630), f"unexpected disp read {offset:#x}")
ret(st["current"][7] if offset in (0x80, 0x625480) else st["current"][8])
elif pc == stop_pc:
emu.emu_stop()
uc.hook_add(unicorn.UC_HOOK_CODE, hook)
arg_regs = [x86.UC_X86_REG_RDI, x86.UC_X86_REG_RSI, x86.UC_X86_REG_RDX]
def call(name: str, call_args: list[int]) -> None:
for reg, value in zip(arg_regs, call_args):
uc.reg_write(reg, value)
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(name), STOP, count=50000)
require(uc.reg_read(x86.UC_X86_REG_RIP) == STOP, f"{name} did not reach STOP")
base = [0, 0, 1, 0, 1, 9, 1, 0x100, 7 << 16]
specs = []
for i, vals in (
(0, [0, 1]),
(1, [0, 1, 2, 0xA5, 0xFF]),
(2, [0, 1]),
(3, [0, 1, 2]),
(4, [0, 1]),
(5, range(64)),
(6, [0, 1, 2, 3, 0xFFFFFFFF]),
(7, [0, 1, 0x100, 0x200, 0xFFFF0100]),
(8, [0, 5 << 16, 7 << 16, 0x1F << 16, 0xFFFFFFFF]),
):
for value in vals:
c = base.copy()
c[i] = value
specs.append(c)
for ip in (3, 8, 9, 10, 14, 18, 20, 63):
for strap in (0, 1, 2, 3):
for cap in (0, 0x100):
for ctl in (5 << 16, 7 << 16):
c = base.copy()
c[5:] = [ip, strap, cap, ctl]
specs.append(c)
for cached in (0, 1, 2, 0xA5, 0xFF):
c = base.copy()
c[0] = 1
c[1] = cached
specs.append(c)
rows = []
for current in specs:
st["current"] = current
initialized, cached, present, disabled, haschild, ip, _strap, _cap, _ctl = current
uc.mem_write(DATA, bytes(0x70000))
w32(chip + 0x10, 35)
w32(rm + 0x10, 2)
uc.mem_write(rm + 0x12, struct.pack("<H", ip))
call("_nv006018rm", [vbios, chip])
call("_nv005963rm", [display, rm, chip])
call("_nv006019rm", [child, chip, rm])
w64(DATA + 0x1F70, display if present else 0)
uc.mem_write(display + 0x190A, bytes([disabled]))
w64(display + 0x1A70, child if haschild else 0)
w64(display + 0x3D38, runtime)
w64(runtime + 0x50, interface)
w64(interface, vtable)
w64(vtable + 0x28, STOP + 0x200)
uc.mem_write(vbios + 0x1728, bytes([initialized]))
uc.mem_write(DATA + 0x929, bytes([cached]))
st["trace"].clear()
frame = STACK + 0x8000
uc.mem_write(frame + 0x23, b"\x00")
uc.reg_write(x86.UC_X86_REG_RBP, frame)
uc.reg_write(x86.UC_X86_REG_RSP, STACK + 0xF000)
uc.reg_write(x86.UC_X86_REG_R12, DATA)
uc.reg_write(x86.UC_X86_REG_R13, vbios)
uc.emu_start(TEXT + (0x63CCF98 - 0x616F810), STOP, count=50000)
require(uc.reg_read(x86.UC_X86_REG_RIP) == stop_pc, f"did not reach stop PC for {current}")
rows.append(
{
"spec": list(current),
"queried": uc.mem_read(frame + 0x23, 1)[0],
"gpu929": uc.mem_read(DATA + 0x929, 1)[0],
"initialized": uc.mem_read(vbios + 0x1728, 1)[0],
"trace": list(st["trace"]),
}
)
lines = [
"// Actual original060180/060173 + selected display/child methods and parent state commit.",
"const SOURCE_CASES: &[Case] = &[",
]
for c in rows:
padded_trace = c["trace"] + [[0, 0]] * (3 - len(c["trace"]))
lines.append(
f"Case {{ spec: {c['spec']}, queried: {c['queried']}, gpu929: {c['gpu929']}, "
f"initialized: {c['initialized']}, count: {len(c['trace'])}, "
f"trace: {padded_trace} }},"
)
lines.append("];")
write_or_check_text(
ROOT / "src/nvidia/src/gpu/rom_source_vectors.rs",
"\n".join(lines) + "\n",
args.check,
"Rust source policy vectors",
)
report = {
"artifact_sha256": DIGEST,
"functions": NAMES,
"cases": len(rows),
"rows": rows,
"interceptions": [
"Two real register interfaces supplied values and exact ordered read addresses",
"No capability/map/policy/binding/parent decisions intercepted",
],
"limits": [
"Exact original063ccf98..63ccfc9 caller window; source winner prefix outside this policy not executed",
"Actual binders and selected child/display callbacks execute for supplied factory-owned facts",
"Display interface callback is not claimed to be rawBAR0; actual ownership/interface mapping remains required",
"No native or physicalGPU execution",
],
}
write_or_check_json(
ROOT / "reports/gpu-pf-rom-source-oracle.json",
report,
args.check,
"PF ROM source JSON report",
)
print(len(rows), "original source-policy cases")
return 0
if __name__ == "__main__":
raise SystemExit(main())