1 #ifndef _INCLUDE_DDK_MMFUNCS_H
2 #define _INCLUDE_DDK_MMFUNCS_H
3 /* $Id: mmfuncs.h,v 1.11 2001/11/13 22:46:24 ekohl Exp $ */
4 /* MEMORY MANAGMENT ******************************************************/
8 extern PVOID EXPORTED MmUserProbeAddress
;
9 extern PVOID EXPORTED MmHighestUserAddress
;
11 extern PVOID IMPORTED MmUserProbeAddress
;
12 extern PVOID IMPORTED MmHighestUserAddress
;
16 extern POBJECT_TYPE EXPORTED MmSectionObjectType
;
18 extern POBJECT_TYPE IMPORTED MmSectionObjectType
;
23 * FUNCTION: Returns the number of pages spanned by an address range
26 * Size = Size of range
27 * RETURNS: The number of pages
30 extern inline unsigned int ADDRESS_AND_SIZE_TO_SPAN_PAGES(PVOID Va
,
36 HighestAddr
= PAGE_ROUND_UP(Size
+ ((ULONG
)Va
));
37 LowestAddr
= PAGE_ROUND_DOWN((ULONG
)Va
);
38 return((HighestAddr
- LowestAddr
) / PAGESIZE
);
41 #define ADDRESS_AND_SIZE_TO_SPAN_PAGES(Va, Size) \
42 (ULONG)((PAGE_ROUND_UP((Size) + ((ULONG)(Va))) - \
43 PAGE_ROUND_DOWN((ULONG)(Va))) / PAGESIZE)
46 * FUNCTION: Returns FALSE is the pointer is NULL, TRUE otherwise
48 #define ARGUMENT_PRESENT(arg) ((arg)!=NULL)
51 * FUNCTION: Returns the byte offset of the address within its page
53 #define BYTE_OFFSET(va) (((ULONG)va)%PAGESIZE)
56 * FUNCTION: Takes a count in bytes and returns the number of pages
59 #define BYTES_TO_PAGES(size) (?)
63 MmAdjustWorkingSetSize (
70 MmAllocateContiguousMemory (
71 IN ULONG NumberOfBytes
,
72 IN PHYSICAL_ADDRESS HighestAcceptableAddress
76 MmAllocateContiguousAlignedMemory(IN ULONG NumberOfBytes
,
77 IN PHYSICAL_ADDRESS HighestAcceptableAddress
,
82 MmAllocateNonCachedMemory (
83 IN ULONG NumberOfBytes
86 * FUNCTION: Fills in the corresponding physical page array for a given MDL
87 * for a buffer in nonpaged system space
89 * MemoryDescriptorList = MDL to fill
93 MmBuildMdlForNonPagedPool (
94 PMDL MemoryDescriptorList
98 MmCanFileBeTruncated (
99 IN PSECTION_OBJECT_POINTERS SectionObjectPointer
,
100 IN PLARGE_INTEGER NewFileSize
103 * FUNCTION: Allocates and initializes an MDL
105 * MemoryDescriptorList = Optional caller allocated MDL to initalize
106 * Base = Base virtual address for buffer
107 * Length = Length in bytes of the buffer
108 * RETURNS: A pointer to the initalized MDL
113 PMDL MemoryDescriptorList
,
121 OUT PSECTION_OBJECT
* SectionObject
,
122 IN ACCESS_MASK DesiredAccess
,
123 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
,
124 IN PLARGE_INTEGER MaximumSize
,
125 IN ULONG SectionPageProtection
,
126 IN ULONG AllocationAttributes
,
127 IN HANDLE FileHandle OPTIONAL
,
128 IN PFILE_OBJECT File OPTIONAL
133 MmDbgTranslatePhysicalAddress (
139 MmDisableModifiedWriteOfSection (
144 MmFlushImageSection (
145 IN PSECTION_OBJECT_POINTERS SectionObjectPointer
,
146 IN MMFLUSH_TYPE FlushType
150 MmForceSectionClosed (
156 MmFreeContiguousMemory (
157 IN OUT PVOID BaseAddress
161 MmFreeNonCachedMemory (
162 IN PVOID BaseAddress
,
163 IN ULONG NumberOfBytes
166 * FUNCTION: Returns the length in bytes of a buffer described by an MDL
169 * RETURNS: Size of the buffer
171 #define MmGetMdlByteCount(Mdl) ((Mdl)->ByteCount)
174 * FUNCTION: Returns the byte offset within a page of the buffer described
178 * RETURNS: The offset in bytes
180 #define MmGetMdlByteOffset(Mdl) \
184 * FUNCTION: Returns the initial virtual address for a buffer described
188 * RETURNS: The initial virtual address
190 #define MmGetMdlVirtualAddress(Mdl) \
191 ((PVOID) ((PCHAR) (Mdl)->StartVa + (Mdl)->ByteOffset))
194 * FUNCTION: Returns the physical address corresponding to a given valid
197 * BaseAddress = the virtual address
198 * RETURNS: The physical address
202 MmGetPhysicalAddress (
206 #define MmGetProcedureAddress(Address) (Address)
210 * MmGetSystemAddressForMdl (
215 * Maps the physical pages described by an MDL into system space
221 * The base system address for the mapped buffer
223 #define MmGetSystemAddressForMdl(Mdl) \
224 (((Mdl)->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA | MDL_SOURCE_IS_NONPAGED_POOL)) ? \
225 ((Mdl)->MappedSystemVa):(MmMapLockedPages((Mdl),KernelMode)))
237 * PMDL MemoryDescriptorList,
246 * MemoryDescriptorList = MDL to be initalized
247 * BaseVa = Base virtual address of the buffer
248 * Length = Length in bytes of the buffer
250 #define MmInitializeMdl(MemoryDescriptorList,BaseVa,Length) \
252 (MemoryDescriptorList)->Next = (PMDL)NULL; \
253 (MemoryDescriptorList)->Size = (CSHORT)(sizeof(MDL) + \
254 (ADDRESS_AND_SIZE_TO_SPAN_PAGES((BaseVa),(Length)) * sizeof(ULONG))); \
255 (MemoryDescriptorList)->MdlFlags = 0; \
256 (MemoryDescriptorList)->StartVa = (PVOID)PAGE_ROUND_DOWN((BaseVa)); \
257 (MemoryDescriptorList)->ByteOffset = (ULONG)((BaseVa) - PAGE_ROUND_DOWN((BaseVa))); \
258 (MemoryDescriptorList)->ByteCount = (Length); \
259 (MemoryDescriptorList)->Process = PsGetCurrentProcess(); \
263 * FUNCTION: Checks whether an address is valid for read/write
265 * VirtualAddress = address to be check
266 * RETURNS: TRUE if an access would be valid
271 IN PVOID VirtualAddress
275 MmIsNonPagedSystemAddressValid (
276 IN PVOID VirtualAddress
280 MmIsRecursiveIoFault (
284 * FUNCTION: Checks if the current platform is a workstation or a server
285 * RETURNS: If the system is a server returns true
286 * NOTE: Drivers can use this as an estimate of the likely resources
291 MmIsThisAnNtAsSystem (
295 * FUNCTION: Locks a section of the driver's code into memory
297 * AddressWithinSection = Any address in the region
298 * RETURNS: A handle to the region
300 #define MmLockPagableCodeSection(Address) \
301 MmLockPagableDataSection(Address)
304 * FUNCTION: Locks a section of the driver's data into memory
306 * AddressWithinSection = Any address in the region
307 * RETURNS: A handle to the region
311 MmLockPagableDataSection (
312 PVOID AddressWithinSection
316 MmLockPagableImageSection (
317 PVOID AddressWithinSection
320 * FUNCTION: Locks a section of memory
322 * ImageSectionHandle = handle returned from MmLockPagableCodeSection
323 * or MmLockPagableDataSection
327 MmLockPagableSectionByHandle (
328 PVOID ImageSectionHandle
333 PHYSICAL_ADDRESS PhysicalAddress
,
338 * FUNCTION: Maps the pages described by a given MDL
340 * MemoryDescriptorList = MDL updated by MmProbeAndLockPages
341 * AccessMode = Access mode in which to map the MDL
342 * RETURNS: The base virtual address which maps the buffer
347 PMDL MemoryDescriptorList
,
348 KPROCESSOR_MODE AccessMode
358 IN PHYSICAL_ADDRESS PhysicalAddress
,
359 IN ULONG NumberOfBytes
,
360 IN MEMORY_CACHING_TYPE CacheType
364 MmMapViewInSystemSpace (
366 OUT PVOID
* MappedBase
,
372 IN PVOID SectionObject
,
373 IN PEPROCESS Process
,
374 IN OUT PVOID
* BaseAddress
,
377 IN OUT PLARGE_INTEGER SectionOffset OPTIONAL
,
378 IN OUT PULONG ViewSize
,
379 IN SECTION_INHERIT InheritDisposition
,
380 IN ULONG AllocationType
,
385 * FUNCTION: Makes the whole driver pageable
387 * AddressWithinSection = Any address within the driver
392 PVOID AddressWithinSection
397 * MmPrepareMdlForReuse (
402 * Reinitializes a caller-allocated MDL
405 * Mdl = Points to the MDL that will be reused
407 #define MmPrepareMdlForReuse(Mdl) \
408 if (((Mdl)->MdlFlags & MDL_PARTIAL_HAS_BEEN_MAPPED) != 0) \
410 assert(((Mdl)->MdlFlags & MDL_PARTIAL) != 0); \
411 MmUnmapLockedPages ((Mdl)->MappedSystemVa, (Mdl)); \
413 else if (((Mdl)->MdlFlags & MDL_PARTIAL) == 0) \
415 assert(((Mdl)->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA) == 0); \
419 * FUNCTION: Probes the specified pages, makes them resident and locks
420 * the physical pages mapped by the virtual address range
422 * MemoryDescriptorList = MDL which supplies the virtual address,
423 * byte offset and length
424 * AccessMode = Access mode with which to probe the arguments
425 * Operation = Types of operation for which the pages should be
430 MmProbeAndLockPages (
431 PMDL MemoryDescriptorList
,
432 KPROCESSOR_MODE AccessMode
,
433 LOCK_OPERATION Operation
437 * FUNCTION: Returns an estimate of the amount of memory in the system
438 * RETURNS: Either MmSmallSystem, MmMediumSystem or MmLargeSystem
447 * FUNCTION: Resets the pageable status of a driver's sections to their
448 * compile time settings
450 * AddressWithinSection = Any address within the driver
454 MmResetDriverPaging (
455 PVOID AddressWithinSection
460 MmSecureVirtualMemory (
467 MmSetAddressRangeModified (
483 * FUNCTION: Returns the number of bytes to allocate for an MDL
484 * describing a given address range
486 * Base = Base virtual address for the region
487 * Length = size in bytes of the region
488 * RETURNS: The number of bytes required for the MDL
497 * FUNCTION: Unlocks the physical pages described by a given MDL
499 * Mdl = Mdl to unlock
507 * FUNCTION: Releases a section of driver code or data previously locked into
510 * ImageSectionHandle = Handle for the locked section
514 MmUnlockPagableImageSection (
515 PVOID ImageSectionHandle
527 PMDL MemoryDescriptorList
531 MmUnmapVideoDisplay (
532 IN PVOID BaseAddress
,
533 IN ULONG NumberOfBytes
537 MmUnmapViewInSystemSpace (
543 MmUnmapViewOfSection (
545 PMEMORY_AREA MemoryArea
550 MmUnsecureVirtualMemory (