# nvidia-driver-rs: agent rules These rules apply to every agent and subagent working in this directory. Put them in every subagent brief that could touch hardware, the original driver, or driver logic. --- ## 1. Exact-logic parity with the original 580.178.04 driver (7-step REA-first loop) We are building a clean-room Rust driver that matches the **exact behavior** of NVIDIA 580.178.04 on GV100 (Tesla V100) and Maxwell–Blackwell — not approximations, guesses, or placeholder stubs. ### Mandatory 7-step development loop (never "guess -> try -> watch it break -> patch symptom") 1. **REA to map the architecture first:** Understand the subsystem's full call graph, object ownership, state machines, and lock hierarchy before writing any code. 2. **REA the exact algorithm step-by-step for each function:** Decompile/disassemble the original 580.178.04 function and its callees completely (struct layouts, field offsets, bitmasks, error ordering, hardware register sequences). 3. **Implement the full algorithm faithfully and verify offline first:** Match the derived algorithm exactly and verify bit-for-bit against Unicorn binary oracles (`tools/mock_gv100_diff.py`), unit/oracle tests, and QEMU guest tests before touching real hardware. 4. **Record canonical actions on the real card + real stock driver:** Capture baseline ioctl/RM/UVM transcripts and output digests for common workloads (open/close, alloc/free, H2D/D2H/D2D, kernel launch, streams/graphs/VMM, cuBLAS/cuBLASLt/cuFFT/cuRAND, multi-process). 5. **Replay those exact recorded actions against `nvidia_rs.ko`:** Run side-by-side (`tools/dual_gpu_parity.py --no-rebind` or disposable VFIO QEMU/KVM VMs via `tools/vm/run_vfio_guest.py`). 6. **For every failed or diverging scenario, REA what the original driver does differently:** Fix the root algorithm/architecture, never a one-off symptom. 7. **Keep all 8 V100s productive via VFIO VMs & `--no-rebind`:** All 8 Tesla V100s on `geodd03` sit in isolated single-device IOMMU groups (`40, 41, 76, 77, 129, 130, 165, 166` with `intel_iommu=on iommu=pt`), allowing parallel disposable QEMU/KVM VFIO VMs (`tools/vm/run_vfio_guest.py --bdf `) so a kernel bug or hang costs a ~10-second VM restart instead of a bare-metal host reboot. Never rebind a GPU back to stock `nvidia.ko` after `nvidia_rs.ko` or `vfio-pci` has touched it unless explicitly required and verified clean. 1. **Plan with REA before implementing.** - Before writing or changing any RM control, RM class constructor/destructor, UVM ioctl, MMU/page-table format, FIFO/runlist/CCSR sequence, FECS/GPCCS/PMU/SEC2/ACR protocol, or GR context sequence, inspect the corresponding function(s) in the closed driver (`nv-kernel.o_binary`, `nvidia-uvm.ko`, `libcuda.so.580.178.04`) using the **REA MCP tools**. - Do **not** guess struct layouts, field offsets, bitfields, status codes, error ordering, or hardware register sequences. 2. **Use the full REA toolset — not only Ghidra pseudocode.** - Combine the right REA tools for the question: - `procedure_pseudo_code` for high-level control flow and status ordering. - `procedure_assembly` / `inspect_with_objdump` / `read_bytes` when calling conventions, exact immediates, bitmasks, shifts, or struct offsets matter. - `procedure_callees`, `procedure_callers`, `resolve_native_call_targets`, `xrefs`, `get_call_graph`, and `trace_call_path` to follow HAL/vtable dispatches and verify every side effect. - `trace_native_values` to track register/struct field values through functions. - `inspect_kernel_module`, `inspect_native_exports`, `inspect_binary_layout`, `search_bytes`, `search_strings` for module interfaces, tables, and firmware/netlist data. - `compare_functions` / `correlate_static_and_runtime` / `capture_process_scenario` (userspace only — never kprobes/mmiotrace on the loaded original driver) to cross-check static analysis with observed userspace behavior. - Treat the shared Ghidra project as **read-only**: never call `close_binary`, `reset_investigation`, or annotation-mutating tools. 3. **Verify exact logic after implementing.** - Cross-check every implemented handler, bit encoding, error status, and register write sequence directly against the REA decompile/disassembly of the original driver and, where available, against the original-instruction oracles (`oracle_tests`, `channel_oracle_tests`, `tools/check_*`). - Whenever a live hardware probe (`runtime_engines=1`) diverges or returns a non-zero RM/UVM status, immediately inspect the failing handler and its caller in REA before patching. - Keep evidence categories strictly separate in notes and reports (`[T]`/`[O]` observed original behavior, `[A]`/`[D]`/`[E]` static/emulated analysis, guest QEMU tests, replacement hardware execution). 4. **Validation gate.** - Always pass 30/30 `tools/validate_all.sh` (including `check_kernel_module.py`: 0 GOT relocations, 0 `memcpy`/`memset`/`memmove`, stack <= 1,024 bytes, no SIMD) on a quiet tree before any hardware run. --- ## 2. Machine safety rules (geodd03) This machine (geodd03, eight Tesla V100s) is in a data center. A hang that needs a hard power cycle costs about four hours of human response time. These rules come from two incidents on 2026-11-18 and 2026-11-19 (details in `HANDOFF.md` and `reports/incident-2026-11-19-reboot-hang.md`). ### What went wrong 1. A PCI bus reset of one GPU (`echo 1 > /sys/bus/pci/devices//reset`) left the card un-posted. The original `nvidia` driver then failed its next first open and crashed in its own cleanup path, inside its kernel thread `nv_open_q`. 2. That thread died holding the driver's locks. Every later open of any `/dev/nvidia*` node, on all eight GPUs, blocked forever in uninterruptible sleep (state `D`). 3. A normal `sudo reboot` was then issued. systemd (PID 1) stopped responding seconds later, while it was stopping the login session that contained the stuck processes. It never stopped another unit, so the reboot never happened. sshd and cron kept running for 50 minutes, but new logins timed out. Only a power cycle recovered the machine. The exact kernel wait PID 1 was stuck in is not known; nothing recorded its stack. A normal reboot did work the day before, when a user process (not the driver's kernel thread) had crashed. Do not rely on that. ### Never do these - Never reset a GPU through sysfs (`.../reset`, `.../remove` + rescan) or with `setpci` bridge resets. The original driver cannot recover from it on this board. - Never trace the original driver with `mmiotrace` across a workload. It oopses on page-crossing copies. Any mmiotrace needs the user's explicit confirmation. - Never run `reboot`, `shutdown`, `systemctl reboot` or `poweroff` yourself. Rebooting is the user's decision. If you believe a reboot is needed, say so and give them the procedure below. - Never run a hardware experiment whose failure mode is "the original driver cannot initialize this GPU afterwards" without first telling the user that the cost of failure is a reboot that may hang, and getting a yes for that specific experiment. The standing approval for bring-up levels on `0000:b3:00.0` does not cover this. ### Before any action that touches a GPU or the original driver 1. Check the driver is healthy: `cat /proc/sys/kernel/tainted` shows no new oops bit, and `ps -eo stat,comm | grep -E '^D.*(nv_open_q|nvidia)'` prints nothing. 2. If either check fails, stop all GPU work. Do not run `nvidia-smi`, CUDA programs, `tools/post_reboot.sh`, `tools/gpu_binding.py` or anything else that opens `/dev/nvidia*`: each one becomes another unkillable process. Report to the user. 3. Give every command that opens `/dev/nvidia*` its own short `timeout`, run it in the foreground, and check the result before the next step. A command that times out is a stop signal, not something to retry. 4. Bring-up levels: validate first (`tools/validate_all.sh`), run only through `BRINGUP= sh tools/post_reboot.sh --first-open`, one GPU (`0000:b3:00.0`), baseline before and after. If a baseline fails, stop and report. ### If the original driver has oopsed or its opens hang Tell the user plainly, and give them this. Do not run it yourself. - A normal reboot may hang systemd. Use the kernel's emergency path instead, from a root shell that is **already open** (new SSH logins may time out): ```sh sudo sh tools/emergency_reboot.sh --confirm ``` It asks the kernel to sync and remount filesystems read-only, then restarts immediately through `/proc/sysrq-trigger`, without going through systemd or the driver's shutdown hooks. - Before rebooting, if there is time, save evidence for root-causing: `sudo sh tools/emergency_reboot.sh --capture` writes all task stacks and PID 1's stack to `reports/`. - Keep one root shell open in a separate SSH session for the whole duration of any hardware experiment, so the emergency path stays reachable. - After the machine is back: `sh tools/post_reboot.sh` (health check only), then continue. ### Machine hardening in place (configured 2026-11-19 with the user's approval) These make a failure cheaper. They do not make the rules above optional. - **Kernel oops now restarts the machine.** `/etc/sysctl.d/90-panic-on-oops.conf` sets `kernel.panic_on_oops=1` and `kernel.panic=30`. An oops in any driver, including `nvidia_rs` during the live smoke or a bring-up run, panics the kernel and the machine restarts by itself about 30 seconds later. Every session and agent on the machine is lost when that happens, so write results to disk as you go and keep `HANDOFF.md` current before each hardware run. - **Hardware watchdog.** `ipmi_watchdog` is loaded at boot and systemd pets it (`/etc/systemd/system.conf.d/10-watchdog.conf`, `RuntimeWatchdogSec=10min`). If PID 1 hangs again, the BMC hard-resets the machine within 10 minutes. Check it with `sudo ipmitool mc watchdog get` ("Started/Running", action "Hard Reset"). - **Crash dumps.** `kdump-tools` is installed with `crashkernel=512M-:512M`; it is active only from the first boot after 2026-11-19 (`kdump-config show` says "ready to kdump"). Dumps land in `/var/crash`. Read the dump's `dmesg` before repeating whatever caused it. - **BMC access.** `ipmitool` is installed. `sudo ipmitool chassis power reset` works from the machine itself even when systemd is hung. Power control from another host needs the BMC's network address and credentials, which only the user has. Do not change, disarm or work around any of these. If one of them fires, the first job of the next session is to find out why (`/var/crash`, `journalctl -b -1`, `sudo ipmitool sel elist`) and record it in `HANDOFF.md` before any further hardware work.