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