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_MIN_PAGES_FOR_SYSPTE_BOOST_BOOST ((256 * _1MB) >> PAGE_SHIFT)
15 #define MI_MAX_INIT_NONPAGED_POOL_SIZE (128 * _1MB)
16 #define MI_MAX_NONPAGED_POOL_SIZE (128 * _1MB)
17 #define MI_MAX_FREE_PAGE_LISTS 4
19 #define MI_MIN_INIT_PAGED_POOLSIZE (32 * _1MB)
21 #define MI_SESSION_VIEW_SIZE (48 * _1MB)
22 #define MI_SESSION_POOL_SIZE (16 * _1MB)
23 #define MI_SESSION_IMAGE_SIZE (8 * _1MB)
24 #define MI_SESSION_WORKING_SET_SIZE (4 * _1MB)
25 #define MI_SESSION_SIZE (MI_SESSION_VIEW_SIZE + \
26 MI_SESSION_POOL_SIZE + \
27 MI_SESSION_IMAGE_SIZE + \
28 MI_SESSION_WORKING_SET_SIZE)
30 #define MI_SYSTEM_VIEW_SIZE (32 * _1MB)
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 #define MI_DEFAULT_SYSTEM_PTE_COUNT 50000
54 #define MI_MAX_ZERO_BITS 21
56 #endif /* !_M_AMD64 */
58 /* Make the code cleaner with some definitions for size multiples */
60 #define _1MB (1024 * _1KB)
61 #define _1GB (1024 * _1MB)
63 /* Everyone loves 64K */
64 #define _64K (64 * _1KB)
66 /* Area mapped by a PDE */
67 #define PDE_MAPPED_VA (PTE_COUNT * PAGE_SIZE)
69 /* Size of a page table */
70 #define PT_SIZE (PTE_COUNT * sizeof(MMPTE))
72 /* Size of a page directory */
73 #define PD_SIZE (PDE_COUNT * sizeof(MMPDE))
75 /* Size of all page directories for a process */
76 #define SYSTEM_PD_SIZE (PD_COUNT * PD_SIZE)
78 /* Architecture specific count of PDEs in a directory, and count of PTEs in a PT */
81 #define PDE_COUNT 1024
82 #define PTE_COUNT 1024
83 C_ASSERT(SYSTEM_PD_SIZE
== PAGE_SIZE
);
84 #define MiIsPteOnPdeBoundary(PointerPte) \
85 ((((ULONG_PTR)PointerPte) & (PAGE_SIZE - 1)) == 0)
87 #define PPE_PER_PAGE 1
88 #define PDE_PER_PAGE 4096
89 #define PTE_PER_PAGE 256
91 #define PDE_COUNT 4096
94 #define PD_COUNT PPE_PER_PAGE
95 #define PDE_COUNT PDE_PER_PAGE
96 #define PTE_COUNT PTE_PER_PAGE
100 // Protection Bits part of the internal memory manager Protection Mask, from:
101 // http://reactos.org/wiki/Techwiki:Memory_management_in_the_Windows_XP_kernel
102 // https://www.reactos.org/wiki/Techwiki:Memory_Protection_constants
103 // and public assertions.
105 #define MM_ZERO_ACCESS 0
106 #define MM_READONLY 1
108 #define MM_EXECUTE_READ 3
109 #define MM_READWRITE 4
110 #define MM_WRITECOPY 5
111 #define MM_EXECUTE_READWRITE 6
112 #define MM_EXECUTE_WRITECOPY 7
113 #define MM_PROTECT_ACCESS 7
116 // These are flags on top of the actual protection mask
118 #define MM_NOCACHE 0x08
119 #define MM_GUARDPAGE 0x10
120 #define MM_WRITECOMBINE 0x18
121 #define MM_PROTECT_SPECIAL 0x18
124 // These are special cases
126 #define MM_DECOMMIT (MM_ZERO_ACCESS | MM_GUARDPAGE)
127 #define MM_NOACCESS (MM_ZERO_ACCESS | MM_WRITECOMBINE)
128 #define MM_OUTSWAPPED_KSTACK (MM_EXECUTE_WRITECOPY | MM_WRITECOMBINE)
129 #define MM_INVALID_PROTECTION 0xFFFFFFFF
132 // Specific PTE Definitions that map to the Memory Manager's Protection Mask Bits
133 // The Memory Manager's definition define the attributes that must be preserved
134 // and these PTE definitions describe the attributes in the hardware sense. This
135 // helps deal with hardware differences between the actual boolean expression of
138 // For example, in the logical attributes, we want to express read-only as a flag
139 // but on x86, it is writability that must be set. On the other hand, on x86, just
140 // like in the kernel, it is disabling the caches that requires a special flag,
141 // while on certain architectures such as ARM, it is enabling the cache which
144 #if defined(_M_IX86) || defined(_M_AMD64)
148 #define PTE_READONLY 0 // Doesn't exist on x86
149 #define PTE_EXECUTE 0 // Not worrying about NX yet
150 #define PTE_EXECUTE_READ 0 // Not worrying about NX yet
151 #define PTE_READWRITE 0x2
152 #define PTE_WRITECOPY 0x200
153 #define PTE_EXECUTE_READWRITE 0x2 // Not worrying about NX yet
154 #define PTE_EXECUTE_WRITECOPY 0x200
155 #define PTE_PROTOTYPE 0x400
160 #define PTE_VALID 0x1
161 #define PTE_ACCESSED 0x20
162 #define PTE_DIRTY 0x40
167 #define PTE_ENABLE_CACHE 0
168 #define PTE_DISABLE_CACHE 0x10
169 #define PTE_WRITECOMBINED_CACHE 0x10
170 #elif defined(_M_ARM)
171 #define PTE_READONLY 0x200
172 #define PTE_EXECUTE 0 // Not worrying about NX yet
173 #define PTE_EXECUTE_READ 0 // Not worrying about NX yet
174 #define PTE_READWRITE 0 // Doesn't exist on ARM
175 #define PTE_WRITECOPY 0 // Doesn't exist on ARM
176 #define PTE_EXECUTE_READWRITE 0 // Not worrying about NX yet
177 #define PTE_EXECUTE_WRITECOPY 0 // Not worrying about NX yet
178 #define PTE_PROTOTYPE 0x400 // Using the Shared bit
182 #define PTE_ENABLE_CACHE 0
183 #define PTE_DISABLE_CACHE 0x10
184 #define PTE_WRITECOMBINED_CACHE 0x10
186 #error Define these please!
189 extern const ULONG_PTR MmProtectToPteMask
[32];
190 extern const ULONG MmProtectToValue
[32];
193 // Assertions for session images, addresses, and PTEs
195 #define MI_IS_SESSION_IMAGE_ADDRESS(Address) \
196 (((Address) >= MiSessionImageStart) && ((Address) < MiSessionImageEnd))
198 #define MI_IS_SESSION_ADDRESS(Address) \
199 (((Address) >= MmSessionBase) && ((Address) < MiSessionSpaceEnd))
201 #define MI_IS_SESSION_PTE(Pte) \
202 ((((PMMPTE)Pte) >= MiSessionBasePte) && (((PMMPTE)Pte) < MiSessionLastPte))
204 #define MI_IS_PAGE_TABLE_ADDRESS(Address) \
205 (((PVOID)(Address) >= (PVOID)PTE_BASE) && ((PVOID)(Address) <= (PVOID)PTE_TOP))
207 #define MI_IS_SYSTEM_PAGE_TABLE_ADDRESS(Address) \
208 (((Address) >= (PVOID)MiAddressToPte(MmSystemRangeStart)) && ((Address) <= (PVOID)PTE_TOP))
210 #define MI_IS_PAGE_TABLE_OR_HYPER_ADDRESS(Address) \
211 (((PVOID)(Address) >= (PVOID)PTE_BASE) && ((PVOID)(Address) <= (PVOID)MmHyperSpaceEnd))
214 // Corresponds to MMPTE_SOFTWARE.Protection
217 #define MM_PTE_SOFTWARE_PROTECTION_BITS 5
219 #define MM_PTE_SOFTWARE_PROTECTION_BITS 6
221 #define MM_PTE_SOFTWARE_PROTECTION_BITS 5
223 #error Define these please!
227 // Creates a software PTE with the given protection
229 #define MI_MAKE_SOFTWARE_PTE(p, x) ((p)->u.Long = (x << MM_PTE_SOFTWARE_PROTECTION_BITS))
232 // Marks a PTE as deleted
234 #define MI_SET_PFN_DELETED(x) ((x)->PteAddress = (PMMPTE)((ULONG_PTR)(x)->PteAddress | 1))
235 #define MI_IS_PFN_DELETED(x) ((ULONG_PTR)((x)->PteAddress) & 1)
238 // Special values for LoadedImports
240 #define MM_SYSLDR_NO_IMPORTS (PVOID)0xFFFFFFFE
241 #define MM_SYSLDR_BOOT_LOADED (PVOID)0xFFFFFFFF
242 #define MM_SYSLDR_SINGLE_ENTRY 0x1
245 // Number of initial session IDs
247 #define MI_INITIAL_SESSION_IDS 64
249 #if defined(_M_IX86) || defined(_M_ARM)
253 #define LIST_HEAD 0xFFFFFFFF
256 // Because GCC cannot automatically downcast 0xFFFFFFFF to lesser-width bits,
257 // we need a manual definition suited to the number of bits in the PteFrame.
258 // This is used as a LIST_HEAD for the colored list
260 #define COLORED_LIST_HEAD ((1 << 25) - 1) // 0x1FFFFFF
261 #elif defined(_M_AMD64)
262 #define LIST_HEAD 0xFFFFFFFFFFFFFFFFLL
263 #define COLORED_LIST_HEAD ((1ULL << 57) - 1) // 0x1FFFFFFFFFFFFFFLL
265 #error Define these please!
269 // Special IRQL value (found in assertions)
271 #define MM_NOIRQL (KIRQL)0xFFFFFFFF
274 // Returns the color of a page
276 #define MI_GET_PAGE_COLOR(x) ((x) & MmSecondaryColorMask)
277 #define MI_GET_NEXT_COLOR() (MI_GET_PAGE_COLOR(++MmSystemPageColor))
278 #define MI_GET_NEXT_PROCESS_COLOR(x) (MI_GET_PAGE_COLOR(++(x)->NextPageColor))
282 // Decodes a Prototype PTE into the underlying PTE
284 #define MiProtoPteToPte(x) \
285 (PMMPTE)((ULONG_PTR)MmPagedPoolStart + \
286 (((x)->u.Proto.ProtoAddressHigh << 9) | (x)->u.Proto.ProtoAddressLow << 2))
289 // Decodes a Prototype PTE into the underlying PTE
291 #define MiSubsectionPteToSubsection(x) \
292 ((x)->u.Subsect.WhichPool == PagedPool) ? \
293 (PMMPTE)((ULONG_PTR)MmSubsectionBase + \
294 (((x)->u.Subsect.SubsectionAddressHigh << 7) | \
295 (x)->u.Subsect.SubsectionAddressLow << 3)) : \
296 (PMMPTE)((ULONG_PTR)MmNonPagedPoolEnd - \
297 (((x)->u.Subsect.SubsectionAddressHigh << 7) | \
298 (x)->u.Subsect.SubsectionAddressLow << 3))
302 // Prototype PTEs that don't yet have a pagefile association
305 #define MI_PTE_LOOKUP_NEEDED 0xffffffffULL
307 #define MI_PTE_LOOKUP_NEEDED 0xFFFFF
311 // Number of session lists in the MM_SESSIONS_SPACE structure
313 #if defined(_M_AMD64)
314 #define SESSION_POOL_LOOKASIDES 21
315 #elif defined(_M_IX86)
316 #define SESSION_POOL_LOOKASIDES 26
317 #elif defined(_M_ARM)
318 #define SESSION_POOL_LOOKASIDES 26 // CHECKME
324 // Number of session data and tag pages
326 #define MI_SESSION_DATA_PAGES_MAXIMUM (MM_ALLOCATION_GRANULARITY / PAGE_SIZE)
327 #define MI_SESSION_TAG_PAGES_MAXIMUM (MM_ALLOCATION_GRANULARITY / PAGE_SIZE)
330 // Used by MiCheckSecuredVad
332 #define MM_READ_WRITE_ALLOWED 11
333 #define MM_READ_ONLY_ALLOWED 10
334 #define MM_NO_ACCESS_ALLOWED 01
335 #define MM_DELETE_CHECK 85
338 // System views are binned into 64K chunks
340 #define MI_SYSTEM_VIEW_BUCKET_SIZE _64K
343 // FIXFIX: These should go in ex.h after the pool merge
346 #define POOL_BLOCK_SIZE 16
348 #define POOL_BLOCK_SIZE 8
350 #define POOL_LISTS_PER_PAGE (PAGE_SIZE / POOL_BLOCK_SIZE)
351 #define BASE_POOL_TYPE_MASK 1
352 #define POOL_MAX_ALLOC (PAGE_SIZE - (sizeof(POOL_HEADER) + POOL_BLOCK_SIZE))
355 // Pool debugging/analysis/tracing flags
357 #define POOL_FLAG_CHECK_TIMERS 0x1
358 #define POOL_FLAG_CHECK_WORKERS 0x2
359 #define POOL_FLAG_CHECK_RESOURCES 0x4
360 #define POOL_FLAG_VERIFIER 0x8
361 #define POOL_FLAG_CHECK_DEADLOCK 0x10
362 #define POOL_FLAG_SPECIAL_POOL 0x20
363 #define POOL_FLAG_DBGPRINT_ON_FAILURE 0x40
364 #define POOL_FLAG_CRASH_ON_FAILURE 0x80
367 // BAD_POOL_HEADER codes during pool bugcheck
369 #define POOL_CORRUPTED_LIST 3
370 #define POOL_SIZE_OR_INDEX_MISMATCH 5
371 #define POOL_ENTRIES_NOT_ALIGNED_PREVIOUS 6
372 #define POOL_HEADER_NOT_ALIGNED 7
373 #define POOL_HEADER_IS_ZERO 8
374 #define POOL_ENTRIES_NOT_ALIGNED_NEXT 9
375 #define POOL_ENTRY_NOT_FOUND 10
378 // BAD_POOL_CALLER codes during pool bugcheck
380 #define POOL_ENTRY_CORRUPTED 1
381 #define POOL_ENTRY_ALREADY_FREE 6
382 #define POOL_ENTRY_NOT_ALLOCATED 7
383 #define POOL_ALLOC_IRQL_INVALID 8
384 #define POOL_FREE_IRQL_INVALID 9
385 #define POOL_BILLED_PROCESS_INVALID 13
386 #define POOL_HEADER_SIZE_INVALID 32
389 #define MiPdeToPte(PDE) ((PMMPTE)MiPteToAddress(PDE))
393 typedef struct _POOL_DESCRIPTOR
398 ULONG RunningDeAllocs
;
404 LONG PendingFreeDepth
;
407 LIST_ENTRY ListHeads
[POOL_LISTS_PER_PAGE
];
408 } POOL_DESCRIPTOR
, *PPOOL_DESCRIPTOR
;
410 typedef struct _POOL_HEADER
417 USHORT PreviousSize
:8;
422 USHORT PreviousSize
:9;
436 PEPROCESS ProcessBilled
;
442 USHORT AllocatorBackTraceIndex
;
446 } POOL_HEADER
, *PPOOL_HEADER
;
448 C_ASSERT(sizeof(POOL_HEADER
) == POOL_BLOCK_SIZE
);
449 C_ASSERT(POOL_BLOCK_SIZE
== sizeof(LIST_ENTRY
));
451 typedef struct _POOL_TRACKER_TABLE
456 SIZE_T NonPagedBytes
;
460 } POOL_TRACKER_TABLE
, *PPOOL_TRACKER_TABLE
;
462 typedef struct _POOL_TRACKER_BIG_PAGES
468 } POOL_TRACKER_BIG_PAGES
, *PPOOL_TRACKER_BIG_PAGES
;
470 extern ULONG ExpNumberOfPagedPools
;
471 extern POOL_DESCRIPTOR NonPagedPoolDescriptor
;
472 extern PPOOL_DESCRIPTOR ExpPagedPoolDescriptor
[16 + 1];
473 extern PPOOL_TRACKER_TABLE PoolTrackTable
;
479 typedef struct _MI_LARGE_PAGE_DRIVER_ENTRY
482 UNICODE_STRING BaseName
;
483 } MI_LARGE_PAGE_DRIVER_ENTRY
, *PMI_LARGE_PAGE_DRIVER_ENTRY
;
485 typedef enum _MMSYSTEM_PTE_POOL_TYPE
488 NonPagedPoolExpansion
,
490 } MMSYSTEM_PTE_POOL_TYPE
;
492 typedef enum _MI_PFN_CACHE_ATTRIBUTE
498 } MI_PFN_CACHE_ATTRIBUTE
, *PMI_PFN_CACHE_ATTRIBUTE
;
500 typedef struct _PHYSICAL_MEMORY_RUN
503 PFN_NUMBER PageCount
;
504 } PHYSICAL_MEMORY_RUN
, *PPHYSICAL_MEMORY_RUN
;
506 typedef struct _PHYSICAL_MEMORY_DESCRIPTOR
509 PFN_NUMBER NumberOfPages
;
510 PHYSICAL_MEMORY_RUN Run
[1];
511 } PHYSICAL_MEMORY_DESCRIPTOR
, *PPHYSICAL_MEMORY_DESCRIPTOR
;
513 typedef struct _MMCOLOR_TABLES
518 } MMCOLOR_TABLES
, *PMMCOLOR_TABLES
;
520 typedef struct _MI_LARGE_PAGE_RANGES
522 PFN_NUMBER StartFrame
;
523 PFN_NUMBER LastFrame
;
524 } MI_LARGE_PAGE_RANGES
, *PMI_LARGE_PAGE_RANGES
;
526 typedef struct _MMVIEW
529 PCONTROL_AREA ControlArea
;
532 typedef struct _MMSESSION
534 KGUARDED_MUTEX SystemSpaceViewLock
;
535 PKGUARDED_MUTEX SystemSpaceViewLockPointer
;
536 PCHAR SystemSpaceViewStart
;
537 PMMVIEW SystemSpaceViewTable
;
538 ULONG SystemSpaceHashSize
;
539 ULONG SystemSpaceHashEntries
;
540 ULONG SystemSpaceHashKey
;
541 ULONG BitmapFailures
;
542 PRTL_BITMAP SystemSpaceBitMap
;
543 } MMSESSION
, *PMMSESSION
;
545 typedef struct _MM_SESSION_SPACE_FLAGS
548 ULONG DeletePending
:1;
550 } MM_SESSION_SPACE_FLAGS
;
552 typedef struct _MM_SESSION_SPACE
554 struct _MM_SESSION_SPACE
*GlobalVirtualAddress
;
559 MM_SESSION_SPACE_FLAGS Flags
;
562 LIST_ENTRY ProcessList
;
563 LARGE_INTEGER LastProcessSwappedOutTime
;
564 PFN_NUMBER SessionPageDirectoryIndex
;
565 SIZE_T NonPageablePages
;
566 SIZE_T CommittedPages
;
567 PVOID PagedPoolStart
;
569 PMMPTE PagedPoolBasePde
;
571 LONG ResidentProcessCount
;
572 ULONG SessionPoolAllocationFailures
[4];
573 LIST_ENTRY ImageList
;
577 PEPROCESS LastProcess
;
578 LONG ProcessReferenceToSession
;
579 LIST_ENTRY WsListEntry
;
580 GENERAL_LOOKASIDE Lookaside
[SESSION_POOL_LOOKASIDES
];
582 KGUARDED_MUTEX PagedPoolMutex
;
583 MM_PAGED_POOL_INFO PagedPoolInfo
;
586 PDRIVER_UNLOAD Win32KDriverUnload
;
587 POOL_DESCRIPTOR PagedPool
;
588 #if defined (_M_AMD64)
593 #if defined (_M_AMD64)
594 PMMPTE SpecialPoolFirstPte
;
595 PMMPTE SpecialPoolLastPte
;
596 PMMPTE NextPdeForSpecialPoolExpansion
;
597 PMMPTE LastPdeForSpecialPoolExpansion
;
598 PFN_NUMBER SpecialPagesInUse
;
600 LONG ImageLoadingCount
;
601 } MM_SESSION_SPACE
, *PMM_SESSION_SPACE
;
603 extern PMM_SESSION_SPACE MmSessionSpace
;
604 extern MMPTE HyperTemplatePte
;
605 extern MMPDE ValidKernelPde
;
606 extern MMPTE ValidKernelPte
;
607 extern MMPDE ValidKernelPdeLocal
;
608 extern MMPTE ValidKernelPteLocal
;
609 extern MMPDE DemandZeroPde
;
610 extern MMPTE DemandZeroPte
;
611 extern MMPTE PrototypePte
;
612 extern MMPTE MmDecommittedPte
;
613 extern BOOLEAN MmLargeSystemCache
;
614 extern BOOLEAN MmZeroPageFile
;
615 extern BOOLEAN MmProtectFreedNonPagedPool
;
616 extern BOOLEAN MmTrackLockedPages
;
617 extern BOOLEAN MmTrackPtes
;
618 extern BOOLEAN MmDynamicPfn
;
619 extern BOOLEAN MmMirroring
;
620 extern BOOLEAN MmMakeLowMemory
;
621 extern BOOLEAN MmEnforceWriteProtection
;
622 extern SIZE_T MmAllocationFragment
;
623 extern ULONG MmConsumedPoolPercentage
;
624 extern ULONG MmVerifyDriverBufferType
;
625 extern ULONG MmVerifyDriverLevel
;
626 extern WCHAR MmVerifyDriverBuffer
[512];
627 extern WCHAR MmLargePageDriverBuffer
[512];
628 extern LIST_ENTRY MiLargePageDriverList
;
629 extern BOOLEAN MiLargePageAllDrivers
;
630 extern ULONG MmVerifyDriverBufferLength
;
631 extern ULONG MmLargePageDriverBufferLength
;
632 extern SIZE_T MmSizeOfNonPagedPoolInBytes
;
633 extern SIZE_T MmMaximumNonPagedPoolInBytes
;
634 extern PFN_NUMBER MmMaximumNonPagedPoolInPages
;
635 extern PFN_NUMBER MmSizeOfPagedPoolInPages
;
636 extern PVOID MmNonPagedSystemStart
;
637 extern SIZE_T MiNonPagedSystemSize
;
638 extern PVOID MmNonPagedPoolStart
;
639 extern PVOID MmNonPagedPoolExpansionStart
;
640 extern PVOID MmNonPagedPoolEnd
;
641 extern SIZE_T MmSizeOfPagedPoolInBytes
;
642 extern PVOID MmPagedPoolStart
;
643 extern PVOID MmPagedPoolEnd
;
644 extern PVOID MmSessionBase
;
645 extern SIZE_T MmSessionSize
;
646 extern PMMPTE MmFirstReservedMappingPte
, MmLastReservedMappingPte
;
647 extern PMMPTE MiFirstReservedZeroingPte
;
648 extern MI_PFN_CACHE_ATTRIBUTE MiPlatformCacheAttributes
[2][MmMaximumCacheType
];
649 extern PPHYSICAL_MEMORY_DESCRIPTOR MmPhysicalMemoryBlock
;
650 extern SIZE_T MmBootImageSize
;
651 extern PMMPTE MmSystemPtesStart
[MaximumPtePoolTypes
];
652 extern PMMPTE MmSystemPtesEnd
[MaximumPtePoolTypes
];
653 extern PMEMORY_ALLOCATION_DESCRIPTOR MxFreeDescriptor
;
654 extern MEMORY_ALLOCATION_DESCRIPTOR MxOldFreeDescriptor
;
655 extern ULONG_PTR MxPfnAllocation
;
656 extern MM_PAGED_POOL_INFO MmPagedPoolInfo
;
657 extern RTL_BITMAP MiPfnBitMap
;
658 extern KGUARDED_MUTEX MmPagedPoolMutex
;
659 extern KGUARDED_MUTEX MmSectionCommitMutex
;
660 extern PVOID MmPagedPoolStart
;
661 extern PVOID MmPagedPoolEnd
;
662 extern PVOID MmNonPagedSystemStart
;
663 extern PVOID MiSystemViewStart
;
664 extern SIZE_T MmSystemViewSize
;
665 extern PVOID MmSessionBase
;
666 extern PVOID MiSessionSpaceEnd
;
667 extern PMMPTE MiSessionImagePteStart
;
668 extern PMMPTE MiSessionImagePteEnd
;
669 extern PMMPTE MiSessionBasePte
;
670 extern PMMPTE MiSessionLastPte
;
671 extern SIZE_T MmSizeOfPagedPoolInBytes
;
672 extern PMMPDE MmSystemPagePtes
;
673 extern PVOID MmSystemCacheStart
;
674 extern PVOID MmSystemCacheEnd
;
675 extern MMSUPPORT MmSystemCacheWs
;
676 extern SIZE_T MmAllocatedNonPagedPool
;
677 extern ULONG MmSpecialPoolTag
;
678 extern PVOID MmHyperSpaceEnd
;
679 extern PMMWSL MmSystemCacheWorkingSetList
;
680 extern SIZE_T MmMinimumNonPagedPoolSize
;
681 extern ULONG MmMinAdditionNonPagedPoolPerMb
;
682 extern SIZE_T MmDefaultMaximumNonPagedPool
;
683 extern ULONG MmMaxAdditionNonPagedPoolPerMb
;
684 extern ULONG MmSecondaryColors
;
685 extern ULONG MmSecondaryColorMask
;
686 extern ULONG MmNumberOfSystemPtes
;
687 extern ULONG MmMaximumNonPagedPoolPercent
;
688 extern ULONG MmLargeStackSize
;
689 extern PMMCOLOR_TABLES MmFreePagesByColor
[FreePageList
+ 1];
690 extern MMPFNLIST MmStandbyPageListByPriority
[8];
691 extern ULONG MmProductType
;
692 extern MM_SYSTEMSIZE MmSystemSize
;
693 extern PKEVENT MiLowMemoryEvent
;
694 extern PKEVENT MiHighMemoryEvent
;
695 extern PKEVENT MiLowPagedPoolEvent
;
696 extern PKEVENT MiHighPagedPoolEvent
;
697 extern PKEVENT MiLowNonPagedPoolEvent
;
698 extern PKEVENT MiHighNonPagedPoolEvent
;
699 extern PFN_NUMBER MmLowMemoryThreshold
;
700 extern PFN_NUMBER MmHighMemoryThreshold
;
701 extern PFN_NUMBER MiLowPagedPoolThreshold
;
702 extern PFN_NUMBER MiHighPagedPoolThreshold
;
703 extern PFN_NUMBER MiLowNonPagedPoolThreshold
;
704 extern PFN_NUMBER MiHighNonPagedPoolThreshold
;
705 extern PFN_NUMBER MmMinimumFreePages
;
706 extern PFN_NUMBER MmPlentyFreePages
;
707 extern SIZE_T MmMinimumStackCommitInBytes
;
708 extern PFN_COUNT MiExpansionPoolPagesInitialCharge
;
709 extern PFN_NUMBER MmResidentAvailablePages
;
710 extern PFN_NUMBER MmResidentAvailableAtInit
;
711 extern ULONG MmTotalFreeSystemPtes
[MaximumPtePoolTypes
];
712 extern PFN_NUMBER MmTotalSystemDriverPages
;
713 extern ULONG MmCritsectTimeoutSeconds
;
714 extern PVOID MiSessionImageStart
;
715 extern PVOID MiSessionImageEnd
;
716 extern PMMPTE MiHighestUserPte
;
717 extern PMMPDE MiHighestUserPde
;
718 extern PFN_NUMBER MmSystemPageDirectory
[PD_COUNT
];
719 extern PMMPTE MmSharedUserDataPte
;
720 extern LIST_ENTRY MmProcessList
;
721 extern BOOLEAN MmZeroingPageThreadActive
;
722 extern KEVENT MmZeroingPageEvent
;
723 extern ULONG MmSystemPageColor
;
724 extern ULONG MmProcessColorSeed
;
725 extern PMMWSL MmWorkingSetList
;
726 extern PFN_NUMBER MiNumberOfFreePages
;
727 extern SIZE_T MmSessionViewSize
;
728 extern SIZE_T MmSessionPoolSize
;
729 extern SIZE_T MmSessionImageSize
;
730 extern PVOID MiSystemViewStart
;
731 extern PVOID MiSessionPoolEnd
; // 0xBE000000
732 extern PVOID MiSessionPoolStart
; // 0xBD000000
733 extern PVOID MiSessionViewStart
; // 0xBE000000
734 extern PVOID MiSessionSpaceWs
;
735 extern ULONG MmMaximumDeadKernelStacks
;
736 extern SLIST_HEADER MmDeadStackSListHead
;
737 extern MM_AVL_TABLE MmSectionBasedRoot
;
738 extern KGUARDED_MUTEX MmSectionBasedMutex
;
739 extern PVOID MmHighSectionBase
;
740 extern SIZE_T MmSystemLockPagesCount
;
741 extern ULONG_PTR MmSubsectionBase
;
742 extern LARGE_INTEGER MmCriticalSectionTimeout
;
743 extern LIST_ENTRY MmWorkingSetExpansionHead
;
747 MiIsMemoryTypeFree(TYPE_OF_MEMORY MemoryType
)
749 return ((MemoryType
== LoaderFree
) ||
750 (MemoryType
== LoaderLoadedProgram
) ||
751 (MemoryType
== LoaderFirmwareTemporary
) ||
752 (MemoryType
== LoaderOsloaderStack
));
757 MiIsMemoryTypeInvisible(TYPE_OF_MEMORY MemoryType
)
759 return ((MemoryType
== LoaderFirmwarePermanent
) ||
760 (MemoryType
== LoaderSpecialMemory
) ||
761 (MemoryType
== LoaderHALCachedMemory
) ||
762 (MemoryType
== LoaderBBTMemory
));
768 MiIsUserPxe(PVOID Address
)
770 return ((ULONG_PTR
)Address
>> 7) == 0x1FFFFEDF6FB7DA0ULL
;
775 MiIsUserPpe(PVOID Address
)
777 return ((ULONG_PTR
)Address
>> 16) == 0xFFFFF6FB7DA0ULL
;
782 MiIsUserPde(PVOID Address
)
784 return ((ULONG_PTR
)Address
>> 25) == 0x7FFFFB7DA0ULL
;
789 MiIsUserPte(PVOID Address
)
791 return ((ULONG_PTR
)Address
>> 34) == 0x3FFFFDA0ULL
;
796 MiIsUserPde(PVOID Address
)
798 return ((Address
>= (PVOID
)MiAddressToPde(NULL
)) &&
799 (Address
<= (PVOID
)MiHighestUserPde
));
804 MiIsUserPte(PVOID Address
)
806 return (Address
<= (PVOID
)MiHighestUserPte
);
811 // Figures out the hardware bits for a PTE
815 MiDetermineUserGlobalPteMask(IN PVOID PointerPte
)
822 /* Make it valid and accessed */
823 TempPte
.u
.Hard
.Valid
= TRUE
;
824 MI_MAKE_ACCESSED_PAGE(&TempPte
);
826 /* Is this for user-mode? */
828 #if (_MI_PAGING_LEVELS == 4)
829 MiIsUserPxe(PointerPte
) ||
831 #if (_MI_PAGING_LEVELS >= 3)
832 MiIsUserPpe(PointerPte
) ||
834 MiIsUserPde(PointerPte
) ||
835 MiIsUserPte(PointerPte
))
837 /* Set the owner bit */
838 MI_MAKE_OWNER_PAGE(&TempPte
);
841 /* FIXME: We should also set the global bit */
843 /* Return the protection */
844 return TempPte
.u
.Long
;
848 // Creates a valid kernel PTE with the given protection
852 MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte
,
853 IN PMMPTE MappingPte
,
854 IN ULONG_PTR ProtectionMask
,
855 IN PFN_NUMBER PageFrameNumber
)
857 /* Only valid for kernel, non-session PTEs */
858 ASSERT(MappingPte
> MiHighestUserPte
);
859 ASSERT(!MI_IS_SESSION_PTE(MappingPte
));
860 ASSERT((MappingPte
< (PMMPTE
)PDE_BASE
) || (MappingPte
> (PMMPTE
)PDE_TOP
));
863 *NewPte
= ValidKernelPte
;
865 /* Set the protection and page */
866 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
867 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
871 // Creates a valid PTE with the given protection
875 MI_MAKE_HARDWARE_PTE(IN PMMPTE NewPte
,
876 IN PMMPTE MappingPte
,
877 IN ULONG_PTR ProtectionMask
,
878 IN PFN_NUMBER PageFrameNumber
)
880 /* Set the protection and page */
881 NewPte
->u
.Long
= MiDetermineUserGlobalPteMask(MappingPte
);
882 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
883 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
887 // Creates a valid user PTE with the given protection
891 MI_MAKE_HARDWARE_PTE_USER(IN PMMPTE NewPte
,
892 IN PMMPTE MappingPte
,
893 IN ULONG_PTR ProtectionMask
,
894 IN PFN_NUMBER PageFrameNumber
)
896 /* Only valid for kernel, non-session PTEs */
897 ASSERT(MappingPte
<= MiHighestUserPte
);
902 /* Set the protection and page */
903 NewPte
->u
.Hard
.Valid
= TRUE
;
904 NewPte
->u
.Hard
.Owner
= TRUE
;
905 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
906 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
911 // Builds a Prototype PTE for the address of the PTE
915 MI_MAKE_PROTOTYPE_PTE(IN PMMPTE NewPte
,
916 IN PMMPTE PointerPte
)
920 /* Mark this as a prototype */
922 NewPte
->u
.Proto
.Prototype
= 1;
925 * Prototype PTEs are only valid in paged pool by design, this little trick
926 * lets us only use 30 bits for the adress of the PTE, as long as the area
927 * stays 1024MB At most.
929 Offset
= (ULONG_PTR
)PointerPte
- (ULONG_PTR
)MmPagedPoolStart
;
932 * 7 bits go in the "low" (but we assume the bottom 2 are zero)
933 * and the other 21 bits go in the "high"
935 NewPte
->u
.Proto
.ProtoAddressLow
= (Offset
& 0x1FC) >> 2;
936 NewPte
->u
.Proto
.ProtoAddressHigh
= (Offset
& 0x3FFFFE00) >> 9;
940 // Builds a Subsection PTE for the address of the Segment
944 MI_MAKE_SUBSECTION_PTE(IN PMMPTE NewPte
,
949 /* Mark this as a prototype */
951 NewPte
->u
.Subsect
.Prototype
= 1;
954 * Segments are only valid either in nonpaged pool. We store the 20 bit
955 * difference either from the top or bottom of nonpaged pool, giving a
956 * maximum of 128MB to each delta, meaning nonpaged pool cannot exceed
959 if ((ULONG_PTR
)Segment
< ((ULONG_PTR
)MmSubsectionBase
+ (128 * _1MB
)))
961 Offset
= (ULONG_PTR
)Segment
- (ULONG_PTR
)MmSubsectionBase
;
962 NewPte
->u
.Subsect
.WhichPool
= PagedPool
;
966 Offset
= (ULONG_PTR
)MmNonPagedPoolEnd
- (ULONG_PTR
)Segment
;
967 NewPte
->u
.Subsect
.WhichPool
= NonPagedPool
;
971 * 4 bits go in the "low" (but we assume the bottom 3 are zero)
972 * and the other 20 bits go in the "high"
974 NewPte
->u
.Subsect
.SubsectionAddressLow
= (Offset
& 0x78) >> 3;
975 NewPte
->u
.Subsect
.SubsectionAddressHigh
= (Offset
& 0xFFFFF80) >> 7;
980 MI_IS_MAPPED_PTE(PMMPTE PointerPte
)
982 /// \todo Make this reasonable code, this is UGLY!
983 return ((PointerPte
->u
.Long
& 0xFFFFFC01) != 0);
989 // Returns if the page is physically resident (ie: a large page)
990 // FIXFIX: CISC/x86 only?
994 MI_IS_PHYSICAL_ADDRESS(IN PVOID Address
)
998 /* Large pages are never paged out, always physically resident */
999 PointerPde
= MiAddressToPde(Address
);
1000 return ((PointerPde
->u
.Hard
.LargePage
) && (PointerPde
->u
.Hard
.Valid
));
1004 // Writes a valid PTE
1008 MI_WRITE_VALID_PTE(IN PMMPTE PointerPte
,
1011 /* Write the valid PTE */
1012 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
1013 ASSERT(TempPte
.u
.Hard
.Valid
== 1);
1014 *PointerPte
= TempPte
;
1018 // Updates a valid PTE
1022 MI_UPDATE_VALID_PTE(IN PMMPTE PointerPte
,
1025 /* Write the valid PTE */
1026 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
1027 ASSERT(TempPte
.u
.Hard
.Valid
== 1);
1028 ASSERT(PointerPte
->u
.Hard
.PageFrameNumber
== TempPte
.u
.Hard
.PageFrameNumber
);
1029 *PointerPte
= TempPte
;
1033 // Writes an invalid PTE
1037 MI_WRITE_INVALID_PTE(IN PMMPTE PointerPte
,
1038 IN MMPTE InvalidPte
)
1040 /* Write the invalid PTE */
1041 ASSERT(InvalidPte
.u
.Hard
.Valid
== 0);
1042 ASSERT(InvalidPte
.u
.Long
!= 0);
1043 *PointerPte
= InvalidPte
;
1047 // Erase the PTE completely
1051 MI_ERASE_PTE(IN PMMPTE PointerPte
)
1053 /* Zero out the PTE */
1054 ASSERT(PointerPte
->u
.Long
!= 0);
1055 PointerPte
->u
.Long
= 0;
1059 // Writes a valid PDE
1063 MI_WRITE_VALID_PDE(IN PMMPDE PointerPde
,
1066 /* Write the valid PDE */
1067 ASSERT(PointerPde
->u
.Hard
.Valid
== 0);
1068 ASSERT(TempPde
.u
.Hard
.Valid
== 1);
1069 *PointerPde
= TempPde
;
1073 // Writes an invalid PDE
1077 MI_WRITE_INVALID_PDE(IN PMMPDE PointerPde
,
1078 IN MMPDE InvalidPde
)
1080 /* Write the invalid PDE */
1081 ASSERT(InvalidPde
.u
.Hard
.Valid
== 0);
1082 ASSERT(InvalidPde
.u
.Long
!= 0);
1083 *PointerPde
= InvalidPde
;
1087 // Checks if the thread already owns a working set
1091 MM_ANY_WS_LOCK_HELD(IN PETHREAD Thread
)
1093 /* If any of these are held, return TRUE */
1094 return ((Thread
->OwnsProcessWorkingSetExclusive
) ||
1095 (Thread
->OwnsProcessWorkingSetShared
) ||
1096 (Thread
->OwnsSystemWorkingSetExclusive
) ||
1097 (Thread
->OwnsSystemWorkingSetShared
) ||
1098 (Thread
->OwnsSessionWorkingSetExclusive
) ||
1099 (Thread
->OwnsSessionWorkingSetShared
));
1103 // Checks if the process owns the working set lock
1107 MI_WS_OWNER(IN PEPROCESS Process
)
1109 /* Check if this process is the owner, and that the thread owns the WS */
1110 if (PsGetCurrentThread()->OwnsProcessWorkingSetExclusive
== 0)
1112 DPRINT("Thread: %p is not an owner\n", PsGetCurrentThread());
1114 if (KeGetCurrentThread()->ApcState
.Process
!= &Process
->Pcb
)
1116 DPRINT("Current thread %p is attached to another process %p\n", PsGetCurrentThread(), Process
);
1118 return ((KeGetCurrentThread()->ApcState
.Process
== &Process
->Pcb
) &&
1119 ((PsGetCurrentThread()->OwnsProcessWorkingSetExclusive
) ||
1120 (PsGetCurrentThread()->OwnsProcessWorkingSetShared
)));
1124 // New ARM3<->RosMM PAGE Architecture
1128 MiIsRosSectionObject(IN PVOID Section
)
1130 PROS_SECTION_OBJECT RosSection
= Section
;
1131 if ((RosSection
->Type
== 'SC') && (RosSection
->Size
== 'TN')) return TRUE
;
1135 #define MI_IS_ROS_PFN(x) ((x)->u4.AweAllocation == TRUE)
1139 MiDecrementReferenceCount(
1141 IN PFN_NUMBER PageFrameIndex
1146 MI_IS_WS_UNSAFE(IN PEPROCESS Process
)
1148 return (Process
->Vm
.Flags
.AcquiredUnsafe
== TRUE
);
1152 // Locks the working set for the given process
1156 MiLockProcessWorkingSet(IN PEPROCESS Process
,
1159 /* Shouldn't already be owning the process working set */
1160 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1161 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1163 /* Block APCs, make sure that still nothing is already held */
1164 KeEnterGuardedRegion();
1165 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1167 /* Lock the working set */
1168 ExAcquirePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1170 /* Now claim that we own the lock */
1171 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1172 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1173 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1178 MiLockProcessWorkingSetShared(IN PEPROCESS Process
,
1181 /* Shouldn't already be owning the process working set */
1182 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1183 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1185 /* Block APCs, make sure that still nothing is already held */
1186 KeEnterGuardedRegion();
1187 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1189 /* Lock the working set */
1190 ExAcquirePushLockShared(&Process
->Vm
.WorkingSetMutex
);
1192 /* Now claim that we own the lock */
1193 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1194 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1195 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1196 Thread
->OwnsProcessWorkingSetShared
= TRUE
;
1201 MiLockProcessWorkingSetUnsafe(IN PEPROCESS Process
,
1204 /* Shouldn't already be owning the process working set */
1205 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1207 /* APCs must be blocked, make sure that still nothing is already held */
1208 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1209 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1211 /* Lock the working set */
1212 ExAcquirePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1214 /* Now claim that we own the lock */
1215 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1216 Process
->Vm
.Flags
.AcquiredUnsafe
= 1;
1217 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1218 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1222 // Unlocks the working set for the given process
1226 MiUnlockProcessWorkingSet(IN PEPROCESS Process
,
1229 /* Make sure we are the owner of a safe acquisition */
1230 ASSERT(MI_WS_OWNER(Process
));
1231 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1233 /* The thread doesn't own it anymore */
1234 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== TRUE
);
1235 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1237 /* Release the lock and re-enable APCs */
1238 ExReleasePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1239 KeLeaveGuardedRegion();
1243 // Unlocks the working set for the given process
1247 MiUnlockProcessWorkingSetShared(IN PEPROCESS Process
,
1250 /* Make sure we are the owner of a safe acquisition (because shared) */
1251 ASSERT(MI_WS_OWNER(Process
));
1252 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1254 /* Ensure we are in a shared acquisition */
1255 ASSERT(Thread
->OwnsProcessWorkingSetShared
== TRUE
);
1256 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1258 /* Don't claim the lock anylonger */
1259 Thread
->OwnsProcessWorkingSetShared
= FALSE
;
1261 /* Release the lock and re-enable APCs */
1262 ExReleasePushLockShared(&Process
->Vm
.WorkingSetMutex
);
1263 KeLeaveGuardedRegion();
1267 // Unlocks the working set for the given process
1271 MiUnlockProcessWorkingSetUnsafe(IN PEPROCESS Process
,
1274 /* Make sure we are the owner of an unsafe acquisition */
1275 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1276 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1277 ASSERT(MI_WS_OWNER(Process
));
1278 ASSERT(MI_IS_WS_UNSAFE(Process
));
1280 /* No longer unsafe */
1281 Process
->Vm
.Flags
.AcquiredUnsafe
= 0;
1283 /* The thread doesn't own it anymore */
1284 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== TRUE
);
1285 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1287 /* Release the lock but don't touch APC state */
1288 ExReleasePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1289 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1293 // Locks the working set
1297 MiLockWorkingSet(IN PETHREAD Thread
,
1298 IN PMMSUPPORT WorkingSet
)
1301 KeEnterGuardedRegion();
1303 /* Working set should be in global memory */
1304 ASSERT(MI_IS_SESSION_ADDRESS((PVOID
)WorkingSet
) == FALSE
);
1306 /* Thread shouldn't already be owning something */
1307 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1309 /* Lock this working set */
1310 ExAcquirePushLockExclusive(&WorkingSet
->WorkingSetMutex
);
1312 /* Which working set is this? */
1313 if (WorkingSet
== &MmSystemCacheWs
)
1315 /* Own the system working set */
1316 ASSERT((Thread
->OwnsSystemWorkingSetExclusive
== FALSE
) &&
1317 (Thread
->OwnsSystemWorkingSetShared
== FALSE
));
1318 Thread
->OwnsSystemWorkingSetExclusive
= TRUE
;
1320 else if (WorkingSet
->Flags
.SessionSpace
)
1322 /* Own the session working set */
1323 ASSERT((Thread
->OwnsSessionWorkingSetExclusive
== FALSE
) &&
1324 (Thread
->OwnsSessionWorkingSetShared
== FALSE
));
1325 Thread
->OwnsSessionWorkingSetExclusive
= TRUE
;
1329 /* Own the process working set */
1330 ASSERT((Thread
->OwnsProcessWorkingSetExclusive
== FALSE
) &&
1331 (Thread
->OwnsProcessWorkingSetShared
== FALSE
));
1332 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1337 // Unlocks the working set
1341 MiUnlockWorkingSet(IN PETHREAD Thread
,
1342 IN PMMSUPPORT WorkingSet
)
1344 /* Working set should be in global memory */
1345 ASSERT(MI_IS_SESSION_ADDRESS((PVOID
)WorkingSet
) == FALSE
);
1347 /* Which working set is this? */
1348 if (WorkingSet
== &MmSystemCacheWs
)
1350 /* Release the system working set */
1351 ASSERT((Thread
->OwnsSystemWorkingSetExclusive
== TRUE
) ||
1352 (Thread
->OwnsSystemWorkingSetShared
== TRUE
));
1353 Thread
->OwnsSystemWorkingSetExclusive
= FALSE
;
1355 else if (WorkingSet
->Flags
.SessionSpace
)
1357 /* Release the session working set */
1358 ASSERT((Thread
->OwnsSessionWorkingSetExclusive
== TRUE
) ||
1359 (Thread
->OwnsSessionWorkingSetShared
== TRUE
));
1360 Thread
->OwnsSessionWorkingSetExclusive
= 0;
1364 /* Release the process working set */
1365 ASSERT((Thread
->OwnsProcessWorkingSetExclusive
) ||
1366 (Thread
->OwnsProcessWorkingSetShared
));
1367 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1370 /* Release the working set lock */
1371 ExReleasePushLockExclusive(&WorkingSet
->WorkingSetMutex
);
1374 KeLeaveGuardedRegion();
1379 MiUnlockProcessWorkingSetForFault(IN PEPROCESS Process
,
1382 OUT PBOOLEAN Shared
)
1384 ASSERT(MI_WS_OWNER(Process
));
1386 /* Check if the current owner is unsafe */
1387 if (MI_IS_WS_UNSAFE(Process
))
1389 /* Release unsafely */
1390 MiUnlockProcessWorkingSetUnsafe(Process
, Thread
);
1394 else if (Thread
->OwnsProcessWorkingSetExclusive
== 1)
1396 /* Owner is safe and exclusive, release normally */
1397 MiUnlockProcessWorkingSet(Process
, Thread
);
1403 /* Owner is shared (implies safe), release normally */
1404 MiUnlockProcessWorkingSetShared(Process
, Thread
);
1412 MiLockProcessWorkingSetForFault(IN PEPROCESS Process
,
1417 /* Check if this was a safe lock or not */
1422 /* Reacquire safely & shared */
1423 MiLockProcessWorkingSetShared(Process
, Thread
);
1427 /* Reacquire safely */
1428 MiLockProcessWorkingSet(Process
, Thread
);
1433 /* Unsafe lock cannot be shared */
1434 ASSERT(Shared
== FALSE
);
1435 /* Reacquire unsafely */
1436 MiLockProcessWorkingSetUnsafe(Process
, Thread
);
1441 // Returns the ProtoPTE inside a VAD for the given VPN
1445 MI_GET_PROTOTYPE_PTE_FOR_VPN(IN PMMVAD Vad
,
1450 /* Find the offset within the VAD's prototype PTEs */
1451 ProtoPte
= Vad
->FirstPrototypePte
+ (Vpn
- Vad
->StartingVpn
);
1452 ASSERT(ProtoPte
<= Vad
->LastContiguousPte
);
1457 // Returns the PFN Database entry for the given page number
1458 // Warning: This is not necessarily a valid PFN database entry!
1462 MI_PFN_ELEMENT(IN PFN_NUMBER Pfn
)
1465 return &MmPfnDatabase
[Pfn
];
1469 // Drops a locked page without dereferencing it
1473 MiDropLockCount(IN PMMPFN Pfn1
)
1475 /* This page shouldn't be locked, but it should be valid */
1476 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1477 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1479 /* Is this the last reference to the page */
1480 if (Pfn1
->u3
.e2
.ReferenceCount
== 1)
1482 /* It better not be valid */
1483 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1485 /* Is it a prototype PTE? */
1486 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1487 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1489 /* FIXME: We should return commit */
1490 DPRINT1("Not returning commit for prototype PTE\n");
1493 /* Update the counter */
1494 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1499 // Drops a locked page and dereferences it
1503 MiDereferencePfnAndDropLockCount(IN PMMPFN Pfn1
)
1505 USHORT RefCount
, OldRefCount
;
1506 PFN_NUMBER PageFrameIndex
;
1508 /* Loop while we decrement the page successfully */
1511 /* There should be at least one reference */
1512 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1513 ASSERT(OldRefCount
!= 0);
1515 /* Are we the last one */
1516 if (OldRefCount
== 1)
1518 /* The page shoudln't be shared not active at this point */
1519 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 1);
1520 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1521 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1523 /* Is it a prototype PTE? */
1524 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1525 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1527 /* FIXME: We should return commit */
1528 DPRINT1("Not returning commit for prototype PTE\n");
1531 /* Update the counter, and drop a reference the long way */
1532 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1533 PageFrameIndex
= MiGetPfnEntryIndex(Pfn1
);
1534 MiDecrementReferenceCount(Pfn1
, PageFrameIndex
);
1538 /* Drop a reference the short way, and that's it */
1539 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1542 ASSERT(RefCount
!= 0);
1543 } while (OldRefCount
!= RefCount
);
1545 /* If we got here, there should be more than one reference */
1546 ASSERT(RefCount
> 1);
1549 /* Is it still being shared? */
1550 if (Pfn1
->u2
.ShareCount
>= 1)
1552 /* Then it should be valid */
1553 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1555 /* Is it a prototype PTE? */
1556 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1557 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1559 /* We don't handle ethis */
1563 /* Update the counter */
1564 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1570 // References a locked page and updates the counter
1571 // Used in MmProbeAndLockPages to handle different edge cases
1575 MiReferenceProbedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1577 USHORT RefCount
, OldRefCount
;
1580 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1582 /* Does ARM3 own the page? */
1583 if (MI_IS_ROS_PFN(Pfn1
))
1585 /* ReactOS Mm doesn't track share count */
1586 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1590 /* On ARM3 pages, we should see a valid share count */
1591 ASSERT((Pfn1
->u2
.ShareCount
!= 0) && (Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
));
1593 /* Is it a prototype PTE? */
1594 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1595 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1597 /* FIXME: We should charge commit */
1598 DPRINT1("Not charging commit for prototype PTE\n");
1602 /* More locked pages! */
1603 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1605 /* Loop trying to update the reference count */
1608 /* Get the current reference count, make sure it's valid */
1609 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1610 ASSERT(OldRefCount
!= 0);
1611 ASSERT(OldRefCount
< 2500);
1613 /* Bump it up by one */
1614 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1617 ASSERT(RefCount
!= 0);
1618 } while (OldRefCount
!= RefCount
);
1620 /* Was this the first lock attempt? If not, undo our bump */
1621 if (OldRefCount
!= 1) InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1625 // References a locked page and updates the counter
1626 // Used in all other cases except MmProbeAndLockPages
1630 MiReferenceUsedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1634 /* Is it a prototype PTE? */
1635 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1636 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1638 /* FIXME: We should charge commit */
1639 DPRINT1("Not charging commit for prototype PTE\n");
1642 /* More locked pages! */
1643 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1645 /* Update the reference count */
1646 NewRefCount
= InterlockedIncrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1647 if (NewRefCount
== 2)
1649 /* Is it locked or shared? */
1650 if (Pfn1
->u2
.ShareCount
)
1652 /* It's shared, so make sure it's active */
1653 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1657 /* It's locked, so we shouldn't lock again */
1658 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1663 /* Someone had already locked the page, so undo our bump */
1664 ASSERT(NewRefCount
< 2500);
1665 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1670 // References a locked page and updates the counter
1671 // Used in all other cases except MmProbeAndLockPages
1675 MiReferenceUnusedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1679 /* Make sure the page isn't used yet */
1680 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1681 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1683 /* Is it a prototype PTE? */
1684 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1685 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1687 /* FIXME: We should charge commit */
1688 DPRINT1("Not charging commit for prototype PTE\n");
1691 /* More locked pages! */
1692 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1694 /* Update the reference count */
1695 NewRefCount
= InterlockedIncrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1696 if (NewRefCount
!= 1)
1698 /* Someone had already locked the page, so undo our bump */
1699 ASSERT(NewRefCount
< 2500);
1700 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1706 MiIncrementPageTableReferences(IN PVOID Address
)
1710 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1713 ASSERT(*RefCount
<= PTE_PER_PAGE
);
1718 MiDecrementPageTableReferences(IN PVOID Address
)
1722 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1725 ASSERT(*RefCount
< PTE_PER_PAGE
);
1730 MiQueryPageTableReferences(IN PVOID Address
)
1734 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1743 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1748 MiInitializeSessionSpaceLayout();
1752 MiInitMachineDependent(
1753 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1758 MiComputeColorInformation(
1765 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1770 MiInitializeColorTables(
1776 MiInitializePfnDatabase(
1777 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1782 MiInitializeSessionWsSupport(
1788 MiInitializeSessionIds(
1794 MiInitializeMemoryEvents(
1801 IN PFN_NUMBER PageCount
1804 PPHYSICAL_MEMORY_DESCRIPTOR
1806 MmInitializeMemoryLimits(
1807 IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
1808 IN PBOOLEAN IncludeType
1813 MiPagesInLoaderBlock(
1814 IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
1815 IN PBOOLEAN IncludeType
1821 IN PVOID AddressStart
,
1827 MiRosProtectVirtualMemory(
1828 IN PEPROCESS Process
,
1829 IN OUT PVOID
*BaseAddress
,
1830 IN OUT PSIZE_T NumberOfBytesToProtect
,
1831 IN ULONG NewAccessProtection
,
1832 OUT PULONG OldAccessProtection OPTIONAL
1838 IN BOOLEAN StoreInstruction
,
1840 IN KPROCESSOR_MODE Mode
,
1841 IN PVOID TrapInformation
1846 MiCheckPdeForPagedPool(
1852 MiInitializeNonPagedPool(
1858 MiInitializeNonPagedPoolThresholds(
1864 MiInitializePoolEvents(
1871 IN POOL_TYPE PoolType
,// FIXFIX: This should go in ex.h after the pool merge
1872 IN ULONG Threshold
//
1875 // FIXFIX: THIS ONE TOO
1879 ExInitializePoolDescriptor(
1880 IN PPOOL_DESCRIPTOR PoolDescriptor
,
1881 IN POOL_TYPE PoolType
,
1889 MiInitializeSessionPool(
1895 MiInitializeSystemPtes(
1896 IN PMMPTE StartingPte
,
1897 IN ULONG NumberOfPtes
,
1898 IN MMSYSTEM_PTE_POOL_TYPE PoolType
1903 MiReserveSystemPtes(
1904 IN ULONG NumberOfPtes
,
1905 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1910 MiReleaseSystemPtes(
1911 IN PMMPTE StartingPte
,
1912 IN ULONG NumberOfPtes
,
1913 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1919 MiFindContiguousPages(
1920 IN PFN_NUMBER LowestPfn
,
1921 IN PFN_NUMBER HighestPfn
,
1922 IN PFN_NUMBER BoundaryPfn
,
1923 IN PFN_NUMBER SizeInPages
,
1924 IN MEMORY_CACHING_TYPE CacheType
1929 MiCheckForContiguousMemory(
1930 IN PVOID BaseAddress
,
1931 IN PFN_NUMBER BaseAddressPages
,
1932 IN PFN_NUMBER SizeInPages
,
1933 IN PFN_NUMBER LowestPfn
,
1934 IN PFN_NUMBER HighestPfn
,
1935 IN PFN_NUMBER BoundaryPfn
,
1936 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
1941 MiAllocatePagesForMdl(
1942 IN PHYSICAL_ADDRESS LowAddress
,
1943 IN PHYSICAL_ADDRESS HighAddress
,
1944 IN PHYSICAL_ADDRESS SkipBytes
,
1945 IN SIZE_T TotalBytes
,
1946 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
,
1952 MiMapLockedPagesInUserSpace(
1955 IN MEMORY_CACHING_TYPE CacheType
,
1956 IN PVOID BaseAddress
1961 MiUnmapLockedPagesInUserSpace(
1962 IN PVOID BaseAddress
,
1969 IN PMMPFNLIST ListHead
,
1970 IN PFN_NUMBER PageFrameIndex
1975 MiUnlinkFreeOrZeroedPage(
1981 MiUnlinkPageFromList(
1988 IN PFN_NUMBER PageFrameIndex
,
1989 IN PMMPTE PointerPte
,
1995 MiInitializeAndChargePfn(
1996 OUT PPFN_NUMBER PageFrameIndex
,
1997 IN PMMPTE PointerPde
,
1998 IN PFN_NUMBER ContainingPageFrame
,
1999 IN BOOLEAN SessionAllocation
2004 MiInitializePfnAndMakePteValid(
2005 IN PFN_NUMBER PageFrameIndex
,
2006 IN PMMPTE PointerPte
,
2012 MiInitializePfnForOtherProcess(
2013 IN PFN_NUMBER PageFrameIndex
,
2014 IN PMMPTE PointerPte
,
2015 IN PFN_NUMBER PteFrame
2020 MiDecrementShareCount(
2022 IN PFN_NUMBER PageFrameIndex
2040 IN PFN_NUMBER PageFrameIndex
2045 MiInsertPageInFreeList(
2046 IN PFN_NUMBER PageFrameIndex
2051 MiDeleteSystemPageableVm(
2052 IN PMMPTE PointerPte
,
2053 IN PFN_NUMBER PageCount
,
2055 OUT PPFN_NUMBER ValidPages
2060 MiGetPageProtection(
2061 IN PMMPTE PointerPte
2064 PLDR_DATA_TABLE_ENTRY
2066 MiLookupDataTableEntry(
2072 MiInitializeDriverLargePageList(
2078 MiInitializeLargePageSupport(
2097 IN PVOID VirtualAddress
2102 MiCheckForConflictingNode(
2103 IN ULONG_PTR StartVpn
,
2104 IN ULONG_PTR EndVpn
,
2105 IN PMM_AVL_TABLE Table
,
2106 OUT PMMADDRESS_NODE
*NodeOrParent
2111 MiFindEmptyAddressRangeDownTree(
2113 IN ULONG_PTR BoundaryAddress
,
2114 IN ULONG_PTR Alignment
,
2115 IN PMM_AVL_TABLE Table
,
2116 OUT PULONG_PTR Base
,
2117 OUT PMMADDRESS_NODE
*Parent
2122 MiFindEmptyAddressRangeDownBasedTree(
2124 IN ULONG_PTR BoundaryAddress
,
2125 IN ULONG_PTR Alignment
,
2126 IN PMM_AVL_TABLE Table
,
2132 MiFindEmptyAddressRangeInTree(
2134 IN ULONG_PTR Alignment
,
2135 IN PMM_AVL_TABLE Table
,
2136 OUT PMMADDRESS_NODE
*PreviousVad
,
2146 IN ULONG ProtectionMask
2153 IN PEPROCESS Process
2160 _In_ ULONG_PTR
*BaseAddress
,
2161 _In_ SIZE_T ViewSize
,
2162 _In_ ULONG_PTR HighestAddress
,
2163 _In_ ULONG_PTR Alignment
,
2164 _In_ ULONG AllocationType
);
2168 MiInsertBasedSection(
2174 MiUnmapViewOfSection(
2175 IN PEPROCESS Process
,
2176 IN PVOID BaseAddress
,
2182 MiRosUnmapViewOfSection(
2183 IN PEPROCESS Process
,
2184 IN PVOID BaseAddress
,
2191 IN PMM_AVL_TABLE Table
,
2192 IN PMMADDRESS_NODE NewNode
,
2193 PMMADDRESS_NODE Parent
,
2194 TABLE_SEARCH_RESULT Result
2200 IN PMMADDRESS_NODE Node
,
2201 IN PMM_AVL_TABLE Table
2207 IN PMMADDRESS_NODE Node
2213 IN PMMADDRESS_NODE Node
2218 MiInitializeSystemSpaceMap(
2219 IN PMMSESSION InputSession OPTIONAL
2224 MiSessionRemoveProcess(
2230 MiReleaseProcessReferenceToSessionDataPage(
2231 IN PMM_SESSION_SPACE SessionGlobal
2236 MiSessionAddProcess(
2237 IN PEPROCESS NewProcess
2242 MiSessionCommitPageTables(
2249 MiMakeProtectionMask(
2255 MiDeleteVirtualAddresses(
2257 IN ULONG_PTR EndingAddress
,
2263 MiMakeSystemAddressValid(
2264 IN PVOID PageTableVirtualAddress
,
2265 IN PEPROCESS CurrentProcess
2270 MiMakeSystemAddressValidPfn(
2271 IN PVOID VirtualAddress
,
2278 IN PEPROCESS CurrentProcess
,
2291 MiDeleteARM3Section(
2297 MiQueryMemorySectionName(
2298 IN HANDLE ProcessHandle
,
2299 IN PVOID BaseAddress
,
2300 OUT PVOID MemoryInformation
,
2301 IN SIZE_T MemoryInformationLength
,
2302 OUT PSIZE_T ReturnLength
2307 MiRosUnmapViewInSystemSpace(
2313 MmDeterminePoolType(
2314 IN PVOID PoolAddress
2319 MiMakePdeExistAndMakeValid(
2320 IN PMMPTE PointerPde
,
2321 IN PEPROCESS TargetProcess
,
2326 // MiRemoveZeroPage will use inline code to zero out the page manually if only
2327 // free pages are available. In some scenarios, we don't/can't run that piece of
2328 // code and would rather only have a real zero page. If we can't have a zero page,
2329 // then we'd like to have our own code to grab a free page and zero it out, by
2330 // using MiRemoveAnyPage. This macro implements this.
2334 MiRemoveZeroPageSafe(IN ULONG Color
)
2336 if (MmFreePagesByColor
[ZeroedPageList
][Color
].Flink
!= LIST_HEAD
) return MiRemoveZeroPage(Color
);