mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
758 lines
27 KiB
Python
758 lines
27 KiB
Python
#!/usr/bin/env python3
|
|
"""Standalone x86_64 Unicorn binary oracle for Display / EVO / NvDisplay & Modeset parity.
|
|
|
|
Executes real 580.178.04 `nv-kernel.o_binary` functions in Unicorn and verifies exact
|
|
bit-for-bit parity against the clean-room Rust driver (`display.rs`, `display_channels.rs`,
|
|
`modeset_ffi.rs`, `nv_modeset_abi.h`) across Maxwell through Blackwell.
|
|
|
|
Exports `run_checks()` returning a list of `{"name": ..., "passed": bool, "diffs": [...]}` dicts.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tools"))
|
|
from rmapi_binary_emulator import Emulator, ORIGINAL, read_elf64_section # noqa: E402
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Rust mirror models (exact 1:1 ports of display.rs / display_channels.rs)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
NV_OK = 0x0000
|
|
NV_ERR_INVALID_ARGUMENT = 0x001F
|
|
NV_ERR_INVALID_CLASS = 0x0021
|
|
NV_ERR_INVALID_STATE = 0x0040
|
|
NV_ERR_NOT_SUPPORTED = 0x0056
|
|
NV_ERR_GENERIC = 0xFFFF
|
|
|
|
|
|
def rust_disp_channel_class_from_rm_class(rm_class: int) -> tuple[int, int | None]:
|
|
if rm_class in (0x917A, 0xC37A, 0xC57A, 0xC67A, 0xC97A, 0xCA7A, 0xCB7A, 0xCC7A):
|
|
return NV_OK, 0
|
|
if rm_class == 0x917B:
|
|
return NV_OK, 1
|
|
if rm_class == 0x927C:
|
|
return NV_OK, 2
|
|
if rm_class in (
|
|
0x947D, 0x957D, 0x977D, 0x987D, 0xC37D, 0xC57D, 0xC67D, 0xC77D,
|
|
0xC97D, 0xCA7D, 0xCB7D, 0xCC7D,
|
|
):
|
|
return NV_OK, 3
|
|
if rm_class == 0x917E:
|
|
return NV_OK, 4
|
|
if rm_class in (0xC37B, 0xC57B, 0xC67B, 0xC97B, 0xCA7B, 0xCB7B, 0xCC7B):
|
|
return NV_OK, 5
|
|
if rm_class in (0xC37E, 0xC57E, 0xC67E, 0xC97E, 0xCA7E, 0xCB7E, 0xCC7E):
|
|
return NV_OK, 6
|
|
if rm_class == 0xC77F:
|
|
return NV_OK, 7
|
|
return NV_ERR_INVALID_ARGUMENT, None
|
|
|
|
|
|
def rust_evo_v0201_channel_num(cls: int, inst: int) -> tuple[int, int | None]:
|
|
if cls == 3:
|
|
return NV_OK, 0
|
|
if cls == 2 and inst < 4:
|
|
return NV_OK, inst + 1
|
|
if cls == 4 and inst < 4:
|
|
return NV_OK, inst + 5
|
|
if cls == 1 and inst < 4:
|
|
return NV_OK, inst + 9
|
|
if cls == 0 and inst < 4:
|
|
return NV_OK, inst + 13
|
|
return NV_ERR_GENERIC, None
|
|
|
|
|
|
def rust_evo_v0205_channel_num(cls: int, inst: int) -> tuple[int, int | None]:
|
|
if cls == 3:
|
|
return NV_OK, 0
|
|
if cls == 2 and inst < 4:
|
|
return NV_OK, inst + 1
|
|
if cls == 4 and inst < 4:
|
|
return NV_OK, inst + 5
|
|
if cls == 1 and inst < 4:
|
|
return NV_OK, inst + 0x0D
|
|
if cls == 0 and inst < 4:
|
|
return NV_OK, inst + 0x11
|
|
return NV_ERR_INVALID_ARGUMENT, None
|
|
|
|
|
|
def rust_nvdisp_v0300_channel_num(
|
|
cls: int, inst: int, head_mask: int, win_mask: int
|
|
) -> tuple[int, int | None]:
|
|
if cls == 3:
|
|
return NV_OK, 0
|
|
if cls == 6:
|
|
if inst >= 32:
|
|
return NV_ERR_INVALID_ARGUMENT, None
|
|
if ((win_mask >> inst) & 1) == 0:
|
|
return NV_ERR_NOT_SUPPORTED, None
|
|
return NV_OK, inst + 1
|
|
if cls == 5:
|
|
if inst >= 32:
|
|
return NV_ERR_INVALID_ARGUMENT, None
|
|
if ((win_mask >> inst) & 1) == 0:
|
|
return NV_ERR_NOT_SUPPORTED, None
|
|
return NV_OK, inst + 0x21
|
|
if cls == 0:
|
|
if inst >= 8:
|
|
return NV_ERR_INVALID_ARGUMENT, None
|
|
if ((head_mask >> inst) & 1) == 0:
|
|
return NV_ERR_NOT_SUPPORTED, None
|
|
return NV_OK, inst + 0x49
|
|
if cls == 7:
|
|
return NV_OK, 0x7F
|
|
return NV_ERR_INVALID_CLASS, None
|
|
|
|
|
|
def rust_nvdisp_user_area(cls: int, ch_num: int) -> tuple[int, int | None]:
|
|
offset = 0x680000 if ch_num == 0 else (ch_num + 0x68F) * 0x1000
|
|
if cls == 3:
|
|
return offset, 0x10000
|
|
if cls in (0, 5, 6):
|
|
return offset, 0x1000
|
|
return offset, None
|
|
|
|
|
|
def rust_pack_caps(flags: list[int]) -> tuple[int, int]:
|
|
b0 = (
|
|
(0x01 if flags[0] else 0)
|
|
| (0x10 if flags[1] else 0)
|
|
| (0x40 if flags[2] else 0)
|
|
| (0x80 if flags[3] else 0)
|
|
)
|
|
b1 = (
|
|
(0x01 if flags[4] else 0)
|
|
| (0x02 if flags[5] else 0)
|
|
| (0x04 if flags[6] else 0)
|
|
| (0x08 if flags[7] else 0)
|
|
| (0x10 if flags[8] else 0)
|
|
| (0x20 if flags[9] else 0)
|
|
| (0x40 if not flags[10] else 0)
|
|
)
|
|
return b0, b1
|
|
|
|
|
|
def rust_merge_sli(acc: tuple[int, int], new: tuple[int, int]) -> tuple[int, int]:
|
|
b0 = (acc[0] & new[0] & 0xD1) | ((acc[0] | new[0]) & 0x2E)
|
|
b1 = (acc[1] & new[1] & 0x2F) | ((acc[1] | new[1]) & 0xD0)
|
|
return b0, b1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Unicorn checks
|
|
# ---------------------------------------------------------------------------
|
|
|
|
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_os_mem_helpers(emu: Emulator):
|
|
if "os_mem_set" in emu.externals and "os_mem_set" not in emu.handlers:
|
|
def _memset(args):
|
|
dst, val, n = args[0], args[1] & 0xFF, args[2] & 0xFFFFFFFF
|
|
if n:
|
|
emu.write(dst, bytes([val]) * n)
|
|
return dst
|
|
emu.install("os_mem_set", _memset)
|
|
if "os_mem_copy" in emu.externals and "os_mem_copy" not in emu.handlers:
|
|
def _memcpy(args):
|
|
dst, src, n = args[0], args[1], args[2] & 0xFFFFFFFF
|
|
if n:
|
|
emu.write(dst, emu.read(src, n))
|
|
return dst
|
|
emu.install("os_mem_copy", _memcpy)
|
|
|
|
|
|
def check_disp_channel_class_map_unicorn(emu: Emulator) -> dict:
|
|
"""Execute `_nv040104rm` in Unicorn for every Maxwell-Blackwell display channel class."""
|
|
emu.reset()
|
|
_stub_noop(emu, "_nv047869rm", 0)
|
|
out = emu.allocate(8)
|
|
diffs = []
|
|
|
|
test_classes = [
|
|
0x917A, 0x917B, 0x917E, 0x927C, 0x947D, 0x957D, 0x977D, 0x987D,
|
|
0xC37A, 0xC37B, 0xC37D, 0xC37E,
|
|
0xC57A, 0xC57B, 0xC57D, 0xC57E,
|
|
0xC67A, 0xC67B, 0xC67D, 0xC67E,
|
|
0xC77D, 0xC77F,
|
|
0xC97A, 0xC97B, 0xC97D, 0xC97E,
|
|
0xCA7A, 0xCA7B, 0xCA7D, 0xCA7E,
|
|
0xCB7A, 0xCB7B, 0xCB7D, 0xCB7E,
|
|
0xCC7A, 0xCC7B, 0xCC7D, 0xCC7E,
|
|
0xC870, 0xC370, 0x9470, 0x0000, 0xDEAD,
|
|
]
|
|
for cid in test_classes:
|
|
emu.wd(out, 0xBEEF)
|
|
rc = emu.call("_nv040104rm", 0, cid, out) & 0xFFFFFFFF
|
|
got_cls = emu.d(out) if rc == 0 else None
|
|
exp_rc, exp_cls = rust_disp_channel_class_from_rm_class(cid)
|
|
if rc != exp_rc or got_cls != exp_cls:
|
|
diffs.append({
|
|
"rm_class": hex(cid),
|
|
"orig": (hex(rc), got_cls),
|
|
"rust": (hex(exp_rc), exp_cls),
|
|
})
|
|
|
|
# NULL out pointer check
|
|
rc_null = emu.call("_nv040104rm", 0, 0xC37D, 0) & 0xFFFFFFFF
|
|
if rc_null != NV_ERR_INVALID_ARGUMENT:
|
|
diffs.append({"case": "null_out", "orig": hex(rc_null), "rust": hex(NV_ERR_INVALID_ARGUMENT)})
|
|
|
|
return {
|
|
"name": "disp_channel_class_map_unicorn",
|
|
"passed": len(diffs) == 0,
|
|
"cases": len(test_classes) + 1,
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def check_evo_channel_numbers_unicorn(emu: Emulator) -> dict:
|
|
"""Execute `_nv040092rm` (EVO v02_01) and `_nv040093rm` (EVO v02_05) in Unicorn."""
|
|
emu.reset()
|
|
_stub_noop(emu, "_nv047869rm", 0)
|
|
out = emu.allocate(8)
|
|
diffs = []
|
|
cases = 0
|
|
|
|
for fn_name, rust_fn in [
|
|
("_nv040092rm", rust_evo_v0201_channel_num),
|
|
("_nv040093rm", rust_evo_v0205_channel_num),
|
|
]:
|
|
for cls in range(8):
|
|
for inst in range(6):
|
|
emu.wd(out, 0xCAFE)
|
|
rc = emu.call(fn_name, 0, cls, inst, out) & 0xFFFFFFFF
|
|
got = emu.d(out) if rc == 0 else None
|
|
exp_rc, exp_val = rust_fn(cls, inst)
|
|
cases += 1
|
|
if rc != exp_rc or got != exp_val:
|
|
diffs.append({
|
|
"fn": fn_name, "cls": cls, "inst": inst,
|
|
"orig": (hex(rc), got), "rust": (hex(exp_rc), exp_val),
|
|
})
|
|
rc_null = emu.call(fn_name, 0, 3, 0, 0) & 0xFFFFFFFF
|
|
exp_null = NV_ERR_GENERIC if fn_name == "_nv040092rm" else NV_ERR_INVALID_ARGUMENT
|
|
cases += 1
|
|
if rc_null != exp_null:
|
|
diffs.append({"fn": fn_name, "case": "null_out", "orig": hex(rc_null), "rust": hex(exp_null)})
|
|
|
|
return {
|
|
"name": "evo_channel_numbers_unicorn",
|
|
"passed": len(diffs) == 0,
|
|
"cases": cases,
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def check_nvdisp_channel_numbers_unicorn(emu: Emulator) -> dict:
|
|
"""Execute `_nv040094rm` (NvDisplay v03_00+ Volta-Blackwell) in Unicorn."""
|
|
emu.reset()
|
|
_stub_noop(emu, "_nv047869rm", 0)
|
|
_stub_noop(emu, "_nv046745rm", 0)
|
|
kdisp = emu.allocate(0x400)
|
|
caps = emu.allocate(16)
|
|
out = emu.allocate(8)
|
|
diffs = []
|
|
cases = 0
|
|
|
|
# NULL cap_ptr -> NV_ERR_INVALID_STATE (0x40)
|
|
emu.wq(kdisp + 0x268, 0)
|
|
rc_nocap = emu.call("_nv040094rm", kdisp, 3, 0, out) & 0xFFFFFFFF
|
|
cases += 1
|
|
if rc_nocap != NV_ERR_INVALID_STATE:
|
|
diffs.append({"case": "null_caps", "orig": hex(rc_nocap), "rust": hex(NV_ERR_INVALID_STATE)})
|
|
|
|
emu.wq(kdisp + 0x268, caps)
|
|
for head_mask, win_mask in [(0x0F, 0x00FF), (0x05, 0x00AA), (0xFF, 0xFFFFFFFF)]:
|
|
emu.wd(caps + 0, head_mask)
|
|
emu.wd(caps + 4, win_mask)
|
|
for cls in range(8):
|
|
for inst in (0, 1, 3, 7, 8, 31, 32):
|
|
emu.wd(out, 0xCAFE)
|
|
rc = emu.call("_nv040094rm", kdisp, cls, inst, out) & 0xFFFFFFFF
|
|
got = emu.d(out) if rc == 0 else None
|
|
exp_rc, exp_val = rust_nvdisp_v0300_channel_num(cls, inst, head_mask, win_mask)
|
|
cases += 1
|
|
if rc != exp_rc or got != exp_val:
|
|
diffs.append({
|
|
"head_mask": hex(head_mask), "win_mask": hex(win_mask),
|
|
"cls": cls, "inst": inst,
|
|
"orig": (hex(rc), got), "rust": (hex(exp_rc), exp_val),
|
|
})
|
|
|
|
return {
|
|
"name": "nvdisp_channel_numbers_unicorn",
|
|
"passed": len(diffs) == 0,
|
|
"cases": cases,
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def check_user_area_and_sfuser_unicorn(emu: Emulator) -> dict:
|
|
"""Execute `_nv040096rm`, `_nv040102rm`, `_nv040103rm`, `_nv040098rm`, `_nv040099rm`, `_nv040100rm`."""
|
|
emu.reset()
|
|
_stub_noop(emu, "_nv047869rm", 0)
|
|
_stub_noop(emu, "_nv046745rm", 0)
|
|
out_off = emu.allocate(8)
|
|
out_sz = emu.allocate(8)
|
|
diffs = []
|
|
cases = 0
|
|
|
|
# DispSfUser bases: EVO=0x640000, Volta-Ada=0x690000, Blackwell=0x6f0000
|
|
for fn_name, exp_base in [
|
|
("_nv040096rm", 0x640000),
|
|
("_nv040102rm", 0x690000),
|
|
("_nv040103rm", 0x6F0000),
|
|
]:
|
|
emu.wd(out_off, 0)
|
|
emu.wd(out_sz, 0)
|
|
emu.call(fn_name, 0, 0, out_off, out_sz)
|
|
cases += 1
|
|
if emu.d(out_off) != exp_base or emu.d(out_sz) != 0x1000:
|
|
diffs.append({
|
|
"fn": fn_name,
|
|
"orig": (hex(emu.d(out_off)), hex(emu.d(out_sz))),
|
|
"rust": (hex(exp_base), "0x1000"),
|
|
})
|
|
|
|
# EVO user area (_nv040098rm -> _nv040092rm, _nv040099rm -> _nv040093rm)
|
|
kdisp = emu.allocate(0x400)
|
|
for ua_fn, ch_fn, rust_ch in [
|
|
("_nv040098rm", "_nv040092rm", rust_evo_v0201_channel_num),
|
|
("_nv040099rm", "_nv040093rm", rust_evo_v0205_channel_num),
|
|
]:
|
|
emu.wq(kdisp + 0x128, emu.by_name[ch_fn])
|
|
for cls, inst in [(3, 0), (2, 0), (2, 3), (4, 2), (1, 1), (0, 3)]:
|
|
emu.wd(out_off, 0)
|
|
emu.wd(out_sz, 0)
|
|
rc = emu.call(ua_fn, 0, kdisp, cls, inst, out_off, out_sz) & 0xFFFFFFFF
|
|
exp_rc, ch_num = rust_ch(cls, inst)
|
|
exp_off = (ch_num + 0x640) * 0x1000
|
|
cases += 1
|
|
if rc != exp_rc or emu.d(out_off) != exp_off or emu.d(out_sz) != 0x1000:
|
|
diffs.append({
|
|
"fn": ua_fn, "cls": cls, "inst": inst,
|
|
"orig": (hex(rc), hex(emu.d(out_off)), hex(emu.d(out_sz))),
|
|
"rust": (hex(exp_rc), hex(exp_off), "0x1000"),
|
|
})
|
|
|
|
# NvDisplay user area (_nv040100rm -> _nv040094rm)
|
|
caps = emu.allocate(16)
|
|
emu.wd(caps + 0, 0x0F)
|
|
emu.wd(caps + 4, 0x00FF)
|
|
emu.wq(kdisp + 0x268, caps)
|
|
emu.wq(kdisp + 0x128, emu.by_name["_nv040094rm"])
|
|
for cls, inst in [(3, 0), (6, 0), (6, 7), (5, 0), (5, 3), (0, 0), (0, 3)]:
|
|
emu.wd(out_off, 0)
|
|
emu.wd(out_sz, 0)
|
|
rc = emu.call("_nv040100rm", 0, kdisp, cls, inst, out_off, out_sz) & 0xFFFFFFFF
|
|
exp_rc, ch_num = rust_nvdisp_v0300_channel_num(cls, inst, 0x0F, 0x00FF)
|
|
exp_off, exp_sz = rust_nvdisp_user_area(cls, ch_num)
|
|
cases += 1
|
|
if rc != exp_rc or emu.d(out_off) != exp_off or emu.d(out_sz) != exp_sz:
|
|
diffs.append({
|
|
"fn": "_nv040100rm", "cls": cls, "inst": inst,
|
|
"orig": (hex(rc), hex(emu.d(out_off)), hex(emu.d(out_sz))),
|
|
"rust": (hex(exp_rc), hex(exp_off), hex(exp_sz)),
|
|
})
|
|
|
|
return {
|
|
"name": "disp_user_area_and_sfuser_unicorn",
|
|
"passed": len(diffs) == 0,
|
|
"cases": cases,
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def check_disp_system_caps_unicorn(emu: Emulator) -> dict:
|
|
"""Execute `_nv023264rm` in Unicorn to verify NV0073_CTRL_CMD_SYSTEM_GET_CAPS_V2 bit packing and SLI merge."""
|
|
emu.reset()
|
|
_stub_noop(emu, "_nv031390rm", 0)
|
|
_stub_noop(emu, "_nv047869rm", 0)
|
|
_stub_noop(emu, "_nv046745rm", 0)
|
|
objdisp = emu.allocate(0x2000)
|
|
out_caps = emu.allocate(8)
|
|
offsets = [
|
|
0x190D, 0x190C, 0x193D, 0x193F,
|
|
0x1961, 0x1962, 0x1963, 0x1916, 0x197D, 0x1922, 0x19B7,
|
|
]
|
|
diffs = []
|
|
cases = 0
|
|
|
|
vectors = [
|
|
[0] * 11,
|
|
[1] * 11,
|
|
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0],
|
|
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1],
|
|
[1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0],
|
|
[0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1],
|
|
]
|
|
for vec in vectors:
|
|
for off, val in zip(offsets, vec):
|
|
emu.wb(objdisp + off, val)
|
|
emu.write(out_caps, b"\x00\x00")
|
|
emu.call("_nv023264rm", 0, objdisp, out_caps, 0)
|
|
got = (emu.b(out_caps), emu.b(out_caps + 1))
|
|
exp = rust_pack_caps(vec)
|
|
cases += 1
|
|
if got != exp:
|
|
diffs.append({"mode": "pack", "vec": vec, "orig": got, "rust": exp})
|
|
|
|
# SLI merge tests against multiple accumulators
|
|
for acc in [(0x00, 0x00), (0xFF, 0xFF), (0xA5, 0x5A), (0xD1, 0x2F)]:
|
|
emu.write(out_caps, bytes(acc))
|
|
emu.call("_nv023264rm", 0, objdisp, out_caps, 1)
|
|
got_m = (emu.b(out_caps), emu.b(out_caps + 1))
|
|
exp_m = rust_merge_sli(acc, exp)
|
|
cases += 1
|
|
if got_m != exp_m:
|
|
diffs.append({"mode": "sli_merge", "acc": acc, "vec": vec, "orig": got_m, "rust": exp_m})
|
|
|
|
return {
|
|
"name": "disp_system_caps_pack_and_sli_merge_unicorn",
|
|
"passed": len(diffs) == 0,
|
|
"cases": cases,
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def check_sor_head_ihub_regs_unicorn(emu: Emulator) -> dict:
|
|
"""Execute `_nv051392rm`, `_nv051408rm`, `_nv040140rm`, `_nv040167rm`, `_nv060382rm`, `_nv060385rm`, `_nv024458rm`."""
|
|
emu.reset()
|
|
diffs = []
|
|
cases = 0
|
|
|
|
# 1. _nv051392rm: per-SOR register offset calculator
|
|
sor_obj = emu.allocate(0x100)
|
|
or_base = emu.allocate(0x100)
|
|
emu.wq(sor_obj + 0x68, or_base)
|
|
p2, p3, p4 = emu.allocate(8), emu.allocate(8), emu.allocate(8)
|
|
for sor in range(4):
|
|
emu.wd(or_base + 0x4C, sor)
|
|
rc = emu.call("_nv051392rm", sor_obj, p2, p3, p4) & 0xFFFFFFFF
|
|
got = (emu.d(p2), emu.d(p3), emu.d(p4))
|
|
exp = (sor * 0x800 + 0x620030, sor * 0x800 + 0x620004, sor * 0x800 + 0x62002C)
|
|
cases += 1
|
|
if rc != 0 or got != exp:
|
|
diffs.append({"fn": "_nv051392rm", "sor": sor, "orig": (rc, got), "rust": (0, exp)})
|
|
|
|
# 2. _nv051408rm: SOR_SEQ_CTL RMW at 0x620018 + sor*0x800
|
|
bar0_regs: dict[int, int] = {}
|
|
bar0_writes: list[tuple[int, int]] = []
|
|
reg_rd_stub = emu.allocate(16)
|
|
reg_wr_stub = emu.allocate(16)
|
|
emu.install(reg_rd_stub, lambda args: bar0_regs.get(args[1] & 0xFFFFFFFF, 0))
|
|
|
|
def _bar0_wr(args):
|
|
off = args[1] & 0xFFFFFFFF
|
|
val = args[2] & 0xFFFFFFFF
|
|
bar0_regs[off] = val
|
|
bar0_writes.append((off, val))
|
|
return 0
|
|
|
|
emu.install(reg_wr_stub, _bar0_wr)
|
|
io_vtbl = emu.allocate(0x100)
|
|
emu.wq(io_vtbl + 0x28, reg_rd_stub)
|
|
emu.wq(io_vtbl + 0x40, reg_wr_stub)
|
|
io_obj = emu.allocate(0x100)
|
|
emu.wq(io_obj, io_vtbl)
|
|
reg_wrap = emu.allocate(0x100)
|
|
emu.wq(reg_wrap + 0x50, io_obj)
|
|
objdisp = emu.allocate(0x4000)
|
|
emu.wq(objdisp + 0x3D38, reg_wrap)
|
|
owner_ptr = emu.allocate(8)
|
|
|
|
for sor, cur, proto, owner in [
|
|
(0, 0xFFFFFFFF, 5, 3),
|
|
(2, 0x12345678, 1, 7),
|
|
(3, 0x00000000, 7, 2),
|
|
]:
|
|
reg = sor * 0x800 + 0x620018
|
|
bar0_regs[reg] = cur
|
|
bar0_writes.clear()
|
|
emu.wd(owner_ptr, owner)
|
|
emu.call("_nv051408rm", 0, objdisp, 0, sor, proto, owner_ptr)
|
|
exp_val = (cur & 0xF8F8FFFF) | ((proto & 7) << 16) | ((owner & 7) << 24)
|
|
cases += 1
|
|
if bar0_writes != [(reg, exp_val)]:
|
|
diffs.append({"fn": "_nv051408rm", "orig": bar0_writes, "rust": [(reg, exp_val)]})
|
|
|
|
# 3. _nv040167rm (HEAD_CTRL RMW) and _nv040140rm (RG_RASTER_ACTIVE unpack)
|
|
emu.install("_nv053907rm", lambda args: bar0_regs.get(args[3] & 0xFFFFFFFF, 0))
|
|
|
|
def _nv053912_wr(args):
|
|
off = args[3] & 0xFFFFFFFF
|
|
val = args[4] & 0xFFFFFFFF
|
|
bar0_regs[off] = val
|
|
bar0_writes.append((off, val))
|
|
return 0
|
|
|
|
emu.install("_nv053912rm", _nv053912_wr)
|
|
prev_out = emu.allocate(8)
|
|
for head, cur, req in [(0, 0xAA000123, 0), (2, 0x55123456, 0x78)]:
|
|
reg = head * 0x800 + 0x610038
|
|
bar0_regs[reg] = cur
|
|
bar0_writes.clear()
|
|
emu.call("_nv040167rm", 0, 0, head, prev_out, req)
|
|
exp_prev = cur & 0x00FFFFFF
|
|
exp_next = (cur & 0xFF000000) | (0x3FF if req == 0 else (req & 0x00FFFFFF))
|
|
cases += 1
|
|
if emu.d(prev_out) != exp_prev or bar0_writes != [(reg, exp_next)]:
|
|
diffs.append({
|
|
"fn": "_nv040167rm", "head": head,
|
|
"orig": (hex(emu.d(prev_out)), bar0_writes),
|
|
"rust": (hex(exp_prev), [(reg, exp_next)]),
|
|
})
|
|
|
|
kdisp_r = emu.allocate(0x400)
|
|
emu.wd(kdisp_r + 0x300, 4)
|
|
out_x, out_y = emu.allocate(8), emu.allocate(8)
|
|
bar0_regs[1 * 0x800 + 0x616340] = 0x04380780
|
|
rc_r = emu.call("_nv040140rm", 0, kdisp_r, 1, out_x, out_y) & 0xFFFFFFFF
|
|
cases += 1
|
|
if rc_r != 0 or emu.d(out_x) != 1920 or emu.d(out_y) != 1080:
|
|
diffs.append({"fn": "_nv040140rm", "orig": (rc_r, emu.d(out_x), emu.d(out_y)), "rust": (0, 1920, 1080)})
|
|
|
|
# 4. IHUB_COMMON_CAPA (0x625000), CAPE (0x625010), and FE_CAP list (_nv024458rm)
|
|
capa = emu.call("_nv060382rm") & 0xFFFFFFFF
|
|
cape = emu.call("_nv060385rm") & 0xFFFFFFFF
|
|
cases += 2
|
|
if capa != 0x625000 or cape != 0x625010:
|
|
diffs.append({"fn": "ihub_caps", "orig": (hex(capa), hex(cape)), "rust": ("0x625000", "0x625010")})
|
|
|
|
fe_buf = emu.allocate(0x200)
|
|
emu.call("_nv024458rm", 0, fe_buf)
|
|
fe_regs = [emu.q(fe_buf + 0x20 + i * 8) for i in range(5)]
|
|
exp_fe = [0x611848, 0x611400, 0x611408, 0x61140C, 0x9080]
|
|
cases += 1
|
|
if fe_regs != exp_fe or emu.d(fe_buf + 0x120) != 5:
|
|
diffs.append({"fn": "_nv024458rm", "orig": fe_regs, "rust": exp_fe})
|
|
|
|
return {
|
|
"name": "sor_head_ihub_regs_unicorn",
|
|
"passed": len(diffs) == 0,
|
|
"cases": cases,
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def check_rodata_class_db_and_nv0073_table() -> dict:
|
|
"""Verify `.rodata` per-chip display class tables and `NV0073` ctrl table sizes."""
|
|
diffs = []
|
|
rodata = read_elf64_section(ORIGINAL, ".rodata")
|
|
|
|
# Verify all 15 NV0073 control IDs & exact parameter sizes in .rodata+0x4d984e0
|
|
found_ctrls: dict[int, int] = {}
|
|
o = 0x4D984E0
|
|
while o < len(rodata) - 32:
|
|
cmd, sz = struct.unpack_from("<II", rodata, o + 0x10)
|
|
if (cmd >> 16) != 0x0073:
|
|
break
|
|
found_ctrls[cmd] = sz
|
|
o += 0x20
|
|
|
|
expected_ctrls = {
|
|
0x00730101: 2,
|
|
0x00730102: 12,
|
|
0x00730103: 32,
|
|
0x00730104: 20,
|
|
0x00730105: 16,
|
|
0x00730106: 12,
|
|
0x00730109: 24,
|
|
0x00730116: 12,
|
|
0x00730129: 136,
|
|
0x00730240: 12,
|
|
0x00730245: 2064,
|
|
0x00730250: 72,
|
|
0x00730401: 16,
|
|
0x00730402: 28,
|
|
0x00731140: 16,
|
|
}
|
|
for cmd, exp_sz in expected_ctrls.items():
|
|
if found_ctrls.get(cmd) != exp_sz:
|
|
diffs.append({"ctrl": hex(cmd), "orig_sz": found_ctrls.get(cmd), "rust_sz": exp_sz})
|
|
|
|
# Verify DisplayProfile class IDs appear in .rodata with ENGINE_KERNEL_DISPLAY (0x55952e00)
|
|
expected_disp_classes = [
|
|
0x0073, 0x917A, 0x917B, 0x917E, 0x927C,
|
|
0x9470, 0x9471, 0x947D, 0x9570, 0x9571, 0x957D,
|
|
0x9770, 0x977D, 0x9870, 0x987D,
|
|
0xC370, 0xC371, 0xC372, 0xC373, 0xC37A, 0xC37B, 0xC37D, 0xC37E,
|
|
0xC570, 0xC573, 0xC57A, 0xC57B, 0xC57D, 0xC57E,
|
|
0xC670, 0xC671, 0xC673, 0xC67A, 0xC67B, 0xC67D, 0xC67E,
|
|
0xC770, 0xC771, 0xC773, 0xC77D, 0xC77F,
|
|
0xC970, 0xC971, 0xC973, 0xC97A, 0xC97B, 0xC97D, 0xC97E,
|
|
0xCA70, 0xCA71, 0xCA73, 0xCA7A, 0xCA7B, 0xCA7D, 0xCA7E,
|
|
0xCB70, 0xCB71, 0xCB73, 0xCB7A, 0xCB7B, 0xCB7D, 0xCB7E,
|
|
]
|
|
eng_tag = struct.pack("<I", 0x55952E00)
|
|
for cid in expected_disp_classes:
|
|
pat = struct.pack("<I", cid) + eng_tag
|
|
if rodata.find(pat, 0x1E800, 0x22000) == -1:
|
|
diffs.append({"class_db_missing": hex(cid)})
|
|
|
|
# Confirm 0xc870 does NOT exist in 580.178.04
|
|
if rodata.find(struct.pack("<I", 0xC870) + eng_tag, 0x1E800, 0x22000) != -1:
|
|
diffs.append({"unexpected_class": "0xc870"})
|
|
|
|
return {
|
|
"name": "rodata_class_db_and_nv0073_table_parity",
|
|
"passed": len(diffs) == 0,
|
|
"cases": len(expected_ctrls) + len(expected_disp_classes) + 1,
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def check_rm_kernel_rmapi_op_dispatch_unicorn(emu: Emulator) -> dict:
|
|
"""Execute `rm_kernel_rmapi_op` (`0x6ed9510`) in Unicorn across all 14 opcodes."""
|
|
emu.reset()
|
|
called: list[tuple[str, int]] = []
|
|
op_to_sym = {
|
|
0x00: "_nv000640rm",
|
|
0x02: "_nv000635rm",
|
|
0x15: "_nv000645rm",
|
|
0x20: "_nv000668rm",
|
|
0x21: "_nv000660rm",
|
|
0x22: "_nv000666rm",
|
|
0x27: "_nv000643rm",
|
|
0x2E: "_nv000658rm",
|
|
0x2F: "_nv000664rm",
|
|
0x31: "_nv000648rm",
|
|
0x36: "_nv000652rm",
|
|
0x37: "_nv000654rm",
|
|
0x3D: "_nv000642rm",
|
|
0x3E: "_nv000662rm",
|
|
}
|
|
for sym in op_to_sym.values():
|
|
emu.install(sym, lambda args, s=sym: (called.append((s, args[0])), 0)[1])
|
|
_stub_noop(emu, "_nv059517rm", 0)
|
|
|
|
sp = emu.allocate(0x40)
|
|
emu.wq(sp + 8, 0x123456789ABCDEF0) # stack canary top
|
|
cmd_buf = emu.allocate(0x40)
|
|
diffs = []
|
|
|
|
for op, exp_sym in op_to_sym.items():
|
|
called.clear()
|
|
emu.wd(cmd_buf, op)
|
|
emu.wq(cmd_buf + 8, 0x44440000 + op)
|
|
emu.call("rm_kernel_rmapi_op", sp, cmd_buf)
|
|
exp_arg = (0x44440000 + op) if op == 0x20 else (cmd_buf + 8)
|
|
if called != [(exp_sym, exp_arg)]:
|
|
diffs.append({"op": hex(op), "orig": called, "rust": [(exp_sym, hex(exp_arg))]})
|
|
|
|
return {
|
|
"name": "rm_kernel_rmapi_op_dispatch_unicorn",
|
|
"passed": len(diffs) == 0,
|
|
"cases": len(op_to_sym),
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def check_modeset_and_drm_gem_c_abi() -> dict:
|
|
"""Compile a C probe against installed 580.178.04 headers to verify exact struct sizes."""
|
|
c_src = r"""
|
|
#include <stdio.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include "kernel-open/modeset_bridge/nv_modeset_abi.h"
|
|
#include "/usr/src/nvidia-srv-580.178.04/nvidia-drm/nvidia-drm-ioctl.h"
|
|
|
|
int main(void) {
|
|
printf("nv_gpu_info_t=%zu:%zu,%zu,%zu\n",
|
|
sizeof(nv_gpu_info_t),
|
|
offsetof(nv_gpu_info_t, pci_info),
|
|
offsetof(nv_gpu_info_t, needs_numa_setup),
|
|
offsetof(nv_gpu_info_t, os_device_ptr));
|
|
printf("nvidia_modeset_callbacks_t=%zu\n", sizeof(nvidia_modeset_callbacks_t));
|
|
printf("nvidia_modeset_rm_ops_t=%zu\n", sizeof(nvidia_modeset_rm_ops_t));
|
|
printf("import_nvkms=%zu\n", sizeof(struct drm_nvidia_gem_import_nvkms_memory_params));
|
|
printf("export_nvkms=%zu\n", sizeof(struct drm_nvidia_gem_export_nvkms_memory_params));
|
|
printf("alloc_nvkms=%zu\n", sizeof(struct drm_nvidia_gem_alloc_nvkms_memory_params));
|
|
printf("export_dmabuf=%zu\n", sizeof(struct drm_nvidia_gem_export_dmabuf_memory_params));
|
|
printf("identify=%zu\n", sizeof(struct drm_nvidia_gem_identify_object_params));
|
|
printf("get_dev_info=%zu\n", sizeof(struct drm_nvidia_get_dev_info_params));
|
|
printf("prime_fence_ctx=%zu\n", sizeof(struct drm_nvidia_prime_fence_context_create_params));
|
|
return 0;
|
|
}
|
|
"""
|
|
expected = {
|
|
"nv_gpu_info_t": "24:4,12,16",
|
|
"nvidia_modeset_callbacks_t": "32",
|
|
"nvidia_modeset_rm_ops_t": "72",
|
|
"import_nvkms": "32",
|
|
"export_nvkms": "24",
|
|
"alloc_nvkms": "24",
|
|
"export_dmabuf": "24",
|
|
"identify": "8",
|
|
"get_dev_info": "36",
|
|
"prime_fence_ctx": "48",
|
|
}
|
|
diffs = []
|
|
with tempfile.TemporaryDirectory() as td:
|
|
src = Path(td) / "probe.c"
|
|
exe = Path(td) / "probe"
|
|
src.write_text(c_src)
|
|
subprocess.check_call(
|
|
["gcc", "-O2", "-I", str(ROOT), "-I", "/usr/include/libdrm", str(src), "-o", str(exe)]
|
|
)
|
|
out = subprocess.check_output([str(exe)], text=True)
|
|
got = dict(line.split("=", 1) for line in out.strip().splitlines())
|
|
for k, v in expected.items():
|
|
if got.get(k) != v:
|
|
diffs.append({"struct": k, "c_header": got.get(k), "rust": v})
|
|
|
|
return {
|
|
"name": "modeset_and_drm_gem_c_abi_parity",
|
|
"passed": len(diffs) == 0,
|
|
"cases": len(expected),
|
|
"diffs": diffs,
|
|
}
|
|
|
|
|
|
def run_checks() -> list[dict]:
|
|
emu = Emulator()
|
|
_install_os_mem_helpers(emu)
|
|
return [
|
|
check_disp_channel_class_map_unicorn(emu),
|
|
check_evo_channel_numbers_unicorn(emu),
|
|
check_nvdisp_channel_numbers_unicorn(emu),
|
|
check_user_area_and_sfuser_unicorn(emu),
|
|
check_disp_system_caps_unicorn(emu),
|
|
check_sor_head_ihub_regs_unicorn(emu),
|
|
check_rm_kernel_rmapi_op_dispatch_unicorn(emu),
|
|
check_rodata_class_db_and_nv0073_table(),
|
|
check_modeset_and_drm_gem_c_abi(),
|
|
]
|
|
|
|
|
|
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())
|