diff options
Diffstat (limited to 'runtime/test/xmem_test.cpp')
| -rw-r--r-- | runtime/test/xmem_test.cpp | 120 |
1 files changed, 119 insertions, 1 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 991388c7f..adc24cca3 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp | |||
| @@ -31,6 +31,7 @@ | |||
| 31 | #include "startup_trace.h" | 31 | #include "startup_trace.h" |
| 32 | #include "xmem.h" | 32 | #include "xmem.h" |
| 33 | #include "../src/title_page_watch.h" | 33 | #include "../src/title_page_watch.h" |
| 34 | #include "../src/title_geometry_watch.h" | ||
| 34 | 35 | ||
| 35 | // The process heap as test_heap_lock_faults needs it (the same gate as d3d12_existing_heap_test's): an allocation that | 36 | // The process heap as test_heap_lock_faults needs it (the same gate as d3d12_existing_heap_test's): an allocation that |
| 36 | // waits while a thread holds the heap's lock. The NT heap's low-fragmentation front end serves most small sizes | 37 | // waits while a thread holds the heap's lock. The NT heap's low-fragmentation front end serves most small sizes |
| @@ -429,6 +430,78 @@ struct PhysicalApi { | |||
| 429 | XMemMapPhysicalPagesScatterFn scatter; | 430 | XMemMapPhysicalPagesScatterFn scatter; |
| 430 | }; | 431 | }; |
| 431 | 432 | ||
| 433 | // A remap that gives watched pages another frame reports them released (geometry_writes::note_released), so the | ||
| 434 | // geometry copies of what they held are retired; a page mapped again to the frame it had is no change. Through | ||
| 435 | // XMemMapPhysicalPages, the scatter call and CopyPageMappingsBatchX. | ||
| 436 | void test_remap_releases(const PhysicalApi& api) { | ||
| 437 | namespace watch = leonida::geometry_writes; | ||
| 438 | constexpr uint64_t kPages = 3; | ||
| 439 | auto* const window = static_cast<uint8_t*>(api.virtual_alloc( | ||
| 440 | nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE)); | ||
| 441 | uint64_t pages[4]{}; | ||
| 442 | uint64_t count = 4; | ||
| 443 | if (!window || api.allocate(0x280, &count, pages) != TRUE || count != 4) { | ||
| 444 | check(false, "remap releases: a window and four physical pages"); | ||
| 445 | return; | ||
| 446 | } | ||
| 447 | const auto base = reinterpret_cast<uintptr_t>(window); | ||
| 448 | watch::ReleaseQueue queue; | ||
| 449 | watch::subscribe(queue); | ||
| 450 | for (uint64_t i = 0; i < kPages; ++i) watch::acquire(base + i * kPage64k); // as a geometry copy over the pages | ||
| 451 | std::vector<std::pair<uintptr_t, uintptr_t>> released; | ||
| 452 | const auto take = [&] { | ||
| 453 | watch::take_released(queue, released); | ||
| 454 | return released; | ||
| 455 | }; | ||
| 456 | const auto covers = [&](uintptr_t from, uintptr_t to) { | ||
| 457 | for (const auto& [lo, hi] : released) | ||
| 458 | if (lo <= from && hi >= to) return true; | ||
| 459 | return false; | ||
| 460 | }; | ||
| 461 | const auto any_overlap = [&](uintptr_t from, uintptr_t to) { | ||
| 462 | for (const auto& [lo, hi] : released) | ||
| 463 | if (lo < to && hi > from) return true; | ||
| 464 | return false; | ||
| 465 | }; | ||
| 466 | |||
| 467 | const uint64_t first[kPages] = {pages[0], pages[1], pages[2]}; | ||
| 468 | check(api.map(window, kPages, first) == window && take().empty(), | ||
| 469 | "remap releases: a first mapping of unmapped pages releases nothing"); | ||
| 470 | check(api.map(window + kPage64k, 1, &pages[1]) == window + kPage64k && take().empty(), | ||
| 471 | "remap releases: a page mapped again to the frame it has is no change"); | ||
| 472 | check(api.map(window + kPage64k, 1, &pages[3]) == window + kPage64k && !take().empty() && | ||
| 473 | covers(base + kPage64k, base + 2 * kPage64k) && !any_overlap(base, base + kPage64k) && | ||
| 474 | !any_overlap(base + 2 * kPage64k, base + 3 * kPage64k), | ||
| 475 | "remap releases: a page given another frame is released, its neighbours are not"); | ||
| 476 | const uint64_t rotated[kPages] = {pages[2], pages[0], pages[1]}; | ||
| 477 | check(api.map(window, kPages, rotated) == window && !take().empty() && covers(base, base + 3 * kPage64k), | ||
| 478 | "remap releases: a run given other frames is released whole"); | ||
| 479 | const uint64_t partly[kPages] = {pages[2], pages[3], pages[1]}; | ||
| 480 | check(api.map(window, kPages, partly) == window && !take().empty() && | ||
| 481 | covers(base + kPage64k, base + 2 * kPage64k) && !any_overlap(base, base + kPage64k) && | ||
| 482 | !any_overlap(base + 2 * kPage64k, base + 3 * kPage64k), | ||
| 483 | "remap releases: of a remapped run, only the page whose frame changed is released"); | ||
| 484 | void* const last = window + 2 * kPage64k; | ||
| 485 | check(api.scatter(&last, 1, &pages[0]) == TRUE && !take().empty() && | ||
| 486 | covers(base + 2 * kPage64k, base + 3 * kPage64k) && !any_overlap(base, base + 2 * kPage64k), | ||
| 487 | "remap releases: the scatter call releases the page it gives another frame"); | ||
| 488 | check(api.scatter(&last, 1, &pages[0]) == TRUE && take().empty(), | ||
| 489 | "remap releases: the scatter call with the frame the page has releases nothing"); | ||
| 490 | // CopyPageMappingsBatchX: page 0 gets the frame at page 1 (kind 0 from a pool at the window itself). | ||
| 491 | const rt::xmem::PageRange range{0, 1, 1}; | ||
| 492 | const rt::xmem::PageBatch batch{base, 1, &range}; | ||
| 493 | check(rt::xmem::copy_page_mappings(1, &batch, base, kPages, 0) && !take().empty() && | ||
| 494 | covers(base, base + kPage64k) && !any_overlap(base + kPage64k, base + 3 * kPage64k), | ||
| 495 | "remap releases: CopyPageMappingsBatchX releases the page it gives another frame"); | ||
| 496 | |||
| 497 | check(api.map(window, kPages, nullptr) == window && !take().empty() && covers(base, base + 3 * kPage64k), | ||
| 498 | "remap releases: unmapping releases the range"); | ||
| 499 | for (uint64_t i = 0; i < kPages; ++i) watch::release(base + i * kPage64k); | ||
| 500 | watch::unsubscribe(queue); | ||
| 501 | api.free_pages(4, pages); | ||
| 502 | VirtualFree(window, 0, MEM_RELEASE); | ||
| 503 | } | ||
| 504 | |||
| 432 | // Physical pages in a 64 KiB-page window, as the measured callers use them: reserve a window, allocate pages, map them, | 505 | // Physical pages in a 64 KiB-page window, as the measured callers use them: reserve a window, allocate pages, map them, |
| 433 | // remap them elsewhere and in another order, unmap, free. | 506 | // remap them elsewhere and in another order, unmap, free. |
| 434 | void test_physical_pages(const PhysicalApi& api) { | 507 | void test_physical_pages(const PhysicalApi& api) { |
| @@ -741,7 +814,19 @@ void test_copy_page_mappings_cpu_view(const PhysicalApi& api) { | |||
| 741 | check(readable(other) && page_holds(other, 0x7300u), | 814 | check(readable(other) && page_holds(other, 0x7300u), |
| 742 | "a GPU-only source page unmapped there is looked up at its CPU range"); | 815 | "a GPU-only source page unmapped there is looked up at its CPU range"); |
| 743 | 816 | ||
| 744 | // A null source unmaps both ranges. | 817 | // A CPU page the title mapped itself is the title's: a copy onto its GPU-only page, or an unmap of it, changes the |
| 818 | // GPU-only page alone (the console's CopyPageMappingsBatchX edits only that range's page table). | ||
| 819 | check(copy(gpu + 2 * kPage64k, pool + kPage64k, 1, false), "a copy onto a GPU-only page whose CPU page is the title's"); | ||
| 820 | check(readable(gpu + 2 * kPage64k) && page_holds(gpu + 2 * kPage64k, 0x7301u), | ||
| 821 | "the GPU-only page shows the copied pool page"); | ||
| 822 | check(readable(cpu + 2 * kPage64k) && page_holds(cpu + 2 * kPage64k, 0x7300u), | ||
| 823 | "the title's own mapping at the paired CPU page is kept, not replaced by the copy"); | ||
| 824 | check(copy(gpu + 2 * kPage64k, nullptr, 1, false), "a null source unmaps a GPU-only page whose CPU page is the title's"); | ||
| 825 | check(!readable(gpu + 2 * kPage64k), "the GPU-only page is unmapped"); | ||
| 826 | check(readable(cpu + 2 * kPage64k) && page_holds(cpu + 2 * kPage64k, 0x7300u), | ||
| 827 | "the title's own mapping at the paired CPU page survives the unmap"); | ||
| 828 | |||
| 829 | // A null source unmaps both ranges where the CPU pages are the copy's own. | ||
| 745 | check(copy(gpu, nullptr, kPool, false), "a null source unmaps the GPU-only range"); | 830 | check(copy(gpu, nullptr, kPool, false), "a null source unmaps the GPU-only range"); |
| 746 | check(!readable(gpu) && !readable(cpu) && !readable(cpu + kPage64k), "the paired CPU range is unmapped with it"); | 831 | check(!readable(gpu) && !readable(cpu) && !readable(cpu + kPage64k), "the paired CPU range is unmapped with it"); |
| 747 | 832 | ||
| @@ -1544,6 +1629,38 @@ void test_physical_pages_commit_on_touch(const PhysicalApi& api) { | |||
| 1544 | check(read_ok, "ReadFile fills a page that commit_physical_range committed"); | 1629 | check(read_ok, "ReadFile fills a page that commit_physical_range committed"); |
| 1545 | 1630 | ||
| 1546 | check(api.map(second, 2, nullptr) == second && api.map(window, kMapped, nullptr) == window, "they unmap"); | 1631 | check(api.map(second, 2, nullptr) == second && api.map(window, kMapped, nullptr) == window, "they unmap"); |
| 1632 | { | ||
| 1633 | // A host guarded read (outside the title image at 0x140000000) of an unmapped window page exits as | ||
| 1634 | // passed_guarded without counting as left_to_title; a title read still counts as left_to_title. | ||
| 1635 | void* const stub_page = VirtualAlloc(nullptr, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | ||
| 1636 | const uint8_t mov_eax_rcx_ret[] = {0x8b, 0x01, 0xc3}; | ||
| 1637 | DWORD stub_old = 0; | ||
| 1638 | bool stub_ready = false; | ||
| 1639 | if (stub_page) { | ||
| 1640 | std::memcpy(stub_page, mov_eax_rcx_ret, sizeof(mov_eax_rcx_ret)); | ||
| 1641 | stub_ready = VirtualProtect(stub_page, 4096, PAGE_EXECUTE_READ, &stub_old) != FALSE; | ||
| 1642 | FlushInstructionCache(GetCurrentProcess(), stub_page, sizeof(mov_eax_rcx_ret)); | ||
| 1643 | } | ||
| 1644 | const auto host_read_faults = [&](const void* p) { | ||
| 1645 | __try { | ||
| 1646 | reinterpret_cast<uint32_t (*)(const void*)>(stub_page)(p); | ||
| 1647 | return false; | ||
| 1648 | } __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER | ||
| 1649 | : EXCEPTION_CONTINUE_SEARCH) { | ||
| 1650 | return true; | ||
| 1651 | } | ||
| 1652 | }; | ||
| 1653 | const uint64_t guarded_before = rt::xmem::fault_exits(rt::xmem::FaultExit::passed_guarded); | ||
| 1654 | const uint64_t left_before = rt::xmem::fault_exits(rt::xmem::FaultExit::left_to_title); | ||
| 1655 | check(stub_ready && host_read_faults(second) && | ||
| 1656 | rt::xmem::fault_exits(rt::xmem::FaultExit::passed_guarded) == guarded_before + 1 && | ||
| 1657 | rt::xmem::fault_exits(rt::xmem::FaultExit::left_to_title) == left_before, | ||
| 1658 | "a host guarded read of an unmapped window page exits as passed_guarded"); | ||
| 1659 | check(access_faults(reinterpret_cast<volatile uint32_t*>(second), false) && | ||
| 1660 | rt::xmem::fault_exits(rt::xmem::FaultExit::left_to_title) == left_before + 1, | ||
| 1661 | "a title read of an unmapped window page still exits as left_to_title"); | ||
| 1662 | if (stub_page) VirtualFree(stub_page, 0, MEM_RELEASE); | ||
| 1663 | } | ||
| 1547 | check(api.free_pages(count, pages.data()) == TRUE, "XMemFreePhysicalPages frees them"); | 1664 | check(api.free_pages(count, pages.data()) == TRUE, "XMemFreePhysicalPages frees them"); |
| 1548 | } | 1665 | } |
| 1549 | 1666 | ||
| @@ -2655,6 +2772,7 @@ int main(int argc, char** argv) { | |||
| 2655 | // Keep the direct SEC_RESERVE control ahead of the first physical allocation, which installs xmem's VEH. | 2772 | // Keep the direct SEC_RESERVE control ahead of the first physical allocation, which installs xmem's VEH. |
| 2656 | g_section_write_control = measure_section_write_control(); | 2773 | g_section_write_control = measure_section_write_control(); |
| 2657 | test_physical_pages(physical); | 2774 | test_physical_pages(physical); |
| 2775 | test_remap_releases(physical); | ||
| 2658 | if (argc == 2 && std::strcmp(argv[1], "--file-pools") == 0) { | 2776 | if (argc == 2 && std::strcmp(argv[1], "--file-pools") == 0) { |
| 2659 | test_placeholders(physical); | 2777 | test_placeholders(physical); |
| 2660 | print_result(); | 2778 | print_result(); |