From 57cdfa15203461b6a7ebd5e355d50a3b918c6c41 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Mon, 9 Nov 2026 21:01:47 -0500 Subject: [PATCH] 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 --- nx2navi.ini.example | 4 ++ runtime/cfg.c | 6 +- runtime/fs_host.c | 2 + runtime/gfx_abi_probe.c | 23 +++++++ runtime/host.h | 4 ++ runtime/memory.c | 118 ++++++++++++++++++++++++++++++++++++ runtime/platform/linux.c | 9 +++ runtime/platform/platform.h | 4 ++ runtime/platform/win32.c | 14 ++++- runtime/vk_memory.c | 6 +- 10 files changed, 183 insertions(+), 7 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 3f1adb75..e0294398 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -579,6 +579,10 @@ # 1: pages a VRAM mirror wrote back to game memory are not copied up into it again, as it already holds them (a write by # the game still is). Outdoors this removed about 57 MiB of copies a frame. 0: they are copied up again. # wb_no_reup = 1 +# 1: finds out who reads what the GPU wrote back into guest memory. The 2 MiB regions inside a range just written back +# are made inaccessible until their first read or write, which is logged with its instruction ("mem: write-back +# probe"). The bytes are there all along; the probe only watches, at the cost of the faults it takes. +# wb_probe = 0 # 1: the game's flushes of mirrored memory are copied into the mirrors even where the write watch already sees every # page the game wrote; 0 (default) copies only the written pages there, which saved 47-57 MiB of copies a frame in # Kokiri Forest. diff --git a/runtime/cfg.c b/runtime/cfg.c index 5981ee02..1ecf076e 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -62,7 +62,7 @@ static const char *const known[] = { "SWKBD_ANSWER", "SET_AT", "SWKBD_TEST", "TAA", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_DISUSE", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TEX_RELEASED_MB", "TEX_REUSE", "TEX_COMMIT_FREE_MB", "TEX_EVICT", "TEX_COMMIT_EVICT", "MIRROR_EVICT", "VRAM_BUDGET_MB", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NAN_PRESERVE", "VK_NOOPT", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECONVERGE", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", - "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WB_NO_REUP", "WB_MERGE_MAX", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", + "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WB_NO_REUP", "WB_MERGE_MAX", "WB_PROBE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", "BIND_REUSE", "DYN_DELTA", "STG_ARENA", "SUBMIT_BATCH", "ASTC_GPU_DESWIZZLE", "ASTC_SCRATCH_KEEP", "ASTC_ARENA", "TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", @@ -118,7 +118,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, - {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, }; @@ -231,7 +231,7 @@ static const CfgMeta metas[] = { {"VK_NOOPT", "0", "0|1", RESTART}, // vk_pipeline.c noopt_flag // diagnostics {"TRACE", "", NULL, RESTART}, {"FLIGHT", "0", "0|1", RESTART}, {"FLIGHT_SPIKE_MS", "100", "0..60000", RESTART}, - {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, {"MEM_CENSUS", "0", "0|1", RESTART}, {"WB_NO_REUP", "1", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, {"GPA_TWIN_MAX_KB", "0", "0..1048576", RESTART}, + {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, {"MEM_CENSUS", "0", "0|1", RESTART}, {"WB_NO_REUP", "1", "0|1", RESTART}, {"WB_PROBE", "0", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, {"GPA_TWIN_MAX_KB", "0", "0..1048576", RESTART}, {"WB_MERGE_MAX", "32", "1..256", RESTART}, {"TEXUP_LOG_MS", "0", "0..60000", RESTART}, // vk_texture.c vk_texstat_frame {"VMA_BLOCK_MB", "64", "16..256", RESTART}, // vk_headroom.c vk_vma_block_size {"ASTC_BC_PROBE", "0", "0|1", RESTART}, // vk_texture.c astc_probe_alpha diff --git a/runtime/fs_host.c b/runtime/fs_host.c index d0aafc05..175d4210 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -984,6 +984,8 @@ static uint32_t write_file(uint64_t h, int64_t offset, const void *buffer, uint6 if (file->save) save_touched(); for (uint64_t done = 0; done < size;) { uint32_t chunk = size - done > 0x40000000 ? 0x40000000 : (uint32_t)(size - done); + // NX2NAVI_WB_PROBE: the kernel reads the buffer, and a probed page would fail the write instead of faulting + if (mem_probing()) mem_thaw((uintptr_t)buffer + done, chunk); int64_t put = plat_file_pwrite(f, (const uint8_t *)buffer + done, chunk, (uint64_t)offset + done); if (put <= 0) rt_fatal("fs: host save write failure"); done += (uint64_t)put; diff --git a/runtime/gfx_abi_probe.c b/runtime/gfx_abi_probe.c index cef294d1..47b76c3b 100644 --- a/runtime/gfx_abi_probe.c +++ b/runtime/gfx_abi_probe.c @@ -94,6 +94,29 @@ static void check_write_watch(void) { CHECK(plat_mem_state(a, a + 0x10000, &end) == PLAT_PROT_RW && end == a + 0x4000, "the thawed pages are not read-write"); CHECK(plat_mem_state(a + 0x4000, a + 0x4001, &end) == PLAT_MEM_RESERVED, "a hole got committed"); printf("write-watch: region with reserved holes frozen and thawed by a write\n"); + // NX2NAVI_WB_PROBE=1 (memory.c, "Probed regions"): a probed region is inaccessible until its first access, which is + // noted and runs again; a frozen one goes back to read-only first and then thaws on a write; mem_thaw ends a probe + // and holds the region. With the setting off mem_probe arms nothing. + if (mem_probing()) { + volatile uint8_t *q = (volatile uint8_t *)(uintptr_t)a; + CHECK(mem_probe(ra, 0x200000) == 1, "probe of a 2 MiB region"); + CHECK(plat_mem_state(a, a + 1, &end) == PLAT_PROT_NONE, "a probed region is not inaccessible"); + CHECK(q[0x2000] == 9, "a read of a probed region did not return its byte"); + CHECK(plat_mem_state(a, a + 0x10000, &end) == PLAT_PROT_RW && end == a + 0x4000, "the read did not end the probe"); + CHECK(mem_freeze(ra) == 1 && mem_probe(ra, 0x200000) == 1, "probe of a frozen region"); + q[0x3000] = 5; // ends the probe (read-only again), faults once more and thaws + CHECK(!mem_frozen(ra) && q[0x3000] == 5, "a write into a probed frozen region did not land"); + CHECK(plat_mem_state(a, a + 0x10000, &end) == PLAT_PROT_RW && end == a + 0x4000, "its pages are not read-write after it"); + CHECK(mem_probe(ra, 0x200000) == 1, "probe again"); + mem_thaw(a, 0x1000); + CHECK(plat_mem_state(a, a + 1, &end) == PLAT_PROT_RW && mem_probe(ra, 0x200000) == 0, + "mem_thaw did not end the probe and hold the region"); + mem_probe_report(0); + printf("write-back probe: a read and a write end a probe and run again, a frozen region thaws after it, " + "mem_thaw ends and holds\n"); + } else { + CHECK(mem_probe(ra, 0x200000) == 0, "mem_probe armed a region with the probe off"); + } CHECK(mem_unmap_physical(a, 0x4000) == 0, "UnmapPhysicalMemory"); CHECK(mem_written_pages(RT_KERNEL_BASE, 0x1000, pages, 8) == UINT64_MAX, "kernel region is not write-watched"); CHECK(mem_set_heap_size(0, &heap) == 0, "SetHeapSize 0"); diff --git a/runtime/host.h b/runtime/host.h index 83b23e18..53bf75ca 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -183,6 +183,10 @@ int mem_frozen(uint64_t a); // the 2 MiB region o void mem_thaw(uint64_t a, uint64_t n); // makes the frozen regions over [a, a+n) writable again; // before a kernel write (file read) into guest memory void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws); +// NX2NAVI_WB_PROBE=1 (memory.c, "Probed regions"): who reads what the GPU wrote back into guest memory. +int mem_probe(uint64_t a, uint64_t n); // the 2 MiB regions inside [a, a+n) fault on their next access +int mem_probing(void); // the probe is on +void mem_probe_report(uint64_t frame); // logs the accesses since the last report // regions frozen now; freezes and thaws since the last call void mem_kernel_free(void *p, uint64_t size); // TLR/TLS reuse after thread exit diff --git a/runtime/memory.c b/runtime/memory.c index 87a72b41..c6a7c02e 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -75,15 +75,43 @@ static volatile int32_t nfrz; static PlatMutex frz_lock = PLAT_MUTEX_INIT; static volatile int64_t frz_count[4]; // freezes; thaws by a write fault, by mem_thaw, by a mapping change +// Probed regions (NX2NAVI_WB_PROBE=1, a diagnosis). The back end copies what the GPU wrote into a mirrored pool back +// into guest memory after every submission, in case the CPU or a host path reads it; whether anything does is what +// the probe finds out. A 2 MiB region that lies wholly inside a range just written back is made inaccessible +// (mem_probe). Its first read or write faults: probe_fault 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 the probe only watches. A probed region is not frozen anew (freeze_locked +// finds pages that are not read-write), and every thaw, which precedes file reads into guest memory and mapping +// changes, ends the probe of its region first. +#define PRB_HITS 64 +static volatile uint8_t prb[RT_ADDRESS_LIMIT >> FRZ_SHIFT]; +static volatile int32_t nprb, prb_on; +static struct { uint64_t addr, ip; uint32_t tid; uint8_t write; } prb_hit[PRB_HITS]; // frz_lock: the first of each report +static int prb_nhit; // frz_lock +static uint64_t prb_arms, prb_ends[2]; // frz_lock: regions armed; probes ended by a read, by a write + static int watched(uint64_t addr, uint64_t size) { const RtGuestLayout *g = &L; return (addr >= g->aslr.base && size <= LOW_SIZE && addr - g->aslr.base <= LOW_SIZE - size) || (addr >= g->alias.base && size <= ALIAS_SIZE && addr - g->alias.base <= ALIAS_SIZE - size); } +// frz_lock held exclusively: the pages of a probed region get back what they had, read-only in a frozen region. +static void unprobe_locked(uint64_t r) { + if (!prb[r]) return; + int prot = frz[r] ? PLAT_PROT_READ : PLAT_PROT_RW; + uint64_t a = r << FRZ_SHIFT, end = a + FRZ_SIZE, b; + for (; a < end; a = b) // every inaccessible run is the probe's: mem_probe took only regions without one + if (plat_mem_state(a, end, &b) == PLAT_PROT_NONE && !plat_mem_protect(a, b - a, prot)) + rt_fatal("memory: ending the probe of %#llx+%#llx failed", (unsigned long long)a, (unsigned long long)(b - a)); + __atomic_store_n(&prb[r], 0, __ATOMIC_RELEASE); + plat_dec32(&nprb); +} + // frz_lock held exclusively. why: 1 a write fault, 2 mem_thaw (holds the region thawed), 3 a mapping change here. static void thaw_locked(uint64_t r, int why) { if (why == 2) thaw_ms[r] = (uint32_t)plat_now_ms() | 1; + if (prb[r]) unprobe_locked(r); if (!frz[r]) return; // The read-only runs only: freeze_locked left reserved holes (heap pages given back) as they were. Every committed // page of a frozen region was read-write at the freeze, and mapping changes thaw before they run, so these are all @@ -149,6 +177,92 @@ int mem_freeze(uint64_t a) { int mem_frozen(uint64_t a) { return a < RT_ADDRESS_LIMIT && __atomic_load_n(&frz[a >> FRZ_SHIFT], __ATOMIC_ACQUIRE); } +// Probes the 2 MiB regions that lie wholly inside [a, a + n): makes them inaccessible until their first access. +// Returns how many it armed. Not a region a file read may still be writing into (the thaw hold), nor one with a page +// that is not plainly read-write (read-only when frozen): those are left alone. +int mem_probe(uint64_t a, uint64_t n) { + if (!__atomic_load_n(&prb_on, __ATOMIC_RELAXED) || n < FRZ_SIZE || a >= RT_ADDRESS_LIMIT || !watched(a, n)) return 0; + uint64_t r0 = (a + FRZ_SIZE - 1) >> FRZ_SHIFT, r1 = (a + n) >> FRZ_SHIFT; // [r0, r1) + int armed = 0; + plat_mutex_lock(&frz_lock); + for (uint64_t r = r0; r < r1; r++) { + if (prb[r]) continue; + if (thaw_ms[r] && (int32_t)((uint32_t)plat_now_ms() - thaw_ms[r]) < (int32_t)FRZ_HOLD_MS) continue; + int want = frz[r] ? PLAT_PROT_READ : PLAT_PROT_RW, k = 0, ok = 1; + uint64_t run[16][2]; + for (uint64_t x = r << FRZ_SHIFT, end = x + FRZ_SIZE, y; x < end && ok; x = y) { + int st = plat_mem_state(x, end, &y); + if (st >= PLAT_PROT_NONE) { + if (st != want || k == 16) ok = 0; + else run[k][0] = x, run[k][1] = y, k++; + } else if (st != PLAT_MEM_RESERVED) ok = 0; + } + if (!ok || !k) continue; + for (int i = 0; i < k && ok; i++) + if (!plat_mem_protect(run[i][0], run[i][1] - run[i][0], PLAT_PROT_NONE)) { + while (i--) plat_mem_protect(run[i][0], run[i][1] - run[i][0], want); + ok = 0; + } + if (!ok) continue; + __atomic_store_n(&prb[r], 1, __ATOMIC_RELEASE); + plat_inc32(&nprb); + prb_arms++, armed++; + } + plat_mutex_unlock(&frz_lock); + return armed; +} + +int mem_probing(void) { return __atomic_load_n(&prb_on, __ATOMIC_RELAXED); } + +// The platform's access-fault hook while the probe is on: the first access of a probed region ends its probe and is +// noted; 1 runs the access again. A read that faulted in a region another thread gave back meanwhile retries too; a +// write goes on to thaw_fault, and any other fault to the crash handler. +static int probe_fault(uint64_t a, int write, uint64_t ip) { + if (a >= RT_ADDRESS_LIMIT || !watched(a, 1)) return 0; + uint64_t r = a >> FRZ_SHIFT; + plat_mutex_lock(&frz_lock); + int was = prb[r]; + if (was) { + unprobe_locked(r); + prb_ends[write != 0]++; + if (prb_nhit < PRB_HITS) prb_hit[prb_nhit++] = (typeof(prb_hit[0])){a, ip, plat_thread_id(), (uint8_t)(write != 0)}; + } + plat_mutex_unlock(&frz_lock); + if (was) return 1; + if (write) return 0; + uint64_t end; + return plat_mem_state(a & ~(PAGE - 1), (a & ~(PAGE - 1)) + PAGE, &end) >= PLAT_PROT_READ; +} + +// One log block for the probe since the last one: how many regions were armed, how many probes a read or a write +// ended, and the first accesses by instruction (an address inside the executable as nx2navi+offset, for the linker +// map or nm). Nothing while the probe is off. +void mem_probe_report(uint64_t frame) { + if (!__atomic_load_n(&prb_on, __ATOMIC_RELAXED)) return; + typeof(prb_hit[0]) hit[PRB_HITS]; + plat_mutex_lock(&frz_lock); + uint64_t arms = prb_arms, reads = prb_ends[0], writes = prb_ends[1]; + int n = prb_nhit; + memcpy(hit, prb_hit, sizeof hit[0] * (size_t)n); + prb_arms = prb_ends[0] = prb_ends[1] = 0, prb_nhit = 0; + plat_mutex_unlock(&frz_lock); + rt_log("mem: write-back probe at frame %llu: %llu regions of 2 MiB armed, probes ended by %llu reads and %llu writes " + "since the last line; %d probed now", (unsigned long long)frame, (unsigned long long)arms, + (unsigned long long)reads, (unsigned long long)writes, (int)__atomic_load_n(&nprb, __ATOMIC_RELAXED)); + uintptr_t exe = plat_exe_base(); + for (int i = 0, lines = 0; i < n && lines < 12; i++) { + if (!hit[i].ip) continue; // counted with an earlier access of the same instruction + int same = 1; + for (int j = i + 1; j < n; j++) + if (hit[j].ip == hit[i].ip && hit[j].write == hit[i].write) same++, hit[j].ip = 0; + rt_log("mem: %d x a %s at %#llx.. from %s%#llx (thread %u)", same, hit[i].write ? "write" : "read", + (unsigned long long)hit[i].addr, hit[i].ip >= exe && hit[i].ip - exe < (1ull << 32) ? "nx2navi+" : "", + (unsigned long long)(hit[i].ip >= exe && hit[i].ip - exe < (1ull << 32) ? hit[i].ip - exe : hit[i].ip), + hit[i].tid); + lines++; + } +} + void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws) { *frozen = (uint64_t)__atomic_load_n(&nfrz, __ATOMIC_RELAXED); *freezes = (uint64_t)plat_xchg64(&frz_count[0], 0); @@ -233,6 +347,10 @@ void mem_init(void) { // nx2navi.ini is read here, once the address space is claimed and before the first setting: the pool size below // depends on NX2NAVI_APP_POOL and on the resolution settings (resolution.c decides them once, on this first call). cfg_init(); + if (cfg_bool("NX2NAVI_WB_PROBE")) { // a diagnosis: off, no access fault reaches probe_fault and nothing is probed + plat_mem_access_hook(probe_fault); + __atomic_store_n(&prb_on, 1, __ATOMIC_RELEASE); + } const char *pool = cfg_get("NX2NAVI_APP_POOL"); L = (RtGuestLayout){ .aslr = {low, RT_ADDRESS_LIMIT - low}, diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index df99a891..8c1ed45a 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -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 diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 82001447..8ad23a8e 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -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); diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 66d8966a..340fbec7 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -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); diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 6d1ea566..3e1cd01a 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -1146,7 +1146,10 @@ static void gpu_written(uint64_t addr, uint64_t size, int back) { } void vk_gpu_written(uint64_t addr, uint64_t size) { gpu_written(addr, size, 0); } -void vk_gpu_written_back(uint64_t addr, uint64_t size) { gpu_written(addr, size, 1); } +void vk_gpu_written_back(uint64_t addr, uint64_t size) { + gpu_written(addr, size, 1); + mem_probe(addr, size); // NX2NAVI_WB_PROBE: the bytes are in guest memory now; note who reads them next +} int vk_mem_mirrored(uint64_t addr, uint64_t size) { VkMemSpan sp; @@ -2426,6 +2429,7 @@ static void mirror_log(uint64_t frame) { ww_last = frame; uint64_t fz, ff, ft, fo; mem_freeze_stats(&fz, &ff, &ft, &fo); + mem_probe_report(frame); if (rt_trace & RT_TRACE_PERF) rt_log("mem: write-watch frame %llu: per frame, every consumer: %.1f queries over %.1f MiB, %.3f ms (%.1f found " "%.0f written pages, %.3f ms); frozen %.0f MiB; since the last line: freezes %llu, thaws by a write %llu, by a file read "