Fix PAGE_ROUND_DOWN, PAGE_ROUND_UP, and a user of them (correctness and speedup)
[reactos.git] / reactos / include / ndk / mmtypes.h
1 /*++ NDK Version: 0098
2
3 Copyright (c) Alex Ionescu. All rights reserved.
4
5 Header Name:
6
7 mmtypes.h
8
9 Abstract:
10
11 Type definitions for the Memory Manager
12
13 Author:
14
15 Alex Ionescu (alexi@tinykrnl.org) - Updated - 27-Feb-2006
16
17 --*/
18
19 #ifndef _MMTYPES_H
20 #define _MMTYPES_H
21
22 //
23 // Dependencies
24 //
25 #include <umtypes.h>
26 #include <arch/mmtypes.h>
27 #include <extypes.h>
28
29 //
30 // Page-Rounding Macros
31 //
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)) )
36 #ifdef NTOS_MODE_USER
37 #define ROUND_TO_PAGES(Size) \
38 (((ULONG_PTR)(Size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
39 #endif
40 #define ROUND_TO_ALLOCATION_GRANULARITY(Size) \
41 (((ULONG_PTR)(Size) + MM_ALLOCATION_GRANULARITY - 1) \
42 & ~(MM_ALLOCATION_GRANULARITY - 1))
43
44 //
45 // Macro for generating pool tags
46 //
47 #define TAG(A, B, C, D) \
48 (ULONG)(((A)<<0) + ((B)<<8) + ((C)<<16) + ((D)<<24))
49
50 #ifndef NTOS_MODE_USER
51
52 //
53 // Virtual Memory Flags
54 //
55 #define MEM_WRITE_WATCH 0x200000
56 #define MEM_PHYSICAL 0x400000
57 #define MEM_ROTATE 0x800000
58 #define MEM_IMAGE SEC_IMAGE
59 #define MEM_DOS_LIM 0x40000000
60
61 //
62 // Section Flags for NtCreateSection
63 //
64 #define SEC_NO_CHANGE 0x400000
65 #define SEC_FILE 0x800000
66 #define SEC_IMAGE 0x1000000
67 #define SEC_PROTECTED_IMAGE 0x2000000
68 #define SEC_RESERVE 0x4000000
69 #define SEC_COMMIT 0x8000000
70 #define SEC_NOCACHE 0x10000000
71 #define SEC_WRITECOMBINE 0x40000000
72 #define SEC_LARGE_PAGES 0x80000000
73 #else
74 #define SEC_BASED 0x200000
75
76 //
77 // Section Inherit Flags for NtCreateSection
78 //
79 typedef enum _SECTION_INHERIT
80 {
81 ViewShare = 1,
82 ViewUnmap = 2
83 } SECTION_INHERIT;
84
85 //
86 // Pool Types
87 //
88 typedef enum _POOL_TYPE
89 {
90 NonPagedPool,
91 PagedPool,
92 NonPagedPoolMustSucceed,
93 DontUseThisType,
94 NonPagedPoolCacheAligned,
95 PagedPoolCacheAligned,
96 NonPagedPoolCacheAlignedMustS,
97 MaxPoolType,
98 NonPagedPoolSession = 32,
99 PagedPoolSession,
100 NonPagedPoolMustSucceedSession,
101 DontUseThisTypeSession,
102 NonPagedPoolCacheAlignedSession,
103 PagedPoolCacheAlignedSession,
104 NonPagedPoolCacheAlignedMustSSession
105 } POOL_TYPE;
106 #endif
107
108 //
109 // Per Processor Non Paged Lookaside List IDs
110 //
111 typedef enum _PP_NPAGED_LOOKASIDE_NUMBER
112 {
113 LookasideSmallIrpList = 0,
114 LookasideLargeIrpList = 1,
115 LookasideMdlList = 2,
116 LookasideCreateInfoList = 3,
117 LookasideNameBufferList = 4,
118 LookasideTwilightList = 5,
119 LookasideCompletionList = 6,
120 LookasideMaximumList = 7
121 } PP_NPAGED_LOOKASIDE_NUMBER;
122
123 //
124 // Memory Information Classes for NtQueryVirtualMemory
125 //
126 typedef enum _MEMORY_INFORMATION_CLASS
127 {
128 MemoryBasicInformation,
129 MemoryWorkingSetList,
130 MemorySectionName,
131 MemoryBasicVlmInformation
132 } MEMORY_INFORMATION_CLASS;
133
134 //
135 // Section Information Clasess for NtQuerySection
136 //
137 typedef enum _SECTION_INFORMATION_CLASS
138 {
139 SectionBasicInformation,
140 SectionImageInformation,
141 } SECTION_INFORMATION_CLASS;
142
143 #ifdef NTOS_MODE_USER
144
145 //
146 // Virtual Memory Counters
147 //
148 typedef struct _VM_COUNTERS
149 {
150 SIZE_T PeakVirtualSize;
151 SIZE_T VirtualSize;
152 ULONG PageFaultCount;
153 SIZE_T PeakWorkingSetSize;
154 SIZE_T WorkingSetSize;
155 SIZE_T QuotaPeakPagedPoolUsage;
156 SIZE_T QuotaPagedPoolUsage;
157 SIZE_T QuotaPeakNonPagedPoolUsage;
158 SIZE_T QuotaNonPagedPoolUsage;
159 SIZE_T PagefileUsage;
160 SIZE_T PeakPagefileUsage;
161 } VM_COUNTERS, *PVM_COUNTERS;
162
163 typedef struct _VM_COUNTERS_EX
164 {
165 SIZE_T PeakVirtualSize;
166 SIZE_T VirtualSize;
167 ULONG PageFaultCount;
168 SIZE_T PeakWorkingSetSize;
169 SIZE_T WorkingSetSize;
170 SIZE_T QuotaPeakPagedPoolUsage;
171 SIZE_T QuotaPagedPoolUsage;
172 SIZE_T QuotaPeakNonPagedPoolUsage;
173 SIZE_T QuotaNonPagedPoolUsage;
174 SIZE_T PagefileUsage;
175 SIZE_T PeakPagefileUsage;
176 SIZE_T PrivateUsage;
177 } VM_COUNTERS_EX, *PVM_COUNTERS_EX;
178 #endif
179
180 //
181 // List of Working Sets
182 //
183 typedef struct _MEMORY_WORKING_SET_LIST
184 {
185 ULONG NumberOfPages;
186 ULONG WorkingSetList[1];
187 } MEMORY_WORKING_SET_LIST, *PMEMORY_WORKING_SET_LIST;
188
189 //
190 // Memory Information Structures for NtQueryVirtualMemory
191 //
192 typedef struct
193 {
194 UNICODE_STRING SectionFileName;
195 WCHAR NameBuffer[ANYSIZE_ARRAY];
196 } MEMORY_SECTION_NAME, *PMEMORY_SECTION_NAME;
197
198 //
199 // Section Information Structures for NtQuerySection
200 //
201 typedef struct _SECTION_BASIC_INFORMATION
202 {
203 PVOID BaseAddress;
204 ULONG Attributes;
205 LARGE_INTEGER Size;
206 } SECTION_BASIC_INFORMATION, *PSECTION_BASIC_INFORMATION;
207
208 typedef struct _SECTION_IMAGE_INFORMATION
209 {
210 PVOID TransferAddress;
211 ULONG ZeroBits;
212 ULONG MaximumStackSize;
213 ULONG CommittedStackSize;
214 ULONG SubsystemType;
215 USHORT SubSystemMinorVersion;
216 USHORT SubSystemMajorVersion;
217 ULONG GpValue;
218 USHORT ImageCharacteristics;
219 USHORT DllChracteristics;
220 USHORT Machine;
221 UCHAR ImageContainsCode;
222 UCHAR Spare1;
223 ULONG LoaderFlags;
224 ULONG ImageFileSIze;
225 ULONG Reserved[1];
226 } SECTION_IMAGE_INFORMATION, *PSECTION_IMAGE_INFORMATION;
227
228 #ifndef NTOS_MODE_USER
229
230 //
231 // PTE Structures
232 //
233 typedef struct _MMPTE
234 {
235 union
236 {
237 ULONG Long;
238 HARDWARE_PTE Flush;
239 MMPTE_HARDWARE Hard;
240 MMPTE_PROTOTYPE Proto;
241 MMPTE_SOFTWARE Soft;
242 MMPTE_TRANSITION Trans;
243 MMPTE_SUBSECTION Subsect;
244 MMPTE_LIST List;
245 } u;
246 } MMPTE, *PMMPTE;
247
248 //
249 // Section Extension Information
250 //
251 typedef struct _MMEXTEND_INFO
252 {
253 ULONGLONG CommittedSize;
254 ULONG ReferenceCount;
255 } MMEXTEND_INFO, *PMMEXTEND_INFO;
256
257 //
258 // Segment and Segment Flags
259 //
260 typedef struct _SEGMENT_FLAGS
261 {
262 ULONG TotalNumberOfPtes4132:10;
263 ULONG ExtraSharedWowSubsections:1;
264 ULONG LargePages:1;
265 ULONG Spare:20;
266 } SEGMENT_FLAGS, *PSEGMENT_FLAGS;
267
268 typedef struct _SEGMENT
269 {
270 struct _CONTROL_AREA *ControlArea;
271 ULONG TotalNumberOfPtes;
272 ULONG NonExtendedPtes;
273 ULONG Spare0;
274 ULONGLONG SizeOfSegment;
275 MMPTE SegmentPteTemplate;
276 ULONG NumberOfCommittedPages;
277 PMMEXTEND_INFO ExtendInfo;
278 SEGMENT_FLAGS SegmentFlags;
279 PVOID BaseAddress;
280 union
281 {
282 SIZE_T ImageCommitment;
283 PEPROCESS CreatingProcess;
284 } u1;
285 union
286 {
287 PSECTION_IMAGE_INFORMATION ImageInformation;
288 PVOID FirstMappedVa;
289 } u2;
290 PMMPTE PrototypePte;
291 MMPTE ThePtes[1];
292 } SEGMENT, *PSEGMENT;
293
294 //
295 // Event Counter Structure
296 //
297 typedef struct _EVENT_COUNTER
298 {
299 ULONG RefCount;
300 KEVENT Event;
301 LIST_ENTRY ListEntry;
302 } EVENT_COUNTER, *PEVENT_COUNTER;
303
304 //
305 // Flags
306 //
307 typedef struct _MMSECTION_FLAGS
308 {
309 ULONG BeingDeleted:1;
310 ULONG BeingCreated:1;
311 ULONG BeingPurged:1;
312 ULONG NoModifiedWriting:1;
313 ULONG FailAllIo:1;
314 ULONG Image:1;
315 ULONG Based:1;
316 ULONG File:1;
317 ULONG Networked:1;
318 ULONG NoCache:1;
319 ULONG PhysicalMemory:1;
320 ULONG CopyOnWrite:1;
321 ULONG Reserve:1;
322 ULONG Commit:1;
323 ULONG FloppyMedia:1;
324 ULONG WasPurged:1;
325 ULONG UserReference:1;
326 ULONG GlobalMemory:1;
327 ULONG DeleteOnClose:1;
328 ULONG FilePointerNull:1;
329 ULONG DebugSymbolsLoaded:1;
330 ULONG SetMappedFileIoComplete:1;
331 ULONG CollidedFlush:1;
332 ULONG NoChange:1;
333 ULONG filler0:1;
334 ULONG ImageMappedInSystemSpace:1;
335 ULONG UserWritable:1;
336 ULONG Accessed:1;
337 ULONG GlobalOnlyPerSession:1;
338 ULONG Rom:1;
339 ULONG WriteCombined:1;
340 ULONG filler:1;
341 } MMSECTION_FLAGS, *PMMSECTION_FLAGS;
342
343 typedef struct _MMSUBSECTION_FLAGS
344 {
345 ULONG ReadOnly:1;
346 ULONG ReadWrite:1;
347 ULONG SubsectionStatic:1;
348 ULONG GlobalMemory:1;
349 ULONG Protection:5;
350 ULONG Spare:1;
351 ULONG StartingSector4132:10;
352 ULONG SectorEndOffset:12;
353 } MMSUBSECTION_FLAGS, *PMMSUBSECTION_FLAGS;
354
355 //
356 // Control Area Structures
357 //
358 typedef struct _CONTROL_AREA
359 {
360 PSEGMENT Segment;
361 LIST_ENTRY DereferenceList;
362 ULONG NumberOfSectionReferences;
363 ULONG NumberOfPfnReferences;
364 ULONG NumberOfMappedViews;
365 ULONG NumberOfSystemCacheViews;
366 ULONG NumberOfUserReferences;
367 union
368 {
369 ULONG LongFlags;
370 MMSECTION_FLAGS Flags;
371 } u;
372 PFILE_OBJECT FilePointer;
373 PEVENT_COUNTER WaitingForDeletion;
374 USHORT ModifiedWriteCount;
375 USHORT FlushInProgressCount;
376 ULONG WritableUserReferences;
377 ULONG QuadwordPad;
378 } CONTROL_AREA, *PCONTROL_AREA;
379
380 typedef struct _LARGE_CONTROL_AREA
381 {
382 PSEGMENT Segment;
383 LIST_ENTRY DereferenceList;
384 ULONG NumberOfSectionReferences;
385 ULONG NumberOfPfnReferences;
386 ULONG NumberOfMappedViews;
387 ULONG NumberOfSystemCacheViews;
388 ULONG NumberOfUserReferences;
389 union
390 {
391 ULONG LongFlags;
392 MMSECTION_FLAGS Flags;
393 } u;
394 PFILE_OBJECT FilePointer;
395 PEVENT_COUNTER WaitingForDeletion;
396 USHORT ModifiedWriteCount;
397 USHORT FlushInProgressCount;
398 ULONG WritableUserReferences;
399 ULONG QuadwordPad;
400 ULONG StartingFrame;
401 LIST_ENTRY UserGlobalList;
402 ULONG SessionId;
403 } LARGE_CONTROL_AREA, *PLARGE_CONTROL_AREA;
404
405 //
406 // Subsection
407 //
408 typedef struct _SUBSECTION
409 {
410 PCONTROL_AREA ControlArea;
411 union
412 {
413 ULONG LongFlags;
414 MMSUBSECTION_FLAGS SubsectionFlags;
415 } u;
416 ULONG StartingSector;
417 ULONG NumberOfFullSectors;
418 PMMPTE SubsectionBase;
419 ULONG UnusedPtes;
420 ULONG PtesInSubsection;
421 struct _SUBSECTION *NextSubSection;
422 } SUBSECTION, *PSUBSECTION;
423
424 //
425 // Segment Object
426 //
427 typedef struct _SEGMENT_OBJECT
428 {
429 PVOID BaseAddress;
430 ULONG TotalNumberOfPtes;
431 LARGE_INTEGER SizeOfSegment;
432 ULONG NonExtendedPtes;
433 ULONG ImageCommitment;
434 PCONTROL_AREA ControlArea;
435 PSUBSECTION Subsection;
436 PLARGE_CONTROL_AREA LargeControlArea;
437 PMMSECTION_FLAGS MmSectionFlags;
438 PMMSUBSECTION_FLAGS MmSubSectionFlags;
439 } SEGMENT_OBJECT, *PSEGMENT_OBJECT;
440
441 //
442 // Section Object
443 //
444 typedef struct _SECTION_OBJECT
445 {
446 PVOID StartingVa;
447 PVOID EndingVa;
448 PVOID LeftChild;
449 PVOID RightChild;
450 PSEGMENT_OBJECT Segment;
451 } SECTION_OBJECT, *PSECTION_OBJECT;
452
453 //
454 // Generic Address Range Structure
455 //
456 typedef struct _ADDRESS_RANGE
457 {
458 ULONG BaseAddrLow;
459 ULONG BaseAddrHigh;
460 ULONG LengthLow;
461 ULONG LengthHigh;
462 ULONG Type;
463 } ADDRESS_RANGE, *PADDRESS_RANGE;
464
465 //
466 // Node in Memory Manager's AVL Table
467 //
468 typedef struct _MMADDRESS_NODE
469 {
470 union
471 {
472 ULONG Balance:2;
473 struct _MMADDRESS_NODE *Parent;
474 } u1;
475 struct _MMADDRESS_NODE *LeftChild;
476 struct _MMADDRESS_NODE *RightChild;
477 ULONG StartingVpn;
478 ULONG EndingVpn;
479 } MMADDRESS_NODE, *PMMADDRESS_NODE;
480
481 //
482 // Memory Manager AVL Table for VADs and other descriptors
483 //
484 typedef struct _MM_AVL_TABLE
485 {
486 MMADDRESS_NODE BalancedRoot;
487 ULONG DepthOfTree:5;
488 ULONG Unused:3;
489 ULONG NumberGenericTableElements:24;
490 PVOID NodeHint;
491 PVOID NodeFreeHint;
492 } MM_AVL_TABLE, *PMM_AVL_TABLE;
493
494 //
495 // Actual Section Object
496 //
497 typedef struct _SECTION
498 {
499 MMADDRESS_NODE Address;
500 PSEGMENT Segment;
501 LARGE_INTEGER SizeOfSection;
502 union
503 {
504 ULONG LongFlags;
505 MMSECTION_FLAGS Flags;
506 } u;
507 ULONG InitialPageProtection;
508 } SECTION, *PSECTION;
509
510 //
511 // Memory Manager Working Set Structures
512 //
513 typedef struct _MMWSLENTRY
514 {
515 ULONG Valid:1;
516 ULONG LockedInWs:1;
517 ULONG LockedInMemory:1;
518 ULONG Protection:5;
519 ULONG Hashed:1;
520 ULONG Direct:1;
521 ULONG Age:2;
522 ULONG VirtualPageNumber:14;
523 } MMWSLENTRY, *PMMWSLENTRY;
524
525 typedef struct _MMWSLE
526 {
527 union
528 {
529 PVOID VirtualAddress;
530 ULONG Long;
531 MMWSLENTRY e1;
532 } u1;
533 } MMWSLE, *PMMWSLE;
534
535 typedef struct _MMWSLE_HASH
536 {
537 PVOID Key;
538 ULONG Index;
539 } MMWSLE_HASH, *PMMWSLE_HASH;
540
541 typedef struct _MMWSL
542 {
543 ULONG FirstFree;
544 ULONG FirstDynamic;
545 ULONG LastEntry;
546 ULONG NextSlot;
547 PMMWSLE Wsle;
548 ULONG LastInitializedWsle;
549 ULONG NonDirectCount;
550 PMMWSLE_HASH HashTable;
551 ULONG HashTableSize;
552 ULONG NumberOfCommittedPageTables;
553 PVOID HashTableStart;
554 PVOID HighestPermittedHashAddress;
555 ULONG NumberOfImageWaiters;
556 ULONG VadBitMapHint;
557 USHORT UsedPageTableEntries[768];
558 ULONG CommittedPageTables[24];
559 } MMWSL, *PMMWSL;
560
561 //
562 // Flags for Memory Support Structure
563 //
564 typedef struct _MMSUPPORT_FLAGS
565 {
566 ULONG SessionSpace:1;
567 ULONG BeingTrimmed:1;
568 ULONG SessionLeader:1;
569 ULONG TrimHard:1;
570 ULONG MaximumWorkingSetHard:1;
571 ULONG ForceTrim:1;
572 ULONG MinimumworkingSetHard:1;
573 ULONG Available0:1;
574 ULONG MemoryPriority:8;
575 ULONG GrowWsleHash:1;
576 ULONG AcquiredUnsafe:1;
577 ULONG Available:14;
578 } MMSUPPORT_FLAGS, *PMMSUPPORT_FLAGS;
579
580 //
581 // Per-Process Memory Manager Data
582 //
583 typedef struct _MMSUPPORT
584 {
585 #if (NTDDI_VERSION >= NTDDI_WS03)
586 LIST_ENTRY WorkingSetExpansionLinks;
587 #endif
588 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
589 USHORT LastTrimpStamp;
590 USHORT NextPageColor;
591 #else
592 LARGE_INTEGER LastTrimTime;
593 #endif
594 MMSUPPORT_FLAGS Flags;
595 ULONG PageFaultCount;
596 ULONG PeakWorkingSetSize;
597 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
598 ULONG Spare0;
599 #else
600 ULONG GrowthSinceLastEstimate;
601 #endif
602 ULONG MinimumWorkingSetSize;
603 ULONG MaximumWorkingSetSize;
604 PMMWSL VmWorkingSetList;
605 #if (NTDDI_VERSION < NTDDI_WS03)
606 LIST_ENTRY WorkingSetExpansionLinks;
607 #endif
608 ULONG Claim;
609 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
610 ULONG Spare;
611 ULONG WorkingSetPrivateSize;
612 ULONG WorkingSetSizeOverhead;
613 #else
614 ULONG NextEstimationSlot;
615 ULONG NextAgingSlot;
616 ULONG EstimatedAvailable;
617 #endif
618 ULONG WorkingSetSize;
619 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
620 PKEVENT ExitEvent;
621 #endif
622 EX_PUSH_LOCK WorkingSetMutex;
623 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
624 PVOID AccessLog;
625 #endif
626 } MMSUPPORT, *PMMSUPPORT;
627
628 //
629 // Memory Information Types
630 //
631 typedef struct _MEMORY_BASIC_INFORMATION
632 {
633 PVOID BaseAddress;
634 PVOID AllocationBase;
635 ULONG AllocationProtect;
636 ULONG RegionSize;
637 ULONG State;
638 ULONG Protect;
639 ULONG Type;
640 } MEMORY_BASIC_INFORMATION,*PMEMORY_BASIC_INFORMATION;
641
642 //
643 // Driver Verifier Data
644 //
645 typedef struct _MM_DRIVER_VERIFIER_DATA
646 {
647 ULONG Level;
648 ULONG RaiseIrqls;
649 ULONG AcquireSpinLocks;
650 ULONG SynchronizeExecutions;
651 ULONG AllocationsAttempted;
652 ULONG AllocationsSucceeded;
653 ULONG AllocationsSucceededSpecialPool;
654 ULONG AllocationsWithNoTag;
655 ULONG TrimRequests;
656 ULONG Trims;
657 ULONG AllocationsFailed;
658 ULONG AllocationsFailedDeliberately;
659 ULONG Loads;
660 ULONG Unloads;
661 ULONG UnTrackedPool;
662 ULONG UserTrims;
663 ULONG CurrentPagedPoolAllocations;
664 ULONG CurrentNonPagedPoolAllocations;
665 ULONG PeakPagedPoolAllocations;
666 ULONG PeakNonPagedPoolAllocations;
667 ULONG PagedBytes;
668 ULONG NonPagedBytes;
669 ULONG PeakPagedBytes;
670 ULONG PeakNonPagedBytes;
671 ULONG BurstAllocationsFailedDeliberately;
672 ULONG SessionTrims;
673 ULONG Reserved[2];
674 } MM_DRIVER_VERIFIER_DATA, *PMM_DRIVER_VERIFIER_DATA;
675
676 //
677 // Internal Driver Verifier Table Data
678 //
679 typedef struct _DRIVER_SPECIFIED_VERIFIER_THUNKS
680 {
681 LIST_ENTRY ListEntry;
682 struct _LDR_DATA_TABLE_ENTRY *DataTableEntry;
683 ULONG NumberOfThunks;
684 } DRIVER_SPECIFIED_VERIFIER_THUNKS, *PDRIVER_SPECIFIED_VERIFIER_THUNKS;
685
686 //
687 // Default heap size values. For user mode, these values are copied to a new
688 // process's PEB by the kernel in MmCreatePeb. In kernel mode, RtlCreateHeap
689 // reads these variables directly.
690 //
691 // These variables should be considered "const"; they are written only once,
692 // during MmInitSystem.
693 //
694 extern SIZE_T MmHeapSegmentReserve;
695 extern SIZE_T MmHeapSegmentCommit;
696 extern SIZE_T MmHeapDeCommitTotalFreeThreshold;
697 extern SIZE_T MmHeapDeCommitFreeBlockThreshold;
698
699 //
700 // Section Object Type
701 //
702 extern POBJECT_TYPE MmSectionObjectType;
703
704 #endif // !NTOS_MODE_USER
705
706 #endif // _MMTYPES_H