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
104 lines
7.8 KiB
Python
104 lines
7.8 KiB
Python
#!/home/geodd/.local/opt/rea-python/bin/python
|
|
"""Original selected format/levels/default getter and complete BAR2 instance bytes.
|
|
Private Unicorn execution only. Address/aperture/volatility getters are explicit
|
|
owned-descriptor models; no native driver, MMIO, root bind or TLB runs.
|
|
"""
|
|
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;RO=0x18000000;DATA=0x30000000;STACK=0x40000000;STUB=0x50000000;STOP=0x60000000
|
|
GPU=DATA;MMU=DATA+0x10000;FAMILY=DATA+0x20000;BUS=DATA+0x30000;INSTANCE=DATA+0x40000;LEVELS=DATA+0x50000;DESC=DATA+0x60000;FMT64=DATA+0x70000;FMT128=DATA+0x71000
|
|
names=['_nv039307rm'];hooks={}
|
|
with P.open('rb') as stream:
|
|
elf=ELFFile(stream);syms=list(elf.get_section_by_name('.symtab').iter_symbols());defined={s.name:s for s in syms}
|
|
text=bytearray(elf.get_section_by_name('.text').data());ro=elf.get_section_by_name('.rodata').data();ti=elf.get_section_index('.text');ri=elf.get_section_index('.rodata')
|
|
functions={n:(defined[n]['st_value'],defined[n]['st_size']) for n in names};undefined={}
|
|
for rel in elf.get_section_by_name('.rela.text').iter_relocations():
|
|
offset=rel['r_offset']
|
|
if not any(start<=offset<start+size for start,size in functions.values()):continue
|
|
symbol=syms[rel['r_info_sym']];kind=rel['r_info_type']
|
|
if symbol['st_shndx']==ti:target=TEXT+symbol['st_value']
|
|
elif symbol['st_shndx']==ri:target=RO+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)
|
|
value=target+rel['r_addend']-(TEXT+offset) if kind in(2,4) else target+rel['r_addend'];struct.pack_into('<i',text,offset,value)
|
|
for name in ['_nv043721rm','_nv040815rm','_nv049631rm','_nv059509rm','_nv059505rm','_nv049819rm','_nv053907rm','_nv053912rm','_nv047869rm']:
|
|
hooks[TEXT+defined[name]['st_value']]=name
|
|
u=Uc(UC_ARCH_X86,UC_MODE_64)
|
|
for base,size in [(TEXT,len(text)),(RO,len(ro)),(DATA,0x80000),(STACK,0x10000),(STUB,0x10000),(STOP,4096)]:u.mem_map(base,(size+4095)&~4095)
|
|
u.mem_write(TEXT,bytes(text));u.mem_write(RO,ro)
|
|
descriptor_address=0;descriptor_aperture=0;descriptor_volatile=0
|
|
|
|
def put(address,value):u.mem_write(address,struct.pack('<Q',value))
|
|
def ret(value):
|
|
sp=u.reg_read(UC_X86_REG_RSP);ip=struct.unpack('<Q',u.mem_read(sp,8))[0];u.reg_write(UC_X86_REG_RSP,sp+8);u.reg_write(UC_X86_REG_RAX,value);u.reg_write(UC_X86_REG_RIP,ip)
|
|
def hook(_,address,size,user):
|
|
global read_count,check_count,set_count,spin_count
|
|
name=hooks.get(address)
|
|
if not name:return
|
|
if name=='_nv043721rm':assert u.reg_read(UC_X86_REG_RDI)==DESC;ret(descriptor_address)
|
|
elif name=='_nv040815rm':assert u.reg_read(UC_X86_REG_RSI)==DESC;ret(descriptor_aperture)
|
|
elif name=='_nv053912rm':
|
|
assert u.reg_read(UC_X86_REG_RCX)==0x1714
|
|
events.append(('write',0x1714,u.reg_read(UC_X86_REG_R8)&0xffffffff));ret(0)
|
|
elif name=='_nv053907rm':
|
|
assert u.reg_read(UC_X86_REG_RCX)==0x1710
|
|
value=statuses[min(read_count,len(statuses)-1)];read_count+=1;events.append(('read',0x1710,value));ret(value)
|
|
elif name=='_nv049631rm':events.append(('fence',));ret(0)
|
|
elif name=='_nv059509rm':
|
|
assert u.reg_read(UC_X86_REG_RDI)==GPU+0x1e08 and u.reg_read(UC_X86_REG_RDX)==0 and u.reg_read(UC_X86_REG_RCX)==0
|
|
set_count+=1;events.append(('set',));ret(0)
|
|
elif name=='_nv059505rm':
|
|
assert u.reg_read(UC_X86_REG_RDI)==GPU+0x1e08 and u.reg_read(UC_X86_REG_RDX)==0x15d8
|
|
value=checks[min(check_count,len(checks)-1)];check_count+=1;events.append(('check',value));ret(value)
|
|
elif name=='_nv049819rm':spin_count+=1;events.append(('spin',));ret(0)
|
|
elif name=='_nv047869rm':events.append(('diagnostic',));ret(0)
|
|
else:raise AssertionError('unexpected external '+name)
|
|
u.hook_add(UC_HOOK_CODE,hook)
|
|
def call(name,args):
|
|
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,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):u.reg_write(reg,value)
|
|
u.reg_write(UC_X86_REG_RSP,STACK+0xfff8);put(STACK+0xfff8,STOP);u.reg_write(UC_X86_REG_RBP,STACK+0x8000)
|
|
u.emu_start(TEXT+functions[name][0],STOP,count=10000);assert u.reg_read(UC_X86_REG_RIP)==STOP
|
|
return u.reg_read(UC_X86_REG_RAX)
|
|
|
|
# The selected spin callback has no hidden scheduling operation.
|
|
spin=defined['_nv049819rm'];assert bytes(text[spin['st_value']:spin['st_value']+spin['st_size']])==bytes.fromhex('f30f1efac3')
|
|
rows=[]
|
|
trails=[([0],[],0),([0xc,0],[0],0),([0xc,0],[0x65],0),([0xc,0xc],[0x65],0x65),([0xc,0xc,0],[0x40,0x40],0),([0x10],[],0)]
|
|
for descriptor_address in [0x100000,0x100000000]:
|
|
for descriptor_aperture in [0,2,3]:
|
|
for virtual in [False,True]:
|
|
for reset in [0,1,2,4]:
|
|
for statuses,checks,expected in trails:
|
|
u.mem_write(DATA,bytes(0x80000));put(GPU+0x1f90,MMU);put(BUS+0x4628,DESC)
|
|
u.mem_write(GPU+0x917,bytes([bool(reset&1)]));u.mem_write(GPU+0x918,struct.pack('<H',0x100 if reset&2 else 0));u.mem_write(GPU+0x926,bytes([bool(reset&4)]))
|
|
events=[];read_count=check_count=set_count=spin_count=0
|
|
actual=call('_nv039307rm',[GPU,BUS,int(virtual)])&0xffffffff
|
|
assert actual==(0 if reset else expected)
|
|
word=((descriptor_aperture<<28)|((descriptor_address>>12)&0xfffffff)|0xc0000000) if virtual else 0x40000000
|
|
assert events[0]==('write',0x1714,word) and events[1]==('fence',)
|
|
if reset:assert read_count==check_count==set_count==spin_count==0
|
|
else:assert set_count==1 and check_count==spin_count and read_count==check_count+1
|
|
rows.append({'address':descriptor_address,'aperture':descriptor_aperture,'virtual':virtual,'reset':reset,'statuses':statuses,'checks':checks,'expected':actual,'word':word,'reads':read_count,'check_count':check_count,'set_count':set_count,'events':events.copy()})
|
|
report={'passed':True,'artifact_sha256':hashlib.sha256(P.read_bytes()).hexdigest(),'cases':rows,'scope':'Whole original039307 bind instructions. Instance address/aperture, MMIO, CPU WCflush and timeout callbacks explicitly modeled. Original049819 provenENDBR64/RET. No native execution, actual GPU bind/TLB/data-drain or physical memory test.'}
|
|
lines=['// Generated from exact580 whole original039307 bind instructions.','pub struct Vector {\n pub address: u64,\n pub aperture: u32,\n pub virtual_mode: bool,\n pub reset: u8,\n pub statuses: [u32; 3],\n pub status_count: usize,\n pub checks: [u32; 2],\n pub expected: u32,\n pub word: u32,\n pub reads: u32,\n}','#[rustfmt::skip]','pub const VECTORS: &[Vector] = &[']
|
|
for row in rows:
|
|
statuses=row['statuses']+[0]*(3-len(row['statuses']));checks=row['checks']+[0]*(2-len(row['checks']))
|
|
lines.append('Vector{address:%d,aperture:%d,virtual_mode:%s,reset:%d,statuses:%s,status_count:%d,checks:%s,expected:%d,word:%d,reads:%d},'%(row['address'],row['aperture'],str(row['virtual']).lower(),row['reset'],statuses,len(row['statuses']),checks,row['expected'],row['word'],row['reads']))
|
|
lines.append('];')
|
|
outputs={R/'reports/bar2-bind-binary.json':json.dumps(report,indent=2)+'\n',R/'src/nvidia/src/mmu/bar2_bind_vectors.rs':'\n'.join(lines)+'\n'}
|
|
for path,content in outputs.items():
|
|
if options.check:
|
|
if not path.exists() or path.read_text()!=content:raise SystemExit('Generated output differs: '+str(path))
|
|
else:path.write_text(content)
|
|
print(len(rows),'original BAR2 bind cases passed')
|