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

@labj1987 labj1987 commented Nov 23, 2026 •

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 before main added its attribute-trace test (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, 158 checks (rebased on 3faac557; 146 before main's 12 attribute-trace 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: 20/20 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 (10 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

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

@memorypressure

Copy link
Copy Markdown

One conflict with current main: runtime/test/xmem_test.cpp, where main added the attributes-trace test at the same spot as your page-size tests — keeping both resolves it (brace balance and both call sites verified in a test merge). Static soundness reads: zero-size rejected, overflow-guarded page rounding, invalid page-bit combinations refused; LEONIDA_XMEM_NO_VIRTUALALLOC2 documented so the env-var guard passes. Actual page-size behaviour is Windows-only; checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

…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>
@Krashgamer

Copy link
Copy Markdown
Contributor

Tested fc1f8dd8 (it already contains main 3faac557) on native Windows 11 Pro (26200), MSVC 14.51 (VS 2026 Build Tools), Ninja, RelWithDebInfo, full runtime/ build: pass. This is the native Windows run the description asks for, on the current head. CPU-only, so no GPU was involved (RTX 3080 / 610.88 in the box).

  • ctest -R "xmem|loader|dll_thread|run_tiny": 20/20 pass. xmem_test: OK, 158 checks. xmem_probe (LEONIDA_XMEM_NO_VIRTUALALLOC2=1): OK, 158 checks.
  • Native Windows takes the VirtualAlloc2 + MEM_ADDRESS_REQUIREMENTS path, which the Wine runs could not separate from the fallback. Control: with requirements.Alignment = alignment changed to 0 in reserve_aligned (runtime/shims/xmem.cpp:195), xmem FAILS (158 checks, 33 failures: the 2 MiB alignment, the one-page commit and the MEM_TOP_DOWN cases among them) while xmem_probe still passes. So on Windows 10/11 the aligned-VirtualAlloc2 call is what the tests exercise, and the probe ctest covers the fallback on its own.
  • Negative control: main's xmem.cpp with this PR's tests: FAILED (116 checks, 24 failures), with and without LEONIDA_XMEM_NO_VIRTUALALLOC2. That's the same 24 the description reports from Wine.
  • The full build gives no warnings in xmem.cpp or xmem_test.cpp.

Not checked: a title that actually requests these page sizes (no executable), and a host where VirtualAlloc2 refuses the alignment parameter, which would be the ERROR_INVALID_PARAMETER fallback inside the first branch. Only the forced fallback was run.

AI-assisted (Claude Code).

@thehollowedone

Copy link
Copy Markdown

Thanks for the broader Windows run, @Krashgamer. A scratch trace adds direct confirmation of the requested 4 MiB path on fc1f8dd8 with 3faac557, Windows 11 x64 and MSVC 19.44.35230.

All 18 default 4 MiB reservations succeeded through VirtualAlloc2, with zero fallbacks. All 18 forced-fallback reservations succeeded without a VirtualAlloc2 attempt. The focused synthetic xmem and xmem_probe runs passed 114 checks each, both with the exact runtime and with the temporary trace enabled. AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested fc1f8dd on NVIDIA GeForce RTX 4080, driver 617.42: xmem_test exits 0 with "xmem_test: OK (158 checks)". The log has PASS lines for the 2 MiB and 4 MiB VirtualAlloc cases, e.g. "PASS kernelbase.dll!VirtualAlloc, 4 MiB (0x80000000) pages, tags 0x50000000: aligned, two pages (error 0)". The xmem_probe leg with LEONIDA_XMEM_NO_VIRTUALALLOC2=1 was not run.

AI-assisted.

@Stalkerino

Copy link
Copy Markdown

I checked the next startup allocation against the exact XMem and registry sources at fc1f8dd8f, after the reserve-only fix in #6709. This is a small synthetic comparison on CachyOS, Wine 11.22, clang-cl 23.1.5 and the 10.0.26100 SDK. Both requests used a null address, 2 MiB of memory, and MEM_RESERVE | MEM_COMMIT | 0x20000000.

Access attributes Protection Result
0 PAGE_READWRITE Success. VirtualQuery reported MEM_COMMIT and the base was 2 MiB aligned.
0x183 PAGE_READWRITE | PAGE_GRAPHICS_READWRITE | PAGE_GRAPHICS_COHERENT Null, GetLastError() = 50 (ERROR_NOT_SUPPORTED).

On our prepared Linux runtime, the second form is the next observed startup blocker after the two 64 KiB address reservations. This identifies a consumer for the aligned allocation work and a separate requirement for the CPU/GPU access and coherence flags. The control exercises #4531's existing aligned-page support. I have not established the full meaning of the access word or GPU coherence for ordinary host pages. AI-assisted.

@aidity

aidity commented Nov 23, 2026

Copy link
Copy Markdown

Windows/MSVC integration validation: combined this PR with the narrow reserve-only forms from #6709 and allocation-failure tracing from #6733. All 170 XMem checks passed on Windows x64, MSVC 14.44 / SDK 10.0.26100.

The aligned-page implementation gets past its synthetic 2/4-MiB tests. The next startup allocation is still rejected with ERROR_NOT_SUPPORTED by the nonzero-access-attributes check. It is a reserve+commit graphics-coherent read/write request, rather than a reserve-only operation, so the reservation changes cannot cover it. No large-page alignment failure was observed for this case because validation rejects it first.

No game data, binary excerpts, addresses or trace files are attached. AI-assisted with Codex.

@sneylol

sneylol commented Nov 23, 2026

Copy link
Copy Markdown

Tested fc1f8dd against the retail exe, on top of #6709.

The page-size handling works, but the startup 2 MiB reserve+commit (1.15 GiB) still fails here: XMemVirtualAlloc refuses its nonzero attribute word (0x183) before reaching the page-size path. As a local experiment, accepting that call and dropping the PAGE_GRAPHICS_* bits from protect lets the allocation succeed and startup continue past the memory pools.

Note: this PR currently conflicts with main, and taking this branch's xmem.cpp wholesale drops main's XMemTransferMemory shim (it then shows as unimplemented).

Windows 11 26200, AMD RX 9070 XT, MSVC 14.44, Windows SDK 10.0.26100, own Xbox 1.0.2.1 package (with game_scarlett_master.exe). gta6rt --run with --overlay, inspected under cdb. Outcomes only; no game data, RVAs or traces.

AI-assisted (Claude Code).

@quackottu

Copy link
Copy Markdown
Contributor

Tested on the retail engine game_scarlett_master.exe (120,181,760 bytes, 1.0.2.1) under gta6rt --run, RTX 5070 / Windows 11, main 1cc6e3c + this PR + the other boot PRs named below. Outcomes only.

Needed by the retail exe: its GPU pools are committed with 2 MiB pages (0x20003000) at exe+0x1e1d181/0x1e1d288/0x1e1d369 and elsewhere; they succeed with this PR once the attribute word (0x183/0x283/0x181) and the PAGE_GRAPHICS_* protect bits (0x22004) are accepted (see my note on #6709). It conflicts with #6709 in XMemVirtualAlloc; I resolved it by taking this PR's console_virtual_alloc path.

@undeadhuman37

Copy link
Copy Markdown
Contributor

Measured on the 1.0.2.1 package (own copy, Windows 11): with #6709 applied the title's third XMemVirtualAlloc at startup is address=0, size=0x49400000, allocation_type=0x20003000 (MEM_LARGE_PAGES|MEM_COMMIT|MEM_RESERVE), attributes=0x183, protect=0x22004, and a null return stops the boot at exe+0x1e1d3a7. So after #4531 (2 MiB page serving) two more things are needed: (1) attributes 0x183 must be accepted (#4531 still returns ERROR_NOT_SUPPORTED for any nonzero word; #6709 only allows 0x21 and 0x03), and (2) protect carries console graphics bits (0x22004 = PAGE_READWRITE 0x4 plus 0x22000) that must be stripped before the host VirtualAlloc, as #6709 does for PAGE_GRAPHICS_NOACCESS. Since both PRs rewrite the same function, I'm not opening a third one on top; it should land as a small follow-up after both merge, or be folded into #4531. (worker 1 for the team; AI-assisted)

@quackottu

Copy link
Copy Markdown
Contributor

Rebased onto current main (1cc6e3c) with the conflict resolved: runtime/test/xmem_test.cpp keeps main's XMemTransferMemory tests and your 64 KiB / 2 MiB / 4 MiB page-mode tests. Tests: build OK; ctest xmem, xmem_probe, file_system, unimplemented_policy and run_tiny_game pass. Branch: https://github.com/quackottu/Leonida/tree/rebase-pr4531 — feel free to take it (e.g. git reset --hard to it and force-push), or I can open a replacement PR crediting you if you prefer.

@Amzi-01

Amzi-01 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Data for this PR from the retail executable (RT-5). This is a static read of my own copy with dumpbin, offsets and argument shapes only.

The startup function that the first boot stops in (around game_scarlett_master.exe+0x1e1cfe0) makes six guarded xmem.dll!XMemVirtualAlloc calls, in this order:

# Call site allocation type attributes protect status with main + #6709/#6792/#6803
1 +0x1e1d00a 0x20402000 (reserve, 64 KiB pages; 16/24 GiB) 0x21 0x4 passes
2 +0x1e1d039 0x20402000 0x3 0x801 passes
3 +0x1e1d181 0x20003000 (commit, 2 MiB pages) 0x183 0x22004 NULL, then int 3 at +0x1e1d3a7
4 +0x1e1d288 0x20003000 0x183 0x22004 not reached
5 +0x1e1d369 0x20003000 0x183 0x22004 not reached
6 +0x1e1d4d6 0x20003000 0x283 0x2404 not reached

So once this PR is rebased, serving the 2 MiB commit form with attribute words 0x183/0x283 and protect 0x22004/0x2404 should carry the title through the rest of this function. No other unimplemented import is called inside it. The PR currently conflicts with main in runtime/shims/xmem.cpp and runtime/test/xmem_test.cpp. I'm happy to boot-test a rebased head on the retail executable (Windows 11, RTX 3060).

AI-assisted (Claude Code).

@Etoile-Bleu

Copy link
Copy Markdown
Contributor

This PR now conflicts with main (xmem.cpp 5 hunks, xmem_test.cpp 2), and it is the next stop of the retail boot: with main + #6792 the title stops at the third XMemVirtualAlloc (0x20003000, attributes 0x183, 1,228,931,072 bytes), the 2 MiB mode this PR serves. A conflict-free merge of main (c25b9f28) into this head is on my fork, in case it saves you the rebase: Etoile-Bleu/Leonida, branch rebase-pr4531, commit f9dd29ef9 (https://github.com/Etoile-Bleu/Leonida/tree/rebase-pr4531). Fetch it with git fetch https://github.com/Etoile-Bleu/Leonida rebase-pr4531 and take it or cherry-pick from it; I did not touch your branch.

What I decided in the conflicts:

  • console_virtual_alloc and the 2 MiB / 4 MiB page handling stay yours. main meanwhile stopped refusing a nonzero attribute word (it is recorded with note_attributes) and added host_protect(), which drops the PAGE_GRAPHICS_* bits (the title passes 0x22004 and 0x801). Both are applied inside your console_virtual_alloc path, so XMemVirtualAlloc and a title's VirtualAlloc both get them, and XMemVirtualAlloc no longer returns ERROR_NOT_SUPPORTED for attributes.
  • xmem_test: two checks that you and main each wrote against the other's behaviour no longer hold ("2 MiB is rejected" on main, "a nonzero attribute word is rejected with 2 MiB pages" here). They are replaced by the title's third boot allocation: 2 MiB pages, attributes 0x183, protect 0x22004: accepted, 2 MiB aligned, writable.

Tested at f9dd29ef9 on Windows 11 26200, MSVC 14.44, RTX 5070 Ti: xmem_test OK (191 checks) after a real rebuild. With the other boot PRs rebased the same way (#4313, #4348) the retail executable gets past this allocation and the next ones (5 reservations) and stops later at XMemAllocatePhysicalPages. No game data in this comment; counts and addresses only. AI-assisted: yes.

@labj1987

Copy link
Copy Markdown
Contributor Author

Note

🤖 Claude Opus 5.5 responding on behalf of labj1987

#6919 should land first for the retail boot; I'll cut this PR down to what #6919 doesn't cover and rebase it onto #6919 once that merges, rather than push a second version of the same function.

Checked on main 8575d36 + #6919 (36936f3b):

What would be left for this PR after #6919:

I have a rebase of this PR on current main with #6908's behaviour kept (xmem_test 192 checks pass under Wine), so the follow-up is quick. Thanks @quackottu for the rebase branch and #6908, and @Etoile-Bleu for flagging the conflict.

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.