From f00e1c4fec6207ed99489947b0180caefaf57c53 Mon Sep 17 00:00:00 2001 From: Prime <215761102+Prime1997@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:32:22 -0800 Subject: [PATCH] Memory: use a 2 MiB grain for allocator page size two Correct PageSize=2 rounding while preserving explicit alignment, checked sizes, and recorded allocation/free accounting. Update the test title's alignment and zeroing-tail probes to the same 2 MiB contract, keeping the tail read inside its allocation. Three focused tests passed on the earlier preserved patch; restoring the old 4 MiB grain failed all three controls. The new squashed head has not been run individually. Full-suite and retail merge checks remain pending. Assisted-by: Codex (GPT-6) --- README.md | 2 +- crates/xcl/src/hle/kernelx.rs | 60 +++++++++++++++++++++++++++++++++-- tests/testtitle/src/main.rs | 12 +++---- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 38c61a9e91..85470bb69e 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ A gamepad is recommended ([Controls](#controls)). The game folder is never modif |---|---|---|---| | **PE loader** | ✅ | relocations, imports by name / ordinal / delay-load, TLS callbacks, exception tables, game DLLs, data-driven import routing (HLE / Windows / stub) | the game's 16 console DLLs load with 0 unresolved imports | | **kernelx** | ✅ | title memory (budget from MicrosoftGame.Config, physical pages), threads on the PC's processors, modules, OS / console info | unit + end-to-end tests | -| **Memory** | ✅ | title graphics memory shared by CPU and GPU without copies, up to 16 GB (4 GiB windows) | GPU tests: loads, stores and atomics across windows | +| **Memory** | ✅ | title graphics memory shared by CPU and GPU without copies, up to 16 GB (4 GiB windows); XMem graphics requests for 2 MiB pages round to 2 MiB boundaries | GPU tests: loads, stores and atomics across windows | | **File system** | ✅ | console paths (`GAME:`, `G:`, `D:` …) → the game folder; writes go to an overlay, the game folder is never modified; the console's drives: `S:\` (persistent local storage) and `T:\` (temp) in the title's data folder; free disk space answered for console paths | end-to-end: the test title's folder stays byte-identical; PLS and free space through the real call path | | **DirectStorage** | ✅ | the console's DirectStorage queues: file and memory sources, zlib, and BCPack – the console's texture compression – for BC1, BC3, BC4, BC5, BC6H and BC7 (BC2 and the hardware swizzle are refused) | unit tests on synthetic streams; retail BCPack chunks of all six modes byte-identical with a reference decoder (`XCL_SAMPLE_BCPACK`) | diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 495991543d..9ead0e2b18 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1361,10 +1361,10 @@ unsafe fn aligned_heap_free(p: *mut c_void) { } /// Rounded byte size and alignment for an XMem graphics allocation honoring `page_size` -/// (`2` = `XALLOC_PAGESIZE_4MB`, otherwise 64 KiB sparse tile grain) and `alignment`. +/// (`2` = 2 MiB pages, otherwise 64 KiB sparse tile grain) and `alignment`. fn xmem_graphics_layout(size: usize, a: XAllocAttrs) -> Option<(usize, usize)> { let page_grain = match a.page_size { - 2 => 4 << 20, // XALLOC_PAGESIZE_4MB + 2 => 2 << 20, _ => crate::gpu::uma::GRAIN, }; let align = (1usize << a.alignment.min(usize::BITS - 2)).max(page_grain); @@ -1449,7 +1449,7 @@ pub unsafe fn adopt_title_hooks(main: &crate::modules::MappedModule) { /// asserts that they start there. pub const D3D_DESCRIPTOR_HEAP: u64 = 4 | 1 << 14 | 0x80 << 16 | 16 << 24 | 1 << 29; -/// XALLOC_ATTRIBUTES of a display target's memory: 4 MiB pages (the retail game's hook for its back buffers +/// XALLOC_ATTRIBUTES of a display target's memory: 2 MiB pages (the retail game's hook for its back buffers /// takes every request with them), allocator 0x80, graphics memory. pub const D3D_DISPLAY_TARGET: u64 = 2 << 14 | 0x80 << 16 | 16 << 24 | 1 << 29; @@ -3468,6 +3468,60 @@ mod tests { } } + #[test] + fn xmem_graphics_page_size_two_uses_two_mib_grain() { + const PAGE: usize = 2 << 20; + let attrs = XAllocAttrs::decode((2 << 14) | (1 << 29)); + for (size, rounded) in [(0, PAGE), (1, PAGE), (PAGE - 1, PAGE), (PAGE, PAGE), (PAGE + 1, 2 * PAGE), (2 * PAGE, 2 * PAGE)] { + assert_eq!(xmem_graphics_layout(size, attrs), Some((rounded, PAGE)), "size {size}"); + } + let larger_alignment = XAllocAttrs::decode((2 << 14) | (23 << 24) | (1 << 29)); + assert_eq!(xmem_graphics_layout(1, larger_alignment), Some((PAGE, 8 << 20)), "alignment does not enlarge the allocation's page grain"); + } + + #[test] + fn xmem_graphics_page_grain_preserves_other_codes_and_checks_overflow() { + const PAGE: usize = 2 << 20; + // Code 3 is reserved; keep its existing fallback rather than defining a new contract here. + for code in [0, 1, 3] { + let attrs = XAllocAttrs::decode((code << 14) | (1 << 29)); + assert_eq!(xmem_graphics_layout(0x1_0001, attrs), Some((0x2_0000, 0x1_0000))); + assert!(xmem_graphics_layout(usize::MAX, attrs).is_none()); + } + let attrs = XAllocAttrs::decode((2 << 14) | (1 << 29)); + let largest = usize::MAX & !(PAGE - 1); + assert_eq!(xmem_graphics_layout(largest, attrs), Some((largest, PAGE))); + assert!(xmem_graphics_layout(largest + 1, attrs).is_none()); + assert!(xmem_graphics_layout(usize::MAX, attrs).is_none()); + } + + #[test] + fn xmem_two_mib_layout_accounts_and_releases_owned_memory() { + const PAGE: usize = 2 << 20; + // An owned CPU-only window keeps global UMA state and GPU initialization out of the fixture. + let u = crate::gpu::uma::Uma::with_windows(1, 16 << 20).expect("owned memory window"); + let attrs = XAllocAttrs::decode((2 << 14) | (1 << 29)); + for (requested, expected) in [(1, PAGE), (PAGE, PAGE), (PAGE + 1, 2 * PAGE)] { + let (size, align) = xmem_graphics_layout(requested, attrs).unwrap(); + let p = u.alloc(size, align).expect("owned allocation"); + let (recorded, used) = (u.block_len(p as usize), u.used()); + let address = p as usize; + unsafe { + p.write(0x37); + p.add(requested - 1).write(0x69); + } + // Reclaim before assertions so the negative control also leaves no live allocation. + let released = u.free(address); + assert!(released); + assert_eq!(u.used(), 0); + assert_eq!(u.block_len(address), None); + assert!(!u.free(address), "one allocation is released only once"); + assert_eq!(address % PAGE, 0); + assert_eq!(recorded, Some(expected), "request {requested}: recorded allocation size"); + assert_eq!(used, expected, "request {requested}: memory charged"); + } + } + #[test] fn xmem_alignment_and_zeroing() { let attrs = |align_log2: u64, mtype: u64| (align_log2 << 24) | (mtype << 29); diff --git a/tests/testtitle/src/main.rs b/tests/testtitle/src/main.rs index d5ae267a44..92aaa31e17 100644 --- a/tests/testtitle/src/main.rs +++ b/tests/testtitle/src/main.rs @@ -348,12 +348,12 @@ fn main() { ensure!(c == a, "freed blocks merge and are reused: {c:?}"); ensure!(std::slice::from_raw_parts(c as *const u8, 0x30000).iter().all(|&x| x == 0), "reused block not zeroed"); XMemFree(c, attrs); - // 4 MiB page-size semantics (XALLOC_PAGESIZE_4MB = 2 << 14): 4 MiB-aligned & zeroed - let attrs_4m = (1u64 << 29) | (2u64 << 14); - let big = XMemAlloc(0x1000, attrs_4m); - ensure!(!big.is_null() && (big as usize) % (4 << 20) == 0, "4MB page alloc not 4 MiB aligned: {big:?}"); - ensure!(*(big as *const u64) == 0 && *((big as usize + (4 << 20) - 8) as *const u64) == 0, "4MB page alloc not zeroed"); - XMemFree(big, attrs_4m); + // Page-size code 2 requests 2 MiB-aligned, zeroed memory. + let attrs_2m = (1u64 << 29) | (2u64 << 14); + let big = XMemAlloc(0x1000, attrs_2m); + ensure!(!big.is_null() && (big as usize) % (2 << 20) == 0, "2MB page alloc not 2 MiB aligned: {big:?}"); + ensure!(*(big as *const u64) == 0 && *((big as usize + (2 << 20) - 8) as *const u64) == 0, "2MB page alloc not zeroed"); + XMemFree(big, attrs_2m); Ok(()) });