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 SIZE_T MaximumStackSize
;
321 SIZE_T CommittedStackSize
;
327 USHORT SubSystemMinorVersion
;
328 USHORT SubSystemMajorVersion
;
330 ULONG SubSystemVersion
;
333 USHORT ImageCharacteristics
;
334 USHORT DllCharacteristics
;
336 UCHAR ImageContainsCode
;
341 } SECTION_IMAGE_INFORMATION
, *PSECTION_IMAGE_INFORMATION
;
343 #ifndef NTOS_MODE_USER
348 typedef struct _MMPTE
355 MMPTE_PROTOTYPE Proto
;
357 MMPTE_TRANSITION Trans
;
358 MMPTE_SUBSECTION Subsect
;
364 // Section Extension Information
366 typedef struct _MMEXTEND_INFO
368 ULONGLONG CommittedSize
;
369 ULONG ReferenceCount
;
370 } MMEXTEND_INFO
, *PMMEXTEND_INFO
;
373 // Segment and Segment Flags
375 typedef struct _SEGMENT_FLAGS
377 ULONG TotalNumberOfPtes4132
:10;
378 ULONG ExtraSharedWowSubsections
:1;
381 } SEGMENT_FLAGS
, *PSEGMENT_FLAGS
;
383 typedef struct _SEGMENT
385 struct _CONTROL_AREA
*ControlArea
;
386 ULONG TotalNumberOfPtes
;
387 ULONG NonExtendedPtes
;
389 ULONGLONG SizeOfSegment
;
390 MMPTE SegmentPteTemplate
;
391 ULONG NumberOfCommittedPages
;
392 PMMEXTEND_INFO ExtendInfo
;
393 SEGMENT_FLAGS SegmentFlags
;
397 SIZE_T ImageCommitment
;
398 PEPROCESS CreatingProcess
;
402 PSECTION_IMAGE_INFORMATION ImageInformation
;
407 } SEGMENT
, *PSEGMENT
;
410 // Event Counter Structure
412 typedef struct _EVENT_COUNTER
414 SLIST_ENTRY ListEntry
;
417 } EVENT_COUNTER
, *PEVENT_COUNTER
;
422 typedef struct _MMSECTION_FLAGS
424 ULONG BeingDeleted
:1;
425 ULONG BeingCreated
:1;
427 ULONG NoModifiedWriting
:1;
434 ULONG PhysicalMemory
:1;
440 ULONG UserReference
:1;
441 ULONG GlobalMemory
:1;
442 ULONG DeleteOnClose
:1;
443 ULONG FilePointerNull
:1;
444 ULONG DebugSymbolsLoaded
:1;
445 ULONG SetMappedFileIoComplete
:1;
446 ULONG CollidedFlush
:1;
449 ULONG ImageMappedInSystemSpace
:1;
450 ULONG UserWritable
:1;
452 ULONG GlobalOnlyPerSession
:1;
454 ULONG WriteCombined
:1;
456 } MMSECTION_FLAGS
, *PMMSECTION_FLAGS
;
458 typedef struct _MMSUBSECTION_FLAGS
462 ULONG SubsectionStatic
:1;
463 ULONG GlobalMemory
:1;
466 ULONG StartingSector4132
:10;
467 ULONG SectorEndOffset
:12;
468 } MMSUBSECTION_FLAGS
, *PMMSUBSECTION_FLAGS
;
470 typedef struct _MMSUBSECTION_FLAGS2
472 ULONG SubsectionAccessed
:1;
473 ULONG SubsectionConverted
:1;
475 } MMSUBSECTION_FLAGS2
;
478 // Control Area Structures
480 typedef struct _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
;
500 } CONTROL_AREA
, *PCONTROL_AREA
;
502 typedef struct _LARGE_CONTROL_AREA
505 LIST_ENTRY DereferenceList
;
506 ULONG NumberOfSectionReferences
;
507 ULONG NumberOfPfnReferences
;
508 ULONG NumberOfMappedViews
;
509 ULONG NumberOfSystemCacheViews
;
510 ULONG NumberOfUserReferences
;
514 MMSECTION_FLAGS Flags
;
516 PFILE_OBJECT FilePointer
;
517 PEVENT_COUNTER WaitingForDeletion
;
518 USHORT ModifiedWriteCount
;
519 USHORT FlushInProgressCount
;
520 ULONG WritableUserReferences
;
523 LIST_ENTRY UserGlobalList
;
525 } LARGE_CONTROL_AREA
, *PLARGE_CONTROL_AREA
;
528 // Subsection and Mapped Subsection
530 typedef struct _SUBSECTION
532 PCONTROL_AREA ControlArea
;
536 MMSUBSECTION_FLAGS SubsectionFlags
;
538 ULONG StartingSector
;
539 ULONG NumberOfFullSectors
;
540 PMMPTE SubsectionBase
;
542 ULONG PtesInSubsection
;
543 struct _SUBSECTION
*NextSubsection
;
544 } SUBSECTION
, *PSUBSECTION
;
546 typedef struct _MSUBSECTION
548 PCONTROL_AREA ControlArea
;
552 MMSUBSECTION_FLAGS SubsectionFlags
;
554 ULONG StartingSector
;
555 ULONG NumberOfFullSectors
;
556 PMMPTE SubsectionBase
;
558 ULONG PtesInSubsection
;
559 struct _SUBSECTION
*NextSubsection
;
560 LIST_ENTRY DereferenceList
;
561 ULONG_PTR NumberOfMappedViews
;
565 MMSUBSECTION_FLAGS2 SubsectionFlags2
;
567 } MSUBSECTION
, *PMSUBSECTION
;
572 typedef struct _SEGMENT_OBJECT
575 ULONG TotalNumberOfPtes
;
576 LARGE_INTEGER SizeOfSegment
;
577 ULONG NonExtendedPtes
;
578 ULONG ImageCommitment
;
579 PCONTROL_AREA ControlArea
;
580 PSUBSECTION Subsection
;
581 PLARGE_CONTROL_AREA LargeControlArea
;
582 PMMSECTION_FLAGS MmSectionFlags
;
583 PMMSUBSECTION_FLAGS MmSubSectionFlags
;
584 } SEGMENT_OBJECT
, *PSEGMENT_OBJECT
;
589 typedef struct _SECTION_OBJECT
595 PSEGMENT_OBJECT Segment
;
596 } SECTION_OBJECT
, *PSECTION_OBJECT
;
599 // Generic Address Range Structure
601 typedef struct _ADDRESS_RANGE
608 } ADDRESS_RANGE
, *PADDRESS_RANGE
;
611 // Node in Memory Manager's AVL Table
613 typedef struct _MMADDRESS_NODE
618 struct _MMADDRESS_NODE
*Parent
;
620 struct _MMADDRESS_NODE
*LeftChild
;
621 struct _MMADDRESS_NODE
*RightChild
;
622 ULONG_PTR StartingVpn
;
624 } MMADDRESS_NODE
, *PMMADDRESS_NODE
;
627 // Memory Manager AVL Table for VADs and other descriptors
629 typedef struct _MM_AVL_TABLE
631 MMADDRESS_NODE BalancedRoot
;
632 ULONG_PTR DepthOfTree
:5;
635 ULONG_PTR NumberGenericTableElements
:56;
637 ULONG_PTR NumberGenericTableElements
:24;
641 } MM_AVL_TABLE
, *PMM_AVL_TABLE
;
644 // Virtual Adress List used in VADs
646 typedef struct _MMADDRESS_LIST
650 } MMADDRESS_LIST
, *PMMADDRESS_LIST
;
653 // Flags used in the VAD
655 typedef struct _MMVAD_FLAGS
658 ULONG_PTR CommitCharge
:51;
660 ULONG_PTR CommitCharge
:19;
662 ULONG_PTR NoChange
:1;
664 ULONG_PTR MemCommit
:1;
665 ULONG_PTR Protection
:5;
667 ULONG_PTR PrivateMemory
:1;
668 } MMVAD_FLAGS
, *PMMVAD_FLAGS
;
671 // Extended flags used in the VAD
673 typedef struct _MMVAD_FLAGS2
678 ULONG MultipleSecured
:1;
681 ULONG ExtendableFile
:1;
684 } MMVAD_FLAGS2
, *PMMVAD_FLAGS2
;
687 // Virtual Address Descriptor (VAD) Structure
689 typedef struct _MMVAD
694 struct _MMVAD
*Parent
;
696 struct _MMVAD
*LeftChild
;
697 struct _MMVAD
*RightChild
;
698 ULONG_PTR StartingVpn
;
703 MMVAD_FLAGS VadFlags
;
705 PCONTROL_AREA ControlArea
;
706 PMMPTE FirstPrototypePte
;
707 PMMPTE LastContiguousPte
;
711 MMVAD_FLAGS2 VadFlags2
;
716 // Long VAD used in section and private allocations
718 typedef struct _MMVAD_LONG
727 ULONG_PTR StartingVpn
;
732 MMVAD_FLAGS VadFlags
;
734 PCONTROL_AREA ControlArea
;
735 PMMPTE FirstPrototypePte
;
736 PMMPTE LastContiguousPte
;
740 MMVAD_FLAGS2 VadFlags2
;
745 MMADDRESS_LIST Secured
;
750 PMMEXTEND_INFO ExtendedInfo
;
752 } MMVAD_LONG
, *PMMVAD_LONG
;
755 // Short VAD used in virtual memory allocations
757 typedef struct _MMVAD_SHORT
766 ULONG_PTR StartingVpn
;
771 MMVAD_FLAGS VadFlags
;
773 } MMVAD_SHORT
, *PMMVAD_SHORT
;
776 // Actual Section Object
778 typedef struct _SECTION
780 MMADDRESS_NODE Address
;
782 LARGE_INTEGER SizeOfSection
;
786 MMSECTION_FLAGS Flags
;
788 ULONG InitialPageProtection
;
789 } SECTION
, *PSECTION
;
792 // Memory Manager Working Set Structures
794 typedef struct _MMWSLENTRY
798 ULONG LockedInMemory
:1;
803 ULONG VirtualPageNumber
:20;
804 } MMWSLENTRY
, *PMMWSLENTRY
;
806 typedef struct _MMWSLE
810 PVOID VirtualAddress
;
816 typedef struct _MMWSLE_HASH
820 } MMWSLE_HASH
, *PMMWSLE_HASH
;
822 typedef struct _MMWSL
829 ULONG LastInitializedWsle
;
830 ULONG NonDirectCount
;
831 PMMWSLE_HASH HashTable
;
833 ULONG NumberOfCommittedPageTables
;
834 PVOID HashTableStart
;
835 PVOID HighestPermittedHashAddress
;
836 ULONG NumberOfImageWaiters
;
838 USHORT UsedPageTableEntries
[768];
839 ULONG CommittedPageTables
[24];
843 // Flags for Memory Support Structure
845 typedef struct _MMSUPPORT_FLAGS
847 ULONG SessionSpace
:1;
848 ULONG BeingTrimmed
:1;
849 ULONG SessionLeader
:1;
851 ULONG MaximumWorkingSetHard
:1;
853 ULONG MinimumWorkingSetHard
:1;
855 ULONG MemoryPriority
:8;
856 ULONG GrowWsleHash
:1;
857 ULONG AcquiredUnsafe
:1;
859 } MMSUPPORT_FLAGS
, *PMMSUPPORT_FLAGS
;
862 // Per-Process Memory Manager Data
864 typedef struct _MMSUPPORT
866 #if (NTDDI_VERSION >= NTDDI_WS03)
867 LIST_ENTRY WorkingSetExpansionLinks
;
869 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
870 USHORT LastTrimpStamp
;
871 USHORT NextPageColor
;
873 LARGE_INTEGER LastTrimTime
;
875 MMSUPPORT_FLAGS Flags
;
876 ULONG PageFaultCount
;
877 ULONG PeakWorkingSetSize
;
878 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
881 ULONG GrowthSinceLastEstimate
;
883 ULONG MinimumWorkingSetSize
;
884 ULONG MaximumWorkingSetSize
;
885 PMMWSL VmWorkingSetList
;
886 #if (NTDDI_VERSION < NTDDI_WS03)
887 LIST_ENTRY WorkingSetExpansionLinks
;
890 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
892 ULONG WorkingSetPrivateSize
;
893 ULONG WorkingSetSizeOverhead
;
895 ULONG NextEstimationSlot
;
897 ULONG EstimatedAvailable
;
899 ULONG WorkingSetSize
;
900 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
903 EX_PUSH_LOCK WorkingSetMutex
;
904 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
907 } MMSUPPORT
, *PMMSUPPORT
;
910 // Memory Information Types
912 typedef struct _MEMORY_BASIC_INFORMATION
915 PVOID AllocationBase
;
916 ULONG AllocationProtect
;
921 } MEMORY_BASIC_INFORMATION
,*PMEMORY_BASIC_INFORMATION
;
924 // Driver Verifier Data
926 typedef struct _MM_DRIVER_VERIFIER_DATA
930 ULONG AcquireSpinLocks
;
931 ULONG SynchronizeExecutions
;
932 ULONG AllocationsAttempted
;
933 ULONG AllocationsSucceeded
;
934 ULONG AllocationsSucceededSpecialPool
;
935 ULONG AllocationsWithNoTag
;
938 ULONG AllocationsFailed
;
939 ULONG AllocationsFailedDeliberately
;
944 ULONG CurrentPagedPoolAllocations
;
945 ULONG CurrentNonPagedPoolAllocations
;
946 ULONG PeakPagedPoolAllocations
;
947 ULONG PeakNonPagedPoolAllocations
;
950 ULONG PeakPagedBytes
;
951 ULONG PeakNonPagedBytes
;
952 ULONG BurstAllocationsFailedDeliberately
;
955 } MM_DRIVER_VERIFIER_DATA
, *PMM_DRIVER_VERIFIER_DATA
;
958 // Internal Driver Verifier Table Data
960 typedef struct _DRIVER_SPECIFIED_VERIFIER_THUNKS
962 LIST_ENTRY ListEntry
;
963 struct _LDR_DATA_TABLE_ENTRY
*DataTableEntry
;
964 ULONG NumberOfThunks
;
965 } DRIVER_SPECIFIED_VERIFIER_THUNKS
, *PDRIVER_SPECIFIED_VERIFIER_THUNKS
;
968 // Default heap size values. For user mode, these values are copied to a new
969 // process's PEB by the kernel in MmCreatePeb. In kernel mode, RtlCreateHeap
970 // reads these variables directly.
972 // These variables should be considered "const"; they are written only once,
973 // during MmInitSystem.
975 extern SIZE_T MmHeapSegmentReserve
;
976 extern SIZE_T MmHeapSegmentCommit
;
977 extern SIZE_T MmHeapDeCommitTotalFreeThreshold
;
978 extern SIZE_T MmHeapDeCommitFreeBlockThreshold
;
981 // Section Object Type
983 extern POBJECT_TYPE MmSectionObjectType
;
985 #endif // !NTOS_MODE_USER