From 80ed5dd30438b507a03c6bb10e0adf0a0a403dea Mon Sep 17 00:00:00 2001 From: hcristosm Date: Mon, 23 Nov 2026 13:50:59 -0300 Subject: [PATCH 1/3] xmem: map physical pages without a cache modifier in the view protection 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 7e8e3c1db1dc3c4c44cc56e2e586f99de2e65ae2) --- runtime/shims/xmem.cpp | 5 ++++- runtime/test/xmem_test.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 1e286d712..552da6897 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp @@ -554,7 +554,10 @@ bool install_physical_page_fault_handler() { // Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still // GPU memory, which the D3D12 layer reads on the CPU. DWORD view_protect(const Window& window) { - return window.protect == PAGE_NOACCESS ? PAGE_READWRITE : window.protect; + // A view takes no cache modifier: MapViewOfFile3 refuses PAGE_WRITECOMBINE (0x400, which three of the measured + // windows carry next to PAGE_READWRITE) and PAGE_NOCACHE with ERROR_INVALID_PARAMETER. + const DWORD protect = window.protect & ~static_cast(PAGE_WRITECOMBINE | PAGE_NOCACHE | PAGE_GUARD); + return protect == PAGE_NOACCESS ? PAGE_READWRITE : protect; } bool aligned_page_address(uintptr_t address) { return (address & (kPhysicalPageSize - 1)) == 0; } diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 3ec621b0b..f552c5b6e 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp @@ -694,6 +694,34 @@ void test_placeholders(const PhysicalApi& api) { check(reserved_only(base), "the rest of the placeholder is still reserved"); } +// Three of the retail executable's five physical-page windows carry 0x400 next to PAGE_READWRITE. That is +// PAGE_WRITECOMBINE on the PC, outside the PAGE_GRAPHICS_* range, and MapViewOfFile3 refuses a view with it, so the +// mapped pages take the window's protection without the cache modifier. +void test_write_combine_window(const PhysicalApi& api) { + auto* const window = static_cast( + api.virtual_alloc(nullptr, kPage64k, MEM_RESERVE | kXmem64kPages, 0x27, + PAGE_READWRITE | PAGE_WRITECOMBINE | PAGE_GRAPHICS_READWRITE)); + check(window != nullptr, "XMemVirtualAlloc reserves a window with PAGE_WRITECOMBINE in its protection"); + if (!window) return; + + uint64_t page = 0; + uint64_t count = 1; + check(api.allocate(0x280, &count, &page) == TRUE && count == 1, "allocate a page for the write-combine window"); + SetLastError(ERROR_SUCCESS); + const bool mapped = api.map(window, 1, &page) == window; + check(mapped, "a page maps into a window reserved with PAGE_WRITECOMBINE"); + if (mapped) { + MEMORY_BASIC_INFORMATION info{}; + VirtualQuery(window, &info, sizeof(info)); + check(info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE, + "the page mapped there is a plain read-write view"); + fill_page(window, 0x77000000u); + check(page_holds(window, 0x77000000u), "the page in the write-combine window holds data"); + } + check(api.map(window, 1, nullptr) == window, "unmap the write-combine window"); + check(api.free_pages(1, &page) == TRUE, "free the write-combine window's page"); +} + // gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off. void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem, XMemVirtualAllocFn virtual_alloc) { wchar_t temp[MAX_PATH]; @@ -942,6 +970,7 @@ int main() { test_commit_physical_pages(physical); test_first_touch_during_remap(physical); test_placeholders(physical); + test_write_combine_window(physical); } print_result(); From 8d4dd7a80cbd55f8cbf1a89ef7780d806e04f401 Mon Sep 17 00:00:00 2001 From: hcristosm Date: Mon, 23 Nov 2026 14:05:51 -0300 Subject: [PATCH 2/3] xmem: keep PAGE_GUARD in the view protection; only the cache modifiers are dropped 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 954fcb10617a456632b3f9b8697138988e7675cd) --- runtime/shims/xmem.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 552da6897..a0335a237 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp @@ -554,9 +554,11 @@ bool install_physical_page_fault_handler() { // Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still // GPU memory, which the D3D12 layer reads on the CPU. DWORD view_protect(const Window& window) { - // A view takes no cache modifier: MapViewOfFile3 refuses PAGE_WRITECOMBINE (0x400, which three of the measured - // windows carry next to PAGE_READWRITE) and PAGE_NOCACHE with ERROR_INVALID_PARAMETER. - const DWORD protect = window.protect & ~static_cast(PAGE_WRITECOMBINE | PAGE_NOCACHE | PAGE_GUARD); + // A view takes no cache modifier. Three of the measured windows carry PAGE_WRITECOMBINE (0x400) next to + // PAGE_READWRITE: Wine's MapViewOfFile3 refuses it, and PAGE_NOCACHE, with ERROR_INVALID_PARAMETER, and on Windows + // the view comes out write-combined, which an import of those pages as a GPU buffer refuses (#7218). PAGE_GUARD + // is an access alarm, not a cache modifier, so it is left as the window has it. + const DWORD protect = window.protect & ~static_cast(PAGE_WRITECOMBINE | PAGE_NOCACHE); return protect == PAGE_NOACCESS ? PAGE_READWRITE : protect; } From f70ed489f5c9ed138cf766468a2e2fd5b2855f12 Mon Sep 17 00:00:00 2001 From: Mumogg <104529023+Mumogg@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:10:20 -0600 Subject: [PATCH 3/3] xmem_test: the write-combine window's page is written before its view is queried 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 Claude-Session: https://claude.ai/code/session_01HxBQf4pXH8PWpC5oZsM4bt --- runtime/test/xmem_test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index f552c5b6e..bf5bd3638 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp @@ -711,6 +711,8 @@ void test_write_combine_window(const PhysicalApi& api) { const bool mapped = api.map(window, 1, &page) == window; check(mapped, "a page maps into a window reserved with PAGE_WRITECOMBINE"); if (mapped) { + // Physical pages are committed on first touch, so the view is written before it is queried. + *static_cast(window) = 1; MEMORY_BASIC_INFORMATION info{}; VirtualQuery(window, &info, sizeof(info)); check(info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE,