1 /* MEMORY MANAGMENT ******************************************************/
3 #include <internal/hal/page.h>
6 * FUNCTION: Determines if the given virtual address is page aligned
8 #define IS_PAGE_ALIGNED(Va) (((ULONG)Va)&0xfff)
11 * PURPOSE: Returns the byte offset of a field within a structure
13 #define FIELD_OFFSET(Type,Field) (LONG)(&(((Type *)(0))->Field))
16 * PURPOSE: Returns the base address structure if the caller knows the
17 * address of a field within the structure
19 * Address = address of the field
20 * Type = Type of the whole structure
21 * Field = Name of the field whose address is none
23 #define CONTAINING_RECORD(Address,Type,Field) (Type *)(((LONG)Address) - FIELD_OFFSET(Type,Field))
26 * FUNCTION: Returns the number of pages spanned by an address range
29 * Size = Size of range
30 * RETURNS: The number of pages
32 extern inline unsigned int ADDRESS_AND_SIZE_TO_SPAN_PAGES(PVOID Va
,
38 HighestAddr
= PAGE_ROUND_UP(Size
+ ((ULONG
)Va
));
39 LowestAddr
= PAGE_ROUND_DOWN((ULONG
)Va
);
40 return((HighestAddr
- LowestAddr
) / PAGESIZE
);
44 * FUNCTION: Returns FALSE is the pointer is NULL, TRUE otherwise
46 #define ARGUMENT_PRESENT(arg) ((arg)!=NULL)
49 * FUNCTION: Returns the byte offset of the address within its page
51 #define BYTE_OFFSET(va) (((ULONG)va)%PAGESIZE)
54 * FUNCTION: Takes a count in bytes and returns the number of pages
57 #define BYTES_TO_PAGES(size) (?)
60 * FUNCTION: Allocates a range of physically contiguous cache aligned
61 * memory from the non-paged pool
63 * NumberOfBytes = Size of the memory block to allocate
64 * HighestAcceptableAddress = Highest address valid for the caller
65 * RETURNS: The virtual address of the memory block on success
68 PVOID
MmAllocateContiguousMemory(ULONG NumberOfBytes
,
69 PHYSICAL_ADDRESS HighestAcceptableAddress
);
72 * FUNCTION: Allocates a virtual address range of noncached and cache
75 * NumberOfBytes = Size of region to allocate
76 * RETURNS: The base address of the range on success
79 PVOID
MmAllocateNonCachedMemory(ULONG NumberOfBytes
);
82 * FUNCTION: Fills in the corresponding physical page array for a given MDL
83 * for a buffer in nonpaged system space
85 * MemoryDescriptorList = MDL to fill
87 VOID
MmBuildMdlForNonPagedPool(PMDL MemoryDescriptorList
);
90 * FUNCTION: Allocates and initializes an MDL
92 * MemoryDescriptorList = Optional caller allocated MDL to initalize
93 * Base = Base virtual address for buffer
94 * Length = Length in bytes of the buffer
95 * RETURNS: A pointer to the initalized MDL
97 PMDL
MmCreateMdl(PMDL MemoryDescriptorList
, PVOID Base
, ULONG Length
);
100 * FUNCTION: Releases a range of physically contiguous memory allocated
101 * with MmAllocateContiguousMemory
103 * BaseAddress = Vritual address of the memory to be freed
105 VOID
MmFreeContiguousMemory(PVOID BaseAddress
);
108 * FUNCTION: Releases a range of noncached memory allocated with
109 * MmAllocateNonCachedMemory
111 * BaseAddress = Virtual address to be freed
112 * NumberOfBytes = size of the region to be freed
114 VOID
MmFreeNonCachedMemory(PVOID BaseAddress
, ULONG NumberOfBytes
);
117 * FUNCTION: Returns the length in bytes of a buffer described by an MDL
120 * RETURNS: Size of the buffer
122 ULONG
MmGetMdlByteCount(PMDL Mdl
);
125 * FUNCTION: Returns the byte offset within a page of the buffer described
129 * RETURNS: The offset in bytes
131 ULONG
MmGetMdlByteOffset(PMDL Mdl
);
134 * FUNCTION: Returns the initial virtual address for a buffer described
138 * RETURNS: The initial virtual address
140 PVOID
MmGetMdlVirtualAddress(PMDL Mdl
);
143 * FUNCTION: Returns the physical address corresponding to a given valid
146 * BaseAddress = the virtual address
147 * RETURNS: The physical address
149 PHYSICAL_ADDRESS
MmGetPhysicalAddress(PVOID BaseAddress
);
152 * FUNCTION: Maps the physical pages described by an MDL into system space
155 * RETURNS: The base system address for the mapped buffer
157 PVOID
MmGetSystemAddressForMdl(PMDL Mdl
);
160 * FUNCTION: Initalizes an mdl
162 * MemoryDescriptorList = MDL to be initalized
163 * BaseVa = Base virtual address of the buffer
164 * Length = Length in bytes of the buffer
166 VOID
MmInitializeMdl(PMDL MemoryDescriptorList
, PVOID BaseVa
, ULONG Length
);
169 * FUNCTION: Checks whether an address is valid for read/write
171 * VirtualAddress = address to be check
172 * RETURNS: TRUE if an access would be valid
174 BOOLEAN
MmIsAddressValid(PVOID VirtualAddress
);
177 * FUNCTION: Checks if the current platform is a workstation or a server
178 * RETURNS: If the system is a server returns true
179 * NOTE: Drivers can use this as an estimate of the likely resources
182 BOOLEAN
MmIsThisAnAsSystem(VOID
);
185 * FUNCTION: Locks a section of the driver's code into memory
187 * AddressWithinSection = Any address in the region
188 * RETURNS: A handle to the region
190 PVOID
MmLockPagableCodeSection(PVOID AddressWithinSection
);
193 * FUNCTION: Locks a section of the driver's data into memory
195 * AddressWithinSection = Any address in the region
196 * RETURNS: A handle to the region
198 PVOID
MmLockPagableDataSection(PVOID AddressWithinSection
);
201 * FUNCTION: Locks a section of memory
203 * ImageSectionHandle = handle returned from MmLockPagableCodeSection
204 * or MmLockPagableDataSection
206 VOID
MmLockPagableSectionByHandle(PVOID ImageSectionHandle
);
209 * FUNCTION: Maps a physical memory range into system space
211 * PhysicalAddress = First physical address to map
212 * NumberOfBytes = Number of bytes to map
213 * CacheEnable = TRUE if the range can be cached
214 * RETURNS: The base virtual address which maps the region
216 PVOID
MmMapIoSpace(PHYSICAL_ADDRESS PhysicalAddress
, ULONG NumberOfBytes
,
217 BOOLEAN CacheEnable
);
220 * FUNCTION: Maps the pages described by a given MDL
222 * MemoryDescriptorList = MDL updated by MmProbeAndLockPages
223 * AccessMode = Access mode in which to map the MDL
224 * RETURNS: The base virtual address which maps the buffer
226 PVOID
MmMapLockedPages(PMDL MemoryDescriptorList
, KPROCESSOR_MODE AccessMode
);
229 * FUNCTION: Makes the whole driver pageable
231 * AddressWithinSection = Any address within the driver
233 VOID
MmPageEntireDriver(PVOID AddressWithinSection
);
236 * FUNCTION: Resets the pageable status of a driver's sections to their
237 * compile time settings
239 * AddressWithinSection = Any address within the driver
241 VOID
MmResetDriverPaging(PVOID AddressWithinSection
);
244 * FUNCTION: Reinitializes a caller-allocated MDL
246 * Mdl = Points to the MDL that will be reused
248 VOID
MmPrepareMdlForReuse(PMDL Mdl
);
251 * FUNCTION: Probes the specified pages, makes them resident and locks
252 * the physical pages mapped by the virtual address range
254 * MemoryDescriptorList = MDL which supplies the virtual address,
255 * byte offset and length
256 * AccessMode = Access mode with which to probe the arguments
257 * Operation = Types of operation for which the pages should be
260 VOID
MmProbeAndLockPages(PMDL MemoryDescriptorList
,
261 KPROCESSOR_MODE AccessMode
,
262 LOCK_OPERATION Operation
);
265 * FUNCTION: Returns an estimate of the amount of memory in the system
266 * RETURNS: Either MmSmallSystem, MmMediumSystem or MmLargeSystem
268 MM_SYSTEM_SIZE
MmQuerySystemSize(VOID
);
271 * FUNCTION: Returns the number of bytes to allocate for an MDL
272 * describing a given address range
274 * Base = Base virtual address for the region
275 * Length = size in bytes of the region
276 * RETURNS: The number of bytes required for the MDL
278 ULONG
MmSizeOfMdl(PVOID Base
, ULONG Length
);
281 * FUNCTION: Unlocks the physical pages described by a given MDL
283 * Mdl = Mdl to unlock
285 VOID
MmUnlockPages(PMDL Mdl
);
288 * FUNCTION: Releases a section of driver code or data previously locked into
291 * ImageSectionHandle = Handle for the locked section
293 VOID
MmUnlockPagableImageSection(PVOID ImageSectionHandle
);
295 VOID
MmUnmapIoSpace(PVOID BaseAddress
, ULONG NumberOfBytes
);
296 VOID
MmUnmapLockedPages(PVOID BaseAddress
, PMDL MemoryDescriptorList
);
297 PVOID
MmAllocateSection(ULONG Length
);