Sync to trunk head (r42241)
[reactos.git] / reactos / ntoskrnl / include / internal / mm.h
index 9eab17b..eaea19e 100644 (file)
@@ -21,7 +21,6 @@ extern ULONG MmPagedPoolSize;
 extern PMEMORY_ALLOCATION_DESCRIPTOR MiFreeDescriptor;
 extern MEMORY_ALLOCATION_DESCRIPTOR MiFreeDescriptorOrg;
 extern ULONG MmHighestPhysicalPage;
-extern PVOID MmPfnDatabase;
 
 struct _KTRAP_FRAME;
 struct _EPROCESS;
@@ -30,6 +29,8 @@ struct _MM_PAGEOP;
 typedef ULONG SWAPENTRY;
 typedef ULONG PFN_TYPE, *PPFN_TYPE;
 
+#define MI_STATIC_MEMORY_AREAS              (8)
+
 #define MEMORY_AREA_INVALID                 (0)
 #define MEMORY_AREA_SECTION_VIEW            (1)
 #define MEMORY_AREA_CONTINUOUS_MEMORY       (2)
@@ -44,6 +45,7 @@ typedef ULONG PFN_TYPE, *PPFN_TYPE;
 #define MEMORY_AREA_PAGED_POOL              (12)
 #define MEMORY_AREA_NO_ACCESS               (13)
 #define MEMORY_AREA_PEB_OR_TEB              (14)
+#define MEMORY_AREA_STATIC                  (0x80000000)
 
 #define MM_PHYSICAL_PAGE_MPW_PENDING        (0x8)
 
@@ -59,9 +61,8 @@ typedef ULONG PFN_TYPE, *PPFN_TYPE;
 /* Number of list heads to use */
 #define MI_FREE_POOL_LISTS 4
 
-#define HYPER_SPACE                                (0xC0400000)
-
 #define MI_HYPERSPACE_PTES                  (256 - 1)
+#define MI_ZERO_PTES                        (32)
 #define MI_MAPPING_RANGE_START              (ULONG)HYPER_SPACE
 #define MI_MAPPING_RANGE_END                (MI_MAPPING_RANGE_START + \
                                              MI_HYPERSPACE_PTES * PAGE_SIZE)
@@ -103,7 +104,8 @@ typedef ULONG PFN_TYPE, *PPFN_TYPE;
 #define MC_USER                             (1)
 #define MC_PPOOL                            (2)
 #define MC_NPPOOL                           (3)
-#define MC_MAXIMUM                          (4)
+#define MC_SYSTEM                           (4)
+#define MC_MAXIMUM                          (5)
 
 #define PAGED_POOL_MASK                     1
 #define MUST_SUCCEED_POOL_MASK              2
@@ -289,31 +291,76 @@ typedef struct
     ULONG PagingRequestsInLastFifteenMinutes;
 } MM_STATS;
 
-typedef struct _PHYSICAL_PAGE
+//
+// These two mappings are actually used by Windows itself, based on the ASSERTS
+//
+#define StartOfAllocation ReadInProgress
+#define EndOfAllocation WriteInProgress
+
+typedef struct _MMPFNENTRY
+{
+    USHORT Modified:1;
+    USHORT ReadInProgress:1;                 // StartOfAllocation
+    USHORT WriteInProgress:1;                // EndOfAllocation
+    USHORT PrototypePte:1;                   // Zero
+    USHORT PageColor:4;                      // LockCount
+    USHORT PageLocation:3;                   // Consumer
+    USHORT RemovalRequested:1;
+    USHORT CacheAttribute:2;                 // Type
+    USHORT Rom:1;
+    USHORT ParityError:1;
+} MMPFNENTRY;
+
+typedef struct _MMPFN
 {
     union
     {
+        PFN_NUMBER Flink;                    // ListEntry.Flink
+        ULONG WsIndex;
+        PKEVENT Event;
+        NTSTATUS ReadStatus;
+        SINGLE_LIST_ENTRY NextStackPfn;
+    } u1;
+    PMMPTE PteAddress;                       // ListEntry.Blink
+    union
+    {
+        PFN_NUMBER Blink;
+        ULONG_PTR ShareCount;                // MapCount
+    } u2;
+    union
+    {
+        struct
+        {
+            USHORT ReferenceCount;           // ReferenceCount
+            MMPFNENTRY e1;
+        };
         struct
         {
-            ULONG Type: 2;
-            ULONG Consumer: 3;
-            ULONG Zero: 1;
-            ULONG StartOfAllocation: 1;
-            ULONG EndOfAllocation: 1;
-        }
-        Flags;
-        ULONG AllFlags;
+            USHORT ReferenceCount;
+            USHORT ShortFlags;
+        } e2;
+    } u3;
+    union
+    {
+        MMPTE OriginalPte;
+        LONG AweReferenceCount;              // RmapListHead
     };
-
-    LIST_ENTRY ListEntry;
-    ULONG ReferenceCount;
-    SWAPENTRY SavedSwapEntry;
-    ULONG LockCount;
-    ULONG MapCount;
-    struct _MM_RMAP_ENTRY* RmapListHead;
-}
-PHYSICAL_PAGE, *PPHYSICAL_PAGE;
-
+    union
+    {
+        ULONG_PTR EntireFrame;               // SavedSwapEntry
+        struct
+        {
+            ULONG_PTR PteFrame:25;
+            ULONG_PTR InPageError:1;
+            ULONG_PTR VerifierAllocation:1;
+            ULONG_PTR AweAllocation:1;
+            ULONG_PTR Priority:3;
+            ULONG_PTR MustBeCached:1;
+        };
+    } u4;
+} MMPFN, *PMMPFN;
+
+extern PMMPFN MmPfnDatabase;
 extern MM_STATS MmStats;
 
 typedef struct _MM_PAGEOP
@@ -388,7 +435,7 @@ extern MM_MEMORY_CONSUMER MiMemoryConsumers[MC_MAXIMUM];
 
 typedef VOID
 (*PMM_ALTER_REGION_FUNC)(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PVOID BaseAddress,
     ULONG Length,
     ULONG OldType,
@@ -412,7 +459,7 @@ typedef VOID
 NTSTATUS
 NTAPI
 MmCreateMemoryArea(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     ULONG Type,
     PVOID *BaseAddress,
     ULONG_PTR Length,
@@ -426,21 +473,21 @@ MmCreateMemoryArea(
 PMEMORY_AREA
 NTAPI
 MmLocateMemoryAreaByAddress(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PVOID Address
 );
 
 ULONG_PTR
 NTAPI
 MmFindGapAtAddress(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PVOID Address
 );
 
 NTSTATUS
 NTAPI
 MmFreeMemoryArea(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PMEMORY_AREA MemoryArea,
     PMM_FREE_PAGE_FUNC FreePage,
     PVOID FreePageContext
@@ -449,7 +496,7 @@ MmFreeMemoryArea(
 NTSTATUS
 NTAPI
 MmFreeMemoryAreaByPtr(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PVOID BaseAddress,
     PMM_FREE_PAGE_FUNC FreePage,
     PVOID FreePageContext
@@ -457,12 +504,12 @@ MmFreeMemoryAreaByPtr(
 
 VOID
 NTAPI
-MmDumpMemoryAreas(PMM_AVL_TABLE AddressSpace);
+MmDumpMemoryAreas(PMMSUPPORT AddressSpace);
 
 PMEMORY_AREA
 NTAPI
 MmLocateMemoryAreaByRegion(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PVOID Address,
     ULONG_PTR Length
 );
@@ -470,7 +517,7 @@ MmLocateMemoryAreaByRegion(
 PVOID
 NTAPI
 MmFindGap(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     ULONG_PTR Length,
     ULONG_PTR Granularity,
     BOOLEAN TopDown
@@ -480,7 +527,7 @@ VOID
 NTAPI
 MmReleaseMemoryAreaIfDecommitted(
     struct _EPROCESS *Process,
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PVOID BaseAddress
 );
 
@@ -558,6 +605,24 @@ VOID
 NTAPI
 ExFreePagedPool(IN PVOID Block);
 
+BOOLEAN
+NTAPI
+ExpIsPoolTagDebuggable(ULONG Tag);
+
+PVOID
+NTAPI
+ExpAllocateDebugPool(
+    POOL_TYPE Type,
+    ULONG Size,
+    ULONG Tag,
+    PVOID Caller,
+    BOOLEAN EndOfPage
+);
+
+VOID
+NTAPI
+ExpFreeDebugPool(PVOID Block, BOOLEAN PagedPool);
+
 VOID
 NTAPI
 MmInitializePagedPool(VOID);
@@ -746,7 +811,7 @@ MmAccessFault(
 NTSTATUS
 NTAPI
 MmNotPresentFaultVirtualMemory(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     MEMORY_AREA* MemoryArea,
     PVOID Address,
     BOOLEAN Locked
@@ -755,7 +820,7 @@ MmNotPresentFaultVirtualMemory(
 NTSTATUS
 NTAPI
 MmPageOutVirtualMemory(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PMEMORY_AREA MemoryArea,
     PVOID Address,
     struct _MM_PAGEOP* PageOp
@@ -780,7 +845,7 @@ MmFreeVirtualMemory(
 NTSTATUS
 NTAPI
 MmProtectAnonMem(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PMEMORY_AREA MemoryArea,
     PVOID BaseAddress,
     ULONG Length,
@@ -791,7 +856,7 @@ MmProtectAnonMem(
 NTSTATUS
 NTAPI
 MmWritePageVirtualMemory(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PMEMORY_AREA MArea,
     PVOID Address,
     PMM_PAGEOP PageOp
@@ -973,24 +1038,23 @@ MmPageOutPhysicalAddress(PFN_TYPE Page);
 
 /* freelist.c **********************************************************/
 
-#define ASSERT_PFN(x) ASSERT((x)->Flags.Type != 0)
+#define ASSERT_PFN(x) ASSERT((x)->u3.e1.CacheAttribute != 0)
 
 FORCEINLINE
-PPHYSICAL_PAGE
+PMMPFN
 MiGetPfnEntry(IN PFN_TYPE Pfn)
 {
-    PPHYSICAL_PAGE Page;
-    extern PPHYSICAL_PAGE MmPageArray;
-    extern ULONG MmPageArraySize;
-
-    /* Mark MmPageArraySize as unreferenced, otherwise it will appear as an unused variable on a Release build */
-    UNREFERENCED_PARAMETER(MmPageArraySize);
+    PMMPFN Page;
+    extern RTL_BITMAP MiPfnBitMap;
 
     /* Make sure the PFN number is valid */
-    ASSERT(Pfn <= MmPageArraySize);
+    if (Pfn > MmHighestPhysicalPage) return NULL;
+    
+    /* Make sure this page actually has a PFN entry */
+    if ((MiPfnBitMap.Buffer) && !(RtlTestBit(&MiPfnBitMap, Pfn))) return NULL;
 
     /* Get the entry */
-    Page = &MmPageArray[Pfn];
+    Page = &MmPfnDatabase[Pfn];
 
     /* Make sure it's valid */
     ASSERT_PFN(Page);
@@ -999,6 +1063,16 @@ MiGetPfnEntry(IN PFN_TYPE Pfn)
     return Page;
 };
 
+FORCEINLINE
+PFN_NUMBER
+MiGetPfnEntryIndex(IN PMMPFN Pfn1)
+{
+    //
+    // This will return the Page Frame Number (PFN) from the MMPFN
+    //
+    return Pfn1 - MmPfnDatabase;
+}
+
 PFN_TYPE
 NTAPI
 MmGetLRUNextUserPage(PFN_TYPE PreviousPage);
@@ -1054,13 +1128,20 @@ MmInitializePageList(
     VOID
 );
 
+VOID
+NTAPI
+MmDumpPfnDatabase(
+   VOID
+);
+
 PFN_TYPE
 NTAPI
 MmGetContinuousPages(
     ULONG NumberOfBytes,
     PHYSICAL_ADDRESS LowestAcceptableAddress,
     PHYSICAL_ADDRESS HighestAcceptableAddress,
-    PHYSICAL_ADDRESS BoundaryAddressMultiple
+    PHYSICAL_ADDRESS BoundaryAddressMultiple,
+    BOOLEAN ZeroPages
 );
 
 NTSTATUS
@@ -1088,19 +1169,33 @@ MiUnmapPageInHyperSpace(IN PEPROCESS Process,
 
 PVOID
 NTAPI
-MiMapPageToZeroInHyperSpace(IN PFN_NUMBER Page);
+MiMapPagesToZeroInHyperSpace(IN PMMPFN *Pages,
+                             IN PFN_NUMBER NumberOfPages);
+
+VOID
+NTAPI
+MiUnmapPagesInZeroSpace(IN PVOID VirtualAddress,
+                        IN PFN_NUMBER NumberOfPages);
 
 //
 // ReactOS Compatibility Layer
 //
-PVOID
 FORCEINLINE
+PVOID
 MmCreateHyperspaceMapping(IN PFN_NUMBER Page)
 {
     HyperProcess = (PEPROCESS)KeGetCurrentThread()->ApcState.Process;
     return MiMapPageInHyperSpace(HyperProcess, Page, &HyperIrql);
 }
 
+FORCEINLINE
+PVOID
+MiMapPageToZeroInHyperSpace(IN PFN_NUMBER Page)
+{
+    PMMPFN Pfn1 = MiGetPfnEntry(Page);
+    return MiMapPagesToZeroInHyperSpace(&Pfn1, 1);
+}
+
 #define MmDeleteHyperspaceMapping(x) MiUnmapPageInHyperSpace(HyperProcess, x, HyperIrql);
 
 /* i386/page.c *********************************************************/
@@ -1260,16 +1355,6 @@ BOOLEAN
 NTAPI
 MmIsPageInUse(PFN_TYPE Page);
 
-VOID
-NTAPI
-MmSetFlagsPage(
-    PFN_TYPE Page,
-    ULONG Flags);
-
-ULONG
-NTAPI
-MmGetFlagsPage(PFN_TYPE Page);
-
 VOID
 NTAPI
 MmSetSavedSwapEntryPage(
@@ -1390,7 +1475,7 @@ MmTrimUserMemory(
 NTSTATUS
 NTAPI
 MmAlterRegion(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PVOID BaseAddress,
     PLIST_ENTRY RegionListHead,
     PVOID StartAddress,
@@ -1458,7 +1543,7 @@ MmQuerySectionView(
 NTSTATUS
 NTAPI
 MmProtectSectionView(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PMEMORY_AREA MemoryArea,
     PVOID BaseAddress,
     ULONG Length,
@@ -1469,7 +1554,7 @@ MmProtectSectionView(
 NTSTATUS
 NTAPI
 MmWritePageSectionView(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PMEMORY_AREA MArea,
     PVOID Address,
     PMM_PAGEOP PageOp
@@ -1482,7 +1567,7 @@ MmInitSectionImplementation(VOID);
 NTSTATUS
 NTAPI
 MmNotPresentFaultSectionView(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     MEMORY_AREA* MemoryArea,
     PVOID Address,
     BOOLEAN Locked
@@ -1491,7 +1576,7 @@ MmNotPresentFaultSectionView(
 NTSTATUS
 NTAPI
 MmPageOutSectionView(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     PMEMORY_AREA MemoryArea,
     PVOID Address,
     struct _MM_PAGEOP *PageOp
@@ -1504,7 +1589,7 @@ MmCreatePhysicalMemorySection(VOID);
 NTSTATUS
 NTAPI
 MmAccessFaultSectionView(
-    PMM_AVL_TABLE AddressSpace,
+    PMMSUPPORT AddressSpace,
     MEMORY_AREA* MemoryArea,
     PVOID Address,
     BOOLEAN Locked
@@ -1608,39 +1693,39 @@ MiSyncForContextSwitch(
     IN PKTHREAD Thread
 );
 
-extern PMM_AVL_TABLE MmKernelAddressSpace;
+extern PMMSUPPORT MmKernelAddressSpace;
 
 FORCEINLINE
 VOID
-MmLockAddressSpace(PMM_AVL_TABLE AddressSpace)
+MmLockAddressSpace(PMMSUPPORT AddressSpace)
 {
-    KeAcquireGuardedMutex(&CONTAINING_RECORD(AddressSpace, EPROCESS, VadRoot)->AddressCreationLock);
+    KeAcquireGuardedMutex(&CONTAINING_RECORD(AddressSpace, EPROCESS, Vm)->AddressCreationLock);
 }
 
 FORCEINLINE
 VOID
-MmUnlockAddressSpace(PMM_AVL_TABLE AddressSpace)
+MmUnlockAddressSpace(PMMSUPPORT AddressSpace)
 {
-    KeReleaseGuardedMutex(&CONTAINING_RECORD(AddressSpace, EPROCESS, VadRoot)->AddressCreationLock);
+    KeReleaseGuardedMutex(&CONTAINING_RECORD(AddressSpace, EPROCESS, Vm)->AddressCreationLock);
 }
 
 FORCEINLINE
 PEPROCESS
-MmGetAddressSpaceOwner(IN PMM_AVL_TABLE AddressSpace)
+MmGetAddressSpaceOwner(IN PMMSUPPORT AddressSpace)
 {
     if (AddressSpace == MmKernelAddressSpace) return NULL;
-    return CONTAINING_RECORD(AddressSpace, EPROCESS, VadRoot);
+    return CONTAINING_RECORD(AddressSpace, EPROCESS, Vm);
 }
 
 FORCEINLINE
-PMM_AVL_TABLE
+PMMSUPPORT
 MmGetCurrentAddressSpace(VOID)
 {
-    return &((PEPROCESS)KeGetCurrentThread()->ApcState.Process)->VadRoot;
+    return &((PEPROCESS)KeGetCurrentThread()->ApcState.Process)->Vm;
 }
 
 FORCEINLINE
-PMM_AVL_TABLE
+PMMSUPPORT
 MmGetKernelAddressSpace(VOID)
 {
     return MmKernelAddressSpace;