#!/usr/bin/env python3 """Run the original GV100 Intr stage bodies and write a comparison oracle. Executed original code: ConstructEngine _nv037522rm, StatePreInitLocked _nv037777rm, StateInitLocked _nv037772rm and StateInitUnlocked _nv037773rm, the binders _nv005896rm, _nv005897rm and _nv005787rm that populate the object, and the bound functions PreInit reaches through the object: the table initialiser _nv037667rm (slot 0x320), its entry count _nv037608rm (slot 0x108) and the vector emptiness test, plus the bit-vector helpers of InitLocked. The registry, containers, allocation, the GPU child and GPU instance iterators, other objects' methods, the host table builder _nv037657rm (slot 0x310) and diagnostics are fixture-supplied. 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_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, RODATA, DATA, STACK, STOP = 0x10000000, 0x20000000, 0x30000000, 0x40000000, 0x50000000 GPU, INTR, GMMU, CHILDREN, VTABLE = DATA, DATA + 0x10000, DATA + 0x13000, DATA + 0x14000, DATA + 0x15000 PEERS, PEER_INTRS, CHIP, RM = DATA + 0x20000, DATA + 0x38000, DATA + 0x3c000, DATA + 0x3d000 INDIRECT, REGISTER, GPU_160, ALLOCATOR, HEAP = STOP + 0x100, STOP + 0x200, STOP + 0x300, STOP + 0x400, 0x70000000 TABLE, TABLE_BYTES, CLASS_ID = INTR + 0x450, 0x10c8, 0x2271cc BODIES = {"C": "_nv037522rm", "P": "_nv037777rm", "I": "_nv037772rm", "U": "_nv037773rm"} # The binders only store pointers, so the functions they select are listed. ROOTS = tuple(BODIES.values()) + ("_nv005896rm", "_nv005897rm", "_nv005787rm", "_nv037667rm", "_nv037608rm") HOOKED = ("_nv052691rm", "_nv060222rm", "_nv042695rm", "_nv042691rm", "_nv052687rm", "_nv052701rm", "_nv030645rm", "_nv037657rm", "_nv049786rm", "_nv031415rm", "_nv031424rm", "_nv047869rm", "_nv046747rm", "_nv046745rm", "_nv052705rm", "_nv052695rm") EXPECTED_SLOTS = {0x108: "_nv037608rm", 0x2f8: "_nv037775rm", 0x310: "_nv037657rm", 0x320: "_nv037667rm"} REGISTRY = ("RMIntrDetailedLogs", "RMDisablePerIntrDPCQueueing", "RMIntrLockingMode", "RM654663") # State fields in oracle order: (object, offset, bytes). FIELDS = ([(INTR, 0x3f0, 1), (INTR, 0x3f5, 1), (INTR, 0x3f6, 1), (INTR, 0x448, 4)] + [(INTR, 0x1518 + 8 * index, 8) for index in range(5)] + [(INTR, 0x1540 + 8 * index, 8) for index in range(5)] + [(INTR, 0x1608 + 8 * index, 8) for index in range(7)] + [(INTR, 0x1640, 1), (INTR, 0x1668, 8), (INTR, 0x1670, 8), (INTR, 0x1678, 4), (INTR, 0x1684, 4)] + [(INTR, 0x1688 + 8 * index, 8) for index in range(3)] + [(INTR, 0x16b8, 4), (INTR, 0x16c8, 4), (INTR, 0x16cc, 1), (GPU, 0xdec, 4), (GMMU, 0x4a0, 4)]) assert hashlib.sha256(ORIGINAL.read_bytes()).hexdigest() == DIGEST with ORIGINAL.open("rb") as stream: elf = ELFFile(stream) symbols = list(elf.get_section_by_name(".symtab").iter_symbols()) by_name = {symbol.name: symbol for symbol in symbols} text = bytearray(elf.get_section_by_name(".text").data()) rodata = elf.get_section_by_name(".rodata").data() text_section, rodata_section = (elf.get_section_index(name) for name in (".text", ".rodata")) 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] names = {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 = INDIRECT elif target["st_shndx"] == rodata_section: address = RODATA + target["st_value"] 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 = names.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("= cursor), None) emu.mem_write(b, struct.pack("> bit & 1), None) emu.mem_write(b, struct.pack("> 16:x}:{low_c:x}") elif name == "_nv046747rm": trace.append(f"a:{low_b:x}:{a & 0xffffffff:x}") elif name == "_nv052705rm": emu.mem_write(a, bytes([b & 0xff]) * low_c) value = a elif name == "_nv052695rm": emu.mem_write(a, bytes(emu.mem_read(c, low_d))) value = a else: raise AssertionError(f"{name} must not be reached") leave(emu, value) return run def thunk(emu, address, size, unused): target = emu.reg_read(UC_X86_REG_RAX) a, b, c = (emu.reg_read(r) for r in (UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX)) if target == REGISTER: assert (a, c) == (GPU, TABLE) slot = (b - CHILDREN) // 0x10 trace.append(f"r:{slot:x}") emu.mem_write(TABLE + slot, bytes([slot + 1])) leave(emu) elif target == GPU_160: assert (a, b) == (GPU, TABLE) trace.append("g160") emu.mem_write(TABLE + TABLE_BYTES - 1, b"\xee") leave(emu) else: assert TEXT <= target < TEXT + len(text), hex(target) emu.reg_write(UC_X86_REG_RIP, target) cpu.hook_add(UC_HOOK_CODE, thunk, begin=INDIRECT, end=INDIRECT) for hooked in HOOKED: entry = TEXT + by_name[hooked]["st_value"] cpu.hook_add(UC_HOOK_CODE, leaf(hooked), 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), 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("> index & 1 else 0)) cpu.mem_write(INTR + 0x1540 + 8 * index, struct.pack( "> index & 1 else 0)) cpu.mem_write(INTR + 0x448, struct.pack("