mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
598 lines
26 KiB
Python
598 lines
26 KiB
Python
#!/usr/bin/env python3
|
|
"""Run the original base GPU StateLoad pass _nv031217rm and write an oracle.
|
|
|
|
Only _nv031217rm and its cold block _nv031217rm.cold execute. Every callee,
|
|
including the PostLoad pass _nv031219rm, is fixture-supplied: the oracle records
|
|
call order, arguments and the GPU fields the pass writes. It proves the pass's
|
|
control flow, not the behavior of any callee or engine.
|
|
"""
|
|
import hashlib
|
|
from _binary_path import resolve_580_binary
|
|
import json
|
|
from pathlib import Path
|
|
import random
|
|
import struct
|
|
|
|
from capstone import CS_ARCH_X86, CS_MODE_64, Cs
|
|
from elftools.elf.elffile import ELFFile
|
|
from unicorn import UC_ARCH_X86, UC_HOOK_BLOCK, 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 = resolve_580_binary()
|
|
DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b"
|
|
TEXT, BSS, DATA, STACK, STOP = 0x10000000, 0x20000000, 0x30000000, 0x40000000, 0x50000000
|
|
RODATA = 0x60000000
|
|
GPU, SYS, INSTANCE, PARENT = DATA, DATA + 0x8000, DATA + 0x10000, DATA + 0x1c000
|
|
OTHERS, DESCRIPTORS, ENGINES = DATA + 0x20000, DATA + 0x30000, DATA + 0x31000
|
|
VTABLE, RPC, GROUP = DATA + 0x3f000, DATA + 0x40000, DATA + 0x41000
|
|
INDIRECT = STOP + 0x100
|
|
SLOTS = {name: STOP + 0x200 + 0x10 * index for index, name in enumerate(
|
|
("sriov", "share", "ctrl", "rpc", "preload", "load"))}
|
|
GPU_SLOTS = {0x2f8: "sriov", 0x358: "share", 0x4298: "ctrl"}
|
|
# Entry of the block that ends in UD2; the cold block falls through into it.
|
|
TRAP_OFFSET = 0x5dc220
|
|
NAME_OFFSET = 0x4c5e890
|
|
HOOKED = ("_nv049674rm", "_nv013521rm", "_nv060495rm", "_nv030693rm", "_nv031390rm",
|
|
"_nv030522rm", "_nv026840rm", "_nv026839rm", "_nv054117rm", "_nv054115rm",
|
|
"_nv013524rm", "_nv060467rm", "_nv060465rm", "_nv030853rm", "_nv031447rm",
|
|
"_nv031219rm", "_nv031400rm", "_nv031424rm", "_nv031415rm", "_nv031475rm",
|
|
"_nv000797rm", "_nv031494rm", "_nv031407rm", "_nv031395rm", "_nv031426rm",
|
|
"_nv031478rm", "_nv031482rm", "_nv036619rm", "_nv041837rm", "_nv031288rm",
|
|
"_nv043825rm", "_nv047869rm", "_nv046745rm", "_nv046747rm", "_nv046751rm")
|
|
TIMES = (0x1111000011110001, 0x2222000022220002)
|
|
CLIENT, SUBDEVICE, CA4, VALUE_31415 = 0xc1d00001, 0xc1d00003, 0x4100, 0x5a5a0001
|
|
CONTROL_VALUE, INSTANCE_VALUE = 0x1234567800200000, 0x0000000904000000
|
|
SYSTEM_218, CHILD_1ED0, CHILD_1FC8 = 0x218218218, 0x1ed01ed0, 0x1fc81fc8
|
|
|
|
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")
|
|
bss_section = elf.get_section_index(".bss")
|
|
rodata_section = elf.get_section_index(".rodata")
|
|
bss_size = elf.get_section_by_name(".bss")["sh_size"]
|
|
stream.seek(elf.get_section_by_name(".rodata")["sh_offset"] + NAME_OFFSET)
|
|
assert stream.read(18) == b"gpuStateLoad_IMPL\0"
|
|
RANGES = [(by_name[name]["st_value"], by_name[name]["st_value"] + by_name[name]["st_size"])
|
|
for name in ("_nv031217rm", "_nv031217rm.cold")]
|
|
for relocation in elf.get_section_by_name(".rela.text").iter_relocations():
|
|
offset = relocation["r_offset"]
|
|
if not any(start <= offset < end for start, end in RANGES):
|
|
continue
|
|
kind = relocation["r_info_type"]
|
|
target = symbols[relocation["r_info_sym"]]
|
|
if target.name == "__x86_indirect_thunk_rax":
|
|
address = INDIRECT
|
|
elif target["st_shndx"] == bss_section:
|
|
address = BSS + target["st_value"]
|
|
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)
|
|
# The cold block ends at the UD2 block and nothing else lies between them.
|
|
assert RANGES[1][1] > TRAP_OFFSET >= RANGES[1][0]
|
|
assert bytes(text[TRAP_OFFSET + 8:TRAP_OFFSET + 10]) == b"\x0f\x0b"
|
|
|
|
cpu = Uc(UC_ARCH_X86, UC_MODE_64)
|
|
for address, length in ((TEXT, (len(text) + 4095) & ~4095), (BSS, (bss_size + 4095) & ~4095),
|
|
(DATA, 0x80000), (STACK, 0x10000), (STOP, 0x1000)):
|
|
cpu.mem_map(address, length)
|
|
cpu.mem_write(TEXT, bytes(text))
|
|
TABLE = BSS + by_name["_nv000672rm"]["st_value"]
|
|
assert by_name["_nv000672rm"]["st_size"] == 0x80
|
|
|
|
|
|
def write8(address, value):
|
|
cpu.mem_write(address, bytes([value & 0xff]))
|
|
|
|
|
|
def write32(address, value):
|
|
cpu.mem_write(address, struct.pack("<I", value & 0xffffffff))
|
|
|
|
|
|
def write64(address, value):
|
|
cpu.mem_write(address, struct.pack("<Q", value))
|
|
|
|
|
|
def read8(address):
|
|
return cpu.mem_read(address, 1)[0]
|
|
|
|
|
|
def read32(address):
|
|
return struct.unpack("<I", cpu.mem_read(address, 4))[0]
|
|
|
|
|
|
def read64(address):
|
|
return struct.unpack("<Q", cpu.mem_read(address, 8))[0]
|
|
|
|
|
|
write64(BSS + by_name["_nv029075rm"]["st_value"], SYS)
|
|
write64(SYS + 0x218, SYSTEM_218)
|
|
write64(VTABLE + 0x60, SLOTS["preload"])
|
|
write64(VTABLE + 0x68, SLOTS["load"])
|
|
write64(RPC + 0x528, SLOTS["rpc"])
|
|
write64(INSTANCE + 0xab90, INSTANCE_VALUE)
|
|
for slot, name in GPU_SLOTS.items():
|
|
write64(GPU + slot, SLOTS[name])
|
|
write64(GPU + 0xdb0, DESCRIPTORS)
|
|
write64(GPU + 0x1ed0, CHILD_1ED0)
|
|
write32(GPU + 0x423c, CLIENT)
|
|
write32(GPU + 0x4244, SUBDEVICE)
|
|
write32(GPU + 0xca4, CA4)
|
|
|
|
case = {}
|
|
trace = []
|
|
engines = {}
|
|
calls = {}
|
|
last = {}
|
|
ENTRIES = {}
|
|
visited = set()
|
|
|
|
|
|
def engine_key(pointer):
|
|
for key, address in engines.items():
|
|
if address == pointer:
|
|
return key
|
|
raise AssertionError(hex(pointer))
|
|
|
|
|
|
def descriptor_row(key):
|
|
for row in case["D"]:
|
|
if row[0] == key:
|
|
return row
|
|
raise AssertionError(hex(key))
|
|
|
|
|
|
def gpu_id(pointer):
|
|
if pointer == GPU:
|
|
return 0
|
|
if pointer == PARENT:
|
|
return 1
|
|
index, rest = divmod(pointer - OTHERS, 0x1000)
|
|
assert rest == 0 and 0 <= index < len(case["G"]), hex(pointer)
|
|
return 2 + index
|
|
|
|
|
|
def gpu_pointer(identity):
|
|
return (GPU, PARENT)[identity] if identity < 2 else OTHERS + 0x1000 * (identity - 2)
|
|
|
|
|
|
def numbered(name):
|
|
index = calls.get(name, 0)
|
|
calls[name] = index + 1
|
|
return index
|
|
|
|
|
|
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 arguments(emu):
|
|
return [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)]
|
|
|
|
|
|
def low(value):
|
|
return value & 0xffffffff
|
|
|
|
|
|
def leaf(name):
|
|
def run(emu, address, size, unused):
|
|
a, b, c, d, e, f = arguments(emu)
|
|
value = 0
|
|
if name == "_nv049674rm":
|
|
index = numbered("perf")
|
|
assert read64(a) == 0
|
|
trace.append("perf")
|
|
value = case[("perf1", "perf2")[index]]
|
|
if value == 0:
|
|
write64(a, TIMES[index])
|
|
elif name in ("_nv013521rm", "_nv060467rm", "_nv000797rm"):
|
|
assert a == GPU
|
|
trace.append(name[4:9].lstrip("0"))
|
|
elif name in ("_nv060495rm", "_nv060465rm", "_nv030853rm"):
|
|
assert a == GPU
|
|
trace.append(name[4:9].lstrip("0"))
|
|
value = case["s" + name[4:9].lstrip("0")]
|
|
elif name == "_nv030693rm":
|
|
assert a == GPU
|
|
trace.append("30693")
|
|
value = RPC if case["rpcobj"] else 0
|
|
elif name == "_nv031390rm":
|
|
assert a == GPU
|
|
trace.append("bc")
|
|
value = (case["bc"] >> min(numbered("bc"), 31)) & 1
|
|
elif name == "_nv030522rm":
|
|
assert a == GPU
|
|
trace.append(f"e:{low(b):x}")
|
|
value = engines.get(low(b), 0)
|
|
elif name in ("_nv026840rm", "_nv026839rm"):
|
|
assert low(b) in (4, 5)
|
|
kind = "pre" if name == "_nv026840rm" else "post"
|
|
trace.append(f"{kind}:{engine_key(a):x}:{low(b):x}")
|
|
elif name == "_nv054117rm":
|
|
assert (low(a), b) == (0, 0)
|
|
trace.append("unlock")
|
|
elif name == "_nv054115rm":
|
|
assert (low(a), low(b)) == (0, 0x400001)
|
|
trace.append("lock")
|
|
value = descriptor_row(last["key"])[3 if last["transition"] == 4 else 5]
|
|
elif name == "_nv013524rm":
|
|
index = read64(emu.reg_read(UC_X86_REG_RSP) + 8)
|
|
assert (a, b, f) == (GPU, GPU + 0x6998, DESCRIPTORS)
|
|
trace.append(f"store:{low(c):x}:{low(d):x}:{e:x}:{low(index):x}")
|
|
if case["clr"]:
|
|
write32(GPU + 0x6990, 0)
|
|
elif name == "_nv031447rm":
|
|
assert a == GPU
|
|
trace.append("count")
|
|
value = case["count1" if numbered("count") == 0 else "count2"]
|
|
elif name == "_nv031219rm":
|
|
identity = gpu_id(a)
|
|
trace.append(f"postload:{identity:x}:{low(b):x}")
|
|
value = (case["post"], case["ppost"])[identity] if identity < 2 else \
|
|
case["G"][identity - 2][2]
|
|
if identity == 0 and value and case["plrec"]:
|
|
write32(GPU + 0x6990, 0x15)
|
|
write32(GPU + 0x6994, value)
|
|
elif name == "_nv031400rm":
|
|
trace.append("info")
|
|
write32(a, case["acnt"])
|
|
write32(b, case["amask"])
|
|
elif name == "_nv031424rm":
|
|
index = read32(b)
|
|
trace.append(f"next:{low(a):x}:{index:x}")
|
|
if index < len(case["G"]):
|
|
write32(b, index + 1)
|
|
value = gpu_pointer(2 + index)
|
|
elif name == "_nv031415rm":
|
|
assert a == GPU
|
|
trace.append("31415")
|
|
value = VALUE_31415
|
|
elif name == "_nv031475rm":
|
|
trace.append(f"31475:{low(a):x}")
|
|
elif name == "_nv031494rm":
|
|
trace.append(f"31494:{low(a):x}:{low(b):x}")
|
|
elif name == "_nv031407rm":
|
|
assert a == GPU
|
|
trace.append("31407")
|
|
value = GROUP
|
|
elif name == "_nv031395rm":
|
|
trace.append(f"devmask:{low(a):x}")
|
|
value = case["devmask"]
|
|
elif name == "_nv031426rm":
|
|
assert a == GPU
|
|
trace.append("parent")
|
|
value = gpu_pointer(case["parent"])
|
|
elif name == "_nv031478rm":
|
|
assert (a, low(b), low(c), e) == (GPU, 1, 0, RODATA + NAME_OFFSET)
|
|
trace.append(f"31478:{low(d):x}")
|
|
elif name == "_nv031482rm":
|
|
assert a == GPU
|
|
trace.append(f"bcset:{low(b):x}")
|
|
elif name == "_nv036619rm":
|
|
trace.append("hyper")
|
|
value = case["hyper"]
|
|
elif name == "_nv041837rm":
|
|
assert (a, b) == (GPU, CHILD_1FC8)
|
|
trace.append("mig")
|
|
value = case["mig"]
|
|
elif name == "_nv031288rm":
|
|
assert (a, read32(c), read64(c + 4)) == (SYSTEM_218, CA4, 0)
|
|
trace.append(f"31288:{low(b):x}:{read32(c):x}")
|
|
value = case["s31288"]
|
|
elif name == "_nv043825rm":
|
|
assert (a, b) == (GPU, CHILD_1ED0)
|
|
trace.append("pma")
|
|
value = case["pma"]
|
|
elif name == "_nv047869rm":
|
|
assert low(a) == 0xe813abb
|
|
code = low(b)
|
|
count = ((code >> 12) & 0xf) + ((code >> 8) & 0xf)
|
|
words = (code, low(c), low(d))[:1 + count]
|
|
trace.append("d:" + ":".join(f"{word:x}" for word in words))
|
|
elif name == "_nv046745rm":
|
|
trace.append(f"a:{low(a):x}")
|
|
elif name == "_nv046747rm":
|
|
trace.append(f"s:{low(a):x}:{low(b):x}")
|
|
elif name == "_nv046751rm":
|
|
trace.append(f"l:{low(a):x}:{low(b):x}")
|
|
leave(emu, value)
|
|
return run
|
|
|
|
|
|
def thunk(emu, address, size, unused):
|
|
target = emu.reg_read(UC_X86_REG_RAX)
|
|
name = next(name for name, slot in SLOTS.items() if slot == target)
|
|
a, b, c, d, e, f = arguments(emu)
|
|
value = 0
|
|
if name == "sriov":
|
|
assert a == GPU
|
|
trace.append("sriov")
|
|
value = case["sriov"]
|
|
if case["sriovset"]:
|
|
write8(GPU + 0x4de7, int(case["sriovset"] == 1))
|
|
elif name == "share":
|
|
assert a == GPU
|
|
trace.append("share")
|
|
value = case["share"]
|
|
elif name == "rpc":
|
|
assert (a, b) == (GPU, RPC)
|
|
trace.append("rpc")
|
|
value = case["rpc"]
|
|
elif name == "ctrl":
|
|
assert (a, low(d), low(f)) == (GPU + 0x4260, 0x2080017e, 8)
|
|
trace.append(f"ctrl:{low(b):x}:{low(c):x}")
|
|
value = case["ctrl"]
|
|
if value == 0:
|
|
write64(e, CONTROL_VALUE)
|
|
else:
|
|
assert a == GPU
|
|
key = engine_key(b)
|
|
trace.append(f"{name}:{key:x}:{low(c):x}")
|
|
value = descriptor_row(key)[2 if name == "preload" else 4]
|
|
last.update(key=key, transition=4 if name == "preload" else 5)
|
|
leave(emu, value)
|
|
|
|
|
|
def trap(emu, address, size, unused):
|
|
trace.append("trap")
|
|
last["trap"] = True
|
|
emu.emu_stop()
|
|
|
|
|
|
def visit(emu, address, size, unused):
|
|
visited.add(address - TEXT)
|
|
|
|
|
|
def block(emu, address, size, unused):
|
|
offset = address - TEXT
|
|
assert (address in ENTRIES or address in (INDIRECT, STOP)
|
|
or any(start <= offset < end for start, end in RANGES)), hex(address)
|
|
|
|
|
|
cpu.hook_add(UC_HOOK_BLOCK, block)
|
|
for start, end in RANGES:
|
|
cpu.hook_add(UC_HOOK_CODE, visit, begin=TEXT + start, end=TEXT + end - 1)
|
|
cpu.hook_add(UC_HOOK_CODE, thunk, begin=INDIRECT, end=INDIRECT)
|
|
cpu.hook_add(UC_HOOK_CODE, trap, begin=TEXT + TRAP_OFFSET, end=TEXT + TRAP_OFFSET)
|
|
for hooked in HOOKED:
|
|
assert by_name[hooked]["st_shndx"] == text_section
|
|
entry = TEXT + by_name[hooked]["st_value"]
|
|
ENTRIES[entry] = hooked
|
|
cpu.hook_add(UC_HOOK_CODE, leaf(hooked), begin=entry, end=entry)
|
|
|
|
SCALARS = ("flags", "perf1", "perf2", "g4de7", "g52f9", "g543e", "e00", "c9c", "cac", "g95c",
|
|
"g5440", "g90c", "g908", "g1fc8", "rec", "s60495", "sriov", "sriovset", "rpcobj",
|
|
"rpc", "share", "bc", "s60465", "s30853", "ctrl", "inst", "count1", "count2", "post",
|
|
"plrec", "clr", "acnt", "amask", "w50", "devmask", "parent", "pc9c", "p908", "p90c",
|
|
"ppost", "hyper", "mig", "s31288", "pma")
|
|
ENGINE_ROWS = [(0x10000100, 1, 0, 0, 0, 0, 0x4c01), (0x10000200, 0, 0, 0, 0, 0, 0x4c02),
|
|
(0x10000300, 1, 0x56, 0, 0x56, 0, 0x4c03)]
|
|
DEFAULT = dict(flags=0, perf1=0, perf2=0, g4de7=0, g52f9=0, g543e=0, e00=0, c9c=3, cac=0,
|
|
g95c=0, g5440=0, g90c=0, g908=0, g1fc8=0, rec=0, s60495=0, sriov=0, sriovset=0,
|
|
rpcobj=1, rpc=0, share=0, bc=0, s60465=0, s30853=0, ctrl=0, inst=1, count1=1,
|
|
count2=1, post=0, plrec=0, clr=0, acnt=1, amask=8, w50=8, devmask=0x18, parent=0,
|
|
pc9c=4, p908=0, p90c=0, ppost=0, hyper=0, mig=0, s31288=0, pma=0, D=ENGINE_ROWS,
|
|
G=[])
|
|
rows = []
|
|
|
|
|
|
def run(**changes):
|
|
case.clear()
|
|
case.update(DEFAULT)
|
|
case.update(changes)
|
|
assert set(case) == set(SCALARS) | {"D", "G"} and case["c9c"] < 16
|
|
trace.clear()
|
|
engines.clear()
|
|
calls.clear()
|
|
last.clear()
|
|
for offset, name in ((0x4de7, "g4de7"), (0x52f9, "g52f9"), (0x543e, "g543e"), (0x95c, "g95c"),
|
|
(0x5440, "g5440"), (0x90c, "g90c")):
|
|
write8(GPU + offset, case[name])
|
|
for offset, name in ((0xe00, "e00"), (0xc9c, "c9c"), (0xcac, "cac"), (0x908, "g908"),
|
|
(0x6990, "rec")):
|
|
write32(GPU + offset, case[name])
|
|
write32(GPU + 0x6994, 0x70000009)
|
|
write64(GPU + 0x1fc8, CHILD_1FC8 if case["g1fc8"] else 0)
|
|
write64(GPU + 0x4138, 0xdeadbeefdeadbeef)
|
|
write64(GPU + 0x4218, 0x7777777777777777)
|
|
write8(GPU + 0x52f1, 0)
|
|
write8(GPU + 0x52f3, 0)
|
|
write32(PARENT + 0xc9c, case["pc9c"])
|
|
write32(PARENT + 0x908, case["p908"])
|
|
write8(PARENT + 0x90c, case["p90c"])
|
|
write32(GROUP + 0x50, case["w50"])
|
|
for index in range(16):
|
|
write64(TABLE + 8 * index, INSTANCE if case["inst"] and index == case["c9c"] else 0)
|
|
identities = range(2 + len(case["G"]))
|
|
for index, (word_908, byte_90c, _post) in enumerate(case["G"]):
|
|
write32(OTHERS + 0x1000 * index + 0x908, word_908)
|
|
write8(OTHERS + 0x1000 * index + 0x90c, byte_90c)
|
|
for identity in identities:
|
|
write8(gpu_pointer(identity) + 0x948, 1)
|
|
write8(gpu_pointer(identity) + 0x9a6, 1)
|
|
write32(GPU + 0xdc0, len(case["D"]))
|
|
for index, (descriptor, present, *_rest, word_4c) in enumerate(case["D"]):
|
|
write32(DESCRIPTORS + 4 * index, descriptor)
|
|
if present:
|
|
pointer = ENGINES + 0x100 * index
|
|
write64(pointer, VTABLE)
|
|
write32(pointer + 0x4c, word_4c)
|
|
engines[descriptor] = pointer
|
|
rsp = STACK + 0xffc8
|
|
cpu.reg_write(UC_X86_REG_RDI, GPU)
|
|
cpu.reg_write(UC_X86_REG_RSI, case["flags"])
|
|
cpu.reg_write(UC_X86_REG_RSP, rsp)
|
|
cpu.reg_write(UC_X86_REG_RBP, STACK + 0x8000)
|
|
write64(rsp, STOP)
|
|
cpu.emu_start(TEXT + RANGES[0][0], STOP, count=400000)
|
|
trapped = int(last.get("trap", False))
|
|
assert trapped or cpu.reg_read(UC_X86_REG_RIP) == STOP
|
|
assert trapped == int(trace[-1] == "trap")
|
|
status = 0 if trapped else cpu.reg_read(UC_X86_REG_RAX) & 0xffffffff
|
|
outputs = [trapped, status, read64(GPU + 0x4138), read8(GPU + 0x52f1), read8(GPU + 0x52f3),
|
|
read64(GPU + 0x4218), read32(GPU + 0x6990), read32(GPU + 0x6994)]
|
|
for offset in (0x948, 0x9a6):
|
|
outputs.append(sum((read8(gpu_pointer(identity) + offset) == 0) << identity
|
|
for identity in identities))
|
|
fields = [f"{name}={case[name]:x}" for name in SCALARS]
|
|
fields.append("D=" + ",".join(":".join(f"{value:x}" for value in row) for row in case["D"]))
|
|
fields.append("G=" + ",".join(":".join(f"{value:x}" for value in row) for row in case["G"]))
|
|
rows.append(" ".join(fields) + ";" + " ".join(f"{value:x}" for value in outputs) + ";" +
|
|
" ".join(trace))
|
|
|
|
|
|
# Scalar cases: every base mode with each single change applied on top.
|
|
FAIL_PRE_LOAD = [(0x10000100, 1, 0x51, 0, 0, 0, 0x4c01)] + ENGINE_ROWS[1:]
|
|
FAIL_PRE_LOCK = [(0x10000100, 1, 0x56, 0x40, 0, 0, 0x4c01)] + ENGINE_ROWS[1:]
|
|
FAIL_LOAD = ENGINE_ROWS[:2] + [(0x10000300, 1, 0, 0, 0x51, 0, 0x4c03)]
|
|
FAIL_LOAD_1E = ENGINE_ROWS[:2] + [(0x10000300, 1, 0, 0, 0x1e, 0, 0x4c03)]
|
|
FAIL_LOAD_LOCK = ENGINE_ROWS[:2] + [(0x10000300, 1, 0, 0, 0x56, 0x40, 0x4c03)]
|
|
PEERS = [(0, 0, 0), (0x100, 0, 0), (0x100, 1, 0)]
|
|
CHANGES = [
|
|
{}, {"perf1": 0x40}, {"perf2": 0x40}, {"s60495": 0x51}, {"sriov": 0x56}, {"sriovset": 1},
|
|
{"sriovset": 2}, {"rpcobj": 0}, {"rpc": 0x1f}, {"share": 0x1a}, {"bc": 1}, {"bc": 0xffffffff},
|
|
{"bc": 0x2}, {"bc": 0x10}, {"g543e": 1}, {"g543e": 1, "s60465": 0x51}, {"s30853": 0x56},
|
|
{"ctrl": 0x56}, {"ctrl": 0x51}, {"inst": 0}, {"post": 0x40}, {"post": 0x40, "plrec": 1},
|
|
{"hyper": 1}, {"hyper": 1, "s31288": 0x51}, {"hyper": 1, "g95c": 1},
|
|
{"hyper": 1, "g1fc8": 1}, {"hyper": 1, "g1fc8": 1, "mig": 1, "s31288": 0x51},
|
|
{"g1fc8": 1}, {"g1fc8": 1, "mig": 1}, {"pma": 0x51}, {"g1fc8": 1, "pma": 0x51},
|
|
{"g1fc8": 1, "mig": 1, "pma": 0x51},
|
|
{"D": []}, {"D": FAIL_PRE_LOAD}, {"D": FAIL_PRE_LOAD, "clr": 1}, {"D": FAIL_PRE_LOCK},
|
|
{"D": FAIL_LOAD}, {"D": FAIL_LOAD, "clr": 1}, {"D": FAIL_LOAD_1E},
|
|
{"D": FAIL_LOAD_1E, "bc": 0xffffffff}, {"D": FAIL_LOAD_LOCK},
|
|
{"g90c": 1}, {"g5440": 1}, {"g90c": 1, "g5440": 1},
|
|
{"g90c": 1, "g5440": 1, "G": PEERS, "acnt": 2}, {"g90c": 1, "g5440": 1, "G": PEERS, "acnt": 3},
|
|
{"count1": 2, "count2": 1}, {"count1": 2, "count2": 1, "w50": 0x18},
|
|
{"count1": 2, "count2": 1, "cac": 0x20}, {"count1": 2, "count2": 1, "cac": 0x20, "g90c": 1,
|
|
"g5440": 1},
|
|
{"count1": 3, "count2": 3, "G": PEERS, "devmask": 0x38, "parent": 1, "bc": 0xffffffff},
|
|
{"count1": 3, "count2": 3, "G": PEERS, "devmask": 0x38, "parent": 1, "p908": 0x100},
|
|
{"count1": 3, "count2": 3, "G": PEERS, "devmask": 0x38, "parent": 1, "ppost": 0x40},
|
|
{"count1": 4, "count2": 4, "G": PEERS, "cac": 0x20, "post": 0x40, "plrec": 1},
|
|
{"count1": 3, "count2": 3, "G": [(0, 0, 0), (0x100, 1, 0x51), (0, 0, 0)], "devmask": 0x38},
|
|
]
|
|
for flags in (0, 1, 2):
|
|
for sriov_fields in ((0, 0), (1, 0), (1, 1)):
|
|
for e00 in (0, 1, 0x100, 0x10000, 0x10001):
|
|
for rec in (0, 0x77):
|
|
for change in CHANGES:
|
|
base = dict(flags=flags, g4de7=sriov_fields[0], g52f9=sriov_fields[1],
|
|
e00=e00, rec=rec)
|
|
base.update(change)
|
|
run(**base)
|
|
scalar_cases = len(rows)
|
|
|
|
generator = random.Random(580178)
|
|
GOOD, PRE_LOAD, LOAD = (0, 0, 0x56), (0, 0, 0, 0x56, 0x51, 0x1e), (0, 0, 0, 0x56, 0x1e, 0x51)
|
|
for _ in range(2500):
|
|
clean = generator.randrange(2) == 0
|
|
pool = [0x10000100 + 0x100 * index for index in range(8)]
|
|
generator.shuffle(pool)
|
|
descriptors = []
|
|
for index, descriptor in enumerate(pool[:generator.randrange(0, 7)]):
|
|
descriptors.append((
|
|
descriptor, int(generator.randrange(4) != 0),
|
|
generator.choice(GOOD if clean else PRE_LOAD),
|
|
0 if clean else generator.choice((0, 0, 0, 0, 0, 0x40)),
|
|
generator.choice(GOOD if clean else LOAD),
|
|
0 if clean else generator.choice((0, 0, 0, 0, 0, 0x40)),
|
|
0x4c00 + index))
|
|
peers = [(generator.choice((0, 0, 0x100, 0x1200, 0xff)), generator.randrange(2),
|
|
generator.choice((0, 0, 0, 0, 0x51)))
|
|
for _peer in range(generator.randrange(0, 5))]
|
|
values = dict(
|
|
flags=generator.randrange(4), g4de7=generator.randrange(2), g52f9=generator.randrange(2),
|
|
g543e=generator.randrange(2), e00=generator.choice((0, 0, 0, 1, 0x100, 0x10001, 0x10000)),
|
|
c9c=generator.randrange(16), cac=generator.choice((0, 1, 0x20)),
|
|
g95c=generator.randrange(2), g5440=generator.randrange(2), g90c=generator.randrange(2),
|
|
g908=generator.choice((0, 0x100, 0xff)), g1fc8=generator.randrange(2),
|
|
rec=generator.choice((0, 0, 0, 0x77)), sriovset=generator.choice((0, 0, 0, 1, 2)),
|
|
rpcobj=generator.randrange(2), bc=generator.choice((0, 0, generator.getrandbits(32))),
|
|
s30853=generator.choice((0, 0x56)), ctrl=generator.choice((0, 0, 0x56, 0x51)),
|
|
inst=int(generator.randrange(4) != 0), count1=generator.randrange(5),
|
|
post=generator.choice((0, 0, 0, 0x40)), plrec=generator.randrange(2),
|
|
clr=generator.randrange(2), amask=generator.getrandbits(8),
|
|
devmask=generator.getrandbits(16), parent=generator.randrange(2),
|
|
pc9c=generator.randrange(40), p908=generator.choice((0, 0x100)),
|
|
p90c=generator.randrange(2), ppost=generator.choice((0, 0, 0, 0x40)),
|
|
hyper=generator.randrange(2), mig=generator.randrange(2),
|
|
s31288=generator.choice((0, 0x51)), pma=generator.choice((0, 0, 0x51)))
|
|
for name in ("perf1", "perf2", "s60495", "sriov", "rpc", "share", "s60465"):
|
|
values[name] = 0 if clean else generator.choice((0, 0, 0, 0, 0, 0, 0, 0x51))
|
|
# Fixture inputs that make the two count comparisons agree in most cases.
|
|
powered = 1 + sum(word & 0xffff00 == 0 for word, _byte, _post in peers)
|
|
values["acnt"] = generator.choice((powered, powered, generator.randrange(6)))
|
|
if values["cac"] == 0x20:
|
|
first, mask = (values["g908"], values["g90c"]), 0
|
|
else:
|
|
first = (values["p908"], values["p90c"]) if values["parent"] else (values["g908"],
|
|
values["g90c"])
|
|
bit = (values["pc9c"] if values["parent"] else values["c9c"]) & 31
|
|
mask = values["devmask"] & ~(1 << bit)
|
|
ready = sum(mask == 0 or word & 0xffff00 == 0 or byte != 0
|
|
for word, byte in [first] + [peer[:2] for peer in peers])
|
|
values["count2"] = generator.choice((ready, ready, ready, generator.randrange(6)))
|
|
values["w50"] = generator.choice((0, 1 << values["c9c"], 1 << values["c9c"], 0x30000))
|
|
run(D=descriptors, G=peers, **values)
|
|
engine_cases = len(rows) - scalar_cases
|
|
|
|
# Instructions of the executed ranges that no case executed. The UD2 block is
|
|
# stopped at its entry, so its two instructions and the padding never execute.
|
|
visited.discard(TRAP_OFFSET)
|
|
decoder = Cs(CS_ARCH_X86, CS_MODE_64)
|
|
instructions = [instruction.address for start, end in RANGES
|
|
for instruction in decoder.disasm(bytes(text[start:end]), start)]
|
|
unvisited = [address for address in instructions if address not in visited]
|
|
assert visited <= set(instructions)
|
|
assert all(mnemonic == "nop" or TRAP_OFFSET <= address < RANGES[1][1]
|
|
for start, end in RANGES
|
|
for address, _size, mnemonic, _operands in decoder.disasm_lite(bytes(text[start:end]),
|
|
start)
|
|
if address not in visited), [hex(address) for address in unvisited]
|
|
|
|
out = ROOT / "target/gpu-stage"
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
oracle = "\n".join(rows) + "\n"
|
|
(out / "original-base-load.txt").write_text(oracle)
|
|
report = {
|
|
"original_sha256": DIGEST,
|
|
"executed": ["_nv031217rm", "_nv031217rm.cold"],
|
|
"fixture_supplied": sorted(HOOKED) + [f"GPU+{slot:x}" for slot in sorted(GPU_SLOTS)] +
|
|
["_nv030693rm object +528", "engine vtable +60", "engine vtable +68"],
|
|
"scalar_cases": scalar_cases, "engine_list_cases": engine_cases,
|
|
"trap_cases": sum(row.endswith(" trap") for row in rows),
|
|
"instructions": len(instructions), "instructions_executed": len(visited),
|
|
"instructions_not_executed": [f"text+{address:x}" for address in unvisited],
|
|
"random_seed": 580178,
|
|
"oracle": "target/gpu-stage/original-base-load.txt",
|
|
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
|
|
"scope": "Original control flow of the base StateLoad pass: the SR-IOV and client-share "
|
|
"prelude, the per-engine PreLoad and Load loops with the first-failure record, the "
|
|
"GPU+4218 control, the single and broadcast PostLoad dispatch and the tail. All "
|
|
"callees including the PostLoad pass _nv031219rm are fixture-supplied, so no callee, "
|
|
"engine or hardware behavior is exercised. The UD2 block is detected at its entry "
|
|
"and recorded as a trap; its instructions are not executed.",
|
|
}
|
|
(ROOT / "reports/gpu-base-load-original.json").write_text(json.dumps(report, indent=2) + "\n")
|
|
print(f"{scalar_cases} scalar and {engine_cases} engine-list original base Load cases")
|