[NTOS]: Kill some debug spew.
[reactos.git] / reactos / ntoskrnl / mm / ARM3 / pool.c
1 /*
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
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 #line 15 "ARMĀ³::POOL"
16 #define MODULE_INVOLVED_IN_ARM3
17 #include "../ARM3/miarm.h"
18
19 /* GLOBALS ********************************************************************/
20
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;
31
32 /* PRIVATE FUNCTIONS **********************************************************/
33
34 VOID
35 NTAPI
36 MiInitializeNonPagedPoolThresholds(VOID)
37 {
38 PFN_NUMBER Size = MmMaximumNonPagedPoolInPages;
39
40 /* Default low threshold of 8MB or one third of nonpaged pool */
41 MiLowNonPagedPoolThreshold = (8 * _1MB) >> PAGE_SHIFT;
42 MiLowNonPagedPoolThreshold = min(MiLowNonPagedPoolThreshold, Size / 3);
43
44 /* Default high threshold of 20MB or 50% */
45 MiHighNonPagedPoolThreshold = (20 * _1MB) >> PAGE_SHIFT;
46 MiHighNonPagedPoolThreshold = min(MiHighNonPagedPoolThreshold, Size / 2);
47 ASSERT(MiLowNonPagedPoolThreshold < MiHighNonPagedPoolThreshold);
48 }
49
50 VOID
51 NTAPI
52 MiInitializePoolEvents(VOID)
53 {
54 KIRQL OldIrql;
55 PFN_NUMBER FreePoolInPages;
56
57 /* Lock paged pool */
58 KeAcquireGuardedMutex(&MmPagedPoolMutex);
59
60 /* Total size of the paged pool minus the allocated size, is free */
61 FreePoolInPages = MmSizeOfPagedPoolInPages - MmPagedPoolInfo.AllocatedPagedPool;
62
63 /* Check the initial state high state */
64 if (FreePoolInPages >= MiHighPagedPoolThreshold)
65 {
66 /* We have plenty of pool */
67 KeSetEvent(MiHighPagedPoolEvent, 0, FALSE);
68 }
69 else
70 {
71 /* We don't */
72 KeClearEvent(MiHighPagedPoolEvent);
73 }
74
75 /* Check the initial low state */
76 if (FreePoolInPages <= MiLowPagedPoolThreshold)
77 {
78 /* We're very low in free pool memory */
79 KeSetEvent(MiLowPagedPoolEvent, 0, FALSE);
80 }
81 else
82 {
83 /* We're not */
84 KeClearEvent(MiLowPagedPoolEvent);
85 }
86
87 /* Release the paged pool lock */
88 KeReleaseGuardedMutex(&MmPagedPoolMutex);
89
90 /* Now it's time for the nonpaged pool lock */
91 OldIrql = KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock);
92
93 /* Free pages are the maximum minus what's been allocated */
94 FreePoolInPages = MmMaximumNonPagedPoolInPages - MmAllocatedNonPagedPool;
95
96 /* Check if we have plenty */
97 if (FreePoolInPages >= MiHighNonPagedPoolThreshold)
98 {
99 /* We do, set the event */
100 KeSetEvent(MiHighNonPagedPoolEvent, 0, FALSE);
101 }
102 else
103 {
104 /* We don't, clear the event */
105 KeClearEvent(MiHighNonPagedPoolEvent);
106 }
107
108 /* Check if we have very little */
109 if (FreePoolInPages <= MiLowNonPagedPoolThreshold)
110 {
111 /* We do, set the event */
112 KeSetEvent(MiLowNonPagedPoolEvent, 0, FALSE);
113 }
114 else
115 {
116 /* We don't, clear it */
117 KeClearEvent(MiLowNonPagedPoolEvent);
118 }
119
120 /* We're done, release the nonpaged pool lock */
121 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
122 }
123
124 VOID
125 NTAPI
126 MiInitializeNonPagedPool(VOID)
127 {
128 ULONG i;
129 PFN_NUMBER PoolPages;
130 PMMFREE_POOL_ENTRY FreeEntry, FirstEntry;
131 PMMPTE PointerPte;
132 PAGED_CODE();
133
134 //
135 // We keep 4 lists of free pages (4 lists help avoid contention)
136 //
137 for (i = 0; i < MI_MAX_FREE_PAGE_LISTS; i++)
138 {
139 //
140 // Initialize each of them
141 //
142 InitializeListHead(&MmNonPagedPoolFreeListHead[i]);
143 }
144
145 //
146 // Calculate how many pages the initial nonpaged pool has
147 //
148 PoolPages = BYTES_TO_PAGES(MmSizeOfNonPagedPoolInBytes);
149 MmNumberOfFreeNonPagedPool = PoolPages;
150
151 //
152 // Initialize the first free entry
153 //
154 FreeEntry = MmNonPagedPoolStart;
155 FirstEntry = FreeEntry;
156 FreeEntry->Size = PoolPages;
157 FreeEntry->Owner = FirstEntry;
158
159 //
160 // Insert it into the last list
161 //
162 InsertHeadList(&MmNonPagedPoolFreeListHead[MI_MAX_FREE_PAGE_LISTS - 1],
163 &FreeEntry->List);
164
165 //
166 // Now create free entries for every single other page
167 //
168 while (PoolPages-- > 1)
169 {
170 //
171 // Link them all back to the original entry
172 //
173 FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)FreeEntry + PAGE_SIZE);
174 FreeEntry->Owner = FirstEntry;
175 }
176
177 //
178 // Validate and remember first allocated pool page
179 //
180 PointerPte = MiAddressToPte(MmNonPagedPoolStart);
181 ASSERT(PointerPte->u.Hard.Valid == 1);
182 MiStartOfInitialPoolFrame = PFN_FROM_PTE(PointerPte);
183
184 //
185 // Keep track of where initial nonpaged pool ends
186 //
187 MmNonPagedPoolEnd0 = (PVOID)((ULONG_PTR)MmNonPagedPoolStart +
188 MmSizeOfNonPagedPoolInBytes);
189
190 //
191 // Validate and remember last allocated pool page
192 //
193 PointerPte = MiAddressToPte((PVOID)((ULONG_PTR)MmNonPagedPoolEnd0 - 1));
194 ASSERT(PointerPte->u.Hard.Valid == 1);
195 MiEndOfInitialPoolFrame = PFN_FROM_PTE(PointerPte);
196
197 //
198 // Validate the first nonpaged pool expansion page (which is a guard page)
199 //
200 PointerPte = MiAddressToPte(MmNonPagedPoolExpansionStart);
201 ASSERT(PointerPte->u.Hard.Valid == 0);
202
203 //
204 // Calculate the size of the expansion region alone
205 //
206 MiExpansionPoolPagesInitialCharge =
207 BYTES_TO_PAGES(MmMaximumNonPagedPoolInBytes - MmSizeOfNonPagedPoolInBytes);
208
209 //
210 // Remove 2 pages, since there's a guard page on top and on the bottom
211 //
212 MiExpansionPoolPagesInitialCharge -= 2;
213
214 //
215 // Now initialize the nonpaged pool expansion PTE space. Remember there's a
216 // guard page on top so make sure to skip it. The bottom guard page will be
217 // guaranteed by the fact our size is off by one.
218 //
219 MiInitializeSystemPtes(PointerPte + 1,
220 MiExpansionPoolPagesInitialCharge,
221 NonPagedPoolExpansion);
222 }
223
224 PVOID
225 NTAPI
226 MiAllocatePoolPages(IN POOL_TYPE PoolType,
227 IN SIZE_T SizeInBytes)
228 {
229 PFN_NUMBER SizeInPages, PageFrameNumber;
230 ULONG i;
231 KIRQL OldIrql;
232 PLIST_ENTRY NextEntry, NextHead, LastHead;
233 PMMPTE PointerPte, StartPte;
234 MMPTE TempPte;
235 PMMPFN Pfn1;
236 PVOID BaseVa, BaseVaStart;
237 PMMFREE_POOL_ENTRY FreeEntry;
238 PKSPIN_LOCK_QUEUE LockQueue;
239
240 //
241 // Figure out how big the allocation is in pages
242 //
243 SizeInPages = BYTES_TO_PAGES(SizeInBytes);
244
245 //
246 // Handle paged pool
247 //
248 if (PoolType == PagedPool)
249 {
250 //
251 // Lock the paged pool mutex
252 //
253 KeAcquireGuardedMutex(&MmPagedPoolMutex);
254
255 //
256 // Find some empty allocation space
257 //
258 i = RtlFindClearBitsAndSet(MmPagedPoolInfo.PagedPoolAllocationMap,
259 SizeInPages,
260 MmPagedPoolInfo.PagedPoolHint);
261 if (i == 0xFFFFFFFF)
262 {
263 //
264 // Get the page bit count
265 //
266 i = ((SizeInPages - 1) / 1024) + 1;
267 DPRINT1("Paged pool expansion: %d %x\n", i, SizeInPages);
268
269 //
270 // Check if there is enougn paged pool expansion space left
271 //
272 if (MmPagedPoolInfo.NextPdeForPagedPoolExpansion >
273 MiAddressToPte(MmPagedPoolInfo.LastPteForPagedPool))
274 {
275 //
276 // Out of memory!
277 //
278 DPRINT1("OUT OF PAGED POOL!!!\n");
279 KeReleaseGuardedMutex(&MmPagedPoolMutex);
280 return NULL;
281 }
282
283 //
284 // Check if we'll have to expand past the last PTE we have available
285 //
286 if (((i - 1) + MmPagedPoolInfo.NextPdeForPagedPoolExpansion) >
287 MiAddressToPte(MmPagedPoolInfo.LastPteForPagedPool))
288 {
289 //
290 // We can only support this much then
291 //
292 SizeInPages = MiAddressToPte(MmPagedPoolInfo.LastPteForPagedPool) -
293 MmPagedPoolInfo.NextPdeForPagedPoolExpansion +
294 1;
295 ASSERT(SizeInPages < i);
296 i = SizeInPages;
297 }
298 else
299 {
300 //
301 // Otherwise, there is plenty of space left for this expansion
302 //
303 SizeInPages = i;
304 }
305
306 //
307 // Get the template PTE we'll use to expand
308 //
309 TempPte = ValidKernelPte;
310
311 //
312 // Get the first PTE in expansion space
313 //
314 PointerPte = MmPagedPoolInfo.NextPdeForPagedPoolExpansion;
315 BaseVa = MiPteToAddress(PointerPte);
316 BaseVaStart = BaseVa;
317
318 //
319 // Lock the PFN database and loop pages
320 //
321 OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
322 do
323 {
324 //
325 // It should not already be valid
326 //
327 ASSERT(PointerPte->u.Hard.Valid == 0);
328
329 /* Request a page */
330 PageFrameNumber = MiRemoveAnyPage(0);
331 TempPte.u.Hard.PageFrameNumber = PageFrameNumber;
332
333 //
334 // Save it into our double-buffered system page directory
335 //
336 /* This seems to be making the assumption that one PDE is one page long */
337 C_ASSERT(PAGE_SIZE == (PD_COUNT * (sizeof(MMPTE) * PDE_COUNT)));
338 MmSystemPagePtes[(ULONG_PTR)PointerPte & (PAGE_SIZE - 1) /
339 sizeof(MMPTE)] = TempPte;
340
341 /* Initialize the PFN */
342 MiInitializePfnForOtherProcess(PageFrameNumber,
343 PointerPte,
344 MmSystemPageDirectory[(PointerPte - (PMMPTE)PDE_BASE) / PDE_COUNT]);
345
346 /* Write the actual PTE now */
347 ASSERT(TempPte.u.Hard.Valid == 1);
348 *PointerPte++ = TempPte;
349
350 //
351 // Move on to the next expansion address
352 //
353 BaseVa = (PVOID)((ULONG_PTR)BaseVa + PAGE_SIZE);
354 } while (--i > 0);
355
356 //
357 // Release the PFN database lock
358 //
359 KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
360
361 //
362 // These pages are now available, clear their availablity bits
363 //
364 RtlClearBits(MmPagedPoolInfo.PagedPoolAllocationMap,
365 (MmPagedPoolInfo.NextPdeForPagedPoolExpansion -
366 MiAddressToPte(MmPagedPoolInfo.FirstPteForPagedPool)) *
367 1024,
368 SizeInPages * 1024);
369
370 //
371 // Update the next expansion location
372 //
373 MmPagedPoolInfo.NextPdeForPagedPoolExpansion += SizeInPages;
374
375 //
376 // Zero out the newly available memory
377 //
378 RtlZeroMemory(BaseVaStart, SizeInPages * PAGE_SIZE);
379
380 //
381 // Now try consuming the pages again
382 //
383 SizeInPages = BYTES_TO_PAGES(SizeInBytes);
384 i = RtlFindClearBitsAndSet(MmPagedPoolInfo.PagedPoolAllocationMap,
385 SizeInPages,
386 0);
387 if (i == 0xFFFFFFFF)
388 {
389 //
390 // Out of memory!
391 //
392 DPRINT1("OUT OF PAGED POOL!!!\n");
393 KeReleaseGuardedMutex(&MmPagedPoolMutex);
394 return NULL;
395 }
396 }
397
398 //
399 // Update the pool hint if the request was just one page
400 //
401 if (SizeInPages == 1) MmPagedPoolInfo.PagedPoolHint = i + 1;
402
403 //
404 // Update the end bitmap so we know the bounds of this allocation when
405 // the time comes to free it
406 //
407 RtlSetBit(MmPagedPoolInfo.EndOfPagedPoolBitmap, i + SizeInPages - 1);
408
409 //
410 // Now we can release the lock (it mainly protects the bitmap)
411 //
412 KeReleaseGuardedMutex(&MmPagedPoolMutex);
413
414 //
415 // Now figure out where this allocation starts
416 //
417 BaseVa = (PVOID)((ULONG_PTR)MmPagedPoolStart + (i << PAGE_SHIFT));
418
419 //
420 // Flush the TLB
421 //
422 KeFlushEntireTb(TRUE, TRUE);
423
424 /* Setup a demand-zero writable PTE */
425 MI_MAKE_SOFTWARE_PTE(&TempPte, MM_READWRITE);
426
427 //
428 // Find the first and last PTE, then loop them all
429 //
430 PointerPte = MiAddressToPte(BaseVa);
431 StartPte = PointerPte + SizeInPages;
432 do
433 {
434 //
435 // Write the demand zero PTE and keep going
436 //
437 ASSERT(PointerPte->u.Hard.Valid == 0);
438 *PointerPte++ = TempPte;
439 } while (PointerPte < StartPte);
440
441 //
442 // Return the allocation address to the caller
443 //
444 return BaseVa;
445 }
446
447 //
448 // Allocations of less than 4 pages go into their individual buckets
449 //
450 i = SizeInPages - 1;
451 if (i >= MI_MAX_FREE_PAGE_LISTS) i = MI_MAX_FREE_PAGE_LISTS - 1;
452
453 //
454 // Loop through all the free page lists based on the page index
455 //
456 NextHead = &MmNonPagedPoolFreeListHead[i];
457 LastHead = &MmNonPagedPoolFreeListHead[MI_MAX_FREE_PAGE_LISTS];
458
459 //
460 // Acquire the nonpaged pool lock
461 //
462 OldIrql = KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock);
463 do
464 {
465 //
466 // Now loop through all the free page entries in this given list
467 //
468 NextEntry = NextHead->Flink;
469 while (NextEntry != NextHead)
470 {
471 //
472 // Grab the entry and see if it can handle our allocation
473 //
474 FreeEntry = CONTAINING_RECORD(NextEntry, MMFREE_POOL_ENTRY, List);
475 if (FreeEntry->Size >= SizeInPages)
476 {
477 //
478 // It does, so consume the pages from here
479 //
480 FreeEntry->Size -= SizeInPages;
481
482 //
483 // The allocation will begin in this free page area
484 //
485 BaseVa = (PVOID)((ULONG_PTR)FreeEntry +
486 (FreeEntry->Size << PAGE_SHIFT));
487
488 //
489 // This is not a free page segment anymore
490 //
491 RemoveEntryList(&FreeEntry->List);
492
493 //
494 // However, check if its' still got space left
495 //
496 if (FreeEntry->Size != 0)
497 {
498 //
499 // Insert it back into a different list, based on its pages
500 //
501 i = FreeEntry->Size - 1;
502 if (i >= MI_MAX_FREE_PAGE_LISTS) i = MI_MAX_FREE_PAGE_LISTS - 1;
503 InsertTailList (&MmNonPagedPoolFreeListHead[i],
504 &FreeEntry->List);
505 }
506
507 //
508 // Grab the PTE for this allocation
509 //
510 PointerPte = MiAddressToPte(BaseVa);
511 ASSERT(PointerPte->u.Hard.Valid == 1);
512
513 //
514 // Grab the PFN NextEntry and index
515 //
516 Pfn1 = MiGetPfnEntry(PFN_FROM_PTE(PointerPte));
517
518 //
519 // Now mark it as the beginning of an allocation
520 //
521 ASSERT(Pfn1->u3.e1.StartOfAllocation == 0);
522 Pfn1->u3.e1.StartOfAllocation = 1;
523
524 //
525 // Check if the allocation is larger than one page
526 //
527 if (SizeInPages != 1)
528 {
529 //
530 // Navigate to the last PFN entry and PTE
531 //
532 PointerPte += SizeInPages - 1;
533 ASSERT(PointerPte->u.Hard.Valid == 1);
534 Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
535 }
536
537 //
538 // Mark this PFN as the last (might be the same as the first)
539 //
540 ASSERT(Pfn1->u3.e1.EndOfAllocation == 0);
541 Pfn1->u3.e1.EndOfAllocation = 1;
542
543 //
544 // Release the nonpaged pool lock, and return the allocation
545 //
546 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
547 return BaseVa;
548 }
549
550 //
551 // Try the next free page entry
552 //
553 NextEntry = FreeEntry->List.Flink;
554 }
555 } while (++NextHead < LastHead);
556
557 //
558 // If we got here, we're out of space.
559 // Start by releasing the lock
560 //
561 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
562
563 //
564 // Allocate some system PTEs
565 //
566 StartPte = MiReserveSystemPtes(SizeInPages, NonPagedPoolExpansion);
567 PointerPte = StartPte;
568 if (StartPte == NULL)
569 {
570 //
571 // Ran out of memory
572 //
573 DPRINT1("Out of NP Expansion Pool\n");
574 return NULL;
575 }
576
577 //
578 // Acquire the pool lock now
579 //
580 OldIrql = KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock);
581
582 //
583 // Lock the PFN database too
584 //
585 LockQueue = &KeGetCurrentPrcb()->LockQueue[LockQueuePfnLock];
586 KeAcquireQueuedSpinLockAtDpcLevel(LockQueue);
587
588 //
589 // Loop the pages
590 //
591 TempPte = ValidKernelPte;
592 do
593 {
594 /* Allocate a page */
595 PageFrameNumber = MiRemoveAnyPage(0);
596
597 /* Get the PFN entry for it and fill it out */
598 Pfn1 = MiGetPfnEntry(PageFrameNumber);
599 Pfn1->u3.e2.ReferenceCount = 1;
600 Pfn1->u2.ShareCount = 1;
601 Pfn1->PteAddress = PointerPte;
602 Pfn1->u3.e1.PageLocation = ActiveAndValid;
603 Pfn1->u4.VerifierAllocation = 0;
604
605 /* Write the PTE for it */
606 TempPte.u.Hard.PageFrameNumber = PageFrameNumber;
607 ASSERT(PointerPte->u.Hard.Valid == 0);
608 ASSERT(TempPte.u.Hard.Valid == 1);
609 *PointerPte++ = TempPte;
610 } while (--SizeInPages > 0);
611
612 //
613 // This is the last page
614 //
615 Pfn1->u3.e1.EndOfAllocation = 1;
616
617 //
618 // Get the first page and mark it as such
619 //
620 Pfn1 = MiGetPfnEntry(StartPte->u.Hard.PageFrameNumber);
621 Pfn1->u3.e1.StartOfAllocation = 1;
622
623 //
624 // Release the PFN and nonpaged pool lock
625 //
626 KeReleaseQueuedSpinLockFromDpcLevel(LockQueue);
627 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
628
629 //
630 // Return the address
631 //
632 return MiPteToAddress(StartPte);
633 }
634
635 ULONG
636 NTAPI
637 MiFreePoolPages(IN PVOID StartingVa)
638 {
639 PMMPTE PointerPte, StartPte;
640 PMMPFN Pfn1, StartPfn;
641 PFN_NUMBER FreePages, NumberOfPages;
642 KIRQL OldIrql;
643 PMMFREE_POOL_ENTRY FreeEntry, NextEntry, LastEntry;
644 ULONG i, End;
645
646 //
647 // Handle paged pool
648 //
649 if ((StartingVa >= MmPagedPoolStart) && (StartingVa <= MmPagedPoolEnd))
650 {
651 //
652 // Calculate the offset from the beginning of paged pool, and convert it
653 // into pages
654 //
655 i = ((ULONG_PTR)StartingVa - (ULONG_PTR)MmPagedPoolStart) >> PAGE_SHIFT;
656 End = i;
657
658 //
659 // Now use the end bitmap to scan until we find a set bit, meaning that
660 // this allocation finishes here
661 //
662 while (!RtlTestBit(MmPagedPoolInfo.EndOfPagedPoolBitmap, End)) End++;
663
664 //
665 // Now calculate the total number of pages this allocation spans
666 //
667 NumberOfPages = End - i + 1;
668
669 /* Delete the actual pages */
670 PointerPte = MmPagedPoolInfo.FirstPteForPagedPool + i;
671 FreePages = MiDeleteSystemPageableVm(PointerPte, NumberOfPages, 0, NULL);
672 ASSERT(FreePages == NumberOfPages);
673
674 //
675 // Acquire the paged pool lock
676 //
677 KeAcquireGuardedMutex(&MmPagedPoolMutex);
678
679 //
680 // Clear the allocation and free bits
681 //
682 RtlClearBit(MmPagedPoolInfo.EndOfPagedPoolBitmap, i);
683 RtlClearBits(MmPagedPoolInfo.PagedPoolAllocationMap, i, NumberOfPages);
684
685 //
686 // Update the hint if we need to
687 //
688 if (i < MmPagedPoolInfo.PagedPoolHint) MmPagedPoolInfo.PagedPoolHint = i;
689
690 //
691 // Release the lock protecting the bitmaps
692 //
693 KeReleaseGuardedMutex(&MmPagedPoolMutex);
694
695 //
696 // And finally return the number of pages freed
697 //
698 return NumberOfPages;
699 }
700
701 //
702 // Get the first PTE and its corresponding PFN entry
703 //
704 StartPte = PointerPte = MiAddressToPte(StartingVa);
705 StartPfn = Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
706
707 //
708 // Loop until we find the last PTE
709 //
710 while (Pfn1->u3.e1.EndOfAllocation == 0)
711 {
712 //
713 // Keep going
714 //
715 PointerPte++;
716 Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
717 }
718
719 //
720 // Now we know how many pages we have
721 //
722 NumberOfPages = PointerPte - StartPte + 1;
723
724 //
725 // Acquire the nonpaged pool lock
726 //
727 OldIrql = KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock);
728
729 //
730 // Mark the first and last PTEs as not part of an allocation anymore
731 //
732 StartPfn->u3.e1.StartOfAllocation = 0;
733 Pfn1->u3.e1.EndOfAllocation = 0;
734
735 //
736 // Assume we will free as many pages as the allocation was
737 //
738 FreePages = NumberOfPages;
739
740 //
741 // Peek one page past the end of the allocation
742 //
743 PointerPte++;
744
745 //
746 // Guard against going past initial nonpaged pool
747 //
748 if (MiGetPfnEntryIndex(Pfn1) == MiEndOfInitialPoolFrame)
749 {
750 //
751 // This page is on the outskirts of initial nonpaged pool, so ignore it
752 //
753 Pfn1 = NULL;
754 }
755 else
756 {
757 //
758 // Otherwise, our entire allocation must've fit within the initial non
759 // paged pool, or the expansion nonpaged pool, so get the PFN entry of
760 // the next allocation
761 //
762 ASSERT((ULONG_PTR)StartingVa + NumberOfPages <= (ULONG_PTR)MmNonPagedPoolEnd);
763 if (PointerPte->u.Hard.Valid == 1)
764 {
765 //
766 // It's either expansion or initial: get the PFN entry
767 //
768 Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
769 }
770 else
771 {
772 //
773 // This means we've reached the guard page that protects the end of
774 // the expansion nonpaged pool
775 //
776 Pfn1 = NULL;
777 }
778
779 }
780
781 //
782 // Check if this allocation actually exists
783 //
784 if ((Pfn1) && (Pfn1->u3.e1.StartOfAllocation == 0))
785 {
786 //
787 // It doesn't, so we should actually locate a free entry descriptor
788 //
789 FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)StartingVa +
790 (NumberOfPages << PAGE_SHIFT));
791 ASSERT(FreeEntry->Owner == FreeEntry);
792
793 //
794 // Consume this entry's pages, and remove it from its free list
795 //
796 FreePages += FreeEntry->Size;
797 RemoveEntryList (&FreeEntry->List);
798 }
799
800 //
801 // Now get the official free entry we'll create for the caller's allocation
802 //
803 FreeEntry = StartingVa;
804
805 //
806 // Check if the our allocation is the very first page
807 //
808 if (MiGetPfnEntryIndex(StartPfn) == MiStartOfInitialPoolFrame)
809 {
810 //
811 // Then we can't do anything or we'll risk underflowing
812 //
813 Pfn1 = NULL;
814 }
815 else
816 {
817 //
818 // Otherwise, get the PTE for the page right before our allocation
819 //
820 PointerPte -= NumberOfPages + 1;
821 if (PointerPte->u.Hard.Valid == 1)
822 {
823 //
824 // It's either expansion or initial nonpaged pool, get the PFN entry
825 //
826 Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
827 }
828 else
829 {
830 //
831 // We must've reached the guard page, so don't risk touching it
832 //
833 Pfn1 = NULL;
834 }
835 }
836
837 //
838 // Check if there is a valid PFN entry for the page before the allocation
839 // and then check if this page was actually the end of an allocation.
840 // If it wasn't, then we know for sure it's a free page
841 //
842 if ((Pfn1) && (Pfn1->u3.e1.EndOfAllocation == 0))
843 {
844 //
845 // Get the free entry descriptor for that given page range
846 //
847 FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)StartingVa - PAGE_SIZE);
848 FreeEntry = FreeEntry->Owner;
849
850 //
851 // Check if the entry is small enough to be indexed on a free list
852 // If it is, we'll want to re-insert it, since we're about to
853 // collapse our pages on top of it, which will change its count
854 //
855 if (FreeEntry->Size < (MI_MAX_FREE_PAGE_LISTS - 1))
856 {
857 //
858 // Remove the list from where it is now
859 //
860 RemoveEntryList(&FreeEntry->List);
861
862 //
863 // Update its size
864 //
865 FreeEntry->Size += FreePages;
866
867 //
868 // And now find the new appropriate list to place it in
869 //
870 i = (ULONG)(FreeEntry->Size - 1);
871 if (i >= MI_MAX_FREE_PAGE_LISTS) i = MI_MAX_FREE_PAGE_LISTS - 1;
872
873 //
874 // Do it
875 //
876 InsertTailList(&MmNonPagedPoolFreeListHead[i], &FreeEntry->List);
877 }
878 else
879 {
880 //
881 // Otherwise, just combine our free pages into this entry
882 //
883 FreeEntry->Size += FreePages;
884 }
885 }
886
887 //
888 // Check if we were unable to do any compaction, and we'll stick with this
889 //
890 if (FreeEntry == StartingVa)
891 {
892 //
893 // Well, now we are a free entry. At worse we just have our newly freed
894 // pages, at best we have our pages plus whatever entry came after us
895 //
896 FreeEntry->Size = FreePages;
897
898 //
899 // Find the appropriate list we should be on
900 //
901 i = FreeEntry->Size - 1;
902 if (i >= MI_MAX_FREE_PAGE_LISTS) i = MI_MAX_FREE_PAGE_LISTS - 1;
903
904 //
905 // And insert us
906 //
907 InsertTailList (&MmNonPagedPoolFreeListHead[i], &FreeEntry->List);
908 }
909
910 //
911 // Just a sanity check
912 //
913 ASSERT(FreePages != 0);
914
915 //
916 // Get all the pages between our allocation and its end. These will all now
917 // become free page chunks.
918 //
919 NextEntry = StartingVa;
920 LastEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)NextEntry + (FreePages << PAGE_SHIFT));
921 do
922 {
923 //
924 // Link back to the parent free entry, and keep going
925 //
926 NextEntry->Owner = FreeEntry;
927 NextEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)NextEntry + PAGE_SIZE);
928 } while (NextEntry != LastEntry);
929
930 //
931 // We're done, release the lock and let the caller know how much we freed
932 //
933 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
934 return NumberOfPages;
935 }
936
937
938 BOOLEAN
939 NTAPI
940 MiRaisePoolQuota(IN POOL_TYPE PoolType,
941 IN ULONG CurrentMaxQuota,
942 OUT PULONG NewMaxQuota)
943 {
944 //
945 // Not implemented
946 //
947 UNIMPLEMENTED;
948 *NewMaxQuota = CurrentMaxQuota + 65536;
949 return TRUE;
950 }
951
952 /* PUBLIC FUNCTIONS ***********************************************************/
953
954 /*
955 * @unimplemented
956 */
957 PVOID
958 NTAPI
959 MmAllocateMappingAddress(IN SIZE_T NumberOfBytes,
960 IN ULONG PoolTag)
961 {
962 UNIMPLEMENTED;
963 return NULL;
964 }
965
966 /*
967 * @unimplemented
968 */
969 VOID
970 NTAPI
971 MmFreeMappingAddress(IN PVOID BaseAddress,
972 IN ULONG PoolTag)
973 {
974 UNIMPLEMENTED;
975 }
976
977 /* EOF */