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

160 lines
7.2 KiB
Python

#!/usr/bin/env python3
"""Run the original GV100 KernelMc Init and Load bodies and write an oracle.
Executed original code: _nv041564rm then _nv041565rm on the same object. The
registry read, the callback registration and the three GPU table calls (bus
type, configuration read and write) are fixture-supplied.
"""
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_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,
UC_X86_REG_R8, UC_X86_REG_R9)
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, RODATA, DATA, STACK, STOP = 0x10000000, 0x20000000, 0x30000000, 0x40000000, 0x50000000
GPU, KMC, CHILD = DATA, DATA + 0x8000, DATA + 0x9000
INDIRECT, BUS, READ, WRITE = STOP + 0x100, STOP + 0x200, STOP + 0x300, STOP + 0x400
EXECUTED = ("_nv041564rm", "_nv041565rm")
HOOKED = ("_nv049786rm", "_nv046906rm")
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())
rodata = elf.get_section_by_name(".rodata").data()
text_section, rodata_section = (elf.get_section_index(name) for name in (".text", ".rodata"))
ranges = [(by_name[name]["st_value"], by_name[name]["st_size"]) for name in EXECUTED]
for relocation in elf.get_section_by_name(".rela.text").iter_relocations():
offset = relocation["r_offset"]
if not any(start <= offset < start + size for start, size in ranges):
continue
kind, target = relocation["r_info_type"], symbols[relocation["r_info_sym"]]
if target.name == "__x86_indirect_thunk_rax":
address = INDIRECT
elif target["st_shndx"] == rodata_section:
address = RODATA + target["st_value"]
else:
assert target["st_shndx"] == text_section, target.name
address = TEXT + target["st_value"]
value = address + relocation["r_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, data in ((TEXT, bytes(text)), (RODATA, bytes(rodata))):
cpu.mem_map(address, (len(data) + 4095) & ~4095)
cpu.mem_write(address, data)
for address, size in ((DATA, 0x10000), (STACK, 0x10000), (STOP, 0x1000)):
cpu.mem_map(address, size)
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 registry(emu, address, size, unused):
gpu, name, out = (emu.reg_read(r) for r in (UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX))
assert gpu == GPU
assert bytes(emu.mem_read(name, 23)) == b"PciLatencyTimerControl\0"
trace.append("reg")
if case["registry_ok"]:
emu.mem_write(out, struct.pack("<I", case["registry"]))
leave(emu, 0 if case["registry_ok"] else 0x57)
def register(emu, address, size, unused):
a, b, c, d, e, f = (emu.reg_read(r) for r 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))
assert (a, b, c, d & 0xffffffff, e & 0xffffffff, f) == (
GPU, CHILD, TEXT + by_name["_nv013997rm"]["st_value"], 1, 0xf, KMC)
trace.append("register")
leave(emu)
def thunk(emu, address, size, unused):
target = emu.reg_read(UC_X86_REG_RAX)
a, b, c = (emu.reg_read(r) for r in (UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX))
assert a == GPU
if target == BUS:
trace.append("bus")
leave(emu, case["bus"])
elif target == READ:
trace.append(f"cr:{b & 0xffffffff:x}")
if case["read_ok"]:
emu.mem_write(c, struct.pack("<I", case["read"]))
leave(emu, 0 if case["read_ok"] else 0x40)
else:
assert target == WRITE
trace.append(f"cw:{b & 0xffffffff:x}:{c & 0xffffffff:x}")
leave(emu, 0x33)
cpu.hook_add(UC_HOOK_CODE, thunk, begin=INDIRECT, end=INDIRECT)
for name, handler in (("_nv049786rm", registry), ("_nv046906rm", register)):
entry = TEXT + by_name[name]["st_value"]
cpu.hook_add(UC_HOOK_CODE, handler, begin=entry, end=entry)
def call(name):
cpu.reg_write(UC_X86_REG_RDI, GPU)
cpu.reg_write(UC_X86_REG_RSI, KMC)
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=20000)
assert cpu.reg_read(UC_X86_REG_RIP) == STOP, name
return cpu.reg_read(UC_X86_REG_RAX) & 0xffffffff
rows = []
for registry_ok, value, present, bus, e00, (read_ok, read) in itertools.product(
(0, 1), (0, 0xffffffff, 0x8, 0xf8, 0x1234, 0xfffffffe, 0x100), (0, 1), (3, 8),
(0, 1), ((0, 0), (1, 0), (1, 0xffffffff), (1, 0x12345678))):
if not registry_ok and value:
continue
case.update(registry_ok=registry_ok, registry=value, bus=bus, read_ok=read_ok, read=read)
trace.clear()
cpu.mem_write(DATA, bytes(0x10000))
for slot, target in ((0x200, BUS), (0x210, READ), (0x208, WRITE)):
cpu.mem_write(GPU + slot, struct.pack("<Q", target))
cpu.mem_write(GPU + 0x22f8, struct.pack("<Q", CHILD if present else 0))
cpu.mem_write(GPU + 0xe00, bytes([e00]))
cpu.mem_write(KMC + 0x108, b"\x77")
cpu.mem_write(KMC + 0x10c, struct.pack("<I", 0x77777777))
init_status = call("_nv041564rm")
skip = cpu.mem_read(KMC + 0x108, 1)[0]
latency = struct.unpack("<I", cpu.mem_read(KMC + 0x10c, 4))[0]
load_status = call("_nv041565rm")
rows.append(" ".join(f"{v:x}" for v in (registry_ok, value, present, bus, e00, read_ok, read))
+ ";" + " ".join(f"{v:x}" for v in (init_status, skip, latency, load_status))
+ ";" + " ".join(trace))
out = ROOT / "target/gpu-stage"
out.mkdir(parents=True, exist_ok=True)
oracle = "\n".join(rows) + "\n"
(out / "original-kernel-mc.txt").write_text(oracle)
report = {"original_sha256": DIGEST, "executed": list(EXECUTED),
"fixture_supplied": list(HOOKED) + ["GPU+200", "GPU+208", "GPU+210"], "cases": len(rows),
"oracle": "target/gpu-stage/original-kernel-mc.txt",
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
"scope": "Original GV100 KernelMc StateInitLocked and StateLoad. The registry, the "
"callback registration and PCI configuration access are fixture-supplied."}
(ROOT / "reports/gpu-kernel-mc-original.json").write_text(json.dumps(report, indent=2) + "\n")
print(f"{len(rows)} original KernelMc cases")