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
60 #ifndef NTOS_MODE_USER
63 // Virtual Memory Flags
65 #define MEM_WRITE_WATCH 0x200000
66 #define MEM_PHYSICAL 0x400000
67 #define MEM_ROTATE 0x800000
68 #define MEM_IMAGE SEC_IMAGE
69 #define MEM_DOS_LIM 0x40000000
72 // Section Flags for NtCreateSection
74 #define SEC_NO_CHANGE 0x400000
75 #define SEC_FILE 0x800000
76 #define SEC_IMAGE 0x1000000
77 #define SEC_PROTECTED_IMAGE 0x2000000
78 #define SEC_RESERVE 0x4000000
79 #define SEC_COMMIT 0x8000000
80 #define SEC_NOCACHE 0x10000000
81 #define SEC_WRITECOMBINE 0x40000000
82 #define SEC_LARGE_PAGES 0x80000000
84 #define SEC_BASED 0x200000
87 // Section Inherit Flags for NtCreateSection
89 typedef enum _SECTION_INHERIT
98 typedef enum _POOL_TYPE
102 NonPagedPoolMustSucceed
,
104 NonPagedPoolCacheAligned
,
105 PagedPoolCacheAligned
,
106 NonPagedPoolCacheAlignedMustS
,
108 NonPagedPoolSession
= 32,
110 NonPagedPoolMustSucceedSession
,
111 DontUseThisTypeSession
,
112 NonPagedPoolCacheAlignedSession
,
113 PagedPoolCacheAlignedSession
,
114 NonPagedPoolCacheAlignedMustSSession
119 // Memory Manager Page Lists
121 typedef enum _MMLISTS
126 ModifiedPageList
= 3,
127 ModifiedNoWritePageList
= 4,
134 // Per Processor Non Paged Lookaside List IDs
136 typedef enum _PP_NPAGED_LOOKASIDE_NUMBER
138 LookasideSmallIrpList
= 0,
139 LookasideLargeIrpList
= 1,
140 LookasideMdlList
= 2,
141 LookasideCreateInfoList
= 3,
142 LookasideNameBufferList
= 4,
143 LookasideTwilightList
= 5,
144 LookasideCompletionList
= 6,
145 LookasideMaximumList
= 7
146 } PP_NPAGED_LOOKASIDE_NUMBER
;
149 // Memory Information Classes for NtQueryVirtualMemory
151 typedef enum _MEMORY_INFORMATION_CLASS
153 MemoryBasicInformation
,
154 MemoryWorkingSetList
,
156 MemoryBasicVlmInformation
157 } MEMORY_INFORMATION_CLASS
;
160 // Section Information Clasess for NtQuerySection
162 typedef enum _SECTION_INFORMATION_CLASS
164 SectionBasicInformation
,
165 SectionImageInformation
,
166 } SECTION_INFORMATION_CLASS
;
168 #ifdef NTOS_MODE_USER
171 // Virtual Memory Counters
173 typedef struct _VM_COUNTERS
175 SIZE_T PeakVirtualSize
;
177 ULONG PageFaultCount
;
178 SIZE_T PeakWorkingSetSize
;
179 SIZE_T WorkingSetSize
;
180 SIZE_T QuotaPeakPagedPoolUsage
;
181 SIZE_T QuotaPagedPoolUsage
;
182 SIZE_T QuotaPeakNonPagedPoolUsage
;
183 SIZE_T QuotaNonPagedPoolUsage
;
184 SIZE_T PagefileUsage
;
185 SIZE_T PeakPagefileUsage
;
186 } VM_COUNTERS
, *PVM_COUNTERS
;
188 typedef struct _VM_COUNTERS_EX
190 SIZE_T PeakVirtualSize
;
192 ULONG PageFaultCount
;
193 SIZE_T PeakWorkingSetSize
;
194 SIZE_T WorkingSetSize
;
195 SIZE_T QuotaPeakPagedPoolUsage
;
196 SIZE_T QuotaPagedPoolUsage
;
197 SIZE_T QuotaPeakNonPagedPoolUsage
;
198 SIZE_T QuotaNonPagedPoolUsage
;
199 SIZE_T PagefileUsage
;
200 SIZE_T PeakPagefileUsage
;
202 } VM_COUNTERS_EX
, *PVM_COUNTERS_EX
;
206 // Sub-Information Types for PFN Identity
208 typedef struct _MEMORY_FRAME_INFORMATION
210 ULONGLONG UseDescription
:4;
211 ULONGLONG ListDescription
:3;
212 ULONGLONG Reserved0
:1;
214 ULONGLONG DontUse
:48;
215 ULONGLONG Priority
:3;
216 ULONGLONG Reserved
:4;
217 } MEMORY_FRAME_INFORMATION
, *PMEMORY_FRAME_INFORMATION
;
219 typedef struct _FILEOFFSET_INFORMATION
223 ULONGLONG Reserved
:7;
224 } FILEOFFSET_INFORMATION
, *PFILEOFFSET_INFORMATION
;
226 typedef struct _PAGEDIR_INFORMATION
229 ULONGLONG PageDirectoryBase
:48;
230 ULONGLONG Reserved
:7;
231 } PAGEDIR_INFORMATION
, *PPAGEDIR_INFORMATION
;
233 typedef struct _UNIQUE_PROCESS_INFORMATION
236 ULONGLONG UniqueProcessKey
:48;
237 ULONGLONG Reserved
:7;
238 } UNIQUE_PROCESS_INFORMATION
, *PUNIQUE_PROCESS_INFORMATION
;
241 // PFN Identity Data Structure
243 typedef struct _MMPFN_IDENTITY
247 MEMORY_FRAME_INFORMATION e1
;
248 FILEOFFSET_INFORMATION e2
;
249 PAGEDIR_INFORMATION e3
;
250 UNIQUE_PROCESS_INFORMATION e4
;
252 SIZE_T PageFrameIndex
;
261 PVOID UniqueFileObjectKey
;
262 PVOID ProtoPteAddress
;
263 PVOID VirtualAddress
;
265 } MMPFN_IDENTITY
, *PMMPFN_IDENTITY
;
268 // List of Working Sets
270 typedef struct _MEMORY_WORKING_SET_LIST
273 ULONG WorkingSetList
[1];
274 } MEMORY_WORKING_SET_LIST
, *PMEMORY_WORKING_SET_LIST
;
277 // Memory Information Structures for NtQueryVirtualMemory
281 UNICODE_STRING SectionFileName
;
282 WCHAR NameBuffer
[ANYSIZE_ARRAY
];
283 } MEMORY_SECTION_NAME
, *PMEMORY_SECTION_NAME
;
286 // Section Information Structures for NtQuerySection
288 typedef struct _SECTION_BASIC_INFORMATION
293 } SECTION_BASIC_INFORMATION
, *PSECTION_BASIC_INFORMATION
;
295 typedef struct _SECTION_IMAGE_INFORMATION
297 PVOID TransferAddress
;
299 ULONG MaximumStackSize
;
300 ULONG CommittedStackSize
;
302 USHORT SubSystemMinorVersion
;
303 USHORT SubSystemMajorVersion
;
305 USHORT ImageCharacteristics
;
306 USHORT DllCharacteristics
;
308 UCHAR ImageContainsCode
;
313 } SECTION_IMAGE_INFORMATION
, *PSECTION_IMAGE_INFORMATION
;
315 #ifndef NTOS_MODE_USER
320 typedef struct _MMPTE
327 MMPTE_PROTOTYPE Proto
;
329 MMPTE_TRANSITION Trans
;
330 MMPTE_SUBSECTION Subsect
;
336 // Section Extension Information
338 typedef struct _MMEXTEND_INFO
340 ULONGLONG CommittedSize
;
341 ULONG ReferenceCount
;
342 } MMEXTEND_INFO
, *PMMEXTEND_INFO
;
345 // Segment and Segment Flags
347 typedef struct _SEGMENT_FLAGS
349 ULONG TotalNumberOfPtes4132
:10;
350 ULONG ExtraSharedWowSubsections
:1;
353 } SEGMENT_FLAGS
, *PSEGMENT_FLAGS
;
355 typedef struct _SEGMENT
357 struct _CONTROL_AREA
*ControlArea
;
358 ULONG TotalNumberOfPtes
;
359 ULONG NonExtendedPtes
;
361 ULONGLONG SizeOfSegment
;
362 MMPTE SegmentPteTemplate
;
363 ULONG NumberOfCommittedPages
;
364 PMMEXTEND_INFO ExtendInfo
;
365 SEGMENT_FLAGS SegmentFlags
;
369 SIZE_T ImageCommitment
;
370 PEPROCESS CreatingProcess
;
374 PSECTION_IMAGE_INFORMATION ImageInformation
;
379 } SEGMENT
, *PSEGMENT
;
382 // Event Counter Structure
384 typedef struct _EVENT_COUNTER
386 SLIST_ENTRY ListEntry
;
389 } EVENT_COUNTER
, *PEVENT_COUNTER
;
394 typedef struct _MMSECTION_FLAGS
396 ULONG BeingDeleted
:1;
397 ULONG BeingCreated
:1;
399 ULONG NoModifiedWriting
:1;
406 ULONG PhysicalMemory
:1;
412 ULONG UserReference
:1;
413 ULONG GlobalMemory
:1;
414 ULONG DeleteOnClose
:1;
415 ULONG FilePointerNull
:1;
416 ULONG DebugSymbolsLoaded
:1;
417 ULONG SetMappedFileIoComplete
:1;
418 ULONG CollidedFlush
:1;
421 ULONG ImageMappedInSystemSpace
:1;
422 ULONG UserWritable
:1;
424 ULONG GlobalOnlyPerSession
:1;
426 ULONG WriteCombined
:1;
428 } MMSECTION_FLAGS
, *PMMSECTION_FLAGS
;
430 typedef struct _MMSUBSECTION_FLAGS
434 ULONG SubsectionStatic
:1;
435 ULONG GlobalMemory
:1;
438 ULONG StartingSector4132
:10;
439 ULONG SectorEndOffset
:12;
440 } MMSUBSECTION_FLAGS
, *PMMSUBSECTION_FLAGS
;
442 typedef struct _MMSUBSECTION_FLAGS2
444 ULONG SubsectionAccessed
:1;
445 ULONG SubsectionConverted
:1;
447 } MMSUBSECTION_FLAGS2
;
450 // Control Area Structures
452 typedef struct _CONTROL_AREA
455 LIST_ENTRY DereferenceList
;
456 ULONG NumberOfSectionReferences
;
457 ULONG NumberOfPfnReferences
;
458 ULONG NumberOfMappedViews
;
459 ULONG NumberOfSystemCacheViews
;
460 ULONG NumberOfUserReferences
;
464 MMSECTION_FLAGS Flags
;
466 PFILE_OBJECT FilePointer
;
467 PEVENT_COUNTER WaitingForDeletion
;
468 USHORT ModifiedWriteCount
;
469 USHORT FlushInProgressCount
;
470 ULONG WritableUserReferences
;
472 } CONTROL_AREA
, *PCONTROL_AREA
;
474 typedef struct _LARGE_CONTROL_AREA
477 LIST_ENTRY DereferenceList
;
478 ULONG NumberOfSectionReferences
;
479 ULONG NumberOfPfnReferences
;
480 ULONG NumberOfMappedViews
;
481 ULONG NumberOfSystemCacheViews
;
482 ULONG NumberOfUserReferences
;
486 MMSECTION_FLAGS Flags
;
488 PFILE_OBJECT FilePointer
;
489 PEVENT_COUNTER WaitingForDeletion
;
490 USHORT ModifiedWriteCount
;
491 USHORT FlushInProgressCount
;
492 ULONG WritableUserReferences
;
495 LIST_ENTRY UserGlobalList
;
497 } LARGE_CONTROL_AREA
, *PLARGE_CONTROL_AREA
;
500 // Subsection and Mapped Subsection
502 typedef struct _SUBSECTION
504 PCONTROL_AREA ControlArea
;
508 MMSUBSECTION_FLAGS SubsectionFlags
;
510 ULONG StartingSector
;
511 ULONG NumberOfFullSectors
;
512 PMMPTE SubsectionBase
;
514 ULONG PtesInSubsection
;
515 struct _SUBSECTION
*NextSubsection
;
516 } SUBSECTION
, *PSUBSECTION
;
518 typedef struct _MSUBSECTION
520 PCONTROL_AREA ControlArea
;
524 MMSUBSECTION_FLAGS SubsectionFlags
;
526 ULONG StartingSector
;
527 ULONG NumberOfFullSectors
;
528 PMMPTE SubsectionBase
;
530 ULONG PtesInSubsection
;
531 struct _SUBSECTION
*NextSubsection
;
532 LIST_ENTRY DereferenceList
;
533 ULONG_PTR NumberOfMappedViews
;
537 MMSUBSECTION_FLAGS2 SubsectionFlags2
;
539 } MSUBSECTION
, *PMSUBSECTION
;
544 typedef struct _SEGMENT_OBJECT
547 ULONG TotalNumberOfPtes
;
548 LARGE_INTEGER SizeOfSegment
;
549 ULONG NonExtendedPtes
;
550 ULONG ImageCommitment
;
551 PCONTROL_AREA ControlArea
;
552 PSUBSECTION Subsection
;
553 PLARGE_CONTROL_AREA LargeControlArea
;
554 PMMSECTION_FLAGS MmSectionFlags
;
555 PMMSUBSECTION_FLAGS MmSubSectionFlags
;
556 } SEGMENT_OBJECT
, *PSEGMENT_OBJECT
;
561 typedef struct _SECTION_OBJECT
567 PSEGMENT_OBJECT Segment
;
568 } SECTION_OBJECT
, *PSECTION_OBJECT
;
571 // Generic Address Range Structure
573 typedef struct _ADDRESS_RANGE
580 } ADDRESS_RANGE
, *PADDRESS_RANGE
;
583 // Node in Memory Manager's AVL Table
585 typedef struct _MMADDRESS_NODE
590 struct _MMADDRESS_NODE
*Parent
;
592 struct _MMADDRESS_NODE
*LeftChild
;
593 struct _MMADDRESS_NODE
*RightChild
;
596 } MMADDRESS_NODE
, *PMMADDRESS_NODE
;
599 // Memory Manager AVL Table for VADs and other descriptors
601 typedef struct _MM_AVL_TABLE
603 MMADDRESS_NODE BalancedRoot
;
606 ULONG NumberGenericTableElements
:24;
609 } MM_AVL_TABLE
, *PMM_AVL_TABLE
;
612 // Actual Section Object
614 typedef struct _SECTION
616 MMADDRESS_NODE Address
;
618 LARGE_INTEGER SizeOfSection
;
622 MMSECTION_FLAGS Flags
;
624 ULONG InitialPageProtection
;
625 } SECTION
, *PSECTION
;
628 // Memory Manager Working Set Structures
630 typedef struct _MMWSLENTRY
634 ULONG LockedInMemory
:1;
639 ULONG VirtualPageNumber
:20;
640 } MMWSLENTRY
, *PMMWSLENTRY
;
642 typedef struct _MMWSLE
646 PVOID VirtualAddress
;
652 typedef struct _MMWSLE_HASH
656 } MMWSLE_HASH
, *PMMWSLE_HASH
;
658 typedef struct _MMWSL
665 ULONG LastInitializedWsle
;
666 ULONG NonDirectCount
;
667 PMMWSLE_HASH HashTable
;
669 ULONG NumberOfCommittedPageTables
;
670 PVOID HashTableStart
;
671 PVOID HighestPermittedHashAddress
;
672 ULONG NumberOfImageWaiters
;
674 USHORT UsedPageTableEntries
[768];
675 ULONG CommittedPageTables
[24];
679 // Flags for Memory Support Structure
681 typedef struct _MMSUPPORT_FLAGS
683 ULONG SessionSpace
:1;
684 ULONG BeingTrimmed
:1;
685 ULONG SessionLeader
:1;
687 ULONG MaximumWorkingSetHard
:1;
689 ULONG MinimumWorkingSetHard
:1;
691 ULONG MemoryPriority
:8;
692 ULONG GrowWsleHash
:1;
693 ULONG AcquiredUnsafe
:1;
695 } MMSUPPORT_FLAGS
, *PMMSUPPORT_FLAGS
;
698 // Per-Process Memory Manager Data
700 typedef struct _MMSUPPORT
702 #if (NTDDI_VERSION >= NTDDI_WS03)
703 LIST_ENTRY WorkingSetExpansionLinks
;
705 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
706 USHORT LastTrimpStamp
;
707 USHORT NextPageColor
;
709 LARGE_INTEGER LastTrimTime
;
711 MMSUPPORT_FLAGS Flags
;
712 ULONG PageFaultCount
;
713 ULONG PeakWorkingSetSize
;
714 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
717 ULONG GrowthSinceLastEstimate
;
719 ULONG MinimumWorkingSetSize
;
720 ULONG MaximumWorkingSetSize
;
721 PMMWSL VmWorkingSetList
;
722 #if (NTDDI_VERSION < NTDDI_WS03)
723 LIST_ENTRY WorkingSetExpansionLinks
;
726 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
728 ULONG WorkingSetPrivateSize
;
729 ULONG WorkingSetSizeOverhead
;
731 ULONG NextEstimationSlot
;
733 ULONG EstimatedAvailable
;
735 ULONG WorkingSetSize
;
736 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
739 EX_PUSH_LOCK WorkingSetMutex
;
740 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
743 } MMSUPPORT
, *PMMSUPPORT
;
746 // Memory Information Types
748 typedef struct _MEMORY_BASIC_INFORMATION
751 PVOID AllocationBase
;
752 ULONG AllocationProtect
;
757 } MEMORY_BASIC_INFORMATION
,*PMEMORY_BASIC_INFORMATION
;
760 // Driver Verifier Data
762 typedef struct _MM_DRIVER_VERIFIER_DATA
766 ULONG AcquireSpinLocks
;
767 ULONG SynchronizeExecutions
;
768 ULONG AllocationsAttempted
;
769 ULONG AllocationsSucceeded
;
770 ULONG AllocationsSucceededSpecialPool
;
771 ULONG AllocationsWithNoTag
;
774 ULONG AllocationsFailed
;
775 ULONG AllocationsFailedDeliberately
;
780 ULONG CurrentPagedPoolAllocations
;
781 ULONG CurrentNonPagedPoolAllocations
;
782 ULONG PeakPagedPoolAllocations
;
783 ULONG PeakNonPagedPoolAllocations
;
786 ULONG PeakPagedBytes
;
787 ULONG PeakNonPagedBytes
;
788 ULONG BurstAllocationsFailedDeliberately
;
791 } MM_DRIVER_VERIFIER_DATA
, *PMM_DRIVER_VERIFIER_DATA
;
794 // Internal Driver Verifier Table Data
796 typedef struct _DRIVER_SPECIFIED_VERIFIER_THUNKS
798 LIST_ENTRY ListEntry
;
799 struct _LDR_DATA_TABLE_ENTRY
*DataTableEntry
;
800 ULONG NumberOfThunks
;
801 } DRIVER_SPECIFIED_VERIFIER_THUNKS
, *PDRIVER_SPECIFIED_VERIFIER_THUNKS
;
804 // Default heap size values. For user mode, these values are copied to a new
805 // process's PEB by the kernel in MmCreatePeb. In kernel mode, RtlCreateHeap
806 // reads these variables directly.
808 // These variables should be considered "const"; they are written only once,
809 // during MmInitSystem.
811 extern SIZE_T MmHeapSegmentReserve
;
812 extern SIZE_T MmHeapSegmentCommit
;
813 extern SIZE_T MmHeapDeCommitTotalFreeThreshold
;
814 extern SIZE_T MmHeapDeCommitFreeBlockThreshold
;
817 // Section Object Type
819 extern POBJECT_TYPE MmSectionObjectType
;
821 #endif // !NTOS_MODE_USER