[NTOS]: This is why you shouldn't let Antoine Dodson commit code.
[reactos.git] / reactos / ntoskrnl / mm / ARM3 / pool.c
index 2e08950..8156f67 100644 (file)
@@ -298,6 +298,7 @@ MiInitializeNonPagedPool(VOID)
     FreeEntry = MmNonPagedPoolStart;
     FirstEntry = FreeEntry;
     FreeEntry->Size = PoolPages;
+    FreeEntry->Signature = MM_FREE_POOL_SIGNATURE;
     FreeEntry->Owner = FirstEntry;
 
     //
@@ -316,6 +317,7 @@ MiInitializeNonPagedPool(VOID)
         //
         FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)FreeEntry + PAGE_SIZE);
         FreeEntry->Owner = FirstEntry;
+        FreeEntry->Signature = MM_FREE_POOL_SIGNATURE;
     }
 
     //
@@ -375,7 +377,10 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
     KIRQL OldIrql;
     PLIST_ENTRY NextEntry, NextHead, LastHead;
     PMMPTE PointerPte, StartPte;
+    PMMPDE PointerPde;
+    ULONG EndAllocation;
     MMPTE TempPte;
+    MMPDE TempPde;
     PMMPFN Pfn1;
     PVOID BaseVa, BaseVaStart;
     PMMFREE_POOL_ENTRY FreeEntry;
@@ -407,7 +412,7 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
             //
             // Get the page bit count
             //
-            i = ((SizeInPages - 1) / 1024) + 1;
+            i = ((SizeInPages - 1) / PTE_COUNT) + 1;
             DPRINT1("Paged pool expansion: %d %x\n", i, SizeInPages);
             
             //
@@ -448,15 +453,15 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
             }
             
             //
-            // Get the template PTE we'll use to expand
+            // Get the template PDE we'll use to expand
             //
-            TempPte = ValidKernelPte;
+            TempPde = ValidKernelPde;
             
             //
             // Get the first PTE in expansion space
             //
-            PointerPte = MmPagedPoolInfo.NextPdeForPagedPoolExpansion;
-            BaseVa = MiPteToAddress(PointerPte);
+            PointerPde = MmPagedPoolInfo.NextPdeForPagedPoolExpansion;
+            BaseVa = MiPteToAddress(PointerPde);
             BaseVaStart = BaseVa;
             
             //
@@ -468,11 +473,13 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
                 //
                 // It should not already be valid
                 //
-                ASSERT(PointerPte->u.Hard.Valid == 0);
+                ASSERT(PointerPde->u.Hard.Valid == 0);
                 
                 /* Request a page */
-                PageFrameNumber = MiRemoveAnyPage(0);
-                TempPte.u.Hard.PageFrameNumber = PageFrameNumber;
+                DPRINT1("Requesting %d PDEs\n", i);
+                PageFrameNumber = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
+                TempPde.u.Hard.PageFrameNumber = PageFrameNumber;
+                DPRINT1("We have a PDE: %lx\n", PageFrameNumber);
 
 #if (_MI_PAGING_LEVELS >= 3)
                 /* On PAE/x64 systems, there's no double-buffering */
@@ -481,38 +488,38 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
                 //
                 // Save it into our double-buffered system page directory
                 //
-                /* This seems to be making the assumption that one PDE is one page long */
-                C_ASSERT(PAGE_SIZE == (PD_COUNT * (sizeof(MMPTE) * PDE_COUNT)));
-                MmSystemPagePtes[(ULONG_PTR)PointerPte & (PAGE_SIZE - 1) /
-                                 sizeof(MMPTE)] = TempPte;
-                            
+                MmSystemPagePtes[((ULONG_PTR)PointerPde & (SYSTEM_PD_SIZE - 1)) / sizeof(MMPTE)] = TempPde;
+                                            
                 /* Initialize the PFN */
                 MiInitializePfnForOtherProcess(PageFrameNumber,
-                                               PointerPte,
-                                               MmSystemPageDirectory[(PointerPte - (PMMPTE)PDE_BASE) / PDE_COUNT]);
+                                               PointerPde,
+                                               MmSystemPageDirectory[(PointerPde - MiAddressToPde(NULL)) / PDE_COUNT]);
                              
-                /* Write the actual PTE now */
-                MI_WRITE_VALID_PTE(PointerPte++, TempPte);
+                /* Write the actual PDE now */
+                MI_WRITE_VALID_PTE(PointerPde, TempPde);
 #endif                
                 //
                 // Move on to the next expansion address
                 //
+                PointerPde++;
                 BaseVa = (PVOID)((ULONG_PTR)BaseVa + PAGE_SIZE);
-            } while (--i > 0);
+                i--;
+            } while (i > 0);
             
             //
             // Release the PFN database lock
             //            
             KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
-            
+                        
             //
             // These pages are now available, clear their availablity bits
             //
+            EndAllocation = (MmPagedPoolInfo.NextPdeForPagedPoolExpansion -
+                             MiAddressToPte(MmPagedPoolInfo.FirstPteForPagedPool)) *
+                             PTE_COUNT;
             RtlClearBits(MmPagedPoolInfo.PagedPoolAllocationMap,
-                         (MmPagedPoolInfo.NextPdeForPagedPoolExpansion -
-                          MiAddressToPte(MmPagedPoolInfo.FirstPteForPagedPool)) *
-                         1024,
-                         SizeInPages * 1024);
+                         EndAllocation,
+                         SizeInPages * PTE_COUNT);
                         
             //
             // Update the next expansion location
@@ -551,7 +558,8 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
         // Update the end bitmap so we know the bounds of this allocation when
         // the time comes to free it
         //
-        RtlSetBit(MmPagedPoolInfo.EndOfPagedPoolBitmap, i + SizeInPages - 1);
+        EndAllocation = i + SizeInPages - 1;
+        RtlSetBit(MmPagedPoolInfo.EndOfPagedPoolBitmap, EndAllocation);
         
         //
         // Now we can release the lock (it mainly protects the bitmap)
@@ -581,9 +589,8 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
             //
             // Write the demand zero PTE and keep going
             //
-            ASSERT(PointerPte->u.Hard.Valid == 0);
-            *PointerPte++ = TempPte;
-        } while (PointerPte < StartPte);
+            MI_WRITE_INVALID_PTE(PointerPte, TempPte);
+        } while (++PointerPte < StartPte);
         
         //
         // Return the allocation address to the caller
@@ -626,6 +633,7 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
             // Grab the entry and see if it can handle our allocation
             //
             FreeEntry = CONTAINING_RECORD(NextEntry, MMFREE_POOL_ENTRY, List);
+            ASSERT(FreeEntry->Signature == MM_FREE_POOL_SIGNATURE);
             if (FreeEntry->Size >= SizeInPages)
             {
                 //
@@ -683,6 +691,10 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
                 ASSERT(Pfn1->u3.e1.StartOfAllocation == 0);
                 Pfn1->u3.e1.StartOfAllocation = 1;
                 
+                /* Mark it as special pool if needed */
+                ASSERT(Pfn1->u4.VerifierAllocation == 0);
+                if (PoolType & 64) Pfn1->u4.VerifierAllocation = 1;
+                
                 //
                 // Check if the allocation is larger than one page
                 //
@@ -761,7 +773,7 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
     do
     {
         /* Allocate a page */
-        PageFrameNumber = MiRemoveAnyPage(0);
+        PageFrameNumber = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
         
         /* Get the PFN entry for it and fill it out */
         Pfn1 = MiGetPfnEntry(PageFrameNumber);
@@ -787,6 +799,10 @@ MiAllocatePoolPages(IN POOL_TYPE PoolType,
     Pfn1 = MiGetPfnEntry(StartPte->u.Hard.PageFrameNumber);
     Pfn1->u3.e1.StartOfAllocation = 1;
     
+    /* Mark it as a verifier allocation if needed */
+    ASSERT(Pfn1->u4.VerifierAllocation == 0);
+    if (PoolType & 64) Pfn1->u4.VerifierAllocation = 1;
+    
     //
     // Release the PFN and nonpaged pool lock
     //
@@ -964,6 +980,7 @@ MiFreePoolPages(IN PVOID StartingVa)
         //
         FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)StartingVa +
                                          (NumberOfPages << PAGE_SHIFT));
+        ASSERT(FreeEntry->Signature == MM_FREE_POOL_SIGNATURE);
         ASSERT(FreeEntry->Owner == FreeEntry);
         
         /* Consume this entry's pages */
@@ -1032,6 +1049,7 @@ MiFreePoolPages(IN PVOID StartingVa)
         // Get the free entry descriptor for that given page range
         //
         FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)StartingVa - PAGE_SIZE);
+        ASSERT(FreeEntry->Signature == MM_FREE_POOL_SIGNATURE);
         FreeEntry = FreeEntry->Owner;
         
         /* Check if protected pool is enabled */
@@ -1118,6 +1136,7 @@ MiFreePoolPages(IN PVOID StartingVa)
         // Link back to the parent free entry, and keep going
         //
         NextEntry->Owner = FreeEntry;
+        NextEntry->Signature = MM_FREE_POOL_SIGNATURE;
         NextEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)NextEntry + PAGE_SIZE);
     } while (NextEntry != LastEntry);