diff options
| author | Gustavo Kissel <kisselgustavo@gmail.com> | 2026-11-23 14:06:59 -0300 |
|---|---|---|
| committer | Gustavo Kissel <kisselgustavo@gmail.com> | 2026-11-23 14:06:59 -0300 |
| commit | 5ba2a7f95fbd6db34a2a3efcbb9602f02b20c917 (patch) | |
| tree | bb9a7cf76cdae3a92668071e3d0c05db546f8eb3 /runtime/shims/xmem.cpp | |
| parent | 203a348cf03d844623284316410de9b5a580140f (diff) | |
xmem: map consecutive physical pages as one view, so a 100,000-page XMemMapPhysicalPages fits the title's hang watchdog under Wine
One view per page cost a placeholder split and a MapViewOfFile3 per 64 KiB page. Under Wine that took
0.36 ms a page and more as views accumulate; the retail boot's 99,776-page call ran past the title's
30-second hang watchdog. Consecutive pages of one allocation now map as one view. A page remapped or
unmapped later splits its view and the neighbours are mapped again from the same section.
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 108 |
1 files changed, 82 insertions, 26 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 37f29459f..43089ad91 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -261,7 +261,16 @@ std::map<uintptr_t, Window> g_windows; // base -> window | |||
| 261 | std::unordered_map<uint64_t, PhysicalPage> g_pages; // page number -> backing | 261 | std::unordered_map<uint64_t, PhysicalPage> g_pages; // page number -> backing |
| 262 | std::vector<uint64_t> g_free_slots; | 262 | std::vector<uint64_t> g_free_slots; |
| 263 | uint64_t g_next_slot = 1; | 263 | uint64_t g_next_slot = 1; |
| 264 | std::unordered_map<uintptr_t, uint64_t> g_mapped; // mapped 64 KiB address -> page number | 264 | // A mapped run: `count` pages of one section at consecutive offsets, mapped as one view at its base. One view per run |
| 265 | // rather than per page: under Wine each view costs a placeholder split and a section map whose time grows with the | ||
| 266 | // number of views, and the retail title maps about 100,000 pages in one call. | ||
| 267 | struct View { | ||
| 268 | uint64_t count; | ||
| 269 | std::shared_ptr<Section> section; // kept for remapping part of the run after its pages are freed | ||
| 270 | uint64_t offset; // section offset of the first page | ||
| 271 | DWORD protect; | ||
| 272 | }; | ||
| 273 | std::map<uintptr_t, View> g_views; // view base -> view | ||
| 265 | 274 | ||
| 266 | // With g_phys_mutex held. A window replaces every window it overlaps: the title released those regions (with | 275 | // 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. | 276 | // kernel32!VirtualFree, which the shim does not see) before the host could hand the addresses out again. |
| @@ -425,18 +434,55 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, | |||
| 425 | return VirtualAlloc(address, size, host_type, host_prot); | 434 | return VirtualAlloc(address, size, host_type, host_prot); |
| 426 | } | 435 | } |
| 427 | 436 | ||
| 428 | // With g_phys_mutex held. Unmaps the page at `address` if one is mapped there; the address becomes a placeholder again. | 437 | // With g_phys_mutex held. Maps `view` at the 64 KiB-aligned `address`, which holds no mapped page. |
| 429 | bool unmap_page(uintptr_t address) { | 438 | bool map_view(uintptr_t address, const View& view) { |
| 430 | const auto it = g_mapped.find(address); | 439 | const SIZE_T size = static_cast<SIZE_T>(view.count * kPhysicalPageSize); |
| 431 | if (it == g_mapped.end()) return true; | 440 | void* const target = reinterpret_cast<void*>(address); |
| 432 | if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(address), | 441 | if (!isolate_placeholder(target, size)) return false; |
| 433 | MEM_PRESERVE_PLACEHOLDER)) { | 442 | if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, view.offset, size, |
| 443 | MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) { | ||
| 434 | return false; | 444 | return false; |
| 435 | } | 445 | } |
| 436 | g_mapped.erase(it); | 446 | g_views.emplace(address, view); |
| 437 | return true; | 447 | return true; |
| 438 | } | 448 | } |
| 439 | 449 | ||
| 450 | // With g_phys_mutex held. Unmaps every page in [start, end); the addresses become placeholders again. A view that | ||
| 451 | // reaches outside the range is unmapped whole, and its pages outside the range are mapped again, so neighbours keep | ||
| 452 | // their pages. | ||
| 453 | bool unmap_range(uintptr_t start, uintptr_t end) { | ||
| 454 | auto it = g_views.upper_bound(start); | ||
| 455 | if (it != g_views.begin()) { | ||
| 456 | const auto prev = std::prev(it); | ||
| 457 | if (prev->first + prev->second.count * kPhysicalPageSize > start) it = prev; | ||
| 458 | } | ||
| 459 | while (it != g_views.end() && it->first < end) { | ||
| 460 | const uintptr_t base = it->first; | ||
| 461 | const View view = it->second; | ||
| 462 | const uintptr_t view_end = base + view.count * kPhysicalPageSize; | ||
| 463 | if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(base), | ||
| 464 | MEM_PRESERVE_PLACEHOLDER)) { | ||
| 465 | return false; | ||
| 466 | } | ||
| 467 | it = g_views.erase(it); | ||
| 468 | if (base < start) { | ||
| 469 | View left = view; | ||
| 470 | left.count = (start - base) / kPhysicalPageSize; | ||
| 471 | if (!map_view(base, left)) return false; | ||
| 472 | } | ||
| 473 | if (view_end > end) { | ||
| 474 | const uint64_t skipped = (end - base) / kPhysicalPageSize; | ||
| 475 | View right = view; | ||
| 476 | right.count = view.count - skipped; | ||
| 477 | right.offset += skipped * kPhysicalPageSize; | ||
| 478 | return map_view(end, right); // the last view the range touches | ||
| 479 | } | ||
| 480 | } | ||
| 481 | return true; | ||
| 482 | } | ||
| 483 | |||
| 484 | bool unmap_page(uintptr_t address) { return unmap_range(address, address + kPhysicalPageSize); } | ||
| 485 | |||
| 440 | // With g_phys_mutex held. Maps physical page `page` (0: unmap) at the 64 KiB-aligned `address`, replacing what was | 486 | // 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. | 487 | // mapped there. The caller has found the window. |
| 442 | bool map_page(uintptr_t address, uint64_t page, DWORD protect) { | 488 | bool map_page(uintptr_t address, uint64_t page, DWORD protect) { |
| @@ -447,14 +493,7 @@ bool map_page(uintptr_t address, uint64_t page, DWORD protect) { | |||
| 447 | SetLastError(ERROR_INVALID_PARAMETER); | 493 | SetLastError(ERROR_INVALID_PARAMETER); |
| 448 | return false; | 494 | return false; |
| 449 | } | 495 | } |
| 450 | void* const target = reinterpret_cast<void*>(address); | 496 | 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 | } | 497 | } |
| 459 | 498 | ||
| 460 | // Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still | 499 | // Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still |
| @@ -556,8 +595,9 @@ BOOL WINAPI XMemFreePhysicalPages(uint64_t page_count, const uint64_t* page_arra | |||
| 556 | // or null to unmap that many (four calls at shutdown). Callers test the 64-bit result for zero, so it is the address on | 595 | // or null to unmap that many (four calls at shutdown). Callers test the 64-bit result for zero, so it is the address on |
| 557 | // success. A page number of 0 in the array unmaps that one page. | 596 | // success. A page number of 0 in the array unmaps that one page. |
| 558 | // | 597 | // |
| 559 | // Each page is its own view (MapViewOfFile3 into a 64 KiB placeholder), so any page can be remapped or unmapped later | 598 | // Consecutive pages of one allocation are mapped as one view (MapViewOfFile3 into a placeholder of their size); a |
| 560 | // without touching its neighbours. A failure part-way leaves the pages before it mapped. | 599 | // page remapped or unmapped later splits its view, and its neighbours stay mapped. The range is unmapped first, so a |
| 600 | // page number that is not a live page fails with the pages before it mapped and the rest of the range unmapped. | ||
| 561 | void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint64_t* page_array) { | 601 | void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint64_t* page_array) { |
| 562 | const uintptr_t start = reinterpret_cast<uintptr_t>(address); | 602 | const uintptr_t start = reinterpret_cast<uintptr_t>(address); |
| 563 | if (!aligned_page_address(start) || page_count > (std::numeric_limits<uintptr_t>::max)() / kPhysicalPageSize || | 603 | if (!aligned_page_address(start) || page_count > (std::numeric_limits<uintptr_t>::max)() / kPhysicalPageSize || |
| @@ -570,20 +610,36 @@ void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint | |||
| 570 | return nullptr; | 610 | return nullptr; |
| 571 | } | 611 | } |
| 572 | const std::lock_guard lock(g_phys_mutex); | 612 | const std::lock_guard lock(g_phys_mutex); |
| 573 | if (!page_array) { | 613 | const uintptr_t end = start + page_count * kPhysicalPageSize; |
| 574 | for (uint64_t i = 0; i < page_count; ++i) { | 614 | if (!page_array) return unmap_range(start, end) ? address : nullptr; |
| 575 | if (!unmap_page(start + i * kPhysicalPageSize)) return nullptr; | ||
| 576 | } | ||
| 577 | return address; | ||
| 578 | } | ||
| 579 | const Window* const window = find_window(start, page_count * kPhysicalPageSize); | 615 | const Window* const window = find_window(start, page_count * kPhysicalPageSize); |
| 580 | if (!window) { | 616 | if (!window) { |
| 581 | SetLastError(ERROR_INVALID_ADDRESS); | 617 | SetLastError(ERROR_INVALID_ADDRESS); |
| 582 | return nullptr; | 618 | return nullptr; |
| 583 | } | 619 | } |
| 584 | const DWORD protect = view_protect(*window); | 620 | const DWORD protect = view_protect(*window); |
| 585 | for (uint64_t i = 0; i < page_count; ++i) { | 621 | if (!unmap_range(start, end)) return nullptr; |
| 586 | if (!map_page(start + i * kPhysicalPageSize, page_array[i], protect)) return nullptr; | 622 | for (uint64_t i = 0; i < page_count;) { |
| 623 | if (page_array[i] == 0) { | ||
| 624 | ++i; | ||
| 625 | continue; | ||
| 626 | } | ||
| 627 | const auto it = g_pages.find(page_array[i]); | ||
| 628 | if (it == g_pages.end()) { | ||
| 629 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 630 | return nullptr; | ||
| 631 | } | ||
| 632 | View view{1, it->second.section, it->second.offset, protect}; | ||
| 633 | while (i + view.count < page_count) { | ||
| 634 | const auto next = g_pages.find(page_array[i + view.count]); | ||
| 635 | if (next == g_pages.end() || next->second.section != view.section || | ||
| 636 | next->second.offset != view.offset + view.count * kPhysicalPageSize) { | ||
| 637 | break; | ||
| 638 | } | ||
| 639 | ++view.count; | ||
| 640 | } | ||
| 641 | if (!map_view(start + i * kPhysicalPageSize, view)) return nullptr; | ||
| 642 | i += view.count; | ||
| 587 | } | 643 | } |
| 588 | return address; | 644 | return address; |
| 589 | } | 645 | } |