Skip to content
Merged
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
41 changes: 30 additions & 11 deletions crates/xcl/src/hle/d3d12x/cpu_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,39 @@ unsafe fn write_now(va: usize, words: &[u32]) {
}
}

/// Whether `[va, va + 8)` is 8-byte aligned, committed, and writable without `PAGE_GUARD`.
unsafe fn u64_writable_now(va: usize) -> bool {
if va == 0 || !va.is_multiple_of(8) {
return false;
}
let Some(end) = va.checked_add(8) else { return false };
if crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(va, 8)) {
return true;
}
use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_EXECUTE_READWRITE, PAGE_GUARD, PAGE_READWRITE};
let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed();
if VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) == 0 || m.State != MEM_COMMIT {
return false;
}
m.Protect & PAGE_GUARD == 0
&& m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0
&& (m.BaseAddress as usize).saturating_add(m.RegionSize) >= end
}

/// An 8-byte aligned 64-bit value at `va`, if the memory is still there and writable.
unsafe fn write_u64_now(va: usize, value: u64) {
let Some(end) = va.checked_add(8) else { return };
if !crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(va, 8)) {
use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_EXECUTE_READWRITE, PAGE_GUARD, PAGE_READWRITE};
let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed();
if VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) == 0 || m.State != MEM_COMMIT {
return;
}
if m.Protect & PAGE_GUARD != 0 || m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) == 0 || (m.BaseAddress as usize).saturating_add(m.RegionSize) < end {
return;
}
if u64_writable_now(va) {
(*(va as *const AtomicU64)).store(value, Ordering::Release);
}
}

/// Advance an 8-byte aligned 64-bit value at `va` to at least `value` (`fetch_max`), if the memory
/// is still committed and writable. Best-effort check before the atomic write (same TOCTOU
/// trade-off as `write_u64_now`: a concurrent title decommit between check and store is unguarded).
pub(super) unsafe fn max_u64_now(va: usize, value: u64) {
if u64_writable_now(va) {
(*(va as *const AtomicU64)).fetch_max(value, Ordering::AcqRel);
}
(*(va as *const AtomicU64)).store(value, Ordering::Release);
}

/// Whether `[va, va + bytes)` is memory the CPU can write for the GPU later.
Expand Down
Loading
Loading