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:
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp79
1 files changed, 76 insertions, 3 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 69a0b9b44..6a0f36e15 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -46,6 +46,7 @@
46#include "../src/dstorage_trace.h" 46#include "../src/dstorage_trace.h"
47#include "../src/gpu_label_memory.h" 47#include "../src/gpu_label_memory.h"
48#include "../src/title_frames.h" 48#include "../src/title_frames.h"
49#include "../src/title_heap.h"
49#include "../src/title_mirror_space.h" 50#include "../src/title_mirror_space.h"
50#include "../src/title_page_watch.h" 51#include "../src/title_page_watch.h"
51#include "../src/title_geometry_watch.h" 52#include "../src/title_geometry_watch.h"
@@ -83,12 +84,57 @@ void note_attributes(const char* api, uint64_t attributes, size_t size, const vo
83// The title's XMemAlloc blocks come from a heap of their own instead of the CRT heap (LEONIDA_TITLE_HEAP=0: not). Slot-133 84// The title's XMemAlloc blocks come from a heap of their own instead of the CRT heap (LEONIDA_TITLE_HEAP=0: not). Slot-133
84// imports pin title memory page by page for the GPU, and on the CRT heap a pinned page also holds the runtime's own 85// imports pin title memory page by page for the GPU, and on the CRT heap a pinned page also holds the runtime's own
85// objects (g_pages' nodes among them): a GPU store past a buffer's end, or into a block freed while its import still 86// objects (g_pages' nodes among them): a GPU store past a buffer's end, or into a block freed while its import still
86// lived, then corrupted the runtime (speed-30/41/49-52: g_pages found corrupt under g_phys_mutex). Each block keeps its 87// lived, then corrupted the runtime (speed-30/41/49-52: g_pages found corrupt under g_phys_mutex).
87// size and the heap allocation it sits in just below it. 88//
89// The heap is the runtime's own (src/title_heap.h) over a reservation in the page mirror's range
90// (src/title_mirror_space.h), so a V# or pointer the title writes into a block reaches the mirror at heap_base + the
91// block's address, and its committed pages are fed to the mirror as they are committed (they read the title's bytes
92// there, not zeros). RtlCreateHeap over a given base faults under Wine. Where the range cannot take the reservation, a
93// heap the host places, as before (each block keeps its size and the heap allocation it sits in just below it).
88struct TitleHeapHeader { 94struct TitleHeapHeader {
89 void* raw; 95 void* raw;
90 size_t size; 96 size_t size;
91}; 97};
98constexpr size_t kTitleHeapBytes = size_t(32) << 30; // address range only: pages are committed as blocks need them
99
100// Tells the page mirror that the pages of [at, at + bytes) of the heap are committed (each shows itself) or not any
101// more (defined with the feed, below).
102void feed_heap_pages(uintptr_t at, size_t bytes, bool committed);
103
104leonida::TitleHeap& mirror_heap() {
105 static leonida::TitleHeap heap;
106 return heap;
107}
108bool mirror_heap_ready() {
109 static const bool ready = [] {
110 const char* v = std::getenv("LEONIDA_TITLE_HEAP");
111 if (v && std::strcmp(v, "0") == 0) return false;
112 uintptr_t base = 0;
113 // Not "plain": every committed page is fed to the mirror as it is committed, so a pointer the title wrote reads
114 // its bytes without anything naming the range first.
115 leonida::title_mirror_space::take(kTitleHeapBytes, 0x10000, false, [&](std::uint64_t at) {
116 void* const made = VirtualAlloc(reinterpret_cast<void*>(static_cast<uintptr_t>(at)), kTitleHeapBytes,
117 MEM_RESERVE, PAGE_READWRITE);
118 base = reinterpret_cast<uintptr_t>(made);
119 return made != nullptr;
120 });
121 leonida::TitleHeap::Hooks hooks;
122 hooks.commit = [](uintptr_t at, size_t bytes) {
123 if (!VirtualAlloc(reinterpret_cast<void*>(at), bytes, MEM_COMMIT, PAGE_READWRITE)) return false;
124 feed_heap_pages(at, bytes, true);
125 return true;
126 };
127 hooks.decommit = [](uintptr_t at, size_t bytes) {
128 feed_heap_pages(at, bytes, false);
129 VirtualFree(reinterpret_cast<void*>(at), bytes, MEM_DECOMMIT);
130 };
131 const bool ok = base && mirror_heap().init(base, kTitleHeapBytes, std::move(hooks));
132 std::fprintf(stderr, "[rt] xmem: XMemAlloc blocks come from %s\n",
133 ok ? "the runtime's heap in the page mirror's range" : "a heap the host places");
134 return ok;
135 }();
136 return ready;
137}
92HANDLE title_heap() { 138HANDLE title_heap() {
93 static const HANDLE heap = [] { 139 static const HANDLE heap = [] {
94 // Set LEONIDA_TITLE_HEAP=0 to use the CRT heap. 140 // Set LEONIDA_TITLE_HEAP=0 to use the CRT heap.
@@ -102,8 +148,10 @@ HANDLE title_heap() {
102} 148}
103void* allocate_block(size_t size) { 149void* allocate_block(size_t size) {
104 size = size ? size : 1; 150 size = size ? size : 1;
151 if (mirror_heap_ready())
152 if (void* p = mirror_heap().allocate(size)) return p; // full: the host's heap, as before
105 const HANDLE heap = title_heap(); 153 const HANDLE heap = title_heap();
106 if (!heap) return _aligned_malloc(size, kAlign); 154 if (!heap) return _aligned_malloc(size, kAlign); // LEONIDA_TITLE_HEAP=0
107 const size_t extra = kAlign + sizeof(TitleHeapHeader); 155 const size_t extra = kAlign + sizeof(TitleHeapHeader);
108 if (size > SIZE_MAX - extra) return nullptr; 156 if (size > SIZE_MAX - extra) return nullptr;
109 void* raw = HeapAlloc(heap, 0, size + extra); 157 void* raw = HeapAlloc(heap, 0, size + extra);
@@ -116,11 +164,16 @@ void* allocate_block(size_t size) {
116} 164}
117size_t block_size(void* p) { 165size_t block_size(void* p) {
118 if (!p) return 0; 166 if (!p) return 0;
167 if (mirror_heap_ready() && mirror_heap().owns(p)) return mirror_heap().size_of(p);
119 if (!title_heap()) return _aligned_msize(p, kAlign, 0); 168 if (!title_heap()) return _aligned_msize(p, kAlign, 0);
120 return (reinterpret_cast<TitleHeapHeader*>(p) - 1)->size; 169 return (reinterpret_cast<TitleHeapHeader*>(p) - 1)->size;
121} 170}
122void free_block(void* p) { 171void free_block(void* p) {
123 if (!p) return; 172 if (!p) return;
173 if (mirror_heap_ready() && mirror_heap().owns(p)) {
174 mirror_heap().release(p);
175 return;
176 }
124 const HANDLE heap = title_heap(); 177 const HANDLE heap = title_heap();
125 if (!heap) return _aligned_free(p); 178 if (!heap) return _aligned_free(p);
126 HeapFree(heap, 0, (reinterpret_cast<TitleHeapHeader*>(p) - 1)->raw); 179 HeapFree(heap, 0, (reinterpret_cast<TitleHeapHeader*>(p) - 1)->raw);
@@ -581,6 +634,16 @@ HostUnorderedMap<uintptr_t, TableFrame> g_table_frames;
581leonida::title_frames::Feed g_mirror_feed = nullptr; 634leonida::title_frames::Feed g_mirror_feed = nullptr;
582void* g_mirror_context = nullptr; 635void* g_mirror_context = nullptr;
583 636
637// The heap's committed pages show themselves (title_heap): fed to the page mirror as they are committed and
638// decommitted, under g_phys_mutex like every other change of the feed. Called with the heap's lock held (heap, then
639// g_phys_mutex; attach_mirror takes the heap's lock only after releasing g_phys_mutex).
640void feed_heap_pages(uintptr_t at, size_t bytes, bool committed) {
641 const std::lock_guard lock(g_phys_mutex);
642 if (!g_mirror_feed) return;
643 for (uintptr_t page = at & ~uintptr_t(kPhysicalPageSize - 1); page < at + bytes; page += kPhysicalPageSize)
644 g_mirror_feed(g_mirror_context, page, committed ? reinterpret_cast<uint8_t*>(page) : nullptr, {});
645}
646
584// With g_phys_mutex held: the page at `address` shows physical page `page` now (0: nothing), for the page mirror. 647// With g_phys_mutex held: the page at `address` shows physical page `page` now (0: nothing), for the page mirror.
585void feed_mirror(uintptr_t address, uint64_t page) { 648void feed_mirror(uintptr_t address, uint64_t page) {
586 if (!g_mirror_feed || !leonida::title_mirror_space::contains(address)) return; 649 if (!g_mirror_feed || !leonida::title_mirror_space::contains(address)) return;
@@ -2922,7 +2985,17 @@ void slot31_mapping_counts(Slot31Counts& out) {
2922// their physical pages' host addresses. 2985// their physical pages' host addresses.
2923// title_frames' Attach (src/title_frames.h): the page mirror's feed from now on, after every page of the mirror's range 2986// title_frames' Attach (src/title_frames.h): the page mirror's feed from now on, after every page of the mirror's range
2924// mapped now (the views, then the table pages), all under g_phys_mutex. 2987// mapped now (the views, then the table pages), all under g_phys_mutex.
2988void attach_mirror_locked(void* context, leonida::title_frames::Feed feed);
2925void attach_mirror(void* context, leonida::title_frames::Feed feed) { 2989void attach_mirror(void* context, leonida::title_frames::Feed feed) {
2990 attach_mirror_locked(context, feed);
2991 if (!feed || !mirror_heap_ready()) return;
2992 // The heap's committed pages, after g_phys_mutex (the heap takes it under its own lock). A page committed or
2993 // decommitted meanwhile was fed by the heap itself; one fed twice is mapped twice to the same bytes.
2994 std::vector<std::pair<uintptr_t, size_t>> committed;
2995 mirror_heap().for_each_committed([&](uintptr_t at, size_t bytes) { committed.emplace_back(at, bytes); });
2996 for (const auto& [at, bytes] : committed) feed_heap_pages(at, bytes, true);
2997}
2998void attach_mirror_locked(void* context, leonida::title_frames::Feed feed) {
2926 const std::lock_guard lock(g_phys_mutex); 2999 const std::lock_guard lock(g_phys_mutex);
2927 g_mirror_feed = feed; 3000 g_mirror_feed = feed;
2928 g_mirror_context = context; 3001 g_mirror_context = context;