Skip to content
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 @@ -146,7 +146,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 @@ -750,6 +750,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 @@ -813,7 +813,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
76 changes: 76 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 @@ -257,6 +261,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 @@ -418,6 +437,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 @@ -481,6 +508,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 @@ -920,6 +948,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 @@ -961,6 +992,9 @@ fn page_home(page: usize) -> Option<usize> {

/// Whether every page of `pages` (at least one) is a physical page allocated now.
pub fn physical_pages_live(pages: &[usize]) -> bool {
if alias::enabled() {
return alias::pages_live(pages);
}
!pages.is_empty() && pages.iter().all(|&p| page_home(p).is_some())
}

Expand Down Expand Up @@ -997,6 +1031,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 @@ -1016,6 +1053,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 @@ -1272,6 +1312,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 All @@ -1291,6 +1338,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 @@ -1330,13 +1385,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 @@ -1459,6 +1529,12 @@ 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| {
let page = if pages.is_null() { std::ptr::null_mut() } else { pages.add(i).cast() };
!map_aliased(*addresses.add(i), 1, page).is_null()
}) as BOOL;
}
if crate::gpu::uma::get().is_none() {
fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER);
return 0;
Expand Down
Loading
Loading