diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index eb20edb1ef..0c5200f3a1 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -58,6 +58,7 @@ pub fn exports() -> Vec<(&'static str, usize)> { XMemFreePhysicalPages, XMemTransferMemory, XMemGetWorkingSetStatistics, + XMemGetPhysicalMemoryLayout, XMemIsToolsMemAvailable, TitleMemoryStatus, JobTitleMemoryStatus, @@ -1639,6 +1640,22 @@ pub unsafe extern "system" fn XMemGetWorkingSetStatistics(_flags: u32, stats: *m 1 } +/// `BOOL XMemGetPhysicalMemoryLayout(XMEM_PHYSICAL_MEMORY_LAYOUT* layout)`: two ULONG_PTR fields – +/// the lowest title physical page number, then the lowest page number of GPU-optimal-bandwidth +/// ("fast") memory. The console splits the title's physical memory into standard- and +/// GPU-optimal-bandwidth regions; here it is one pool with no bandwidth split, so both are 0 – all +/// title memory counts as GPU-optimal from page 0, matching the single fast-memory pool +/// `XMemGetWorkingSetStatistics` reports. +pub unsafe extern "system" fn XMemGetPhysicalMemoryLayout(layout: *mut u8) -> BOOL { + if layout.is_null() { + fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER); + return 0; + } + (layout as *mut usize).write_unaligned(0); // LowestPageNumber + (layout.add(std::mem::size_of::()) as *mut usize).write_unaligned(0); // LowestPageNumberGpuOptimalBandwidth + 1 +} + /// `BOOL XMemTransferMemory(direction, size)`: moving budget between the title and the tools /// pools – there is one budget here pub unsafe extern "system" fn XMemTransferMemory(_direction: u32, _size: usize) -> BOOL { @@ -3237,6 +3254,19 @@ pub unsafe extern "system" fn DisableThreadLibraryCalls(h: usize) -> BOOL { mod tests { use super::*; + #[test] + fn physical_memory_layout_writes_both_page_numbers() { + // 0xCC..-filled buffer with a guard word after the two fields + let mut buf = [usize::MAX; 3]; + unsafe { + assert_eq!(XMemGetPhysicalMemoryLayout(buf.as_mut_ptr() as *mut u8), 1); + assert_eq!(XMemGetPhysicalMemoryLayout(std::ptr::null_mut()), 0, "null layout fails"); + } + assert_eq!(buf[0], 0, "LowestPageNumber"); + assert_eq!(buf[1], 0, "LowestPageNumberGpuOptimalBandwidth"); + assert_eq!(buf[2], usize::MAX, "nothing written past the two fields"); + } + /// `XCL_COHERENT_HEAPS=sparse[:N]`: which heaps go to the sparse windows #[test] fn sparse_heaps_setting() {