[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / mm / mminit.c
index 6f386d2..41732e3 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/*
  * PROJECT:         ReactOS Kernel
  * LICENSE:         GPL - See COPYING in the top level directory
  * FILE:            ntoskrnl/mm/mminit.c
@@ -31,6 +31,11 @@ UCHAR MmDisablePagingExecutive = 1; // Forced to off
 PMMPTE MmSharedUserDataPte;
 PMMSUPPORT MmKernelAddressSpace;
 
+extern KEVENT MmWaitPageEvent;
+extern FAST_MUTEX MiGlobalPageOperation;
+extern LIST_ENTRY MiSegmentList;
+extern NTSTATUS MiRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed);
+
 /* PRIVATE FUNCTIONS *********************************************************/
 
 VOID
@@ -39,10 +44,23 @@ NTAPI
 MiInitSystemMemoryAreas()
 {
     PVOID BaseAddress;
-    PHYSICAL_ADDRESS BoundaryAddressMultiple;
     PMEMORY_AREA MArea;
     NTSTATUS Status;
-    BoundaryAddressMultiple.QuadPart = 0;
+
+    //
+    // 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,
+                                &MArea,
+                                TRUE,
+                                0,
+                                PAGE_SIZE);
+    ASSERT(Status == STATUS_SUCCESS);
 
     //
     // Create the memory area to define the PTE base
@@ -51,12 +69,12 @@ MiInitSystemMemoryAreas()
     Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
                                 MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
                                 &BaseAddress,
-                                4 * 1024 * 1024,
+                                PTE_TOP - PTE_BASE + 1,
                                 PAGE_READWRITE,
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
     //
@@ -66,12 +84,12 @@ MiInitSystemMemoryAreas()
     Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
                                 MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
                                 &BaseAddress,
-                                4 * 1024 * 1024,
+                                HYPER_SPACE_END - HYPER_SPACE + 1,
                                 PAGE_READWRITE,
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
     //
@@ -86,7 +104,7 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
     //
@@ -101,7 +119,7 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
     //
@@ -111,13 +129,12 @@ MiInitSystemMemoryAreas()
     Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
                                 MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
                                 &BaseAddress,
-                                (ULONG_PTR)MmNonPagedPoolEnd -
-                                (ULONG_PTR)MmNonPagedSystemStart,
+                                MiNonPagedSystemSize,
                                 PAGE_READWRITE,
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
     //
@@ -132,7 +149,7 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
     //
@@ -148,7 +165,7 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
     //
@@ -163,9 +180,9 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
-
+#ifndef _M_AMD64
     //
     // Next, the KPCR
     //
@@ -178,9 +195,9 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
-
+#endif
     //
     // Now the KUSER_SHARED_DATA
     //
@@ -193,7 +210,7 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
     //
@@ -208,7 +225,7 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 
 #if defined(_X86_)
@@ -224,7 +241,7 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
 #endif
 }
@@ -238,8 +255,8 @@ MiDbgDumpAddressSpace(VOID)
     // Print the memory layout
     //
     DPRINT1("          0x%p - 0x%p\t%s\n",
-            MmSystemRangeStart,
-            (ULONG_PTR)MmSystemRangeStart + MmBootImageSize,
+            KSEG0_BASE,
+            (ULONG_PTR)KSEG0_BASE + MmBootImageSize,
             "Boot Loaded Image");
     DPRINT1("          0x%p - 0x%p\t%s\n",
             MmPfnDatabase,
@@ -248,7 +265,7 @@ MiDbgDumpAddressSpace(VOID)
     DPRINT1("          0x%p - 0x%p\t%s\n",
             MmNonPagedPoolStart,
             (ULONG_PTR)MmNonPagedPoolStart + MmSizeOfNonPagedPoolInBytes,
-            "ARM³ Non Paged Pool");
+            "ARM3 Non Paged Pool");
     DPRINT1("          0x%p - 0x%p\t%s\n",
             MiSystemViewStart,
             (ULONG_PTR)MiSystemViewStart + MmSystemViewSize,
@@ -258,18 +275,18 @@ MiDbgDumpAddressSpace(VOID)
             MiSessionSpaceEnd,
             "Session Space");
     DPRINT1("          0x%p - 0x%p\t%s\n",
-            PTE_BASE, PDE_BASE,
+            PTE_BASE, PTE_TOP,
             "Page Tables");
     DPRINT1("          0x%p - 0x%p\t%s\n",
-            PDE_BASE, HYPER_SPACE,
+            PDE_BASE, PDE_TOP,
             "Page Directories");
     DPRINT1("          0x%p - 0x%p\t%s\n",
-            HYPER_SPACE, HYPER_SPACE + (4 * 1024 * 1024),
+            HYPER_SPACE, HYPER_SPACE_END,
             "Hyperspace");
     DPRINT1("          0x%p - 0x%p\t%s\n",
             MmPagedPoolStart,
             (ULONG_PTR)MmPagedPoolStart + MmSizeOfPagedPoolInBytes,
-            "ARM³ Paged Pool");
+            "ARM3 Paged Pool");
     DPRINT1("          0x%p - 0x%p\t%s\n",
             MmNonPagedSystemStart, MmNonPagedPoolExpansionStart,
             "System PTE Space");
@@ -278,37 +295,40 @@ MiDbgDumpAddressSpace(VOID)
             "Non Paged Pool Expansion PTE Space");
 }
 
-NTSTATUS NTAPI
-MmMpwThreadMain(PVOID Ignored)
+VOID
+NTAPI
+MmMpwThreadMain(PVOID Parameter)
 {
-   NTSTATUS Status;
-   ULONG PagesWritten;
-   LARGE_INTEGER Timeout;
-
-   Timeout.QuadPart = -50000000;
-
-   for(;;)
-   {
-      Status = KeWaitForSingleObject(&MpwThreadEvent,
-                                     0,
-                                     KernelMode,
-                                     FALSE,
-                                     &Timeout);
-      if (!NT_SUCCESS(Status))
-      {
-         DbgPrint("MpwThread: Wait failed\n");
-         KeBugCheck(MEMORY_MANAGEMENT);
-         return(STATUS_UNSUCCESSFUL);
-      }
-
-      PagesWritten = 0;
+    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);
+        // XXX arty -- we flush when evicting pages or destorying cache
+        // sections.
+        CcRosFlushDirtyPages(128, &PagesWritten, FALSE);
 #endif
-   }
+    }
 }
 
 NTSTATUS
@@ -316,31 +336,31 @@ 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,
-                                 (PKSTART_ROUTINE) MmMpwThreadMain,
-                                 NULL);
-   if (!NT_SUCCESS(Status))
-   {
-      return(Status);
-   }
-
-   Priority = 27;
-   NtSetInformationThread(MpwThreadHandle,
-                          ThreadPriority,
-                          &Priority,
-                          sizeof(Priority));
-
-   return(STATUS_SUCCESS);
+    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
@@ -380,7 +400,14 @@ MmInitSystem(IN ULONG Phase,
 
     /* Initialize the kernel address space */
     ASSERT(Phase == 1);
-    KeInitializeGuardedMutex(&PsIdleProcess->AddressCreationLock);
+
+    InitializeListHead(&MiSegmentList);
+    ExInitializeFastMutex(&MiGlobalPageOperation);
+    KeInitializeEvent(&MmWaitPageEvent, SynchronizationEvent, FALSE);
+    // Until we're fully demand paged, we can do things the old way through
+    // the balance manager
+    MmInitializeMemoryConsumer(MC_CACHE, MiRosTrimCache);
+
     MmKernelAddressSpace = &PsIdleProcess->Vm;
 
     /* Intialize system memory areas */
@@ -393,7 +420,6 @@ MmInitSystem(IN ULONG Phase,
     MiInitializeUserPfnBitmap();
     MmInitializeMemoryConsumer(MC_USER, MmTrimUserMemory);
     MmInitializeRmapList();
-    MmInitializePageOp();
     MmInitSectionImplementation();
     MmInitPagingFile();
 
@@ -404,8 +430,8 @@ MmInitSystem(IN ULONG Phase,
     // by the fault handler.
     //
     MmSharedUserDataPte = ExAllocatePoolWithTag(PagedPool,
-                                                sizeof(MMPTE),
-                                                '  mM');
+                          sizeof(MMPTE),
+                          '  mM');
     if (!MmSharedUserDataPte) return FALSE;
 
     //
@@ -422,6 +448,12 @@ MmInitSystem(IN ULONG Phase,
                                 PageFrameNumber);
     *MmSharedUserDataPte = TempPte;
 
+    /* Initialize session working set support */
+    MiInitializeSessionWsSupport();
+
+    /* Setup session IDs */
+    MiInitializeSessionIds();
+
     /* Setup the memory threshold events */
     if (!MiInitializeMemoryEvents()) return FALSE;