upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorubiquitarianism <283527159+ubiquitarianism@users.noreply.github.com>2026-11-23 23:31:15 -0500
committerGitHub <noreply@github.com>2026-11-23 23:31:15 -0500
commitd2cd6c0d970083eee4d7b40daabb2480a77b1790 (patch)
tree19bd172484af8e935feaf4fcf892e3e151b3147f /runtime
parentf65decc927b65524f98c3733c2f6684fd970df7a (diff)
parentf70ed489f5c9ed138cf766468a2e2fd5b2855f12 (diff)
Merge pull request #328 from Mumogg/xmem-view-protect
xmem: map physical pages without a cache modifier in the view protection, again (RT-XMEM-VIEW-PROTECT)
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/xmem.cpp7
-rw-r--r--runtime/test/xmem_test.cpp31
2 files changed, 37 insertions, 1 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 1e286d712..a0335a237 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -554,7 +554,12 @@ bool install_physical_page_fault_handler() {
554// Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still 554// Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still
555// GPU memory, which the D3D12 layer reads on the CPU. 555// GPU memory, which the D3D12 layer reads on the CPU.
556DWORD view_protect(const Window& window) { 556DWORD view_protect(const Window& window) {
557 return window.protect == PAGE_NOACCESS ? PAGE_READWRITE : window.protect; 557 // A view takes no cache modifier. Three of the measured windows carry PAGE_WRITECOMBINE (0x400) next to
558 // PAGE_READWRITE: Wine's MapViewOfFile3 refuses it, and PAGE_NOCACHE, with ERROR_INVALID_PARAMETER, and on Windows
559 // the view comes out write-combined, which an import of those pages as a GPU buffer refuses (#7218). PAGE_GUARD
560 // is an access alarm, not a cache modifier, so it is left as the window has it.
561 const DWORD protect = window.protect & ~static_cast<DWORD>(PAGE_WRITECOMBINE | PAGE_NOCACHE);
562 return protect == PAGE_NOACCESS ? PAGE_READWRITE : protect;
558} 563}
559 564
560bool aligned_page_address(uintptr_t address) { return (address & (kPhysicalPageSize - 1)) == 0; } 565bool 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..bf5bd3638 100644
--- a/runtime/test/xmem_test.cpp
+++ b/runtime/test/xmem_test.cpp
@@ -694,6 +694,36 @@ void test_placeholders(const PhysicalApi& api) {
694 check(reserved_only(base), "the rest of the placeholder is still reserved"); 694 check(reserved_only(base), "the rest of the placeholder is still reserved");
695} 695}
696 696
697// Three of the retail executable's five physical-page windows carry 0x400 next to PAGE_READWRITE. That is
698// PAGE_WRITECOMBINE on the PC, outside the PAGE_GRAPHICS_* range, and MapViewOfFile3 refuses a view with it, so the
699// mapped pages take the window's protection without the cache modifier.
700void test_write_combine_window(const PhysicalApi& api) {
701 auto* const window = static_cast<uint8_t*>(
702 api.virtual_alloc(nullptr, kPage64k, MEM_RESERVE | kXmem64kPages, 0x27,
703 PAGE_READWRITE | PAGE_WRITECOMBINE | PAGE_GRAPHICS_READWRITE));
704 check(window != nullptr, "XMemVirtualAlloc reserves a window with PAGE_WRITECOMBINE in its protection");
705 if (!window) return;
706
707 uint64_t page = 0;
708 uint64_t count = 1;
709 check(api.allocate(0x280, &count, &page) == TRUE && count == 1, "allocate a page for the write-combine window");
710 SetLastError(ERROR_SUCCESS);
711 const bool mapped = api.map(window, 1, &page) == window;
712 check(mapped, "a page maps into a window reserved with PAGE_WRITECOMBINE");
713 if (mapped) {
714 // Physical pages are committed on first touch, so the view is written before it is queried.
715 *static_cast<volatile unsigned char*>(window) = 1;
716 MEMORY_BASIC_INFORMATION info{};
717 VirtualQuery(window, &info, sizeof(info));
718 check(info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE,
719 "the page mapped there is a plain read-write view");
720 fill_page(window, 0x77000000u);
721 check(page_holds(window, 0x77000000u), "the page in the write-combine window holds data");
722 }
723 check(api.map(window, 1, nullptr) == window, "unmap the write-combine window");
724 check(api.free_pages(1, &page) == TRUE, "free the write-combine window's page");
725}
726
697// gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off. 727// gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off.
698void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem, XMemVirtualAllocFn virtual_alloc) { 728void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem, XMemVirtualAllocFn virtual_alloc) {
699 wchar_t temp[MAX_PATH]; 729 wchar_t temp[MAX_PATH];
@@ -942,6 +972,7 @@ int main() {
942 test_commit_physical_pages(physical); 972 test_commit_physical_pages(physical);
943 test_first_touch_during_remap(physical); 973 test_first_touch_during_remap(physical);
944 test_placeholders(physical); 974 test_placeholders(physical);
975 test_write_combine_window(physical);
945 } 976 }
946 977
947 print_result(); 978 print_result();