#!/usr/bin/env python3 """Run the original GV100 bodies behind the cross-object inputs of OBJACR Load and Unload. Executed original code (function pointers stored as the GV100 binders and creators store them; reports/gv100-engine-hal-bindings.json and section 9 of analysis/closed-driver-580/acr-load-implementation-notes.md): _nv031156rm the GPU check OBJACR+150 calls first, with GPU+2c8 = _nv031157rm and the attach test _nv030927rm. _nv039060rm the Jtag (GPU+1e70) function at +100. _nv060173rm the OBJVBIOS (GPU+20c8) function at +238. _nv051199rm the function at +178 of the Lpwr child (Lpwr+11b8). _nv041589rm with KernelMemorySystem+160 = _nv041625rm and +170 = _nv041610rm: the flush buffer the halt request programs. _nv031481rm the interrupt service of the PMU idle wait, with _nv030927rm and the vector test _nv060224rm. Fixture-supplied: register read _nv053907rm and write _nv053912rm, the diagnostics, the PCI lookup _nv030949rm, the memory descriptor services _nv043694rm, _nv043691rm, _nv043721rm, _nv043704rm and _nv043699rm, the GPU lookup _nv031398rm, the stall service _nv037739rm and the function at +170 of the Lpwr child. No hardware is involved. """ import bisect import hashlib import itertools import json from pathlib import Path import struct from elftools.elf.elffile import ELFFile from unicorn import UC_ARCH_X86, UC_HOOK_CODE, UC_MODE_64, Uc from unicorn.x86_const import (UC_X86_REG_R8, UC_X86_REG_R9, UC_X86_REG_RAX, UC_X86_REG_RBP, UC_X86_REG_RCX, UC_X86_REG_RDI, UC_X86_REG_RDX, UC_X86_REG_RIP, UC_X86_REG_RSI, UC_X86_REG_RSP) ROOT = Path(__file__).resolve().parents[1] ORIGINAL = Path("/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary") DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" TEXT, DATA, STACK, STOP = 0x10000000, 0x30000000, 0x40000000, 0x50000000 GPU, KMS, INTR, CHILD, VBIOS, JTAG, OUT, HANDLE, OTHER = (DATA + 0x10000 * index for index in range(9)) ROOTS = ("_nv031156rm", "_nv031157rm", "_nv030927rm", "_nv039060rm", "_nv060173rm", "_nv051199rm", "_nv041589rm", "_nv041625rm", "_nv041610rm", "_nv031481rm", "_nv060224rm") HOOKED = ("_nv053907rm", "_nv053912rm", "_nv047869rm", "_nv046745rm", "_nv049554rm", "_nv030949rm", "_nv043694rm", "_nv043691rm", "_nv043721rm", "_nv043704rm", "_nv043699rm", "_nv031398rm", "_nv037739rm", "_nv043744rm", "_nv049620rm") FILL = 0x77777777 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") starts = sorted((symbol["st_value"], symbol["st_size"], symbol.name) for symbol in symbols if symbol["st_shndx"] == text_section and symbol["st_size"]) relocations = sorted((relocation["r_offset"], relocation["r_info_type"], relocation["r_info_sym"], relocation["r_addend"]) for relocation in elf.get_section_by_name(".rela.text").iter_relocations()) offsets = [entry[0] for entry in relocations] entries = {symbol[0]: symbol[2] for symbol in starts} executed, pending = [], list(ROOTS) while pending: name = pending.pop() if name in executed or name in HOOKED: continue executed.append(name) start, size = by_name[name]["st_value"], by_name[name]["st_size"] for offset, kind, index, addend in relocations[bisect.bisect_left(offsets, start): bisect.bisect_left(offsets, start + size)]: target = symbols[index] if target.name == "__x86_indirect_thunk_rax": address = STOP + 0x100 else: assert target["st_shndx"] == text_section, (name, target.name) address = TEXT + target["st_value"] # Follow direct calls and tail calls only, not stored pointers. if kind in (2, 4) and text[offset - 1] in (0xe8, 0xe9): callee = entries.get(target["st_value"] + addend + 4) if callee and callee not in HOOKED: pending.append(callee) value = address + addend if kind in (2, 4): value -= TEXT + offset else: assert kind == 11, kind struct.pack_into("