1 /*++ NDK Version: 0098 2 3 Copyright (c) Alex Ionescu. All rights reserved. 4 5 Header Name: 6 7 mmfuncs.h 8 9 Abstract: 10 11 Functions definitions for the Memory Manager. 12 13 Author: 14 15 Alex Ionescu (alexi@tinykrnl.org) - Updated - 27-Feb-2006 16 17 --*/ 18 19 #ifndef _MMFUNCS_H 20 #define _MMFUNCS_H 21 22 // 23 // Dependencies 24 // 25 #include <umtypes.h> 26 #include <mmtypes.h> 27 28 #ifndef NTOS_MODE_USER 29 30 // 31 // Section Functions 32 // 33 NTSTATUS 34 NTAPI 35 MmCreateSection( 36 _Out_ PVOID *SectionObject, 37 _In_ ACCESS_MASK DesiredAccess, 38 _In_opt_ POBJECT_ATTRIBUTES ObjectAttributes, 39 _In_ PLARGE_INTEGER MaximumSize, 40 _In_ ULONG SectionPageProtection, 41 _In_ ULONG AllocationAttributes, 42 _In_opt_ HANDLE FileHandle, 43 _In_opt_ PFILE_OBJECT File 44 ); 45 46 NTSTATUS 47 NTAPI 48 MmMapViewOfSection( 49 _In_ PVOID SectionObject, 50 _In_ PEPROCESS Process, 51 _Inout_ PVOID *BaseAddress, 52 _In_ ULONG_PTR ZeroBits, 53 _In_ SIZE_T CommitSize, 54 _Inout_opt_ PLARGE_INTEGER SectionOffset, 55 _Inout_ PSIZE_T ViewSize, 56 _In_ SECTION_INHERIT InheritDisposition, 57 _In_ ULONG AllocationType, 58 _In_ ULONG Protect 59 ); 60 61 NTSTATUS 62 NTAPI 63 MmUnmapViewOfSection( 64 _In_ struct _EPROCESS* Process, 65 _In_ PVOID BaseAddress 66 ); 67 68 #endif 69 70 // 71 // Native calls 72 // 73 NTSYSCALLAPI 74 NTSTATUS 75 NTAPI 76 NtAreMappedFilesTheSame( 77 _In_ PVOID File1MappedAsAnImage, 78 _In_ PVOID File2MappedAsFile 79 ); 80 81 NTSTATUS 82 NTAPI 83 NtAllocateUserPhysicalPages( 84 _In_ HANDLE ProcessHandle, 85 _Inout_ PULONG_PTR NumberOfPages, 86 _Inout_ PULONG_PTR UserPfnArray 87 ); 88 89 _Must_inspect_result_ 90 _At_(*BaseAddress, __drv_allocatesMem(Mem)) 91 __kernel_entry 92 NTSYSCALLAPI 93 NTSTATUS 94 NTAPI 95 NtAllocateVirtualMemory( 96 _In_ HANDLE ProcessHandle, 97 _Inout_ _Outptr_result_buffer_(*RegionSize) PVOID *BaseAddress, 98 _In_ ULONG_PTR ZeroBits, 99 _Inout_ PSIZE_T RegionSize, 100 _In_ ULONG AllocationType, 101 _In_ ULONG Protect 102 ); 103 104 NTSYSCALLAPI 105 NTSTATUS 106 NTAPI 107 NtCreatePagingFile( 108 _In_ PUNICODE_STRING FileName, 109 _In_ PLARGE_INTEGER InitialSize, 110 _In_ PLARGE_INTEGER MaxiumSize, 111 _In_ ULONG Reserved 112 ); 113 114 _Must_inspect_result_ 115 __kernel_entry 116 NTSYSCALLAPI 117 NTSTATUS 118 NTAPI 119 NtCreateSection( 120 _Out_ PHANDLE SectionHandle, 121 _In_ ACCESS_MASK DesiredAccess, 122 _In_opt_ POBJECT_ATTRIBUTES ObjectAttributes, 123 _In_opt_ PLARGE_INTEGER MaximumSize, 124 _In_ ULONG SectionPageProtection, 125 _In_ ULONG AllocationAttributes, 126 _In_opt_ HANDLE FileHandle 127 ); 128 129 NTSYSCALLAPI 130 NTSTATUS 131 NTAPI 132 NtExtendSection( 133 _In_ HANDLE SectionHandle, 134 _In_ PLARGE_INTEGER NewMaximumSize 135 ); 136 137 NTSYSCALLAPI 138 NTSTATUS 139 NTAPI 140 NtFlushInstructionCache( 141 _In_ HANDLE ProcessHandle, 142 _In_ PVOID BaseAddress, 143 _In_ SIZE_T NumberOfBytesToFlush 144 ); 145 146 NTSYSCALLAPI 147 NTSTATUS 148 NTAPI 149 NtFlushVirtualMemory( 150 _In_ HANDLE ProcessHandle, 151 _Inout_ PVOID *BaseAddress, 152 _Inout_ PSIZE_T RegionSize, 153 _Out_ PIO_STATUS_BLOCK IoStatus 154 ); 155 156 NTSTATUS 157 NTAPI 158 NtFreeUserPhysicalPages( 159 _In_ HANDLE ProcessHandle, 160 _Inout_ PULONG_PTR NumberOfPages, 161 _Inout_ PULONG_PTR UserPfnArray 162 ); 163 164 __kernel_entry 165 _IRQL_requires_max_(PASSIVE_LEVEL) 166 NTSYSCALLAPI 167 NTSTATUS 168 NTAPI 169 NtFreeVirtualMemory( 170 _In_ HANDLE ProcessHandle, 171 _Inout_ __drv_freesMem(Mem) PVOID *BaseAddress, 172 _Inout_ PSIZE_T RegionSize, 173 _In_ ULONG FreeType 174 ); 175 176 NTSTATUS 177 NTAPI 178 NtGetWriteWatch( 179 _In_ HANDLE ProcessHandle, 180 _In_ ULONG Flags, 181 _In_ PVOID BaseAddress, 182 _In_ SIZE_T RegionSize, 183 _In_ PVOID *UserAddressArray, 184 _Out_ PULONG_PTR EntriesInUserAddressArray, 185 _Out_ PULONG Granularity 186 ); 187 188 NTSYSCALLAPI 189 NTSTATUS 190 NTAPI 191 NtLockVirtualMemory( 192 _In_ HANDLE ProcessHandle, 193 _Inout_ PVOID *BaseAddress, 194 _Inout_ PSIZE_T NumberOfBytesToLock, 195 _In_ ULONG MapType 196 ); 197 198 NTSTATUS 199 NTAPI 200 NtMapUserPhysicalPages( 201 _In_ PVOID VirtualAddresses, 202 _In_ ULONG_PTR NumberOfPages, 203 _Inout_ PULONG_PTR UserPfnArray 204 ); 205 206 NTSTATUS 207 NTAPI 208 NtMapUserPhysicalPagesScatter( 209 _In_ PVOID *VirtualAddresses, 210 _In_ ULONG_PTR NumberOfPages, 211 _Inout_ PULONG_PTR UserPfnArray 212 ); 213 214 NTSYSCALLAPI 215 NTSTATUS 216 NTAPI 217 NtMapViewOfSection( 218 _In_ HANDLE SectionHandle, 219 _In_ HANDLE ProcessHandle, 220 _Inout_ PVOID *BaseAddress, 221 _In_ ULONG_PTR ZeroBits, 222 _In_ SIZE_T CommitSize, 223 _Inout_opt_ PLARGE_INTEGER SectionOffset, 224 _Inout_ PSIZE_T ViewSize, 225 _In_ SECTION_INHERIT InheritDisposition, 226 _In_ ULONG AllocationType, 227 _In_ ULONG AccessProtection 228 ); 229 230 NTSYSCALLAPI 231 NTSTATUS 232 NTAPI 233 NtOpenSection( 234 _Out_ PHANDLE SectionHandle, 235 _In_ ACCESS_MASK DesiredAccess, 236 _In_ POBJECT_ATTRIBUTES ObjectAttributes 237 ); 238 239 NTSYSCALLAPI 240 NTSTATUS 241 NTAPI 242 NtProtectVirtualMemory( 243 _In_ HANDLE ProcessHandle, 244 _In_ PVOID *BaseAddress, 245 _In_ SIZE_T *NumberOfBytesToProtect, 246 _In_ ULONG NewAccessProtection, 247 _Out_ PULONG OldAccessProtection 248 ); 249 250 NTSYSCALLAPI 251 NTSTATUS 252 NTAPI 253 NtQuerySection( 254 _In_ HANDLE SectionHandle, 255 _In_ SECTION_INFORMATION_CLASS SectionInformationClass, 256 _Out_ PVOID SectionInformation, 257 _In_ SIZE_T Length, 258 _Out_ PSIZE_T ResultLength 259 ); 260 261 NTSYSCALLAPI 262 NTSTATUS 263 NTAPI 264 NtQueryVirtualMemory( 265 _In_ HANDLE ProcessHandle, 266 _In_ PVOID Address, 267 _In_ MEMORY_INFORMATION_CLASS VirtualMemoryInformationClass, 268 _Out_ PVOID VirtualMemoryInformation, 269 _In_ SIZE_T Length, 270 _Out_opt_ PSIZE_T ResultLength 271 ); 272 273 NTSYSCALLAPI 274 NTSTATUS 275 NTAPI 276 NtReadVirtualMemory( 277 _In_ HANDLE ProcessHandle, 278 _In_ PVOID BaseAddress, 279 _Out_ PVOID Buffer, 280 _In_ SIZE_T NumberOfBytesToRead, 281 _Out_opt_ PSIZE_T NumberOfBytesRead 282 ); 283 284 NTSTATUS 285 NTAPI 286 NtResetWriteWatch( 287 _In_ HANDLE ProcessHandle, 288 _In_ PVOID BaseAddress, 289 _In_ SIZE_T RegionSize 290 ); 291 292 NTSYSCALLAPI 293 NTSTATUS 294 NTAPI 295 NtUnlockVirtualMemory( 296 _In_ HANDLE ProcessHandle, 297 _Inout_ PVOID *BaseAddress, 298 _Inout_ PSIZE_T NumberOfBytesToUnlock, 299 _In_ ULONG MapType 300 ); 301 302 NTSYSCALLAPI 303 NTSTATUS 304 NTAPI 305 NtUnmapViewOfSection( 306 _In_ HANDLE ProcessHandle, 307 _In_ PVOID BaseAddress 308 ); 309 310 NTSYSCALLAPI 311 NTSTATUS 312 NTAPI 313 NtWriteVirtualMemory( 314 _In_ HANDLE ProcessHandle, 315 _In_ PVOID BaseAddress, 316 _In_ PVOID Buffer, 317 _In_ SIZE_T NumberOfBytesToWrite, 318 _Out_opt_ PSIZE_T NumberOfBytesWritten 319 ); 320 321 NTSYSAPI 322 NTSTATUS 323 NTAPI 324 ZwAreMappedFilesTheSame( 325 _In_ PVOID File1MappedAsAnImage, 326 _In_ PVOID File2MappedAsFile 327 ); 328 329 _Must_inspect_result_ 330 _At_(*BaseAddress, __drv_allocatesMem(Mem)) 331 __kernel_entry 332 NTSYSAPI 333 NTSTATUS 334 NTAPI 335 ZwAllocateVirtualMemory( 336 _In_ HANDLE ProcessHandle, 337 _Inout_ _Outptr_result_buffer_(*RegionSize) PVOID *BaseAddress, 338 _In_ ULONG_PTR ZeroBits, 339 _Inout_ PSIZE_T RegionSize, 340 _In_ ULONG AllocationType, 341 _In_ ULONG Protect 342 ); 343 344 NTSYSAPI 345 NTSTATUS 346 NTAPI 347 ZwCreatePagingFile( 348 _In_ PUNICODE_STRING FileName, 349 _In_ PLARGE_INTEGER InitialSize, 350 _In_ PLARGE_INTEGER MaxiumSize, 351 _In_ ULONG Reserved 352 ); 353 354 _IRQL_requires_max_(APC_LEVEL) 355 NTSYSAPI 356 NTSTATUS 357 NTAPI 358 ZwCreateSection( 359 _Out_ PHANDLE SectionHandle, 360 _In_ ACCESS_MASK DesiredAccess, 361 _In_opt_ POBJECT_ATTRIBUTES ObjectAttributes, 362 _In_opt_ PLARGE_INTEGER MaximumSize, 363 _In_ ULONG SectionPageProtection, 364 _In_ ULONG AllocationAttributes, 365 _In_opt_ HANDLE FileHandle 366 ); 367 368 NTSYSAPI 369 NTSTATUS 370 NTAPI 371 ZwExtendSection( 372 _In_ HANDLE SectionHandle, 373 _In_ PLARGE_INTEGER NewMaximumSize 374 ); 375 376 _IRQL_requires_max_(PASSIVE_LEVEL) 377 NTSYSAPI 378 NTSTATUS 379 NTAPI 380 ZwFreeVirtualMemory( 381 _In_ HANDLE ProcessHandle, 382 _Inout_ __drv_freesMem(Mem) PVOID *BaseAddress, 383 _Inout_ PSIZE_T RegionSize, 384 _In_ ULONG FreeType 385 ); 386 387 NTSYSAPI 388 NTSTATUS 389 NTAPI 390 ZwLockVirtualMemory( 391 _In_ HANDLE ProcessHandle, 392 _Inout_ PVOID *BaseAddress, 393 _Inout_ PSIZE_T NumberOfBytesToLock, 394 _In_ ULONG MapType 395 ); 396 397 _IRQL_requires_max_(PASSIVE_LEVEL) 398 NTSYSAPI 399 NTSTATUS 400 NTAPI 401 ZwMapViewOfSection( 402 _In_ HANDLE SectionHandle, 403 _In_ HANDLE ProcessHandle, 404 _Outptr_result_bytebuffer_(*ViewSize) PVOID *BaseAddress, 405 _In_ ULONG_PTR ZeroBits, 406 _In_ SIZE_T CommitSize, 407 _Inout_opt_ PLARGE_INTEGER SectionOffset, 408 _Inout_ PSIZE_T ViewSize, 409 _In_ SECTION_INHERIT InheritDisposition, 410 _In_ ULONG AllocationType, 411 _In_ ULONG Win32Protect 412 ); 413 414 NTSYSAPI 415 NTSTATUS 416 NTAPI 417 ZwOpenSection( 418 _Out_ PHANDLE SectionHandle, 419 _In_ ACCESS_MASK DesiredAccess, 420 _In_ POBJECT_ATTRIBUTES ObjectAttributes 421 ); 422 423 NTSYSAPI 424 NTSTATUS 425 NTAPI 426 ZwProtectVirtualMemory( 427 _In_ HANDLE ProcessHandle, 428 _In_ PVOID *BaseAddress, 429 _In_ SIZE_T *NumberOfBytesToProtect, 430 _In_ ULONG NewAccessProtection, 431 _Out_ PULONG OldAccessProtection 432 ); 433 434 NTSYSAPI 435 NTSTATUS 436 NTAPI 437 ZwQuerySection( 438 _In_ HANDLE SectionHandle, 439 _In_ SECTION_INFORMATION_CLASS SectionInformationClass, 440 _Out_ PVOID SectionInformation, 441 _In_ SIZE_T Length, 442 _Out_opt_ PSIZE_T ResultLength 443 ); 444 445 NTSYSAPI 446 NTSTATUS 447 NTAPI 448 ZwQueryVirtualMemory( 449 _In_ HANDLE ProcessHandle, 450 _In_ PVOID Address, 451 _In_ MEMORY_INFORMATION_CLASS VirtualMemoryInformationClass, 452 _Out_ PVOID VirtualMemoryInformation, 453 _In_ SIZE_T Length, 454 _Out_opt_ PSIZE_T ResultLength 455 ); 456 457 NTSYSAPI 458 NTSTATUS 459 NTAPI 460 ZwReadVirtualMemory( 461 _In_ HANDLE ProcessHandle, 462 _In_ PVOID BaseAddress, 463 _Out_ PVOID Buffer, 464 _In_ SIZE_T NumberOfBytesToRead, 465 _Out_opt_ PSIZE_T NumberOfBytesRead 466 ); 467 468 NTSYSAPI 469 NTSTATUS 470 NTAPI 471 ZwUnlockVirtualMemory( 472 _In_ HANDLE ProcessHandle, 473 _Inout_ PVOID *BaseAddress, 474 _Inout_ PSIZE_T NumberOfBytesToUnlock, 475 _In_ ULONG MapType 476 ); 477 478 _IRQL_requires_max_(PASSIVE_LEVEL) 479 NTSYSAPI 480 NTSTATUS 481 NTAPI 482 ZwUnmapViewOfSection( 483 _In_ HANDLE ProcessHandle, 484 _In_opt_ PVOID BaseAddress 485 ); 486 487 NTSYSAPI 488 NTSTATUS 489 NTAPI 490 ZwWriteVirtualMemory( 491 _In_ HANDLE ProcessHandle, 492 _In_ PVOID BaseAddress, 493 _In_ PVOID Buffer, 494 _In_ SIZE_T NumberOfBytesToWrite, 495 _Out_opt_ PSIZE_T NumberOfBytesWritten 496 ); 497 498 #endif 499