mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
- 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
124 lines
13 KiB
Python
Executable File
124 lines
13 KiB
Python
Executable File
#!/home/geodd/.local/opt/rea-python/bin/python
|
|
"""Original GV100 engine-cache constructor in Unicorn with mocked OS/GPU services."""
|
|
import 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 *
|
|
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;RO=0x20000000;DATA=0x30000000;STACK=0x40000000;STUB=0x50000000;STOP=0x60000000;HEAP=0x70000000
|
|
GPU=DATA;KF=DATA+0x10000;FIFO=DATA+0x20000
|
|
names=['_nv027760rm','_nv027763rm','_nv027765rm','_nv040335rm','_nv027828rm','_nv027812rm','_nv028098rm','_nv027885rm','_nv027886rm','_nv027854rm','_nv031279rm','_nv031280rm','_nv040256rm','_nv040255rm','_nv040300rm','_nv052714rm','_nv005820rm'];hooks={}
|
|
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());ro=bytearray(e.get_section_by_name('.rodata').data());ti=e.get_section_index('.text');ri=e.get_section_index('.rodata');functions={n:(defined[n]['st_value'],defined[n]['st_size']) for n in names};undef={}
|
|
for r in e.get_section_by_name('.rela.text').iter_relocations():
|
|
off=r['r_offset']
|
|
if not any(a<=off<a+size for a,size in functions.values()):continue
|
|
s=symbols[r['r_info_sym']]
|
|
if s['st_shndx']==ti:target=TEXT+s['st_value']
|
|
elif s['st_shndx']==ri:target=RO+s['st_value']
|
|
elif s['st_shndx']=='SHN_UNDEF':target=undef.setdefault(s.name,STUB+0x100+len(undef)*16);hooks[target]=s.name
|
|
else:raise AssertionError((s.name,s['st_shndx']))
|
|
kind=r['r_info_type'];assert kind in [2,4,11];value=target+r['r_addend']-(TEXT+off) if kind in [2,4] else target+r['r_addend'];struct.pack_into('<i',text,off,value)
|
|
meta_offset=int(json.loads((R/'reports/engine-discovery-data.json').read_text())['rodata_offsets']['engine_metadata'],16)
|
|
for r in e.get_section_by_name('.rela.rodata').iter_relocations():
|
|
if meta_offset<=r['r_offset']<meta_offset+15*24:
|
|
s=symbols[r['r_info_sym']];assert r['r_info_type']==1 and s['st_shndx']==ri;struct.pack_into('<Q',ro,r['r_offset'],RO+s['st_value']+r['r_addend'])
|
|
for name in ['_nv030608rm','_nv030927rm','_nv030214rm','_nv053907rm','_nv052687rm','_nv052701rm','_nv052695rm','_nv047869rm','_nv046745rm','_nv046747rm','_nv017883rm','_nv017901rm','_nv017897rm']:hooks[TEXT+defined[name]['st_value']]=name
|
|
uc=Uc(UC_ARCH_X86,UC_MODE_64)
|
|
for base,blob in [(TEXT,text),(RO,ro)]:uc.mem_map(base,(len(blob)+4095)&~4095);uc.mem_write(base,bytes(blob))
|
|
for base,size in [(DATA,0x40000),(STACK,0x20000),(STUB,0x10000),(STOP,4096),(HEAP,0x10000)]:uc.mem_map(base,size)
|
|
hooks[STUB+0x10]='access_gate';hooks[STUB+0x20]='software_translation_failure';reads=[];allocs=[];live=set();frees=[];bit_sets=[];device_words=[];cfg=0;runlist_masks=[];allocation_failure=0;software_failure=False;software_sources=[]
|
|
|
|
def ret(value):
|
|
sp=uc.reg_read(UC_X86_REG_RSP);addr=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,addr)
|
|
def hook(emu,addr,size,user):
|
|
if addr==TEXT+defined['_nv027763rm']['st_value']:
|
|
source=struct.unpack('<I',emu.mem_read(emu.reg_read(UC_X86_REG_RCX),4))[0];software_sources.append(source)
|
|
if software_failure:put(KF,0x1b8,STUB+0x20)
|
|
name=hooks.get(addr)
|
|
if not name:return
|
|
if name.startswith('__x86_indirect_thunk_'):
|
|
reg={'rax':UC_X86_REG_RAX,'r9':UC_X86_REG_R9}[name.rsplit('_',1)[1]];emu.reg_write(UC_X86_REG_RIP,emu.reg_read(reg));return
|
|
args=[emu.reg_read(r) for r in [UC_X86_REG_RDI,UC_X86_REG_RSI,UC_X86_REG_RDX,UC_X86_REG_RCX,UC_X86_REG_R8,UC_X86_REG_R9]]
|
|
if name=='access_gate':ret(0)
|
|
elif name=='software_translation_failure':ret(0x57)
|
|
elif name=='_nv030608rm':ret(KF)
|
|
elif name in ['_nv030927rm','_nv030214rm']:ret(1)
|
|
elif name=='_nv053907rm':
|
|
offset=args[3];reads.append(offset)
|
|
if offset==0x2004:ret(cfg)
|
|
elif 0x2390<=offset<0x23d0:ret(runlist_masks[(offset-0x2390)//4])
|
|
elif 0x22700<=offset<0x22800:ret(device_words[(offset-0x22700)//4])
|
|
else:raise AssertionError(hex(offset))
|
|
elif name=='_nv052687rm':
|
|
allocs.append(args[0])
|
|
if len(allocs)==allocation_failure:ret(0)
|
|
else:
|
|
pointer=HEAP+(len(allocs)-1)*0x4000;live.add(pointer);emu.mem_write(pointer,bytes(args[0]));ret(pointer)
|
|
elif name=='_nv052701rm':
|
|
if args[0]:assert args[0] in live;live.remove(args[0]);frees.append(args[0])
|
|
ret(0)
|
|
elif name=='_nv052695rm':assert args[1]>=args[3];emu.mem_write(args[0],bytes(emu.mem_read(args[2],args[3])));ret(args[0])
|
|
elif name=='_nv017883rm':emu.mem_write(args[0],bytes(64));ret(0)
|
|
elif name in ['_nv017901rm','_nv017897rm']:
|
|
base,_,bit=args[:3];assert bit<512;offset=bit//8;value=emu.mem_read(base+offset,1)[0];mask=1<<(bit%8)
|
|
if name=='_nv017897rm':bit_sets.append((base,bit));emu.mem_write(base+offset,bytes([value|mask]));ret(0)
|
|
else:ret(int(bool(value&mask)))
|
|
elif name in ['_nv047869rm','_nv046745rm','_nv046747rm']:ret(0)
|
|
else:raise AssertionError(name)
|
|
uc.hook_add(UC_HOOK_CODE,hook)
|
|
def put(base,offset,value,width=8):uc.mem_write(base+offset,value.to_bytes(width,'little'))
|
|
def initialize():
|
|
uc.mem_write(DATA,bytes(0x40000));put(GPU,0x915,1,1);put(GPU,0x558,STUB+0x10);put(GPU,0x570,TEXT+defined['_nv031279rm']['st_value'])
|
|
for off,name in [(0x288,'_nv040335rm'),(0x1b8,'_nv040256rm')]:put(KF,off,TEXT+defined[name]['st_value'])
|
|
for off,name in [(0x5e0,'_nv027828rm'),(0x700,'_nv027812rm'),(0x4f8,'_nv028098rm'),(0x558,'_nv027885rm'),(0x310,'_nv027854rm')]:put(FIFO,off,TEXT+defined[name]['st_value'])
|
|
|
|
def run(name="_nv027760rm",args=None):
|
|
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_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_RSP,STACK+0x1fff8);uc.mem_write(STACK+0x1fff8,struct.pack('<Q',STOP));uc.reg_write(UC_X86_REG_RBP,STACK+0x10000);uc.reg_write(UC_X86_REG_RDI,GPU);uc.reg_write(UC_X86_REG_RSI,FIFO)
|
|
if args is not None:
|
|
for reg,value in zip([UC_X86_REG_RDI,UC_X86_REG_RSI,UC_X86_REG_RDX,UC_X86_REG_RCX,UC_X86_REG_R8,UC_X86_REG_R9],args):uc.reg_write(reg,value)
|
|
try:uc.emu_start(TEXT+functions[name][0],STOP,count=50000)
|
|
except Exception as error:raise RuntimeError((hex(uc.reg_read(UC_X86_REG_RIP)),reads,allocs,frees)) from error
|
|
assert uc.reg_read(UC_X86_REG_RIP)==STOP
|
|
return uc.reg_read(UC_X86_REG_RAX)&0xffffffff
|
|
|
|
def chain(typ,instance,tag,runlist,fault):return [0x8000003e|(tag<<26)|(runlist<<21)|(1<<9)|(2<<15),0x80000003|(typ<<2),5|(instance<<26)|(fault<<3)]
|
|
cases=[]
|
|
for label,engines,fail in [('gr_ce',[chain(0,0,0,2,0x10),chain(19,2,2,3,0x20)],0),('ce_gr',[chain(19,2,2,3,0x20),chain(0,0,0,2,0x10)],0),('ce0_ce2',[chain(19,0,0,2,0x10),chain(19,2,2,3,0x20)],0),('sec2_ce0',[chain(13,0,3,4,0x30),chain(19,0,0,2,0x10)],0),('unknown_after_gr',[chain(0,0,0,2,0x10),chain(99,0,3,4,0x30)],0),('software_failure',[chain(0,0,0,2,0x10)],0),('scratch_alloc_failure',[chain(0,0,0,2,0x10)],1),('final_alloc_failure',[chain(0,0,0,2,0x10)],2)]:
|
|
initialize();allocs=[];live=set();frees=[];reads=[];bit_sets=[];allocation_failure=fail;software_failure=label=='software_failure';software_sources=[];cfg=(0x20<<16)|2;runlist_masks=[(1<<2)|(1<<3)|(1<<4),1<<3]+[0]*14;device_words=sum(engines,[]);device_words +=[0]*(64-len(device_words));status=run();count=struct.unpack('<I',uc.mem_read(KF+0x340,4))[0];pointer=struct.unpack('<Q',uc.mem_read(KF+0x348,8))[0];table=bytes(uc.mem_read(pointer,count*100)) if pointer else b'';source={'case':label,'device_words':device_words,'status':status,'record_count':count,'cache_pointer_present':bool(pointer),'table_bytes':list(table),'allocation_bytes':allocs,'live_allocations':len(live),'freed_allocations':len(frees),'register_reads':reads,'pbdma_valid_bits':[bit for base,bit in bit_sets if base==KF+0x2f8],'software_source':software_sources[0] if software_sources else None};cases.append(source)
|
|
if status==0:
|
|
old_reads=len(reads);old_allocs=len(allocs);old_pointer=pointer;again=run();now_pointer=struct.unpack('<Q',uc.mem_read(KF+0x348,8))[0];assert again==0 and len(reads)==old_reads and len(allocs)==old_allocs and now_pointer==old_pointer;source['idempotent_fast_path']={'status':again,'additional_reads':len(reads)-old_reads,'additional_allocations':len(allocs)-old_allocs,'pointer_unchanged':now_pointer==old_pointer}
|
|
print(label,status,count,hex(pointer),'live',len(live),'software',struct.unpack_from('<21I',table,len(table)-100) if table else None,flush=True)
|
|
# Real GV100 topology: read-only BAR0 snapshot of 0000:b3:00.0 under the original driver.
|
|
snapshot=json.loads((R/'reports/hardware-inventory-original-b3.json').read_text())['inventory']
|
|
initialize();allocs=[];live=set();frees=[];reads=[];bit_sets=[];allocation_failure=0;software_failure=False;software_sources=[]
|
|
# The selected GV100 property binder _nv005820rm (chip HAL 0x23, RM HAL 2) sets KernelFifo+7c1, which makes the constructor skip device types without engine metadata.
|
|
CHIPHAL=DATA+0x3e000;RMHAL=DATA+0x3e100;uc.mem_write(CHIPHAL+0x10,struct.pack('<H',0x23));uc.mem_write(RMHAL+0x10,struct.pack('<H',2));saved=bytes(uc.mem_read(FIFO,0x800));run('_nv005820rm',[FIFO,CHIPHAL,RMHAL]);flag_7c1=uc.mem_read(FIFO+0x7c1,1)[0];assert flag_7c1==1;uc.mem_write(FIFO,saved);uc.mem_write(FIFO+0x7c1,bytes([flag_7c1]))
|
|
cfg=int(snapshot['002004'],16);runlist_masks=[int(snapshot['%06x'%(0x2390+4*i)],16) for i in range(16)];device_words=[int(snapshot['%06x'%(0x22700+4*i)],16) for i in range(64)]
|
|
v100_status=run();v100_count=struct.unpack('<I',uc.mem_read(KF+0x340,4))[0];v100_pointer=struct.unpack('<Q',uc.mem_read(KF+0x348,8))[0]
|
|
print("v100 status",hex(v100_status),"count",v100_count,"reads",len(reads),"sources",software_sources,flush=True)
|
|
assert v100_status==0 and v100_pointer
|
|
v100={'kernel_fifo_7c1':flag_7c1,'pbdma_valid_bits':sorted({bit for base,bit in bit_sets if base==KF+0x2f8}),'cfg':cfg,'runlist_masks':runlist_masks,'device_words':device_words,'record_count':v100_count,'table_bytes':list(bytes(uc.mem_read(v100_pointer,v100_count*100))),'software_source':software_sources[0] if software_sources else None,'register_reads':len(reads),'source':'reports/hardware-inventory-original-b3.json'}
|
|
print('v100_b3',v100_status,v100_count,'records',flush=True)
|
|
# Observe count-gated consumers and an explicitly unsupported raw-column query.
|
|
inactive_audit=[];base=HEAP+0x8000;out=DATA+0x38000
|
|
for inactive in [0x12345,0x54321]:
|
|
payload=bytearray(bytes(cases[0]['table_bytes']))
|
|
for off in range(0,len(payload),100):
|
|
if struct.unpack_from('<I',payload,off+80)[0]==1:struct.pack_into('<I',payload,off+68,inactive);struct.pack_into('<I',payload,off+76,inactive)
|
|
uc.mem_write(base,bytes(payload));put(KF,0x348,base);put(KF,0x340,3,4)
|
|
uc.mem_write(out,struct.pack('<I',0xaabbccdd));xlate=run('_nv040255rm',[GPU,KF,17,0x12345,0,out]);assert xlate==0x57
|
|
listed=run('_nv040300rm',[GPU,KF,3,3,out,out+8]);ptr,count=struct.unpack('<QI',uc.mem_read(out,12));active=struct.unpack('<I',uc.mem_read(ptr,4))[0];assert listed==0 and count==1 and active==0
|
|
raw_selector=run('_nv040300rm',[GPU,KF,17,0x12345,out,out+8])
|
|
inactive_audit.append({'inactive_lane_value':inactive,'Xlate_PBDMA17_status':xlate,'GetPbdmaIds_RUNLIST3_status':listed,'active_count':count,'active_ID0':active,'GetPbdmaIds_raw_type17_fixed_query_status':raw_selector})
|
|
assert [a['GetPbdmaIds_raw_type17_fixed_query_status'] for a in inactive_audit]==[0,0x1f]
|
|
lines=['// Complete original constructor instruction outputs with mocked zeroed OS allocations.','pub type Vector = (&\'static [u32; 64], &\'static [u8], u32);','#[rustfmt::skip]','pub const VECTORS: &[Vector] = &[']
|
|
for case in cases:
|
|
if case['status']==0:lines.append('(&'+str(case['device_words'])+',&'+str(case['table_bytes'])+','+str(case['software_source'])+'),')
|
|
lines.append('];');lines += ['// Original constructor output for a physical GV100 topology snapshot.','#[allow(dead_code)]','pub type Physical = (u32, &\'static [u32; 16], &\'static [u32; 64], &\'static [u8], u32);','#[allow(dead_code)]','#[rustfmt::skip]','pub const V100: Physical = ('+str(v100['cfg'])+',&'+str(v100['runlist_masks'])+',&'+str(v100['device_words'])+',&'+str(v100['table_bytes'])+','+str(v100['software_source'])+');'];(R/'src/nvidia/src/mmu/engine_cache_vectors.rs').write_text('\n'.join(lines)+'\n')
|
|
assert [c['status'] for c in cases]==[0,0,0,0,0x56,0x57,0x51,0x51]
|
|
(R/'reports/engine-cache-binary.json').write_text(json.dumps({'artifact_sha256':hashlib.sha256(P.read_bytes()).hexdigest(),'cases':cases,'physical_gv100':v100,'inactive_lane_consumer_audit':inactive_audit,'scope':'Original constructor and metadata/normalizer/SOFTWARE/lookup instructions in isolated Unicorn; deterministic zeroed OS allocations, intercepted OS copy/bitvector services, access-state and register reads. No native/GPU execution.'},indent=2)+'\n')
|