mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
1154 lines
51 KiB
Python
1154 lines
51 KiB
Python
#!/usr/bin/env python3
|
|
"""Run the original producers of the OBJLSFM Falcon mask and write an oracle.
|
|
|
|
The dword at OBJLSFM+198 has one bit per light-secure Falcon id. A set bit
|
|
means the manager does not handle that Falcon: _nv043190rm(gpu, lsfm, id)
|
|
returns it, the NVDEC HS run (core gpu::nvdec_hs_run) asks it for the NVDEC
|
|
Falcon, and a Falcon whose bit is clear after InitLocked is a client of the
|
|
WPR image. This harness executes the original code that produces the dword on
|
|
a GV100 (chip HAL 0x23, RM HAL 2) with the object's real relocations for
|
|
.text, .rodata and .data, function tables filled by the original binders and
|
|
creators, and writes the cases core gpu::lsfm_mask is compared with.
|
|
|
|
Executed original code:
|
|
|
|
OBJLSFM ConstructEngine _nv043185rm with the table entries +108, +128,
|
|
+130, +138; PreInitLocked _nv043239rm; InitLocked _nv043236rm with
|
|
_nv043182rm, _nv043197rm, the entries +100, +118, +188; the
|
|
query _nv043190rm.
|
|
OBJACR ConstructEngine _nv016731rm (the stores to +37b, +37c, +381 and
|
|
+391 are compared); PreInitLocked _nv016961rm with the entries
|
|
+100 _nv016960rm, +108, +118; _nv016949rm, +148, +1a8, +1b0.
|
|
OBJFUSE binder _nv005974rm; _nv030951rm, +118 _nv029006rm, +100
|
|
_nv029045rm, +168 _nv029047rm, _nv028943rm.
|
|
Jtag object creation _nv006537rm (function +100 and byte +108 for the
|
|
chip), +100 _nv039060rm.
|
|
OBJVBIOS binder _nv006018rm and property initializer _nv005846rm; the
|
|
function they store at +270.
|
|
PMU class creator _nv005744rm, property initializer _nv005852rm, the start
|
|
of the class constructor body _nv051986rm up to the function at
|
|
+330 (_nv052503rm), +308 _nv052063rm, _nv052305rm, _nv052182rm,
|
|
+3d8 _nv052308rm.
|
|
Falcons as tools/check_gpu_wpr_blob_binary.py builds them: NVDEC, SEC2,
|
|
PMU, FECS and GPCCS by their creators, binders and configuration
|
|
functions, attached with _nv021629rm, configured with _nv028412rm
|
|
(with _nv043743rm executed), images prepared by _nv028614rm from
|
|
the packed records in .rodata.
|
|
GPU chip identification _nv030179rm and _nv030823rm from the traced
|
|
BOOT0 and BOOT42 words; the chip predicates _nv030932rm and
|
|
_nv030933rm with the table _nv031281rm.
|
|
|
|
Fixture-supplied: register reads (_nv053907rm), diagnostics, the allocator,
|
|
memory fill and copy performed as such, the registry lookup _nv049786rm, the
|
|
class cast of the Jtag creation, the base constructor _nv003389rm, the
|
|
ancestor lookup of the PMU constructor, _nv015308rm (where the PMU constructor
|
|
is stopped), the OBJACR function at +130 (the HS ACR preparation, recorded),
|
|
the HS ACR image preparation _nv016905rm of the OBJACR constructor (recorded),
|
|
the 32-bit read of the OBJFUSE register aperture (the register at its base
|
|
plus the offset), the object _nv006784rm creates at OBJFUSE+2f8, the device
|
|
aperture at GPU+1d30,
|
|
the memory-descriptor and multi-GPU services, the netlist views of the FECS
|
|
and GPCCS objects, and the order in which the Falcons were registered. No
|
|
hardware is involved.
|
|
"""
|
|
import bisect
|
|
from _binary_path import resolve_580_binary
|
|
import hashlib
|
|
import itertools
|
|
import json
|
|
from pathlib import Path
|
|
import struct
|
|
import warnings
|
|
|
|
warnings.simplefilter("ignore")
|
|
from elftools.elf.elffile import ELFFile # noqa: E402
|
|
from unicorn import (UC_ARCH_X86, UC_HOOK_BLOCK, UC_HOOK_CODE, UC_HOOK_MEM_WRITE, # noqa: E402
|
|
UC_MODE_64, Uc, UcError)
|
|
from unicorn.x86_const import (UC_X86_REG_R8, UC_X86_REG_R9, UC_X86_REG_RAX, # noqa: E402
|
|
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"
|
|
TRANSCRIPT = ROOT / "target/trace/b3-bar0.txt"
|
|
RODATA_ADDRESS = 0x111d660 # address of .rodata in the analysis database
|
|
TEXT, RODATA, DATAS, BSS, DATA, STACK, STOP, HEAP = (
|
|
0x10000000, 0x20000000, 0x28000000, 0x2c000000, 0x30000000, 0x40000000, 0x50000000,
|
|
0x60000000)
|
|
INDIRECT, EXTERNAL, SERVICE = STOP + 0x100, STOP + 0x200, STOP + 0xc000
|
|
DATA_BYTES, HEAP_BYTES = 0x400000, 0x1000000
|
|
(GPU, CHIP, RM, NVDEC, PMUX, SEC2, GRAPHICS, FECS, GPCCS, ACR, LSFM, F_NVDEC, F_PMU, F_SEC2,
|
|
F_FECS, F_GPCCS, VBIOS, FUSE, SCRATCH, MEMORY, JTAG) = (
|
|
DATA + 0x20000 * index for index in range(21))
|
|
PMU = PMUX + 8
|
|
(KERNEL_GRAPHICS, SYSTEM, MANAGER, FACTORY, MEMMGR, REGION, PMU_CHILD, TABLES, PARENT,
|
|
MAPPING) = (MEMORY + 0x1000 * index for index in range(10))
|
|
ARGUMENTS = (UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX, UC_X86_REG_RCX, UC_X86_REG_R8,
|
|
UC_X86_REG_R9)
|
|
# Class vtables and self pointers the creators _nv006485rm and _nv006495rm store.
|
|
GRAPHICS_FALCONS = (("FECS", FECS, (0x4db66c0, 0x4db66d8, 0x4db6838), "_nv005875rm",
|
|
"_nv005777rm"),
|
|
("GPCCS", GPCCS, (0x4db69e0, 0x4db69f8, 0x4db6b58), "_nv005879rm",
|
|
"_nv005781rm"))
|
|
DIAGNOSTICS = ("_nv047869rm", "_nv047868rm", "_nv046745rm", "_nv046747rm", "_nv046751rm")
|
|
# Register values of the traced V100 (target/trace/b3-bar0.txt).
|
|
BOOT0, BOOT42 = 0x140000a1, 0x140a1000
|
|
PHYSICAL = {0x10ac08: 0, 0x21644: 1, 0x10a108: 0x400e0100, 0x87108: 0x20420100,
|
|
0x1373a0: 0xa, 0x21434: 1}
|
|
JTAG_REGISTER, FUSE_REGISTER, DEBUG_REGISTER = 0x1373a0, 0x21434, 0x10ac08
|
|
LSFM_KEYS = ("RMLSFMDisableMask", "RmLSFMFeature")
|
|
ACR_KEYS = ("RmAcrSigningAlgo", "RmUseAutoAcr", "RMDisableAcr", "RMEnableAcr", "RmSec2EnableApm",
|
|
"RMAcrExecuteOnSec2", "RmEnableRiscvLs", "RMAcrUseCeForShadowCopy",
|
|
"RmAllowPrivSecDisabled", "RmBootFromHs", "RmPmuBootstrapMode", "RMEnableAcrRiscv",
|
|
"RmSwapAcrRegions")
|
|
FEATURES = (0x2b, 0x3c, 0x3f, 0x4a, 0x4b)
|
|
|
|
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}
|
|
sections = {}
|
|
for name, base in ((".text", TEXT), (".rodata", RODATA), (".data", DATAS)):
|
|
sections[elf.get_section_index(name)] = (base, bytearray(
|
|
elf.get_section_by_name(name).data()), name)
|
|
bss_index, bss_size = elf.get_section_index(".bss"), elf.get_section_by_name(".bss")["sh_size"]
|
|
text_index = elf.get_section_index(".text")
|
|
starts = sorted((symbol["st_value"], symbol["st_size"], symbol.name) for symbol in symbols
|
|
if symbol["st_shndx"] == text_index and symbol["st_size"])
|
|
relocations = {name: [(entry["r_offset"], entry["r_info_type"], entry["r_info_sym"],
|
|
entry["r_addend"])
|
|
for entry in elf.get_section_by_name(".rela" + name).iter_relocations()]
|
|
for name in (".text", ".rodata", ".data")}
|
|
entries = {start: name for start, size, name in starts}
|
|
start_offsets = [start for start, size, name in starts]
|
|
external = {}
|
|
|
|
|
|
def address_of(symbol):
|
|
index = symbol["st_shndx"]
|
|
if index in sections:
|
|
return sections[index][0] + symbol["st_value"]
|
|
if index == bss_index:
|
|
return BSS + symbol["st_value"]
|
|
if symbol.name == "__x86_indirect_thunk_rax":
|
|
return INDIRECT
|
|
return external.setdefault(symbol.name, EXTERNAL + 8 * len(external))
|
|
|
|
|
|
for base, data, name in sections.values():
|
|
for offset, kind, index, addend in relocations[name]:
|
|
value = address_of(symbols[index]) + addend
|
|
assert len(external) < (SERVICE - EXTERNAL) // 8
|
|
if kind in (2, 4):
|
|
struct.pack_into("<I", data, offset, (value - (base + offset)) & 0xffffffff)
|
|
elif kind in (10, 11):
|
|
struct.pack_into("<I", data, offset, value & 0xffffffff)
|
|
else:
|
|
assert kind == 1, kind
|
|
struct.pack_into("<Q", data, offset, value & 0xffffffffffffffff)
|
|
|
|
cpu = Uc(UC_ARCH_X86, UC_MODE_64)
|
|
for base, data, name in sections.values():
|
|
cpu.mem_map(base, (len(data) + 4095) & ~4095)
|
|
cpu.mem_write(base, bytes(data))
|
|
cpu.mem_map(BSS, (bss_size + 4095) & ~4095)
|
|
for base, size in ((DATA, DATA_BYTES), (STACK, 0x100000), (STOP, 0x10000), (HEAP, HEAP_BYTES)):
|
|
cpu.mem_map(base, size)
|
|
case, hooks, executed, heap, diagnostics = {}, {}, set(), [HEAP], [0]
|
|
reads, calls = [], []
|
|
|
|
|
|
def q(address, value):
|
|
cpu.mem_write(address, struct.pack("<Q", value))
|
|
|
|
|
|
def d(address, value):
|
|
cpu.mem_write(address, struct.pack("<I", value & 0xffffffff))
|
|
|
|
|
|
def b(address, value):
|
|
cpu.mem_write(address, bytes([value]))
|
|
|
|
|
|
def byte(address):
|
|
return cpu.mem_read(address, 1)[0]
|
|
|
|
|
|
def word(address):
|
|
return struct.unpack("<I", cpu.mem_read(address, 4))[0]
|
|
|
|
|
|
def half(address):
|
|
return struct.unpack("<H", cpu.mem_read(address, 2))[0]
|
|
|
|
|
|
def pointer(address):
|
|
return struct.unpack("<Q", cpu.mem_read(address, 8))[0]
|
|
|
|
|
|
def text(name):
|
|
return TEXT + by_name[name]["st_value"]
|
|
|
|
|
|
def function_at(address):
|
|
index = bisect.bisect_right(start_offsets, address - TEXT) - 1
|
|
return starts[index][2] if index >= 0 else hex(address)
|
|
|
|
|
|
def rodata(address):
|
|
"""Emulator address of an object at an analysis-database address."""
|
|
return RODATA + address - RODATA_ADDRESS
|
|
|
|
|
|
def analysis(address):
|
|
return address - RODATA + RODATA_ADDRESS
|
|
|
|
|
|
def string(address):
|
|
data = bytes(cpu.mem_read(address, 64))
|
|
return data[:data.index(0)].decode()
|
|
|
|
|
|
def leave(emu, value):
|
|
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 & 0xffffffffffffffff)
|
|
emu.reg_write(UC_X86_REG_RIP, pointer(rsp))
|
|
|
|
|
|
def hook(*names):
|
|
def register(function):
|
|
for name in names:
|
|
hooks[text(name) if name.startswith("_nv") else name] = (name, function)
|
|
return function
|
|
return register
|
|
|
|
|
|
def service(name):
|
|
def register(function):
|
|
address = SERVICE + 16 * sum(1 for key in hooks if isinstance(key, int)
|
|
and key >= SERVICE)
|
|
hooks[address] = (name, function)
|
|
return address
|
|
return register
|
|
|
|
|
|
@hook("_nv053907rm")
|
|
def register_read(a, low):
|
|
assert (a[0], low[1], low[2]) == (GPU + 0x4120, 0, 0), a
|
|
value = case["registers"][low[3]]
|
|
reads.append(low[3])
|
|
return value
|
|
|
|
|
|
@hook("_nv053912rm")
|
|
def register_write(a, low):
|
|
raise AssertionError(("no register is written", hex(low[3]), hex(low[4])))
|
|
|
|
|
|
@hook(*DIAGNOSTICS)
|
|
def diagnostic(a, low):
|
|
diagnostics[0] += 1
|
|
return 0
|
|
|
|
|
|
@hook("_nv049765rm")
|
|
def state_assertion(a, low):
|
|
return 0
|
|
|
|
|
|
@hook("_nv052687rm")
|
|
def allocate(a, low):
|
|
address = heap[0]
|
|
heap[0] += (low[0] + 0x3f) & ~0x1f
|
|
assert heap[0] < HEAP + HEAP_BYTES
|
|
# Allocations are not zeroed by the service; a pattern exposes a missing fill.
|
|
cpu.mem_write(address, b"\xa5" * low[0])
|
|
return address
|
|
|
|
|
|
@hook("_nv052701rm", "_nv059714rm")
|
|
def release(a, low):
|
|
return 0
|
|
|
|
|
|
@hook("_nv052705rm")
|
|
def fill(a, low):
|
|
cpu.mem_write(a[0], bytes([low[1] & 0xff]) * low[2])
|
|
return a[0]
|
|
|
|
|
|
@hook("_nv049786rm")
|
|
def registry(a, low):
|
|
"""(gpu, key, &value): the status; the value only on success."""
|
|
assert a[0] == GPU
|
|
key = string(a[1])
|
|
calls.append("k:" + key)
|
|
if key in case["registry"]:
|
|
d(a[2], case["registry"][key])
|
|
return 0
|
|
return 0x56
|
|
|
|
|
|
@hook("_nv043717rm")
|
|
def descriptor_for_gpu(a, low):
|
|
assert a[1] == GPU
|
|
return a[0]
|
|
|
|
|
|
@hook("_nv031390rm")
|
|
def multi_gpu(a, low):
|
|
assert a[0] == GPU
|
|
return 0
|
|
|
|
|
|
@hook("_nv031478rm")
|
|
def gpu_section(a, low):
|
|
assert (a[0], low[1], low[2]) == (GPU, 0, 0), a
|
|
return 0
|
|
|
|
|
|
@hook("_nv000333rm")
|
|
def kernel_graphics(a, low):
|
|
assert a[0] == GPU
|
|
return KERNEL_GRAPHICS
|
|
|
|
|
|
@hook("_nv017883rm", "_nv017897rm", "_nv030258rm")
|
|
def bookkeeping(a, low):
|
|
return 0
|
|
|
|
|
|
@hook("_nv031521rm")
|
|
def sec2_construct_service(a, low):
|
|
return 0
|
|
|
|
|
|
@hook("_nv003389rm")
|
|
def base_constructor(a, low):
|
|
return 0
|
|
|
|
|
|
@hook("_nv006784rm")
|
|
def fuse_second_object(a, low):
|
|
"""The object _nv028936rm creates at OBJFUSE+2f8 over the aperture."""
|
|
assert (a[0], a[1], low[2], low[4]) == (FUSE + 0x2f8, FUSE, 0, 1), a
|
|
return 0
|
|
|
|
|
|
@hook("_nv015308rm")
|
|
def pmu_constructor_stop(a, low):
|
|
"""The PMU constructor body is stopped here, after its function at +330."""
|
|
calls.append("stop")
|
|
return 0x40
|
|
|
|
|
|
@hook("_nv016905rm")
|
|
def hs_image_preparation(a, low):
|
|
assert (a[0], a[1]) == (GPU, ACR), a
|
|
calls.append(f"hs:{low[2]:x}")
|
|
return 0
|
|
|
|
|
|
def copy(a, low):
|
|
cpu.mem_write(a[0], bytes(cpu.mem_read(a[1], low[2])))
|
|
return a[0]
|
|
|
|
|
|
hooks[address_of(by_name["os_mem_copy"])] = ("os_mem_copy", copy)
|
|
|
|
|
|
@service("PMU+b08 object, vtable +40")
|
|
def pmu_child(a, low):
|
|
assert a[0] == PMU_CHILD
|
|
return 0
|
|
|
|
|
|
@service("OBJACR+130")
|
|
def acr_130(a, low):
|
|
assert (a[0], a[1], low[2]) == (GPU, ACR, 0), a
|
|
calls.append("acr130")
|
|
return case.get("acr130", 0)
|
|
|
|
|
|
def hooked(emu, address, size, unused):
|
|
a = [emu.reg_read(register) for register in ARGUMENTS]
|
|
leave(emu, hooks[address][1](a, [value & 0xffffffff for value in a]))
|
|
|
|
|
|
def unresolved(emu, address, size, unused):
|
|
assert address in hooks, [name for name, value in external.items() if value == address]
|
|
|
|
|
|
def block(emu, address, size, unused):
|
|
if address - TEXT in entries:
|
|
executed.add(entries[address - TEXT])
|
|
|
|
|
|
def install_hooks():
|
|
"""One code hook per fixture address; imports without a fixture trap."""
|
|
for address in hooks:
|
|
if address not in installed:
|
|
installed[address] = cpu.hook_add(UC_HOOK_CODE, hooked, begin=address, end=address)
|
|
|
|
|
|
def track(enabled):
|
|
"""Function-entry tracking costs a callback per block; it is switched on
|
|
for the setup, the GV100 chain and the first case of each kind."""
|
|
if enabled and not tracking:
|
|
tracking.append(cpu.hook_add(UC_HOOK_BLOCK, block))
|
|
elif not enabled and tracking:
|
|
cpu.hook_del(tracking.pop())
|
|
|
|
|
|
installed, tracking = {}, []
|
|
cpu.hook_add(UC_HOOK_CODE, unresolved, begin=EXTERNAL, end=SERVICE - 1)
|
|
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)
|
|
|
|
|
|
def call(name, *arguments):
|
|
for register, argument in zip(ARGUMENTS, arguments):
|
|
cpu.reg_write(register, argument)
|
|
rsp = STACK + 0xfff00
|
|
cpu.reg_write(UC_X86_REG_RSP, rsp)
|
|
cpu.reg_write(UC_X86_REG_RBP, STACK + 0x80000)
|
|
q(rsp, STOP)
|
|
executed.add(name)
|
|
install_hooks()
|
|
cpu.emu_start(text(name), STOP, count=400000000)
|
|
assert cpu.reg_read(UC_X86_REG_RIP) == STOP, name
|
|
return cpu.reg_read(UC_X86_REG_RAX) & 0xffffffff
|
|
|
|
|
|
# A fixture that answers only some calls of a function and lets the others run.
|
|
conditional = {}
|
|
|
|
|
|
def conditional_hook(name, function):
|
|
def run(emu, address, size, unused):
|
|
a = [emu.reg_read(register) for register in ARGUMENTS]
|
|
result = function(a, [value & 0xffffffff for value in a])
|
|
if result is not None:
|
|
leave(emu, result)
|
|
conditional[name] = cpu.hook_add(UC_HOOK_CODE, run, begin=text(name), end=text(name))
|
|
|
|
|
|
def conditional_remove(name):
|
|
cpu.hook_del(conditional.pop(name))
|
|
|
|
|
|
# ---------------------------------------------------------------- setup
|
|
case.update(registers=dict(PHYSICAL), registry={})
|
|
track(True)
|
|
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)
|
|
# Chip identification as the GPU constructor performs it.
|
|
q(BSS + by_name["_nv029075rm"]["st_value"], SYSTEM)
|
|
q(SYSTEM + 0x258, MANAGER)
|
|
q(MANAGER + 0x48 + 35 * 8, FACTORY)
|
|
assert call("_nv030179rm", GPU, BOOT0, BOOT42, 0) == 0
|
|
call("_nv030823rm", GPU)
|
|
identity = {"GPU+0x9e8": word(GPU + 0x9e8), "GPU+0xd00": word(GPU + 0xd00),
|
|
"GPU+0xd04": word(GPU + 0xd04), "GPU+0xde0": word(GPU + 0xde0),
|
|
"GPU+0xde4": word(GPU + 0xde4)}
|
|
assert identity == {"GPU+0x9e8": 0x23, "GPU+0xd00": 0x140, "GPU+0xd04": 0, "GPU+0xde0": BOOT0,
|
|
"GPU+0xde4": BOOT42}, identity
|
|
call("_nv005980rm", GPU)
|
|
call("_nv005982rm", GPU)
|
|
# Engine objects by their creators, properties by the property initialisers.
|
|
for creator, properties, target in (("_nv005713rm", "_nv005811rm", NVDEC),
|
|
("_nv005735rm", "_nv005840rm", SEC2)):
|
|
call(creator, target, CHIP, RM)
|
|
call(properties, target, CHIP, RM)
|
|
call("_nv005744rm", PMUX, CHIP, RM)
|
|
# Argument order as the callers pass it (reports/gv100-engine-hal-bindings.json,
|
|
# argument_conventions): the PMU and OBJLSFM property initializers take
|
|
# (object, RM owner, chip owner); the other routines here take the chip owner first.
|
|
call("_nv005852rm", PMU, RM, CHIP)
|
|
call("_nv006032rm", PMU + 0xa00, CHIP, RM)
|
|
for binder, properties, target in (("_nv006000rm", "_nv005835rm", LSFM),
|
|
("_nv005947rm", "_nv005809rm", ACR)):
|
|
call(binder, target, CHIP, RM)
|
|
call(properties, target, *((RM, CHIP) if properties == "_nv005835rm" else (CHIP, RM)))
|
|
# OBJVBIOS: the binder with the chip selector, the property initializer with
|
|
# both (object creation _nv006698rm).
|
|
call("_nv006018rm", VBIOS, CHIP, RM)
|
|
call("_nv005846rm", VBIOS, CHIP, RM)
|
|
VBIOS_270 = function_at(pointer(VBIOS + 0x270))
|
|
# OBJFUSE function table.
|
|
call("_nv005974rm", FUSE, CHIP, RM)
|
|
FUSE_SLOTS = {offset: function_at(pointer(FUSE + offset)) for offset in range(0x100, 0x300, 8)
|
|
if TEXT <= pointer(FUSE + offset) < TEXT + len(sections[text_index][1])}
|
|
assert (FUSE_SLOTS[0x100], FUSE_SLOTS[0x118], FUSE_SLOTS[0x168]) == (
|
|
"_nv029045rm", "_nv029006rm", "_nv029047rm"), FUSE_SLOTS
|
|
# OBJFUSE ConstructEngine _nv028937rm calls the function at +270, on GV100
|
|
# _nv028936rm: one register-aperture object over [0x21000, 0x22000) (the
|
|
# bounds are those of +278, _nv028942rm), built by _nv006535rm below the
|
|
# device aperture at GPU+1d30 and stored at OBJFUSE+2f0. Fuse words are then
|
|
# read through that object's function at +28 instead of OBJFUSE+168 and the
|
|
# device read.
|
|
assert (FUSE_SLOTS[0x270], FUSE_SLOTS[0x278]) == ("_nv028936rm", "_nv028942rm"), FUSE_SLOTS
|
|
cpu.mem_write(PARENT, bytes(0x88))
|
|
q(PARENT + 0x60, GPU)
|
|
d(PARENT + 0x68, 0x11)
|
|
d(PARENT + 0x6c, 0x22)
|
|
q(PARENT + 0x70, MAPPING)
|
|
d(PARENT + 0x80, 0x1000000)
|
|
q(GPU + 0x1d30, PARENT)
|
|
assert call("_nv028937rm", GPU, FUSE) == 0
|
|
FUSE_TABLE = pointer(FUSE + 0x2f0)
|
|
FUSE_APERTURE = {"OBJFUSE+0x2ec": word(FUSE + 0x2ec),
|
|
"aperture +0x78 +0x7c +0x80": [f"{word(FUSE_TABLE + offset):#x}"
|
|
for offset in (0x78, 0x7c, 0x80)],
|
|
"aperture +0x60 is the GPU": pointer(FUSE_TABLE + 0x60) == GPU,
|
|
"aperture +0x70 is the device mapping": pointer(FUSE_TABLE + 0x70) == MAPPING}
|
|
assert pointer(FUSE_TABLE + 0x50) == FUSE_TABLE + 0x38
|
|
APERTURE_READ = pointer(pointer(FUSE_TABLE + 0x38) + 0x28)
|
|
FUSE_APERTURE["read function (+0x28 of the object at +0x38)"] = function_at(APERTURE_READ)
|
|
|
|
|
|
def aperture_read(a, low):
|
|
"""The aperture's 32-bit read: the register at its base plus the offset."""
|
|
assert a[0] == FUSE_TABLE + 0x38, a
|
|
offset = word(FUSE_TABLE + 0x7c) + low[1]
|
|
reads.append(offset)
|
|
return case["registers"][offset]
|
|
|
|
|
|
hooks[APERTURE_READ] = ("register-aperture read " + function_at(APERTURE_READ), aperture_read)
|
|
# Jtag: object creation in place (flags 3: no parent link, caller's storage).
|
|
# Its class cast of the parent to the chip selector is the fixture's.
|
|
q(SCRATCH, JTAG)
|
|
conditional_hook("_nv005245rm", lambda a, low: CHIP if a[0] == GPU else None)
|
|
assert call("_nv006537rm", SCRATCH, GPU, 3) == 0
|
|
conditional_remove("_nv005245rm")
|
|
JTAG_STATE = {"Jtag+0x100": function_at(pointer(JTAG + 0x100)), "Jtag+0x108": byte(JTAG + 0x108)}
|
|
assert JTAG_STATE == {"Jtag+0x100": "_nv039060rm", "Jtag+0x108": 1}, JTAG_STATE
|
|
# The interface constructor _nv003365rm creates the Falcon through
|
|
# _nv028792rm; here that step is the binder and the attach function.
|
|
hooks[text("_nv028792rm")] = ("_nv028792rm", lambda a, low: 0)
|
|
for label, target, vtables, binder, properties in GRAPHICS_FALCONS:
|
|
q(target + 0x68, target)
|
|
q(target + 0x58, target + 8)
|
|
q(target + 0x60, target + 0x28)
|
|
call("_nv005720rm", target + 8, CHIP, RM)
|
|
call("_nv005738rm", target + 0x28, CHIP, RM)
|
|
for offset, table in zip((0, 8, 0x28), vtables):
|
|
q(target + offset, RODATA + table)
|
|
call(binder, target, CHIP, RM)
|
|
call("_nv003365rm", target + 8, CHIP)
|
|
call(properties, target, CHIP, RM)
|
|
for owner in (NVDEC + 0xe8, SEC2 + 0xe8, PMU + 0xe8):
|
|
call("_nv003365rm", owner, CHIP)
|
|
cpu.hook_del(installed.pop(text("_nv028792rm")))
|
|
del hooks[text("_nv028792rm")]
|
|
OWNERS = {"FECS": (F_FECS, FECS + 8), "GPCCS": (F_GPCCS, GPCCS + 8),
|
|
"NVDEC": (F_NVDEC, NVDEC + 0xe8), "PMU": (F_PMU, PMU + 0xe8),
|
|
"SEC2": (F_SEC2, SEC2 + 0xe8)}
|
|
for falcon, owner in OWNERS.values():
|
|
call("_nv005876rm", falcon, CHIP, RM)
|
|
assert call("_nv021629rm", falcon, owner) == 0
|
|
q(owner + 0x18, falcon)
|
|
for offset, target in ((0x2080, NVDEC), (0x20c8, VBIOS), (0x2120, PMU), (0x2308, SEC2),
|
|
(0x2310, LSFM), (0x2318, ACR), (0x1e78, FUSE), (0x1e70, JTAG),
|
|
(0x1e20, GPU), (0x1fb8, GRAPHICS), (0x1ed0, MEMMGR)):
|
|
q(GPU + offset, target)
|
|
d(GPU + 0xcac, 0x20)
|
|
q(GRAPHICS + 0x15e8, FECS)
|
|
q(GRAPHICS + 0x15f0, GPCCS)
|
|
q(PMU_CHILD, PMU_CHILD + 0x100)
|
|
q(PMU_CHILD + 0x140, pmu_child)
|
|
q(PMU + 0xb08, PMU_CHILD)
|
|
# The PMU class constructor _nv003396rm calls _nv051986rm, which clears
|
|
# Falcon+650 (OBJPMU+435 is clear on GV100) and calls the function at +330
|
|
# before it builds further objects. The ancestor lookup (class 0x7ef3cb) and
|
|
# its cast are the fixture's.
|
|
hooks[text("_nv049086rm")] = ("_nv049086rm", lambda a, low: GPU)
|
|
conditional_hook("_nv005245rm", lambda a, low: GPU if a[0] == GPU else None)
|
|
PMU_BEFORE = (byte(PMU + 0x435), byte(PMU + 0x43c), word(PMU + 0xd8c))
|
|
del calls[:]
|
|
assert call("_nv051986rm", PMU, 0) == 0x40 and calls == ["k:RmPmuUcodeProfile",
|
|
"k:RmOverrideUprocEngineArch", "stop"]
|
|
conditional_remove("_nv005245rm")
|
|
cpu.hook_del(installed.pop(text("_nv049086rm")))
|
|
del hooks[text("_nv049086rm")]
|
|
PMU_STATE = {"OBJPMU+0x330": function_at(pointer(PMU + 0x330)),
|
|
"OBJPMU+0x435 before": PMU_BEFORE[0], "OBJPMU+0x43c before": PMU_BEFORE[1],
|
|
"OBJPMU+0x43c": byte(PMU + 0x43c), "OBJPMU+0xd8c before": PMU_BEFORE[2],
|
|
"OBJPMU+0xd8c": word(PMU + 0xd8c), "Falcon+0x650": word(F_PMU + 0x650)}
|
|
assert PMU_STATE == {"OBJPMU+0x330": "_nv052503rm", "OBJPMU+0x435 before": 0,
|
|
"OBJPMU+0x43c before": 0, "OBJPMU+0x43c": 1, "OBJPMU+0xd8c before": 0,
|
|
"OBJPMU+0xd8c": 0, "Falcon+0x650": 0}, PMU_STATE
|
|
# Falcon configuration in the registration order of the physical transcript
|
|
# (tools/check_gpu_wpr_blob_binary.py: FECS, GPCCS, NVDEC, PMU, SEC2).
|
|
assert call("_nv027357rm", GPU, FECS, 0x5ee8dc00) == 0
|
|
assert call("_nv029624rm", GPU, GPCCS, 0x4781e800) == 0
|
|
assert call("_nv046468rm", GPU, NVDEC) == 0
|
|
assert call("_nv052063rm", GPU, PMU) == 0
|
|
assert call("_nv055607rm", GPU, SEC2) == 0
|
|
FALCONS = [pointer(GPU + 0x3bc8 + 8 * index) for index in range(word(GPU + 0x3e00))]
|
|
assert FALCONS == [F_FECS, F_GPCCS, F_NVDEC, F_PMU, F_SEC2], FALCONS
|
|
NAMES = {F_FECS: "FECS", F_GPCCS: "GPCCS", F_NVDEC: "NVDEC", F_PMU: "PMU", F_SEC2: "SEC2"}
|
|
ORDER = ("FECS", "GPCCS", "NVDEC", "PMU", "SEC2")
|
|
ENGINES = {name: word(OWNERS[name][0] + 0x5ec) for name in ORDER}
|
|
assert ENGINES == {"FECS": 0x5ee8dc00, "GPCCS": 0x4781e800, "NVDEC": 0x8f99e100,
|
|
"PMU": 0xf3d72200, "SEC2": 0x28c40800}, ENGINES
|
|
# The instance-location list _nv028412rm selects: GPU+5308 bits 23:22 are
|
|
# clear, so _nv043743rm leaves the default table, whose first entry is 2.
|
|
INSTANCE_DEFAULT = pointer(F_NVDEC + 0x5e0)
|
|
assert INSTANCE_DEFAULT == RODATA + by_name["_nv000009rm"]["st_value"]
|
|
assert all(pointer(falcon + 0x5e0) == INSTANCE_DEFAULT for falcon in FALCONS)
|
|
assert struct.unpack("<3I", cpu.mem_read(INSTANCE_DEFAULT, 12)) == (2, 1, 0)
|
|
assert word(GPU + 0x5308) == 0
|
|
# A list whose first entry is 1, for cases.
|
|
cpu.mem_write(TABLES, struct.pack("<3I", 1, 2, 0))
|
|
# The netlist the graphics engine keeps: the original descriptor, decoded by
|
|
# the original unpacker. Its code and data regions are what Graphics stores in
|
|
# the FECS and GPCCS objects (regions 1, 0, 3, 2; netlist slot 3).
|
|
q(SCRATCH, 0)
|
|
assert call("_nv017879rm", rodata(0x1148528), SCRATCH) == 0
|
|
NETLIST = pointer(SCRATCH)
|
|
netlist = bytes(cpu.mem_read(NETLIST, call("_nv017874rm", rodata(0x1148528))))
|
|
REGIONS = {struct.unpack_from("<I", netlist, 8 + 12 * index)[0]:
|
|
struct.unpack_from("<II", netlist, 12 + 12 * index)
|
|
for index in range(struct.unpack_from("<I", netlist, 4)[0])}
|
|
for target, slot, code, sizes, data_field, data_sizes, regions in (
|
|
(FECS, 0x1c8, 0x1d0, 0x1d8, 0x1e0, 0x1e8, (1, 0)),
|
|
(GPCCS, 0x1f8, 0x1d8, 0x1e0, 0x1e8, 0x1f0, (3, 2))):
|
|
d(target + slot, 3)
|
|
for field, size_field, region in ((code, sizes, regions[0]), (data_field, data_sizes,
|
|
regions[1])):
|
|
length = REGIONS[region][0]
|
|
q(target + field, NETLIST + REGIONS[region][1])
|
|
cpu.mem_write(target + size_field, struct.pack("<II", (length + 3) & ~3, length))
|
|
q(ACR + 0x130, acr_130)
|
|
# The object the PMU Falcon's owner casts to with class _nv003202rm, whose
|
|
# byte +18f3 OBJLSFM InitLocked would read: none for the GV100 PMU class.
|
|
PMU_OWNER_CAST = call("_nv005245rm", pointer(F_PMU + 0x5d8), address_of(by_name["_nv003202rm"]))
|
|
assert PMU_OWNER_CAST == 0
|
|
SETUP = sorted(executed)
|
|
executed.clear()
|
|
|
|
|
|
def snapshot():
|
|
return (bytes(cpu.mem_read(DATA, DATA_BYTES)), heap[0],
|
|
bytes(cpu.mem_read(HEAP, heap[0] - HEAP)))
|
|
|
|
|
|
# The state the constructors leave, for the GV100 chain; and the same with
|
|
# the five images prepared (_nv028614rm, a no-op once Falcon+408 is set), so
|
|
# that the many InitLocked cases do not each rebuild the FECS and GPCCS images.
|
|
CONSTRUCTED = snapshot()
|
|
for falcon in FALCONS:
|
|
assert call("_nv028614rm", GPU, falcon) == 0 and byte(falcon + 0x408) == 1
|
|
PREPARE = sorted(executed)
|
|
executed.clear()
|
|
PREPARED = snapshot()
|
|
rows, counts, kinds_executed = [], {}, {}
|
|
|
|
|
|
def restore(state=None):
|
|
state = state or PREPARED
|
|
cpu.mem_write(DATA, state[0])
|
|
cpu.mem_write(HEAP, state[2])
|
|
heap[0] = state[1]
|
|
diagnostics[0] = 0
|
|
del reads[:], calls[:]
|
|
case.clear()
|
|
case.update(registers=dict(PHYSICAL), registry={})
|
|
|
|
|
|
def begin(kind, state=None):
|
|
track(kind not in counts)
|
|
counts[kind] = counts.get(kind, 0) + 1
|
|
restore(state)
|
|
|
|
|
|
def end(kind, row):
|
|
rows.append(row)
|
|
if tracking:
|
|
kinds_executed.setdefault(kind, set()).update(executed)
|
|
executed.clear()
|
|
|
|
|
|
def hexes(values):
|
|
return " ".join(f"{value:x}" for value in values)
|
|
|
|
|
|
def answers(keys):
|
|
"""`key=value` for the keys the case answers, in the order given."""
|
|
return ",".join(f"{key}={case['registry'][key]:x}" for key in keys if key in case["registry"])
|
|
|
|
|
|
# ------------------------------------------------- K: chip predicates
|
|
# _nv031281rm(feature, &architecture, &implementation) and the two tests on
|
|
# it: _nv030933rm (the exact chip) and _nv030932rm (the same architecture
|
|
# byte and a chip selector not below the feature).
|
|
PREDICATES = {0x2b: "_nv054304rm", 0x3c: "_nv054320rm", 0x3f: "_nv054310rm", 0x4a: "_nv054315rm",
|
|
0x4b: "_nv054316rm"}
|
|
feature_table = {}
|
|
for feature in FEATURES:
|
|
begin("K")
|
|
cpu.mem_write(SCRATCH, bytes(16))
|
|
call("_nv031281rm", feature, SCRATCH, SCRATCH + 4)
|
|
feature_table[feature] = (word(SCRATCH), word(SCRATCH + 4))
|
|
end("K", f"K t {feature:x};{word(SCRATCH):x} {word(SCRATCH + 4):x}")
|
|
CHIPS = sorted({(0x140, 0, 0x23)} | {(architecture, implementation, selector)
|
|
for architecture, implementation in feature_table.values()
|
|
for selector in (0x23, 0x2b, 0x4b, 0x70)}
|
|
| {(architecture ^ 0x01000000, implementation, 0x70)
|
|
for architecture, implementation in feature_table.values()}
|
|
| {(architecture, implementation + 1, 0x2a)
|
|
for architecture, implementation in feature_table.values()})
|
|
for (architecture, implementation, selector), feature in itertools.product(CHIPS, FEATURES):
|
|
begin("K")
|
|
d(GPU + 0xd00, architecture)
|
|
d(GPU + 0xd04, implementation)
|
|
d(GPU + 0x9e8, selector)
|
|
result = call(PREDICATES[feature], GPU) & 0xff
|
|
end("K", f"K p {feature:x} {architecture:x} {implementation:x} {selector:x};{result:x}")
|
|
|
|
# ------------------------------------------------- Q: the query
|
|
for mask, identifier in itertools.product(
|
|
(0, 1, 0x10, 0x80, 0x8d, 0x4000000, 0x80000000, 0xffffffef, 0xffffffff),
|
|
(0, 2, 3, 4, 7, 0x1a, 0x1f, 0x20, 0x24, 0xff)):
|
|
begin("Q")
|
|
d(LSFM + 0x198, mask)
|
|
result = call("_nv043190rm", GPU, LSFM, identifier) & 0xff
|
|
end("Q", f"Q {mask:x} {identifier:x};{result:x}")
|
|
|
|
|
|
# ------------------------------------------------- F: the fuse predicate
|
|
def fuse_state():
|
|
return f"{hexes(reads)};{diagnostics[0]:x}"
|
|
|
|
|
|
# `aperture` clear: OBJFUSE+2f0 is null, as before OBJFUSE ConstructEngine, and
|
|
# the word is read through OBJFUSE+168 and the device read.
|
|
for jtag, fuse, g4de4, guard, aperture in itertools.product(
|
|
(0xa, 2, 0, 0xffffffff, 0xfffffff7), (0, 1, 2, 3, 0xfffffffe, 0xffffffff), (0, 1),
|
|
(0, 1), (1, 0)):
|
|
begin("F")
|
|
case["registers"].update({JTAG_REGISTER: jtag, FUSE_REGISTER: fuse})
|
|
b(GPU + 0x4de4, g4de4)
|
|
b(JTAG + 0x108, guard)
|
|
if not aperture:
|
|
q(FUSE + 0x2f0, 0)
|
|
result = call("_nv030951rm", GPU) & 0xff
|
|
end("F", f"F {jtag:x} {fuse:x} {g4de4:x} {guard:x} {aperture:x};{result:x};{fuse_state()}")
|
|
|
|
# ------------------------------------------------- L: instance-location lists
|
|
# _nv043743rm(selector, name, &list, &count) as _nv028412rm calls it with bits
|
|
# 23:22 of GPU+5308 after storing the default list: the first entry of the
|
|
# list Falcon+5e0 then points to.
|
|
for selector in range(4):
|
|
begin("L")
|
|
q(SCRATCH, INSTANCE_DEFAULT)
|
|
d(SCRATCH + 8, 1)
|
|
call("_nv043743rm", selector, RODATA, SCRATCH, SCRATCH + 8)
|
|
end("L", f"L {selector:x};{word(pointer(SCRATCH)):x} {word(SCRATCH + 8):x}")
|
|
|
|
# ------------------------------------------------- U: the PMU Falcon's +650
|
|
# The start of the PMU class constructor body, _nv051986rm, to the point
|
|
# where the fixture stops it: Falcon+650 as it leaves it (zero becomes 1 in
|
|
# _nv028412rm) and whether it reached the function at +330.
|
|
for override, profile in itertools.product(
|
|
(None, 0, 0x100, 0x200, 0x300, 0xf00, 0x1ff, 0xfffff0ff), (None, 0, 4, 1)):
|
|
begin("U")
|
|
for key, value in (("RmOverrideUprocEngineArch", override), ("RmPmuUcodeProfile", profile)):
|
|
if value is not None:
|
|
case["registry"][key] = value
|
|
d(F_PMU + 0x650, 0x77)
|
|
b(PMU + 0x43c, 0)
|
|
hooks[text("_nv049086rm")] = ("_nv049086rm", lambda a, low: GPU)
|
|
conditional_hook("_nv005245rm", lambda a, low: GPU if a[0] == GPU else None)
|
|
status = call("_nv051986rm", PMU, 0)
|
|
conditional_remove("_nv005245rm")
|
|
cpu.hook_del(installed.pop(text("_nv049086rm")))
|
|
del hooks[text("_nv049086rm")]
|
|
end("U", f"U {answers(('RmPmuUcodeProfile', 'RmOverrideUprocEngineArch'))};{status:x};"
|
|
f"{word(F_PMU + 0x650):x} {byte(PMU + 0x43c):x} {int('stop' in calls):x}")
|
|
|
|
# ------------------------------------------------- C: ConstructEngine
|
|
MASKS = (None, 0, 1, 0x10, 0x4000000, 0x4000010, 0xffffffff)
|
|
for mask, feature, chip, initial, mode in itertools.product(
|
|
MASKS, (None, 0, 1, 2, 3), ((0x140, 0), feature_table[0x3f]), (0, 0x5a5a5a5a), (0, 1)):
|
|
begin("C")
|
|
if mask is not None:
|
|
case["registry"]["RMLSFMDisableMask"] = mask
|
|
if feature is not None:
|
|
case["registry"]["RmLSFMFeature"] = feature
|
|
d(GPU + 0xd00, chip[0])
|
|
d(GPU + 0xd04, chip[1])
|
|
d(LSFM + 0x198, initial)
|
|
b(LSFM + 0x190, mode)
|
|
status = call("_nv043185rm", GPU, LSFM)
|
|
assert calls == ["k:" + key for key in LSFM_KEYS], calls
|
|
end("C", f"C {answers(LSFM_KEYS)} {chip[0]:x} {chip[1]:x} {initial:x} {mode:x};{status:x};"
|
|
f"{word(LSFM + 0x198):x} {byte(LSFM + 0x190):x} "
|
|
+ hexes(half(LSFM + offset) for offset in (0x1c0, 0x1c2, 0x1c4, 0x1c6)))
|
|
|
|
# ------------------------------------------------- P: PreInitLocked
|
|
for acr, a37b, g4de4, fuse, initial in itertools.product(
|
|
(1, 0), (0, 1), (0, 1), (0, 1, 2, 3), (0, 0x10, 0xffffffff)):
|
|
begin("P")
|
|
if not acr:
|
|
q(GPU + 0x2318, 0)
|
|
b(ACR + 0x37b, a37b)
|
|
b(GPU + 0x4de4, g4de4)
|
|
case["registers"][FUSE_REGISTER] = fuse
|
|
d(LSFM + 0x198, initial)
|
|
status = call("_nv043239rm", GPU, LSFM)
|
|
end("P", f"P {acr:x} {a37b:x} {g4de4:x} {fuse:x} {initial:x};{status:x};"
|
|
f"{word(LSFM + 0x198):x};{fuse_state()}")
|
|
|
|
|
|
# ------------------------------------------------- A: OBJACR ConstructEngine
|
|
def acr_flags():
|
|
return hexes(byte(ACR + offset) for offset in (0x37b, 0x37c, 0x381, 0x391))
|
|
|
|
|
|
def acr_construct(registry_values=None, chip=(0x140, 0, 0x23), fuse=1, pmu=1, d8c=0, sec2=1,
|
|
g90e=0, g4de2=0, g4de4=0, g4de5=0, g914=0, g92b=0, a391=0):
|
|
begin("A")
|
|
case["registry"].update(registry_values or {})
|
|
d(GPU + 0xd00, chip[0])
|
|
d(GPU + 0xd04, chip[1])
|
|
d(GPU + 0x9e8, chip[2])
|
|
case["registers"][FUSE_REGISTER] = fuse
|
|
if not pmu:
|
|
q(GPU + 0x2120, 0)
|
|
d(PMU + 0xd8c, d8c)
|
|
if not sec2:
|
|
q(GPU + 0x2308, 0)
|
|
for offset, value in ((0x90e, g90e), (0x4de2, g4de2), (0x4de4, g4de4), (0x4de5, g4de5),
|
|
(0x914, g914), (0x92b, g92b)):
|
|
b(GPU + offset, value)
|
|
b(ACR + 0x391, a391)
|
|
status = call("_nv016731rm", GPU, ACR)
|
|
assert [entry for entry in calls if entry.startswith("k:")] == [
|
|
"k:" + key for key in ACR_KEYS], calls
|
|
end("A", f"A {answers(ACR_KEYS)} {hexes(chip)} {fuse:x} {pmu:x} {d8c:x} {sec2:x} {g90e:x} "
|
|
f"{g4de2:x} {g4de4:x} {g4de5:x} {g914:x} {g92b:x} {a391:x};{status:x};{acr_flags()};"
|
|
f"{word(F_SEC2 + 0x650):x}")
|
|
return status
|
|
|
|
|
|
ACR_CHIPS = ((0x140, 0, 0x23),) + tuple(
|
|
(feature_table[feature][0], feature_table[feature][1], feature) for feature in FEATURES)
|
|
acr_construct()
|
|
for key in ACR_KEYS:
|
|
for value in (0, 1, 2, 3, 0x15, 0x3f):
|
|
acr_construct({key: value})
|
|
for chip, g4de4, pmu, d8c, fuse in itertools.product(ACR_CHIPS, (0, 1), (1, 0), (0, 1), (0, 1)):
|
|
if pmu or not d8c:
|
|
acr_construct(chip=chip, g4de4=g4de4, pmu=pmu, d8c=d8c, fuse=fuse)
|
|
for chip, mode, g4de4, a391 in itertools.product(ACR_CHIPS, (0, 1, 2), (0, 1), (0, 1)):
|
|
acr_construct({"RmPmuBootstrapMode": mode}, chip=chip, g4de4=g4de4, a391=a391)
|
|
for disable, enable, riscv, g4de5, a391 in itertools.product(
|
|
(None, 0, 1), (None, 0, 1), (None, 0, 1), (0, 1), (0, 1)):
|
|
values = {key: value for key, value in (("RMDisableAcr", disable), ("RMEnableAcr", enable),
|
|
("RMEnableAcrRiscv", riscv)) if value is not None}
|
|
acr_construct(values, g4de5=g4de5, a391=a391)
|
|
for g914, g92b, g90e, g4de2, sec2, fuse in itertools.product((0, 1), repeat=6):
|
|
acr_construct(g914=g914, g92b=g92b, g90e=g90e, g4de2=g4de2, sec2=sec2, fuse=fuse)
|
|
|
|
|
|
# ------------------------------------------------- B: OBJACR PreInitLocked
|
|
def instance_list(falcon, first):
|
|
q(falcon + 0x5e0, INSTANCE_DEFAULT if first == 2 else TABLES)
|
|
|
|
|
|
for a37b, a391, a388, pmu, d8c, p43c, first, answer in itertools.product(
|
|
(0, 1), (0,), (0, 1), (1, 0), (0, 1), (0, 1), (2, 1), (0, 0x40)):
|
|
if not pmu and (d8c or not p43c or first != 2):
|
|
continue
|
|
begin("B")
|
|
b(ACR + 0x37b, a37b)
|
|
b(ACR + 0x391, a391)
|
|
b(ACR + 0x388, a388)
|
|
if not pmu:
|
|
q(GPU + 0x2120, 0)
|
|
d(PMU + 0xd8c, d8c)
|
|
b(PMU + 0x43c, p43c)
|
|
instance_list(F_PMU, first)
|
|
case["acr130"] = answer
|
|
status = call("_nv016961rm", GPU, ACR)
|
|
end("B", f"B {a37b:x} {a391:x} {a388:x} {pmu:x} {d8c:x} {p43c:x} {first:x} {answer:x};"
|
|
f"{status:x};{byte(ACR + 0x37b):x} {word(ACR + 0x3b8):x} {int('acr130' in calls):x}")
|
|
|
|
|
|
# ------------------------------------------------- I: InitLocked
|
|
def falcon_state(falcon):
|
|
return (f"{byte(falcon + 0x3a5):x}{byte(falcon + 0x3a7):x}{byte(falcon + 0x3a8):x}"
|
|
f"{byte(falcon + 0x3aa):x}.{word(falcon + 0x608):x}")
|
|
|
|
|
|
def nodes():
|
|
result, node = [], pointer(LSFM + 0x1a0)
|
|
while node:
|
|
result.append(NAMES[pointer(node + 0x1510)])
|
|
node = pointer(node)
|
|
return result
|
|
|
|
|
|
def initialize(kind, order=ORDER, mask=0, acr=1, a37b=0, a381=0, a392=0, sec2=1, s513=0,
|
|
g4de4=0, debug=0, changes=None):
|
|
"""`changes`: per Falcon name a dict of skip (Falcon+3a8), isa (+650),
|
|
first (the first instance-location entry), state: real, none (prepared
|
|
without a signature record), fail (the preparation returns 0x40) or zero
|
|
(the 32 signature bytes of the current mode are zero), and f3a7, f3a5."""
|
|
begin(kind)
|
|
changes = changes or {}
|
|
for index in range(5):
|
|
q(GPU + 0x3bc8 + 8 * index, OWNERS[order[index]][0] if index < len(order) else 0)
|
|
d(GPU + 0x3e00, len(order))
|
|
d(LSFM + 0x198, mask)
|
|
if not acr:
|
|
q(GPU + 0x2318, 0)
|
|
b(ACR + 0x37b, a37b)
|
|
b(ACR + 0x381, a381)
|
|
b(ACR + 0x392, a392)
|
|
if not sec2:
|
|
q(GPU + 0x2308, 0)
|
|
b(SEC2 + 0x513, s513)
|
|
b(GPU + 0x4de4, g4de4)
|
|
case["registers"][DEBUG_REGISTER] = debug << 20
|
|
failing, described = set(), {}
|
|
for name in ORDER:
|
|
change = dict(dict(skip=0, isa=1, first=2, state="real", f3a7=0, f3a5=0),
|
|
**changes.get(name, {}))
|
|
falcon = OWNERS[name][0]
|
|
b(falcon + 0x3a8, change["skip"])
|
|
d(falcon + 0x650, change["isa"])
|
|
instance_list(falcon, change["first"])
|
|
b(falcon + 0x3a7, change["f3a7"])
|
|
b(falcon + 0x3a5, change["f3a5"])
|
|
if change["state"] == "none":
|
|
# Prepared (Falcon+408) with no signature record.
|
|
q(falcon + 0x3f0, 0)
|
|
q(falcon + 0x3f8, 0)
|
|
b(falcon + 0x408, 1)
|
|
elif change["state"] == "fail":
|
|
b(falcon + 0x408, 0)
|
|
q(falcon + 0x3f0, 0)
|
|
failing.add(falcon)
|
|
elif change["state"] == "zero":
|
|
assert call("_nv028614rm", GPU, falcon) == 0
|
|
cpu.mem_write(pointer(falcon + 0x3f0) + 8 + 32 * debug, bytes(32))
|
|
described[name] = (f"{name}:{change['skip']:x}:{change['isa']:x}:{change['first']:x}:"
|
|
f"{change['state']}:{change['f3a7']:x}{change['f3a5']:x}:"
|
|
f"{word(falcon + 0x5ec):x}")
|
|
described = [described[name] for name in order]
|
|
if failing:
|
|
conditional_hook("_nv028614rm", lambda a, low: 0x40 if a[1] in failing else None)
|
|
del reads[:], calls[:]
|
|
diagnostics[0] = 0
|
|
try:
|
|
status = call("_nv043236rm", GPU, LSFM)
|
|
finally:
|
|
if failing:
|
|
conditional_remove("_nv028614rm")
|
|
registered = nodes()
|
|
end(kind, f"{kind} {' '.join(described)}|{mask:x} {acr:x} {a37b:x} {a381:x} {a392:x} "
|
|
f"{sec2:x} {s513:x} {g4de4:x} {debug:x};{status:x};{word(LSFM + 0x198):x} "
|
|
f"{half(LSFM + 0x192):x};{','.join(registered)};"
|
|
+ " ".join(f"{name}={falcon_state(OWNERS[name][0])}" for name in order)
|
|
+ f";{sum(1 for offset in reads if offset == DEBUG_REGISTER):x}")
|
|
return status, word(LSFM + 0x198), registered
|
|
|
|
|
|
# The GV100 list and state, with every base setting.
|
|
for mask, acr, a37b, a392, sec2, s513, g4de4, debug in itertools.product(
|
|
(0, 1, 0x10, 0x80, 0x8d, 0xffffffff), (1, 0), (0, 1), (0, 1), (1, 0), (0, 1), (0, 1),
|
|
(0, 1)):
|
|
if not acr and (a37b or a392):
|
|
continue
|
|
initialize("I", mask=mask, acr=acr, a37b=a37b, a392=a392, sec2=sec2, s513=s513,
|
|
g4de4=g4de4, debug=debug)
|
|
# One Falcon changed at a time, with the settings that interact with it.
|
|
VARIANTS = (dict(skip=1), dict(isa=0), dict(isa=2), dict(isa=3), dict(isa=4), dict(first=1),
|
|
dict(state="none"), dict(state="fail"), dict(state="zero"), dict(f3a7=1, f3a5=1),
|
|
dict(first=1, isa=2), dict(state="zero", first=1), dict(state="none", skip=1))
|
|
for name, variant, a392, s513, g4de4, debug, sec2 in itertools.product(
|
|
ORDER, VARIANTS, (0, 1), (0, 1), (0, 1), (0, 1), (1, 0)):
|
|
if not sec2 and (s513 or name == "SEC2"):
|
|
continue
|
|
initialize("I", a392=a392, s513=s513, g4de4=g4de4, debug=debug, sec2=sec2,
|
|
changes={name: variant})
|
|
# OBJACR+381 set: a Falcon with +650 of 2 or 3 is no longer excluded for that
|
|
# alone. Only the cases in which it does not become a client are run; a
|
|
# RISC-V client needs a descriptor none of these Falcons has.
|
|
for name, isa, a392 in itertools.product(ORDER, (1, 2, 3), (0,)):
|
|
initialize("I", a381=1, a392=a392, changes={name: dict(isa=isa, first=2 if isa == 1 else 1)})
|
|
# Two Falcons changed.
|
|
PAIR_VARIANTS = (dict(skip=1), dict(isa=2), dict(first=1), dict(state="none"),
|
|
dict(state="fail"), dict(state="zero"))
|
|
for (first_name, second_name), first, second, g4de4 in itertools.product(
|
|
itertools.combinations(ORDER, 2), PAIR_VARIANTS, PAIR_VARIANTS, (0, 1)):
|
|
initialize("I", g4de4=g4de4, changes={first_name: first, second_name: second})
|
|
# Subsets and orders of the list. The PMU Falcon is reached through GPU+2120
|
|
# whether or not it is in the list.
|
|
for size in range(0, 6):
|
|
for subset in itertools.combinations(ORDER, size):
|
|
for order in ((subset, tuple(reversed(subset))) if size > 1 else (subset,)):
|
|
for mask in (0, 0x10):
|
|
initialize("I", order=order, mask=mask)
|
|
for order in itertools.permutations(ORDER):
|
|
initialize("I", order=order)
|
|
initialize("I", order=order, changes={"PMU": dict(state="zero")})
|
|
initialize("I", order=order, changes={"NVDEC": dict(state="zero")})
|
|
|
|
# ------------------------------------------------- V: the GV100 chain
|
|
# ConstructEngine of OBJLSFM and OBJACR, PreInitLocked of OBJLSFM and OBJACR,
|
|
# InitLocked of OBJLSFM and the query per Falcon, with no registry value and
|
|
# the register words of the traced V100.
|
|
begin("V", CONSTRUCTED)
|
|
track(True)
|
|
before = {name: falcon_state(OWNERS[name][0]) for name in ORDER}
|
|
chain = [call("_nv043185rm", GPU, LSFM)]
|
|
masks = [word(LSFM + 0x198)]
|
|
chain.append(call("_nv016731rm", GPU, ACR))
|
|
flags_constructed = acr_flags()
|
|
construct_reads = list(reads)
|
|
assert construct_reads == [JTAG_REGISTER, FUSE_REGISTER] * 2, construct_reads
|
|
chain.append(call("_nv043239rm", GPU, LSFM))
|
|
masks.append(word(LSFM + 0x198))
|
|
pre_init_reads = reads[len(construct_reads):]
|
|
chain.append(call("_nv016961rm", GPU, ACR))
|
|
flags_pre_init = acr_flags()
|
|
# OBJACR PreInitLocked reads no register before its function at +130.
|
|
assert reads == construct_reads + pre_init_reads and calls[-1] == "acr130", (reads, calls)
|
|
chain.append(call("_nv043236rm", GPU, LSFM))
|
|
masks.append(word(LSFM + 0x198))
|
|
registered = nodes()
|
|
queries = {name: call("_nv043190rm", GPU, LSFM, word(OWNERS[name][0] + 0x608)) & 0xff
|
|
for name in ORDER}
|
|
identifiers = {name: word(OWNERS[name][0] + 0x608) for name in ORDER}
|
|
signatures = {name: f"{analysis(pointer(OWNERS[name][0] + 0x4b0)):#x}" for name in ORDER}
|
|
assert (signatures["NVDEC"], signatures["PMU"], signatures["SEC2"]) == (
|
|
"0x1147610", "0x114b138", "0x1153b68"), signatures
|
|
# The signature record: 192 stored bytes, copied behind an 8-byte header at
|
|
# Falcon+3f0. Falcon+608 is its dword at +72 (instance 0).
|
|
records = {}
|
|
for name in ORDER:
|
|
falcon = OWNERS[name][0]
|
|
assert call("_nv017874rm", pointer(falcon + 0x4b0)) == 192
|
|
records[name] = bytes(cpu.mem_read(pointer(falcon + 0x3f0) + 8, 192))
|
|
assert struct.unpack_from("<I", records[name], 72)[0] == identifiers[name]
|
|
vbios_270 = call(VBIOS_270, GPU, VBIOS) & 0xff
|
|
end("V", "V;" + hexes(chain) + ";" + hexes(masks) + f";{flags_constructed};{flags_pre_init};"
|
|
+ ",".join(registered) + ";"
|
|
+ " ".join(f"{name}={falcon_state(OWNERS[name][0])}" for name in ORDER) + ";"
|
|
+ " ".join(f"{name}={queries[name]:x}" for name in ORDER) + f";{vbios_270:x};"
|
|
+ " ".join(f"{name}={records[name].hex()}" for name in ORDER))
|
|
assert chain == [0, 0, 0, 0, 0] and masks == [0, 0, 0], (chain, masks)
|
|
assert registered == ["NVDEC", "GPCCS", "FECS", "PMU", "SEC2"], registered
|
|
assert identifiers == {"FECS": 2, "GPCCS": 3, "NVDEC": 4, "PMU": 0, "SEC2": 7}, identifiers
|
|
assert not any(queries.values()) and vbios_270 == 0
|
|
assert pre_init_reads == [JTAG_REGISTER, FUSE_REGISTER], pre_init_reads
|
|
assert flags_constructed == flags_pre_init == "0 0 0 0"
|
|
# The physical transcript: OBJLSFM PreInitLocked follows the Graphics tables
|
|
# (lines 37610-37712) and precedes the WPR bounds OBJACR PreInitLocked reads
|
|
# through OBJACR+130 (lines 37715-37722).
|
|
transcript = TRANSCRIPT.read_text().split("\n")
|
|
assert [line.split()[:3] for line in transcript[37712:37715]] == [
|
|
["R", "1373a0", "0xa"], ["R", "021434", "0x1"], ["W", "100cd4", "0x2"]], transcript[37712:37715]
|
|
|
|
track(False)
|
|
out = ROOT / "target/gpu-stage"
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
oracle = "\n".join(rows) + "\n"
|
|
(out / "original-lsfm-mask.txt").write_text(oracle)
|
|
report = {
|
|
"original_sha256": DIGEST, "chip_hal": "0x23", "rm_hal": 2,
|
|
"function_chain": [
|
|
"OBJLSFM ConstructEngine _nv043185rm: _nv049786rm(gpu, \"RMLSFMDisableMask\", "
|
|
"OBJLSFM+198) stores the registry value when present; with it present and the chip "
|
|
"predicate 0x3f (_nv054310rm) true, bit 26 is cleared. Then the four version words "
|
|
"through +108, +128, +130, +138 (each 1 on GV100) and \"RmLSFMFeature\": bit 0 of a "
|
|
"present value replaces OBJLSFM+190; with +190 set and a version word of 1 the status "
|
|
"is 0x56.",
|
|
"OBJLSFM PreInitLocked _nv043239rm: GPU+2318 (OBJACR) absent or OBJACR+37b set stores "
|
|
"0xffffffff; otherwise, with GPU+4de4 clear, _nv030951rm false stores 0xffffffff.",
|
|
"_nv030951rm: OBJFUSE+118 (_nv029006rm) with 0xff000020, which reads fuse word 0x434 "
|
|
"through OBJFUSE+100 (_nv029045rm): with GPU+4de4 set the word is zero without an "
|
|
"access; otherwise the Jtag object's function +100 (_nv039060rm, register 0x1373a0, "
|
|
"only when Jtag+108 is set; its answer selects a diagnostic) and register 0x21434 "
|
|
"(OBJFUSE+168 _nv029047rm). True when bit 0 of the word is set.",
|
|
"OBJLSFM InitLocked _nv043236rm: OBJACR absent or +37b set stores 0xffffffff and "
|
|
"returns. Otherwise _nv043182rm for the first listed Falcon with engine 0x28c40800 "
|
|
"(SEC2), then for the PMU Falcon (GPU+2120 -> +200 -> +18); if that left the PMU "
|
|
"Falcon unmanaged (+3a7 clear) and SEC2 exists with +513 clear and OBJACR+392 clear, "
|
|
"0xffffffff is stored; otherwise _nv043182rm for every other listed Falcon.",
|
|
"_nv043182rm per Falcon with +3a8 clear: the image preparation _nv028614rm; with a "
|
|
"signature record (+3f0 or +3f8) and the Falcon's bit clear: Falcon+650 of 2 or 3 "
|
|
"sets the bit (OBJACR+381 and OBJLSFM+190 are clear on GV100); otherwise OBJACR+392 "
|
|
"set or a first instance-location entry of 2 makes it a client (a record on the list "
|
|
"at +1a0, +192 incremented, Falcon+3a7 = 1); otherwise the bit is set.",
|
|
"_nv043190rm(gpu, lsfm, id): bit (id & 0x1f) of OBJLSFM+198."],
|
|
"setup_functions": SETUP,
|
|
"executed": {kind: sorted(names) for kind, names in sorted(kinds_executed.items())},
|
|
"fixture_supplied": sorted(str(name) for name, function in hooks.values()),
|
|
"chip_identification": {key: f"{value:#x}" for key, value in identity.items()},
|
|
"feature_table": {f"{feature:#x}": [f"{value:#x}" for value in pair]
|
|
for feature, pair in feature_table.items()},
|
|
"vbios": {"binder": "_nv006018rm", "property_initializer": "_nv005846rm",
|
|
"object_creation": "_nv006698rm", "OBJVBIOS+0x270": VBIOS_270,
|
|
"executed_result": vbios_270},
|
|
"fuse": {"binder": "_nv005974rm",
|
|
"slots": {f"{offset:#x}": name for offset, name in sorted(FUSE_SLOTS.items())},
|
|
"OBJFUSE+0x2f0 after the binder": 0, "jtag": JTAG_STATE,
|
|
"jtag_object_creation": "_nv006537rm"},
|
|
"fuse_aperture": FUSE_APERTURE,
|
|
"pmu": PMU_STATE,
|
|
"pmu_owner_cast_to_class__nv003202rm": PMU_OWNER_CAST,
|
|
"prepare_functions": PREPARE,
|
|
"falcon_engines": {name: f"{value:#x}" for name, value in ENGINES.items()},
|
|
"instance_location_default": [2, 1, 0],
|
|
"gv100_chain": {
|
|
"registry": "no value for any key",
|
|
"registers": {f"{offset:#x}": f"{value:#x}" for offset, value in PHYSICAL.items()},
|
|
"statuses": dict(zip(("OBJLSFM ConstructEngine", "OBJACR ConstructEngine",
|
|
"OBJLSFM PreInitLocked", "OBJACR PreInitLocked",
|
|
"OBJLSFM InitLocked"), chain)),
|
|
"OBJLSFM+0x198": dict(zip(("after ConstructEngine", "after PreInitLocked",
|
|
"after InitLocked"), (f"{mask:#x}" for mask in masks))),
|
|
"OBJACR +37b +37c +381 +391": {"after ConstructEngine": flags_constructed,
|
|
"after PreInitLocked": flags_pre_init},
|
|
"OBJACR ConstructEngine reads": [f"{offset:#x}" for offset in construct_reads],
|
|
"OBJLSFM PreInitLocked reads": [f"{offset:#x}" for offset in pre_init_reads],
|
|
"OBJACR PreInitLocked reads before +130": [],
|
|
"falcons_before": before,
|
|
"falcons_after": {name: falcon_state(OWNERS[name][0]) for name in ORDER},
|
|
"falcon_state_format": "+3a5 +3a7 +3a8 +3aa . +608",
|
|
"falcon_ids": identifiers, "signature_records": signatures,
|
|
"signature_record_sha256": {name: hashlib.sha256(record).hexdigest()
|
|
for name, record in records.items()},
|
|
"clients_in_list_order": registered,
|
|
"query_per_falcon": queries},
|
|
"physical_transcript": {
|
|
"file": "target/trace/b3-bar0.txt",
|
|
"lines_37713_37715": transcript[37712:37715],
|
|
"reading": "The Jtag check and fuse word 0x21434 = 1 directly after the Graphics "
|
|
"PreInitLocked tables and directly before the WPR bounds OBJACR "
|
|
"PreInitLocked reads through OBJACR+130: the read of OBJLSFM PreInitLocked, "
|
|
"which it makes only with OBJACR present, OBJACR+37b clear and GPU+4de4 "
|
|
"clear."},
|
|
"cases": counts, "total_cases": sum(counts.values()),
|
|
"oracle": "target/gpu-stage/original-lsfm-mask.txt",
|
|
"oracle_sha256": hashlib.sha256(oracle.encode()).hexdigest(),
|
|
"scope": "Original producers of OBJLSFM+198 on a GV100 and of the OBJACR bytes they read. "
|
|
"Registry answers and register words are fixture-supplied; the Falcons are the "
|
|
"five of tools/check_gpu_wpr_blob_binary.py, not the device's full list. No "
|
|
"hardware executed."}
|
|
(ROOT / "reports/gpu-lsfm-mask-original.json").write_text(json.dumps(report, indent=2) + "\n")
|
|
print(f"{sum(counts.values())} original cases {counts}; GV100 chain masks "
|
|
f"{[hex(mask) for mask in masks]}, clients {registered}, OBJVBIOS+270 {VBIOS_270} "
|
|
f"-> {vbios_270}")
|