2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/pool.c
5 * PURPOSE: ARM Memory Manager Pool Allocator
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 #line 15 "ARMĀ³::POOL"
16 #define MODULE_INVOLVED_IN_ARM3
17 #include "../ARM3/miarm.h"
19 /* GLOBALS ********************************************************************/
21 LIST_ENTRY MmNonPagedPoolFreeListHead
[MI_MAX_FREE_PAGE_LISTS
];
22 PFN_NUMBER MmNumberOfFreeNonPagedPool
, MiExpansionPoolPagesInitialCharge
;
23 PVOID MmNonPagedPoolEnd0
;
24 PFN_NUMBER MiStartOfInitialPoolFrame
, MiEndOfInitialPoolFrame
;
25 KGUARDED_MUTEX MmPagedPoolMutex
;
26 MM_PAGED_POOL_INFO MmPagedPoolInfo
;
27 SIZE_T MmAllocatedNonPagedPool
;
28 ULONG MmSpecialPoolTag
;
29 ULONG MmConsumedPoolPercentage
;
30 BOOLEAN MmProtectFreedNonPagedPool
;
32 /* PRIVATE FUNCTIONS **********************************************************/
36 MiProtectFreeNonPagedPool(IN PVOID VirtualAddress
,
39 PMMPTE PointerPte
, LastPte
;
42 /* If pool is physical, can't protect PTEs */
43 if (MI_IS_PHYSICAL_ADDRESS(VirtualAddress
)) return;
45 /* Get PTE pointers and loop */
46 PointerPte
= MiAddressToPte(VirtualAddress
);
47 LastPte
= PointerPte
+ PageCount
;
50 /* Capture the PTE for safety */
51 TempPte
= *PointerPte
;
53 /* Mark it as an invalid PTE, set proto bit to recognize it as pool */
54 TempPte
.u
.Hard
.Valid
= 0;
55 TempPte
.u
.Soft
.Prototype
= 1;
56 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
57 } while (++PointerPte
< LastPte
);
60 KeFlushEntireTb(TRUE
, TRUE
);
65 MiUnProtectFreeNonPagedPool(IN PVOID VirtualAddress
,
70 PFN_NUMBER UnprotectedPages
= 0;
72 /* If pool is physical, can't protect PTEs */
73 if (MI_IS_PHYSICAL_ADDRESS(VirtualAddress
)) return FALSE
;
75 /* Get, and capture the PTE */
76 PointerPte
= MiAddressToPte(VirtualAddress
);
77 TempPte
= *PointerPte
;
79 /* Loop protected PTEs */
80 while ((TempPte
.u
.Hard
.Valid
== 0) && (TempPte
.u
.Soft
.Prototype
== 1))
82 /* Unprotect the PTE */
83 TempPte
.u
.Hard
.Valid
= 1;
84 TempPte
.u
.Soft
.Prototype
= 0;
85 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
88 if (++UnprotectedPages
== PageCount
) break;
90 /* Capture next PTE */
91 TempPte
= *(++PointerPte
);
94 /* Return if any pages were unprotected */
95 return UnprotectedPages
? TRUE
: FALSE
;
100 MiProtectedPoolUnProtectLinks(IN PLIST_ENTRY Links
,
101 OUT PVOID
* PoolFlink
,
102 OUT PVOID
* PoolBlink
)
107 /* Initialize variables */
108 *PoolFlink
= *PoolBlink
= NULL
;
110 /* Check if the list has entries */
111 if (IsListEmpty(Links
) == FALSE
)
113 /* We are going to need to forward link to do an insert */
114 PoolVa
= Links
->Flink
;
116 /* So make it safe to access */
117 Safe
= MiUnProtectFreeNonPagedPool(PoolVa
, 1);
118 if (Safe
) PoolFlink
= PoolVa
;
121 /* Are we going to need a backward link too? */
122 if (Links
!= Links
->Blink
)
124 /* Get the head's backward link for the insert */
125 PoolVa
= Links
->Blink
;
127 /* Make it safe to access */
128 Safe
= MiUnProtectFreeNonPagedPool(PoolVa
, 1);
129 if (Safe
) PoolBlink
= PoolVa
;
135 MiProtectedPoolProtectLinks(IN PVOID PoolFlink
,
138 /* Reprotect the pages, if they got unprotected earlier */
139 if (PoolFlink
) MiProtectFreeNonPagedPool(PoolFlink
, 1);
140 if (PoolBlink
) MiProtectFreeNonPagedPool(PoolBlink
, 1);
145 MiProtectedPoolInsertList(IN PLIST_ENTRY ListHead
,
146 IN PLIST_ENTRY Entry
,
149 PVOID PoolFlink
, PoolBlink
;
151 /* Make the list accessible */
152 MiProtectedPoolUnProtectLinks(ListHead
, &PoolFlink
, &PoolBlink
);
154 /* Now insert in the right position */
155 Critical
? InsertHeadList(ListHead
, Entry
) : InsertTailList(ListHead
, Entry
);
157 /* And reprotect the pages containing the free links */
158 MiProtectedPoolProtectLinks(PoolFlink
, PoolBlink
);
163 MiProtectedPoolRemoveEntryList(IN PLIST_ENTRY Entry
)
165 PVOID PoolFlink
, PoolBlink
;
167 /* Make the list accessible */
168 MiProtectedPoolUnProtectLinks(Entry
, &PoolFlink
, &PoolBlink
);
171 RemoveEntryList(Entry
);
173 /* And reprotect the pages containing the free links */
174 if (PoolFlink
) MiProtectFreeNonPagedPool(PoolFlink
, 1);
175 if (PoolBlink
) MiProtectFreeNonPagedPool(PoolBlink
, 1);
180 MiInitializeNonPagedPoolThresholds(VOID
)
182 PFN_NUMBER Size
= MmMaximumNonPagedPoolInPages
;
184 /* Default low threshold of 8MB or one third of nonpaged pool */
185 MiLowNonPagedPoolThreshold
= (8 * _1MB
) >> PAGE_SHIFT
;
186 MiLowNonPagedPoolThreshold
= min(MiLowNonPagedPoolThreshold
, Size
/ 3);
188 /* Default high threshold of 20MB or 50% */
189 MiHighNonPagedPoolThreshold
= (20 * _1MB
) >> PAGE_SHIFT
;
190 MiHighNonPagedPoolThreshold
= min(MiHighNonPagedPoolThreshold
, Size
/ 2);
191 ASSERT(MiLowNonPagedPoolThreshold
< MiHighNonPagedPoolThreshold
);
196 MiInitializePoolEvents(VOID
)
199 PFN_NUMBER FreePoolInPages
;
201 /* Lock paged pool */
202 KeAcquireGuardedMutex(&MmPagedPoolMutex
);
204 /* Total size of the paged pool minus the allocated size, is free */
205 FreePoolInPages
= MmSizeOfPagedPoolInPages
- MmPagedPoolInfo
.AllocatedPagedPool
;
207 /* Check the initial state high state */
208 if (FreePoolInPages
>= MiHighPagedPoolThreshold
)
210 /* We have plenty of pool */
211 KeSetEvent(MiHighPagedPoolEvent
, 0, FALSE
);
216 KeClearEvent(MiHighPagedPoolEvent
);
219 /* Check the initial low state */
220 if (FreePoolInPages
<= MiLowPagedPoolThreshold
)
222 /* We're very low in free pool memory */
223 KeSetEvent(MiLowPagedPoolEvent
, 0, FALSE
);
228 KeClearEvent(MiLowPagedPoolEvent
);
231 /* Release the paged pool lock */
232 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
234 /* Now it's time for the nonpaged pool lock */
235 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
237 /* Free pages are the maximum minus what's been allocated */
238 FreePoolInPages
= MmMaximumNonPagedPoolInPages
- MmAllocatedNonPagedPool
;
240 /* Check if we have plenty */
241 if (FreePoolInPages
>= MiHighNonPagedPoolThreshold
)
243 /* We do, set the event */
244 KeSetEvent(MiHighNonPagedPoolEvent
, 0, FALSE
);
248 /* We don't, clear the event */
249 KeClearEvent(MiHighNonPagedPoolEvent
);
252 /* Check if we have very little */
253 if (FreePoolInPages
<= MiLowNonPagedPoolThreshold
)
255 /* We do, set the event */
256 KeSetEvent(MiLowNonPagedPoolEvent
, 0, FALSE
);
260 /* We don't, clear it */
261 KeClearEvent(MiLowNonPagedPoolEvent
);
264 /* We're done, release the nonpaged pool lock */
265 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
270 MiInitializeNonPagedPool(VOID
)
273 PFN_NUMBER PoolPages
;
274 PMMFREE_POOL_ENTRY FreeEntry
, FirstEntry
;
279 // We keep 4 lists of free pages (4 lists help avoid contention)
281 for (i
= 0; i
< MI_MAX_FREE_PAGE_LISTS
; i
++)
284 // Initialize each of them
286 InitializeListHead(&MmNonPagedPoolFreeListHead
[i
]);
290 // Calculate how many pages the initial nonpaged pool has
292 PoolPages
= BYTES_TO_PAGES(MmSizeOfNonPagedPoolInBytes
);
293 MmNumberOfFreeNonPagedPool
= PoolPages
;
296 // Initialize the first free entry
298 FreeEntry
= MmNonPagedPoolStart
;
299 FirstEntry
= FreeEntry
;
300 FreeEntry
->Size
= PoolPages
;
301 FreeEntry
->Signature
= MM_FREE_POOL_SIGNATURE
;
302 FreeEntry
->Owner
= FirstEntry
;
305 // Insert it into the last list
307 InsertHeadList(&MmNonPagedPoolFreeListHead
[MI_MAX_FREE_PAGE_LISTS
- 1],
311 // Now create free entries for every single other page
313 while (PoolPages
-- > 1)
316 // Link them all back to the original entry
318 FreeEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)FreeEntry
+ PAGE_SIZE
);
319 FreeEntry
->Owner
= FirstEntry
;
320 FreeEntry
->Signature
= MM_FREE_POOL_SIGNATURE
;
324 // Validate and remember first allocated pool page
326 PointerPte
= MiAddressToPte(MmNonPagedPoolStart
);
327 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
328 MiStartOfInitialPoolFrame
= PFN_FROM_PTE(PointerPte
);
331 // Keep track of where initial nonpaged pool ends
333 MmNonPagedPoolEnd0
= (PVOID
)((ULONG_PTR
)MmNonPagedPoolStart
+
334 MmSizeOfNonPagedPoolInBytes
);
337 // Validate and remember last allocated pool page
339 PointerPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)MmNonPagedPoolEnd0
- 1));
340 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
341 MiEndOfInitialPoolFrame
= PFN_FROM_PTE(PointerPte
);
344 // Validate the first nonpaged pool expansion page (which is a guard page)
346 PointerPte
= MiAddressToPte(MmNonPagedPoolExpansionStart
);
347 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
350 // Calculate the size of the expansion region alone
352 MiExpansionPoolPagesInitialCharge
=
353 BYTES_TO_PAGES(MmMaximumNonPagedPoolInBytes
- MmSizeOfNonPagedPoolInBytes
);
356 // Remove 2 pages, since there's a guard page on top and on the bottom
358 MiExpansionPoolPagesInitialCharge
-= 2;
361 // Now initialize the nonpaged pool expansion PTE space. Remember there's a
362 // guard page on top so make sure to skip it. The bottom guard page will be
363 // guaranteed by the fact our size is off by one.
365 MiInitializeSystemPtes(PointerPte
+ 1,
366 MiExpansionPoolPagesInitialCharge
,
367 NonPagedPoolExpansion
);
372 MiAllocatePoolPages(IN POOL_TYPE PoolType
,
373 IN SIZE_T SizeInBytes
)
375 PFN_NUMBER SizeInPages
, PageFrameNumber
;
378 PLIST_ENTRY NextEntry
, NextHead
, LastHead
;
379 PMMPTE PointerPte
, StartPte
;
385 PVOID BaseVa
, BaseVaStart
;
386 PMMFREE_POOL_ENTRY FreeEntry
;
387 PKSPIN_LOCK_QUEUE LockQueue
;
390 // Figure out how big the allocation is in pages
392 SizeInPages
= BYTES_TO_PAGES(SizeInBytes
);
397 if ((PoolType
& BASE_POOL_TYPE_MASK
) == PagedPool
)
400 // Lock the paged pool mutex
402 KeAcquireGuardedMutex(&MmPagedPoolMutex
);
405 // Find some empty allocation space
407 i
= RtlFindClearBitsAndSet(MmPagedPoolInfo
.PagedPoolAllocationMap
,
409 MmPagedPoolInfo
.PagedPoolHint
);
413 // Get the page bit count
415 i
= ((SizeInPages
- 1) / PTE_COUNT
) + 1;
416 DPRINT1("Paged pool expansion: %d %x\n", i
, SizeInPages
);
419 // Check if there is enougn paged pool expansion space left
421 if (MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
>
422 MiAddressToPte(MmPagedPoolInfo
.LastPteForPagedPool
))
427 DPRINT1("OUT OF PAGED POOL!!!\n");
428 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
433 // Check if we'll have to expand past the last PTE we have available
435 if (((i
- 1) + MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
) >
436 MiAddressToPte(MmPagedPoolInfo
.LastPteForPagedPool
))
439 // We can only support this much then
441 SizeInPages
= MiAddressToPte(MmPagedPoolInfo
.LastPteForPagedPool
) -
442 MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
+
444 ASSERT(SizeInPages
< i
);
450 // Otherwise, there is plenty of space left for this expansion
456 // Get the template PDE we'll use to expand
458 TempPde
= ValidKernelPde
;
461 // Get the first PTE in expansion space
463 PointerPde
= MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
;
464 BaseVa
= MiPteToAddress(PointerPde
);
465 BaseVaStart
= BaseVa
;
468 // Lock the PFN database and loop pages
470 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
474 // It should not already be valid
476 ASSERT(PointerPde
->u
.Hard
.Valid
== 0);
479 DPRINT1("Requesting %d PDEs\n", i
);
480 PageFrameNumber
= MiRemoveAnyPage(MI_GET_NEXT_COLOR());
481 TempPde
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
482 DPRINT1("We have a PDE: %lx\n", PageFrameNumber
);
484 #if (_MI_PAGING_LEVELS >= 3)
485 /* On PAE/x64 systems, there's no double-buffering */
489 // Save it into our double-buffered system page directory
491 MmSystemPagePtes
[((ULONG_PTR
)PointerPde
& (SYSTEM_PD_SIZE
- 1)) / sizeof(MMPTE
)] = TempPde
;
493 /* Initialize the PFN */
494 MiInitializePfnForOtherProcess(PageFrameNumber
,
496 MmSystemPageDirectory
[(PointerPde
- MiAddressToPde(NULL
)) / PDE_COUNT
]);
498 /* Write the actual PDE now */
499 MI_WRITE_VALID_PTE(PointerPde
, TempPde
);
502 // Move on to the next expansion address
505 BaseVa
= (PVOID
)((ULONG_PTR
)BaseVa
+ PAGE_SIZE
);
510 // Release the PFN database lock
512 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
515 // These pages are now available, clear their availablity bits
517 EndAllocation
= (MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
-
518 MiAddressToPte(MmPagedPoolInfo
.FirstPteForPagedPool
)) *
520 RtlClearBits(MmPagedPoolInfo
.PagedPoolAllocationMap
,
522 SizeInPages
* PTE_COUNT
);
525 // Update the next expansion location
527 MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
+= SizeInPages
;
530 // Zero out the newly available memory
532 RtlZeroMemory(BaseVaStart
, SizeInPages
* PAGE_SIZE
);
535 // Now try consuming the pages again
537 SizeInPages
= BYTES_TO_PAGES(SizeInBytes
);
538 i
= RtlFindClearBitsAndSet(MmPagedPoolInfo
.PagedPoolAllocationMap
,
546 DPRINT1("OUT OF PAGED POOL!!!\n");
547 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
553 // Update the pool hint if the request was just one page
555 if (SizeInPages
== 1) MmPagedPoolInfo
.PagedPoolHint
= i
+ 1;
558 // Update the end bitmap so we know the bounds of this allocation when
559 // the time comes to free it
561 EndAllocation
= i
+ SizeInPages
- 1;
562 RtlSetBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, EndAllocation
);
565 // Now we can release the lock (it mainly protects the bitmap)
567 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
570 // Now figure out where this allocation starts
572 BaseVa
= (PVOID
)((ULONG_PTR
)MmPagedPoolStart
+ (i
<< PAGE_SHIFT
));
577 KeFlushEntireTb(TRUE
, TRUE
);
579 /* Setup a demand-zero writable PTE */
580 MI_MAKE_SOFTWARE_PTE(&TempPte
, MM_READWRITE
);
583 // Find the first and last PTE, then loop them all
585 PointerPte
= MiAddressToPte(BaseVa
);
586 StartPte
= PointerPte
+ SizeInPages
;
590 // Write the demand zero PTE and keep going
592 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
593 } while (++PointerPte
< StartPte
);
596 // Return the allocation address to the caller
602 // Allocations of less than 4 pages go into their individual buckets
605 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
608 // Loop through all the free page lists based on the page index
610 NextHead
= &MmNonPagedPoolFreeListHead
[i
];
611 LastHead
= &MmNonPagedPoolFreeListHead
[MI_MAX_FREE_PAGE_LISTS
];
614 // Acquire the nonpaged pool lock
616 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
620 // Now loop through all the free page entries in this given list
622 NextEntry
= NextHead
->Flink
;
623 while (NextEntry
!= NextHead
)
625 /* Is freed non paged pool enabled */
626 if (MmProtectFreedNonPagedPool
)
628 /* We need to be able to touch this page, unprotect it */
629 MiUnProtectFreeNonPagedPool(NextEntry
, 0);
633 // Grab the entry and see if it can handle our allocation
635 FreeEntry
= CONTAINING_RECORD(NextEntry
, MMFREE_POOL_ENTRY
, List
);
636 ASSERT(FreeEntry
->Signature
== MM_FREE_POOL_SIGNATURE
);
637 if (FreeEntry
->Size
>= SizeInPages
)
640 // It does, so consume the pages from here
642 FreeEntry
->Size
-= SizeInPages
;
645 // The allocation will begin in this free page area
647 BaseVa
= (PVOID
)((ULONG_PTR
)FreeEntry
+
648 (FreeEntry
->Size
<< PAGE_SHIFT
));
650 /* Remove the item from the list, depending if pool is protected */
651 MmProtectFreedNonPagedPool
?
652 MiProtectedPoolRemoveEntryList(&FreeEntry
->List
) :
653 RemoveEntryList(&FreeEntry
->List
);
656 // However, check if its' still got space left
658 if (FreeEntry
->Size
!= 0)
660 /* Check which list to insert this entry into */
661 i
= FreeEntry
->Size
- 1;
662 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
664 /* Insert the entry into the free list head, check for prot. pool */
665 MmProtectFreedNonPagedPool
?
666 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
, TRUE
) :
667 InsertTailList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
);
669 /* Is freed non paged pool protected? */
670 if (MmProtectFreedNonPagedPool
)
672 /* Protect the freed pool! */
673 MiProtectFreeNonPagedPool(FreeEntry
, FreeEntry
->Size
);
678 // Grab the PTE for this allocation
680 PointerPte
= MiAddressToPte(BaseVa
);
681 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
684 // Grab the PFN NextEntry and index
686 Pfn1
= MiGetPfnEntry(PFN_FROM_PTE(PointerPte
));
689 // Now mark it as the beginning of an allocation
691 ASSERT(Pfn1
->u3
.e1
.StartOfAllocation
== 0);
692 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
694 /* Mark it as special pool if needed */
695 ASSERT(Pfn1
->u4
.VerifierAllocation
== 0);
696 if (PoolType
& 64) Pfn1
->u4
.VerifierAllocation
= 1;
699 // Check if the allocation is larger than one page
701 if (SizeInPages
!= 1)
704 // Navigate to the last PFN entry and PTE
706 PointerPte
+= SizeInPages
- 1;
707 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
708 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
712 // Mark this PFN as the last (might be the same as the first)
714 ASSERT(Pfn1
->u3
.e1
.EndOfAllocation
== 0);
715 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
718 // Release the nonpaged pool lock, and return the allocation
720 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
725 // Try the next free page entry
727 NextEntry
= FreeEntry
->List
.Flink
;
729 /* Is freed non paged pool protected? */
730 if (MmProtectFreedNonPagedPool
)
732 /* Protect the freed pool! */
733 MiProtectFreeNonPagedPool(FreeEntry
, FreeEntry
->Size
);
736 } while (++NextHead
< LastHead
);
739 // If we got here, we're out of space.
740 // Start by releasing the lock
742 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
745 // Allocate some system PTEs
747 StartPte
= MiReserveSystemPtes(SizeInPages
, NonPagedPoolExpansion
);
748 PointerPte
= StartPte
;
749 if (StartPte
== NULL
)
754 DPRINT1("Out of NP Expansion Pool\n");
759 // Acquire the pool lock now
761 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
764 // Lock the PFN database too
766 LockQueue
= &KeGetCurrentPrcb()->LockQueue
[LockQueuePfnLock
];
767 KeAcquireQueuedSpinLockAtDpcLevel(LockQueue
);
772 TempPte
= ValidKernelPte
;
775 /* Allocate a page */
776 PageFrameNumber
= MiRemoveAnyPage(MI_GET_NEXT_COLOR());
778 /* Get the PFN entry for it and fill it out */
779 Pfn1
= MiGetPfnEntry(PageFrameNumber
);
780 Pfn1
->u3
.e2
.ReferenceCount
= 1;
781 Pfn1
->u2
.ShareCount
= 1;
782 Pfn1
->PteAddress
= PointerPte
;
783 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
784 Pfn1
->u4
.VerifierAllocation
= 0;
786 /* Write the PTE for it */
787 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
788 MI_WRITE_VALID_PTE(PointerPte
++, TempPte
);
789 } while (--SizeInPages
> 0);
792 // This is the last page
794 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
797 // Get the first page and mark it as such
799 Pfn1
= MiGetPfnEntry(StartPte
->u
.Hard
.PageFrameNumber
);
800 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
802 /* Mark it as a verifier allocation if needed */
803 ASSERT(Pfn1
->u4
.VerifierAllocation
== 0);
804 if (PoolType
& 64) Pfn1
->u4
.VerifierAllocation
= 1;
807 // Release the PFN and nonpaged pool lock
809 KeReleaseQueuedSpinLockFromDpcLevel(LockQueue
);
810 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
813 // Return the address
815 return MiPteToAddress(StartPte
);
820 MiFreePoolPages(IN PVOID StartingVa
)
822 PMMPTE PointerPte
, StartPte
;
823 PMMPFN Pfn1
, StartPfn
;
824 PFN_NUMBER FreePages
, NumberOfPages
;
826 PMMFREE_POOL_ENTRY FreeEntry
, NextEntry
, LastEntry
;
832 if ((StartingVa
>= MmPagedPoolStart
) && (StartingVa
<= MmPagedPoolEnd
))
835 // Calculate the offset from the beginning of paged pool, and convert it
838 i
= ((ULONG_PTR
)StartingVa
- (ULONG_PTR
)MmPagedPoolStart
) >> PAGE_SHIFT
;
842 // Now use the end bitmap to scan until we find a set bit, meaning that
843 // this allocation finishes here
845 while (!RtlTestBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, End
)) End
++;
848 // Now calculate the total number of pages this allocation spans
850 NumberOfPages
= End
- i
+ 1;
852 /* Delete the actual pages */
853 PointerPte
= MmPagedPoolInfo
.FirstPteForPagedPool
+ i
;
854 FreePages
= MiDeleteSystemPageableVm(PointerPte
, NumberOfPages
, 0, NULL
);
855 ASSERT(FreePages
== NumberOfPages
);
858 // Acquire the paged pool lock
860 KeAcquireGuardedMutex(&MmPagedPoolMutex
);
863 // Clear the allocation and free bits
865 RtlClearBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, i
);
866 RtlClearBits(MmPagedPoolInfo
.PagedPoolAllocationMap
, i
, NumberOfPages
);
869 // Update the hint if we need to
871 if (i
< MmPagedPoolInfo
.PagedPoolHint
) MmPagedPoolInfo
.PagedPoolHint
= i
;
874 // Release the lock protecting the bitmaps
876 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
879 // And finally return the number of pages freed
881 return NumberOfPages
;
885 // Get the first PTE and its corresponding PFN entry
887 StartPte
= PointerPte
= MiAddressToPte(StartingVa
);
888 StartPfn
= Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
891 // Loop until we find the last PTE
893 while (Pfn1
->u3
.e1
.EndOfAllocation
== 0)
899 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
903 // Now we know how many pages we have
905 NumberOfPages
= PointerPte
- StartPte
+ 1;
908 // Acquire the nonpaged pool lock
910 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
913 // Mark the first and last PTEs as not part of an allocation anymore
915 StartPfn
->u3
.e1
.StartOfAllocation
= 0;
916 Pfn1
->u3
.e1
.EndOfAllocation
= 0;
919 // Assume we will free as many pages as the allocation was
921 FreePages
= NumberOfPages
;
924 // Peek one page past the end of the allocation
929 // Guard against going past initial nonpaged pool
931 if (MiGetPfnEntryIndex(Pfn1
) == MiEndOfInitialPoolFrame
)
934 // This page is on the outskirts of initial nonpaged pool, so ignore it
941 ASSERT((ULONG_PTR
)StartingVa
+ NumberOfPages
<= (ULONG_PTR
)MmNonPagedPoolEnd
);
943 /* Check if protected pool is enabled */
944 if (MmProtectFreedNonPagedPool
)
946 /* The freed block will be merged, it must be made accessible */
947 MiUnProtectFreeNonPagedPool(MiPteToAddress(PointerPte
), 0);
951 // Otherwise, our entire allocation must've fit within the initial non
952 // paged pool, or the expansion nonpaged pool, so get the PFN entry of
953 // the next allocation
955 if (PointerPte
->u
.Hard
.Valid
== 1)
958 // It's either expansion or initial: get the PFN entry
960 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
965 // This means we've reached the guard page that protects the end of
966 // the expansion nonpaged pool
974 // Check if this allocation actually exists
976 if ((Pfn1
) && (Pfn1
->u3
.e1
.StartOfAllocation
== 0))
979 // It doesn't, so we should actually locate a free entry descriptor
981 FreeEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)StartingVa
+
982 (NumberOfPages
<< PAGE_SHIFT
));
983 ASSERT(FreeEntry
->Signature
== MM_FREE_POOL_SIGNATURE
);
984 ASSERT(FreeEntry
->Owner
== FreeEntry
);
986 /* Consume this entry's pages */
987 FreePages
+= FreeEntry
->Size
;
989 /* Remove the item from the list, depending if pool is protected */
990 MmProtectFreedNonPagedPool
?
991 MiProtectedPoolRemoveEntryList(&FreeEntry
->List
) :
992 RemoveEntryList(&FreeEntry
->List
);
996 // Now get the official free entry we'll create for the caller's allocation
998 FreeEntry
= StartingVa
;
1001 // Check if the our allocation is the very first page
1003 if (MiGetPfnEntryIndex(StartPfn
) == MiStartOfInitialPoolFrame
)
1006 // Then we can't do anything or we'll risk underflowing
1013 // Otherwise, get the PTE for the page right before our allocation
1015 PointerPte
-= NumberOfPages
+ 1;
1017 /* Check if protected pool is enabled */
1018 if (MmProtectFreedNonPagedPool
)
1020 /* The freed block will be merged, it must be made accessible */
1021 MiUnProtectFreeNonPagedPool(MiPteToAddress(PointerPte
), 0);
1024 /* Check if this is valid pool, or a guard page */
1025 if (PointerPte
->u
.Hard
.Valid
== 1)
1028 // It's either expansion or initial nonpaged pool, get the PFN entry
1030 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
1035 // We must've reached the guard page, so don't risk touching it
1042 // Check if there is a valid PFN entry for the page before the allocation
1043 // and then check if this page was actually the end of an allocation.
1044 // If it wasn't, then we know for sure it's a free page
1046 if ((Pfn1
) && (Pfn1
->u3
.e1
.EndOfAllocation
== 0))
1049 // Get the free entry descriptor for that given page range
1051 FreeEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)StartingVa
- PAGE_SIZE
);
1052 ASSERT(FreeEntry
->Signature
== MM_FREE_POOL_SIGNATURE
);
1053 FreeEntry
= FreeEntry
->Owner
;
1055 /* Check if protected pool is enabled */
1056 if (MmProtectFreedNonPagedPool
)
1058 /* The freed block will be merged, it must be made accessible */
1059 MiUnProtectFreeNonPagedPool(FreeEntry
, 0);
1063 // Check if the entry is small enough to be indexed on a free list
1064 // If it is, we'll want to re-insert it, since we're about to
1065 // collapse our pages on top of it, which will change its count
1067 if (FreeEntry
->Size
< (MI_MAX_FREE_PAGE_LISTS
- 1))
1069 /* Remove the item from the list, depending if pool is protected */
1070 MmProtectFreedNonPagedPool
?
1071 MiProtectedPoolRemoveEntryList(&FreeEntry
->List
) :
1072 RemoveEntryList(&FreeEntry
->List
);
1077 FreeEntry
->Size
+= FreePages
;
1080 // And now find the new appropriate list to place it in
1082 i
= (ULONG
)(FreeEntry
->Size
- 1);
1083 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
1085 /* Insert the entry into the free list head, check for prot. pool */
1086 MmProtectFreedNonPagedPool
?
1087 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
, TRUE
) :
1088 InsertTailList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
);
1093 // Otherwise, just combine our free pages into this entry
1095 FreeEntry
->Size
+= FreePages
;
1100 // Check if we were unable to do any compaction, and we'll stick with this
1102 if (FreeEntry
== StartingVa
)
1105 // Well, now we are a free entry. At worse we just have our newly freed
1106 // pages, at best we have our pages plus whatever entry came after us
1108 FreeEntry
->Size
= FreePages
;
1111 // Find the appropriate list we should be on
1113 i
= FreeEntry
->Size
- 1;
1114 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
1116 /* Insert the entry into the free list head, check for prot. pool */
1117 MmProtectFreedNonPagedPool
?
1118 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
, TRUE
) :
1119 InsertTailList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
);
1123 // Just a sanity check
1125 ASSERT(FreePages
!= 0);
1128 // Get all the pages between our allocation and its end. These will all now
1129 // become free page chunks.
1131 NextEntry
= StartingVa
;
1132 LastEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)NextEntry
+ (FreePages
<< PAGE_SHIFT
));
1136 // Link back to the parent free entry, and keep going
1138 NextEntry
->Owner
= FreeEntry
;
1139 NextEntry
->Signature
= MM_FREE_POOL_SIGNATURE
;
1140 NextEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)NextEntry
+ PAGE_SIZE
);
1141 } while (NextEntry
!= LastEntry
);
1143 /* Is freed non paged pool protected? */
1144 if (MmProtectFreedNonPagedPool
)
1146 /* Protect the freed pool! */
1147 MiProtectFreeNonPagedPool(FreeEntry
, FreeEntry
->Size
);
1151 // We're done, release the lock and let the caller know how much we freed
1153 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
1154 return NumberOfPages
;
1160 MiRaisePoolQuota(IN POOL_TYPE PoolType
,
1161 IN ULONG CurrentMaxQuota
,
1162 OUT PULONG NewMaxQuota
)
1168 *NewMaxQuota
= CurrentMaxQuota
+ 65536;
1172 /* PUBLIC FUNCTIONS ***********************************************************/
1179 MmAllocateMappingAddress(IN SIZE_T NumberOfBytes
,
1191 MmFreeMappingAddress(IN PVOID BaseAddress
,