upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/xmem.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
commit8d832fd10913064e1c8aec74191e3ba8f70211c8 (patch)
treee365199d7d86ebf592a5a0dc2f07cda4b74b58a7 /runtime/shims/xmem.cpp
parent5da3b6be3cefc1499452ba8c8370a50092674d61 (diff)
parentc50ed94b35b37b697ab9c5fd176f3738480b4db5 (diff)
Merge main into rt-title-every-budget (#2122)
#2098 split the every-pass loop of refresh_title_textures by mode: with LEONIDA_TEXTURE_REFRESH_ASYNC every persistent mapping is polled each pass and only stale unmapped textures are visited, without it all mapped or stale ones are. The async branch is kept as main has it. LEONIDA_TITLE_EVERY_BUDGET_US applies to the synchronous pass, the one before a submission on the submitting thread; under async the pass runs on the refresh worker and the limit does not apply (README says so). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp23
1 files changed, 16 insertions, 7 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 40bc28f2a..0184642e3 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -78,7 +78,7 @@ void note_attributes(const char* api, uint64_t attributes, size_t size, const vo
78 rt::startup_trace::event("xmem_attributes", fields); 78 rt::startup_trace::event("xmem_attributes", fields);
79} 79}
80 80
81// LEONIDA_TITLE_HEAP=1: the title's XMemAlloc blocks come from a heap of their own instead of the CRT heap. Slot-133 81// The title's XMemAlloc blocks come from a heap of their own instead of the CRT heap (LEONIDA_TITLE_HEAP=0: not). Slot-133
82// imports pin title memory page by page for the GPU, and on the CRT heap a pinned page also holds the runtime's own 82// imports pin title memory page by page for the GPU, and on the CRT heap a pinned page also holds the runtime's own
83// objects (g_pages' nodes among them): a GPU store past a buffer's end, or into a block freed while its import still 83// objects (g_pages' nodes among them): a GPU store past a buffer's end, or into a block freed while its import still
84// lived, then corrupted the runtime (speed-30/41/49-52: g_pages found corrupt under g_phys_mutex). Each block keeps its 84// lived, then corrupted the runtime (speed-30/41/49-52: g_pages found corrupt under g_phys_mutex). Each block keeps its
@@ -89,8 +89,9 @@ struct TitleHeapHeader {
89}; 89};
90HANDLE title_heap() { 90HANDLE title_heap() {
91 static const HANDLE heap = [] { 91 static const HANDLE heap = [] {
92 // Set LEONIDA_TITLE_HEAP=0 to use the CRT heap.
92 const char* v = std::getenv("LEONIDA_TITLE_HEAP"); 93 const char* v = std::getenv("LEONIDA_TITLE_HEAP");
93 if (!v || std::strcmp(v, "1") != 0) return HANDLE(nullptr); 94 if (v && std::strcmp(v, "0") == 0) return HANDLE(nullptr);
94 const HANDLE h = HeapCreate(0, 0, 0); 95 const HANDLE h = HeapCreate(0, 0, 0);
95 std::fprintf(stderr, "[rt] xmem: XMemAlloc blocks come from a heap of their own (%p)\n", h); 96 std::fprintf(stderr, "[rt] xmem: XMemAlloc blocks come from a heap of their own (%p)\n", h);
96 return h; 97 return h;
@@ -391,7 +392,7 @@ uintptr_t stream_cpu, stream_gpu, stream_size;
391bool mirror_stream; 392bool mirror_stream;
392leonida::host_heap::map<uintptr_t, Window> stream_offsets; 393leonida::host_heap::map<uintptr_t, Window> stream_offsets;
393std::mutex g_phys_mutex; 394std::mutex g_phys_mutex;
394std::map<uintptr_t, Window> g_windows; // base -> window 395leonida::host_heap::map<uintptr_t, Window> g_windows; // base -> window (host_heap: see g_pages)
395// The same table for the fault handler, which may not wait for g_phys_mutex: a holder of that mutex allocates 396// The same table for the fault handler, which may not wait for g_phys_mutex: a holder of that mutex allocates
396// (g_mapped, g_windows) and the faulting title thread can hold the process heap's lock (see g_fault_lock). Host heap, 397// (g_mapped, g_windows) and the faulting title thread can hold the process heap's lock (see g_fault_lock). Host heap,
397// read and written under g_fault_lock. 398// read and written under g_fault_lock.
@@ -400,7 +401,10 @@ leonida::host_heap::map<uintptr_t, Window> g_fault_windows;
400// until the title releases them (TitleVirtualFree). With the windows these are the title's reserved memory: the only 401// until the title releases them (TitleVirtualFree). With the windows these are the title's reserved memory: the only
401// place the fault handler commits a mapped section page it has no view of (commit_title_section_page). 402// place the fault handler commits a mapped section page it has no view of (commit_title_section_page).
402leonida::host_heap::map<uintptr_t, Window> g_title_placeholders; 403leonida::host_heap::map<uintptr_t, Window> g_title_placeholders;
403std::unordered_map<uint64_t, PhysicalPage> g_pages; // page number -> backing 404// Keep page table nodes on the runtime heap alongside the window table.
405std::unordered_map<uint64_t, PhysicalPage, std::hash<uint64_t>, std::equal_to<uint64_t>,
406 leonida::host_heap::Allocator<std::pair<const uint64_t, PhysicalPage>>>
407 g_pages; // page number -> backing
404std::vector<uint64_t> g_free_slots; 408std::vector<uint64_t> g_free_slots;
405uint64_t g_next_slot = 1; 409uint64_t g_next_slot = 1;
406// A mapped run: `count` pages of one section at consecutive offsets, mapped as one view at its base. One view per run 410// A mapped run: `count` pages of one section at consecutive offsets, mapped as one view at its base. One view per run
@@ -797,10 +801,15 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type,
797 const DWORD host_type = allocation_type & kXmemHostAllocationTypeMask; 801 const DWORD host_type = allocation_type & kXmemHostAllocationTypeMask;
798 if (result && host_type && !(host_type & MEM_COMMIT)) { 802 if (result && host_type && !(host_type & MEM_COMMIT)) {
799 const bool gpu_only = (protect & kPageGraphicsMask) && host_protect(protect) == PAGE_NOACCESS; 803 const bool gpu_only = (protect & kPageGraphicsMask) && host_protect(protect) == PAGE_NOACCESS;
800 if (leonida::title_gpu_views::note_reservation(reinterpret_cast<uintptr_t>(result), size, gpu_only)) 804 const auto base = reinterpret_cast<uintptr_t>(result);
801 std::fprintf(stderr, "[rt] xmem: GPU-only view %p (%llu MiB, protect 0x%lx) reads at the CPU view %p\n", 805 const bool stream_queue_view = allocation_type == 0x20402000 && gpu_only &&
806 reinterpret_cast<uintptr_t>(_ReturnAddress()) - kTitleImage == 0x1e1d03e &&
807 rt::xmem::slot31_runs() && rt::xmem::queue_page_mappings_enabled();
808 if (leonida::title_gpu_views::note_reservation(base, size, gpu_only, stream_queue_view))
809 std::fprintf(stderr, "[rt] xmem: GPU-only view %p (%llu MiB, protect 0x%lx) paired with CPU view %p (%s)\n",
802 result, static_cast<unsigned long long>(size >> 20), static_cast<unsigned long>(protect), 810 result, static_cast<unsigned long long>(size >> 20), static_cast<unsigned long>(protect),
803 reinterpret_cast<void*>(leonida::title_gpu_views::to_cpu(reinterpret_cast<uintptr_t>(result)))); 811 reinterpret_cast<void*>(leonida::title_gpu_views::other_view(base)),
812 stream_queue_view ? "separate queue mappings" : "CPU backing");
804 else if (gpu_only && size >= (64u << 20)) 813 else if (gpu_only && size >= (64u << 20))
805 std::fprintf(stderr, "[rt] xmem: GPU-only reservation %p (%llu MiB, protect 0x%lx) has no CPU view\n", result, 814 std::fprintf(stderr, "[rt] xmem: GPU-only reservation %p (%llu MiB, protect 0x%lx) has no CPU view\n", result,
806 static_cast<unsigned long long>(size >> 20), static_cast<unsigned long>(protect)); 815 static_cast<unsigned long long>(size >> 20), static_cast<unsigned long>(protect));