Skip to content

kernelx: XMemVirtualAlloc reads its 4th argument as a flags word - #3173

Open
KOLESNiii wants to merge 1 commit into
xcl-dev:mainfrom
KOLESNiii:kernelx-xmem-virtualalloc-flags
Open

KOLESNiii wants to merge 1 commit into
xcl-dev:mainfrom
KOLESNiii:kernelx-xmem-virtualalloc-flags

Conversation

@KOLESNiii

@KOLESNiii KOLESNiii commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Status: a guess that needs evidence before it can be merged. Reading XMemVirtualAlloc's 4th argument as a flags word is reasoned from how titles call the API and from the PC's D3D12; it is not taken from any console documentation, and no title has confirmed it yet. Draft until then.

Evidence that would confirm it: XCL_TRACE_MEM=1 logs every call's decoded flags with its result; titles' words and what they then do with the memory settle the meaning of each bit. No bit is acted on yet (see below).

If you run a title that gets there, please post those lines here (not the whole log, which names your paths).

XMemVirtualAlloc's 4th argument looks like a flags word, not an allocator's XALLOC_ATTRIBUTES. This PR decodes it as flags, for a trace and for a log of the address limits it can ask for. It changes no allocation: main's routing is kept as it is (the XAllocAttrs decode: MEM_GRAPHICS from a graphics memory type in bits 29–32, 0x8000_0000 for a page-size field of 2; the test title's check that a graphics memory type lands in UMA depends on it), and type and protect otherwise go to VirtualAlloc as given. Placing memory below an address limit is left to a second PR.

Why the flags are not acted on: an earlier revision (1664d4bb) added MEM_GRAPHICS on bit 0x2. The retail game sets it on its big committed large-page heaps (about 4.6 GB), which then went to the sparse VRAM pool instead of the coherent window: 640, 705 and 707 frames against main's 1297, 1259 and 1316, at about +9.5 GB peak commit (see the comments). A later revision (c5aff7ad) removed main's attribute routing instead, which broke the test title's UMA check. Whether a console title's graphics bit should make memory GPU-visible needs a design for where such heaps go; it is not done here.

What the retail game asks for: in the regression run below it makes one address-limited request, flags 0x7 (CPU, graphics, below 4 GiB) for 0xb900000 bytes, and gets 0x44c610000, above the limit (mem: XMemVirtualAlloc asks for an address below 0x100000000 (flags 0x7): 0x44c610000 + 0xb900000 is above it: not placed yet). It runs regardless; whether it depends on that memory being low is not known.

What is in it

  • crates/xcl/src/hle/kernelx.rs VirtualAllocFlags:
    • CPU 0x1, graphics 0x2, below 4 GiB 0x4, tools 0x10, mappable 0x20;
    • bandwidth (bits 7–9), address bits (bits 12–14: the address fits in 47 − n bits), tag (bits 40–63);
    • limit() gives the first address the memory must stay below.
  • trace_virtual_alloc_flags: logs every call, decoded, under XCL_TRACE_MEM; logs one line per run, unconditionally, for the first request with an address limit, with whether the returned address fits.
  • XMemVirtualAlloc: the allocation as on main, then the trace.
  • README.md: XCL_TRACE_MEM also logs each XMemVirtualAlloc call's decoded flags.

Tested

  • NVIDIA RTX 2070 SUPER (driver 32.0.16.2140), AMD Ryzen 5 3600XT, Windows 11 build 26300, Rust 1.100.1 MSVC; tests in debug
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 599bf81a (built on main 10d27297; it merges into the current main 7db2f391 without conflict): 2155 passed, 0 failed, 48 ignored, 25 SKIPPED lines (21 no wave64, 3 gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426, 1 no heap windows). The e2e test title tests pass, including its graphics-memory-in-UMA check.
  • New tests in kernelx::tests, ok in that run:
    • virtual_alloc_flags_decode;
    • virtual_alloc_graphics_flag_keeps_the_type: a committed heap with word 0x283 is not in the GPU-visible pool (fails on 1664d4bb);
    • virtual_alloc_preserves_legacy_graphics_routing: a word with memory type bit 29 lands in UMA, as on main (the test title's check of the same thing failed on c5aff7ad).

Regression run

Both 60-second runs started from copies of the same DXIL cache, with XCL_PREPARE=0 and XCL_PRESS_AT=25:A.

xcl-smoke v1
commit: 7db2f391cefd748d47c7de63d50dd43ebb852185
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1553 (PresentX 1553)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 17206 MB
xcl-smoke v1
commit: 599bf81a81d0ef98c2cfea973bf319da95e64150
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1302 (PresentX 1302)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14905 MB

The main run is on a newer main than the PR's base (10d27297).

Not tested

  • A title that needs its address limit honoured: the retail game's one request is not placed below 4 GiB (this head allocates it as main does).
  • Full preparation. The smoke runs used XCL_PREPARE=0.
  • AMD and Intel GPUs; wave64 hardware (the RTX 2070 SUPER is wave32).

AI use

  • Claude Code, Claude Opus 5.5: wrote the code, the tests and this description; ran the regression runs. A second Claude Code session (Claude Opus 5.5) traced the 1664d4bb regression to the graphics bit.
  • Codex (exact model name unavailable): restored main's routing in 599bf81a, added virtual_alloc_preserves_legacy_graphics_routing, and ran the suite on this head.

🤖 Generated with Claude Code

@KOLESNiii

KOLESNiii commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Regression run on this head fails the merge gate (RTX 2070 SUPER, the retail game, 60 s, XCL_PRESS_AT=25:A, XCL_PREPARE=0, release builds, same starting DXIL cache):

frames shown peak commit device removals / crashes
main 309c3ec5 1297 14701 MB 0 / 0
this PR 1664d4bb 640 (49 %) 24873 MB (169 %) 0 / 0

The gate is at least 80 % of main's frames and at most 125 % of its peak commit. One run only, but the size of both changes makes noise unlikely.

Likely cause, from the diff (not yet confirmed by a trace): main makes an XMemVirtualAlloc GPU-visible (MEM_GRAPHICS) when the 4th argument decodes as an allocator attribute with a graphics memory type, and adds 0x8000_0000 for its 2 MiB page size. This head makes it GPU-visible only on bit 0x2 of a flags word and drops the large-page bit. So the retail game's allocations land in a different pool with different pages. The run logged one address-limited request (flags 0x7, 0xb900000 bytes at 0x4f2e00000: above 4 GiB, not placed).

I have stopped work on this PR until the author decides how to go on.

Rerun (same settings, interleaved main / this head / main / this head in one queue job; CPU load 2–3 % and no build processes at the start and end of every run, so not background load):

frames shown presents per 2 s (last 3) peak commit
main 309c3ec5 (a) 1259 60 60 60 14852 MB
this PR 1664d4bb (a) 705 52 51 51 24368 MB
main 309c3ec5 (b) 1316 60 60 60 14862 MB
this PR 1664d4bb (b) 707 50 52 53 24241 MB

The regression reproduces: about 55 % of main's frames, below 60 fps even at the end, and about +9.5 GB peak commit on both runs.

— Claude Code (Claude Opus 5.5), on behalf of the PR author

@KOLESNiii

Copy link
Copy Markdown
Contributor Author

Regression confirmed: on two more interleaved runs on an idle machine this head showed about 55 % of main's frames (705 and 707 against 1259 and 1316) and about +9.5 GB peak commit (numbers in the comment above). Not merge-ready as it stands.

The author is now investigating exactly why it is slower: which of the title's XMemVirtualAlloc calls decode differently under the flags-word reading than under main's allocator-attribute reading, and whether the cost comes from the pool they land in (GPU-visible or not), from losing the large-page bit, or from the 4 GiB-limited request placed above its limit. The findings and a fix will be posted here; the PR stays a draft until then.

— Claude Code (Claude Opus 5.5), on behalf of the PR author

@KOLESNiii
KOLESNiii force-pushed the kernelx-xmem-virtualalloc-flags branch from 1664d4b to c5aff7a Compare November 28, 2026 17:38
@KOLESNiii

Copy link
Copy Markdown
Contributor Author

Cause found and fixed in c5aff7ad. Under main's decode every word the retail game passes has memory type 0, so on main XMemVirtualAlloc is a plain VirtualAlloc for it. The earlier head added MEM_GRAPHICS on the flags' bit 0x2, which the game sets on its big committed large-page heaps (about 4.6 GB). They went to the sparse VRAM pool instead of the coherent window; video memory went over budget (10.8 GB against 6.8), and a 6 GB physical-page allocation found no room in unified memory and went to plain write-watched memory, where each look scanned 5.8 GB instead of 1.7. The 4 GiB-limited request and the large-page bit were ruled out.

c5aff7ad decodes the flags for the trace and the address-limit log only and passes type / protect to VirtualAlloc as given. A new test checks a committed heap with word 0x283 stays out of the GPU-visible pool; it fails on 1664d4bb. The suite and a new regression pair against main are queued; the results will go into the description.

— Claude Code (Claude Opus 5.5), on behalf of the PR author

Decode the flags word for XCL_TRACE_MEM and the address-limit diagnostic.
Keep the existing XAllocAttrs-based allocation type so the test title's
legacy graphics requests still land in UMA. Interpreting bit 0x2 as
MEM_GRAPHICS sent retail large heaps to sparse VRAM and regressed smoke.
The focused tests cover flag decoding and both allocation cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Codex (exact model unavailable)
@thehollowedone

Copy link
Copy Markdown
Contributor

Additional call-site evidence supports keeping these arguments separate: Microsoft’s Xbox GDK FSR3 sample builds the third XMemVirtualAlloc argument from MEM_RESERVE | pageSizeFlag (using MEM_2MB_PAGES or MEM_64K_PAGES) and passes XMEM_GRAPHICS | XMEM_MAPPABLE separately as the fourth argument.

This sample supports treating the page-size/type and flags arguments as distinct contracts. It does not establish the numeric XALLOC_ATTRIBUTES.PageSize mapping for XMemAllocDefault, so the evidence gap on #3092 remains.

AI-assisted.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 599bf81

#3353 is stacked on this PR (it carries this commit 599bf81a plus its own address-limit counter), so it is not a duplicate.
The title says the 4th argument is read as a flags word, but XMemVirtualAlloc still routes on XAllocAttrs::decode(flags); VirtualAllocFlags is only used for the trace, as the description says. virtual_alloc_graphics_flag_keeps_the_type also passes on main (routing unchanged), so it is a regression guard rather than a test of a fix. The comment on trace_virtual_alloc_flags refers to a "PR 2" that does not exist yet.
Recommendation: keep open.

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

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

Labels

area:docs-ci Documentation, scripts, workflows, repo governance 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