mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
160 lines
7.1 KiB
Python
160 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Native five-level Falcon walker removal with private descriptor backings."""
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
import check_gpu_memory_placement_binary as o
|
|
|
|
|
|
def main():
|
|
o.TEMPLATE = o.build_template(0x20, 0x136000a1, 0x136a1000)
|
|
o.reset(dict(registers={}, registry={}, sizes={}))
|
|
fmt = o.q(o.q(o.MMU + 0x4b0) + 0x520)
|
|
root_format = o.q(fmt + 8)
|
|
space, context, holder = [o.allocate(0x100) for _ in range(3)]
|
|
o.wq(context, space)
|
|
o.wq(context + 8, o.GPU)
|
|
o.wq(context + 16, holder)
|
|
o.wq(holder, fmt)
|
|
walker, callbacks = o.allocate(0x100), o.allocate(0x40)
|
|
o.wq(walker, context)
|
|
o.wq(walker + 8, callbacks)
|
|
levels, buffers, descriptors = [], {}, {}
|
|
serial = [0]
|
|
def stub(callback):
|
|
address = o.STOP + 0xc00 + 16 * serial[0]
|
|
serial[0] += 1
|
|
o.cpu.mem_write(address, b'\xc3')
|
|
o.install(address, callback, replace=True)
|
|
return address
|
|
|
|
def existing(a):
|
|
# Existing table: the allocator callback cannot resize or replace it.
|
|
assert o.q(a[5]) in descriptors
|
|
return 0
|
|
|
|
def release(a):
|
|
o.emit(f'release {descriptors[a[3]]} 0')
|
|
return 0
|
|
|
|
def root_changed(a):
|
|
assert a[2] == 0
|
|
o.emit('root 0 0')
|
|
return 1
|
|
|
|
o.wq(callbacks, stub(existing))
|
|
o.wq(callbacks + 8, stub(release))
|
|
o.wq(callbacks + 16, stub(root_changed))
|
|
o.wq(callbacks + 24, o.by_name['_nv013512rm'])
|
|
o.wq(callbacks + 32, o.by_name['_nv013503rm'])
|
|
|
|
def build(level, level_desc, parent, depth):
|
|
low, high, entry, _, children = o.cpu.mem_read(level, 5)
|
|
o.wq(level_desc, level)
|
|
o.wq(level_desc + 8, parent)
|
|
child_descriptors = o.allocate(0x68 * children) if children else 0
|
|
o.wq(level_desc + 16, child_descriptors)
|
|
node, descriptor, backing = [o.allocate(0x1000) for _ in range(3)]
|
|
state_bytes = o.allocate(1 << (high - low + 1))
|
|
o.wq(level_desc + 24, node)
|
|
o.wq(node + 8, 0)
|
|
o.wq(node + 16, (1 << (high + 1)) - 1)
|
|
o.wq(node + 0x38, descriptor)
|
|
o.wd(node + 0x40, 0x1000)
|
|
o.wq(node + 0x58, state_bytes)
|
|
levels.append((level, node, state_bytes, level_desc, low, high, entry))
|
|
buffers[descriptor] = backing
|
|
descriptors[descriptor] = depth
|
|
if children:
|
|
o.wd(node + 0x44, 1)
|
|
o.wb(state_bytes, 3)
|
|
# GP106 dual PD0 has an absent big-page table and an owned small
|
|
# table. Include the real big format/parent but no interval node.
|
|
for index in range(children):
|
|
child_format = o.q(level + 16) + 24 * index
|
|
child_desc = child_descriptors + 0x68 * index
|
|
if index == children - 1:
|
|
build(child_format, child_desc, level_desc, depth + 1)
|
|
else:
|
|
o.wq(child_desc, child_format)
|
|
o.wq(child_desc + 8, level_desc)
|
|
return node
|
|
|
|
build(root_format, walker + 0x18, 0, 0)
|
|
assert [(r[4], r[5], r[6]) for r in levels] == [
|
|
(47, 48, 8), (38, 46, 8), (29, 37, 8), (21, 28, 16), (12, 20, 8)]
|
|
def mapping(a):
|
|
descriptor, offset = o.q(a[1]), o.q(a[1] + 8)
|
|
assert descriptor in buffers
|
|
o.emit(f'clear {descriptors[descriptor]:x} {offset:x} {a[2]:x}')
|
|
return buffers[descriptor] + offset
|
|
|
|
o.install(o.by_name['_nv044025rm'], mapping, replace=True)
|
|
o.install(o.by_name['_nv044031rm'], lambda a: 0, replace=True)
|
|
o.install(o.by_name['_nv052701rm'], lambda a: 0, replace=True)
|
|
for name in ('_nv047869rm', '_nv046745rm', '_nv046747rm'):
|
|
o.install(o.by_name[name], lambda a: (o.emit('diagnostic ' + name), 0)[1], replace=True)
|
|
output = Path('target/gp106-sec2-unmap')
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
rows = []
|
|
# Each run consumes its walker. Rebuild state and single-node trees before
|
|
# the next run; descriptors/backings are fixture-owned private allocations.
|
|
for pages in (1, 5, 511):
|
|
for depth, (_, node, states, ld, low, high, _) in enumerate(levels):
|
|
o.cpu.mem_write(node, bytes(0x60))
|
|
descriptor = next(d for d, n in descriptors.items() if n == depth)
|
|
o.wq(node + 8, 0)
|
|
o.wq(node + 16, (1 << (high + 1)) - 1)
|
|
o.wq(node + 0x38, descriptor)
|
|
o.wd(node + 0x40, 0x1000)
|
|
o.wd(node + 0x44, pages if depth == 4 else 1)
|
|
o.wd(node + 0x4c, 1 if depth == 0 else 0)
|
|
o.wq(node + 0x58, states)
|
|
o.wq(ld + 24, node)
|
|
o.cpu.mem_write(states, bytes(1 << (high - low + 1)))
|
|
if depth == 4:
|
|
o.cpu.mem_write(states + 1, bytes([2]) * pages)
|
|
else:
|
|
o.wb(states, 0x83 if depth == 0 else 3)
|
|
o.cpu.mem_write(buffers[descriptor], bytes([0xa5]) * 0x1000)
|
|
o.state['events'] = []
|
|
result = o.status(o.call('_nv046261rm', walker, 0x1000, (pages + 1) * 0x1000 - 1))
|
|
assert result == 0, result
|
|
unmap_events = list(o.state['events'])
|
|
assert not any('diagnostic' in e for e in unmap_events), unmap_events
|
|
expected = {depth: bytearray([0xa5]) * 0x1000 for depth in descriptors.values()}
|
|
for operation in unmap_events:
|
|
if operation.startswith('clear '):
|
|
_, depth, offset, length = operation.split()
|
|
backing = expected[int(depth, 16)]
|
|
start, size = int(offset, 16), int(length, 16)
|
|
assert start + size <= len(backing), operation
|
|
backing[start:start + size] = bytes(size)
|
|
for descriptor, depth in descriptors.items():
|
|
assert o.cpu.mem_read(buffers[descriptor], 0x1000) == expected[depth], (pages, depth)
|
|
assert o.d(levels[0][1] + 0x4c) == 1
|
|
assert o.q(walker + 0x30) == levels[0][1]
|
|
# Native root-format unpin spans one complete root entry (2^47).
|
|
o.state['events'] = []
|
|
assert o.call('_nv046251rm', walker, root_format, 0, (1 << 47) - 1) == 0
|
|
unpin_events = list(o.state['events'])
|
|
assert not any('diagnostic' in e for e in unpin_events), unpin_events
|
|
assert o.q(walker + 0x30) == 0
|
|
(output / f'unmap-{pages}.txt').write_text('\n'.join(unmap_events + unpin_events) + '\n')
|
|
rows.append(dict(pages=pages, unmap=unmap_events, unpin=unpin_events))
|
|
Path('target/gp106-reports').mkdir(parents=True, exist_ok=True)
|
|
Path('target/gp106-reports/gp106-sec2-unmap-original.json').write_text(json.dumps(dict(
|
|
original_sha256=o.DIGEST, evidence='E: native Unicorn walker/clear algorithms',
|
|
cases=rows, scope='046261/046250/014127/014122 and original 013503 clear writer, '
|
|
'then 046251 root unpin. Existing single-path nodes, root pin and page states '
|
|
'are CPU fixtures; descriptor backing allocation/map/release are services. '
|
|
'No VM range bookkeeping, TLB publication or GPU access.',
|
|
), indent=2) + '\n')
|
|
print(f'GP106 SEC2 page removal: {len(rows)} original traces')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|