Files
nvdriver/tools/check_gpu_acr_wpr_region_binary.py
nvidia-driver-rs 4526e6a816 Baseline: validated 30/30 tree with passing Level 7 hardware regression
- Updated tools/post_reboot.sh to check kernel oops taint bit and stuck driver tasks instead of <86400s uptime
- Level 7 hardware regression passed on 0000:b3:00.0 (module 37e6fa430835eee2b426019db3ad70eeb7a9e9070c09c84bd9b973cf56cbe529, 107 inventory words, 0 static mismatches, CUDA baseline verified before and after)
- Updated .gitignore to exclude build archives (*.a, *.rlib), __pycache__, and raw trace dumps
2026-11-23 23:48:38 +00:00

194 lines
9.2 KiB
Python

#!/usr/bin/env python3
"""Run the original GV100 OBJACR function at +150 (_nv016745rm) and write an oracle.
Executed original code: _nv016745rm and the region-count function _nv016916rm
it calls at OBJACR+148, both reached through the pointers the original OBJACR
binder _nv005947rm stores with chip HAL 0x23 and RM HAL 2.
Fixture-supplied services: register read _nv053907rm and write _nv053912rm,
the check _nv031156rm and the diagnostic _nv047869rm. No hardware is involved.
"""
import bisect
import hashlib
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_R8, UC_X86_REG_R9, 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)
ROOT = Path(__file__).resolve().parents[1]
ORIGINAL = Path("/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary")
DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b"
TEXT, DATA, STACK, STOP = 0x10000000, 0x30000000, 0x40000000, 0x50000000
GPU, ACR, CHIP, RM, OUT = DATA, DATA + 0x10000, DATA + 0x20000, DATA + 0x21000, DATA + 0x22000
ROOTS = ("_nv016745rm", "_nv016916rm", "_nv005947rm")
HOOKED = ("_nv053907rm", "_nv053912rm", "_nv047869rm", "_nv031156rm")
FILL = 0x7777777777777777
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())
offsets = [entry[0] for entry in relocations]
entries = {symbol[0]: symbol[2] for symbol in starts}
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 = STOP + 0x100
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):
callee = entries.get(target["st_value"] + addend + 4)
if callee and callee not in HOOKED:
pending.append(callee)
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, 0x30000), (STACK, 0x10000),
(STOP, 0x1000)):
cpu.mem_map(address, size)
cpu.mem_write(TEXT, bytes(text))
case, trace = {}, []
def leaf(name):
def run(emu, address, size, unused):
full = [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,
UC_X86_REG_R9)]
low = [value & 0xffffffff for value in full]
value = 0
if name == "_nv053907rm":
assert (full[0], low[1], low[2]) == (GPU + 0x4120, 0, 0), full
value = case["reads"].pop(0)
trace.append(f"r:{low[3]:x}:{value:x}")
elif name == "_nv053912rm":
assert (full[0], low[1], low[2]) == (GPU + 0x4120, 0, 0), full
trace.append(f"w:{low[3]:x}:{low[4]:x}")
elif name == "_nv031156rm":
assert (full[0], low[1], low[2]) == (GPU, 0, 0), full
value = case["check"]
trace.append(f"nv031156:{value:x}")
elif low[1] == 0x2831000:
trace.append(f"log:{low[0]:x}:{low[1]:x}:{low[2]:x}")
else:
trace.append(f"log:{low[0]:x}:{low[1]:x}")
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])
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=STOP + 0x100, end=STOP + 0x100)
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(address, *arguments, stack=()):
for register, argument in zip((UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX,
UC_X86_REG_RCX, UC_X86_REG_R8, UC_X86_REG_R9), 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(f"<{1 + len(stack)}Q", STOP, *stack))
cpu.emu_start(address, STOP, count=400000)
assert cpu.reg_read(UC_X86_REG_RIP) == STOP
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(TEXT + by_name["_nv005947rm"]["st_value"], ACR, CHIP, RM)
BODY, COUNT = struct.unpack("<QQ", cpu.mem_read(ACR + 0x148, 16))[::-1]
assert (entries[BODY - TEXT], entries[COUNT - TEXT]) == ("_nv016745rm", "_nv016916rm")
WATCHED = ((GPU, 0x10000), (ACR, 0x10000))
rows, paths = [], {"read": 0, "refused": 0}
def run(region, check=0, reads=()):
case.update(reads=list(reads), check=check)
trace.clear()
cpu.mem_write(OUT, struct.pack("<4Q", FILL, FILL, FILL, FILL))
before = [bytes(cpu.mem_read(base, size)) for base, size in WATCHED]
# (gpu, acr, region, start, end, third, fourth): the last is on the stack.
status = call(BODY, GPU, ACR, region, OUT, OUT + 8, OUT + 16, stack=(OUT + 24,))
assert not case["reads"], (region, case["reads"])
assert before == [bytes(cpu.mem_read(base, size)) for base, size in WATCHED]
outputs = struct.unpack("<4Q", cpu.mem_read(OUT, 32))
# The last two outputs are dword stores.
assert all(value >> 32 == FILL >> 32 for value in outputs[2:])
paths["read" if reads else "refused"] += 1
rows.append(f"{region:x} {check:x};" + " ".join(f"{value:x}" for value in reads)
+ f";{status:x};" + " ".join(f"{value:x}" for value in outputs) + ";"
+ " ".join(trace))
return status, outputs, list(trace)
# Physical first-open sequence of the original driver on a V100 (BAR0
# offsets), lines 37714 to 37721 of the per-GPU register transcript.
status, outputs, observed = run(1, reads=(0xfffe02, 0x3, 0x8000000, 0x8000001))
assert status == 0 and observed == [
"nv031156:0", "w:100cd4:2", "r:100cd4:fffe02", "w:100cd4:3", "r:100cd4:3", "w:100cd4:0",
"r:100cd4:8000000", "w:100cd4:1", "r:100cd4:8000001"], observed
assert outputs == (0, 0, FILL & ~0xffffffff, FILL & ~0xffffffff), outputs
VALUES = (0, 0xfffe02, 0x3, 0xffffffff, 0x8000000, 0x8000001, 0x12345678, 0xfffe0f, 0x1f)
for region in (1, 2):
for reads in itertools.product(VALUES, VALUES, (0, 0x8000000, 0xffffffff, 0x12345678, 0xa50),
(0, 0x8000001, 0xffffffff, 0x12345678, 0x5a1)):
run(region, reads=reads)
for region, check in itertools.product((0, 1, 2, 3, 4, 9, 0x80000001, 0xffffffff),
(0, 0x56, 0x1f, 0xffff)):
if check or region not in (1, 2):
run(region, check=check)
assert all(paths.values()), paths
out = ROOT / "target/gpu-stage"
out.mkdir(parents=True, exist_ok=True)
oracle = "\n".join(rows) + "\n"
(out / "original-acr-wpr-region.txt").write_text(oracle)
report = {
"original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "executed": sorted(executed),
"fixture_supplied": list(HOOKED),
"bindings": {"OBJACR+0x148": "_nv016916rm", "OBJACR+0x150": "_nv016745rm"},
"cases": len(rows), "cases_by_path": paths,
"oracle": "target/gpu-stage/original-acr-wpr-region.txt",
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
"scope": "Original GV100 OBJACR function at +150 with the binder-selected pointers. "
"Register values, the check _nv031156rm and the diagnostic are "
"fixture-supplied; no hardware executed."}
(ROOT / "reports/gpu-acr-wpr-region-original.json").write_text(json.dumps(report, indent=1) + "\n")
print(f"{len(rows)} original OBJACR +150 cases {paths}; executed {len(executed)} functions")