From f1e2113a76246e766570c66dda69cf767f7b6931 Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Tue, 24 Nov 2026 07:41:27 +0100 Subject: [PATCH 1/2] KernelX: a page mapped away moves to its next address directly, not by way of its home A physical page mapped at one address that the title maps at another went back home first: its home was committed again, the page copied there, the old address decommitted, then the home copied to the new address and decommitted. In a sparse window each commit and decommit is also a tile mapping. A retail game moving pages one at a time after its first movie spent over 20 s in them, and its watchdog ended it. The page is now copied from where it is to the new address and the old address is given up; its home is not touched. A page never used where it was (lazy) still comes from its home. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SKtJseucqXR6iuAE6HBtiA --- crates/xcl/src/hle/kernelx.rs | 87 ++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 5d2be0114..843aaa8cd 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1036,17 +1036,52 @@ unsafe fn unmap_away(away: &mut Away, at: usize, committed: &std::ops::Range Result { + away.at.remove(&old); + away.page.remove(&page); + if lazy::unmark(old) { + return Ok(false); + } + if !in_uma && mem::VirtualAlloc(at as *const c_void, PHYS_PAGE, mem::MEM_COMMIT, mem::PAGE_READWRITE).is_null() { + return Err(()); + } + std::ptr::copy_nonoverlapping(old as *const u8, at as *mut u8, PHYS_PAGE); + if crate::gpu::uma::get().is_some_and(|u| u.is_sparse(at)) { + crate::hle::d3d12x::sparse::committed(at, PHYS_PAGE); + } + release_at(old); + away.at.insert(at, page); + away.page.insert(page, at); + Ok(true) +} + pub unsafe extern "system" fn AllocateTitlePhysicalPages(_p: HANDLE, _t: u32, num: *mut usize, arr: *mut usize) -> BOOL { if num.is_null() || arr.is_null() || *num == 0 { fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER); @@ -1177,7 +1212,14 @@ unsafe fn map_pages(away: &mut Away, va: *mut c_void, n: usize, arr: *mut usize) continue; } if let Some(&old) = away.page.get(&p) { - unmap_away(away, old, &(0..0)); + match move_away(away, old, at, p, in_uma(at)) { + Ok(true) => continue, + Ok(false) => {} + Err(()) => { + fnd::SetLastError(fnd::ERROR_INVALID_ADDRESS); + return std::ptr::null_mut(); + } + } } let fresh = !in_uma(at) && !committed_page(at); let home = page_home(p).unwrap(); @@ -2180,6 +2222,39 @@ mod tests { } } + thread_local! { + /// pages `unmap_away` copied back home on this thread + pub(super) static HOME_COPIES: std::cell::Cell = const { std::cell::Cell::new(0) }; + } + + /// A page mapped away that the title maps somewhere else goes there straight, with what it + /// holds, and not by way of its home; the address it left is given up. + #[test] + fn a_page_away_moves_on_without_going_home() { + unsafe { + let Some(_) = crate::gpu::uma::get() else { return }; + let mut n = 1usize; + let mut pages = [0usize; 1]; + assert_eq!(AllocateTitlePhysicalPages(std::ptr::null_mut(), 0, &mut n, pages.as_mut_ptr()), 1); + let r = mem::VirtualAlloc(std::ptr::null(), 2 * PHYS_PAGE, mem::MEM_RESERVE, mem::PAGE_READWRITE) as usize; + let (a, b) = (r as *mut c_void, (r + PHYS_PAGE) as *mut c_void); + let mut one = [pages[0] as u64]; + assert_eq!(XMemMapPhysicalPagesScatter(&a, 1, one.as_mut_ptr()), 1); + *(a as *mut u32) = 7; + *((r + PHYS_PAGE - 4) as *mut u32) = 8; + let before = HOME_COPIES.with(|c| c.get()); + assert_eq!(XMemMapPhysicalPagesScatter(&b, 1, one.as_mut_ptr()), 1); + assert_eq!(HOME_COPIES.with(|c| c.get()), before, "the page went by way of its home"); + assert_eq!(*(b as *const u32), 7); + assert_eq!(*((r + 2 * PHYS_PAGE - 4) as *const u32), 8); + assert!(!committed_page(r), "the address the page left stays committed"); + let mut zero = [0u64]; + assert_eq!(XMemMapPhysicalPagesScatter(&b, 1, zero.as_mut_ptr()), 1); + assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 1, pages.as_mut_ptr()), 1); + mem::VirtualFree(r as *mut c_void, 0, mem::MEM_RELEASE); + } + } + /// A page mapped at another page's home, then the whole allocation mapped back home: the /// first page goes home and leaves that address committed, as the second one's home. #[test] From 5b693e67524d63282911ce9cfacc7b980cbb04af Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Tue, 24 Nov 2026 09:10:34 +0100 Subject: [PATCH 2/2] KernelX: a page whose move cannot be committed stays mapped where it was Reported in review: move_away dropped both lookups of a page before committing its destination, so a failed commit left its bytes at the old address with nothing pointing at them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SKtJseucqXR6iuAE6HBtiA --- crates/xcl/src/hle/kernelx.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 843aaa8cd..0c3db4f89 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1064,14 +1064,17 @@ unsafe fn release_at(at: usize) { /// at a time after its first movie spent 20 s in them and its watchdog ended it. False: the page /// was never used at `old` (its home holds it, zeros) and is not mapped anywhere now. unsafe fn move_away(away: &mut Away, old: usize, at: usize, page: usize, in_uma: bool) -> Result { - away.at.remove(&old); - away.page.remove(&page); if lazy::unmark(old) { + away.at.remove(&old); + away.page.remove(&page); return Ok(false); } + // (a destination that cannot be committed leaves the page where it was, mapped as before) if !in_uma && mem::VirtualAlloc(at as *const c_void, PHYS_PAGE, mem::MEM_COMMIT, mem::PAGE_READWRITE).is_null() { return Err(()); } + away.at.remove(&old); + away.page.remove(&page); std::ptr::copy_nonoverlapping(old as *const u8, at as *mut u8, PHYS_PAGE); if crate::gpu::uma::get().is_some_and(|u| u.is_sparse(at)) { crate::hle::d3d12x::sparse::committed(at, PHYS_PAGE); @@ -2255,6 +2258,28 @@ mod tests { } } + /// A move whose destination cannot be committed fails with the page still mapped away where + /// it was: its bytes and both lookups are kept. + #[test] + fn a_page_whose_move_fails_stays_mapped_where_it_was() { + unsafe { + let old = mem::VirtualAlloc(std::ptr::null(), PHYS_PAGE, mem::MEM_RESERVE | mem::MEM_COMMIT, mem::PAGE_READWRITE) as usize; + *(old as *mut u32) = 7; + // an address with nothing reserved: committing there fails + let at = mem::VirtualAlloc(std::ptr::null(), PHYS_PAGE, mem::MEM_RESERVE, mem::PAGE_READWRITE) as usize; + mem::VirtualFree(at as *mut c_void, 0, mem::MEM_RELEASE); + let page = 0x7E57_0000usize; + let mut away = Away { at: Default::default(), page: Default::default(), gone: Vec::new() }; + away.at.insert(old, page); + away.page.insert(page, old); + assert!(move_away(&mut away, old, at, page, false).is_err()); + assert_eq!(away.page.get(&page), Some(&old), "the page lost where it is"); + assert_eq!(away.at.get(&old), Some(&page)); + assert_eq!(*(old as *const u32), 7); + mem::VirtualFree(old as *mut c_void, 0, mem::MEM_RELEASE); + } + } + /// A page mapped at another page's home, then the whole allocation mapped back home: the /// first page goes home and leaves that address committed, as the second one's home. #[test]