xmem: map physical pages without a cache modifier in the view protection, again (RT-XMEM-VIEW-PROTECT) #328

Manually merged
github merged 3 commits from gh-pr/328/head into gh-pr/328/base 2026-11-26 08:53:38 +00:00
Owner

@⁠Mumogg on GitHub · 2026-11-24 03:11 UTC · original
Mumogg:xmem-view-protect → main · +37 −1 · 2 files · 3 commits · merged 2026-11-24 04:31 UTC

Claimed in #14 as RT-XMEM-VIEW-PROTECT.

What and why

Three of the retail executable's physical-page windows carry PAGE_WRITECOMBINE next to PAGE_READWRITE. On main, view_protect hands that protection to MapViewOfFile3, which refuses a cache modifier, so the first XMemMapPhysicalPages into such a window fails and the title's device set-up returns false.

The fix is two commits by hcristosm, cherry-picked unchanged (-x, authorship kept): 7e8e3c1db drops the cache modifiers from the view protection and 954fcb106 keeps PAGE_GUARD. Both are ancestors of main, but the change is not in the tree (finding in #14), so a merge does not bring it back.

One commit of mine on top: the test case that came with the fix queried the view right after mapping it. main's xmem commits a page on first touch, so the case now writes the page first, as the read-write case above it does.

Measured

Linux (Nobara 44, kernel 7.3), Wine 11.21 staging, RTX 5070 Ti (driver 615.78.08), clang-cl 22.1.8 + xwin SDK 10.0.26100.

xmem_test under Wine, the new write-combine case:

a page maps into a window reserved with PAGE_WRITECOMBINE
main's xmem.cpp with the new test case FAIL
this branch PASS, and the page in the write-combine window holds data passes too

At boot (own 1.0.2.1 copy, network off), measured in the integration branch of #321 with main's xmem.cpp swapped in: without the fix the run stops at 1,181 D3D12 calls, on the title's own int3 after the function that creates its device returns false; with it the run goes on to 50,468 pipeline states. It cannot be measured on main alone yet, because main stops earlier (#127, #131, then device slot 26).

Two checks that fail under Wine with and without this change

a mapped page is a read-write view with the window's protection already fails on main under Wine, and the new the page mapped there is a plain read-write view fails the same way: after the page has been written, Wine's VirtualQuery still reports the view as MEM_RESERVE / MEM_MAPPED with protection 0. The data checks around them pass, so the page is there. I do not know what Windows reports for these two; CI will say, and if the new check is wrong for the commit-on-touch design I will change it.

This matters beyond the test: any code that decides whether title memory is readable from VirtualQuery's State == MEM_COMMIT will see these pages as unreadable under Wine. That may be why #321 fails a submit with an unknown resource when it uses main's xmem; not confirmed.

Not built with MSVC and not run on Windows.

AI-assisted (Claude Code); built and run on the machine above.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HxBQf4pXH8PWpC5oZsM4bt

**@⁠Mumogg** on GitHub · 2026-11-24 03:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/328) `Mumogg:xmem-view-protect` → `main` · +37 −1 · 2 files · 3 commits · merged 2026-11-24 04:31 UTC Claimed in #14 as `RT-XMEM-VIEW-PROTECT`. ### What and why Three of the retail executable's physical-page windows carry `PAGE_WRITECOMBINE` next to `PAGE_READWRITE`. On `main`, `view_protect` hands that protection to `MapViewOfFile3`, which refuses a cache modifier, so the first `XMemMapPhysicalPages` into such a window fails and the title's device set-up returns false. The fix is two commits by hcristosm, cherry-picked unchanged (`-x`, authorship kept): `7e8e3c1db` drops the cache modifiers from the view protection and `954fcb106` keeps `PAGE_GUARD`. Both are ancestors of `main`, but the change is not in the tree (finding in #14), so a merge does not bring it back. One commit of mine on top: the test case that came with the fix queried the view right after mapping it. `main`'s xmem commits a page on first touch, so the case now writes the page first, as the read-write case above it does. ### Measured Linux (Nobara 44, kernel 7.3), Wine 11.21 staging, RTX 5070 Ti (driver 615.78.08), clang-cl 22.1.8 + xwin SDK 10.0.26100. `xmem_test` under Wine, the new write-combine case: | | `a page maps into a window reserved with PAGE_WRITECOMBINE` | |---|---| | `main`'s `xmem.cpp` with the new test case | **FAIL** | | this branch | PASS, and `the page in the write-combine window holds data` passes too | At boot (own 1.0.2.1 copy, network off), measured in the integration branch of #321 with `main`'s `xmem.cpp` swapped in: without the fix the run stops at 1,181 D3D12 calls, on the title's own `int3` after the function that creates its device returns false; with it the run goes on to 50,468 pipeline states. It cannot be measured on `main` alone yet, because `main` stops earlier (#127, #131, then device slot 26). ### Two checks that fail under Wine with and without this change `a mapped page is a read-write view with the window's protection` already fails on `main` under Wine, and the new `the page mapped there is a plain read-write view` fails the same way: after the page has been written, Wine's `VirtualQuery` still reports the view as `MEM_RESERVE` / `MEM_MAPPED` with protection 0. The data checks around them pass, so the page is there. I do not know what Windows reports for these two; CI will say, and if the new check is wrong for the commit-on-touch design I will change it. This matters beyond the test: any code that decides whether title memory is readable from `VirtualQuery`'s `State == MEM_COMMIT` will see these pages as unreadable under Wine. That may be why #321 fails a submit with an unknown resource when it uses `main`'s xmem; not confirmed. Not built with MSVC and not run on Windows. AI-assisted (Claude Code); built and run on the machine above. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01HxBQf4pXH8PWpC5oZsM4bt
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.

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

(cherry picked from commit 954fcb1061)
main's xmem commits a physical page on first touch, so the check added with the view-protection fix has to touch
the page first, as the read-write case above it does.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HxBQf4pXH8PWpC5oZsM4bt
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:53:37 +00:00
github manually merged commit f70ed489f5 into gh-pr/328/base 2026-11-26 08:53:38 +00:00
Author
Owner

@⁠moffington on GitHub · 2026-11-24 03:41 UTC · original

Tested f70ed489 against merge base 84347bbb on Omarchy Linux with Wine 11.20 and Clang 23.1.3 targeting MinGW. A standalone probe linked against unmodified xmem.cpp and registry.cpp: both XMemMapPhysicalPages and XMemMapPhysicalPagesScatter failed with error 87 on PAGE_WRITECOMBINE and PAGE_NOCACHE windows at the merge base. Both mapped successfully and read back the written data at the PR head.

I also reproduced the VirtualQuery discrepancy in an adapted xmem_test. Wine reported MEM_RESERVE and Protect=0 even after a successful write to a commit-on-touch mapping. Precommitting the page before mapping made it report MEM_COMMIT with the expected protection. The adapted test skipped SEH-dependent coverage due to local toolchain limits. The unmodified xmem_test on native Windows is still pending.

**@⁠moffington** on GitHub · 2026-11-24 03:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/328#issuecomment-7121324037) Tested f70ed489 against merge base 84347bbb on Omarchy Linux with Wine 11.20 and Clang 23.1.3 targeting MinGW. A standalone probe linked against unmodified xmem.cpp and registry.cpp: both XMemMapPhysicalPages and XMemMapPhysicalPagesScatter failed with error 87 on PAGE_WRITECOMBINE and PAGE_NOCACHE windows at the merge base. Both mapped successfully and read back the written data at the PR head. I also reproduced the VirtualQuery discrepancy in an adapted xmem_test. Wine reported MEM_RESERVE and Protect=0 even after a successful write to a commit-on-touch mapping. Precommitting the page before mapping made it report MEM_COMMIT with the expected protection. The adapted test skipped SEH-dependent coverage due to local toolchain limits. The unmodified xmem_test on native Windows is still pending.
Author
Owner

@⁠BadgersMC on GitHub · 2026-11-24 03:49 UTC · original

Tested f70ed489f5c9ed138cf766468a2e2fd5b2855f12 on native Windows 11 Home build 26100, MSVC 19.44.35228, Windows SDK 10.0.26100, and RelWithDebInfo. The synthetic test uses the CPU only and requires no game files.

ctest --test-dir <build> -C RelWithDebInfo -R '^xmem$' -V passed all 217 checks. Both VirtualQuery assertions called out in the PR passed, including the check that the write-combine window has a committed PAGE_READWRITE view after the first write.

As a control, I kept the PR's xmem_test.cpp, replaced only runtime/shims/xmem.cpp with the version from merge base 84347bbb61ed406db10195f50d5911893b8b9a4b, and rebuilt the same target. All 217 checks still passed, including the mapping, protection, and readback checks. I restored the PR source, rebuilt, and repeated the passing run. This confirms the expected protection on this Windows host. The fixture distinguishes the implementations in the reported Wine run, but did not distinguish them here.

AI-assisted review and testing (Codex and Claude Code).

**@⁠BadgersMC** on GitHub · 2026-11-24 03:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/328#issuecomment-7121454935) Tested `f70ed489f5c9ed138cf766468a2e2fd5b2855f12` on native Windows 11 Home build 26100, MSVC 19.44.35228, Windows SDK 10.0.26100, and `RelWithDebInfo`. The synthetic test uses the CPU only and requires no game files. `ctest --test-dir <build> -C RelWithDebInfo -R '^xmem$' -V` passed all 217 checks. Both `VirtualQuery` assertions called out in the PR passed, including the check that the write-combine window has a committed `PAGE_READWRITE` view after the first write. As a control, I kept the PR's `xmem_test.cpp`, replaced only `runtime/shims/xmem.cpp` with the version from merge base `84347bbb61ed406db10195f50d5911893b8b9a4b`, and rebuilt the same target. All 217 checks still passed, including the mapping, protection, and readback checks. I restored the PR source, rebuilt, and repeated the passing run. This confirms the expected protection on this Windows host. The fixture distinguishes the implementations in the reported Wine run, but did not distinguish them here. AI-assisted review and testing (Codex and Claude Code).
Author
Owner

@⁠Mumogg on GitHub · 2026-11-24 03:56 UTC · original

Thank you both. That settles what I could not measure, so to state the scope plainly:

  • Under Wine the change is needed: MapViewOfFile3 refuses the cache modifier there (error 87, reproduced independently above), and the retail boot stops at 1,181 D3D12 calls without it.
  • On Windows the map succeeds either way on the host tested above, so this fixture does not tell the two implementations apart there. The description's first paragraph reads as if the refusal were general; it is what I measured under Wine only.
  • The second commit's comment gives a Windows reason of its own (a write-combined view that a later import of those pages as a GPU buffer refuses). I have not measured that and nobody here has, so it should be read as the original author's note, not as something this pull request shows.
  • The two VirtualQuery checks are right for Windows (they pass there) and fail under Wine for commit-on-touch pages, with and without this change. I would leave them as they are.

AI-assisted.

**@⁠Mumogg** on GitHub · 2026-11-24 03:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/328#issuecomment-7121556827) Thank you both. That settles what I could not measure, so to state the scope plainly: - **Under Wine the change is needed**: `MapViewOfFile3` refuses the cache modifier there (error 87, reproduced independently above), and the retail boot stops at 1,181 D3D12 calls without it. - **On Windows the map succeeds either way** on the host tested above, so this fixture does not tell the two implementations apart there. The description's first paragraph reads as if the refusal were general; it is what I measured under Wine only. - The second commit's comment gives a Windows reason of its own (a write-combined view that a later import of those pages as a GPU buffer refuses). I have not measured that and nobody here has, so it should be read as the original author's note, not as something this pull request shows. - The two `VirtualQuery` checks are right for Windows (they pass there) and fail under Wine for commit-on-touch pages, with and without this change. I would leave them as they are. AI-assisted.
This discussion has been locked. Commenting is limited to contributors.
No description provided.