diff --git a/docs/TURING_OPTIMIZATION.md b/docs/TURING_OPTIMIZATION.md new file mode 100644 index 0000000..a8d7102 --- /dev/null +++ b/docs/TURING_OPTIMIZATION.md @@ -0,0 +1,55 @@ +# Turing (TU104 / TU106 / TU116) WindowServer Stutter & 256 MB BAR1 Fix + +## Overview + +On macOS 15 running NVIDIA Turing GPUs (RTX 2060 SUPER / TU106, RTX 2070 / TU104, GTX 1660 / TU116) with a 256 MB BAR1 aperture, desktop interaction can exhibit: +- UI micro-stutters when dragging windows or resuming interaction after brief idle periods. +- WindowServer surface allocation drops or black window contents under rapid compositing. + +## Root Causes + +1. **Metal Small-BAR1 Pooling & Headroom (`nvrm610.conf`)** + Default Metal pooling and buffer grace settings hoard GPU allocations and exhaust a 256 MB BAR1 window under WindowServer load. Setting `NVMTL_VRAM_HEADROOM_MB=48`, enabling `NVMTL_NO_SURFACE_PAGEOFF=0`, disabling buffer reuse/grace pools (`NVMTL_NO_BUFREUSE=1`, `NVMTL_IMAGE_POOL=0`, `NVMTL_PRIVATE_POOL=0`, `NVMTL_BUFFER_GRACE=0`, `NVMTL_IMAGE_GRACE=0`), and enabling `NVMTL_GSUB_HOSTPTR_LIFT=1` stabilizes surface allocation. + +2. **Framebuffer 256 MB VRAM Grant Ceiling (`NVRMFB.kext`)** + On driver `3.0` (macOS 15), `vramGrant` in `NVRMFB` checks an internal 256 MB grant ceiling at file offset `0x21ea3` (`0f 8e 6d 01 00 00` -> `e9 6e 01 00 00 90`), returning `kIOReturnNoMemory` under heavy WindowServer surface turnover. + +3. **Inactivity Surface Eviction & Sticky VM Churn (`NVAccel.kext`)** + On driver `3.0` (macOS 15), `gSurfGpuOn` (`0x251e0`) and `gVmSticky` (`0x23f1c`) evict idle surfaces and retain sticky VM mappings (`debug.nvaccel_surface_gpu_on`, `debug.nvaccel_vm_sticky`). + +## Architecture Split in 1401 + +1. **Main Installation Path (Safe Metal Rules — `mac/Resources/nullmoth-rules.json`)** + `turing-bar1-stutter-optimization` matches `{"arch": ["turing"]}` and passes its `NVMTL_*` / `NVK_*` knobs through `nullmoth-setup.sh` into `# --- 1401 per-system rules ---` inside `/Library/GPUBundles/nvmtl/nvrm610.conf`. + `nullmoth-setup.sh` never modifies kext binaries in place, preserving SHA-256 package verification, per-OS kext manifests (`/Library/NullMoth/kexts/{15,26}`), and audited uninstallation. + +2. **Optional Standalone Kext Tool (`tools/apply-turing-fix.sh`)** + Users who still hit `NVRMFB`/`NVAccel` drops on verified driver `3.0` / macOS `15` builds can run `tools/apply-turing-fix.sh` manually. Before touching any kext binary, it verifies: + - Root privileges and disabled SIP filesystem protections. + - macOS major `15`, installed driver version `3.0`, and a Turing PCI device ID (`1e00..21ff`). + - 64-bit Mach-O magic, bounds, expected `NVAccel` sysctl symbol strings, optional `--accel-sha256` / `--fb-sha256` pins, and exact expected bytes at each offset. + - SHA-256-recorded backups under `/Library/NullMoth/turing-backup/` and `kmutil inspect` verification with automatic rollback on failure. + - Non-destructive marked `# --- 1401 turing-fix ---` block updates in `nvrm610.conf` (preserving existing rules and user settings). + +### Usage + +```bash +# Inspect status without modifying files +bash tools/apply-turing-fix.sh --check + +# Apply optional kext patches (driver 3.0 / macOS 15) +sudo bash tools/apply-turing-fix.sh --apply + +# Apply only the marked Metal nvrm610.conf block +sudo bash tools/apply-turing-fix.sh --conf-only + +# Verify backup SHA256SUMS and restore stock binaries + AuxKC +sudo bash tools/apply-turing-fix.sh --revert +``` + +## Hardware Tested + +- **CPU**: AMD Ryzen 5 5600 +- **GPU**: NVIDIA GeForce RTX 2060 SUPER 8 GB (`10de:1f06`, Turing TU106, 256 MB BAR1) +- **OS**: macOS Sequoia 15.8.3 (x86_64) +- **Observed**: Smooth 60 FPS window dragging with no black surfaces or WindowServer hitches; NVENC remains active. diff --git a/mac/Resources/nullmoth-rules.json b/mac/Resources/nullmoth-rules.json index 446a45e..bbb32bc 100644 --- a/mac/Resources/nullmoth-rules.json +++ b/mac/Resources/nullmoth-rules.json @@ -22,6 +22,47 @@ "match": {"arch": ["turing"]}, "conf": {"NVK_CMDPOOL_GPU_UNCACHED": "1", "NVK_SYSMEM_GPU_UNCACHED": "1"} }, + { + "id": "turing-bar1-stutter-optimization", + "why": "On Turing cards (TU104/TU106/TU116) with a 256 MB BAR1 window, WindowServer dragging can stutter or drop surfaces when Metal hoards chunk pools, defers buffer frees, and runs out of VRAM headroom. Keeping 48 MB headroom, allowing surface page-off, disabling buffer reuse/grace pooling, and lifting hostptrs stabilizes window dragging via nvrm610.conf without modifying signed driver binaries.", + "evidence": [ + "macOS 15.8.3 on AMD Ryzen 5 5600 + RTX 2060 SUPER (TU106, 10de:1f06): eliminates WindowServer drag stutter and black surfaces", + "Applies Metal driver nvrm610.conf knobs through the standard per-system rules block" + ], + "match": {"arch": ["turing"]}, + "conf": { + "NVK_LOCAL_MAP_SYSMEM": "1", + "NVMTL_MANAGED_SYSMEM": "1", + "NVMTL_SHARED_VRAM": "0", + "NVMTL_SHARED_POOL_VRAM": "0", + "NVK_BAR_VRAM": "0", + "NVMTL_VRAM_HEADROOM_MB": "48", + "NVMTL_SHARED_VRAM_BUDGET_MB": "0", + "NVMTL_NO_SURFACE_PAGEOFF": "0", + "NVMTL_FRESH_ZERO": "1", + "NVMTL_RES2_WS": "1", + "NVMTL_NO_RES2": "0", + "NVMTL_NO_RES2_EVICT": "0", + "NVMTL_NO_PURGE_RECLAIM": "0", + "NVMTL_RES2_MARGIN_MB": "32", + "NVMTL_GSUB_HOSTPTR_LIFT": "1", + "NVMTL_NAK_ARR_SCRATCH": "0", + "NVMTL_SURFACE_BUSY": "1", + "NVMTL_SURFIN_ORDERED": "1", + "NVMTL_SCHEDULE_AFTER_SURFACE_WRITES": "1", + "NVMTL_PRESENT_INLINE": "1", + "NVMTL_WS_WAIT_COMPLETION": "1", + "NVMTL_NO_BUFREUSE": "1", + "NVMTL_IMAGE_POOL": "0", + "NVMTL_PRIVATE_POOL": "0", + "NVMTL_BUFFER_GRACE": "0", + "NVMTL_IMAGE_GRACE": "0", + "NVMTL_SURF_IMPORT_CACHE": "0", + "NVMTL_POOL_POLL_MS": "0", + "NVMTL_CMD_POOL_MAX_CHUNKS": "256", + "NVMTL_SURFACE_DEFER_FREE_MS": "0" + } + }, { "id": "image-size-pre-blackwell", "builtin": true, diff --git a/tests/test_turing_stutter_rules.py b/tests/test_turing_stutter_rules.py new file mode 100644 index 0000000..025c610 --- /dev/null +++ b/tests/test_turing_stutter_rules.py @@ -0,0 +1,65 @@ +"""Tests for Turing micro-stutter rules and standalone tool safety guards.""" +import json +import pathlib +import re +import unittest + +ROOT = pathlib.Path(__file__).resolve().parents[1] +RULES_PATH = ROOT / "mac/Resources/nullmoth-rules.json" +SETUP_PATH = ROOT / "mac/Resources/nullmoth-setup.sh" +TOOL_PATH = ROOT / "tools/apply-turing-fix.sh" +LEGACY_RES_SCRIPT = ROOT / "mac/Resources/nullmoth-turing-optimize.sh" + + +class TuringStutterRulesTest(unittest.TestCase): + def setUp(self): + self.data = json.loads(RULES_PATH.read_text(encoding="utf-8")) + self.rules = self.data.get("rules", []) + + def test_turing_stutter_rule_exists_and_configured(self): + rule = next((r for r in self.rules if r.get("id") == "turing-bar1-stutter-optimization"), None) + self.assertIsNotNone(rule, "turing-bar1-stutter-optimization rule must exist in nullmoth-rules.json") + + match = rule.get("match", {}) + self.assertEqual(match.get("arch"), ["turing"], "Rule must match turing architecture") + + conf = rule.get("conf", {}) + self.assertEqual(conf.get("NVMTL_VRAM_HEADROOM_MB"), "48", "VRAM headroom must be 48MB for small BAR1") + self.assertEqual(conf.get("NVMTL_NO_SURFACE_PAGEOFF"), "0", "Surface page-off must not be forced off") + self.assertEqual(conf.get("NVMTL_NO_BUFREUSE"), "1", "Buffer reuse must be disabled") + self.assertEqual(conf.get("NVMTL_CMD_POOL_MAX_CHUNKS"), "256") + + # Validate that no rule breaks hardware video encoding on Turing + self.assertNotIn("NVMTL_NO_NVENC", conf) + self.assertNotIn("NVMTL_NO_NVENC_HEVC", conf) + + def test_all_rule_conf_keys_and_values_valid_for_profile_swift(self): + key_re = re.compile(r"^(NVMTL|NVK|NVRM)_[A-Z0-9_]+$") + val_re = re.compile(r"^[A-Za-z0-9._-]{0,64}$") + + for rule in self.rules: + for k, v in rule.get("conf", {}).items(): + self.assertTrue(key_re.match(k), f"Invalid conf key: {k} in rule {rule.get('id')}") + self.assertTrue(val_re.match(str(v)), f"Invalid conf value: {v} in rule {rule.get('id')}") + + def test_setup_script_does_not_patch_signed_kext_binaries(self): + setup_src = SETUP_PATH.read_text(encoding="utf-8") + for forbidden in ("patch_turing_stutter", "0x21ea3", "0x251e0", "0x23f1c", "NVAccel.orig.bin"): + self.assertNotIn(forbidden, setup_src, f"nullmoth-setup.sh must not contain binary patcher token {forbidden}") + self.assertFalse(LEGACY_RES_SCRIPT.exists(), "duplicate script must not live in mac/Resources/") + + def test_standalone_tool_has_version_checksum_and_rollback_guards(self): + self.assertTrue(TOOL_PATH.is_file(), "tools/apply-turing-fix.sh must exist") + src = TOOL_PATH.read_text(encoding="utf-8") + self.assertTrue(src.startswith("#!/bin/bash\n"), "apply-turing-fix.sh must use LF line endings and bash shebang") + self.assertNotIn("\r", src, "apply-turing-fix.sh must not contain CRLF line endings") + self.assertIn('EXPECTED_OS_MAJOR="15"', src) + self.assertIn('EXPECTED_DRIVER_VERSION="3.0"', src) + self.assertIn("shasum -a 256 --strict -c SHA256SUMS", src) + self.assertIn("rollback_on_error", src) + self.assertIn("kmutil inspect", src) + self.assertIn("# --- 1401 turing-fix ---", src) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/apply-turing-fix.sh b/tools/apply-turing-fix.sh new file mode 100755 index 0000000..e4a74d3 --- /dev/null +++ b/tools/apply-turing-fix.sh @@ -0,0 +1,485 @@ +#!/bin/bash +# Optional standalone Turing (TU104/TU106/TU116) BAR1 & WindowServer stutter tool. +# +# Normal 1401 installs keep driver packages SHA-256 verified and never patch kexts +# in place; the Metal nvrm610.conf knobs live in mac/Resources/nullmoth-rules.json +# (`turing-bar1-stutter-optimization`) and are applied automatically by setup. +# +# This standalone tool applies/checks/reverts the optional NVAccel and NVRMFB +# binary patches for users hitting 256 MB BAR1 grant drops or 100 ms eviction +# hitches on verified driver 3.0 / macOS 15 builds. It validates OS version, +# driver version, GPU family, SIP status, Mach-O headers, optional SHA-256 pins, +# and exact byte patterns before modifying any file, and rolls back if kmutil fails. + +set -euo pipefail +PATH=/usr/bin:/bin:/usr/sbin:/sbin; export PATH + +TARGET_ACCEL="/Library/Extensions/NVAccel.kext/Contents/MacOS/NVAccel" +TARGET_FB="/Library/Extensions/NVRMFB.kext/Contents/MacOS/NVRMFB" +DRIVER_VER_FILE="/Library/NullMoth/driver-version" +BACKUP_DIR="/Library/NullMoth/turing-backup" +BACKUP_ACCEL="$BACKUP_DIR/NVAccel.orig" +BACKUP_FB="$BACKUP_DIR/NVRMFB.orig" +BACKUP_SUMS="$BACKUP_DIR/SHA256SUMS" +CONF_DIR="/Library/GPUBundles/nvmtl" +CONF_FILE="$CONF_DIR/nvrm610.conf" +CONF_START="# --- 1401 turing-fix ---" +CONF_END="# --- end 1401 turing-fix ---" +KC="/Library/KernelCollections/AuxiliaryKernelExtensions.kc" +KB="/System/Library/KernelCollections/BootKernelExtensions.kc" +KS="/System/Library/KernelCollections/SystemKernelExtensions.kc" + +EXPECTED_OS_MAJOR="15" +EXPECTED_DRIVER_VERSION="3.0" +EXPECTED_ACCEL_SHA256="${EXPECTED_ACCEL_SHA256:-}" +EXPECTED_FB_SHA256="${EXPECTED_FB_SHA256:-}" +ALLOW_VERSION_MISMATCH=0 +ACTION="apply" + +# Verified offsets for NullMoth driver 3.0 (macOS 15 x86_64) +OFF_ACCEL_VM_STICKY="0x23f1c" +OFF_ACCEL_SURF_GPU_ON="0x251e0" +OFF_FB_VRAM_GRANT="0x21ea3" + +die() { + echo "STOP: $*" >&2 + exit 1 +} + +usage() { + cat <<'EOF' +Usage: + bash tools/apply-turing-fix.sh --check + sudo bash tools/apply-turing-fix.sh [--apply] [--accel-sha256 HEX] [--fb-sha256 HEX] + sudo bash tools/apply-turing-fix.sh --conf-only + sudo bash tools/apply-turing-fix.sh --revert + +Options: + --check, --status Read-only check of OS/driver version, binary patch state, and nvrm610.conf + --apply Verify guards and apply optional NVAccel/NVRMFB patches + AuxKC rebuild + --conf-only Only write the marked Turing block into nvrm610.conf (no kext changes) + --revert Verify backup SHA256SUMS, restore kext binaries, remove conf block, rebuild AuxKC + --accel-sha256 HEX Require NVAccel binary to match this stock SHA-256 before patching + --fb-sha256 HEX Require NVRMFB binary to match this stock SHA-256 before patching + --allow-version-mismatch Allow patching if driver-version != 3.0 or macOS != 15 (exact bytes still checked) + --help, -h Show this help text +EOF +} + +nv_device_id() { + ioreg -r -c IOPCIDevice -l -w0 2>/dev/null | LC_ALL=C awk ' + function emit() { + if (v && d != "") { + id = substr(d,3,2) substr(d,1,2) + if (c == 300 && vga == "") vga = id + else if (c == 302 && d3d == "") d3d = id + } + } + /\+-o / { emit(); v=0; c=0; d="" } + /"vendor-id" = / { v=1 } + /"class-code" = <00000300>/ { c=300 } + /"class-code" = <00000302>/ { c=302 } + /"device-id" = /)) d=substr($0, RSTART+1, 4) } + END { emit(); print (vga != "" ? vga : d3d) }' +} + +is_turing_id() { + case "${1:-}" in + 1[eEfF]??|21[89a-fA-F]?) return 0 ;; + *) return 1 ;; + esac +} + +sip_protecting_fs() { + local status + status=$(csrutil status 2>/dev/null || true) + printf '%s\n' "$status" | grep -qE 'status: enabled\.|Filesystem Protections: enabled' +} + +read_driver_version() { + if [ -f "$DRIVER_VER_FILE" ]; then + tr -d '[:space:]' < "$DRIVER_VER_FILE" 2>/dev/null || true + return + fi + plutil -extract CFBundleShortVersionString raw -o - /Library/Extensions/NVRMFB.kext/Contents/Info.plist 2>/dev/null || true +} + +inspect_accel_state() { + /usr/bin/perl -e ' + use strict; use warnings; + my ($file, $off1, $off2) = @ARGV; + open(my $fh, "<", $file) or die "cannot open $file: $!\n"; + binmode $fh; + my $size = -s $file; + die "file too small ($size bytes)\n" if !defined($size) || $size <= ($off2 + 4); + read($fh, my $magic, 4) == 4 or die "short read magic\n"; + die "not 64-bit Mach-O\n" if $magic ne "\xcf\xfa\xed\xfe"; + seek($fh, $off1, 0) or die "seek $off1 failed\n"; + read($fh, my $b1, 4) == 4 or die "short read $off1\n"; + seek($fh, $off2, 0) or die "seek $off2 failed\n"; + read($fh, my $b2, 4) == 4 or die "short read $off2\n"; + close($fh); + my $v1 = unpack("V", $b1); + my $v2 = unpack("V", $b2); + print "$v1 $v2\n"; + ' "$1" "$(($OFF_ACCEL_VM_STICKY))" "$(($OFF_ACCEL_SURF_GPU_ON))" +} + +inspect_fb_state() { + /usr/bin/perl -e ' + use strict; use warnings; + my ($file, $off) = @ARGV; + open(my $fh, "<", $file) or die "cannot open $file: $!\n"; + binmode $fh; + my $size = -s $file; + die "file too small ($size bytes)\n" if !defined($size) || $size <= ($off + 6); + read($fh, my $magic, 4) == 4 or die "short read magic\n"; + die "not 64-bit Mach-O\n" if $magic ne "\xcf\xfa\xed\xfe"; + seek($fh, $off, 0) or die "seek $off failed\n"; + read($fh, my $buf, 6) == 6 or die "short read $off\n"; + close($fh); + if ($buf eq "\xe9\x6e\x01\x00\x00\x90") { print "patched\n"; } + elsif ($buf eq "\x0f\x8e\x6d\x01\x00\x00") { print "stock\n"; } + else { print "unknown\n"; } + ' "$1" "$(($OFF_FB_VRAM_GRANT))" +} + +write_conf_block() { + mkdir -p "$CONF_DIR" + local tmp + tmp=$(mktemp "$CONF_DIR/.nvrm610.XXXXXX") || die "could not create temporary file in $CONF_DIR" + if [ -f "$CONF_FILE" ]; then + awk -v start="$CONF_START" -v end="$CONF_END" ' + $0 == start { skip=1; next } + $0 == end { skip=0; next } + !skip { print } + ' "$CONF_FILE" > "$tmp" || { rm -f "$tmp"; die "could not preserve existing $CONF_FILE"; } + fi + { + echo "$CONF_START" + cat <<'EOF' +NVK_CMDPOOL_GPU_UNCACHED=1 +NVK_SYSMEM_GPU_UNCACHED=1 +NVK_LOCAL_MAP_SYSMEM=1 +NVMTL_MANAGED_SYSMEM=1 +NVMTL_SHARED_VRAM=0 +NVMTL_SHARED_POOL_VRAM=0 +NVK_BAR_VRAM=0 +NVMTL_VRAM_HEADROOM_MB=48 +NVMTL_SHARED_VRAM_BUDGET_MB=0 +NVMTL_NO_SURFACE_PAGEOFF=0 +NVMTL_FRESH_ZERO=1 +NVMTL_RES2_WS=1 +NVMTL_NO_RES2=0 +NVMTL_NO_RES2_EVICT=0 +NVMTL_NO_PURGE_RECLAIM=0 +NVMTL_RES2_MARGIN_MB=32 +NVMTL_GSUB_HOSTPTR_LIFT=1 +NVMTL_NAK_ARR_SCRATCH=0 +NVMTL_SURFACE_BUSY=1 +NVMTL_SURFIN_ORDERED=1 +NVMTL_SCHEDULE_AFTER_SURFACE_WRITES=1 +NVMTL_PRESENT_INLINE=1 +NVMTL_WS_WAIT_COMPLETION=1 +NVMTL_NO_BUFREUSE=1 +NVMTL_IMAGE_POOL=0 +NVMTL_PRIVATE_POOL=0 +NVMTL_BUFFER_GRACE=0 +NVMTL_IMAGE_GRACE=0 +NVMTL_SURF_IMPORT_CACHE=0 +NVMTL_POOL_POLL_MS=0 +NVMTL_CMD_POOL_MAX_CHUNKS=256 +NVMTL_SURFACE_DEFER_FREE_MS=0 +EOF + echo "$CONF_END" + } >> "$tmp" + chmod 644 "$tmp" && chown root:wheel "$tmp" && mv -f "$tmp" "$CONF_FILE" || { + rm -f "$tmp" + die "could not update $CONF_FILE" + } + rm -rf "$CONF_DIR/aircache"/* "$CONF_DIR/spvcache"/* 2>/dev/null || true + mkdir -p "$CONF_DIR/aircache" "$CONF_DIR/spvcache" + chmod 777 "$CONF_DIR/aircache" "$CONF_DIR/spvcache" + echo "[+] Updated marked Turing block in $CONF_FILE (existing settings preserved)" +} + +remove_conf_block() { + [ -f "$CONF_FILE" ] || return 0 + local tmp + tmp=$(mktemp "$CONF_DIR/.nvrm610.XXXXXX") || die "could not create temporary file in $CONF_DIR" + awk -v start="$CONF_START" -v end="$CONF_END" ' + $0 == start { skip=1; next } + $0 == end { skip=0; next } + !skip { print } + ' "$CONF_FILE" > "$tmp" || { rm -f "$tmp"; die "could not clean $CONF_FILE"; } + chmod 644 "$tmp" && chown root:wheel "$tmp" && mv -f "$tmp" "$CONF_FILE" || { + rm -f "$tmp" + die "could not save cleaned $CONF_FILE" + } + echo "[+] Removed marked Turing block from $CONF_FILE" +} + +rebuild_and_verify_kc() { + echo "[*] Rebuilding Auxiliary Kernel Collection..." + local K=/System/Library/Kernels/kernel + local KARG=(--allow-missing-kdk) + [ -f "$K" ] && KARG+=(--kernel "$K") + + local new_kc="$KC.nullmoth-turing-new" + rm -f "$new_kc" + kmutil create -n aux --volume-root / "${KARG[@]}" \ + -B "$KB" -S "$KS" --repository /Library/Extensions \ + -A "$new_kc" -z || { + rm -f "$new_kc" + return 1 + } + [ -s "$new_kc" ] || { rm -f "$new_kc"; return 1; } + + local ins + ins=$(kmutil inspect -a x86_64 -A "$new_kc" 2>/dev/null || true) + printf '%s\n' "$ins" | grep -q "com\.nullmoth\.NVAccel" || { rm -f "$new_kc"; return 1; } + printf '%s\n' "$ins" | grep -q "com\.nullmoth\.NVRMFB" || { rm -f "$new_kc"; return 1; } + + mv -f "$new_kc" "$KC" || { rm -f "$new_kc"; return 1; } + echo "[+] Auxiliary Kernel Collection rebuilt and verified (NVAccel + NVRMFB present)" +} + +preflight_binary_checks() { + [ "$(id -u)" -eq 0 ] || die "root privileges required (run with sudo)" + ! sip_protecting_fs || die "System Integrity Protection protects /Library/Extensions or /Library/KernelCollections; disable filesystem protection via OpenCore first" + + local os_ver os_major drv_ver nvdev + os_ver=$(sw_vers -productVersion 2>/dev/null || true) + os_major=${os_ver%%.*} + drv_ver=$(read_driver_version) + nvdev=$(nv_device_id) + + if [ "$ALLOW_VERSION_MISMATCH" != 1 ]; then + [ "$os_major" = "$EXPECTED_OS_MAJOR" ] || die "macOS major is '${os_major:-unknown}'; offsets apply only to macOS $EXPECTED_OS_MAJOR (pass --allow-version-mismatch to override byte-checked)" + [ -n "$drv_ver" ] || die "cannot determine NullMoth driver version ($DRIVER_VER_FILE missing); pass --allow-version-mismatch if you verified the target binary" + [ "$drv_ver" = "$EXPECTED_DRIVER_VERSION" ] || die "installed driver version is '$drv_ver'; offsets were verified only for $EXPECTED_DRIVER_VERSION (driver 3.1+ uses different offsets)" + fi + + is_turing_id "$nvdev" || die "detected NVIDIA PCI device '${nvdev:-none}' is not Turing (1e00..21ff); refusing to patch" + + [ -f "$TARGET_ACCEL" ] && [ ! -L "$TARGET_ACCEL" ] || die "missing or symlinked binary: $TARGET_ACCEL" + [ -f "$TARGET_FB" ] && [ ! -L "$TARGET_FB" ] || die "missing or symlinked binary: $TARGET_FB" + + LC_ALL=C grep -q "nvaccel_vm_sticky" "$TARGET_ACCEL" || die "$TARGET_ACCEL lacks nvaccel_vm_sticky symbol string" + LC_ALL=C grep -q "nvaccel_surface_gpu_on" "$TARGET_ACCEL" || die "$TARGET_ACCEL lacks nvaccel_surface_gpu_on symbol string" + + local accel_state fb_state + accel_state=$(inspect_accel_state "$TARGET_ACCEL") || die "NVAccel preflight failed" + fb_state=$(inspect_fb_state "$TARGET_FB") || die "NVRMFB preflight failed" + + local v_sticky v_evict + v_sticky=${accel_state%% *} + v_evict=${accel_state##* } + + if [ "$v_sticky" != "0" ] && [ "$v_sticky" != "1" ]; then + die "NVAccel $OFF_ACCEL_VM_STICKY has unexpected value '$v_sticky' (expected 0 or 1); nothing changed" + fi + if [ "$v_evict" != "0" ] && [ "$v_evict" != "1" ]; then + die "NVAccel $OFF_ACCEL_SURF_GPU_ON has unexpected value '$v_evict' (expected 0 or 1); nothing changed" + fi + if [ "$fb_state" != "stock" ] && [ "$fb_state" != "patched" ]; then + die "NVRMFB $OFF_FB_VRAM_GRANT has unrecognized bytes (driver build mismatch); nothing changed" + fi + + if [ -n "$EXPECTED_ACCEL_SHA256" ]; then + EXPECTED_ACCEL_SHA256=$(printf '%s' "$EXPECTED_ACCEL_SHA256" | tr 'A-Z' 'a-z') + local check_a="$TARGET_ACCEL" + [ "$v_sticky" = "0" ] && [ "$v_evict" = "0" ] && [ -f "$BACKUP_ACCEL" ] && check_a="$BACKUP_ACCEL" + local got_a + got_a=$(shasum -a 256 "$check_a" | awk '{print $1}') + [ "$got_a" = "$EXPECTED_ACCEL_SHA256" ] || die "NVAccel SHA-256 '$got_a' does not match expected '$EXPECTED_ACCEL_SHA256'" + fi + if [ -n "$EXPECTED_FB_SHA256" ]; then + EXPECTED_FB_SHA256=$(printf '%s' "$EXPECTED_FB_SHA256" | tr 'A-Z' 'a-z') + local check_f="$TARGET_FB" + [ "$fb_state" = "patched" ] && [ -f "$BACKUP_FB" ] && check_f="$BACKUP_FB" + local got_f + got_f=$(shasum -a 256 "$check_f" | awk '{print $1}') + [ "$got_f" = "$EXPECTED_FB_SHA256" ] || die "NVRMFB SHA-256 '$got_f' does not match expected '$EXPECTED_FB_SHA256'" + fi +} + +check_status() { + echo "=== Turing Fix Status Check ===" + local os_ver drv_ver nvdev ok=1 + os_ver=$(sw_vers -productVersion 2>/dev/null || echo "unknown") + drv_ver=$(read_driver_version) + nvdev=$(nv_device_id) + echo "[*] macOS: $os_ver | Driver: ${drv_ver:-unknown} | NVIDIA GPU: ${nvdev:-none}" + + if [ -f "$TARGET_ACCEL" ]; then + local accel_state v_sticky v_evict + if accel_state=$(inspect_accel_state "$TARGET_ACCEL" 2>/dev/null); then + v_sticky=${accel_state%% *} + v_evict=${accel_state##* } + echo "[*] NVAccel gVmSticky ($OFF_ACCEL_VM_STICKY): $v_sticky (0 = patched, 1 = stock)" + echo "[*] NVAccel gSurfGpuOn ($OFF_ACCEL_SURF_GPU_ON): $v_evict (0 = patched, 1 = stock)" + if [ "$v_sticky" != "0" ] || [ "$v_evict" != "0" ]; then ok=0; fi + else + echo "[-] NVAccel failed Mach-O/bounds inspection" + ok=0 + fi + else + echo "[-] NVAccel binary not found at $TARGET_ACCEL" + ok=0 + fi + + if [ -f "$TARGET_FB" ]; then + local s_fb + s_fb=$(inspect_fb_state "$TARGET_FB" 2>/dev/null || echo "error") + echo "[*] NVRMFB vramGrant budget check ($OFF_FB_VRAM_GRANT): $s_fb" + if [ "$s_fb" != "patched" ]; then ok=0; fi + else + echo "[-] NVRMFB binary not found at $TARGET_FB" + ok=0 + fi + + if [ -f "$CONF_FILE" ] && grep -q "NVMTL_VRAM_HEADROOM_MB=48" "$CONF_FILE"; then + echo "[*] Metal nvrm610.conf: NVMTL_VRAM_HEADROOM_MB=48 present" + else + echo "[-] Metal nvrm610.conf: Turing BAR1 headroom rule not present" + ok=0 + fi + + if [ -f "$BACKUP_SUMS" ]; then + echo "[*] Verified backup manifest present at $BACKUP_SUMS" + fi + + if [ "$ok" -eq 1 ]; then + echo "[+] Overall: all checked Turing optimizations are active" + exit 0 + else + echo "[!] Overall: one or more checks are inactive or mismatched" + exit 1 + fi +} + +revert_patches() { + [ "$(id -u)" -eq 0 ] || die "root privileges required to revert patches (run with sudo)" + ! sip_protecting_fs || die "System Integrity Protection is enabled; cannot restore /Library/Extensions" + [ -f "$BACKUP_SUMS" ] || die "backup checksum manifest missing at $BACKUP_SUMS; nothing restored" + + echo "=== Reverting Turing Patches ===" + (cd "$BACKUP_DIR" && shasum -a 256 --strict -c SHA256SUMS --quiet) || die "backup files failed SHA-256 verification against $BACKUP_SUMS" + + [ -f "$BACKUP_ACCEL" ] && cp -p "$BACKUP_ACCEL" "$TARGET_ACCEL" && codesign -f -s - /Library/Extensions/NVAccel.kext + [ -f "$BACKUP_FB" ] && cp -p "$BACKUP_FB" "$TARGET_FB" && codesign -f -s - /Library/Extensions/NVRMFB.kext + remove_conf_block + + rebuild_and_verify_kc || die "kmutil rebuild/inspect failed during revert" + rm -rf "$BACKUP_DIR" + echo "[+] Revert complete and AuxKC verified. Restart your system." +} + +rollback_on_error() { + echo "[!] Patching or AuxKC verification failed; restoring stock binaries..." >&2 + [ -f "$BACKUP_ACCEL" ] && cp -p "$BACKUP_ACCEL" "$TARGET_ACCEL" 2>/dev/null || true + [ -f "$BACKUP_FB" ] && cp -p "$BACKUP_FB" "$TARGET_FB" 2>/dev/null || true + codesign -f -s - /Library/Extensions/NVAccel.kext 2>/dev/null || true + codesign -f -s - /Library/Extensions/NVRMFB.kext 2>/dev/null || true + rebuild_and_verify_kc >/dev/null 2>&1 || true + die "aborted and restored original driver binaries" +} + +apply_fixes() { + preflight_binary_checks + + echo "=== Applying Optional Turing Stutter & BAR1 Kext Patches ===" + mkdir -p "$BACKUP_DIR" && chmod 700 "$BACKUP_DIR" + + local accel_state fb_state v_sticky v_evict + accel_state=$(inspect_accel_state "$TARGET_ACCEL") + v_sticky=${accel_state%% *} + v_evict=${accel_state##* } + fb_state=$(inspect_fb_state "$TARGET_FB") + + if [ ! -f "$BACKUP_ACCEL" ]; then + [ "$v_sticky" = "1" ] && [ "$v_evict" = "1" ] || die "cannot record clean backup: NVAccel is already modified without a backup" + cp -p "$TARGET_ACCEL" "$BACKUP_ACCEL" + fi + if [ ! -f "$BACKUP_FB" ]; then + [ "$fb_state" = "stock" ] || die "cannot record clean backup: NVRMFB is already modified without a backup" + cp -p "$TARGET_FB" "$BACKUP_FB" + fi + (cd "$BACKUP_DIR" && shasum -a 256 NVAccel.orig NVRMFB.orig > SHA256SUMS.tmp && mv -f SHA256SUMS.tmp SHA256SUMS) + chmod 600 "$BACKUP_ACCEL" "$BACKUP_FB" "$BACKUP_SUMS" + + /usr/bin/perl -e ' + use strict; use warnings; + my ($file, $off1, $off2) = @ARGV; + open(my $fh, "+<", $file) or die "cannot open $file: $!\n"; + binmode $fh; + seek($fh, $off1, 0); read($fh, my $b1, 4) == 4 or die "short read\n"; + if (unpack("V", $b1) == 1) { + seek($fh, $off1, 0); print $fh pack("V", 0); + print "[+] Patched NVAccel gVmSticky -> 0\n"; + } else { + print "[*] NVAccel gVmSticky already 0\n"; + } + seek($fh, $off2, 0); read($fh, my $b2, 4) == 4 or die "short read\n"; + if (unpack("V", $b2) == 1) { + seek($fh, $off2, 0); print $fh pack("V", 0); + print "[+] Patched NVAccel gSurfGpuOn -> 0\n"; + } else { + print "[*] NVAccel gSurfGpuOn already 0\n"; + } + close($fh); + ' "$TARGET_ACCEL" "$(($OFF_ACCEL_VM_STICKY))" "$(($OFF_ACCEL_SURF_GPU_ON))" || rollback_on_error + codesign -f -s - /Library/Extensions/NVAccel.kext || rollback_on_error + + /usr/bin/perl -e ' + use strict; use warnings; + my ($file, $off) = @ARGV; + open(my $fh, "+<", $file) or die "cannot open $file: $!\n"; + binmode $fh; + seek($fh, $off, 0); read($fh, my $buf, 6) == 6 or die "short read\n"; + if ($buf eq "\x0f\x8e\x6d\x01\x00\x00") { + seek($fh, $off, 0); print $fh "\xe9\x6e\x01\x00\x00\x90"; + print "[+] Patched NVRMFB vramGrant branch\n"; + } elsif ($buf eq "\xe9\x6e\x01\x00\x00\x90") { + print "[*] NVRMFB vramGrant branch already patched\n"; + } else { + die "unexpected bytes at $off\n"; + } + close($fh); + ' "$TARGET_FB" "$(($OFF_FB_VRAM_GRANT))" || rollback_on_error + codesign -f -s - /Library/Extensions/NVRMFB.kext || rollback_on_error + + rebuild_and_verify_kc || rollback_on_error + + if [ ! -f "$CONF_FILE" ] || ! grep -q "NVMTL_VRAM_HEADROOM_MB=48" "$CONF_FILE"; then + write_conf_block + else + echo "[*] $CONF_FILE already contains Turing BAR1 rules; leaving existing blocks intact" + fi + + sysctl debug.nvaccel_vm_sticky=0 debug.nvaccel_surface_gpu_on=0 >/dev/null 2>&1 || true + echo "[+] Live sysctl overrides applied. Restart to load the updated AuxKC." +} + +while [ $# -gt 0 ]; do + case "$1" in + --check|--status) ACTION="check" ;; + --apply) ACTION="apply" ;; + --conf-only) ACTION="conf" ;; + --revert) ACTION="revert" ;; + --accel-sha256) [ $# -ge 2 ] || die "--accel-sha256 requires a value"; EXPECTED_ACCEL_SHA256=$2; shift ;; + --fb-sha256) [ $# -ge 2 ] || die "--fb-sha256 requires a value"; EXPECTED_FB_SHA256=$2; shift ;; + --allow-version-mismatch) ALLOW_VERSION_MISMATCH=1 ;; + --help|-h) usage; exit 0 ;; + *) die "unknown option: $1 (see --help)" ;; + esac + shift +done + +case "$ACTION" in + check) check_status ;; + conf) [ "$(id -u)" -eq 0 ] || die "root privileges required (run with sudo)"; write_conf_block ;; + revert) revert_patches ;; + apply) apply_fixes ;; +esac