2 * Higher level memory managment definitions
5 #ifndef __INCLUDE_INTERNAL_MM_H
6 #define __INCLUDE_INTERNAL_MM_H
8 #include <internal/ntoskrnl.h>
9 #include <internal/arch/mm.h>
11 /* TYPES *********************************************************************/
14 typedef ULONG SWAPENTRY
;
16 #define MEMORY_AREA_INVALID (0)
17 #define MEMORY_AREA_SECTION_VIEW_COMMIT (1)
18 #define MEMORY_AREA_CONTINUOUS_MEMORY (2)
19 #define MEMORY_AREA_NO_CACHE (3)
20 #define MEMORY_AREA_IO_MAPPING (4)
21 #define MEMORY_AREA_SYSTEM (5)
22 #define MEMORY_AREA_MDL_MAPPING (7)
23 #define MEMORY_AREA_VIRTUAL_MEMORY (8)
24 #define MEMORY_AREA_SECTION_VIEW_RESERVE (9)
25 #define MEMORY_AREA_CACHE_SEGMENT (10)
26 #define MEMORY_AREA_SHARED_DATA (11)
27 #define MEMORY_AREA_WORKING_SET (12)
28 #define MEMORY_AREA_KERNEL_STACK (13)
30 #define PAGE_TO_SECTION_PAGE_DIRECTORY_OFFSET(x) \
32 #define PAGE_TO_SECTION_PAGE_TABLE_OFFSET(x) \
33 ((((x)) % (4*1024*1024)) / (4*1024))
35 #define NR_SECTION_PAGE_TABLES (1024)
36 #define NR_SECTION_PAGE_ENTRIES (1024)
39 * Flags for section objects
41 #define SO_PHYSICAL_MEMORY (0x1)
44 * Additional flags for protection attributes
46 #define PAGE_WRITETHROUGH (1024)
47 #define PAGE_SYSTEM (2048)
48 #define PAGE_FLAGS_VALID_FROM_USER_MODE (PAGE_READONLY | \
53 PAGE_EXECUTE_READWRITE | \
54 PAGE_EXECUTE_WRITECOPY | \
61 ULONG Pages
[NR_SECTION_PAGE_ENTRIES
];
62 } SECTION_PAGE_TABLE
, *PSECTION_PAGE_TABLE
;
66 PSECTION_PAGE_TABLE PageTables
[NR_SECTION_PAGE_TABLES
];
67 } SECTION_PAGE_DIRECTORY
, *PSECTION_PAGE_DIRECTORY
;
69 #define MM_PAGEFILE_SECTION (0x1)
70 #define MM_IMAGE_SECTION (0x2)
72 #define MM_SECTION_SEGMENT_BSS (0x1)
74 typedef struct _MM_SECTION_SEGMENT
83 SECTION_PAGE_DIRECTORY PageDirectory
;
86 ULONG Characteristics
;
87 } MM_SECTION_SEGMENT
, *PMM_SECTION_SEGMENT
;
93 LARGE_INTEGER MaximumSize
;
94 ULONG SectionPageProtection
;
95 ULONG AllocateAttributes
;
96 PFILE_OBJECT FileObject
;
97 LIST_ENTRY ViewListHead
;
98 KSPIN_LOCK ViewListLock
;
102 PMM_SECTION_SEGMENT Segments
;
108 ULONG MinorSubsystemVersion
;
109 ULONG MajorSubsystemVersion
;
110 ULONG ImageCharacteristics
;
113 } SECTION_OBJECT
, *PSECTION_OBJECT
;
123 struct _EPROCESS
* Process
;
128 SECTION_OBJECT
* Section
;
130 LIST_ENTRY ViewListEntry
;
131 PMM_SECTION_SEGMENT Segment
;
135 LIST_ENTRY SegmentListHead
;
138 } MEMORY_AREA
, *PMEMORY_AREA
;
140 typedef struct _KCIRCULAR_QUEUE
147 } KCIRCULAR_QUEUE
, *PKCIRCULAR_QUEUE
;
149 typedef struct _MADDRESS_SPACE
151 LIST_ENTRY MAreaListHead
;
154 struct _EPROCESS
* Process
;
155 PMEMORY_AREA WorkingSetArea
;
156 KCIRCULAR_QUEUE WSQueue
;
157 PUSHORT PageTableRefCountTable
;
158 ULONG PageTableRefCountTableSize
;
159 } MADDRESS_SPACE
, *PMADDRESS_SPACE
;
163 VOID
MmLockAddressSpace(PMADDRESS_SPACE AddressSpace
);
164 VOID
MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace
);
165 VOID
MmInitializeKernelAddressSpace(VOID
);
166 PMADDRESS_SPACE
MmGetCurrentAddressSpace(VOID
);
167 PMADDRESS_SPACE
MmGetKernelAddressSpace(VOID
);
168 NTSTATUS
MmInitializeAddressSpace(struct _EPROCESS
* Process
,
169 PMADDRESS_SPACE AddressSpace
);
170 NTSTATUS
MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace
);
171 PVOID STDCALL
MmAllocateSection (IN ULONG Length
);
172 NTSTATUS
MmCreateMemoryArea(struct _EPROCESS
* Process
,
173 PMADDRESS_SPACE AddressSpace
,
178 MEMORY_AREA
** Result
,
180 MEMORY_AREA
* MmOpenMemoryAreaByAddress(PMADDRESS_SPACE AddressSpace
,
182 NTSTATUS
MmInitMemoryAreas(VOID
);
183 VOID
ExInitNonPagedPool(ULONG BaseAddress
);
184 NTSTATUS
MmFreeMemoryArea(PMADDRESS_SPACE AddressSpace
,
187 VOID (*FreePage
)(PVOID Context
, PVOID Address
,
189 PVOID FreePageContext
);
190 VOID
MmDumpMemoryAreas(PLIST_ENTRY ListHead
);
191 NTSTATUS
MmLockMemoryArea(MEMORY_AREA
* MemoryArea
);
192 NTSTATUS
MmUnlockMemoryArea(MEMORY_AREA
* MemoryArea
);
193 NTSTATUS
MmInitSectionImplementation(VOID
);
195 #define MM_LOWEST_USER_ADDRESS (4096)
197 PMEMORY_AREA
MmSplitMemoryArea(struct _EPROCESS
* Process
,
198 PMADDRESS_SPACE AddressSpace
,
199 PMEMORY_AREA OriginalMemoryArea
,
203 ULONG NewAttributes
);
204 PVOID
MmInitializePageList(PVOID FirstPhysKernelAddress
,
205 PVOID LastPhysKernelAddress
,
206 ULONG MemorySizeInPages
,
207 ULONG LastKernelBase
,
208 PADDRESS_RANGE BIOSMemoryMap
,
209 ULONG AddressRangeCount
);
211 PVOID
MmAllocPage(SWAPENTRY SavedSwapEntry
);
212 VOID
MmDereferencePage(PVOID PhysicalAddress
);
213 VOID
MmReferencePage(PVOID PhysicalAddress
);
214 VOID
MmDeletePageTable(struct _EPROCESS
* Process
,
216 NTSTATUS
MmCopyMmInfo(struct _EPROCESS
* Src
,
217 struct _EPROCESS
* Dest
);
218 NTSTATUS
MmReleaseMmInfo(struct _EPROCESS
* Process
);
219 NTSTATUS
Mmi386ReleaseMmInfo(struct _EPROCESS
* Process
);
221 MmDeleteVirtualMapping(struct _EPROCESS
* Process
,
225 ULONG
* PhysicalPage
);
227 #define MM_PAGE_CLEAN (0)
228 #define MM_PAGE_DIRTY (1)
230 VOID
MmBuildMdlFromPages(PMDL Mdl
, PULONG Pages
);
231 PVOID
MmGetMdlPageAddress(PMDL Mdl
, PVOID Offset
);
232 VOID
MiShutdownMemoryManager(VOID
);
233 ULONG
MmGetPhysicalAddressForProcess(struct _EPROCESS
* Process
,
236 MmUnmapViewOfSection(struct _EPROCESS
* Process
, PVOID BaseAddress
);
237 VOID
MmInitPagingFile(VOID
);
239 /* FIXME: it should be in ddk/mmfuncs.h */
243 OUT PSECTION_OBJECT
* SectionObject
,
244 IN ACCESS_MASK DesiredAccess
,
245 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
,
246 IN PLARGE_INTEGER MaximumSize
,
247 IN ULONG SectionPageProtection
,
248 IN ULONG AllocationAttributes
,
249 IN HANDLE FileHandle OPTIONAL
,
250 IN PFILE_OBJECT File OPTIONAL
253 NTSTATUS
MmPageFault(ULONG Cs
,
260 MmAccessFault(KPROCESSOR_MODE Mode
,
264 MmNotPresentFault(KPROCESSOR_MODE Mode
,
268 MmNotPresentFaultVirtualMemory(PMADDRESS_SPACE AddressSpace
,
269 MEMORY_AREA
* MemoryArea
,
273 MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace
,
274 MEMORY_AREA
* MemoryArea
,
277 NTSTATUS
MmWaitForPage(PVOID Page
);
278 VOID
MmClearWaitPage(PVOID Page
);
279 VOID
MmSetWaitPage(PVOID Page
);
280 BOOLEAN
MmIsPageDirty(struct _EPROCESS
* Process
, PVOID Address
);
281 BOOLEAN
MmIsPageTablePresent(PVOID PAddress
);
282 ULONG
MmPageOutSectionView(PMADDRESS_SPACE AddressSpace
,
283 MEMORY_AREA
* MemoryArea
,
286 ULONG
MmPageOutVirtualMemory(PMADDRESS_SPACE AddressSpace
,
287 PMEMORY_AREA MemoryArea
,
290 MEMORY_AREA
* MmOpenMemoryAreaByRegion(PMADDRESS_SPACE AddressSpace
,
294 VOID
ExUnmapPage(PVOID Addr
);
295 PVOID
ExAllocatePage(VOID
);
297 VOID
MmLockWorkingSet(struct _EPROCESS
* Process
);
298 VOID
MmUnlockWorkingSet(struct _EPROCESS
* Process
);
299 VOID
MmInitializeWorkingSet(struct _EPROCESS
* Process
,
300 PMADDRESS_SPACE AddressSpace
);
301 ULONG
MmTrimWorkingSet(struct _EPROCESS
* Process
,
303 VOID
MmRemovePageFromWorkingSet(struct _EPROCESS
* Process
,
305 VOID
MmAddPageToWorkingSet(struct _EPROCESS
* Process
,
308 VOID
MmInitPagingFile(VOID
);
309 BOOLEAN
MmReserveSwapPages(ULONG Nr
);
310 VOID
MmDereserveSwapPages(ULONG Nr
);
311 SWAPENTRY
MmAllocSwapPage(VOID
);
312 VOID
MmFreeSwapPage(SWAPENTRY Entry
);
314 VOID
MmInit1(ULONG FirstKernelPhysAddress
,
315 ULONG LastKernelPhysAddress
,
316 ULONG LastKernelAddress
,
317 PADDRESS_RANGE BIOSMemoryMap
,
318 ULONG AddressRangeCount
);
321 NTSTATUS
MmInitPagerThread(VOID
);
323 VOID
MmInitKernelMap(PVOID BaseAddress
);
325 VOID
MmWaitForFreePages(VOID
);
326 PVOID
MmMustAllocPage(SWAPENTRY SavedSwapEntry
);
327 PVOID
MmAllocPageMaybeSwap(SWAPENTRY SavedSwapEntry
);
328 NTSTATUS
MmCreatePageTable(PVOID PAddress
);
334 ULONG NrReservedPages
;
339 ULONG PagingRequestsInLastMinute
;
340 ULONG PagingRequestsInLastFiveMinutes
;
341 ULONG PagingRequestsInLastFifteenMinutes
;
344 extern MM_STATS MmStats
;
347 MmWritePageSectionView(PMADDRESS_SPACE AddressSpace
,
351 MmWritePageVirtualMemory(PMADDRESS_SPACE AddressSpace
,
355 MmGetDirtyPagesFromWorkingSet(struct _EPROCESS
* Process
);
357 MmWriteToSwapPage(SWAPENTRY SwapEntry
, PMDL Mdl
);
359 MmReadFromSwapPage(SWAPENTRY SwapEntry
, PMDL Mdl
);
361 MmSetFlagsPage(PVOID PhysicalAddress
, ULONG Flags
);
363 MmGetFlagsPage(PVOID PhysicalAddress
);
364 VOID
MmSetSavedSwapEntryPage(PVOID PhysicalAddress
,
365 SWAPENTRY SavedSwapEntry
);
366 SWAPENTRY
MmGetSavedSwapEntryPage(PVOID PhysicalAddress
);
367 VOID
MmSetCleanPage(struct _EPROCESS
* Process
, PVOID Address
);
368 VOID
MmLockPage(PVOID PhysicalPage
);
369 VOID
MmUnlockPage(PVOID PhysicalPage
);
371 NTSTATUS
MmSafeCopyFromUser(PVOID Dest
, PVOID Src
, ULONG Count
);
372 NTSTATUS
MmSafeCopyToUser(PVOID Dest
, PVOID Src
, ULONG Count
);
374 MmCreatePhysicalMemorySection(VOID
);
376 MmGetContinuousPages(ULONG NumberOfBytes
,
377 PHYSICAL_ADDRESS HighestAcceptableAddress
,
380 #define MM_PHYSICAL_PAGE_MPW_PENDING (0x8)
383 MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace
,
384 MEMORY_AREA
* MemoryArea
,
388 MmGetPageProtect(struct _EPROCESS
* Process
, PVOID Address
);
390 ExAllocatePageWithPhysPage(ULONG PhysPage
);
392 MmGetReferenceCountPage(PVOID PhysicalAddress
);
394 MmIsUsablePage(PVOID PhysicalAddress
);
396 #define MM_PAGEOP_PAGEIN (1)
397 #define MM_PAGEOP_PAGEOUT (2)
398 #define MM_PAGEOP_PAGESYNCH (3)
399 #define MM_PAGEOP_ACCESSFAULT (4)
401 typedef struct _MM_PAGEOP
403 /* Type of operation. */
405 /* Number of threads interested in this operation. */
406 ULONG ReferenceCount
;
407 /* Event that will be set when the operation is completed. */
408 KEVENT CompletionEvent
;
409 /* Status of the operation once it is completed. */
411 /* TRUE if the operation was abandoned. */
413 /* The memory area to be affected by the operation. */
416 struct _MM_PAGEOP
* Next
;
417 struct _ETHREAD
* Thread
;
419 * These fields are used to identify the operation if it is against a
420 * virtual memory area.
425 * These fields are used to identify the operation if it is against a
428 PMM_SECTION_SEGMENT Segment
;
430 } MM_PAGEOP
, *PMM_PAGEOP
;
433 MmReleasePageOp(PMM_PAGEOP PageOp
);
436 MmGetPageOp(PMEMORY_AREA MArea
, ULONG Pid
, PVOID Address
,
437 PMM_SECTION_SEGMENT Segment
, ULONG Offset
, ULONG OpType
);
440 MiDebugDumpNonPagedPool(BOOLEAN NewOnly
);
442 MiDebugDumpNonPagedPoolStats(BOOLEAN NewOnly
);
444 MmMarkPageMapped(PVOID PhysicalAddress
);
446 MmMarkPageUnmapped(PVOID PhysicalAddress
);
448 MmFreeSectionSegments(PFILE_OBJECT FileObject
);
450 typedef struct _MM_IMAGE_SECTION_OBJECT
453 MM_SECTION_SEGMENT Segments
[0];
454 } MM_IMAGE_SECTION_OBJECT
, *PMM_IMAGE_SECTION_OBJECT
;
457 MmFreeVirtualMemory(struct _EPROCESS
* Process
, PMEMORY_AREA MemoryArea
);
459 MiCopyFromUserPage(ULONG DestPhysPage
, PVOID SourceAddress
);
461 MiZeroPage(ULONG PhysPage
);
463 MmIsAccessedAndResetAccessPage(struct _EPROCESS
* Process
, PVOID Address
);
465 MmGetSavedSwapEntryPage(PVOID PhysicalAddress
);
467 #define STATUS_MM_RESTART_OPERATION (0xD0000001)
470 MmCreateVirtualMappingForKernel(PVOID Address
,
472 ULONG PhysicalAddress
);