1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// All files in the project carrying such notice may not be copied, modified, or distributed
// except according to those terms.
//! ApiSet Contract for api-ms-win-core-memory-l1-1-0
use ctypes::c_void;
use shared::basetsd::{PSIZE_T, PULONG_PTR, SIZE_T, ULONG64, ULONG_PTR};
use shared::minwindef::{
BOOL, DWORD, LPCVOID, LPDWORD, LPVOID, PBOOL, PDWORD, PULONG, UINT, ULONG,
};
use um::minwinbase::{LPSECURITY_ATTRIBUTES, PSECURITY_ATTRIBUTES};
use um::winnt::{
HANDLE, LPCWSTR, PCWSTR, PMEMORY_BASIC_INFORMATION, PVOID, SECTION_ALL_ACCESS,
SECTION_MAP_EXECUTE_EXPLICIT, SECTION_MAP_READ, SECTION_MAP_WRITE,
};
pub const FILE_MAP_WRITE: DWORD = SECTION_MAP_WRITE;
pub const FILE_MAP_READ: DWORD = SECTION_MAP_READ;
pub const FILE_MAP_ALL_ACCESS: DWORD = SECTION_ALL_ACCESS;
pub const FILE_MAP_EXECUTE: DWORD = SECTION_MAP_EXECUTE_EXPLICIT;
pub const FILE_MAP_COPY: DWORD = 0x00000001;
pub const FILE_MAP_RESERVE: DWORD = 0x80000000;
pub const FILE_MAP_TARGETS_INVALID: DWORD = 0x40000000;
pub const FILE_MAP_LARGE_PAGES: DWORD = 0x20000000;
extern "system" {
pub fn VirtualAlloc(
lpAddress: LPVOID,
dwSize: SIZE_T,
flAllocationType: DWORD,
flProtect: DWORD,
) -> LPVOID;
pub fn VirtualProtect(
lpAddress: LPVOID,
dwSize: SIZE_T,
flNewProtect: DWORD,
lpflOldProtect: PDWORD,
) -> BOOL;
pub fn VirtualFree(
lpAddress: LPVOID,
dwSize: SIZE_T,
dwFreeType: DWORD,
) -> BOOL;
pub fn VirtualQuery(
lpAddress: LPCVOID,
lpBuffer: PMEMORY_BASIC_INFORMATION,
dwLength: SIZE_T,
) -> SIZE_T;
pub fn VirtualAllocEx(
hProcess: HANDLE,
lpAddress: LPVOID,
dwSize: SIZE_T,
flAllocationType: DWORD,
flProtect: DWORD,
) -> LPVOID;
pub fn VirtualFreeEx(
hProcess: HANDLE,
lpAddress: LPVOID,
dwSize: SIZE_T,
dwFreeType: DWORD,
) -> BOOL;
pub fn VirtualProtectEx(
hProcess: HANDLE,
lpAddress: LPVOID,
dwSize: SIZE_T,
flNewProtect: DWORD,
lpflOldProtect: PDWORD,
) -> BOOL;
pub fn VirtualQueryEx(
hProcess: HANDLE,
lpAddress: LPCVOID,
lpBuffer: PMEMORY_BASIC_INFORMATION,
dwLength: SIZE_T,
) -> SIZE_T;
pub fn ReadProcessMemory(
hProcess: HANDLE,
lpBaseAddress: LPCVOID,
lpBuffer: LPVOID,
nSize: SIZE_T,
lpNumberOfBytesRead: *mut SIZE_T,
) -> BOOL;
pub fn WriteProcessMemory(
hProcess: HANDLE,
lpBaseAddress: LPVOID,
lpBuffer: LPCVOID,
nSize: SIZE_T,
lpNumberOfBytesWritten: *mut SIZE_T,
) -> BOOL;
pub fn CreateFileMappingW(
hFile: HANDLE,
lpFileMappingAttributes: LPSECURITY_ATTRIBUTES,
flProtect: DWORD,
dwMaximumSizeHigh: DWORD,
dwMaximumSizeLow: DWORD,
lpName: LPCWSTR,
) -> HANDLE;
pub fn OpenFileMappingW(
dwDesiredAccess: DWORD,
bInheritHandle: BOOL,
lpName: LPCWSTR,
) -> HANDLE;
pub fn MapViewOfFile(
hFileMappingObject: HANDLE,
dwDesiredAccess: DWORD,
dwFileOffsetHigh: DWORD,
dwFileOffsetLow: DWORD,
dwNumberOfBytesToMap: SIZE_T,
) -> LPVOID;
pub fn MapViewOfFileEx(
hFileMappingObject: HANDLE,
dwDesiredAccess: DWORD,
dwFileOffsetHigh: DWORD,
dwFileOffsetLow: DWORD,
dwNumberOfBytesToMap: SIZE_T,
lpBaseAddress: LPVOID,
) -> LPVOID;
pub fn FlushViewOfFile(
lpBaseAddress: LPCVOID,
dwNumberOfBytesToFlush: SIZE_T,
) -> BOOL;
pub fn UnmapViewOfFile(
lpBaseAddress: LPCVOID,
) -> BOOL;
pub fn GetLargePageMinimum() -> SIZE_T;
pub fn GetProcessWorkingSetSizeEx(
hProcess: HANDLE,
lpMinimumWorkingSetSize: PSIZE_T,
lpMaximumWorkingSetSize: PSIZE_T,
Flags: PDWORD,
) -> BOOL;
pub fn SetProcessWorkingSetSizeEx(
hProcess: HANDLE,
dwMinimumWorkingSetSize: SIZE_T,
dwMaximumWorkingSetSize: SIZE_T,
Flags: DWORD,
) -> BOOL;
pub fn VirtualLock(
lpAddress: LPVOID,
dwSize: SIZE_T,
) -> BOOL;
pub fn VirtualUnlock(
lpAddress: LPVOID,
dwSize: SIZE_T,
) -> BOOL;
pub fn GetWriteWatch(
dwFlags: DWORD,
lpBaseAddress: PVOID,
dwRegionSize: SIZE_T,
lpAddresses: *mut PVOID,
lpdwCount: *mut ULONG_PTR,
lpdwGranularity: LPDWORD,
) -> UINT;
pub fn ResetWriteWatch(
lpBaseAddress: LPVOID,
dwRegionSize: SIZE_T,
) -> UINT;
}
ENUM!{enum MEMORY_RESOURCE_NOTIFICATION_TYPE {
LowMemoryResourceNotification,
HighMemoryResourceNotification,
}}
extern "system" {
pub fn CreateMemoryResourceNotification(
NotificationType: MEMORY_RESOURCE_NOTIFICATION_TYPE,
) -> HANDLE;
pub fn QueryMemoryResourceNotification(
ResourceNotificationHandle: HANDLE,
ResourceState: PBOOL,
) -> BOOL;
}
pub const FILE_CACHE_MAX_HARD_ENABLE: DWORD = 0x00000001;
pub const FILE_CACHE_MAX_HARD_DISABLE: DWORD = 0x00000002;
pub const FILE_CACHE_MIN_HARD_ENABLE: DWORD = 0x00000004;
pub const FILE_CACHE_MIN_HARD_DISABLE: DWORD = 0x00000008;
extern "system" {
pub fn GetSystemFileCacheSize(
lpMinimumFileCacheSize: PSIZE_T,
lpMaximumFileCacheSize: PSIZE_T,
lpFlags: PDWORD,
) -> BOOL;
pub fn SetSystemFileCacheSize(
MinimumFileCacheSize: SIZE_T,
MaximumFileCacheSize: SIZE_T,
Flags: DWORD,
) -> BOOL;
pub fn CreateFileMappingNumaW(
hFile: HANDLE,
lpFileMappingAttributes: LPSECURITY_ATTRIBUTES,
flProtect: DWORD,
dwMaximumSizeHigh: DWORD,
dwMaximumSizeLow: DWORD,
lpName: LPCWSTR,
nndPreferred: DWORD,
) -> HANDLE;
}
STRUCT!{struct WIN32_MEMORY_RANGE_ENTRY {
VirtualAddress: PVOID,
NumberOfBytes: SIZE_T,
}}
pub type PWIN32_MEMORY_RANGE_ENTRY = *mut WIN32_MEMORY_RANGE_ENTRY;
extern "system" {
pub fn PrefetchVirtualMemory(
hProcess: HANDLE,
NumberOfEntries: ULONG_PTR,
VirtualAddresses: PWIN32_MEMORY_RANGE_ENTRY,
Flags: ULONG,
) -> BOOL;
pub fn CreateFileMappingFromApp(
hFile: HANDLE,
SecurityAttributes: PSECURITY_ATTRIBUTES,
PageProtection: ULONG,
MaximumSize: ULONG64,
Name: PCWSTR,
) -> HANDLE;
pub fn MapViewOfFileFromApp(
hFileMappingObject: HANDLE,
DesiredAccess: ULONG,
FileOffset: ULONG64,
NumberOfBytesToMap: SIZE_T,
) -> PVOID;
pub fn UnmapViewOfFileEx(
BaseAddress: PVOID,
UnmapFlags: ULONG,
) -> BOOL;
pub fn AllocateUserPhysicalPages(
hProcess: HANDLE,
NumberOfPages: PULONG_PTR,
PageArray: PULONG_PTR,
) -> BOOL;
pub fn FreeUserPhysicalPages(
hProcess: HANDLE,
NumberOfPages: PULONG_PTR,
PageArray: PULONG_PTR,
) -> BOOL;
pub fn MapUserPhysicalPages(
VirtualAddress: PVOID,
NumberOfPages: ULONG_PTR,
PageArray: PULONG_PTR,
) -> BOOL;
pub fn AllocateUserPhysicalPagesNuma(
hProcess: HANDLE,
NumberOfPages: PULONG_PTR,
PageArray: PULONG_PTR,
nndPreferred: DWORD,
) -> BOOL;
pub fn VirtualAllocExNuma(
hProcess: HANDLE,
lpAddress: LPVOID,
dwSize: SIZE_T,
flAllocationType: DWORD,
flProtect: DWORD,
nndPreferred: DWORD,
) -> LPVOID;
}
pub const MEHC_PATROL_SCRUBBER_PRESENT: ULONG = 0x1;
extern "system" {
pub fn GetMemoryErrorHandlingCapabilities(
Capabilities: PULONG,
) -> BOOL;
}
FN!{stdcall PBAD_MEMORY_CALLBACK_ROUTINE() -> ()}
extern "system" {
pub fn RegisterBadMemoryNotification(
Callback: PBAD_MEMORY_CALLBACK_ROUTINE,
) -> PVOID;
pub fn UnregisterBadMemoryNotification(
RegistrationHandle: PVOID,
) -> BOOL;
}
ENUM!{enum OFFER_PRIORITY {
VmOfferPriorityVeryLow = 1,
VmOfferPriorityLow,
VmOfferPriorityBelowNormal,
VmOfferPriorityNormal,
}}
extern "system" {
pub fn OfferVirtualMemory(
VirtualAddress: PVOID,
Size: SIZE_T,
Priority: OFFER_PRIORITY,
) -> DWORD;
pub fn ReclaimVirtualMemory(
VirtualAddress: *const c_void,
Size: SIZE_T,
) -> DWORD;
pub fn DiscardVirtualMemory(
VirtualAddress: PVOID,
Size: SIZE_T,
) -> DWORD;
// TODO: Needs winnt::PCFG_CALL_TARGET_INFO.
/* pub fn SetProcessValidCallTargets(
hProcess: HANDLE,
VirtualAddress: PVOID,
RegionSize: SIZE_T,
NumberOfOffsets: ULONG,
OffsetInformation: PCFG_CALL_TARGET_INFO,
) -> BOOL; */
pub fn VirtualAllocFromApp(
BaseAddress: PVOID,
Size: SIZE_T,
AllocationType: ULONG,
Protection: ULONG,
) -> PVOID;
pub fn VirtualProtectFromApp(
Address: PVOID,
Size: SIZE_T,
NewProtection: ULONG,
OldProtection: PULONG,
) -> BOOL;
pub fn OpenFileMappingFromApp(
DesiredAccess: ULONG,
InheritHandle: BOOL,
Name: PCWSTR,
) -> HANDLE;
}
// TODO: Under WINAPI_PARTITION_APP, define CreateFileMappingW, MapViewOfFile, VirtualAlloc,
// VirtualProtect, and OpenFileMappingW as wrappers around the *FromApp functions.
ENUM!{enum WIN32_MEMORY_INFORMATION_CLASS {
MemoryRegionInfo,
}}
STRUCT!{struct WIN32_MEMORY_REGION_INFORMATION {
AllocationBase: PVOID,
AllocationProtect: ULONG,
u: WIN32_MEMORY_REGION_INFORMATION_u,
RegionSize: SIZE_T,
CommitSize: SIZE_T,
}}
UNION!{union WIN32_MEMORY_REGION_INFORMATION_u {
[u32; 1],
Flags Flags_mut: ULONG,
s s_mut: WIN32_MEMORY_REGION_INFORMATION_u_s,
}}
STRUCT!{struct WIN32_MEMORY_REGION_INFORMATION_u_s {
Bitfield: ULONG,
}}
BITFIELD!{WIN32_MEMORY_REGION_INFORMATION_u_s Bitfield: ULONG [
Private set_Private[0..1],
MappedDataFile set_MappedDataFile[1..2],
MappedImage set_MappedImage[2..3],
MappedPageFile set_MappedPageFile[3..4],
MappedPhysical set_MappedPhysical[4..5],
DirectMapped set_DirectMapped[5..6],
Reserved set_Reserved[6..32],
]}
// TODO: Need to resolve issue #323 first.
/*extern "system" {
pub fn QueryVirtualMemoryInformation(
Process: HANDLE,
VirtualAddress: *const VOID,
MemoryInformationClass: WIN32_MEMORY_INFORMATION_CLASS,
MemoryInformation: PVOID,
MemoryInformationSize: SIZE_T,
ReturnSize: PSIZE_T,
) -> BOOL;
pub fn MapViewOfFileNuma2(
FileMappingHandle: HANDLE,
ProcessHandle: HANDLE,
Offset: ULONG64,
BaseAddress: PVOID,
ViewSize: SIZE_T,
AllocationType: ULONG,
PageProtection: ULONG,
PreferredNode: ULONG,
) -> PVOID;
}
#[inline]
pub unsafe fn MapViewOfFile2(
FileMappingHandle: HANDLE,
ProcessHandle: HANDLE,
Offset: ULONG64,
BaseAddress: PVOID,
ViewSize: SIZE_T,
AllocationType: ULONG,
PageProtection: ULONG,
) -> PVOID {
MapViewOfFileNuma2(FileMappingHandle,
ProcessHandle,
Offset,
BaseAddress,
ViewSize,
AllocationType,
PageProtection,
NUMA_NO_PREFERRED_NODE)
}*/
extern "system" {
pub fn UnmapViewOfFile2(
ProcessHandle: HANDLE,
BaseAddress: PVOID,
UnmapFlags: ULONG,
) -> BOOL;
}
|