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 #define MODULE_INVOLVED_IN_ARM3
16 #include "../ARM3/miarm.h"
19 #define ASSERT_LIST_INVARIANT(x) \
21 ASSERT(((x)->Total == 0 && \
22 (x)->Flink == LIST_HEAD && \
23 (x)->Blink == LIST_HEAD) || \
25 (x)->Flink != LIST_HEAD && \
26 (x)->Blink != LIST_HEAD)); \
29 #define ASSERT_LIST_INVARIANT(x)
32 /* GLOBALS ********************************************************************/
36 ULONG MmSystemPageColor
;
38 ULONG MmTransitionSharedPages
;
39 ULONG MmTotalPagesForPagingFile
;
41 MMPFNLIST MmZeroedPageListHead
= {0, ZeroedPageList
, LIST_HEAD
, LIST_HEAD
};
42 MMPFNLIST MmFreePageListHead
= {0, FreePageList
, LIST_HEAD
, LIST_HEAD
};
43 MMPFNLIST MmStandbyPageListHead
= {0, StandbyPageList
, LIST_HEAD
, LIST_HEAD
};
44 MMPFNLIST MmStandbyPageListByPriority
[8];
45 MMPFNLIST MmModifiedPageListHead
= {0, ModifiedPageList
, LIST_HEAD
, LIST_HEAD
};
46 MMPFNLIST MmModifiedPageListByColor
[1] = {{0, ModifiedPageList
, LIST_HEAD
, LIST_HEAD
}};
47 MMPFNLIST MmModifiedNoWritePageListHead
= {0, ModifiedNoWritePageList
, LIST_HEAD
, LIST_HEAD
};
48 MMPFNLIST MmBadPageListHead
= {0, BadPageList
, LIST_HEAD
, LIST_HEAD
};
49 MMPFNLIST MmRomPageListHead
= {0, StandbyPageList
, LIST_HEAD
, LIST_HEAD
};
51 PMMPFNLIST MmPageLocationList
[] =
53 &MmZeroedPageListHead
,
55 &MmStandbyPageListHead
,
56 &MmModifiedPageListHead
,
57 &MmModifiedNoWritePageListHead
,
63 ULONG MI_PFN_CURRENT_USAGE
;
64 CHAR MI_PFN_CURRENT_PROCESS_NAME
[16] = "None yet";
66 /* FUNCTIONS ******************************************************************/
70 MiZeroPhysicalPage(IN PFN_NUMBER PageFrameIndex
)
74 PEPROCESS Process
= PsGetCurrentProcess();
76 /* Map in hyperspace, then wipe it using XMMI or MEMSET */
77 VirtualAddress
= MiMapPageInHyperSpace(Process
, PageFrameIndex
, &OldIrql
);
78 ASSERT(VirtualAddress
);
79 KeZeroPages(VirtualAddress
, PAGE_SIZE
);
80 MiUnmapPageInHyperSpace(Process
, VirtualAddress
, OldIrql
);
85 MiUnlinkFreeOrZeroedPage(IN PMMPFN Entry
)
87 PFN_NUMBER OldFlink
, OldBlink
;
91 PMMCOLOR_TABLES ColorTable
;
94 /* Make sure the PFN lock is held */
95 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
97 /* Make sure the PFN entry isn't in-use */
98 ASSERT(Entry
->u3
.e1
.WriteInProgress
== 0);
99 ASSERT(Entry
->u3
.e1
.ReadInProgress
== 0);
101 /* Find the list for this entry, make sure it's the free or zero list */
102 ListHead
= MmPageLocationList
[Entry
->u3
.e1
.PageLocation
];
103 ListName
= ListHead
->ListName
;
104 ASSERT(ListHead
!= NULL
);
105 ASSERT(ListName
<= FreePageList
);
106 ASSERT_LIST_INVARIANT(ListHead
);
108 /* Remove one count */
109 ASSERT(ListHead
->Total
!= 0);
112 /* Get the forward and back pointers */
113 OldFlink
= Entry
->u1
.Flink
;
114 OldBlink
= Entry
->u2
.Blink
;
116 /* Check if the next entry is the list head */
117 if (OldFlink
!= LIST_HEAD
)
119 /* It is not, so set the backlink of the actual entry, to our backlink */
120 MI_PFN_ELEMENT(OldFlink
)->u2
.Blink
= OldBlink
;
124 /* Set the list head's backlink instead */
125 ListHead
->Blink
= OldBlink
;
128 /* Check if the back entry is the list head */
129 if (OldBlink
!= LIST_HEAD
)
131 /* It is not, so set the backlink of the actual entry, to our backlink */
132 MI_PFN_ELEMENT(OldBlink
)->u1
.Flink
= OldFlink
;
136 /* Set the list head's backlink instead */
137 ListHead
->Flink
= OldFlink
;
140 /* Get the page color */
141 OldBlink
= MiGetPfnEntryIndex(Entry
);
142 Color
= OldBlink
& MmSecondaryColorMask
;
144 /* Get the first page on the color list */
145 ColorTable
= &MmFreePagesByColor
[ListName
][Color
];
147 /* Check if this was was actually the head */
148 OldFlink
= ColorTable
->Flink
;
149 if (OldFlink
== OldBlink
)
151 /* Make the table point to the next page this page was linking to */
152 ColorTable
->Flink
= Entry
->OriginalPte
.u
.Long
;
153 if (ColorTable
->Flink
!= LIST_HEAD
)
155 /* And make the previous link point to the head now */
156 MI_PFN_ELEMENT(ColorTable
->Flink
)->u4
.PteFrame
= COLORED_LIST_HEAD
;
160 /* And if that page was the head, loop the list back around */
161 ColorTable
->Blink
= (PVOID
)LIST_HEAD
;
166 /* This page shouldn't be pointing back to the head */
167 ASSERT(Entry
->u4
.PteFrame
!= COLORED_LIST_HEAD
);
169 /* Make the back link point to whoever the next page is */
170 Pfn1
= MI_PFN_ELEMENT(Entry
->u4
.PteFrame
);
171 Pfn1
->OriginalPte
.u
.Long
= Entry
->OriginalPte
.u
.Long
;
173 /* Check if this page was pointing to the head */
174 if (Entry
->OriginalPte
.u
.Long
!= LIST_HEAD
)
176 /* Make the back link point to the head */
177 Pfn1
= MI_PFN_ELEMENT(Entry
->OriginalPte
.u
.Long
);
178 Pfn1
->u4
.PteFrame
= Entry
->u4
.PteFrame
;
182 /* Then the table is directly back pointing to this page now */
183 ColorTable
->Blink
= Pfn1
;
187 /* One less colored page */
188 ASSERT(ColorTable
->Count
>= 1);
192 Entry
->OriginalPte
.u
.Long
= 0;
194 /* We are not on a list anymore */
195 Entry
->u1
.Flink
= Entry
->u2
.Blink
= 0;
196 ASSERT_LIST_INVARIANT(ListHead
);
198 /* See if we hit any thresholds */
199 if (MmAvailablePages
== MmHighMemoryThreshold
)
201 /* Clear the high memory event */
202 KeClearEvent(MiHighMemoryEvent
);
204 else if (MmAvailablePages
== MmLowMemoryThreshold
)
206 /* Signal the low memory event */
207 KeSetEvent(MiLowMemoryEvent
, 0, FALSE
);
211 if (--MmAvailablePages
< MmMinimumFreePages
)
213 /* FIXME: Should wake up the MPW and working set manager, if we had one */
215 DPRINT1("Running low on pages: %lu remaining\n", MmAvailablePages
);
217 /* Call RosMm and see if it can release any pages for us */
218 MmRebalanceMemoryConsumers();
222 ASSERT(MI_PFN_CURRENT_USAGE
!= MI_USAGE_NOT_SET
);
223 Entry
->PfnUsage
= MI_PFN_CURRENT_USAGE
;
224 memcpy(Entry
->ProcessName
, MI_PFN_CURRENT_PROCESS_NAME
, 16);
225 // MI_PFN_CURRENT_USAGE = MI_USAGE_NOT_SET;
226 // memcpy(MI_PFN_CURRENT_PROCESS_NAME, "Not Set", 16);
232 MiUnlinkPageFromList(IN PMMPFN Pfn
)
235 PFN_NUMBER OldFlink
, OldBlink
;
237 /* Make sure the PFN lock is held */
238 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
240 /* ARM3 should only call this for dead pages */
241 ASSERT(Pfn
->u3
.e2
.ReferenceCount
== 0);
243 /* Transition pages are supposed to be standby/modified/nowrite */
244 ListHead
= MmPageLocationList
[Pfn
->u3
.e1
.PageLocation
];
245 ASSERT(ListHead
->ListName
>= StandbyPageList
);
247 /* Check if this was standby, or modified */
248 if (ListHead
== &MmStandbyPageListHead
)
250 /* Should not be a ROM page */
251 ASSERT(Pfn
->u3
.e1
.Rom
== 0);
253 /* Get the exact list */
254 ListHead
= &MmStandbyPageListByPriority
[Pfn
->u4
.Priority
];
256 /* See if we hit any thresholds */
257 if (MmAvailablePages
== MmHighMemoryThreshold
)
259 /* Clear the high memory event */
260 KeClearEvent(MiHighMemoryEvent
);
262 else if (MmAvailablePages
== MmLowMemoryThreshold
)
264 /* Signal the low memory event */
265 KeSetEvent(MiLowMemoryEvent
, 0, FALSE
);
268 /* Decrease transition page counter */
269 ASSERT(Pfn
->u3
.e1
.PrototypePte
== 1); /* Only supported ARM3 case */
270 MmTransitionSharedPages
--;
273 if (--MmAvailablePages
< MmMinimumFreePages
)
275 /* FIXME: Should wake up the MPW and working set manager, if we had one */
276 DPRINT1("Running low on pages: %lu remaining\n", MmAvailablePages
);
278 /* Call RosMm and see if it can release any pages for us */
279 MmRebalanceMemoryConsumers();
282 else if (ListHead
== &MmModifiedPageListHead
)
284 /* Only shared memory (page-file backed) modified pages are supported */
285 ASSERT(Pfn
->OriginalPte
.u
.Soft
.Prototype
== 0);
287 /* Decrement the counters */
289 MmTotalPagesForPagingFile
--;
291 /* Pick the correct colored list */
292 ListHead
= &MmModifiedPageListByColor
[0];
294 /* Decrease transition page counter */
295 MmTransitionSharedPages
--;
297 else if (ListHead
== &MmModifiedNoWritePageListHead
)
299 /* List not yet supported */
303 /* Nothing should be in progress and the list should not be empty */
304 ASSERT(Pfn
->u3
.e1
.WriteInProgress
== 0);
305 ASSERT(Pfn
->u3
.e1
.ReadInProgress
== 0);
306 ASSERT(ListHead
->Total
!= 0);
308 /* Get the forward and back pointers */
309 OldFlink
= Pfn
->u1
.Flink
;
310 OldBlink
= Pfn
->u2
.Blink
;
312 /* Check if the next entry is the list head */
313 if (OldFlink
!= LIST_HEAD
)
315 /* It is not, so set the backlink of the actual entry, to our backlink */
316 MI_PFN_ELEMENT(OldFlink
)->u2
.Blink
= OldBlink
;
320 /* Set the list head's backlink instead */
321 ListHead
->Blink
= OldBlink
;
324 /* Check if the back entry is the list head */
325 if (OldBlink
!= LIST_HEAD
)
327 /* It is not, so set the backlink of the actual entry, to our backlink */
328 MI_PFN_ELEMENT(OldBlink
)->u1
.Flink
= OldFlink
;
332 /* Set the list head's backlink instead */
333 ListHead
->Flink
= OldFlink
;
337 Pfn
->OriginalPte
.u
.Long
= 0;
339 /* We are not on a list anymore */
340 Pfn
->u1
.Flink
= Pfn
->u2
.Blink
= 0;
341 ASSERT_LIST_INVARIANT(ListHead
);
343 /* Remove one entry from the list */
349 MiRemovePageByColor(IN PFN_NUMBER PageIndex
,
355 PFN_NUMBER OldFlink
, OldBlink
;
356 USHORT OldColor
, OldCache
;
357 PMMCOLOR_TABLES ColorTable
;
359 /* Make sure PFN lock is held */
360 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
361 ASSERT(Color
< MmSecondaryColors
);
363 /* Get the PFN entry */
364 Pfn1
= MI_PFN_ELEMENT(PageIndex
);
365 ASSERT(Pfn1
->u3
.e1
.RemovalRequested
== 0);
366 ASSERT(Pfn1
->u3
.e1
.Rom
== 0);
368 /* Capture data for later */
369 OldColor
= Pfn1
->u3
.e1
.PageColor
;
370 OldCache
= Pfn1
->u3
.e1
.CacheAttribute
;
372 /* Could be either on free or zero list */
373 ListHead
= MmPageLocationList
[Pfn1
->u3
.e1
.PageLocation
];
374 ASSERT_LIST_INVARIANT(ListHead
);
375 ListName
= ListHead
->ListName
;
376 ASSERT(ListName
<= FreePageList
);
381 /* Get the forward and back pointers */
382 OldFlink
= Pfn1
->u1
.Flink
;
383 OldBlink
= Pfn1
->u2
.Blink
;
385 /* Check if the next entry is the list head */
386 if (OldFlink
!= LIST_HEAD
)
388 /* It is not, so set the backlink of the actual entry, to our backlink */
389 MI_PFN_ELEMENT(OldFlink
)->u2
.Blink
= OldBlink
;
393 /* Set the list head's backlink instead */
394 ListHead
->Blink
= OldBlink
;
397 /* Check if the back entry is the list head */
398 if (OldBlink
!= LIST_HEAD
)
400 /* It is not, so set the backlink of the actual entry, to our backlink */
401 MI_PFN_ELEMENT(OldBlink
)->u1
.Flink
= OldFlink
;
405 /* Set the list head's backlink instead */
406 ListHead
->Flink
= OldFlink
;
409 /* We are not on a list anymore */
410 ASSERT_LIST_INVARIANT(ListHead
);
411 Pfn1
->u1
.Flink
= Pfn1
->u2
.Blink
= 0;
413 /* Zero flags but restore color and cache */
414 Pfn1
->u3
.e2
.ShortFlags
= 0;
415 Pfn1
->u3
.e1
.PageColor
= OldColor
;
416 Pfn1
->u3
.e1
.CacheAttribute
= OldCache
;
418 /* Get the first page on the color list */
419 ASSERT(Color
< MmSecondaryColors
);
420 ColorTable
= &MmFreePagesByColor
[ListName
][Color
];
421 ASSERT(ColorTable
->Count
>= 1);
423 /* Set the forward link to whoever we were pointing to */
424 ColorTable
->Flink
= Pfn1
->OriginalPte
.u
.Long
;
426 /* Get the first page on the color list */
427 if (ColorTable
->Flink
== LIST_HEAD
)
429 /* This is the beginning of the list, so set the sentinel value */
430 ColorTable
->Blink
= (PVOID
)LIST_HEAD
;
434 /* The list is empty, so we are the first page */
435 MI_PFN_ELEMENT(ColorTable
->Flink
)->u4
.PteFrame
= COLORED_LIST_HEAD
;
442 Pfn1
->OriginalPte
.u
.Long
= 0;
444 /* See if we hit any thresholds */
445 if (MmAvailablePages
== MmHighMemoryThreshold
)
447 /* Clear the high memory event */
448 KeClearEvent(MiHighMemoryEvent
);
450 else if (MmAvailablePages
== MmLowMemoryThreshold
)
452 /* Signal the low memory event */
453 KeSetEvent(MiLowMemoryEvent
, 0, FALSE
);
457 if (--MmAvailablePages
< MmMinimumFreePages
)
459 /* FIXME: Should wake up the MPW and working set manager, if we had one */
461 DPRINT1("Running low on pages: %lu remaining\n", MmAvailablePages
);
463 /* Call RosMm and see if it can release any pages for us */
464 MmRebalanceMemoryConsumers();
468 //ASSERT(MI_PFN_CURRENT_USAGE != MI_USAGE_NOT_SET);
469 Pfn1
->PfnUsage
= MI_PFN_CURRENT_USAGE
;
470 memcpy(Pfn1
->ProcessName
, MI_PFN_CURRENT_PROCESS_NAME
, 16);
471 //MI_PFN_CURRENT_USAGE = MI_USAGE_NOT_SET;
472 //memcpy(MI_PFN_CURRENT_PROCESS_NAME, "Not Set", 16);
475 /* Return the page */
481 MiRemoveAnyPage(IN ULONG Color
)
483 PFN_NUMBER PageIndex
;
486 /* Make sure PFN lock is held and we have pages */
487 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
488 ASSERT(MmAvailablePages
!= 0);
489 ASSERT(Color
< MmSecondaryColors
);
491 /* Check the colored free list */
492 PageIndex
= MmFreePagesByColor
[FreePageList
][Color
].Flink
;
493 if (PageIndex
== LIST_HEAD
)
495 /* Check the colored zero list */
496 PageIndex
= MmFreePagesByColor
[ZeroedPageList
][Color
].Flink
;
497 if (PageIndex
== LIST_HEAD
)
499 /* Check the free list */
500 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
501 PageIndex
= MmFreePageListHead
.Flink
;
502 Color
= PageIndex
& MmSecondaryColorMask
;
503 if (PageIndex
== LIST_HEAD
)
505 /* Check the zero list */
506 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
507 PageIndex
= MmZeroedPageListHead
.Flink
;
508 Color
= PageIndex
& MmSecondaryColorMask
;
509 ASSERT(PageIndex
!= LIST_HEAD
);
510 if (PageIndex
== LIST_HEAD
)
512 /* FIXME: Should check the standby list */
513 ASSERT(MmZeroedPageListHead
.Total
== 0);
519 /* Remove the page from its list */
520 PageIndex
= MiRemovePageByColor(PageIndex
, Color
);
523 Pfn1
= MI_PFN_ELEMENT(PageIndex
);
524 ASSERT((Pfn1
->u3
.e1
.PageLocation
== FreePageList
) ||
525 (Pfn1
->u3
.e1
.PageLocation
== ZeroedPageList
));
526 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
527 ASSERT(Pfn1
->u2
.ShareCount
== 0);
528 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
529 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
531 /* Return the page */
537 MiRemoveZeroPage(IN ULONG Color
)
539 PFN_NUMBER PageIndex
;
541 BOOLEAN Zero
= FALSE
;
543 /* Make sure PFN lock is held and we have pages */
544 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
545 ASSERT(MmAvailablePages
!= 0);
546 ASSERT(Color
< MmSecondaryColors
);
548 /* Check the colored zero list */
549 PageIndex
= MmFreePagesByColor
[ZeroedPageList
][Color
].Flink
;
550 if (PageIndex
== LIST_HEAD
)
552 /* Check the zero list */
553 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
554 PageIndex
= MmZeroedPageListHead
.Flink
;
555 if (PageIndex
== LIST_HEAD
)
557 /* This means there's no zero pages, we have to look for free ones */
558 ASSERT(MmZeroedPageListHead
.Total
== 0);
561 /* Check the colored free list */
562 PageIndex
= MmFreePagesByColor
[FreePageList
][Color
].Flink
;
563 if (PageIndex
== LIST_HEAD
)
565 /* Check the free list */
566 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
567 PageIndex
= MmFreePageListHead
.Flink
;
568 Color
= PageIndex
& MmSecondaryColorMask
;
569 ASSERT(PageIndex
!= LIST_HEAD
);
570 if (PageIndex
== LIST_HEAD
)
572 /* FIXME: Should check the standby list */
573 ASSERT(MmZeroedPageListHead
.Total
== 0);
579 Color
= PageIndex
& MmSecondaryColorMask
;
584 Pfn1
= MI_PFN_ELEMENT(PageIndex
);
585 ASSERT((Pfn1
->u3
.e1
.PageLocation
== FreePageList
) ||
586 (Pfn1
->u3
.e1
.PageLocation
== ZeroedPageList
));
588 /* Remove the page from its list */
589 PageIndex
= MiRemovePageByColor(PageIndex
, Color
);
590 ASSERT(Pfn1
== MI_PFN_ELEMENT(PageIndex
));
592 /* Zero it, if needed */
593 if (Zero
) MiZeroPhysicalPage(PageIndex
);
596 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
597 ASSERT(Pfn1
->u2
.ShareCount
== 0);
598 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
599 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
601 /* Return the page */
607 MiInsertPageInFreeList(IN PFN_NUMBER PageFrameIndex
)
614 PMMCOLOR_TABLES ColorTable
;
616 /* Make sure the page index is valid */
617 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
618 ASSERT((PageFrameIndex
!= 0) &&
619 (PageFrameIndex
<= MmHighestPhysicalPage
) &&
620 (PageFrameIndex
>= MmLowestPhysicalPage
));
622 /* Get the PFN entry */
623 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
625 /* Sanity checks that a right kind of page is being inserted here */
626 ASSERT(Pfn1
->u4
.MustBeCached
== 0);
627 ASSERT(Pfn1
->u3
.e1
.Rom
!= 1);
628 ASSERT(Pfn1
->u3
.e1
.RemovalRequested
== 0);
629 ASSERT(Pfn1
->u4
.VerifierAllocation
== 0);
630 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
632 /* Get the free page list and increment its count */
633 ListHead
= &MmFreePageListHead
;
634 ASSERT_LIST_INVARIANT(ListHead
);
637 /* Get the last page on the list */
638 LastPage
= ListHead
->Blink
;
639 if (LastPage
!= LIST_HEAD
)
641 /* Link us with the previous page, so we're at the end now */
642 MI_PFN_ELEMENT(LastPage
)->u1
.Flink
= PageFrameIndex
;
646 /* The list is empty, so we are the first page */
647 ListHead
->Flink
= PageFrameIndex
;
650 /* Now make the list head point back to us (since we go at the end) */
651 ListHead
->Blink
= PageFrameIndex
;
652 ASSERT_LIST_INVARIANT(ListHead
);
654 /* And initialize our own list pointers */
655 Pfn1
->u1
.Flink
= LIST_HEAD
;
656 Pfn1
->u2
.Blink
= LastPage
;
658 /* Set the list name and default priority */
659 Pfn1
->u3
.e1
.PageLocation
= FreePageList
;
660 Pfn1
->u4
.Priority
= 3;
662 /* Clear some status fields */
663 Pfn1
->u4
.InPageError
= 0;
664 Pfn1
->u4
.AweAllocation
= 0;
666 /* Increase available pages */
669 /* Check if we've reached the configured low memory threshold */
670 if (MmAvailablePages
== MmLowMemoryThreshold
)
672 /* Clear the event, because now we're ABOVE the threshold */
673 KeClearEvent(MiLowMemoryEvent
);
675 else if (MmAvailablePages
== MmHighMemoryThreshold
)
677 /* Otherwise check if we reached the high threshold and signal the event */
678 KeSetEvent(MiHighMemoryEvent
, 0, FALSE
);
681 /* Get the page color */
682 Color
= PageFrameIndex
& MmSecondaryColorMask
;
684 /* Get the first page on the color list */
685 ColorTable
= &MmFreePagesByColor
[FreePageList
][Color
];
686 if (ColorTable
->Flink
== LIST_HEAD
)
688 /* The list is empty, so we are the first page */
689 Pfn1
->u4
.PteFrame
= COLORED_LIST_HEAD
;
690 ColorTable
->Flink
= PageFrameIndex
;
694 /* Get the previous page */
695 Blink
= (PMMPFN
)ColorTable
->Blink
;
697 /* Make it link to us, and link back to it */
698 Blink
->OriginalPte
.u
.Long
= PageFrameIndex
;
699 Pfn1
->u4
.PteFrame
= MiGetPfnEntryIndex(Blink
);
702 /* Now initialize our own list pointers */
703 ColorTable
->Blink
= Pfn1
;
705 /* This page is now the last */
706 Pfn1
->OriginalPte
.u
.Long
= LIST_HEAD
;
708 /* And increase the count in the colored list */
711 /* Notify zero page thread if enough pages are on the free list now */
712 if ((ListHead
->Total
>= 8) && !(MmZeroingPageThreadActive
))
715 MmZeroingPageThreadActive
= TRUE
;
716 KeSetEvent(&MmZeroingPageEvent
, IO_NO_INCREMENT
, FALSE
);
720 Pfn1
->PfnUsage
= MI_USAGE_FREE_PAGE
;
721 RtlZeroMemory(Pfn1
->ProcessName
, 16);
727 MiInsertStandbyListAtFront(IN PFN_NUMBER PageFrameIndex
)
733 /* Make sure the lock is held */
734 DPRINT1("Inserting page: %lx into standby list !\n", PageFrameIndex
);
735 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
737 /* Make sure the PFN is valid */
738 ASSERT((PageFrameIndex
!= 0) &&
739 (PageFrameIndex
<= MmHighestPhysicalPage
) &&
740 (PageFrameIndex
>= MmLowestPhysicalPage
));
742 /* Grab the PFN and validate it is the right kind of PFN being inserted */
743 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
744 ASSERT(Pfn1
->u4
.MustBeCached
== 0);
745 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
746 ASSERT(Pfn1
->u3
.e1
.PrototypePte
== 1);
747 ASSERT(Pfn1
->u3
.e1
.Rom
!= 1);
749 /* One more transition page on a list */
750 MmTransitionSharedPages
++;
752 /* Get the standby page list and increment its count */
753 ListHead
= &MmStandbyPageListByPriority
[Pfn1
->u4
.Priority
];
754 ASSERT_LIST_INVARIANT(ListHead
);
757 /* Make the head of the list point to this page now */
758 Flink
= ListHead
->Flink
;
759 ListHead
->Flink
= PageFrameIndex
;
761 /* Make the page point to the previous head, and back to the list */
762 Pfn1
->u1
.Flink
= Flink
;
763 Pfn1
->u2
.Blink
= LIST_HEAD
;
765 /* Was the list empty? */
766 if (Flink
!= LIST_HEAD
)
768 /* It wasn't, so update the backlink of the previous head page */
769 Pfn2
= MI_PFN_ELEMENT(Flink
);
770 Pfn2
->u2
.Blink
= PageFrameIndex
;
774 /* It was empty, so have it loop back around to this new page */
775 ListHead
->Blink
= PageFrameIndex
;
778 /* Move the page onto its new location */
779 Pfn1
->u3
.e1
.PageLocation
= StandbyPageList
;
781 /* One more page on the system */
784 /* Check if we've reached the configured low memory threshold */
785 if (MmAvailablePages
== MmLowMemoryThreshold
)
787 /* Clear the event, because now we're ABOVE the threshold */
788 KeClearEvent(MiLowMemoryEvent
);
790 else if (MmAvailablePages
== MmHighMemoryThreshold
)
792 /* Otherwise check if we reached the high threshold and signal the event */
793 KeSetEvent(MiHighMemoryEvent
, 0, FALSE
);
799 MiInsertPageInList(IN PMMPFNLIST ListHead
,
800 IN PFN_NUMBER PageFrameIndex
)
802 PFN_NUMBER Flink
, LastPage
;
805 PMMCOLOR_TABLES ColorHead
;
808 /* For free pages, use MiInsertPageInFreeList */
809 ASSERT(ListHead
!= &MmFreePageListHead
);
811 /* Make sure the lock is held */
812 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
814 /* Make sure the PFN is valid */
815 ASSERT((PageFrameIndex
) &&
816 (PageFrameIndex
<= MmHighestPhysicalPage
) &&
817 (PageFrameIndex
>= MmLowestPhysicalPage
));
819 /* Page should be unused */
820 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
821 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
822 ASSERT(Pfn1
->u3
.e1
.Rom
!= 1);
824 /* Is a standby or modified page being inserted? */
825 ListName
= ListHead
->ListName
;
826 if ((ListName
== StandbyPageList
) || (ListName
== ModifiedPageList
))
828 /* If the page is in transition, it must also be a prototype page */
829 if ((Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0) &&
830 (Pfn1
->OriginalPte
.u
.Soft
.Transition
== 1))
832 /* Crash the system on inconsistency */
833 KeBugCheckEx(MEMORY_MANAGEMENT
, 0x8888, 0, 0, 0);
837 /* Standby pages are prioritized, so we need to get the real head */
838 if (ListHead
== &MmStandbyPageListHead
)
840 /* Obviously the prioritized list should still have the same name */
841 ListHead
= &MmStandbyPageListByPriority
[Pfn1
->u4
.Priority
];
842 ASSERT(ListHead
->ListName
== ListName
);
845 /* Increment the list count */
848 /* Is a modified page being inserted? */
849 if (ListHead
== &MmModifiedPageListHead
)
851 /* For now, only single-prototype pages should end up in this path */
852 DPRINT("Modified page being added: %lx\n", PageFrameIndex
);
853 ASSERT(Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0);
855 /* Modified pages are colored when they are selected for page file */
856 ListHead
= &MmModifiedPageListByColor
[0];
857 ASSERT (ListHead
->ListName
== ListName
);
860 /* Increment the number of paging file modified pages */
861 MmTotalPagesForPagingFile
++;
864 /* Don't handle bad pages yet yet */
865 ASSERT(Pfn1
->u3
.e1
.RemovalRequested
== 0);
867 /* Zero pages go to the head, all other pages go to the end */
868 if (ListName
== ZeroedPageList
)
870 /* Make the head of the list point to this page now */
871 Flink
= ListHead
->Flink
;
872 ListHead
->Flink
= PageFrameIndex
;
874 /* Make the page point to the previous head, and back to the list */
875 Pfn1
->u1
.Flink
= Flink
;
876 Pfn1
->u2
.Blink
= LIST_HEAD
;
878 /* Was the list empty? */
879 if (Flink
!= LIST_HEAD
)
881 /* It wasn't, so update the backlink of the previous head page */
882 Pfn2
= MI_PFN_ELEMENT(Flink
);
883 Pfn2
->u2
.Blink
= PageFrameIndex
;
887 /* It was empty, so have it loop back around to this new page */
888 ListHead
->Blink
= PageFrameIndex
;
893 /* Get the last page on the list */
894 LastPage
= ListHead
->Blink
;
895 if (LastPage
!= LIST_HEAD
)
897 /* Link us with the previous page, so we're at the end now */
898 MI_PFN_ELEMENT(LastPage
)->u1
.Flink
= PageFrameIndex
;
902 /* The list is empty, so we are the first page */
903 ListHead
->Flink
= PageFrameIndex
;
906 /* Now make the list head point back to us (since we go at the end) */
907 ListHead
->Blink
= PageFrameIndex
;
908 ASSERT_LIST_INVARIANT(ListHead
);
910 /* And initialize our own list pointers */
911 Pfn1
->u1
.Flink
= LIST_HEAD
;
912 Pfn1
->u2
.Blink
= LastPage
;
915 /* Move the page onto its new location */
916 Pfn1
->u3
.e1
.PageLocation
= ListName
;
918 /* For zero/free pages, we also have to handle the colored lists */
919 if (ListName
<= StandbyPageList
)
921 /* One more page on the system */
924 /* Check if we've reached the configured low memory threshold */
925 if (MmAvailablePages
== MmLowMemoryThreshold
)
927 /* Clear the event, because now we're ABOVE the threshold */
928 KeClearEvent(MiLowMemoryEvent
);
930 else if (MmAvailablePages
== MmHighMemoryThreshold
)
932 /* Otherwise check if we reached the high threshold and signal the event */
933 KeSetEvent(MiHighMemoryEvent
, 0, FALSE
);
937 ASSERT(ListName
== ZeroedPageList
);
938 ASSERT(Pfn1
->u4
.InPageError
== 0);
940 /* Get the page color */
941 Color
= PageFrameIndex
& MmSecondaryColorMask
;
943 /* Get the list for this color */
944 ColorHead
= &MmFreePagesByColor
[ZeroedPageList
][Color
];
946 /* Get the old head */
947 Flink
= ColorHead
->Flink
;
949 /* Make this page point back to the list, and point forwards to the old head */
950 Pfn1
->OriginalPte
.u
.Long
= Flink
;
951 Pfn1
->u4
.PteFrame
= COLORED_LIST_HEAD
;
953 /* Set the new head */
954 ColorHead
->Flink
= PageFrameIndex
;
956 /* Was the head empty? */
957 if (Flink
!= LIST_HEAD
)
959 /* No, so make the old head point to this page */
960 Pfn2
= MI_PFN_ELEMENT(Flink
);
961 Pfn2
->u4
.PteFrame
= PageFrameIndex
;
965 /* Yes, make it loop back to this page */
966 ColorHead
->Blink
= (PVOID
)Pfn1
;
969 /* One more paged on the colored list */
973 //ASSERT(MI_PFN_CURRENT_USAGE == MI_USAGE_NOT_SET);
974 Pfn1
->PfnUsage
= MI_USAGE_FREE_PAGE
;
975 MI_PFN_CURRENT_USAGE
= MI_USAGE_NOT_SET
;
976 RtlZeroMemory(Pfn1
->ProcessName
, 16);
979 else if (ListName
== ModifiedPageList
)
981 /* In ARM3, page must be destined for page file, and not yet written out */
982 ASSERT(Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0);
983 ASSERT(Pfn1
->OriginalPte
.u
.Soft
.PageFileHigh
== 0);
985 /* One more transition page */
986 MmTransitionSharedPages
++;
988 /* Increment the number of per-process modified pages */
989 PsGetCurrentProcess()->ModifiedPageCount
++;
991 /* FIXME: Wake up modified page writer if there are not enough free pages */
993 else if (ListName
== ModifiedNoWritePageList
)
995 /* This list is not yet implemented */
1002 MiInitializePfn(IN PFN_NUMBER PageFrameIndex
,
1003 IN PMMPTE PointerPte
,
1004 IN BOOLEAN Modified
)
1008 PMMPTE PointerPtePte
;
1009 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1012 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
1013 Pfn1
->PteAddress
= PointerPte
;
1015 /* Check if this PFN is part of a valid address space */
1016 if (PointerPte
->u
.Hard
.Valid
== 1)
1018 /* Only valid from MmCreateProcessAddressSpace path */
1019 ASSERT(PsGetCurrentProcess()->Vm
.WorkingSetSize
== 0);
1021 /* Make this a demand zero PTE */
1022 MI_MAKE_SOFTWARE_PTE(&Pfn1
->OriginalPte
, MM_READWRITE
);
1026 /* Copy the PTE data */
1027 Pfn1
->OriginalPte
= *PointerPte
;
1028 ASSERT(!((Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0) &&
1029 (Pfn1
->OriginalPte
.u
.Soft
.Transition
== 1)));
1032 /* Otherwise this is a fresh page -- set it up */
1033 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
1034 Pfn1
->u3
.e2
.ReferenceCount
= 1;
1035 Pfn1
->u2
.ShareCount
= 1;
1036 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
1037 ASSERT(Pfn1
->u3
.e1
.Rom
== 0);
1038 Pfn1
->u3
.e1
.Modified
= Modified
;
1040 /* Get the page table for the PTE */
1041 PointerPtePte
= MiAddressToPte(PointerPte
);
1042 if (PointerPtePte
->u
.Hard
.Valid
== 0)
1044 /* Make sure the PDE gets paged in properly */
1045 Status
= MiCheckPdeForPagedPool(PointerPte
);
1046 if (!NT_SUCCESS(Status
))
1049 KeBugCheckEx(MEMORY_MANAGEMENT
,
1051 (ULONG_PTR
)PointerPte
,
1052 (ULONG_PTR
)PointerPtePte
->u
.Long
,
1053 (ULONG_PTR
)MiPteToAddress(PointerPte
));
1057 /* Get the PFN for the page table */
1058 PageFrameIndex
= PFN_FROM_PTE(PointerPtePte
);
1059 ASSERT(PageFrameIndex
!= 0);
1060 Pfn1
->u4
.PteFrame
= PageFrameIndex
;
1062 /* Increase its share count so we don't get rid of it */
1063 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
1064 Pfn1
->u2
.ShareCount
++;
1069 MiInitializePfnAndMakePteValid(IN PFN_NUMBER PageFrameIndex
,
1070 IN PMMPTE PointerPte
,
1075 PMMPTE PointerPtePte
;
1076 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1078 /* PTE must be invalid */
1079 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
1082 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
1083 Pfn1
->PteAddress
= PointerPte
;
1084 Pfn1
->OriginalPte
= DemandZeroPte
;
1086 /* Otherwise this is a fresh page -- set it up */
1087 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
1088 Pfn1
->u3
.e2
.ReferenceCount
++;
1089 Pfn1
->u2
.ShareCount
++;
1090 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
1091 ASSERT(Pfn1
->u3
.e1
.Rom
== 0);
1092 Pfn1
->u3
.e1
.Modified
= 1;
1094 /* Get the page table for the PTE */
1095 PointerPtePte
= MiAddressToPte(PointerPte
);
1096 if (PointerPtePte
->u
.Hard
.Valid
== 0)
1098 /* Make sure the PDE gets paged in properly */
1099 Status
= MiCheckPdeForPagedPool(PointerPte
);
1100 if (!NT_SUCCESS(Status
))
1103 KeBugCheckEx(MEMORY_MANAGEMENT
,
1105 (ULONG_PTR
)PointerPte
,
1106 (ULONG_PTR
)PointerPtePte
->u
.Long
,
1107 (ULONG_PTR
)MiPteToAddress(PointerPte
));
1111 /* Get the PFN for the page table */
1112 PageFrameIndex
= PFN_FROM_PTE(PointerPtePte
);
1113 ASSERT(PageFrameIndex
!= 0);
1114 Pfn1
->u4
.PteFrame
= PageFrameIndex
;
1116 /* Increase its share count so we don't get rid of it */
1117 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
1118 Pfn1
->u2
.ShareCount
++;
1120 /* Write valid PTE */
1121 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
1126 MiInitializeAndChargePfn(OUT PPFN_NUMBER PageFrameIndex
,
1127 IN PMMPTE PointerPde
,
1128 IN PFN_NUMBER ContainingPageFrame
,
1129 IN BOOLEAN SessionAllocation
)
1134 /* Use either a global or local PDE */
1135 TempPte
= SessionAllocation
? ValidKernelPdeLocal
: ValidKernelPde
;
1137 /* Lock the PFN database */
1138 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1140 /* Make sure nobody is racing us */
1141 if (PointerPde
->u
.Hard
.Valid
== 1)
1143 /* Return special error if that was the case */
1144 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1145 return STATUS_RETRY
;
1148 /* Grab a zero page and set the PFN, then make it valid */
1149 *PageFrameIndex
= MiRemoveZeroPage(MI_GET_NEXT_COLOR());
1150 TempPte
.u
.Hard
.PageFrameNumber
= *PageFrameIndex
;
1151 MI_WRITE_VALID_PTE(PointerPde
, TempPte
);
1153 /* Initialize the PFN */
1154 MiInitializePfnForOtherProcess(*PageFrameIndex
,
1156 ContainingPageFrame
);
1157 ASSERT(MI_PFN_ELEMENT(*PageFrameIndex
)->u1
.WsIndex
== 0);
1159 /* Release the lock and return success */
1160 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1161 return STATUS_SUCCESS
;
1166 MiAllocatePfn(IN PMMPTE PointerPte
,
1167 IN ULONG Protection
)
1170 PFN_NUMBER PageFrameIndex
;
1173 /* Sanity check that we aren't passed a valid PTE */
1174 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
1176 /* Make an empty software PTE */
1177 MI_MAKE_SOFTWARE_PTE(&TempPte
, MM_READWRITE
);
1179 /* Lock the PFN database */
1180 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1182 /* Check if we're running low on pages */
1183 if (MmAvailablePages
< 128)
1185 DPRINT1("Warning, running low on memory: %lu pages left\n", MmAvailablePages
);
1187 //MiEnsureAvailablePageOrWait(NULL, OldIrql);
1189 /* Call RosMm and see if it can release any pages for us */
1190 MmRebalanceMemoryConsumers();
1194 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
1195 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
1196 PageFrameIndex
= MiRemoveAnyPage(MI_GET_NEXT_COLOR());
1198 /* Write the software PTE */
1199 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
1200 PointerPte
->u
.Soft
.Protection
|= Protection
;
1202 /* Initialize its PFN entry */
1203 MiInitializePfn(PageFrameIndex
, PointerPte
, TRUE
);
1205 /* Release the PFN lock and return the page */
1206 ASSERT_LIST_INVARIANT(&MmFreePageListHead
);
1207 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead
);
1208 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1209 return PageFrameIndex
;
1214 MiDecrementShareCount(IN PMMPFN Pfn1
,
1215 IN PFN_NUMBER PageFrameIndex
)
1220 ASSERT(PageFrameIndex
> 0);
1221 ASSERT(MI_PFN_ELEMENT(PageFrameIndex
) != NULL
);
1222 ASSERT(Pfn1
== MI_PFN_ELEMENT(PageFrameIndex
));
1223 ASSERT(MI_IS_ROS_PFN(Pfn1
) == FALSE
);
1225 /* Page must be in-use */
1226 if ((Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
) &&
1227 (Pfn1
->u3
.e1
.PageLocation
!= StandbyPageList
))
1229 /* Otherwise we have PFN corruption */
1230 KeBugCheckEx(PFN_LIST_CORRUPT
,
1233 Pfn1
->u3
.e1
.PageLocation
,
1237 /* Check if the share count is now 0 */
1238 ASSERT(Pfn1
->u2
.ShareCount
< 0xF000000);
1239 if (!--Pfn1
->u2
.ShareCount
)
1241 /* Was this a prototype PTE? */
1242 if (Pfn1
->u3
.e1
.PrototypePte
)
1244 /* Grab the PTE address and make sure it's in prototype pool */
1245 PointerPte
= Pfn1
->PteAddress
;
1246 ASSERT((PointerPte
>= (PMMPTE
)MmPagedPoolStart
) && (PointerPte
<= (PMMPTE
)MmPagedPoolEnd
));
1248 /* The PTE that backs it should also be valdi */
1249 PointerPte
= MiAddressToPte(PointerPte
);
1250 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
1252 /* Get the original prototype PTE and turn it into a transition PTE */
1253 PointerPte
= Pfn1
->PteAddress
;
1254 TempPte
= *PointerPte
;
1255 TempPte
.u
.Soft
.Transition
= 1;
1256 TempPte
.u
.Soft
.Valid
= 0;
1257 TempPte
.u
.Soft
.Prototype
= 0;
1258 TempPte
.u
.Soft
.Protection
= Pfn1
->OriginalPte
.u
.Soft
.Protection
;
1259 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
1260 DPRINT("Marking PTE: %p as transition (%p - %lx)\n", PointerPte
, Pfn1
, MiGetPfnEntryIndex(Pfn1
));
1263 /* Put the page in transition */
1264 Pfn1
->u3
.e1
.PageLocation
= TransitionPage
;
1266 /* PFN lock must be held */
1267 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1269 /* Page should at least have one reference */
1270 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1271 if (Pfn1
->u3
.e2
.ReferenceCount
== 1)
1273 /* Is there still a PFN for this page? */
1274 if (MI_IS_PFN_DELETED(Pfn1
) == TRUE
)
1276 /* Clear the last reference */
1277 Pfn1
->u3
.e2
.ReferenceCount
= 0;
1278 ASSERT(Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0);
1280 /* Mark the page temporarily as valid, we're going to make it free soon */
1281 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
1283 /* Bring it back into the free list */
1284 MiInsertPageInFreeList(PageFrameIndex
);
1288 /* PFN not yet deleted, drop a ref count */
1289 MiDecrementReferenceCount(Pfn1
, PageFrameIndex
);
1294 /* Otherwise, just drop the reference count */
1295 InterlockedDecrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1302 MiDecrementReferenceCount(IN PMMPFN Pfn1
,
1303 IN PFN_NUMBER PageFrameIndex
)
1305 /* PFN lock must be held */
1306 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1308 /* Sanity checks on the page */
1309 if (PageFrameIndex
> MmHighestPhysicalPage
||
1310 Pfn1
!= MI_PFN_ELEMENT(PageFrameIndex
) ||
1311 Pfn1
->u3
.e2
.ReferenceCount
== 0 ||
1312 Pfn1
->u3
.e2
.ReferenceCount
>= 2500)
1314 DPRINT1("PageFrameIndex=0x%lx, MmHighestPhysicalPage=0x%lx\n", PageFrameIndex
, MmHighestPhysicalPage
);
1315 DPRINT1("Pfn1=%p, Element=%p, RefCount=%u\n", Pfn1
, MI_PFN_ELEMENT(PageFrameIndex
), Pfn1
->u3
.e2
.ReferenceCount
);
1316 ASSERT(PageFrameIndex
<= MmHighestPhysicalPage
);
1317 ASSERT(Pfn1
== MI_PFN_ELEMENT(PageFrameIndex
));
1318 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1319 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
< 2500);
1322 /* Dereference the page, bail out if it's still alive */
1323 InterlockedDecrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1324 if (Pfn1
->u3
.e2
.ReferenceCount
) return;
1326 /* Nobody should still have reference to this page */
1327 if (Pfn1
->u2
.ShareCount
!= 0)
1329 /* Otherwise something's really wrong */
1330 KeBugCheckEx(PFN_LIST_CORRUPT
, 7, PageFrameIndex
, Pfn1
->u2
.ShareCount
, 0);
1333 /* And it should be lying on some page list */
1334 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1336 /* Did someone set the delete flag? */
1337 if (MI_IS_PFN_DELETED(Pfn1
))
1339 /* Insert it into the free list, there's nothing left to do */
1340 MiInsertPageInFreeList(PageFrameIndex
);
1344 /* Check to see which list this page should go into */
1345 if (Pfn1
->u3
.e1
.Modified
== 1)
1347 /* Push it into the modified page list */
1348 MiInsertPageInList(&MmModifiedPageListHead
, PageFrameIndex
);
1352 /* Otherwise, insert this page into the standby list */
1353 ASSERT(Pfn1
->u3
.e1
.RemovalRequested
== 0);
1354 MiInsertStandbyListAtFront(PageFrameIndex
);
1360 MiInitializePfnForOtherProcess(IN PFN_NUMBER PageFrameIndex
,
1361 IN PMMPTE PointerPte
,
1362 IN PFN_NUMBER PteFrame
)
1367 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
1368 Pfn1
->PteAddress
= PointerPte
;
1370 /* Make this a software PTE */
1371 MI_MAKE_SOFTWARE_PTE(&Pfn1
->OriginalPte
, MM_READWRITE
);
1373 /* Setup the page */
1374 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 0);
1375 Pfn1
->u3
.e2
.ReferenceCount
= 1;
1376 Pfn1
->u2
.ShareCount
= 1;
1377 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
1378 Pfn1
->u3
.e1
.Modified
= TRUE
;
1379 Pfn1
->u4
.InPageError
= FALSE
;
1381 /* Did we get a PFN for the page table */
1385 Pfn1
->u4
.PteFrame
= PteFrame
;
1387 /* Increase its share count so we don't get rid of it */
1388 Pfn1
= MI_PFN_ELEMENT(PteFrame
);
1389 Pfn1
->u2
.ShareCount
++;