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:
authoredupode <30982306+edupode@users.noreply.github.com>2026-11-22 16:11:40 +0000
committeredupode <30982306+edupode@users.noreply.github.com>2026-11-22 16:11:40 +0000
commiteb09c40f125ab8c24f5bc0dfb0ce6cd9890b8eaa (patch)
tree5a6fcd5e6e074581c2042d5309b7161503b6ad9d /runtime/shims/xmem.cpp
parent4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff)
runtime: add XMem virtual memory watch exports
Expose XMemVirtualAlloc and the write-watch queries through the xmem shim. Map ordinary and 64 KiB allocations to host virtual memory, and reject unsupported page modes instead of silently using the wrong alignment.
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index ea9385c74..ff793996d 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -17,6 +17,7 @@
17#include <cstdint> 17#include <cstdint>
18#include <cstdio> 18#include <cstdio>
19#include <cstring> 19#include <cstring>
20#include <limits>
20 21
21#include "shims.h" 22#include "shims.h"
22 23
@@ -84,6 +85,97 @@ BOOL __stdcall XMemGetWorkingSetStatistics(uint32_t /*kind*/, void* stats) {
84 return TRUE; 85 return TRUE;
85} 86}
86 87
88// XMem allocation-type page bits select the guest page granularity and must not be forwarded as Win32 flags.
89constexpr DWORD kXmemLargePages = 0x20000000;
90constexpr DWORD kXmem64kPages = 0x20400000;
91constexpr DWORD kXmemPageModeMask = kXmem64kPages;
92constexpr SIZE_T kXmemPageSize64k = 0x10000;
93constexpr DWORD kXmemHostAllocationTypeMask =
94 MEM_COMMIT | MEM_RESERVE | MEM_RESET | MEM_TOP_DOWN | MEM_WRITE_WATCH | 0x01000000;
95
96bool round_up_size(SIZE_T value, SIZE_T alignment, SIZE_T& rounded) {
97 const SIZE_T max = (std::numeric_limits<SIZE_T>::max)();
98 if (alignment == 0 || (alignment & (alignment - 1)) != 0 || value > max - (alignment - 1)) return false;
99 rounded = (value + alignment - 1) & ~(alignment - 1);
100 return true;
101}
102
103void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) {
104 // Host-backed support covers ordinary and 64 KiB guest reservations. The 2 MiB guest mode needs an aligned
105 // reservation strategy that this shim does not provide, so fail explicitly instead of returning a misaligned
106 // region. Attribute bits are accepted but have no host mapping yet.
107 if (size == 0) {
108 SetLastError(ERROR_INVALID_PARAMETER);
109 return nullptr;
110 }
111
112 const DWORD page_mode = allocation_type & kXmemPageModeMask;
113 if (page_mode == kXmemLargePages) {
114 SetLastError(ERROR_NOT_SUPPORTED);
115 return nullptr;
116 }
117 if (page_mode != 0 && page_mode != kXmem64kPages) {
118 SetLastError(ERROR_INVALID_PARAMETER);
119 return nullptr;
120 }
121
122 const DWORD unsupported = allocation_type & ~(kXmemHostAllocationTypeMask | kXmemPageModeMask);
123 if (unsupported != 0) {
124 SetLastError(ERROR_INVALID_PARAMETER);
125 return nullptr;
126 }
127
128 DWORD host_type = allocation_type & kXmemHostAllocationTypeMask;
129 if (host_type == 0) host_type = MEM_RESERVE | MEM_COMMIT;
130
131 SYSTEM_INFO system_info{};
132 GetSystemInfo(&system_info);
133 const SIZE_T page_granularity = page_mode == kXmem64kPages ? kXmemPageSize64k : system_info.dwPageSize;
134 const SIZE_T base_granularity =
135 page_mode == kXmem64kPages
136 ? kXmemPageSize64k
137 : ((host_type & MEM_RESERVE) ? system_info.dwAllocationGranularity : system_info.dwPageSize);
138 if (page_granularity == 0 || (page_granularity & (page_granularity - 1)) != 0 || base_granularity == 0 ||
139 (base_granularity & (base_granularity - 1)) != 0) {
140 SetLastError(ERROR_INVALID_PARAMETER);
141 return nullptr;
142 }
143
144 if (address != nullptr) {
145 const uintptr_t requested = reinterpret_cast<uintptr_t>(address);
146 const uintptr_t max = (std::numeric_limits<uintptr_t>::max)();
147 if (size > max - requested) {
148 SetLastError(ERROR_INVALID_PARAMETER);
149 return nullptr;
150 }
151 const uintptr_t requested_end = requested + size;
152 const uintptr_t base = requested & ~(static_cast<uintptr_t>(base_granularity) - 1);
153 if (requested_end > max - (static_cast<uintptr_t>(page_granularity) - 1)) {
154 SetLastError(ERROR_INVALID_PARAMETER);
155 return nullptr;
156 }
157 const uintptr_t end = (requested_end + static_cast<uintptr_t>(page_granularity) - 1) &
158 ~(static_cast<uintptr_t>(page_granularity) - 1);
159 address = reinterpret_cast<void*>(base);
160 size = static_cast<SIZE_T>(end - base);
161 } else if (page_mode == kXmem64kPages && !round_up_size(size, page_granularity, size)) {
162 SetLastError(ERROR_INVALID_PARAMETER);
163 return nullptr;
164 }
165
166 (void)attributes;
167 return VirtualAlloc(address, size, host_type, protect);
168}
169
170UINT WINAPI XMemGetWriteWatch(DWORD flags, void* base_address, SIZE_T region_size, void** addresses, ULONG_PTR* count,
171 ULONG* granularity) {
172 return GetWriteWatch(flags, base_address, region_size, addresses, count, granularity);
173}
174
175UINT WINAPI XMemResetWriteWatch(void* base_address, SIZE_T region_size) {
176 return ResetWriteWatch(base_address, region_size);
177}
178
87} // namespace 179} // namespace
88 180
89SHIM_TABLE(xmem, 181SHIM_TABLE(xmem,
@@ -91,4 +183,7 @@ SHIM_TABLE(xmem,
91 {"xmem.dll", "XMemFree", reinterpret_cast<void*>(&XMemFree)}, 183 {"xmem.dll", "XMemFree", reinterpret_cast<void*>(&XMemFree)},
92 {"xmem.dll", "XMemVirtualQuery", reinterpret_cast<void*>(&XMemVirtualQuery)}, 184 {"xmem.dll", "XMemVirtualQuery", reinterpret_cast<void*>(&XMemVirtualQuery)},
93 {"xmem.dll", "XMemGetWorkingSetStatistics", reinterpret_cast<void*>(&XMemGetWorkingSetStatistics)}, 185 {"xmem.dll", "XMemGetWorkingSetStatistics", reinterpret_cast<void*>(&XMemGetWorkingSetStatistics)},
186 {"xmem.dll", "XMemVirtualAlloc", reinterpret_cast<void*>(&XMemVirtualAlloc)},
187 {"xmem.dll", "XMemGetWriteWatch", reinterpret_cast<void*>(&XMemGetWriteWatch)},
188 {"xmem.dll", "XMemResetWriteWatch", reinterpret_cast<void*>(&XMemResetWriteWatch)},
94); 189);