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 (20 * _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_HIGHEST_USER_ADDRESS (PVOID)0x7FFEFFFF
33 #define MI_USER_PROBE_ADDRESS (PVOID)0x7FFF0000
34 #define MI_DEFAULT_SYSTEM_RANGE_START (PVOID)0x80000000
35 #define MI_SYSTEM_CACHE_WS_START (PVOID)0xC0C00000
36 #define MI_PAGED_POOL_START (PVOID)0xE1000000
37 #define MI_NONPAGED_POOL_END (PVOID)0xFFBE0000
38 #define MI_DEBUG_MAPPING (PVOID)0xFFBFF000
40 #define MI_SYSTEM_PTE_BASE (PVOID)MiAddressToPte(NULL)
42 #define MI_MIN_SECONDARY_COLORS 8
43 #define MI_SECONDARY_COLORS 64
44 #define MI_MAX_SECONDARY_COLORS 1024
46 #define MI_MIN_ALLOCATION_FRAGMENT (4 * _1KB)
47 #define MI_ALLOCATION_FRAGMENT (64 * _1KB)
48 #define MI_MAX_ALLOCATION_FRAGMENT (2 * _1MB)
50 #define MM_HIGHEST_VAD_ADDRESS \
51 (PVOID)((ULONG_PTR)MM_HIGHEST_USER_ADDRESS - (16 * PAGE_SIZE))
52 #define MI_LOWEST_VAD_ADDRESS (PVOID)MM_LOWEST_USER_ADDRESS
54 #define MI_DEFAULT_SYSTEM_PTE_COUNT 50000
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)
88 #define PDE_COUNT 4096
91 #define PD_COUNT PPE_PER_PAGE
92 #define PDE_COUNT PDE_PER_PAGE
93 #define PTE_COUNT PTE_PER_PAGE
97 // Protection Bits part of the internal memory manager Protection Mask, from:
98 // http://reactos.org/wiki/Techwiki:Memory_management_in_the_Windows_XP_kernel
99 // and public assertions.
101 #define MM_ZERO_ACCESS 0
102 #define MM_READONLY 1
104 #define MM_EXECUTE_READ 3
105 #define MM_READWRITE 4
106 #define MM_WRITECOPY 5
107 #define MM_EXECUTE_READWRITE 6
108 #define MM_EXECUTE_WRITECOPY 7
110 #define MM_DECOMMIT 0x10
111 #define MM_NOACCESS (MM_DECOMMIT | MM_NOCACHE)
112 #define MM_INVALID_PROTECTION 0xFFFFFFFF
115 // Specific PTE Definitions that map to the Memory Manager's Protection Mask Bits
116 // The Memory Manager's definition define the attributes that must be preserved
117 // and these PTE definitions describe the attributes in the hardware sense. This
118 // helps deal with hardware differences between the actual boolean expression of
121 // For example, in the logical attributes, we want to express read-only as a flag
122 // but on x86, it is writability that must be set. On the other hand, on x86, just
123 // like in the kernel, it is disabling the caches that requires a special flag,
124 // while on certain architectures such as ARM, it is enabling the cache which
127 #if defined(_M_IX86) || defined(_M_AMD64)
131 #define PTE_READONLY 0 // Doesn't exist on x86
132 #define PTE_EXECUTE 0 // Not worrying about NX yet
133 #define PTE_EXECUTE_READ 0 // Not worrying about NX yet
134 #define PTE_READWRITE 0x2
135 #define PTE_WRITECOPY 0x200
136 #define PTE_EXECUTE_READWRITE 0x2 // Not worrying about NX yet
137 #define PTE_EXECUTE_WRITECOPY 0x200
138 #define PTE_PROTOTYPE 0x400
143 #define PTE_VALID 0x1
144 #define PTE_ACCESSED 0x20
145 #define PTE_DIRTY 0x40
150 #define PTE_ENABLE_CACHE 0
151 #define PTE_DISABLE_CACHE 0x10
152 #define PTE_WRITECOMBINED_CACHE 0x10
153 #elif defined(_M_ARM)
154 #define PTE_READONLY 0x200
155 #define PTE_EXECUTE 0 // Not worrying about NX yet
156 #define PTE_EXECUTE_READ 0 // Not worrying about NX yet
157 #define PTE_READWRITE 0 // Doesn't exist on ARM
158 #define PTE_WRITECOPY 0 // Doesn't exist on ARM
159 #define PTE_EXECUTE_READWRITE 0 // Not worrying about NX yet
160 #define PTE_EXECUTE_WRITECOPY 0 // Not worrying about NX yet
161 #define PTE_PROTOTYPE 0x400 // Using the Shared bit
165 #define PTE_ENABLE_CACHE 0
166 #define PTE_DISABLE_CACHE 0x10
167 #define PTE_WRITECOMBINED_CACHE 0x10
169 #error Define these please!
172 extern const ULONG_PTR MmProtectToPteMask
[32];
173 extern const ULONG MmProtectToValue
[32];
176 // Assertions for session images, addresses, and PTEs
178 #define MI_IS_SESSION_IMAGE_ADDRESS(Address) \
179 (((Address) >= MiSessionImageStart) && ((Address) < MiSessionImageEnd))
181 #define MI_IS_SESSION_ADDRESS(Address) \
182 (((Address) >= MmSessionBase) && ((Address) < MiSessionSpaceEnd))
184 #define MI_IS_SESSION_PTE(Pte) \
185 ((((PMMPTE)Pte) >= MiSessionBasePte) && (((PMMPTE)Pte) < MiSessionLastPte))
187 #define MI_IS_PAGE_TABLE_ADDRESS(Address) \
188 (((PVOID)(Address) >= (PVOID)PTE_BASE) && ((PVOID)(Address) <= (PVOID)PTE_TOP))
190 #define MI_IS_SYSTEM_PAGE_TABLE_ADDRESS(Address) \
191 (((Address) >= (PVOID)MiAddressToPte(MmSystemRangeStart)) && ((Address) <= (PVOID)PTE_TOP))
193 #define MI_IS_PAGE_TABLE_OR_HYPER_ADDRESS(Address) \
194 (((PVOID)(Address) >= (PVOID)PTE_BASE) && ((PVOID)(Address) <= (PVOID)MmHyperSpaceEnd))
197 // Corresponds to MMPTE_SOFTWARE.Protection
200 #define MM_PTE_SOFTWARE_PROTECTION_BITS 5
202 #define MM_PTE_SOFTWARE_PROTECTION_BITS 6
204 #define MM_PTE_SOFTWARE_PROTECTION_BITS 5
206 #error Define these please!
210 // Creates a software PTE with the given protection
212 #define MI_MAKE_SOFTWARE_PTE(p, x) ((p)->u.Long = (x << MM_PTE_SOFTWARE_PROTECTION_BITS))
215 // Marks a PTE as deleted
217 #define MI_SET_PFN_DELETED(x) ((x)->PteAddress = (PMMPTE)((ULONG_PTR)(x)->PteAddress | 1))
218 #define MI_IS_PFN_DELETED(x) ((ULONG_PTR)((x)->PteAddress) & 1)
221 // Special values for LoadedImports
223 #define MM_SYSLDR_NO_IMPORTS (PVOID)0xFFFFFFFE
224 #define MM_SYSLDR_BOOT_LOADED (PVOID)0xFFFFFFFF
225 #define MM_SYSLDR_SINGLE_ENTRY 0x1
228 // Number of initial session IDs
230 #define MI_INITIAL_SESSION_IDS 64
232 #if defined(_M_IX86) || defined(_M_ARM)
236 #define LIST_HEAD 0xFFFFFFFF
239 // Because GCC cannot automatically downcast 0xFFFFFFFF to lesser-width bits,
240 // we need a manual definition suited to the number of bits in the PteFrame.
241 // This is used as a LIST_HEAD for the colored list
243 #define COLORED_LIST_HEAD ((1 << 25) - 1) // 0x1FFFFFF
244 #elif defined(_M_AMD64)
245 #define LIST_HEAD 0xFFFFFFFFFFFFFFFFLL
246 #define COLORED_LIST_HEAD ((1ULL << 57) - 1) // 0x1FFFFFFFFFFFFFFLL
248 #error Define these please!
252 // Special IRQL value (found in assertions)
254 #define MM_NOIRQL (KIRQL)0xFFFFFFFF
257 // Returns the color of a page
259 #define MI_GET_PAGE_COLOR(x) ((x) & MmSecondaryColorMask)
260 #define MI_GET_NEXT_COLOR() (MI_GET_PAGE_COLOR(++MmSystemPageColor))
261 #define MI_GET_NEXT_PROCESS_COLOR(x) (MI_GET_PAGE_COLOR(++(x)->NextPageColor))
265 // Decodes a Prototype PTE into the underlying PTE
267 #define MiProtoPteToPte(x) \
268 (PMMPTE)((ULONG_PTR)MmPagedPoolStart + \
269 (((x)->u.Proto.ProtoAddressHigh << 9) | (x)->u.Proto.ProtoAddressLow << 2))
272 // Decodes a Prototype PTE into the underlying PTE
274 #define MiSubsectionPteToSubsection(x) \
275 ((x)->u.Subsect.WhichPool == PagedPool) ? \
276 (PMMPTE)((ULONG_PTR)MmSubsectionBase + \
277 (((x)->u.Subsect.SubsectionAddressHigh << 7) | \
278 (x)->u.Subsect.SubsectionAddressLow << 3)) : \
279 (PMMPTE)((ULONG_PTR)MmNonPagedPoolEnd - \
280 (((x)->u.Subsect.SubsectionAddressHigh << 7) | \
281 (x)->u.Subsect.SubsectionAddressLow << 3))
285 // Prototype PTEs that don't yet have a pagefile association
288 #define MI_PTE_LOOKUP_NEEDED 0xffffffffULL
290 #define MI_PTE_LOOKUP_NEEDED 0xFFFFF
294 // Number of session lists in the MM_SESSIONS_SPACE structure
296 #if defined(_M_AMD64)
297 #define SESSION_POOL_LOOKASIDES 21
298 #elif defined(_M_IX86)
299 #define SESSION_POOL_LOOKASIDES 26
305 // Number of session data and tag pages
307 #define MI_SESSION_DATA_PAGES_MAXIMUM (MM_ALLOCATION_GRANULARITY / PAGE_SIZE)
308 #define MI_SESSION_TAG_PAGES_MAXIMUM (MM_ALLOCATION_GRANULARITY / PAGE_SIZE)
311 // Used by MiCheckSecuredVad
313 #define MM_READ_WRITE_ALLOWED 11
314 #define MM_READ_ONLY_ALLOWED 10
315 #define MM_NO_ACCESS_ALLOWED 01
316 #define MM_DELETE_CHECK 85
319 // System views are binned into 64K chunks
321 #define MI_SYSTEM_VIEW_BUCKET_SIZE _64K
324 // FIXFIX: These should go in ex.h after the pool merge
327 #define POOL_BLOCK_SIZE 16
329 #define POOL_BLOCK_SIZE 8
331 #define POOL_LISTS_PER_PAGE (PAGE_SIZE / POOL_BLOCK_SIZE)
332 #define BASE_POOL_TYPE_MASK 1
333 #define POOL_MAX_ALLOC (PAGE_SIZE - (sizeof(POOL_HEADER) + POOL_BLOCK_SIZE))
336 // Pool debugging/analysis/tracing flags
338 #define POOL_FLAG_CHECK_TIMERS 0x1
339 #define POOL_FLAG_CHECK_WORKERS 0x2
340 #define POOL_FLAG_CHECK_RESOURCES 0x4
341 #define POOL_FLAG_VERIFIER 0x8
342 #define POOL_FLAG_CHECK_DEADLOCK 0x10
343 #define POOL_FLAG_SPECIAL_POOL 0x20
344 #define POOL_FLAG_DBGPRINT_ON_FAILURE 0x40
345 #define POOL_FLAG_CRASH_ON_FAILURE 0x80
348 // BAD_POOL_HEADER codes during pool bugcheck
350 #define POOL_CORRUPTED_LIST 3
351 #define POOL_SIZE_OR_INDEX_MISMATCH 5
352 #define POOL_ENTRIES_NOT_ALIGNED_PREVIOUS 6
353 #define POOL_HEADER_NOT_ALIGNED 7
354 #define POOL_HEADER_IS_ZERO 8
355 #define POOL_ENTRIES_NOT_ALIGNED_NEXT 9
356 #define POOL_ENTRY_NOT_FOUND 10
359 // BAD_POOL_CALLER codes during pool bugcheck
361 #define POOL_ENTRY_CORRUPTED 1
362 #define POOL_ENTRY_ALREADY_FREE 6
363 #define POOL_ENTRY_NOT_ALLOCATED 7
364 #define POOL_ALLOC_IRQL_INVALID 8
365 #define POOL_FREE_IRQL_INVALID 9
366 #define POOL_BILLED_PROCESS_INVALID 13
367 #define POOL_HEADER_SIZE_INVALID 32
369 typedef struct _POOL_DESCRIPTOR
374 ULONG RunningDeAllocs
;
380 LONG PendingFreeDepth
;
383 LIST_ENTRY ListHeads
[POOL_LISTS_PER_PAGE
];
384 } POOL_DESCRIPTOR
, *PPOOL_DESCRIPTOR
;
386 typedef struct _POOL_HEADER
393 USHORT PreviousSize
:8;
398 USHORT PreviousSize
:9;
412 PEPROCESS ProcessBilled
;
418 USHORT AllocatorBackTraceIndex
;
422 } POOL_HEADER
, *PPOOL_HEADER
;
424 C_ASSERT(sizeof(POOL_HEADER
) == POOL_BLOCK_SIZE
);
425 C_ASSERT(POOL_BLOCK_SIZE
== sizeof(LIST_ENTRY
));
427 typedef struct _POOL_TRACKER_TABLE
432 SIZE_T NonPagedBytes
;
436 } POOL_TRACKER_TABLE
, *PPOOL_TRACKER_TABLE
;
438 typedef struct _POOL_TRACKER_BIG_PAGES
444 } POOL_TRACKER_BIG_PAGES
, *PPOOL_TRACKER_BIG_PAGES
;
446 extern ULONG ExpNumberOfPagedPools
;
447 extern POOL_DESCRIPTOR NonPagedPoolDescriptor
;
448 extern PPOOL_DESCRIPTOR ExpPagedPoolDescriptor
[16 + 1];
449 extern PPOOL_TRACKER_TABLE PoolTrackTable
;
455 typedef struct _MI_LARGE_PAGE_DRIVER_ENTRY
458 UNICODE_STRING BaseName
;
459 } MI_LARGE_PAGE_DRIVER_ENTRY
, *PMI_LARGE_PAGE_DRIVER_ENTRY
;
461 typedef enum _MMSYSTEM_PTE_POOL_TYPE
464 NonPagedPoolExpansion
,
466 } MMSYSTEM_PTE_POOL_TYPE
;
468 typedef enum _MI_PFN_CACHE_ATTRIBUTE
474 } MI_PFN_CACHE_ATTRIBUTE
, *PMI_PFN_CACHE_ATTRIBUTE
;
476 typedef struct _PHYSICAL_MEMORY_RUN
479 PFN_NUMBER PageCount
;
480 } PHYSICAL_MEMORY_RUN
, *PPHYSICAL_MEMORY_RUN
;
482 typedef struct _PHYSICAL_MEMORY_DESCRIPTOR
485 PFN_NUMBER NumberOfPages
;
486 PHYSICAL_MEMORY_RUN Run
[1];
487 } PHYSICAL_MEMORY_DESCRIPTOR
, *PPHYSICAL_MEMORY_DESCRIPTOR
;
489 typedef struct _MMCOLOR_TABLES
494 } MMCOLOR_TABLES
, *PMMCOLOR_TABLES
;
496 typedef struct _MI_LARGE_PAGE_RANGES
498 PFN_NUMBER StartFrame
;
499 PFN_NUMBER LastFrame
;
500 } MI_LARGE_PAGE_RANGES
, *PMI_LARGE_PAGE_RANGES
;
502 typedef struct _MMVIEW
505 PCONTROL_AREA ControlArea
;
508 typedef struct _MMSESSION
510 KGUARDED_MUTEX SystemSpaceViewLock
;
511 PKGUARDED_MUTEX SystemSpaceViewLockPointer
;
512 PCHAR SystemSpaceViewStart
;
513 PMMVIEW SystemSpaceViewTable
;
514 ULONG SystemSpaceHashSize
;
515 ULONG SystemSpaceHashEntries
;
516 ULONG SystemSpaceHashKey
;
517 ULONG BitmapFailures
;
518 PRTL_BITMAP SystemSpaceBitMap
;
519 } MMSESSION
, *PMMSESSION
;
521 typedef struct _MM_SESSION_SPACE_FLAGS
524 ULONG DeletePending
:1;
526 } MM_SESSION_SPACE_FLAGS
;
528 typedef struct _MM_SESSION_SPACE
530 struct _MM_SESSION_SPACE
*GlobalVirtualAddress
;
535 MM_SESSION_SPACE_FLAGS Flags
;
538 LIST_ENTRY ProcessList
;
539 LARGE_INTEGER LastProcessSwappedOutTime
;
540 PFN_NUMBER SessionPageDirectoryIndex
;
541 SIZE_T NonPageablePages
;
542 SIZE_T CommittedPages
;
543 PVOID PagedPoolStart
;
545 PMMPTE PagedPoolBasePde
;
547 LONG ResidentProcessCount
;
548 ULONG SessionPoolAllocationFailures
[4];
549 LIST_ENTRY ImageList
;
553 PEPROCESS LastProcess
;
554 LONG ProcessReferenceToSession
;
555 LIST_ENTRY WsListEntry
;
556 GENERAL_LOOKASIDE Lookaside
[SESSION_POOL_LOOKASIDES
];
558 KGUARDED_MUTEX PagedPoolMutex
;
559 MM_PAGED_POOL_INFO PagedPoolInfo
;
562 PDRIVER_UNLOAD Win32KDriverUnload
;
563 POOL_DESCRIPTOR PagedPool
;
564 #if defined (_M_AMD64)
569 #if defined (_M_AMD64)
570 PMMPTE SpecialPoolFirstPte
;
571 PMMPTE SpecialPoolLastPte
;
572 PMMPTE NextPdeForSpecialPoolExpansion
;
573 PMMPTE LastPdeForSpecialPoolExpansion
;
574 PFN_NUMBER SpecialPagesInUse
;
576 LONG ImageLoadingCount
;
577 } MM_SESSION_SPACE
, *PMM_SESSION_SPACE
;
579 extern PMM_SESSION_SPACE MmSessionSpace
;
580 extern MMPTE HyperTemplatePte
;
581 extern MMPDE ValidKernelPde
;
582 extern MMPTE ValidKernelPte
;
583 extern MMPDE ValidKernelPdeLocal
;
584 extern MMPTE ValidKernelPteLocal
;
585 extern MMPDE DemandZeroPde
;
586 extern MMPTE DemandZeroPte
;
587 extern MMPTE PrototypePte
;
588 extern MMPTE MmDecommittedPte
;
589 extern BOOLEAN MmLargeSystemCache
;
590 extern BOOLEAN MmZeroPageFile
;
591 extern BOOLEAN MmProtectFreedNonPagedPool
;
592 extern BOOLEAN MmTrackLockedPages
;
593 extern BOOLEAN MmTrackPtes
;
594 extern BOOLEAN MmDynamicPfn
;
595 extern BOOLEAN MmMirroring
;
596 extern BOOLEAN MmMakeLowMemory
;
597 extern BOOLEAN MmEnforceWriteProtection
;
598 extern SIZE_T MmAllocationFragment
;
599 extern ULONG MmConsumedPoolPercentage
;
600 extern ULONG MmVerifyDriverBufferType
;
601 extern ULONG MmVerifyDriverLevel
;
602 extern WCHAR MmVerifyDriverBuffer
[512];
603 extern WCHAR MmLargePageDriverBuffer
[512];
604 extern LIST_ENTRY MiLargePageDriverList
;
605 extern BOOLEAN MiLargePageAllDrivers
;
606 extern ULONG MmVerifyDriverBufferLength
;
607 extern ULONG MmLargePageDriverBufferLength
;
608 extern SIZE_T MmSizeOfNonPagedPoolInBytes
;
609 extern SIZE_T MmMaximumNonPagedPoolInBytes
;
610 extern PFN_NUMBER MmMaximumNonPagedPoolInPages
;
611 extern PFN_NUMBER MmSizeOfPagedPoolInPages
;
612 extern PVOID MmNonPagedSystemStart
;
613 extern SIZE_T MiNonPagedSystemSize
;
614 extern PVOID MmNonPagedPoolStart
;
615 extern PVOID MmNonPagedPoolExpansionStart
;
616 extern PVOID MmNonPagedPoolEnd
;
617 extern SIZE_T MmSizeOfPagedPoolInBytes
;
618 extern PVOID MmPagedPoolStart
;
619 extern PVOID MmPagedPoolEnd
;
620 extern PVOID MmSessionBase
;
621 extern SIZE_T MmSessionSize
;
622 extern PMMPTE MmFirstReservedMappingPte
, MmLastReservedMappingPte
;
623 extern PMMPTE MiFirstReservedZeroingPte
;
624 extern MI_PFN_CACHE_ATTRIBUTE MiPlatformCacheAttributes
[2][MmMaximumCacheType
];
625 extern PPHYSICAL_MEMORY_DESCRIPTOR MmPhysicalMemoryBlock
;
626 extern SIZE_T MmBootImageSize
;
627 extern PMMPTE MmSystemPtesStart
[MaximumPtePoolTypes
];
628 extern PMMPTE MmSystemPtesEnd
[MaximumPtePoolTypes
];
629 extern PMEMORY_ALLOCATION_DESCRIPTOR MxFreeDescriptor
;
630 extern MEMORY_ALLOCATION_DESCRIPTOR MxOldFreeDescriptor
;
631 extern ULONG_PTR MxPfnAllocation
;
632 extern MM_PAGED_POOL_INFO MmPagedPoolInfo
;
633 extern RTL_BITMAP MiPfnBitMap
;
634 extern KGUARDED_MUTEX MmPagedPoolMutex
;
635 extern KGUARDED_MUTEX MmSectionCommitMutex
;
636 extern PVOID MmPagedPoolStart
;
637 extern PVOID MmPagedPoolEnd
;
638 extern PVOID MmNonPagedSystemStart
;
639 extern PVOID MiSystemViewStart
;
640 extern SIZE_T MmSystemViewSize
;
641 extern PVOID MmSessionBase
;
642 extern PVOID MiSessionSpaceEnd
;
643 extern PMMPTE MiSessionImagePteStart
;
644 extern PMMPTE MiSessionImagePteEnd
;
645 extern PMMPTE MiSessionBasePte
;
646 extern PMMPTE MiSessionLastPte
;
647 extern SIZE_T MmSizeOfPagedPoolInBytes
;
648 extern PMMPDE MmSystemPagePtes
;
649 extern PVOID MmSystemCacheStart
;
650 extern PVOID MmSystemCacheEnd
;
651 extern MMSUPPORT MmSystemCacheWs
;
652 extern SIZE_T MmAllocatedNonPagedPool
;
653 extern ULONG MmSpecialPoolTag
;
654 extern PVOID MmHyperSpaceEnd
;
655 extern PMMWSL MmSystemCacheWorkingSetList
;
656 extern SIZE_T MmMinimumNonPagedPoolSize
;
657 extern ULONG MmMinAdditionNonPagedPoolPerMb
;
658 extern SIZE_T MmDefaultMaximumNonPagedPool
;
659 extern ULONG MmMaxAdditionNonPagedPoolPerMb
;
660 extern ULONG MmSecondaryColors
;
661 extern ULONG MmSecondaryColorMask
;
662 extern ULONG MmNumberOfSystemPtes
;
663 extern ULONG MmMaximumNonPagedPoolPercent
;
664 extern ULONG MmLargeStackSize
;
665 extern PMMCOLOR_TABLES MmFreePagesByColor
[FreePageList
+ 1];
666 extern MMPFNLIST MmStandbyPageListByPriority
[8];
667 extern ULONG MmProductType
;
668 extern MM_SYSTEMSIZE MmSystemSize
;
669 extern PKEVENT MiLowMemoryEvent
;
670 extern PKEVENT MiHighMemoryEvent
;
671 extern PKEVENT MiLowPagedPoolEvent
;
672 extern PKEVENT MiHighPagedPoolEvent
;
673 extern PKEVENT MiLowNonPagedPoolEvent
;
674 extern PKEVENT MiHighNonPagedPoolEvent
;
675 extern PFN_NUMBER MmLowMemoryThreshold
;
676 extern PFN_NUMBER MmHighMemoryThreshold
;
677 extern PFN_NUMBER MiLowPagedPoolThreshold
;
678 extern PFN_NUMBER MiHighPagedPoolThreshold
;
679 extern PFN_NUMBER MiLowNonPagedPoolThreshold
;
680 extern PFN_NUMBER MiHighNonPagedPoolThreshold
;
681 extern PFN_NUMBER MmMinimumFreePages
;
682 extern PFN_NUMBER MmPlentyFreePages
;
683 extern PFN_COUNT MiExpansionPoolPagesInitialCharge
;
684 extern PFN_NUMBER MmResidentAvailablePages
;
685 extern PFN_NUMBER MmResidentAvailableAtInit
;
686 extern ULONG MmTotalFreeSystemPtes
[MaximumPtePoolTypes
];
687 extern PFN_NUMBER MmTotalSystemDriverPages
;
688 extern PVOID MiSessionImageStart
;
689 extern PVOID MiSessionImageEnd
;
690 extern PMMPTE MiHighestUserPte
;
691 extern PMMPDE MiHighestUserPde
;
692 extern PFN_NUMBER MmSystemPageDirectory
[PD_COUNT
];
693 extern PMMPTE MmSharedUserDataPte
;
694 extern LIST_ENTRY MmProcessList
;
695 extern BOOLEAN MmZeroingPageThreadActive
;
696 extern KEVENT MmZeroingPageEvent
;
697 extern ULONG MmSystemPageColor
;
698 extern ULONG MmProcessColorSeed
;
699 extern PMMWSL MmWorkingSetList
;
700 extern PFN_NUMBER MiNumberOfFreePages
;
701 extern SIZE_T MmSessionViewSize
;
702 extern SIZE_T MmSessionPoolSize
;
703 extern SIZE_T MmSessionImageSize
;
704 extern PVOID MiSystemViewStart
;
705 extern PVOID MiSessionPoolEnd
; // 0xBE000000
706 extern PVOID MiSessionPoolStart
; // 0xBD000000
707 extern PVOID MiSessionViewStart
; // 0xBE000000
708 extern PVOID MiSessionSpaceWs
;
709 extern ULONG MmMaximumDeadKernelStacks
;
710 extern SLIST_HEADER MmDeadStackSListHead
;
711 extern MM_AVL_TABLE MmSectionBasedRoot
;
712 extern KGUARDED_MUTEX MmSectionBasedMutex
;
713 extern PVOID MmHighSectionBase
;
714 extern SIZE_T MmSystemLockPagesCount
;
715 extern ULONG_PTR MmSubsectionBase
;
719 MiIsMemoryTypeFree(TYPE_OF_MEMORY MemoryType
)
721 return ((MemoryType
== LoaderFree
) ||
722 (MemoryType
== LoaderLoadedProgram
) ||
723 (MemoryType
== LoaderFirmwareTemporary
) ||
724 (MemoryType
== LoaderOsloaderStack
));
729 MiIsMemoryTypeInvisible(TYPE_OF_MEMORY MemoryType
)
731 return ((MemoryType
== LoaderFirmwarePermanent
) ||
732 (MemoryType
== LoaderSpecialMemory
) ||
733 (MemoryType
== LoaderHALCachedMemory
) ||
734 (MemoryType
== LoaderBBTMemory
));
740 MiIsUserPxe(PVOID Address
)
742 return ((ULONG_PTR
)Address
>> 7) == 0x1FFFFEDF6FB7DA0ULL
;
747 MiIsUserPpe(PVOID Address
)
749 return ((ULONG_PTR
)Address
>> 16) == 0xFFFFF6FB7DA0ULL
;
754 MiIsUserPde(PVOID Address
)
756 return ((ULONG_PTR
)Address
>> 25) == 0x7FFFFB7DA0ULL
;
761 MiIsUserPte(PVOID Address
)
763 return ((ULONG_PTR
)Address
>> 34) == 0x3FFFFDA0ULL
;
768 MiIsUserPde(PVOID Address
)
770 return ((Address
>= (PVOID
)MiAddressToPde(NULL
)) &&
771 (Address
<= (PVOID
)MiHighestUserPde
));
776 MiIsUserPte(PVOID Address
)
778 return (Address
<= (PVOID
)MiHighestUserPte
);
783 // Figures out the hardware bits for a PTE
787 MiDetermineUserGlobalPteMask(IN PVOID PointerPte
)
794 /* Make it valid and accessed */
795 TempPte
.u
.Hard
.Valid
= TRUE
;
796 MI_MAKE_ACCESSED_PAGE(&TempPte
);
798 /* Is this for user-mode? */
800 #if (_MI_PAGING_LEVELS == 4)
801 MiIsUserPxe(PointerPte
) ||
803 #if (_MI_PAGING_LEVELS >= 3)
804 MiIsUserPpe(PointerPte
) ||
806 MiIsUserPde(PointerPte
) ||
807 MiIsUserPte(PointerPte
))
809 /* Set the owner bit */
810 MI_MAKE_OWNER_PAGE(&TempPte
);
813 /* FIXME: We should also set the global bit */
815 /* Return the protection */
816 return TempPte
.u
.Long
;
820 // Creates a valid kernel PTE with the given protection
824 MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte
,
825 IN PMMPTE MappingPte
,
826 IN ULONG_PTR ProtectionMask
,
827 IN PFN_NUMBER PageFrameNumber
)
829 /* Only valid for kernel, non-session PTEs */
830 ASSERT(MappingPte
> MiHighestUserPte
);
831 ASSERT(!MI_IS_SESSION_PTE(MappingPte
));
832 ASSERT((MappingPte
< (PMMPTE
)PDE_BASE
) || (MappingPte
> (PMMPTE
)PDE_TOP
));
835 *NewPte
= ValidKernelPte
;
837 /* Set the protection and page */
838 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
839 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
843 // Creates a valid PTE with the given protection
847 MI_MAKE_HARDWARE_PTE(IN PMMPTE NewPte
,
848 IN PMMPTE MappingPte
,
849 IN ULONG_PTR ProtectionMask
,
850 IN PFN_NUMBER PageFrameNumber
)
852 /* Set the protection and page */
853 NewPte
->u
.Long
= MiDetermineUserGlobalPteMask(MappingPte
);
854 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
855 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
859 // Creates a valid user PTE with the given protection
863 MI_MAKE_HARDWARE_PTE_USER(IN PMMPTE NewPte
,
864 IN PMMPTE MappingPte
,
865 IN ULONG_PTR ProtectionMask
,
866 IN PFN_NUMBER PageFrameNumber
)
868 /* Only valid for kernel, non-session PTEs */
869 ASSERT(MappingPte
<= MiHighestUserPte
);
872 *NewPte
= ValidKernelPte
;
874 /* Set the protection and page */
875 NewPte
->u
.Hard
.Owner
= TRUE
;
876 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
877 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
882 // Builds a Prototype PTE for the address of the PTE
886 MI_MAKE_PROTOTYPE_PTE(IN PMMPTE NewPte
,
887 IN PMMPTE PointerPte
)
891 /* Mark this as a prototype */
893 NewPte
->u
.Proto
.Prototype
= 1;
896 * Prototype PTEs are only valid in paged pool by design, this little trick
897 * lets us only use 30 bits for the adress of the PTE, as long as the area
898 * stays 1024MB At most.
900 Offset
= (ULONG_PTR
)PointerPte
- (ULONG_PTR
)MmPagedPoolStart
;
903 * 7 bits go in the "low" (but we assume the bottom 2 are zero)
904 * and the other 21 bits go in the "high"
906 NewPte
->u
.Proto
.ProtoAddressLow
= (Offset
& 0x1FC) >> 2;
907 NewPte
->u
.Proto
.ProtoAddressHigh
= (Offset
& 0x3FFFFE00) >> 9;
911 // Builds a Subsection PTE for the address of the Segment
915 MI_MAKE_SUBSECTION_PTE(IN PMMPTE NewPte
,
920 /* Mark this as a prototype */
922 NewPte
->u
.Subsect
.Prototype
= 1;
925 * Segments are only valid either in nonpaged pool. We store the 20 bit
926 * difference either from the top or bottom of nonpaged pool, giving a
927 * maximum of 128MB to each delta, meaning nonpaged pool cannot exceed
930 if ((ULONG_PTR
)Segment
< ((ULONG_PTR
)MmSubsectionBase
+ (128 * _1MB
)))
932 Offset
= (ULONG_PTR
)Segment
- (ULONG_PTR
)MmSubsectionBase
;
933 NewPte
->u
.Subsect
.WhichPool
= PagedPool
;
937 Offset
= (ULONG_PTR
)MmNonPagedPoolEnd
- (ULONG_PTR
)Segment
;
938 NewPte
->u
.Subsect
.WhichPool
= NonPagedPool
;
942 * 4 bits go in the "low" (but we assume the bottom 3 are zero)
943 * and the other 20 bits go in the "high"
945 NewPte
->u
.Subsect
.SubsectionAddressLow
= (Offset
& 0x78) >> 3;
946 NewPte
->u
.Subsect
.SubsectionAddressHigh
= (Offset
& 0xFFFFF80) >> 7;
952 // Returns if the page is physically resident (ie: a large page)
953 // FIXFIX: CISC/x86 only?
957 MI_IS_PHYSICAL_ADDRESS(IN PVOID Address
)
961 /* Large pages are never paged out, always physically resident */
962 PointerPde
= MiAddressToPde(Address
);
963 return ((PointerPde
->u
.Hard
.LargePage
) && (PointerPde
->u
.Hard
.Valid
));
967 // Writes a valid PTE
971 MI_WRITE_VALID_PTE(IN PMMPTE PointerPte
,
974 /* Write the valid PTE */
975 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
976 ASSERT(TempPte
.u
.Hard
.Valid
== 1);
977 *PointerPte
= TempPte
;
981 // Writes an invalid PTE
985 MI_WRITE_INVALID_PTE(IN PMMPTE PointerPte
,
988 /* Write the invalid PTE */
989 ASSERT(InvalidPte
.u
.Hard
.Valid
== 0);
990 *PointerPte
= InvalidPte
;
994 // Writes a valid PDE
998 MI_WRITE_VALID_PDE(IN PMMPDE PointerPde
,
1001 /* Write the valid PDE */
1002 ASSERT(PointerPde
->u
.Hard
.Valid
== 0);
1003 ASSERT(TempPde
.u
.Hard
.Valid
== 1);
1004 *PointerPde
= TempPde
;
1008 // Writes an invalid PDE
1012 MI_WRITE_INVALID_PDE(IN PMMPDE PointerPde
,
1013 IN MMPDE InvalidPde
)
1015 /* Write the invalid PDE */
1016 ASSERT(InvalidPde
.u
.Hard
.Valid
== 0);
1017 *PointerPde
= InvalidPde
;
1021 // Checks if the thread already owns a working set
1025 MM_ANY_WS_LOCK_HELD(IN PETHREAD Thread
)
1027 /* If any of these are held, return TRUE */
1028 return ((Thread
->OwnsProcessWorkingSetExclusive
) ||
1029 (Thread
->OwnsProcessWorkingSetShared
) ||
1030 (Thread
->OwnsSystemWorkingSetExclusive
) ||
1031 (Thread
->OwnsSystemWorkingSetShared
) ||
1032 (Thread
->OwnsSessionWorkingSetExclusive
) ||
1033 (Thread
->OwnsSessionWorkingSetShared
));
1037 // Checks if the process owns the working set lock
1041 MI_WS_OWNER(IN PEPROCESS Process
)
1043 /* Check if this process is the owner, and that the thread owns the WS */
1044 if (PsGetCurrentThread()->OwnsProcessWorkingSetExclusive
== 0)
1046 DPRINT1("Thread: %p is not an owner\n", PsGetCurrentThread());
1048 if (KeGetCurrentThread()->ApcState
.Process
!= &Process
->Pcb
)
1050 DPRINT1("Current thread %p is attached to another process %p\n", PsGetCurrentThread(), Process
);
1052 return ((KeGetCurrentThread()->ApcState
.Process
== &Process
->Pcb
) &&
1053 ((PsGetCurrentThread()->OwnsProcessWorkingSetExclusive
) ||
1054 (PsGetCurrentThread()->OwnsProcessWorkingSetShared
)));
1058 // New ARM3<->RosMM PAGE Architecture
1062 MiIsRosSectionObject(IN PVOID Section
)
1064 PROS_SECTION_OBJECT RosSection
= Section
;
1065 if ((RosSection
->Type
== 'SC') && (RosSection
->Size
== 'TN')) return TRUE
;
1070 // HACK ON TOP OF HACK ALERT!!!
1071 #define MI_GET_ROS_DATA(x) \
1072 (((x)->RosMmData == 0) ? NULL : ((PMMROSPFN)((ULONG64)(ULONG)((x)->RosMmData) | \
1073 ((ULONG64)MmNonPagedPoolStart & 0xffffffff00000000ULL))))
1075 #define MI_GET_ROS_DATA(x) ((PMMROSPFN)(x->RosMmData))
1077 #define MI_IS_ROS_PFN(x) (((x)->u4.AweAllocation == TRUE) && (MI_GET_ROS_DATA(x) != NULL))
1078 #define ASSERT_IS_ROS_PFN(x) ASSERT(MI_IS_ROS_PFN(x) == TRUE);
1079 typedef struct _MMROSPFN
1081 PMM_RMAP_ENTRY RmapListHead
;
1082 SWAPENTRY SwapEntry
;
1083 } MMROSPFN
, *PMMROSPFN
;
1085 #define RosMmData AweReferenceCount
1089 MiDecrementReferenceCount(
1091 IN PFN_NUMBER PageFrameIndex
1096 MI_IS_WS_UNSAFE(IN PEPROCESS Process
)
1098 return (Process
->Vm
.Flags
.AcquiredUnsafe
== TRUE
);
1102 // Locks the working set for the given process
1106 MiLockProcessWorkingSet(IN PEPROCESS Process
,
1109 /* Shouldn't already be owning the process working set */
1110 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1111 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1113 /* Block APCs, make sure that still nothing is already held */
1114 KeEnterGuardedRegion();
1115 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1117 /* Lock the working set */
1118 ExAcquirePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1120 /* Now claim that we own the lock */
1121 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1122 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1123 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1128 MiLockProcessWorkingSetShared(IN PEPROCESS Process
,
1131 /* Shouldn't already be owning the process working set */
1132 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1133 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1135 /* Block APCs, make sure that still nothing is already held */
1136 KeEnterGuardedRegion();
1137 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1139 /* Lock the working set */
1140 ExAcquirePushLockShared(&Process
->Vm
.WorkingSetMutex
);
1142 /* Now claim that we own the lock */
1143 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1144 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1145 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1146 Thread
->OwnsProcessWorkingSetShared
= TRUE
;
1151 MiLockProcessWorkingSetUnsafe(IN PEPROCESS Process
,
1154 /* Shouldn't already be owning the process working set */
1155 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1157 /* APCs must be blocked, make sure that still nothing is already held */
1158 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1159 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1161 /* Lock the working set */
1162 ExAcquirePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1164 /* Now claim that we own the lock */
1165 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1166 Process
->Vm
.Flags
.AcquiredUnsafe
= 1;
1167 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1168 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1172 // Unlocks the working set for the given process
1176 MiUnlockProcessWorkingSet(IN PEPROCESS Process
,
1179 /* Make sure we are the owner of a safe acquisition */
1180 ASSERT(MI_WS_OWNER(Process
));
1181 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1183 /* The thread doesn't own it anymore */
1184 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== TRUE
);
1185 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1187 /* Release the lock and re-enable APCs */
1188 ExReleasePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1189 KeLeaveGuardedRegion();
1193 // Unlocks the working set for the given process
1197 MiUnlockProcessWorkingSetUnsafe(IN PEPROCESS Process
,
1200 /* Make sure we are the owner of an unsafe acquisition */
1201 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1202 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1203 ASSERT(MI_WS_OWNER(Process
));
1204 ASSERT(MI_IS_WS_UNSAFE(Process
));
1206 /* No longer unsafe */
1207 Process
->Vm
.Flags
.AcquiredUnsafe
= 0;
1209 /* The thread doesn't own it anymore */
1210 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== TRUE
);
1211 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1213 /* Release the lock but don't touch APC state */
1214 ExReleasePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1215 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1219 // Locks the working set
1223 MiLockWorkingSet(IN PETHREAD Thread
,
1224 IN PMMSUPPORT WorkingSet
)
1227 KeEnterGuardedRegion();
1229 /* Working set should be in global memory */
1230 ASSERT(MI_IS_SESSION_ADDRESS((PVOID
)WorkingSet
) == FALSE
);
1232 /* Thread shouldn't already be owning something */
1233 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1235 /* Lock this working set */
1236 ExAcquirePushLockExclusive(&WorkingSet
->WorkingSetMutex
);
1238 /* Which working set is this? */
1239 if (WorkingSet
== &MmSystemCacheWs
)
1241 /* Own the system working set */
1242 ASSERT((Thread
->OwnsSystemWorkingSetExclusive
== FALSE
) &&
1243 (Thread
->OwnsSystemWorkingSetShared
== FALSE
));
1244 Thread
->OwnsSystemWorkingSetExclusive
= TRUE
;
1246 else if (WorkingSet
->Flags
.SessionSpace
)
1248 /* Own the session working set */
1249 ASSERT((Thread
->OwnsSessionWorkingSetExclusive
== FALSE
) &&
1250 (Thread
->OwnsSessionWorkingSetShared
== FALSE
));
1251 Thread
->OwnsSessionWorkingSetExclusive
= TRUE
;
1255 /* Own the process working set */
1256 ASSERT((Thread
->OwnsProcessWorkingSetExclusive
== FALSE
) &&
1257 (Thread
->OwnsProcessWorkingSetShared
== FALSE
));
1258 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1263 // Unlocks the working set
1267 MiUnlockWorkingSet(IN PETHREAD Thread
,
1268 IN PMMSUPPORT WorkingSet
)
1270 /* Working set should be in global memory */
1271 ASSERT(MI_IS_SESSION_ADDRESS((PVOID
)WorkingSet
) == FALSE
);
1273 /* Which working set is this? */
1274 if (WorkingSet
== &MmSystemCacheWs
)
1276 /* Release the system working set */
1277 ASSERT((Thread
->OwnsSystemWorkingSetExclusive
== TRUE
) ||
1278 (Thread
->OwnsSystemWorkingSetShared
== TRUE
));
1279 Thread
->OwnsSystemWorkingSetExclusive
= FALSE
;
1281 else if (WorkingSet
->Flags
.SessionSpace
)
1283 /* Release the session working set */
1284 ASSERT((Thread
->OwnsSessionWorkingSetExclusive
== TRUE
) ||
1285 (Thread
->OwnsSessionWorkingSetShared
== TRUE
));
1286 Thread
->OwnsSessionWorkingSetExclusive
= 0;
1290 /* Release the process working set */
1291 ASSERT((Thread
->OwnsProcessWorkingSetExclusive
) ||
1292 (Thread
->OwnsProcessWorkingSetShared
));
1293 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1296 /* Release the working set lock */
1297 ExReleasePushLockExclusive(&WorkingSet
->WorkingSetMutex
);
1300 KeLeaveGuardedRegion();
1305 MiUnlockProcessWorkingSetForFault(IN PEPROCESS Process
,
1310 ASSERT(MI_WS_OWNER(Process
));
1312 /* Check if the current owner is unsafe */
1313 if (MI_IS_WS_UNSAFE(Process
))
1315 /* Release unsafely */
1316 MiUnlockProcessWorkingSetUnsafe(Process
, Thread
);
1320 else if (Thread
->OwnsProcessWorkingSetExclusive
== 1)
1322 /* Owner is safe and exclusive, release normally */
1323 MiUnlockProcessWorkingSet(Process
, Thread
);
1329 /* Owner is shared (implies safe), release normally */
1338 MiLockProcessWorkingSetForFault(IN PEPROCESS Process
,
1343 ASSERT(Shared
== FALSE
);
1345 /* Check if this was a safe lock or not */
1348 /* Reacquire safely */
1349 MiLockProcessWorkingSet(Process
, Thread
);
1353 /* Reacquire unsafely */
1354 MiLockProcessWorkingSetUnsafe(Process
, Thread
);
1359 // Returns the ProtoPTE inside a VAD for the given VPN
1363 MI_GET_PROTOTYPE_PTE_FOR_VPN(IN PMMVAD Vad
,
1368 /* Find the offset within the VAD's prototype PTEs */
1369 ProtoPte
= Vad
->FirstPrototypePte
+ (Vpn
- Vad
->StartingVpn
);
1370 ASSERT(ProtoPte
<= Vad
->LastContiguousPte
);
1375 // Returns the PFN Database entry for the given page number
1376 // Warning: This is not necessarily a valid PFN database entry!
1380 MI_PFN_ELEMENT(IN PFN_NUMBER Pfn
)
1383 return &MmPfnDatabase
[Pfn
];
1387 // Drops a locked page without dereferencing it
1391 MiDropLockCount(IN PMMPFN Pfn1
)
1393 /* This page shouldn't be locked, but it should be valid */
1394 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1395 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1397 /* Is this the last reference to the page */
1398 if (Pfn1
->u3
.e2
.ReferenceCount
== 1)
1400 /* It better not be valid */
1401 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1403 /* Is it a prototype PTE? */
1404 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1405 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1407 /* FIXME: We should return commit */
1408 DPRINT1("Not returning commit for prototype PTE\n");
1411 /* Update the counter */
1412 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1417 // Drops a locked page and dereferences it
1421 MiDereferencePfnAndDropLockCount(IN PMMPFN Pfn1
)
1423 USHORT RefCount
, OldRefCount
;
1424 PFN_NUMBER PageFrameIndex
;
1426 /* Loop while we decrement the page successfully */
1429 /* There should be at least one reference */
1430 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1431 ASSERT(OldRefCount
!= 0);
1433 /* Are we the last one */
1434 if (OldRefCount
== 1)
1436 /* The page shoudln't be shared not active at this point */
1437 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 1);
1438 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1439 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1441 /* Is it a prototype PTE? */
1442 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1443 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1445 /* FIXME: We should return commit */
1446 DPRINT1("Not returning commit for prototype PTE\n");
1449 /* Update the counter, and drop a reference the long way */
1450 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1451 PageFrameIndex
= MiGetPfnEntryIndex(Pfn1
);
1452 MiDecrementReferenceCount(Pfn1
, PageFrameIndex
);
1456 /* Drop a reference the short way, and that's it */
1457 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1460 ASSERT(RefCount
!= 0);
1461 } while (OldRefCount
!= RefCount
);
1463 /* If we got here, there should be more than one reference */
1464 ASSERT(RefCount
> 1);
1467 /* Is it still being shared? */
1468 if (Pfn1
->u2
.ShareCount
>= 1)
1470 /* Then it should be valid */
1471 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1473 /* Is it a prototype PTE? */
1474 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1475 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1477 /* We don't handle ethis */
1481 /* Update the counter */
1482 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1488 // References a locked page and updates the counter
1489 // Used in MmProbeAndLockPages to handle different edge cases
1493 MiReferenceProbedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1495 USHORT RefCount
, OldRefCount
;
1498 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1500 /* Does ARM3 own the page? */
1501 if (MI_IS_ROS_PFN(Pfn1
))
1503 /* ReactOS Mm doesn't track share count */
1504 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1508 /* On ARM3 pages, we should see a valid share count */
1509 ASSERT((Pfn1
->u2
.ShareCount
!= 0) && (Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
));
1511 /* Is it a prototype PTE? */
1512 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1513 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1515 /* FIXME: We should charge commit */
1516 DPRINT1("Not charging commit for prototype PTE\n");
1520 /* More locked pages! */
1521 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1523 /* Loop trying to update the reference count */
1526 /* Get the current reference count, make sure it's valid */
1527 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1528 ASSERT(OldRefCount
!= 0);
1529 ASSERT(OldRefCount
< 2500);
1531 /* Bump it up by one */
1532 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1535 ASSERT(RefCount
!= 0);
1536 } while (OldRefCount
!= RefCount
);
1538 /* Was this the first lock attempt? If not, undo our bump */
1539 if (OldRefCount
!= 1) InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1543 // References a locked page and updates the counter
1544 // Used in all other cases except MmProbeAndLockPages
1548 MiReferenceUsedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1552 /* Is it a prototype PTE? */
1553 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1554 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1556 /* FIXME: We should charge commit */
1557 DPRINT1("Not charging commit for prototype PTE\n");
1560 /* More locked pages! */
1561 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1563 /* Update the reference count */
1564 NewRefCount
= InterlockedIncrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1565 if (NewRefCount
== 2)
1567 /* Is it locked or shared? */
1568 if (Pfn1
->u2
.ShareCount
)
1570 /* It's shared, so make sure it's active */
1571 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1575 /* It's locked, so we shouldn't lock again */
1576 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1581 /* Someone had already locked the page, so undo our bump */
1582 ASSERT(NewRefCount
< 2500);
1583 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1588 // References a locked page and updates the counter
1589 // Used in all other cases except MmProbeAndLockPages
1593 MiReferenceUnusedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1597 /* Make sure the page isn't used yet */
1598 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1599 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1601 /* Is it a prototype PTE? */
1602 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1603 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1605 /* FIXME: We should charge commit */
1606 DPRINT1("Not charging commit for prototype PTE\n");
1609 /* More locked pages! */
1610 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1612 /* Update the reference count */
1613 NewRefCount
= InterlockedIncrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1614 if (NewRefCount
!= 1)
1616 /* Someone had already locked the page, so undo our bump */
1617 ASSERT(NewRefCount
< 2500);
1618 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1624 MiIncrementPageTableReferences(IN PVOID Address
)
1628 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1631 ASSERT(*RefCount
<= PTE_PER_PAGE
);
1636 MiDecrementPageTableReferences(IN PVOID Address
)
1640 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1643 ASSERT(*RefCount
< PTE_PER_PAGE
);
1648 MiQueryPageTableReferences(IN PVOID Address
)
1652 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1661 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1666 MiInitializeSessionSpaceLayout();
1670 MiInitMachineDependent(
1671 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1676 MiComputeColorInformation(
1683 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1688 MiInitializeColorTables(
1694 MiInitializePfnDatabase(
1695 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1700 MiInitializeSessionIds(
1706 MiInitializeMemoryEvents(
1713 IN PFN_NUMBER PageCount
1716 PPHYSICAL_MEMORY_DESCRIPTOR
1718 MmInitializeMemoryLimits(
1719 IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
1720 IN PBOOLEAN IncludeType
1725 MiPagesInLoaderBlock(
1726 IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
1727 IN PBOOLEAN IncludeType
1733 IN PVOID AddressStart
,
1739 MiRosProtectVirtualMemory(
1740 IN PEPROCESS Process
,
1741 IN OUT PVOID
*BaseAddress
,
1742 IN OUT PSIZE_T NumberOfBytesToProtect
,
1743 IN ULONG NewAccessProtection
,
1744 OUT PULONG OldAccessProtection OPTIONAL
1750 IN BOOLEAN StoreInstruction
,
1752 IN KPROCESSOR_MODE Mode
,
1753 IN PVOID TrapInformation
1758 MiCheckPdeForPagedPool(
1764 MiInitializeNonPagedPool(
1770 MiInitializeNonPagedPoolThresholds(
1776 MiInitializePoolEvents(
1783 IN POOL_TYPE PoolType
,// FIXFIX: This should go in ex.h after the pool merge
1784 IN ULONG Threshold
//
1787 // FIXFIX: THIS ONE TOO
1791 ExInitializePoolDescriptor(
1792 IN PPOOL_DESCRIPTOR PoolDescriptor
,
1793 IN POOL_TYPE PoolType
,
1801 MiInitializeSessionPool(
1807 MiInitializeSystemPtes(
1808 IN PMMPTE StartingPte
,
1809 IN ULONG NumberOfPtes
,
1810 IN MMSYSTEM_PTE_POOL_TYPE PoolType
1815 MiReserveSystemPtes(
1816 IN ULONG NumberOfPtes
,
1817 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1822 MiReleaseSystemPtes(
1823 IN PMMPTE StartingPte
,
1824 IN ULONG NumberOfPtes
,
1825 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1831 MiFindContiguousPages(
1832 IN PFN_NUMBER LowestPfn
,
1833 IN PFN_NUMBER HighestPfn
,
1834 IN PFN_NUMBER BoundaryPfn
,
1835 IN PFN_NUMBER SizeInPages
,
1836 IN MEMORY_CACHING_TYPE CacheType
1841 MiCheckForContiguousMemory(
1842 IN PVOID BaseAddress
,
1843 IN PFN_NUMBER BaseAddressPages
,
1844 IN PFN_NUMBER SizeInPages
,
1845 IN PFN_NUMBER LowestPfn
,
1846 IN PFN_NUMBER HighestPfn
,
1847 IN PFN_NUMBER BoundaryPfn
,
1848 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
1853 MiAllocatePagesForMdl(
1854 IN PHYSICAL_ADDRESS LowAddress
,
1855 IN PHYSICAL_ADDRESS HighAddress
,
1856 IN PHYSICAL_ADDRESS SkipBytes
,
1857 IN SIZE_T TotalBytes
,
1858 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
,
1864 MiMapLockedPagesInUserSpace(
1867 IN MEMORY_CACHING_TYPE CacheType
,
1868 IN PVOID BaseAddress
1873 MiUnmapLockedPagesInUserSpace(
1874 IN PVOID BaseAddress
,
1881 IN PMMPFNLIST ListHead
,
1882 IN PFN_NUMBER PageFrameIndex
1887 MiUnlinkFreeOrZeroedPage(
1893 MiUnlinkPageFromList(
1900 IN PMMPTE PointerPte
,
1907 IN PFN_NUMBER PageFrameIndex
,
1908 IN PMMPTE PointerPte
,
1914 MiInitializeAndChargePfn(
1915 OUT PPFN_NUMBER PageFrameIndex
,
1916 IN PMMPTE PointerPde
,
1917 IN PFN_NUMBER ContainingPageFrame
,
1918 IN BOOLEAN SessionAllocation
1923 MiInitializePfnAndMakePteValid(
1924 IN PFN_NUMBER PageFrameIndex
,
1925 IN PMMPTE PointerPte
,
1931 MiInitializePfnForOtherProcess(
1932 IN PFN_NUMBER PageFrameIndex
,
1933 IN PMMPTE PointerPte
,
1934 IN PFN_NUMBER PteFrame
1939 MiDecrementShareCount(
1941 IN PFN_NUMBER PageFrameIndex
1959 IN PFN_NUMBER PageFrameIndex
1964 MiInsertPageInFreeList(
1965 IN PFN_NUMBER PageFrameIndex
1970 MiDeleteSystemPageableVm(
1971 IN PMMPTE PointerPte
,
1972 IN PFN_NUMBER PageCount
,
1974 OUT PPFN_NUMBER ValidPages
1979 MiGetPageProtection(
1980 IN PMMPTE PointerPte
1983 PLDR_DATA_TABLE_ENTRY
1985 MiLookupDataTableEntry(
1991 MiInitializeDriverLargePageList(
1997 MiInitializeLargePageSupport(
2016 IN PVOID VirtualAddress
2021 MiCheckForConflictingNode(
2022 IN ULONG_PTR StartVpn
,
2023 IN ULONG_PTR EndVpn
,
2024 IN PMM_AVL_TABLE Table
2029 MiFindEmptyAddressRangeDownTree(
2031 IN ULONG_PTR BoundaryAddress
,
2032 IN ULONG_PTR Alignment
,
2033 IN PMM_AVL_TABLE Table
,
2034 OUT PULONG_PTR Base
,
2035 OUT PMMADDRESS_NODE
*Parent
2040 MiFindEmptyAddressRangeDownBasedTree(
2042 IN ULONG_PTR BoundaryAddress
,
2043 IN ULONG_PTR Alignment
,
2044 IN PMM_AVL_TABLE Table
,
2050 MiFindEmptyAddressRangeInTree(
2052 IN ULONG_PTR Alignment
,
2053 IN PMM_AVL_TABLE Table
,
2054 OUT PMMADDRESS_NODE
*PreviousVad
,
2064 IN ULONG ProtectionMask
2071 IN PEPROCESS Process
2076 MiInsertBasedSection(
2082 MiUnmapViewOfSection(
2083 IN PEPROCESS Process
,
2084 IN PVOID BaseAddress
,
2090 MiRosUnmapViewOfSection(
2091 IN PEPROCESS Process
,
2092 IN PVOID BaseAddress
,
2099 IN PMM_AVL_TABLE Table
,
2100 IN PMMADDRESS_NODE NewNode
,
2101 PMMADDRESS_NODE Parent
,
2102 TABLE_SEARCH_RESULT Result
2108 IN PMMADDRESS_NODE Node
,
2109 IN PMM_AVL_TABLE Table
2115 IN PMMADDRESS_NODE Node
2121 IN PMMADDRESS_NODE Node
2126 MiInitializeSystemSpaceMap(
2127 IN PMMSESSION InputSession OPTIONAL
2132 MiSessionRemoveProcess(
2138 MiReleaseProcessReferenceToSessionDataPage(
2139 IN PMM_SESSION_SPACE SessionGlobal
2144 MiSessionAddProcess(
2145 IN PEPROCESS NewProcess
2150 MiSessionCommitPageTables(
2157 MiMakeProtectionMask(
2163 MiDeleteVirtualAddresses(
2165 IN ULONG_PTR EndingAddress
,
2171 MiMakeSystemAddressValid(
2172 IN PVOID PageTableVirtualAddress
,
2173 IN PEPROCESS CurrentProcess
2178 MiMakeSystemAddressValidPfn(
2179 IN PVOID VirtualAddress
,
2186 IN PEPROCESS CurrentProcess
,
2199 MiQueryMemorySectionName(
2200 IN HANDLE ProcessHandle
,
2201 IN PVOID BaseAddress
,
2202 OUT PVOID MemoryInformation
,
2203 IN SIZE_T MemoryInformationLength
,
2204 OUT PSIZE_T ReturnLength
2209 MiRosUnmapViewInSystemSpace(
2215 MmDeterminePoolType(
2216 IN PVOID PoolAddress
2221 MiMakePdeExistAndMakeValid(
2222 IN PMMPTE PointerPde
,
2223 IN PEPROCESS TargetProcess
,
2228 // MiRemoveZeroPage will use inline code to zero out the page manually if only
2229 // free pages are available. In some scenarios, we don't/can't run that piece of
2230 // code and would rather only have a real zero page. If we can't have a zero page,
2231 // then we'd like to have our own code to grab a free page and zero it out, by
2232 // using MiRemoveAnyPage. This macro implements this.
2236 MiRemoveZeroPageSafe(IN ULONG Color
)
2238 if (MmFreePagesByColor
[ZeroedPageList
][Color
].Flink
!= LIST_HEAD
) return MiRemoveZeroPage(Color
);