Skip to content
Open
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
30 changes: 30 additions & 0 deletions crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub fn exports() -> Vec<(&'static str, usize)> {
XMemFreePhysicalPages,
XMemTransferMemory,
XMemGetWorkingSetStatistics,
XMemGetPhysicalMemoryLayout,
XMemIsToolsMemAvailable,
TitleMemoryStatus,
JobTitleMemoryStatus,
Expand Down Expand Up @@ -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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not certain about this one: the XMEM_PHYSICAL_MEMORY_LAYOUT layout (exactly two ULONG_PTR fields, no size/version field) is assumed and written through an untyped *mut u8, with no cited source, and no title has been seen calling it. If the real struct has more fields or a leading size/version, the title reads uninitialised stack for them after a TRUE return, which is worse than the old failure return. A #[repr(C)] struct like TitleMemoryStatusT would at least pin the assumed layout.

if layout.is_null() {
fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER);
return 0;
}
(layout as *mut usize).write_unaligned(0); // LowestPageNumber

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LowestPageNumber is reported as 0, but XCL's allocator never hands out page 0: page_number(index) = (index + 1) * FRAMES makes the first title page 16, and the module says "0 means no page" (it's the unmap value in XMemMapPhysicalPagesScatter). A title that indexes its page table with (page - LowestPageNumber) / 16, or checks page >= LowestPageNumber, is off by one 64 KiB page, and the unmap sentinel passes as a valid page. Deriving it from page_number(0) keeps it in step with the allocator.

(layout.add(std::mem::size_of::<usize>()) as *mut usize).write_unaligned(0); // LowestPageNumberGpuOptimalBandwidth

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not certain about this one: with both fields equal, the standard-bandwidth region is empty (GpuOptimal - Lowest = 0), which no real console reports, since Series X and S both have a non-empty standard region. A title that splits its budget from LowestPageNumberGpuOptimalBandwidth - LowestPageNumber, or asserts the standard region is non-empty, gets a 0-byte pool and fails its own allocation or breakpoint. Before this PR the stub's failure return at least let such titles fall back.

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 {
Expand Down Expand Up @@ -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() {
Expand Down
Loading