[NTOSKRNL] Define the Mm global variables related to Cc on Mm init.
[reactos.git] / ntoskrnl / mm / mminit.c
index 910f397..e0166ee 100644 (file)
@@ -19,9 +19,6 @@
 
 VOID NTAPI MiInitializeUserPfnBitmap(VOID);
 
-HANDLE MpwThreadHandle;
-KEVENT MpwThreadEvent;
-
 BOOLEAN Mm64BitPhysicalAddress = FALSE;
 ULONG MmReadClusterSize;
 //
@@ -38,212 +35,86 @@ extern NTSTATUS MiRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed);
 
 /* PRIVATE FUNCTIONS *********************************************************/
 
+//
+// Helper function to create initial memory areas.
+// The created area is always read/write.
+//
 VOID
 INIT_FUNCTION
 NTAPI
-MiInitSystemMemoryAreas()
+MiCreateArm3StaticMemoryArea(PVOID BaseAddress, SIZE_T Size, BOOLEAN Executable)
 {
-    PVOID BaseAddress;
+    const ULONG Protection = Executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
+    PVOID pBaseAddress = BaseAddress;
     PMEMORY_AREA MArea;
     NTSTATUS Status;
 
-    //
-    // Create the memory area to define the loader mappings
-    //
-    BaseAddress = (PVOID)KSEG0_BASE;
     Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
                                 MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                MmBootImageSize,
-                                PAGE_EXECUTE_READWRITE,
+                                &pBaseAddress,
+                                Size,
+                                Protection,
                                 &MArea,
-                                TRUE,
                                 0,
                                 PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
+    // TODO: Perhaps it would be  prudent to bugcheck here, not only assert?
+}
 
+VOID
+INIT_FUNCTION
+NTAPI
+MiInitSystemMemoryAreas(VOID)
+{
     //
-    // Create the memory area to define the PTE base
+    // Create all the static memory areas.
     //
-    BaseAddress = (PVOID)PTE_BASE;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                PTE_TOP - PTE_BASE + 1,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
 
-    //
-    // Create the memory area to define Hyperspace
-    //
-    BaseAddress = (PVOID)HYPER_SPACE;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                HYPER_SPACE_END - HYPER_SPACE + 1,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
+    // The loader mappings. The only Executable area.
+    MiCreateArm3StaticMemoryArea((PVOID)KSEG0_BASE, MmBootImageSize, TRUE);
+
+    // The PTE base
+    MiCreateArm3StaticMemoryArea((PVOID)PTE_BASE, PTE_TOP - PTE_BASE + 1, FALSE);
+
+    // Hyperspace
+    MiCreateArm3StaticMemoryArea((PVOID)HYPER_SPACE, HYPER_SPACE_END - HYPER_SPACE + 1, FALSE);
 
-    //
     // Protect the PFN database
-    //
-    BaseAddress = MmPfnDatabase;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                (MxPfnAllocation << PAGE_SHIFT),
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
+    MiCreateArm3StaticMemoryArea(MmPfnDatabase, (MxPfnAllocation << PAGE_SHIFT), FALSE);
 
-    //
     // ReactOS requires a memory area to keep the initial NP area off-bounds
-    //
-    BaseAddress = MmNonPagedPoolStart;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                MmSizeOfNonPagedPoolInBytes,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
+    MiCreateArm3StaticMemoryArea(MmNonPagedPoolStart, MmSizeOfNonPagedPoolInBytes, FALSE);
 
-    //
-    // And we need one more for the system NP
-    //
-    BaseAddress = MmNonPagedSystemStart;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                MiNonPagedSystemSize,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
+    // System PTE space
+    MiCreateArm3StaticMemoryArea(MmNonPagedSystemStart, (MmNumberOfSystemPtes + 1) * PAGE_SIZE, FALSE);
 
-    //
-    // We also need one for system view space
-    //
-    BaseAddress = MiSystemViewStart;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                MmSystemViewSize,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
+    // Nonpaged pool expansion space
+    MiCreateArm3StaticMemoryArea(MmNonPagedPoolExpansionStart, (ULONG_PTR)MmNonPagedPoolEnd - (ULONG_PTR)MmNonPagedPoolExpansionStart, FALSE);
 
-    //
-    // And another for session space
-    //
-    BaseAddress = MmSessionBase;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                (ULONG_PTR)MiSessionSpaceEnd -
-                                (ULONG_PTR)MmSessionBase,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
+    // System view space
+    MiCreateArm3StaticMemoryArea(MiSystemViewStart, MmSystemViewSize, FALSE);
 
-    //
-    // One more for ARM paged pool
-    //
-    BaseAddress = MmPagedPoolStart;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                MmSizeOfPagedPoolInBytes,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
-#ifndef _M_AMD64
-    //
-    // Next, the KPCR
-    //
-    BaseAddress = (PVOID)PCR;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                PAGE_SIZE * KeNumberProcessors,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
-#endif
-    //
-    // Now the KUSER_SHARED_DATA
-    //
-    BaseAddress = (PVOID)KI_USER_SHARED_DATA;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                PAGE_SIZE,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
+    // Session space
+    MiCreateArm3StaticMemoryArea(MmSessionBase, (ULONG_PTR)MiSessionSpaceEnd - (ULONG_PTR)MmSessionBase, FALSE);
 
-    //
-    // And the debugger mapping
-    //
-    BaseAddress = MI_DEBUG_MAPPING;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                PAGE_SIZE,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
+    // Paged pool
+    MiCreateArm3StaticMemoryArea(MmPagedPoolStart, MmSizeOfPagedPoolInBytes, FALSE);
+
+    // Debugger mapping
+    MiCreateArm3StaticMemoryArea(MI_DEBUG_MAPPING, PAGE_SIZE, FALSE);
 
 #if defined(_X86_)
-    //
-    // Finally, reserve the 2 pages we currently make use of for HAL mappings
-    //
-    BaseAddress = (PVOID)0xFFC00000;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
-                                &BaseAddress,
-                                PAGE_SIZE * 2,
-                                PAGE_READWRITE,
-                                &MArea,
-                                TRUE,
-                                0,
-                                PAGE_SIZE);
-    ASSERT(Status == STATUS_SUCCESS);
-#endif
+    // Reserved HAL area (includes KUSER_SHARED_DATA and KPCR)
+    MiCreateArm3StaticMemoryArea((PVOID)MM_HAL_VA_START, MM_HAL_VA_END - MM_HAL_VA_START + 1, FALSE);
+#else /* _X86_ */
+#ifndef _M_AMD64
+    // KPCR, one page per CPU. Only for 32-bit kernel.
+    MiCreateArm3StaticMemoryArea(PCR, PAGE_SIZE * KeNumberProcessors, FALSE);
+#endif /* _M_AMD64 */
+
+    // KUSER_SHARED_DATA
+    MiCreateArm3StaticMemoryArea((PVOID)KI_USER_SHARED_DATA, PAGE_SIZE, FALSE);
+#endif /* _X86_ */
 }
 
 VOID
@@ -283,6 +154,9 @@ MiDbgDumpAddressSpace(VOID)
     DPRINT1("          0x%p - 0x%p\t%s\n",
             HYPER_SPACE, HYPER_SPACE_END,
             "Hyperspace");
+    DPRINT1("          0x%p - 0x%p\t%s\n",
+            MmSystemCacheStart, MmSystemCacheEnd,
+            "System Cache");
     DPRINT1("          0x%p - 0x%p\t%s\n",
             MmPagedPoolStart,
             (ULONG_PTR)MmPagedPoolStart + MmSizeOfPagedPoolInBytes,
@@ -295,74 +169,6 @@ MiDbgDumpAddressSpace(VOID)
             "Non Paged Pool Expansion PTE Space");
 }
 
-VOID
-NTAPI
-MmMpwThreadMain(PVOID Parameter)
-{
-   NTSTATUS Status;
-   ULONG PagesWritten;
-   LARGE_INTEGER Timeout;
-
-   UNREFERENCED_PARAMETER(Parameter);
-
-   Timeout.QuadPart = -50000000;
-
-   for(;;)
-   {
-      Status = KeWaitForSingleObject(&MpwThreadEvent,
-                                     0,
-                                     KernelMode,
-                                     FALSE,
-                                     &Timeout);
-      if (!NT_SUCCESS(Status))
-      {
-         DbgPrint("MpwThread: Wait failed\n");
-         KeBugCheck(MEMORY_MANAGEMENT);
-         return;
-      }
-
-      PagesWritten = 0;
-
-#ifndef NEWCC
-      // XXX arty -- we flush when evicting pages or destorying cache
-      // sections.
-      CcRosFlushDirtyPages(128, &PagesWritten, FALSE);
-#endif
-   }
-}
-
-NTSTATUS
-NTAPI
-INIT_FUNCTION
-MmInitMpwThread(VOID)
-{
-   KPRIORITY Priority;
-   NTSTATUS Status;
-   CLIENT_ID MpwThreadId;
-
-   KeInitializeEvent(&MpwThreadEvent, SynchronizationEvent, FALSE);
-
-   Status = PsCreateSystemThread(&MpwThreadHandle,
-                                 THREAD_ALL_ACCESS,
-                                 NULL,
-                                 NULL,
-                                 &MpwThreadId,
-                                 MmMpwThreadMain,
-                                 NULL);
-   if (!NT_SUCCESS(Status))
-   {
-      return(Status);
-   }
-
-   Priority = 27;
-   NtSetInformationThread(MpwThreadHandle,
-                          ThreadPriority,
-                          &Priority,
-                          sizeof(Priority));
-
-   return(STATUS_SUCCESS);
-}
-
 NTSTATUS
 NTAPI
 INIT_FUNCTION
@@ -430,8 +236,8 @@ MmInitSystem(IN ULONG Phase,
     // by the fault handler.
     //
     MmSharedUserDataPte = ExAllocatePoolWithTag(PagedPool,
-                                                sizeof(MMPTE),
-                                                '  mM');
+                          sizeof(MMPTE),
+                          TAG_MM);
     if (!MmSharedUserDataPte) return FALSE;
 
     //
@@ -462,11 +268,6 @@ MmInitSystem(IN ULONG Phase,
      */
     MiInitBalancerThread();
 
-    /*
-     * Initialise the modified page writer.
-     */
-    MmInitMpwThread();
-
     /* Initialize the balance set manager */
     MmInitBsmThread();