Add wb_probe = 1, which finds out who reads what the GPU wrote back into guest memory.

Per-pass GPU timestamps put the write-back of GPU-written storage buffers at 3.6 to 5.3 ms of GPU time a frame in play, against 1.8 to 2.5 ms for all of the game's own passes and dispatches: about 67 MiB a frame goes from the mirrors back into guest memory, from the same eleven pools all session, in case the CPU or a host path reads it. Nothing said whether anything does.

With wb_probe = 1 the 2 MiB regions that lie wholly inside a range just written back are made inaccessible. The first read or write of such a region faults; the handler gives the region back the protection it had (read-only when frozen, else read-write), notes the address, the instruction and the thread, and the access runs again. The bytes are in guest memory all along, so nothing changes for the game. A log block every perf window gives the regions armed, the probes a read or a write ended, and the first accesses by instruction. Every thaw ends the probe of its region first, which covers file reads into guest memory and mapping changes; a file write from guest memory thaws its buffer while the probe is on; a probed region is not frozen anew.

The platform layer gets a hook for every read or write access fault beside the write-fault hook; it is installed only with the setting. --probe-gfx-abi checks that nothing is armed without it and, run with NX2NAVI_WB_PROBE=1, that reads and writes end a probe and run again, that a frozen region thaws after it and that mem_thaw ends and holds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-09 21:01:47 -05:00
parent c32b9c40a8
commit 57cdfa1520
10 changed files with 183 additions and 7 deletions

View File

@@ -1006,11 +1006,15 @@ uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t m
// SDL), then goes to the crash hook (main.c), which reports it and ends the process. Without a crash hook the fault
// gets the default action. Runs on the thread's signal stack.
static int (*fault_hook)(uint64_t addr);
static int (*access_hook)(uint64_t addr, int write, uint64_t ip); // every read or write fault, before fault_hook
static void (*crash_fn)(const PlatCrash *c);
static void fault_handler(int sig, siginfo_t *si, void *uc) {
ucontext_t *u = uc;
long err = u->uc_mcontext.gregs[REG_ERR];
if (sig == SIGSEGV && access_hook && !(err & 16) &&
access_hook((uint64_t)(uintptr_t)si->si_addr, (err & 2) != 0, (uint64_t)u->uc_mcontext.gregs[REG_RIP]))
return;
if (sig == SIGSEGV && fault_hook && (err & 2) && fault_hook((uint64_t)(uintptr_t)si->si_addr)) return;
uintptr_t rip = (uintptr_t)u->uc_mcontext.gregs[REG_RIP];
Dl_info at, self;
@@ -1050,6 +1054,11 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
fault_install();
}
void plat_mem_access_hook(int (*fn)(uint64_t addr, int write, uint64_t ip)) {
access_hook = fn;
fault_install();
}
void plat_crash_context(const char *s) { (void)s; } // the Windows crash notes only
int plat_crash_filter_reassert(char *who, size_t n) { (void)who, (void)n; return 0; } // Windows only

View File

@@ -243,6 +243,10 @@ uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t m
// A write that faulted on a read-only page at addr goes to fn first; fn returns 1 when it made the page writable and
// the write should run again, 0 to let the fault go on (to the crash handler). One hook per process.
void plat_mem_fault_hook(int (*fn)(uint64_t addr));
// A read or write that faulted at addr (write: 1 for a write; ip: the faulting instruction) goes to fn before the
// write-fault hook; fn returns 1 when it made the page accessible and the access should run again, 0 to let the fault
// go on. fn sees every access violation of the process and tells its own pages apart. One hook per process.
void plat_mem_access_hook(int (*fn)(uint64_t addr, int write, uint64_t ip));
// Starts this program again with the same arguments and ends this process with the new one's exit code (Windows), or
// replaces this process with it (Linux). Environment changes made before the call are passed on.
_Noreturn void plat_reexec(void);

View File

@@ -807,15 +807,23 @@ uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t m
}
static int (*fault_hook)(uint64_t addr);
static int (*access_hook)(uint64_t addr, int write, uint64_t ip);
// A write access violation (ExceptionInformation[0] == 1) goes to the hook; registered first, before the crash handler.
// A read or write access violation (ExceptionInformation[0] 0 or 1) goes to the access hook when there is one, then a
// write to the write-fault hook; registered first, before the crash handler.
static LONG CALLBACK fault_veh(EXCEPTION_POINTERS *ep) {
const EXCEPTION_RECORD *er = ep->ExceptionRecord;
if (er->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || er->NumberParameters < 2 || er->ExceptionInformation[0] != 1)
if (er->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || er->NumberParameters < 2 || er->ExceptionInformation[0] > 1)
return EXCEPTION_CONTINUE_SEARCH;
return fault_hook((uint64_t)er->ExceptionInformation[1]) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
int write = er->ExceptionInformation[0] == 1;
uint64_t addr = (uint64_t)er->ExceptionInformation[1];
if (access_hook && access_hook(addr, write, (uint64_t)(uintptr_t)er->ExceptionAddress)) return EXCEPTION_CONTINUE_EXECUTION;
if (!write) return EXCEPTION_CONTINUE_SEARCH;
return fault_hook(addr) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
}
void plat_mem_access_hook(int (*fn)(uint64_t addr, int write, uint64_t ip)) { access_hook = fn; }
void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
fault_hook = fn;
AddVectoredExceptionHandler(1, fault_veh);