Skip to content
Draft
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
44 changes: 41 additions & 3 deletions crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,9 +1221,11 @@ pub unsafe extern "system" fn XMemMapPhysicalPages(va: *mut c_void, n: usize, ar
}

/// `BOOL XMemMapPhysicalPagesScatter(void** addresses, SIZE_T count, ULONG64* pages)`: one 64 KiB
/// page at each address (page 0: the address is unmapped)
/// page at each address (page 0: the address is unmapped). No page array unmaps every address:
/// a retail game unmaps that way while it streams its world and stops on its own `int3` when the
/// call fails.
pub unsafe extern "system" fn XMemMapPhysicalPagesScatter(addresses: *const *mut c_void, n: usize, pages: *mut u64) -> BOOL {
if addresses.is_null() || pages.is_null() {
if addresses.is_null() {
fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER);
return 0;
}
Expand All @@ -1233,7 +1235,12 @@ pub unsafe extern "system" fn XMemMapPhysicalPagesScatter(addresses: *const *mut
}
let mut g = AWAY.lock().unwrap();
let away = g.get_or_insert_with(Away::default);
let ok = (0..n).all(|i| !map_pages(away, *addresses.add(i), 1, pages.add(i) as *mut usize).is_null());
// no page array: page 0 (unmap) for every address
let ok = (0..n).all(|i| {
let mut none = 0usize;
let page = if pages.is_null() { &mut none as *mut usize } else { pages.add(i) as *mut usize };
!map_pages(away, *addresses.add(i), 1, page).is_null()
});
give_back_homes(away);
ok as BOOL
}
Expand Down Expand Up @@ -2008,6 +2015,37 @@ mod tests {
}
}

/// `XMemMapPhysicalPagesScatter` with no page array unmaps each address, as page 0 does (a
/// retail game unmaps so while it streams; refused, it stopped on its own `int3`).
#[test]
fn scatter_without_pages_unmaps() {
unsafe {
let Some(_) = crate::gpu::uma::get() else { return };
let mut n = 2;
let mut pages = [0usize; 2];
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;
assert!(r != 0);
let addrs = [r as *mut c_void, (r + PHYS_PAGE) as *mut c_void];
let mut ids = [pages[0] as u64, pages[1] as u64];
assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 2, ids.as_mut_ptr()), 1);
(r as *mut u32).write_volatile(7);
((r + PHYS_PAGE) as *mut u32).write_volatile(9);
assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 2, std::ptr::null_mut()), 1, "no page array: unmapped, not refused");
for a in addrs {
assert!(!crate::gpu::shader::readable(a as usize, 4), "{a:p} still mapped");
}
// the pages kept what was written: mapped again, they read it
assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 2, ids.as_mut_ptr()), 1);
assert_eq!(((r as *const u32).read_volatile(), ((r + PHYS_PAGE) as *const u32).read_volatile()), (7, 9));
assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 2, std::ptr::null_mut()), 1);
// no addresses: the same answer with and without a page array
let mut none = [0u64; 1];
assert_eq!(XMemMapPhysicalPagesScatter(addrs.as_ptr(), 0, std::ptr::null_mut()), XMemMapPhysicalPagesScatter(addrs.as_ptr(), 0, none.as_mut_ptr()));
mem::VirtualFree(r as *mut c_void, 0, mem::MEM_RELEASE);
}
}

/// `XMemMapPhysicalPagesScatter` against a model of physical pages: random scatters of
/// pages to addresses (page 0: unmap), writes through the addresses and maps of the whole
/// allocation at its home. After each step every page reads what was last written to it
Expand Down
Loading