diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 5d2be0114..0c3db4f89 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1036,17 +1036,55 @@ unsafe fn unmap_away(away: &mut Away, at: usize, committed: &std::ops::Range Result { + 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); + } + 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 +1215,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 +2225,61 @@ 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 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]