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
6 changes: 4 additions & 2 deletions crates/xcl/src/eh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,12 @@ fn is_writable(va: usize, bytes: usize) -> bool {
}
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
(found
&& m.State == MEM_COMMIT
&& m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0
&& m.BaseAddress as usize + m.RegionSize >= va + bytes
&& m.BaseAddress as usize + m.RegionSize >= va + bytes)
// (pages XCL made read-only: the zeroing faults and is let through, `XCL_GUEST_WRITE_GUARD=1`)
|| crate::hle::kernelx::guest_write::writable(va, bytes)
}

const ILLEGAL_INSTRUCTION: u32 = 0xC000_001D;
Expand Down
20 changes: 12 additions & 8 deletions crates/xcl/src/hle/d3d12x/cpu_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ unsafe fn write_now_in(va: usize, words: &[u32], run: &dyn Fn(usize, usize) -> O
if !crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(va, bytes)) {
use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_EXECUTE_READWRITE, 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_READWRITE | PAGE_EXECUTE_READWRITE) == 0 || (m.BaseAddress as usize + m.RegionSize) < va + bytes {
let rw = VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) != 0
&& m.State == MEM_COMMIT
&& m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0
&& (m.BaseAddress as usize + m.RegionSize) >= va + bytes;
// (pages XCL made read-only: the store faults and is let through, `XCL_GUEST_WRITE_GUARD=1`)
if !rw && !crate::hle::kernelx::guest_write::writable(va, bytes) {
return;
}
}
Expand Down Expand Up @@ -181,9 +183,10 @@ unsafe fn u64_writable_now(va: usize) -> bool {
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_GUARD == 0
&& m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0
&& (m.BaseAddress as usize).saturating_add(m.RegionSize) >= end
&& (m.BaseAddress as usize).saturating_add(m.RegionSize) >= end)
|| crate::hle::kernelx::guest_write::writable(va, 8)
}

/// An 8-byte aligned 64-bit value at `va`, if the memory is still there and writable.
Expand Down Expand Up @@ -220,10 +223,11 @@ pub(super) fn writable(va: usize, bytes: usize) -> bool {
use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_EXECUTE_READWRITE, PAGE_READWRITE};
let mut m: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() };
let found = unsafe { VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) } != 0;
found
(found
&& m.State == MEM_COMMIT
&& m.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0
&& m.BaseAddress as usize + m.RegionSize >= va + bytes
&& m.BaseAddress as usize + m.RegionSize >= va + bytes)
|| crate::hle::kernelx::guest_write::writable(va, bytes)
}

/// Receipt published only after the worker has applied every deferred store in a submission.
Expand Down
4 changes: 3 additions & 1 deletion crates/xcl/src/hle/d3d12x/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ unsafe fn note_signaled(block: *const u8, index: i32) {
return;
}
let out = (block.add(0x10) as *const usize).read_unaligned();
if writable(out, 4) {
// (pages XCL made read-only or no-access count too, `XCL_GUEST_WRITE_GUARD=1`: the store
// faults and is opened by the protector's handler)
if writable(out, 4) || crate::hle::kernelx::guest_write::writable(out, 4) {
(out as *mut i32).write_unaligned(index);
}
}
Expand Down
10 changes: 7 additions & 3 deletions crates/xcl/src/hle/dstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,11 @@ unsafe fn write_title(p: usize, bytes: &[u8]) -> bool {
fn NtWriteVirtualMemory(process: HANDLE, at: *mut c_void, data: *const c_void, len: usize, done: *mut usize) -> i32;
}
let mut done = 0usize;
let st = NtWriteVirtualMemory(windows_sys::Win32::System::Threading::GetCurrentProcess(), p as *mut c_void, bytes.as_ptr().cast(), bytes.len(), &mut done);
st >= 0 && done == bytes.len()
crate::hle::kernelx::guest_write::kernel_write("DirectStorage", p, bytes.len(), || {
// SAFETY: the kernel copy returns a status instead of faulting
let st = unsafe { NtWriteVirtualMemory(windows_sys::Win32::System::Threading::GetCurrentProcess(), p as *mut c_void, bytes.as_ptr().cast(), bytes.len(), &mut done) };
st >= 0 && done == bytes.len()
})
}

/// Where the CPU reads or writes `[p, p + n)` of the title's memory for a request: a GPU view of
Expand Down Expand Up @@ -934,7 +937,8 @@ fn writable(p: usize, n: usize) -> bool {
}
let rw = 0x04 | 0x08 | 0x40 | 0x80; // PAGE_READWRITE / WRITECOPY / EXECUTE_READWRITE / EXECUTE_WRITECOPY
if m.State != MEM_COMMIT || m.Protect & rw == 0 || m.Protect & 0x100 != 0 {
return false;
// (pages XCL made read-only count, `XCL_GUEST_WRITE_GUARD=1`)
return crate::hle::kernelx::guest_write::writable(p, n);
}
at = m.BaseAddress as usize + m.RegionSize;
}
Expand Down
8 changes: 4 additions & 4 deletions crates/xcl/src/hle/dstorage/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,10 @@ extern "system" {
fn write_destination(at: usize, bytes: &[u8]) -> bool {
let mut done = 0;
// Use the current-process handle so an access fault is returned as a status.
let status = unsafe {
NtWriteVirtualMemory(GetCurrentProcess(), at as *mut c_void, bytes.as_ptr().cast(), bytes.len(), &mut done)
};
status >= 0 && done == bytes.len()
crate::hle::kernelx::guest_write::kernel_write("DirectStorage", at, bytes.len(), || {
let status = unsafe { NtWriteVirtualMemory(GetCurrentProcess(), at as *mut c_void, bytes.as_ptr().cast(), bytes.len(), &mut done) };
status >= 0 && done == bytes.len()
})
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub mod alias;
pub mod guards;
mod sections;
pub mod lazy;
pub mod guest_write;
pub const MEM_TITLE: u32 = 0x4000_0000;
const STD_ALLOC_MASK: u32 = 0x1000 | 0x2000 | 0x8_0000 | 0x10_0000 | 0x20_0000 | 0x100_0000;
/// Standard Win32 page access + PAGE_GUARD (0x100). Strips console PAGE_GRAPHICS_* bits as well
Expand Down
8 changes: 7 additions & 1 deletion crates/xcl/src/hle/kernelx/guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ fn writable(view: usize) -> bool {
n != 0 && m.State == MEM_COMMIT && m.Protect == PAGE_READWRITE
}

/// Whether the 64 KiB page at `at` is a guarded view (`guest_write`: XCL may have made it
/// read-only, and its handler opens it on a write fault). One atomic load while nothing is watched.
pub(super) fn is_view(at: usize) -> bool {
WATCHING.load(Ordering::Acquire) && with(|s| s.at.contains_key(&(at & !(PAGE - 1))))
}

/// Physical page `page` is shown at `view` (64 KiB aligned): writes there are caught from now on.
/// A view not committed yet cannot be guarded; `committed` notes it when it is.
pub fn watch(page: usize, view: usize) {
Expand Down Expand Up @@ -224,7 +230,7 @@ pub fn open_until_done(opened: Opened, ov: usize) {

/// Whether the overlapped I/O at `ov` is still going on (its `Internal` is STATUS_PENDING). The
/// title may have freed the OVERLAPPED once it is done, so it is read without faulting.
fn still_pending(ov: usize) -> bool {
pub(super) fn still_pending(ov: usize) -> bool {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
const STATUS_PENDING: usize = 0x103;
Expand Down
Loading
Loading