Files
nvdriver/tools/oracle_display_advanced_e2e.py

717 lines
26 KiB
Python

#!/usr/bin/env python3
"""Standalone x86_64 Unicorn binary oracle & E2E Boot-to-KMS harness for Advanced Display.
Verifies exact bit-for-bit parity against 580.178.04 `nv-kernel.o_binary` for:
1. DP-MST stream & SF_DP_STREAM_CTL register RMW (`_nv057193rm`, `_nv025343rm`)
2. VRR / G-Sync timing, stall-lock packing & RG raster vtotal (`_nv025573rm`, `_nv025557rm`, `_nv035189rm`)
3. HDR10 HDMI enable & CTA-861.3 DRM (0x87) InfoFrame (`_nv025538rm`, `_nv025553rm`)
4. Multi-Head SLI / Mosaic Framelock (`_nv022997rm`, `_nv031431rm`, `_nv022998rm`) —
verifying Quadro brand gate is UNLOCKED in clean-room Rust driver
5. `.rodata` NV0073 & NV30F1 control table parity
6. End-to-end synthetic Boot-to-KMS pipeline exercising real Rust code (`e2e_boot_to_kms_full_pipeline`).
Exports `run_checks()` returning a list of `{"name": ..., "passed": bool, "diffs": [...]}` dicts.
"""
from __future__ import annotations
import json
import os
import struct
import subprocess
import sys
from pathlib import Path
from elftools.elf.elffile import ELFFile
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tools"))
from rmapi_binary_emulator import Emulator, ORIGINAL # noqa: E402
NV_OK = 0x0000
NV_ERR_INVALID_ARGUMENT = 0x001F
NV_ERR_INVALID_OPERATION = 0x0053
NV_ERR_NOT_SUPPORTED = 0x0056
def _stub_noop(emu: Emulator, name: str, ret: int = 0):
if name in emu.handlers:
return
if name in emu.by_name or name in emu.externals:
emu.install(name, lambda _args: ret)
def _install_common(emu: Emulator):
for fn in ("_nv047869rm", "_nv047868rm", "_nv046745rm", "_nv046751rm", "_nv029892rm"):
_stub_noop(emu, fn, 0)
if "_nv052695rm" in emu.by_name and "_nv052695rm" not in emu.handlers:
def _memcpy_s(args):
dst, dst_sz, src, n = args[0], args[1] & 0xFFFFFFFF, args[2], args[3] & 0xFFFFFFFF
k = min(dst_sz, n)
if k:
emu.write(dst, emu.read(src, k))
return dst
emu.install("_nv052695rm", _memcpy_s)
def _make_bar0_io(emu: Emulator):
regs: dict[int, int] = {}
writes: list[tuple[int, int]] = []
rd_stub = emu.allocate(16)
wr_stub = emu.allocate(16)
emu.install(rd_stub, lambda args: regs.get(args[1] & 0xFFFFFFFF, 0))
def _wr(args):
off = args[1] & 0xFFFFFFFF
val = args[2] & 0xFFFFFFFF
regs[off] = val
writes.append((off, val))
return 0
emu.install(wr_stub, _wr)
vtbl = emu.allocate(0x100)
emu.wq(vtbl + 0x28, rd_stub)
emu.wq(vtbl + 0x40, wr_stub)
io_obj = emu.allocate(0x100)
emu.wq(io_obj, vtbl)
wrap = emu.allocate(0x100)
emu.wq(wrap + 0x50, io_obj)
return regs, writes, wrap
# ---------------------------------------------------------------------------
# Rust mirror models (1:1 ports of display_channels.rs)
# ---------------------------------------------------------------------------
def rust_sf_dp_stream_ctl_rmw(
sor: int,
stream_idx: int,
cur_alloc: int,
cur_slots: int,
cur_ts: int,
cur_linkctl: int,
slot_start: int,
slot_count: int,
timeslice: int,
tu_size: int,
watermark: int,
send_act: bool,
) -> list[tuple[int, int]]:
base = (sor * 0x800) & 0xFFFFFFFF
shift = (stream_idx * 8) & 0x18
clear_mask = (~(0xFF << shift)) & 0xFFFFFFFF
next_alloc = (cur_alloc & clear_mask) | ((slot_start & 0xFF) << shift)
next_slots = (cur_slots & clear_mask) | ((slot_count & 0xFF) << shift)
next_ts = (cur_ts & clear_mask) | ((timeslice & 0xFF) << shift)
tu_bits = (tu_size & 0xF) << 8
wm_bits = (watermark & 0x1F) << 12
if send_act:
next_linkctl = tu_bits | wm_bits | (cur_linkctl & 0xFFFE00FF) | 0x00400000
else:
next_linkctl = tu_bits | wm_bits | (cur_linkctl & 0xFFBE00FF)
return [
(base + 0x61C14C, next_alloc),
(base + 0x61C120, next_slots),
(base + 0x61C118, next_ts),
(base + 0x61C130, next_linkctl),
]
def rust_vrr_ext_ns10(pclk: int, h_total: int, v_min: int, v_max: int) -> int:
return ((h_total * 100000) // pclk) * ((v_max - v_min) & 0xFFFFFFFFFFFFFFFF)
def rust_pack_vrr_head_enable(
head: int, b0: int, b1: int, b2: int, b3: int, b4: int, b5: int, b6: int, b7: int, b8: int
) -> int:
out = (
(head & 7)
| ((b0 & 1) << 3)
| ((b1 & 1) << 4)
| ((b2 & 1) << 5)
| ((b3 & 3) << 6)
| ((b4 & 0x7F) << 8)
)
out |= 0x18000 if b5 else 0x08000
if b6:
out |= 0x40000
if b7:
out |= 0x20000
if b8:
out |= 0x80000
return out
def rust_rg_effective_vtotal(base_vtotal: int, fp: int, bp: int, mult: int) -> int:
ext_fp = ((fp >> 16) & 0x3FFF) if (fp & 0x4000) else 0
ext_bp = ((bp >> 16) & 0x3FFF) if (bp & 0x4000) else 0
if ((mult >> 4) & 0xF) == 1:
ext_fp *= 2
if (mult & 0xF) == 1:
ext_bp *= 2
return (ext_bp + ext_fp + base_vtotal) & 0xFFFFFFFF
def pack_cta861_drm_infoframe(eotf: int, max_lum: int, min_lum: int, max_cll: int, max_fall: int) -> bytes:
p = bytearray(30)
p[0] = 0x87
p[1] = 0x01
p[2] = 26
p[4] = eotf & 0x07
p[5] = 0
struct.pack_into("<HHHHHHHHHH", p, 6, 34000, 16000, 13250, 34500, 7500, 3000, 15635, 16450, max_lum, min_lum)
struct.pack_into("<HH", p, 26, max_cll, max_fall)
p[3] = (-sum(p)) & 0xFF
return bytes(p)
# ---------------------------------------------------------------------------
# Unicorn checks
# ---------------------------------------------------------------------------
def check_dp_mst_stream_ctl_unicorn(emu: Emulator) -> dict:
"""Execute `_nv057193rm` in Unicorn verifying SF_DP_STREAM_CTL & SF_DP_LINKCTL RMW."""
emu.reset()
_install_common(emu)
regs, writes, wrap = _make_bar0_io(emu)
gpu = emu.allocate(0x2000)
disp = emu.allocate(0x4000)
emu.wq(gpu + 0x1F70, disp)
emu.wq(disp + 0x3D38, wrap)
sf_obj = emu.allocate(0x100)
or_base = emu.allocate(0x100)
emu.wq(sf_obj + 0x68, or_base)
diffs = []
cases = 0
vectors = [
(0, 0, 0x11223344, 0x55667788, 0x99AABBCC, 0x80000001, 1, 30, 0x20, 0x0A, 0x15, True),
(1, 2, 0x11223344, 0x55667788, 0x99AABBCC, 0x80000001, 5, 30, 0x33, 0x0A, 0x15, True),
(2, 3, 0xAAAAAAAA, 0xBBBBBBBB, 0xCCCCCCCC, 0x00401234, 31, 32, 0x44, 0x07, 0x0F, False),
(3, 1, 0x00000000, 0xFFFFFFFF, 0x12345678, 0xFF0000FF, 16, 16, 0x10, 0x0F, 0x1F, True),
]
for sor, sidx, ca, cs, ct, cl, start, cnt, ts, tu, wm, act in vectors:
emu.wd(or_base + 0x4C, sor)
base = sor * 0x800
regs[base + 0x61C14C] = ca
regs[base + 0x61C120] = cs
regs[base + 0x61C118] = ct
regs[base + 0x61C130] = cl
writes.clear()
emu.call("_nv057193rm", gpu, sf_obj, 0, sidx, start, cnt, stack=(ts, tu, 1 if act else 0, wm))
exp = rust_sf_dp_stream_ctl_rmw(sor, sidx, ca, cs, ct, cl, start, cnt, ts, tu, wm, act)
cases += 1
if writes != exp:
diffs.append({"sor": sor, "stream": sidx, "orig": list(writes), "rust": exp})
return {
"name": "dp_mst_sf_stream_ctl_unicorn",
"passed": len(diffs) == 0,
"cases": cases,
"diffs": diffs,
}
def check_dp_mst_config_stream_unicorn(emu: Emulator) -> dict:
"""Execute `_nv025343rm` (`NV0073_CTRL_CMD_DP_SET_STREAM_CONFIG`) in Unicorn."""
emu.reset()
_install_common(emu)
disp_common = emu.allocate(0x200)
disp_parent = emu.allocate(0x200)
emu.wq(disp_common + 0x1B8, disp_parent)
gpu = emu.allocate(0x400)
objdisp = emu.allocate(0x2000)
emu.wb(objdisp + 0x1989, 1) # 2H1OR supported
heads = [emu.allocate(0x800) for _ in range(4)]
head_cb_calls: list[tuple[int, int]] = []
head_cb = emu.allocate(16)
emu.install(head_cb, lambda args: (head_cb_calls.append((args[1], args[2] & 0xFFFFFFFF)), 0)[1])
for h in heads:
emu.wq(h + 0x2F0, head_cb)
resolve_subdev = emu.allocate(16)
def _res_sub(args):
emu.wq(args[2], gpu)
emu.wq(args[3], objdisp)
return 0
emu.install(resolve_subdev, _res_sub)
emu.wq(disp_parent + 0x118, resolve_subdev)
emu.install("_nv023420rm", lambda args: heads[args[1] & 0xFFFFFFFF] if (args[1] & 0xFFFFFFFF) < 4 else 0)
params = emu.allocate(84)
diffs = []
cases = 0
# Valid MST stream config on head 0, stream 1
emu.write(params, b"\x00" * 84)
emu.wd(params + 0x04, 0) # head=0
emu.wd(params + 0x08, 2) # sor=2
emu.wd(params + 0x0C, 1) # dp_link=1
emu.wb(params + 0x11, 1) # isMST=1
emu.wd(params + 0x18, 0x120) # hBlankSym
emu.wd(params + 0x1C, 0x240) # vBlankSym
emu.wd(params + 0x28, 1) # slotStart
emu.wd(params + 0x2C, 30) # slotEnd
emu.wd(params + 0x30, 1200) # PBN
emu.wd(params + 0x34, 30) # timeslice
emu.wd(params + 0x3C, 1) # streamIdx=1
emu.wb(params + 0x40, 1) # sendACT=1
head_cb_calls.clear()
rc = emu.call("_nv025343rm", disp_common, params) & 0xFFFFFFFF
cases += 1
got_fields = (
emu.b(heads[0] + 0x662),
emu.d(heads[0] + 0x678 + 0x20),
emu.d(heads[0] + 0x67C + 0x20),
emu.d(heads[0] + 0x680 + 0x20),
emu.d(heads[0] + 0x684 + 0x20),
emu.b(heads[0] + 0x694 + 0x20),
)
if rc != 0 or got_fields != (1, 1, 30, 1200, 30, 1) or head_cb_calls != [(heads[0], 1)]:
diffs.append({"case": "valid_mst", "rc": hex(rc), "fields": got_fields, "cb": head_cb_calls})
# Invalid streamIdx > 1 -> 0x1f
emu.wd(params + 0x3C, 2)
rc_bad_s = emu.call("_nv025343rm", disp_common, params) & 0xFFFFFFFF
cases += 1
if rc_bad_s != NV_ERR_INVALID_ARGUMENT:
diffs.append({"case": "stream_idx_2", "orig": hex(rc_bad_s), "rust": hex(NV_ERR_INVALID_ARGUMENT)})
# Invalid 2H1OR on odd head (head=1) -> 0x1f
emu.wd(params + 0x3C, 0)
emu.wd(params + 0x04, 1)
emu.wb(params + 0x24, 1)
rc_odd = emu.call("_nv025343rm", disp_common, params) & 0xFFFFFFFF
cases += 1
if rc_odd != NV_ERR_INVALID_ARGUMENT:
diffs.append({"case": "2h1or_odd_head", "orig": hex(rc_odd), "rust": hex(NV_ERR_INVALID_ARGUMENT)})
# Invalid 2H1OR with SHMS mode != 0 -> 0x1f
emu.wd(params + 0x04, 0)
emu.wd(params + 0x14, 2)
rc_shms = emu.call("_nv025343rm", disp_common, params) & 0xFFFFFFFF
cases += 1
if rc_shms != NV_ERR_INVALID_ARGUMENT:
diffs.append({"case": "2h1or_with_shms", "orig": hex(rc_shms), "rust": hex(NV_ERR_INVALID_ARGUMENT)})
return {
"name": "dp_mst_config_stream_unicorn",
"passed": len(diffs) == 0,
"cases": cases,
"diffs": diffs,
}
def check_vrr_gsync_unicorn(emu: Emulator) -> dict:
"""Execute `_nv025573rm`, `_nv025557rm`, and `_nv035189rm` in Unicorn."""
emu.reset()
_install_common(emu)
diffs = []
cases = 0
# 1. _nv025573rm (VRR timing formula) & _nv025557rm (VRR head enable bit packing)
disp_common = emu.allocate(0x200)
disp_parent = emu.allocate(0x200)
emu.wq(disp_common + 0x1B8, disp_parent)
gpu = emu.allocate(0x2400)
vrr_obj = emu.allocate(0x500)
vrr_vtbl = emu.allocate(0x100)
kdisp = emu.allocate(0x400)
emu.wd(kdisp + 0x300, 4) # 4 heads
emu.wq(gpu + 0x1F58, kdisp)
emu.wq(gpu + 0x2108, vrr_obj)
emu.wq(vrr_obj + 0x100, vrr_vtbl)
vrr_noop = emu.allocate(16)
emu.install(vrr_noop, lambda _a: 0)
emu.wq(vrr_vtbl + 0x10, vrr_noop)
emu.wq(vrr_vtbl + 0x28, vrr_noop)
res_sub = emu.allocate(16)
emu.install(res_sub, lambda args: (emu.wq(args[2], gpu), 0)[1])
emu.wq(disp_parent + 0x118, res_sub)
timing_buf = emu.allocate(44)
for head, pclk, ht, vmin, vmax in [
(0, 594000, 4400, 2250, 4500),
(1, 297000, 2200, 1125, 2250),
(3, 148500, 2200, 1125, 1800),
]:
emu.write(timing_buf, b"\x00" * 44)
emu.wd(timing_buf + 0x04, head)
emu.wd(timing_buf + 0x08, pclk)
emu.write(timing_buf + 0x0C, struct.pack("<H", ht))
emu.write(timing_buf + 0x18, struct.pack("<H", vmax))
emu.write(timing_buf + 0x1C, struct.pack("<H", vmin))
rc = emu.call("_nv025573rm", disp_common, timing_buf) & 0xFFFFFFFF
got_ext = emu.q(vrr_obj + 0x388 + head * 8)
exp_ext = rust_vrr_ext_ns10(pclk, ht, vmin, vmax)
cases += 1
if rc != 0 or got_ext != exp_ext:
diffs.append({"fn": "_nv025573rm", "head": head, "orig": (rc, got_ext), "rust": (0, exp_ext)})
# Invalid pclk=0 -> 0x1f
emu.wd(timing_buf + 0x08, 0)
rc_zero = emu.call("_nv025573rm", disp_common, timing_buf) & 0xFFFFFFFF
cases += 1
if rc_zero != NV_ERR_INVALID_ARGUMENT:
diffs.append({"fn": "_nv025573rm", "case": "pclk_zero", "orig": hex(rc_zero), "rust": hex(NV_ERR_INVALID_ARGUMENT)})
# 2. _nv025557rm: VRR head enable bitfield packing
captured_flags: list[int] = []
emu.install("_nv057362rm", lambda args: (captured_flags.append(args[1] & 0xFFFFFFFF), 0)[1])
_stub_noop(emu, "_nv057367rm", 0)
en_buf = emu.allocate(20)
for vec in [
(0, 1, 0, 1, 0, 3, 0, 0, 0, 0),
(2, 1, 1, 1, 2, 7, 1, 1, 0, 1),
(3, 0, 1, 0, 3, 0x7F, 1, 1, 1, 1),
]:
head, b0, b1, b2, b3, b4, b5, b6, b7, b8 = vec
emu.write(en_buf, struct.pack("<IIBBBBBBBBB3x", 0, head, b0, b1, b2, b3, b4, b5, b6, b7, b8))
captured_flags.clear()
rc = emu.call("_nv025557rm", disp_common, en_buf) & 0xFFFFFFFF
exp = rust_pack_vrr_head_enable(head, b0, b1, b2, b3, b4, b5, b6, b7, b8)
cases += 1
if rc != 0 or captured_flags != [exp]:
diffs.append({"fn": "_nv025557rm", "vec": vec, "orig": (rc, captured_flags), "rust": (0, [exp])})
# 3. _nv035189rm: RG raster vertical extend height calculation
regs, _, wrap = _make_bar0_io(emu)
objdisp = emu.allocate(0x4000)
emu.wq(objdisp + 0x3D38, wrap)
head_obj = emu.allocate(0x400)
rg_table = emu.allocate(0x800)
get_rg_tbl = emu.allocate(16)
emu.install(get_rg_tbl, lambda _a: rg_table)
emu.wq(objdisp + 0x10D8, get_rg_tbl)
for head, base_vt, fp, bp, mult in [
(0, 2250, (100 << 16) | 0x4000, (50 << 16) | 0x4000, 0x11),
(1, 1125, (200 << 16) | 0x4000, (0 << 16), 0x00),
(2, 1440, (300 << 16) | 0x4000, (40 << 16) | 0x4000, 0x10),
]:
emu.wd(head_obj + 0x3CC, head)
emu.wd(rg_table + head * 0x178 + 0x120, 1)
emu.wd(rg_table + head * 0x178 + 0x54, base_vt)
regs[head * 0x800 + 0x616364] = fp
regs[head * 0x800 + 0x616360] = bp
regs[head * 0x800 + 0x6163C8] = mult
got_vt = emu.call("_nv035189rm", 0, objdisp, head_obj) & 0xFFFFFFFF
exp_vt = rust_rg_effective_vtotal(base_vt, fp, bp, mult)
cases += 1
if got_vt != exp_vt:
diffs.append({"fn": "_nv035189rm", "head": head, "orig": got_vt, "rust": exp_vt})
return {
"name": "vrr_gsync_timing_and_rg_vtotal_unicorn",
"passed": len(diffs) == 0,
"cases": cases,
"diffs": diffs,
}
def check_hdr10_infoframe_unicorn(emu: Emulator) -> dict:
"""Execute `_nv025538rm` (SET_HDMI_ENABLE) and `_nv025553rm` (SET_OD_PACKET / DRM 0x87) in Unicorn."""
emu.reset()
_install_common(emu)
diffs = []
cases = 0
disp_common = emu.allocate(0x200)
disp_parent = emu.allocate(0x200)
emu.wq(disp_common + 0x1B8, disp_parent)
gpu = emu.allocate(0x400)
objdisp = emu.allocate(0x2000)
emu.wd(objdisp + 0x1A20, 4) # 4 heads
res_sub = emu.allocate(16)
emu.install(res_sub, lambda args: (emu.wq(args[2], gpu), emu.wq(args[3], objdisp), 0)[2])
emu.wq(disp_parent + 0x118, res_sub)
_stub_noop(emu, "_nv025212rm", 0)
od_obj = emu.allocate(0x100)
dfp_obj = emu.allocate(0x100)
hdmi_obj = emu.allocate(0x100)
emu.install("_nv023483rm", lambda _a: od_obj)
emu.install("_nv005245rm", lambda args: hdmi_obj if args[1] == emu.by_name["_nv003163rm"] else dfp_obj)
emu.install("_nv049185rm", lambda _a: 0) # HDMI (not DP)
emu.install("_nv049191rm", lambda _a: 1) # DFP
emu.install("_nv059531rm", lambda _a: 1) # HDMI enabled
emu.install("_nv023420rm", lambda args: 0x5000 + (args[1] & 0xFFFFFFFF))
emu.install("_nv035183rm", lambda args: 1 << (args[0] - 0x5000))
hdmi_calls: list[int] = []
emu.install("_nv059537rm", lambda args: (hdmi_calls.append(args[1] & 0xFF), 0)[1])
# 1. _nv025538rm: SET_HDMI_ENABLE
hdmi_p = emu.allocate(12)
emu.write(hdmi_p, struct.pack("<IIB3x", 0, 0x01, 1))
rc_h = emu.call("_nv025538rm", disp_common, hdmi_p) & 0xFFFFFFFF
cases += 1
if rc_h != 0 or hdmi_calls != [1]:
diffs.append({"fn": "_nv025538rm", "case": "enable", "orig": (rc_h, hdmi_calls)})
emu.write(hdmi_p, struct.pack("<IIB3x", 0, 0x03, 1)) # non-power-of-two displayId -> 0x1f
rc_h_bad = emu.call("_nv025538rm", disp_common, hdmi_p) & 0xFFFFFFFF
cases += 1
if rc_h_bad != NV_ERR_INVALID_ARGUMENT:
diffs.append({"fn": "_nv025538rm", "case": "multi_bit_dpy", "orig": hex(rc_h_bad)})
# 2. _nv025553rm: SET_OD_PACKET with 30-byte CTA-861.3 DRM (0x87) InfoFrame
pkt_calls: list[tuple[int, int, int, int, bytes]] = []
def _cap_pkt(args):
from unicorn.x86_const import UC_X86_REG_RSP
head = args[2] & 0xFFFFFFFF
ptype = args[3] & 0xFF
tctrl = args[4] & 0xFFFFFFFF
psz = args[5] & 0xFFFFFFFF
ptr = emu.q(emu.cpu.reg_read(UC_X86_REG_RSP) + 8)
pkt_calls.append((head, ptype, tctrl, psz, emu.read(ptr, psz)))
return 0
emu.install("_nv022439rm", _cap_pkt)
hdr_bytes = pack_cta861_drm_infoframe(2, 1000, 50, 1000, 400)
od_buf = emu.allocate(60)
emu.write(od_buf, struct.pack("<IIIIIB", 0, 0x01, 0x01, len(hdr_bytes), 0, 0) + hdr_bytes + b"\x00" * (36 - len(hdr_bytes) + 3))
rc_p = emu.call("_nv025553rm", disp_common, od_buf) & 0xFFFFFFFF
cases += 1
if rc_p != 0 or pkt_calls != [(0, 0x87, 1, 30, hdr_bytes)]:
diffs.append({"fn": "_nv025553rm", "case": "hdr10_drm_0x87", "orig": (hex(rc_p), pkt_calls)})
# Packet size > 0x24 (36) -> 0x1f
emu.wd(od_buf + 0x0C, 37)
rc_big = emu.call("_nv025553rm", disp_common, od_buf) & 0xFFFFFFFF
cases += 1
if rc_big != NV_ERR_INVALID_ARGUMENT:
diffs.append({"fn": "_nv025553rm", "case": "oversize_37", "orig": hex(rc_big)})
# Negative transmitControl -> 0x1f
emu.wd(od_buf + 0x0C, 30)
emu.wd(od_buf + 0x08, 0x80000000)
rc_neg = emu.call("_nv025553rm", disp_common, od_buf) & 0xFFFFFFFF
cases += 1
if rc_neg != NV_ERR_INVALID_ARGUMENT:
diffs.append({"fn": "_nv025553rm", "case": "neg_tctrl", "orig": hex(rc_neg)})
return {
"name": "hdr10_hdmi_and_cta861_drm_infoframe_unicorn",
"passed": len(diffs) == 0,
"cases": cases,
"diffs": diffs,
}
def check_framelock_sli_unlocked_unicorn(emu: Emulator) -> dict:
"""Execute `_nv022997rm`, `_nv031431rm`, and `_nv022998rm` in Unicorn.
Confirms bit-for-bit parity with `_nv022997rm` pin-toggle algorithm AND proves that
stock `_nv031431rm`/`_nv022998rm` enforce the Quadro-only brand gate (`bIsQuadro` at
`pGpu+0x541b`), which our clean-room Rust driver explicitly UNLOCKS.
"""
emu.reset()
_install_common(emu)
diffs = []
cases = 0
gpu0 = emu.allocate(0x7000)
gpu1 = emu.allocate(0x7000)
disp0 = emu.allocate(0x1000)
disp1 = emu.allocate(0x1000)
gpio0 = emu.allocate(0x400)
gpio1 = emu.allocate(0x400)
emu.wq(gpu0 + 0x1F70, disp0)
emu.wq(gpu1 + 0x1F70, disp1)
emu.wq(gpu0 + 0x20D8, gpio0)
emu.wq(gpu1 + 0x20D8, gpio1)
# 1. Verify _nv022997rm pin-toggle link detection (all 4 phases + invalid masks)
pin_resolve = emu.allocate(16)
def _res_pin(args):
from unicorn.x86_const import UC_X86_REG_RSP
emu.wd(args[4], 1) # func
emu.wd(args[5], 5) # pin
prev_dir_ptr = emu.q(emu.cpu.reg_read(UC_X86_REG_RSP) + 8)
emu.wb(prev_dir_ptr, 0) # prev dir
return 0
emu.install(pin_resolve, _res_pin)
emu.wq(disp0 + 0xD10, pin_resolve)
phase_seq: list[int] = []
gpio_noop = emu.allocate(16)
emu.install(gpio_noop, lambda _a: 0)
gpio_rd = emu.allocate(16)
def _rd_pin(args):
val = phase_seq.pop(0) if phase_seq else 0
emu.wd(args[3], val)
return 0
emu.install(gpio_rd, _rd_pin)
for g in (gpio0, gpio1):
emu.wq(g + 0x1E0, gpio_noop)
emu.wq(g + 0x1D0, gpio_noop)
emu.wq(g + 0x1C8, gpio_rd)
# Pass case: [1, 0, 1, 0] -> NV_OK
phase_seq[:] = [1, 0, 1, 0]
rc_ok = emu.call("_nv022997rm", disp0, gpu0, gpu1, 0x1, 0x2) & 0xFFFFFFFF
cases += 1
if rc_ok != NV_OK:
diffs.append({"fn": "_nv022997rm", "case": "all_4_phases_ok", "orig": hex(rc_ok), "rust": hex(NV_OK)})
# Fail phase 3 -> NV_ERR_INVALID_OPERATION (0x53)
phase_seq[:] = [1, 0, 0, 0]
rc_fail = emu.call("_nv022997rm", disp0, gpu0, gpu1, 0x1, 0x2) & 0xFFFFFFFF
cases += 1
if rc_fail != NV_ERR_INVALID_OPERATION:
diffs.append({"fn": "_nv022997rm", "case": "phase3_mismatch", "orig": hex(rc_fail), "rust": hex(NV_ERR_INVALID_OPERATION)})
# Invalid non-power-of-two mask -> 0x1f
rc_inv = emu.call("_nv022997rm", disp0, gpu0, gpu1, 0x3, 0x2) & 0xFFFFFFFF
cases += 1
if rc_inv != NV_ERR_INVALID_ARGUMENT:
diffs.append({"fn": "_nv022997rm", "case": "non_pow2", "orig": hex(rc_inv), "rust": hex(NV_ERR_INVALID_ARGUMENT)})
# 2. Prove stock _nv031431rm & _nv022998rm gate on bIsQuadro (pGpu+0x541b)
emu.install("_nv031403rm", lambda _a: gpu0)
emu.wb(gpu0 + 0x541B, 0) # non-Quadro
emu.wb(gpu0 + 0x6A68, 0x05)
p31431 = emu.allocate(16)
emu.wd(p31431, 0)
emu.wd(p31431 + 4, 0xFF)
rc_q0 = emu.call("_nv031431rm", p31431) & 0xFFFFFFFF
stock_non_quadro_val = emu.d(p31431 + 4)
emu.wb(gpu0 + 0x541B, 1) # Quadro
rc_q1 = emu.call("_nv031431rm", p31431) & 0xFFFFFFFF
stock_quadro_val = emu.d(p31431 + 4)
cases += 2
if rc_q0 != 0 or stock_non_quadro_val != 0 or rc_q1 != 0 or stock_quadro_val != 0x05:
diffs.append({
"fn": "_nv031431rm",
"stock_non_quadro": stock_non_quadro_val,
"stock_quadro": stock_quadro_val,
})
# In _nv022998rm, when brand-gate check triggers, stock returns 0x56 (NV_ERR_NOT_SUPPORTED)
gate_true = emu.allocate(16)
emu.install(gate_true, lambda _a: 1)
emu.wq(disp0 + 0xD08, gate_true)
emu.wq(disp1 + 0xD08, gate_true)
emu.wb(gpu0 + 0x541B, 1)
emu.wb(gpu1 + 0x541B, 1)
rc_gated = emu.call("_nv022998rm", disp0, gpu0, gpu1, 0x1, 0x2) & 0xFFFFFFFF
cases += 1
if rc_gated != NV_ERR_NOT_SUPPORTED:
diffs.append({"fn": "_nv022998rm", "case": "stock_brand_gate", "orig": hex(rc_gated), "exp": hex(NV_ERR_NOT_SUPPORTED)})
return {
"name": "framelock_sli_pin_toggle_and_quadro_gate_unlocked_unicorn",
"passed": len(diffs) == 0,
"cases": cases,
"diffs": diffs,
}
def check_advanced_nv0073_nv30f1_rodata() -> dict:
"""Verify advanced display NV0073 & NV30F1 control IDs and param sizes against `.rodata`."""
diffs = []
with ORIGINAL.open("rb") as f:
elf = ELFFile(f)
rodata = elf.get_section_by_name(".rodata").data()
found: dict[int, int] = {}
for start, prefix in [(0x4D984E0, 0x0073), (0x4DA11C0, 0x30F1)]:
o = start
while o < len(rodata) - 32:
cmd, sz = struct.unpack_from("<II", rodata, o + 0x10)
if (cmd >> 16) != prefix:
break
found[cmd] = sz
o += 0x20
expected = {
0x00730273: 12, # SPECIFIC_SET_HDMI_ENABLE
0x00730288: 60, # SPECIFIC_SET_OD_PACKET (HDR10 DRM 0x87)
0x007302A9: 56, # SPECIFIC_SET_OD_PACKET_CTRL
0x00731362: 84, # DP_SET_STREAM_CONFIG (DP-MST)
0x00731369: 60, # DP_GET_CAPS
0x0073136C: 28, # DP_SINGLE_HEAD_MULTI_STREAM_MODE
0x00731701: 8, # SYSTEM_GET_VRR_STATE
0x00731702: 16, # SYSTEM_SET_VRR_CONTROL
0x00731703: 44, # SPECIFIC_SET_VRR
0x00731704: 20, # SYSTEM_ENABLE_VRR_HEAD
0x00731705: 8, # SYSTEM_DISABLE_VRR_HEAD
0x30F10101: 8, # GSYNC_GET_VERSION
0x30F10110: 24, # GSYNC_GET_STATUS_SYNC
}
for cmd, exp_sz in expected.items():
if found.get(cmd) != exp_sz:
diffs.append({"cmd": hex(cmd), "orig_sz": found.get(cmd), "rust_sz": exp_sz})
return {
"name": "advanced_nv0073_and_nv30f1_rodata_parity",
"passed": len(diffs) == 0,
"cases": len(expected),
"diffs": diffs,
}
def check_e2e_boot_to_kms_rust() -> dict:
"""Execute real Rust `e2e_boot_to_kms_full_pipeline` and `advanced_display_*` tests."""
env = os.environ.copy()
env["PATH"] = f"{Path.home() / '.cargo/bin'}:{env.get('PATH', '')}"
cmd = [
"cargo", "test",
"--manifest-path", str(ROOT / "src/nvidia/Cargo.toml"),
"--lib",
"gpu::display::display_channels::tests",
]
proc = subprocess.run(cmd, env=env, capture_output=True, text=True)
diffs = []
if proc.returncode != 0:
diffs.append({"returncode": proc.returncode, "stderr": proc.stderr[-500:], "stdout": proc.stdout[-500:]})
for must_pass in ("e2e_boot_to_kms_full_pipeline", "advanced_display_dp_mst_vrr_hdr10_and_unlocked_framelock"):
if f"{must_pass} ... ok" not in proc.stdout:
diffs.append({"missing_passed_test": must_pass})
return {
"name": "e2e_boot_to_kms_rust_pipeline",
"passed": len(diffs) == 0,
"cases": 5,
"diffs": diffs,
}
def run_checks() -> list[dict]:
return [
check_dp_mst_stream_ctl_unicorn(Emulator()),
check_dp_mst_config_stream_unicorn(Emulator()),
check_vrr_gsync_unicorn(Emulator()),
check_hdr10_infoframe_unicorn(Emulator()),
check_framelock_sli_unlocked_unicorn(Emulator()),
check_advanced_nv0073_nv30f1_rodata(),
check_e2e_boot_to_kms_rust(),
]
def main() -> int:
results = run_checks()
total_cases = sum(r.get("cases", 0) for r in results)
total_diffs = sum(len(r["diffs"]) for r in results)
all_ok = all(r["passed"] for r in results)
print(json.dumps({
"passed": all_ok,
"checks": len(results),
"total_cases": total_cases,
"total_diffs": total_diffs,
"results": results,
}, indent=2))
return 0 if all_ok else 1
if __name__ == "__main__":
raise SystemExit(main())