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 /* TYPES *******************************************************************/
26 #define MM_PHYSICAL_PAGE_FREE (0x1)
27 #define MM_PHYSICAL_PAGE_USED (0x2)
29 /* GLOBALS ****************************************************************/
33 // ReactOS to NT Physical Page Descriptor Entry Legacy Mapping Definitions
37 #define Consumer PageLocation
38 #define Type CacheAttribute
39 #define Zero PrototypePte
40 #define LockCount u3.e1.PageColor
41 #define RmapListHead AweReferenceCount
42 #define SavedSwapEntry u4.EntireFrame
44 #define ReferenceCount u3.ReferenceCount
45 #define RemoveEntryList(x) RemoveEntryList((PLIST_ENTRY)x)
46 #define InsertTailList(x, y) InsertTailList(x, (PLIST_ENTRY)y)
48 #define PHYSICAL_PAGE MMPFN
49 #define PPHYSICAL_PAGE PMMPFN
51 PPHYSICAL_PAGE MmPfnDatabase
;
53 ULONG MmAvailablePages
;
54 ULONG MmResidentAvailablePages
;
56 SIZE_T MmTotalCommitLimit
;
57 SIZE_T MmTotalCommittedPages
;
58 SIZE_T MmSharedCommit
;
59 SIZE_T MmDriverCommit
;
60 SIZE_T MmProcessCommit
;
61 SIZE_T MmPagedPoolCommit
;
62 SIZE_T MmPeakCommitment
;
63 SIZE_T MmtotalCommitLimitMaximum
;
65 MMPFNLIST MmZeroedPageListHead
;
66 MMPFNLIST MmFreePageListHead
;
67 MMPFNLIST MmStandbyPageListHead
;
68 MMPFNLIST MmModifiedPageListHead
;
69 MMPFNLIST MmModifiedNoWritePageListHead
;
71 /* List of pages allocated to the MC_USER Consumer */
72 static LIST_ENTRY UserPageListHead
;
73 /* List of pages zeroed by the ZPW (MmZeroPageThreadMain) */
74 static LIST_ENTRY FreeZeroedPageListHead
;
75 /* List of free pages, filled by MmGetReferenceCountPage and
76 * and MmInitializePageList */
77 static LIST_ENTRY FreeUnzeroedPageListHead
;
79 static KEVENT ZeroPageThreadEvent
;
80 static BOOLEAN ZeroPageThreadShouldTerminate
= FALSE
;
82 static ULONG UnzeroedPageCount
= 0;
84 /* FUNCTIONS *************************************************************/
88 MmGetLRUFirstUserPage(VOID
)
90 PLIST_ENTRY NextListEntry
;
91 PHYSICAL_PAGE
* PageDescriptor
;
94 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
95 NextListEntry
= UserPageListHead
.Flink
;
96 if (NextListEntry
== &UserPageListHead
)
98 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
101 PageDescriptor
= CONTAINING_RECORD(NextListEntry
, PHYSICAL_PAGE
, ListEntry
);
102 ASSERT_PFN(PageDescriptor
);
103 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
104 return PageDescriptor
- MmPfnDatabase
;
109 MmInsertLRULastUserPage(PFN_TYPE Pfn
)
114 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
115 Page
= MiGetPfnEntry(Pfn
);
117 ASSERT(Page
->Flags
.Type
== MM_PHYSICAL_PAGE_USED
);
118 ASSERT(Page
->Flags
.Consumer
== MC_USER
);
119 InsertTailList(&UserPageListHead
, &Page
->ListEntry
);
120 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
125 MmGetLRUNextUserPage(PFN_TYPE PreviousPfn
)
127 PLIST_ENTRY NextListEntry
;
128 PHYSICAL_PAGE
* PageDescriptor
;
132 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
133 Page
= MiGetPfnEntry(PreviousPfn
);
135 ASSERT(Page
->Flags
.Type
== MM_PHYSICAL_PAGE_USED
);
136 ASSERT(Page
->Flags
.Consumer
== MC_USER
);
137 NextListEntry
= (PLIST_ENTRY
)Page
->ListEntry
.Flink
;
138 if (NextListEntry
== &UserPageListHead
)
140 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
143 PageDescriptor
= CONTAINING_RECORD(NextListEntry
, PHYSICAL_PAGE
, ListEntry
);
144 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
145 return PageDescriptor
- MmPfnDatabase
;
150 MmRemoveLRUUserPage(PFN_TYPE Page
)
152 RemoveEntryList(&MiGetPfnEntry(Page
)->ListEntry
);
157 MiFindContiguousPages(IN PFN_NUMBER LowestPfn
,
158 IN PFN_NUMBER HighestPfn
,
159 IN PFN_NUMBER BoundaryPfn
,
160 IN PFN_NUMBER SizeInPages
,
161 IN MEMORY_CACHING_TYPE CacheType
)
163 PFN_NUMBER Page
, PageCount
, LastPage
, Length
, BoundaryMask
;
168 ASSERT(SizeInPages
!= 0);
171 // Convert the boundary PFN into an alignment mask
173 BoundaryMask
= ~(BoundaryPfn
- 1);
176 // Loop all the physical memory blocks
181 // Capture the base page and length of this memory block
183 Page
= MmPhysicalMemoryBlock
->Run
[i
].BasePage
;
184 PageCount
= MmPhysicalMemoryBlock
->Run
[i
].PageCount
;
187 // Check how far this memory block will go
189 LastPage
= Page
+ PageCount
;
192 // Trim it down to only the PFNs we're actually interested in
194 if ((LastPage
- 1) > HighestPfn
) LastPage
= HighestPfn
+ 1;
195 if (Page
< LowestPfn
) Page
= LowestPfn
;
198 // Skip this run if it's empty or fails to contain all the pages we need
200 if (!(PageCount
) || ((Page
+ SizeInPages
) > LastPage
)) continue;
203 // Now scan all the relevant PFNs in this run
206 for (Pfn1
= MiGetPfnEntry(Page
); Page
< LastPage
; Page
++, Pfn1
++)
209 // If this PFN is in use, ignore it
211 if (Pfn1
->Flags
.Type
!= MM_PHYSICAL_PAGE_FREE
) continue;
214 // If we haven't chosen a start PFN yet and the caller specified an
215 // alignment, make sure the page matches the alignment restriction
217 if ((!(Length
) && (BoundaryPfn
)) &&
218 (((Page
^ (Page
+ SizeInPages
- 1)) & BoundaryMask
)))
221 // It does not, so bail out
227 // Increase the number of valid pages, and check if we have enough
229 if (++Length
== SizeInPages
)
232 // It appears we've amassed enough legitimate pages, rollback
234 Pfn1
-= (Length
- 1);
235 Page
-= (Length
- 1);
238 // Acquire the PFN lock
240 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
244 // Things might've changed for us. Is the page still free?
246 if (Pfn1
->Flags
.Type
!= MM_PHYSICAL_PAGE_FREE
) break;
249 // So far so good. Is this the last confirmed valid page?
254 // Sanity check that we didn't go out of bounds
256 ASSERT(i
!= MmPhysicalMemoryBlock
->NumberOfRuns
);
259 // Loop until all PFN entries have been processed
261 EndPfn
= Pfn1
- SizeInPages
+ 1;
265 // If this was an unzeroed page, there are now less
267 if (Pfn1
->Flags
.Zero
== 0) UnzeroedPageCount
--;
270 // One less free page
275 // This PFN is now a used page, set it up
277 RemoveEntryList(&Pfn1
->ListEntry
);
278 Pfn1
->Flags
.Type
= MM_PHYSICAL_PAGE_USED
;
279 Pfn1
->Flags
.Consumer
= MC_NPPOOL
;
280 Pfn1
->ReferenceCount
= 1;
282 Pfn1
->SavedSwapEntry
= 0;
285 // Check if it was already zeroed
287 if (Pfn1
->Flags
.Zero
== 0)
290 // It wasn't, so zero it
292 MiZeroPage(MiGetPfnEntryIndex(Pfn1
));
296 // Check if this is the last PFN, otherwise go on
298 if (Pfn1
== EndPfn
) break;
303 // Mark the first and last PFN so we can find them later
305 Pfn1
->Flags
.StartOfAllocation
= 1;
306 (Pfn1
+ SizeInPages
- 1)->Flags
.EndOfAllocation
= 1;
309 // Now it's safe to let go of the PFN lock
311 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
314 // Quick sanity check that the last PFN is consistent
316 EndPfn
= Pfn1
+ SizeInPages
;
317 ASSERT(EndPfn
== MiGetPfnEntry(Page
+ 1));
320 // Compute the first page, and make sure it's consistent
322 Page
-= SizeInPages
- 1;
323 ASSERT(Pfn1
== MiGetPfnEntry(Page
));
329 // Keep going. The purpose of this loop is to reconfirm that
330 // after acquiring the PFN lock these pages are still usable
337 // If we got here, something changed while we hadn't acquired
338 // the PFN lock yet, so we'll have to restart
340 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
344 } while (++i
!= MmPhysicalMemoryBlock
->NumberOfRuns
);
347 // And if we get here, it means no suitable physical memory runs were found
354 MiAllocatePagesForMdl(IN PHYSICAL_ADDRESS LowAddress
,
355 IN PHYSICAL_ADDRESS HighAddress
,
356 IN PHYSICAL_ADDRESS SkipBytes
,
357 IN SIZE_T TotalBytes
,
358 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
,
362 PFN_NUMBER PageCount
, LowPage
, HighPage
, SkipPages
, PagesFound
= 0, Page
;
363 PPFN_NUMBER MdlPage
, LastMdlPage
;
365 PLIST_ENTRY ListEntry
;
367 INT LookForZeroedPages
;
368 ASSERT (KeGetCurrentIrql() <= APC_LEVEL
);
371 // Convert the low address into a PFN
373 LowPage
= (PFN_NUMBER
)(LowAddress
.QuadPart
>> PAGE_SHIFT
);
376 // Convert, and normalize, the high address into a PFN
378 HighPage
= (PFN_NUMBER
)(HighAddress
.QuadPart
>> PAGE_SHIFT
);
379 if (HighPage
> MmHighestPhysicalPage
) HighPage
= MmHighestPhysicalPage
;
382 // Validate skipbytes and convert them into pages
384 if (BYTE_OFFSET(SkipBytes
.LowPart
)) return NULL
;
385 SkipPages
= (PFN_NUMBER
)(SkipBytes
.QuadPart
>> PAGE_SHIFT
);
388 // Now compute the number of pages the MDL will cover
390 PageCount
= (PFN_NUMBER
)ADDRESS_AND_SIZE_TO_SPAN_PAGES(0, TotalBytes
);
394 // Try creating an MDL for these many pages
396 Mdl
= MmCreateMdl(NULL
, NULL
, PageCount
<< PAGE_SHIFT
);
400 // This function is not required to return the amount of pages requested
401 // In fact, it can return as little as 1 page, and callers are supposed
402 // to deal with this scenario. So re-attempt the allocation with less
403 // pages than before, and see if it worked this time.
405 PageCount
-= (PageCount
>> 4);
409 // Wow, not even a single page was around!
411 if (!Mdl
) return NULL
;
414 // This is where the page array starts....
416 MdlPage
= (PPFN_NUMBER
)(Mdl
+ 1);
419 // Lock the PFN database
421 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
424 // Are we looking for any pages, without discriminating?
426 if ((LowPage
== 0) && (HighPage
== MmHighestPhysicalPage
))
429 // Well then, let's go shopping
431 while (PagesFound
< PageCount
)
434 // Do we have zeroed pages?
436 if (!IsListEmpty(&FreeZeroedPageListHead
))
441 ListEntry
= RemoveTailList(&FreeZeroedPageListHead
);
443 else if (!IsListEmpty(&FreeUnzeroedPageListHead
))
446 // Nope, grab an unzeroed page
448 ListEntry
= RemoveTailList(&FreeUnzeroedPageListHead
);
454 // This is not good... hopefully we have at least SOME pages
461 // Get the PFN entry for this page
463 Pfn1
= CONTAINING_RECORD(ListEntry
, PHYSICAL_PAGE
, ListEntry
);
466 // Make sure it's really free
468 ASSERT(Pfn1
->Flags
.Type
== MM_PHYSICAL_PAGE_FREE
);
469 ASSERT(Pfn1
->ReferenceCount
== 0);
472 // Allocate it and mark it
474 Pfn1
->Flags
.Type
= MM_PHYSICAL_PAGE_USED
;
475 Pfn1
->Flags
.Consumer
= MC_NPPOOL
;
476 Pfn1
->Flags
.StartOfAllocation
= 1;
477 Pfn1
->Flags
.EndOfAllocation
= 1;
478 Pfn1
->ReferenceCount
= 1;
480 Pfn1
->SavedSwapEntry
= 0;
483 // Decrease available pages
488 // Save it into the MDL
490 *MdlPage
++ = MiGetPfnEntryIndex(Pfn1
);
497 // You want specific range of pages. We'll do this in two runs
499 for (LookForZeroedPages
= 1; LookForZeroedPages
>= 0; LookForZeroedPages
--)
502 // Scan the range you specified
504 for (Page
= LowPage
; Page
< HighPage
; Page
++)
507 // Get the PFN entry for this page
509 Pfn1
= MiGetPfnEntry(Page
);
513 // Make sure it's free and if this is our first pass, zeroed
515 if (Pfn1
->Flags
.Type
!= MM_PHYSICAL_PAGE_FREE
) continue;
516 if (Pfn1
->Flags
.Zero
!= LookForZeroedPages
) continue;
521 ASSERT(Pfn1
->ReferenceCount
== 0);
524 // Now setup the page and mark it
526 Pfn1
->Flags
.Type
= MM_PHYSICAL_PAGE_USED
;
527 Pfn1
->Flags
.Consumer
= MC_NPPOOL
;
528 Pfn1
->ReferenceCount
= 1;
529 Pfn1
->Flags
.StartOfAllocation
= 1;
530 Pfn1
->Flags
.EndOfAllocation
= 1;
532 Pfn1
->SavedSwapEntry
= 0;
535 // If this page was unzeroed, we've consumed such a page
537 if (!Pfn1
->Flags
.Zero
) UnzeroedPageCount
--;
540 // Decrease available pages
545 // Save this page into the MDL
548 if (++PagesFound
== PageCount
) break;
552 // If the first pass was enough, don't keep going, otherwise, go again
554 if (PagesFound
== PageCount
) break;
559 // Now release the PFN count
561 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
564 // We might've found less pages, but not more ;-)
566 if (PagesFound
!= PageCount
) ASSERT(PagesFound
< PageCount
);
570 // If we didn' tfind any pages at all, fail
572 DPRINT1("NO MDL PAGES!\n");
578 // Write out how many pages we found
580 Mdl
->ByteCount
= (ULONG
)(PagesFound
<< PAGE_SHIFT
);
583 // Terminate the MDL array if there's certain missing pages
585 if (PagesFound
!= PageCount
) *MdlPage
= -1;
588 // Now go back and loop over all the MDL pages
590 MdlPage
= (PPFN_NUMBER
)(Mdl
+ 1);
591 LastMdlPage
= MdlPage
+ PagesFound
;
592 while (MdlPage
< LastMdlPage
)
595 // Check if we've reached the end
598 if (Page
== (PFN_NUMBER
)-1) break;
601 // Get the PFN entry for the page and check if we should zero it out
603 Pfn1
= MiGetPfnEntry(Page
);
605 if (Pfn1
->Flags
.Zero
== 0) MiZeroPage(Page
);
609 // We're done, mark the pages as locked (should we lock them, though???)
612 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
618 MmDumpPfnDatabase(VOID
)
622 PCHAR State
= "????", Consumer
= "Unknown";
624 ULONG Totals
[5] = {0}, FreePages
= 0;
626 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
629 // Loop the PFN database
631 for (i
= 0; i
<= MmHighestPhysicalPage
; i
++)
633 Pfn1
= MiGetPfnEntry(i
);
639 switch (Pfn1
->Flags
.Consumer
)
643 Consumer
= "Nonpaged Pool";
648 Consumer
= "Paged Pool";
653 Consumer
= "File System Cache";
658 Consumer
= "Process Working Set";
670 switch (Pfn1
->Flags
.Type
)
672 case MM_PHYSICAL_PAGE_USED
:
675 Totals
[Pfn1
->Flags
.Consumer
]++;
678 case MM_PHYSICAL_PAGE_FREE
:
687 // Pretty-print the page
689 DbgPrint("0x%08p:\t%04s\t%20s\t(%02d.%02d) [%08p])\n",
693 Pfn1
->ReferenceCount
,
698 DbgPrint("Nonpaged Pool: %d pages\t[%d KB]\n", Totals
[MC_NPPOOL
], (Totals
[MC_NPPOOL
] << PAGE_SHIFT
) / 1024);
699 DbgPrint("Paged Pool: %d pages\t[%d KB]\n", Totals
[MC_PPOOL
], (Totals
[MC_PPOOL
] << PAGE_SHIFT
) / 1024);
700 DbgPrint("File System Cache: %d pages\t[%d KB]\n", Totals
[MC_CACHE
], (Totals
[MC_CACHE
] << PAGE_SHIFT
) / 1024);
701 DbgPrint("Process Working Set: %d pages\t[%d KB]\n", Totals
[MC_USER
], (Totals
[MC_USER
] << PAGE_SHIFT
) / 1024);
702 DbgPrint("System: %d pages\t[%d KB]\n", Totals
[MC_SYSTEM
], (Totals
[MC_SYSTEM
] << PAGE_SHIFT
) / 1024);
703 DbgPrint("Free: %d pages\t[%d KB]\n", FreePages
, (FreePages
<< PAGE_SHIFT
) / 1024);
705 KeLowerIrql(OldIrql
);
710 MmInitializePageList(VOID
)
713 PHYSICAL_PAGE UsedPage
;
714 PMEMORY_ALLOCATION_DESCRIPTOR Md
;
715 PLIST_ENTRY NextEntry
;
716 ULONG NrSystemPages
= 0;
718 /* Initialize the page lists */
719 InitializeListHead(&UserPageListHead
);
720 InitializeListHead(&FreeUnzeroedPageListHead
);
721 InitializeListHead(&FreeZeroedPageListHead
);
723 /* This is what a used page looks like */
724 RtlZeroMemory(&UsedPage
, sizeof(UsedPage
));
725 UsedPage
.Flags
.Type
= MM_PHYSICAL_PAGE_USED
;
726 UsedPage
.Flags
.Consumer
= MC_NPPOOL
;
727 UsedPage
.ReferenceCount
= 1;
729 /* Loop the memory descriptors */
730 for (NextEntry
= KeLoaderBlock
->MemoryDescriptorListHead
.Flink
;
731 NextEntry
!= &KeLoaderBlock
->MemoryDescriptorListHead
;
732 NextEntry
= NextEntry
->Flink
)
735 /* Get the descriptor */
736 Md
= CONTAINING_RECORD(NextEntry
,
737 MEMORY_ALLOCATION_DESCRIPTOR
,
741 /* Skip bad memory */
742 if ((Md
->MemoryType
== LoaderFirmwarePermanent
) ||
743 (Md
->MemoryType
== LoaderBBTMemory
) ||
744 (Md
->MemoryType
== LoaderSpecialMemory
) ||
745 (Md
->MemoryType
== LoaderBad
))
748 // We do not build PFN entries for this
752 else if ((Md
->MemoryType
== LoaderFree
) ||
753 (Md
->MemoryType
== LoaderLoadedProgram
) ||
754 (Md
->MemoryType
== LoaderFirmwareTemporary
) ||
755 (Md
->MemoryType
== LoaderOsloaderStack
))
757 /* Loop every page part of the block */
758 for (i
= 0; i
< Md
->PageCount
; i
++)
760 /* Mark it as a free page */
761 MmPfnDatabase
[Md
->BasePage
+ i
].Flags
.Type
= MM_PHYSICAL_PAGE_FREE
;
762 InsertTailList(&FreeUnzeroedPageListHead
,
763 &MmPfnDatabase
[Md
->BasePage
+ i
].ListEntry
);
770 /* Loop every page part of the block */
771 for (i
= 0; i
< Md
->PageCount
; i
++)
773 /* Everything else is used memory */
774 MmPfnDatabase
[Md
->BasePage
+ i
] = UsedPage
;
780 /* Finally handle the pages describing the PFN database themselves */
781 for (i
= MxOldFreeDescriptor
.BasePage
; i
< MxFreeDescriptor
->BasePage
; i
++)
783 /* Ensure this page was not added previously */
784 ASSERT(MmPfnDatabase
[i
].Flags
.Type
== 0);
786 /* Mark it as used kernel memory */
787 MmPfnDatabase
[i
] = UsedPage
;
791 KeInitializeEvent(&ZeroPageThreadEvent
, NotificationEvent
, TRUE
);
792 DPRINT("Pages: %x %x\n", MmAvailablePages
, NrSystemPages
);
793 MmInitializeBalancer(MmAvailablePages
, NrSystemPages
);
798 MmSetRmapListHeadPage(PFN_TYPE Pfn
, struct _MM_RMAP_ENTRY
* ListHead
)
802 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
803 MiGetPfnEntry(Pfn
)->RmapListHead
= (LONG
)ListHead
;
804 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
807 struct _MM_RMAP_ENTRY
*
809 MmGetRmapListHeadPage(PFN_TYPE Pfn
)
812 struct _MM_RMAP_ENTRY
* ListHead
;
814 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
815 ListHead
= (struct _MM_RMAP_ENTRY
*)MiGetPfnEntry(Pfn
)->RmapListHead
;
816 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
823 MmSetSavedSwapEntryPage(PFN_TYPE Pfn
, SWAPENTRY SwapEntry
)
827 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
828 MiGetPfnEntry(Pfn
)->SavedSwapEntry
= SwapEntry
;
829 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
834 MmGetSavedSwapEntryPage(PFN_TYPE Pfn
)
839 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
840 SwapEntry
= MiGetPfnEntry(Pfn
)->SavedSwapEntry
;
841 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
848 MmReferencePage(PFN_TYPE Pfn
)
852 DPRINT("MmReferencePage(PysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
854 if (Pfn
== 0 || Pfn
> MmHighestPhysicalPage
)
859 Page
= MiGetPfnEntry(Pfn
);
861 if (Page
->Flags
.Type
!= MM_PHYSICAL_PAGE_USED
)
863 DPRINT1("Referencing non-used page\n");
864 KeBugCheck(MEMORY_MANAGEMENT
);
867 Page
->ReferenceCount
++;
872 MmGetReferenceCountPage(PFN_TYPE Pfn
)
878 DPRINT("MmGetReferenceCountPage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
880 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
881 Page
= MiGetPfnEntry(Pfn
);
883 if (Page
->Flags
.Type
!= MM_PHYSICAL_PAGE_USED
)
885 DPRINT1("Getting reference count for free page\n");
886 KeBugCheck(MEMORY_MANAGEMENT
);
889 RCount
= Page
->ReferenceCount
;
891 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
897 MmIsPageInUse(PFN_TYPE Pfn
)
900 DPRINT("MmIsPageInUse(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
902 return (MiGetPfnEntry(Pfn
)->Flags
.Type
== MM_PHYSICAL_PAGE_USED
);
907 MmDereferencePage(PFN_TYPE Pfn
)
911 DPRINT("MmDereferencePage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
913 Page
= MiGetPfnEntry(Pfn
);
916 if (Page
->Flags
.Type
!= MM_PHYSICAL_PAGE_USED
)
918 DPRINT1("Dereferencing free page\n");
919 KeBugCheck(MEMORY_MANAGEMENT
);
921 if (Page
->ReferenceCount
== 0)
923 DPRINT1("Derefrencing page with reference count 0\n");
924 KeBugCheck(MEMORY_MANAGEMENT
);
927 Page
->ReferenceCount
--;
928 if (Page
->ReferenceCount
== 0)
931 if (Page
->Flags
.Consumer
== MC_USER
) RemoveEntryList(&Page
->ListEntry
);
932 if (Page
->RmapListHead
!= (LONG
)NULL
)
934 DPRINT1("Freeing page with rmap entries.\n");
935 KeBugCheck(MEMORY_MANAGEMENT
);
937 if (Page
->LockCount
> 0)
939 DPRINT1("Freeing locked page\n");
940 KeBugCheck(MEMORY_MANAGEMENT
);
942 if (Page
->SavedSwapEntry
!= 0)
944 DPRINT1("Freeing page with swap entry.\n");
945 KeBugCheck(MEMORY_MANAGEMENT
);
947 if (Page
->Flags
.Type
!= MM_PHYSICAL_PAGE_USED
)
949 DPRINT1("Freeing page with flags %x\n",
951 KeBugCheck(MEMORY_MANAGEMENT
);
953 Page
->Flags
.Type
= MM_PHYSICAL_PAGE_FREE
;
954 Page
->Flags
.Consumer
= MC_MAXIMUM
;
955 InsertTailList(&FreeUnzeroedPageListHead
,
958 if (UnzeroedPageCount
> 8 && 0 == KeReadStateEvent(&ZeroPageThreadEvent
))
960 KeSetEvent(&ZeroPageThreadEvent
, IO_NO_INCREMENT
, FALSE
);
967 MmGetLockCountPage(PFN_TYPE Pfn
)
970 ULONG CurrentLockCount
;
973 DPRINT("MmGetLockCountPage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
975 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
977 Page
= MiGetPfnEntry(Pfn
);
979 if (Page
->Flags
.Type
!= MM_PHYSICAL_PAGE_USED
)
981 DPRINT1("Getting lock count for free page\n");
982 KeBugCheck(MEMORY_MANAGEMENT
);
985 CurrentLockCount
= Page
->LockCount
;
986 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
988 return(CurrentLockCount
);
993 MmLockPage(PFN_TYPE Pfn
)
997 DPRINT("MmLockPage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
999 Page
= MiGetPfnEntry(Pfn
);
1001 if (Page
->Flags
.Type
!= MM_PHYSICAL_PAGE_USED
)
1003 DPRINT1("Locking free page\n");
1004 KeBugCheck(MEMORY_MANAGEMENT
);
1012 MmUnlockPage(PFN_TYPE Pfn
)
1014 PPHYSICAL_PAGE Page
;
1016 DPRINT("MmUnlockPage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
1018 Page
= MiGetPfnEntry(Pfn
);
1020 if (Page
->Flags
.Type
!= MM_PHYSICAL_PAGE_USED
)
1022 DPRINT1("Unlocking free page\n");
1023 KeBugCheck(MEMORY_MANAGEMENT
);
1031 MmAllocPage(ULONG Consumer
, SWAPENTRY SwapEntry
)
1034 PLIST_ENTRY ListEntry
;
1035 PPHYSICAL_PAGE PageDescriptor
;
1036 BOOLEAN NeedClear
= FALSE
;
1038 DPRINT("MmAllocPage()\n");
1040 if (IsListEmpty(&FreeZeroedPageListHead
))
1042 if (IsListEmpty(&FreeUnzeroedPageListHead
))
1044 /* Check if this allocation is for the PFN DB itself */
1045 if (MmNumberOfPhysicalPages
== 0)
1050 DPRINT1("MmAllocPage(): Out of memory\n");
1053 ListEntry
= RemoveTailList(&FreeUnzeroedPageListHead
);
1054 UnzeroedPageCount
--;
1056 PageDescriptor
= CONTAINING_RECORD(ListEntry
, PHYSICAL_PAGE
, ListEntry
);
1062 ListEntry
= RemoveTailList(&FreeZeroedPageListHead
);
1064 PageDescriptor
= CONTAINING_RECORD(ListEntry
, PHYSICAL_PAGE
, ListEntry
);
1067 if (PageDescriptor
->Flags
.Type
!= MM_PHYSICAL_PAGE_FREE
)
1069 DPRINT1("Got non-free page from freelist\n");
1070 KeBugCheck(MEMORY_MANAGEMENT
);
1072 if (PageDescriptor
->ReferenceCount
!= 0)
1074 DPRINT1("%d\n", PageDescriptor
->ReferenceCount
);
1075 KeBugCheck(MEMORY_MANAGEMENT
);
1077 PageDescriptor
->Flags
.Type
= MM_PHYSICAL_PAGE_USED
;
1078 PageDescriptor
->Flags
.Consumer
= Consumer
;
1079 PageDescriptor
->ReferenceCount
= 1;
1080 PageDescriptor
->LockCount
= 0;
1081 PageDescriptor
->SavedSwapEntry
= SwapEntry
;
1085 PfnOffset
= PageDescriptor
- MmPfnDatabase
;
1086 if ((NeedClear
) && (Consumer
!= MC_SYSTEM
))
1088 MiZeroPage(PfnOffset
);
1095 MiZeroPage(PFN_TYPE Page
)
1100 Irql
= KeRaiseIrqlToDpcLevel();
1101 TempAddress
= MiMapPageToZeroInHyperSpace(Page
);
1102 if (TempAddress
== NULL
)
1104 return(STATUS_NO_MEMORY
);
1106 memset(TempAddress
, 0, PAGE_SIZE
);
1107 MiUnmapPagesInZeroSpace(TempAddress
, 1);
1109 return(STATUS_SUCCESS
);
1114 MmZeroPageThreadMain(PVOID Ignored
)
1118 PLIST_ENTRY ListEntry
;
1119 PPHYSICAL_PAGE PageDescriptor
;
1123 /* Free initial kernel memory */
1124 //MiFreeInitMemory();
1126 /* Set our priority to 0 */
1127 KeGetCurrentThread()->BasePriority
= 0;
1128 KeSetPriorityThread(KeGetCurrentThread(), 0);
1132 Status
= KeWaitForSingleObject(&ZeroPageThreadEvent
,
1137 if (!NT_SUCCESS(Status
))
1139 DPRINT1("ZeroPageThread: Wait failed\n");
1140 KeBugCheck(MEMORY_MANAGEMENT
);
1143 if (ZeroPageThreadShouldTerminate
)
1145 DPRINT1("ZeroPageThread: Terminating\n");
1146 return STATUS_SUCCESS
;
1149 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1150 while (!IsListEmpty(&FreeUnzeroedPageListHead
))
1152 ListEntry
= RemoveTailList(&FreeUnzeroedPageListHead
);
1153 UnzeroedPageCount
--;
1154 PageDescriptor
= CONTAINING_RECORD(ListEntry
, PHYSICAL_PAGE
, ListEntry
);
1155 /* We set the page to used, because MmCreateVirtualMapping failed with unused pages */
1156 PageDescriptor
->Flags
.Type
= MM_PHYSICAL_PAGE_USED
;
1157 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
1158 Pfn
= PageDescriptor
- MmPfnDatabase
;
1159 Status
= MiZeroPage(Pfn
);
1161 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1162 PageDescriptor
->Flags
.Zero
= 1;
1163 PageDescriptor
->Flags
.Type
= MM_PHYSICAL_PAGE_FREE
;
1164 if (NT_SUCCESS(Status
))
1166 InsertHeadList(&FreeZeroedPageListHead
, ListEntry
);
1171 InsertHeadList(&FreeUnzeroedPageListHead
, ListEntry
);
1172 UnzeroedPageCount
++;
1176 DPRINT("Zeroed %d pages.\n", Count
);
1177 KeResetEvent(&ZeroPageThreadEvent
);
1178 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
1181 return STATUS_SUCCESS
;