# CLAUDE.md This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. ## Rules that override everything below All agent, REA parity-verification, and machine safety rules for this repository are maintained in [AGENTS.md](AGENTS.md). Follow it in full, and copy its rules into every subagent brief that could touch hardware, the original driver, or driver logic. @AGENTS.md The points most likely to be violated by accident: - Driver logic is never guessed. Any RM control, UVM ioctl, MMU format, FIFO/runlist sequence or firmware protocol is derived from the closed 580.178.04 binaries with the REA MCP tools first, then implemented, then checked against oracles (AGENTS.md §1). - Never run `reboot`/`shutdown`/`poweroff`, never reset a GPU through sysfs or `setpci`, never mmiotrace the original driver. Before anything that opens `/dev/nvidia*`, run the health checks in AGENTS.md §2 and give the command its own `timeout`. - `tools/validate_all.sh` is not purely offline: its last step (`live`) runs `sudo -n python3 tools/run_live_control.py`, which `insmod`s `nvidia_rs.ko` with an empty `pci_target` (no GPU binding) and exercises `/dev/nvidia-rsctl`. With `kernel.panic_on_oops=1` on geodd03, a bug there restarts the machine. - `make modules_install` refuses to run without `INSTALL_MOD_PATH` pointing at a staging directory. Replacing the working driver is done only through `./install.sh`, and only when the user asks. - Never merge a PR containing code changes without CodeRabbit review and approval (AGENTS.md §3). Do not merge while CodeRabbit is rate-limited, unreviewed, or has unresolved findings. ## Commands Run everything from the repository root so rustup picks the pinned toolchain (`rust-toolchain.toml`, 1.100.0, target `x86_64-unknown-none`). ```sh sh tools/validate_all.sh # the 36-step gate; one PASS/FAIL/SKIP line per step, logs in target/validate-logs/.log make modules # Rust core -> nv-rust.o_binary, Kbuild -> kernel-open/*.ko, then tools/check_kernel_module.py make check # fmt + clippy -D warnings + `--check` of every generated table (needs REA_PYTHON) make test # cargo test --workspace make uvm-bridge # CONFIG_MODVERSIONS bridge for stock nvidia-uvm.ko + symbol conformance check make modeset-bridge # same for stock nvidia-modeset.ko make -C src/nvidia audit-codegen && python3 tools/check_native_codegen.py ARCH=arm64 make modules # aarch64-unknown-none build ``` Single tests: ```sh cargo test -p nv-rs-core # unit test inside src/nvidia/src cargo test -p nv-rs-core --test gr_load # one integration test file from src/nvidia/tests/ sh tools/test_dma_bridge.sh # one C<->Rust host fixture (see the list in validate_all.sh; two of them need bash) python3 tools/vm/run_guest.py [--iommu] # QEMU guest tests (after tools/vm/build_guest_test.sh and build_initramfs.sh) python3 tools/mock_gv100_diff.py # Unicorn oracles: Rust vs. original 580 instructions ``` Kernel tree selection: `KERNEL_UNAME`, `KERNEL_SOURCES`/`SYSSRC`, `KERNEL_OUTPUT`/`SYSOUT`. `utils.mk` turns on `LLVM=1` for clang-built kernels and the IBT flags (`-Z cf-protection=branch`, `RUSTC_BOOTSTRAP=1`) when the kernel has `CONFIG_X86_KERNEL_IBT=y`; override with `NV_RS_KERNEL_IBT=0/1`. When a `kernel-open` build breaks on a removed kernel symbol, read `docs/KERNEL_COMPAT.md` before adding a version guard: the preferred fix mirrors what stock 580 does without a guard. Live testing on a real GPU goes through a disposable VFIO VM (`sudo python3 tools/vm/run_vfio_guest.py --bdf --driver rust`) or `tools/dual_gpu_parity.py --no-rebind`, never by rebinding a card back to stock `nvidia.ko`. ## Architecture The module is a Rust static object linked into a thin C shim. The kernel does not need `CONFIG_RUST`. ``` src/common/abi nv_rs_abi #![no_std], forbid(unsafe) C/Rust wire structs and ioctl layouts src/nvidia/src nv_rs_core #![no_std], forbid(unsafe) all driver logic: gpu/, mmu/, fifo/, rmapi/, uvm/, ... src/nvidia/kernel nv_rs_kernel staticlib, unsafe allowed FFI boundary + live bring-up (ACR, PMU, FIFO, GR, GSP, GA10x) kernel-open/nvidia C PCI/chardev/DMA/IRQ/hwmon/dma-buf adapters -> nvidia_rs.ko ``` - **Build pipeline** (`src/nvidia/Makefile`): `rustc` is invoked directly, not through cargo, with the kernel flags from `utils.mk` (soft-float, no redzone, kernel code model, `panic=abort`). `nv_rs_abi` and `nv_rs_core` become rlibs, `nv_rs_kernel` a fat-LTO staticlib, and `ld -r --gc-sections` keeps only what is reachable from an explicit `-u nv_rs_` list. **A new `#[no_mangle] nv_rs_*` export must be added to that list** or it is garbage-collected and the C side fails to link. The result, `kernel-open/nvidia/nv-rust.o_binary`, is copied to `rust-core.o` by `kernel-open/Kbuild`. - **Cargo is for host tests only.** The workspace has two members (`src/common/abi`, `src/nvidia`); `nv_rs_kernel` is not a cargo crate and is compiled for the host only by the `tools/test_*.sh` fixtures and the VM guest build. - **C shim rule of thumb:** each `kernel-open/nvidia/*_adapter.c` owns one kernel facility (PCI, DMA, IRQ, timers, threads, registry, ROM) and has a matching Rust module in `src/nvidia/kernel/` and a mock under `tests/-mock/` that compiles the same C with `NV_RS_*_HOST_FIXTURE`. Buffer ownership and user copies stay in C; Rust receives kernel-copied bytes and sized/aligned state allocations. - **Device nodes:** `rmapi_node.c` (RM API, the `/dev/nvidia*` ioctl surface) and `uvm_node.c` (integrated UVM) route into `kernel/rmapi_node.rs`, `rmapi_engine.rs` and `uvm_node.rs`, which dispatch into `nv_rs_core::rmapi` and `nv_rs_core::uvm`. `diagnostic.c` is the isolated `/dev/nvidia-rsctl` control node used by the live gate step. Module parameters are declared in the shim file that owns the feature (mostly `gpu_owner.c` and `rmapi_node.c`). - **Chip selection:** `src/nvidia/src/gpu/chip_arch.rs` maps `PMC_BOOT` to an `ArchFamily` and MMU format; pre-GSP families (Maxwell, Pascal, Volta) run natively, Turing and later go through the GSP-RM transport (`gpu/gsp_*.rs`, `kernel/gsp_bringup.rs`, ABI chosen by `gsp_abi`), and GA10x has its own split-RM path in `kernel/ga10x*.rs`. - **Sibling modules** (`kernel-open/nvidia-modeset`, `nvidia-drm`, `nvidia-peermem`) are built by the same Kbuild; `uvm_bridge` and `modeset_bridge` are separate builds that export stock-CRC symbols so unmodified `nvidia-uvm.ko` / `nvidia-modeset.ko` load against `nvidia_rs.ko`. ### Kernel-object constraints `tools/check_kernel_module.py` runs at the end of every `make modules` and fails the build on: GOT relocations, SIMD/x87 instructions, calls to `memcpy`/`memset`/`memmove`, unwind sections, or any stack frame over 1,024 bytes. In practice: no large by-value structs or arrays on the stack in `nv_rs_core`/`nv_rs_kernel`, no floating point, and large state is initialised in place (the FFI layer uses volatile byte stores for this). Panics compile to `UD2`, and `overflow-checks` is on, so an arithmetic overflow is a kernel oops. ### Generated code and oracle vectors - Files headed `// Generated by tools/...; do not edit` (e.g. `src/nvidia/src/hardware/generated.rs`, the RM API class/control tables) are regenerated with the matching `tools/generate_*.py`; `make check` runs each with `--check`. - `*_vectors.rs` files next to a module are recorded outputs of the original driver's instructions (Unicorn or compiled-C oracles). A test that disagrees with them means the Rust is wrong; regenerate vectors only through the generating tool, never by hand. - `*_tests.rs` sits beside the module it tests and is pulled in under `#[cfg(test)]`. ### Evidence and history - `HANDOFF.md` is the running engineering log (latest status at the top, current artifact digests); `reports/HANDOFF-history-through-2026-11-20.md` holds older history. Update `HANDOFF.md` before any hardware run. - `reports/-rea/` holds per-subsystem reverse-engineering evidence, `analysis/` the extracted reference material from the closed driver. Keep the evidence categories from AGENTS.md §1.3 separate when adding to them. - `docs/PARITY_AND_UNLOCKS.md` is the source of truth for what is verified on silicon versus offline only. Read it before claiming parity for anything.