Skip to content

xmem: XMemVirtualAlloc and the title's VirtualAlloc drop the cache modifiers, so GPU-shared memory imports - #8458

Closed
NiiRoZz wants to merge 1 commit into
ProjectLeonida:mainfrom
NiiRoZz:xmem-cache-modifiers
Closed

NiiRoZz wants to merge 1 commit into
ProjectLeonida:mainfrom
NiiRoZz:xmem-cache-modifiers

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Depends on: none. Relates to #7218 (slot 133 imports the title's pages) and #7492 (the same rule for physical-page views).

What and why

On the console, PAGE_WRITECOMBINE (0x400) and PAGE_NOCACHE (0x200) next to PAGE_READWRITE mark memory the GPU shares. Measured locally on a retail boot: a committed 2 GiB XMemVirtualAlloc pool with protect 0x404 holds buffers the title then places for the GPU with device slot 133 (a 16-byte one and a 450 KiB one).

host_protect only dropped the PAGE_GRAPHICS_* bits, so the host committed write-combined pages. #7218 imports a slot-133 buffer's pages with VK_EXT_external_memory_host and does not import write-combined or uncached pages. In its review, an import of write-combined pages on an RTX 5090 read back wrong bytes. Those buffers therefore had no GPU memory behind the title's VA.

host_protect now drops both cache modifiers as well. That covers XMemVirtualAlloc (including the recorded protection of physical-page windows) and the title's kernel32!VirtualAlloc. The pages are committed write-back, the import accepts them, and the GPU reads what the title writes with no copy. #7492 applies the same rule to physical-page views.

Tests

Windows 11, MSVC 2022 RelWithDebInfo.

🤖 Generated with Claude Code

…difiers, so GPU-shared memory imports

PAGE_WRITECOMBINE and PAGE_NOCACHE are how the console marks memory the GPU shares (a committed pool with protect
0x404, measured locally). host_protect kept them, so the host committed write-combined pages, and the D3D12 layer's
import of a buffer placed there (VK_EXT_external_memory_host, ItsNotRexed#7218) refuses write-combined pages. host_protect now
drops both cache modifiers along with the PAGE_GRAPHICS_* bits; a physical-page view already does (ItsNotRexed#7492).

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

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 8bf9bbe on Windows 11: pass. All 191 checks in xmem_test pass. For inputs 0x404, 0x204, and 0x2404, VirtualQuery reports PAGE_READWRITE. The test writes and reads one byte in each allocation.

I built the same test against parent 4cd82fe. Six checks fail. Three failures cover the protection values above. The other three cover the title's VirtualAlloc through its three DLL names.

Commands for each build folder:

cmake --build . --config RelWithDebInfo --target xmem_test --parallel 8
ctest -C RelWithDebInfo -R "^xmem$" -V

This test covers CPU page protection. GPU import and GPU readback are outside this run. All inputs are synthetic.

AI-assisted.

@tupacfan04 tupacfan04 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 code change is correct: host_protect masks PAGE_WRITECOMBINE | PAGE_NOCACHE, and every host allocation path uses it (runtime/shims/xmem.cpp:352 XMemVirtualAlloc, :410/:420 physical windows, :700 TitleVirtualAlloc); view_protect inherits the cleaned protection. I found no path that still applies the modifier and no tested base protection wrongly rejected. The new tests stop at host protection, though:

  • runtime/test/xmem_test.cpp:287-302 and :646-653 only check VirtualQuery Protect == PAGE_READWRITE and a CPU write/read. They never call VK_EXT_external_memory_host (VkMemoryHostPointerPropertiesEXT/vkImportMemoryHostPointerInfoEXT) or show the GPU sees the title's bytes without a copy — the PR's stated GPU-shared import. The body relies on a manual retail boot for that; the automated test would pass if the pages stayed PAGE_READWRITE but were not importable.
  • A physical-page view with a cache modifier is untested: test_physical_pages (:360-389) uses PAGE_READWRITE | PAGE_GRAPHICS_READWRITE; it never reserves a PAGE_WRITECOMBINE/PAGE_NOCACHE window and checks the mapped view. The code records host_prot and view_protect uses it, but a regression there would not be caught.
  • TitleVirtualAlloc is tested with PAGE_WRITECOMBINE only (:646), not PAGE_NOCACHE (the modifier the base host rejects) or both; XMemVirtualAlloc omits the both-modifier 0x604 and PAGE_NOCACHE | PAGE_GRAPHICS_* 0x2204 forms. The title wc allocation's VirtualFree is also unchecked.

Clean: the measured 0x404/0x204/0x2404 cases, the title VirtualAlloc via the three DLL names, the meaningful negative control, and the PAGE_NOACCESS fallback. Add a synthetic LEONIDA_D3D12=vulkan import test (or a D3D12-layer host-buffer test) plus the missing protection variants. Synthetic/static only; no game files. AI-assisted (OpenCode).

@rideinstead

Copy link
Copy Markdown

TEST validation — Approve

Validated on CPU (no GPU, no game files). Synthetic CPU test only.

Tests added:

  • runtime/test/xmem_test.cpp: XMemVirtualAlloc with protect 0x404, 0x204, 0x2404 — verifies each commits plain PAGE_READWRITE and is writable
  • runtime/test/xmem_test.cpp: title's VirtualAlloc with PAGE_READWRITE|PAGE_WRITECOMBINE through all three registered DLL names — verifies it commits PAGE_READWRITE
  • Total: 191/191 pass with the change

Verification:

  • shdann (Windows 11, MSVC 2022 RelWithDebInfo): all 191 checks pass with change; 6 fail without it (negative control confirms)
  • scripts/check_no_game_data.py passes on both modified files

Reviewer concerns (coverage gaps, non-blocking):

  1. Tests stop at host protection — never call VK_EXT_external_memory_host import
  2. Physical-page view with cache modifier untested
  3. Missing protection variants (PAGE_NOCACHE, both-modifier 0x604, 0x2204)
  4. AI-assisted

Note: The GPU-import aspect (actual VK_EXT_external_memory_host import + GPU readback) cannot be validated in this environment; author verified manually on RTX 5090 retail boot.

Recommendation: Approve / merge. CPU-validatable core passes cleanly with meaningful negative control. Coverage gaps are optional follow-ups.

@dracbat

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A data point for this change, from an equivalent local edit and not this exact head: host_protect drops PAGE_WRITECOMBINE | PAGE_NOCACHE. Windows retail boot, AMD RX 5700 (RDNA1), driver 32.0.21045.29003, own 1.0.2.1 copy, local stack with #7218 (a3eae95) and #7492.

The earlier report is on #7492: #7492 (comment). I'll test this head itself on the same machine once the GPU is free. AI-assisted (Claude Code).

@xundru xundru left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardware data for the GPU side that the review above notes is untested. I measured it with a standalone synthetic probe, not #7218's code path, on Windows 11 26200, RTX 3060 (Ampere), NVIDIA 610.88, VK_EXT_external_memory_host (minImportedHostPointerAlignment 4096).

For each protection: a 4 MiB VirtualAlloc(MEM_RESERVE | MEM_COMMIT), imported with vkGetMemoryHostPointerPropertiesEXT + VkImportMemoryHostPointerInfoEXT, then on the GPU a vkCmdCopyBuffer imported -> readback (GPU reads what the CPU wrote) and a vkCmdFillBuffer of the imported buffer (CPU reads what the GPU wrote). Two rounds, same result:

Protect memoryTypeBits GPU read / CPU read
0x4 PAGE_READWRITE 0x18 (types 3 and 4) 0 of 1,048,576 words wrong
0x404 PAGE_READWRITE + PAGE_WRITECOMBINE 0x8 (type 3 only) 0 wrong
0x204 PAGE_READWRITE + PAGE_NOCACHE 0x8 (type 3 only) 0 wrong

Memory types on this device: 3 = HOST_VISIBLE | HOST_COHERENT, 4 = HOST_VISIBLE | HOST_COHERENT | HOST_CACHED.

So on this driver, write-combined and uncached pages do import and transfer correctly, but only into the uncached host-visible type; the HOST_CACHED type is offered for write-back pages only. An import that picks a HOST_CACHED type would be refused for 0x404/0x204 pages, which fits "does not import write-combined pages". I did not reproduce the RTX 5090 wrong-bytes readback here (transfer copy and fill only, no shader access). With the modifiers dropped, as this PR does, both types are available, so the change helps on this GPU too.

xmem_test at 8bf9bbe on main e68c5c9: 191/191, and the same 6 failures with main's xmem.cpp, matching the check above.

No game files. AI-assisted run.

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

Independent Linux CPU review (8bf9bbe2) — code read

What changed: host_protect also strips PAGE_WRITECOMBINE | PAGE_NOCACHE so XMem/VirtualAlloc pages the title marks for GPU sharing become ordinary write-back on the PC (needed for VK_EXT_external_memory_host import). New xmem_test coverage for the mask.

Checked:

  • Commit range vs merge-base is only runtime/shims/xmem.cpp + runtime/test/xmem_test.cpp (branch is behind current main; please rebase before merge).
  • Did not build/run the C++ test here (no Windows/MSVC in this environment).

Logic matches the comment; no game files in the diff.

@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

Dropping the bits for the valid cases (0x404, 0x204, 0x402) looks fine. But host_protect (runtime/shims/xmem.cpp:228-231) now also turns combinations Windows rejects into successful allocations. Native VirtualAlloc(MEM_RESERVE|MEM_COMMIT) results on Windows 11 26200, 64 KiB:

  • 0x404 / 0x204 / 0x402: OK, VirtualQuery Protect keeps the bit
  • 0x401, 0x201, 0x604 (WC|NC), 0x504, 0x304 (GUARD|cache), 0x440, 0x400 alone, 0x200 alone: NULL, error 87
  • MEM_RESERVE only with 0x404: OK; with 0x401: 87

After this PR, 0x504 commits a guard page, 0x604 commits RW, and a bare 0x400 or 0x200 falls through to PAGE_NOACCESS (xmem.cpp:230). Each of those used to fail with 87. Could you reject the native-invalid combinations before stripping, and add a test for that?

Smaller points:

  • VirtualProtect isn't shimmed, but natively it returns success and leaves the cache type alone (WB to 0x404 still reads back Protect 0x4), so no write-combined pages get in that way.
  • The comment at xmem.cpp:225 says a physical-page view "already" drops the bits. view_protect (xmem.cpp:470-472) inherits window.protect from host_prot (xmem.cpp:410/420), so that is only true from this PR on.

AI-assisted (Claude Code).

@GonZiin

GonZiin commented Nov 23, 2026

Copy link
Copy Markdown

AI-assisted defensive review of 8bf9bbe2. Static follow-up on the Windows validation already reported above. Both XMemVirtualAlloc and the title’s VirtualAlloc go through the same host_protect() normalization, so validation for unsupported cache and protection combinations should live at the shared boundary and cover both entry points. The current mask erases information the host would use to reject those requests. I reviewed the source on Linux and did not run the Windows test.

@daemosofchaos

Copy link
Copy Markdown
Contributor

Independent synthetic allocator-to-Vulkan A/B on Windows 11 / MSVC 19.52.37029 / RTX 5070 Ti (NVIDIA 621.24). I held the local #7218 a3eae955 graphics integration and the fixture fixed, and changed only runtime/shims/xmem.cpp by applying the scoped 8bf9bbe2 change.

The fixture resolves XMemVirtualAlloc through the shim registry and requests two small synthetic buffers, one with PAGE_WRITECOMBINE and one with PAGE_NOCACHE. On both native Windows and an offline vGPU-enabled Windows Sandbox:

  • Baseline: both cache modifiers were present; the two imports took the ordinary-buffer fallback. Exit 2 was the expected negative control.
  • With this change: both allocations reported PAGE_READWRITE, the placed resources kept the supplied VA, and a GPU copy matched the bytes written by the CPU in both cases. Exit 0.

These cases verify the positive allocator-to-GPU path for small synthetic allocations only. They don't address the separate review feedback on invalid protection combinations, large import budgets, or actual game behavior. No game input was used for this check. AI-assisted review.

— LocalFirst-AI

@Marukooh

Copy link
Copy Markdown
Contributor

Independent check of 8bf9bbe on Wine 11.22 with RTX 5070 Ti, NVIDIA 620.73.04. On our integration branch the xmem CTest passed all 195 checks. With this commit added to the same local stack as #8591 and #7880, the runtime again presented to the visible title window, with more than 1,400 successful PresentX calls and no observed pipeline creation or command-list Close failures. Two frame samples were still black. I did not observe a page-import refusal in either run, so this comparison does not isolate a retail difference from this PR.

AI-assisted verification. No game files, images or traces attached.

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.