3 Copyright (c) Alex Ionescu. All rights reserved.
11 Type definitions for the Memory Manager
15 Alex Ionescu (alexi@tinykrnl.org) - Updated - 27-Feb-2006
26 #include <arch/mmtypes.h>
30 // Page-Rounding Macros
32 #define PAGE_ROUND_DOWN(x) \
33 (((ULONG_PTR)(x))&(~(PAGE_SIZE-1)))
34 #define PAGE_ROUND_UP(x) \
35 ( (((ULONG_PTR)(x)) + PAGE_SIZE-1) & (~(PAGE_SIZE-1)) )
37 #define ROUND_TO_PAGES(Size) \
38 (((ULONG_PTR)(Size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
40 #define ROUND_TO_ALLOCATION_GRANULARITY(Size) \
41 (((ULONG_PTR)(Size) + MM_ALLOCATION_GRANULARITY - 1) \
42 & ~(MM_ALLOCATION_GRANULARITY - 1))
47 #define MMPFNUSE_PROCESSPRIVATE 0
48 #define MMPFNUSE_FILE 1
49 #define MMPFNUSE_PAGEFILEMAPPED 2
50 #define MMPFNUSE_PAGETABLE 3
51 #define MMPFNUSE_PAGEDPOOL 4
52 #define MMPFNUSE_NONPAGEDPOOL 5
53 #define MMPFNUSE_SYSTEMPTE 6
54 #define MMPFNUSE_SESSIONPRIVATE 7
55 #define MMPFNUSE_METAFILE 8
56 #define MMPFNUSE_AWEPAGE 9
57 #define MMPFNUSE_DRIVERLOCKPAGE 10
58 #define MMPFNUSE_KERNELSTACK 11
61 // Lock/Unlock Virtuam Memory Flags
66 #ifndef NTOS_MODE_USER
69 // Virtual Memory Flags
71 #define MEM_WRITE_WATCH 0x200000
72 #define MEM_PHYSICAL 0x400000
73 #define MEM_ROTATE 0x800000
74 #define MEM_IMAGE SEC_IMAGE
75 #define MEM_DOS_LIM 0x40000000
78 // Section Flags for NtCreateSection
80 #define SEC_NO_CHANGE 0x400000
81 #define SEC_FILE 0x800000
82 #define SEC_IMAGE 0x1000000
83 #define SEC_PROTECTED_IMAGE 0x2000000
84 #define SEC_RESERVE 0x4000000
85 #define SEC_COMMIT 0x8000000
86 #define SEC_NOCACHE 0x10000000
87 #define SEC_WRITECOMBINE 0x40000000
88 #define SEC_LARGE_PAGES 0x80000000
90 #define SEC_BASED 0x200000
93 // Section Inherit Flags for NtCreateSection
95 typedef enum _SECTION_INHERIT
104 typedef enum _POOL_TYPE
108 NonPagedPoolMustSucceed
,
110 NonPagedPoolCacheAligned
,
111 PagedPoolCacheAligned
,
112 NonPagedPoolCacheAlignedMustS
,
114 NonPagedPoolSession
= 32,
116 NonPagedPoolMustSucceedSession
,
117 DontUseThisTypeSession
,
118 NonPagedPoolCacheAlignedSession
,
119 PagedPoolCacheAlignedSession
,
120 NonPagedPoolCacheAlignedMustSSession
125 // Memory Manager Page Lists
127 typedef enum _MMLISTS
132 ModifiedPageList
= 3,
133 ModifiedNoWritePageList
= 4,
140 // Per Processor Non Paged Lookaside List IDs
142 typedef enum _PP_NPAGED_LOOKASIDE_NUMBER
144 LookasideSmallIrpList
= 0,
145 LookasideLargeIrpList
= 1,
146 LookasideMdlList
= 2,
147 LookasideCreateInfoList
= 3,
148 LookasideNameBufferList
= 4,
149 LookasideTwilightList
= 5,
150 LookasideCompletionList
= 6,
151 LookasideMaximumList
= 7
152 } PP_NPAGED_LOOKASIDE_NUMBER
;
155 // Memory Information Classes for NtQueryVirtualMemory
157 typedef enum _MEMORY_INFORMATION_CLASS
159 MemoryBasicInformation
,
160 MemoryWorkingSetList
,
162 MemoryBasicVlmInformation
163 } MEMORY_INFORMATION_CLASS
;
166 // Section Information Clasess for NtQuerySection
168 typedef enum _SECTION_INFORMATION_CLASS
170 SectionBasicInformation
,
171 SectionImageInformation
,
172 } SECTION_INFORMATION_CLASS
;
174 #ifdef NTOS_MODE_USER
177 // Virtual Memory Counters
179 typedef struct _VM_COUNTERS
181 SIZE_T PeakVirtualSize
;
183 ULONG PageFaultCount
;
184 SIZE_T PeakWorkingSetSize
;
185 SIZE_T WorkingSetSize
;
186 SIZE_T QuotaPeakPagedPoolUsage
;
187 SIZE_T QuotaPagedPoolUsage
;
188 SIZE_T QuotaPeakNonPagedPoolUsage
;
189 SIZE_T QuotaNonPagedPoolUsage
;
190 SIZE_T PagefileUsage
;
191 SIZE_T PeakPagefileUsage
;
192 } VM_COUNTERS
, *PVM_COUNTERS
;
194 typedef struct _VM_COUNTERS_EX
196 SIZE_T PeakVirtualSize
;
198 ULONG PageFaultCount
;
199 SIZE_T PeakWorkingSetSize
;
200 SIZE_T WorkingSetSize
;
201 SIZE_T QuotaPeakPagedPoolUsage
;
202 SIZE_T QuotaPagedPoolUsage
;
203 SIZE_T QuotaPeakNonPagedPoolUsage
;
204 SIZE_T QuotaNonPagedPoolUsage
;
205 SIZE_T PagefileUsage
;
206 SIZE_T PeakPagefileUsage
;
208 } VM_COUNTERS_EX
, *PVM_COUNTERS_EX
;
212 // Sub-Information Types for PFN Identity
214 typedef struct _MEMORY_FRAME_INFORMATION
216 ULONGLONG UseDescription
:4;
217 ULONGLONG ListDescription
:3;
218 ULONGLONG Reserved0
:1;
220 ULONGLONG DontUse
:48;
221 ULONGLONG Priority
:3;
222 ULONGLONG Reserved
:4;
223 } MEMORY_FRAME_INFORMATION
, *PMEMORY_FRAME_INFORMATION
;
225 typedef struct _FILEOFFSET_INFORMATION
229 ULONGLONG Reserved
:7;
230 } FILEOFFSET_INFORMATION
, *PFILEOFFSET_INFORMATION
;
232 typedef struct _PAGEDIR_INFORMATION
235 ULONGLONG PageDirectoryBase
:48;
236 ULONGLONG Reserved
:7;
237 } PAGEDIR_INFORMATION
, *PPAGEDIR_INFORMATION
;
239 typedef struct _UNIQUE_PROCESS_INFORMATION
242 ULONGLONG UniqueProcessKey
:48;
243 ULONGLONG Reserved
:7;
244 } UNIQUE_PROCESS_INFORMATION
, *PUNIQUE_PROCESS_INFORMATION
;
247 // PFN Identity Data Structure
249 typedef struct _MMPFN_IDENTITY
253 MEMORY_FRAME_INFORMATION e1
;
254 FILEOFFSET_INFORMATION e2
;
255 PAGEDIR_INFORMATION e3
;
256 UNIQUE_PROCESS_INFORMATION e4
;
258 SIZE_T PageFrameIndex
;
267 PVOID UniqueFileObjectKey
;
268 PVOID ProtoPteAddress
;
269 PVOID VirtualAddress
;
271 } MMPFN_IDENTITY
, *PMMPFN_IDENTITY
;
274 // List of Working Sets
276 typedef struct _MEMORY_WORKING_SET_LIST
279 ULONG WorkingSetList
[1];
280 } MEMORY_WORKING_SET_LIST
, *PMEMORY_WORKING_SET_LIST
;
283 // Memory Information Structures for NtQueryVirtualMemory
287 UNICODE_STRING SectionFileName
;
288 WCHAR NameBuffer
[ANYSIZE_ARRAY
];
289 } MEMORY_SECTION_NAME
, *PMEMORY_SECTION_NAME
;
292 // Section Information Structures for NtQuerySection
294 typedef struct _SECTION_BASIC_INFORMATION
299 } SECTION_BASIC_INFORMATION
, *PSECTION_BASIC_INFORMATION
;
301 typedef struct _SECTION_IMAGE_INFORMATION
303 PVOID TransferAddress
;
305 ULONG MaximumStackSize
;
306 ULONG CommittedStackSize
;
308 USHORT SubSystemMinorVersion
;
309 USHORT SubSystemMajorVersion
;
311 USHORT ImageCharacteristics
;
312 USHORT DllCharacteristics
;
314 UCHAR ImageContainsCode
;
319 } SECTION_IMAGE_INFORMATION
, *PSECTION_IMAGE_INFORMATION
;
321 #ifndef NTOS_MODE_USER
326 typedef struct _MMPTE
333 MMPTE_PROTOTYPE Proto
;
335 MMPTE_TRANSITION Trans
;
336 MMPTE_SUBSECTION Subsect
;
342 // Section Extension Information
344 typedef struct _MMEXTEND_INFO
346 ULONGLONG CommittedSize
;
347 ULONG ReferenceCount
;
348 } MMEXTEND_INFO
, *PMMEXTEND_INFO
;
351 // Segment and Segment Flags
353 typedef struct _SEGMENT_FLAGS
355 ULONG TotalNumberOfPtes4132
:10;
356 ULONG ExtraSharedWowSubsections
:1;
359 } SEGMENT_FLAGS
, *PSEGMENT_FLAGS
;
361 typedef struct _SEGMENT
363 struct _CONTROL_AREA
*ControlArea
;
364 ULONG TotalNumberOfPtes
;
365 ULONG NonExtendedPtes
;
367 ULONGLONG SizeOfSegment
;
368 MMPTE SegmentPteTemplate
;
369 ULONG NumberOfCommittedPages
;
370 PMMEXTEND_INFO ExtendInfo
;
371 SEGMENT_FLAGS SegmentFlags
;
375 SIZE_T ImageCommitment
;
376 PEPROCESS CreatingProcess
;
380 PSECTION_IMAGE_INFORMATION ImageInformation
;
385 } SEGMENT
, *PSEGMENT
;
388 // Event Counter Structure
390 typedef struct _EVENT_COUNTER
392 SLIST_ENTRY ListEntry
;
395 } EVENT_COUNTER
, *PEVENT_COUNTER
;
400 typedef struct _MMSECTION_FLAGS
402 ULONG BeingDeleted
:1;
403 ULONG BeingCreated
:1;
405 ULONG NoModifiedWriting
:1;
412 ULONG PhysicalMemory
:1;
418 ULONG UserReference
:1;
419 ULONG GlobalMemory
:1;
420 ULONG DeleteOnClose
:1;
421 ULONG FilePointerNull
:1;
422 ULONG DebugSymbolsLoaded
:1;
423 ULONG SetMappedFileIoComplete
:1;
424 ULONG CollidedFlush
:1;
427 ULONG ImageMappedInSystemSpace
:1;
428 ULONG UserWritable
:1;
430 ULONG GlobalOnlyPerSession
:1;
432 ULONG WriteCombined
:1;
434 } MMSECTION_FLAGS
, *PMMSECTION_FLAGS
;
436 typedef struct _MMSUBSECTION_FLAGS
440 ULONG SubsectionStatic
:1;
441 ULONG GlobalMemory
:1;
444 ULONG StartingSector4132
:10;
445 ULONG SectorEndOffset
:12;
446 } MMSUBSECTION_FLAGS
, *PMMSUBSECTION_FLAGS
;
448 typedef struct _MMSUBSECTION_FLAGS2
450 ULONG SubsectionAccessed
:1;
451 ULONG SubsectionConverted
:1;
453 } MMSUBSECTION_FLAGS2
;
456 // Control Area Structures
458 typedef struct _CONTROL_AREA
461 LIST_ENTRY DereferenceList
;
462 ULONG NumberOfSectionReferences
;
463 ULONG NumberOfPfnReferences
;
464 ULONG NumberOfMappedViews
;
465 ULONG NumberOfSystemCacheViews
;
466 ULONG NumberOfUserReferences
;
470 MMSECTION_FLAGS Flags
;
472 PFILE_OBJECT FilePointer
;
473 PEVENT_COUNTER WaitingForDeletion
;
474 USHORT ModifiedWriteCount
;
475 USHORT FlushInProgressCount
;
476 ULONG WritableUserReferences
;
478 } CONTROL_AREA
, *PCONTROL_AREA
;
480 typedef struct _LARGE_CONTROL_AREA
483 LIST_ENTRY DereferenceList
;
484 ULONG NumberOfSectionReferences
;
485 ULONG NumberOfPfnReferences
;
486 ULONG NumberOfMappedViews
;
487 ULONG NumberOfSystemCacheViews
;
488 ULONG NumberOfUserReferences
;
492 MMSECTION_FLAGS Flags
;
494 PFILE_OBJECT FilePointer
;
495 PEVENT_COUNTER WaitingForDeletion
;
496 USHORT ModifiedWriteCount
;
497 USHORT FlushInProgressCount
;
498 ULONG WritableUserReferences
;
501 LIST_ENTRY UserGlobalList
;
503 } LARGE_CONTROL_AREA
, *PLARGE_CONTROL_AREA
;
506 // Subsection and Mapped Subsection
508 typedef struct _SUBSECTION
510 PCONTROL_AREA ControlArea
;
514 MMSUBSECTION_FLAGS SubsectionFlags
;
516 ULONG StartingSector
;
517 ULONG NumberOfFullSectors
;
518 PMMPTE SubsectionBase
;
520 ULONG PtesInSubsection
;
521 struct _SUBSECTION
*NextSubsection
;
522 } SUBSECTION
, *PSUBSECTION
;
524 typedef struct _MSUBSECTION
526 PCONTROL_AREA ControlArea
;
530 MMSUBSECTION_FLAGS SubsectionFlags
;
532 ULONG StartingSector
;
533 ULONG NumberOfFullSectors
;
534 PMMPTE SubsectionBase
;
536 ULONG PtesInSubsection
;
537 struct _SUBSECTION
*NextSubsection
;
538 LIST_ENTRY DereferenceList
;
539 ULONG_PTR NumberOfMappedViews
;
543 MMSUBSECTION_FLAGS2 SubsectionFlags2
;
545 } MSUBSECTION
, *PMSUBSECTION
;
550 typedef struct _SEGMENT_OBJECT
553 ULONG TotalNumberOfPtes
;
554 LARGE_INTEGER SizeOfSegment
;
555 ULONG NonExtendedPtes
;
556 ULONG ImageCommitment
;
557 PCONTROL_AREA ControlArea
;
558 PSUBSECTION Subsection
;
559 PLARGE_CONTROL_AREA LargeControlArea
;
560 PMMSECTION_FLAGS MmSectionFlags
;
561 PMMSUBSECTION_FLAGS MmSubSectionFlags
;
562 } SEGMENT_OBJECT
, *PSEGMENT_OBJECT
;
567 typedef struct _SECTION_OBJECT
573 PSEGMENT_OBJECT Segment
;
574 } SECTION_OBJECT
, *PSECTION_OBJECT
;
577 // Generic Address Range Structure
579 typedef struct _ADDRESS_RANGE
586 } ADDRESS_RANGE
, *PADDRESS_RANGE
;
589 // Node in Memory Manager's AVL Table
591 typedef struct _MMADDRESS_NODE
596 struct _MMADDRESS_NODE
*Parent
;
598 struct _MMADDRESS_NODE
*LeftChild
;
599 struct _MMADDRESS_NODE
*RightChild
;
602 } MMADDRESS_NODE
, *PMMADDRESS_NODE
;
605 // Memory Manager AVL Table for VADs and other descriptors
607 typedef struct _MM_AVL_TABLE
609 MMADDRESS_NODE BalancedRoot
;
612 ULONG NumberGenericTableElements
:24;
615 } MM_AVL_TABLE
, *PMM_AVL_TABLE
;
618 // Actual Section Object
620 typedef struct _SECTION
622 MMADDRESS_NODE Address
;
624 LARGE_INTEGER SizeOfSection
;
628 MMSECTION_FLAGS Flags
;
630 ULONG InitialPageProtection
;
631 } SECTION
, *PSECTION
;
634 // Memory Manager Working Set Structures
636 typedef struct _MMWSLENTRY
640 ULONG LockedInMemory
:1;
645 ULONG VirtualPageNumber
:20;
646 } MMWSLENTRY
, *PMMWSLENTRY
;
648 typedef struct _MMWSLE
652 PVOID VirtualAddress
;
658 typedef struct _MMWSLE_HASH
662 } MMWSLE_HASH
, *PMMWSLE_HASH
;
664 typedef struct _MMWSL
671 ULONG LastInitializedWsle
;
672 ULONG NonDirectCount
;
673 PMMWSLE_HASH HashTable
;
675 ULONG NumberOfCommittedPageTables
;
676 PVOID HashTableStart
;
677 PVOID HighestPermittedHashAddress
;
678 ULONG NumberOfImageWaiters
;
680 USHORT UsedPageTableEntries
[768];
681 ULONG CommittedPageTables
[24];
685 // Flags for Memory Support Structure
687 typedef struct _MMSUPPORT_FLAGS
689 ULONG SessionSpace
:1;
690 ULONG BeingTrimmed
:1;
691 ULONG SessionLeader
:1;
693 ULONG MaximumWorkingSetHard
:1;
695 ULONG MinimumWorkingSetHard
:1;
697 ULONG MemoryPriority
:8;
698 ULONG GrowWsleHash
:1;
699 ULONG AcquiredUnsafe
:1;
701 } MMSUPPORT_FLAGS
, *PMMSUPPORT_FLAGS
;
704 // Per-Process Memory Manager Data
706 typedef struct _MMSUPPORT
708 #if (NTDDI_VERSION >= NTDDI_WS03)
709 LIST_ENTRY WorkingSetExpansionLinks
;
711 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
712 USHORT LastTrimpStamp
;
713 USHORT NextPageColor
;
715 LARGE_INTEGER LastTrimTime
;
717 MMSUPPORT_FLAGS Flags
;
718 ULONG PageFaultCount
;
719 ULONG PeakWorkingSetSize
;
720 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
723 ULONG GrowthSinceLastEstimate
;
725 ULONG MinimumWorkingSetSize
;
726 ULONG MaximumWorkingSetSize
;
727 PMMWSL VmWorkingSetList
;
728 #if (NTDDI_VERSION < NTDDI_WS03)
729 LIST_ENTRY WorkingSetExpansionLinks
;
732 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
734 ULONG WorkingSetPrivateSize
;
735 ULONG WorkingSetSizeOverhead
;
737 ULONG NextEstimationSlot
;
739 ULONG EstimatedAvailable
;
741 ULONG WorkingSetSize
;
742 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
745 EX_PUSH_LOCK WorkingSetMutex
;
746 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
749 } MMSUPPORT
, *PMMSUPPORT
;
752 // Memory Information Types
754 typedef struct _MEMORY_BASIC_INFORMATION
757 PVOID AllocationBase
;
758 ULONG AllocationProtect
;
763 } MEMORY_BASIC_INFORMATION
,*PMEMORY_BASIC_INFORMATION
;
766 // Driver Verifier Data
768 typedef struct _MM_DRIVER_VERIFIER_DATA
772 ULONG AcquireSpinLocks
;
773 ULONG SynchronizeExecutions
;
774 ULONG AllocationsAttempted
;
775 ULONG AllocationsSucceeded
;
776 ULONG AllocationsSucceededSpecialPool
;
777 ULONG AllocationsWithNoTag
;
780 ULONG AllocationsFailed
;
781 ULONG AllocationsFailedDeliberately
;
786 ULONG CurrentPagedPoolAllocations
;
787 ULONG CurrentNonPagedPoolAllocations
;
788 ULONG PeakPagedPoolAllocations
;
789 ULONG PeakNonPagedPoolAllocations
;
792 ULONG PeakPagedBytes
;
793 ULONG PeakNonPagedBytes
;
794 ULONG BurstAllocationsFailedDeliberately
;
797 } MM_DRIVER_VERIFIER_DATA
, *PMM_DRIVER_VERIFIER_DATA
;
800 // Internal Driver Verifier Table Data
802 typedef struct _DRIVER_SPECIFIED_VERIFIER_THUNKS
804 LIST_ENTRY ListEntry
;
805 struct _LDR_DATA_TABLE_ENTRY
*DataTableEntry
;
806 ULONG NumberOfThunks
;
807 } DRIVER_SPECIFIED_VERIFIER_THUNKS
, *PDRIVER_SPECIFIED_VERIFIER_THUNKS
;
810 // Default heap size values. For user mode, these values are copied to a new
811 // process's PEB by the kernel in MmCreatePeb. In kernel mode, RtlCreateHeap
812 // reads these variables directly.
814 // These variables should be considered "const"; they are written only once,
815 // during MmInitSystem.
817 extern SIZE_T MmHeapSegmentReserve
;
818 extern SIZE_T MmHeapSegmentCommit
;
819 extern SIZE_T MmHeapDeCommitTotalFreeThreshold
;
820 extern SIZE_T MmHeapDeCommitFreeBlockThreshold
;
823 // Section Object Type
825 extern POBJECT_TYPE MmSectionObjectType
;
827 #endif // !NTOS_MODE_USER