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.
This commit is contained in:
hcristosm
2026-11-23 13:50:59 -03:00
parent 37f2151079
commit 7e8e3c1db1
2 changed files with 33 additions and 1 deletions

View File

@ -460,7 +460,10 @@ bool map_page(uintptr_t address, uint64_t page, DWORD protect) {
// 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<DWORD>(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; }

View File

@ -474,6 +474,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<uint8_t*>(
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];
@ -719,6 +747,7 @@ int main() {
if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) {
test_physical_pages(physical);
test_placeholders(physical);
test_write_combine_window(physical);
}
print_result();