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 PHYSICAL_PAGE MMPFN
34 #define PPHYSICAL_PAGE PMMPFN
36 PPHYSICAL_PAGE MmPfnDatabase
;
38 PFN_NUMBER MmAvailablePages
;
39 PFN_NUMBER MmResidentAvailablePages
;
40 PFN_NUMBER MmResidentAvailableAtInit
;
42 SIZE_T MmTotalCommitLimit
;
43 SIZE_T MmTotalCommittedPages
;
44 SIZE_T MmSharedCommit
;
45 SIZE_T MmDriverCommit
;
46 SIZE_T MmProcessCommit
;
47 SIZE_T MmPagedPoolCommit
;
48 SIZE_T MmPeakCommitment
;
49 SIZE_T MmtotalCommitLimitMaximum
;
51 KEVENT ZeroPageThreadEvent
;
52 static BOOLEAN ZeroPageThreadShouldTerminate
= FALSE
;
53 static RTL_BITMAP MiUserPfnBitMap
;
55 /* FUNCTIONS *************************************************************/
59 MiInitializeUserPfnBitmap(VOID
)
63 /* Allocate enough buffer for the PFN bitmap and align it on 32-bits */
64 Bitmap
= ExAllocatePoolWithTag(NonPagedPool
,
65 (((MmHighestPhysicalPage
+ 1) + 31) / 32) * 4,
69 /* Initialize it and clear all the bits to begin with */
70 RtlInitializeBitMap(&MiUserPfnBitMap
,
72 MmHighestPhysicalPage
+ 1);
73 RtlClearAllBits(&MiUserPfnBitMap
);
78 MmGetLRUFirstUserPage(VOID
)
83 /* Find the first user page */
84 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
85 Position
= RtlFindSetBits(&MiUserPfnBitMap
, 1, 0);
86 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
87 if (Position
== 0xFFFFFFFF) return 0;
95 MmInsertLRULastUserPage(PFN_TYPE Pfn
)
99 /* Set the page as a user page */
100 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
101 RtlSetBit(&MiUserPfnBitMap
, Pfn
);
102 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
107 MmGetLRUNextUserPage(PFN_TYPE PreviousPfn
)
112 /* Find the next user page */
113 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
114 Position
= RtlFindSetBits(&MiUserPfnBitMap
, 1, PreviousPfn
+ 1);
115 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
116 if (Position
== 0xFFFFFFFF) return 0;
124 MmRemoveLRUUserPage(PFN_TYPE Page
)
126 /* Unset the page as a user page */
127 RtlClearBit(&MiUserPfnBitMap
, Page
);
132 MiIsPfnFree(IN PMMPFN Pfn1
)
134 /* Must be a free or zero page, with no references, linked */
135 return ((Pfn1
->u3
.e1
.PageLocation
<= StandbyPageList
) &&
138 !(Pfn1
->u3
.e2
.ReferenceCount
));
143 MiIsPfnInUse(IN PMMPFN Pfn1
)
145 /* Standby list or higher, unlinked, and with references */
146 return !MiIsPfnFree(Pfn1
);
151 MiAllocatePagesForMdl(IN PHYSICAL_ADDRESS LowAddress
,
152 IN PHYSICAL_ADDRESS HighAddress
,
153 IN PHYSICAL_ADDRESS SkipBytes
,
154 IN SIZE_T TotalBytes
,
155 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
,
159 PFN_NUMBER PageCount
, LowPage
, HighPage
, SkipPages
, PagesFound
= 0, Page
;
160 PPFN_NUMBER MdlPage
, LastMdlPage
;
163 INT LookForZeroedPages
;
164 ASSERT (KeGetCurrentIrql() <= APC_LEVEL
);
167 // Convert the low address into a PFN
169 LowPage
= (PFN_NUMBER
)(LowAddress
.QuadPart
>> PAGE_SHIFT
);
172 // Convert, and normalize, the high address into a PFN
174 HighPage
= (PFN_NUMBER
)(HighAddress
.QuadPart
>> PAGE_SHIFT
);
175 if (HighPage
> MmHighestPhysicalPage
) HighPage
= MmHighestPhysicalPage
;
178 // Validate skipbytes and convert them into pages
180 if (BYTE_OFFSET(SkipBytes
.LowPart
)) return NULL
;
181 SkipPages
= (PFN_NUMBER
)(SkipBytes
.QuadPart
>> PAGE_SHIFT
);
184 // Now compute the number of pages the MDL will cover
186 PageCount
= (PFN_NUMBER
)ADDRESS_AND_SIZE_TO_SPAN_PAGES(0, TotalBytes
);
190 // Try creating an MDL for these many pages
192 Mdl
= MmCreateMdl(NULL
, NULL
, PageCount
<< PAGE_SHIFT
);
196 // This function is not required to return the amount of pages requested
197 // In fact, it can return as little as 1 page, and callers are supposed
198 // to deal with this scenario. So re-attempt the allocation with less
199 // pages than before, and see if it worked this time.
201 PageCount
-= (PageCount
>> 4);
205 // Wow, not even a single page was around!
207 if (!Mdl
) return NULL
;
210 // This is where the page array starts....
212 MdlPage
= (PPFN_NUMBER
)(Mdl
+ 1);
215 // Lock the PFN database
217 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
220 // Are we looking for any pages, without discriminating?
222 if ((LowPage
== 0) && (HighPage
== MmHighestPhysicalPage
))
225 // Well then, let's go shopping
227 while (PagesFound
< PageCount
)
230 // Do we have zeroed pages?
232 if (MmZeroedPageListHead
.Total
)
237 Pfn1
= MiRemoveHeadList(&MmZeroedPageListHead
);
239 else if (MmFreePageListHead
.Total
)
242 // Nope, grab an unzeroed page
244 Pfn1
= MiRemoveHeadList(&MmFreePageListHead
);
249 // This is not good... hopefully we have at least SOME pages
256 // Make sure it's really free
258 ASSERT(MiIsPfnInUse(Pfn1
) == FALSE
);
259 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
262 // Allocate it and mark it
264 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
265 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
266 Pfn1
->u3
.e2
.ReferenceCount
= 1;
269 // Decrease available pages
274 // Save it into the MDL
276 *MdlPage
++ = MiGetPfnEntryIndex(Pfn1
);
283 // You want specific range of pages. We'll do this in two runs
285 for (LookForZeroedPages
= 1; LookForZeroedPages
>= 0; LookForZeroedPages
--)
288 // Scan the range you specified
290 for (Page
= LowPage
; Page
< HighPage
; Page
++)
293 // Get the PFN entry for this page
295 Pfn1
= MiGetPfnEntry(Page
);
299 // Make sure it's free and if this is our first pass, zeroed
301 if (MiIsPfnInUse(Pfn1
)) continue;
302 if ((Pfn1
->u3
.e1
.PageLocation
== ZeroedPageList
) != LookForZeroedPages
) continue;
307 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
310 // Now setup the page and mark it
312 Pfn1
->u3
.e2
.ReferenceCount
= 1;
313 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
314 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
317 // Decrease available pages
322 // Save this page into the MDL
325 if (++PagesFound
== PageCount
) break;
329 // If the first pass was enough, don't keep going, otherwise, go again
331 if (PagesFound
== PageCount
) break;
336 // Now release the PFN count
338 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
341 // We might've found less pages, but not more ;-)
343 if (PagesFound
!= PageCount
) ASSERT(PagesFound
< PageCount
);
347 // If we didn' tfind any pages at all, fail
349 DPRINT1("NO MDL PAGES!\n");
355 // Write out how many pages we found
357 Mdl
->ByteCount
= (ULONG
)(PagesFound
<< PAGE_SHIFT
);
360 // Terminate the MDL array if there's certain missing pages
362 if (PagesFound
!= PageCount
) *MdlPage
= -1;
365 // Now go back and loop over all the MDL pages
367 MdlPage
= (PPFN_NUMBER
)(Mdl
+ 1);
368 LastMdlPage
= MdlPage
+ PagesFound
;
369 while (MdlPage
< LastMdlPage
)
372 // Check if we've reached the end
375 if (Page
== (PFN_NUMBER
)-1) break;
378 // Get the PFN entry for the page and check if we should zero it out
380 Pfn1
= MiGetPfnEntry(Page
);
382 if (Pfn1
->u3
.e1
.PageLocation
!= ZeroedPageList
) MiZeroPage(Page
);
383 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
387 // We're done, mark the pages as locked (should we lock them, though???)
390 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
396 MmDumpPfnDatabase(VOID
)
400 PCHAR State
= "????", Type
= "Unknown";
402 ULONG Totals
[5] = {0}, FreePages
= 0;
404 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
407 // Loop the PFN database
409 for (i
= 0; i
<= MmHighestPhysicalPage
; i
++)
411 Pfn1
= MiGetPfnEntry(i
);
417 if (MiIsPfnInUse(Pfn1
))
430 // Pretty-print the page
432 DbgPrint("0x%08p:\t%04s\t%20s\t(%02d) [%08p])\n",
436 Pfn1
->u3
.e2
.ReferenceCount
,
440 DbgPrint("Nonpaged Pool: %d pages\t[%d KB]\n", Totals
[MC_NPPOOL
], (Totals
[MC_NPPOOL
] << PAGE_SHIFT
) / 1024);
441 DbgPrint("Paged Pool: %d pages\t[%d KB]\n", Totals
[MC_PPOOL
], (Totals
[MC_PPOOL
] << PAGE_SHIFT
) / 1024);
442 DbgPrint("File System Cache: %d pages\t[%d KB]\n", Totals
[MC_CACHE
], (Totals
[MC_CACHE
] << PAGE_SHIFT
) / 1024);
443 DbgPrint("Process Working Set: %d pages\t[%d KB]\n", Totals
[MC_USER
], (Totals
[MC_USER
] << PAGE_SHIFT
) / 1024);
444 DbgPrint("System: %d pages\t[%d KB]\n", Totals
[MC_SYSTEM
], (Totals
[MC_SYSTEM
] << PAGE_SHIFT
) / 1024);
445 DbgPrint("Free: %d pages\t[%d KB]\n", FreePages
, (FreePages
<< PAGE_SHIFT
) / 1024);
447 KeLowerIrql(OldIrql
);
452 MmSetRmapListHeadPage(PFN_TYPE Pfn
, struct _MM_RMAP_ENTRY
* ListHead
)
456 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
457 MiGetPfnEntry(Pfn
)->RmapListHead
= (LONG
)ListHead
;
458 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
461 struct _MM_RMAP_ENTRY
*
463 MmGetRmapListHeadPage(PFN_TYPE Pfn
)
466 struct _MM_RMAP_ENTRY
* ListHead
;
468 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
469 ListHead
= (struct _MM_RMAP_ENTRY
*)MiGetPfnEntry(Pfn
)->RmapListHead
;
470 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
477 MmSetSavedSwapEntryPage(PFN_TYPE Pfn
, SWAPENTRY SwapEntry
)
481 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
482 MiGetPfnEntry(Pfn
)->u1
.WsIndex
= SwapEntry
;
483 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
488 MmGetSavedSwapEntryPage(PFN_TYPE Pfn
)
493 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
494 SwapEntry
= MiGetPfnEntry(Pfn
)->u1
.WsIndex
;
495 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
502 MmReferencePage(PFN_TYPE Pfn
)
506 DPRINT("MmReferencePage(PysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
508 if (Pfn
== 0 || Pfn
> MmHighestPhysicalPage
)
513 Page
= MiGetPfnEntry(Pfn
);
516 Page
->u3
.e2
.ReferenceCount
++;
521 MmGetReferenceCountPage(PFN_TYPE Pfn
)
527 DPRINT("MmGetReferenceCountPage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
529 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
530 Page
= MiGetPfnEntry(Pfn
);
533 RCount
= Page
->u3
.e2
.ReferenceCount
;
535 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
541 MmIsPageInUse(PFN_TYPE Pfn
)
543 return MiIsPfnInUse(MiGetPfnEntry(Pfn
));
548 MiSetConsumer(IN PFN_TYPE Pfn
,
551 MiGetPfnEntry(Pfn
)->u3
.e1
.PageLocation
= ActiveAndValid
;
556 MmDereferencePage(PFN_TYPE Pfn
)
560 DPRINT("MmDereferencePage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
562 Page
= MiGetPfnEntry(Pfn
);
565 Page
->u3
.e2
.ReferenceCount
--;
566 if (Page
->u3
.e2
.ReferenceCount
== 0)
569 Page
->u3
.e1
.PageLocation
= FreePageList
;
570 MiInsertInListTail(&MmFreePageListHead
, Page
);
571 if (MmFreePageListHead
.Total
> 8 && 0 == KeReadStateEvent(&ZeroPageThreadEvent
))
573 KeSetEvent(&ZeroPageThreadEvent
, IO_NO_INCREMENT
, FALSE
);
580 MmAllocPage(ULONG Type
)
583 PPHYSICAL_PAGE PageDescriptor
;
584 BOOLEAN NeedClear
= FALSE
;
586 DPRINT("MmAllocPage()\n");
588 if (MmZeroedPageListHead
.Total
== 0)
590 if (MmFreePageListHead
.Total
== 0)
592 /* Check if this allocation is for the PFN DB itself */
593 if (MmNumberOfPhysicalPages
== 0)
598 DPRINT1("MmAllocPage(): Out of memory\n");
601 PageDescriptor
= MiRemoveHeadList(&MmFreePageListHead
);
607 PageDescriptor
= MiRemoveHeadList(&MmZeroedPageListHead
);
610 PageDescriptor
->u3
.e2
.ReferenceCount
= 1;
614 PfnOffset
= MiGetPfnEntryIndex(PageDescriptor
);
615 if ((NeedClear
) && (Type
!= MC_SYSTEM
))
617 MiZeroPage(PfnOffset
);
620 PageDescriptor
->u3
.e1
.PageLocation
= ActiveAndValid
;
626 MiZeroPage(PFN_TYPE Page
)
631 Irql
= KeRaiseIrqlToDpcLevel();
632 TempAddress
= MiMapPageToZeroInHyperSpace(Page
);
633 if (TempAddress
== NULL
)
635 return(STATUS_NO_MEMORY
);
637 memset(TempAddress
, 0, PAGE_SIZE
);
638 MiUnmapPagesInZeroSpace(TempAddress
, 1);
640 return(STATUS_SUCCESS
);
645 MmZeroPageThreadMain(PVOID Ignored
)
649 PPHYSICAL_PAGE PageDescriptor
;
653 /* Free initial kernel memory */
654 //MiFreeInitMemory();
656 /* Set our priority to 0 */
657 KeGetCurrentThread()->BasePriority
= 0;
658 KeSetPriorityThread(KeGetCurrentThread(), 0);
662 Status
= KeWaitForSingleObject(&ZeroPageThreadEvent
,
668 if (ZeroPageThreadShouldTerminate
)
670 DPRINT1("ZeroPageThread: Terminating\n");
671 return STATUS_SUCCESS
;
674 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
675 while (MmFreePageListHead
.Total
)
677 PageDescriptor
= MiRemoveHeadList(&MmFreePageListHead
);
678 /* We set the page to used, because MmCreateVirtualMapping failed with unused pages */
679 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
680 Pfn
= MiGetPfnEntryIndex(PageDescriptor
);
681 Status
= MiZeroPage(Pfn
);
683 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
684 if (NT_SUCCESS(Status
))
686 MiInsertZeroListAtBack(Pfn
);
691 MiInsertInListTail(&MmFreePageListHead
, PageDescriptor
);
692 PageDescriptor
->u3
.e1
.PageLocation
= FreePageList
;
696 DPRINT("Zeroed %d pages.\n", Count
);
697 KeResetEvent(&ZeroPageThreadEvent
);
698 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
701 return STATUS_SUCCESS
;