mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
112 lines
5.2 KiB
Python
112 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Selected GP106 CPU-only PF/architecture constructors in private Unicorn RAM."""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
from unicorn import UC_HOOK_MEM_WRITE
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
import check_gpu_memory_placement_binary as o
|
|
|
|
CANONICAL = {'.text': 0x0616f810, '.rodata': 0x0111d660, '.data': 0x00f7e890, '.bss': 0x05fd2180}
|
|
|
|
def canonical(value):
|
|
for name, base in o.BASES.items():
|
|
if base <= value < base + len(o.sections[name]):
|
|
return value - base + CANONICAL[name]
|
|
return value
|
|
|
|
def normalize(image, root):
|
|
result = bytearray(image)
|
|
for at in range(0, len(result) - 7, 8):
|
|
value = int.from_bytes(result[at:at + 8], 'little')
|
|
value = 0x24000000 + value - root if root <= value < root + 0x40000 else canonical(value)
|
|
result[at:at + 8] = value.to_bytes(8, 'little')
|
|
return result
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--write', action='store_true', help='update the tracked GP106 metadata')
|
|
args = parser.parse_args()
|
|
o.TEMPLATE = o.build_template(0x20, 0x136000a1, 0x136a1000)
|
|
o.reset(dict(registers={}, registry={}, sizes={}))
|
|
gpu = o.allocate(0x40000)
|
|
stores = []
|
|
def written(cpu, access, address, size, value, unused):
|
|
if gpu <= address < gpu + 0x6e70:
|
|
stores.append((address - gpu, size * 8, value & ((1 << (size * 8)) - 1)))
|
|
hook = o.cpu.hook_add(UC_HOOK_MEM_WRITE, written)
|
|
patterns = []
|
|
for fill in (0, 0xa5):
|
|
o.cpu.mem_write(gpu, bytes([fill]) * 0x6e70)
|
|
stores.clear()
|
|
o.call('_nv005722rm', gpu, 0x13, 6, 0, 0, 3, stack=(0,))
|
|
o.call('_nv005825rm', gpu)
|
|
patterns.append(stores.copy())
|
|
assert patterns[0] == patterns[1]
|
|
o.cpu.hook_del(hook)
|
|
lines = []
|
|
for at, width, value in patterns[0]:
|
|
kind = 'Gpu' if gpu <= value < gpu + 0x6e70 else 'Constant'
|
|
value = value - gpu if kind == 'Gpu' else canonical(value)
|
|
lines.append(f'Store{{offset:{at},width:{width},value:Value::{kind}({value})}},')
|
|
output = Path('target/gp106-pf-cpu')
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
generated = output / 'gp106_pf_metadata.rs'
|
|
generated.write_text(
|
|
'// Original005722/005825; GP106 arch13/impl6, physical PF runtime3.\n'
|
|
'#[rustfmt::skip]\nconst GP106_PF_METADATA: &[Store] = &[\n' + '\n'.join(lines) + '\n];\n')
|
|
tracked = Path('src/nvidia/src/gpu/gp106_pf_metadata.rs')
|
|
if args.write:
|
|
tracked.write_bytes(generated.read_bytes())
|
|
elif not tracked.is_file() or tracked.read_bytes() != generated.read_bytes():
|
|
sys.exit(f'{tracked} differs from {generated}; review the difference or regenerate with --write')
|
|
state = {}
|
|
def allocate(args):
|
|
index = state['allocations']
|
|
state['allocations'] += 1
|
|
if index == state['fail']:
|
|
return 0
|
|
result = state['next']
|
|
state['next'] += (args[0] + 15) & ~15
|
|
return result
|
|
o.install(o.by_name['_nv052687rm'], allocate, replace=True)
|
|
cases = []
|
|
for case in range(8):
|
|
fill = 0xa5 if case < 4 else 0
|
|
state.update(allocations=0, fail=[0, 1, 99, 99][case % 4], next=gpu + 0x10000)
|
|
o.cpu.mem_write(gpu, bytes(0x10000))
|
|
o.cpu.mem_write(gpu + 0x10000, bytes([fill]) * 0x10000)
|
|
o.cpu.mem_write(gpu + 0x20000, bytes((case + i) & 255 for i in range(16)))
|
|
o.call('_nv005722rm', gpu, 0x13, 6, 0, 0, 3, stack=(0,))
|
|
status = o.status(o.call('_nv003368rm', gpu, case, case * 17,
|
|
gpu + 0x20000 if case & 2 else 0, gpu + 0x30000))
|
|
image = normalize(o.cpu.mem_read(gpu, 0x6e70), gpu)
|
|
image += o.cpu.mem_read(gpu + 0x10000, 0xcc)
|
|
(output / f'pf-{case}.bin').write_bytes(image)
|
|
cases.append(dict(case=case, status=status, allocations=state['allocations']))
|
|
# Full root GpuArch construction, using the actual GP106 factors.
|
|
output_pointer = gpu + 0x38000
|
|
state.update(allocations=0, fail=99, next=gpu + 0x30000)
|
|
status = o.call('_nv006506rm', output_pointer, 0, 0, 0x13, 6, 0,
|
|
stack=(0, 0x13, 6, 0, 0))
|
|
assert status == 0 and o.q(output_pointer) == gpu + 0x30000
|
|
arch = normalize(o.cpu.mem_read(gpu + 0x30000, 0x98), gpu)
|
|
(output / 'arch.bin').write_bytes(arch)
|
|
labels = [int.from_bytes(arch[at:at + 8], 'little') for at in (0x68, 0x70, 0x78, 0x80)]
|
|
callbacks = []
|
|
for at in (0x68, 0x70, 0x78, 0x80):
|
|
address = o.q(gpu + 0x30000 + at)
|
|
callbacks.append(dict(offset=at, function=o.function_at(address), value=o.call(address, gpu + 0x30000)))
|
|
Path('target/gp106-reports').mkdir(parents=True, exist_ok=True)
|
|
Path('target/gp106-reports/gp106-pf-cpu-original.json').write_text(json.dumps(dict(
|
|
original_sha256=o.DIGEST, gpu=gpu, ordered_stores=len(lines), cases=cases,
|
|
architecture_labels=labels, architecture_callbacks=callbacks,
|
|
evidence='E: full native CPU constructors; memory allocation/copy/diagnostics fixtures; no GPU access',
|
|
), indent=2) + '\n')
|
|
print(f'GP106 PF CPU: {len(lines)} metadata stores, {len(cases)} complete constructors and GpuArch')
|
|
|
|
if __name__ == '__main__':
|
|
main()
|