Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`) |

Expand Down
60 changes: 57 additions & 3 deletions crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions tests/testtitle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
});

Expand Down
Loading