2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/mm/freelist.c
5 * PURPOSE: Handle the list of free physical pages
7 * PROGRAMMERS: David Welch (welch@cwcom.net)
11 /* INCLUDES ****************************************************************/
17 #if defined (ALLOC_PRAGMA)
18 #pragma alloc_text(INIT, MmInitializePageList)
21 #define MODULE_INVOLVED_IN_ARM3
22 #include "ARM3/miarm.h"
24 /* GLOBALS ****************************************************************/
28 // ReactOS to NT Physical Page Descriptor Entry Legacy Mapping Definitions
32 #define RmapListHead AweReferenceCount
33 #define SavedSwapEntry u4.EntireFrame
34 #define RemoveEntryList(x) RemoveEntryList((PLIST_ENTRY)x)
35 #define InsertTailList(x, y) InsertTailList(x, (PLIST_ENTRY)y)
37 #define PHYSICAL_PAGE MMPFN
38 #define PPHYSICAL_PAGE PMMPFN
40 /* The first array contains ReactOS PFNs, the second contains ARM3 PFNs */
41 PPHYSICAL_PAGE MmPfnDatabase
[2];
43 ULONG MmAvailablePages
;
44 ULONG MmResidentAvailablePages
;
46 SIZE_T MmTotalCommitLimit
;
47 SIZE_T MmTotalCommittedPages
;
48 SIZE_T MmSharedCommit
;
49 SIZE_T MmDriverCommit
;
50 SIZE_T MmProcessCommit
;
51 SIZE_T MmPagedPoolCommit
;
52 SIZE_T MmPeakCommitment
;
53 SIZE_T MmtotalCommitLimitMaximum
;
55 MMPFNLIST MmZeroedPageListHead
;
56 MMPFNLIST MmFreePageListHead
;
57 MMPFNLIST MmStandbyPageListHead
;
58 MMPFNLIST MmModifiedPageListHead
;
59 MMPFNLIST MmModifiedNoWritePageListHead
;
61 /* List of pages zeroed by the ZPW (MmZeroPageThreadMain) */
62 static LIST_ENTRY FreeZeroedPageListHead
;
63 /* List of free pages, filled by MmGetReferenceCountPage and
64 * and MmInitializePageList */
65 static LIST_ENTRY FreeUnzeroedPageListHead
;
67 static KEVENT ZeroPageThreadEvent
;
68 static BOOLEAN ZeroPageThreadShouldTerminate
= FALSE
;
70 static ULONG UnzeroedPageCount
= 0;
72 /* FUNCTIONS *************************************************************/
74 static RTL_BITMAP MiUserPfnBitMap
;
76 /* FUNCTIONS *************************************************************/
80 MiInitializeUserPfnBitmap(VOID
)
84 /* Allocate enough buffer for the PFN bitmap and align it on 32-bits */
85 Bitmap
= ExAllocatePoolWithTag(NonPagedPool
,
86 (((MmHighestPhysicalPage
+ 1) + 31) / 32) * 4,
90 /* Initialize it and clear all the bits to begin with */
91 RtlInitializeBitMap(&MiUserPfnBitMap
,
93 MmHighestPhysicalPage
+ 1);
94 RtlClearAllBits(&MiUserPfnBitMap
);
99 MmGetLRUFirstUserPage(VOID
)
104 /* Find the first user page */
105 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
106 Position
= RtlFindSetBits(&MiUserPfnBitMap
, 1, 0);
107 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
108 if (Position
== 0xFFFFFFFF) return 0;
116 MmInsertLRULastUserPage(PFN_TYPE Pfn
)
120 /* Set the page as a user page */
121 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
122 RtlSetBit(&MiUserPfnBitMap
, Pfn
);
123 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
128 MmGetLRUNextUserPage(PFN_TYPE PreviousPfn
)
133 /* Find the next user page */
134 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
135 Position
= RtlFindSetBits(&MiUserPfnBitMap
, 1, PreviousPfn
+ 1);
136 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
137 if (Position
== 0xFFFFFFFF) return 0;
145 MmRemoveLRUUserPage(PFN_TYPE Page
)
147 /* Unset the page as a user page */
148 RtlClearBit(&MiUserPfnBitMap
, Page
);
153 MiIsPfnInUse(IN PMMPFN Pfn1
)
155 return ((Pfn1
->u3
.e1
.PageLocation
!= FreePageList
) &&
156 (Pfn1
->u3
.e1
.PageLocation
!= ZeroedPageList
));
161 MiFindContiguousPages(IN PFN_NUMBER LowestPfn
,
162 IN PFN_NUMBER HighestPfn
,
163 IN PFN_NUMBER BoundaryPfn
,
164 IN PFN_NUMBER SizeInPages
,
165 IN MEMORY_CACHING_TYPE CacheType
)
167 PFN_NUMBER Page
, PageCount
, LastPage
, Length
, BoundaryMask
;
172 ASSERT(SizeInPages
!= 0);
175 // Convert the boundary PFN into an alignment mask
177 BoundaryMask
= ~(BoundaryPfn
- 1);
180 // Loop all the physical memory blocks
185 // Capture the base page and length of this memory block
187 Page
= MmPhysicalMemoryBlock
->Run
[i
].BasePage
;
188 PageCount
= MmPhysicalMemoryBlock
->Run
[i
].PageCount
;
191 // Check how far this memory block will go
193 LastPage
= Page
+ PageCount
;
196 // Trim it down to only the PFNs we're actually interested in
198 if ((LastPage
- 1) > HighestPfn
) LastPage
= HighestPfn
+ 1;
199 if (Page
< LowestPfn
) Page
= LowestPfn
;
202 // Skip this run if it's empty or fails to contain all the pages we need
204 if (!(PageCount
) || ((Page
+ SizeInPages
) > LastPage
)) continue;
207 // Now scan all the relevant PFNs in this run
210 for (Pfn1
= MiGetPfnEntry(Page
); Page
< LastPage
; Page
++, Pfn1
++)
213 // If this PFN is in use, ignore it
215 if (MiIsPfnInUse(Pfn1
)) continue;
218 // If we haven't chosen a start PFN yet and the caller specified an
219 // alignment, make sure the page matches the alignment restriction
221 if ((!(Length
) && (BoundaryPfn
)) &&
222 (((Page
^ (Page
+ SizeInPages
- 1)) & BoundaryMask
)))
225 // It does not, so bail out
231 // Increase the number of valid pages, and check if we have enough
233 if (++Length
== SizeInPages
)
236 // It appears we've amassed enough legitimate pages, rollback
238 Pfn1
-= (Length
- 1);
239 Page
-= (Length
- 1);
242 // Acquire the PFN lock
244 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
248 // Things might've changed for us. Is the page still free?
250 if (MiIsPfnInUse(Pfn1
)) break;
253 // So far so good. Is this the last confirmed valid page?
258 // Sanity check that we didn't go out of bounds
260 ASSERT(i
!= MmPhysicalMemoryBlock
->NumberOfRuns
);
263 // Loop until all PFN entries have been processed
265 EndPfn
= Pfn1
- SizeInPages
+ 1;
269 // If this was an unzeroed page, there are now less
271 if (Pfn1
->u3
.e1
.PageLocation
== ZeroedPageList
) UnzeroedPageCount
--;
274 // One less free page
279 // This PFN is now a used page, set it up
281 RemoveEntryList(&Pfn1
->ListEntry
);
282 Pfn1
->u3
.e2
.ReferenceCount
= 1;
283 Pfn1
->SavedSwapEntry
= 0;
286 // Check if it was already zeroed
288 if (Pfn1
->u3
.e1
.PageLocation
!= ZeroedPageList
)
291 // It wasn't, so zero it
293 MiZeroPage(MiGetPfnEntryIndex(Pfn1
));
299 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
302 // Check if this is the last PFN, otherwise go on
304 if (Pfn1
== EndPfn
) break;
309 // Mark the first and last PFN so we can find them later
311 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
312 (Pfn1
+ SizeInPages
- 1)->u3
.e1
.EndOfAllocation
= 1;
315 // Now it's safe to let go of the PFN lock
317 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
320 // Quick sanity check that the last PFN is consistent
322 EndPfn
= Pfn1
+ SizeInPages
;
323 ASSERT(EndPfn
== MiGetPfnEntry(Page
+ 1));
326 // Compute the first page, and make sure it's consistent
328 Page
-= SizeInPages
- 1;
329 ASSERT(Pfn1
== MiGetPfnEntry(Page
));
335 // Keep going. The purpose of this loop is to reconfirm that
336 // after acquiring the PFN lock these pages are still usable
343 // If we got here, something changed while we hadn't acquired
344 // the PFN lock yet, so we'll have to restart
346 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
350 } while (++i
!= MmPhysicalMemoryBlock
->NumberOfRuns
);
353 // And if we get here, it means no suitable physical memory runs were found
360 MiAllocatePagesForMdl(IN PHYSICAL_ADDRESS LowAddress
,
361 IN PHYSICAL_ADDRESS HighAddress
,
362 IN PHYSICAL_ADDRESS SkipBytes
,
363 IN SIZE_T TotalBytes
,
364 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
,
368 PFN_NUMBER PageCount
, LowPage
, HighPage
, SkipPages
, PagesFound
= 0, Page
;
369 PPFN_NUMBER MdlPage
, LastMdlPage
;
371 PLIST_ENTRY ListEntry
;
373 INT LookForZeroedPages
;
374 ASSERT (KeGetCurrentIrql() <= APC_LEVEL
);
377 // Convert the low address into a PFN
379 LowPage
= (PFN_NUMBER
)(LowAddress
.QuadPart
>> PAGE_SHIFT
);
382 // Convert, and normalize, the high address into a PFN
384 HighPage
= (PFN_NUMBER
)(HighAddress
.QuadPart
>> PAGE_SHIFT
);
385 if (HighPage
> MmHighestPhysicalPage
) HighPage
= MmHighestPhysicalPage
;
388 // Validate skipbytes and convert them into pages
390 if (BYTE_OFFSET(SkipBytes
.LowPart
)) return NULL
;
391 SkipPages
= (PFN_NUMBER
)(SkipBytes
.QuadPart
>> PAGE_SHIFT
);
394 // Now compute the number of pages the MDL will cover
396 PageCount
= (PFN_NUMBER
)ADDRESS_AND_SIZE_TO_SPAN_PAGES(0, TotalBytes
);
400 // Try creating an MDL for these many pages
402 Mdl
= MmCreateMdl(NULL
, NULL
, PageCount
<< PAGE_SHIFT
);
406 // This function is not required to return the amount of pages requested
407 // In fact, it can return as little as 1 page, and callers are supposed
408 // to deal with this scenario. So re-attempt the allocation with less
409 // pages than before, and see if it worked this time.
411 PageCount
-= (PageCount
>> 4);
415 // Wow, not even a single page was around!
417 if (!Mdl
) return NULL
;
420 // This is where the page array starts....
422 MdlPage
= (PPFN_NUMBER
)(Mdl
+ 1);
425 // Lock the PFN database
427 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
430 // Are we looking for any pages, without discriminating?
432 if ((LowPage
== 0) && (HighPage
== MmHighestPhysicalPage
))
435 // Well then, let's go shopping
437 while (PagesFound
< PageCount
)
440 // Do we have zeroed pages?
442 if (!IsListEmpty(&FreeZeroedPageListHead
))
447 ListEntry
= RemoveTailList(&FreeZeroedPageListHead
);
449 else if (!IsListEmpty(&FreeUnzeroedPageListHead
))
452 // Nope, grab an unzeroed page
454 ListEntry
= RemoveTailList(&FreeUnzeroedPageListHead
);
460 // This is not good... hopefully we have at least SOME pages
467 // Get the PFN entry for this page
469 Pfn1
= CONTAINING_RECORD(ListEntry
, PHYSICAL_PAGE
, ListEntry
);
472 // Make sure it's really free
474 ASSERT(MiIsPfnInUse(Pfn1
) == FALSE
);
475 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
478 // Allocate it and mark it
480 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
481 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
482 Pfn1
->u3
.e2
.ReferenceCount
= 1;
483 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
484 Pfn1
->SavedSwapEntry
= 0;
487 // Decrease available pages
492 // Save it into the MDL
494 *MdlPage
++ = MiGetPfnEntryIndex(Pfn1
);
501 // You want specific range of pages. We'll do this in two runs
503 for (LookForZeroedPages
= 1; LookForZeroedPages
>= 0; LookForZeroedPages
--)
506 // Scan the range you specified
508 for (Page
= LowPage
; Page
< HighPage
; Page
++)
511 // Get the PFN entry for this page
513 Pfn1
= MiGetPfnEntry(Page
);
517 // Make sure it's free and if this is our first pass, zeroed
519 if (MiIsPfnInUse(Pfn1
)) continue;
520 if ((Pfn1
->u3
.e1
.PageLocation
== ZeroedPageList
) != LookForZeroedPages
) continue;
525 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
528 // Now setup the page and mark it
530 Pfn1
->u3
.e2
.ReferenceCount
= 1;
531 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
532 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
533 Pfn1
->SavedSwapEntry
= 0;
536 // If this page was unzeroed, we've consumed such a page
538 if (Pfn1
->u3
.e1
.PageLocation
!= ZeroedPageList
) UnzeroedPageCount
--;
541 // Decrease available pages
546 // Save this page into the MDL
549 if (++PagesFound
== PageCount
) break;
553 // If the first pass was enough, don't keep going, otherwise, go again
555 if (PagesFound
== PageCount
) break;
560 // Now release the PFN count
562 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
565 // We might've found less pages, but not more ;-)
567 if (PagesFound
!= PageCount
) ASSERT(PagesFound
< PageCount
);
571 // If we didn' tfind any pages at all, fail
573 DPRINT1("NO MDL PAGES!\n");
579 // Write out how many pages we found
581 Mdl
->ByteCount
= (ULONG
)(PagesFound
<< PAGE_SHIFT
);
584 // Terminate the MDL array if there's certain missing pages
586 if (PagesFound
!= PageCount
) *MdlPage
= -1;
589 // Now go back and loop over all the MDL pages
591 MdlPage
= (PPFN_NUMBER
)(Mdl
+ 1);
592 LastMdlPage
= MdlPage
+ PagesFound
;
593 while (MdlPage
< LastMdlPage
)
596 // Check if we've reached the end
599 if (Page
== (PFN_NUMBER
)-1) break;
602 // Get the PFN entry for the page and check if we should zero it out
604 Pfn1
= MiGetPfnEntry(Page
);
606 if (Pfn1
->u3
.e1
.PageLocation
!= ZeroedPageList
) MiZeroPage(Page
);
607 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
611 // We're done, mark the pages as locked (should we lock them, though???)
614 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
620 MmDumpPfnDatabase(VOID
)
624 PCHAR State
= "????", Type
= "Unknown";
626 ULONG Totals
[5] = {0}, FreePages
= 0;
628 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
631 // Loop the PFN database
633 for (i
= 0; i
<= MmHighestPhysicalPage
; i
++)
635 Pfn1
= MiGetPfnEntry(i
);
641 if (MiIsPfnInUse(Pfn1
))
654 // Pretty-print the page
656 DbgPrint("0x%08p:\t%04s\t%20s\t(%02d) [%08p])\n",
660 Pfn1
->u3
.e2
.ReferenceCount
,
664 DbgPrint("Nonpaged Pool: %d pages\t[%d KB]\n", Totals
[MC_NPPOOL
], (Totals
[MC_NPPOOL
] << PAGE_SHIFT
) / 1024);
665 DbgPrint("Paged Pool: %d pages\t[%d KB]\n", Totals
[MC_PPOOL
], (Totals
[MC_PPOOL
] << PAGE_SHIFT
) / 1024);
666 DbgPrint("File System Cache: %d pages\t[%d KB]\n", Totals
[MC_CACHE
], (Totals
[MC_CACHE
] << PAGE_SHIFT
) / 1024);
667 DbgPrint("Process Working Set: %d pages\t[%d KB]\n", Totals
[MC_USER
], (Totals
[MC_USER
] << PAGE_SHIFT
) / 1024);
668 DbgPrint("System: %d pages\t[%d KB]\n", Totals
[MC_SYSTEM
], (Totals
[MC_SYSTEM
] << PAGE_SHIFT
) / 1024);
669 DbgPrint("Free: %d pages\t[%d KB]\n", FreePages
, (FreePages
<< PAGE_SHIFT
) / 1024);
671 KeLowerIrql(OldIrql
);
676 MmInitializePageList(VOID
)
679 PHYSICAL_PAGE UsedPage
;
680 PMEMORY_ALLOCATION_DESCRIPTOR Md
;
681 PLIST_ENTRY NextEntry
;
682 ULONG NrSystemPages
= 0;
684 /* Initialize the page lists */
685 InitializeListHead(&FreeUnzeroedPageListHead
);
686 InitializeListHead(&FreeZeroedPageListHead
);
688 /* This is what a used page looks like */
689 RtlZeroMemory(&UsedPage
, sizeof(UsedPage
));
690 UsedPage
.u3
.e1
.PageLocation
= ActiveAndValid
;
691 UsedPage
.u3
.e2
.ReferenceCount
= 1;
693 /* Loop the memory descriptors */
694 for (NextEntry
= KeLoaderBlock
->MemoryDescriptorListHead
.Flink
;
695 NextEntry
!= &KeLoaderBlock
->MemoryDescriptorListHead
;
696 NextEntry
= NextEntry
->Flink
)
699 /* Get the descriptor */
700 Md
= CONTAINING_RECORD(NextEntry
,
701 MEMORY_ALLOCATION_DESCRIPTOR
,
705 /* Skip bad memory */
706 if ((Md
->MemoryType
== LoaderFirmwarePermanent
) ||
707 (Md
->MemoryType
== LoaderBBTMemory
) ||
708 (Md
->MemoryType
== LoaderSpecialMemory
) ||
709 (Md
->MemoryType
== LoaderBad
))
712 // We do not build PFN entries for this
716 else if ((Md
->MemoryType
== LoaderFree
) ||
717 (Md
->MemoryType
== LoaderLoadedProgram
) ||
718 (Md
->MemoryType
== LoaderFirmwareTemporary
) ||
719 (Md
->MemoryType
== LoaderOsloaderStack
))
721 /* Loop every page part of the block */
722 for (i
= 0; i
< Md
->PageCount
; i
++)
724 /* Mark it as a free page */
725 MmPfnDatabase
[0][Md
->BasePage
+ i
].u3
.e1
.PageLocation
= FreePageList
;
726 InsertTailList(&FreeUnzeroedPageListHead
,
727 &MmPfnDatabase
[0][Md
->BasePage
+ i
].ListEntry
);
734 /* Loop every page part of the block */
735 for (i
= 0; i
< Md
->PageCount
; i
++)
737 /* Everything else is used memory */
738 MmPfnDatabase
[0][Md
->BasePage
+ i
] = UsedPage
;
744 /* Finally handle the pages describing the PFN database themselves */
745 for (i
= MxOldFreeDescriptor
.BasePage
; i
< MxFreeDescriptor
->BasePage
; i
++)
747 /* Mark it as used kernel memory */
748 MmPfnDatabase
[0][i
] = UsedPage
;
752 KeInitializeEvent(&ZeroPageThreadEvent
, NotificationEvent
, TRUE
);
753 DPRINT("Pages: %x %x\n", MmAvailablePages
, NrSystemPages
);
754 MmInitializeBalancer(MmAvailablePages
, NrSystemPages
);
759 MmSetRmapListHeadPage(PFN_TYPE Pfn
, struct _MM_RMAP_ENTRY
* ListHead
)
763 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
764 MiGetPfnEntry(Pfn
)->RmapListHead
= (LONG
)ListHead
;
765 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
768 struct _MM_RMAP_ENTRY
*
770 MmGetRmapListHeadPage(PFN_TYPE Pfn
)
773 struct _MM_RMAP_ENTRY
* ListHead
;
775 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
776 ListHead
= (struct _MM_RMAP_ENTRY
*)MiGetPfnEntry(Pfn
)->RmapListHead
;
777 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
784 MmSetSavedSwapEntryPage(PFN_TYPE Pfn
, SWAPENTRY SwapEntry
)
788 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
789 MiGetPfnEntry(Pfn
)->SavedSwapEntry
= SwapEntry
;
790 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
795 MmGetSavedSwapEntryPage(PFN_TYPE Pfn
)
800 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
801 SwapEntry
= MiGetPfnEntry(Pfn
)->SavedSwapEntry
;
802 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
809 MmReferencePage(PFN_TYPE Pfn
)
813 DPRINT("MmReferencePage(PysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
815 if (Pfn
== 0 || Pfn
> MmHighestPhysicalPage
)
820 Page
= MiGetPfnEntry(Pfn
);
823 Page
->u3
.e2
.ReferenceCount
++;
828 MmGetReferenceCountPage(PFN_TYPE Pfn
)
834 DPRINT("MmGetReferenceCountPage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
836 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
837 Page
= MiGetPfnEntry(Pfn
);
840 RCount
= Page
->u3
.e2
.ReferenceCount
;
842 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
848 MmIsPageInUse(PFN_TYPE Pfn
)
850 return MiIsPfnInUse(MiGetPfnEntry(Pfn
));
855 MiSetConsumer(IN PFN_TYPE Pfn
,
858 MiGetPfnEntry(Pfn
)->u3
.e1
.PageLocation
= ActiveAndValid
;
863 MmDereferencePage(PFN_TYPE Pfn
)
867 DPRINT("MmDereferencePage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
869 Page
= MiGetPfnEntry(Pfn
);
872 Page
->u3
.e2
.ReferenceCount
--;
873 if (Page
->u3
.e2
.ReferenceCount
== 0)
876 Page
->u3
.e1
.PageLocation
= FreePageList
;
877 InsertTailList(&FreeUnzeroedPageListHead
,
880 if (UnzeroedPageCount
> 8 && 0 == KeReadStateEvent(&ZeroPageThreadEvent
))
882 KeSetEvent(&ZeroPageThreadEvent
, IO_NO_INCREMENT
, FALSE
);
889 MmAllocPage(ULONG Type
, SWAPENTRY SwapEntry
)
892 PLIST_ENTRY ListEntry
;
893 PPHYSICAL_PAGE PageDescriptor
;
894 BOOLEAN NeedClear
= FALSE
;
896 DPRINT("MmAllocPage()\n");
898 if (IsListEmpty(&FreeZeroedPageListHead
))
900 if (IsListEmpty(&FreeUnzeroedPageListHead
))
902 /* Check if this allocation is for the PFN DB itself */
903 if (MmNumberOfPhysicalPages
== 0)
908 DPRINT1("MmAllocPage(): Out of memory\n");
911 ListEntry
= RemoveTailList(&FreeUnzeroedPageListHead
);
914 PageDescriptor
= CONTAINING_RECORD(ListEntry
, PHYSICAL_PAGE
, ListEntry
);
920 ListEntry
= RemoveTailList(&FreeZeroedPageListHead
);
922 PageDescriptor
= CONTAINING_RECORD(ListEntry
, PHYSICAL_PAGE
, ListEntry
);
925 PageDescriptor
->u3
.e2
.ReferenceCount
= 1;
926 PageDescriptor
->SavedSwapEntry
= SwapEntry
;
930 PfnOffset
= PageDescriptor
- MmPfnDatabase
[0];
931 if ((NeedClear
) && (Type
!= MC_SYSTEM
))
933 MiZeroPage(PfnOffset
);
936 PageDescriptor
->u3
.e1
.PageLocation
= ActiveAndValid
;
942 MiZeroPage(PFN_TYPE Page
)
947 Irql
= KeRaiseIrqlToDpcLevel();
948 TempAddress
= MiMapPageToZeroInHyperSpace(Page
);
949 if (TempAddress
== NULL
)
951 return(STATUS_NO_MEMORY
);
953 memset(TempAddress
, 0, PAGE_SIZE
);
954 MiUnmapPagesInZeroSpace(TempAddress
, 1);
956 return(STATUS_SUCCESS
);
961 MmZeroPageThreadMain(PVOID Ignored
)
965 PLIST_ENTRY ListEntry
;
966 PPHYSICAL_PAGE PageDescriptor
;
970 /* Free initial kernel memory */
971 //MiFreeInitMemory();
973 /* Set our priority to 0 */
974 KeGetCurrentThread()->BasePriority
= 0;
975 KeSetPriorityThread(KeGetCurrentThread(), 0);
979 Status
= KeWaitForSingleObject(&ZeroPageThreadEvent
,
985 if (ZeroPageThreadShouldTerminate
)
987 DPRINT1("ZeroPageThread: Terminating\n");
988 return STATUS_SUCCESS
;
991 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
992 while (!IsListEmpty(&FreeUnzeroedPageListHead
))
994 ListEntry
= RemoveTailList(&FreeUnzeroedPageListHead
);
996 PageDescriptor
= CONTAINING_RECORD(ListEntry
, PHYSICAL_PAGE
, ListEntry
);
997 /* We set the page to used, because MmCreateVirtualMapping failed with unused pages */
998 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
999 Pfn
= PageDescriptor
- MmPfnDatabase
[0];
1000 Status
= MiZeroPage(Pfn
);
1002 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1003 PageDescriptor
->u3
.e1
.PageLocation
= ZeroedPageList
;
1004 if (NT_SUCCESS(Status
))
1006 InsertHeadList(&FreeZeroedPageListHead
, ListEntry
);
1011 InsertHeadList(&FreeUnzeroedPageListHead
, ListEntry
);
1012 UnzeroedPageCount
++;
1016 DPRINT("Zeroed %d pages.\n", Count
);
1017 KeResetEvent(&ZeroPageThreadEvent
);
1018 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
1021 return STATUS_SUCCESS
;