Files
nvdriver/tools/gpu_generate_clock_cache_data.py

67 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Exact selected020153 base-level cache-prefix stores from original inference."""
from pathlib import Path
import argparse, hashlib, json, os, re, shutil, subprocess
def find_rustfmt(repo_root):
if os.environ.get('RUSTFMT'):
return os.environ['RUSTFMT']
rustup = shutil.which('rustup')
if rustup:
rustup = os.path.abspath(rustup)
res = subprocess.run([rustup, 'which', 'rustfmt'], cwd=str(repo_root), capture_output=True, text=True)
if res.returncode == 0 and res.stdout.strip():
return res.stdout.strip()
which = shutil.which('rustfmt')
if which:
return which
toolchain = re.search(r'channel\s*=\s*"([^"]+)"', (repo_root / 'rust-toolchain.toml').read_text()).group(1)
rustup_home = Path(os.environ.get('RUSTUP_HOME', str(Path.home() / '.rustup')))
return str(rustup_home / 'toolchains' / f'{toolchain}-x86_64-unknown-linux-gnu/bin/rustfmt')
def main(argv=None):
r = Path(__file__).resolve().parent.parent
p = argparse.ArgumentParser()
p.add_argument('--check', action='store_true')
a = p.parse_args(argv)
analysis_root = Path(os.environ.get('NV_RS_ANALYSIS_ROOT', '/home/geodd/nvidia-driver/analysis-580.178.04'))
matches = list((analysis_root / 'decompiled/kernel-rm-all/functions').glob('*/*__nv020153rm.c'))
if not matches:
raise SystemExit(f'missing __nv020153rm.c under {analysis_root} (set NV_RS_ANALYSIS_ROOT)')
s = matches[0].read_text()
s = s[:s.index(' lVar11 = *(long *)(param_1 + 0x1ec0);')]
rows = []
for index, value in re.findall(r'(?:\*param_5|param_5\[([^]]+)\]) = (0x[0-9a-f]+);', s):
i = 0 if not index else int(index, 0)
assert i % 4 == 0
rows.append((i // 4, int(value, 16)))
flags = [int(off, 16) // 16 for off in re.findall(r'\*\(undefined1 \*\)\(\(long\)param_5 \+ (0x[0-9a-f]+)\) = 0;', s)]
assert [i for i, v in rows] == list(range(66)) + list(range(67, 75)) and flags == list(range(66))
code = '// Generated from literal original020153 base-level cache prefix.\nconst ADDRESS_STORES: &[(usize,u32)] = &[\n' + ''.join(f' ({i},0x{v:x}),\n' for i, v in rows) + '];\n'
code = subprocess.run([find_rustfmt(r), '--edition', '2024'], input=code, text=True, capture_output=True, check=True).stdout
report = {
'source_sha256': '5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b',
'inferred_prefix_sha256': hashlib.sha256(s.encode()).hexdigest(),
'address_stores': rows,
'clear_byte_d_rows': flags,
'untouched_address_rows': [66] + list(range(75, 130)),
'limits': 'Only actual constructor stores; all other record fields retain caller-provided state. No empty/default records or hardware access.',
}
for out, text in [
(r / 'src/nvidia/src/gpu/clock_cache_data.rs', code),
(r / 'reports/gpu-clock-cache-data.json', json.dumps(report, indent=2) + '\n'),
]:
if a.check:
if out.read_text() != text:
raise SystemExit('Stale ' + str(out))
else:
out.write_text(text)
print('Original020153cache:74address stores,66byteD clears; record66 address untouched')
return 0
if __name__ == '__main__':
raise SystemExit(main())