Files
nvdriver/tools/check_gv100_tlb_oracle.py
morebrackets 7818fd7a18 tools: make UVM/TLB/time C oracles portable and harden control compare/capture
- tools/check_uvm_ioctl_layout.py: resolve NVIDIA 580.178.04 sources via
  NV_RS_KERNEL_SRC / NV_RS_ANALYSIS_ROOT / /usr/src/nvidia-580.178.04 /
  /usr/src/nvidia-srv-580.178.04 instead of failing when /home/geodd is
  absent; replace -O-disabled asserts with explicit errors and argparse.
- tools/check_gv100_tlb_oracle.py and tools/check_system_time_source.py:
  resolve kernel sources portably, wrap execution in main(), create
  report parent dirs, and replace -O-disabled asserts.
- tools/compare_software_controls.py: wrap in main(), guard unknown
  ioctl kinds instead of KeyError, create output parent dirs, and use
  single-pass boundary-aware hex handle normalization so chained or
  prefix-overlapping handle mappings do not corrupt labels.
- tools/ioctl_capture/cuda_basic.py: set explicit 64-bit ctypes argtypes
  for all CUDA Driver API calls, fail non-zero on H2D/D2H roundtrip
  mismatch, and clean up allocations/context in a try/finally inside
  main().
- tests/offline/test_oracle_portability_and_capture.py: offline unit
  tests covering path resolution, CLI validation, single-pass handle
  normalization, and 64-bit ctypes signatures.
2026-12-02 04:16:27 -08:00

137 lines
5.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Differential vectors from exact580 shipped Pascal host functions (GV100 HAL inheritance)."""
import hashlib
import json
import os
import subprocess
import tempfile
from pathlib import Path
R = Path(__file__).resolve().parents[1]
def resolve_uvm_source_dir():
candidates = []
if os.environ.get("NV_RS_KERNEL_SRC"):
candidates.append(Path(os.environ["NV_RS_KERNEL_SRC"]) / "nvidia-uvm")
if os.environ.get("NV_RS_ANALYSIS_ROOT"):
candidates.append(Path(os.environ["NV_RS_ANALYSIS_ROOT"]) / "extracted/kernel/nvidia-uvm")
candidates.extend([
Path("/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia-uvm"),
Path("/usr/src/nvidia-580.178.04/nvidia-uvm"),
Path("/usr/src/nvidia-srv-580.178.04/nvidia-uvm"),
])
for candidate in candidates:
if (candidate / "uvm_pascal_host.c").is_file():
return candidate
raise SystemExit(
"Unable to locate uvm_pascal_host.c; set NV_RS_KERNEL_SRC or NV_RS_ANALYSIS_ROOT."
)
def function(text, name):
start = text.rfind("\nvoid ", 0, text.index(name + "(")) + 1
pos = text.index("{", start)
level = 1
end = pos + 1
while level:
level += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
PREFIX = r'''
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include "clc06f.h"
#include "clb06f.h"
typedef uint32_t NvU32;typedef int uvm_membar_t;
typedef struct {uint64_t address;int aperture;} uvm_gpu_phys_address_t;
typedef struct {uint32_t words[64];unsigned n,hshub;} uvm_push_t;
#define UVM_APERTURE_VID 0
#define UVM_APERTURE_SYS 1
#define UVM_MEMBAR_NONE 0
#define UVM_MEMBAR_GPU 1
#define UVM_MEMBAR_SYS 2
#define UVM_ASSERT_MSG(x,...) assert(x)
#define IS_ALIGNED(x,a) (((x)&((a)-1))==0)
#define FIELD(p,t,f) NV##p##_##t##_##f
#define HWSIZE(p,t,f) ((1?FIELD(p,t,f))-(0?FIELD(p,t,f))+1)
#define HWMASK(p,t,f) ((UINT32_C(1)<<HWSIZE(p,t,f))-1)
#define HWVALUE(p,t,f,v) (((uint32_t)(v)&HWMASK(p,t,f))<<(0?FIELD(p,t,f)))
#define HWCONST(p,t,f,c) HWVALUE(p,t,f,NV##p##_##t##_##f##_##c)
#define NV_PUSH_4U(p,a,x,b,y,c,z,d,w) do{push->words[push->n++]=HWCONST(B06F,DMA,SEC_OP,INC_METHOD)|HWVALUE(B06F,DMA,INCR_ADDRESS,NV##p##_##a>>2)|HWVALUE(B06F,DMA,INCR_SUBCHANNEL,0)|HWVALUE(B06F,DMA,INCR_COUNT,4);push->words[push->n++]=(x);push->words[push->n++]=(y);push->words[push->n++]=(z);push->words[push->n++]=(w);}while(0)
static void uvm_hal_tlb_invalidate_membar(uvm_push_t*,uvm_membar_t);
'''
# The original helper dispatches hshub GPU barriers then the requested barrier.
# Callback plumbing is replaced; the called encoding functions remain original C.
TAIL = r'''
static void uvm_hal_tlb_invalidate_membar(uvm_push_t*p,uvm_membar_t m){if(m==0)return;for(unsigned i=0;i<p->hshub;i++)uvm_hal_pascal_host_membar_gpu(p);if(m==1)uvm_hal_pascal_host_membar_gpu(p);else uvm_hal_pascal_host_membar_sys(p);}
int main(void){uint64_t addresses[]={0,4096,0x12345000ULL,0x12345678000ULL,0x7fffffffffff000ULL};for(unsigned a=0;a<5;a++)for(unsigned ap=0;ap<2;ap++)for(unsigned d=0;d<5;d++)for(unsigned m=0;m<3;m++)for(unsigned h=0;h<3;h++){uvm_push_t p={.hshub=h};uvm_gpu_phys_address_t pdb={addresses[a],ap};uvm_hal_pascal_host_tlb_invalidate_all(&p,pdb,d,m);printf("%llu %u %u %u %u",(unsigned long long)addresses[a],ap,d,m,h);for(unsigned n=0;n<p.n;n++)printf(" %u",p.words[n]);puts("");}}
'''
def main():
uvm_dir = resolve_uvm_source_dir()
source = (uvm_dir / "uvm_pascal_host.c").read_text()
body = "\n".join(
function(source, n)
for n in [
"uvm_hal_pascal_host_membar_sys",
"uvm_hal_pascal_host_membar_gpu",
"uvm_hal_pascal_host_tlb_invalidate_all",
]
)
with tempfile.TemporaryDirectory() as t:
p = Path(t)
(p / "oracle.c").write_text(PREFIX + body + TAIL)
subprocess.run(
[
"cc",
"-std=c11",
"-Wall",
"-Werror",
"-O2",
"-I",
str(uvm_dir),
"-I",
str(uvm_dir.parent / "common/inc"),
str(p / "oracle.c"),
"-o",
str(p / "oracle"),
],
check=True,
)
lines = subprocess.check_output([str(p / "oracle")], text=True).splitlines()
rows = [list(map(int, line.split())) for line in lines]
output = [
"// Generated by tools/check_gv100_tlb_oracle.py from exact shipped C host functions.",
"pub type Vector = (u64, u8, u8, u8, u32, &'static [u32]);",
"#[rustfmt::skip]",
"pub const VECTORS: &[Vector] = &[",
]
for a, ap, d, m, h, *words in rows:
output.append(f"({a},{ap},{d},{m},{h},&{words}),")
output.append("];")
(R / "src/nvidia/src/mmu/tlb_oracle_vectors.rs").write_text("\n".join(output) + "\n")
report = {
"passed": True,
"vectors": len(rows),
"execution": "Pure original C host method encoders only; no driver or GPU execution",
"callback_plumbing": "Original membar helper dispatch represented by finite callback shim; original GPU/SYS encoders unchanged",
"source_hashes": {
str(uvm_dir / n): hashlib.sha256((uvm_dir / n).read_bytes()).hexdigest()
for n in ["uvm_pascal_host.c", "uvm_hal.c", "uvm_push_macros.h", "clc06f.h", "clb06f.h"]
},
}
report_path = R / "reports/gv100-tlb-oracle.json"
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(json.dumps(report, indent=2) + "\n")
print(len(rows), "actual C TLB method vectors")
if __name__ == "__main__":
main()