diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 05:49:06 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 05:49:06 -0500 |
| commit | 4f00daef503c04cc3a96e853b14f3b68319bcf14 (patch) | |
| tree | 2af3dd55a7266af624a2e228c7841dd25785b4a1 /runtime/shims/xmem.cpp | |
| parent | edb0f8502f0c0fbaaa1b2f8969877203d9005735 (diff) | |
WIP: step C (slot-133 buffers on the mirror, write-back of mirrored pages, XMemAlloc heap placed)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 69a0b9b44..9b8e9c169 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -94,8 +94,27 @@ HANDLE title_heap() { | |||
| 94 | // Set LEONIDA_TITLE_HEAP=0 to use the CRT heap. | 94 | // Set LEONIDA_TITLE_HEAP=0 to use the CRT heap. |
| 95 | const char* v = std::getenv("LEONIDA_TITLE_HEAP"); | 95 | const char* v = std::getenv("LEONIDA_TITLE_HEAP"); |
| 96 | if (v && std::strcmp(v, "0") == 0) return HANDLE(nullptr); | 96 | if (v && std::strcmp(v, "0") == 0) return HANDLE(nullptr); |
| 97 | const HANDLE h = HeapCreate(0, 0, 0); | 97 | // In the page mirror's range (src/title_mirror_space.h), so a V# or pointer the title writes into a block |
| 98 | std::fprintf(stderr, "[rt] xmem: XMemAlloc blocks come from a heap of their own (%p)\n", h); | 98 | // reaches the mirror: a heap over a reservation there (ntdll's RtlCreateHeap with a base), up to kTitleHeapBytes. |
| 99 | // The host's own heap where that is refused. | ||
| 100 | HANDLE h = nullptr; | ||
| 101 | using RtlCreateHeapFn = PVOID(NTAPI*)(ULONG, PVOID, SIZE_T, SIZE_T, PVOID, PVOID); | ||
| 102 | const auto create = reinterpret_cast<RtlCreateHeapFn>( | ||
| 103 | reinterpret_cast<void*>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlCreateHeap"))); | ||
| 104 | constexpr SIZE_T kTitleHeapBytes = SIZE_T(8) << 30; | ||
| 105 | if (create) | ||
| 106 | leonida::title_mirror_space::take(kTitleHeapBytes, 0x10000, true, [&](std::uint64_t at) { | ||
| 107 | void* const base = VirtualAlloc(reinterpret_cast<void*>(static_cast<uintptr_t>(at)), kTitleHeapBytes, | ||
| 108 | MEM_RESERVE, PAGE_READWRITE); | ||
| 109 | if (!base) return false; // something of the host's there: the next address | ||
| 110 | h = create(0x2 /* HEAP_GROWABLE */, base, kTitleHeapBytes, 0, nullptr, nullptr); | ||
| 111 | if (!h) VirtualFree(base, 0, MEM_RELEASE); // refused with a base: not tried again | ||
| 112 | return true; | ||
| 113 | }); | ||
| 114 | const bool placed = h != nullptr; | ||
| 115 | if (!h) h = HeapCreate(0, 0, 0); | ||
| 116 | std::fprintf(stderr, "[rt] xmem: XMemAlloc blocks come from a heap of their own (%p, %s)\n", h, | ||
| 117 | placed ? "in the page mirror's range" : "placed by the host"); | ||
| 99 | return h; | 118 | return h; |
| 100 | }(); | 119 | }(); |
| 101 | return heap; | 120 | return heap; |