mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
515 lines
27 KiB
Python
515 lines
27 KiB
Python
#!/usr/bin/env python3
|
|
"""Run the original GV100 clock-gating table loaders and write an oracle.
|
|
|
|
Executed original code: the Graphics routine _nv033505rm that the Graphics
|
|
PreInitLocked body _nv033667rm calls, the Graphics functions it reaches at
|
|
+d10 (_nv033245rm), +1478 (_nv033738rm) and +1480 (_nv033764rm), the table
|
|
loaders _nv030976rm and _nv030310rm, the GPU-object loader at GPU+5c8
|
|
(_nv030977rm), and the GPU-object functions they call at +740 (_nv030423rm
|
|
with _nv030556rm), +748 (_nv030728rm with _nv030732rm), +750 (_nv030519rm),
|
|
+758 (_nv030891rm), +760 (_nv030902rm) and +768 (_nv030894rm). The function
|
|
pointers are stored by the original binders: _nv005980rm and _nv005982rm for
|
|
the GPU object, _nv005887rm, _nv005888rm and _nv005890rm for Graphics, chip HAL
|
|
0x23 and RM HAL 2. The tables are read by the original instructions from the
|
|
object's .data and .rodata images with their relocations applied.
|
|
|
|
Fixture-supplied services: register read _nv053907rm and write _nv053912rm,
|
|
the feature query _nv031034rm and record _nv031133rm, the Graphics lookup
|
|
_nv000333rm with the function at +40 of the vtable of its +17a8 object, the
|
|
routines _nv042890rm, _nv042924rm and _nv042925rm, the functions at +118 and
|
|
+178 of the object at +11d8 of the GPU+22c8 object, and the Graphics functions
|
|
at +c78 (GV100 _nv032989rm) and +1038 (GV100 _nv031785rm). No hardware is
|
|
involved.
|
|
|
|
The same data is compared with the Rust constants in
|
|
src/nvidia/src/gpu/clock_gating_tables.rs; --write-tables regenerates them.
|
|
"""
|
|
import bisect
|
|
from _binary_path import resolve_580_binary
|
|
import hashlib
|
|
import itertools
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import struct
|
|
import sys
|
|
|
|
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 = resolve_580_binary()
|
|
DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b"
|
|
TABLES = ROOT / "src/nvidia/src/gpu/clock_gating_tables.rs"
|
|
TEXT, SDATA, DATA, STACK, STOP, RODATA = (0x10000000, 0x20000000, 0x30000000, 0x40000000,
|
|
0x50000000, 0x60000000)
|
|
(GPU, GR, CHIP, RM, OWNER, CHILD, CONTEXT, KGR, REGISTERS, VTABLE) = (
|
|
DATA, DATA + 0x10000, DATA + 0x20000, DATA + 0x21000, DATA + 0x22000, DATA + 0x25000,
|
|
DATA + 0x26000, DATA + 0x27000, DATA + 0x2a000, DATA + 0x2b000)
|
|
(INDIRECT_RAX, INDIRECT_R8, GR_WRITE, CHILD_118, CHILD_178, GR_C78, GR_1038) = (
|
|
STOP + 0x100, STOP + 0x110, STOP + 0x200, STOP + 0x210, STOP + 0x220, STOP + 0x230,
|
|
STOP + 0x240)
|
|
# Section offsets of the descriptor arrays the three lookups walk, and lengths.
|
|
BLCG_AT, BLCG_COUNT = 0x46c40, 0x15 # _nv030423rm
|
|
SLCG_AT, SLCG_COUNT = 0x47720, 0x1e # _nv030728rm
|
|
ENGINES_AT, ENGINES_COUNT = 0x4ce0f00, 0x12 # _nv030519rm, .rodata
|
|
# The binders only store pointers, so the functions they select are listed.
|
|
ROOTS = ("_nv033505rm", "_nv005980rm", "_nv005982rm", "_nv005887rm", "_nv005888rm",
|
|
"_nv005890rm",
|
|
"_nv033245rm", "_nv033738rm", "_nv033764rm", "_nv030977rm", "_nv030976rm",
|
|
"_nv030310rm", "_nv030423rm", "_nv030728rm", "_nv030519rm", "_nv030891rm",
|
|
"_nv030902rm", "_nv030894rm")
|
|
HOOKED = ("_nv053907rm", "_nv053912rm", "_nv031034rm", "_nv031133rm", "_nv000333rm",
|
|
"_nv042890rm", "_nv042924rm", "_nv042925rm")
|
|
GPU_SLOTS = {0x5c8: "_nv030977rm", 0x740: "_nv030423rm", 0x748: "_nv030728rm",
|
|
0x750: "_nv030519rm", 0x758: "_nv030891rm", 0x760: "_nv030902rm",
|
|
0x768: "_nv030894rm"}
|
|
GR_SLOTS = {0xd10: "_nv033245rm", 0x1478: "_nv033738rm", 0x1480: "_nv033764rm",
|
|
0xc78: "_nv032989rm", 0x1038: "_nv031785rm"}
|
|
|
|
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
|
|
|
|
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())
|
|
data = bytearray(elf.get_section_by_name(".data").data())
|
|
text_section = elf.get_section_index(".text")
|
|
data_section = elf.get_section_index(".data")
|
|
rodata_section = elf.get_section_index(".rodata")
|
|
rodata_header = elf.get_section_by_name(".rodata")
|
|
stream.seek(rodata_header["sh_offset"] + ENGINES_AT)
|
|
engine_bytes = stream.read(8 * ENGINES_COUNT)
|
|
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())
|
|
data_relocations = {relocation["r_offset"]: (relocation["r_info_type"],
|
|
relocation["r_info_sym"],
|
|
relocation["r_addend"])
|
|
for relocation in elf.get_section_by_name(".rela.data").iter_relocations()}
|
|
|
|
|
|
def descriptors(at, count, width):
|
|
"""The (id, entries) list of one descriptor array, read from the object's data."""
|
|
tables = []
|
|
for index in range(count):
|
|
offset = at + 24 * index
|
|
ident, pad, pointer, length, tail = struct.unpack_from("<IIQII", data, offset)
|
|
kind, symbol, addend = data_relocations[offset + 8]
|
|
assert (kind, pad, pointer, tail) == (1, 0, 0, 0), hex(offset)
|
|
assert symbols[symbol]["st_shndx"] == data_section and symbols[symbol]["st_value"] == 0
|
|
words = struct.unpack_from(f"<{length * width}I", data, addend)
|
|
tables.append((ident, [words[i:i + width] for i in range(0, len(words), width)], addend))
|
|
return tables
|
|
|
|
|
|
BLCG = descriptors(BLCG_AT, BLCG_COUNT, 2)
|
|
SLCG = descriptors(SLCG_AT, SLCG_COUNT, 3)
|
|
ENGINES = [struct.unpack_from("<II", engine_bytes, 8 * index) for index in range(ENGINES_COUNT)]
|
|
assert ENGINES[0][0] == 0x400000 # _nv030519rm compares the first identifier as a constant
|
|
table_bytes = (bytes(data[BLCG_AT:BLCG_AT + 24 * BLCG_COUNT])
|
|
+ bytes(data[SLCG_AT:SLCG_AT + 24 * SLCG_COUNT]) + engine_bytes
|
|
+ b"".join(struct.pack(f"<{len(row)}I", *row)
|
|
for tables in (BLCG, SLCG) for _id, rows, _at in tables for row in rows))
|
|
TABLE_DIGEST = hashlib.sha256(table_bytes).hexdigest()
|
|
# Descriptor arrays as stored (pointers unrelocated), engine pairs, then entries.
|
|
assert TABLE_DIGEST == "29abc8da7db58c5d000e11a2eb65a3acab6b5054792ac6a8e0fbfa6d33dcf072"
|
|
|
|
|
|
def rust_tables():
|
|
def rows(tables):
|
|
return "".join(
|
|
f" (\n {ident:#x},\n &[\n" + "".join(
|
|
" (" + ", ".join(f"{word:#x}" for word in row) + "),\n"
|
|
for row in entries) + " ],\n ),\n"
|
|
for ident, entries, _at in tables)
|
|
return (
|
|
"//! Clock-gating tables of the GV100 GPU object, copied from the .data and\n"
|
|
"//! .rodata of the original object by tools/check_gpu_clock_gating_binary.py\n"
|
|
"//! (--write-tables). The same tool compares every number below with the\n"
|
|
"//! object on each run. Do not edit by hand.\n\n"
|
|
"/// A table identifier and its entries.\n"
|
|
"pub type Table<E> = (u32, &'static [E]);\n\n"
|
|
f"/// The {BLCG_COUNT:#x} descriptors _nv030423rm (GPU+740) searches: table identifier, then\n"
|
|
"/// (register, value) pairs.\n"
|
|
"#[rustfmt::skip]\n"
|
|
"pub const BLCG: &[Table<(u32, u32)>] = &[\n" + rows(BLCG) + "];\n\n"
|
|
f"/// The {SLCG_COUNT:#x} descriptors _nv030728rm (GPU+748) searches: table identifier, then\n"
|
|
"/// (register, first value, second value) triples.\n"
|
|
"#[rustfmt::skip]\n"
|
|
"pub const SLCG: &[Table<(u32, u32, u32)>] = &[\n" + rows(SLCG) + "];\n\n"
|
|
f"/// The {ENGINES_COUNT:#x} (table identifier, engine descriptor) pairs _nv030519rm (GPU+750)\n"
|
|
"/// searches.\n"
|
|
"#[rustfmt::skip]\n"
|
|
"pub const ENGINE_DESCRIPTORS: &[(u32, u32)] = &[\n" + "".join(
|
|
f" ({ident:#x}, {descriptor:#x}),\n" for ident, descriptor in ENGINES) + "];\n")
|
|
|
|
|
|
if "--write-tables" in sys.argv:
|
|
TABLES.write_text(rust_tables())
|
|
source = TABLES.read_text()
|
|
for name, expected in (
|
|
("BLCG", [w for ident, rows, _at in BLCG for w in (ident, *itertools.chain(*rows))]),
|
|
("SLCG", [w for ident, rows, _at in SLCG for w in (ident, *itertools.chain(*rows))]),
|
|
("ENGINE_DESCRIPTORS", [w for pair in ENGINES for w in pair])):
|
|
block = re.search(rf"pub const {name}: [^=]*= &\[\n(.*?)\n\];", source, re.S)[1]
|
|
found = [int(word.replace("_", ""), 16) for word in re.findall(r"0x[0-9a-f_]+", block)]
|
|
assert found == expected, f"{TABLES} {name} differs from the original object"
|
|
# The nesting must match too, so the full text is compared as generated.
|
|
assert source == rust_tables(), f"{TABLES} is not the generated text"
|
|
|
|
# Relocate the executed code. Data references go to the section images below.
|
|
offsets = [entry[0] for entry in relocations]
|
|
entries = {symbol[0]: symbol[2] for symbol in starts}
|
|
executed, pending = [], list(ROOTS)
|
|
bases = {text_section: TEXT, data_section: SDATA, rodata_section: RODATA}
|
|
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_RAX
|
|
elif target.name == "__x86_indirect_thunk_r8":
|
|
address = INDIRECT_R8
|
|
else:
|
|
assert target["st_shndx"] in bases, (name, target.name)
|
|
address = bases[target["st_shndx"]] + target["st_value"]
|
|
# Follow direct calls and tail calls only, not stored pointers.
|
|
if (target["st_shndx"] == text_section and 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)
|
|
# The entry pointers of the two descriptor arrays, as the linker would store them.
|
|
for at, count in ((BLCG_AT, BLCG_COUNT), (SLCG_AT, SLCG_COUNT)):
|
|
for index in range(count):
|
|
kind, symbol, addend = data_relocations[at + 24 * index + 8]
|
|
struct.pack_into("<Q", data, at + 24 * index + 8, SDATA + addend)
|
|
|
|
cpu = Uc(UC_ARCH_X86, UC_MODE_64)
|
|
rodata_page = ENGINES_AT & ~0xfff
|
|
for address, size in ((TEXT, (len(text) + 4095) & ~4095), (SDATA, (len(data) + 4095) & ~4095),
|
|
(DATA, 0x40000), (STACK, 0x10000), (STOP, 0x1000),
|
|
(RODATA + rodata_page, 0x2000)):
|
|
cpu.mem_map(address, size)
|
|
cpu.mem_write(TEXT, bytes(text))
|
|
cpu.mem_write(SDATA, bytes(data))
|
|
cpu.mem_write(RODATA + ENGINES_AT, engine_bytes)
|
|
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], low[5]) == (GPU + 0x4120, 0, 0, 0)
|
|
trace.append(f"w:{low[3]:x}:{low[4]:x}")
|
|
elif name == "_nv031034rm":
|
|
assert a == GPU
|
|
value = case["feature"][low[1]]
|
|
trace.append(f"q:{low[1]:x}:{low[2]:x}:{value:x}")
|
|
elif name == "_nv031133rm":
|
|
assert a == GPU and low[3] in (0, 1), full
|
|
trace.append(f"s:{low[1]:x}:{low[2]:x}:{low[3]:x}")
|
|
elif name == "_nv000333rm":
|
|
assert a == GPU
|
|
value = KGR
|
|
trace.append("k")
|
|
elif name == "GR_WRITE":
|
|
assert a == REGISTERS
|
|
trace.append(f"g:{low[1]:x}:{low[2]:x}")
|
|
elif name == "_nv042890rm":
|
|
assert (a, b, low[2]) == (GPU, CONTEXT, 0), full
|
|
# The two fixture values alternate from call to call.
|
|
value = case["gate"][case["gate_calls"] % 2]
|
|
case["gate_calls"] += 1
|
|
trace.append(f"p:{value:x}")
|
|
elif name in ("_nv042924rm", "_nv042925rm"):
|
|
assert (a, b, low[2], low[3]) == (GPU, CONTEXT, 0, 0), full
|
|
trace.append("p+" if name == "_nv042924rm" else "p-")
|
|
elif name == "CHILD_118":
|
|
assert (a, b) == (GPU, CHILD) and low[2] in (0, 1), full
|
|
trace.append(f"f118:{low[2]:x}")
|
|
elif name == "CHILD_178":
|
|
assert (a, b) == (GPU, CHILD), full
|
|
value = (0, 1, low[2] >> 4 & 1)[case["skip"]]
|
|
trace.append(f"f178:{low[2]:x}:{value:x}")
|
|
elif name == "GR_C78":
|
|
assert (a, b) == (GPU, GR), full
|
|
value = case["c78"]
|
|
trace.append(f"c78:{value:x}")
|
|
elif name == "GR_1038":
|
|
assert (a, b) == (GPU, GR), full
|
|
trace.append("x1038")
|
|
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_RAX, end=INDIRECT_RAX)
|
|
cpu.hook_add(UC_HOOK_CODE, lambda emu, address, size, unused: emu.reg_write(
|
|
UC_X86_REG_RIP, emu.reg_read(UC_X86_REG_R8)), begin=INDIRECT_R8, end=INDIRECT_R8)
|
|
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 (("GR_WRITE", GR_WRITE), ("CHILD_118", CHILD_118), ("CHILD_178", CHILD_178),
|
|
("GR_C78", GR_C78), ("GR_1038", GR_1038)):
|
|
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=4000000)
|
|
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 pointer(address):
|
|
return struct.unpack("<Q", cpu.mem_read(address, 8))[0]
|
|
|
|
|
|
cpu.mem_write(CHIP + 0x10, struct.pack("<H", 0x23))
|
|
cpu.mem_write(RM + 0x10, struct.pack("<H", 2))
|
|
q(GPU + 0x80, CHIP)
|
|
q(GPU + 0x88, RM)
|
|
call("_nv005980rm", GPU)
|
|
call("_nv005982rm", GPU)
|
|
call("_nv005887rm", GR, CHIP, RM)
|
|
call("_nv005888rm", GR, CHIP, RM)
|
|
call("_nv005890rm", GR, CHIP, RM)
|
|
bound = {}
|
|
for label, base, slots in (("GPU", GPU, GPU_SLOTS), ("Graphics", GR, GR_SLOTS)):
|
|
for slot, function in slots.items():
|
|
assert entries.get(pointer(base + slot) - TEXT) == function, (label, hex(slot))
|
|
bound[f"{label}+{slot:#x}"] = function
|
|
# The two Graphics functions below are services of this oracle.
|
|
q(GR + 0xc78, GR_C78)
|
|
q(GR + 0x1038, GR_1038)
|
|
q(KGR + 0x17a8, REGISTERS)
|
|
q(REGISTERS, VTABLE)
|
|
q(VTABLE + 0x40, GR_WRITE)
|
|
q(CHILD + 0x118, CHILD_118)
|
|
q(CHILD + 0x178, CHILD_178)
|
|
|
|
WATCHED = ((GPU, 0x10000), (GR, 0x2000), (OWNER, 0x2000), (CHILD, 0x1000), (KGR, 0x2000),
|
|
(SDATA + BLCG_AT - 0x400, 0x1800))
|
|
FIELDS = ("entry", "ident", "flag", "owner", "context", "byte_269", "byte_280", "child",
|
|
"byte_183", "byte_184", "byte_189", "byte_18b", "gpu_6d41", "gr_160c", "gr_1596",
|
|
"feature_7", "feature_13", "gate_first", "gate_second", "skip", "c78")
|
|
rows, counts = [], {}
|
|
ENTRY = {"load_blcg": 0, "clear_blcg": 1, "load_slcg": 2, "gr_blcg": 3, "gr_slcg": 4,
|
|
"gr_clock_gating": 5}
|
|
|
|
|
|
def run(entry, ident=0, flag=0, owner=0, context=0, byte_269=0, byte_280=0, child=0,
|
|
byte_183=0, byte_184=0, byte_189=0, byte_18b=0, gpu_6d41=0, gr_160c=0, gr_1596=0,
|
|
feature_7=0, feature_13=0, gate_first=0, gate_second=0, skip=0, c78=0, reads=()):
|
|
q(GPU + 0x22c8, OWNER if owner else 0)
|
|
q(OWNER + 0x11c0, CONTEXT if context else 0)
|
|
q(OWNER + 0x11d8, CHILD if child else 0)
|
|
cpu.mem_write(OWNER + 0x269, bytes([byte_269]))
|
|
cpu.mem_write(OWNER + 0x280, bytes([byte_280]))
|
|
for offset, value in ((0x183, byte_183), (0x184, byte_184), (0x189, byte_189),
|
|
(0x18b, byte_18b)):
|
|
cpu.mem_write(CHILD + offset, bytes([value]))
|
|
cpu.mem_write(GPU + 0x6d41, bytes([gpu_6d41]))
|
|
cpu.mem_write(GR + 0x160c, bytes([gr_160c]))
|
|
cpu.mem_write(GR + 0x1596, bytes([gr_1596]))
|
|
case.update(reads=list(reads), feature={7: feature_7, 0x13: feature_13},
|
|
gate=[gate_first, gate_second], gate_calls=0, skip=skip, c78=c78)
|
|
trace.clear()
|
|
before = [bytes(cpu.mem_read(base, size)) for base, size in WATCHED]
|
|
status = "-"
|
|
if entry == "load_blcg":
|
|
call("_nv030976rm", GPU, ident)
|
|
elif entry == "clear_blcg":
|
|
call("_nv030310rm", GPU, ident)
|
|
elif entry == "load_slcg":
|
|
cpu.reg_write(UC_X86_REG_RAX, pointer(GPU + 0x5c8))
|
|
call("_nv030977rm", GPU, ident)
|
|
elif entry == "gr_blcg":
|
|
status = f"{call('_nv033738rm', GPU, GR, flag):x}"
|
|
elif entry == "gr_slcg":
|
|
call("_nv033764rm", GPU, GR)
|
|
else:
|
|
status = f"{call('_nv033505rm', GPU, GR):x}"
|
|
assert not case["reads"], (entry, case["reads"])
|
|
# The routines store nothing outside their stack frames.
|
|
for (base, size), old in zip(WATCHED, before):
|
|
assert bytes(cpu.mem_read(base, size)) == old, hex(base)
|
|
values = locals()
|
|
inputs = [ENTRY[entry]] + [values[field] for field in FIELDS[1:]]
|
|
rows.append(" ".join(f"{value:x}" for value in inputs) + ";"
|
|
+ " ".join(f"{value:x}" for value in reads) + f";{status};" + " ".join(trace))
|
|
counts[entry] = counts.get(entry, 0) + 1
|
|
return list(trace)
|
|
|
|
|
|
# Physical first-open sequence of the original driver on a V100 (BAR0 offsets),
|
|
# lines 37609 to 37712 of the per-GPU register transcript: the PMC enable read
|
|
# of _nv033505rm, 54 writes of the first-table values, 49 of the second.
|
|
PHYSICAL = """r:200:1fecdff1
|
|
w:4041f0:c646 w:409890:7f w:4098b0:7f w:407000:4000c242 w:405bf0:c444 w:4078c0:4242
|
|
w:406000:c444 w:405860:c242 w:40590c:c444 w:408040:404 w:408810:c242 w:408a80:c242
|
|
w:4089a8:c242 w:41a890:427f w:41a8b0:7f w:418c70:c444 w:418cf0:c444 w:419020:c242
|
|
w:419038:42 w:418898:4242 w:419c70:c444 w:419c80:4048 w:419c88:48 w:419c90:a w:419c98:42
|
|
w:419ca0:43 w:419ca8:3 w:419cb0:2 w:419a40:545 w:419a48:4545 w:419a50:4545 w:419a58:4545
|
|
w:419a60:505 w:419a68:505 w:419a78:505 w:419a80:4545 w:418500:c244 w:418608:c242
|
|
w:418688:c242 w:418718:42 w:418828:8444 w:418bbc:c242 w:418970:c242 w:418d70:c444
|
|
w:418f0c:c444 w:418e0c:c444 w:419868:8243 w:41be28:8242 w:41bfe8:c444 w:41bed0:c444
|
|
w:13c820:1004a w:13dc04:1004a w:13c920:4a w:13cbe0:42
|
|
w:4041f4:0 w:40917c:20008 w:409894:0 w:407004:0 w:405bf4:0 w:4078c4:0 w:406004:200
|
|
w:405864:0 w:405910:fffffff0 w:408044:0 w:408814:0 w:408a84:0 w:4089ac:0 w:408a24:0
|
|
w:41a17c:20008 w:41a894:0 w:418c74:ffffff80 w:418cf4:fffffff8 w:419024:1fe w:41889c:0
|
|
w:419d24:0 w:419c74:1e w:419c84:3fffe w:419c8c:ffffff84 w:419c94:240 w:419ca4:3ffe
|
|
w:419cac:1fffe w:419a44:8 w:419a4c:1f8 w:419a54:3c w:419a5c:c w:419a64:186 w:419a7c:3c
|
|
w:419a84:c w:418504:0 w:41860c:0 w:41868c:0 w:41871c:0 w:418388:0 w:41882c:0 w:418bc0:0
|
|
w:418974:0 w:418d74:ffffffe0 w:418f10:ffffffe0 w:418e10:fffffffe w:41986c:104
|
|
w:41be2c:4115fc0 w:41bfec:fffffff0 w:41bed4:fffffff8""".split()
|
|
assert len(PHYSICAL) == 104
|
|
transcript = ROOT / "target/trace/b3-bar0.txt"
|
|
if transcript.exists():
|
|
lines = transcript.read_text().splitlines()[37608:37712]
|
|
assert [f"{kind.lower()}:{int(offset, 16):x}:{int(value, 16):x}" for kind, offset, value, _time
|
|
in (line.split() for line in lines)] == PHYSICAL
|
|
# Writes through the Graphics register object appear as plain BAR0 writes. The
|
|
# GPU+22c8 object is present: without it the original faults on table 0x400002.
|
|
observed = run("gr_clock_gating", owner=1, gr_160c=1, c78=1, reads=(0x1fecdff1,))
|
|
assert [("w" + entry[1:]) if entry[0] == "g" else entry for entry in observed
|
|
if entry[0] in "rwg"] == PHYSICAL, observed
|
|
|
|
IDENTS = sorted({ident for ident, _rows, _at in BLCG + SLCG} | {0x999000, 0x400004, 0x3fffff})
|
|
KEY = (0x400000, 0x400002, 0x9a0000, 0x240000, 0x13b000, 0x100000, 0x999000)
|
|
OWNERS = ((0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1)) # owner, child, byte 183 or 184
|
|
STATES = (0, 1, 2, 0x101)
|
|
|
|
# _nv030976rm. Table 0x400002 reads the owner's byte 280, and with it set calls
|
|
# the child: the original faults without them, so those inputs are left out.
|
|
for ident, (owner, child, byte_183), byte_280, gpu_6d41, state in itertools.product(
|
|
IDENTS, OWNERS, (0, 1), (0, 1), STATES):
|
|
if ident == 0x400002 and not (child and byte_183):
|
|
if not owner or (byte_280 and not child):
|
|
continue
|
|
for skip in ((0, 1, 2) if ident == 0x400002 and byte_280 and child and not byte_183
|
|
else (0,)):
|
|
run("load_blcg", ident=ident, owner=owner, child=child, byte_183=byte_183,
|
|
byte_280=byte_280, gpu_6d41=gpu_6d41, feature_7=state, feature_13=state ^ 1,
|
|
skip=skip)
|
|
# _nv030310rm reads neither the owner nor the feature state.
|
|
for ident, owner in itertools.product(IDENTS, (0, 1)):
|
|
run("clear_blcg", ident=ident, owner=owner, child=owner, byte_183=owner, feature_7=1)
|
|
# _nv030977rm.
|
|
CHILDREN = [(0, 0, 0, 0)] + [(1, *flags) for flags in itertools.product((0, 1), repeat=3)]
|
|
for ident, state, (gate_first, gate_second) in itertools.product(
|
|
KEY, (0, 1, 2), ((0, 0), (1, 1), (1, 0), (0, 1))):
|
|
run("load_slcg", ident=ident, feature_13=state, feature_7=state ^ 1,
|
|
gate_first=gate_first, gate_second=gate_second)
|
|
for context, byte_269, (child, byte_184, byte_189, byte_18b) in itertools.product(
|
|
(0, 1), (0, 1), CHILDREN):
|
|
run("load_slcg", ident=ident, owner=1, context=context, byte_269=byte_269, child=child,
|
|
byte_184=byte_184, byte_189=byte_189, byte_18b=byte_18b, feature_13=state,
|
|
feature_7=state ^ 1, gate_first=gate_first, gate_second=gate_second)
|
|
for ident, state, (owner, child, byte_184), byte_189 in itertools.product(
|
|
IDENTS, STATES, OWNERS, (0, 1)):
|
|
run("load_slcg", ident=ident, owner=owner, child=child, byte_184=byte_184,
|
|
byte_189=byte_189, feature_13=state, feature_7=state ^ 1)
|
|
# _nv033738rm, _nv033764rm and _nv033505rm.
|
|
for flag, gr_160c, c78, state, gpu_6d41, (owner, child, byte_183) in itertools.product(
|
|
(0, 1), (0, 1), (0, 1, 5), (0, 1), (0, 1), OWNERS[1:]):
|
|
run("gr_blcg", flag=flag, gr_160c=gr_160c, c78=c78, feature_7=state, feature_13=state ^ 1,
|
|
gpu_6d41=gpu_6d41, owner=owner, child=child, byte_183=byte_183)
|
|
for gr_160c, c78, state, (owner, child, byte_184), byte_18b in itertools.product(
|
|
(0, 1), (0, 1, 5), (0, 1), OWNERS, (0, 1)):
|
|
run("gr_slcg", gr_160c=gr_160c, c78=c78, feature_13=state, feature_7=state ^ 1, owner=owner,
|
|
child=child, byte_184=byte_184, byte_18b=byte_18b, context=1, byte_269=1, gate_first=1,
|
|
gate_second=1)
|
|
for enable, gr_160c, gr_1596, c78, feature_7, feature_13, (owner, child, flags) in (
|
|
itertools.product((0x1fecdff1, 0x1feccff1, 0x1000, 0), (0, 1),
|
|
(0, 1), (0, 1, 5), (0, 1), (0, 1), OWNERS[1:])):
|
|
run("gr_clock_gating", gr_160c=gr_160c, gr_1596=gr_1596, c78=c78, feature_7=feature_7,
|
|
feature_13=feature_13, owner=owner, child=child, byte_183=flags, byte_184=flags,
|
|
reads=(enable,))
|
|
|
|
out = ROOT / "target/gpu-stage"
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
# Table rows: kind, identifier, then the words of every entry.
|
|
table_rows = [f"T;{kind};{ident:x};" + " ".join(f"{word:x}" for row in entries for word in row)
|
|
for kind, tables in (("B", BLCG), ("S", SLCG)) for ident, entries, _at in tables]
|
|
table_rows += [f"T;E;{ident:x};{descriptor:x}" for ident, descriptor in ENGINES]
|
|
oracle = "\n".join(table_rows + rows) + "\n"
|
|
(out / "original-clock-gating.txt").write_text(oracle)
|
|
report = {
|
|
"original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2, "executed": sorted(executed),
|
|
"fixture_supplied": list(HOOKED) + [
|
|
"function at +40 of the vtable of the +17a8 object of the Graphics object "
|
|
"_nv000333rm returns (register, value)",
|
|
"functions at +118 and +178 of the +11d8 object of the GPU+22c8 object",
|
|
"Graphics function at +c78 (GV100 _nv032989rm)",
|
|
"Graphics function at +1038 (GV100 _nv031785rm)"],
|
|
"bindings": bound,
|
|
"tables": {"blcg_descriptors": f".data+{BLCG_AT:#x} x {BLCG_COUNT}",
|
|
"slcg_descriptors": f".data+{SLCG_AT:#x} x {SLCG_COUNT}",
|
|
"engine_descriptors": f".rodata+{ENGINES_AT:#x} x {ENGINES_COUNT}",
|
|
"entries": sum(len(rows_) for _i, rows_, _a in BLCG + SLCG),
|
|
"bytes_sha256": TABLE_DIGEST,
|
|
"rust_constants": str(TABLES.relative_to(ROOT)),
|
|
"rust_constants_sha256": hashlib.sha256(source.encode()).hexdigest()},
|
|
"cases": len(rows), "cases_by_entry": counts, "table_rows": len(table_rows),
|
|
"oracle": "target/gpu-stage/original-clock-gating.txt",
|
|
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
|
|
"physical_sequence_reproduced": PHYSICAL,
|
|
"scope": "Original GV100 clock-gating table loaders with the binder-selected function "
|
|
"tables and the object's own table data. Register values, the feature state, "
|
|
"the Graphics register object, the GPU+22c8 object's functions and the two "
|
|
"Graphics functions are fixture-supplied; no hardware executed."}
|
|
(ROOT / "reports/gpu-clock-gating-original.json").write_text(json.dumps(report, indent=1) + "\n")
|
|
print(f"{len(rows)} original clock-gating cases {counts}; {len(table_rows)} table rows; "
|
|
f"executed {len(executed)} functions")
|