Repository navigation
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
Conversation
…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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
Tested a57fa30 merged with main d0835dd (merge 249f477c9, no conflicts) on native Windows 11, MSVC 14.51, full
|
Problem
The console's allocation type picks the page size with bits the PC reads differently:
0x20400000MEM_LARGE_PAGES | MEM_PHYSICAL0x20000000MEM_LARGE_PAGES(needsSeLockMemoryPrivilege)0x80000000(with or without0x20000000)MEM_4MB_PAGES, rejectedOn
main:XMemVirtualAllocfails the 2 MiB mode withERROR_NOT_SUPPORTED("needs an aligned reservation strategy that this shim does not provide") and rejects0x80000000as an unknown bit (ERROR_INVALID_PARAMETER).VirtualAllocshim (RT-217) strips onlyMEM_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), and0x20400000gets the host'sMEM_LARGE_PAGES | MEM_PHYSICALmeaning instead of 64 KiB pages.An engine that allocates its big pools with large pages gets
NULLat 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:ERROR_INVALID_PARAMETER.XMemVirtualAllocand the titleVirtualAllocshare one path (console_virtual_alloc). The bits are never forwarded; every page size is ordinary host pages, with what a title can observe kept:VirtualAlloc2withMEM_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'sreserve_aligned);MEM_TOP_DOWNis honoured in both;VirtualFree(base, 0, MEM_RELEASE)andVirtualQueryreport exactly the base and rounded size returned. NoVirtualFree/VirtualQueryshim is needed.XMemVirtualAllockeeps defaulting to reserve + commit with no operation bit; the titleVirtualAllockeeps Win32's rule that an operation is required.MEM_PHYSICALwithoutMEM_LARGE_PAGESin a title'sVirtualAllocstill goes to the host unchanged.XMemVirtualAllocattribute word still fails withERROR_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 thexmem.dllline inruntime/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
XMemAlloconly.References
Facts and method, credited in the code comments:
include/openscarlett/kernel/kernelx_abi.h(MEM_LARGE_PAGES0x20000000,MEM_4MB_PAGES0x80000000) andsrc/kernel/xmemory.cpp(4 MiB =MEM_4MB_PAGES | MEM_LARGE_PAGES), commit2241564.crates/compat/xmem/src/virt.rs(page-size decode, aligned large-page reservation and its test) andsrc/sys.rs(reserve_aligned), commitad24144.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_testwith the new cases againstmain'sxmem.cpp: FAILED, 24 failures of 104 checks (2 MiB returnsNULLwith error 50, 4 MiB with error 87, and every titleVirtualAllocwith0x20000000,0x80000000,0xa0000000or0x20400000fails; the remaining large-page checks never run because the allocation isNULL). With the change: OK, 146 checks.xmem_proberunsxmem_testagain withLEONIDA_XMEM_NO_VIRTUALALLOC2=1, so the probe fallback is covered: OK, 146 checks.xmem.cppand has a test:xmem,xmem_probe,loader,dll_thread_calls,run_tiny_game*and the rest of the loader set: 14/14 pass. The fullctestunder 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 linksxmem.cpp.python -m compileall -q make_game.py tools tests,tests/test_docs_*.py(7 files),scripts/check_no_game_data.pyon the changed files,git diff --check: pass.Not run on Windows itself: I have no Windows host with MSVC. A run of
xmem_testthere (and of the 4 MiB case on a machine where the host'sVirtualAlloc2alignment 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