#!/usr/bin/env python3 """Generate a transcript from the original legacy PMU allocator instructions.""" import hashlib from _binary_path import resolve_580_binary from pathlib import Path import random import struct from elftools.elf.elffile import ELFFile from unicorn import UC_ARCH_X86, UC_HOOK_CODE, UC_MODE_64, Uc from unicorn.x86_const import ( UC_X86_REG_RAX, UC_X86_REG_RBX, UC_X86_REG_RBP, UC_X86_REG_RCX, UC_X86_REG_RDI, UC_X86_REG_RDX, UC_X86_REG_RIP, UC_X86_REG_RSI, UC_X86_REG_RSP, UC_X86_REG_R8, UC_X86_REG_R9, ) ROOT = Path(__file__).resolve().parents[1] ORIGINAL = resolve_580_binary() DIGEST = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" TEXT, DATA, STACK, STOP = 0x10000000, 0x30000000, 0x40000000, 0x50000000 INDIRECT = STOP + 0x100 INDIRECT_RBX = STOP + 0x110 INDIRECT_RDX = STOP + 0x120 INDIRECT_R9 = STOP + 0x130 OS_WORK_QUEUE = STOP + 0x4a0 CHIP, BINDINGS, POOL, CALL, REQUEST = (DATA + i * 0x2000 for i in range(5)) NAMES = ("_nv006035rm", "_nv052503rm", "_nv052242rm", "_nv052004rm", "_nv055426rm", "_nv052002rm", "_nv052414rm", "_nv052162rm", "_nv005876rm", "_nv028457rm", "_nv028460rm", "_nv028551rm", "_nv028576rm", "_nv028577rm", "_nv028672rm", "_nv028674rm", "_nv052510rm", "_nv028510rm", "_nv028663rm", "_nv006031rm", "_nv006032rm", "_nv052008rm", "_nv052021rm", "_nv052215rm", "_nv051854rm", "_nv052635rm", "_nv015256rm", "_nv049819rm", "_nv052478rm", "_nv017897rm", "_nv052479rm", "_nv052055rm", "_nv015279rm", "_nv049781rm", "_nv051847rm", "_nv059880rm", "_nv028435rm") QUEUE_NAMES = ( "_nv028631rm", "_nv028633rm", "_nv013400rm", "_nv013394rm", "_nv013412rm", "_nv013405rm", "_nv013384rm", "_nv013404rm", "_nv013396rm", "_nv013397rm", "_nv013420rm", "_nv013424rm", "_nv028405rm", "_nv028407rm", "_nv028409rm", "_nv028452rm", "_nv028644rm", "_nv013398rm", "_nv013399rm", "_nv013410rm", "_nv013402rm", "_nv013408rm", "_nv013422rm", "_nv028585rm", "_nv028589rm", "_nv028591rm", "_nv028450rm", "_nv028473rm", ) SEQUENCE_NAMES = ("_nv052061rm", "_nv052441rm", "_nv028648rm", "_nv028646rm", "_nv028653rm", "_nv028656rm") assert hashlib.sha256(ORIGINAL.read_bytes()).hexdigest() == DIGEST with ORIGINAL.open("rb") as stream: elf = ELFFile(stream) symbols = list(elf.get_section_by_name(".symtab").iter_symbols()) by_name = {symbol.name: symbol for symbol in symbols} # Local symbols also exist in the older PMU object. Select the same # 0x56–57xxxx Falcon implementation as the GV100 DMEM queue constructor. queue_symbols = {symbol.name: symbol for symbol in symbols if symbol.name in QUEUE_NAMES and 0x560000 <= symbol["st_value"] < 0x580000} assert set(queue_symbols) == set(QUEUE_NAMES) by_name.update(queue_symbols) text = bytearray(elf.get_section_by_name(".text").data()) text_section = elf.get_section_index(".text") rodata_section = elf.get_section_index(".rodata") ranges = [(by_name[name]["st_value"], by_name[name]["st_size"]) for name in (*NAMES, *QUEUE_NAMES, *SEQUENCE_NAMES)] for relocation in elf.get_section_by_name(".rela.text").iter_relocations(): offset = relocation["r_offset"] if not any(base <= offset < base + size for base, size in ranges): continue target = symbols[relocation["r_info_sym"]] if target.name == "__x86_indirect_thunk_rax": address = INDIRECT elif target.name == "__x86_indirect_thunk_rbx": address = INDIRECT_RBX elif target.name == "__x86_indirect_thunk_rdx": address = INDIRECT_RDX elif target.name == "__x86_indirect_thunk_r9": address = INDIRECT_R9 elif target.name == "os_queue_work_item": address = OS_WORK_QUEUE else: section = target["st_shndx"] assert section in (text_section, rodata_section), target.name address = (TEXT if section == text_section else 0x54000000) + target["st_value"] value = address + relocation["r_addend"] if relocation["r_info_type"] in (2, 4): value -= TEXT + offset else: assert relocation["r_info_type"] == 11 struct.pack_into("> 1])) for initialized, faulted, ready_on, expires_on in ( (1, 0, 0, 2), (0, 0, 1, 2), (1, 1, 2, 2), (0, 0, 3, 2), (0, 0, 4, 2), (0, 1, 4, 3), ): cpu.mem_write(FALCON + 0x583, bytes([initialized])) cpu.mem_write(PROVIDER + 0x48, bytes([faulted])) events.clear() service_calls = clock_calls = 0 result = call("_nv051854rm", GPU, WAIT_OBJECT, finite) inputs = (finite, admission, initialized, faulted, ready_on, expires_on, result) rows.append(" ".join(f"{value:x}" for value in inputs) + ";" + " ".join(events)) (ROOT / "target/pmu-wait-original.txt").write_text("\n".join(rows) + "\n") print(f"{len(rows)} original PMU bring-up wait cases") def command_predicate(emu, address, size, unused): events.append("predicate") stub_diagnostic(emu, address, size, unused) emu.reg_write(UC_X86_REG_RAX, int(service_calls >= ready_on)) predicate = STOP + 0x7c0 cpu.hook_add(UC_HOOK_CODE, command_predicate, begin=predicate, end=predicate) rows = [] for wait_line in (0xc3, 0x203, 0x3d7): for ready_on, expires_on in ((0, 2), (1, 2), (2, 1), (3, 1), (3, 4), (5, 3)): events.clear() service_calls = clock_calls = 0 result = call("_nv052635rm", GPU, PMU, predicate, CALL, REQUEST, 0, wait_line) assert result in (0, 0x65) rows.append(f"{wait_line:x} {ready_on:x} {expires_on:x} {result:x};" + " ".join(events)) (ROOT / "target/pmu-command-wait-original.txt").write_text("\n".join(rows) + "\n") print(f"{len(rows)} original PMU command wait cases") # Real deferral decision, cursor comparison and worker flag release. POWER = DATA + 0x1a000 GPU_OS, WORK_STORAGE, WORK_QUEUE = POWER + 0x1000, POWER + 0x1800, POWER + 0x1a00 GPU_NUMBER = 3 HEAD, TAIL = 0x10a4c8, 0x10a4cc REASON, WORK_DISPATCH = 0x10203040, STOP + 0x350 cpu.mem_write(GPU + 0x2100, struct.pack("> index) & 1) + bytes(0x4c)) cpu.mem_write(TRANSPORT + 0x5c, struct.pack("= size: continue for cursor, old_tail in ((base, base), (base + 4, base + 4), (base, base + size - 4)): for is_reply in (False, True): if is_reply and frame_size < 22: continue message = bytearray([2, frame_size, 0, 0] + list(range(4, frame_size))) if is_reply: message[5:7] = bytes([0xf, 4]) message[20:22] = bytes([63, 1]) head = cursor + aligned runtime_dmem[old_tail:old_tail + 4] = bytes([0, 4, 0, 0]) runtime_dmem[cursor:cursor + frame_size] = message irq_registers = {0x10a4c8: head, 0x10a4cc: old_tail} queue = QUEUES + 2 * 0xd0 cpu.mem_write(CALL, struct.pack("