[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / mm / ARM3 / pfnlist.c
1 /*
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
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 #line 15 "ARMĀ³::PFNLIST"
16 #define MODULE_INVOLVED_IN_ARM3
17 #include "../ARM3/miarm.h"
18
19 #if DBG
20 #define ASSERT_LIST_INVARIANT(x) \
21 do { \
22 ASSERT(((x)->Total == 0 && \
23 (x)->Flink == LIST_HEAD && \
24 (x)->Blink == LIST_HEAD) || \
25 ((x)->Total != 0 && \
26 (x)->Flink != LIST_HEAD && \
27 (x)->Blink != LIST_HEAD)); \
28 } while (0)
29 #else
30 #define ASSERT_LIST_INVARIANT(x)
31 #endif
32
33 /* GLOBALS ********************************************************************/
34
35 BOOLEAN MmDynamicPfn;
36 BOOLEAN MmMirroring;
37 ULONG MmSystemPageColor;
38
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};
46
47 PMMPFNLIST MmPageLocationList[] =
48 {
49 &MmZeroedPageListHead,
50 &MmFreePageListHead,
51 &MmStandbyPageListHead,
52 &MmModifiedPageListHead,
53 &MmModifiedNoWritePageListHead,
54 &MmBadPageListHead,
55 NULL,
56 NULL
57 };
58
59 ULONG MI_PFN_CURRENT_USAGE;
60 CHAR MI_PFN_CURRENT_PROCESS_NAME[16] = "None yet";
61
62 /* FUNCTIONS ******************************************************************/
63
64 VOID
65 NTAPI
66 MiZeroPhysicalPage(IN PFN_NUMBER PageFrameIndex)
67 {
68 KIRQL OldIrql;
69 PVOID VirtualAddress;
70 PEPROCESS Process = PsGetCurrentProcess();
71
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);
77 }
78
79 VOID
80 NTAPI
81 MiUnlinkFreeOrZeroedPage(IN PMMPFN Entry)
82 {
83 PFN_NUMBER OldFlink, OldBlink;
84 PMMPFNLIST ListHead;
85 MMLISTS ListName;
86 ULONG Color;
87 PMMCOLOR_TABLES ColorTable;
88 PMMPFN Pfn1;
89
90 /* Make sure the PFN lock is held */
91 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
92
93 /* Make sure the PFN entry isn't in-use */
94 ASSERT(Entry->u3.e1.WriteInProgress == 0);
95 ASSERT(Entry->u3.e1.ReadInProgress == 0);
96
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);
103
104 /* Remove one count */
105 ASSERT(ListHead->Total != 0);
106 ListHead->Total--;
107
108 /* Get the forward and back pointers */
109 OldFlink = Entry->u1.Flink;
110 OldBlink = Entry->u2.Blink;
111
112 /* Check if the next entry is the list head */
113 if (OldFlink != LIST_HEAD)
114 {
115 /* It is not, so set the backlink of the actual entry, to our backlink */
116 MI_PFN_ELEMENT(OldFlink)->u2.Blink = OldBlink;
117 }
118 else
119 {
120 /* Set the list head's backlink instead */
121 ListHead->Blink = OldBlink;
122 }
123
124 /* Check if the back entry is the list head */
125 if (OldBlink != LIST_HEAD)
126 {
127 /* It is not, so set the backlink of the actual entry, to our backlink */
128 MI_PFN_ELEMENT(OldBlink)->u1.Flink = OldFlink;
129 }
130 else
131 {
132 /* Set the list head's backlink instead */
133 ListHead->Flink = OldFlink;
134 }
135
136 /* Get the page color */
137 OldBlink = MiGetPfnEntryIndex(Entry);
138 Color = OldBlink & MmSecondaryColorMask;
139
140 /* Get the first page on the color list */
141 ColorTable = &MmFreePagesByColor[ListName][Color];
142
143 /* Check if this was was actually the head */
144 OldFlink = ColorTable->Flink;
145 if (OldFlink == OldBlink)
146 {
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)
150 {
151 /* And make the previous link point to the head now */
152 MI_PFN_ELEMENT(ColorTable->Flink)->u4.PteFrame = COLORED_LIST_HEAD;
153 }
154 else
155 {
156 /* And if that page was the head, loop the list back around */
157 ColorTable->Blink = (PVOID)LIST_HEAD;
158 }
159 }
160 else
161 {
162 /* This page shouldn't be pointing back to the head */
163 ASSERT(Entry->u4.PteFrame != COLORED_LIST_HEAD);
164
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;
168
169 /* Check if this page was pointing to the head */
170 if (Entry->OriginalPte.u.Long != LIST_HEAD)
171 {
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;
175 }
176 else
177 {
178 /* Then the table is directly back pointing to this page now */
179 ColorTable->Blink = Pfn1;
180 }
181 }
182
183 /* One less colored page */
184 ASSERT(ColorTable->Count >= 1);
185 ColorTable->Count--;
186
187 /* ReactOS Hack */
188 Entry->OriginalPte.u.Long = 0;
189
190 /* We are not on a list anymore */
191 Entry->u1.Flink = Entry->u2.Blink = 0;
192 ASSERT_LIST_INVARIANT(ListHead);
193
194 /* See if we hit any thresholds */
195 if (MmAvailablePages == MmHighMemoryThreshold)
196 {
197 /* Clear the high memory event */
198 KeClearEvent(MiHighMemoryEvent);
199 }
200 else if (MmAvailablePages == MmLowMemoryThreshold)
201 {
202 /* Signal the low memory event */
203 KeSetEvent(MiLowMemoryEvent, 0, FALSE);
204 }
205
206 /* One less page */
207 if (--MmAvailablePages < MmMinimumFreePages)
208 {
209 /* FIXME: Should wake up the MPW and working set manager, if we had one */
210 }
211
212 #if MI_TRACE_PFNS
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);
218 #endif
219 }
220
221 PFN_NUMBER
222 NTAPI
223 MiRemovePageByColor(IN PFN_NUMBER PageIndex,
224 IN ULONG Color)
225 {
226 PMMPFN Pfn1;
227 PMMPFNLIST ListHead;
228 MMLISTS ListName;
229 PFN_NUMBER OldFlink, OldBlink;
230 ULONG OldColor, OldCache;
231 PMMCOLOR_TABLES ColorTable;
232
233 /* Make sure PFN lock is held */
234 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
235 ASSERT(Color < MmSecondaryColors);
236
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);
241
242 /* Capture data for later */
243 OldColor = Pfn1->u3.e1.PageColor;
244 OldCache = Pfn1->u3.e1.CacheAttribute;
245
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);
251
252 /* Remove a page */
253 ListHead->Total--;
254
255 /* Get the forward and back pointers */
256 OldFlink = Pfn1->u1.Flink;
257 OldBlink = Pfn1->u2.Blink;
258
259 /* Check if the next entry is the list head */
260 if (OldFlink != LIST_HEAD)
261 {
262 /* It is not, so set the backlink of the actual entry, to our backlink */
263 MI_PFN_ELEMENT(OldFlink)->u2.Blink = OldBlink;
264 }
265 else
266 {
267 /* Set the list head's backlink instead */
268 ListHead->Blink = OldBlink;
269 }
270
271 /* Check if the back entry is the list head */
272 if (OldBlink != LIST_HEAD)
273 {
274 /* It is not, so set the backlink of the actual entry, to our backlink */
275 MI_PFN_ELEMENT(OldBlink)->u1.Flink = OldFlink;
276 }
277 else
278 {
279 /* Set the list head's backlink instead */
280 ListHead->Flink = OldFlink;
281 }
282
283 /* We are not on a list anymore */
284 ASSERT_LIST_INVARIANT(ListHead);
285 Pfn1->u1.Flink = Pfn1->u2.Blink = 0;
286
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;
291
292 /* Get the first page on the color list */
293 ASSERT(Color < MmSecondaryColors);
294 ColorTable = &MmFreePagesByColor[ListName][Color];
295 ASSERT(ColorTable->Count >= 1);
296
297 /* Set the forward link to whoever we were pointing to */
298 ColorTable->Flink = Pfn1->OriginalPte.u.Long;
299
300 /* Get the first page on the color list */
301 if (ColorTable->Flink == LIST_HEAD)
302 {
303 /* This is the beginning of the list, so set the sentinel value */
304 ColorTable->Blink = (PVOID)LIST_HEAD;
305 }
306 else
307 {
308 /* The list is empty, so we are the first page */
309 MI_PFN_ELEMENT(ColorTable->Flink)->u4.PteFrame = COLORED_LIST_HEAD;
310 }
311
312 /* One less page */
313 ColorTable->Count--;
314
315 /* ReactOS Hack */
316 Pfn1->OriginalPte.u.Long = 0;
317
318 /* See if we hit any thresholds */
319 if (MmAvailablePages == MmHighMemoryThreshold)
320 {
321 /* Clear the high memory event */
322 KeClearEvent(MiHighMemoryEvent);
323 }
324 else if (MmAvailablePages == MmLowMemoryThreshold)
325 {
326 /* Signal the low memory event */
327 KeSetEvent(MiLowMemoryEvent, 0, FALSE);
328 }
329
330 /* One less page */
331 if (--MmAvailablePages < MmMinimumFreePages)
332 {
333 /* FIXME: Should wake up the MPW and working set manager, if we had one */
334 }
335
336 #if MI_TRACE_PFNS
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);
342 #endif
343
344 /* Return the page */
345 return PageIndex;
346 }
347
348 PFN_NUMBER
349 NTAPI
350 MiRemoveAnyPage(IN ULONG Color)
351 {
352 PFN_NUMBER PageIndex;
353 PMMPFN Pfn1;
354
355 /* Make sure PFN lock is held and we have pages */
356 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
357 ASSERT(MmAvailablePages != 0);
358 ASSERT(Color < MmSecondaryColors);
359
360 /* Check the colored free list */
361 PageIndex = MmFreePagesByColor[FreePageList][Color].Flink;
362 if (PageIndex == LIST_HEAD)
363 {
364 /* Check the colored zero list */
365 PageIndex = MmFreePagesByColor[ZeroedPageList][Color].Flink;
366 if (PageIndex == LIST_HEAD)
367 {
368 /* Check the free list */
369 ASSERT_LIST_INVARIANT(&MmFreePageListHead);
370 PageIndex = MmFreePageListHead.Flink;
371 Color = PageIndex & MmSecondaryColorMask;
372 if (PageIndex == LIST_HEAD)
373 {
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)
380 {
381 /* FIXME: Should check the standby list */
382 ASSERT(MmZeroedPageListHead.Total == 0);
383 }
384 }
385 }
386 }
387
388 /* Remove the page from its list */
389 PageIndex = MiRemovePageByColor(PageIndex, Color);
390
391 /* Sanity checks */
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);
399
400 /* Return the page */
401 return PageIndex;
402 }
403
404 PFN_NUMBER
405 NTAPI
406 MiRemoveZeroPage(IN ULONG Color)
407 {
408 PFN_NUMBER PageIndex;
409 PMMPFN Pfn1;
410 BOOLEAN Zero = FALSE;
411
412 /* Make sure PFN lock is held and we have pages */
413 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
414 ASSERT(MmAvailablePages != 0);
415 ASSERT(Color < MmSecondaryColors);
416
417 /* Check the colored zero list */
418 PageIndex = MmFreePagesByColor[ZeroedPageList][Color].Flink;
419 if (PageIndex == LIST_HEAD)
420 {
421 /* Check the zero list */
422 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead);
423 PageIndex = MmZeroedPageListHead.Flink;
424 if (PageIndex == LIST_HEAD)
425 {
426 /* This means there's no zero pages, we have to look for free ones */
427 ASSERT(MmZeroedPageListHead.Total == 0);
428 Zero = TRUE;
429
430 /* Check the colored free list */
431 PageIndex = MmFreePagesByColor[FreePageList][Color].Flink;
432 if (PageIndex == LIST_HEAD)
433 {
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)
440 {
441 /* FIXME: Should check the standby list */
442 ASSERT(MmZeroedPageListHead.Total == 0);
443 }
444 }
445 }
446 else
447 {
448 Color = PageIndex & MmSecondaryColorMask;
449 }
450 }
451
452 /* Sanity checks */
453 Pfn1 = MI_PFN_ELEMENT(PageIndex);
454 ASSERT((Pfn1->u3.e1.PageLocation == FreePageList) ||
455 (Pfn1->u3.e1.PageLocation == ZeroedPageList));
456
457 /* Remove the page from its list */
458 PageIndex = MiRemovePageByColor(PageIndex, Color);
459 ASSERT(Pfn1 == MI_PFN_ELEMENT(PageIndex));
460
461 /* Zero it, if needed */
462 if (Zero) MiZeroPhysicalPage(PageIndex);
463
464 /* Sanity checks */
465 ASSERT(Pfn1->u3.e2.ReferenceCount == 0);
466 ASSERT(Pfn1->u2.ShareCount == 0);
467 ASSERT_LIST_INVARIANT(&MmFreePageListHead);
468 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead);
469
470 /* Return the page */
471 return PageIndex;
472 }
473
474 VOID
475 NTAPI
476 MiInsertPageInFreeList(IN PFN_NUMBER PageFrameIndex)
477 {
478 PMMPFNLIST ListHead;
479 PFN_NUMBER LastPage;
480 PMMPFN Pfn1;
481 ULONG Color;
482 PMMPFN Blink;
483 PMMCOLOR_TABLES ColorTable;
484
485 /* Make sure the page index is valid */
486 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL);
487 ASSERT((PageFrameIndex != 0) &&
488 (PageFrameIndex <= MmHighestPhysicalPage) &&
489 (PageFrameIndex >= MmLowestPhysicalPage));
490
491 /* Get the PFN entry */
492 Pfn1 = MI_PFN_ELEMENT(PageFrameIndex);
493
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);
500
501 /* Get the free page list and increment its count */
502 ListHead = &MmFreePageListHead;
503 ASSERT_LIST_INVARIANT(ListHead);
504 ListHead->Total++;
505
506 /* Get the last page on the list */
507 LastPage = ListHead->Blink;
508 if (LastPage != LIST_HEAD)
509 {
510 /* Link us with the previous page, so we're at the end now */
511 MI_PFN_ELEMENT(LastPage)->u1.Flink = PageFrameIndex;
512 }
513 else
514 {
515 /* The list is empty, so we are the first page */
516 ListHead->Flink = PageFrameIndex;
517 }
518
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);
522
523 /* And initialize our own list pointers */
524 Pfn1->u1.Flink = LIST_HEAD;
525 Pfn1->u2.Blink = LastPage;
526
527 /* Set the list name and default priority */
528 Pfn1->u3.e1.PageLocation = FreePageList;
529 Pfn1->u4.Priority = 3;
530
531 /* Clear some status fields */
532 Pfn1->u4.InPageError = 0;
533 Pfn1->u4.AweAllocation = 0;
534
535 /* Increase available pages */
536 MmAvailablePages++;
537
538 /* Check if we've reached the configured low memory threshold */
539 if (MmAvailablePages == MmLowMemoryThreshold)
540 {
541 /* Clear the event, because now we're ABOVE the threshold */
542 KeClearEvent(MiLowMemoryEvent);
543 }
544 else if (MmAvailablePages == MmHighMemoryThreshold)
545 {
546 /* Otherwise check if we reached the high threshold and signal the event */
547 KeSetEvent(MiHighMemoryEvent, 0, FALSE);
548 }
549
550 /* Get the page color */
551 Color = PageFrameIndex & MmSecondaryColorMask;
552
553 /* Get the first page on the color list */
554 ColorTable = &MmFreePagesByColor[FreePageList][Color];
555 if (ColorTable->Flink == LIST_HEAD)
556 {
557 /* The list is empty, so we are the first page */
558 Pfn1->u4.PteFrame = COLORED_LIST_HEAD;
559 ColorTable->Flink = PageFrameIndex;
560 }
561 else
562 {
563 /* Get the previous page */
564 Blink = (PMMPFN)ColorTable->Blink;
565
566 /* Make it link to us, and link back to it */
567 Blink->OriginalPte.u.Long = PageFrameIndex;
568 Pfn1->u4.PteFrame = MiGetPfnEntryIndex(Blink);
569 }
570
571 /* Now initialize our own list pointers */
572 ColorTable->Blink = Pfn1;
573
574 /* This page is now the last */
575 Pfn1->OriginalPte.u.Long = LIST_HEAD;
576
577 /* And increase the count in the colored list */
578 ColorTable->Count++;
579
580 /* Notify zero page thread if enough pages are on the free list now */
581 if ((ListHead->Total >= 8) && !(MmZeroingPageThreadActive))
582 {
583 /* Set the event */
584 MmZeroingPageThreadActive = TRUE;
585 KeSetEvent(&MmZeroingPageEvent, IO_NO_INCREMENT, FALSE);
586 }
587
588 #if MI_TRACE_PFNS
589 Pfn1->PfnUsage = MI_USAGE_FREE_PAGE;
590 RtlZeroMemory(Pfn1->ProcessName, 16);
591 #endif
592 }
593
594 /* Note: This function is hardcoded only for the zeroed page list, for now */
595 VOID
596 NTAPI
597 MiInsertPageInList(IN PMMPFNLIST ListHead,
598 IN PFN_NUMBER PageFrameIndex)
599 {
600 PFN_NUMBER Flink;
601 PMMPFN Pfn1, Pfn2;
602 MMLISTS ListName;
603 PMMCOLOR_TABLES ColorHead;
604 ULONG Color;
605
606 /* For free pages, use MiInsertPageInFreeList */
607 ASSERT(ListHead != &MmFreePageListHead);
608
609 /* Make sure the lock is held */
610 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
611
612 /* Make sure the PFN is valid */
613 ASSERT((PageFrameIndex) &&
614 (PageFrameIndex <= MmHighestPhysicalPage) &&
615 (PageFrameIndex >= MmLowestPhysicalPage));
616
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);
621
622 /* Only used for zero pages in ReactOS */
623 ListName = ListHead->ListName;
624 ASSERT(ListName == ZeroedPageList);
625 ListHead->Total++;
626
627 /* Don't handle bad pages yet yet */
628 ASSERT(Pfn1->u3.e1.RemovalRequested == 0);
629
630 /* Make the head of the list point to this page now */
631 Flink = ListHead->Flink;
632 ListHead->Flink = PageFrameIndex;
633
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;
637
638 /* Was the list empty? */
639 if (Flink != LIST_HEAD)
640 {
641 /* It wasn't, so update the backlink of the previous head page */
642 Pfn2 = MI_PFN_ELEMENT(Flink);
643 Pfn2->u2.Blink = PageFrameIndex;
644 }
645 else
646 {
647 /* It was empty, so have it loop back around to this new page */
648 ListHead->Blink = PageFrameIndex;
649 }
650
651 /* Move the page onto its new location */
652 Pfn1->u3.e1.PageLocation = ListName;
653
654 /* One more page on the system */
655 MmAvailablePages++;
656
657 /* Check if we've reached the configured low memory threshold */
658 if (MmAvailablePages == MmLowMemoryThreshold)
659 {
660 /* Clear the event, because now we're ABOVE the threshold */
661 KeClearEvent(MiLowMemoryEvent);
662 }
663 else if (MmAvailablePages == MmHighMemoryThreshold)
664 {
665 /* Otherwise check if we reached the high threshold and signal the event */
666 KeSetEvent(MiHighMemoryEvent, 0, FALSE);
667 }
668
669 /* Sanity checks */
670 ASSERT(ListName == ZeroedPageList);
671 ASSERT(Pfn1->u4.InPageError == 0);
672
673 /* Get the page color */
674 Color = PageFrameIndex & MmSecondaryColorMask;
675
676 /* Get the list for this color */
677 ColorHead = &MmFreePagesByColor[ZeroedPageList][Color];
678
679 /* Get the old head */
680 Flink = ColorHead->Flink;
681
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;
685
686 /* Set the new head */
687 ColorHead->Flink = PageFrameIndex;
688
689 /* Was the head empty? */
690 if (Flink != LIST_HEAD)
691 {
692 /* No, so make the old head point to this page */
693 Pfn2 = MI_PFN_ELEMENT(Flink);
694 Pfn2->u4.PteFrame = PageFrameIndex;
695 }
696 else
697 {
698 /* Yes, make it loop back to this page */
699 ColorHead->Blink = (PVOID)Pfn1;
700 }
701
702 /* One more paged on the colored list */
703 ColorHead->Count++;
704
705 #if MI_TRACE_PFNS
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);
710 #endif
711 }
712
713 VOID
714 NTAPI
715 MiInitializePfn(IN PFN_NUMBER PageFrameIndex,
716 IN PMMPTE PointerPte,
717 IN BOOLEAN Modified)
718 {
719 PMMPFN Pfn1;
720 NTSTATUS Status;
721 PMMPTE PointerPtePte;
722 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
723
724 /* Setup the PTE */
725 Pfn1 = MI_PFN_ELEMENT(PageFrameIndex);
726 Pfn1->PteAddress = PointerPte;
727
728 /* Check if this PFN is part of a valid address space */
729 if (PointerPte->u.Hard.Valid == 1)
730 {
731 /* Only valid from MmCreateProcessAddressSpace path */
732 ASSERT(PsGetCurrentProcess()->Vm.WorkingSetSize == 0);
733
734 /* Make this a demand zero PTE */
735 MI_MAKE_SOFTWARE_PTE(&Pfn1->OriginalPte, MM_READWRITE);
736 }
737 else
738 {
739 /* Copy the PTE data */
740 Pfn1->OriginalPte = *PointerPte;
741 ASSERT(!((Pfn1->OriginalPte.u.Soft.Prototype == 0) &&
742 (Pfn1->OriginalPte.u.Soft.Transition == 1)));
743 }
744
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;
752
753 /* Get the page table for the PTE */
754 PointerPtePte = MiAddressToPte(PointerPte);
755 if (PointerPtePte->u.Hard.Valid == 0)
756 {
757 /* Make sure the PDE gets paged in properly */
758 Status = MiCheckPdeForPagedPool(PointerPte);
759 if (!NT_SUCCESS(Status))
760 {
761 /* Crash */
762 KeBugCheckEx(MEMORY_MANAGEMENT,
763 0x61940,
764 (ULONG_PTR)PointerPte,
765 (ULONG_PTR)PointerPtePte->u.Long,
766 (ULONG_PTR)MiPteToAddress(PointerPte));
767 }
768 }
769
770 /* Get the PFN for the page table */
771 PageFrameIndex = PFN_FROM_PTE(PointerPtePte);
772 ASSERT(PageFrameIndex != 0);
773 Pfn1->u4.PteFrame = PageFrameIndex;
774
775 /* Increase its share count so we don't get rid of it */
776 Pfn1 = MI_PFN_ELEMENT(PageFrameIndex);
777 Pfn1->u2.ShareCount++;
778 }
779
780 PFN_NUMBER
781 NTAPI
782 MiAllocatePfn(IN PMMPTE PointerPte,
783 IN ULONG Protection)
784 {
785 KIRQL OldIrql;
786 PFN_NUMBER PageFrameIndex;
787 MMPTE TempPte;
788
789 /* Sanity check that we aren't passed a valid PTE */
790 ASSERT(PointerPte->u.Hard.Valid == 0);
791
792 /* Make an empty software PTE */
793 MI_MAKE_SOFTWARE_PTE(&TempPte, MM_READWRITE);
794
795 /* Lock the PFN database */
796 OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
797
798 /* Check if we're running low on pages */
799 if (MmAvailablePages < 128)
800 {
801 DPRINT1("Warning, running low on memory: %d pages left\n", MmAvailablePages);
802 //MiEnsureAvailablePageOrWait(NULL, OldIrql);
803 }
804
805 /* Grab a page */
806 ASSERT_LIST_INVARIANT(&MmFreePageListHead);
807 ASSERT_LIST_INVARIANT(&MmZeroedPageListHead);
808 PageFrameIndex = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
809
810 /* Write the software PTE */
811 MI_WRITE_INVALID_PTE(PointerPte, TempPte);
812 PointerPte->u.Soft.Protection |= Protection;
813
814 /* Initialize its PFN entry */
815 MiInitializePfn(PageFrameIndex, PointerPte, TRUE);
816
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;
822 }
823
824 VOID
825 NTAPI
826 MiDecrementShareCount(IN PMMPFN Pfn1,
827 IN PFN_NUMBER PageFrameIndex)
828 {
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);
833
834 /* Page must be in-use */
835 if ((Pfn1->u3.e1.PageLocation != ActiveAndValid) &&
836 (Pfn1->u3.e1.PageLocation != StandbyPageList))
837 {
838 /* Otherwise we have PFN corruption */
839 KeBugCheckEx(PFN_LIST_CORRUPT,
840 0x99,
841 PageFrameIndex,
842 Pfn1->u3.e1.PageLocation,
843 0);
844 }
845
846 /* Check if the share count is now 0 */
847 ASSERT(Pfn1->u2.ShareCount < 0xF000000);
848 if (!--Pfn1->u2.ShareCount)
849 {
850 /* ReactOS does not handle these */
851 ASSERT(Pfn1->u3.e1.PrototypePte == 0);
852
853 /* Put the page in transition */
854 Pfn1->u3.e1.PageLocation = TransitionPage;
855
856 /* PFN lock must be held */
857 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
858
859 /* Page should at least have one reference */
860 ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
861 if (Pfn1->u3.e2.ReferenceCount == 1)
862 {
863 /* In ReactOS, this path should always be hit with a deleted PFN */
864 ASSERT(MI_IS_PFN_DELETED(Pfn1) == TRUE);
865
866 /* Clear the last reference */
867 Pfn1->u3.e2.ReferenceCount = 0;
868 ASSERT(Pfn1->OriginalPte.u.Soft.Prototype == 0);
869
870 /* Mark the page temporarily as valid, we're going to make it free soon */
871 Pfn1->u3.e1.PageLocation = ActiveAndValid;
872
873 /* Bring it back into the free list */
874 MiInsertPageInFreeList(PageFrameIndex);
875 }
876 else
877 {
878 /* Otherwise, just drop the reference count */
879 InterlockedDecrement16((PSHORT)&Pfn1->u3.e2.ReferenceCount);
880 }
881 }
882 }
883
884 VOID
885 NTAPI
886 MiDecrementReferenceCount(IN PMMPFN Pfn1,
887 IN PFN_NUMBER PageFrameIndex)
888 {
889 /* PFN lock must be held */
890 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
891
892 /* Sanity checks on the page */
893 ASSERT(PageFrameIndex < MmHighestPhysicalPage);
894 ASSERT(Pfn1 == MI_PFN_ELEMENT(PageFrameIndex));
895 ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
896
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;
900
901 /* Nobody should still have reference to this page */
902 if (Pfn1->u2.ShareCount != 0)
903 {
904 /* Otherwise something's really wrong */
905 KeBugCheckEx(PFN_LIST_CORRUPT, 7, PageFrameIndex, Pfn1->u2.ShareCount, 0);
906 }
907
908 /* And it should be lying on some page list */
909 ASSERT(Pfn1->u3.e1.PageLocation != ActiveAndValid);
910
911 /* Did someone set the delete flag? */
912 if (MI_IS_PFN_DELETED(Pfn1))
913 {
914 /* Insert it into the free list, there's nothing left to do */
915 MiInsertPageInFreeList(PageFrameIndex);
916 return;
917 }
918
919 /* We don't have a modified list yet */
920 ASSERT(Pfn1->u3.e1.Modified == 0);
921 ASSERT(Pfn1->u3.e1.RemovalRequested == 0);
922
923 /* FIXME: Normally it would go on the standby list, but we're pushing it on the free list */
924 MiInsertPageInFreeList(PageFrameIndex);
925 }
926
927 VOID
928 NTAPI
929 MiInitializePfnForOtherProcess(IN PFN_NUMBER PageFrameIndex,
930 IN PMMPTE PointerPte,
931 IN PFN_NUMBER PteFrame)
932 {
933 PMMPFN Pfn1;
934
935 /* Setup the PTE */
936 Pfn1 = MI_PFN_ELEMENT(PageFrameIndex);
937 Pfn1->PteAddress = PointerPte;
938
939 /* Make this a software PTE */
940 MI_MAKE_SOFTWARE_PTE(&Pfn1->OriginalPte, MM_READWRITE);
941
942 /* Setup the page */
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;
949
950 /* Did we get a PFN for the page table */
951 if (PteFrame)
952 {
953 /* Store it */
954 Pfn1->u4.PteFrame = PteFrame;
955
956 /* Increase its share count so we don't get rid of it */
957 Pfn1 = MI_PFN_ELEMENT(PteFrame);
958 Pfn1->u2.ShareCount++;
959 }
960 }
961
962 /* EOF */