mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
101 lines
5.0 KiB
Python
Executable File
101 lines
5.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Capture exact native timeoutSet ABI setup slices, not whole RM parents.
|
|
No driver load, GPU MMIO or substituted timeout execution. Stop immediately at
|
|
original CALL after emulating only its actual argument-setup instructions.
|
|
"""
|
|
import argparse
|
|
from _binary_path import resolve_580_binary
|
|
import hashlib
|
|
import json
|
|
import warnings
|
|
warnings.filterwarnings("ignore", category=UserWarning, module="unicorn")
|
|
from pathlib import Path
|
|
from capstone import Cs, CS_ARCH_X86, CS_MODE_64
|
|
from elftools.elf.elffile import ELFFile
|
|
from unicorn import Uc, UC_ARCH_X86, UC_MODE_64, UC_HOOK_CODE
|
|
from unicorn.x86_const import (
|
|
UC_X86_REG_RBP, UC_X86_REG_RBX, UC_X86_REG_RCX, UC_X86_REG_RDX,
|
|
UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_R9, UC_X86_REG_R10,
|
|
)
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
ARTIFACT = resolve_580_binary()
|
|
EXPECTED = '5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b'
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--check', action='store_true')
|
|
args = parser.parse_args()
|
|
assert hashlib.sha256(ARTIFACT.read_bytes()).hexdigest() == EXPECTED
|
|
with ARTIFACT.open('rb') as stream:
|
|
elf = ELFFile(stream)
|
|
symbols = list(elf.get_section_by_name('.symtab').iter_symbols())
|
|
defined = {s.name: s for s in symbols}
|
|
text = elf.get_section_by_name('.text').data()
|
|
relocs = {r['r_offset']: (symbols[r['r_info_sym']].name, r['r_info_type'], r['r_addend'])
|
|
for r in elf.get_section_by_name('.rela.text').iter_relocations()}
|
|
rows = []
|
|
GPU = 0x30000000
|
|
FRAME = 0x40008000
|
|
for name, expected in [('_nv040843rm', (0, 0x105)), ('_nv018613rm', (0, 0))]:
|
|
symbol = defined[name]
|
|
address = symbol['st_value']
|
|
instructions = list(Cs(CS_ARCH_X86, CS_MODE_64).disasm(
|
|
text[address:address + symbol['st_size']], address))
|
|
calls = [(i, ins) for i, ins in enumerate(instructions)
|
|
if ins.mnemonic == 'call' and relocs.get(ins.address + 1, ('',))[0] == '_nv059509rm']
|
|
assert len(calls) == 1, (name, calls)
|
|
index, call = calls[0]
|
|
relocation = relocs[call.address + 1]
|
|
assert relocation[1:] == (2, -4), relocation
|
|
# Both original callers zero requested_us/EDX and explicitly set flags/ECX.
|
|
clears = [i for i in range(max(0, index - 8), index)
|
|
if instructions[i].mnemonic == 'xor'
|
|
and instructions[i].op_str in ('edx, edx', 'ecx, ecx')]
|
|
assert clears
|
|
start = instructions[min(clears)].address
|
|
block = text[start:call.address + call.size]
|
|
uc = Uc(UC_ARCH_X86, UC_MODE_64)
|
|
page = start & ~4095
|
|
uc.mem_map(page, 8192)
|
|
uc.mem_write(start, block)
|
|
uc.mem_map(FRAME & ~0xffff, 0x10000)
|
|
uc.reg_write(UC_X86_REG_RBP, FRAME)
|
|
uc.reg_write(UC_X86_REG_RBX, GPU if name == '_nv040843rm' else GPU + 0x1e08)
|
|
uc.reg_write(UC_X86_REG_RCX, 0xdeadbeef)
|
|
uc.reg_write(UC_X86_REG_RDX, 0x12345678)
|
|
uc.reg_write(UC_X86_REG_R9, 0xaabbccdd)
|
|
uc.reg_write(UC_X86_REG_R10, 0xabcdef0123456789)
|
|
captured = []
|
|
def at_call(emulator, pc, size, _):
|
|
if pc == call.address:
|
|
captured.append(tuple(emulator.reg_read(reg) for reg in (
|
|
UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX, UC_X86_REG_RCX)))
|
|
emulator.emu_stop()
|
|
uc.hook_add(UC_HOOK_CODE, at_call)
|
|
uc.emu_start(start, call.address + call.size, count=16)
|
|
assert len(captured) == 1
|
|
data, token, requested, flags = captured[0]
|
|
assert data == GPU + 0x1e08
|
|
assert token == FRAME + (0x28 if name == '_nv040843rm' else 8)
|
|
assert (requested, flags) == expected, (name, captured)
|
|
rows.append({'function': name, 'start_text_offset': start,
|
|
'call_text_offset': call.address, 'call_target': relocation[0],
|
|
'relocation_type': relocation[1], 'relocation_addend': relocation[2],
|
|
'native_bytes': block.hex(),
|
|
'instructions': [f'{ins.address:x}: {ins.mnemonic} {ins.op_str}'
|
|
for ins in instructions if start <= ins.address <= call.address],
|
|
'data_pointer': data, 'token_pointer': token,
|
|
'requested_us': requested, 'flags': flags})
|
|
report = {'passed': True, 'artifact_sha256': EXPECTED, 'callsites': rows,
|
|
'scope': 'Exact original timeoutSet argument-setup slices through the call boundary only. No whole parent admission/control flow, timeout execution, MMIO or physical hardware claim.'}
|
|
fixture = '// Generated by tools/check_bar2_timeout_policy_binary.py; native call ABI slices.\n'
|
|
fixture += f"pub const ROOT_REQUEST: (u32, u32) = ({rows[0]['requested_us']}, {rows[0]['flags']:#x});\n"
|
|
fixture += f"pub const FLUSH_REQUEST: (u32, u32) = ({rows[1]['requested_us']}, {rows[1]['flags']:#x});\n"
|
|
for path, value in [
|
|
(ROOT / 'reports/bar2-timeout-policy-binary.json', json.dumps(report, indent=2) + '\n'),
|
|
(ROOT / 'src/nvidia/src/mmu/rm_tlb_timeout_vectors.rs', fixture),
|
|
]:
|
|
if args.check:
|
|
assert path.exists() and path.read_text() == value, f'drift: {path}'
|
|
else:
|
|
path.write_text(value)
|
|
print('2 original timeout callsites match:040843(0,0x105),018613(0,0)')
|