Files
nvdriver/tools/check_gpu_pmu_surface_size_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

209 lines
9.5 KiB
Python

#!/usr/bin/env python3
"""Run the original producers of the PMU InitLocked surface sizes and write an oracle.
PMU InitLocked _nv051858rm makes three framebuffer requests through the
surface allocator _nv052601rm; their byte counts come from three functions:
instance block _nv052270rm asks _nv052299rm(object, &size, &second, &third)
+f08 surface _nv052623rm stores _nv052618rm() when its predicates hold
super surface _nv052583rm -> _nv052522rm asks the dispatcher _nv052179rm
(gpu, &surface, &second), which selects one of _nv052174rm,
_nv052175rm, _nv052176rm, _nv052177rm, _nv052180rm by the
chip word at *(gpu+80)+10 (0x23 on a GV100 -> _nv052177rm)
Executed original code: those eight functions, for every chip word below
0x100 and a few above, with valid and null output pointers.
Fixture-supplied services: the predicate _nv054300rm that _nv052175rm asks
(both answers per chip word that reaches it), the diagnostics _nv047869rm
and _nv047868rm, and the status reporter _nv046747rm. No hardware is
involved; no register is read.
"""
import bisect
import hashlib
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)
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, CHIP, OUT = DATA, DATA + 0x10000, DATA + 0x11000
ROOTS = ("_nv052179rm", "_nv052299rm", "_nv052618rm")
VARIANTS = ("_nv052174rm", "_nv052175rm", "_nv052176rm", "_nv052177rm", "_nv052180rm")
HOOKED = ("_nv054300rm", "_nv047869rm", "_nv047868rm", "_nv046747rm")
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}
loaded, pending = [], list(ROOTS)
while pending:
name = pending.pop()
if name in loaded or name in HOOKED:
continue
loaded.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]
assert target["st_shndx"] == text_section, (name, target.name)
# Direct calls, tail calls and the branch to the cold part only.
assert kind in (2, 4), kind
if text[offset - 1] in (0xe8, 0xe9):
callee = entries.get(target["st_value"] + addend + 4)
if callee and callee not in HOOKED:
pending.append(callee)
struct.pack_into("<i", text, offset, target["st_value"] + addend - offset)
assert set(VARIANTS) <= set(loaded), loaded
cpu = Uc(UC_ARCH_X86, UC_MODE_64)
for address, size in ((TEXT, (len(text) + 4095) & ~4095), (DATA, 0x20000), (STACK, 0x10000),
(STOP, 0x1000)):
cpu.mem_map(address, size)
cpu.mem_write(TEXT, bytes(text))
case, trace, executed = {}, [], set()
def leaf(name):
def run(emu, address, size, unused):
low = [emu.reg_read(register) & 0xffffffff for register in (
UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX)]
value = 0
if name == "_nv054300rm":
assert emu.reg_read(UC_X86_REG_RDI) == GPU
value = case["predicate"]
trace.append(f"nv054300:{value:x}")
elif name == "_nv046747rm":
trace.append(f"status:{low[0]:x}:{low[1]:x}")
elif name == "_nv047868rm":
trace.append(f"note:{low[0]:x}:{low[1]:x}")
else:
trace.append(f"log:{low[0]:x}:{low[1]:x}:{low[2]: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
def entered(emu, address, size, unused):
name = entries.get(address - TEXT)
if name:
executed.add(name)
case["body"] = name
for hooked in HOOKED:
entry = TEXT + by_name[hooked]["st_value"]
cpu.hook_add(UC_HOOK_CODE, leaf(hooked), begin=entry, end=entry)
for name in loaded:
entry = TEXT + by_name[name]["st_value"]
cpu.hook_add(UC_HOOK_CODE, entered, 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,
UC_X86_REG_RCX), 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=10000)
assert cpu.reg_read(UC_X86_REG_RIP) == STOP
return cpu.reg_read(UC_X86_REG_RAX) & 0xffffffff
cpu.mem_write(GPU + 0x80, struct.pack("<Q", CHIP))
WATCHED = ((GPU, 0x10000), (CHIP, 0x1000))
rows, paths = [], {}
def outputs():
return struct.unpack("<3Q", cpu.mem_read(OUT, 24))
def run_super(word, first, second, predicate):
"""_nv052179rm(gpu, &surface, &second) with the chip word and the pointers given."""
cpu.mem_write(CHIP + 0x10, struct.pack("<H", word))
cpu.mem_write(OUT, struct.pack("<3Q", FILL, FILL, FILL))
case.update(predicate=predicate, body=None)
trace.clear()
before = [bytes(cpu.mem_read(base, size)) for base, size in WATCHED]
status = call("_nv052179rm", GPU, OUT if first else 0, OUT + 8 if second else 0)
assert before == [bytes(cpu.mem_read(base, size)) for base, size in WATCHED]
values = outputs()
assert values[2] == FILL
body = case["body"]
assert body in VARIANTS, body
asked = any(token.startswith("nv054300") for token in trace)
paths[body] = paths.get(body, 0) + 1
rows.append(f"super;{word:x} {first:x} {second:x} {predicate:x};{status:x};{body};"
+ " ".join(f"{value:x}" for value in values[:2]) + ";" + " ".join(trace))
return status, values, body, asked
# The GV100 chip word, as the GPU constructor stores it for chip HAL 0x23.
status, values, body, asked = run_super(0x23, 1, 1, 0)
assert (status, values[:2], body, asked) == (0, (0x207a00, 0x207a00), "_nv052177rm", False)
WORDS = list(range(0x100)) + [0x1ff, 0x220, 0x223, 0x400, 0x7e3, 0x8023, 0xffe3, 0xffff]
for word in WORDS:
for first, second in ((1, 1), (0, 1), (1, 0), (0, 0)):
status, values, body, asked = run_super(word, first, second, 0)
if asked:
# Only _nv052175rm asks, and only with both pointers.
assert body == "_nv052175rm" and first and second
run_super(word, first, second, 1)
# _nv052299rm(object, &size, &second, &third): one qword and two dword stores.
cpu.mem_write(OUT, struct.pack("<3Q", FILL, FILL, FILL))
trace.clear()
assert call("_nv052299rm", GPU, OUT, OUT + 8, OUT + 16) == 0 and not trace
instance = outputs()
assert instance == (0x1000, FILL & ~0xffffffff | 0x1000, FILL & ~0xffffffff | 0x1000)
rows.append("instance;;0;_nv052299rm;" + " ".join(f"{value:x}" for value in instance) + ";")
# _nv052618rm(gpu, object): the value _nv052623rm stores as the +f08 surface size.
trace.clear()
plain = call("_nv052618rm", GPU, GPU)
assert not trace
rows.append(f"plain;;{plain:x};_nv052618rm;;")
assert set(paths) == set(VARIANTS), paths
out = ROOT / "target/gpu-stage"
out.mkdir(parents=True, exist_ok=True)
oracle = "\n".join(rows) + "\n"
(out / "original-pmu-surface-size.txt").write_text(oracle)
report = {
"original_sha256": DIGEST, "gv100_chip_word": "0x23",
"executed": sorted(executed), "fixture_supplied": list(HOOKED),
"cases": len(rows), "super_surface_cases_by_body": paths,
"gv100": {"body": "_nv052177rm", "surface_bytes": "0x207a00", "second": "0x207a00",
"instance_block_bytes": f"0x{instance[0]:x}", "plain_surface_bytes": f"0x{plain:x}"},
"oracle": "target/gpu-stage/original-pmu-surface-size.txt",
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
"scope": "Original size producers of the three PMU InitLocked surface requests. The chip "
"word and the predicate _nv054300rm are case inputs; whether _nv052623rm's "
"predicates hold on a GV100 is not executed here. No hardware executed."}
(ROOT / "reports/gpu-pmu-surface-size-original.json").write_text(json.dumps(report, indent=1) + "\n")
print(f"{len(rows)} original PMU surface size cases {paths}; executed {sorted(executed)}; "
f"oracle {report['oracle_sha256']}")