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
;
382 PVOID BaseVa
, BaseVaStart
;
383 PMMFREE_POOL_ENTRY FreeEntry
;
384 PKSPIN_LOCK_QUEUE LockQueue
;
387 // Figure out how big the allocation is in pages
389 SizeInPages
= BYTES_TO_PAGES(SizeInBytes
);
394 if ((PoolType
& BASE_POOL_TYPE_MASK
) == PagedPool
)
397 // Lock the paged pool mutex
399 KeAcquireGuardedMutex(&MmPagedPoolMutex
);
402 // Find some empty allocation space
404 i
= RtlFindClearBitsAndSet(MmPagedPoolInfo
.PagedPoolAllocationMap
,
406 MmPagedPoolInfo
.PagedPoolHint
);
410 // Get the page bit count
412 i
= ((SizeInPages
- 1) / 1024) + 1;
413 DPRINT1("Paged pool expansion: %d %x\n", i
, SizeInPages
);
416 // Check if there is enougn paged pool expansion space left
418 if (MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
>
419 MiAddressToPte(MmPagedPoolInfo
.LastPteForPagedPool
))
424 DPRINT1("OUT OF PAGED POOL!!!\n");
425 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
430 // Check if we'll have to expand past the last PTE we have available
432 if (((i
- 1) + MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
) >
433 MiAddressToPte(MmPagedPoolInfo
.LastPteForPagedPool
))
436 // We can only support this much then
438 SizeInPages
= MiAddressToPte(MmPagedPoolInfo
.LastPteForPagedPool
) -
439 MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
+
441 ASSERT(SizeInPages
< i
);
447 // Otherwise, there is plenty of space left for this expansion
453 // Get the template PTE we'll use to expand
455 TempPte
= ValidKernelPte
;
458 // Get the first PTE in expansion space
460 PointerPte
= MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
;
461 BaseVa
= MiPteToAddress(PointerPte
);
462 BaseVaStart
= BaseVa
;
465 // Lock the PFN database and loop pages
467 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
471 // It should not already be valid
473 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
476 PageFrameNumber
= MiRemoveAnyPage(0);
477 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
479 #if (_MI_PAGING_LEVELS >= 3)
480 /* On PAE/x64 systems, there's no double-buffering */
484 // Save it into our double-buffered system page directory
486 /* This seems to be making the assumption that one PDE is one page long */
487 C_ASSERT(PAGE_SIZE
== (PD_COUNT
* (sizeof(MMPTE
) * PDE_COUNT
)));
488 MmSystemPagePtes
[(ULONG_PTR
)PointerPte
& (PAGE_SIZE
- 1) /
489 sizeof(MMPTE
)] = TempPte
;
491 /* Initialize the PFN */
492 MiInitializePfnForOtherProcess(PageFrameNumber
,
494 MmSystemPageDirectory
[(PointerPte
- (PMMPTE
)PDE_BASE
) / PDE_COUNT
]);
496 /* Write the actual PTE now */
497 MI_WRITE_VALID_PTE(PointerPte
++, TempPte
);
500 // Move on to the next expansion address
502 BaseVa
= (PVOID
)((ULONG_PTR
)BaseVa
+ PAGE_SIZE
);
506 // Release the PFN database lock
508 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
511 // These pages are now available, clear their availablity bits
513 RtlClearBits(MmPagedPoolInfo
.PagedPoolAllocationMap
,
514 (MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
-
515 MiAddressToPte(MmPagedPoolInfo
.FirstPteForPagedPool
)) *
520 // Update the next expansion location
522 MmPagedPoolInfo
.NextPdeForPagedPoolExpansion
+= SizeInPages
;
525 // Zero out the newly available memory
527 RtlZeroMemory(BaseVaStart
, SizeInPages
* PAGE_SIZE
);
530 // Now try consuming the pages again
532 SizeInPages
= BYTES_TO_PAGES(SizeInBytes
);
533 i
= RtlFindClearBitsAndSet(MmPagedPoolInfo
.PagedPoolAllocationMap
,
541 DPRINT1("OUT OF PAGED POOL!!!\n");
542 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
548 // Update the pool hint if the request was just one page
550 if (SizeInPages
== 1) MmPagedPoolInfo
.PagedPoolHint
= i
+ 1;
553 // Update the end bitmap so we know the bounds of this allocation when
554 // the time comes to free it
556 RtlSetBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, i
+ SizeInPages
- 1);
559 // Now we can release the lock (it mainly protects the bitmap)
561 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
564 // Now figure out where this allocation starts
566 BaseVa
= (PVOID
)((ULONG_PTR
)MmPagedPoolStart
+ (i
<< PAGE_SHIFT
));
571 KeFlushEntireTb(TRUE
, TRUE
);
573 /* Setup a demand-zero writable PTE */
574 MI_MAKE_SOFTWARE_PTE(&TempPte
, MM_READWRITE
);
577 // Find the first and last PTE, then loop them all
579 PointerPte
= MiAddressToPte(BaseVa
);
580 StartPte
= PointerPte
+ SizeInPages
;
584 // Write the demand zero PTE and keep going
586 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
587 *PointerPte
++ = TempPte
;
588 } while (PointerPte
< StartPte
);
591 // Return the allocation address to the caller
597 // Allocations of less than 4 pages go into their individual buckets
600 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
603 // Loop through all the free page lists based on the page index
605 NextHead
= &MmNonPagedPoolFreeListHead
[i
];
606 LastHead
= &MmNonPagedPoolFreeListHead
[MI_MAX_FREE_PAGE_LISTS
];
609 // Acquire the nonpaged pool lock
611 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
615 // Now loop through all the free page entries in this given list
617 NextEntry
= NextHead
->Flink
;
618 while (NextEntry
!= NextHead
)
620 /* Is freed non paged pool enabled */
621 if (MmProtectFreedNonPagedPool
)
623 /* We need to be able to touch this page, unprotect it */
624 MiUnProtectFreeNonPagedPool(NextEntry
, 0);
628 // Grab the entry and see if it can handle our allocation
630 FreeEntry
= CONTAINING_RECORD(NextEntry
, MMFREE_POOL_ENTRY
, List
);
631 ASSERT(FreeEntry
->Signature
== MM_FREE_POOL_SIGNATURE
);
632 if (FreeEntry
->Size
>= SizeInPages
)
635 // It does, so consume the pages from here
637 FreeEntry
->Size
-= SizeInPages
;
640 // The allocation will begin in this free page area
642 BaseVa
= (PVOID
)((ULONG_PTR
)FreeEntry
+
643 (FreeEntry
->Size
<< PAGE_SHIFT
));
645 /* Remove the item from the list, depending if pool is protected */
646 MmProtectFreedNonPagedPool
?
647 MiProtectedPoolRemoveEntryList(&FreeEntry
->List
) :
648 RemoveEntryList(&FreeEntry
->List
);
651 // However, check if its' still got space left
653 if (FreeEntry
->Size
!= 0)
655 /* Check which list to insert this entry into */
656 i
= FreeEntry
->Size
- 1;
657 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
659 /* Insert the entry into the free list head, check for prot. pool */
660 MmProtectFreedNonPagedPool
?
661 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
, TRUE
) :
662 InsertTailList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
);
664 /* Is freed non paged pool protected? */
665 if (MmProtectFreedNonPagedPool
)
667 /* Protect the freed pool! */
668 MiProtectFreeNonPagedPool(FreeEntry
, FreeEntry
->Size
);
673 // Grab the PTE for this allocation
675 PointerPte
= MiAddressToPte(BaseVa
);
676 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
679 // Grab the PFN NextEntry and index
681 Pfn1
= MiGetPfnEntry(PFN_FROM_PTE(PointerPte
));
684 // Now mark it as the beginning of an allocation
686 ASSERT(Pfn1
->u3
.e1
.StartOfAllocation
== 0);
687 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
690 // Check if the allocation is larger than one page
692 if (SizeInPages
!= 1)
695 // Navigate to the last PFN entry and PTE
697 PointerPte
+= SizeInPages
- 1;
698 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
699 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
703 // Mark this PFN as the last (might be the same as the first)
705 ASSERT(Pfn1
->u3
.e1
.EndOfAllocation
== 0);
706 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
709 // Release the nonpaged pool lock, and return the allocation
711 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
716 // Try the next free page entry
718 NextEntry
= FreeEntry
->List
.Flink
;
720 /* Is freed non paged pool protected? */
721 if (MmProtectFreedNonPagedPool
)
723 /* Protect the freed pool! */
724 MiProtectFreeNonPagedPool(FreeEntry
, FreeEntry
->Size
);
727 } while (++NextHead
< LastHead
);
730 // If we got here, we're out of space.
731 // Start by releasing the lock
733 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
736 // Allocate some system PTEs
738 StartPte
= MiReserveSystemPtes(SizeInPages
, NonPagedPoolExpansion
);
739 PointerPte
= StartPte
;
740 if (StartPte
== NULL
)
745 DPRINT1("Out of NP Expansion Pool\n");
750 // Acquire the pool lock now
752 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
755 // Lock the PFN database too
757 LockQueue
= &KeGetCurrentPrcb()->LockQueue
[LockQueuePfnLock
];
758 KeAcquireQueuedSpinLockAtDpcLevel(LockQueue
);
763 TempPte
= ValidKernelPte
;
766 /* Allocate a page */
767 PageFrameNumber
= MiRemoveAnyPage(0);
769 /* Get the PFN entry for it and fill it out */
770 Pfn1
= MiGetPfnEntry(PageFrameNumber
);
771 Pfn1
->u3
.e2
.ReferenceCount
= 1;
772 Pfn1
->u2
.ShareCount
= 1;
773 Pfn1
->PteAddress
= PointerPte
;
774 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
775 Pfn1
->u4
.VerifierAllocation
= 0;
777 /* Write the PTE for it */
778 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
779 MI_WRITE_VALID_PTE(PointerPte
++, TempPte
);
780 } while (--SizeInPages
> 0);
783 // This is the last page
785 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
788 // Get the first page and mark it as such
790 Pfn1
= MiGetPfnEntry(StartPte
->u
.Hard
.PageFrameNumber
);
791 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
794 // Release the PFN and nonpaged pool lock
796 KeReleaseQueuedSpinLockFromDpcLevel(LockQueue
);
797 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
800 // Return the address
802 return MiPteToAddress(StartPte
);
807 MiFreePoolPages(IN PVOID StartingVa
)
809 PMMPTE PointerPte
, StartPte
;
810 PMMPFN Pfn1
, StartPfn
;
811 PFN_NUMBER FreePages
, NumberOfPages
;
813 PMMFREE_POOL_ENTRY FreeEntry
, NextEntry
, LastEntry
;
819 if ((StartingVa
>= MmPagedPoolStart
) && (StartingVa
<= MmPagedPoolEnd
))
822 // Calculate the offset from the beginning of paged pool, and convert it
825 i
= ((ULONG_PTR
)StartingVa
- (ULONG_PTR
)MmPagedPoolStart
) >> PAGE_SHIFT
;
829 // Now use the end bitmap to scan until we find a set bit, meaning that
830 // this allocation finishes here
832 while (!RtlTestBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, End
)) End
++;
835 // Now calculate the total number of pages this allocation spans
837 NumberOfPages
= End
- i
+ 1;
839 /* Delete the actual pages */
840 PointerPte
= MmPagedPoolInfo
.FirstPteForPagedPool
+ i
;
841 FreePages
= MiDeleteSystemPageableVm(PointerPte
, NumberOfPages
, 0, NULL
);
842 ASSERT(FreePages
== NumberOfPages
);
845 // Acquire the paged pool lock
847 KeAcquireGuardedMutex(&MmPagedPoolMutex
);
850 // Clear the allocation and free bits
852 RtlClearBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, i
);
853 RtlClearBits(MmPagedPoolInfo
.PagedPoolAllocationMap
, i
, NumberOfPages
);
856 // Update the hint if we need to
858 if (i
< MmPagedPoolInfo
.PagedPoolHint
) MmPagedPoolInfo
.PagedPoolHint
= i
;
861 // Release the lock protecting the bitmaps
863 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
866 // And finally return the number of pages freed
868 return NumberOfPages
;
872 // Get the first PTE and its corresponding PFN entry
874 StartPte
= PointerPte
= MiAddressToPte(StartingVa
);
875 StartPfn
= Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
878 // Loop until we find the last PTE
880 while (Pfn1
->u3
.e1
.EndOfAllocation
== 0)
886 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
890 // Now we know how many pages we have
892 NumberOfPages
= PointerPte
- StartPte
+ 1;
895 // Acquire the nonpaged pool lock
897 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
900 // Mark the first and last PTEs as not part of an allocation anymore
902 StartPfn
->u3
.e1
.StartOfAllocation
= 0;
903 Pfn1
->u3
.e1
.EndOfAllocation
= 0;
906 // Assume we will free as many pages as the allocation was
908 FreePages
= NumberOfPages
;
911 // Peek one page past the end of the allocation
916 // Guard against going past initial nonpaged pool
918 if (MiGetPfnEntryIndex(Pfn1
) == MiEndOfInitialPoolFrame
)
921 // This page is on the outskirts of initial nonpaged pool, so ignore it
928 ASSERT((ULONG_PTR
)StartingVa
+ NumberOfPages
<= (ULONG_PTR
)MmNonPagedPoolEnd
);
930 /* Check if protected pool is enabled */
931 if (MmProtectFreedNonPagedPool
)
933 /* The freed block will be merged, it must be made accessible */
934 MiUnProtectFreeNonPagedPool(MiPteToAddress(PointerPte
), 0);
938 // Otherwise, our entire allocation must've fit within the initial non
939 // paged pool, or the expansion nonpaged pool, so get the PFN entry of
940 // the next allocation
942 if (PointerPte
->u
.Hard
.Valid
== 1)
945 // It's either expansion or initial: get the PFN entry
947 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
952 // This means we've reached the guard page that protects the end of
953 // the expansion nonpaged pool
961 // Check if this allocation actually exists
963 if ((Pfn1
) && (Pfn1
->u3
.e1
.StartOfAllocation
== 0))
966 // It doesn't, so we should actually locate a free entry descriptor
968 FreeEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)StartingVa
+
969 (NumberOfPages
<< PAGE_SHIFT
));
970 ASSERT(FreeEntry
->Signature
== MM_FREE_POOL_SIGNATURE
);
971 ASSERT(FreeEntry
->Owner
== FreeEntry
);
973 /* Consume this entry's pages */
974 FreePages
+= FreeEntry
->Size
;
976 /* Remove the item from the list, depending if pool is protected */
977 MmProtectFreedNonPagedPool
?
978 MiProtectedPoolRemoveEntryList(&FreeEntry
->List
) :
979 RemoveEntryList(&FreeEntry
->List
);
983 // Now get the official free entry we'll create for the caller's allocation
985 FreeEntry
= StartingVa
;
988 // Check if the our allocation is the very first page
990 if (MiGetPfnEntryIndex(StartPfn
) == MiStartOfInitialPoolFrame
)
993 // Then we can't do anything or we'll risk underflowing
1000 // Otherwise, get the PTE for the page right before our allocation
1002 PointerPte
-= NumberOfPages
+ 1;
1004 /* Check if protected pool is enabled */
1005 if (MmProtectFreedNonPagedPool
)
1007 /* The freed block will be merged, it must be made accessible */
1008 MiUnProtectFreeNonPagedPool(MiPteToAddress(PointerPte
), 0);
1011 /* Check if this is valid pool, or a guard page */
1012 if (PointerPte
->u
.Hard
.Valid
== 1)
1015 // It's either expansion or initial nonpaged pool, get the PFN entry
1017 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
1022 // We must've reached the guard page, so don't risk touching it
1029 // Check if there is a valid PFN entry for the page before the allocation
1030 // and then check if this page was actually the end of an allocation.
1031 // If it wasn't, then we know for sure it's a free page
1033 if ((Pfn1
) && (Pfn1
->u3
.e1
.EndOfAllocation
== 0))
1036 // Get the free entry descriptor for that given page range
1038 FreeEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)StartingVa
- PAGE_SIZE
);
1039 ASSERT(FreeEntry
->Signature
== MM_FREE_POOL_SIGNATURE
);
1040 FreeEntry
= FreeEntry
->Owner
;
1042 /* Check if protected pool is enabled */
1043 if (MmProtectFreedNonPagedPool
)
1045 /* The freed block will be merged, it must be made accessible */
1046 MiUnProtectFreeNonPagedPool(FreeEntry
, 0);
1050 // Check if the entry is small enough to be indexed on a free list
1051 // If it is, we'll want to re-insert it, since we're about to
1052 // collapse our pages on top of it, which will change its count
1054 if (FreeEntry
->Size
< (MI_MAX_FREE_PAGE_LISTS
- 1))
1056 /* Remove the item from the list, depending if pool is protected */
1057 MmProtectFreedNonPagedPool
?
1058 MiProtectedPoolRemoveEntryList(&FreeEntry
->List
) :
1059 RemoveEntryList(&FreeEntry
->List
);
1064 FreeEntry
->Size
+= FreePages
;
1067 // And now find the new appropriate list to place it in
1069 i
= (ULONG
)(FreeEntry
->Size
- 1);
1070 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
1072 /* Insert the entry into the free list head, check for prot. pool */
1073 MmProtectFreedNonPagedPool
?
1074 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
, TRUE
) :
1075 InsertTailList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
);
1080 // Otherwise, just combine our free pages into this entry
1082 FreeEntry
->Size
+= FreePages
;
1087 // Check if we were unable to do any compaction, and we'll stick with this
1089 if (FreeEntry
== StartingVa
)
1092 // Well, now we are a free entry. At worse we just have our newly freed
1093 // pages, at best we have our pages plus whatever entry came after us
1095 FreeEntry
->Size
= FreePages
;
1098 // Find the appropriate list we should be on
1100 i
= FreeEntry
->Size
- 1;
1101 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
1103 /* Insert the entry into the free list head, check for prot. pool */
1104 MmProtectFreedNonPagedPool
?
1105 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
, TRUE
) :
1106 InsertTailList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
);
1110 // Just a sanity check
1112 ASSERT(FreePages
!= 0);
1115 // Get all the pages between our allocation and its end. These will all now
1116 // become free page chunks.
1118 NextEntry
= StartingVa
;
1119 LastEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)NextEntry
+ (FreePages
<< PAGE_SHIFT
));
1123 // Link back to the parent free entry, and keep going
1125 NextEntry
->Owner
= FreeEntry
;
1126 NextEntry
->Signature
= MM_FREE_POOL_SIGNATURE
;
1127 NextEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)NextEntry
+ PAGE_SIZE
);
1128 } while (NextEntry
!= LastEntry
);
1130 /* Is freed non paged pool protected? */
1131 if (MmProtectFreedNonPagedPool
)
1133 /* Protect the freed pool! */
1134 MiProtectFreeNonPagedPool(FreeEntry
, FreeEntry
->Size
);
1138 // We're done, release the lock and let the caller know how much we freed
1140 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
1141 return NumberOfPages
;
1147 MiRaisePoolQuota(IN POOL_TYPE PoolType
,
1148 IN ULONG CurrentMaxQuota
,
1149 OUT PULONG NewMaxQuota
)
1155 *NewMaxQuota
= CurrentMaxQuota
+ 65536;
1159 /* PUBLIC FUNCTIONS ***********************************************************/
1166 MmAllocateMappingAddress(IN SIZE_T NumberOfBytes
,
1178 MmFreeMappingAddress(IN PVOID BaseAddress
,