diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:11:40 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:17:45 -0500 |
| commit | 255f17001705101aa2e484b9e2caaf1daab275e3 (patch) | |
| tree | 6886830a22b896907f137f7b0baeddde84322a14 /runtime/test/xmem_test.cpp | |
| parent | 6fdeadfe509f37203317a967bf28e57fc75676b2 (diff) | |
xmem: map slot-31 copies at the GPU-only range's paired CPU view too
Applies the fix oscarlosu-lab proposed on #968:
https://github.com/ubiquitarianism/leonida/pull/968#issuecomment-7142206199
What was wrong: queue slot 31 (CopyPageMappingsBatchX) targets the title's
GPU-only reservation, but the D3D12 layer reads that memory at its paired CPU
view (title_gpu_views::to_cpu). copy_page_mappings mapped the pages only at the
GPU-only address, so the paired CPU view stayed reserved and the layer's reads of
that memory (the slot-70 views waiting for readable memory) never became
possible.
The change, in copy_page_mappings:
- when the destination is in a paired GPU-only range and its CPU range is a
physical-page window, every page mapped or unmapped at the destination is
mapped or unmapped at the CPU address as well, with that window's protection;
- a source page not mapped at a GPU-only address is looked up at its CPU
address.
Cost: a second view per copied page, so such a page is mapped at three
addresses (pool, GPU-only, CPU) and counts as aliased: the shadow tracker copies
it at every submission. Risk: the pairing is a heuristic (the CPU reservation of
the same size made just before the GPU-only one); if it is ever wrong, the
mirror replaces the title's own mappings in that CPU range.
The comment reports 9 alias checks but does not give them, so the test is new:
xmem_test's test_copy_page_mappings_cpu_view reserves a CPU + GPU-only pair and
checks that after a batch copy onto the GPU-only range the CPU range is
committed and shows the same bytes (and a write there is one memory with the
pool), that a GPU-only source mapped only at its CPU range is found there, and
that a null source unmaps both ranges.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/xmem_test.cpp')
| -rw-r--r-- | runtime/test/xmem_test.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 96924e185..2dd93d9f1 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp | |||
| @@ -590,6 +590,68 @@ void test_copy_page_mappings(const PhysicalApi& api) { | |||
| 590 | check(api.free_pages(kPool, pages) == TRUE, "the pool's pages are freed"); | 590 | check(api.free_pages(kPool, pages) == TRUE, "the pool's pages are freed"); |
| 591 | } | 591 | } |
| 592 | 592 | ||
| 593 | bool access_faults(volatile uint32_t* address, bool write); | ||
| 594 | |||
| 595 | // The same hook on a GPU-only destination: the title reserves a CPU-mappable range and a GPU-only one of the same size | ||
| 596 | // right after it (title_gpu_views), and the D3D12 layer reads the GPU-only range's memory at the CPU range's address. | ||
| 597 | // A batch copy onto the GPU-only range maps the same pages at the CPU range too, so that memory becomes readable there. | ||
| 598 | void test_copy_page_mappings_cpu_view(const PhysicalApi& api) { | ||
| 599 | const auto copy = rt::xmem::copy_page_mappings_fn; | ||
| 600 | if (!copy) return; // test_copy_page_mappings reports it | ||
| 601 | constexpr uint64_t kPool = 2, kPair = 3; | ||
| 602 | auto* const pool = static_cast<uint8_t*>(api.virtual_alloc( | ||
| 603 | nullptr, kPool * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE)); | ||
| 604 | auto* const other = static_cast<uint8_t*>(api.virtual_alloc( | ||
| 605 | nullptr, kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE)); | ||
| 606 | // The pair: the CPU-mappable range, then the GPU-only one (PAGE_GRAPHICS_NOACCESS | PAGE_NOACCESS) of its size. | ||
| 607 | auto* const cpu = static_cast<uint8_t*>(api.virtual_alloc( | ||
| 608 | nullptr, kPair * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE)); | ||
| 609 | auto* const gpu = static_cast<uint8_t*>( | ||
| 610 | api.virtual_alloc(nullptr, kPair * kPage64k, MEM_RESERVE | kXmem64kPages, 0x3, 0x801)); | ||
| 611 | check(pool && other && cpu && gpu, "XMemVirtualAlloc reserves a pool, a window and a CPU + GPU-only pair"); | ||
| 612 | if (!pool || !other || !cpu || !gpu) return; | ||
| 613 | check(leonida::title_gpu_views::to_cpu(reinterpret_cast<uintptr_t>(gpu)) == reinterpret_cast<uintptr_t>(cpu), | ||
| 614 | "the GPU-only range reads at the CPU range reserved just before it"); | ||
| 615 | uint64_t pages[kPool]{}; | ||
| 616 | uint64_t count = kPool; | ||
| 617 | check(api.allocate(0x280, &count, pages) == TRUE && count == kPool, "pages for the pool"); | ||
| 618 | check(api.map(pool, kPool, pages) == pool, "the pool maps its pages"); | ||
| 619 | for (uint64_t i = 0; i < kPool; ++i) fill_page(pool + i * kPage64k, 0x7300u + static_cast<uint32_t>(i)); | ||
| 620 | |||
| 621 | // A run onto the GPU-only range: its CPU range shows the same bytes. Each page is read under a guard first, so a | ||
| 622 | // page left reserved fails its check instead of faulting the test (Wine's VirtualQuery does not report a view | ||
| 623 | // mapped into a placeholder as committed, so the state is not asked for). | ||
| 624 | const auto readable = [](const uint8_t* page) { | ||
| 625 | return !access_faults(reinterpret_cast<volatile uint32_t*>(const_cast<uint8_t*>(page)), false); | ||
| 626 | }; | ||
| 627 | check(copy(gpu, pool, kPool, false), "a run of pool pages maps onto the GPU-only range"); | ||
| 628 | check(readable(gpu) && page_holds(gpu, 0x7300u), "the GPU-only range shows the pool pages"); | ||
| 629 | check(readable(cpu) && readable(cpu + kPage64k), "the paired CPU range is readable after the copy"); | ||
| 630 | check(readable(cpu) && readable(cpu + kPage64k) && page_holds(cpu, 0x7300u) && page_holds(cpu + kPage64k, 0x7301u), | ||
| 631 | "the paired CPU range shows the same bytes as the pool"); | ||
| 632 | check(!readable(cpu + 2 * kPage64k), "a CPU page past the copied run stays unmapped"); | ||
| 633 | if (readable(cpu + kPage64k)) { | ||
| 634 | static_cast<volatile uint32_t*>(static_cast<void*>(cpu + kPage64k))[1] = 0xc0ffee32; | ||
| 635 | check(read_u32(pool + kPage64k, 4) == 0xc0ffee32 && read_u32(gpu + kPage64k, 4) == 0xc0ffee32, | ||
| 636 | "a write at the CPU range is seen in the pool and the GPU-only range: one memory"); | ||
| 637 | } | ||
| 638 | |||
| 639 | // A GPU-only source whose page is mapped only at its CPU range is found there. | ||
| 640 | check(api.map(cpu + 2 * kPage64k, 1, &pages[0]) == cpu + 2 * kPage64k, "a page maps at the CPU range alone"); | ||
| 641 | check(copy(other, gpu + 2 * kPage64k, 1, false), "a copy from the GPU-only range maps"); | ||
| 642 | check(readable(other) && page_holds(other, 0x7300u), | ||
| 643 | "a GPU-only source page unmapped there is looked up at its CPU range"); | ||
| 644 | |||
| 645 | // A null source unmaps both ranges. | ||
| 646 | check(copy(gpu, nullptr, kPool, false), "a null source unmaps the GPU-only range"); | ||
| 647 | check(!readable(gpu) && !readable(cpu) && !readable(cpu + kPage64k), "the paired CPU range is unmapped with it"); | ||
| 648 | |||
| 649 | api.map(other, 1, nullptr); | ||
| 650 | api.map(cpu, kPair, nullptr); | ||
| 651 | api.map(pool, kPool, nullptr); | ||
| 652 | check(api.free_pages(kPool, pages) == TRUE, "the pool's pages are freed"); | ||
| 653 | } | ||
| 654 | |||
| 593 | bool access_faults(volatile uint32_t* address, bool write) { | 655 | bool access_faults(volatile uint32_t* address, bool write) { |
| 594 | __try { | 656 | __try { |
| 595 | if (write) { | 657 | if (write) { |
| @@ -1561,6 +1623,7 @@ int main() { | |||
| 1561 | if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) { | 1623 | if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) { |
| 1562 | test_physical_pages(physical); | 1624 | test_physical_pages(physical); |
| 1563 | test_copy_page_mappings(physical); | 1625 | test_copy_page_mappings(physical); |
| 1626 | test_copy_page_mappings_cpu_view(physical); | ||
| 1564 | test_physical_pages_commit_on_touch(physical); | 1627 | test_physical_pages_commit_on_touch(physical); |
| 1565 | test_commit_physical_pages(physical); | 1628 | test_commit_physical_pages(physical); |
| 1566 | test_first_touch_during_remap(physical); | 1629 | test_first_touch_during_remap(physical); |