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
334 MMPTE_HARDWARE_LARGEPAGE HardLarge
;
336 MMPTE_PROTOTYPE Proto
;
338 MMPTE_TRANSITION Trans
;
339 MMPTE_SUBSECTION Subsect
;
341 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
342 volatile ULONG_PTR VolatileLong
;
343 MMPTE_TIMESTAMP TimeStamp
;
349 // Section Extension Information
351 typedef struct _MMEXTEND_INFO
353 ULONGLONG CommittedSize
;
354 ULONG ReferenceCount
;
355 } MMEXTEND_INFO
, *PMMEXTEND_INFO
;
358 // Segment and Segment Flags
360 typedef struct _SEGMENT_FLAGS
362 ULONG TotalNumberOfPtes4132
:10;
363 ULONG ExtraSharedWowSubsections
:1;
366 } SEGMENT_FLAGS
, *PSEGMENT_FLAGS
;
368 typedef struct _SEGMENT
370 struct _CONTROL_AREA
*ControlArea
;
371 ULONG TotalNumberOfPtes
;
372 ULONG NonExtendedPtes
;
374 ULONGLONG SizeOfSegment
;
375 MMPTE SegmentPteTemplate
;
376 ULONG NumberOfCommittedPages
;
377 PMMEXTEND_INFO ExtendInfo
;
378 SEGMENT_FLAGS SegmentFlags
;
382 SIZE_T ImageCommitment
;
383 PEPROCESS CreatingProcess
;
387 PSECTION_IMAGE_INFORMATION ImageInformation
;
392 } SEGMENT
, *PSEGMENT
;
395 // Event Counter Structure
397 typedef struct _EVENT_COUNTER
399 SLIST_ENTRY ListEntry
;
402 } EVENT_COUNTER
, *PEVENT_COUNTER
;
407 typedef struct _MMSECTION_FLAGS
409 ULONG BeingDeleted
:1;
410 ULONG BeingCreated
:1;
412 ULONG NoModifiedWriting
:1;
419 ULONG PhysicalMemory
:1;
425 ULONG UserReference
:1;
426 ULONG GlobalMemory
:1;
427 ULONG DeleteOnClose
:1;
428 ULONG FilePointerNull
:1;
429 ULONG DebugSymbolsLoaded
:1;
430 ULONG SetMappedFileIoComplete
:1;
431 ULONG CollidedFlush
:1;
434 ULONG ImageMappedInSystemSpace
:1;
435 ULONG UserWritable
:1;
437 ULONG GlobalOnlyPerSession
:1;
439 ULONG WriteCombined
:1;
441 } MMSECTION_FLAGS
, *PMMSECTION_FLAGS
;
443 typedef struct _MMSUBSECTION_FLAGS
447 ULONG SubsectionStatic
:1;
448 ULONG GlobalMemory
:1;
451 ULONG StartingSector4132
:10;
452 ULONG SectorEndOffset
:12;
453 } MMSUBSECTION_FLAGS
, *PMMSUBSECTION_FLAGS
;
455 typedef struct _MMSUBSECTION_FLAGS2
457 ULONG SubsectionAccessed
:1;
458 ULONG SubsectionConverted
:1;
460 } MMSUBSECTION_FLAGS2
;
463 // Control Area Structures
465 typedef struct _CONTROL_AREA
468 LIST_ENTRY DereferenceList
;
469 ULONG NumberOfSectionReferences
;
470 ULONG NumberOfPfnReferences
;
471 ULONG NumberOfMappedViews
;
472 ULONG NumberOfSystemCacheViews
;
473 ULONG NumberOfUserReferences
;
477 MMSECTION_FLAGS Flags
;
479 PFILE_OBJECT FilePointer
;
480 PEVENT_COUNTER WaitingForDeletion
;
481 USHORT ModifiedWriteCount
;
482 USHORT FlushInProgressCount
;
483 ULONG WritableUserReferences
;
485 } CONTROL_AREA
, *PCONTROL_AREA
;
487 typedef struct _LARGE_CONTROL_AREA
490 LIST_ENTRY DereferenceList
;
491 ULONG NumberOfSectionReferences
;
492 ULONG NumberOfPfnReferences
;
493 ULONG NumberOfMappedViews
;
494 ULONG NumberOfSystemCacheViews
;
495 ULONG NumberOfUserReferences
;
499 MMSECTION_FLAGS Flags
;
501 PFILE_OBJECT FilePointer
;
502 PEVENT_COUNTER WaitingForDeletion
;
503 USHORT ModifiedWriteCount
;
504 USHORT FlushInProgressCount
;
505 ULONG WritableUserReferences
;
508 LIST_ENTRY UserGlobalList
;
510 } LARGE_CONTROL_AREA
, *PLARGE_CONTROL_AREA
;
513 // Subsection and Mapped Subsection
515 typedef struct _SUBSECTION
517 PCONTROL_AREA ControlArea
;
521 MMSUBSECTION_FLAGS SubsectionFlags
;
523 ULONG StartingSector
;
524 ULONG NumberOfFullSectors
;
525 PMMPTE SubsectionBase
;
527 ULONG PtesInSubsection
;
528 struct _SUBSECTION
*NextSubsection
;
529 } SUBSECTION
, *PSUBSECTION
;
531 typedef struct _MSUBSECTION
533 PCONTROL_AREA ControlArea
;
537 MMSUBSECTION_FLAGS SubsectionFlags
;
539 ULONG StartingSector
;
540 ULONG NumberOfFullSectors
;
541 PMMPTE SubsectionBase
;
543 ULONG PtesInSubsection
;
544 struct _SUBSECTION
*NextSubsection
;
545 LIST_ENTRY DereferenceList
;
546 ULONG_PTR NumberOfMappedViews
;
550 MMSUBSECTION_FLAGS2 SubsectionFlags2
;
552 } MSUBSECTION
, *PMSUBSECTION
;
557 typedef struct _SEGMENT_OBJECT
560 ULONG TotalNumberOfPtes
;
561 LARGE_INTEGER SizeOfSegment
;
562 ULONG NonExtendedPtes
;
563 ULONG ImageCommitment
;
564 PCONTROL_AREA ControlArea
;
565 PSUBSECTION Subsection
;
566 PLARGE_CONTROL_AREA LargeControlArea
;
567 PMMSECTION_FLAGS MmSectionFlags
;
568 PMMSUBSECTION_FLAGS MmSubSectionFlags
;
569 } SEGMENT_OBJECT
, *PSEGMENT_OBJECT
;
574 typedef struct _SECTION_OBJECT
580 PSEGMENT_OBJECT Segment
;
581 } SECTION_OBJECT
, *PSECTION_OBJECT
;
584 // Generic Address Range Structure
586 typedef struct _ADDRESS_RANGE
593 } ADDRESS_RANGE
, *PADDRESS_RANGE
;
596 // Node in Memory Manager's AVL Table
598 typedef struct _MMADDRESS_NODE
603 struct _MMADDRESS_NODE
*Parent
;
605 struct _MMADDRESS_NODE
*LeftChild
;
606 struct _MMADDRESS_NODE
*RightChild
;
609 } MMADDRESS_NODE
, *PMMADDRESS_NODE
;
612 // Memory Manager AVL Table for VADs and other descriptors
614 typedef struct _MM_AVL_TABLE
616 MMADDRESS_NODE BalancedRoot
;
619 ULONG NumberGenericTableElements
:24;
622 } MM_AVL_TABLE
, *PMM_AVL_TABLE
;
625 // Actual Section Object
627 typedef struct _SECTION
629 MMADDRESS_NODE Address
;
631 LARGE_INTEGER SizeOfSection
;
635 MMSECTION_FLAGS Flags
;
637 ULONG InitialPageProtection
;
638 } SECTION
, *PSECTION
;
641 // Memory Manager Working Set Structures
643 typedef struct _MMWSLENTRY
647 ULONG LockedInMemory
:1;
652 ULONG VirtualPageNumber
:20;
653 } MMWSLENTRY
, *PMMWSLENTRY
;
655 typedef struct _MMWSLE
659 PVOID VirtualAddress
;
665 typedef struct _MMWSLE_HASH
669 } MMWSLE_HASH
, *PMMWSLE_HASH
;
671 typedef struct _MMWSL
678 ULONG LastInitializedWsle
;
679 ULONG NonDirectCount
;
680 PMMWSLE_HASH HashTable
;
682 ULONG NumberOfCommittedPageTables
;
683 PVOID HashTableStart
;
684 PVOID HighestPermittedHashAddress
;
685 ULONG NumberOfImageWaiters
;
687 USHORT UsedPageTableEntries
[768];
688 ULONG CommittedPageTables
[24];
692 // Flags for Memory Support Structure
694 typedef struct _MMSUPPORT_FLAGS
696 ULONG SessionSpace
:1;
697 ULONG BeingTrimmed
:1;
698 ULONG SessionLeader
:1;
700 ULONG MaximumWorkingSetHard
:1;
702 ULONG MinimumWorkingSetHard
:1;
704 ULONG MemoryPriority
:8;
705 ULONG GrowWsleHash
:1;
706 ULONG AcquiredUnsafe
:1;
708 } MMSUPPORT_FLAGS
, *PMMSUPPORT_FLAGS
;
711 // Per-Process Memory Manager Data
713 typedef struct _MMSUPPORT
715 #if (NTDDI_VERSION >= NTDDI_WS03)
716 LIST_ENTRY WorkingSetExpansionLinks
;
718 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
719 USHORT LastTrimpStamp
;
720 USHORT NextPageColor
;
722 LARGE_INTEGER LastTrimTime
;
724 MMSUPPORT_FLAGS Flags
;
725 ULONG PageFaultCount
;
726 ULONG PeakWorkingSetSize
;
727 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
730 ULONG GrowthSinceLastEstimate
;
732 ULONG MinimumWorkingSetSize
;
733 ULONG MaximumWorkingSetSize
;
734 PMMWSL VmWorkingSetList
;
735 #if (NTDDI_VERSION < NTDDI_WS03)
736 LIST_ENTRY WorkingSetExpansionLinks
;
739 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
741 ULONG WorkingSetPrivateSize
;
742 ULONG WorkingSetSizeOverhead
;
744 ULONG NextEstimationSlot
;
746 ULONG EstimatedAvailable
;
748 ULONG WorkingSetSize
;
749 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
752 EX_PUSH_LOCK WorkingSetMutex
;
753 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
756 } MMSUPPORT
, *PMMSUPPORT
;
759 // Memory Information Types
761 typedef struct _MEMORY_BASIC_INFORMATION
764 PVOID AllocationBase
;
765 ULONG AllocationProtect
;
770 } MEMORY_BASIC_INFORMATION
,*PMEMORY_BASIC_INFORMATION
;
773 // Driver Verifier Data
775 typedef struct _MM_DRIVER_VERIFIER_DATA
779 ULONG AcquireSpinLocks
;
780 ULONG SynchronizeExecutions
;
781 ULONG AllocationsAttempted
;
782 ULONG AllocationsSucceeded
;
783 ULONG AllocationsSucceededSpecialPool
;
784 ULONG AllocationsWithNoTag
;
787 ULONG AllocationsFailed
;
788 ULONG AllocationsFailedDeliberately
;
793 ULONG CurrentPagedPoolAllocations
;
794 ULONG CurrentNonPagedPoolAllocations
;
795 ULONG PeakPagedPoolAllocations
;
796 ULONG PeakNonPagedPoolAllocations
;
799 ULONG PeakPagedBytes
;
800 ULONG PeakNonPagedBytes
;
801 ULONG BurstAllocationsFailedDeliberately
;
804 } MM_DRIVER_VERIFIER_DATA
, *PMM_DRIVER_VERIFIER_DATA
;
807 // Internal Driver Verifier Table Data
809 typedef struct _DRIVER_SPECIFIED_VERIFIER_THUNKS
811 LIST_ENTRY ListEntry
;
812 struct _LDR_DATA_TABLE_ENTRY
*DataTableEntry
;
813 ULONG NumberOfThunks
;
814 } DRIVER_SPECIFIED_VERIFIER_THUNKS
, *PDRIVER_SPECIFIED_VERIFIER_THUNKS
;
817 // Default heap size values. For user mode, these values are copied to a new
818 // process's PEB by the kernel in MmCreatePeb. In kernel mode, RtlCreateHeap
819 // reads these variables directly.
821 // These variables should be considered "const"; they are written only once,
822 // during MmInitSystem.
824 extern SIZE_T MmHeapSegmentReserve
;
825 extern SIZE_T MmHeapSegmentCommit
;
826 extern SIZE_T MmHeapDeCommitTotalFreeThreshold
;
827 extern SIZE_T MmHeapDeCommitFreeBlockThreshold
;
830 // Section Object Type
832 extern POBJECT_TYPE MmSectionObjectType
;
834 #endif // !NTOS_MODE_USER