Files
nvdriver/tools/check_gpu_timer_low_binary.py
nvidia-driver-rs 4526e6a816 Baseline: validated 30/30 tree with passing Level 7 hardware regression
- Updated tools/post_reboot.sh to check kernel oops taint bit and stuck driver tasks instead of <86400s uptime
- Level 7 hardware regression passed on 0000:b3:00.0 (module 37e6fa430835eee2b426019db3ad70eeb7a9e9070c09c84bd9b973cf56cbe529, 107 inventory words, 0 static mismatches, CUDA baseline verified before and after)
- Updated .gitignore to exclude build archives (*.a, *.rlib), __pycache__, and raw trace dumps
2026-11-23 23:48:38 +00:00

92 lines
6.5 KiB
Python

#!/home/geodd/.local/opt/rea-python/bin/python
"""Execute selected original timer instructions under Unicorn with scripted MMIO.
No native target, driver or GPU execution. Regenerates independent Rust vectors.
"""
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 *
parser=argparse.ArgumentParser();parser.add_argument('--check',action='store_true');options=parser.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')
TEXT=0x10000000;DATA=0x30000000;STACK=0x40000000;STUB=0x50000000;STOP=0x60000000
names=['_nv059597rm','_nv059609rm','_nv059606rm','_nv059608rm','_nv059605rm','_nv059598rm']
hooks={};functions={}
with P.open('rb') as stream:
e=ELFFile(stream);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());text_index=e.get_section_index('.text');undefined={}
for name in names:
s=defined[name];functions[name]=(s['st_value'],s['st_size'])
for rel in e.get_section_by_name('.rela.text').iter_relocations():
off=rel['r_offset']
if not any(start<=off<start+size for start,size in functions.values()):continue
symbol=symbols[rel['r_info_sym']];kind=rel['r_info_type']
if symbol['st_shndx']==text_index:target=TEXT+symbol['st_value']
elif symbol['st_shndx']=='SHN_UNDEF':
target=undefined.setdefault(symbol.name,STUB+0x100+len(undefined)*16);hooks[target]=symbol.name
else:target=STUB+0x8000
assert kind in (2,4,11),(off,kind,symbol.name)
value=target+rel['r_addend']-(TEXT+off) if kind in (2,4) else target+rel['r_addend']
struct.pack_into('<i',text,off,value)
for name in ['_nv053907rm','_nv047869rm','_nv047868rm']:hooks[TEXT+defined[name]['st_value']]=name
uc=Uc(UC_ARCH_X86,UC_MODE_64)
for address,size in [(TEXT,(len(text)+4095)&~4095),(DATA,0x30000),(STACK,0x10000),(STUB,0x10000),(STOP,4096)]:uc.mem_map(address,size)
uc.mem_write(TEXT,bytes(text))
hooks[STUB+0x10]='state_init';hooks[STUB+0x20]='state_load'
def put(offset,value):uc.mem_write(DATA+offset,struct.pack('<Q',value))
def ret(value):
sp=uc.reg_read(UC_X86_REG_RSP);target=struct.unpack('<Q',uc.mem_read(sp,8))[0]
uc.reg_write(UC_X86_REG_RAX,value);uc.reg_write(UC_X86_REG_RSP,sp+8);uc.reg_write(UC_X86_REG_RIP,target)
reads=[];consumed=[];events=[]
def on_code(emu,address,size,user):
if address==STOP:emu.emu_stop();return
name=hooks.get(address)
if name is None:return
if name=='__x86_indirect_thunk_rax':emu.reg_write(UC_X86_REG_RIP,emu.reg_read(UC_X86_REG_RAX));return
if name=='_nv053907rm':
offset=emu.reg_read(UC_X86_REG_RCX);assert len(consumed)<len(reads),'unexpected read'
expected,value=reads[len(consumed)];assert offset==expected,(offset,expected)
consumed.append((offset,value));ret(value)
elif name=='_nv047869rm':events.append(('bad_low',emu.reg_read(UC_X86_REG_RDX)));ret(0)
elif name=='_nv047868rm':events.append(('unstable_low',emu.reg_read(UC_X86_REG_RDX)));ret(0)
elif name=='state_init':events.append(('init',));ret(0xf)
elif name=='state_load':
flags=emu.reg_read(UC_X86_REG_RDX);assert flags==0;events.append(('load',flags));ret(0x56)
else:raise AssertionError('unexpected helper '+name)
uc.hook_add(UC_HOOK_CODE,on_code)
rows=[]
for usermode in (False,True):
lo,hi=(0x810080,0x810084) if usermode else (0x9400,0x9410)
cases=[(0,[(lo,0)]),(2,[(lo,0xffffffe0)]),
(2,[(lo,1),(hi,7),(lo,32),(hi,7)]),
(2,[(lo,0xffffffff),(hi,7),(lo,1),(lo,3)]),
(0,[(lo,1),(hi,7)]),
(2,[(lo,1),(hi,0xffffffff),(lo,0xffffffe0),(hi,0),(hi,0),(lo,32),(hi,0)])]
for retry,values in cases:
for initial_error in (0,0xffffffff):
reads=values;consumed=[];events=[]
uc.mem_write(DATA,bytes(0x30000));uc.mem_write(STACK,bytes(0x10000));uc.mem_write(DATA+0x96c,bytes([usermode]))
put(0x10000,DATA+0x20000);put(0x10000+0x1b8,TEXT+defined['_nv059597rm']['st_value']);put(0x10000+0x1c0,TEXT+defined['_nv059609rm']['st_value']);put(0x10000+0x1c8,TEXT+defined['_nv059606rm']['st_value'])
put(0x20000+0x168,STUB+0x10);put(0x20000+0x178,STUB+0x20)
uc.mem_write(DATA+0x10000+0x2510,struct.pack('<II',retry,initial_error))
for reg in (UC_X86_REG_RAX,UC_X86_REG_RBX,UC_X86_REG_RCX,UC_X86_REG_RDX,UC_X86_REG_R8,UC_X86_REG_R9,UC_X86_REG_R10,UC_X86_REG_R11,UC_X86_REG_R12,UC_X86_REG_R13,UC_X86_REG_R14,UC_X86_REG_R15):uc.reg_write(reg,0)
uc.reg_write(UC_X86_REG_RDI,DATA);uc.reg_write(UC_X86_REG_RSI,DATA+0x10000)
uc.reg_write(UC_X86_REG_RSP,STACK+0xfff8);uc.reg_write(UC_X86_REG_RBP,STACK+0x8000);uc.mem_write(STACK+0xfff8,struct.pack('<Q',STOP))
uc.emu_start(TEXT+defined['_nv059598rm']['st_value'],STOP,count=10000)
assert uc.reg_read(UC_X86_REG_RIP)==STOP and consumed==reads
recovery=any(event[0]=='bad_low' for event in events);value=uc.reg_read(UC_X86_REG_RAX)&0xffffffff;final_error=struct.unpack('<I',uc.mem_read(DATA+0x10000+0x2514,4))[0]
if recovery:assert [event[0] for event in events]==['unstable_low','bad_low','init','load'] and value==0
rows.append({'usermode':usermode,'retry':retry,'initial_error':initial_error,'reads':reads,'value':value,'final_error':final_error,'recovery':recovery,'warning':events[0][1] if events else None})
lines=['// Original _nv059598 plus full-reader instructions with mocked MMIO/recovery.','#[rustfmt::skip]','const LOW_VECTORS:&[LowVector]=&[']
for row in rows:
values=','.join(f'({offset},{value})' for offset,value in row['reads'])
warning='None' if row['warning'] is None else f"Some({row['warning']})"
lines.append(f"LowVector {{usermode:{str(row['usermode']).lower()},retry:{row['retry']},initial_error:{row['initial_error']},reads:&[{values}],value:{row['value']},final_error:{row['final_error']},recovery:{str(row['recovery']).lower()},warning:{warning}}},")
lines.append('];');generated='\n'.join(lines)+'\n'
dest=R/'src/nvidia/src/gpu/timer_low_oracle_vectors.rs'
if options.check:assert dest.read_text()==generated,'Low timer vectors differ from original instructions'
else:dest.write_text(generated)
(R/'reports/gpu-timer-low-binary.json').write_text(json.dumps({'passed':True,'artifact_sha256':hashlib.sha256(P.read_bytes()).hexdigest(),'vectors':len(rows),'functions':functions,'cases':rows,'execution':'Original low/full timer reader instructions only under Unicorn; MMIO, diagnostics and reset callbacks intercepted; no GPU or native driver execution.'},indent=2)+'\n')
print(len(rows),'original low timer vectors')