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_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 SIZE_T MmMinimumStackCommitInBytes
;
684 extern PFN_COUNT MiExpansionPoolPagesInitialCharge
;
685 extern PFN_NUMBER MmResidentAvailablePages
;
686 extern PFN_NUMBER MmResidentAvailableAtInit
;
687 extern ULONG MmTotalFreeSystemPtes
[MaximumPtePoolTypes
];
688 extern PFN_NUMBER MmTotalSystemDriverPages
;
689 extern ULONG MmCritsectTimeoutSeconds
;
690 extern PVOID MiSessionImageStart
;
691 extern PVOID MiSessionImageEnd
;
692 extern PMMPTE MiHighestUserPte
;
693 extern PMMPDE MiHighestUserPde
;
694 extern PFN_NUMBER MmSystemPageDirectory
[PD_COUNT
];
695 extern PMMPTE MmSharedUserDataPte
;
696 extern LIST_ENTRY MmProcessList
;
697 extern BOOLEAN MmZeroingPageThreadActive
;
698 extern KEVENT MmZeroingPageEvent
;
699 extern ULONG MmSystemPageColor
;
700 extern ULONG MmProcessColorSeed
;
701 extern PMMWSL MmWorkingSetList
;
702 extern PFN_NUMBER MiNumberOfFreePages
;
703 extern SIZE_T MmSessionViewSize
;
704 extern SIZE_T MmSessionPoolSize
;
705 extern SIZE_T MmSessionImageSize
;
706 extern PVOID MiSystemViewStart
;
707 extern PVOID MiSessionPoolEnd
; // 0xBE000000
708 extern PVOID MiSessionPoolStart
; // 0xBD000000
709 extern PVOID MiSessionViewStart
; // 0xBE000000
710 extern PVOID MiSessionSpaceWs
;
711 extern ULONG MmMaximumDeadKernelStacks
;
712 extern SLIST_HEADER MmDeadStackSListHead
;
713 extern MM_AVL_TABLE MmSectionBasedRoot
;
714 extern KGUARDED_MUTEX MmSectionBasedMutex
;
715 extern PVOID MmHighSectionBase
;
716 extern SIZE_T MmSystemLockPagesCount
;
717 extern ULONG_PTR MmSubsectionBase
;
718 extern LARGE_INTEGER MmCriticalSectionTimeout
;
719 extern LIST_ENTRY MmWorkingSetExpansionHead
;
723 MiIsMemoryTypeFree(TYPE_OF_MEMORY MemoryType
)
725 return ((MemoryType
== LoaderFree
) ||
726 (MemoryType
== LoaderLoadedProgram
) ||
727 (MemoryType
== LoaderFirmwareTemporary
) ||
728 (MemoryType
== LoaderOsloaderStack
));
733 MiIsMemoryTypeInvisible(TYPE_OF_MEMORY MemoryType
)
735 return ((MemoryType
== LoaderFirmwarePermanent
) ||
736 (MemoryType
== LoaderSpecialMemory
) ||
737 (MemoryType
== LoaderHALCachedMemory
) ||
738 (MemoryType
== LoaderBBTMemory
));
744 MiIsUserPxe(PVOID Address
)
746 return ((ULONG_PTR
)Address
>> 7) == 0x1FFFFEDF6FB7DA0ULL
;
751 MiIsUserPpe(PVOID Address
)
753 return ((ULONG_PTR
)Address
>> 16) == 0xFFFFF6FB7DA0ULL
;
758 MiIsUserPde(PVOID Address
)
760 return ((ULONG_PTR
)Address
>> 25) == 0x7FFFFB7DA0ULL
;
765 MiIsUserPte(PVOID Address
)
767 return ((ULONG_PTR
)Address
>> 34) == 0x3FFFFDA0ULL
;
772 MiIsUserPde(PVOID Address
)
774 return ((Address
>= (PVOID
)MiAddressToPde(NULL
)) &&
775 (Address
<= (PVOID
)MiHighestUserPde
));
780 MiIsUserPte(PVOID Address
)
782 return (Address
<= (PVOID
)MiHighestUserPte
);
787 // Figures out the hardware bits for a PTE
791 MiDetermineUserGlobalPteMask(IN PVOID PointerPte
)
798 /* Make it valid and accessed */
799 TempPte
.u
.Hard
.Valid
= TRUE
;
800 MI_MAKE_ACCESSED_PAGE(&TempPte
);
802 /* Is this for user-mode? */
804 #if (_MI_PAGING_LEVELS == 4)
805 MiIsUserPxe(PointerPte
) ||
807 #if (_MI_PAGING_LEVELS >= 3)
808 MiIsUserPpe(PointerPte
) ||
810 MiIsUserPde(PointerPte
) ||
811 MiIsUserPte(PointerPte
))
813 /* Set the owner bit */
814 MI_MAKE_OWNER_PAGE(&TempPte
);
817 /* FIXME: We should also set the global bit */
819 /* Return the protection */
820 return TempPte
.u
.Long
;
824 // Creates a valid kernel PTE with the given protection
828 MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte
,
829 IN PMMPTE MappingPte
,
830 IN ULONG_PTR ProtectionMask
,
831 IN PFN_NUMBER PageFrameNumber
)
833 /* Only valid for kernel, non-session PTEs */
834 ASSERT(MappingPte
> MiHighestUserPte
);
835 ASSERT(!MI_IS_SESSION_PTE(MappingPte
));
836 ASSERT((MappingPte
< (PMMPTE
)PDE_BASE
) || (MappingPte
> (PMMPTE
)PDE_TOP
));
839 *NewPte
= ValidKernelPte
;
841 /* Set the protection and page */
842 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
843 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
847 // Creates a valid PTE with the given protection
851 MI_MAKE_HARDWARE_PTE(IN PMMPTE NewPte
,
852 IN PMMPTE MappingPte
,
853 IN ULONG_PTR ProtectionMask
,
854 IN PFN_NUMBER PageFrameNumber
)
856 /* Set the protection and page */
857 NewPte
->u
.Long
= MiDetermineUserGlobalPteMask(MappingPte
);
858 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
859 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
863 // Creates a valid user PTE with the given protection
867 MI_MAKE_HARDWARE_PTE_USER(IN PMMPTE NewPte
,
868 IN PMMPTE MappingPte
,
869 IN ULONG_PTR ProtectionMask
,
870 IN PFN_NUMBER PageFrameNumber
)
872 /* Only valid for kernel, non-session PTEs */
873 ASSERT(MappingPte
<= MiHighestUserPte
);
876 *NewPte
= ValidKernelPte
;
878 /* Set the protection and page */
879 NewPte
->u
.Hard
.Owner
= TRUE
;
880 NewPte
->u
.Hard
.PageFrameNumber
= PageFrameNumber
;
881 NewPte
->u
.Long
|= MmProtectToPteMask
[ProtectionMask
];
886 // Builds a Prototype PTE for the address of the PTE
890 MI_MAKE_PROTOTYPE_PTE(IN PMMPTE NewPte
,
891 IN PMMPTE PointerPte
)
895 /* Mark this as a prototype */
897 NewPte
->u
.Proto
.Prototype
= 1;
900 * Prototype PTEs are only valid in paged pool by design, this little trick
901 * lets us only use 30 bits for the adress of the PTE, as long as the area
902 * stays 1024MB At most.
904 Offset
= (ULONG_PTR
)PointerPte
- (ULONG_PTR
)MmPagedPoolStart
;
907 * 7 bits go in the "low" (but we assume the bottom 2 are zero)
908 * and the other 21 bits go in the "high"
910 NewPte
->u
.Proto
.ProtoAddressLow
= (Offset
& 0x1FC) >> 2;
911 NewPte
->u
.Proto
.ProtoAddressHigh
= (Offset
& 0x3FFFFE00) >> 9;
915 // Builds a Subsection PTE for the address of the Segment
919 MI_MAKE_SUBSECTION_PTE(IN PMMPTE NewPte
,
924 /* Mark this as a prototype */
926 NewPte
->u
.Subsect
.Prototype
= 1;
929 * Segments are only valid either in nonpaged pool. We store the 20 bit
930 * difference either from the top or bottom of nonpaged pool, giving a
931 * maximum of 128MB to each delta, meaning nonpaged pool cannot exceed
934 if ((ULONG_PTR
)Segment
< ((ULONG_PTR
)MmSubsectionBase
+ (128 * _1MB
)))
936 Offset
= (ULONG_PTR
)Segment
- (ULONG_PTR
)MmSubsectionBase
;
937 NewPte
->u
.Subsect
.WhichPool
= PagedPool
;
941 Offset
= (ULONG_PTR
)MmNonPagedPoolEnd
- (ULONG_PTR
)Segment
;
942 NewPte
->u
.Subsect
.WhichPool
= NonPagedPool
;
946 * 4 bits go in the "low" (but we assume the bottom 3 are zero)
947 * and the other 20 bits go in the "high"
949 NewPte
->u
.Subsect
.SubsectionAddressLow
= (Offset
& 0x78) >> 3;
950 NewPte
->u
.Subsect
.SubsectionAddressHigh
= (Offset
& 0xFFFFF80) >> 7;
955 MI_IS_MAPPED_PTE(PMMPTE PointerPte
)
957 /// \todo Make this reasonable code, this is UGLY!
958 return ((PointerPte
->u
.Long
& 0xFFFFFC01) != 0);
964 // Returns if the page is physically resident (ie: a large page)
965 // FIXFIX: CISC/x86 only?
969 MI_IS_PHYSICAL_ADDRESS(IN PVOID Address
)
973 /* Large pages are never paged out, always physically resident */
974 PointerPde
= MiAddressToPde(Address
);
975 return ((PointerPde
->u
.Hard
.LargePage
) && (PointerPde
->u
.Hard
.Valid
));
979 // Writes a valid PTE
983 MI_WRITE_VALID_PTE(IN PMMPTE PointerPte
,
986 /* Write the valid PTE */
987 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
988 ASSERT(TempPte
.u
.Hard
.Valid
== 1);
989 *PointerPte
= TempPte
;
993 // Writes an invalid PTE
997 MI_WRITE_INVALID_PTE(IN PMMPTE PointerPte
,
1000 /* Write the invalid PTE */
1001 ASSERT(InvalidPte
.u
.Hard
.Valid
== 0);
1002 *PointerPte
= InvalidPte
;
1006 // Writes a valid PDE
1010 MI_WRITE_VALID_PDE(IN PMMPDE PointerPde
,
1013 /* Write the valid PDE */
1014 ASSERT(PointerPde
->u
.Hard
.Valid
== 0);
1015 ASSERT(TempPde
.u
.Hard
.Valid
== 1);
1016 *PointerPde
= TempPde
;
1020 // Writes an invalid PDE
1024 MI_WRITE_INVALID_PDE(IN PMMPDE PointerPde
,
1025 IN MMPDE InvalidPde
)
1027 /* Write the invalid PDE */
1028 ASSERT(InvalidPde
.u
.Hard
.Valid
== 0);
1029 *PointerPde
= InvalidPde
;
1033 // Checks if the thread already owns a working set
1037 MM_ANY_WS_LOCK_HELD(IN PETHREAD Thread
)
1039 /* If any of these are held, return TRUE */
1040 return ((Thread
->OwnsProcessWorkingSetExclusive
) ||
1041 (Thread
->OwnsProcessWorkingSetShared
) ||
1042 (Thread
->OwnsSystemWorkingSetExclusive
) ||
1043 (Thread
->OwnsSystemWorkingSetShared
) ||
1044 (Thread
->OwnsSessionWorkingSetExclusive
) ||
1045 (Thread
->OwnsSessionWorkingSetShared
));
1049 // Checks if the process owns the working set lock
1053 MI_WS_OWNER(IN PEPROCESS Process
)
1055 /* Check if this process is the owner, and that the thread owns the WS */
1056 if (PsGetCurrentThread()->OwnsProcessWorkingSetExclusive
== 0)
1058 DPRINT1("Thread: %p is not an owner\n", PsGetCurrentThread());
1060 if (KeGetCurrentThread()->ApcState
.Process
!= &Process
->Pcb
)
1062 DPRINT1("Current thread %p is attached to another process %p\n", PsGetCurrentThread(), Process
);
1064 return ((KeGetCurrentThread()->ApcState
.Process
== &Process
->Pcb
) &&
1065 ((PsGetCurrentThread()->OwnsProcessWorkingSetExclusive
) ||
1066 (PsGetCurrentThread()->OwnsProcessWorkingSetShared
)));
1070 // New ARM3<->RosMM PAGE Architecture
1074 MiIsRosSectionObject(IN PVOID Section
)
1076 PROS_SECTION_OBJECT RosSection
= Section
;
1077 if ((RosSection
->Type
== 'SC') && (RosSection
->Size
== 'TN')) return TRUE
;
1082 // HACK ON TOP OF HACK ALERT!!!
1083 #define MI_GET_ROS_DATA(x) \
1084 (((x)->RosMmData == 0) ? NULL : ((PMMROSPFN)((ULONG64)(ULONG)((x)->RosMmData) | \
1085 ((ULONG64)MmNonPagedPoolStart & 0xffffffff00000000ULL))))
1087 #define MI_GET_ROS_DATA(x) ((PMMROSPFN)(x->RosMmData))
1089 #define MI_IS_ROS_PFN(x) (((x)->u4.AweAllocation == TRUE) && (MI_GET_ROS_DATA(x) != NULL))
1090 #define ASSERT_IS_ROS_PFN(x) ASSERT(MI_IS_ROS_PFN(x) == TRUE);
1091 typedef struct _MMROSPFN
1093 PMM_RMAP_ENTRY RmapListHead
;
1094 SWAPENTRY SwapEntry
;
1095 } MMROSPFN
, *PMMROSPFN
;
1097 #define RosMmData AweReferenceCount
1101 MiDecrementReferenceCount(
1103 IN PFN_NUMBER PageFrameIndex
1108 MI_IS_WS_UNSAFE(IN PEPROCESS Process
)
1110 return (Process
->Vm
.Flags
.AcquiredUnsafe
== TRUE
);
1114 // Locks the working set for the given process
1118 MiLockProcessWorkingSet(IN PEPROCESS Process
,
1121 /* Shouldn't already be owning the process working set */
1122 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1123 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1125 /* Block APCs, make sure that still nothing is already held */
1126 KeEnterGuardedRegion();
1127 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1129 /* Lock the working set */
1130 ExAcquirePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1132 /* Now claim that we own the lock */
1133 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1134 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1135 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1140 MiLockProcessWorkingSetShared(IN PEPROCESS Process
,
1143 /* Shouldn't already be owning the process working set */
1144 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1145 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1147 /* Block APCs, make sure that still nothing is already held */
1148 KeEnterGuardedRegion();
1149 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1151 /* Lock the working set */
1152 ExAcquirePushLockShared(&Process
->Vm
.WorkingSetMutex
);
1154 /* Now claim that we own the lock */
1155 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1156 ASSERT(Thread
->OwnsProcessWorkingSetShared
== FALSE
);
1157 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1158 Thread
->OwnsProcessWorkingSetShared
= TRUE
;
1163 MiLockProcessWorkingSetUnsafe(IN PEPROCESS Process
,
1166 /* Shouldn't already be owning the process working set */
1167 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1169 /* APCs must be blocked, make sure that still nothing is already held */
1170 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1171 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1173 /* Lock the working set */
1174 ExAcquirePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1176 /* Now claim that we own the lock */
1177 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1178 Process
->Vm
.Flags
.AcquiredUnsafe
= 1;
1179 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1180 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1184 // Unlocks the working set for the given process
1188 MiUnlockProcessWorkingSet(IN PEPROCESS Process
,
1191 /* Make sure we are the owner of a safe acquisition */
1192 ASSERT(MI_WS_OWNER(Process
));
1193 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1195 /* The thread doesn't own it anymore */
1196 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== TRUE
);
1197 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1199 /* Release the lock and re-enable APCs */
1200 ExReleasePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1201 KeLeaveGuardedRegion();
1205 // Unlocks the working set for the given process
1209 MiUnlockProcessWorkingSetShared(IN PEPROCESS Process
,
1212 /* Make sure we are the owner of a safe acquisition (because shared) */
1213 ASSERT(MI_WS_OWNER(Process
));
1214 ASSERT(!MI_IS_WS_UNSAFE(Process
));
1216 /* Ensure we are in a shared acquisition */
1217 ASSERT(Thread
->OwnsProcessWorkingSetShared
== TRUE
);
1218 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== FALSE
);
1220 /* Don't claim the lock anylonger */
1221 Thread
->OwnsProcessWorkingSetShared
= FALSE
;
1223 /* Release the lock and re-enable APCs */
1224 ExReleasePushLockShared(&Process
->Vm
.WorkingSetMutex
);
1225 KeLeaveGuardedRegion();
1229 // Unlocks the working set for the given process
1233 MiUnlockProcessWorkingSetUnsafe(IN PEPROCESS Process
,
1236 /* Make sure we are the owner of an unsafe acquisition */
1237 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1238 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1239 ASSERT(MI_WS_OWNER(Process
));
1240 ASSERT(MI_IS_WS_UNSAFE(Process
));
1242 /* No longer unsafe */
1243 Process
->Vm
.Flags
.AcquiredUnsafe
= 0;
1245 /* The thread doesn't own it anymore */
1246 ASSERT(Thread
->OwnsProcessWorkingSetExclusive
== TRUE
);
1247 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1249 /* Release the lock but don't touch APC state */
1250 ExReleasePushLockExclusive(&Process
->Vm
.WorkingSetMutex
);
1251 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1255 // Locks the working set
1259 MiLockWorkingSet(IN PETHREAD Thread
,
1260 IN PMMSUPPORT WorkingSet
)
1263 KeEnterGuardedRegion();
1265 /* Working set should be in global memory */
1266 ASSERT(MI_IS_SESSION_ADDRESS((PVOID
)WorkingSet
) == FALSE
);
1268 /* Thread shouldn't already be owning something */
1269 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread
));
1271 /* Lock this working set */
1272 ExAcquirePushLockExclusive(&WorkingSet
->WorkingSetMutex
);
1274 /* Which working set is this? */
1275 if (WorkingSet
== &MmSystemCacheWs
)
1277 /* Own the system working set */
1278 ASSERT((Thread
->OwnsSystemWorkingSetExclusive
== FALSE
) &&
1279 (Thread
->OwnsSystemWorkingSetShared
== FALSE
));
1280 Thread
->OwnsSystemWorkingSetExclusive
= TRUE
;
1282 else if (WorkingSet
->Flags
.SessionSpace
)
1284 /* Own the session working set */
1285 ASSERT((Thread
->OwnsSessionWorkingSetExclusive
== FALSE
) &&
1286 (Thread
->OwnsSessionWorkingSetShared
== FALSE
));
1287 Thread
->OwnsSessionWorkingSetExclusive
= TRUE
;
1291 /* Own the process working set */
1292 ASSERT((Thread
->OwnsProcessWorkingSetExclusive
== FALSE
) &&
1293 (Thread
->OwnsProcessWorkingSetShared
== FALSE
));
1294 Thread
->OwnsProcessWorkingSetExclusive
= TRUE
;
1299 // Unlocks the working set
1303 MiUnlockWorkingSet(IN PETHREAD Thread
,
1304 IN PMMSUPPORT WorkingSet
)
1306 /* Working set should be in global memory */
1307 ASSERT(MI_IS_SESSION_ADDRESS((PVOID
)WorkingSet
) == FALSE
);
1309 /* Which working set is this? */
1310 if (WorkingSet
== &MmSystemCacheWs
)
1312 /* Release the system working set */
1313 ASSERT((Thread
->OwnsSystemWorkingSetExclusive
== TRUE
) ||
1314 (Thread
->OwnsSystemWorkingSetShared
== TRUE
));
1315 Thread
->OwnsSystemWorkingSetExclusive
= FALSE
;
1317 else if (WorkingSet
->Flags
.SessionSpace
)
1319 /* Release the session working set */
1320 ASSERT((Thread
->OwnsSessionWorkingSetExclusive
== TRUE
) ||
1321 (Thread
->OwnsSessionWorkingSetShared
== TRUE
));
1322 Thread
->OwnsSessionWorkingSetExclusive
= 0;
1326 /* Release the process working set */
1327 ASSERT((Thread
->OwnsProcessWorkingSetExclusive
) ||
1328 (Thread
->OwnsProcessWorkingSetShared
));
1329 Thread
->OwnsProcessWorkingSetExclusive
= FALSE
;
1332 /* Release the working set lock */
1333 ExReleasePushLockExclusive(&WorkingSet
->WorkingSetMutex
);
1336 KeLeaveGuardedRegion();
1341 MiUnlockProcessWorkingSetForFault(IN PEPROCESS Process
,
1344 OUT PBOOLEAN Shared
)
1346 ASSERT(MI_WS_OWNER(Process
));
1348 /* Check if the current owner is unsafe */
1349 if (MI_IS_WS_UNSAFE(Process
))
1351 /* Release unsafely */
1352 MiUnlockProcessWorkingSetUnsafe(Process
, Thread
);
1356 else if (Thread
->OwnsProcessWorkingSetExclusive
== 1)
1358 /* Owner is safe and exclusive, release normally */
1359 MiUnlockProcessWorkingSet(Process
, Thread
);
1365 /* Owner is shared (implies safe), release normally */
1366 MiUnlockProcessWorkingSetShared(Process
, Thread
);
1374 MiLockProcessWorkingSetForFault(IN PEPROCESS Process
,
1379 /* Check if this was a safe lock or not */
1384 /* Reacquire safely & shared */
1385 MiLockProcessWorkingSetShared(Process
, Thread
);
1389 /* Reacquire safely */
1390 MiLockProcessWorkingSet(Process
, Thread
);
1395 /* Unsafe lock cannot be shared */
1396 ASSERT(Shared
== FALSE
);
1397 /* Reacquire unsafely */
1398 MiLockProcessWorkingSetUnsafe(Process
, Thread
);
1403 // Returns the ProtoPTE inside a VAD for the given VPN
1407 MI_GET_PROTOTYPE_PTE_FOR_VPN(IN PMMVAD Vad
,
1412 /* Find the offset within the VAD's prototype PTEs */
1413 ProtoPte
= Vad
->FirstPrototypePte
+ (Vpn
- Vad
->StartingVpn
);
1414 ASSERT(ProtoPte
<= Vad
->LastContiguousPte
);
1419 // Returns the PFN Database entry for the given page number
1420 // Warning: This is not necessarily a valid PFN database entry!
1424 MI_PFN_ELEMENT(IN PFN_NUMBER Pfn
)
1427 return &MmPfnDatabase
[Pfn
];
1431 // Drops a locked page without dereferencing it
1435 MiDropLockCount(IN PMMPFN Pfn1
)
1437 /* This page shouldn't be locked, but it should be valid */
1438 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1439 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1441 /* Is this the last reference to the page */
1442 if (Pfn1
->u3
.e2
.ReferenceCount
== 1)
1444 /* It better not be valid */
1445 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1447 /* Is it a prototype PTE? */
1448 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1449 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1451 /* FIXME: We should return commit */
1452 DPRINT1("Not returning commit for prototype PTE\n");
1455 /* Update the counter */
1456 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1461 // Drops a locked page and dereferences it
1465 MiDereferencePfnAndDropLockCount(IN PMMPFN Pfn1
)
1467 USHORT RefCount
, OldRefCount
;
1468 PFN_NUMBER PageFrameIndex
;
1470 /* Loop while we decrement the page successfully */
1473 /* There should be at least one reference */
1474 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1475 ASSERT(OldRefCount
!= 0);
1477 /* Are we the last one */
1478 if (OldRefCount
== 1)
1480 /* The page shoudln't be shared not active at this point */
1481 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 1);
1482 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1483 ASSERT(Pfn1
->u2
.ShareCount
== 0);
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, and drop a reference the long way */
1494 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1495 PageFrameIndex
= MiGetPfnEntryIndex(Pfn1
);
1496 MiDecrementReferenceCount(Pfn1
, PageFrameIndex
);
1500 /* Drop a reference the short way, and that's it */
1501 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1504 ASSERT(RefCount
!= 0);
1505 } while (OldRefCount
!= RefCount
);
1507 /* If we got here, there should be more than one reference */
1508 ASSERT(RefCount
> 1);
1511 /* Is it still being shared? */
1512 if (Pfn1
->u2
.ShareCount
>= 1)
1514 /* Then it should be valid */
1515 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1517 /* Is it a prototype PTE? */
1518 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1519 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1521 /* We don't handle ethis */
1525 /* Update the counter */
1526 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1532 // References a locked page and updates the counter
1533 // Used in MmProbeAndLockPages to handle different edge cases
1537 MiReferenceProbedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1539 USHORT RefCount
, OldRefCount
;
1542 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1544 /* Does ARM3 own the page? */
1545 if (MI_IS_ROS_PFN(Pfn1
))
1547 /* ReactOS Mm doesn't track share count */
1548 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1552 /* On ARM3 pages, we should see a valid share count */
1553 ASSERT((Pfn1
->u2
.ShareCount
!= 0) && (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 /* FIXME: We should charge commit */
1560 DPRINT1("Not charging commit for prototype PTE\n");
1564 /* More locked pages! */
1565 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1567 /* Loop trying to update the reference count */
1570 /* Get the current reference count, make sure it's valid */
1571 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1572 ASSERT(OldRefCount
!= 0);
1573 ASSERT(OldRefCount
< 2500);
1575 /* Bump it up by one */
1576 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1579 ASSERT(RefCount
!= 0);
1580 } while (OldRefCount
!= RefCount
);
1582 /* Was this the first lock attempt? If not, undo our bump */
1583 if (OldRefCount
!= 1) InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1587 // References a locked page and updates the counter
1588 // Used in all other cases except MmProbeAndLockPages
1592 MiReferenceUsedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1596 /* Is it a prototype PTE? */
1597 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1598 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1600 /* FIXME: We should charge commit */
1601 DPRINT1("Not charging commit for prototype PTE\n");
1604 /* More locked pages! */
1605 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1607 /* Update the reference count */
1608 NewRefCount
= InterlockedIncrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1609 if (NewRefCount
== 2)
1611 /* Is it locked or shared? */
1612 if (Pfn1
->u2
.ShareCount
)
1614 /* It's shared, so make sure it's active */
1615 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1619 /* It's locked, so we shouldn't lock again */
1620 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1625 /* Someone had already locked the page, so undo our bump */
1626 ASSERT(NewRefCount
< 2500);
1627 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1632 // References a locked page and updates the counter
1633 // Used in all other cases except MmProbeAndLockPages
1637 MiReferenceUnusedPageAndBumpLockCount(IN PMMPFN Pfn1
)
1641 /* Make sure the page isn't used yet */
1642 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1643 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1645 /* Is it a prototype PTE? */
1646 if ((Pfn1
->u3
.e1
.PrototypePte
== 1) &&
1647 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 1))
1649 /* FIXME: We should charge commit */
1650 DPRINT1("Not charging commit for prototype PTE\n");
1653 /* More locked pages! */
1654 InterlockedIncrementSizeT(&MmSystemLockPagesCount
);
1656 /* Update the reference count */
1657 NewRefCount
= InterlockedIncrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1658 if (NewRefCount
!= 1)
1660 /* Someone had already locked the page, so undo our bump */
1661 ASSERT(NewRefCount
< 2500);
1662 InterlockedDecrementSizeT(&MmSystemLockPagesCount
);
1668 MiIncrementPageTableReferences(IN PVOID Address
)
1672 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1675 ASSERT(*RefCount
<= PTE_PER_PAGE
);
1680 MiDecrementPageTableReferences(IN PVOID Address
)
1684 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1687 ASSERT(*RefCount
< PTE_PER_PAGE
);
1692 MiQueryPageTableReferences(IN PVOID Address
)
1696 RefCount
= &MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)];
1705 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1710 MiInitializeSessionSpaceLayout();
1714 MiInitMachineDependent(
1715 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1720 MiComputeColorInformation(
1727 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1732 MiInitializeColorTables(
1738 MiInitializePfnDatabase(
1739 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1744 MiInitializeSessionWsSupport(
1750 MiInitializeSessionIds(
1756 MiInitializeMemoryEvents(
1763 IN PFN_NUMBER PageCount
1766 PPHYSICAL_MEMORY_DESCRIPTOR
1768 MmInitializeMemoryLimits(
1769 IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
1770 IN PBOOLEAN IncludeType
1775 MiPagesInLoaderBlock(
1776 IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
1777 IN PBOOLEAN IncludeType
1783 IN PVOID AddressStart
,
1789 MiRosProtectVirtualMemory(
1790 IN PEPROCESS Process
,
1791 IN OUT PVOID
*BaseAddress
,
1792 IN OUT PSIZE_T NumberOfBytesToProtect
,
1793 IN ULONG NewAccessProtection
,
1794 OUT PULONG OldAccessProtection OPTIONAL
1800 IN BOOLEAN StoreInstruction
,
1802 IN KPROCESSOR_MODE Mode
,
1803 IN PVOID TrapInformation
1808 MiCheckPdeForPagedPool(
1814 MiInitializeNonPagedPool(
1820 MiInitializeNonPagedPoolThresholds(
1826 MiInitializePoolEvents(
1833 IN POOL_TYPE PoolType
,// FIXFIX: This should go in ex.h after the pool merge
1834 IN ULONG Threshold
//
1837 // FIXFIX: THIS ONE TOO
1841 ExInitializePoolDescriptor(
1842 IN PPOOL_DESCRIPTOR PoolDescriptor
,
1843 IN POOL_TYPE PoolType
,
1851 MiInitializeSessionPool(
1857 MiInitializeSystemPtes(
1858 IN PMMPTE StartingPte
,
1859 IN ULONG NumberOfPtes
,
1860 IN MMSYSTEM_PTE_POOL_TYPE PoolType
1865 MiReserveSystemPtes(
1866 IN ULONG NumberOfPtes
,
1867 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1872 MiReleaseSystemPtes(
1873 IN PMMPTE StartingPte
,
1874 IN ULONG NumberOfPtes
,
1875 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1881 MiFindContiguousPages(
1882 IN PFN_NUMBER LowestPfn
,
1883 IN PFN_NUMBER HighestPfn
,
1884 IN PFN_NUMBER BoundaryPfn
,
1885 IN PFN_NUMBER SizeInPages
,
1886 IN MEMORY_CACHING_TYPE CacheType
1891 MiCheckForContiguousMemory(
1892 IN PVOID BaseAddress
,
1893 IN PFN_NUMBER BaseAddressPages
,
1894 IN PFN_NUMBER SizeInPages
,
1895 IN PFN_NUMBER LowestPfn
,
1896 IN PFN_NUMBER HighestPfn
,
1897 IN PFN_NUMBER BoundaryPfn
,
1898 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
1903 MiAllocatePagesForMdl(
1904 IN PHYSICAL_ADDRESS LowAddress
,
1905 IN PHYSICAL_ADDRESS HighAddress
,
1906 IN PHYSICAL_ADDRESS SkipBytes
,
1907 IN SIZE_T TotalBytes
,
1908 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
,
1914 MiMapLockedPagesInUserSpace(
1917 IN MEMORY_CACHING_TYPE CacheType
,
1918 IN PVOID BaseAddress
1923 MiUnmapLockedPagesInUserSpace(
1924 IN PVOID BaseAddress
,
1931 IN PMMPFNLIST ListHead
,
1932 IN PFN_NUMBER PageFrameIndex
1937 MiUnlinkFreeOrZeroedPage(
1943 MiUnlinkPageFromList(
1950 IN PMMPTE PointerPte
,
1957 IN PFN_NUMBER PageFrameIndex
,
1958 IN PMMPTE PointerPte
,
1964 MiInitializeAndChargePfn(
1965 OUT PPFN_NUMBER PageFrameIndex
,
1966 IN PMMPTE PointerPde
,
1967 IN PFN_NUMBER ContainingPageFrame
,
1968 IN BOOLEAN SessionAllocation
1973 MiInitializePfnAndMakePteValid(
1974 IN PFN_NUMBER PageFrameIndex
,
1975 IN PMMPTE PointerPte
,
1981 MiInitializePfnForOtherProcess(
1982 IN PFN_NUMBER PageFrameIndex
,
1983 IN PMMPTE PointerPte
,
1984 IN PFN_NUMBER PteFrame
1989 MiDecrementShareCount(
1991 IN PFN_NUMBER PageFrameIndex
2009 IN PFN_NUMBER PageFrameIndex
2014 MiInsertPageInFreeList(
2015 IN PFN_NUMBER PageFrameIndex
2020 MiDeleteSystemPageableVm(
2021 IN PMMPTE PointerPte
,
2022 IN PFN_NUMBER PageCount
,
2024 OUT PPFN_NUMBER ValidPages
2029 MiGetPageProtection(
2030 IN PMMPTE PointerPte
2033 PLDR_DATA_TABLE_ENTRY
2035 MiLookupDataTableEntry(
2041 MiInitializeDriverLargePageList(
2047 MiInitializeLargePageSupport(
2066 IN PVOID VirtualAddress
2071 MiCheckForConflictingNode(
2072 IN ULONG_PTR StartVpn
,
2073 IN ULONG_PTR EndVpn
,
2074 IN PMM_AVL_TABLE Table
2079 MiFindEmptyAddressRangeDownTree(
2081 IN ULONG_PTR BoundaryAddress
,
2082 IN ULONG_PTR Alignment
,
2083 IN PMM_AVL_TABLE Table
,
2084 OUT PULONG_PTR Base
,
2085 OUT PMMADDRESS_NODE
*Parent
2090 MiFindEmptyAddressRangeDownBasedTree(
2092 IN ULONG_PTR BoundaryAddress
,
2093 IN ULONG_PTR Alignment
,
2094 IN PMM_AVL_TABLE Table
,
2100 MiFindEmptyAddressRangeInTree(
2102 IN ULONG_PTR Alignment
,
2103 IN PMM_AVL_TABLE Table
,
2104 OUT PMMADDRESS_NODE
*PreviousVad
,
2114 IN ULONG ProtectionMask
2121 IN PEPROCESS Process
2126 MiInsertBasedSection(
2132 MiUnmapViewOfSection(
2133 IN PEPROCESS Process
,
2134 IN PVOID BaseAddress
,
2140 MiRosUnmapViewOfSection(
2141 IN PEPROCESS Process
,
2142 IN PVOID BaseAddress
,
2149 IN PMM_AVL_TABLE Table
,
2150 IN PMMADDRESS_NODE NewNode
,
2151 PMMADDRESS_NODE Parent
,
2152 TABLE_SEARCH_RESULT Result
2158 IN PMMADDRESS_NODE Node
,
2159 IN PMM_AVL_TABLE Table
2165 IN PMMADDRESS_NODE Node
2171 IN PMMADDRESS_NODE Node
2176 MiInitializeSystemSpaceMap(
2177 IN PMMSESSION InputSession OPTIONAL
2182 MiSessionRemoveProcess(
2188 MiReleaseProcessReferenceToSessionDataPage(
2189 IN PMM_SESSION_SPACE SessionGlobal
2194 MiSessionAddProcess(
2195 IN PEPROCESS NewProcess
2200 MiSessionCommitPageTables(
2207 MiMakeProtectionMask(
2213 MiDeleteVirtualAddresses(
2215 IN ULONG_PTR EndingAddress
,
2221 MiMakeSystemAddressValid(
2222 IN PVOID PageTableVirtualAddress
,
2223 IN PEPROCESS CurrentProcess
2228 MiMakeSystemAddressValidPfn(
2229 IN PVOID VirtualAddress
,
2236 IN PEPROCESS CurrentProcess
,
2249 MiQueryMemorySectionName(
2250 IN HANDLE ProcessHandle
,
2251 IN PVOID BaseAddress
,
2252 OUT PVOID MemoryInformation
,
2253 IN SIZE_T MemoryInformationLength
,
2254 OUT PSIZE_T ReturnLength
2259 MiRosUnmapViewInSystemSpace(
2265 MmDeterminePoolType(
2266 IN PVOID PoolAddress
2271 MiMakePdeExistAndMakeValid(
2272 IN PMMPTE PointerPde
,
2273 IN PEPROCESS TargetProcess
,
2278 // MiRemoveZeroPage will use inline code to zero out the page manually if only
2279 // free pages are available. In some scenarios, we don't/can't run that piece of
2280 // code and would rather only have a real zero page. If we can't have a zero page,
2281 // then we'd like to have our own code to grab a free page and zero it out, by
2282 // using MiRemoveAnyPage. This macro implements this.
2286 MiRemoveZeroPageSafe(IN ULONG Color
)
2288 if (MmFreePagesByColor
[ZeroedPageList
][Color
].Flink
!= LIST_HEAD
) return MiRemoveZeroPage(Color
);