2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/pfnlist.c
5 * PURPOSE: ARM Memory Manager PFN List Manipulation
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 #line 15 "ARMĀ³::PFNLIST"
16 #define MODULE_INVOLVED_IN_ARM3
17 #include "../ARM3/miarm.h"
20 #define ASSERT_LIST_INVARIANT(x) \
22 ASSERT(((x)->Total == 0 && \
23 (x)->Flink == LIST_HEAD && \
24 (x)->Blink == LIST_HEAD) || \
26 (x)->Flink != LIST_HEAD && \
27 (x)->Blink != LIST_HEAD)); \
30 #define ASSERT_LIST_INVARIANT(x)
33 /* GLOBALS ********************************************************************/
37 ULONG MmSystemPageColor
;
39 MMPFNLIST MmZeroedPageListHead
= {0, ZeroedPageList
, LIST_HEAD
, LIST_HEAD
};
40 MMPFNLIST MmFreePageListHead
= {0, FreePageList
, LIST_HEAD
, LIST_HEAD
};
41 MMPFNLIST MmStandbyPageListHead
= {0, StandbyPageList
, LIST_HEAD
, LIST_HEAD
};
42 MMPFNLIST MmModifiedPageListHead
= {0, ModifiedPageList
, LIST_HEAD
, LIST_HEAD
};
43 MMPFNLIST MmModifiedNoWritePageListHead
= {0, ModifiedNoWritePageList
, LIST_HEAD
, LIST_HEAD
};
44 MMPFNLIST MmBadPageListHead
= {0, BadPageList
, LIST_HEAD
, LIST_HEAD
};
45 MMPFNLIST MmRomPageListHead
= {0, StandbyPageList
, LIST_HEAD
, LIST_HEAD
};
47 PMMPFNLIST MmPageLocationList
[] =
49 &MmZeroedPageListHead
,
51 &MmStandbyPageListHead
,
52 &MmModifiedPageListHead
,
53 &MmModifiedNoWritePageListHead
,
59 ULONG MI_PFN_CURRENT_USAGE
;
60 CHAR MI_PFN_CURRENT_PROCESS_NAME
[16] = "None yet";
62 /* FUNCTIONS ******************************************************************/
66 MiZeroPhysicalPage(IN PFN_NUMBER PageFrameIndex
)
70 PEPROCESS Process
= PsGetCurrentProcess();
72 /* Map in hyperspace, then wipe it using XMMI or MEMSET */
73 VirtualAddress
= MiMapPageInHyperSpace(Process
, PageFrameIndex
, &OldIrql
);
74 ASSERT(VirtualAddress
);
75 KeZeroPages(VirtualAddress
, PAGE_SIZE
);
76 MiUnmapPageInHyperSpace(Process
, VirtualAddress
, OldIrql
);
81 MiUnlinkFreeOrZeroedPage(IN PMMPFN Entry
)
83 PFN_NUMBER OldFlink
, OldBlink
;
87 PMMCOLOR_TABLES ColorTable
;
90 /* Make sure the PFN lock is held */
91 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
93 /* Make sure the PFN entry isn't in-use */
94 ASSERT(Entry
->u3
.e1
.WriteInProgress
== 0);
95 ASSERT(Entry
->u3
.e1
.ReadInProgress
== 0);
97 /* Find the list for this entry, make sure it's the free or zero list */
98 ListHead
= MmPageLocationList
[Entry
->u3
.e1
.PageLocation
];
99 ListName
= ListHead
->ListName
;
100 ASSERT(ListHead
!= NULL
);
101 ASSERT(ListName
<= FreePageList
);
102 ASSERT_LIST_INVARIANT(ListHead
);
104 /* Remove one count */
105 ASSERT(ListHead
->Total
!= 0);
108 /* Get the forward and back pointers */
109 OldFlink
= Entry
->u1
.Flink
;
110 OldBlink
= Entry
->u2
.Blink
;
112 /* Check if the next entry is the list head */
113 if (OldFlink
!= LIST_HEAD
)
115 /* It is not, so set the backlink of the actual entry, to our backlink */
116 MI_PFN_ELEMENT(OldFlink
)->u2
.Blink
= OldBlink
;
120 /* Set the list head's backlink instead */
121 ListHead
->Blink
= OldBlink
;
124 /* Check if the back entry is the list head */
125 if (OldBlink
!= LIST_HEAD
)
127 /* It is not, so set the backlink of the actual entry, to our backlink */
128 MI_PFN_ELEMENT(OldBlink
)->u1
.Flink
= OldFlink
;
132 /* Set the list head's backlink instead */
133 ListHead
->Flink
= OldFlink
;
136 /* Get the page color */
137 OldBlink
= MiGetPfnEntryIndex(Entry
);
138 Color
= OldBlink
& MmSecondaryColorMask
;
140 /* Get the first page on the color list */
141 ColorTable
= &MmFreePagesByColor
[ListName
][Color
];
143 /* Check if this was was actually the head */
144 OldFlink
= ColorTable
->Flink
;
145 if (OldFlink
== OldBlink
)
147 /* Make the table point to the next page this page was linking to */
148 ColorTable
->Flink
= Entry
->OriginalPte
.u
.Long
;
149 if (ColorTable
->Flink
!= LIST_HEAD
)
151 /* And make the previous link point to the head now */
152 MI_PFN_ELEMENT(ColorTable
->Flink
)->u4
.PteFrame
= COLORED_LIST_HEAD
;
156 /* And if that page was the head, loop the list back around */
157 ColorTable
->Blink
= (PVOID
)LIST_HEAD
;
162 /* This page shouldn't be pointing back to the head */
163 ASSERT(Entry
->u4
.PteFrame
!= COLORED_LIST_HEAD
);
165 /* Make the back link point to whoever the next page is */
166 Pfn1
= MI_PFN_ELEMENT(Entry
->u4
.PteFrame
);
167 Pfn1
->OriginalPte
.u
.Long
= Entry
->OriginalPte
.u
.Long
;
169 /* Check if this page was pointing to the head */
170 if (Entry
->OriginalPte
.u
.Long
!= LIST_HEAD
)
172 /* Make the back link point to the head */
173 Pfn1
= MI_PFN_ELEMENT(Entry
->OriginalPte
.u
.Long
);
174 Pfn1
->u4
.PteFrame
= Entry
->u4
.PteFrame
;
178 /* Then the table is directly back pointing to this page now */
179 ColorTable
->Blink
= Pfn1
;
183 /* One less colored page */
184 ASSERT(ColorTable
->Count
>= 1);
188 Entry
->OriginalPte
.u
.Long
= 0;
190 /* We are not on a list anymore */
191 Entry
->u1
.Flink
= Entry
->u2
.Blink
= 0;
192 ASSERT_LIST_INVARIANT(ListHead
);
194 /* See if we hit any thresholds */
195 if (MmAvailablePages
== MmHighMemoryThreshold
)
197 /* Clear the high memory event */
198 KeClearEvent(MiHighMemoryEvent
);
200 else if (MmAvailablePages
== MmLowMemoryThreshold
)
202 /* Signal the low memory event */
203 KeSetEvent(MiLowMemoryEvent
, 0, FALSE
);
207 if (--MmAvailablePages
< MmMinimumFreePages
)
209 /* FIXME: Should wake up the MPW and working set manager, if we had one */
213 ASSERT(MI_PFN_CURRENT_USAGE
!= MI_USAGE_NOT_SET
);
214 Entry
->PfnUsage
= MI_PFN_CURRENT_USAGE
;
215 memcpy(Entry
->ProcessName
, MI_PFN_CURRENT_PROCESS_NAME
, 16);
216 // MI_PFN_CURRENT_USAGE = MI_USAGE_NOT_SET;
217 // memcpy(MI_PFN_CURRENT_PROCESS_NAME, "Not Set", 16);
223 MiRemovePageByColor(IN PFN_NUMBER PageIndex
,
229 PFN_NUMBER OldFlink
, OldBlink
;
230 ULONG OldColor
, OldCache
;
231 PMMCOLOR_TABLES ColorTable
;
233 /* Make sure PFN lock is held */
234 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
235 ASSERT(Color
< MmSecondaryColors
);
237 /* Get the PFN entry */
238 Pfn1
= MI_PFN_ELEMENT(PageIndex
);
239 ASSERT(Pfn1
->u3
.e1
.RemovalRequested
== 0);
240 ASSERT(Pfn1
->u3
.e1
.Rom
== 0);
242 /* Capture data for later */
243 OldColor
= Pfn1
->u3
.e1
.PageColor
;
244 OldCache
= Pfn1
->u3
.e1
.CacheAttribute
;
246 /* Could be either on free or zero list */
247 ListHead
= MmPageLocationList
[Pfn1
->u3
.e1
.PageLocation
];
248 ASSERT_LIST_INVARIANT(ListHead
);
249 ListName
= ListHead
->ListName
;
250 ASSERT(ListName
<= FreePageList
);
255 /* Get the forward and back pointers */
256 OldFlink
= Pfn1
->u1
.Flink
;
257 OldBlink
= Pfn1
->u2
.Blink
;
259 /* Check if the next entry is the list head */
260 if (OldFlink
!= LIST_HEAD
)
262 /* It is not, so set the backlink of the actual entry, to our backlink */
263 MI_PFN_ELEMENT(OldFlink
)->u2
.Blink
= OldBlink
;
267 /* Set the list head's backlink instead */
268 ListHead
->Blink
= OldBlink
;
271 /* Check if the back entry is the list head */
272 if (OldBlink
!= LIST_HEAD
)
274 /* It is not, so set the backlink of the actual entry, to our backlink */
275 MI_PFN_ELEMENT(OldBlink
)->u1
.Flink
= OldFlink
;
279 /* Set the list head's backlink instead */
280 ListHead
->Flink
= OldFlink
;
283 /* We are not on a list anymore */
284 ASSERT_LIST_INVARIANT(ListHead
);
285 Pfn1
->u1
.Flink
= Pfn1
->u2
.Blink
= 0;
287 /* Zero flags but restore color and cache */
288 Pfn1
->u3
.e2
.ShortFlags
= 0;
289 Pfn1
->u3
.e1
.PageColor
= OldColor
;
290 Pfn1
->u3
.e1
.CacheAttribute
= OldCache
;
292 /* Get the first page on the color list */
293 ASSERT(Color
< MmSecondaryColors
);
294 ColorTable
= &MmFreePagesByColor
[ListName
][Color
];
295 ASSERT(ColorTable
->Count
>= 1);
297 /* Set the forward link to whoever we were pointing to */
298 ColorTable
->Flink
= Pfn1
->OriginalPte
.u
.Long
;
300 /* Get the first page on the color list */
301 if (ColorTable
->Flink
== LIST_HEAD
)
303 /* This is the beginning of the list, so set the sentinel value */
304 ColorTable
->Blink
= (PVOID
)LIST_HEAD
;
308 /* The list is empty, so we are the first page */
309 MI_PFN_ELEMENT(ColorTable
->Flink
)->u4
.PteFrame
= COLORED_LIST_HEAD
;
316 Pfn1
->OriginalPte
.u
.Long
= 0;
318 /* See if we hit any thresholds */
319 if (MmAvailablePages
== MmHighMemoryThreshold
)
321 /* Clear the high memory event */
322 KeClearEvent(MiHighMemoryEvent
);
324 else if (MmAvailablePages
== MmLowMemoryThreshold
)
326 /* Signal the low memory event */
327 KeSetEvent(MiLowMemoryEvent
, 0, FALSE
);
331 if (--MmAvailablePages
< MmMinimumFreePages
)
333 /* FIXME: Should wake up the MPW and working set manager, if we had one */
337 //ASSERT(MI_PFN_CURRENT_USAGE != MI_USAGE_NOT_SET);
338 Pfn1
->PfnUsage
= MI_PFN_CURRENT_USAGE
;
339 memcpy(Pfn1
->ProcessName
, MI_PFN_CURRENT_PROCESS_NAME
, 16);
340 //MI_PFN_CURRENT_USAGE = MI_USAGE_NOT_SET;
341 //memcpy(MI_PFN_CURRENT_PROCESS_NAME, "Not Set", 16);
344 /* Return the page */
350 MiRemoveAnyPage(IN ULONG Color
)
352 PFN_NUMBER PageIndex
;
355 /* Make sure PFN lock is held and we have pages */
356 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
357 ASSERT(MmAvailablePages
!= 0);
358 ASSERT(Color
< MmSecondaryColors
);
360 /* Check the colored free list */
361 PageIndex
= MmFreePagesByColor
[FreePageList
][Color
].Flink
;
362 if (PageIndex
== LIST_HEAD
)
364 /* Check the colored zero list */
365 PageIndex
= MmFreePagesByColor
[ZeroedPageList
][Color
].Flink
;
366 if (PageIndex
== LIST_HEAD
)
368 /* Check the free list */
369 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
370 PageIndex
= MmFreePageListHead
.Flink
;
371 Color
= PageIndex
& MmSecondaryColorMask
;
372 if (PageIndex
== LIST_HEAD
)
374 /* Check the zero list */
375 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
376 PageIndex
= MmZeroedPageListHead
.Flink
;
377 Color
= PageIndex
& MmSecondaryColorMask
;
378 ASSERT(PageIndex
!= LIST_HEAD
);
379 if (PageIndex
== LIST_HEAD
)
381 /* FIXME: Should check the standby list */
382 ASSERT(MmZeroedPageListHead
.Total
== 0);
388 /* Remove the page from its list */
389 PageIndex
= MiRemovePageByColor(PageIndex
, Color
);
392 Pfn1
= MI_PFN_ELEMENT(PageIndex
);
393 ASSERT((Pfn1
->u3
.e1
.PageLocation
== FreePageList
) ||
394 (Pfn1
->u3
.e1
.PageLocation
== ZeroedPageList
));
395 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
396 ASSERT(Pfn1
->u2
.ShareCount
== 0);
397 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
398 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
400 /* Return the page */
406 MiRemoveZeroPage(IN ULONG Color
)
408 PFN_NUMBER PageIndex
;
410 BOOLEAN Zero
= FALSE
;
412 /* Make sure PFN lock is held and we have pages */
413 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
414 ASSERT(MmAvailablePages
!= 0);
415 ASSERT(Color
< MmSecondaryColors
);
417 /* Check the colored zero list */
418 PageIndex
= MmFreePagesByColor
[ZeroedPageList
][Color
].Flink
;
419 if (PageIndex
== LIST_HEAD
)
421 /* Check the zero list */
422 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
423 PageIndex
= MmZeroedPageListHead
.Flink
;
424 if (PageIndex
== LIST_HEAD
)
426 /* This means there's no zero pages, we have to look for free ones */
427 ASSERT(MmZeroedPageListHead
.Total
== 0);
430 /* Check the colored free list */
431 PageIndex
= MmFreePagesByColor
[FreePageList
][Color
].Flink
;
432 if (PageIndex
== LIST_HEAD
)
434 /* Check the free list */
435 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
436 PageIndex
= MmFreePageListHead
.Flink
;
437 Color
= PageIndex
& MmSecondaryColorMask
;
438 ASSERT(PageIndex
!= LIST_HEAD
);
439 if (PageIndex
== LIST_HEAD
)
441 /* FIXME: Should check the standby list */
442 ASSERT(MmZeroedPageListHead
.Total
== 0);
448 Color
= PageIndex
& MmSecondaryColorMask
;
453 Pfn1
= MI_PFN_ELEMENT(PageIndex
);
454 ASSERT((Pfn1
->u3
.e1
.PageLocation
== FreePageList
) ||
455 (Pfn1
->u3
.e1
.PageLocation
== ZeroedPageList
));
457 /* Remove the page from its list */
458 PageIndex
= MiRemovePageByColor(PageIndex
, Color
);
459 ASSERT(Pfn1
== MI_PFN_ELEMENT(PageIndex
));
461 /* Zero it, if needed */
462 if (Zero
) MiZeroPhysicalPage(PageIndex
);
465 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
466 ASSERT(Pfn1
->u2
.ShareCount
== 0);
467 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
468 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
470 /* Return the page */
476 MiInsertPageInFreeList(IN PFN_NUMBER PageFrameIndex
)
483 PMMCOLOR_TABLES ColorTable
;
485 /* Make sure the page index is valid */
486 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
487 ASSERT((PageFrameIndex
!= 0) &&
488 (PageFrameIndex
<= MmHighestPhysicalPage
) &&
489 (PageFrameIndex
>= MmLowestPhysicalPage
));
491 /* Get the PFN entry */
492 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
494 /* Sanity checks that a right kind of page is being inserted here */
495 ASSERT(Pfn1
->u4
.MustBeCached
== 0);
496 ASSERT(Pfn1
->u3
.e1
.Rom
!= 1);
497 ASSERT(Pfn1
->u3
.e1
.RemovalRequested
== 0);
498 ASSERT(Pfn1
->u4
.VerifierAllocation
== 0);
499 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
501 /* Get the free page list and increment its count */
502 ListHead
= &MmFreePageListHead
;
503 ASSERT_LIST_INVARIANT(ListHead
);
506 /* Get the last page on the list */
507 LastPage
= ListHead
->Blink
;
508 if (LastPage
!= LIST_HEAD
)
510 /* Link us with the previous page, so we're at the end now */
511 MI_PFN_ELEMENT(LastPage
)->u1
.Flink
= PageFrameIndex
;
515 /* The list is empty, so we are the first page */
516 ListHead
->Flink
= PageFrameIndex
;
519 /* Now make the list head point back to us (since we go at the end) */
520 ListHead
->Blink
= PageFrameIndex
;
521 ASSERT_LIST_INVARIANT(ListHead
);
523 /* And initialize our own list pointers */
524 Pfn1
->u1
.Flink
= LIST_HEAD
;
525 Pfn1
->u2
.Blink
= LastPage
;
527 /* Set the list name and default priority */
528 Pfn1
->u3
.e1
.PageLocation
= FreePageList
;
529 Pfn1
->u4
.Priority
= 3;
531 /* Clear some status fields */
532 Pfn1
->u4
.InPageError
= 0;
533 Pfn1
->u4
.AweAllocation
= 0;
535 /* Increase available pages */
538 /* Check if we've reached the configured low memory threshold */
539 if (MmAvailablePages
== MmLowMemoryThreshold
)
541 /* Clear the event, because now we're ABOVE the threshold */
542 KeClearEvent(MiLowMemoryEvent
);
544 else if (MmAvailablePages
== MmHighMemoryThreshold
)
546 /* Otherwise check if we reached the high threshold and signal the event */
547 KeSetEvent(MiHighMemoryEvent
, 0, FALSE
);
550 /* Get the page color */
551 Color
= PageFrameIndex
& MmSecondaryColorMask
;
553 /* Get the first page on the color list */
554 ColorTable
= &MmFreePagesByColor
[FreePageList
][Color
];
555 if (ColorTable
->Flink
== LIST_HEAD
)
557 /* The list is empty, so we are the first page */
558 Pfn1
->u4
.PteFrame
= COLORED_LIST_HEAD
;
559 ColorTable
->Flink
= PageFrameIndex
;
563 /* Get the previous page */
564 Blink
= (PMMPFN
)ColorTable
->Blink
;
566 /* Make it link to us, and link back to it */
567 Blink
->OriginalPte
.u
.Long
= PageFrameIndex
;
568 Pfn1
->u4
.PteFrame
= MiGetPfnEntryIndex(Blink
);
571 /* Now initialize our own list pointers */
572 ColorTable
->Blink
= Pfn1
;
574 /* This page is now the last */
575 Pfn1
->OriginalPte
.u
.Long
= LIST_HEAD
;
577 /* And increase the count in the colored list */
580 /* Notify zero page thread if enough pages are on the free list now */
581 if ((ListHead
->Total
>= 8) && !(MmZeroingPageThreadActive
))
584 MmZeroingPageThreadActive
= TRUE
;
585 KeSetEvent(&MmZeroingPageEvent
, IO_NO_INCREMENT
, FALSE
);
589 Pfn1
->PfnUsage
= MI_USAGE_FREE_PAGE
;
590 RtlZeroMemory(Pfn1
->ProcessName
, 16);
594 /* Note: This function is hardcoded only for the zeroed page list, for now */
597 MiInsertPageInList(IN PMMPFNLIST ListHead
,
598 IN PFN_NUMBER PageFrameIndex
)
603 PMMCOLOR_TABLES ColorHead
;
606 /* For free pages, use MiInsertPageInFreeList */
607 ASSERT(ListHead
!= &MmFreePageListHead
);
609 /* Make sure the lock is held */
610 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
612 /* Make sure the PFN is valid */
613 ASSERT((PageFrameIndex
) &&
614 (PageFrameIndex
<= MmHighestPhysicalPage
) &&
615 (PageFrameIndex
>= MmLowestPhysicalPage
));
617 /* Page should be unused */
618 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
619 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
620 ASSERT(Pfn1
->u3
.e1
.Rom
!= 1);
622 /* Only used for zero pages in ReactOS */
623 ListName
= ListHead
->ListName
;
624 ASSERT(ListName
== ZeroedPageList
);
627 /* Don't handle bad pages yet yet */
628 ASSERT(Pfn1
->u3
.e1
.RemovalRequested
== 0);
630 /* Make the head of the list point to this page now */
631 Flink
= ListHead
->Flink
;
632 ListHead
->Flink
= PageFrameIndex
;
634 /* Make the page point to the previous head, and back to the list */
635 Pfn1
->u1
.Flink
= Flink
;
636 Pfn1
->u2
.Blink
= LIST_HEAD
;
638 /* Was the list empty? */
639 if (Flink
!= LIST_HEAD
)
641 /* It wasn't, so update the backlink of the previous head page */
642 Pfn2
= MI_PFN_ELEMENT(Flink
);
643 Pfn2
->u2
.Blink
= PageFrameIndex
;
647 /* It was empty, so have it loop back around to this new page */
648 ListHead
->Blink
= PageFrameIndex
;
651 /* Move the page onto its new location */
652 Pfn1
->u3
.e1
.PageLocation
= ListName
;
654 /* One more page on the system */
657 /* Check if we've reached the configured low memory threshold */
658 if (MmAvailablePages
== MmLowMemoryThreshold
)
660 /* Clear the event, because now we're ABOVE the threshold */
661 KeClearEvent(MiLowMemoryEvent
);
663 else if (MmAvailablePages
== MmHighMemoryThreshold
)
665 /* Otherwise check if we reached the high threshold and signal the event */
666 KeSetEvent(MiHighMemoryEvent
, 0, FALSE
);
670 ASSERT(ListName
== ZeroedPageList
);
671 ASSERT(Pfn1
->u4
.InPageError
== 0);
673 /* Get the page color */
674 Color
= PageFrameIndex
& MmSecondaryColorMask
;
676 /* Get the list for this color */
677 ColorHead
= &MmFreePagesByColor
[ZeroedPageList
][Color
];
679 /* Get the old head */
680 Flink
= ColorHead
->Flink
;
682 /* Make this page point back to the list, and point forwards to the old head */
683 Pfn1
->OriginalPte
.u
.Long
= Flink
;
684 Pfn1
->u4
.PteFrame
= COLORED_LIST_HEAD
;
686 /* Set the new head */
687 ColorHead
->Flink
= PageFrameIndex
;
689 /* Was the head empty? */
690 if (Flink
!= LIST_HEAD
)
692 /* No, so make the old head point to this page */
693 Pfn2
= MI_PFN_ELEMENT(Flink
);
694 Pfn2
->u4
.PteFrame
= PageFrameIndex
;
698 /* Yes, make it loop back to this page */
699 ColorHead
->Blink
= (PVOID
)Pfn1
;
702 /* One more paged on the colored list */
706 //ASSERT(MI_PFN_CURRENT_USAGE == MI_USAGE_NOT_SET);
707 Pfn1
->PfnUsage
= MI_USAGE_FREE_PAGE
;
708 MI_PFN_CURRENT_USAGE
= MI_USAGE_NOT_SET
;
709 RtlZeroMemory(Pfn1
->ProcessName
, 16);
715 MiInitializePfn(IN PFN_NUMBER PageFrameIndex
,
716 IN PMMPTE PointerPte
,
721 PMMPTE PointerPtePte
;
722 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
725 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
726 Pfn1
->PteAddress
= PointerPte
;
728 /* Check if this PFN is part of a valid address space */
729 if (PointerPte
->u
.Hard
.Valid
== 1)
731 /* Only valid from MmCreateProcessAddressSpace path */
732 ASSERT(PsGetCurrentProcess()->Vm
.WorkingSetSize
== 0);
734 /* Make this a demand zero PTE */
735 MI_MAKE_SOFTWARE_PTE(&Pfn1
->OriginalPte
, MM_READWRITE
);
739 /* Copy the PTE data */
740 Pfn1
->OriginalPte
= *PointerPte
;
741 ASSERT(!((Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0) &&
742 (Pfn1
->OriginalPte
.u
.Soft
.Transition
== 1)));
745 /* Otherwise this is a fresh page -- set it up */
746 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
747 Pfn1
->u3
.e2
.ReferenceCount
= 1;
748 Pfn1
->u2
.ShareCount
= 1;
749 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
750 ASSERT(Pfn1
->u3
.e1
.Rom
== 0);
751 Pfn1
->u3
.e1
.Modified
= Modified
;
753 /* Get the page table for the PTE */
754 PointerPtePte
= MiAddressToPte(PointerPte
);
755 if (PointerPtePte
->u
.Hard
.Valid
== 0)
757 /* Make sure the PDE gets paged in properly */
758 Status
= MiCheckPdeForPagedPool(PointerPte
);
759 if (!NT_SUCCESS(Status
))
762 KeBugCheckEx(MEMORY_MANAGEMENT
,
764 (ULONG_PTR
)PointerPte
,
765 (ULONG_PTR
)PointerPtePte
->u
.Long
,
766 (ULONG_PTR
)MiPteToAddress(PointerPte
));
770 /* Get the PFN for the page table */
771 PageFrameIndex
= PFN_FROM_PTE(PointerPtePte
);
772 ASSERT(PageFrameIndex
!= 0);
773 Pfn1
->u4
.PteFrame
= PageFrameIndex
;
775 /* Increase its share count so we don't get rid of it */
776 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
777 Pfn1
->u2
.ShareCount
++;
782 MiAllocatePfn(IN PMMPTE PointerPte
,
786 PFN_NUMBER PageFrameIndex
;
789 /* Sanity check that we aren't passed a valid PTE */
790 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
792 /* Make an empty software PTE */
793 MI_MAKE_SOFTWARE_PTE(&TempPte
, MM_READWRITE
);
795 /* Lock the PFN database */
796 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
798 /* Check if we're running low on pages */
799 if (MmAvailablePages
< 128)
801 DPRINT1("Warning, running low on memory: %d pages left\n", MmAvailablePages
);
802 //MiEnsureAvailablePageOrWait(NULL, OldIrql);
806 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
807 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
808 PageFrameIndex
= MiRemoveAnyPage(MI_GET_NEXT_COLOR());
810 /* Write the software PTE */
811 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
812 PointerPte
->u
.Soft
.Protection
|= Protection
;
814 /* Initialize its PFN entry */
815 MiInitializePfn(PageFrameIndex
, PointerPte
, TRUE
);
817 /* Release the PFN lock and return the page */
818 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
819 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
820 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
821 return PageFrameIndex
;
826 MiDecrementShareCount(IN PMMPFN Pfn1
,
827 IN PFN_NUMBER PageFrameIndex
)
829 ASSERT(PageFrameIndex
> 0);
830 ASSERT(MI_PFN_ELEMENT(PageFrameIndex
) != NULL
);
831 ASSERT(Pfn1
== MI_PFN_ELEMENT(PageFrameIndex
));
832 ASSERT(MI_IS_ROS_PFN(Pfn1
) == FALSE
);
834 /* Page must be in-use */
835 if ((Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
) &&
836 (Pfn1
->u3
.e1
.PageLocation
!= StandbyPageList
))
838 /* Otherwise we have PFN corruption */
839 KeBugCheckEx(PFN_LIST_CORRUPT
,
842 Pfn1
->u3
.e1
.PageLocation
,
846 /* Check if the share count is now 0 */
847 ASSERT(Pfn1
->u2
.ShareCount
< 0xF000000);
848 if (!--Pfn1
->u2
.ShareCount
)
850 /* ReactOS does not handle these */
851 ASSERT(Pfn1
->u3
.e1
.PrototypePte
== 0);
853 /* Put the page in transition */
854 Pfn1
->u3
.e1
.PageLocation
= TransitionPage
;
856 /* PFN lock must be held */
857 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
859 /* Page should at least have one reference */
860 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
861 if (Pfn1
->u3
.e2
.ReferenceCount
== 1)
863 /* In ReactOS, this path should always be hit with a deleted PFN */
864 ASSERT(MI_IS_PFN_DELETED(Pfn1
) == TRUE
);
866 /* Clear the last reference */
867 Pfn1
->u3
.e2
.ReferenceCount
= 0;
868 ASSERT(Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0);
870 /* Mark the page temporarily as valid, we're going to make it free soon */
871 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
873 /* Bring it back into the free list */
874 MiInsertPageInFreeList(PageFrameIndex
);
878 /* Otherwise, just drop the reference count */
879 InterlockedDecrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
886 MiDecrementReferenceCount(IN PMMPFN Pfn1
,
887 IN PFN_NUMBER PageFrameIndex
)
889 /* PFN lock must be held */
890 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
892 /* Sanity checks on the page */
893 ASSERT(PageFrameIndex
< MmHighestPhysicalPage
);
894 ASSERT(Pfn1
== MI_PFN_ELEMENT(PageFrameIndex
));
895 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
897 /* Dereference the page, bail out if it's still alive */
898 InterlockedDecrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
899 if (Pfn1
->u3
.e2
.ReferenceCount
) return;
901 /* Nobody should still have reference to this page */
902 if (Pfn1
->u2
.ShareCount
!= 0)
904 /* Otherwise something's really wrong */
905 KeBugCheckEx(PFN_LIST_CORRUPT
, 7, PageFrameIndex
, Pfn1
->u2
.ShareCount
, 0);
908 /* And it should be lying on some page list */
909 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
911 /* Did someone set the delete flag? */
912 if (MI_IS_PFN_DELETED(Pfn1
))
914 /* Insert it into the free list, there's nothing left to do */
915 MiInsertPageInFreeList(PageFrameIndex
);
919 /* We don't have a modified list yet */
920 ASSERT(Pfn1
->u3
.e1
.Modified
== 0);
921 ASSERT(Pfn1
->u3
.e1
.RemovalRequested
== 0);
923 /* FIXME: Normally it would go on the standby list, but we're pushing it on the free list */
924 MiInsertPageInFreeList(PageFrameIndex
);
929 MiInitializePfnForOtherProcess(IN PFN_NUMBER PageFrameIndex
,
930 IN PMMPTE PointerPte
,
931 IN PFN_NUMBER PteFrame
)
936 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
937 Pfn1
->PteAddress
= PointerPte
;
939 /* Make this a software PTE */
940 MI_MAKE_SOFTWARE_PTE(&Pfn1
->OriginalPte
, MM_READWRITE
);
943 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
944 Pfn1
->u3
.e2
.ReferenceCount
= 1;
945 Pfn1
->u2
.ShareCount
= 1;
946 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
947 Pfn1
->u3
.e1
.Modified
= TRUE
;
948 Pfn1
->u4
.InPageError
= FALSE
;
950 /* Did we get a PFN for the page table */
954 Pfn1
->u4
.PteFrame
= PteFrame
;
956 /* Increase its share count so we don't get rid of it */
957 Pfn1
= MI_PFN_ELEMENT(PteFrame
);
958 Pfn1
->u2
.ShareCount
++;