Files
nvdriver/tools/gp106/protected_layout.py

83 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Replay original GP106 placement with console and retained firmware holes."""
import itertools
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 regions import read_regions
def encoded(rows):
return ' '.join(','.join(f'{v:x}' for v in row) for row in rows)
def main():
o.TEMPLATE = o.build_template(0x20, 0x136000a1, 0x136a1000)
supported = o.SERVICE + 0x850
o.install(supported, lambda a: 1)
cases = []
holes = ((), ((0x40000000, 0x42000000),), ((0x160000000, 0x161000000),),
((0x40000000, 0x42000000), (0x160000000, 0x161000000)))
for console, retained, attribute in itertools.product((0, 0x400000, 0x4000000), holes,
(0, 0x400000)):
o.reset(dict(registers={0x100ce0: 0x6a, 0x12006c: 6,
0x17e3f4: 0x401c0002, 0x2a78: 0x1000},
registry={}, sizes={'attribute': attribute},
console_feature=1, console=(o.MASK, console)))
o.wq(o.MEMSYS + 0xc50, supported)
# These GP106 producers execute rather than supplying zero size fixtures.
for offset, function in ((0x310, '_nv043958rm'), (0x318, '_nv043978rm'),
(0x1d8, '_nv043943rm')):
o.wq(o.MEMMGR + offset, o.by_name[function])
o.wq(o.MMU + 0x128, o.by_name['_nv040795rm'])
values = []
o.stage_regions(values)
for start, end in retained:
descriptor = o.allocate(0x28)
o.wq(descriptor, start)
o.wq(descriptor + 8, end - 1)
o.wq(descriptor + 16, end - start)
o.wb(descriptor + 0x18, 1)
o.wb(descriptor + 0x22, 1)
o.wb(descriptor + 0x23, 1)
o.call('_nv044000rm', o.GPU, o.MEMMGR, descriptor)
o.call('_nv044071rm', o.GPU, o.MEMMGR)
before = read_regions()
pma = o.cpu.mem_read(o.MEMMGR + 0x41b, 1)[0]
region_policy = o.cpu.mem_read(o.MEMMGR + 0x41a, 1)[0]
for stage in o.STAGES[1:-1]:
stage(values)
top = o.call(o.q(o.MEMMGR + 0x1f8), o.GPU, o.MEMMGR)
o.call(o.q(o.MEMMGR + 0x1e0), o.GPU, o.MEMMGR)
whole, start = o.q(o.MEMMGR + 0x830) << 20, o.q(o.MEMMGR + 0x480)
mark = len(o.state['events'])
o.checked('heap', o.call('_nv035551rm', o.GPU, o.HEAPOBJ, start,
whole - min(top, whole) - start, 0, 0, stack=(0,)), mark)
# A console quarter may also be internal, but remains protected.
internal = [r for r in read_regions() if r[8] and not r[3] and not r[7]]
assert len(internal) == 1
values += [internal[0][0], internal[0][1] + 1]
cases.append(' '.join(f'{v:x}' for v in (attribute, pma, region_policy)) + ';' +
encoded(before) + ';' + ' '.join(f'{v:x}' for v in values) + ';' +
encoded(read_regions()))
output = Path('target/gp106-memory/protected-layout.txt')
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text('\n'.join(cases) + '\n')
Path('target/gp106-reports').mkdir(parents=True, exist_ok=True)
Path('target/gp106-reports/gp106-protected-layout-original.json').write_text(json.dumps(dict(
original_sha256=o.DIGEST, evidence='E: original GP106 instructions under Unicorn',
cases=len(cases), oracle=str(output),
scope='Console and retained firmware descriptors, reserved region and internal heap '
'placement; original runlist/USERD/context/fault size producers',
fixture_supplied='Console callback, retained firmware ranges, GR attribute bytes '
'and register values; no actual console detach or GPU ownership',
), indent=2) + '\n')
print(f'GP106: {len(cases)} original protected/console layout cases')
if __name__ == '__main__':
main()