upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/xmem.cpp
diff options
context:
space:
mode:
authorMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 13:34:47 -0600
committerMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 13:34:47 -0600
commitced361512405814bbdfcfb08a9b7cda11b6d0f7a (patch)
tree4eb8877e666a3d2370deae2ba47898f47dfa0c48 /runtime/shims/xmem.cpp
parentd2b4538c4c6129bb2472bbcb78343ae555c1cfae (diff)
parent2cf0ff8b10d00e2753037e9f141725f14850004c (diff)
local: merge #7584 (docs conflict: ours)
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp130
1 files changed, 103 insertions, 27 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 75f927458..e25bf4d4e 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -18,6 +18,7 @@
18#include <malloc.h> 18#include <malloc.h>
19#include <psapi.h> 19#include <psapi.h>
20 20
21#include <algorithm>
21#include <atomic> 22#include <atomic>
22#include <cstdint> 23#include <cstdint>
23#include <cstdio> 24#include <cstdio>
@@ -261,7 +262,16 @@ std::map<uintptr_t, Window> g_windows; // base -> window
261std::unordered_map<uint64_t, PhysicalPage> g_pages; // page number -> backing 262std::unordered_map<uint64_t, PhysicalPage> g_pages; // page number -> backing
262std::vector<uint64_t> g_free_slots; 263std::vector<uint64_t> g_free_slots;
263uint64_t g_next_slot = 1; 264uint64_t g_next_slot = 1;
264std::unordered_map<uintptr_t, uint64_t> g_mapped; // mapped 64 KiB address -> page number 265// A mapped run: `count` pages of one section at consecutive offsets, mapped as one view at its base. One view per run
266// rather than per page: under Wine each view costs a placeholder split and a section map whose time grows with the
267// number of views, and the retail title maps about 100,000 pages in one call.
268struct View {
269 uint64_t count;
270 std::shared_ptr<Section> section; // kept for remapping part of the run after its pages are freed
271 uint64_t offset; // section offset of the first page
272 DWORD protect;
273};
274std::map<uintptr_t, View> g_views; // view base -> view
265 275
266// With g_phys_mutex held. A window replaces every window it overlaps: the title released those regions (with 276// With g_phys_mutex held. A window replaces every window it overlaps: the title released those regions (with
267// kernel32!VirtualFree, which the shim does not see) before the host could hand the addresses out again. 277// kernel32!VirtualFree, which the shim does not see) before the host could hand the addresses out again.
@@ -425,18 +435,74 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type,
425 return VirtualAlloc(address, size, host_type, host_prot); 435 return VirtualAlloc(address, size, host_type, host_prot);
426} 436}
427 437
428// With g_phys_mutex held. Unmaps the page at `address` if one is mapped there; the address becomes a placeholder again. 438// With g_phys_mutex held. Maps `view` at the 64 KiB-aligned `address`, which holds no mapped page. MEM_REPLACE_PLACEHOLDER
429bool unmap_page(uintptr_t address) { 439// needs the view to fill one placeholder exactly, and earlier partial unmaps can leave several host placeholders in one
430 const auto it = g_mapped.find(address); 440// run, so the run is mapped as one view per placeholder it spans. Views are capped at kMaxViewPages so that unmapping a
431 if (it == g_mapped.end()) return true; 441// page inside a long run only remaps the rest of one capped view, not the whole run.
432 if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(address), 442constexpr uint64_t kMaxViewPages = 1024; // 64 MiB
433 MEM_PRESERVE_PLACEHOLDER)) { 443
434 return false; 444bool map_view(uintptr_t address, const View& view) {
445 for (uint64_t mapped = 0; mapped < view.count;) {
446 const uintptr_t base = address + mapped * kPhysicalPageSize;
447 void* const target = reinterpret_cast<void*>(base);
448 MEMORY_BASIC_INFORMATION info{};
449 if (!VirtualQuery(target, &info, sizeof(info)) || info.State != MEM_RESERVE ||
450 info.RegionSize < kPhysicalPageSize) {
451 SetLastError(ERROR_INVALID_ADDRESS);
452 return false;
453 }
454 const uint64_t available = info.RegionSize / kPhysicalPageSize;
455 const uint64_t remaining = view.count - mapped;
456 const uint64_t count = (std::min)({available, remaining, kMaxViewPages});
457 const SIZE_T size = static_cast<SIZE_T>(count * kPhysicalPageSize);
458 const uint64_t offset = view.offset + mapped * kPhysicalPageSize;
459 if (!isolate_placeholder(target, size)) return false;
460 if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, offset, size,
461 MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) {
462 return false;
463 }
464 g_views.emplace(base, View{count, view.section, offset, view.protect});
465 mapped += count;
466 }
467 return true;
468}
469
470// With g_phys_mutex held. Unmaps every page in [start, end); the addresses become placeholders again. A view that
471// reaches outside the range is unmapped whole, and its pages outside the range are mapped again, so neighbours keep
472// their pages.
473bool unmap_range(uintptr_t start, uintptr_t end) {
474 auto it = g_views.upper_bound(start);
475 if (it != g_views.begin()) {
476 const auto prev = std::prev(it);
477 if (prev->first + prev->second.count * kPhysicalPageSize > start) it = prev;
478 }
479 while (it != g_views.end() && it->first < end) {
480 const uintptr_t base = it->first;
481 const View view = it->second;
482 const uintptr_t view_end = base + view.count * kPhysicalPageSize;
483 if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(base),
484 MEM_PRESERVE_PLACEHOLDER)) {
485 return false;
486 }
487 it = g_views.erase(it);
488 if (base < start) {
489 View left = view;
490 left.count = (start - base) / kPhysicalPageSize;
491 if (!map_view(base, left)) return false;
492 }
493 if (view_end > end) {
494 const uint64_t skipped = (end - base) / kPhysicalPageSize;
495 View right = view;
496 right.count = view.count - skipped;
497 right.offset += skipped * kPhysicalPageSize;
498 return map_view(end, right); // the last view the range touches
499 }
435 } 500 }
436 g_mapped.erase(it);
437 return true; 501 return true;
438} 502}
439 503
504bool unmap_page(uintptr_t address) { return unmap_range(address, address + kPhysicalPageSize); }
505
440// With g_phys_mutex held. Maps physical page `page` (0: unmap) at the 64 KiB-aligned `address`, replacing what was 506// With g_phys_mutex held. Maps physical page `page` (0: unmap) at the 64 KiB-aligned `address`, replacing what was
441// mapped there. The caller has found the window. 507// mapped there. The caller has found the window.
442bool map_page(uintptr_t address, uint64_t page, DWORD protect) { 508bool map_page(uintptr_t address, uint64_t page, DWORD protect) {
@@ -447,14 +513,7 @@ bool map_page(uintptr_t address, uint64_t page, DWORD protect) {
447 SetLastError(ERROR_INVALID_PARAMETER); 513 SetLastError(ERROR_INVALID_PARAMETER);
448 return false; 514 return false;
449 } 515 }
450 void* const target = reinterpret_cast<void*>(address); 516 return map_view(address, View{1, it->second.section, it->second.offset, protect});
451 if (!isolate_placeholder(target, kPhysicalPageSize)) return false;
452 void* const view = placeholder_api().map_view3(it->second.section->handle, GetCurrentProcess(), target,
453 it->second.offset, kPhysicalPageSize, MEM_REPLACE_PLACEHOLDER,
454 protect, nullptr, 0);
455 if (!view) return false;
456 g_mapped[address] = page;
457 return true;
458} 517}
459 518
460// Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still 519// Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still
@@ -561,8 +620,9 @@ BOOL WINAPI XMemFreePhysicalPages(uint64_t page_count, const uint64_t* page_arra
561// or null to unmap that many (four calls at shutdown). Callers test the 64-bit result for zero, so it is the address on 620// or null to unmap that many (four calls at shutdown). Callers test the 64-bit result for zero, so it is the address on
562// success. A page number of 0 in the array unmaps that one page. 621// success. A page number of 0 in the array unmaps that one page.
563// 622//
564// Each page is its own view (MapViewOfFile3 into a 64 KiB placeholder), so any page can be remapped or unmapped later 623// Consecutive pages of one allocation are mapped as one view (MapViewOfFile3 into a placeholder of their size); a
565// without touching its neighbours. A failure part-way leaves the pages before it mapped. 624// page remapped or unmapped later splits its view, and its neighbours stay mapped. The range is unmapped first, so a
625// page number that is not a live page fails with the pages before it mapped and the rest of the range unmapped.
566void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint64_t* page_array) { 626void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint64_t* page_array) {
567 const uintptr_t start = reinterpret_cast<uintptr_t>(address); 627 const uintptr_t start = reinterpret_cast<uintptr_t>(address);
568 if (!aligned_page_address(start) || page_count > (std::numeric_limits<uintptr_t>::max)() / kPhysicalPageSize || 628 if (!aligned_page_address(start) || page_count > (std::numeric_limits<uintptr_t>::max)() / kPhysicalPageSize ||
@@ -575,20 +635,36 @@ void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint
575 return nullptr; 635 return nullptr;
576 } 636 }
577 const std::lock_guard lock(g_phys_mutex); 637 const std::lock_guard lock(g_phys_mutex);
578 if (!page_array) { 638 const uintptr_t end = start + page_count * kPhysicalPageSize;
579 for (uint64_t i = 0; i < page_count; ++i) { 639 if (!page_array) return unmap_range(start, end) ? address : nullptr;
580 if (!unmap_page(start + i * kPhysicalPageSize)) return nullptr;
581 }
582 return address;
583 }
584 const Window* const window = find_window(start, page_count * kPhysicalPageSize); 640 const Window* const window = find_window(start, page_count * kPhysicalPageSize);
585 if (!window) { 641 if (!window) {
586 SetLastError(ERROR_INVALID_ADDRESS); 642 SetLastError(ERROR_INVALID_ADDRESS);
587 return nullptr; 643 return nullptr;
588 } 644 }
589 const DWORD protect = view_protect(*window); 645 const DWORD protect = view_protect(*window);
590 for (uint64_t i = 0; i < page_count; ++i) { 646 if (!unmap_range(start, end)) return nullptr;
591 if (!map_page(start + i * kPhysicalPageSize, page_array[i], protect)) return nullptr; 647 for (uint64_t i = 0; i < page_count;) {
648 if (page_array[i] == 0) {
649 ++i;
650 continue;
651 }
652 const auto it = g_pages.find(page_array[i]);
653 if (it == g_pages.end()) {
654 SetLastError(ERROR_INVALID_PARAMETER);
655 return nullptr;
656 }
657 View view{1, it->second.section, it->second.offset, protect};
658 while (i + view.count < page_count) {
659 const auto next = g_pages.find(page_array[i + view.count]);
660 if (next == g_pages.end() || next->second.section != view.section ||
661 next->second.offset != view.offset + view.count * kPhysicalPageSize) {
662 break;
663 }
664 ++view.count;
665 }
666 if (!map_view(start + i * kPhysicalPageSize, view)) return nullptr;
667 i += view.count;
592 } 668 }
593 return address; 669 return address;
594} 670}