Skip to content

xmem: map physical pages without a cache modifier in the view protection - #7492

Closed
hcristosm wants to merge 2 commits into
ProjectLeonida:mainfrom
hcristosm:fix/xmem-view-writecombine
Closed

hcristosm wants to merge 2 commits into
ProjectLeonida:mainfrom
hcristosm:fix/xmem-view-writecombine

Conversation

@hcristosm

@hcristosm hcristosm commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue: #7333 (new RT-5 item, no ID). Fixes a bug that came into main with #6919.

AI-assisted (Claude).

What and why

Three of the retail executable's five physical-page windows (the MEM_REPLACE_PLACEHOLDER windows carved out of
the placeholder) carry 0x400 next to PAGE_READWRITE in their protection word. host_protect drops only the
PAGE_GRAPHICS_* range (0x800..0x40000), so 0x400, which is PAGE_WRITECOMBINE on the PC, stays in the
window's protection. map_page then calls MapViewOfFile3(..., MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE | PAGE_WRITECOMBINE, ...), which fails with ERROR_INVALID_PARAMETER. The first
XMemMapPhysicalPages into such a window returns NULL; the title unmaps everything, its descriptor-heap setup
returns false and it asserts. The two windows without 0x400 mapped fine.

view_protect now drops PAGE_WRITECOMBINE and PAGE_NOCACHE (a view takes no cache modifier); PAGE_GUARD, an
access alarm rather than a cache modifier, is kept as the window has it.
The window's own recorded protection is unchanged.

Note for #7218, which reads PAGE_WRITECOMBINE back with VirtualQuery as a write-combine hint: with this change
the mapped pages no longer report it, so that hint would have to come from the window's recorded protection instead.

Tests

On native Windows MapViewOfFile3 accepts the bit and the view comes out write-combined (Protect = 0x404), which
#7218's import of the slot-133 pages refuses; with this change it imports them (RX 9070 XT, AMD Windows driver, tested
in the comments here by @jimokulou2-ops). Wine 11.18 refuses PAGE_NOCACHE with error 87 as well (comment here).

Three of the retail executable's five physical-page windows carry 0x400
(PAGE_WRITECOMBINE on the PC) next to PAGE_READWRITE. host_protect drops only the
PAGE_GRAPHICS_* range, so map_page passed PAGE_READWRITE | PAGE_WRITECOMBINE to
MapViewOfFile3, which refuses it with ERROR_INVALID_PARAMETER: the first
XMemMapPhysicalPages into such a window failed and the title's descriptor-heap
setup returned false.

view_protect now drops PAGE_WRITECOMBINE, PAGE_NOCACHE and PAGE_GUARD.
xmem_test maps a page into a window reserved with that protection.

AI-assisted.
@jtrudeau1530

Copy link
Copy Markdown

Code read of 7e8e3c1, plus related local diagnostic evidence; this is not a test of this PR head.

Our Wine 11.18 / RX 7900 XT (RADV Mesa 26.2.3), clang-cl 23.1.6 + xwin integration reproduced the section-view failure. A synthetic pagefile-section/placeholder probe accepts READWRITE and READONLY, but rejects READWRITE|WRITECOMBINE and READWRITE|NOCACHE with error 87. A local reproduction of the cache-only view_protect change proposed by @ramenbellic and @gkissel passes xmem and changes the retail stop from the post-map assertion to a later null execute fault. Map success in that final run is inferred from progression; we did not repeat it with allocation tracing. Native Windows was not tested. The original proposal and implementation are here and here; we have not executed gkissel's exact commit or its added tests.

One scope concern in view_protect: PAGE_GUARD is not a caching modifier. Microsoft documents it as a one-time access alarm. Dropping it silently can change access-fault behavior independently of the WC/NC mapping fix. Our diagnostic masks only WC|NC (0x600), retains the existing NOACCESS fallback, and leaves host_protect unchanged. Could this PR preserve PAGE_GUARD, or handle guarded views separately with an explicit policy and regression test? The added test currently covers WC, not guard semantics or NOCACHE.

Executed by the collaborating test runner; I reviewed its frozen-source diff, matching executable/PDB hashes, exit record and xmem result. No raw game logs or inputs shared. AI-assisted.

@jimokulou2-ops

Copy link
Copy Markdown

Tested 7e8e3c1db with the retail executable (own 1.0.2.1 copy) on AMD Radeon RX 9070 XT (RDNA4), AMD Windows driver 32.0.32029.2010 (26.11.2), Windows 11 Pro 26300, MSVC 19.44 RelWithDebInfo, LEONIDA_D3D12=vulkan: pass, it is what lets #7218 import the slot-133 pages on this card.

Base: main@51e7c9dc8 + #6919 36936f3bf + #4313 (rebased, IAmNotSiob:tls-4313-rebased bd71d37b7) + #6582 (IAmNotSiob:xerr-check b928f0290) + #4348 45c39cf6f + #7078 3ace9869d + #6954 72ab7623e + #2490 5be2fd287 + #7183 e7b3fade2 + #7218 c3fe82529.

Not checked: whether GPU reads of that buffer see the title's writes (no draw is reached yet), and the runtime's unit tests on this exact stack.

No game data: only flag values, sizes and counts.

AI-assisted (Claude Code).

…s are dropped

PAGE_GUARD is an access alarm, not a cache modifier (review on ItsNotRexed#7492). The comment
also says what the write-combine bit does on Windows, where the view is created
write-combined and ItsNotRexed#7218's import of those pages refuses it.
@hcristosm

Copy link
Copy Markdown
Author

Thanks both.

AI-assisted.

@jtrudeau1530

Copy link
Copy Markdown

Code read of 954fcb1: the PAGE_GUARD scope concern from my earlier review is addressed. view_protect now masks only WC|NC and retains the existing NOACCESS fallback; host_protect is unchanged.

This is source review, not an exact-head test. Our earlier cache-only local diagnostic remains related evidence, not validation of this PR's test addition. The original reporter now also has #7550, and #7499 overlaps; authors/coordinator can choose one without a fourth implementation from us. AI-assisted.

@SkillIssueXO

Copy link
Copy Markdown

Independent native Windows A/B of 954fcb106 against its upstream base 37f2151. Windows 11 build 26300, LLVM-MinGW Clang 23.1.6, CPU-only synthetic tests.

xmem_test passed 180/180 in all three runs on this head. The same test linked with the base shim failed exactly one check in each run, the plain read/write protection of the mapped write-combine window. For libc++ only, the test copy used path.c_str() in its two ifstream calls. Both shim trees were unmodified.

A separate probe allocated and mapped one 64 KiB page for each case through the registered XMem exports. All maps succeeded. VirtualQuery().Protect returned these values.

Window protection Base 37f2151 PR 954fcb106
PAGE_READWRITE 0x004 0x004
PAGE_READWRITE | PAGE_WRITECOMBINE 0x404 0x004
PAGE_READWRITE | PAGE_NOCACHE 0x204 0x004
PAGE_READWRITE | PAGE_GUARD 0x104 0x104

I queried the guard view without touching it. This confirms the latest head's cache-only change on native Windows. No game files or Vulkan import were used. AI-assisted testing (Codex).

@gkissel

gkissel commented Nov 23, 2026

Copy link
Copy Markdown

Independent confirmation under Wine 11.21 (Linux, clang-cl + xwin, GTX 1650 Mobile), retail 1.0.2.1 (own copy). Outcomes only. I'd found the same thing and opened #7852, now closed as a duplicate of this one.

  • In the boot (main + open PRs), the first XMemMapPhysicalPages into the descriptor pool failed with ERROR_INVALID_PARAMETER (87) on page 0, with view protection 0x404. With view_protect dropping PAGE_WRITECOMBINE | PAGE_NOCACHE, the run goes past the pool.
  • A synthetic test with the pool's measured shape gives the same result on current main under Wine 11.21: page 0 of 752 failed to map, error 87 before the change, xmem_test: OK after. The shape: 185 MiB placeholder, five MEM_REPLACE_PLACEHOLDER | MEM_PHYSICAL | MEM_LARGE_PAGES windows with PAGE_READWRITE | PAGE_WRITECOMBINE, 0x2c60 + 0x5c80 pages, and 0x2f0 mapped in one call.

AI-assisted (Claude Code). No game files here.

@DeSunShine

Copy link
Copy Markdown
Contributor

Tested 954fcb106 merged into main dde82f27e on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass.

  • ctest -R "xmem|tiny_game" --output-on-failure: 100% passed out of 11 (xmem and the run_tiny_game* tests). Native Windows only; the Wine behaviour the PR targets was not exercised.

AI-assisted (Claude Code).

@ramenbellic

Copy link
Copy Markdown

Tested 954fcb10 under Wine: Proton Experimental (wine 11), clang-cl 23.1.6 + xwin cross-build, RTX 3070. xmem_test: OK (180 checks).

On the retail boot (own 1.0.2.1 copy, outcomes only), the same view_protect change lets all five XMemMapPhysicalPages calls succeed on Wine (752, 1600, 160, 16 and 432 pages). Without it, the first map into the 0x2404 window fails with ERROR_INVALID_PARAMETER and the device init asserts.

I opened #7550 for the same fix without seeing this one; it is closed in favour of this PR, and its test adds nothing this one lacks.

AI-assisted (Claude Code); outcomes only, no game data.

@AwkGrep256

Copy link
Copy Markdown
Contributor

Tested 954fcb1 on Linux/Wine 11.22 (CachyOS, RX 9070 XT/RADV 26.3.1, own 1.0.2.1 copy, merged into tibzejoker:rt5-integration + #7673 + #7417): pass, and it's what unblocks Linux.

Without it, the title's descriptor-heap allocator calls XMemMapPhysicalPages on 752 pages into a window whose view protection is 0x404 (PAGE_READWRITE | PAGE_WRITECOMBINE); Wine's MapViewOfFile3(MEM_REPLACE_PLACEHOLDER) fails it with ERROR_INVALID_PARAMETER, so graphics init returns false right after XboxIDXGIDevice::GetAdapter (1,181 D3D12 calls). With this PR every map succeeds and the run goes on to 2,262 calls (stub pipelines, root signatures, the first DirectStorage queue) before the next, unrelated stops. Details: #7599. AI-assisted.

@dracbat

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A data point from a Windows retail boot. RX 5700 (RDNA1), AMD driver 32.0.21045.29003, own 1.0.2.1 copy, a local stack with #7218 (a3eae95) and this PR.

This PR's view_protect covers mapped views. But the slot-133 buffers that #7218 tries to import live in memory the title commits directly through XMemVirtualAlloc, and host_protect keeps the cache modifier there. I added a local log to #7218's create_host_buffer. It refused both buffers it saw, at 460,800 bytes and at 16 bytes, with page state MEM_COMMIT and protect 0x404 (PAGE_READWRITE | PAGE_WRITECOMBINE).

The same strip in host_protect fixes it: p & ~(kPageGraphicsMask | PAGE_WRITECOMBINE | PAGE_NOCACHE). With that, the next run had 0 import refusals, and #7218 imported every slot-133 buffer, using the RX 5700's VK_EXT_external_memory_host (minImportedHostPointerAlignment 0x1000). The boot runs its frame loop with PresentX succeeding, but the frames are still black. As expected, the UI's textures are still ordinary textures, not on the title's pages.

So this PR could cover host_protect as well. Otherwise #7218 falls back for every directly committed range on Windows.

AI-assisted (Claude Code).

@thedarkcr0w

Copy link
Copy Markdown

Tested on the retail boot, together with #7218 (and #8225 for the comparison). Own 1.0.2.1 copy, Windows 11, NVIDIA GeForce RTX 4090 (driver 32.0.16.1062), LEONIDA_D3D12=vulkan, MSVC 2022 RelWithDebInfo; local integration of main with the boot-chain PRs (#4313, #6582, #4348, #6953, #7075, #7066, #7218, #7235, #8225 among them).

slot 133's buffer on the title's pages
without this PR import refused (the write-combined windows fail #7218's VirtualQuery check), E_NOTIMPL; #8225's shadow copy takes over
with this PR imported: one buffer of 0xae00000 bytes at the title's VA (LEONIDA_ARENA_LOG: "title-placed buffer VA ... + 0xae00000 (outside the arena)"), no shadow

So on this driver the whole 174 MiB range imports once the views carry no cache modifier; the ~64 MiB host-import budget #8166 measured on an RTX 3070 Ti does not show up on the 4090. The boot continues to the same later stop either way. As your description says, #7218's write-combine hint then has to come from the window's recorded protection, not from VirtualQuery.

AI-assisted (Claude Code).

@miguelsilva5989

Copy link
Copy Markdown

Tested head 954fcb1 on CachyOS with Wine 11.22 and a Clang 23.1.5 / MinGW build. The PR's synthetic xmem_test failed when linked against the base XMem implementation: the physical page would not map into a window reserved with PAGE_WRITECOMBINE. With the patched implementation, all 180 checks passed, including the read-write view and its contents.

Both builds used the same fixture, with only two ifstream arguments changed to .c_str() in scratch for MinGW compatibility. No title files were used. AI-assisted verification.

@Cryptoteep

Copy link
Copy Markdown
Contributor

checked: #749275f9274 | with: git merge-tree $(git merge-base main pr-7492) main pr-7492 + check_no_game_data | on: Windows 11, Intel CC150, RTX 5060 | result: clean merge onto main 867d922, 0 conflicts across all files, 0 game data

Audited diff:

  • view_protect in runtime/shims/xmem.cpp explicitly strips PAGE_WRITECOMBINE and PAGE_NOCACHE (protect & ~static_cast<DWORD>(PAGE_WRITECOMBINE | PAGE_NOCACHE)) while preserving PAGE_GUARD alarm and underlying window attributes.
  • Solves MapViewOfFile3 failing with ERROR_INVALID_PARAMETER (error 87) on physical-page windows carved out of the placeholder that carry 0x400 modifier.
  • New test test_write_combine_window in runtime/test/xmem_test.cpp allocates, maps, inspects with VirtualQuery, fills/verifies data, unmaps and frees 64k page window with write-combine protection.
  • Merges cleanly against main (867d922f0) with zero conflicts.
  • Verified python -I scripts/check_no_game_data.py runtime/shims/xmem.cpp runtime/test/xmem_test.cpp -> clean, exit 0.

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

checked: #7492 at 954fcb1, merged onto main 867d922 (clean merge, 2 files, +35/-1) | on: Wine 11 on RunPod RTX 4090 (Linux), clang-cl cross-build; not native Windows | ran: xmem_test built from the merged tree: EXIT 0, xmem_test: OK (180 checks), including the six new write-combine-window checks. Negative control: same test with only runtime/shims/xmem.cpp reverted to main: EXIT 1, FAIL a page maps into a window reserved with PAGE_WRITECOMBINE, xmem_test: FAILED (178 checks, 1 failure). So the new test fails without the fix and passes with it, under Wine.

diff review: view_protect now clears only PAGE_WRITECOMBINE | PAGE_NOCACHE (0x600) from the window's protection and keeps the existing PAGE_NOACCESS -> PAGE_READWRITE fallback; PAGE_GUARD is deliberately left (the author narrowed it after review, and the comment says why). The change is one function with one caller path for views, so the blast radius is small. The test reserves a window with PAGE_READWRITE | PAGE_WRITECOMBINE | PAGE_GRAPHICS_READWRITE, maps a page, checks the view is MEM_COMMIT and exactly PAGE_READWRITE, writes and reads it, then unmaps and frees it; that matches the PR's claim. No game files.

Limits to read carefully: what I reproduced is the Wine refusal. The PR's second reason (on Windows the view comes out write-combined and a GPU-buffer import then refuses it, #7218) is not something I could run here. Also, dropping the cache modifier means pages the title expects to be write-combined are ordinary cached views; the PR states this and I did not find a case in the diff or the test that needs the modifier.

Not verified: native Windows, retail boot, #7218's import path. I did not re-run anything on top of a D3D12 stack.

agent: yes
AI-assisted.

@byPreaZy

Copy link
Copy Markdown

checked: #7492 | with: applied the diff on main 867d922, built xmem_test (MSVC, Ninja RelWithDebInfo) | on: Windows 11, RX 9060 XT | result: xmem_test OK (180 checks), incl. the new write-combine window test: reserve with PAGE_WRITECOMBINE -> map -> VirtualQuery shows PAGE_READWRITE, data round-trips

Code read: view_protect now strips PAGE_WRITECOMBINE|PAGE_NOCACHE (Wine MapViewOfFile3 refuses them; a WC view breaks GPU buffer import per #7218) while keeping PAGE_GUARD and the NOACCESS->READWRITE special case. Matches the three measured retail windows carrying 0x400.

AI-assisted check.

Raxstreams added a commit to Raxstreams/Leonida that referenced this pull request Nov 23, 2026
xmem: map physical pages without a cache modifier in the view protection

Fork integration. AI-assisted (Codex).
@nullmarsh

Copy link
Copy Markdown
Contributor

checked: #7492 (A38) | head 954fcb1 merged onto main 867d922 (clean) | on: native Windows 11 26200, MSVC 14.44 RelWithDebInfo, RX 9060 XT box | result: pass. xmem_test OK (180 checks), and loader passes.
Negative control, natively: this PR's test with main's xmem.cpp gives 180 checks, 1 failure. On Windows the map succeeds (unlike Wine), but FAIL the page mapped there is a plain read-write view: the view keeps the window's write-combine modifier. So the fix matters on native Windows too, for the view's cache type rather than the map call, which bears on #7550 being the Wine-only alternative. Diff read: no process, network or file-removal calls.
agent: yes

@SirKingg

Copy link
Copy Markdown
Contributor

Independent Windows Sandbox comparison of 954fcb106 in a temporary stack including #7218, #8379, #2490, #6582, #8461, #6964, #8258 and #7189. The host-memory integration and earlier proxy/mock overlaps required local reconciliation; this is not the PR branch as a whole. Networking was disabled, game inputs were read-only and overlays were fresh.

  • Before this change: 2/2 runs reached slot 133 but logged an E_NOTIMPL import failure and an unaliased ordinary-buffer fallback.
  • With this change: 2/2 runs recorded a successful slot-133 result without either the buffer or texture fallback diagnostic, then reached descriptor-heap creation and the same later guest breakpoint. This is consistent with the import path succeeding; the retail guest's CPU/GPU data coherence was not directly verified by those runs.
  • xmem, xs_proxy_mock, gameinput and d3d12_existing_heap CTests passed (4/4) on Windows 11 / MSVC 2022, host RTX 4070 Ti, Vulkan validation enabled. The synthetic host-memory test separately confirmed GPU readback of CPU writes for ordinary and section-view allocations.

Only counts, error classes and public API/slot names are shared; traces stay local. AI-assisted.

@isfcrctfs

Copy link
Copy Markdown

checked: #7492954fcb1 | with: fresh clone of origin/main (867d922f0) + git apply the PR (clean, 2 files +35/-1); cmake -S runtime -B build --toolchain runtime/cmake/clang-cl-xwin.cmake ... then cmake --build build -j16 (218 targets, exit 0); wine xmem_test.exe 3x; negative control with only runtime/shims/xmem.cpp reverted to main and the PR's test kept | on: Linux, clang-cl 23.1.5 + lld-link against xwin SDK 10.0.26100, CMake 4.5.1, Wine 11.22-staging, no GPU (llvmpipe only; this test does not use it) | result: cross-build pass — xmem_test: OK (180 checks), exit 0, 3 of 3 runs, including the six new write-combine-window checks. Negative control: xmem_test: FAILED (178 checks, 1 failure), exit 1, exactly at "a page maps into a window reserved with PAGE_WRITECOMBINE".

Audit: diff read line by line; audit.py diff clean; only runtime/shims/xmem.cpp and runtime/test/xmem_test.cpp; no CI, build-script, network, submodule or binary changes.

Build: configure per the standard cross recipe; full cmake --build build -j16 exit 0 in 10m16s. Shader-dependent targets are not built here (no glslangValidator); none is touched by this PR. Not run: the retail executable (no game copy), native Windows/MSVC, any GPU path — this is a cross-build + Wine result, not a Windows check.

Import status, which a maintainer should see before importing this: the forge main already has this fix in another form, and this head would undo #7693 there.

agent: yes
AI-assisted.

hexxone pushed a commit to hexxone/Leonida that referenced this pull request Nov 23, 2026
…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>
(cherry picked from commit 8bf9bbe)
Impre-visible pushed a commit to Impre-visible/Leonida that referenced this pull request Nov 23, 2026
…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>
(cherry picked from commit 8bf9bbe)
Signed-off-by: local <local@localhost>
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.