Files
nvdriver/tools/check_timeout_config_binary.py

85 lines
6.1 KiB
Python

#!/usr/bin/env python3
"""Run whole original049687/059486/059487/059507 privately under Unicorn.
Hypervisor detection and current-thread reset are explicit scripted boundaries;
no native driver, kernel, GPU MMIO or original readiness is executed.
"""
import argparse, hashlib, json, struct, warnings
from pathlib import Path
warnings.filterwarnings('ignore', category=UserWarning, module='unicorn')
from elftools.elf.elffile import ELFFile
from unicorn import Uc, UC_ARCH_X86, UC_MODE_64, UC_HOOK_CODE
from unicorn.x86_const import *
a=argparse.ArgumentParser();a.add_argument('--check',action='store_true');args=a.parse_args()
R=Path(__file__).resolve().parents[1]
P=Path('/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary')
EXPECTED='5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b'
assert hashlib.sha256(P.read_bytes()).hexdigest()==EXPECTED
TEXT=0x10000000;GLOBAL=0x20000000;GPU=0x30000000;STACK=0x40000000;STOP=0x50000000
NAMES=['_nv049687rm','_nv059486rm','_nv059487rm','_nv059507rm']
with P.open('rb') as f:
e=ELFFile(f);symbols=list(e.get_section_by_name('.symtab').iter_symbols());defined={s.name:s for s in symbols};text=bytearray(e.get_section_by_name('.text').data());ti=e.get_section_index('.text')
spans=[(defined[n]['st_value'],defined[n]['st_size']) for n in NAMES]
for rel in e.get_section_by_name('.rela.text').iter_relocations():
off=rel['r_offset']
if not any(s<=off<s+z for s,z in spans):continue
s=symbols[rel['r_info_sym']];assert rel['r_info_type']==2
if s['st_shndx']==ti:target=TEXT+s['st_value']
else:
assert s.name=='_nv059471rm';target=GLOBAL+s['st_value']
struct.pack_into('<i',text,off,target+rel['r_addend']-(TEXT+off))
DB=GLOBAL+defined['_nv059471rm']['st_value']
offsets={n:(defined[n]['st_value'],defined[n]['st_size']) for n in NAMES}
u=Uc(UC_ARCH_X86,UC_MODE_64)
for base,size in [(TEXT,(len(text)+4095)&~4095),(GLOBAL,0x10000),(GPU,0x20000),(STACK,0x10000),(STOP,4096)]:u.mem_map(base,size)
u.mem_write(TEXT,bytes(text))
def w32(p,v):u.mem_write(p,struct.pack('<I',v&0xffffffff))
def w64(p,v):u.mem_write(p,struct.pack('<Q',v&0xffffffffffffffff))
def r32(p):return struct.unpack('<I',u.mem_read(p,4))[0]
def r64(p):return struct.unpack('<Q',u.mem_read(p,8))[0]
def dbput():
w32(DB,0x87);w32(DB+0x4c,0x10203040);w64(DB+0x40,0x100000001);w64(DB+0x38,0x200000002);w32(DB+0x48,0xa5a55a5a)
def dbget():return [r32(DB),r32(DB+0x4c),r64(DB+0x40),r64(DB+0x38),r32(DB+0x48)]
def ret(v):
sp=u.reg_read(UC_X86_REG_RSP);pc=r64(sp);u.reg_write(UC_X86_REG_RAX,v);u.reg_write(UC_X86_REG_RSP,sp+8);u.reg_write(UC_X86_REG_RIP,pc)
variant=0;resets=0;hyper_calls=0;visited=set()
def hook(emu,pc,size,user):
global resets,hyper_calls
if pc==STOP:emu.emu_stop();return
for n,(off,z) in offsets.items():
if pc==TEXT+off:visited.add(n)
if pc==TEXT+defined['_nv036619rm']['st_value']:
hyper_calls+=1;ret((variant>>6)&1)
elif pc==TEXT+defined['_nv059492rm']['st_value']:
assert emu.reg_read(UC_X86_REG_RDI)==GPU;resets+=1;ret(0x65 if variant&1 else 0xffff)
u.hook_add(UC_HOOK_CODE,hook)
def call(name,params):
for reg in [UC_X86_REG_RAX,UC_X86_REG_RBX,UC_X86_REG_RCX,UC_X86_REG_RDX,UC_X86_REG_RSI,UC_X86_REG_RDI,UC_X86_REG_R8,UC_X86_REG_R9,UC_X86_REG_R12,UC_X86_REG_R13,UC_X86_REG_R14,UC_X86_REG_R15]:u.reg_write(reg,0)
for reg,v in zip([UC_X86_REG_RDI,UC_X86_REG_RSI,UC_X86_REG_RDX,UC_X86_REG_RCX],params):u.reg_write(reg,v)
w64(STACK+0xff00,STOP);u.reg_write(UC_X86_REG_RSP,STACK+0xff00);u.reg_write(UC_X86_REG_RBP,STACK+0x8000)
u.emu_start(TEXT+offsets[name][0],STOP,count=1000);assert u.reg_read(UC_X86_REG_RIP)==STOP
rows=[]
for variant in range(512):
u.mem_write(GPU,bytes(0x20000));refs=[0,1,0xffffffff,0x80000000,0x7fffffff][variant%5];scale=[0,1,60,0xffffffff][variant%4];instance=[0,1,31,32,63,0xffffffff][variant%6]
w32(GPU+0x1e30,refs);w32(GPU+0x1e18,scale);w32(GPU+0xc9c,instance)
for bit,off in enumerate([0xe01,0x975,0xcd9,0x90e,0x4de2]):u.mem_write(GPU+off,bytes([(variant>>bit)&1]))
# GPUe00 word is separate from GSP_CLIENT bytee01 above.
u.mem_write(GPU+0xe00,bytes([(variant>>7)&1]));u.mem_write(GPU+0x921,bytes([(variant>>5)&1]))
hyper_calls=0;resets=0
call('_nv049687rm',[GPU,GPU+0x10000,GPU+0x10004,GPU+0x10008]);params=[r32(GPU+0x10000),r32(GPU+0x10004),r32(GPU+0x10008)];assert hyper_calls==1
dbput();call('_nv059486rm',[variant*0x10001]);setup=dbget()
dbput();requested=[0,999,1000,0xffffffff][variant%4];flags=(variant*0x1234567)&0xffffffff
call('_nv059487rm',[GPU,requested,flags]);direct=dbget()
data=GPU+0x1e08;u.mem_write(data,bytes(0x20));u.mem_write(data,bytes([(variant>>8)&1,(variant>>3)&1]));w32(data+4,0xfedcba98);w32(data+8,0x12345678);w32(data+0xc,0x10);w32(data+0x10,99)
dbput();call('_nv059507rm',[data,GPU]);defaults=list(u.mem_read(data,2))+[r32(data+4),r32(data+8),r32(data+0xc),r32(data+0x10),int(r64(data+0x18)==GPU)]
assert resets==1
rows.append([variant]+params+setup+direct+defaults+dbget()+[resets])
assert visited==set(NAMES)
width=len(rows[0]);assert all(len(row)==width for row in rows)
fixture='// Whole-original closed580 instructions; scripted VGX/reset boundaries.\n'
fixture+=f'pub const CASES: &[[u64; {width}]] = &[\n'+''.join(' ['+', '.join(map(str,row))+'],\n' for row in rows)+'];\n'
report={'passed':True,'cases':len(rows),'whole_original_functions':NAMES,'artifact_sha256':EXPECTED,'vectors_sha256':hashlib.sha256(fixture.encode()).hexdigest(),'rea_source_evidence':'reports/timer-timeout-provenance.json','scope':'Unicorn executes original whole functions including nested Linuxparams/configure calls. VGX detector and thread reset are scripted; no native kernel, hardware access, registry override or full GPU runtime readiness proof.','scripted_reset_statuses':[0x65,0xffff],'inputs':'signed compute refs, all VGX/GSP/init/emulation/simulation branches, instance shift wrapping, scaling overflow, overridden/scaled preservation, startup flag preservation.'}
for p,s in [(R/'src/nvidia/src/gpu/timeout/config/vectors.rs',fixture),(R/'reports/timeout-config-binary.json',json.dumps(report,indent=2)+'\n')]:
if args.check:assert p.read_text()==s,str(p)+' differs'
else:p.write_text(s)
print(json.dumps(report,indent=2))