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

377 lines
19 KiB
Python

#!/usr/bin/env python3
"""Run the original GV100 OBJMC engine reset (HAL slot 0x130) and write an oracle.
Executed original code: the reset body _nv043609rm, the GV100 HAL binder
_nv006001rm that populates the OBJMC function table, the OBJMC functions the
reset reaches through that table (_nv043605rm at +138, _nv043583rm at +140,
_nv043623rm at +148), the engine-list helpers _nv017899rm and _nv017901rm,
_nv017895rm which builds the all-engines list the first-open wrapper passes,
the interrupt-enable snapshot _nv037727rm with the GV100 Intr functions at
+3a0 (_nv037574rm) and +150 (_nv037527rm), the GV100 OBJBIF functions at +608
(_nv017686rm) and +610 (_nv017682rm), and the GPU-object configuration
accessors they call at GPU+208 (_nv031264rm), GPU+210 (_nv031094rm) and
GPU+218 (_nv031092rm).
Only the OBJMC binder is run. The function pointers of the other objects are
stored by this harness and checked against reports/gv100-engine-hal-bindings.json.
Fixture-supplied services: register read _nv053907rm and write _nv053912rm,
the delay _nv049598rm, diagnostics, the FIFO lookup _nv030608rm with the FIFO
function at +1b8 that translates a CE engine descriptor to its reset bit, the
timer function at +180 of the GPU+1f60 object (GV100 _nv059553rm) and the bus
function at +220 of the GPU+1f78 object (GV100 _nv018640rm, which has its own
oracle in check_gpu_bus_stage_binary.py). 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, MC, CHILD, CHIP, RM, BIF, KERNEL_BIF, BUS, TIMER, INTR, PMU, FIFO, LIST) = (
DATA, DATA + 0x10000, DATA + 0x20000, DATA + 0x30000, DATA + 0x31000, DATA + 0x32000,
DATA + 0x33000, DATA + 0x36000, DATA + 0x37000, DATA + 0x38000, DATA + 0x3a000,
DATA + 0x3b000, DATA + 0x3c000)
INDIRECT, FIFO_1B8, TIMER_180, BUS_220 = STOP + 0x100, STOP + 0x200, STOP + 0x210, STOP + 0x220
FILL = 0x77777777
BITS = 0xb3
# The binder only stores pointers, so the functions it selects are listed.
ROOTS = ("_nv043609rm", "_nv006001rm", "_nv043605rm", "_nv043583rm", "_nv043623rm",
"_nv017895rm", "_nv017686rm", "_nv017682rm", "_nv031264rm", "_nv031094rm",
"_nv031092rm", "_nv037574rm", "_nv037527rm")
HOOKED = ("_nv053907rm", "_nv053912rm", "_nv049598rm", "_nv047869rm", "_nv046747rm",
"_nv046745rm", "_nv030608rm", "_nv052705rm")
# Object, slot, function: stored by the harness, taken from the bindings report.
FOREIGN = (("gpu_object", GPU, 0x208, "_nv031264rm"), ("gpu_object", GPU, 0x210, "_nv031094rm"),
("gpu_object", GPU, 0x218, "_nv031092rm"), ("OBJBIF", BIF, 0x608, "_nv017686rm"),
("OBJBIF", BIF, 0x610, "_nv017682rm"), ("Intr", INTR, 0x3a0, "_nv037574rm"),
("Intr", INTR, 0x150, "_nv037527rm"))
SERVICES = {"OBJTMR": ("0x180", "_nv059553rm"), "OBJBUS": ("0x220", "_nv018640rm")}
assert hashlib.sha256(ORIGINAL.read_bytes()).hexdigest() == DIGEST
bindings = json.loads((ROOT / "reports/gv100-engine-hal-bindings.json").read_text())
assert bindings["original_sha256"] == DIGEST
tables = {entry["class_label"]: entry["hal_slots"] for entry in bindings["classes"]}
tables["gpu_object"] = bindings["gpu_object"]["hal_slots"]
for label, unused, slot, function in FOREIGN:
assert tables[label][f"{slot:#x}"]["function"] == function, (label, slot)
for label, (slot, function) in SERVICES.items():
assert tables[label][slot]["function"] == function, (label, slot)
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 = INDIRECT
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, 0x40000), (STACK, 0x10000),
(STOP, 0x1000)):
cpu.mem_map(address, size)
cpu.mem_write(TEXT, bytes(text))
case, trace = {}, []
def leave(emu, value=0):
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])
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)]
a, b, c, d, e, f = full
low = [value & 0xffffffff for value in full]
value = 0
if name == "_nv053907rm":
assert (a, low[1], low[2]) == (GPU + 0x4120, 0, 0)
value = case["reads"].pop(0)
trace.append(f"r:{low[3]:x}:{value:x}")
elif name == "_nv053912rm":
assert (a, low[1], low[2]) == (GPU + 0x4120, 0, 0)
trace.append(f"w:{low[3]:x}:{low[4]:x}")
elif name == "_nv049598rm":
trace.append(f"t:{low[0]:x}")
elif name == "_nv052705rm":
emu.mem_write(a, bytes([b & 0xff]) * (c & 0xffffffff))
value = a
elif name == "_nv030608rm":
assert a == GPU
value = FIFO
elif name == "_nv047869rm":
assert low[:2] == [0xe3598f4, 0x361000], low
trace.append(f"log:{low[0]:x}:{low[1]:x}:{low[2]:x}")
elif name == "_nv046747rm":
trace.append(f"nv046747:{low[0]:x}:{low[1]:x}")
elif name == "FIFO_1B8":
# (gpu, fifo, input type 0, CE engine descriptor, output type 6, out).
assert (a, b, low[2], low[4]) == (GPU, FIFO, 0, 6), full
assert low[3] & 0xffffff00 == 0x793ceb00, full
instance = low[3] & 0xff
if instance == case["fifo_fail"]:
value = case["fifo_status"]
trace.append(f"f:{low[3]:x}:{value:x}")
else:
out = (case["fifo_base"] + instance * case["fifo_step"]) & 0xffffffff
emu.mem_write(f, struct.pack("<I", out))
trace.append(f"f:{low[3]:x}:0:{out:x}")
elif name == "TIMER_180":
assert a == TIMER
value = case["service_status"]
trace.append(f"d:{low[1]:x}")
elif name == "BUS_220":
assert (a, b) == (GPU, BUS)
value = case["service_status"]
trace.append("b220")
else:
raise AssertionError(name)
leave(emu, value)
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=INDIRECT, end=INDIRECT)
for hooked in HOOKED:
entry = TEXT + by_name[hooked]["st_value"]
cpu.hook_add(UC_HOOK_CODE, leaf(hooked), begin=entry, end=entry)
for label, entry in (("FIFO_1B8", FIFO_1B8), ("TIMER_180", TIMER_180), ("BUS_220", BUS_220)):
cpu.hook_add(UC_HOOK_CODE, leaf(label), 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=400000)
assert cpu.reg_read(UC_X86_REG_RIP) == STOP, name
return cpu.reg_read(UC_X86_REG_RAX) & 0xffffffff
def q(address, value):
cpu.mem_write(address, struct.pack("<Q", value))
def d(address, value):
cpu.mem_write(address, struct.pack("<I", value))
def word(address):
return struct.unpack("<I", cpu.mem_read(address, 4))[0]
cpu.mem_write(CHIP + 0x10, struct.pack("<H", 0x23))
cpu.mem_write(RM + 0x10, struct.pack("<H", 2))
call("_nv006001rm", MC, CHIP, RM)
bound = {f"{slot:#x}": entries[struct.unpack("<Q", cpu.mem_read(MC + slot, 8))[0] - TEXT]
for slot in (0x130, 0x138, 0x140, 0x148)}
assert bound == {"0x130": "_nv043609rm", "0x138": "_nv043605rm", "0x140": "_nv043583rm",
"0x148": "_nv043623rm"}, bound
for label, base, slot, function in FOREIGN:
q(base + slot, TEXT + by_name[function]["st_value"])
q(FIFO + 0x1b8, FIFO_1B8)
q(TIMER + 0x180, TIMER_180)
q(BUS + 0x220, BUS_220)
for offset, target in ((0x1e88, BIF), (0x1e90, KERNEL_BIF), (0x1f60, TIMER), (0x1f78, BUS),
(0x2110, INTR)):
q(GPU + offset, target)
# The first-open wrapper _nv000790rm builds its list with _nv017895rm.
call("_nv017895rm", LIST, BITS)
ALL = struct.unpack("<3Q", cpu.mem_read(LIST, 24))
assert ALL == ((1 << 64) - 1, (1 << 64) - 1, (1 << 51) - 1), ALL
WATCHED = ((GPU, 0x8000), (MC, 0x1000), (KERNEL_BIF, 0x3000), (INTR, 0x2000), (BIF, 0x1000),
(CHILD, 0x2000), (PMU, 0x1000), (LIST, 0x100))
rows, paths = [], {"full": 0, "partial": 0}
def run(engines, delay=0, child=0, byte_190a=0, gpu_52fc=0, gpu_6d29=0, pmu=0, byte_7f9=0,
gpu_69e4=0, gpu_dec=0, gpu_96c=0x77, fifo_base=0, fifo_step=1, fifo_fail=0xff,
fifo_status=0, service_status=0, reads=()):
cpu.mem_write(LIST, struct.pack("<3Q", *engines))
q(GPU + 0x1f70, CHILD if child else 0)
cpu.mem_write(CHILD + 0x190a, bytes([byte_190a]))
cpu.mem_write(GPU + 0x52fc, bytes([gpu_52fc]))
cpu.mem_write(GPU + 0x6d29, bytes([gpu_6d29]))
q(GPU + 0x2328, PMU if pmu else 0)
cpu.mem_write(PMU + 0x7f9, bytes([byte_7f9]))
d(GPU + 0x69e4, gpu_69e4)
d(GPU + 0xdec, gpu_dec)
cpu.mem_write(GPU + 0x96c, bytes([gpu_96c]))
for address in (GPU + 0xde8, MC + 0x1c0, KERNEL_BIF + 0x2358, INTR + 0x16d0):
d(address, FILL)
case.update(reads=list(reads), fifo_base=fifo_base, fifo_step=fifo_step,
fifo_fail=fifo_fail, fifo_status=fifo_status, service_status=service_status)
trace.clear()
before = [bytes(cpu.mem_read(base, size)) for base, size in WATCHED]
status = call("_nv043609rm", GPU, MC, LIST, delay)
assert not case["reads"], (engines, case["reads"])
# Only the five recorded fields may change.
allowed = {GPU + 0x96c, *range(GPU + 0xde8, GPU + 0xdec), *range(MC + 0x1c0, MC + 0x1c4),
*range(KERNEL_BIF + 0x2358, KERNEL_BIF + 0x235c),
*range(INTR + 0x16d0, INTR + 0x16d4)}
for (base, size), old in zip(WATCHED, before):
new = cpu.mem_read(base, size)
assert all(old[i] == new[i] or base + i in allowed for i in range(size)), hex(base)
state = (cpu.mem_read(GPU + 0x96c, 1)[0], word(GPU + 0xde8), word(MC + 0x1c0),
word(KERNEL_BIF + 0x2358), word(INTR + 0x16d0))
# The full path always stores MC+1c0 with bits 3:2 set, which FILL lacks.
paths["full" if word(MC + 0x1c0) != FILL else "partial"] += 1
inputs = (*engines, delay, child, byte_190a, gpu_52fc, gpu_6d29, pmu, byte_7f9, gpu_69e4,
gpu_dec, gpu_96c, fifo_base, fifo_step, fifo_fail, fifo_status, service_status)
rows.append(" ".join(f"{value:x}" for value in inputs) + ";"
+ " ".join(f"{value:x}" for value in reads) + f";{status:x};"
+ " ".join(f"{value:x}" for value in state) + ";" + " ".join(trace))
return list(trace)
def bits(*numbers):
value = 0
for number in numbers:
value |= 1 << number
return (value & (1 << 64) - 1, value >> 64 & (1 << 64) - 1, value >> 128)
# Physical first-open sequence of the original driver on a V100 (BAR0 offsets).
PHYSICAL = ["r:88004:100506", "w:88004:100502", "r:200:42001120", "w:200:42000020",
"w:200:42001120", "r:200:42001120", "r:140:0", "w:200:bfffffff", "r:200:1fecdff1",
"w:88004:100506"]
observed = run(ALL, reads=(0x100506, 0x42001120, 0x42001120, 0, 0x1fecdff1))
assert [entry for entry in observed if entry[0] in "rw"] == PHYSICAL, observed
assert observed[-1] == "b220"
# Full reset: every one of the 0xb3 engine bits is set.
CHILDREN = ((0, 0), (1, 0), (1, 1))
VALUES = (0x42001120, 0, 0xffffffff, 0x1fecdff1, 0x00101100)
for (child, byte_190a), gpu_52fc, gpu_6d29, (pmu, byte_7f9) in itertools.product(
CHILDREN, (0, 1), (0, 1), CHILDREN):
for first, second, third in itertools.product(VALUES, (0x42001120, 0xffefeeff),
(0x1fecdff1, 0x5fecdff1)):
run(ALL, delay=7, child=child, byte_190a=byte_190a, gpu_52fc=gpu_52fc,
gpu_6d29=gpu_6d29, pmu=pmu, byte_7f9=byte_7f9, gpu_96c=first & 1,
reads=(0x100506, first, second, 0x200, third))
# The interrupt-enable snapshot; GPU+69e4 equal to 3 skips the register read.
for gpu_69e4, gpu_dec, enable in itertools.product(
(0, 2, 3, 4), (0, 0x200, 0x7fffffff, 0x80000000, 0xffffffff),
(0, 1, 0x200, 0x7fffffff, 0x80000000, 0x80000200, 0xffffffff)):
skipped = gpu_69e4 == 3
run(ALL, gpu_69e4=gpu_69e4, gpu_dec=gpu_dec, pmu=1, byte_7f9=1,
reads=(0x100506, 0x42001120, 0x42001120) + (() if skipped else (enable,))
+ (0x1fecdff1,))
# Saved configuration word, ignored service results, bits above the list length.
for command, service_status, high in itertools.product(
(0, 4, 0x100502, 0x100506, 0xffffffff), (0, 0x56), (0, 1)):
engines = (ALL[0], ALL[1], ALL[2] | (0x1fff << 51 if high else 0))
run(engines, child=1, service_status=service_status,
reads=(command, 0x42001120, 0x42001120, 0, 0x1fecdff1))
# Partial reset: at least one of the 0xb3 bits is clear.
for selected in itertools.product((0, 1), repeat=4):
chosen = [engine for engine, wanted in zip((0x54, 4, 2, 0xd), selected) if wanted]
for (child, byte_190a), first, delay in itertools.product(
CHILDREN, (0x42001120, 0xffffffff, 0, 0x1fecdff1), (0, 3, 0x00418938)):
run(bits(*chosen), delay=delay, child=child, byte_190a=byte_190a, gpu_96c=delay & 1,
reads=(0x100506, first))
# CE engines 0xf to 0x22 ask the FIFO for their reset bit.
for instance, (base, step), first in itertools.product(
range(20), ((0, 1), (0x25, 3), (0xffffffe0, 1)), (0xffffffff, 0)):
run(bits(0xf + instance), fifo_base=base, fifo_step=step, reads=(0x100506, first))
for fail, status, first in itertools.product((0, 1, 7, 19), (0x56, 0xffff),
(0xffffffff, 0, 0x42001120)):
run(bits(*range(0xf, 0x23)), fifo_fail=fail, fifo_status=status, fifo_base=1,
reads=(0x100506, first))
run(bits(2, 4, 0xd, 0x54, 0x10, 0x11 + fail), child=1, fifo_fail=0x11 + fail - 0xf,
fifo_status=status, fifo_base=9, fifo_step=5, reads=(0x100506, first))
# Lists that miss a single engine take the partial path with everything else.
for missing, (child, byte_190a), first, service_status in itertools.product(
(0, 2, 4, 0xd, 0xe, 0xf, 0x22, 0x23, 0x54, 0x80, 0xb2), CHILDREN,
(0x42001120, 0xffffffff, 0), (0, 0x56)):
engines = tuple(value & ~mask for value, mask in zip(ALL, bits(missing)))
run(engines, delay=1, child=child, byte_190a=byte_190a, fifo_base=3, fifo_step=7,
service_status=service_status, reads=(0x100506, first))
# Engines the routine does not look at, and the empty list.
for engines in (bits(), bits(0), bits(1), bits(3), bits(0xe), bits(0x23), bits(0x53),
bits(0x55), bits(0xb2), (0, 0, 0x1fff << 51)):
for first in (0x42001120, 0xffffffff):
run(engines, delay=2, child=1, reads=(0x100506, first))
assert paths["full"] and paths["partial"], paths
out = ROOT / "target/gpu-stage"
out.mkdir(parents=True, exist_ok=True)
oracle = "\n".join(rows) + "\n"
(out / "original-mc-reset.txt").write_text(oracle)
report = {
"original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "executed": sorted(executed),
"fixture_supplied": list(HOOKED) + [
"FIFO function at +1b8 (CE engine descriptor to reset bit)",
"timer function at +180 of the GPU+1f60 object (GV100 _nv059553rm)",
"bus function at +220 of the GPU+1f78 object (GV100 _nv018640rm)",
"memory fill is performed as such"],
"bindings": bound,
"pointers_stored_by_harness": {f"{label}+{slot:#x}": function
for label, unused, slot, function in FOREIGN},
"cases": len(rows), "full_reset_cases": paths["full"],
"partial_reset_cases": paths["partial"],
"oracle": "target/gpu-stage/original-mc-reset.txt",
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
"physical_sequence_reproduced": PHYSICAL,
"scope": "Original GV100 OBJMC engine reset with the binder-selected function table. "
"Register values, delays, the FIFO translation, the timer delay, the bus "
"function and diagnostics are fixture-supplied; no hardware executed."}
(ROOT / "reports/gpu-mc-reset-original.json").write_text(json.dumps(report, indent=2) + "\n")
print(f"{len(rows)} original MC reset cases ({paths['full']} full, {paths['partial']} partial); "
f"executed {len(executed)} functions")