Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/xcl/src/gpu/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ pub fn readable(p: usize, len: usize) -> bool {
true
}

/// Up to this many pages `pages_readable` reads no byte of a range: a page not resident is `None`
pub(crate) const SMALL_RANGE_PAGES: usize = 16;

/// Whether each page of `[p, end)` is readable, asked of the working set – the kernel answers for
/// up to 512 pages in one call that touches none of them. `readable` asks this first because
/// `VirtualQuery` sizes the whole region around an address by walking it: in a title's 2 GB block
Expand All @@ -243,15 +246,14 @@ pub fn readable(p: usize, len: usize) -> bool {
/// out, guard, reserved, no access). In a longer range such a page is read by the kernel (a
/// byte of it): a committed one is then resident, as the caller's reads would make it, any other
/// is not readable.
fn pages_readable(p: usize, end: usize) -> Option<bool> {
pub(crate) fn pages_readable(p: usize, end: usize) -> Option<bool> {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Memory::{PAGE_GUARD, PAGE_NOACCESS};
use windows_sys::Win32::System::ProcessStatus::{K32QueryWorkingSetEx, PSAPI_WORKING_SET_EX_INFORMATION};
use windows_sys::Win32::System::Threading::GetCurrentProcess;
const SMALL: usize = 16;
const BATCH: usize = 512;
let (first, last) = (p >> 12, (end - 1) >> 12);
let small = last - first < SMALL;
let small = last - first < SMALL_RANGE_PAGES;
let mut ws: [PSAPI_WORKING_SET_EX_INFORMATION; BATCH] = unsafe { std::mem::zeroed() };
let mut at = first;
while at <= last {
Expand Down
41 changes: 39 additions & 2 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3321,6 +3321,11 @@ pub(super) unsafe fn read_committed(at: usize, len: usize) -> Vec<u8> {

/// `read_committed` of `out.len()` bytes at `at` into `out` (every byte of it written).
pub(super) unsafe fn read_committed_into(at: usize, out: &mut [u8]) {
read_committed_into_as(at, out, crate::util::under_wine())
}

/// `read_committed_into` as it reads on Windows or, with `wine`, under Wine.
unsafe fn read_committed_into_as(at: usize, out: &mut [u8], wine: bool) {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT};
use windows_sys::Win32::System::Threading::GetCurrentProcess;
Expand All @@ -3337,12 +3342,17 @@ pub(super) unsafe fn read_committed_into(at: usize, out: &mut [u8]) {
// of MiB – a 16 KiB texture cost ~120 µs at the start of a 64 MiB run, ~1.3 ms of 512 MiB.
// `ReadProcessMemory` refuses a range with a page that is not committed, no-access or a guard
// page (and leaves the guard in place: `title_bytes_leave_guard_pages_alone`). Under Wine
// that test fails with it (#2844), so there it is region by region.
// that test fails with it (#2844), so there it is one read of up to 16 pages the working set
// holds as readable (a guard page never is), else region by region.
let mut read = 0usize;
if len == 0 {
return;
}
if !crate::util::under_wine() && ReadProcessMemory(GetCurrentProcess(), at as *const c_void, out.as_mut_ptr() as *mut c_void, len, &mut read) != 0 {
if !wine && ReadProcessMemory(GetCurrentProcess(), at as *const c_void, out.as_mut_ptr() as *mut c_void, len, &mut read) != 0 {
return;
}
if wine && ((limit - 1) >> 12) - (at >> 12) < crate::gpu::shader::SMALL_RANGE_PAGES && crate::gpu::shader::pages_readable(at, limit) == Some(true) {
copy_committed_region(at, out.as_mut_ptr(), len);
return;
}
// with holes: region by region (a refused `ReadProcessMemory` costs more than the walk; a
Expand All @@ -3351,6 +3361,8 @@ pub(super) unsafe fn read_committed_into(at: usize, out: &mut [u8]) {
let mut p = at;
while p < limit {
let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed();
#[cfg(test)]
tests::REGION_QUERIES.with(|n| n.set(n.get() + 1));
if VirtualQuery(p as *const c_void, &mut m, std::mem::size_of_val(&m)) == 0 || m.RegionSize == 0 {
break;
}
Expand Down Expand Up @@ -4858,6 +4870,8 @@ pub(super) mod tests {
thread_local! {
/// the VirtualQuery calls `mirror_source` made on this thread
pub(super) static QUERIES: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
/// the VirtualQuery calls `read_committed_into` made on this thread
pub(super) static REGION_QUERIES: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

/// One call of the queue's page-mapping method: `ranges` (kind, start, count) from GPU-view
Expand Down Expand Up @@ -7200,6 +7214,29 @@ pub(super) mod tests {
}
}

#[test]
fn a_few_resident_pages_are_read_without_asking_their_regions() {
use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE};
let few = crate::gpu::shader::SMALL_RANGE_PAGES << 12;
unsafe {
let base = VirtualAlloc(std::ptr::null(), 2 * few, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) as usize;
assert_ne!(base, 0);
std::ptr::write_bytes(base as *mut u8, 0x5A, 2 * few);
let under_wine = |at: usize, len: usize| {
let mut out = vec![0u8; len];
read_committed_into_as(at, &mut out, true);
out
};
let asked = || REGION_QUERIES.with(|n| n.get());
let before = asked();
assert_eq!(under_wine(base + 0x1F0, 32), vec![0x5A; 32]);
assert_eq!(under_wine(base + 0x1000, few), vec![0x5A; few]);
assert_eq!(asked(), before, "no region asked about");
assert_eq!(under_wine(base, 2 * few), vec![0x5A; 2 * few]);
VirtualFree(base as *mut c_void, 0, MEM_RELEASE);
}
}

/// A guard or no-access page in the title's bytes reads as zeros, and the guard stays (a
/// title's guard page is its own: reading must not use it up).
#[test]
Expand Down
Loading