Skip to content

kernelx: report the physical memory layout (XMemGetPhysicalMemoryLayout) - #3352

Open
illu0x wants to merge 1 commit into
xcl-dev:mainfrom
illu0x:kernelx-physical-memory-layout
Open

illu0x wants to merge 1 commit into
xcl-dev:mainfrom
illu0x:kernelx-physical-memory-layout

Conversation

@illu0x

@illu0x illu0x commented Nov 29, 2026

Copy link
Copy Markdown

Export XMemGetPhysicalMemoryLayout, which was auto-stubbed. A title that queries the split between standard- and GPU-optimal-bandwidth physical memory (XMEM_PHYSICAL_MEMORY_LAYOUT) now gets an answer instead of a stub. XCL has one memory pool with no bandwidth split on the PC, so both page numbers are 0 — all title memory counts as GPU-optimal from page 0, matching the single fast-memory pool XMemGetWorkingSetStatistics already reports. A null layout returns FALSE with ERROR_INVALID_PARAMETER.

Part of #1833.

What is in it

  • kernelx: report the physical memory layout (XMemGetPhysicalMemoryLayout) — new export, implementation and unit test, in crates/xcl/src/hle/kernelx.rs

Tested

  • Windows 11 x64, Rust stable-x86_64-pc-windows-msvc, debug; GPU AMD Radeon RX 6600
  • cargo test --workspace -- --test-threads=4: 2469 passed, 1 failed, 29 ignored, 0 SKIPPED
  • The new test hle::kernelx::tests::physical_memory_layout_writes_both_page_numbers passes.
  • The one failure, gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1, is a pre-existing RDNA2-only wave32 WaveReadLaneAt GPU/CPU mismatch (the documented amd_reduced_seeds family, HANDOFF.md). It fails identically on main with this change stashed, and is unrelated to kernelx.

Regression run
Both runs on one retail title (title id b41e6560), AMD RX 6600, 60 s measured, XCL_PREPARE=0 (pipelines built on demand; identical flags for both, so the comparison is fair):

xcl-smoke v1
commit: fa204595a488c60c6c5f905244f0172072c9c2a9
gpu: AMD Radeon RX 6600
title: b41e6560
seconds: 60
exit: 0x0
frames shown: 1501 (PresentX 1501)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 17206 MB
xcl-smoke v1
commit: e5776a9a7e0e16666891d7e526e9ccd970f6262a
gpu: AMD Radeon RX 6600
title: b41e6560
seconds: 60
exit: 0x0
frames shown: 1438 (PresentX 1438)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 18011 MB

Head vs main: 1438 / 1501 frames = 96%, peak commit 18011 / 17206 MB = 105%, no device removal, no crash. The title does not call XMemGetPhysicalMemoryLayout, so this is a no-regression check on the surrounding runtime rather than a test of the new path.

Not tested

  • NVIDIA, other titles, wave32-only GPUs
  • The new export against a title that actually calls it (none observed yet; this is header-contract completeness)

AI use

  • Claude Code with Claude Opus 4.8: wrote the code, the test, ran the tests and the regression runs, and wrote this description. The author has read and understands the diff.

🤖 Generated with Claude Code

XMemGetPhysicalMemoryLayout was auto-stubbed, so a title that queries the
split between standard- and GPU-optimal-bandwidth physical memory got no
answer. Export it and fill XMEM_PHYSICAL_MEMORY_LAYOUT: both page numbers
are 0 -- one pool with no bandwidth split on the PC, matching the single
fast-memory pool XMemGetWorkingSetStatistics already reports. Returns FALSE
with ERROR_INVALID_PARAMETER on a null layout.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions github-actions Bot added the area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH label Nov 29, 2026
@illu0x
illu0x marked this pull request as ready for review November 29, 2026 10:43
@Foniass

Foniass commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: 7d86c5d
head: e5776a9
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2897 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (+30). XMemGetPhysicalMemoryLayout is added as an export; main has no export of that name, so today it falls through to an auto-stub. Findings: (1) LowestPageNumber is written as 0 (kernelx.rs ~1654), but XCL never hands out page 0. Page numbers count 4 KiB frames and the first title page is page_number(0) = 16 (kernelx.rs ~1919-1926 on main), while 0 means "no page" and is what the scatter-map path treats as unmap. A title that offsets from the lowest page would be off by 16 frames. page_number(0) (FRAMES) is the consistent value. Writing 0 for the GPU-optimal boundary is harmless: every page counts as fast. (2) Nits: the test comment at ~3259 says "0xCC..-filled", but the buffer holds usize::MAX. The doc comment says it matches the "single fast-memory pool" XMemGetWorkingSetStatistics reports, but that function reports a limit and usage, not pools. Fine: the export is registered in hle_exports! like the others, a null pointer gets ERROR_INVALID_PARAMETER and FALSE as in the neighbouring XMem* calls, and the test checks the write, a guard word and null. The PR's red CI is the pipeline-library flake that #3221 (2c003dd) fixed on main. It replaces #3316 (closed). No smoke run here: the retail title doesn't call it.
performance: no smoke run (the default path doesn't reach this change); not measured.
verdict: not yet: LowestPageNumber should be page_number(0) (16), the first page XCL hands out, not 0, which XCL uses for "no page" (finding 1)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head e5776a9

No open or merged PR adds XMemGetPhysicalMemoryLayout. #3316 carried the same commit and was closed by the same author, so this PR replaces it. The export writes both page numbers as 0 and fails a null pointer with ERROR_INVALID_PARAMETER, and the test checks that nothing is written past the two fields. Minor: the test comment says "0xCC..-filled", but the buffer is filled with usize::MAX. No other problems found in the diff.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@bmjask bmjask left a comment

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.

Review of e5776a9a: 3 finding(s). 3 are inline.


These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

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.

return 0;
}
(layout as *mut usize).write_unaligned(0); // LowestPageNumber
(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.

/// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants