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
3 changes: 2 additions & 1 deletion crates/xcl/src/hle/d3d12x/cpu_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ pub(super) fn writable(va: usize, bytes: usize) -> bool {
if crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(va, bytes)) {
return true;
}
crate::hle::kernelx::lazy::ensure(va, bytes);
// (held while the protection is asked: a view of a physical page is not guarded again meanwhile)
let _open = crate::hle::kernelx::lazy::ensure(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 = unsafe { std::mem::zeroed() };
let found = unsafe { VirtualQuery(va as *const c_void, &mut m, std::mem::size_of_val(&m)) } != 0;
Expand Down
3 changes: 3 additions & 0 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,9 @@ unsafe fn refresh_once(queue: *mut c_void) {
take_written(base, size, &mut buf, &mut dirty, &mut page);
}
});
// views of physical pages are not write-watched: their writes are caught by guards
// (`XCL_PAGE_ALIAS`), a write through one view counting for all of them
crate::hle::kernelx::guards::take_dirty(&mut dirty, page as usize);
dirty.sort_unstable();
dirty.len()
});
Expand Down
4 changes: 3 additions & 1 deletion crates/xcl/src/hle/dstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,9 @@ unsafe fn read_own(at: usize, out: *mut u8, len: usize) -> bool {
/// Whether `n` bytes at `p` are committed, writable memory.
fn writable(p: usize, n: usize) -> bool {
let Some(end) = p.checked_add(n) else { return false };
crate::hle::kernelx::lazy::ensure(p, n);
// (held while the protection is asked: a view of a physical page is not guarded again
// meanwhile; the copy that follows faults into the guard if one is)
let _open = crate::hle::kernelx::lazy::ensure(p, n);
if crate::gpu::uma::get().is_some_and(|u| u.in_committed_window(p, n)) {
return true;
}
Expand Down
70 changes: 70 additions & 0 deletions crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ pub fn exports() -> Vec<(&'static str, usize)> {
pub const MEM_GRAPHICS: u32 = 0x1000_0000;
mod placeholders;
pub mod lazy;
#[allow(dead_code)] // `replace_with_reservation` and its inverse: not used yet
pub mod sections;
pub mod guards;
pub mod alias;
pub const MEM_TITLE: u32 = 0x4000_0000;
const STD_ALLOC_MASK: u32 = 0x1000 | 0x2000 | 0x8_0000 | 0x10_0000 | 0x20_0000 | 0x100_0000;
const STD_PROTECT_MASK: u32 = 0xFF | 0x100 | 0x200 | 0x400;
Expand Down Expand Up @@ -248,6 +252,21 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t:
return q;
}
let uma = crate::gpu::uma::get();
// physical page aliasing: the title's reservations of 64 KiB pages hold views of its pages,
// so it maps pages there and does not commit memory of its own (a retail title never does)
if alias::enabled() && !addr.is_null() && t & 0x1000 != 0 && alias::in_space(addr as usize) {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| crate::xlog_warn!("VirtualAlloc: a commit at {addr:p} in a reservation of 64 KiB pages that holds physical pages (XCL_PAGE_ALIAS) – refused"));
fnd::SetLastError(fnd::ERROR_INVALID_ADDRESS);
return std::ptr::null_mut();
}
if alias::enabled() && addr.is_null() && t & 0x2000 != 0 && t & 0x1000 == 0 && t & MEM_64K_PAGES == MEM_64K_PAGES {
if let Some(q) = alias::reserve_space(size) {
mirrors::reserved(q, size, t, p);
trace_mem("VirtualAlloc (64K pages, physical page space)", 0, size, t as u64, p, q);
return q as *mut c_void;
}
}
// committing inside one of the title's reservations in a sparse window
if !addr.is_null() && t & 0x1000 != 0 && uma.is_some_and(|u| u.is_sparse(addr as usize)) {
let q = commit_sparse(addr as usize, size, p);
Expand Down Expand Up @@ -409,6 +428,14 @@ pub unsafe extern "system" fn VirtualFree(addr: *mut c_void, size: usize, t: u32
if let Some(ok) = placeholders::free(addr, size, t) {
return ok;
}
if alias::enabled() {
if let Some(ok) = alias::free_space(addr as usize, size, t) {
if ok && t & mem::MEM_RELEASE != 0 {
mirrors::released(addr as usize);
}
return ok as BOOL;
}
}
// the title's reservations in a sparse window: decommit (and on release give the address
// space back); their video memory goes with it
if let Some(u) = crate::gpu::uma::get().filter(|u| u.is_sparse(addr as usize)) {
Expand Down Expand Up @@ -472,6 +499,7 @@ pub unsafe extern "system" fn VirtualFreeEx(proc_: HANDLE, addr: *mut c_void, si
let a = addr as usize;
if crate::gpu::uma::get().is_some_and(|u| u.is_sparse(a) || u.contains(a))
|| (t & 0x8000 != 0 && in_graphics_reservation(a))
|| alias::in_space(a)
{
return VirtualFree(addr, size, t);
}
Expand Down Expand Up @@ -893,6 +921,9 @@ static AWAY: std::sync::Mutex<Option<Away>> = std::sync::Mutex::new(None);
/// Whether a physical page is mapped at the page holding `va` away from its home – committed
/// or not yet (`lazy`: a page never used is committed at its first access).
pub fn physical_page_at(va: usize) -> bool {
if alias::enabled() {
return alias::physical_page_at(va);
}
AWAY.lock().unwrap().as_ref().is_some_and(|a| a.at.contains_key(&(va & !(PHYS_PAGE - 1))))
}

Expand Down Expand Up @@ -965,6 +996,9 @@ unsafe fn resolve_physical_pages(away: Option<&Away>, pages: &[usize], offset: u
/// number from `AllocateTitlePhysicalPages`). Returns `false` if `offset >= PHYS_PAGE_SIZE`,
/// `pages` is too short, or any page is not a live allocation.
pub unsafe fn read_physical_pages(pages: &[usize], offset: usize, dst: &mut [u8]) -> bool {
if alias::enabled() {
return alias::read(pages, offset, dst);
}
if dst.is_empty() {
return offset < PHYS_PAGE;
}
Expand All @@ -984,6 +1018,9 @@ pub unsafe fn read_physical_pages(pages: &[usize], offset: usize, dst: &mut [u8]
/// Copy `src` into `pages` starting at `offset` within the first page. Validates every page
/// before writing so the destination gets all bytes or nothing.
pub unsafe fn write_physical_pages(pages: &[usize], offset: usize, src: &[u8]) -> bool {
if alias::enabled() {
return alias::write(pages, offset, src);
}
if src.is_empty() {
return offset < PHYS_PAGE;
}
Expand Down Expand Up @@ -1216,6 +1253,13 @@ pub unsafe extern "system" fn AllocateTitlePhysicalPages(_p: HANDLE, _t: u32, nu
fnd::SetLastError(fnd::ERROR_NOT_ENOUGH_MEMORY);
return 0;
};
if alias::enabled() {
if alias::allocate(n, arr) {
return 1;
}
fnd::SetLastError(fnd::ERROR_NOT_ENOUGH_MEMORY);
return 0;
}
let Some(u) = crate::gpu::uma::get() else {
fnd::SetLastError(fnd::ERROR_NOT_ENOUGH_MEMORY);
return 0;
Expand Down Expand Up @@ -1243,6 +1287,14 @@ pub unsafe extern "system" fn AllocateTitlePhysicalPages(_p: HANDLE, _t: u32, nu
}

pub unsafe extern "system" fn FreeTitlePhysicalPages(_p: HANDLE, n: usize, arr: *mut usize) -> BOOL {
if alias::enabled() {
if arr.is_null() {
fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER);
return 0;
}
alias::free(std::slice::from_raw_parts(arr, n));
return 1;
}
let Some(u) = crate::gpu::uma::get() else { return 0 };
if arr.is_null() {
fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER);
Expand Down Expand Up @@ -1282,13 +1334,28 @@ pub unsafe extern "system" fn FreeTitlePhysicalPages(_p: HANDLE, n: usize, arr:
}

pub unsafe extern "system" fn MapTitlePhysicalPages(va: *mut c_void, n: usize, _t: u32, _p: u32, arr: *mut usize) -> *mut c_void {
if alias::enabled() {
return map_aliased(va, n, arr);
}
let mut g = AWAY.lock().unwrap();
let away = g.get_or_insert_with(Away::default);
let r = map_pages(away, va, n, arr);
give_back_homes(away);
r
}

/// `MapTitlePhysicalPages` with physical page aliasing (`alias`): the title always names the
/// address (a retail title never maps a page at its home, which is XCL's own view here).
unsafe fn map_aliased(va: *mut c_void, n: usize, arr: *mut usize) -> *mut c_void {
if va.is_null() || n == 0 {
fnd::SetLastError(if va.is_null() { fnd::ERROR_NOT_SUPPORTED } else { fnd::ERROR_INVALID_PARAMETER });
return std::ptr::null_mut();
}
note_mapping_at(va as usize);
let pages: Vec<usize> = if arr.is_null() { vec![0; n] } else { std::slice::from_raw_parts(arr, n).to_vec() };
if alias::map(va as usize, &pages) { va } else { std::ptr::null_mut() }
}

/// `MapTitlePhysicalPages` with `AWAY` held; the homes it moves away are given back by the
/// caller (`give_back_homes`).
unsafe fn map_pages(away: &mut Away, va: *mut c_void, n: usize, arr: *mut usize) -> *mut c_void {
Expand Down Expand Up @@ -1409,6 +1476,9 @@ pub unsafe extern "system" fn XMemMapPhysicalPagesScatter(addresses: *const *mut
fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER);
return 0;
}
if alias::enabled() {
return (0..n).all(|i| !map_aliased(*addresses.add(i), 1, pages.add(i) as *mut usize).is_null()) as BOOL;
}
if crate::gpu::uma::get().is_none() {
fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER);
return 0;
Expand Down
Loading
Loading