upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/xmem_test.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 21:09:27 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 21:09:27 -0500
commit7ea226e65b87491b0977f23d6ff693145d64d02b (patch)
tree39d3d45cd393db1a896d5e24a559e12257c5ed94 /runtime/test/xmem_test.cpp
parent52ef7df72f856e4be441bc2cb274838a17cfce28 (diff)
parent0aecc5efab56a77eddf00ad6cefa046d42aaae6c (diff)
Merge main (0aecc5efa: #984 and #968) into the #939 merge
main now holds the #984 merge (18a22c1ae) and #968, queue slot 31 as CopyPageMappingsBatchX. Two conflicts: the include list of d3d12_xs_device.cpp (both includes kept) and the call order in xmem_test.cpp (the two page-mapping tests of #968 run after test_physical_pages, after the early return of #939's file-pools mode). The slot 31 dispatch of #968 stays ahead of the presentation routes, so #939's rule for queue slots 30 to 32 on a device that had a presentation handler does not reach it where xmem is linked. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/xmem_test.cpp')
-rw-r--r--runtime/test/xmem_test.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp
index 7b9d8d7ae..03944b1e1 100644
--- a/runtime/test/xmem_test.cpp
+++ b/runtime/test/xmem_test.cpp
@@ -556,6 +556,128 @@ void test_physical_pages(const PhysicalApi& api) {
556 // The windows were split into one placeholder per page; the process exit releases them. 556 // The windows were split into one placeholder per page; the process exit releases them.
557} 557}
558 558
559// rt::xmem::copy_page_mappings_fn, behind console queue slot 31 (CopyPageMappingsBatchX): a destination page maps the
560// physical page that is mapped at the source page, so both addresses are one memory, as two XMemMapPhysicalPages calls
561// with the same page would make them.
562void test_copy_page_mappings(const PhysicalApi& api) {
563 const auto copy = rt::xmem::copy_page_mappings_fn;
564 check(copy != nullptr, "xmem.cpp sets the copy_page_mappings hook");
565 if (!copy) return;
566 constexpr uint64_t kPool = 4, kHeap = 8;
567 auto* const pool = static_cast<uint8_t*>(api.virtual_alloc(
568 nullptr, kPool * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
569 auto* const heap = static_cast<uint8_t*>(api.virtual_alloc(
570 nullptr, kHeap * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
571 check(pool && heap, "XMemVirtualAlloc reserves a pool window and a heap window");
572 if (!pool || !heap) return;
573 uint64_t pages[kPool]{};
574 uint64_t count = kPool;
575 check(api.allocate(0x280, &count, pages) == TRUE && count == kPool, "pages for the pool");
576 // Pool pages 0..2 mapped, pool page 3 left unmapped.
577 check(api.map(pool, 3, pages) == pool, "the pool maps three pages");
578 for (uint64_t i = 0; i < 3; ++i) fill_page(pool + i * kPage64k, 0x7100u + static_cast<uint32_t>(i));
579
580 // A run: heap pages 0 and 1 show pool pages 1 and 2, and are the same memory.
581 check(copy(heap, pool + kPage64k, 2, false), "a run of pool pages maps into the heap");
582 check(page_holds(heap, 0x7101u) && page_holds(heap + kPage64k, 0x7102u), "the heap pages show pool pages 1 and 2");
583 static_cast<volatile uint32_t*>(static_cast<void*>(heap + kPage64k))[1] = 0xc0ffee31;
584 check(read_u32(pool + 2 * kPage64k, 4) == 0xc0ffee31, "a write through the heap is seen in the pool: one memory");
585
586 // One page repeated.
587 check(copy(heap + 2 * kPage64k, pool, 3, true), "one pool page maps at three heap pages");
588 check(page_holds(heap + 2 * kPage64k, 0x7100u) && page_holds(heap + 4 * kPage64k, 0x7100u),
589 "every repeated page shows pool page 0");
590
591 // A pool page with nothing mapped leaves its destination unmapped; so does the null source.
592 check(copy(heap + 5 * kPage64k, pool + 2 * kPage64k, 2, false), "a run that ends in an unmapped pool page maps");
593 check(page_holds(heap + 5 * kPage64k, 0x7102u) && reserved_only(heap + 6 * kPage64k),
594 "the unmapped pool page leaves its heap page unmapped");
595 check(copy(heap, nullptr, 2, false), "a null source unmaps");
596 check(reserved_only(heap) && reserved_only(heap + kPage64k), "the heap pages are reserved again");
597 check(page_holds(pool + kPage64k, 0x7101u), "unmapping the heap leaves the pool's own mapping");
598
599 // Overlapping source and destination: every source page is read before any destination page changes.
600 check(copy(pool + kPage64k, pool, 2, false), "a run maps over an overlapping range");
601 check(page_holds(pool + kPage64k, 0x7100u) && page_holds(pool + 2 * kPage64k, 0x7101u),
602 "the overlapping run shows the pages from before it");
603
604 uint8_t outside = 0;
605 const auto outside_page =
606 reinterpret_cast<uint8_t*>((reinterpret_cast<uintptr_t>(&outside) + 0xffff) & ~uintptr_t{0xffff});
607 check(!copy(outside_page, pool, 1, false), "a destination outside any window fails");
608 SetLastError(ERROR_SUCCESS);
609 check(!copy(heap + 1, pool, 1, false) && GetLastError() == ERROR_INVALID_PARAMETER,
610 "a destination that is not 64 KiB-aligned fails");
611 check(!copy(heap, pool + 1, 1, false) && GetLastError() == ERROR_INVALID_PARAMETER,
612 "a source that is not 64 KiB-aligned fails");
613
614 api.map(heap, kHeap, nullptr);
615 api.map(pool, kPool, nullptr);
616 check(api.free_pages(kPool, pages) == TRUE, "the pool's pages are freed");
617}
618
619bool access_faults(volatile uint32_t* address, bool write);
620
621// The same hook on a GPU-only destination: the title reserves a CPU-mappable range and a GPU-only one of the same size
622// right after it (title_gpu_views), and the D3D12 layer reads the GPU-only range's memory at the CPU range's address.
623// A batch copy onto the GPU-only range maps the same pages at the CPU range too, so that memory becomes readable there.
624void test_copy_page_mappings_cpu_view(const PhysicalApi& api) {
625 const auto copy = rt::xmem::copy_page_mappings_fn;
626 if (!copy) return; // test_copy_page_mappings reports it
627 constexpr uint64_t kPool = 2, kPair = 3;
628 auto* const pool = static_cast<uint8_t*>(api.virtual_alloc(
629 nullptr, kPool * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
630 auto* const other = static_cast<uint8_t*>(api.virtual_alloc(
631 nullptr, kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
632 // The pair: the CPU-mappable range, then the GPU-only one (PAGE_GRAPHICS_NOACCESS | PAGE_NOACCESS) of its size.
633 auto* const cpu = static_cast<uint8_t*>(api.virtual_alloc(
634 nullptr, kPair * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
635 auto* const gpu = static_cast<uint8_t*>(
636 api.virtual_alloc(nullptr, kPair * kPage64k, MEM_RESERVE | kXmem64kPages, 0x3, 0x801));
637 check(pool && other && cpu && gpu, "XMemVirtualAlloc reserves a pool, a window and a CPU + GPU-only pair");
638 if (!pool || !other || !cpu || !gpu) return;
639 check(leonida::title_gpu_views::to_cpu(reinterpret_cast<uintptr_t>(gpu)) == reinterpret_cast<uintptr_t>(cpu),
640 "the GPU-only range reads at the CPU range reserved just before it");
641 uint64_t pages[kPool]{};
642 uint64_t count = kPool;
643 check(api.allocate(0x280, &count, pages) == TRUE && count == kPool, "pages for the pool");
644 check(api.map(pool, kPool, pages) == pool, "the pool maps its pages");
645 for (uint64_t i = 0; i < kPool; ++i) fill_page(pool + i * kPage64k, 0x7300u + static_cast<uint32_t>(i));
646
647 // A run onto the GPU-only range: its CPU range shows the same bytes. Each page is read under a guard first, so a
648 // page left reserved fails its check instead of faulting the test (Wine's VirtualQuery does not report a view
649 // mapped into a placeholder as committed, so the state is not asked for).
650 const auto readable = [](const uint8_t* page) {
651 return !access_faults(reinterpret_cast<volatile uint32_t*>(const_cast<uint8_t*>(page)), false);
652 };
653 check(copy(gpu, pool, kPool, false), "a run of pool pages maps onto the GPU-only range");
654 check(readable(gpu) && page_holds(gpu, 0x7300u), "the GPU-only range shows the pool pages");
655 check(readable(cpu) && readable(cpu + kPage64k), "the paired CPU range is readable after the copy");
656 check(readable(cpu) && readable(cpu + kPage64k) && page_holds(cpu, 0x7300u) && page_holds(cpu + kPage64k, 0x7301u),
657 "the paired CPU range shows the same bytes as the pool");
658 check(!readable(cpu + 2 * kPage64k), "a CPU page past the copied run stays unmapped");
659 if (readable(cpu + kPage64k)) {
660 static_cast<volatile uint32_t*>(static_cast<void*>(cpu + kPage64k))[1] = 0xc0ffee32;
661 check(read_u32(pool + kPage64k, 4) == 0xc0ffee32 && read_u32(gpu + kPage64k, 4) == 0xc0ffee32,
662 "a write at the CPU range is seen in the pool and the GPU-only range: one memory");
663 }
664
665 // A GPU-only source whose page is mapped only at its CPU range is found there.
666 check(api.map(cpu + 2 * kPage64k, 1, &pages[0]) == cpu + 2 * kPage64k, "a page maps at the CPU range alone");
667 check(copy(other, gpu + 2 * kPage64k, 1, false), "a copy from the GPU-only range maps");
668 check(readable(other) && page_holds(other, 0x7300u),
669 "a GPU-only source page unmapped there is looked up at its CPU range");
670
671 // A null source unmaps both ranges.
672 check(copy(gpu, nullptr, kPool, false), "a null source unmaps the GPU-only range");
673 check(!readable(gpu) && !readable(cpu) && !readable(cpu + kPage64k), "the paired CPU range is unmapped with it");
674
675 api.map(other, 1, nullptr);
676 api.map(cpu, kPair, nullptr);
677 api.map(pool, kPool, nullptr);
678 check(api.free_pages(kPool, pages) == TRUE, "the pool's pages are freed");
679}
680
559bool access_faults(volatile uint32_t* address, bool write) { 681bool access_faults(volatile uint32_t* address, bool write) {
560 __try { 682 __try {
561 if (write) { 683 if (write) {
@@ -1535,6 +1657,8 @@ int main(int argc, char** argv) {
1535 print_result(); 1657 print_result();
1536 return g_failures ? 1 : 0; 1658 return g_failures ? 1 : 0;
1537 } 1659 }
1660 test_copy_page_mappings(physical);
1661 test_copy_page_mappings_cpu_view(physical);
1538 test_physical_pages_commit_on_touch(physical); 1662 test_physical_pages_commit_on_touch(physical);
1539 test_commit_physical_pages(physical); 1663 test_commit_physical_pages(physical);
1540 test_first_touch_during_remap(physical); 1664 test_first_touch_during_remap(physical);