[RSHELL]
[reactos.git] / ntoskrnl / mm / mminit.c
index 1ad5b5c..910f397 100644 (file)
@@ -1,9 +1,9 @@
-/*
+/*
  * PROJECT:         ReactOS Kernel
  * LICENSE:         GPL - See COPYING in the top level directory
  * FILE:            ntoskrnl/mm/mminit.c
  * PURPOSE:         Memory Manager Initialization
- * PROGRAMMERS:     
+ * PROGRAMMERS:
  */
 
 /* INCLUDES ******************************************************************/
@@ -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,11 +44,24 @@ 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,14 +69,14 @@ 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);
-    
+
     //
     // Create the memory area to define Hyperspace
     //
@@ -66,14 +84,14 @@ 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);
-    
+
     //
     // Protect the PFN database
     //
@@ -86,9 +104,9 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
-    
+
     //
     // ReactOS requires a memory area to keep the initial NP area off-bounds
     //
@@ -101,9 +119,9 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
-    
+
     //
     // And we need one more for the system NP
     //
@@ -111,15 +129,14 @@ 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);
-    
+
     //
     // We also need one for system view space
     //
@@ -132,9 +149,9 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
-    
+
     //
     // And another for session space
     //
@@ -148,9 +165,9 @@ MiInitSystemMemoryAreas()
                                 &MArea,
                                 TRUE,
                                 0,
-                                BoundaryAddressMultiple);
+                                PAGE_SIZE);
     ASSERT(Status == STATUS_SUCCESS);
-    
+
     //
     // One more for ARM paged pool
     //
@@ -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,28 +265,28 @@ 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,
-            "System View Space");        
+            "System View Space");
     DPRINT1("          0x%p - 0x%p\t%s\n",
             MmSessionBase,
             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,13 +295,16 @@ 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;
 
+   UNREFERENCED_PARAMETER(Parameter);
+
    Timeout.QuadPart = -50000000;
 
    for(;;)
@@ -298,15 +318,15 @@ MmMpwThreadMain(PVOID Ignored)
       {
          DbgPrint("MpwThread: Wait failed\n");
          KeBugCheck(MEMORY_MANAGEMENT);
-         return(STATUS_UNSUCCESSFUL);
+         return;
       }
 
       PagesWritten = 0;
 
 #ifndef NEWCC
-         // XXX arty -- we flush when evicting pages or destorying cache
-         // sections.
-      CcRosFlushDirtyPages(128, &PagesWritten);
+      // XXX arty -- we flush when evicting pages or destorying cache
+      // sections.
+      CcRosFlushDirtyPages(128, &PagesWritten, FALSE);
 #endif
    }
 }
@@ -319,7 +339,7 @@ MmInitMpwThread(VOID)
    KPRIORITY Priority;
    NTSTATUS Status;
    CLIENT_ID MpwThreadId;
-   
+
    KeInitializeEvent(&MpwThreadEvent, SynchronizationEvent, FALSE);
 
    Status = PsCreateSystemThread(&MpwThreadHandle,
@@ -327,7 +347,7 @@ MmInitMpwThread(VOID)
                                  NULL,
                                  NULL,
                                  &MpwThreadId,
-                                 (PKSTART_ROUTINE) MmMpwThreadMain,
+                                 MmMpwThreadMain,
                                  NULL);
    if (!NT_SUCCESS(Status))
    {
@@ -377,10 +397,17 @@ MmInitSystem(IN ULONG Phase,
     PMMPTE PointerPte;
     MMPTE TempPte = ValidKernelPte;
     PFN_NUMBER PageFrameNumber;
-    
+
     /* 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,10 +420,9 @@ MmInitSystem(IN ULONG Phase,
     MiInitializeUserPfnBitmap();
     MmInitializeMemoryConsumer(MC_USER, MmTrimUserMemory);
     MmInitializeRmapList();
-    MmInitializePageOp();
     MmInitSectionImplementation();
     MmInitPagingFile();
-    
+
     //
     // Create a PTE to double-map the shared data section. We allocate it
     // from paged pool so that we can't fault when trying to touch the PTE
@@ -407,34 +433,40 @@ MmInitSystem(IN ULONG Phase,
                                                 sizeof(MMPTE),
                                                 '  mM');
     if (!MmSharedUserDataPte) return FALSE;
-    
+
     //
     // Now get the PTE for shared data, and read the PFN that holds it
     //
     PointerPte = MiAddressToPte((PVOID)KI_USER_SHARED_DATA);
     ASSERT(PointerPte->u.Hard.Valid == 1);
     PageFrameNumber = PFN_FROM_PTE(PointerPte);
-    
+
     /* Build the PTE and write it */
     MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte,
                                 PointerPte,
                                 MM_READONLY,
                                 PageFrameNumber);
     *MmSharedUserDataPte = TempPte;
-    
+
+    /* Initialize session working set support */
+    MiInitializeSessionWsSupport();
+
+    /* Setup session IDs */
+    MiInitializeSessionIds();
+
     /* Setup the memory threshold events */
     if (!MiInitializeMemoryEvents()) return FALSE;
-    
+
     /*
      * Unmap low memory
      */
     MiInitBalancerThread();
-    
+
     /*
      * Initialise the modified page writer.
      */
     MmInitMpwThread();
-    
+
     /* Initialize the balance set manager */
     MmInitBsmThread();