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:
authorTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
committerTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
commitbeffaa9a476ca1b3dfd9cfa55e39eafbb1f5eec6 (patch)
tree32877805bf4eb46d9a89d5753db6b5a2b06dc845 /runtime/shims/xmem.cpp
parentf29ca7bacae9507f83e8c56abdb604bd4efb4cf1 (diff)
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
Merge main into fix/null-descriptor-generated-dir
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp52
1 files changed, 42 insertions, 10 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 40bc28f2a..384f7bd27 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;
@@ -145,7 +146,12 @@ void free_title_block(void* p) {
145 if (p) leonida::title_va_aliases().remove_title_memory(reinterpret_cast<uintptr_t>(p)); 146 if (p) leonida::title_va_aliases().remove_title_memory(reinterpret_cast<uintptr_t>(p));
146 const size_t size = block_size(p); 147 const size_t size = block_size(p);
147 free_block(p); 148 free_block(p);
148 if (p) leonida::title_memory_map::changed(p, size ? size : 1); 149 if (p) {
150 const auto from = reinterpret_cast<uintptr_t>(p);
151 const size_t bytes = size ? size : 1;
152 leonida::title_memory_map::changed(p, bytes);
153 leonida::geometry_writes::note_released(from, from + bytes);
154 }
149} 155}
150 156
151void* __stdcall XMemAlloc(size_t size, uint64_t attributes) { 157void* __stdcall XMemAlloc(size_t size, uint64_t attributes) {
@@ -391,7 +397,7 @@ uintptr_t stream_cpu, stream_gpu, stream_size;
391bool mirror_stream; 397bool mirror_stream;
392leonida::host_heap::map<uintptr_t, Window> stream_offsets; 398leonida::host_heap::map<uintptr_t, Window> stream_offsets;
393std::mutex g_phys_mutex; 399std::mutex g_phys_mutex;
394std::map<uintptr_t, Window> g_windows; // base -> window 400leonida::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 401// 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, 402// (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. 403// read and written under g_fault_lock.
@@ -400,7 +406,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 406// 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). 407// 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; 408leonida::host_heap::map<uintptr_t, Window> g_title_placeholders;
403std::unordered_map<uint64_t, PhysicalPage> g_pages; // page number -> backing 409// Keep page table nodes on the runtime heap alongside the window table.
410std::unordered_map<uint64_t, PhysicalPage, std::hash<uint64_t>, std::equal_to<uint64_t>,
411 leonida::host_heap::Allocator<std::pair<const uint64_t, PhysicalPage>>>
412 g_pages; // page number -> backing
404std::vector<uint64_t> g_free_slots; 413std::vector<uint64_t> g_free_slots;
405uint64_t g_next_slot = 1; 414uint64_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 415// A mapped run: `count` pages of one section at consecutive offsets, mapped as one view at its base. One view per run
@@ -797,10 +806,15 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type,
797 const DWORD host_type = allocation_type & kXmemHostAllocationTypeMask; 806 const DWORD host_type = allocation_type & kXmemHostAllocationTypeMask;
798 if (result && host_type && !(host_type & MEM_COMMIT)) { 807 if (result && host_type && !(host_type & MEM_COMMIT)) {
799 const bool gpu_only = (protect & kPageGraphicsMask) && host_protect(protect) == PAGE_NOACCESS; 808 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)) 809 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", 810 const bool stream_queue_view = allocation_type == 0x20402000 && gpu_only &&
811 reinterpret_cast<uintptr_t>(_ReturnAddress()) - kTitleImage == 0x1e1d03e &&
812 rt::xmem::slot31_runs() && rt::xmem::queue_page_mappings_enabled();
813 if (leonida::title_gpu_views::note_reservation(base, size, gpu_only, stream_queue_view))
814 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), 815 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)))); 816 reinterpret_cast<void*>(leonida::title_gpu_views::other_view(base)),
817 stream_queue_view ? "separate queue mappings" : "CPU backing");
804 else if (gpu_only && size >= (64u << 20)) 818 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, 819 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)); 820 static_cast<unsigned long long>(size >> 20), static_cast<unsigned long>(protect));
@@ -1823,8 +1837,13 @@ void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint
1823 const uintptr_t end = start + page_count * kPhysicalPageSize; 1837 const uintptr_t end = start + page_count * kPhysicalPageSize;
1824 const UnmapCause cause(page_array ? "XMemMapPhysicalPages (remap)" : "XMemMapPhysicalPages (unmap)"); 1838 const UnmapCause cause(page_array ? "XMemMapPhysicalPages (remap)" : "XMemMapPhysicalPages (unmap)");
1825 if (!page_array) { 1839 if (!page_array) {
1826 const FaultLockExclusive view_lock; 1840 bool ok = false;
1827 return unmap_range_locked(start, end) ? address : nullptr; 1841 {
1842 const FaultLockExclusive view_lock;
1843 ok = unmap_range_locked(start, end);
1844 }
1845 if (ok) leonida::geometry_writes::note_released(start, end);
1846 return ok ? address : nullptr;
1828 } 1847 }
1829 const Window* const window = find_window(start, page_count * kPhysicalPageSize); 1848 const Window* const window = find_window(start, page_count * kPhysicalPageSize);
1830 if (!window) { 1849 if (!window) {
@@ -2196,6 +2215,14 @@ BOOL title_release(void* address, SIZE_T size, DWORD free_type, const void* call
2196BOOL title_free_impl(LPVOID address, SIZE_T size, DWORD free_type) { 2215BOOL title_free_impl(LPVOID address, SIZE_T size, DWORD free_type) {
2197 drain_fault_notes(); 2216 drain_fault_notes();
2198 const bool releases = (free_type & MEM_RELEASE) && !(free_type & (MEM_PRESERVE_PLACEHOLDER | MEM_COALESCE_PLACEHOLDERS)); 2217 const bool releases = (free_type & MEM_RELEASE) && !(free_type & (MEM_PRESERVE_PLACEHOLDER | MEM_COALESCE_PLACEHOLDERS));
2218 const uintptr_t start = reinterpret_cast<uintptr_t>(address);
2219 uintptr_t release_end = 0;
2220 if (releases && address) allocation_extent(address, release_end);
2221 uintptr_t decommit_end = 0;
2222 if ((free_type & MEM_DECOMMIT) && address) {
2223 decommit_end = size ? start + size : leonida::title_write_watch::end_of(start);
2224 if (decommit_end <= start) allocation_extent(address, decommit_end);
2225 }
2199 BOOL result; 2226 BOOL result;
2200 if (releases) { 2227 if (releases) {
2201 result = title_release(address, size, free_type, _ReturnAddress()); 2228 result = title_release(address, size, free_type, _ReturnAddress());
@@ -2213,6 +2240,10 @@ BOOL title_free_impl(LPVOID address, SIZE_T size, DWORD free_type) {
2213 if (result && (free_type & MEM_DECOMMIT)) note_unwatched_change(address, size, 0, true); 2240 if (result && (free_type & MEM_DECOMMIT)) note_unwatched_change(address, size, 0, true);
2214 if (result && (free_type & MEM_RELEASE) && !(free_type & MEM_PRESERVE_PLACEHOLDER)) 2241 if (result && (free_type & MEM_RELEASE) && !(free_type & MEM_PRESERVE_PLACEHOLDER))
2215 leonida::title_write_watch::remove(reinterpret_cast<uintptr_t>(address)); 2242 leonida::title_write_watch::remove(reinterpret_cast<uintptr_t>(address));
2243 if (result && releases && release_end > start)
2244 leonida::geometry_writes::note_released(start, release_end);
2245 if (result && (free_type & MEM_DECOMMIT) && decommit_end > start)
2246 leonida::geometry_writes::note_released(start, decommit_end);
2216 return result; 2247 return result;
2217} 2248}
2218 2249
@@ -2434,6 +2465,7 @@ bool copy_page_mappings(std::uint32_t count, const PageBatch* batches, std::uint
2434 if (!mirror && r.spec.kind == 2) { 2465 if (!mirror && r.spec.kind == 2) {
2435 const UnmapCause cause("CopyPageMappingsBatchX (unmap range)"); 2466 const UnmapCause cause("CopyPageMappingsBatchX (unmap range)");
2436 ok = unmap_range(r.at, r.at + bytes); 2467 ok = unmap_range(r.at, r.at + bytes);
2468 if (ok) leonida::geometry_writes::note_released(r.at, r.at + bytes);
2437 } else if (!mirror && !slot31_runs()) { 2469 } else if (!mirror && !slot31_runs()) {
2438 for (uint32_t j = 0; j < r.spec.count; ++j) { 2470 for (uint32_t j = 0; j < r.spec.count; ++j) {
2439 const auto at = r.at + uint64_t(j) * kPhysicalPageSize; 2471 const auto at = r.at + uint64_t(j) * kPhysicalPageSize;