c405bb29844232f815a487d8e6afd8aae994eba9
[reactos.git] / include / xdk / mmfuncs.h
1 /******************************************************************************
2 * Memory manager Functions *
3 ******************************************************************************/
4 $if (_WDMDDK_)
5 /* Alignment Macros */
6 #define ALIGN_DOWN_BY(size, align) \
7 ((ULONG_PTR)(size) & ~((ULONG_PTR)(align) - 1))
8
9 #define ALIGN_UP_BY(size, align) \
10 (ALIGN_DOWN_BY(((ULONG_PTR)(size) + align - 1), align))
11
12 #define ALIGN_DOWN_POINTER_BY(ptr, align) \
13 ((PVOID)ALIGN_DOWN_BY(ptr, align))
14
15 #define ALIGN_UP_POINTER_BY(ptr, align) \
16 ((PVOID)ALIGN_UP_BY(ptr, align))
17
18 #define ALIGN_DOWN(size, type) \
19 ALIGN_DOWN_BY(size, sizeof(type))
20
21 #define ALIGN_UP(size, type) \
22 ALIGN_UP_BY(size, sizeof(type))
23
24 #define ALIGN_DOWN_POINTER(ptr, type) \
25 ALIGN_DOWN_POINTER_BY(ptr, sizeof(type))
26
27 #define ALIGN_UP_POINTER(ptr, type) \
28 ALIGN_UP_POINTER_BY(ptr, sizeof(type))
29
30 #ifndef FIELD_OFFSET
31 #define FIELD_OFFSET(type, field) ((ULONG)&(((type *)0)->field))
32 #endif
33
34 #ifndef FIELD_SIZE
35 #define FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
36 #endif
37
38 #define POOL_TAGGING 1
39
40 #if DBG
41 #define IF_DEBUG if (TRUE)
42 #else
43 #define IF_DEBUG if (FALSE)
44 #endif /* DBG */
45
46 /* ULONG
47 * BYTE_OFFSET(
48 * IN PVOID Va)
49 */
50 #define BYTE_OFFSET(Va) \
51 ((ULONG) ((ULONG_PTR) (Va) & (PAGE_SIZE - 1)))
52
53 /* ULONG
54 * BYTES_TO_PAGES(
55 * IN ULONG Size)
56 */
57 #define BYTES_TO_PAGES(Size) \
58 (((Size) >> PAGE_SHIFT) + (((Size) & (PAGE_SIZE - 1)) != 0))
59
60 /* PVOID
61 * PAGE_ALIGN(
62 * IN PVOID Va)
63 */
64 #define PAGE_ALIGN(Va) \
65 ((PVOID) ((ULONG_PTR)(Va) & ~(PAGE_SIZE - 1)))
66
67 /* ULONG_PTR
68 * ROUND_TO_PAGES(
69 * IN ULONG_PTR Size)
70 */
71 #define ROUND_TO_PAGES(Size) \
72 (((ULONG_PTR) (Size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
73
74 /* ULONG
75 * ADDRESS_AND_SIZE_TO_SPAN_PAGES(
76 * IN PVOID Va,
77 * IN ULONG Size)
78 */
79 #define ADDRESS_AND_SIZE_TO_SPAN_PAGES(_Va, _Size) \
80 ((ULONG) ((((ULONG_PTR) (_Va) & (PAGE_SIZE - 1)) \
81 + (_Size) + (PAGE_SIZE - 1)) >> PAGE_SHIFT))
82
83 #define COMPUTE_PAGES_SPANNED(Va, Size) \
84 ADDRESS_AND_SIZE_TO_SPAN_PAGES(Va,Size)
85
86 /*
87 * ULONG
88 * MmGetMdlByteCount(
89 * IN PMDL Mdl)
90 */
91 #define MmGetMdlByteCount(_Mdl) \
92 ((_Mdl)->ByteCount)
93
94 /*
95 * ULONG
96 * MmGetMdlByteOffset(
97 * IN PMDL Mdl)
98 */
99 #define MmGetMdlByteOffset(_Mdl) \
100 ((_Mdl)->ByteOffset)
101
102 #define MmGetMdlBaseVa(Mdl) ((Mdl)->StartVa)
103
104 /*
105 * PPFN_NUMBER
106 * MmGetMdlPfnArray(
107 * IN PMDL Mdl)
108 */
109 #define MmGetMdlPfnArray(_Mdl) \
110 ((PPFN_NUMBER) ((_Mdl) + 1))
111
112 /*
113 * PVOID
114 * MmGetMdlVirtualAddress(
115 * IN PMDL Mdl)
116 */
117 #define MmGetMdlVirtualAddress(_Mdl) \
118 ((PVOID) ((PCHAR) ((_Mdl)->StartVa) + (_Mdl)->ByteOffset))
119
120 #define MmGetProcedureAddress(Address) (Address)
121
122 /* PVOID MmGetSystemAddressForMdl(
123 * IN PMDL Mdl);
124 */
125 #define MmGetSystemAddressForMdl(Mdl) \
126 (((Mdl)->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA | \
127 MDL_SOURCE_IS_NONPAGED_POOL)) ? \
128 ((Mdl)->MappedSystemVa) : \
129 (MmMapLockedPages((Mdl), KernelMode)))
130
131 /* PVOID
132 * MmGetSystemAddressForMdlSafe(
133 * IN PMDL Mdl,
134 * IN MM_PAGE_PRIORITY Priority)
135 */
136 #define MmGetSystemAddressForMdlSafe(_Mdl, _Priority) \
137 (((_Mdl)->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA \
138 | MDL_SOURCE_IS_NONPAGED_POOL)) ? \
139 (_Mdl)->MappedSystemVa : \
140 (PVOID) MmMapLockedPagesSpecifyCache((_Mdl), \
141 KernelMode, MmCached, NULL, FALSE, (_Priority)))
142
143 /*
144 * VOID
145 * MmInitializeMdl(
146 * IN PMDL MemoryDescriptorList,
147 * IN PVOID BaseVa,
148 * IN SIZE_T Length)
149 */
150 #define MmInitializeMdl(_MemoryDescriptorList, \
151 _BaseVa, \
152 _Length) \
153 { \
154 (_MemoryDescriptorList)->Next = (PMDL) NULL; \
155 (_MemoryDescriptorList)->Size = (CSHORT) (sizeof(MDL) + \
156 (sizeof(PFN_NUMBER) * ADDRESS_AND_SIZE_TO_SPAN_PAGES(_BaseVa, _Length))); \
157 (_MemoryDescriptorList)->MdlFlags = 0; \
158 (_MemoryDescriptorList)->StartVa = (PVOID) PAGE_ALIGN(_BaseVa); \
159 (_MemoryDescriptorList)->ByteOffset = BYTE_OFFSET(_BaseVa); \
160 (_MemoryDescriptorList)->ByteCount = (ULONG) _Length; \
161 }
162
163 /*
164 * VOID
165 * MmPrepareMdlForReuse(
166 * IN PMDL Mdl)
167 */
168 #define MmPrepareMdlForReuse(_Mdl) \
169 { \
170 if (((_Mdl)->MdlFlags & MDL_PARTIAL_HAS_BEEN_MAPPED) != 0) { \
171 ASSERT(((_Mdl)->MdlFlags & MDL_PARTIAL) != 0); \
172 MmUnmapLockedPages((_Mdl)->MappedSystemVa, (_Mdl)); \
173 } else if (((_Mdl)->MdlFlags & MDL_PARTIAL) == 0) { \
174 ASSERT(((_Mdl)->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA) == 0); \
175 } \
176 }
177 $endif
178
179 #if (NTDDI_VERSION >= NTDDI_WIN2K)
180 $if (_NTDDK_)
181 NTKERNELAPI
182 PPHYSICAL_MEMORY_RANGE
183 NTAPI
184 MmGetPhysicalMemoryRanges(VOID);
185
186 NTKERNELAPI
187 PHYSICAL_ADDRESS
188 NTAPI
189 MmGetPhysicalAddress(
190 IN PVOID BaseAddress);
191
192 NTKERNELAPI
193 BOOLEAN
194 NTAPI
195 MmIsNonPagedSystemAddressValid(
196 IN PVOID VirtualAddress);
197
198 NTKERNELAPI
199 PVOID
200 NTAPI
201 MmAllocateNonCachedMemory(
202 IN SIZE_T NumberOfBytes);
203
204 NTKERNELAPI
205 VOID
206 NTAPI
207 MmFreeNonCachedMemory(
208 IN PVOID BaseAddress,
209 IN SIZE_T NumberOfBytes);
210
211 NTKERNELAPI
212 PVOID
213 NTAPI
214 MmGetVirtualForPhysical(
215 IN PHYSICAL_ADDRESS PhysicalAddress);
216
217 NTKERNELAPI
218 NTSTATUS
219 NTAPI
220 MmMapUserAddressesToPage(
221 IN PVOID BaseAddress,
222 IN SIZE_T NumberOfBytes,
223 IN PVOID PageAddress);
224
225 NTKERNELAPI
226 PVOID
227 NTAPI
228 MmMapVideoDisplay(
229 IN PHYSICAL_ADDRESS PhysicalAddress,
230 IN SIZE_T NumberOfBytes,
231 IN MEMORY_CACHING_TYPE CacheType);
232
233 NTKERNELAPI
234 NTSTATUS
235 NTAPI
236 MmMapViewInSessionSpace(
237 IN PVOID Section,
238 OUT PVOID *MappedBase,
239 IN OUT PSIZE_T ViewSize);
240
241 NTKERNELAPI
242 NTSTATUS
243 NTAPI
244 MmMapViewInSystemSpace(
245 IN PVOID Section,
246 OUT PVOID *MappedBase,
247 IN OUT PSIZE_T ViewSize);
248
249 NTKERNELAPI
250 BOOLEAN
251 NTAPI
252 MmIsAddressValid(
253 IN PVOID VirtualAddress);
254
255 NTKERNELAPI
256 BOOLEAN
257 NTAPI
258 MmIsThisAnNtAsSystem(VOID);
259
260 NTKERNELAPI
261 VOID
262 NTAPI
263 MmLockPagableSectionByHandle(
264 IN PVOID ImageSectionHandle);
265
266 NTKERNELAPI
267 NTSTATUS
268 NTAPI
269 MmUnmapViewInSessionSpace(
270 IN PVOID MappedBase);
271
272 NTKERNELAPI
273 NTSTATUS
274 NTAPI
275 MmUnmapViewInSystemSpace(
276 IN PVOID MappedBase);
277
278 NTKERNELAPI
279 VOID
280 NTAPI
281 MmUnsecureVirtualMemory(
282 IN HANDLE SecureHandle);
283
284 NTKERNELAPI
285 NTSTATUS
286 NTAPI
287 MmRemovePhysicalMemory(
288 IN PPHYSICAL_ADDRESS StartAddress,
289 IN OUT PLARGE_INTEGER NumberOfBytes);
290
291 NTKERNELAPI
292 HANDLE
293 NTAPI
294 MmSecureVirtualMemory(
295 IN PVOID Address,
296 IN SIZE_T Size,
297 IN ULONG ProbeMode);
298
299 NTKERNELAPI
300 VOID
301 NTAPI
302 MmUnmapVideoDisplay(
303 IN PVOID BaseAddress,
304 IN SIZE_T NumberOfBytes);
305
306 NTKERNELAPI
307 NTSTATUS
308 NTAPI
309 MmAddPhysicalMemory(
310 IN PPHYSICAL_ADDRESS StartAddress,
311 IN OUT PLARGE_INTEGER NumberOfBytes);
312
313 NTKERNELAPI
314 PVOID
315 NTAPI
316 MmAllocateContiguousMemory(
317 IN SIZE_T NumberOfBytes,
318 IN PHYSICAL_ADDRESS HighestAcceptableAddress);
319
320 NTKERNELAPI
321 PVOID
322 NTAPI
323 MmAllocateContiguousMemorySpecifyCache(
324 IN SIZE_T NumberOfBytes,
325 IN PHYSICAL_ADDRESS LowestAcceptableAddress,
326 IN PHYSICAL_ADDRESS HighestAcceptableAddress,
327 IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
328 IN MEMORY_CACHING_TYPE CacheType);
329
330 NTKERNELAPI
331 PVOID
332 NTAPI
333 MmAllocateContiguousMemorySpecifyCacheNode(
334 IN SIZE_T NumberOfBytes,
335 IN PHYSICAL_ADDRESS LowestAcceptableAddress,
336 IN PHYSICAL_ADDRESS HighestAcceptableAddress,
337 IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
338 IN MEMORY_CACHING_TYPE CacheType,
339 IN NODE_REQUIREMENT PreferredNode);
340
341 NTKERNELAPI
342 VOID
343 NTAPI
344 MmFreeContiguousMemory(
345 IN PVOID BaseAddress);
346
347 NTKERNELAPI
348 VOID
349 NTAPI
350 MmFreeContiguousMemorySpecifyCache(
351 IN PVOID BaseAddress,
352 IN SIZE_T NumberOfBytes,
353 IN MEMORY_CACHING_TYPE CacheType);
354 $endif
355
356 $if (_WDMDDK_)
357 NTKERNELAPI
358 PVOID
359 NTAPI
360 MmAllocateContiguousMemory(
361 IN SIZE_T NumberOfBytes,
362 IN PHYSICAL_ADDRESS HighestAcceptableAddress);
363
364 NTKERNELAPI
365 PVOID
366 NTAPI
367 MmAllocateContiguousMemorySpecifyCache(
368 IN SIZE_T NumberOfBytes,
369 IN PHYSICAL_ADDRESS LowestAcceptableAddress,
370 IN PHYSICAL_ADDRESS HighestAcceptableAddress,
371 IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
372 IN MEMORY_CACHING_TYPE CacheType);
373
374 NTKERNELAPI
375 PMDL
376 NTAPI
377 MmAllocatePagesForMdl(
378 IN PHYSICAL_ADDRESS LowAddress,
379 IN PHYSICAL_ADDRESS HighAddress,
380 IN PHYSICAL_ADDRESS SkipBytes,
381 IN SIZE_T TotalBytes);
382
383 NTKERNELAPI
384 VOID
385 NTAPI
386 MmBuildMdlForNonPagedPool(
387 IN OUT PMDLX MemoryDescriptorList);
388
389 //DECLSPEC_DEPRECATED_DDK
390 NTKERNELAPI
391 PMDL
392 NTAPI
393 MmCreateMdl(
394 IN PMDL MemoryDescriptorList OPTIONAL,
395 IN PVOID Base,
396 IN SIZE_T Length);
397
398 NTKERNELAPI
399 VOID
400 NTAPI
401 MmFreeContiguousMemory(
402 IN PVOID BaseAddress);
403
404 NTKERNELAPI
405 VOID
406 NTAPI
407 MmFreeContiguousMemorySpecifyCache(
408 IN PVOID BaseAddress,
409 IN SIZE_T NumberOfBytes,
410 IN MEMORY_CACHING_TYPE CacheType);
411
412 NTKERNELAPI
413 VOID
414 NTAPI
415 MmFreePagesFromMdl(
416 IN PMDLX MemoryDescriptorList);
417
418 NTKERNELAPI
419 PVOID
420 NTAPI
421 MmGetSystemRoutineAddress(
422 IN PUNICODE_STRING SystemRoutineName);
423
424 NTKERNELAPI
425 LOGICAL
426 NTAPI
427 MmIsDriverVerifying(
428 IN struct _DRIVER_OBJECT *DriverObject);
429
430 NTKERNELAPI
431 PVOID
432 NTAPI
433 MmLockPagableDataSection(
434 IN PVOID AddressWithinSection);
435
436 NTKERNELAPI
437 PVOID
438 NTAPI
439 MmMapIoSpace(
440 IN PHYSICAL_ADDRESS PhysicalAddress,
441 IN SIZE_T NumberOfBytes,
442 IN MEMORY_CACHING_TYPE CacheEnable);
443
444 NTKERNELAPI
445 PVOID
446 NTAPI
447 MmMapLockedPages(
448 IN PMDL MemoryDescriptorList,
449 IN KPROCESSOR_MODE AccessMode);
450
451 NTKERNELAPI
452 PVOID
453 NTAPI
454 MmMapLockedPagesSpecifyCache(
455 IN PMDLX MemoryDescriptorList,
456 IN KPROCESSOR_MODE AccessMode,
457 IN MEMORY_CACHING_TYPE CacheType,
458 IN PVOID BaseAddress OPTIONAL,
459 IN ULONG BugCheckOnFailure,
460 IN MM_PAGE_PRIORITY Priority);
461
462 NTKERNELAPI
463 PVOID
464 NTAPI
465 MmPageEntireDriver(
466 IN PVOID AddressWithinSection);
467
468 NTKERNELAPI
469 VOID
470 NTAPI
471 MmProbeAndLockPages(
472 IN OUT PMDL MemoryDescriptorList,
473 IN KPROCESSOR_MODE AccessMode,
474 IN LOCK_OPERATION Operation);
475
476 NTKERNELAPI
477 MM_SYSTEMSIZE
478 NTAPI
479 MmQuerySystemSize(VOID);
480
481 NTKERNELAPI
482 VOID
483 NTAPI
484 MmResetDriverPaging(
485 IN PVOID AddressWithinSection);
486
487 NTKERNELAPI
488 SIZE_T
489 NTAPI
490 MmSizeOfMdl(
491 IN PVOID Base,
492 IN SIZE_T Length);
493
494 NTKERNELAPI
495 VOID
496 NTAPI
497 MmUnlockPagableImageSection(
498 IN PVOID ImageSectionHandle);
499
500 NTKERNELAPI
501 VOID
502 NTAPI
503 MmUnlockPages(
504 IN OUT PMDL MemoryDescriptorList);
505
506 NTKERNELAPI
507 VOID
508 NTAPI
509 MmUnmapIoSpace(
510 IN PVOID BaseAddress,
511 IN SIZE_T NumberOfBytes);
512
513 NTKERNELAPI
514 VOID
515 NTAPI
516 MmProbeAndLockProcessPages(
517 IN OUT PMDL MemoryDescriptorList,
518 IN PEPROCESS Process,
519 IN KPROCESSOR_MODE AccessMode,
520 IN LOCK_OPERATION Operation);
521
522 NTKERNELAPI
523 VOID
524 NTAPI
525 MmUnmapLockedPages(
526 IN PVOID BaseAddress,
527 IN PMDL MemoryDescriptorList);
528
529 NTKERNELAPI
530 PVOID
531 NTAPI
532 MmAllocateContiguousMemorySpecifyCacheNode(
533 IN SIZE_T NumberOfBytes,
534 IN PHYSICAL_ADDRESS LowestAcceptableAddress,
535 IN PHYSICAL_ADDRESS HighestAcceptableAddress,
536 IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
537 IN MEMORY_CACHING_TYPE CacheType,
538 IN NODE_REQUIREMENT PreferredNode);
539 $endif
540
541 #endif /* (NTDDI_VERSION >= NTDDI_WIN2K) */
542
543 #if (NTDDI_VERSION >= NTDDI_WINXP)
544
545 NTKERNELAPI
546 NTSTATUS
547 NTAPI
548 MmAdvanceMdl(
549 IN OUT PMDL Mdl,
550 IN ULONG NumberOfBytes);
551
552 NTKERNELAPI
553 PVOID
554 NTAPI
555 MmAllocateMappingAddress(
556 IN SIZE_T NumberOfBytes,
557 IN ULONG PoolTag);
558
559 NTKERNELAPI
560 VOID
561 NTAPI
562 MmFreeMappingAddress(
563 IN PVOID BaseAddress,
564 IN ULONG PoolTag);
565
566 NTKERNELAPI
567 NTSTATUS
568 NTAPI
569 MmIsVerifierEnabled(
570 OUT PULONG VerifierFlags);
571
572 NTKERNELAPI
573 PVOID
574 NTAPI
575 MmMapLockedPagesWithReservedMapping(
576 IN PVOID MappingAddress,
577 IN ULONG PoolTag,
578 IN PMDL MemoryDescriptorList,
579 IN MEMORY_CACHING_TYPE CacheType);
580
581 NTKERNELAPI
582 NTSTATUS
583 NTAPI
584 MmProtectMdlSystemAddress(
585 IN PMDL MemoryDescriptorList,
586 IN ULONG NewProtect);
587
588 NTKERNELAPI
589 VOID
590 NTAPI
591 MmUnmapReservedMapping(
592 IN PVOID BaseAddress,
593 IN ULONG PoolTag,
594 IN PMDL MemoryDescriptorList);
595
596 NTKERNELAPI
597 NTSTATUS
598 NTAPI
599 MmAddVerifierThunks(
600 IN PVOID ThunkBuffer,
601 IN ULONG ThunkBufferSize);
602
603 #endif /* (NTDDI_VERSION >= NTDDI_WINXP) */
604
605 #if (NTDDI_VERSION >= NTDDI_WS03)
606 $if (_NTDDK_)
607 NTKERNELAPI
608 NTSTATUS
609 NTAPI
610 MmCreateMirror(VOID);
611 $endif
612 $if (_WDMDDK_)
613 NTKERNELAPI
614 LOGICAL
615 NTAPI
616 MmIsIoSpaceActive(
617 IN PHYSICAL_ADDRESS StartAddress,
618 IN SIZE_T NumberOfBytes);
619 $endif
620 #endif
621
622 $if (_WDMDDK_)
623 #if (NTDDI_VERSION >= NTDDI_WS03SP1)
624 NTKERNELAPI
625 PMDL
626 NTAPI
627 MmAllocatePagesForMdlEx(
628 IN PHYSICAL_ADDRESS LowAddress,
629 IN PHYSICAL_ADDRESS HighAddress,
630 IN PHYSICAL_ADDRESS SkipBytes,
631 IN SIZE_T TotalBytes,
632 IN MEMORY_CACHING_TYPE CacheType,
633 IN ULONG Flags);
634 #endif
635 $endif
636
637 #if (NTDDI_VERSION >= NTDDI_VISTA)
638 $if (_NTDDK_)
639 NTSTATUS
640 NTAPI
641 MmRotatePhysicalView(
642 IN PVOID VirtualAddress,
643 IN OUT PSIZE_T NumberOfBytes,
644 IN PMDLX NewMdl OPTIONAL,
645 IN MM_ROTATE_DIRECTION Direction,
646 IN PMM_ROTATE_COPY_CALLBACK_FUNCTION CopyFunction,
647 IN PVOID Context OPTIONAL);
648 $endif
649
650 $if (_WDMDDK_)
651 NTKERNELAPI
652 LOGICAL
653 NTAPI
654 MmIsDriverVerifyingByAddress(
655 IN PVOID AddressWithinSection);
656 $endif
657 #endif
658