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.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index ea9385c74..7d4a1bcfd 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,100 @@ 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. Nonzero attribute flags also fail until their host mapping is known.
107 if (size == 0) {
108 SetLastError(ERROR_INVALID_PARAMETER);
109 return nullptr;
110 }
111 if (attributes != 0) {
112 SetLastError(ERROR_NOT_SUPPORTED);
113 return nullptr;
114 }
115
116 const DWORD page_mode = allocation_type & kXmemPageModeMask;
117 if (page_mode == kXmemLargePages) {
118 SetLastError(ERROR_NOT_SUPPORTED);
119 return nullptr;
120 }
121 if (page_mode != 0 && page_mode != kXmem64kPages) {
122 SetLastError(ERROR_INVALID_PARAMETER);
123 return nullptr;
124 }
125
126 const DWORD unsupported = allocation_type & ~(kXmemHostAllocationTypeMask | kXmemPageModeMask);
127 if (unsupported != 0) {
128 SetLastError(ERROR_INVALID_PARAMETER);
129 return nullptr;
130 }
131
132 DWORD host_type = allocation_type & kXmemHostAllocationTypeMask;
133 if (host_type == 0) host_type = MEM_RESERVE | MEM_COMMIT;
134
135 SYSTEM_INFO system_info{};
136 GetSystemInfo(&system_info);
137 const SIZE_T page_granularity = page_mode == kXmem64kPages ? kXmemPageSize64k : system_info.dwPageSize;
138 const SIZE_T base_granularity =
139 page_mode == kXmem64kPages
140 ? kXmemPageSize64k
141 : ((host_type & MEM_RESERVE) ? system_info.dwAllocationGranularity : system_info.dwPageSize);
142 if (page_granularity == 0 || (page_granularity & (page_granularity - 1)) != 0 || base_granularity == 0 ||
143 (base_granularity & (base_granularity - 1)) != 0) {
144 SetLastError(ERROR_INVALID_PARAMETER);
145 return nullptr;
146 }
147
148 if (address != nullptr) {
149 const uintptr_t requested = reinterpret_cast<uintptr_t>(address);
150 const uintptr_t max = (std::numeric_limits<uintptr_t>::max)();
151 if (size > max - requested) {
152 SetLastError(ERROR_INVALID_PARAMETER);
153 return nullptr;
154 }
155 const uintptr_t requested_end = requested + size;
156 const uintptr_t base = requested & ~(static_cast<uintptr_t>(base_granularity) - 1);
157 if (requested_end > max - (static_cast<uintptr_t>(page_granularity) - 1)) {
158 SetLastError(ERROR_INVALID_PARAMETER);
159 return nullptr;
160 }
161 const uintptr_t end = (requested_end + static_cast<uintptr_t>(page_granularity) - 1) &
162 ~(static_cast<uintptr_t>(page_granularity) - 1);
163 address = reinterpret_cast<void*>(base);
164 size = static_cast<SIZE_T>(end - base);
165 } else if (page_mode == kXmem64kPages && !round_up_size(size, page_granularity, size)) {
166 SetLastError(ERROR_INVALID_PARAMETER);
167 return nullptr;
168 }
169
170 return VirtualAlloc(address, size, host_type, protect);
171}
172
173UINT WINAPI XMemGetWriteWatch(DWORD flags, void* base_address, SIZE_T region_size, void** addresses, ULONG_PTR* count,
174 ULONG* granularity) {
175 return GetWriteWatch(flags, base_address, region_size, addresses, count, granularity);
176}
177
178UINT WINAPI XMemResetWriteWatch(void* base_address, SIZE_T region_size) {
179 return ResetWriteWatch(base_address, region_size);
180}
181
87} // namespace 182} // namespace
88 183
89SHIM_TABLE(xmem, 184SHIM_TABLE(xmem,
@@ -91,4 +186,7 @@ SHIM_TABLE(xmem,
91 {"xmem.dll", "XMemFree", reinterpret_cast<void*>(&XMemFree)}, 186 {"xmem.dll", "XMemFree", reinterpret_cast<void*>(&XMemFree)},
92 {"xmem.dll", "XMemVirtualQuery", reinterpret_cast<void*>(&XMemVirtualQuery)}, 187 {"xmem.dll", "XMemVirtualQuery", reinterpret_cast<void*>(&XMemVirtualQuery)},
93 {"xmem.dll", "XMemGetWorkingSetStatistics", reinterpret_cast<void*>(&XMemGetWorkingSetStatistics)}, 188 {"xmem.dll", "XMemGetWorkingSetStatistics", reinterpret_cast<void*>(&XMemGetWorkingSetStatistics)},
189 {"xmem.dll", "XMemVirtualAlloc", reinterpret_cast<void*>(&XMemVirtualAlloc)},
190 {"xmem.dll", "XMemGetWriteWatch", reinterpret_cast<void*>(&XMemGetWriteWatch)},
191 {"xmem.dll", "XMemResetWriteWatch", reinterpret_cast<void*>(&XMemResetWriteWatch)},
94); 192);