Skip to content

Runtime: serve the console's 2 MiB and 4 MiB pages in XMemVirtualAlloc and a title's VirtualAlloc, without the host's large-page privilege - #4531

Open
labj1987 wants to merge 1 commit into
ItsNotRexed:mainfrom
labj1987:xmem-large-pages
Open

labj1987 wants to merge 1 commit into
ItsNotRexed:mainfrom
labj1987:xmem-large-pages

Conversation

@labj1987

Copy link
Copy Markdown
Contributor

Problem

The console's allocation type picks the page size with bits the PC reads differently:

Bits Console Host
0x20400000 64 KiB pages MEM_LARGE_PAGES | MEM_PHYSICAL
0x20000000 2 MiB pages MEM_LARGE_PAGES (needs SeLockMemoryPrivilege)
0x80000000 (with or without 0x20000000) 4 MiB pages MEM_4MB_PAGES, rejected

On main:

  • XMemVirtualAlloc fails the 2 MiB mode with ERROR_NOT_SUPPORTED ("needs an aligned reservation strategy that this shim does not provide") and rejects 0x80000000 as an unknown bit (ERROR_INVALID_PARAMETER).
  • The title VirtualAlloc shim (RT-217) strips only MEM_GRAPHICS/MEM_TITLE, so a page-size request goes to the host as the host's flags: 2 MiB and 4 MiB requests fail (no player account holds the lock-memory privilege), and 0x20400000 gets the host's MEM_LARGE_PAGES | MEM_PHYSICAL meaning instead of 64 KiB pages.

An engine that allocates its big pools with large pages gets NULL at boot. Other console layers hit this with real titles before us; xbox-emulator and OpenScarlett both serve these page sizes.

Fix

runtime/shims/xmem.cpp:

  • One decoder for the page-size bits: 0 (host pages), 64 KiB, 2 MiB, 4 MiB; any other combination is ERROR_INVALID_PARAMETER.
  • XMemVirtualAlloc and the title VirtualAlloc share one path (console_virtual_alloc). The bits are never forwarded; every page size is ordinary host pages, with what a title can observe kept:
    • a new region's base is a multiple of the page size: VirtualAlloc2 with MEM_ADDRESS_REQUIREMENTS.Alignment (looked up at run time), falling back where it is missing or refuses the parameter (ERROR_INVALID_PARAMETER) to a reserve-size + alignment / release / reserve-aligned probe that retries when another thread takes the range (the method of xbox-emulator's reserve_aligned); MEM_TOP_DOWN is honoured in both;
    • sizes round up to whole pages, and an address inside an existing reservation rounds out to the pages it touches, so a commit inside a 2 MiB reservation commits that whole page (the existing 64 KiB rule, extended);
    • each new region is a single host allocation, so VirtualFree(base, 0, MEM_RELEASE) and VirtualQuery report exactly the base and rounded size returned. No VirtualFree/VirtualQuery shim is needed.
  • XMemVirtualAlloc keeps defaulting to reserve + commit with no operation bit; the title VirtualAlloc keeps Win32's rule that an operation is required. MEM_PHYSICAL without MEM_LARGE_PAGES in a title's VirtualAlloc still goes to the host unchanged.
  • Unchanged: a nonzero XMemVirtualAlloc attribute word still fails with ERROR_NOT_SUPPORTED (that word is RT-202's, Runtime: xmem honours the attribute word's alignment and reports the memory kind (RT-202) #3601).

Docs: a "Console page sizes" section in docs/runtime_imports.md, and the xmem.dll line in runtime/README.md.

Not covered: the page size comes from each call's bits and is not remembered per region, so a commit without the bits inside a 2 MiB reservation commits host pages only (documented); the host still pages 4 KiB at a time underneath (only TLB timing could tell), and no shim serves the physical-page calls (XMemAllocatePhysicalPages, XMemMapPhysicalPages*), which take the same bits; nothing in the package imports them.

Overlap

#3390 (RT-217) and #3601 (RT-202) edit the same file. Both leave the 2 MiB mode unsupported, so this does not duplicate either; whichever lands second needs a small rebase. #2401 (attribute-word trace) touches XMemAlloc only.

References

Facts and method, credited in the code comments:

  • OpenScarlett (MIT), include/openscarlett/kernel/kernelx_abi.h (MEM_LARGE_PAGES 0x20000000, MEM_4MB_PAGES 0x80000000) and src/kernel/xmemory.cpp (4 MiB = MEM_4MB_PAGES | MEM_LARGE_PAGES), commit 2241564.
  • omonkeyz/xbox-emulator (GPL-3.0-or-later), crates/compat/xmem/src/virt.rs (page-size decode, aligned large-page reservation and its test) and src/sys.rs (reserve_aligned), commit ad24144.

No code is copied; the retry probe follows xbox-emulator's method (GPL-3.0-or-later, compatible with this repository's GPL-3.0).

Tested

Linux, Wine 10.0, clang-cl 21 + lld-link with the xwin Windows SDK 10.0.26100 (runtime/cmake/clang-cl-xwin.cmake), RelWithDebInfo. CPU-only; no GPU involved.

  • xmem_test with the new cases against main's xmem.cpp: FAILED, 24 failures of 104 checks (2 MiB returns NULL with error 50, 4 MiB with error 87, and every title VirtualAlloc with 0x20000000, 0x80000000, 0xa0000000 or 0x20400000 fails; the remaining large-page checks never run because the allocation is NULL). With the change: OK, 146 checks.
  • New ctest xmem_probe runs xmem_test again with LEONIDA_XMEM_NO_VIRTUALALLOC2=1, so the probe fallback is covered: OK, 146 checks.
  • Every target that compiles xmem.cpp and has a test: xmem, xmem_probe, loader, dll_thread_calls, run_tiny_game* and the rest of the loader set: 14/14 pass. The full ctest under Wine passed apart from five Vulkan/D3D12 tests (vk_fence_dispatcher_sync, vk_presenter_sync, d3d12_fence_timing, allocator_proxy, d3d12_private_data) and two that did not finish (pipeline_cache_file, xbox_present); none of them links xmem.cpp.
  • python -m compileall -q make_game.py tools tests, tests/test_docs_*.py (7 files), scripts/check_no_game_data.py on the changed files, git diff --check: pass.

Not run on Windows itself: I have no Windows host with MSVC. A run of xmem_test there (and of the 4 MiB case on a machine where the host's VirtualAlloc2 alignment path is used) would be welcome.

No game data: the tests use synthetic sizes and flag values only.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…c and a title's VirtualAlloc

The console's allocation type picks the page size with bits the host reads
differently: 0x20000000 (2 MiB) is the host's MEM_LARGE_PAGES, which needs
SeLockMemoryPrivilege, 0x80000000 (4 MiB) is rejected, and 0x20400000 (64 KiB)
becomes MEM_LARGE_PAGES | MEM_PHYSICAL. XMemVirtualAlloc failed the 2 MiB mode
and rejected 4 MiB, and the title VirtualAlloc shim forwarded all three.

Both now share one path: the bits are decoded (other combinations stay
ERROR_INVALID_PARAMETER), never forwarded, and every page size is served from
ordinary host pages with the base aligned to the page size (VirtualAlloc2 with
an alignment requirement, else a reserve/release/reserve probe with retry),
sizes and addresses rounded to whole pages, and one host allocation per
region so VirtualFree and VirtualQuery match what was returned. A title's
VirtualAlloc still needs an operation; nonzero XMem attribute words still
fail with ERROR_NOT_SUPPORTED.

Page-size values as read in OpenScarlett (MIT, kernelx_abi.h, xmemory.cpp) and
omonkeyz/xbox-emulator (GPL-3.0-or-later, crates/compat/xmem/src/virt.rs); the
probe fallback follows xbox-emulator's reserve_aligned (sys.rs).

xmem_test gains 2 MiB / 4 MiB / 64 KiB cases for both entry points (24 of 104
checks fail on main; 146/146 pass here), and xmem_probe reruns it with
LEONIDA_XMEM_NO_VIRTUALALLOC2 set to cover the fallback.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Kam471 Kam471 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.

Built and ran xmem_test at a57fa304 on Windows 11: 146 checks, 0 failures, exit 0.

The console's 2 MiB and 4 MiB page requests work through XMemVirtualAlloc and a title's VirtualAlloc without the host large-page privilege, including the 64 KiB one-page case and the expected Win32 failure. No blocking issue.

@Flyander Flyander 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.

The shim keeps console page-size bits away from Win32, validates unsupported combinations before allocation, and rounds the requested region with overflow checks. New reservations use VirtualAlloc2 alignment when available and a bounded release-and-retry fallback otherwise; existing-address commits are expanded to the requested guest page granularity. The tests cover both allocator entry points and the fallback. I found no correctness issue in the diff.

@kapitche

Copy link
Copy Markdown
Contributor

Tested a57fa30 merged with main d0835dd (merge 249f477c9, no conflicts) on native Windows 11, MSVC 14.51, full runtime/ build: pass.

  • ctest -R xmem: xmem and the new xmem_probe both pass (104 checks). tests/test_docs_*.py (7 files): OK on Linux Python 3.12.16.
  • Negative control 1: runtime/shims/xmem.cpp from main with the PR's tests: both tests FAILED (104 checks, 24 failures).
  • Negative control 2 (does xmem_probe really exercise the fallback?): in the PR's reserve_aligned fallback I moved the aligned base by 0x1000: xmem still passes and xmem_probe FAILS, so the probe path is covered only by the new test.
  • Read the diff: a title VirtualAlloc without MEM_RESERVE/MEM_COMMIT and a page-size bit, and the combinations 0x00400000 alone / MEM_4MB_PAGES|MEM_PHYSICAL, fail with ERROR_INVALID_PARAMETER as docs/runtime_imports.md says. Not run: a title actually using these flags (no game copy), so only the shim contract is checked.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants