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
);
209 PVOID
MmAllocPage(SWAPENTRY SavedSwapEntry
);
210 VOID
MmDereferencePage(PVOID PhysicalAddress
);
211 VOID
MmReferencePage(PVOID PhysicalAddress
);
212 VOID
MmDeletePageTable(struct _EPROCESS
* Process
,
214 NTSTATUS
MmCopyMmInfo(struct _EPROCESS
* Src
,
215 struct _EPROCESS
* Dest
);
216 NTSTATUS
MmReleaseMmInfo(struct _EPROCESS
* Process
);
217 NTSTATUS
Mmi386ReleaseMmInfo(struct _EPROCESS
* Process
);
219 MmDeleteVirtualMapping(struct _EPROCESS
* Process
,
223 ULONG
* PhysicalPage
);
225 #define MM_PAGE_CLEAN (0)
226 #define MM_PAGE_DIRTY (1)
228 VOID
MmBuildMdlFromPages(PMDL Mdl
, PULONG Pages
);
229 PVOID
MmGetMdlPageAddress(PMDL Mdl
, PVOID Offset
);
230 VOID
MiShutdownMemoryManager(VOID
);
231 ULONG
MmGetPhysicalAddressForProcess(struct _EPROCESS
* Process
,
234 MmUnmapViewOfSection(struct _EPROCESS
* Process
, PVOID BaseAddress
);
235 VOID
MmInitPagingFile(VOID
);
237 /* FIXME: it should be in ddk/mmfuncs.h */
241 OUT PSECTION_OBJECT
* SectionObject
,
242 IN ACCESS_MASK DesiredAccess
,
243 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
,
244 IN PLARGE_INTEGER MaximumSize
,
245 IN ULONG SectionPageProtection
,
246 IN ULONG AllocationAttributes
,
247 IN HANDLE FileHandle OPTIONAL
,
248 IN PFILE_OBJECT File OPTIONAL
251 NTSTATUS
MmPageFault(ULONG Cs
,
258 MmAccessFault(KPROCESSOR_MODE Mode
,
262 MmNotPresentFault(KPROCESSOR_MODE Mode
,
266 MmNotPresentFaultVirtualMemory(PMADDRESS_SPACE AddressSpace
,
267 MEMORY_AREA
* MemoryArea
,
271 MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace
,
272 MEMORY_AREA
* MemoryArea
,
275 NTSTATUS
MmWaitForPage(PVOID Page
);
276 VOID
MmClearWaitPage(PVOID Page
);
277 VOID
MmSetWaitPage(PVOID Page
);
278 BOOLEAN
MmIsPageDirty(struct _EPROCESS
* Process
, PVOID Address
);
279 BOOLEAN
MmIsPageTablePresent(PVOID PAddress
);
280 ULONG
MmPageOutSectionView(PMADDRESS_SPACE AddressSpace
,
281 MEMORY_AREA
* MemoryArea
,
284 ULONG
MmPageOutVirtualMemory(PMADDRESS_SPACE AddressSpace
,
285 PMEMORY_AREA MemoryArea
,
288 MEMORY_AREA
* MmOpenMemoryAreaByRegion(PMADDRESS_SPACE AddressSpace
,
292 VOID
ExUnmapPage(PVOID Addr
);
293 PVOID
ExAllocatePage(VOID
);
295 VOID
MmLockWorkingSet(struct _EPROCESS
* Process
);
296 VOID
MmUnlockWorkingSet(struct _EPROCESS
* Process
);
297 VOID
MmInitializeWorkingSet(struct _EPROCESS
* Process
,
298 PMADDRESS_SPACE AddressSpace
);
299 ULONG
MmTrimWorkingSet(struct _EPROCESS
* Process
,
301 VOID
MmRemovePageFromWorkingSet(struct _EPROCESS
* Process
,
303 VOID
MmAddPageToWorkingSet(struct _EPROCESS
* Process
,
306 VOID
MmInitPagingFile(VOID
);
307 BOOLEAN
MmReserveSwapPages(ULONG Nr
);
308 VOID
MmDereserveSwapPages(ULONG Nr
);
309 SWAPENTRY
MmAllocSwapPage(VOID
);
310 VOID
MmFreeSwapPage(SWAPENTRY Entry
);
312 VOID
MmInit1(ULONG FirstKernelPhysAddress
,
313 ULONG LastKernelPhysAddress
,
314 ULONG LastKernelAddress
);
317 NTSTATUS
MmInitPagerThread(VOID
);
319 VOID
MmInitKernelMap(PVOID BaseAddress
);
321 VOID
MmWaitForFreePages(VOID
);
322 PVOID
MmMustAllocPage(SWAPENTRY SavedSwapEntry
);
323 PVOID
MmAllocPageMaybeSwap(SWAPENTRY SavedSwapEntry
);
324 NTSTATUS
MmCreatePageTable(PVOID PAddress
);
330 ULONG NrReservedPages
;
335 ULONG PagingRequestsInLastMinute
;
336 ULONG PagingRequestsInLastFiveMinutes
;
337 ULONG PagingRequestsInLastFifteenMinutes
;
340 extern MM_STATS MmStats
;
343 MmWritePageSectionView(PMADDRESS_SPACE AddressSpace
,
347 MmWritePageVirtualMemory(PMADDRESS_SPACE AddressSpace
,
351 MmGetDirtyPagesFromWorkingSet(struct _EPROCESS
* Process
);
353 MmWriteToSwapPage(SWAPENTRY SwapEntry
, PMDL Mdl
);
355 MmReadFromSwapPage(SWAPENTRY SwapEntry
, PMDL Mdl
);
357 MmSetFlagsPage(PVOID PhysicalAddress
, ULONG Flags
);
359 MmGetFlagsPage(PVOID PhysicalAddress
);
360 VOID
MmSetSavedSwapEntryPage(PVOID PhysicalAddress
,
361 SWAPENTRY SavedSwapEntry
);
362 SWAPENTRY
MmGetSavedSwapEntryPage(PVOID PhysicalAddress
);
363 VOID
MmSetCleanPage(struct _EPROCESS
* Process
, PVOID Address
);
364 VOID
MmLockPage(PVOID PhysicalPage
);
365 VOID
MmUnlockPage(PVOID PhysicalPage
);
367 NTSTATUS
MmSafeCopyFromUser(PVOID Dest
, PVOID Src
, ULONG Count
);
368 NTSTATUS
MmSafeCopyToUser(PVOID Dest
, PVOID Src
, ULONG Count
);
370 MmCreatePhysicalMemorySection(VOID
);
372 MmGetContinuousPages(ULONG NumberOfBytes
,
373 PHYSICAL_ADDRESS HighestAcceptableAddress
,
376 #define MM_PHYSICAL_PAGE_MPW_PENDING (0x8)
379 MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace
,
380 MEMORY_AREA
* MemoryArea
,
384 MmGetPageProtect(struct _EPROCESS
* Process
, PVOID Address
);
386 ExAllocatePageWithPhysPage(ULONG PhysPage
);
388 MmGetReferenceCountPage(PVOID PhysicalAddress
);
390 MmIsUsablePage(PVOID PhysicalAddress
);
392 #define MM_PAGEOP_PAGEIN (1)
393 #define MM_PAGEOP_PAGEOUT (2)
394 #define MM_PAGEOP_PAGESYNCH (3)
395 #define MM_PAGEOP_ACCESSFAULT (4)
397 typedef struct _MM_PAGEOP
399 /* Type of operation. */
401 /* Number of threads interested in this operation. */
402 ULONG ReferenceCount
;
403 /* Event that will be set when the operation is completed. */
404 KEVENT CompletionEvent
;
405 /* Status of the operation once it is completed. */
407 /* TRUE if the operation was abandoned. */
409 /* The memory area to be affected by the operation. */
412 struct _MM_PAGEOP
* Next
;
413 struct _ETHREAD
* Thread
;
415 * These fields are used to identify the operation if it is against a
416 * virtual memory area.
421 * These fields are used to identify the operation if it is against a
424 PMM_SECTION_SEGMENT Segment
;
426 } MM_PAGEOP
, *PMM_PAGEOP
;
429 MmReleasePageOp(PMM_PAGEOP PageOp
);
432 MmGetPageOp(PMEMORY_AREA MArea
, ULONG Pid
, PVOID Address
,
433 PMM_SECTION_SEGMENT Segment
, ULONG Offset
, ULONG OpType
);
436 MiDebugDumpNonPagedPool(BOOLEAN NewOnly
);
438 MiDebugDumpNonPagedPoolStats(BOOLEAN NewOnly
);
440 MmMarkPageMapped(PVOID PhysicalAddress
);
442 MmMarkPageUnmapped(PVOID PhysicalAddress
);
444 MmFreeSectionSegments(PFILE_OBJECT FileObject
);
446 typedef struct _MM_IMAGE_SECTION_OBJECT
449 MM_SECTION_SEGMENT Segments
[0];
450 } MM_IMAGE_SECTION_OBJECT
, *PMM_IMAGE_SECTION_OBJECT
;
453 MmFreeVirtualMemory(struct _EPROCESS
* Process
, PMEMORY_AREA MemoryArea
);
455 MiCopyFromUserPage(ULONG DestPhysPage
, PVOID SourceAddress
);
457 MiZeroPage(ULONG PhysPage
);
459 MmIsAccessedAndResetAccessPage(struct _EPROCESS
* Process
, PVOID Address
);
461 MmGetSavedSwapEntryPage(PVOID PhysicalAddress
);
463 #define STATUS_MM_RESTART_OPERATION (0xD0000001)
466 MmCreateVirtualMappingForKernel(PVOID Address
,
468 ULONG PhysicalAddress
);