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:
authorMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 13:34:47 -0600
committerMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 13:34:47 -0600
commitced361512405814bbdfcfb08a9b7cda11b6d0f7a (patch)
tree4eb8877e666a3d2370deae2ba47898f47dfa0c48 /runtime/test/xmem_test.cpp
parentd2b4538c4c6129bb2472bbcb78343ae555c1cfae (diff)
parent2cf0ff8b10d00e2753037e9f141725f14850004c (diff)
local: merge #7584 (docs conflict: ours)
Diffstat (limited to 'runtime/test/xmem_test.cpp')
-rw-r--r--runtime/test/xmem_test.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp
index 53df7fcc5..415f2c9e5 100644
--- a/runtime/test/xmem_test.cpp
+++ b/runtime/test/xmem_test.cpp
@@ -369,10 +369,27 @@ void test_physical_pages(const PhysicalApi& api) {
369 VirtualQuery(window, &info, sizeof(info)); 369 VirtualQuery(window, &info, sizeof(info));
370 check(info.State == MEM_COMMIT && info.Type == MEM_MAPPED && info.Protect == PAGE_READWRITE, 370 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"); 371 "a mapped page is a read-write view with the window's protection");
372 check(info.AllocationBase == window && info.RegionSize == kPages * kPage64k,
373 "consecutive pages of one allocation are mapped as one view");
372 } 374 }
373 375
376 // A page unmapped or remapped inside that view splits it; its neighbours keep their pages.
377 void* const middle = window + kPage64k;
378 check(api.scatter(&middle, 1, nullptr) == TRUE && reserved_only(window + kPage64k),
379 "a page inside a view unmaps on its own");
380 check(page_holds(window, 0x1000u) && page_holds(window + 2 * kPage64k, 0x3000u) &&
381 page_holds(window + 3 * kPage64k, 0x4000u),
382 "the pages around it stay mapped with their contents");
383 check(api.map(middle, 1, &pages[1]) == middle && page_holds(window + kPage64k, 0x2000u),
384 "a page maps again into the gap");
385
374 check(api.map(window, kPages, nullptr) == window, "XMemMapPhysicalPages with no page array unmaps"); 386 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"); 387 check(reserved_only(window) && reserved_only(window + 3 * kPage64k), "unmapped pages are reserved again");
388 // The split above left the range as several host placeholders; the whole run maps again over them.
389 check(api.map(window, kPages, pages) == window && page_holds(window, 0x1000u) &&
390 page_holds(window + 3 * kPage64k, 0x4000u),
391 "a run maps again over a range an earlier partial unmap split");
392 check(api.map(window, kPages, nullptr) == window && reserved_only(window), "and unmaps again");
376 393
377 // Remap in reverse order into the other window: each page keeps its contents. 394 // Remap in reverse order into the other window: each page keeps its contents.
378 uint64_t reversed[kPages]; 395 uint64_t reversed[kPages];
@@ -441,6 +458,47 @@ void test_physical_pages(const PhysicalApi& api) {
441} 458}
442 459
443// The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it. 460// The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it.
461// A run longer than one host view is mapped as capped views, so unmapping one page inside it only remaps the rest of
462// that view; every other page keeps its contents.
463void test_long_run(const PhysicalApi& api) {
464 constexpr uint64_t kPages = 1100; // more than the 1024-page view cap
465 auto* const window = static_cast<uint8_t*>(
466 api.virtual_alloc(nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE));
467 std::vector<uint64_t> pages(kPages);
468 uint64_t count = kPages;
469 check(window && api.allocate(0x280, &count, pages.data()) == TRUE, "a long run's window and pages are allocated");
470 if (!window || count != kPages) return;
471 check(api.map(window, kPages, pages.data()) == window, "a run longer than the view cap maps");
472 MEMORY_BASIC_INFORMATION info{};
473 VirtualQuery(window, &info, sizeof(info));
474 check(info.State == MEM_COMMIT && info.RegionSize == 1024 * kPage64k, "it is mapped as views of at most 1024 pages");
475 for (uint64_t i = 0; i < kPages; ++i) fill_page(window + i * kPage64k, static_cast<uint32_t>(i + 1));
476 void* const hole = window + 10 * kPage64k;
477 check(api.scatter(&hole, 1, nullptr) == TRUE && reserved_only(hole), "a page inside the first view unmaps");
478 bool kept = true;
479 for (uint64_t i = 0; i < kPages; ++i) {
480 if (i != 10) kept &= page_holds(window + i * kPage64k, static_cast<uint32_t>(i + 1));
481 }
482 check(kept, "every other page of the run keeps its contents");
483 // Several pages across the boundary of two capped views unmap together; the pages on both sides stay.
484 check(api.map(window + 1000 * kPage64k, 50, nullptr) == window + 1000 * kPage64k &&
485 reserved_only(window + 1000 * kPage64k) && reserved_only(window + 1049 * kPage64k),
486 "a multi-page range across two views unmaps");
487 check(page_holds(window + 999 * kPage64k, 1000u) && page_holds(window + 1050 * kPage64k, 1051u),
488 "the pages on both sides of it keep their contents");
489 // Mapping other pages over the middle of a live run replaces just those pages.
490 check(api.map(window + 500 * kPage64k, 3, &pages[600]) == window + 500 * kPage64k &&
491 page_holds(window + 500 * kPage64k, 601u) && page_holds(window + 502 * kPage64k, 603u),
492 "pages mapped over the middle of a live run replace it there");
493 check(page_holds(window + 499 * kPage64k, 500u) && page_holds(window + 503 * kPage64k, 504u),
494 "and the run's pages around them keep their contents");
495 check(api.map(window, kPages, nullptr) == window && reserved_only(window) &&
496 reserved_only(window + (kPages - 1) * kPage64k),
497 "the whole run unmaps");
498 check(api.free_pages(kPages, pages.data()) == TRUE, "the long run's pages are freed");
499 check(api.map(window, 1, pages.data()) == nullptr && reserved_only(window), "a freed page does not map again");
500}
501
444void test_placeholders(const PhysicalApi& api) { 502void test_placeholders(const PhysicalApi& api) {
445 auto* const base = static_cast<uint8_t*>( 503 auto* const base = static_cast<uint8_t*>(
446 api.virtual_alloc(nullptr, 6 * kPage64k, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, 7, PAGE_NOACCESS)); 504 api.virtual_alloc(nullptr, 6 * kPage64k, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, 7, PAGE_NOACCESS));
@@ -746,6 +804,7 @@ int main() {
746 if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)"); 804 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) { 805 if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) {
748 test_physical_pages(physical); 806 test_physical_pages(physical);
807 test_long_run(physical);
749 test_placeholders(physical); 808 test_placeholders(physical);
750 test_write_combine_window(physical); 809 test_write_combine_window(physical);
751 } 810 }