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
;
30 /* PRIVATE FUNCTIONS **********************************************************/
34 MiInitializeArmPool(VOID
)
38 PMMFREE_POOL_ENTRY FreeEntry
, FirstEntry
;
43 // We keep 4 lists of free pages (4 lists help avoid contention)
45 for (i
= 0; i
< MI_MAX_FREE_PAGE_LISTS
; i
++)
48 // Initialize each of them
50 InitializeListHead(&MmNonPagedPoolFreeListHead
[i
]);
54 // Calculate how many pages the initial nonpaged pool has
56 PoolPages
= BYTES_TO_PAGES(MmSizeOfNonPagedPoolInBytes
);
57 MmNumberOfFreeNonPagedPool
= PoolPages
;
60 // Initialize the first free entry
62 FreeEntry
= MmNonPagedPoolStart
;
63 FirstEntry
= FreeEntry
;
64 FreeEntry
->Size
= PoolPages
;
65 FreeEntry
->Owner
= FirstEntry
;
68 // Insert it into the last list
70 InsertHeadList(&MmNonPagedPoolFreeListHead
[MI_MAX_FREE_PAGE_LISTS
- 1],
74 // Now create free entries for every single other page
76 while (PoolPages
-- > 1)
79 // Link them all back to the original entry
81 FreeEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)FreeEntry
+ PAGE_SIZE
);
82 FreeEntry
->Owner
= FirstEntry
;
86 // Validate and remember first allocated pool page
88 PointerPte
= MiAddressToPte(MmNonPagedPoolStart
);
89 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
90 MiStartOfInitialPoolFrame
= PFN_FROM_PTE(PointerPte
);
93 // Keep track of where initial nonpaged pool ends
95 MmNonPagedPoolEnd0
= (PVOID
)((ULONG_PTR
)MmNonPagedPoolStart
+
96 MmSizeOfNonPagedPoolInBytes
);
99 // Validate and remember last allocated pool page
101 PointerPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)MmNonPagedPoolEnd0
- 1));
102 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
103 MiEndOfInitialPoolFrame
= PFN_FROM_PTE(PointerPte
);
106 // Validate the first nonpaged pool expansion page (which is a guard page)
108 PointerPte
= MiAddressToPte(MmNonPagedPoolExpansionStart
);
109 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
112 // Calculate the size of the expansion region alone
114 MiExpansionPoolPagesInitialCharge
=
115 BYTES_TO_PAGES(MmMaximumNonPagedPoolInBytes
- MmSizeOfNonPagedPoolInBytes
);
118 // Remove 2 pages, since there's a guard page on top and on the bottom
120 MiExpansionPoolPagesInitialCharge
-= 2;
123 // Now initialize the nonpaged pool expansion PTE space. Remember there's a
124 // guard page on top so make sure to skip it. The bottom guard page will be
125 // guaranteed by the fact our size is off by one.
127 MiInitializeSystemPtes(PointerPte
+ 1,
128 MiExpansionPoolPagesInitialCharge
,
129 NonPagedPoolExpansion
);
134 MiAllocatePoolPages(IN POOL_TYPE PoolType
,
135 IN SIZE_T SizeInBytes
)
137 PFN_NUMBER SizeInPages
, PageFrameNumber
;
140 PLIST_ENTRY NextEntry
, NextHead
, LastHead
;
141 PMMPTE PointerPte
, StartPte
;
145 PMMFREE_POOL_ENTRY FreeEntry
;
146 PKSPIN_LOCK_QUEUE LockQueue
;
149 // Figure out how big the allocation is in pages
151 SizeInPages
= BYTES_TO_PAGES(SizeInBytes
);
156 if (PoolType
== PagedPool
)
159 // Lock the paged pool mutex
161 KeAcquireGuardedMutex(&MmPagedPoolMutex
);
164 // Find some empty allocation space
166 i
= RtlFindClearBitsAndSet(MmPagedPoolInfo
.PagedPoolAllocationMap
,
168 MmPagedPoolInfo
.PagedPoolHint
);
174 DPRINT1("OUT OF PAGED POOL!!!\n");
175 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
180 // Update the pool hint if the request was just one page
182 if (SizeInPages
== 1) MmPagedPoolInfo
.PagedPoolHint
= i
+ 1;
185 // Update the end bitmap so we know the bounds of this allocation when
186 // the time comes to free it
188 RtlSetBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, i
+ SizeInPages
- 1);
191 // Now we can release the lock (it mainly protects the bitmap)
193 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
196 // Now figure out where this allocation starts
198 BaseVa
= (PVOID
)((ULONG_PTR
)MmPagedPoolStart
+ (i
<< PAGE_SHIFT
));
203 KeFlushEntireTb(TRUE
, TRUE
);
206 // Setup a demand-zero writable PTE
209 MI_MAKE_WRITE_PAGE(&TempPte
);
212 // Find the first and last PTE, then loop them all
214 PointerPte
= MiAddressToPte(BaseVa
);
215 StartPte
= PointerPte
+ SizeInPages
;
219 // Write the demand zero PTE and keep going
221 *PointerPte
++ = TempPte
;
222 } while (PointerPte
< StartPte
);
225 // Return the allocation address to the caller
231 // Allocations of less than 4 pages go into their individual buckets
234 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
237 // Loop through all the free page lists based on the page index
239 NextHead
= &MmNonPagedPoolFreeListHead
[i
];
240 LastHead
= &MmNonPagedPoolFreeListHead
[MI_MAX_FREE_PAGE_LISTS
];
243 // Acquire the nonpaged pool lock
245 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
249 // Now loop through all the free page entries in this given list
251 NextEntry
= NextHead
->Flink
;
252 while (NextEntry
!= NextHead
)
255 // Grab the entry and see if it can handle our allocation
257 FreeEntry
= CONTAINING_RECORD(NextEntry
, MMFREE_POOL_ENTRY
, List
);
258 if (FreeEntry
->Size
>= SizeInPages
)
261 // It does, so consume the pages from here
263 FreeEntry
->Size
-= SizeInPages
;
266 // The allocation will begin in this free page area
268 BaseVa
= (PVOID
)((ULONG_PTR
)FreeEntry
+
269 (FreeEntry
->Size
<< PAGE_SHIFT
));
272 // This is not a free page segment anymore
274 RemoveEntryList(&FreeEntry
->List
);
277 // However, check if its' still got space left
279 if (FreeEntry
->Size
!= 0)
282 // Insert it back into a different list, based on its pages
284 i
= FreeEntry
->Size
- 1;
285 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
286 InsertTailList (&MmNonPagedPoolFreeListHead
[i
],
291 // Grab the PTE for this allocation
293 PointerPte
= MiAddressToPte(BaseVa
);
294 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
297 // Grab the PFN NextEntry and index
299 Pfn1
= MiGetPfnEntry(PFN_FROM_PTE(PointerPte
));
302 // Now mark it as the beginning of an allocation
304 ASSERT(Pfn1
->u3
.e1
.StartOfAllocation
== 0);
305 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
308 // Check if the allocation is larger than one page
310 if (SizeInPages
!= 1)
313 // Navigate to the last PFN entry and PTE
315 PointerPte
+= SizeInPages
- 1;
316 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
317 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
321 // Mark this PFN as the last (might be the same as the first)
323 ASSERT(Pfn1
->u3
.e1
.EndOfAllocation
== 0);
324 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
327 // Release the nonpaged pool lock, and return the allocation
329 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
334 // Try the next free page entry
336 NextEntry
= FreeEntry
->List
.Flink
;
338 } while (++NextHead
< LastHead
);
341 // If we got here, we're out of space.
342 // Start by releasing the lock
344 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
347 // Allocate some system PTEs
349 StartPte
= MiReserveSystemPtes(SizeInPages
, NonPagedPoolExpansion
);
350 PointerPte
= StartPte
;
351 if (StartPte
== NULL
)
356 DPRINT1("Out of NP Expansion Pool\n");
361 // Acquire the pool lock now
363 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
366 // Lock the PFN database too
368 LockQueue
= &KeGetCurrentPrcb()->LockQueue
[LockQueuePfnLock
];
369 KeAcquireQueuedSpinLockAtDpcLevel(LockQueue
);
374 TempPte
= HyperTemplatePte
;
380 PageFrameNumber
= MmAllocPage(MC_NPPOOL
, 0);
383 // Get the PFN entry for it
385 Pfn1
= MiGetPfnEntry(PageFrameNumber
);
388 // Write the PTE for it
390 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
391 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
392 ASSERT(TempPte
.u
.Hard
.Valid
== 1);
393 *PointerPte
++ = TempPte
;
394 } while (--SizeInPages
> 0);
397 // This is the last page
399 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
402 // Get the first page and mark it as such
404 Pfn1
= MiGetPfnEntry(StartPte
->u
.Hard
.PageFrameNumber
);
405 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
408 // Release the PFN and nonpaged pool lock
410 KeReleaseQueuedSpinLockFromDpcLevel(LockQueue
);
411 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
414 // Return the address
416 return MiPteToAddress(StartPte
);
421 MiFreePoolPages(IN PVOID StartingVa
)
423 PMMPTE PointerPte
, StartPte
;
424 PMMPFN Pfn1
, StartPfn
;
425 PFN_NUMBER FreePages
, NumberOfPages
;
427 PMMFREE_POOL_ENTRY FreeEntry
, NextEntry
, LastEntry
;
433 if ((StartingVa
>= MmPagedPoolStart
) && (StartingVa
<= MmPagedPoolEnd
))
436 // Calculate the offset from the beginning of paged pool, and convert it
439 i
= ((ULONG_PTR
)StartingVa
- (ULONG_PTR
)MmPagedPoolStart
) >> PAGE_SHIFT
;
443 // Now use the end bitmap to scan until we find a set bit, meaning that
444 // this allocation finishes here
446 while (!RtlTestBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, End
)) End
++;
449 // Now calculate the total number of pages this allocation spans
451 NumberOfPages
= End
- i
+ 1;
454 // Acquire the paged pool lock
456 KeAcquireGuardedMutex(&MmPagedPoolMutex
);
459 // Clear the allocation and free bits
461 RtlClearBit(MmPagedPoolInfo
.EndOfPagedPoolBitmap
, i
);
462 RtlClearBits(MmPagedPoolInfo
.PagedPoolAllocationMap
, i
, NumberOfPages
);
465 // Update the hint if we need to
467 if (i
< MmPagedPoolInfo
.PagedPoolHint
) MmPagedPoolInfo
.PagedPoolHint
= i
;
470 // Release the lock protecting the bitmaps
472 KeReleaseGuardedMutex(&MmPagedPoolMutex
);
475 // And finally return the number of pages freed
477 return NumberOfPages
;
481 // Get the first PTE and its corresponding PFN entry
483 StartPte
= PointerPte
= MiAddressToPte(StartingVa
);
484 StartPfn
= Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
487 // Loop until we find the last PTE
489 while (Pfn1
->u3
.e1
.EndOfAllocation
== 0)
495 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
499 // Now we know how many pages we have
501 NumberOfPages
= PointerPte
- StartPte
+ 1;
504 // Acquire the nonpaged pool lock
506 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock
);
509 // Mark the first and last PTEs as not part of an allocation anymore
511 StartPfn
->u3
.e1
.StartOfAllocation
= 0;
512 Pfn1
->u3
.e1
.EndOfAllocation
= 0;
515 // Assume we will free as many pages as the allocation was
517 FreePages
= NumberOfPages
;
520 // Peek one page past the end of the allocation
525 // Guard against going past initial nonpaged pool
527 if (MiGetPfnEntryIndex(Pfn1
) == MiEndOfInitialPoolFrame
)
530 // This page is on the outskirts of initial nonpaged pool, so ignore it
537 // Otherwise, our entire allocation must've fit within the initial non
538 // paged pool, or the expansion nonpaged pool, so get the PFN entry of
539 // the next allocation
541 ASSERT((ULONG_PTR
)StartingVa
+ NumberOfPages
<= (ULONG_PTR
)MmNonPagedPoolEnd
);
542 if (PointerPte
->u
.Hard
.Valid
== 1)
545 // It's either expansion or initial: get the PFN entry
547 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
552 // This means we've reached the guard page that protects the end of
553 // the expansion nonpaged pool
561 // Check if this allocation actually exists
563 if ((Pfn1
) && (Pfn1
->u3
.e1
.StartOfAllocation
== 0))
566 // It doesn't, so we should actually locate a free entry descriptor
568 FreeEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)StartingVa
+
569 (NumberOfPages
<< PAGE_SHIFT
));
570 ASSERT(FreeEntry
->Owner
== FreeEntry
);
573 // Consume this entry's pages, and remove it from its free list
575 FreePages
+= FreeEntry
->Size
;
576 RemoveEntryList (&FreeEntry
->List
);
580 // Now get the official free entry we'll create for the caller's allocation
582 FreeEntry
= StartingVa
;
585 // Check if the our allocation is the very first page
587 if (MiGetPfnEntryIndex(StartPfn
) == MiStartOfInitialPoolFrame
)
590 // Then we can't do anything or we'll risk underflowing
597 // Otherwise, get the PTE for the page right before our allocation
599 PointerPte
-= NumberOfPages
+ 1;
600 if (PointerPte
->u
.Hard
.Valid
== 1)
603 // It's either expansion or initial nonpaged pool, get the PFN entry
605 Pfn1
= MiGetPfnEntry(PointerPte
->u
.Hard
.PageFrameNumber
);
610 // We must've reached the guard page, so don't risk touching it
617 // Check if there is a valid PFN entry for the page before the allocation
618 // and then check if this page was actually the end of an allocation.
619 // If it wasn't, then we know for sure it's a free page
621 if ((Pfn1
) && (Pfn1
->u3
.e1
.EndOfAllocation
== 0))
624 // Get the free entry descriptor for that given page range
626 FreeEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)StartingVa
- PAGE_SIZE
);
627 FreeEntry
= FreeEntry
->Owner
;
630 // Check if the entry is small enough to be indexed on a free list
631 // If it is, we'll want to re-insert it, since we're about to
632 // collapse our pages on top of it, which will change its count
634 if (FreeEntry
->Size
< (MI_MAX_FREE_PAGE_LISTS
- 1))
637 // Remove the list from where it is now
639 RemoveEntryList(&FreeEntry
->List
);
644 FreeEntry
->Size
+= FreePages
;
647 // And now find the new appropriate list to place it in
649 i
= (ULONG
)(FreeEntry
->Size
- 1);
650 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
655 InsertTailList(&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
);
660 // Otherwise, just combine our free pages into this entry
662 FreeEntry
->Size
+= FreePages
;
667 // Check if we were unable to do any compaction, and we'll stick with this
669 if (FreeEntry
== StartingVa
)
672 // Well, now we are a free entry. At worse we just have our newly freed
673 // pages, at best we have our pages plus whatever entry came after us
675 FreeEntry
->Size
= FreePages
;
678 // Find the appropriate list we should be on
680 i
= FreeEntry
->Size
- 1;
681 if (i
>= MI_MAX_FREE_PAGE_LISTS
) i
= MI_MAX_FREE_PAGE_LISTS
- 1;
686 InsertTailList (&MmNonPagedPoolFreeListHead
[i
], &FreeEntry
->List
);
690 // Just a sanity check
692 ASSERT(FreePages
!= 0);
695 // Get all the pages between our allocation and its end. These will all now
696 // become free page chunks.
698 NextEntry
= StartingVa
;
699 LastEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)NextEntry
+ (FreePages
<< PAGE_SHIFT
));
703 // Link back to the parent free entry, and keep going
705 NextEntry
->Owner
= FreeEntry
;
706 NextEntry
= (PMMFREE_POOL_ENTRY
)((ULONG_PTR
)NextEntry
+ PAGE_SIZE
);
707 } while (NextEntry
!= LastEntry
);
710 // We're done, release the lock and let the caller know how much we freed
712 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock
, OldIrql
);
713 return NumberOfPages
;
719 MiRaisePoolQuota(IN POOL_TYPE PoolType
,
720 IN ULONG CurrentMaxQuota
,
721 OUT PULONG NewMaxQuota
)
727 *NewMaxQuota
= CurrentMaxQuota
+ 65536;
731 /* PUBLIC FUNCTIONS ***********************************************************/
738 MmAllocateMappingAddress(IN SIZE_T NumberOfBytes
,
750 MmFreeMappingAddress(IN PVOID BaseAddress
,