diff options
| author | TreXito <81475944+TreXito@users.noreply.github.com> | 2026-11-23 21:14:12 +0000 |
|---|---|---|
| committer | TreXito <81475944+TreXito@users.noreply.github.com> | 2026-11-23 21:14:12 +0000 |
| commit | e3a33db9d0c06f5ce83dd5025d7ffa27cb7b35ff (patch) | |
| tree | 41468a16b694d6b0e8b4c253b18ad003c3790e95 /runtime | |
| parent | 7651b77059ef1b24020a120fd9f682f028ebe989 (diff) | |
| parent | cef56a296ece813ecbac9d430fe90f64a21f2aab (diff) | |
Merge #7584 (xmem: consecutive physical pages as one view) at cef56a296
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/xmem.cpp | 200 | ||||
| -rw-r--r-- | runtime/test/xmem_test.cpp | 126 |
2 files changed, 299 insertions, 27 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 75f927458..97ee60571 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,82 @@ std::map<uintptr_t, Window> g_windows; // base -> window | |||
| 261 | std::unordered_map<uint64_t, PhysicalPage> g_pages; // page number -> backing | 262 | std::unordered_map<uint64_t, PhysicalPage> g_pages; // page number -> backing |
| 262 | std::vector<uint64_t> g_free_slots; | 263 | std::vector<uint64_t> g_free_slots; |
| 263 | uint64_t g_next_slot = 1; | 264 | uint64_t g_next_slot = 1; |
| 264 | std::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. | ||
| 268 | struct 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 | }; | ||
| 274 | std::map<uintptr_t, View> g_views; // view base -> view | ||
| 275 | |||
| 276 | // Splitting a view (unmap_range) unmaps it whole and maps its remainders again, so for that moment pages the caller | ||
| 277 | // did not name are unmapped. On the console they stay mapped, and another thread may be reading them. The span being | ||
| 278 | // split is published here, and a vectored handler makes a thread that faults inside it wait for the remap, then | ||
| 279 | // retries the access. The last finished split stays published too, for a fault the handler sees only after the split | ||
| 280 | // is done; any other access violation goes on to the next handler. | ||
| 281 | // Only the last finished span is kept, so a fault delivered after two later splits is not retried. | ||
| 282 | // A span is one word, so a reader never pairs one split's start with another's end: views are 64 KiB aligned and at | ||
| 283 | // most kMaxViewPages long, so the base keeps its low 16 bits for the page count. 0 is no span. | ||
| 284 | std::atomic<uint64_t> g_split{0}; | ||
| 285 | std::atomic<uint64_t> g_last_split{0}; | ||
| 286 | std::atomic<uint64_t> g_splits{0}; // splits started | ||
| 287 | |||
| 288 | uint64_t pack_span(uintptr_t begin, uintptr_t end) { return begin | (end - begin) / kPhysicalPageSize; } | ||
| 289 | |||
| 290 | bool span_holds(uint64_t span, uintptr_t address) { | ||
| 291 | const uintptr_t begin = span & ~uint64_t{0xffff}; | ||
| 292 | return span && address >= begin && address < begin + (span & 0xffff) * kPhysicalPageSize; | ||
| 293 | } | ||
| 294 | bool g_split_handler = false; // with g_phys_mutex held | ||
| 295 | |||
| 296 | LONG CALLBACK wait_for_split(EXCEPTION_POINTERS* info) { | ||
| 297 | const EXCEPTION_RECORD& record = *info->ExceptionRecord; | ||
| 298 | if (record.ExceptionCode != EXCEPTION_ACCESS_VIOLATION || record.NumberParameters < 2) { | ||
| 299 | return EXCEPTION_CONTINUE_SEARCH; | ||
| 300 | } | ||
| 301 | const ULONG_PTR access = record.ExceptionInformation[0]; // 0 read, 1 write, 8 execute | ||
| 302 | const uintptr_t address = record.ExceptionInformation[1]; | ||
| 303 | if (access != 0 && access != 1) return EXCEPTION_CONTINUE_SEARCH; | ||
| 304 | for (;;) { | ||
| 305 | const uint64_t splits = g_splits.load(std::memory_order_acquire); | ||
| 306 | bool in_split = false; | ||
| 307 | while (span_holds(g_split.load(std::memory_order_acquire), address)) { | ||
| 308 | in_split = true; | ||
| 309 | SwitchToThread(); | ||
| 310 | } | ||
| 311 | in_split = in_split || span_holds(g_last_split.load(std::memory_order_acquire), address); | ||
| 312 | if (!in_split) return EXCEPTION_CONTINUE_SEARCH; | ||
| 313 | // Retry only an access the page now allows: the fault may have come in during a split that has since finished. | ||
| 314 | MEMORY_BASIC_INFORMATION mbi{}; | ||
| 315 | if (VirtualQuery(reinterpret_cast<void*>(address), &mbi, sizeof(mbi)) && mbi.State == MEM_COMMIT && | ||
| 316 | mbi.Type == MEM_MAPPED) { | ||
| 317 | const DWORD protect = mbi.Protect & 0xff; | ||
| 318 | const bool allowed = | ||
| 319 | access == 1 ? protect == PAGE_READWRITE : protect == PAGE_READWRITE || protect == PAGE_READONLY; | ||
| 320 | return allowed ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH; | ||
| 321 | } | ||
| 322 | // Not mapped. A split that started since the wait ended may have unmapped it again: wait for that one too. | ||
| 323 | if (g_splits.load(std::memory_order_acquire) == splits) return EXCEPTION_CONTINUE_SEARCH; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | // With g_phys_mutex held: publishes [begin, end) as being split until it goes out of scope. | ||
| 328 | struct SplitGuard { | ||
| 329 | SplitGuard(uintptr_t begin, uintptr_t end) { | ||
| 330 | if (!g_split_handler) g_split_handler = AddVectoredExceptionHandler(1, &wait_for_split) != nullptr; | ||
| 331 | g_split.store(pack_span(begin, end), std::memory_order_release); | ||
| 332 | g_splits.fetch_add(1, std::memory_order_release); | ||
| 333 | } | ||
| 334 | ~SplitGuard() { | ||
| 335 | g_last_split.store(g_split.load(std::memory_order_relaxed), std::memory_order_release); | ||
| 336 | g_split.store(0, std::memory_order_release); | ||
| 337 | } | ||
| 338 | SplitGuard(const SplitGuard&) = delete; | ||
| 339 | SplitGuard& operator=(const SplitGuard&) = delete; | ||
| 340 | }; | ||
| 265 | 341 | ||
| 266 | // With g_phys_mutex held. A window replaces every window it overlaps: the title released those regions (with | 342 | // 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. | 343 | // kernel32!VirtualFree, which the shim does not see) before the host could hand the addresses out again. |
| @@ -425,18 +501,78 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, | |||
| 425 | return VirtualAlloc(address, size, host_type, host_prot); | 501 | return VirtualAlloc(address, size, host_type, host_prot); |
| 426 | } | 502 | } |
| 427 | 503 | ||
| 428 | // With g_phys_mutex held. Unmaps the page at `address` if one is mapped there; the address becomes a placeholder again. | 504 | // With g_phys_mutex held. Maps `view` at the 64 KiB-aligned `address`, which holds no mapped page. MEM_REPLACE_PLACEHOLDER |
| 429 | bool unmap_page(uintptr_t address) { | 505 | // 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); | 506 | // 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; | 507 | // 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), | 508 | constexpr uint64_t kMaxViewPages = 1024; // 64 MiB |
| 433 | MEM_PRESERVE_PLACEHOLDER)) { | 509 | static_assert(kMaxViewPages < 0x10000, "a split span keeps its page count in 16 bits"); |
| 434 | return false; | 510 | |
| 511 | bool map_view(uintptr_t address, const View& view) { | ||
| 512 | for (uint64_t mapped = 0; mapped < view.count;) { | ||
| 513 | const uintptr_t base = address + mapped * kPhysicalPageSize; | ||
| 514 | void* const target = reinterpret_cast<void*>(base); | ||
| 515 | MEMORY_BASIC_INFORMATION info{}; | ||
| 516 | if (!VirtualQuery(target, &info, sizeof(info)) || info.State != MEM_RESERVE || | ||
| 517 | info.RegionSize < kPhysicalPageSize) { | ||
| 518 | SetLastError(ERROR_INVALID_ADDRESS); | ||
| 519 | return false; | ||
| 520 | } | ||
| 521 | const uint64_t available = info.RegionSize / kPhysicalPageSize; | ||
| 522 | const uint64_t remaining = view.count - mapped; | ||
| 523 | const uint64_t count = (std::min)({available, remaining, kMaxViewPages}); | ||
| 524 | const SIZE_T size = static_cast<SIZE_T>(count * kPhysicalPageSize); | ||
| 525 | const uint64_t offset = view.offset + mapped * kPhysicalPageSize; | ||
| 526 | if (!isolate_placeholder(target, size)) return false; | ||
| 527 | if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, offset, size, | ||
| 528 | MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) { | ||
| 529 | return false; | ||
| 530 | } | ||
| 531 | g_views.emplace(base, View{count, view.section, offset, view.protect}); | ||
| 532 | mapped += count; | ||
| 533 | } | ||
| 534 | return true; | ||
| 535 | } | ||
| 536 | |||
| 537 | // With g_phys_mutex held. Unmaps every page in [start, end); the addresses become placeholders again. A view that | ||
| 538 | // reaches outside the range is unmapped whole, and its pages outside the range are mapped again, so neighbours keep | ||
| 539 | // their pages. | ||
| 540 | // There is no rollback: if mapping a remainder again fails, those neighbouring pages stay unmapped and the caller only | ||
| 541 | // sees the failure. | ||
| 542 | bool unmap_range(uintptr_t start, uintptr_t end) { | ||
| 543 | auto it = g_views.upper_bound(start); | ||
| 544 | if (it != g_views.begin()) { | ||
| 545 | const auto prev = std::prev(it); | ||
| 546 | if (prev->first + prev->second.count * kPhysicalPageSize > start) it = prev; | ||
| 547 | } | ||
| 548 | while (it != g_views.end() && it->first < end) { | ||
| 549 | const uintptr_t base = it->first; | ||
| 550 | const View view = it->second; | ||
| 551 | const uintptr_t view_end = base + view.count * kPhysicalPageSize; | ||
| 552 | const SplitGuard guard(base, view_end); | ||
| 553 | if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(base), | ||
| 554 | MEM_PRESERVE_PLACEHOLDER)) { | ||
| 555 | return false; | ||
| 556 | } | ||
| 557 | it = g_views.erase(it); | ||
| 558 | if (base < start) { | ||
| 559 | View left = view; | ||
| 560 | left.count = (start - base) / kPhysicalPageSize; | ||
| 561 | if (!map_view(base, left)) return false; | ||
| 562 | } | ||
| 563 | if (view_end > end) { | ||
| 564 | const uint64_t skipped = (end - base) / kPhysicalPageSize; | ||
| 565 | View right = view; | ||
| 566 | right.count = view.count - skipped; | ||
| 567 | right.offset += skipped * kPhysicalPageSize; | ||
| 568 | return map_view(end, right); // the last view the range touches | ||
| 569 | } | ||
| 435 | } | 570 | } |
| 436 | g_mapped.erase(it); | ||
| 437 | return true; | 571 | return true; |
| 438 | } | 572 | } |
| 439 | 573 | ||
| 574 | bool unmap_page(uintptr_t address) { return unmap_range(address, address + kPhysicalPageSize); } | ||
| 575 | |||
| 440 | // With g_phys_mutex held. Maps physical page `page` (0: unmap) at the 64 KiB-aligned `address`, replacing what was | 576 | // 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. | 577 | // mapped there. The caller has found the window. |
| 442 | bool map_page(uintptr_t address, uint64_t page, DWORD protect) { | 578 | bool map_page(uintptr_t address, uint64_t page, DWORD protect) { |
| @@ -447,14 +583,7 @@ bool map_page(uintptr_t address, uint64_t page, DWORD protect) { | |||
| 447 | SetLastError(ERROR_INVALID_PARAMETER); | 583 | SetLastError(ERROR_INVALID_PARAMETER); |
| 448 | return false; | 584 | return false; |
| 449 | } | 585 | } |
| 450 | void* const target = reinterpret_cast<void*>(address); | 586 | 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 | } | 587 | } |
| 459 | 588 | ||
| 460 | // Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still | 589 | // Two of the measured windows are reserved PAGE_NOACCESS for the CPU and the GPU alike; pages mapped there are still |
| @@ -561,8 +690,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 | 690 | // 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. | 691 | // success. A page number of 0 in the array unmaps that one page. |
| 563 | // | 692 | // |
| 564 | // Each page is its own view (MapViewOfFile3 into a 64 KiB placeholder), so any page can be remapped or unmapped later | 693 | // 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. | 694 | // page remapped or unmapped later splits its view, and its neighbours stay mapped. The range is unmapped first, so a |
| 695 | // page number that is not a live page fails with the pages before it mapped and the rest of the range unmapped. | ||
| 566 | void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint64_t* page_array) { | 696 | void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint64_t* page_array) { |
| 567 | const uintptr_t start = reinterpret_cast<uintptr_t>(address); | 697 | const uintptr_t start = reinterpret_cast<uintptr_t>(address); |
| 568 | if (!aligned_page_address(start) || page_count > (std::numeric_limits<uintptr_t>::max)() / kPhysicalPageSize || | 698 | if (!aligned_page_address(start) || page_count > (std::numeric_limits<uintptr_t>::max)() / kPhysicalPageSize || |
| @@ -575,20 +705,36 @@ void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint | |||
| 575 | return nullptr; | 705 | return nullptr; |
| 576 | } | 706 | } |
| 577 | const std::lock_guard lock(g_phys_mutex); | 707 | const std::lock_guard lock(g_phys_mutex); |
| 578 | if (!page_array) { | 708 | const uintptr_t end = start + page_count * kPhysicalPageSize; |
| 579 | for (uint64_t i = 0; i < page_count; ++i) { | 709 | 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); | 710 | const Window* const window = find_window(start, page_count * kPhysicalPageSize); |
| 585 | if (!window) { | 711 | if (!window) { |
| 586 | SetLastError(ERROR_INVALID_ADDRESS); | 712 | SetLastError(ERROR_INVALID_ADDRESS); |
| 587 | return nullptr; | 713 | return nullptr; |
| 588 | } | 714 | } |
| 589 | const DWORD protect = view_protect(*window); | 715 | const DWORD protect = view_protect(*window); |
| 590 | for (uint64_t i = 0; i < page_count; ++i) { | 716 | if (!unmap_range(start, end)) return nullptr; |
| 591 | if (!map_page(start + i * kPhysicalPageSize, page_array[i], protect)) return nullptr; | 717 | for (uint64_t i = 0; i < page_count;) { |
| 718 | if (page_array[i] == 0) { | ||
| 719 | ++i; | ||
| 720 | continue; | ||
| 721 | } | ||
| 722 | const auto it = g_pages.find(page_array[i]); | ||
| 723 | if (it == g_pages.end()) { | ||
| 724 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 725 | return nullptr; | ||
| 726 | } | ||
| 727 | View view{1, it->second.section, it->second.offset, protect}; | ||
| 728 | while (i + view.count < page_count) { | ||
| 729 | const auto next = g_pages.find(page_array[i + view.count]); | ||
| 730 | if (next == g_pages.end() || next->second.section != view.section || | ||
| 731 | next->second.offset != view.offset + view.count * kPhysicalPageSize) { | ||
| 732 | break; | ||
| 733 | } | ||
| 734 | ++view.count; | ||
| 735 | } | ||
| 736 | if (!map_view(start + i * kPhysicalPageSize, view)) return nullptr; | ||
| 737 | i += view.count; | ||
| 592 | } | 738 | } |
| 593 | return address; | 739 | return address; |
| 594 | } | 740 | } |
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 53df7fcc5..87359ca67 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | #include <windows.h> | 11 | #include <windows.h> |
| 12 | #include <psapi.h> | 12 | #include <psapi.h> |
| 13 | 13 | ||
| 14 | #include <atomic> | ||
| 14 | #include <cstdint> | 15 | #include <cstdint> |
| 15 | #include <cstdio> | 16 | #include <cstdio> |
| 16 | #include <cstring> | 17 | #include <cstring> |
| @@ -18,6 +19,7 @@ | |||
| 18 | #include <limits> | 19 | #include <limits> |
| 19 | #include <sstream> | 20 | #include <sstream> |
| 20 | #include <string> | 21 | #include <string> |
| 22 | #include <thread> | ||
| 21 | 23 | ||
| 22 | #include "shims.h" | 24 | #include "shims.h" |
| 23 | #include "startup_trace.h" | 25 | #include "startup_trace.h" |
| @@ -369,10 +371,27 @@ void test_physical_pages(const PhysicalApi& api) { | |||
| 369 | VirtualQuery(window, &info, sizeof(info)); | 371 | VirtualQuery(window, &info, sizeof(info)); |
| 370 | check(info.State == MEM_COMMIT && info.Type == MEM_MAPPED && info.Protect == PAGE_READWRITE, | 372 | check(info.State == MEM_COMMIT && info.Type == MEM_MAPPED && info.Protect == PAGE_READWRITE, |
| 371 | "a mapped page is a read-write view with the window's protection"); | 373 | "a mapped page is a read-write view with the window's protection"); |
| 374 | check(info.AllocationBase == window && info.RegionSize == kPages * kPage64k, | ||
| 375 | "consecutive pages of one allocation are mapped as one view"); | ||
| 372 | } | 376 | } |
| 373 | 377 | ||
| 378 | // A page unmapped or remapped inside that view splits it; its neighbours keep their pages. | ||
| 379 | void* const middle = window + kPage64k; | ||
| 380 | check(api.scatter(&middle, 1, nullptr) == TRUE && reserved_only(window + kPage64k), | ||
| 381 | "a page inside a view unmaps on its own"); | ||
| 382 | check(page_holds(window, 0x1000u) && page_holds(window + 2 * kPage64k, 0x3000u) && | ||
| 383 | page_holds(window + 3 * kPage64k, 0x4000u), | ||
| 384 | "the pages around it stay mapped with their contents"); | ||
| 385 | check(api.map(middle, 1, &pages[1]) == middle && page_holds(window + kPage64k, 0x2000u), | ||
| 386 | "a page maps again into the gap"); | ||
| 387 | |||
| 374 | check(api.map(window, kPages, nullptr) == window, "XMemMapPhysicalPages with no page array unmaps"); | 388 | check(api.map(window, kPages, nullptr) == window, "XMemMapPhysicalPages with no page array unmaps"); |
| 375 | check(reserved_only(window) && reserved_only(window + 3 * kPage64k), "unmapped pages are reserved again"); | 389 | check(reserved_only(window) && reserved_only(window + 3 * kPage64k), "unmapped pages are reserved again"); |
| 390 | // The split above left the range as several host placeholders; the whole run maps again over them. | ||
| 391 | check(api.map(window, kPages, pages) == window && page_holds(window, 0x1000u) && | ||
| 392 | page_holds(window + 3 * kPage64k, 0x4000u), | ||
| 393 | "a run maps again over a range an earlier partial unmap split"); | ||
| 394 | check(api.map(window, kPages, nullptr) == window && reserved_only(window), "and unmaps again"); | ||
| 376 | 395 | ||
| 377 | // Remap in reverse order into the other window: each page keeps its contents. | 396 | // Remap in reverse order into the other window: each page keeps its contents. |
| 378 | uint64_t reversed[kPages]; | 397 | uint64_t reversed[kPages]; |
| @@ -440,6 +459,111 @@ void test_physical_pages(const PhysicalApi& api) { | |||
| 440 | // The windows were split into one placeholder per page; the process exit releases them. | 459 | // The windows were split into one placeholder per page; the process exit releases them. |
| 441 | } | 460 | } |
| 442 | 461 | ||
| 462 | // True when a read fault raised for `address` reaches this handler rather than being retried by the split handler. | ||
| 463 | bool read_fault_reaches_caller(const void* address) { | ||
| 464 | const ULONG_PTR info[2] = {0, reinterpret_cast<ULONG_PTR>(address)}; | ||
| 465 | __try { | ||
| 466 | RaiseException(EXCEPTION_ACCESS_VIOLATION, 0, 2, info); | ||
| 467 | } __except (EXCEPTION_EXECUTE_HANDLER) { | ||
| 468 | return true; | ||
| 469 | } | ||
| 470 | return false; | ||
| 471 | } | ||
| 472 | |||
| 473 | // Splitting a view briefly unmaps the view's other pages; a thread reading one of them meanwhile must not fault. | ||
| 474 | // Unmapping page k of the view [k, end) splits it every time, and the last page of each view is the one being read. | ||
| 475 | void test_split_under_reader(const PhysicalApi& api) { | ||
| 476 | constexpr uint64_t kPages = 2048; // two capped views | ||
| 477 | auto* const window = static_cast<uint8_t*>( | ||
| 478 | api.virtual_alloc(nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE)); | ||
| 479 | std::vector<uint64_t> pages(kPages); | ||
| 480 | uint64_t count = kPages; | ||
| 481 | check(window && api.allocate(0x280, &count, pages.data()) == TRUE && count == kPages && | ||
| 482 | api.map(window, kPages, pages.data()) == window, | ||
| 483 | "a 2,048-page run maps for the split test"); | ||
| 484 | if (!window || count != kPages) return; | ||
| 485 | uint8_t* const read_pages[2] = {window + 1023 * kPage64k, window + 2047 * kPage64k}; | ||
| 486 | fill_page(read_pages[0], 0x11000u); | ||
| 487 | fill_page(read_pages[1], 0x11000u); | ||
| 488 | std::atomic<bool> done{false}; | ||
| 489 | std::atomic<uint64_t> reads{0}, wrong{0}; | ||
| 490 | std::thread reader([&] { | ||
| 491 | for (uint64_t i = 0; !done.load(std::memory_order_relaxed); ++i) { | ||
| 492 | wrong += read_u32(read_pages[i & 1], 0) != 0x11000u; | ||
| 493 | ++reads; | ||
| 494 | } | ||
| 495 | }); | ||
| 496 | bool unmapped = true; | ||
| 497 | for (uint64_t k = 0; k < kPages - 1; ++k) { | ||
| 498 | if (k == 1023) continue; | ||
| 499 | void* const page = window + k * kPage64k; | ||
| 500 | unmapped &= api.scatter(&page, 1, nullptr) == TRUE; | ||
| 501 | } | ||
| 502 | done = true; | ||
| 503 | reader.join(); | ||
| 504 | check(unmapped, "2,046 pages unmap one at a time, each splitting the view a reader is in"); | ||
| 505 | check(reads.load() > 0 && wrong.load() == 0, "the reader of the views' last pages never faults or misreads"); | ||
| 506 | // The last split was of [2046, 2048): a fault there is retried, one on another mapped page is not. | ||
| 507 | check(!read_fault_reaches_caller(read_pages[1]), "a read fault inside the last split span is retried"); | ||
| 508 | check(read_fault_reaches_caller(read_pages[0]), "a read fault on a mapped page outside it reaches the caller"); | ||
| 509 | api.map(window, kPages, nullptr); | ||
| 510 | api.free_pages(kPages, pages.data()); | ||
| 511 | } | ||
| 512 | |||
| 513 | // A run longer than one host view is mapped as capped views, so unmapping one page inside it only remaps the rest of | ||
| 514 | // that view; every other page keeps its contents. | ||
| 515 | void test_long_run(const PhysicalApi& api) { | ||
| 516 | constexpr uint64_t kPages = 1100; // more than the 1024-page view cap | ||
| 517 | auto* const window = static_cast<uint8_t*>( | ||
| 518 | api.virtual_alloc(nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE)); | ||
| 519 | std::vector<uint64_t> pages(kPages); | ||
| 520 | uint64_t count = kPages; | ||
| 521 | check(window && api.allocate(0x280, &count, pages.data()) == TRUE, "a long run's window and pages are allocated"); | ||
| 522 | if (!window || count != kPages) return; | ||
| 523 | check(api.map(window, kPages, pages.data()) == window, "a run longer than the view cap maps"); | ||
| 524 | MEMORY_BASIC_INFORMATION info{}; | ||
| 525 | VirtualQuery(window, &info, sizeof(info)); | ||
| 526 | check(info.State == MEM_COMMIT && info.RegionSize == 1024 * kPage64k, "it is mapped as views of at most 1024 pages"); | ||
| 527 | for (uint64_t i = 0; i < kPages; ++i) fill_page(window + i * kPage64k, static_cast<uint32_t>(i + 1)); | ||
| 528 | void* const hole = window + 10 * kPage64k; | ||
| 529 | check(api.scatter(&hole, 1, nullptr) == TRUE && reserved_only(hole), "a page inside the first view unmaps"); | ||
| 530 | bool kept = true; | ||
| 531 | for (uint64_t i = 0; i < kPages; ++i) { | ||
| 532 | if (i != 10) kept &= page_holds(window + i * kPage64k, static_cast<uint32_t>(i + 1)); | ||
| 533 | } | ||
| 534 | check(kept, "every other page of the run keeps its contents"); | ||
| 535 | // Several pages across the boundary of two capped views unmap together; the pages on both sides stay. | ||
| 536 | check(api.map(window + 1000 * kPage64k, 50, nullptr) == window + 1000 * kPage64k && | ||
| 537 | reserved_only(window + 1000 * kPage64k) && reserved_only(window + 1049 * kPage64k), | ||
| 538 | "a multi-page range across two views unmaps"); | ||
| 539 | check(page_holds(window + 999 * kPage64k, 1000u) && page_holds(window + 1050 * kPage64k, 1051u), | ||
| 540 | "the pages on both sides of it keep their contents"); | ||
| 541 | // Mapping other pages over the middle of a live run replaces just those pages. | ||
| 542 | check(api.map(window + 500 * kPage64k, 3, &pages[600]) == window + 500 * kPage64k && | ||
| 543 | page_holds(window + 500 * kPage64k, 601u) && page_holds(window + 502 * kPage64k, 603u), | ||
| 544 | "pages mapped over the middle of a live run replace it there"); | ||
| 545 | check(page_holds(window + 499 * kPage64k, 500u) && page_holds(window + 503 * kPage64k, 504u), | ||
| 546 | "and the run's pages around them keep their contents"); | ||
| 547 | check(api.map(window, kPages, nullptr) == window && reserved_only(window) && | ||
| 548 | reserved_only(window + (kPages - 1) * kPage64k), | ||
| 549 | "the whole run unmaps"); | ||
| 550 | check(api.free_pages(kPages, pages.data()) == TRUE, "the long run's pages are freed"); | ||
| 551 | check(api.map(window, 1, pages.data()) == nullptr && reserved_only(window), "a freed page does not map again"); | ||
| 552 | // Pages freed while still mapped keep their memory, and splitting their view later still maps the rest again. | ||
| 553 | count = 4; | ||
| 554 | check(api.allocate(0x280, &count, pages.data()) == TRUE && count == 4 && api.map(window, 4, pages.data()) == window, | ||
| 555 | "four pages map as one view"); | ||
| 556 | if (count != 4) return; | ||
| 557 | for (uint64_t i = 0; i < 4; ++i) fill_page(window + i * kPage64k, static_cast<uint32_t>(i + 1)); | ||
| 558 | check(api.free_pages(4, pages.data()) == TRUE, "they are freed while mapped"); | ||
| 559 | void* const second = window + kPage64k; | ||
| 560 | check(api.scatter(&second, 1, nullptr) == TRUE && !reserved_only(window) && | ||
| 561 | !reserved_only(window + 2 * kPage64k) && page_holds(window, 1u) && | ||
| 562 | page_holds(window + 2 * kPage64k, 3u) && page_holds(window + 3 * kPage64k, 4u), | ||
| 563 | "unmapping one of them afterwards keeps the others' memory"); | ||
| 564 | api.map(window, 4, nullptr); | ||
| 565 | } | ||
| 566 | |||
| 443 | // The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it. | 567 | // The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it. |
| 444 | void test_placeholders(const PhysicalApi& api) { | 568 | void test_placeholders(const PhysicalApi& api) { |
| 445 | auto* const base = static_cast<uint8_t*>( | 569 | auto* const base = static_cast<uint8_t*>( |
| @@ -746,6 +870,8 @@ int main() { | |||
| 746 | if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)"); | 870 | if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)"); |
| 747 | if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) { | 871 | if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) { |
| 748 | test_physical_pages(physical); | 872 | test_physical_pages(physical); |
| 873 | test_long_run(physical); | ||
| 874 | test_split_under_reader(physical); | ||
| 749 | test_placeholders(physical); | 875 | test_placeholders(physical); |
| 750 | test_write_combine_window(physical); | 876 | test_write_combine_window(physical); |
| 751 | } | 877 | } |