Files
nvdriver/tools/gp106/sec2.py

238 lines
12 KiB
Python

#!/usr/bin/env python3
"""GP106 SEC2 instance/format oracle; original instructions, no GPU access."""
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
from unicorn import UC_HOOK_MEM_WRITE
from unicorn.x86_const import UC_X86_REG_R14, UC_X86_REG_RBX
def main():
o.TEMPLATE = o.build_template(0x20, 0x136000a1, 0x136a1000)
o.reset(dict(registers={}, registry={}, sizes={}))
o.call('_nv005825rm', o.GPU)
assert o.call('_nv030827rm', o.GPU) == 0
locations = [o.d(o.GPU + 0x5300 + i * 4) for i in range(4)]
assert locations == [0x30033030, 0, 0xc00000, 0]
# Execute the actual instance requester; only allocation and framebuffer
# fill are CPU service boundaries. Placement override registry is absent.
o.install(o.by_name['_nv043691rm'], lambda a: 0, replace=True)
o.install(o.by_name['_nv044030rm'], lambda a: 0, replace=True)
requester = o.allocate(0x1000)
assert o.call('_nv028536rm', o.GPU, requester) == 0
holder = o.q(requester + 0x618)
assert (o.q(holder), o.d(holder + 0x10), o.d(holder + 0x14)) == (0x1000, 2, 2)
bindings = {hex(s): o.function_at(o.q(o.MMU + s))
for s in (0x130, 0x138, 0x140, 0x148)}
assert bindings == {'0x130': '_nv040831rm', '0x138': '_nv040840rm',
'0x140': '_nv040833rm', '0x148': '_nv040837rm'}
fmt = o.q(o.q(o.MMU + 0x4b0) + 0x520)
level = o.q(fmt + 8)
levels = []
while True:
low, high, entry, table, children = o.cpu.mem_read(level, 5)
levels.append([low, high, entry])
if not children:
break
level = o.q(level + 0x10) + 0x18 * (children - 1)
assert levels == [[47, 48, 8], [38, 46, 8], [29, 37, 8], [21, 28, 16], [12, 20, 8]]
level_addresses = []
level = o.q(fmt + 8)
while True:
level_addresses.append(level)
children = o.cpu.mem_read(level + 4, 1)[0]
if not children:
break
level = o.q(level + 0x10) + 0x18 * (children - 1)
serial = [0]
def service(handler):
address = o.STOP + 0xc00 + 16 * serial[0]
serial[0] += 1
o.cpu.mem_write(address, b'\xc3')
o.install(address, handler, replace=True)
return address
space, table, root, instance, params = (o.allocate(n) for n in
(0x1000, 0x100, 0x1000, 0x1000, 0x20))
o.wq(space, table)
o.wq(table + 0x60, service(lambda a: 0x20000fff))
o.wq(table + 0xa0, service(lambda a: 0x10000))
o.wq(table + 0xc0, service(lambda a: 0))
o.wq(table + 0xd0, service(lambda a: root))
o.wq(table + 0xd8, service(lambda a: root))
o.install(o.by_name['_nv034576rm'], lambda a: fmt, replace=True)
o.install(o.by_name['_nv043734rm'], lambda a: 0, replace=True)
o.install(o.by_name['_nv043705rm'], lambda a: 2, replace=True)
o.install(o.by_name['_nv044028rm'], lambda a: instance, replace=True)
o.install(o.by_name['_nv044029rm'], lambda a: 0, replace=True)
o.wd(o.GPU + 0x915, 1)
o.cpu.mem_write(params, bytes(0x20))
# Skip only the post-store cache service. The parameter does not change
# any of the VA-limit/PDB stores whose exact order is recorded below.
o.wb(params + 2, 1)
physical = [0]
o.install(o.by_name['_nv043721rm'], lambda a: physical[0], replace=True)
output = Path('target/gp106-sec2')
output.mkdir(parents=True, exist_ok=True)
rows = []
writes = []
hook = o.cpu.hook_add(UC_HOOK_MEM_WRITE,
lambda cpu, access, address, size, value, data:
writes.append([address - instance, size, value])
if instance <= address < instance + 0x1000 else None)
for index, address in enumerate((0x180000, 0x17fff0000, (1 << 37) - 0x1000)):
physical[0] = address
o.cpu.mem_write(instance, bytes(0x1000))
writes.clear()
assert o.call('_nv040832rm', o.MMU, root, space, 0xffffffff, params) == 0
assert writes == [[0x208, 8, 0x20000fff], [0x204, 4, address >> 32],
[0x200, 4, (address & 0xfffff000) | 0xc00]], writes
assert not [e for e in o.state['events'] if e.startswith('A:')]
(output / f'instance-{index}.bin').write_bytes(o.cpu.mem_read(instance, 0x1000))
rows.append(dict(root=address, writes=list(writes)))
o.cpu.hook_del(hook)
# The RM directory callback supplies its actual 8/16-byte entry to the
# memory-copy service. Only descriptor address/cache and the CPU copy are
# modeled; the format masks and entry construction are original code.
context, fmt_holder, children, child = (o.allocate(n) for n in (0x40, 0x40, 16, 0x1000))
o.wq(context, space)
o.wq(context + 8, o.GPU)
o.wq(context + 0x10, fmt_holder)
o.wq(fmt_holder, fmt)
o.install(o.by_name['_nv043729rm'], lambda a: physical[0], replace=True)
directory_entries = []
copied = []
def copy_entry(args):
copied.append(bytes(o.cpu.mem_read(args[2], args[3])).hex())
return 0
o.install(o.by_name['_nv044058rm'], copy_entry, replace=True)
for number, level in enumerate(level_addresses[:-1]):
o.wq(children, 0 if number == 3 else child)
o.wq(children + 8, child if number == 3 else 0)
o.wq(child + 0x30, 0x1000)
for address in (0x180000, 0x17fff0000, (1 << 37) - 0x1000):
physical[0] = address
copied.clear()
assert o.call('_nv013512rm', context, level, root, 0, children) == 1
expected = ((address >> 12) << 8 | 2).to_bytes(8, 'little').hex()
assert copied == [('00' * 8 if number == 3 else '') + expected], copied
directory_entries.append(dict(level=number, child=address, entry=copied[0]))
assert not [e for e in o.state['events'] if e.startswith('A:')], o.state['events']
# Execute the original HS descriptor construction with prepared firmware
# supplied at the already-tested unpack/signature boundary. Capture at
# Falcon+248, before any reset, IMEM, DMEM or DMA execution.
acr, sec2, interface, falcon, lpwr, misc = (o.allocate(0x2000) for _ in range(6))
image, layout, mapped, image_desc = (o.allocate(n) for n in (0x10000, 0x100, 0x10000, 0x1000))
o.call('_nv005947rm', acr, o.CHIP, o.RM)
o.call('_nv005809rm', acr, o.CHIP, o.RM)
o.wq(o.GPU + 0x22c8, lpwr)
o.wq(o.GPU + 0x1e70, misc)
o.wq(o.GPU + 0x2308, sec2)
o.wq(sec2 + 0x1b8, interface)
o.wq(interface + 0x18, falcon)
o.wq(acr + 0x110, service(lambda a: 0))
o.wq(acr + 0x158, service(lambda a: 0))
o.wq(acr + 0x1c0, service(lambda a: 0))
o.install(o.by_name['_nv016965rm'], lambda a: 0, replace=True)
o.install(o.by_name['_nv016748rm'], lambda a: 0, replace=True)
def prepared(args):
o.wd(args[3], 0x4300)
o.wq(args[3] + 8, image)
o.wq(args[3] + 0x30, layout)
return 0
o.install(o.by_name['_nv016905rm'], prepared, replace=True)
def descriptor(args):
o.wq(args[0], image_desc)
return 0
o.install(o.by_name['_nv043694rm'], descriptor, replace=True)
for name in ('_nv043691rm', '_nv043773rm', '_nv043704rm', '_nv043699rm'):
o.install(o.by_name[name], lambda a: 0, replace=True)
o.install(o.by_name['_nv043739rm'], lambda a: mapped, replace=True)
captured = []
o.wq(falcon + 0x248, service(lambda a: captured.append(a[2]) or 0))
# The layout is the actual GP106 HS record extracted independently.
layout_bytes = Path('target/gp106-acr/layout.bin').read_bytes()
o.cpu.mem_write(layout, layout_bytes)
assert o.call('_nv016741rm', o.GPU, acr, 1, 0) == 0
assert len(captured) == 1
desc = captured[0]
before = bytes(o.cpu.mem_read(desc, 0x54))
assert int.from_bytes(before[0x20:0x24], 'little') == 1
# Execute only the original single-mapping VA-fixup basic blocks. The
# enclosing function's reset/launch/teardown is deliberately not executed.
o.wq(falcon + 0x630, 0x1000)
o.wb(falcon + 0x638, 0)
o.cpu.reg_write(UC_X86_REG_RBX, falcon)
o.cpu.reg_write(UC_X86_REG_R14, desc)
entry = o.by_name['_nv028470rm']
o.cpu.emu_start(entry + 0x41b, entry + 0x452, count=100)
descriptor_bytes = bytes(o.cpu.mem_read(desc, 0x54))
assert int.from_bytes(descriptor_bytes[0x24:0x2c], 'little') == 0x1000
assert int.from_bytes(descriptor_bytes[0x40:0x48], 'little') == 0x1000 + int.from_bytes(layout_bytes[8:12], 'little')
(output / 'dma-descriptor.bin').write_bytes(descriptor_bytes)
assert not [e for e in o.state['events'] if e.startswith('A:')], o.state['events']
# Address insertion through the original mapping writer. The ordinary
# valid/cached/RW/atomic PTE template is an explicit fixture here; this
# does not claim to execute all _nv026096rm mapping-policy branches.
pte_state, node, table_bytes, count, source = (o.allocate(n) for n in (0x100, 0x40, 0x1000, 8, 0x100))
o.wq(node, level_addresses[-1])
o.wq(node + 8, pte_state)
o.wq(node + 0x18, 0x1000)
o.wq(pte_state, fmt)
o.wd(pte_state + 8, 2)
o.wq(pte_state + 0x10, source)
o.wq(pte_state + 0x60, 1)
o.wq(pte_state + 0x70, o.call('_nv029455rm', o.q(fmt + 0x20), 1))
o.wb(pte_state + 0x7d, 1)
o.wq(pte_state + 0x80, table_bytes)
o.wd(source + 0xc, 1)
o.install(o.by_name['_nv026114rm'], lambda a: physical[0] + a[1] * 0x1000, replace=True)
ptes = []
for address in (0x180000, 0x17fff0000, (1 << 37) - 0x5000):
physical[0] = address
o.wd(pte_state + 0x18, 0)
o.cpu.mem_write(table_bytes, bytes(0x1000))
o.call('_nv013508rm', context, node, 1, 5, count)
entries = [o.q(table_bytes + i * 8) for i in range(5)]
assert entries == [((address + i * 0x1000) >> 12) << 8 | 1 for i in range(5)], entries
ptes.append(dict(address=address, entries=entries))
for index in range(3):
pages = bytearray(6 * 0x1000)
pages[:0x1000] = (output / f'instance-{index}.bin').read_bytes()
for number, level in enumerate(level_addresses[:-1]):
physical[0] = [0x90000, 0xa0000, 0xb0000, 0xc0000][number]
o.wq(children, 0 if number == 3 else child)
o.wq(children + 8, child if number == 3 else 0)
copied.clear()
assert o.call('_nv013512rm', context, level, root, 0, children) == 1
value = bytes.fromhex(copied[0])
at = (number + 1) * 0x1000
pages[at:at + len(value)] = value
physical[0] = 0x1000000
o.wd(pte_state + 0x18, 0)
o.call('_nv013508rm', context, node, 1, 5, count)
pages[0x5008:0x5030] = o.cpu.mem_read(table_bytes, 40)
(output / f'pages-{index}.bin').write_bytes(pages)
assert not [e for e in o.state['events'] if e.startswith('A:')], o.state['events']
Path('target/gp106-reports').mkdir(parents=True, exist_ok=True)
Path('target/gp106-reports/gp106-sec2-original.json').write_text(json.dumps(dict(
evidence='E: original 580.178.04 GP106 binder and instance instructions under Unicorn',
original_sha256=o.DIGEST, bindings=bindings, levels=levels, instances=rows,
location_words=locations, instance_request=dict(bytes=4096, space=2, cache=2),
directories=directory_entries,
ptes=ptes,
dma_descriptor_defined_bytes=descriptor_bytes[0x20:].hex(),
fixtures='CPU allocation, descriptor address/aperture/volatility and address-space getters; '
'post-store cache service skipped explicitly; no MMIO, DMA or authentication',
), indent=2) + '\n')
print(f'GP106 SEC2: {len(rows)} instance/page images, {len(directory_entries)} directory entries, '
f'{len(ptes) * 5} PTEs and HS DMA descriptor; original instructions only')
if __name__ == '__main__':
main()