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:
authorNolan Cotter <186326284+NolanCotter@users.noreply.github.com>2026-11-22 20:39:59 -0700
committerNolan Cotter <186326284+NolanCotter@users.noreply.github.com>2026-11-22 20:39:59 -0700
commitfa5f26db8eafd2cc27f627b68c0a5a5949bcf2ff (patch)
treefc57e238be829295fc216dff89a6d93e64c16b50 /runtime/shims/xmem.cpp
parent9b693ecd8a2f90799f6c47f8f06cf062feb9820c (diff)
parent7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff)
Merge origin/main: DXGI device fields coexist with the console-ABI device state
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp48
1 files changed, 44 insertions, 4 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index e925d3ee4..c7cfb76c0 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -10,6 +10,7 @@
10// site shows the alignment/type fields, every block is 64-byte aligned (covers cache lines and SIMD) and a size of 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. 11// zero is rounded up to one byte so the result is a stable, non-null pointer that XMemFree can release.
12#include <windows.h> 12#include <windows.h>
13#include <intrin.h>
13#include <malloc.h> 14#include <malloc.h>
14#include <psapi.h> 15#include <psapi.h>
15 16
@@ -18,16 +19,46 @@
18#include <cstdio> 19#include <cstdio>
19#include <cstring> 20#include <cstring>
20#include <limits> 21#include <limits>
22#include <mutex>
23#include <set>
24#include <utility>
21 25
22#include "shims.h" 26#include "shims.h"
27#include "startup_trace.h"
23 28
24namespace { 29namespace {
25 30
26constexpr size_t kAlign = 64; 31constexpr size_t kAlign = 64;
27 32
28void* __stdcall XMemAlloc(size_t size, uint64_t /*attributes*/) { return _aligned_malloc(size ? size : 1, kAlign); } 33// gta6rt --trace FILE: the first XMemAlloc and the first XMemFree for each distinct raw attribute word go to the
34// startup trace as an "xmem_attributes" event (api, the word, the size for an allocation, the caller as module+RVA). The
35// bits of the attribute word are not known yet; this records them from a real run without logging every allocation,
36// which would fill the file. Off: one relaxed load per call.
37void note_attributes(bool is_alloc, uint64_t attributes, size_t size, const void* caller) {
38 if (!rt::startup_trace::enabled()) return;
39 static std::mutex mutex;
40 static std::set<std::pair<bool, uint64_t>> seen;
41 {
42 const std::lock_guard lock(mutex);
43 if (!seen.insert({is_alloc, attributes}).second) return;
44 }
45 std::string fields = std::string("\"api\":\"") + (is_alloc ? "XMemAlloc" : "XMemFree") +
46 "\",\"attributes\":" + rt::startup_trace::quote(rt::startup_trace::hex(attributes));
47 if (is_alloc) fields += ",\"size\":" + std::to_string(size);
48 fields += ",\"caller\":" +
49 rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)));
50 rt::startup_trace::event("xmem_attributes", fields);
51}
29 52
30void __stdcall XMemFree(void* p, uint64_t /*attributes*/) { _aligned_free(p); } 53void* __stdcall XMemAlloc(size_t size, uint64_t attributes) {
54 note_attributes(true, attributes, size, _ReturnAddress());
55 return _aligned_malloc(size ? size : 1, kAlign);
56}
57
58void __stdcall XMemFree(void* p, uint64_t attributes) {
59 note_attributes(false, attributes, 0, _ReturnAddress());
60 _aligned_free(p);
61}
31 62
32// Measured call sites pass a zeroed 64-byte structure. Only the MEMORY_BASIC_INFORMATION-compatible prefix is 63// Measured call sites pass a zeroed 64-byte structure. Only the MEMORY_BASIC_INFORMATION-compatible prefix is
33// known: callers read at least the base and size fields. Fill that prefix from VirtualQueryEx and zero the rest. 64// known: callers read at least the base and size fields. Fill that prefix from VirtualQueryEx and zero the rest.
@@ -64,8 +95,10 @@ SIZE_T __stdcall XMemVirtualQuery(HANDLE process, const void* address, void* out
64// 0x75b1): ECX is the constant 0x20000001 (its meaning is not known) and RDX points at a stack buffer (0x80 bytes 95// 0x75b1): ECX is the constant 0x20000001 (its meaning is not known) and RDX points at a stack buffer (0x80 bytes
65// where the frame shows it). The caller reads a 64-bit size at +8 and treats a zero return as "no data". Taking the 96// where the frame shows it). The caller reads a 64-bit size at +8 and treats a zero return as "no data". Taking the
66// pointer from the first argument wrote to address 0x20000001. 97// pointer from the first argument wrote to address 0x20000001.
67// Only the first 16 bytes are touched: zero at +0 (unknown) and the process working set at +8. Do not claim more of 98// The measured FSR sites establish argument shape, kind and the 64-bit working-set read at +8; +0's title-total memory
68// the struct than is measured. 99// limit interpretation is inferred from other Series implementations (xcl, xbox-emulator) and title engine telemetry
100// sizing memory pools (RT-220). Note that if GlobalMemoryStatusEx reports 0 physical memory, XMem falls back to the
101// 12 GiB title limit, whereas XSystemGetAppMemoryUsageLimit remains 0.
69BOOL __stdcall XMemGetWorkingSetStatistics(uint32_t /*kind*/, void* stats) { 102BOOL __stdcall XMemGetWorkingSetStatistics(uint32_t /*kind*/, void* stats) {
70 if (!stats) { 103 if (!stats) {
71 static std::atomic<bool> warned{false}; 104 static std::atomic<bool> warned{false};
@@ -78,8 +111,15 @@ BOOL __stdcall XMemGetWorkingSetStatistics(uint32_t /*kind*/, void* stats) {
78 PROCESS_MEMORY_COUNTERS pmc{}; 111 PROCESS_MEMORY_COUNTERS pmc{};
79 if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) return FALSE; 112 if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) return FALSE;
80 113
114 MEMORYSTATUSEX ms{sizeof(ms)};
115 uint64_t total_memory = 12ULL * 1024 * 1024 * 1024;
116 if (GlobalMemoryStatusEx(&ms) && ms.ullTotalPhys != 0) {
117 total_memory = ms.ullTotalPhys;
118 }
119
81 uint8_t* bytes = static_cast<uint8_t*>(stats); 120 uint8_t* bytes = static_cast<uint8_t*>(stats);
82 std::memset(bytes, 0, 16); 121 std::memset(bytes, 0, 16);
122 std::memcpy(bytes + 0, &total_memory, sizeof(total_memory));
83 uint64_t working_set = pmc.WorkingSetSize; 123 uint64_t working_set = pmc.WorkingSetSize;
84 std::memcpy(bytes + 8, &working_set, sizeof(working_set)); 124 std::memcpy(bytes + 8, &working_set, sizeof(working_set));
85 return TRUE; 125 return TRUE;