2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/miarm.h
5 * PURPOSE: ARM Memory Manager Header
6 * PROGRAMMERS: ReactOS Portable Systems Group
11 #define MI_MIN_PAGES_FOR_NONPAGED_POOL_TUNING ((255 * _1MB) >> PAGE_SHIFT)
12 #define MI_MIN_PAGES_FOR_SYSPTE_TUNING ((19 * _1MB) >> PAGE_SHIFT)
13 #define MI_MIN_PAGES_FOR_SYSPTE_BOOST ((32 * _1MB) >> PAGE_SHIFT)
14 #define MI_MAX_INIT_NONPAGED_POOL_SIZE (128 * _1MB)
15 #define MI_MAX_NONPAGED_POOL_SIZE (128 * _1MB)
16 #define MI_MAX_FREE_PAGE_LISTS 4
18 #define MI_MIN_INIT_PAGED_POOLSIZE (32 * _1MB)
20 #define MI_SESSION_VIEW_SIZE (20 * _1MB)
21 #define MI_SESSION_POOL_SIZE (16 * _1MB)
22 #define MI_SESSION_IMAGE_SIZE (8 * _1MB)
23 #define MI_SESSION_WORKING_SET_SIZE (4 * _1MB)
24 #define MI_SESSION_SIZE (MI_SESSION_VIEW_SIZE + \
25 MI_SESSION_POOL_SIZE + \
26 MI_SESSION_IMAGE_SIZE + \
27 MI_SESSION_WORKING_SET_SIZE)
29 #define MI_SYSTEM_VIEW_SIZE (16 * _1MB)
31 #define MI_HIGHEST_USER_ADDRESS (PVOID)0x7FFEFFFF
32 #define MI_USER_PROBE_ADDRESS (PVOID)0x7FFF0000
33 #define MI_DEFAULT_SYSTEM_RANGE_START (PVOID)0x80000000
34 #define MI_SYSTEM_CACHE_WS_START (PVOID)0xC0C00000
35 #define MI_PAGED_POOL_START (PVOID)0xE1000000
36 #define MI_NONPAGED_POOL_END (PVOID)0xFFBE0000
37 #define MI_DEBUG_MAPPING (PVOID)0xFFBFF000
39 #define MI_SYSTEM_PTE_BASE (PVOID)MiAddressToPte(NULL)
41 #define MI_MIN_SECONDARY_COLORS 8
42 #define MI_SECONDARY_COLORS 64
43 #define MI_MAX_SECONDARY_COLORS 1024
45 #define MI_MIN_ALLOCATION_FRAGMENT (4 * _1KB)
46 #define MI_ALLOCATION_FRAGMENT (64 * _1KB)
47 #define MI_MAX_ALLOCATION_FRAGMENT (2 * _1MB)
49 #define MM_HIGHEST_VAD_ADDRESS \
50 (PVOID)((ULONG_PTR)MM_HIGHEST_USER_ADDRESS - (16 * PAGE_SIZE))
51 #define MI_LOWEST_VAD_ADDRESS (PVOID)MM_LOWEST_USER_ADDRESS
53 #endif /* !_M_AMD64 */
55 /* Make the code cleaner with some definitions for size multiples */
57 #define _1MB (1024 * _1KB)
58 #define _1GB (1024 * _1MB)
60 /* Everyone loves 64K */
61 #define _64K (64 * _1KB)
63 /* Area mapped by a PDE */
64 #define PDE_MAPPED_VA (PTE_COUNT * PAGE_SIZE)
66 /* Size of a page table */
67 #define PT_SIZE (PTE_COUNT * sizeof(MMPTE))
69 /* Size of a page directory */
70 #define PD_SIZE (PDE_COUNT * sizeof(MMPDE))
72 /* Size of all page directories for a process */
73 #define SYSTEM_PD_SIZE (PD_COUNT * PD_SIZE)
75 /* Architecture specific count of PDEs in a directory, and count of PTEs in a PT */
78 #define PDE_COUNT 1024
79 #define PTE_COUNT 1024
80 C_ASSERT(SYSTEM_PD_SIZE
== PAGE_SIZE
);
81 #define MiIsPteOnPdeBoundary(PointerPte) \
82 ((((ULONG_PTR)PointerPte) & (PAGE_SIZE - 1)) == 0)
85 #define PDE_COUNT 4096
88 #define PD_COUNT PPE_PER_PAGE
89 #define PDE_COUNT PDE_PER_PAGE
90 #define PTE_COUNT PTE_PER_PAGE
94 // Protection Bits part of the internal memory manager Protection Mask
95 // Taken from http://www.reactos.org/wiki/Techwiki:Memory_management_in_the_Windows_XP_kernel
96 // and public assertions.
98 #define MM_ZERO_ACCESS 0
101 #define MM_EXECUTE_READ 3
102 #define MM_READWRITE 4
103 #define MM_WRITECOPY 5
104 #define MM_EXECUTE_READWRITE 6
105 #define MM_EXECUTE_WRITECOPY 7
107 #define MM_DECOMMIT 0x10
108 #define MM_NOACCESS (MM_DECOMMIT | MM_NOCACHE)
109 #define MM_INVALID_PROTECTION 0xFFFFFFFF
112 // Specific PTE Definitions that map to the Memory Manager's Protection Mask Bits
113 // The Memory Manager's definition define the attributes that must be preserved
114 // and these PTE definitions describe the attributes in the hardware sense. This
115 // helps deal with hardware differences between the actual boolean expression of
118 // For example, in the logical attributes, we want to express read-only as a flag
119 // but on x86, it is writability that must be set. On the other hand, on x86, just
120 // like in the kernel, it is disabling the caches that requires a special flag,
121 // while on certain architectures such as ARM, it is enabling the cache which
124 #if defined(_M_IX86) || defined(_M_AMD64)
128 #define PTE_READONLY 0 // Doesn't exist on x86
129 #define PTE_EXECUTE 0 // Not worrying about NX yet
130 #define PTE_EXECUTE_READ 0 // Not worrying about NX yet
131 #define PTE_READWRITE 0x2
132 #define PTE_WRITECOPY 0x200
133 #define PTE_EXECUTE_READWRITE 0x2 // Not worrying about NX yet
134 #define PTE_EXECUTE_WRITECOPY 0x200
135 #define PTE_PROTOTYPE 0x400
140 #define PTE_VALID 0x1
141 #define PTE_ACCESSED 0x20
142 #define PTE_DIRTY 0x40
147 #define PTE_ENABLE_CACHE 0
148 #define PTE_DISABLE_CACHE 0x10
149 #define PTE_WRITECOMBINED_CACHE 0x10
150 #elif defined(_M_ARM)
151 #define PTE_READONLY 0x200
152 #define PTE_EXECUTE 0 // Not worrying about NX yet
153 #define PTE_EXECUTE_READ 0 // Not worrying about NX yet
154 #define PTE_READWRITE 0 // Doesn't exist on ARM
155 #define PTE_WRITECOPY 0 // Doesn't exist on ARM
156 #define PTE_EXECUTE_READWRITE 0 // Not worrying about NX yet
157 #define PTE_EXECUTE_WRITECOPY 0 // Not worrying about NX yet
158 #define PTE_PROTOTYPE 0x400 // Using the Shared bit
162 #define PTE_ENABLE_CACHE 0
163 #define PTE_DISABLE_CACHE 0x10
164 #define PTE_WRITECOMBINED_CACHE 0x10
166 #error Define these please!
169 extern const ULONG_PTR MmProtectToPteMask
[32];
170 extern const ULONG MmProtectToValue
[32];
173 // Assertions for session images, addresses, and PTEs
175 #define MI_IS_SESSION_IMAGE_ADDRESS(Address) \
176 (((Address) >= MiSessionImageStart) && ((Address) < MiSessionImageEnd))
178 #define MI_IS_SESSION_ADDRESS(Address) \
179 (((Address) >= MmSessionBase) && ((Address) < MiSessionSpaceEnd))
181 #define MI_IS_SESSION_PTE(Pte) \
182 ((((PMMPTE)Pte) >= MiSessionBasePte) && (((PMMPTE)Pte) < MiSessionLastPte))
184 #define MI_IS_PAGE_TABLE_ADDRESS(Address) \
185 (((PVOID)(Address) >= (PVOID)PTE_BASE) && ((PVOID)(Address) <= (PVOID)PTE_TOP))
187 #define MI_IS_SYSTEM_PAGE_TABLE_ADDRESS(Address) \
188 (((Address) >= (PVOID)MiAddressToPte(MmSystemRangeStart)) && ((Address) <= (PVOID)PTE_TOP))
190 #define MI_IS_PAGE_TABLE_OR_HYPER_ADDRESS(Address) \
191 (((PVOID)(Address) >= (PVOID)PTE_BASE) && ((PVOID)(Address) <= (PVOID)MmHyperSpaceEnd))
194 // Corresponds to MMPTE_SOFTWARE.Protection
197 #define MM_PTE_SOFTWARE_PROTECTION_BITS 5
199 #define MM_PTE_SOFTWARE_PROTECTION_BITS 6
201 #define MM_PTE_SOFTWARE_PROTECTION_BITS 5
203 #error Define these please!
207 // Creates a software PTE with the given protection
209 #define MI_MAKE_SOFTWARE_PTE(p, x) ((p)->u.Long = (x << MM_PTE_SOFTWARE_PROTECTION_BITS))
212 // Marks a PTE as deleted
214 #define MI_SET_PFN_DELETED(x) ((x)->PteAddress = (PMMPTE)((ULONG_PTR)(x)->PteAddress | 1))
215 #define MI_IS_PFN_DELETED(x) ((ULONG_PTR)((x)->PteAddress) & 1)
218 // Special values for LoadedImports
220 #define MM_SYSLDR_NO_IMPORTS (PVOID)0xFFFFFFFE
221 #define MM_SYSLDR_BOOT_LOADED (PVOID)0xFFFFFFFF
222 #define MM_SYSLDR_SINGLE_ENTRY 0x1
224 #if defined(_M_IX86) || defined(_M_ARM)
228 #define LIST_HEAD 0xFFFFFFFF
231 // Because GCC cannot automatically downcast 0xFFFFFFFF to lesser-width bits,
232 // we need a manual definition suited to the number of bits in the PteFrame.
233 // This is used as a LIST_HEAD for the colored list
235 #define COLORED_LIST_HEAD ((1 << 25) - 1) // 0x1FFFFFF
236 #elif defined(_M_AMD64)
237 #define LIST_HEAD 0xFFFFFFFFFFFFFFFFLL
238 #define COLORED_LIST_HEAD ((1ULL << 57) - 1) // 0x1FFFFFFFFFFFFFFLL
240 #error Define these please!
244 // Special IRQL value (found in assertions)
246 #define MM_NOIRQL (KIRQL)0xFFFFFFFF
249 // Returns the color of a page
251 #define MI_GET_PAGE_COLOR(x) ((x) & MmSecondaryColorMask)
252 #define MI_GET_NEXT_COLOR() (MI_GET_PAGE_COLOR(++MmSystemPageColor))
253 #define MI_GET_NEXT_PROCESS_COLOR(x) (MI_GET_PAGE_COLOR(++(x)->NextPageColor))
257 // Decodes a Prototype PTE into the underlying PTE
259 #define MiProtoPteToPte(x) \
260 (PMMPTE)((ULONG_PTR)MmPagedPoolStart + \
261 (((x)->u.Proto.ProtoAddressHigh << 7) | (x)->u.Proto.ProtoAddressLow))
265 // Prototype PTEs that don't yet have a pagefile association
268 #define MI_PTE_LOOKUP_NEEDED 0xffffffffULL
270 #define MI_PTE_LOOKUP_NEEDED 0xFFFFF
274 // System views are binned into 64K chunks
276 #define MI_SYSTEM_VIEW_BUCKET_SIZE _64K
279 // FIXFIX: These should go in ex.h after the pool merge
282 #define POOL_BLOCK_SIZE 16
284 #define POOL_BLOCK_SIZE 8
286 #define POOL_LISTS_PER_PAGE (PAGE_SIZE / POOL_BLOCK_SIZE)
287 #define BASE_POOL_TYPE_MASK 1
288 #define POOL_MAX_ALLOC (PAGE_SIZE - (sizeof(POOL_HEADER) + POOL_BLOCK_SIZE))
290 typedef struct _POOL_DESCRIPTOR
295 ULONG RunningDeAllocs
;
301 LONG PendingFreeDepth
;
304 LIST_ENTRY ListHeads
[POOL_LISTS_PER_PAGE
];
305 } POOL_DESCRIPTOR
, *PPOOL_DESCRIPTOR
;
307 typedef struct _POOL_HEADER
314 USHORT PreviousSize
:8;
319 USHORT PreviousSize
:9;
333 PEPROCESS ProcessBilled
;
339 USHORT AllocatorBackTraceIndex
;
343 } POOL_HEADER
, *PPOOL_HEADER
;
345 C_ASSERT(sizeof(POOL_HEADER
) == POOL_BLOCK_SIZE
);
346 C_ASSERT(POOL_BLOCK_SIZE
== sizeof(LIST_ENTRY
));
348 extern ULONG ExpNumberOfPagedPools
;
349 extern POOL_DESCRIPTOR NonPagedPoolDescriptor
;
350 extern PPOOL_DESCRIPTOR ExpPagedPoolDescriptor
[16 + 1];
351 extern PVOID PoolTrackTable
;
357 typedef struct _MI_LARGE_PAGE_DRIVER_ENTRY
360 UNICODE_STRING BaseName
;
361 } MI_LARGE_PAGE_DRIVER_ENTRY
, *PMI_LARGE_PAGE_DRIVER_ENTRY
;
363 typedef enum _MMSYSTEM_PTE_POOL_TYPE
366 NonPagedPoolExpansion
,
368 } MMSYSTEM_PTE_POOL_TYPE
;
370 typedef enum _MI_PFN_CACHE_ATTRIBUTE
376 } MI_PFN_CACHE_ATTRIBUTE
, *PMI_PFN_CACHE_ATTRIBUTE
;
378 typedef struct _PHYSICAL_MEMORY_RUN
381 PFN_NUMBER PageCount
;
382 } PHYSICAL_MEMORY_RUN
, *PPHYSICAL_MEMORY_RUN
;
384 typedef struct _PHYSICAL_MEMORY_DESCRIPTOR
387 PFN_NUMBER NumberOfPages
;
388 PHYSICAL_MEMORY_RUN Run
[1];
389 } PHYSICAL_MEMORY_DESCRIPTOR
, *PPHYSICAL_MEMORY_DESCRIPTOR
;
391 typedef struct _MMCOLOR_TABLES
396 } MMCOLOR_TABLES
, *PMMCOLOR_TABLES
;
398 typedef struct _MI_LARGE_PAGE_RANGES
400 PFN_NUMBER StartFrame
;
401 PFN_NUMBER LastFrame
;
402 } MI_LARGE_PAGE_RANGES
, *PMI_LARGE_PAGE_RANGES
;
404 typedef struct _MMVIEW
407 PCONTROL_AREA ControlArea
;
410 typedef struct _MMSESSION
412 KGUARDED_MUTEX SystemSpaceViewLock
;
413 PKGUARDED_MUTEX SystemSpaceViewLockPointer
;
414 PCHAR SystemSpaceViewStart
;
415 PMMVIEW SystemSpaceViewTable
;
416 ULONG SystemSpaceHashSize
;
417 ULONG SystemSpaceHashEntries
;
418 ULONG SystemSpaceHashKey
;
419 ULONG BitmapFailures
;
420 PRTL_BITMAP SystemSpaceBitMap
;
421 } MMSESSION
, *PMMSESSION
;
423 extern MMPTE HyperTemplatePte
;
424 extern MMPDE ValidKernelPde
;
425 extern MMPTE ValidKernelPte
;
426 extern MMPDE DemandZeroPde
;
427 extern MMPTE DemandZeroPte
;
428 extern MMPTE PrototypePte
;
429 extern BOOLEAN MmLargeSystemCache
;
430 extern BOOLEAN MmZeroPageFile
;
431 extern BOOLEAN MmProtectFreedNonPagedPool
;
432 extern BOOLEAN MmTrackLockedPages
;
433 extern BOOLEAN MmTrackPtes
;
434 extern BOOLEAN MmDynamicPfn
;
435 extern BOOLEAN MmMirroring
;
436 extern BOOLEAN MmMakeLowMemory
;
437 extern BOOLEAN MmEnforceWriteProtection
;
438 extern SIZE_T MmAllocationFragment
;
439 extern ULONG MmConsumedPoolPercentage
;
440 extern ULONG MmVerifyDriverBufferType
;
441 extern ULONG MmVerifyDriverLevel
;
442 extern WCHAR MmVerifyDriverBuffer
[512];
443 extern WCHAR MmLargePageDriverBuffer
[512];
444 extern LIST_ENTRY MiLargePageDriverList
;
445 extern BOOLEAN MiLargePageAllDrivers
;
446 extern ULONG MmVerifyDriverBufferLength
;
447 extern ULONG MmLargePageDriverBufferLength
;
448 extern SIZE_T MmSizeOfNonPagedPoolInBytes
;
449 extern SIZE_T MmMaximumNonPagedPoolInBytes
;
450 extern PFN_NUMBER MmMaximumNonPagedPoolInPages
;
451 extern PFN_NUMBER MmSizeOfPagedPoolInPages
;
452 extern PVOID MmNonPagedSystemStart
;
453 extern SIZE_T MiNonPagedSystemSize
;
454 extern PVOID MmNonPagedPoolStart
;
455 extern PVOID MmNonPagedPoolExpansionStart
;
456 extern PVOID MmNonPagedPoolEnd
;
457 extern SIZE_T MmSizeOfPagedPoolInBytes
;
458 extern PVOID MmPagedPoolStart
;
459 extern PVOID MmPagedPoolEnd
;
460 extern PVOID MmSessionBase
;
461 extern SIZE_T MmSessionSize
;
462 extern PMMPTE MmFirstReservedMappingPte
, MmLastReservedMappingPte
;
463 extern PMMPTE MiFirstReservedZeroingPte
;
464 extern MI_PFN_CACHE_ATTRIBUTE MiPlatformCacheAttributes
[2][MmMaximumCacheType
];
465 extern PPHYSICAL_MEMORY_DESCRIPTOR MmPhysicalMemoryBlock
;
466 extern SIZE_T MmBootImageSize
;
467 extern PMMPTE MmSystemPtesStart
[MaximumPtePoolTypes
];
468 extern PMMPTE MmSystemPtesEnd
[MaximumPtePoolTypes
];
469 extern PMEMORY_ALLOCATION_DESCRIPTOR MxFreeDescriptor
;
470 extern MEMORY_ALLOCATION_DESCRIPTOR MxOldFreeDescriptor
;
471 extern ULONG_PTR MxPfnAllocation
;
472 extern MM_PAGED_POOL_INFO MmPagedPoolInfo
;
473 extern RTL_BITMAP MiPfnBitMap
;
474 extern KGUARDED_MUTEX MmPagedPoolMutex
;
475 extern PVOID MmPagedPoolStart
;
476 extern PVOID MmPagedPoolEnd
;
477 extern PVOID MmNonPagedSystemStart
;
478 extern PVOID MiSystemViewStart
;
479 extern SIZE_T MmSystemViewSize
;
480 extern PVOID MmSessionBase
;
481 extern PVOID MiSessionSpaceEnd
;
482 extern PMMPTE MiSessionImagePteStart
;
483 extern PMMPTE MiSessionImagePteEnd
;
484 extern PMMPTE MiSessionBasePte
;
485 extern PMMPTE MiSessionLastPte
;
486 extern SIZE_T MmSizeOfPagedPoolInBytes
;
487 extern PMMPDE MmSystemPagePtes
;
488 extern PVOID MmSystemCacheStart
;
489 extern PVOID MmSystemCacheEnd
;
490 extern MMSUPPORT MmSystemCacheWs
;
491 extern SIZE_T MmAllocatedNonPagedPool
;
492 extern ULONG_PTR MmSubsectionBase
;
493 extern ULONG MmSpecialPoolTag
;
494 extern PVOID MmHyperSpaceEnd
;
495 extern PMMWSL MmSystemCacheWorkingSetList
;
496 extern SIZE_T MmMinimumNonPagedPoolSize
;
497 extern ULONG MmMinAdditionNonPagedPoolPerMb
;
498 extern SIZE_T MmDefaultMaximumNonPagedPool
;
499 extern ULONG MmMaxAdditionNonPagedPoolPerMb
;
500 extern ULONG MmSecondaryColors
;
501 extern ULONG MmSecondaryColorMask
;
502 extern ULONG MmNumberOfSystemPtes
;
503 extern ULONG MmMaximumNonPagedPoolPercent
;
504 extern ULONG MmLargeStackSize
;
505 extern PMMCOLOR_TABLES MmFreePagesByColor
[FreePageList
+ 1];
506 extern ULONG MmProductType
;
507 extern MM_SYSTEMSIZE MmSystemSize
;
508 extern PKEVENT MiLowMemoryEvent
;
509 extern PKEVENT MiHighMemoryEvent
;
510 extern PKEVENT MiLowPagedPoolEvent
;
511 extern PKEVENT MiHighPagedPoolEvent
;
512 extern PKEVENT MiLowNonPagedPoolEvent
;
513 extern PKEVENT MiHighNonPagedPoolEvent
;
514 extern PFN_NUMBER MmLowMemoryThreshold
;
515 extern PFN_NUMBER MmHighMemoryThreshold
;
516 extern PFN_NUMBER MiLowPagedPoolThreshold
;
517 extern PFN_NUMBER MiHighPagedPoolThreshold
;
518 extern PFN_NUMBER MiLowNonPagedPoolThreshold
;
519 extern PFN_NUMBER MiHighNonPagedPoolThreshold
;
520 extern PFN_NUMBER MmMinimumFreePages
;
521 extern PFN_NUMBER MmPlentyFreePages
;
522 extern PFN_COUNT MiExpansionPoolPagesInitialCharge
;
523 extern PFN_NUMBER MmResidentAvailablePages
;
524 extern PFN_NUMBER MmResidentAvailableAtInit
;
525 extern ULONG MmTotalFreeSystemPtes
[MaximumPtePoolTypes
];
526 extern PFN_NUMBER MmTotalSystemDriverPages
;
527 extern PVOID MiSessionImageStart
;
528 extern PVOID MiSessionImageEnd
;
529 extern PMMPTE MiHighestUserPte
;
530 extern PMMPDE MiHighestUserPde
;
531 extern PFN_NUMBER MmSystemPageDirectory
[PD_COUNT
];
532 extern PMMPTE MmSharedUserDataPte
;
533 extern LIST_ENTRY MmProcessList
;
534 extern BOOLEAN MmZeroingPageThreadActive
;
535 extern KEVENT MmZeroingPageEvent
;
536 extern ULONG MmSystemPageColor
;
537 extern ULONG MmProcessColorSeed
;
538 extern PMMWSL MmWorkingSetList
;
539 extern PFN_NUMBER MiNumberOfFreePages
;
540 extern SIZE_T MmSessionViewSize
;
541 extern SIZE_T MmSessionPoolSize
;
542 extern SIZE_T MmSessionImageSize
;
543 extern PVOID MiSystemViewStart
;
544 extern PVOID MiSessionPoolEnd
; // 0xBE000000
545 extern PVOID MiSessionPoolStart
; // 0xBD000000
546 extern PVOID MiSessionViewStart
; // 0xBE000000
550 MiIsMemoryTypeFree(TYPE_OF_MEMORY MemoryType
)
552 return ((MemoryType
== LoaderFree
) ||
553 (MemoryType
== LoaderLoadedProgram
) ||
554 (MemoryType
== LoaderFirmwareTemporary
) ||
555 (MemoryType
== LoaderOsloaderStack
));
560 MiIsMemoryTypeInvisible(TYPE_OF_MEMORY MemoryType
)
562 return ((MemoryType
== LoaderFirmwarePermanent
) ||
563 (MemoryType
== LoaderSpecialMemory
) ||
564 (MemoryType
== LoaderHALCachedMemory
) ||
565 (MemoryType
== LoaderBBTMemory
));
570 // Figures out the hardware bits for a PTE
574 MiDetermineUserGlobalPteMask(IN PVOID PointerPte
)
581 /* Make it valid and accessed */
582 TempPte
.u
.Hard
.Valid
= TRUE
;
583 MI_MAKE_ACCESSED_PAGE(&TempPte
);
585 /* Is this for user-mode? */
586 if ((PointerPte
<= (PVOID
)MiHighestUserPte
) ||
587 ((PointerPte
>= (PVOID
)MiAddressToPde(NULL
)) &&
588 (PointerPte
<= (PVOID
)MiHighestUserPde
)))
590 /* Set the owner bit */
591 MI_MAKE_OWNER_PAGE(&TempPte
);
594 /* FIXME: We should also set the global bit */
596 /* Return the protection */
597 return TempPte
.u
.Long
;
601 // Creates a valid kernel PTE with the given protection
605 MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte
,
606 IN PMMPTE MappingPte
,
607 IN ULONG_PTR ProtectionMask
,
608 IN PFN_NUMBER PageFrameNumber
)
610 /* Only valid for kernel, non-session PTEs */
611 ASSERT(MappingPte
> MiHighestUserPte
);
612 ASSERT(!MI_IS_SESSION_PTE(MappingPte
));
613 ASSERT((MappingPte
< (PMMPTE
)PDE_BASE
) || (MappingPte
> (PMMPTE
)PDE_TOP
));
616 *NewPte
= ValidKernelPte
;
618 /* Set the protection and page */
619 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
620 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
624 // Creates a valid PTE with the given protection
628 MI_MAKE_HARDWARE_PTE(IN PMMPTE NewPte
,
629 IN PMMPTE MappingPte
,
630 IN ULONG_PTR ProtectionMask
,
631 IN PFN_NUMBER PageFrameNumber
)
633 /* Set the protection and page */
634 NewPte
->u
.Long
= MiDetermineUserGlobalPteMask(MappingPte
);
635 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
636 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
640 // Creates a valid user PTE with the given protection
644 MI_MAKE_HARDWARE_PTE_USER(IN PMMPTE NewPte
,
645 IN PMMPTE MappingPte
,
646 IN ULONG_PTR ProtectionMask
,
647 IN PFN_NUMBER PageFrameNumber
)
649 /* Only valid for kernel, non-session PTEs */
650 ASSERT(MappingPte
<= MiHighestUserPte
);
653 *NewPte
= ValidKernelPte
;
655 /* Set the protection and page */
656 NewPte
->u
.Hard
.Owner
= TRUE
;
657 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
658 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
663 // Builds a Prototype PTE for the address of the PTE
667 MI_MAKE_PROTOTYPE_PTE(IN PMMPTE NewPte
,
668 IN PMMPTE PointerPte
)
672 /* Mark this as a prototype */
674 NewPte
->u
.Proto
.Prototype
= 1;
677 * Prototype PTEs are only valid in paged pool by design, this little trick
678 * lets us only use 28 bits for the adress of the PTE
680 Offset
= (ULONG_PTR
)PointerPte
- (ULONG_PTR
)MmPagedPoolStart
;
682 /* 7 bits go in the "low", and the other 21 bits go in the "high" */
683 NewPte
->u
.Proto
.ProtoAddressLow
= Offset
& 0x7F;
684 NewPte
->u
.Proto
.ProtoAddressHigh
= (Offset
& 0xFFFFFF80) >> 7;
685 ASSERT(MiProtoPteToPte(NewPte
) == PointerPte
);
690 // Returns if the page is physically resident (ie: a large page)
691 // FIXFIX: CISC/x86 only?
695 MI_IS_PHYSICAL_ADDRESS(IN PVOID Address
)
699 /* Large pages are never paged out, always physically resident */
700 PointerPde
= MiAddressToPde(Address
);
701 return ((PointerPde
->u
.Hard
.LargePage
) && (PointerPde
->u
.Hard
.Valid
));
705 // Writes a valid PTE
709 MI_WRITE_VALID_PTE(IN PMMPTE PointerPte
,
712 /* Write the valid PTE */
713 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
714 ASSERT(TempPte
.u
.Hard
.Valid
== 1);
715 *PointerPte
= TempPte
;
719 // Writes an invalid PTE
723 MI_WRITE_INVALID_PTE(IN PMMPTE PointerPte
,
726 /* Write the invalid PTE */
727 ASSERT(InvalidPte
.u
.Hard
.Valid
== 0);
728 *PointerPte
= InvalidPte
;
732 // Writes a valid PDE
736 MI_WRITE_VALID_PDE(IN PMMPDE PointerPde
,
739 /* Write the valid PDE */
740 ASSERT(PointerPde
->u
.Hard
.Valid
== 0);
741 ASSERT(TempPde
.u
.Hard
.Valid
== 1);
742 *PointerPde
= TempPde
;
746 // Writes an invalid PDE
750 MI_WRITE_INVALID_PDE(IN PMMPDE PointerPde
,
753 /* Write the invalid PDE */
754 ASSERT(InvalidPde
.u
.Hard
.Valid
== 0);
755 *PointerPde
= InvalidPde
;
759 // Checks if the thread already owns a working set
763 MM_ANY_WS_LOCK_HELD(IN PETHREAD Thread
)
765 /* If any of these are held, return TRUE */
766 return ((Thread
->OwnsProcessWorkingSetExclusive
) ||
767 (Thread
->OwnsProcessWorkingSetShared
) ||
768 (Thread
->OwnsSystemWorkingSetExclusive
) ||
769 (Thread
->OwnsSystemWorkingSetShared
) ||
770 (Thread
->OwnsSessionWorkingSetExclusive
) ||
771 (Thread
->OwnsSessionWorkingSetShared
));
775 // Checks if the process owns the working set lock
779 MI_WS_OWNER(IN PEPROCESS Process
)
781 /* Check if this process is the owner, and that the thread owns the WS */
782 return ((KeGetCurrentThread()->ApcState
.Process
== &Process
->Pcb
) &&
783 ((PsGetCurrentThread()->OwnsProcessWorkingSetExclusive
) ||
784 (PsGetCurrentThread()->OwnsProcessWorkingSetShared
)));
788 // Locks the working set for the given process
792 MiLockProcessWorkingSet(IN PEPROCESS Process
,
795 /* Shouldn't already be owning the process working set */
796 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
797 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
799 /* Block APCs, make sure that still nothing is already held */
800 KeEnterGuardedRegion();
801 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
803 /* FIXME: Actually lock it (we can't because Vm is used by MAREAs) */
805 /* FIXME: This also can't be checked because Vm is used by MAREAs) */
806 //ASSERT(Process->Vm.Flags.AcquiredUnsafe == 0);
808 /* Okay, now we can own it exclusively */
809 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
810 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
814 // Unlocks the working set for the given process
818 MiUnlockProcessWorkingSet(IN PEPROCESS Process
,
821 /* Make sure this process really is owner, and it was a safe acquisition */
822 ASSERT(MI_WS_OWNER(Process
));
823 /* This can't be checked because Vm is used by MAREAs) */
824 //ASSERT(Process->Vm.Flags.AcquiredUnsafe == 0);
826 /* The thread doesn't own it anymore */
827 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== TRUE
);
828 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
830 /* FIXME: Actually release it (we can't because Vm is used by MAREAs) */
833 KeLeaveGuardedRegion();
837 // Locks the working set
841 MiLockWorkingSet(IN PETHREAD Thread
,
842 IN PMMSUPPORT WorkingSet
)
845 KeEnterGuardedRegion();
847 /* Working set should be in global memory */
848 ASSERT(MI_IS_SESSION_ADDRESS((PVOID
)WorkingSet
) == FALSE
);
850 /* Thread shouldn't already be owning something */
851 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
853 /* FIXME: Actually lock it (we can't because Vm is used by MAREAs) */
855 /* Which working set is this? */
856 if (WorkingSet
== &MmSystemCacheWs
)
858 /* Own the system working set */
859 ASSERT((Thread
->OwnsSystemWorkingSetExclusive
== FALSE
) &&
860 (Thread
->OwnsSystemWorkingSetShared
== FALSE
));
861 Thread
->OwnsSystemWorkingSetExclusive
= TRUE
;
863 else if (WorkingSet
->Flags
.SessionSpace
)
865 /* We don't implement this yet */
871 /* Own the process working set */
872 ASSERT((Thread
->OwnsProcessWorkingSetExclusive
== FALSE
) &&
873 (Thread
->OwnsProcessWorkingSetShared
== FALSE
));
874 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
879 // Unlocks the working set
883 MiUnlockWorkingSet(IN PETHREAD Thread
,
884 IN PMMSUPPORT WorkingSet
)
886 /* Working set should be in global memory */
887 ASSERT(MI_IS_SESSION_ADDRESS((PVOID
)WorkingSet
) == FALSE
);
889 /* Which working set is this? */
890 if (WorkingSet
== &MmSystemCacheWs
)
892 /* Release the system working set */
893 ASSERT((Thread
->OwnsSystemWorkingSetExclusive
== TRUE
) ||
894 (Thread
->OwnsSystemWorkingSetShared
== TRUE
));
895 Thread
->OwnsSystemWorkingSetExclusive
= FALSE
;
897 else if (WorkingSet
->Flags
.SessionSpace
)
899 /* We don't implement this yet */
905 /* Release the process working set */
906 ASSERT((Thread
->OwnsProcessWorkingSetExclusive
) ||
907 (Thread
->OwnsProcessWorkingSetShared
));
908 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
911 /* FIXME: Actually release it (we can't because Vm is used by MAREAs) */
914 KeLeaveGuardedRegion();
918 // Returns the ProtoPTE inside a VAD for the given VPN
922 MI_GET_PROTOTYPE_PTE_FOR_VPN(IN PMMVAD Vad
,
927 /* Find the offset within the VAD's prototype PTEs */
928 ProtoPte
= Vad
->FirstPrototypePte
+ (Vpn
- Vad
->StartingVpn
);
929 ASSERT(ProtoPte
<= Vad
->LastContiguousPte
);
934 // Returns the PFN Database entry for the given page number
935 // Warning: This is not necessarily a valid PFN database entry!
939 MI_PFN_ELEMENT(IN PFN_NUMBER Pfn
)
942 return &MmPfnDatabase
[Pfn
];
949 IN PLOADER_PARAMETER_BLOCK LoaderBlock
954 MiInitializeSessionSpaceLayout();
958 MiInitMachineDependent(
959 IN PLOADER_PARAMETER_BLOCK LoaderBlock
964 MiComputeColorInformation(
971 IN PLOADER_PARAMETER_BLOCK LoaderBlock
976 MiInitializeColorTables(
982 MiInitializePfnDatabase(
983 IN PLOADER_PARAMETER_BLOCK LoaderBlock
988 MiInitializeMemoryEvents(
995 IN PFN_NUMBER PageCount
998 PPHYSICAL_MEMORY_DESCRIPTOR
1000 MmInitializeMemoryLimits(
1001 IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
1002 IN PBOOLEAN IncludeType
1007 MiPagesInLoaderBlock(
1008 IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
1009 IN PBOOLEAN IncludeType
1015 IN PVOID AddressStart
,
1022 IN BOOLEAN StoreInstruction
,
1024 IN KPROCESSOR_MODE Mode
,
1025 IN PVOID TrapInformation
1030 MiCheckPdeForPagedPool(
1036 MiInitializeNonPagedPool(
1042 MiInitializeNonPagedPoolThresholds(
1048 MiInitializePoolEvents(
1055 IN POOL_TYPE PoolType
,// FIXFIX: This should go in ex.h after the pool merge
1056 IN ULONG Threshold
//
1061 MiInitializeSystemPtes(
1062 IN PMMPTE StartingPte
,
1063 IN ULONG NumberOfPtes
,
1064 IN MMSYSTEM_PTE_POOL_TYPE PoolType
1069 MiReserveSystemPtes(
1070 IN ULONG NumberOfPtes
,
1071 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1076 MiReleaseSystemPtes(
1077 IN PMMPTE StartingPte
,
1078 IN ULONG NumberOfPtes
,
1079 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1085 MiFindContiguousPages(
1086 IN PFN_NUMBER LowestPfn
,
1087 IN PFN_NUMBER HighestPfn
,
1088 IN PFN_NUMBER BoundaryPfn
,
1089 IN PFN_NUMBER SizeInPages
,
1090 IN MEMORY_CACHING_TYPE CacheType
1095 MiCheckForContiguousMemory(
1096 IN PVOID BaseAddress
,
1097 IN PFN_NUMBER BaseAddressPages
,
1098 IN PFN_NUMBER SizeInPages
,
1099 IN PFN_NUMBER LowestPfn
,
1100 IN PFN_NUMBER HighestPfn
,
1101 IN PFN_NUMBER BoundaryPfn
,
1102 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
1107 MiAllocatePagesForMdl(
1108 IN PHYSICAL_ADDRESS LowAddress
,
1109 IN PHYSICAL_ADDRESS HighAddress
,
1110 IN PHYSICAL_ADDRESS SkipBytes
,
1111 IN SIZE_T TotalBytes
,
1112 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
,
1118 MiMapLockedPagesInUserSpace(
1121 IN MEMORY_CACHING_TYPE CacheType
,
1122 IN PVOID BaseAddress
1127 MiUnmapLockedPagesInUserSpace(
1128 IN PVOID BaseAddress
,
1135 IN PMMPFNLIST ListHead
,
1136 IN PFN_NUMBER PageFrameIndex
1141 MiUnlinkFreeOrZeroedPage(
1148 IN PMMPTE PointerPte
,
1155 IN PFN_NUMBER PageFrameIndex
,
1156 IN PMMPTE PointerPte
,
1162 MiInitializePfnAndMakePteValid(
1163 IN PFN_NUMBER PageFrameIndex
,
1164 IN PMMPTE PointerPte
,
1170 MiInitializePfnForOtherProcess(
1171 IN PFN_NUMBER PageFrameIndex
,
1172 IN PMMPTE PointerPte
,
1173 IN PFN_NUMBER PteFrame
1178 MiDecrementShareCount(
1180 IN PFN_NUMBER PageFrameIndex
1185 MiDecrementReferenceCount(
1187 IN PFN_NUMBER PageFrameIndex
1205 IN PFN_NUMBER PageFrameIndex
1210 MiInsertPageInFreeList(
1211 IN PFN_NUMBER PageFrameIndex
1216 MiDeleteSystemPageableVm(
1217 IN PMMPTE PointerPte
,
1218 IN PFN_NUMBER PageCount
,
1220 OUT PPFN_NUMBER ValidPages
1223 PLDR_DATA_TABLE_ENTRY
1225 MiLookupDataTableEntry(
1231 MiInitializeDriverLargePageList(
1237 MiInitializeLargePageSupport(
1256 IN PVOID VirtualAddress
1261 MiCheckForConflictingNode(
1262 IN ULONG_PTR StartVpn
,
1263 IN ULONG_PTR EndVpn
,
1264 IN PMM_AVL_TABLE Table
1269 MiFindEmptyAddressRangeDownTree(
1271 IN ULONG_PTR BoundaryAddress
,
1272 IN ULONG_PTR Alignment
,
1273 IN PMM_AVL_TABLE Table
,
1274 OUT PULONG_PTR Base
,
1275 OUT PMMADDRESS_NODE
*Parent
1280 MiFindEmptyAddressRangeInTree(
1282 IN ULONG_PTR Alignment
,
1283 IN PMM_AVL_TABLE Table
,
1284 OUT PMMADDRESS_NODE
*PreviousVad
,
1292 IN PEPROCESS Process
1298 IN PMM_AVL_TABLE Table
,
1299 IN PMMADDRESS_NODE NewNode
,
1300 PMMADDRESS_NODE Parent
,
1301 TABLE_SEARCH_RESULT Result
1307 IN PMMADDRESS_NODE Node
,
1308 IN PMM_AVL_TABLE Table
1314 IN PMMADDRESS_NODE Node
1320 IN PMMADDRESS_NODE Node
1325 MiInitializeSystemSpaceMap(
1326 IN PVOID InputSession OPTIONAL
1331 MiMakeProtectionMask(
1337 MiDeleteVirtualAddresses(
1339 IN ULONG_PTR EndingAddress
,
1345 MiMakeSystemAddressValid(
1346 IN PVOID PageTableVirtualAddress
,
1347 IN PEPROCESS CurrentProcess
1352 MiMakeSystemAddressValidPfn(
1353 IN PVOID VirtualAddress
,
1360 IN PEPROCESS CurrentProcess
,
1372 // MiRemoveZeroPage will use inline code to zero out the page manually if only
1373 // free pages are available. In some scenarios, we don't/can't run that piece of
1374 // code and would rather only have a real zero page. If we can't have a zero page,
1375 // then we'd like to have our own code to grab a free page and zero it out, by
1376 // using MiRemoveAnyPage. This macro implements this.
1380 MiRemoveZeroPageSafe(IN ULONG Color
)
1382 if (MmFreePagesByColor
[ZeroedPageList
][Color
].Flink
!= LIST_HEAD
) return MiRemoveZeroPage(Color
);
1387 // New ARM3<->RosMM PAGE Architecture
1390 // HACK ON TOP OF HACK ALERT!!!
1391 #define MI_GET_ROS_DATA(x) \
1392 (((x)->RosMmData == 0) ? NULL : ((PMMROSPFN)((ULONG64)(ULONG)((x)->RosMmData) | \
1393 ((ULONG64)MmNonPagedPoolStart & 0xffffffff00000000ULL))))
1395 #define MI_GET_ROS_DATA(x) ((PMMROSPFN)(x->RosMmData))
1397 #define MI_IS_ROS_PFN(x) (((x)->u4.AweAllocation == TRUE) && (MI_GET_ROS_DATA(x) != NULL))
1398 #define ASSERT_IS_ROS_PFN(x) ASSERT(MI_IS_ROS_PFN(x) == TRUE);
1399 typedef struct _MMROSPFN
1401 PMM_RMAP_ENTRY RmapListHead
;
1402 SWAPENTRY SwapEntry
;
1403 } MMROSPFN
, *PMMROSPFN
;
1405 #define RosMmData AweReferenceCount