diff options
| author | Kam471 <115741199+Kam471@users.noreply.github.com> | 2026-11-21 17:09:33 +0000 |
|---|---|---|
| committer | Kam471 <115741199+Kam471@users.noreply.github.com> | 2026-11-21 17:23:05 +0000 |
| commit | 34d2e63c65e66bd70fbd2766fb48a3a9cb57e651 (patch) | |
| tree | c593a200e986799a40ac7a3e9fa0a64d23b3e4af /runtime/shims/xmem.cpp | |
| parent | 718b714871e07ea1aa156095d45a0c8f9ff4794e (diff) | |
Runtime: match the measured Xbox xmem/d3d12_xs ABI
- xmem: XMemVirtualQuery(process, address, out64) fills the known MEMORY_BASIC_INFORMATION prefix and zeroes the rest; the working-set size goes at +8 with a TRUE return; allocator alignment and size-0 behaviour are documented.
- d3d12_xs: copy root-signature descriptors and clear the Xbox high flag byte (0x03000000) before forwarding to the PC serializer; add the legacy serializer; keep the deserializer.
- d3d12_xs: add D3D12XboxCreateDevice, create a PC device and log the 24-word params block. The Xbox ABI proxy is not implemented; measured slot overrides are documented as TODO and logged at runtime.
- test: root_sig_test serializes synthetic flags=0x03000000 descriptors through the PC d3d12.dll, checks the untouched copy is rejected and the cleared copy is accepted, and exercises the registered shim.
Clean-room: implemented from an ABI-facts spec derived from public write-ups; no source from americas-recomp or AnyPS5 was read or copied.
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 76 |
1 files changed, 59 insertions, 17 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index d8fd76568..215ffbe22 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -1,12 +1,21 @@ | |||
| 1 | // xmem.dll: the GDK's tracked allocator. Mapped onto the CRT's aligned heap and VirtualQuery. | 1 | // xmem.dll: the GDK's tracked allocator. Mapped onto the CRT's aligned heap and VirtualQueryEx. |
| 2 | // | 2 | // |
| 3 | // TODO: decode the XALLOC_ATTRIBUTES alignment and memory-type fields. Until that's checked against real call | 3 | // The measured ABI (docs from the clean-room spec) shows four imports: |
| 4 | // sites, every block is 64-byte aligned, which covers cache-line and SIMD alignment. | 4 | // XMemAlloc(size, attributes) |
| 5 | // XMemFree(ptr, attributes) | ||
| 6 | // XMemVirtualQuery(process, address, out64) | ||
| 7 | // XMemGetWorkingSetStatistics(out) | ||
| 8 | // | ||
| 9 | // ASSUMPTION: the attribute word is not decoded by the measured call sites (one passes 0x5e50000). Until a call | ||
| 10 | // site shows the alignment/type fields, every block is 64-byte aligned (covers cache lines and SIMD) and a size of | ||
| 11 | // zero is rounded up to one byte so the result is a stable, non-null pointer that XMemFree can release. | ||
| 5 | #include <windows.h> | 12 | #include <windows.h> |
| 6 | #include <malloc.h> | 13 | #include <malloc.h> |
| 7 | #include <psapi.h> | 14 | #include <psapi.h> |
| 8 | 15 | ||
| 16 | #include <atomic> | ||
| 9 | #include <cstdint> | 17 | #include <cstdint> |
| 18 | #include <cstdio> | ||
| 10 | #include <cstring> | 19 | #include <cstring> |
| 11 | 20 | ||
| 12 | #include "shims.h" | 21 | #include "shims.h" |
| @@ -19,23 +28,56 @@ void* __stdcall XMemAlloc(size_t size, uint64_t /*attributes*/) { return _aligne | |||
| 19 | 28 | ||
| 20 | void __stdcall XMemFree(void* p, uint64_t /*attributes*/) { _aligned_free(p); } | 29 | void __stdcall XMemFree(void* p, uint64_t /*attributes*/) { _aligned_free(p); } |
| 21 | 30 | ||
| 22 | // Same shape as VirtualQuery on the console (MEMORY_BASIC_INFORMATION out, returns bytes written). | 31 | // Measured call sites pass a zeroed 64-byte structure. Only the MEMORY_BASIC_INFORMATION-compatible prefix is |
| 23 | SIZE_T __stdcall XMemVirtualQuery(const void* address, MEMORY_BASIC_INFORMATION* info, SIZE_T length) { | 32 | // known: callers read at least the base and size fields. Fill that prefix from VirtualQueryEx and zero the rest. |
| 24 | return VirtualQuery(address, info, length); | 33 | constexpr size_t kVirtualQueryOutSize = 64; |
| 34 | static_assert(sizeof(MEMORY_BASIC_INFORMATION) <= kVirtualQueryOutSize, | ||
| 35 | "the 64-byte XMemVirtualQuery output must hold MEMORY_BASIC_INFORMATION"); | ||
| 36 | |||
| 37 | // Returns the number of bytes written (the VirtualQuery convention) or 0 on failure. The exact return type and the | ||
| 38 | // meaning of every output field are not fully known; callers in the package only test for success. | ||
| 39 | SIZE_T __stdcall XMemVirtualQuery(HANDLE process, const void* address, void* out) { | ||
| 40 | if (!out) return 0; | ||
| 41 | std::memset(out, 0, kVirtualQueryOutSize); | ||
| 42 | if (!address) return 0; | ||
| 43 | |||
| 44 | static std::atomic<bool> warned{false}; | ||
| 45 | if (!warned.exchange(true, std::memory_order_relaxed)) { | ||
| 46 | std::fprintf(stderr, | ||
| 47 | "[rt] xmem: XMemVirtualQuery: fields after MEMORY_BASIC_INFORMATION are unknown; " | ||
| 48 | "the 64-byte output is zero-filled there\n"); | ||
| 49 | } | ||
| 50 | |||
| 51 | // A null handle means the current process; GetCurrentProcess() is the (HANDLE)-1 pseudo handle. | ||
| 52 | HANDLE target = process ? process : GetCurrentProcess(); | ||
| 53 | MEMORY_BASIC_INFORMATION info{}; | ||
| 54 | SIZE_T written = VirtualQueryEx(target, address, &info, sizeof(info)); | ||
| 55 | if (!written) return 0; | ||
| 56 | |||
| 57 | std::memcpy(out, &info, written < kVirtualQueryOutSize ? written : kVirtualQueryOutSize); | ||
| 58 | return written; | ||
| 25 | } | 59 | } |
| 26 | 60 | ||
| 27 | // GDK: HRESULT XMemGetWorkingSetStatistics(XMEM_WORKING_SET_STATISTICS*). The struct's exact layout isn't known | 61 | // GDK: BOOL XMemGetWorkingSetStatistics(XMEM_WORKING_SET_STATISTICS*). The layout is not fully known. The FidelityFX |
| 28 | // yet, so it's zeroed up to a conservative size and filled with the process's working set in the first field. | 62 | // upscaler reads a 64-bit size at +8 and treats FALSE as "no data", so only the first 16 bytes are touched: zero at |
| 29 | // Revisit once the exe's call sites show which fields are read. | 63 | // +0 (unknown) and the process working set at +8. Do not claim more of the struct than is measured. |
| 30 | long __stdcall XMemGetWorkingSetStatistics(void* stats) { | 64 | BOOL __stdcall XMemGetWorkingSetStatistics(void* stats) { |
| 31 | if (!stats) return static_cast<long>(0x80070057); // E_INVALIDARG | 65 | if (!stats) { |
| 32 | std::memset(stats, 0, 32); | 66 | static std::atomic<bool> warned{false}; |
| 33 | PROCESS_MEMORY_COUNTERS pmc{}; | 67 | if (!warned.exchange(true, std::memory_order_relaxed)) { |
| 34 | if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) { | 68 | std::fprintf(stderr, "[rt] xmem: XMemGetWorkingSetStatistics(nullptr) -> FALSE\n"); |
| 35 | uint64_t ws = pmc.WorkingSetSize; | 69 | } |
| 36 | std::memcpy(stats, &ws, sizeof(ws)); | 70 | return FALSE; |
| 37 | } | 71 | } |
| 38 | return 0; // S_OK | 72 | |
| 73 | PROCESS_MEMORY_COUNTERS pmc{}; | ||
| 74 | if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) return FALSE; | ||
| 75 | |||
| 76 | uint8_t* bytes = static_cast<uint8_t*>(stats); | ||
| 77 | std::memset(bytes, 0, 16); | ||
| 78 | uint64_t working_set = pmc.WorkingSetSize; | ||
| 79 | std::memcpy(bytes + 8, &working_set, sizeof(working_set)); | ||
| 80 | return TRUE; | ||
| 39 | } | 81 | } |
| 40 | 82 | ||
| 41 | } // namespace | 83 | } // namespace |