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_NUMBER Pfn
)
99 /* Set the page as a user page */
100 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
101 RtlSetBit(&MiUserPfnBitMap
, Pfn
);
102 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
107 MmGetLRUNextUserPage(PFN_NUMBER 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_NUMBER 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(Pfn1
->u3
.e2
.ReferenceCount
== 0);
261 // Allocate it and mark it
263 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
264 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
265 Pfn1
->u3
.e2
.ReferenceCount
= 1;
268 // Decrease available pages
273 // Save it into the MDL
275 *MdlPage
++ = MiGetPfnEntryIndex(Pfn1
);
282 // You want specific range of pages. We'll do this in two runs
284 for (LookForZeroedPages
= 1; LookForZeroedPages
>= 0; LookForZeroedPages
--)
287 // Scan the range you specified
289 for (Page
= LowPage
; Page
< HighPage
; Page
++)
292 // Get the PFN entry for this page
294 Pfn1
= MiGetPfnEntry(Page
);
298 // Make sure it's free and if this is our first pass, zeroed
300 if (MiIsPfnInUse(Pfn1
)) continue;
301 if ((Pfn1
->u3
.e1
.PageLocation
== ZeroedPageList
) != LookForZeroedPages
) continue;
306 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
309 // Now setup the page and mark it
311 Pfn1
->u3
.e2
.ReferenceCount
= 1;
312 Pfn1
->u3
.e1
.StartOfAllocation
= 1;
313 Pfn1
->u3
.e1
.EndOfAllocation
= 1;
316 // Decrease available pages
321 // Save this page into the MDL
324 if (++PagesFound
== PageCount
) break;
328 // If the first pass was enough, don't keep going, otherwise, go again
330 if (PagesFound
== PageCount
) break;
335 // Now release the PFN count
337 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
340 // We might've found less pages, but not more ;-)
342 if (PagesFound
!= PageCount
) ASSERT(PagesFound
< PageCount
);
346 // If we didn' tfind any pages at all, fail
348 DPRINT1("NO MDL PAGES!\n");
354 // Write out how many pages we found
356 Mdl
->ByteCount
= (ULONG
)(PagesFound
<< PAGE_SHIFT
);
359 // Terminate the MDL array if there's certain missing pages
361 if (PagesFound
!= PageCount
) *MdlPage
= -1;
364 // Now go back and loop over all the MDL pages
366 MdlPage
= (PPFN_NUMBER
)(Mdl
+ 1);
367 LastMdlPage
= MdlPage
+ PagesFound
;
368 while (MdlPage
< LastMdlPage
)
371 // Check if we've reached the end
374 if (Page
== (PFN_NUMBER
)-1) break;
377 // Get the PFN entry for the page and check if we should zero it out
379 Pfn1
= MiGetPfnEntry(Page
);
381 if (Pfn1
->u3
.e1
.PageLocation
!= ZeroedPageList
) MiZeroPage(Page
);
382 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
386 // We're done, mark the pages as locked (should we lock them, though???)
389 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
395 MmDumpPfnDatabase(VOID
)
399 PCHAR State
= "????", Type
= "Unknown";
401 ULONG Totals
[5] = {0}, FreePages
= 0;
403 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
406 // Loop the PFN database
408 for (i
= 0; i
<= MmHighestPhysicalPage
; i
++)
410 Pfn1
= MiGetPfnEntry(i
);
416 if (MiIsPfnInUse(Pfn1
))
429 // Pretty-print the page
431 DbgPrint("0x%08p:\t%04s\t%20s\t(%02d) [%08p])\n",
435 Pfn1
->u3
.e2
.ReferenceCount
,
439 DbgPrint("Nonpaged Pool: %d pages\t[%d KB]\n", Totals
[MC_NPPOOL
], (Totals
[MC_NPPOOL
] << PAGE_SHIFT
) / 1024);
440 DbgPrint("Paged Pool: %d pages\t[%d KB]\n", Totals
[MC_PPOOL
], (Totals
[MC_PPOOL
] << PAGE_SHIFT
) / 1024);
441 DbgPrint("File System Cache: %d pages\t[%d KB]\n", Totals
[MC_CACHE
], (Totals
[MC_CACHE
] << PAGE_SHIFT
) / 1024);
442 DbgPrint("Process Working Set: %d pages\t[%d KB]\n", Totals
[MC_USER
], (Totals
[MC_USER
] << PAGE_SHIFT
) / 1024);
443 DbgPrint("System: %d pages\t[%d KB]\n", Totals
[MC_SYSTEM
], (Totals
[MC_SYSTEM
] << PAGE_SHIFT
) / 1024);
444 DbgPrint("Free: %d pages\t[%d KB]\n", FreePages
, (FreePages
<< PAGE_SHIFT
) / 1024);
446 KeLowerIrql(OldIrql
);
451 MmSetRmapListHeadPage(PFN_NUMBER Pfn
, struct _MM_RMAP_ENTRY
* ListHead
)
455 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
456 MiGetPfnEntry(Pfn
)->RmapListHead
= (LONG
)ListHead
;
457 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
460 struct _MM_RMAP_ENTRY
*
462 MmGetRmapListHeadPage(PFN_NUMBER Pfn
)
465 struct _MM_RMAP_ENTRY
* ListHead
;
467 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
468 ListHead
= (struct _MM_RMAP_ENTRY
*)MiGetPfnEntry(Pfn
)->RmapListHead
;
469 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
476 MmSetSavedSwapEntryPage(PFN_NUMBER Pfn
, SWAPENTRY SwapEntry
)
480 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
481 MiGetPfnEntry(Pfn
)->u1
.WsIndex
= SwapEntry
;
482 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
487 MmGetSavedSwapEntryPage(PFN_NUMBER Pfn
)
492 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
493 SwapEntry
= MiGetPfnEntry(Pfn
)->u1
.WsIndex
;
494 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
501 MmReferencePage(PFN_NUMBER Pfn
)
505 DPRINT("MmReferencePage(PysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
507 if (Pfn
== 0 || Pfn
> MmHighestPhysicalPage
)
512 Page
= MiGetPfnEntry(Pfn
);
515 Page
->u3
.e2
.ReferenceCount
++;
520 MmGetReferenceCountPage(PFN_NUMBER Pfn
)
526 DPRINT("MmGetReferenceCountPage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
528 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
529 Page
= MiGetPfnEntry(Pfn
);
532 RCount
= Page
->u3
.e2
.ReferenceCount
;
534 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
540 MmIsPageInUse(PFN_NUMBER Pfn
)
542 return MiIsPfnInUse(MiGetPfnEntry(Pfn
));
547 MiSetConsumer(IN PFN_NUMBER Pfn
,
550 MiGetPfnEntry(Pfn
)->u3
.e1
.PageLocation
= ActiveAndValid
;
555 MmDereferencePage(PFN_NUMBER Pfn
)
559 DPRINT("MmDereferencePage(PhysicalAddress %x)\n", Pfn
<< PAGE_SHIFT
);
561 Page
= MiGetPfnEntry(Pfn
);
564 Page
->u3
.e2
.ReferenceCount
--;
565 if (Page
->u3
.e2
.ReferenceCount
== 0)
568 Page
->u3
.e1
.PageLocation
= FreePageList
;
569 MiInsertInListTail(&MmFreePageListHead
, Page
);
570 if (MmFreePageListHead
.Total
> 8 && 0 == KeReadStateEvent(&ZeroPageThreadEvent
))
572 KeSetEvent(&ZeroPageThreadEvent
, IO_NO_INCREMENT
, FALSE
);
579 MmAllocPage(ULONG Type
)
581 PFN_NUMBER PfnOffset
;
582 PPHYSICAL_PAGE PageDescriptor
;
583 BOOLEAN NeedClear
= FALSE
;
585 DPRINT("MmAllocPage()\n");
587 if (MmZeroedPageListHead
.Total
== 0)
589 if (MmFreePageListHead
.Total
== 0)
591 /* Check if this allocation is for the PFN DB itself */
592 if (MmNumberOfPhysicalPages
== 0)
597 DPRINT1("MmAllocPage(): Out of memory\n");
600 PageDescriptor
= MiRemoveHeadList(&MmFreePageListHead
);
606 PageDescriptor
= MiRemoveHeadList(&MmZeroedPageListHead
);
609 PageDescriptor
->u3
.e2
.ReferenceCount
= 1;
613 PfnOffset
= MiGetPfnEntryIndex(PageDescriptor
);
614 if ((NeedClear
) && (Type
!= MC_SYSTEM
))
616 MiZeroPage(PfnOffset
);
619 PageDescriptor
->u3
.e1
.PageLocation
= ActiveAndValid
;
625 MiZeroPage(PFN_NUMBER Page
)
630 Irql
= KeRaiseIrqlToDpcLevel();
631 TempAddress
= MiMapPageToZeroInHyperSpace(Page
);
632 if (TempAddress
== NULL
)
634 return(STATUS_NO_MEMORY
);
636 memset(TempAddress
, 0, PAGE_SIZE
);
637 MiUnmapPagesInZeroSpace(TempAddress
, 1);
639 return(STATUS_SUCCESS
);
644 MmZeroPageThreadMain(PVOID Ignored
)
648 PPHYSICAL_PAGE PageDescriptor
;
652 /* Free initial kernel memory */
653 //MiFreeInitMemory();
655 /* Set our priority to 0 */
656 KeGetCurrentThread()->BasePriority
= 0;
657 KeSetPriorityThread(KeGetCurrentThread(), 0);
661 Status
= KeWaitForSingleObject(&ZeroPageThreadEvent
,
667 if (ZeroPageThreadShouldTerminate
)
669 DPRINT1("ZeroPageThread: Terminating\n");
670 return STATUS_SUCCESS
;
673 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
674 while (MmFreePageListHead
.Total
)
676 PageDescriptor
= MiRemoveHeadList(&MmFreePageListHead
);
677 /* We set the page to used, because MmCreateVirtualMapping failed with unused pages */
678 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
679 Pfn
= MiGetPfnEntryIndex(PageDescriptor
);
680 Status
= MiZeroPage(Pfn
);
682 oldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
683 if (NT_SUCCESS(Status
))
685 MiInsertZeroListAtBack(Pfn
);
690 MiInsertInListTail(&MmFreePageListHead
, PageDescriptor
);
691 PageDescriptor
->u3
.e1
.PageLocation
= FreePageList
;
695 DPRINT("Zeroed %d pages.\n", Count
);
696 KeResetEvent(&ZeroPageThreadEvent
);
697 KeReleaseQueuedSpinLock(LockQueuePfnLock
, oldIrql
);
700 return STATUS_SUCCESS
;