#!/usr/bin/env python3 """Execute exact selected fault-buffer HAL instructions; mock only MMIO/memset/logging.""" import argparse,hashlib,json,struct,warnings from _binary_path import resolve_580_binary 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');args=parser.parse_args() R=Path(__file__).resolve().parents[1] P=resolve_580_binary() TEXT=0x10000000;DATA=0x30000000;STACK=0x40000000;STUB=0x50000000;STOP=0x60000000 GPU=DATA;FB=DATA+0x10000;DESCRIPTOR=DATA+0x20000;BUFFER=DATA+0x30000 names=['_nv040937rm'];hooks={} with P.open('rb') as f: e=ELFFile(f);syms=list(e.get_section_by_name('.symtab').iter_symbols());defined={s.name:s for s in syms};text=bytearray(e.get_section_by_name('.text').data());ti=e.get_section_index('.text');functions={n:(defined[n]['st_value'],defined[n]['st_size']) for n in names};undef={} for rel in e.get_section_by_name('.rela.text').iter_relocations(): o=rel['r_offset'] if not any(a<=o>5 custom=bool((flags&3)==3 or flags&4) assert capacity==(0x2800 if custom else default_capacity) reads=[v for kind,offset,v in events if kind=='read'];writes=[v for kind,offset,v in events if kind=='write'] assert len(reads)==(3 if custom else 2) and len(writes)==(2 if custom else 1) rows.append({'flags':flags,'initial':initial,'hardware_default':default_capacity,'capacity':capacity,'events':events.copy()}) report={'passed':True,'artifact_sha256':hashlib.sha256(P.read_bytes()).hexdigest(),'cases':rows,'scope':'Original040937 whole instructions, all8 real policy flag combinations,5 disabled size states and4 modeled hardware capacities. Only register read/write and logging mocked; SET_DEFAULT hardware count behavior modeled explicitly. No GPU, BAR2 mapping, IRQ, producer setup or hardware default-capacity proof.'} lines=['// Generated from original040937 whole instruction execution.','pub struct Vector {\n pub flags: u8,\n pub reads: [u32; 3],\n pub writes: [u32; 2],\n pub count: u32,\n pub custom: bool,\n}','#[rustfmt::skip]','pub const VECTORS:&[Vector]=&['] for row in rows: reads=[v for kind,_,v in row['events'] if kind=='read'];writes=[v for kind,_,v in row['events'] if kind=='write'];custom=len(writes)==2 reads+= [0]*(3-len(reads));writes+=[0]*(2-len(writes)) lines.append('Vector{flags:%d,reads:%s,writes:%s,count:%d,custom:%s},'%(row['flags'],str(reads),str(writes),row['capacity'],str(custom).lower())) lines.append('];') outputs={R/'reports/fault-default-size-binary.json':json.dumps(report,indent=2)+'\n',R/'src/nvidia/src/mmu/fault_setup_vectors.rs':'\n'.join(lines)+'\n'} for path,content in outputs.items(): if args.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 default-size cases passed')