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
;
177 typedef enum _MI_VAD_TYPE
180 VadDevicePhysicalMemory
,
187 } MI_VAD_TYPE
, *PMI_VAD_TYPE
;
189 #ifdef NTOS_MODE_USER
192 // Virtual Memory Counters
194 typedef struct _VM_COUNTERS
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
;
207 } VM_COUNTERS
, *PVM_COUNTERS
;
209 typedef struct _VM_COUNTERS_EX
211 SIZE_T PeakVirtualSize
;
213 ULONG PageFaultCount
;
214 SIZE_T PeakWorkingSetSize
;
215 SIZE_T WorkingSetSize
;
216 SIZE_T QuotaPeakPagedPoolUsage
;
217 SIZE_T QuotaPagedPoolUsage
;
218 SIZE_T QuotaPeakNonPagedPoolUsage
;
219 SIZE_T QuotaNonPagedPoolUsage
;
220 SIZE_T PagefileUsage
;
221 SIZE_T PeakPagefileUsage
;
223 } VM_COUNTERS_EX
, *PVM_COUNTERS_EX
;
227 // Sub-Information Types for PFN Identity
229 typedef struct _MEMORY_FRAME_INFORMATION
231 ULONGLONG UseDescription
:4;
232 ULONGLONG ListDescription
:3;
233 ULONGLONG Reserved0
:1;
235 ULONGLONG DontUse
:48;
236 ULONGLONG Priority
:3;
237 ULONGLONG Reserved
:4;
238 } MEMORY_FRAME_INFORMATION
, *PMEMORY_FRAME_INFORMATION
;
240 typedef struct _FILEOFFSET_INFORMATION
244 ULONGLONG Reserved
:7;
245 } FILEOFFSET_INFORMATION
, *PFILEOFFSET_INFORMATION
;
247 typedef struct _PAGEDIR_INFORMATION
250 ULONGLONG PageDirectoryBase
:48;
251 ULONGLONG Reserved
:7;
252 } PAGEDIR_INFORMATION
, *PPAGEDIR_INFORMATION
;
254 typedef struct _UNIQUE_PROCESS_INFORMATION
257 ULONGLONG UniqueProcessKey
:48;
258 ULONGLONG Reserved
:7;
259 } UNIQUE_PROCESS_INFORMATION
, *PUNIQUE_PROCESS_INFORMATION
;
262 // PFN Identity Data Structure
264 typedef struct _MMPFN_IDENTITY
268 MEMORY_FRAME_INFORMATION e1
;
269 FILEOFFSET_INFORMATION e2
;
270 PAGEDIR_INFORMATION e3
;
271 UNIQUE_PROCESS_INFORMATION e4
;
273 SIZE_T PageFrameIndex
;
282 PVOID UniqueFileObjectKey
;
283 PVOID ProtoPteAddress
;
284 PVOID VirtualAddress
;
286 } MMPFN_IDENTITY
, *PMMPFN_IDENTITY
;
289 // List of Working Sets
291 typedef struct _MEMORY_WORKING_SET_LIST
294 ULONG WorkingSetList
[1];
295 } MEMORY_WORKING_SET_LIST
, *PMEMORY_WORKING_SET_LIST
;
298 // Memory Information Structures for NtQueryVirtualMemory
302 UNICODE_STRING SectionFileName
;
303 WCHAR NameBuffer
[ANYSIZE_ARRAY
];
304 } MEMORY_SECTION_NAME
, *PMEMORY_SECTION_NAME
;
307 // Section Information Structures for NtQuerySection
309 typedef struct _SECTION_BASIC_INFORMATION
314 } SECTION_BASIC_INFORMATION
, *PSECTION_BASIC_INFORMATION
;
316 typedef struct _SECTION_IMAGE_INFORMATION
318 PVOID TransferAddress
;
320 ULONG MaximumStackSize
;
321 ULONG CommittedStackSize
;
323 USHORT SubSystemMinorVersion
;
324 USHORT SubSystemMajorVersion
;
326 USHORT ImageCharacteristics
;
327 USHORT DllCharacteristics
;
329 UCHAR ImageContainsCode
;
334 } SECTION_IMAGE_INFORMATION
, *PSECTION_IMAGE_INFORMATION
;
336 #ifndef NTOS_MODE_USER
341 typedef struct _MMPTE
348 MMPTE_PROTOTYPE Proto
;
350 MMPTE_TRANSITION Trans
;
351 MMPTE_SUBSECTION Subsect
;
357 // Section Extension Information
359 typedef struct _MMEXTEND_INFO
361 ULONGLONG CommittedSize
;
362 ULONG ReferenceCount
;
363 } MMEXTEND_INFO
, *PMMEXTEND_INFO
;
366 // Segment and Segment Flags
368 typedef struct _SEGMENT_FLAGS
370 ULONG TotalNumberOfPtes4132
:10;
371 ULONG ExtraSharedWowSubsections
:1;
374 } SEGMENT_FLAGS
, *PSEGMENT_FLAGS
;
376 typedef struct _SEGMENT
378 struct _CONTROL_AREA
*ControlArea
;
379 ULONG TotalNumberOfPtes
;
380 ULONG NonExtendedPtes
;
382 ULONGLONG SizeOfSegment
;
383 MMPTE SegmentPteTemplate
;
384 ULONG NumberOfCommittedPages
;
385 PMMEXTEND_INFO ExtendInfo
;
386 SEGMENT_FLAGS SegmentFlags
;
390 SIZE_T ImageCommitment
;
391 PEPROCESS CreatingProcess
;
395 PSECTION_IMAGE_INFORMATION ImageInformation
;
400 } SEGMENT
, *PSEGMENT
;
403 // Event Counter Structure
405 typedef struct _EVENT_COUNTER
407 SLIST_ENTRY ListEntry
;
410 } EVENT_COUNTER
, *PEVENT_COUNTER
;
415 typedef struct _MMSECTION_FLAGS
417 ULONG BeingDeleted
:1;
418 ULONG BeingCreated
:1;
420 ULONG NoModifiedWriting
:1;
427 ULONG PhysicalMemory
:1;
433 ULONG UserReference
:1;
434 ULONG GlobalMemory
:1;
435 ULONG DeleteOnClose
:1;
436 ULONG FilePointerNull
:1;
437 ULONG DebugSymbolsLoaded
:1;
438 ULONG SetMappedFileIoComplete
:1;
439 ULONG CollidedFlush
:1;
442 ULONG ImageMappedInSystemSpace
:1;
443 ULONG UserWritable
:1;
445 ULONG GlobalOnlyPerSession
:1;
447 ULONG WriteCombined
:1;
449 } MMSECTION_FLAGS
, *PMMSECTION_FLAGS
;
451 typedef struct _MMSUBSECTION_FLAGS
455 ULONG SubsectionStatic
:1;
456 ULONG GlobalMemory
:1;
459 ULONG StartingSector4132
:10;
460 ULONG SectorEndOffset
:12;
461 } MMSUBSECTION_FLAGS
, *PMMSUBSECTION_FLAGS
;
463 typedef struct _MMSUBSECTION_FLAGS2
465 ULONG SubsectionAccessed
:1;
466 ULONG SubsectionConverted
:1;
468 } MMSUBSECTION_FLAGS2
;
471 // Control Area Structures
473 typedef struct _CONTROL_AREA
476 LIST_ENTRY DereferenceList
;
477 ULONG NumberOfSectionReferences
;
478 ULONG NumberOfPfnReferences
;
479 ULONG NumberOfMappedViews
;
480 ULONG NumberOfSystemCacheViews
;
481 ULONG NumberOfUserReferences
;
485 MMSECTION_FLAGS Flags
;
487 PFILE_OBJECT FilePointer
;
488 PEVENT_COUNTER WaitingForDeletion
;
489 USHORT ModifiedWriteCount
;
490 USHORT FlushInProgressCount
;
491 ULONG WritableUserReferences
;
493 } CONTROL_AREA
, *PCONTROL_AREA
;
495 typedef struct _LARGE_CONTROL_AREA
498 LIST_ENTRY DereferenceList
;
499 ULONG NumberOfSectionReferences
;
500 ULONG NumberOfPfnReferences
;
501 ULONG NumberOfMappedViews
;
502 ULONG NumberOfSystemCacheViews
;
503 ULONG NumberOfUserReferences
;
507 MMSECTION_FLAGS Flags
;
509 PFILE_OBJECT FilePointer
;
510 PEVENT_COUNTER WaitingForDeletion
;
511 USHORT ModifiedWriteCount
;
512 USHORT FlushInProgressCount
;
513 ULONG WritableUserReferences
;
516 LIST_ENTRY UserGlobalList
;
518 } LARGE_CONTROL_AREA
, *PLARGE_CONTROL_AREA
;
521 // Subsection and Mapped Subsection
523 typedef struct _SUBSECTION
525 PCONTROL_AREA ControlArea
;
529 MMSUBSECTION_FLAGS SubsectionFlags
;
531 ULONG StartingSector
;
532 ULONG NumberOfFullSectors
;
533 PMMPTE SubsectionBase
;
535 ULONG PtesInSubsection
;
536 struct _SUBSECTION
*NextSubsection
;
537 } SUBSECTION
, *PSUBSECTION
;
539 typedef struct _MSUBSECTION
541 PCONTROL_AREA ControlArea
;
545 MMSUBSECTION_FLAGS SubsectionFlags
;
547 ULONG StartingSector
;
548 ULONG NumberOfFullSectors
;
549 PMMPTE SubsectionBase
;
551 ULONG PtesInSubsection
;
552 struct _SUBSECTION
*NextSubsection
;
553 LIST_ENTRY DereferenceList
;
554 ULONG_PTR NumberOfMappedViews
;
558 MMSUBSECTION_FLAGS2 SubsectionFlags2
;
560 } MSUBSECTION
, *PMSUBSECTION
;
565 typedef struct _SEGMENT_OBJECT
568 ULONG TotalNumberOfPtes
;
569 LARGE_INTEGER SizeOfSegment
;
570 ULONG NonExtendedPtes
;
571 ULONG ImageCommitment
;
572 PCONTROL_AREA ControlArea
;
573 PSUBSECTION Subsection
;
574 PLARGE_CONTROL_AREA LargeControlArea
;
575 PMMSECTION_FLAGS MmSectionFlags
;
576 PMMSUBSECTION_FLAGS MmSubSectionFlags
;
577 } SEGMENT_OBJECT
, *PSEGMENT_OBJECT
;
582 typedef struct _SECTION_OBJECT
588 PSEGMENT_OBJECT Segment
;
589 } SECTION_OBJECT
, *PSECTION_OBJECT
;
592 // Generic Address Range Structure
594 typedef struct _ADDRESS_RANGE
601 } ADDRESS_RANGE
, *PADDRESS_RANGE
;
604 // Node in Memory Manager's AVL Table
606 typedef struct _MMADDRESS_NODE
611 struct _MMADDRESS_NODE
*Parent
;
613 struct _MMADDRESS_NODE
*LeftChild
;
614 struct _MMADDRESS_NODE
*RightChild
;
615 ULONG_PTR StartingVpn
;
617 } MMADDRESS_NODE
, *PMMADDRESS_NODE
;
620 // Memory Manager AVL Table for VADs and other descriptors
622 typedef struct _MM_AVL_TABLE
624 MMADDRESS_NODE BalancedRoot
;
625 ULONG_PTR DepthOfTree
:5;
628 ULONG_PTR NumberGenericTableElements
:56;
630 ULONG_PTR NumberGenericTableElements
:24;
634 } MM_AVL_TABLE
, *PMM_AVL_TABLE
;
637 // Virtual Adress List used in VADs
639 typedef struct _MMADDRESS_LIST
643 } MMADDRESS_LIST
, *PMMADDRESS_LIST
;
646 // Flags used in the VAD
648 typedef struct _MMVAD_FLAGS
650 ULONG CommitCharge
:19;
656 ULONG PrivateMemory
:1;
657 } MMVAD_FLAGS
, *PMMVAD_FLAGS
;
660 // Extended flags used in the VAD
662 typedef struct _MMVAD_FLAGS2
667 ULONG MultipleSecured
:1;
670 ULONG ExtendableFile
:1;
673 } MMVAD_FLAGS2
, *PMMVAD_FLAGS2
;
676 // Virtual Address Descriptor (VAD) Structure
678 typedef struct _MMVAD
683 struct _MMVAD
*Parent
;
685 struct _MMVAD
*LeftChild
;
686 struct _MMVAD
*RightChild
;
692 MMVAD_FLAGS VadFlags
;
694 PCONTROL_AREA ControlArea
;
695 PMMPTE FirstPrototypePte
;
696 PMMPTE LastContiguousPte
;
700 MMVAD_FLAGS2 VadFlags2
;
705 // Long VAD used in section and private allocations
707 typedef struct _MMVAD_LONG
721 MMVAD_FLAGS VadFlags
;
723 PCONTROL_AREA ControlArea
;
724 PMMPTE FirstPrototypePte
;
725 PMMPTE LastContiguousPte
;
729 MMVAD_FLAGS2 VadFlags2
;
734 MMADDRESS_LIST Secured
;
739 PMMEXTEND_INFO ExtendedInfo
;
741 } MMVAD_LONG
, *PMMVAD_LONG
;
744 // Short VAD used in virtual memory allocations
746 typedef struct _MMVAD_SHORT
760 MMVAD_FLAGS VadFlags
;
762 } MMVAD_SHORT
, *PMMVAD_SHORT
;
765 // Actual Section Object
767 typedef struct _SECTION
769 MMADDRESS_NODE Address
;
771 LARGE_INTEGER SizeOfSection
;
775 MMSECTION_FLAGS Flags
;
777 ULONG InitialPageProtection
;
778 } SECTION
, *PSECTION
;
781 // Memory Manager Working Set Structures
783 typedef struct _MMWSLENTRY
787 ULONG LockedInMemory
:1;
792 ULONG VirtualPageNumber
:20;
793 } MMWSLENTRY
, *PMMWSLENTRY
;
795 typedef struct _MMWSLE
799 PVOID VirtualAddress
;
805 typedef struct _MMWSLE_HASH
809 } MMWSLE_HASH
, *PMMWSLE_HASH
;
811 typedef struct _MMWSL
818 ULONG LastInitializedWsle
;
819 ULONG NonDirectCount
;
820 PMMWSLE_HASH HashTable
;
822 ULONG NumberOfCommittedPageTables
;
823 PVOID HashTableStart
;
824 PVOID HighestPermittedHashAddress
;
825 ULONG NumberOfImageWaiters
;
827 USHORT UsedPageTableEntries
[768];
828 ULONG CommittedPageTables
[24];
832 // Flags for Memory Support Structure
834 typedef struct _MMSUPPORT_FLAGS
836 ULONG SessionSpace
:1;
837 ULONG BeingTrimmed
:1;
838 ULONG SessionLeader
:1;
840 ULONG MaximumWorkingSetHard
:1;
842 ULONG MinimumWorkingSetHard
:1;
844 ULONG MemoryPriority
:8;
845 ULONG GrowWsleHash
:1;
846 ULONG AcquiredUnsafe
:1;
848 } MMSUPPORT_FLAGS
, *PMMSUPPORT_FLAGS
;
851 // Per-Process Memory Manager Data
853 typedef struct _MMSUPPORT
855 #if (NTDDI_VERSION >= NTDDI_WS03)
856 LIST_ENTRY WorkingSetExpansionLinks
;
858 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
859 USHORT LastTrimpStamp
;
860 USHORT NextPageColor
;
862 LARGE_INTEGER LastTrimTime
;
864 MMSUPPORT_FLAGS Flags
;
865 ULONG PageFaultCount
;
866 ULONG PeakWorkingSetSize
;
867 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
870 ULONG GrowthSinceLastEstimate
;
872 ULONG MinimumWorkingSetSize
;
873 ULONG MaximumWorkingSetSize
;
874 PMMWSL VmWorkingSetList
;
875 #if (NTDDI_VERSION < NTDDI_WS03)
876 LIST_ENTRY WorkingSetExpansionLinks
;
879 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
881 ULONG WorkingSetPrivateSize
;
882 ULONG WorkingSetSizeOverhead
;
884 ULONG NextEstimationSlot
;
886 ULONG EstimatedAvailable
;
888 ULONG WorkingSetSize
;
889 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
892 EX_PUSH_LOCK WorkingSetMutex
;
893 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
896 } MMSUPPORT
, *PMMSUPPORT
;
899 // Memory Information Types
901 typedef struct _MEMORY_BASIC_INFORMATION
904 PVOID AllocationBase
;
905 ULONG AllocationProtect
;
910 } MEMORY_BASIC_INFORMATION
,*PMEMORY_BASIC_INFORMATION
;
913 // Driver Verifier Data
915 typedef struct _MM_DRIVER_VERIFIER_DATA
919 ULONG AcquireSpinLocks
;
920 ULONG SynchronizeExecutions
;
921 ULONG AllocationsAttempted
;
922 ULONG AllocationsSucceeded
;
923 ULONG AllocationsSucceededSpecialPool
;
924 ULONG AllocationsWithNoTag
;
927 ULONG AllocationsFailed
;
928 ULONG AllocationsFailedDeliberately
;
933 ULONG CurrentPagedPoolAllocations
;
934 ULONG CurrentNonPagedPoolAllocations
;
935 ULONG PeakPagedPoolAllocations
;
936 ULONG PeakNonPagedPoolAllocations
;
939 ULONG PeakPagedBytes
;
940 ULONG PeakNonPagedBytes
;
941 ULONG BurstAllocationsFailedDeliberately
;
944 } MM_DRIVER_VERIFIER_DATA
, *PMM_DRIVER_VERIFIER_DATA
;
947 // Internal Driver Verifier Table Data
949 typedef struct _DRIVER_SPECIFIED_VERIFIER_THUNKS
951 LIST_ENTRY ListEntry
;
952 struct _LDR_DATA_TABLE_ENTRY
*DataTableEntry
;
953 ULONG NumberOfThunks
;
954 } DRIVER_SPECIFIED_VERIFIER_THUNKS
, *PDRIVER_SPECIFIED_VERIFIER_THUNKS
;
957 // Default heap size values. For user mode, these values are copied to a new
958 // process's PEB by the kernel in MmCreatePeb. In kernel mode, RtlCreateHeap
959 // reads these variables directly.
961 // These variables should be considered "const"; they are written only once,
962 // during MmInitSystem.
964 extern SIZE_T MmHeapSegmentReserve
;
965 extern SIZE_T MmHeapSegmentCommit
;
966 extern SIZE_T MmHeapDeCommitTotalFreeThreshold
;
967 extern SIZE_T MmHeapDeCommitFreeBlockThreshold
;
970 // Section Object Type
972 extern POBJECT_TYPE MmSectionObjectType
;
974 #endif // !NTOS_MODE_USER