From 83deb3c24b0e4139290e650f42b0734741325c10 Mon Sep 17 00:00:00 2001 From: 3L1A5-1 <276908492+3L1A5-1@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:43:18 +0100 Subject: [PATCH] Console CPU instructions: CLZERO into a title's own memory asks the working set On a CPU without CLZERO, `eh::is_writable` checks each emulated CLZERO line or loop before it writes. Outside the coherent window it asked `VirtualQuery`, which sizes the committed run around the address by walking it to its end. A retail game's CLZERO loops zero 0.9-15 KB at a time inside its 1.1 GB block of committed private memory (not in the coherent window, not a sparse window), and each of those checks took 0.3-4.4 ms: 4,096 of them cost 10.3 s of the first minute on the thread that ran them, and the title presented ~5 frames a second with the GPU mostly idle. `is_writable` now first asks the working set, as `readable` already does (`resident_and_readable`, #332): up to 16 pages that are resident with a read-write protection and no guard are writable, in one call that touches none of them. Anything else (not resident, guard, no access, read-only, longer than 16 pages) takes the `VirtualQuery` path as before. The working-set check is shared with `resident_and_readable` (`resident_with`). Measured on an RTX 2060 SUPER / i5-9400F, retail title, `smoke.ps1 -Seconds 60`: 326 and 323 frames on main, 732 and 726 with this change. Assisted-by: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XM3GUHydjd5DSUVNT9WNSR --- crates/xcl/src/eh.rs | 5 ++++ crates/xcl/src/gpu/shader.rs | 57 +++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/eh.rs b/crates/xcl/src/eh.rs index 2d431e5a4..940b37ae6 100644 --- a/crates/xcl/src/eh.rs +++ b/crates/xcl/src/eh.rs @@ -253,6 +253,11 @@ fn is_writable(va: usize, bytes: usize) -> bool { if bytes > 0 && crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(va, bytes)) { return true; } + // nor a few pages the working set holds as read-write: `VirtualQuery` would size the title's + // whole block of memory around them (see `resident_and_writable`) + if crate::gpu::shader::resident_and_writable(va, bytes) { + return true; + } let mut m: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; let found = unsafe { VirtualQuery(va as *const std::ffi::c_void, &mut m, std::mem::size_of_val(&m)) } != 0; found diff --git a/crates/xcl/src/gpu/shader.rs b/crates/xcl/src/gpu/shader.rs index 2eb90fb85..56b9263b6 100644 --- a/crates/xcl/src/gpu/shader.rs +++ b/crates/xcl/src/gpu/shader.rs @@ -239,6 +239,25 @@ pub fn readable(p: usize, len: usize) -> bool { /// guard, reserved, no access): the `VirtualQuery` walk answers for those. fn resident_and_readable(p: usize, end: usize) -> bool { use windows_sys::Win32::System::Memory::{PAGE_GUARD, PAGE_NOACCESS}; + resident_with(p, end, |protect| protect != 0 && protect & (PAGE_NOACCESS | PAGE_GUARD) == 0) +} + +/// Whether each page of `[p, p + len)` (up to 16 pages) is in the working set as read-write and +/// not a guard page – `resident_and_readable` for the writes XCL makes in a title's memory +/// (`eh::is_writable`, before an emulated CLZERO). A retail game's CLZERO loops zero a few KB at a +/// time inside its 1 GB block of committed memory, and the `VirtualQuery` that sized the block took +/// 0.3–4 ms a check (10 s of the first minute on the thread that ran them). `false` for a page that +/// is not resident: the `VirtualQuery` there answers for it. +pub(crate) fn resident_and_writable(p: usize, len: usize) -> bool { + use windows_sys::Win32::System::Memory::{PAGE_EXECUTE_READWRITE, PAGE_GUARD, PAGE_READWRITE}; + let Some(end) = p.checked_add(len).filter(|_| p != 0 && len != 0) else { + return false; + }; + resident_with(p, end, |protect| protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0 && protect & PAGE_GUARD == 0) +} + +/// Each page of `[p, end)` (up to 16) is in the working set and `ok` accepts its protection. +fn resident_with(p: usize, end: usize, ok: impl Fn(u32) -> bool) -> bool { use windows_sys::Win32::System::ProcessStatus::{K32QueryWorkingSetEx, PSAPI_WORKING_SET_EX_INFORMATION}; const MAX_PAGES: usize = 16; let first = p >> 12; @@ -258,7 +277,7 @@ fn resident_and_readable(p: usize, end: usize) -> bool { ws[..pages].iter().all(|e| { let flags = unsafe { e.VirtualAttributes.Flags }; let protect = ((flags >> 4) & 0x7FF) as u32; - flags & 1 != 0 && protect != 0 && protect & (PAGE_NOACCESS | PAGE_GUARD) == 0 + flags & 1 != 0 && ok(protect) }) } @@ -529,4 +548,40 @@ mod tests { assert!(!resident_and_readable(v.as_ptr() as usize, v.as_ptr() as usize + v.len())); assert!(readable(v.as_ptr() as usize, v.len())); } + + #[test] + fn writable_pages_are_answered_from_the_working_set() { + use windows_sys::Win32::System::Memory::*; + unsafe { + // 0, 1 read-write and written; 2 read-only and read; 3 read-write, never touched; 4 guard + let base = VirtualAlloc(std::ptr::null(), 5 << 12, MEM_RESERVE, PAGE_NOACCESS) as usize; + assert_ne!(base, 0); + let page = |k: usize| base + (k << 12); + for (k, protect) in [(0, PAGE_READWRITE), (1, PAGE_READWRITE), (2, PAGE_READONLY), (3, PAGE_READWRITE), (4, PAGE_READWRITE | PAGE_GUARD)] { + assert!(!VirtualAlloc(page(k) as *const _, 1 << 12, MEM_COMMIT, protect).is_null()); + } + std::ptr::write_volatile(page(0) as *mut u8, 1); + std::ptr::write_volatile(page(1) as *mut u8, 1); + let _ = std::ptr::read_volatile(page(2) as *const u8); + assert!(resident_and_writable(page(0), 2 << 12)); + assert!(resident_and_writable(page(0) + 100, 64)); + // resident but read-only, also as the tail of a range + assert!(!resident_and_writable(page(2), 8)); + assert!(!resident_and_writable(page(1) + 0xFF0, 0x20)); + // never touched, or a guard page: not in the working set, `VirtualQuery` answers + assert!(!resident_and_writable(page(3), 8)); + assert!(!resident_and_writable(page(4), 8)); + let mut mbi: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); + assert_ne!(VirtualQuery(page(4) as *const _, &mut mbi, std::mem::size_of_val(&mbi)), 0); + assert_ne!(mbi.Protect & PAGE_GUARD, 0, "asking leaves a guard page a guard page"); + // no range, no pointer, a wrapping end + assert!(!resident_and_writable(page(0), 0)); + assert!(!resident_and_writable(0, 8)); + assert!(!resident_and_writable(usize::MAX - 10, 100)); + VirtualFree(base as *mut _, 0, MEM_RELEASE); + } + // more pages than one working-set query takes: `VirtualQuery` answers + let mut v = vec![1u8; 32 << 12]; + assert!(!resident_and_writable(v.as_mut_ptr() as usize, v.len())); + } }