mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
39 lines
4.7 KiB
Python
39 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Extract original RM GV100 child order/presence; never execute target code."""
|
|
from pathlib import Path
|
|
import re,json,struct,hashlib,argparse
|
|
from elftools.elf.elffile import ELFFile
|
|
from elftools.elf.relocation import RelocationSection
|
|
R=Path(__file__).resolve().parent.parent;E=Path('/home/geodd/nvidia-driver/analysis-580.178.04');F=E/'extracted/kernel/nvidia/nv-kernel.o_binary';S=E/'reference/open-gpu-kernel-modules-580.178.04/src/nvidia'
|
|
p=argparse.ArgumentParser();p.add_argument('--check',action='store_true');args=p.parse_args();names={}
|
|
for source in (S/'generated').glob('*.h'):
|
|
for name,value in re.findall(r'#define __nvoc_class_id_(\w+) (0x[0-9a-fA-F]+)',source.read_text(errors='replace')):names[int(value,16)]=name
|
|
with F.open('rb') as f:
|
|
e=ELFFile(f);section=e.get_section_by_name('.rodata');data=section.data();symbols={s.name:s for s in e.get_section_by_name('.symtab').iter_symbols()};sectionidx=e.get_section_index('.rodata');relocs={};section_data={sectionidx:data}
|
|
for sec in e.iter_sections():
|
|
if isinstance(sec,RelocationSection) and sec['sh_info']==sectionidx:
|
|
symtable=e.get_section(sec['sh_link'])
|
|
for rel in sec.iter_relocations():relocs[rel['r_offset']]={'symbol':symtable.get_symbol(rel['r_info_sym']).name,'type':rel['r_info_type'],'addend':rel['r_addend']}
|
|
start=0x4c60ee0;presence_start=0x4c63760
|
|
order=[]
|
|
for i in range(100):
|
|
cls,flags=struct.unpack_from('<II',data,start+i*8);assert cls<1<<24 and 0<flags<=15;order.append({'class_id':cls,'flags':flags,'name':names.get(cls,f'class_{cls:06x}'),'section_offset':start+i*8})
|
|
presence=[]
|
|
for i in range(75):
|
|
off=presence_start+i*16;cls,n=struct.unpack_from('<II',data,off);ref=relocs[off+8];sym=symbols[ref['symbol']];raw_section=section_data.get(sym['st_shndx']);assert raw_section is not None;record=raw_section[sym['st_value']:sym['st_value']+sym['st_size']];size,info_id=struct.unpack_from('<II',record);assert cls<1<<24 and info_id<1<<24 and 0<n<=256;presence.append({'class_id':cls,'instances':n,'name':names.get(cls,f'class_{cls:06x}'),'class_info_symbol':sym.name,'class_object_size':size,'concrete_class_id':info_id,'class_info_bytes':record.hex(),'class_info_relocations':[{**rel,'relative_offset':offset-sym['st_value']} for offset,rel in relocs.items() if sym['st_value']<=offset<sym['st_value']+sym['st_size']],'section_offset':off})
|
|
assert len({x['class_id'] for x in presence})==75
|
|
def ordered(mask,reverse=False):
|
|
out=[];available={x['class_id']:x['instances'] for x in presence}
|
|
for entry in (reversed(order) if reverse else order):
|
|
if entry['flags']&mask!=mask:continue
|
|
for instance in (reversed(range(available.get(entry['class_id'],0))) if reverse else range(available.get(entry['class_id'],0))):out.append(entry['class_id']<<8|instance)
|
|
return out
|
|
phases={n:ordered(mask,rev) for n,mask,rev in [('init',1,False),('load',2,False),('unload',4,True),('destroy',8,True)]}
|
|
report={'source_sha256':hashlib.sha256(F.read_bytes()).hexdigest(),'architecture':{'architecture':0x14,'implementation':0,'HAL_index':0x23,'evidence':'_nv006052rm + _nv005978rm'},'order':{'source_function':'_nv030434rm','section':'.rodata','offset':start,'record_size':8,'records':order},'presence':{'source_function':'_nv030452rm','section':'.rodata','offset':presence_start,'record_size':16,'records':presence},'descriptor_encoding':'(class_id <<8)|instance; init/load forward; unload/destroy reverse including instances','phases':phases,'limits':['Static GV100 monolithic class/HAL binding; no live register reads or boot writes.','Actual constructed engine availability can remove classes later; caller must check each engine object.','Class-info metadata layouts differ from public NVOCv2; raw records and relocations retained without guessing extra field names.']}
|
|
code='// Generated from original580.178.04 RM GV100 tables; see reports/gpu-gv100-tables.json.\nuse super::{ChildOrder,ChildPresence};\npub static ORDER:[ChildOrder;100]=[\n'+''.join(' ChildOrder{class_id:0x%x,flags:0x%x}, // %s\n'%(x['class_id'],x['flags'],x['name']) for x in order)+'];\npub static PRESENT:[ChildPresence;75]=[\n'+''.join(' ChildPresence{class_id:0x%x,concrete_class_id:0x%x,instances:%d,object_bytes:0x%x}, // %s %s\n'%(x['class_id'],x['concrete_class_id'],x['instances'],x['class_object_size'],x['name'],x['class_info_symbol']) for x in presence)+'];\n'
|
|
code+='\n'+''.join('pub static '+name.upper()+'_DESCRIPTORS:[u32;'+str(len(values))+']=['+','.join(hex(x) for x in values)+'];\n' for name,values in phases.items())
|
|
output=R/'src/nvidia/src/gpu/gv100_data.rs'
|
|
if args.check:assert output.read_text()==code
|
|
else:output.write_text(code);(R/'reports/gpu-gv100-tables.json').write_text(json.dumps(report,indent=2)+'\n')
|
|
print('GV100',len(order),'order rows',len(presence),'present classes;', {k:len(v) for k,v in phases.items()})
|