mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
234 lines
8.8 KiB
Python
234 lines
8.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Compile actual shipped580 inherited Volta kernel GPFIFO setter in an isolated C harness."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def _discover_kernel_source(explicit=None) -> Path:
|
|
if explicit is not None:
|
|
return Path(explicit).expanduser().resolve()
|
|
candidates = []
|
|
env_kernel = os.environ.get("NV_RS_KERNEL_SRC")
|
|
if env_kernel:
|
|
candidates.append(Path(env_kernel).expanduser())
|
|
env_analysis = os.environ.get("NV_RS_ANALYSIS_ROOT")
|
|
if env_analysis:
|
|
candidates.append(Path(env_analysis).expanduser() / "extracted/kernel")
|
|
candidates.extend(
|
|
[
|
|
Path.home() / "nvidia-driver/analysis-580.178.04/extracted/kernel",
|
|
Path("/usr/src/nvidia-580.178.04"),
|
|
Path("/usr/src/nvidia-srv-580.178.04"),
|
|
]
|
|
)
|
|
for cand in candidates:
|
|
if (cand / "nvidia-uvm/uvm_hal.c").is_file() and (
|
|
cand / "common/inc/nvtypes.h"
|
|
).is_file():
|
|
return cand.resolve()
|
|
return candidates[0].resolve()
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Generate or verify Volta inherited kernel GPFIFO C-oracle vectors."
|
|
)
|
|
parser.add_argument(
|
|
"--source",
|
|
type=Path,
|
|
default=None,
|
|
help="Path to 580.178.04 extracted/installed kernel source tree",
|
|
)
|
|
parser.add_argument("--check", action="store_true")
|
|
args = parser.parse_args(argv)
|
|
|
|
src_root = _discover_kernel_source(args.source)
|
|
uvm_dir = src_root / "nvidia-uvm"
|
|
required = [
|
|
src_root / "common/inc/nvtypes.h",
|
|
src_root / "common/inc/nvmisc.h",
|
|
uvm_dir / "uvm_hal.c",
|
|
uvm_dir / "uvm_maxwell_host.c",
|
|
uvm_dir / "uvm_push_macros.h",
|
|
uvm_dir / "clc36f.h",
|
|
uvm_dir / "cla16f.h",
|
|
uvm_dir / "uvm_channel.c",
|
|
]
|
|
for req in required:
|
|
if not req.is_file():
|
|
raise SystemExit(
|
|
f"Missing 580.178.04 kernel source file: {req} "
|
|
"(set NV_RS_KERNEL_SRC, NV_RS_ANALYSIS_ROOT, or --source)"
|
|
)
|
|
|
|
source = (uvm_dir / "uvm_hal.c").read_text()
|
|
table = source[source.index("static uvm_hal_class_ops_t host_table[]") :]
|
|
table = table[: table.index("\n};")]
|
|
entries = {}
|
|
starts = list(re.finditer(r"\.id\s*=\s*([A-Z0-9_]+)\s*,", table))
|
|
for i, m in enumerate(starts):
|
|
body = table[m.end() : starts[i + 1].start() if i + 1 < len(starts) else len(table)]
|
|
parent = re.search(r"\.parent_id\s*=\s*([A-Z0-9_]+)", body)
|
|
setter = re.search(r"\.set_gpfifo_entry\s*=\s*([a-z0-9_]+)", body)
|
|
entries[m[1]] = (
|
|
parent[1] if parent else None,
|
|
setter[1] if setter else None,
|
|
)
|
|
|
|
chain = []
|
|
name = "VOLTA_CHANNEL_GPFIFO_A"
|
|
visited = set()
|
|
while True:
|
|
if name in visited or name not in entries:
|
|
raise SystemExit(f"Invalid UVM host HAL inheritance chain at {name!r}")
|
|
visited.add(name)
|
|
chain.append(name)
|
|
parent, setter = entries[name]
|
|
if setter:
|
|
break
|
|
if not parent:
|
|
raise SystemExit(f"No set_gpfifo_entry setter or parent found for {name!r}")
|
|
name = parent
|
|
|
|
if setter != "uvm_hal_maxwell_host_set_gpfifo_entry":
|
|
raise SystemExit(f"Unexpected Volta set_gpfifo_entry setter: {setter!r}")
|
|
turing_entry = entries.get("TURING_CHANNEL_GPFIFO_A")
|
|
if not turing_entry or turing_entry[1] != "uvm_hal_turing_host_set_gpfifo_entry":
|
|
raise SystemExit(f"Unexpected Turing set_gpfifo_entry override: {turing_entry!r}")
|
|
|
|
text = (uvm_dir / "uvm_maxwell_host.c").read_text()
|
|
a = text.index("void " + setter + "(")
|
|
b = text.index("{", a)
|
|
depth = 1
|
|
idx = b + 1
|
|
while depth:
|
|
depth += (text[idx] == "{") - (text[idx] == "}")
|
|
idx += 1
|
|
function = text[a:idx]
|
|
macros = (uvm_dir / "uvm_push_macros.h").read_text()
|
|
a = macros.index("#define HWCONST(d,")
|
|
b = macros.index("\n\n", a)
|
|
macro_text = macros[a:b]
|
|
if "PRIV, KERNEL" not in function:
|
|
raise SystemExit("Expected PRIV, KERNEL in uvm_hal_maxwell_host_set_gpfifo_entry")
|
|
|
|
c_src = (
|
|
"""#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <assert.h>
|
|
#include "nvtypes.h"
|
|
#include "nvmisc.h"
|
|
#include "cla16f.h"
|
|
#include "clc36f.h"
|
|
#define UVM_ASSERT(x) assert(x)
|
|
#define UVM_ASSERT_MSG(x,...) assert(x)
|
|
#define HWMASK(d,r,f) DRF_MASK(NV ## d ## _ ## r ## _ ## f)
|
|
typedef enum {UVM_GPFIFO_SYNC_PROCEED=0,UVM_GPFIFO_SYNC_WAIT=1} uvm_gpfifo_sync_t;
|
|
static bool uvm_global_is_suspended(void){return false;}
|
|
_Static_assert(DRF_DEF(C36F,_GP_ENTRY1,_PRIV,_KERNEL)==(1u<<8),"Volta PRIV_KERNEL bit");
|
|
"""
|
|
+ macro_text
|
|
+ "\n"
|
|
+ function
|
|
+ """
|
|
int main(void){NvU64 addresses[]={0,4,0x105000,0x1234567800ULL,0xffff000000ULL};NvU32 lengths[]={4,8,48,4096,65536};
|
|
for(unsigned a=0;a<5;a++)for(unsigned n=0;n<5;n++)for(unsigned w=0;w<2;w++){
|
|
NvU64 result;uvm_hal_maxwell_host_set_gpfifo_entry(&result,addresses[a],lengths[n],w?UVM_GPFIFO_SYNC_WAIT:UVM_GPFIFO_SYNC_PROCEED);
|
|
NvU32 lo=DRF_NUM(C36F,_GP_ENTRY0,_GET,NvU64_LO32(addresses[a])>>2);NvU32 hi=DRF_NUM(C36F,_GP_ENTRY1,_GET_HI,NvU64_HI32(addresses[a]))|DRF_NUM(C36F,_GP_ENTRY1,_LENGTH,lengths[n]>>2)|DRF_DEF(C36F,_GP_ENTRY1,_PRIV,_KERNEL)|DRF_NUM(C36F,_GP_ENTRY1,_SYNC,w);
|
|
if(result!=(lo|((NvU64)hi<<32))){fprintf(stderr,"C36F mismatch\\n");return 1;}printf("(%llu,%u,%s,%llu),\\n",(unsigned long long)addresses[a],lengths[n],w?"true":"false",(unsigned long long)result);
|
|
}return 0;}
|
|
"""
|
|
)
|
|
with tempfile.TemporaryDirectory(prefix="nv-rs-native-gpfifo-") as tmp:
|
|
path = Path(tmp)
|
|
(path / "oracle.c").write_text(c_src)
|
|
subprocess.run(
|
|
[
|
|
"cc",
|
|
"-std=gnu11",
|
|
"-Wall",
|
|
"-Wextra",
|
|
"-Werror",
|
|
"-UNDEBUG",
|
|
f"-I{src_root / 'common/inc'}",
|
|
f"-I{uvm_dir}",
|
|
str(path / "oracle.c"),
|
|
"-o",
|
|
str(path / "oracle"),
|
|
],
|
|
check=True,
|
|
)
|
|
rows = subprocess.check_output([str(path / "oracle")], text=True)
|
|
|
|
generated = (
|
|
"// Actual shipped580 Volta inherited kernel setter and C36F class macros.\n"
|
|
"const CASES: &[(u64,u32,bool,u64)] = &[\n"
|
|
+ rows
|
|
+ "];\n"
|
|
)
|
|
out = ROOT / "tests/oracles/native-gpfifo-vectors.rs"
|
|
files = [
|
|
uvm_dir / n
|
|
for n in [
|
|
"uvm_hal.c",
|
|
"uvm_maxwell_host.c",
|
|
"uvm_push_macros.h",
|
|
"clc36f.h",
|
|
"cla16f.h",
|
|
"uvm_channel.c",
|
|
]
|
|
]
|
|
report = dict(
|
|
passed=True,
|
|
inheritance=chain,
|
|
resolved_setter=setter,
|
|
turing_override=entries["TURING_CHANNEL_GPFIFO_A"][1],
|
|
kernel_privilege_bit=8,
|
|
cases=50,
|
|
files={str(f): hashlib.sha256(f.read_bytes()).hexdigest() for f in files},
|
|
scope="Actual shipped580 C setter body compiled unchanged with lifecycle/assert adapters and its HWVALUE/HWCONST macros; C36F class independently checks resulting word. Exact ordinary kernel-owned privileged UVM/acceptance-channel policy, not user CUDA channels or GPU execution.",
|
|
ramfc_requirement="Configure actual kernel channel privilege (selected027974 sets RAMFC HCE_CTRL0xe4 bit5).",
|
|
rust_format="HostFormat::MaxwellKernel",
|
|
)
|
|
report_path = ROOT / "reports/native-gpfifo-privilege-proof.json"
|
|
if args.check:
|
|
if not out.is_file() or out.read_text() != generated:
|
|
raise SystemExit("Native Volta GPFIFO vectors differ")
|
|
if not report_path.is_file():
|
|
raise SystemExit(f"Missing native GPFIFO privilege-proof report: {report_path}")
|
|
existing = json.loads(report_path.read_text())
|
|
for key in (
|
|
"passed",
|
|
"inheritance",
|
|
"resolved_setter",
|
|
"turing_override",
|
|
"kernel_privilege_bit",
|
|
"cases",
|
|
"scope",
|
|
"ramfc_requirement",
|
|
"rust_format",
|
|
):
|
|
if existing.get(key) != report[key]:
|
|
raise SystemExit(f"Native GPFIFO privilege-proof report differs for {key!r}")
|
|
existing_hashes = {Path(k).name: v for k, v in existing.get("files", {}).items()}
|
|
expected_hashes = {Path(k).name: v for k, v in report["files"].items()}
|
|
if existing_hashes != expected_hashes:
|
|
raise SystemExit("Native GPFIFO privilege-proof report file hashes differ")
|
|
else:
|
|
out.write_text(generated)
|
|
report_path.write_text(json.dumps(report, indent=2) + "\n")
|
|
print("50 actual shipped580 inherited Volta GPFIFO setter/class cases passed")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|