[ros-diffs] [akorotaev] 31567: Merge changes from HEAD. It still doesn't link.

akorotaev at svn.reactos.org akorotaev at svn.reactos.org
Wed Jan 2 20:12:58 CET 2008


Author: akorotaev
Date: Wed Jan  2 22:12:58 2008
New Revision: 31567

URL: http://svn.reactos.org/svn/reactos?rev=31567&view=rev
Log:
Merge changes from HEAD. It still doesn't link.

Modified:
    branches/cache_manager_rewrite/mm/anonmem.c
    branches/cache_manager_rewrite/mm/cont.c
    branches/cache_manager_rewrite/mm/drvlck.c
    branches/cache_manager_rewrite/mm/elf.inc.h
    branches/cache_manager_rewrite/mm/elf32.c
    branches/cache_manager_rewrite/mm/elf64.c
    branches/cache_manager_rewrite/mm/freelist.c
    branches/cache_manager_rewrite/mm/iospace.c
    branches/cache_manager_rewrite/mm/marea.c
    branches/cache_manager_rewrite/mm/mm.c
    branches/cache_manager_rewrite/mm/mminit.c
    branches/cache_manager_rewrite/mm/mpw.c
    branches/cache_manager_rewrite/mm/npool.c
    branches/cache_manager_rewrite/mm/pagefile.c

Modified: branches/cache_manager_rewrite/mm/anonmem.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/anonmem.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/anonmem.c (original)
+++ branches/cache_manager_rewrite/mm/anonmem.c Wed Jan  2 22:12:58 2008
@@ -27,14 +27,14 @@
  *                  Ge van Geldorp
  *                  Eric Kohl
  *                  Royce Mitchell III
- *                  Aleksey Bragin 
+ *                  Aleksey Bragin
  *                  Jason Filby
  *                  Art Yerkes
  *                  Gunnar Andre' Dalsnes
  *                  Filip Navara
  *                  Thomas Weidenmueller
  *                  Alex Ionescu
- *                  Trevor McCort 
+ *                  Trevor McCort
  *                  Steven Edwards
  */
 
@@ -516,12 +516,12 @@
  * @implemented
  */
 NTSTATUS STDCALL
-NtAllocateVirtualMemory(IN HANDLE ProcessHandle,
-                        IN OUT PVOID*  UBaseAddress,
-                        IN ULONG ZeroBits,
+NtAllocateVirtualMemory(IN     HANDLE ProcessHandle,
+                        IN OUT PVOID* UBaseAddress,
+                        IN     ULONG  ZeroBits,
                         IN OUT PULONG URegionSize,
-                        IN ULONG AllocationType,
-                        IN ULONG Protect)
+                        IN     ULONG  AllocationType,
+                        IN     ULONG  Protect)
 /*
  * FUNCTION: Allocates a block of virtual memory in the process address space
  * ARGUMENTS:
@@ -542,6 +542,10 @@
  *                PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_GUARD,
  *                PAGE_NOACCESS
  * RETURNS: Status
+ * NOTES: Must run at IRQL PASSIVE_LEVEL? (or is APC_LEVEL cool too?)
+ *        MSDN states that ZwAllocateVirtualMemory IRQL must be PASSIVE_LEVEL,
+ *        but why wouldn't APC_LEVEL be valid (or is that only for the Zw* version
+ *        and Nt* can indeed run at APC_LEVEL?)
  */
 {
    PEPROCESS Process;
@@ -555,6 +559,12 @@
    PVOID PBaseAddress;
    ULONG PRegionSize;
    PHYSICAL_ADDRESS BoundaryAddressMultiple;
+   KPROCESSOR_MODE PreviousMode;
+
+   // TMN: Someone Pick one of these. Until it's clear which
+   // level is allowed, I play it safe and check for <= APC_LEVEL
+   PAGED_CODE();
+//   ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
 
    DPRINT("NtAllocateVirtualMemory(*UBaseAddress %x, "
           "ZeroBits %d, *URegionSize %x, AllocationType %x, Protect %x)\n",
@@ -576,7 +586,7 @@
    }
 
    /* Check for valid Allocation Types */
-   if ((AllocationType &~ (MEM_COMMIT | MEM_RESERVE | MEM_RESET | MEM_PHYSICAL |
+   if ((AllocationType & ~(MEM_COMMIT | MEM_RESERVE | MEM_RESET | MEM_PHYSICAL |
                            MEM_TOP_DOWN | MEM_WRITE_WATCH)))
    {
       DPRINT1("Invalid Allocation Type\n");
@@ -589,11 +599,11 @@
       DPRINT1("No memory allocation base type\n");
       return STATUS_INVALID_PARAMETER_5;
    }
-   
+
    /* MEM_RESET is an exclusive flag, make sure that is valid too */
    if ((AllocationType & MEM_RESET) && (AllocationType != MEM_RESET))
    {
-      DPRINT1("MEM_RESET used illegaly\n");
+      DPRINT1("Invalid use of MEM_RESET\n");
       return STATUS_INVALID_PARAMETER_5;
    }
 
@@ -623,27 +633,45 @@
       }
    }
 
-   PBaseAddress = *UBaseAddress;
-   PRegionSize = *URegionSize;
+   PreviousMode = KeGetPreviousMode();
+
+   _SEH_TRY
+   {
+      if (PreviousMode != KernelMode)
+      {
+         ProbeForWritePointer(UBaseAddress);
+         ProbeForWriteUlong(URegionSize);
+      }
+      PBaseAddress = *UBaseAddress;
+      PRegionSize  = *URegionSize;
+   }
+   _SEH_HANDLE
+   {
+      /* Get the exception code */
+      Status = _SEH_GetExceptionCode();
+      _SEH_YIELD(return Status);
+   }
+   _SEH_END;
+
    BoundaryAddressMultiple.QuadPart = 0;
 
    BaseAddress = (PVOID)PAGE_ROUND_DOWN(PBaseAddress);
    RegionSize = PAGE_ROUND_UP((ULONG_PTR)PBaseAddress + PRegionSize) -
                 PAGE_ROUND_DOWN(PBaseAddress);
 
-   /* 
+   /*
     * We've captured and calculated the data, now do more checks
     * Yes, MmCreateMemoryArea does similar checks, but they don't return
     * the right status codes that a caller of this routine would expect.
     */
    if (BaseAddress >= MM_HIGHEST_USER_ADDRESS)
    {
-      DPRINT1("Virtual allocation above User Space\n");
+      DPRINT1("Virtual allocation base above User Space\n");
       return STATUS_INVALID_PARAMETER_2;
    }
    if (!RegionSize)
    {
-      DPRINT1("Region size is invalid\n");
+      DPRINT1("Region size is invalid (zero)\n");
       return STATUS_INVALID_PARAMETER_4;
    }
    if (((ULONG_PTR)MM_HIGHEST_USER_ADDRESS - (ULONG_PTR)BaseAddress) < RegionSize)
@@ -652,12 +680,12 @@
       return STATUS_INVALID_PARAMETER_4;
    }
 
-   /* 
+   /*
     * Copy on Write is reserved for system use. This case is a certain failure
     * but there may be other cases...needs more testing
     */
-   if ((!BaseAddress || (AllocationType & MEM_RESERVE)) && 
-       ((Protect & PAGE_WRITECOPY) || (Protect & PAGE_EXECUTE_WRITECOPY)))
+   if ((!BaseAddress || (AllocationType & MEM_RESERVE)) &&
+       (Protect & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY)))
    {
       DPRINT1("Copy on write is not supported by VirtualAlloc\n");
       return STATUS_INVALID_PAGE_PROTECTION;
@@ -666,8 +694,8 @@
 
    Status = ObReferenceObjectByHandle(ProcessHandle,
                                       PROCESS_VM_OPERATION,
-                                      NULL,
-                                      UserMode,
+                                      PsProcessType,
+                                      PreviousMode,
                                       (PVOID*)(&Process),
                                       NULL);
    if (!NT_SUCCESS(Status))
@@ -750,10 +778,10 @@
                       MemoryAreaLength, Type, Protect);
 
    if ((AllocationType & MEM_COMMIT) &&
-         ((Protect & PAGE_READWRITE) ||
-          (Protect & PAGE_EXECUTE_READWRITE)))
-   {
-      MmReserveSwapPages(MemoryAreaLength);
+       (Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE)))
+   {
+      const ULONG nPages = PAGE_ROUND_UP(MemoryAreaLength) >> PAGE_SHIFT;
+      MmReserveSwapPages(nPages);
    }
 
    *UBaseAddress = BaseAddress;
@@ -816,17 +844,11 @@
    {
       ULONG_PTR MemoryAreaLength = (ULONG_PTR)MemoryArea->EndingAddress -
                                    (ULONG_PTR)MemoryArea->StartingAddress;
-
-      /* FiN TODO: Optimize loop counter! */
-      for (i = 0; i < PAGE_ROUND_UP(MemoryAreaLength) / PAGE_SIZE; i++)
+      const ULONG nPages = PAGE_ROUND_UP(MemoryAreaLength) >> PAGE_SHIFT;
+
+      for (i = 0; i < nPages && MemoryArea->PageOpCount != 0; ++i)
       {
          PMM_PAGEOP PageOp;
-
-         if (MemoryArea->PageOpCount == 0)
-         {
-            break;
-         }
-
          PageOp = MmCheckForPageOp(MemoryArea, Process->UniqueProcessId,
                                    (PVOID)((ULONG_PTR)MemoryArea->StartingAddress + (i * PAGE_SIZE)),
                                    NULL, 0);
@@ -919,9 +941,8 @@
    MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, BaseAddress);
    if (MemoryArea == NULL)
    {
-      MmUnlockAddressSpace(AddressSpace);
-      ObDereferenceObject(Process);
-      return(STATUS_UNSUCCESSFUL);
+      Status = STATUS_UNSUCCESSFUL;
+      goto unlock_deref_and_return;
    }
 
    switch (FreeType)
@@ -931,14 +952,13 @@
          if (MemoryArea->StartingAddress != BaseAddress ||
              MemoryArea->Type != MEMORY_AREA_VIRTUAL_MEMORY)
          {
-            MmUnlockAddressSpace(AddressSpace);
-            ObDereferenceObject(Process);
-            return(STATUS_UNSUCCESSFUL);
+            Status = STATUS_UNSUCCESSFUL;
+            goto unlock_deref_and_return;
          }
+
          MmFreeVirtualMemory(Process, MemoryArea);
-         MmUnlockAddressSpace(AddressSpace);
-         ObDereferenceObject(Process);
-         return(STATUS_SUCCESS);
+         Status = STATUS_SUCCESS;
+         goto unlock_deref_and_return;
 
       case MEM_DECOMMIT:
          Status =
@@ -950,13 +970,17 @@
                           MEM_RESERVE,
                           PAGE_NOACCESS,
                           MmModifyAttributes);
-         MmUnlockAddressSpace(AddressSpace);
-         ObDereferenceObject(Process);
-         return(Status);
-   }
+         goto unlock_deref_and_return;
+   }
+
+   Status = STATUS_NOT_IMPLEMENTED;
+
+unlock_deref_and_return:
+
    MmUnlockAddressSpace(AddressSpace);
    ObDereferenceObject(Process);
-   return(STATUS_NOT_IMPLEMENTED);
+
+   return(Status);
 }
 
 NTSTATUS

Modified: branches/cache_manager_rewrite/mm/cont.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/cont.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/cont.c (original)
+++ branches/cache_manager_rewrite/mm/cont.c Wed Jan  2 22:12:58 2008
@@ -16,7 +16,7 @@
 
 /* FUNCTIONS *****************************************************************/
 
-static VOID 
+static VOID
 MmFreeContinuousPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
                      PFN_TYPE Page, SWAPENTRY SwapEntry,
                      BOOLEAN Dirty)

Modified: branches/cache_manager_rewrite/mm/drvlck.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/drvlck.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/drvlck.c (original)
+++ branches/cache_manager_rewrite/mm/drvlck.c Wed Jan  2 22:12:58 2008
@@ -14,6 +14,8 @@
 #include <internal/debug.h>
 
 /* FUNCTIONS *****************************************************************/
+
+#undef MmLockPagableDataSection
 
 #if 0
 VOID

Modified: branches/cache_manager_rewrite/mm/elf.inc.h
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/elf.inc.h?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/elf.inc.h (original)
+++ branches/cache_manager_rewrite/mm/elf.inc.h Wed Jan  2 22:12:58 2008
@@ -1,6 +1,5 @@
 /* $Id$
 */
-#include <ntoskrnl.h>
 
 #define NDEBUG
 #include <internal/debug.h>

Modified: branches/cache_manager_rewrite/mm/elf32.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/elf32.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/elf32.c (original)
+++ branches/cache_manager_rewrite/mm/elf32.c Wed Jan  2 22:12:58 2008
@@ -7,7 +7,7 @@
  *
  * PROGRAMMERS:     No programmer listed.
  */
-
+#include <ntoskrnl.h>
 #define __ELF_WORD_SIZE 32
 #include "elf.inc.h"
 

Modified: branches/cache_manager_rewrite/mm/elf64.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/elf64.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/elf64.c (original)
+++ branches/cache_manager_rewrite/mm/elf64.c Wed Jan  2 22:12:58 2008
@@ -7,6 +7,6 @@
  *
  * PROGRAMMERS:     No programmer listed.
  */
-
+#include <ntoskrnl.h>
 #define __ELF_WORD_SIZE 64
 #include "elf.inc.h"

Modified: branches/cache_manager_rewrite/mm/freelist.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/freelist.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/freelist.c (original)
+++ branches/cache_manager_rewrite/mm/freelist.c Wed Jan  2 22:12:58 2008
@@ -190,9 +190,9 @@
    {
       start = -1;
       length = 0;
-      /* First try to allocate the pages above the 16MB area. This may fail 
-       * because there are not enough continuous pages or we cannot allocate 
-       * pages above the 16MB area because the caller has specify an upper limit. 
+      /* First try to allocate the pages above the 16MB area. This may fail
+       * because there are not enough continuous pages or we cannot allocate
+       * pages above the 16MB area because the caller has specify an upper limit.
        * The second try uses the specified lower limit.
        */
       for (i = j == 0 ? 0x100000 / PAGE_SIZE : LowestAcceptableAddress.QuadPart / PAGE_SIZE; i <= last; )
@@ -428,7 +428,7 @@
 	    if (j == 0)
 	    {
                /*
-                * Page zero is reserved
+                * Page zero is reserved for the IVT
                 */
                MmPageArray[0].Flags.Type = MM_PHYSICAL_PAGE_BIOS;
                MmPageArray[0].Flags.Consumer = MC_NPPOOL;
@@ -450,6 +450,19 @@
                MmPageArray[1].ReferenceCount = 0;
                InsertTailList(&BiosPageListHead,
                               &MmPageArray[1].ListEntry);
+	       MmStats.NrReservedPages++;
+	    }
+	    else if (j == 2)
+	    {
+               /*
+                * Page two is reserved for the KUSER_SHARED_DATA
+                */
+               MmPageArray[2].Flags.Type = MM_PHYSICAL_PAGE_BIOS;
+               MmPageArray[2].Flags.Consumer = MC_NPPOOL;
+               MmPageArray[2].Flags.Zero = 0;
+               MmPageArray[2].ReferenceCount = 0;
+               InsertTailList(&BiosPageListHead,
+                              &MmPageArray[2].ListEntry);
 	       MmStats.NrReservedPages++;
 	    }
         /* Protect the Page Directory. This will be changed in r3 */

Modified: branches/cache_manager_rewrite/mm/iospace.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/iospace.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/iospace.c (original)
+++ branches/cache_manager_rewrite/mm/iospace.c Wed Jan  2 22:12:58 2008
@@ -95,7 +95,7 @@
       DPRINT("MmMapIoSpace failed (%lx)\n", Status);
       return (NULL);
    }
-   Pfn = PhysicalAddress.QuadPart >> PAGE_SHIFT;
+   Pfn = PhysicalAddress.LowPart >> PAGE_SHIFT;
    for (i = 0; i < PAGE_ROUND_UP(NumberOfBytes); i += PAGE_SIZE, Pfn++)
    {
       Status = MmCreateVirtualMappingForKernel((char*)Result + i,

Modified: branches/cache_manager_rewrite/mm/marea.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/marea.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/marea.c (original)
+++ branches/cache_manager_rewrite/mm/marea.c Wed Jan  2 22:12:58 2008
@@ -29,7 +29,7 @@
  *                  Eric Kohl
  *                  Ge van Geldorp
  *                  Royce Mitchell III
- *                  Aleksey Bragin 
+ *                  Aleksey Bragin
  *                  Jason Filby
  *                  Thomas Weidenmueller
  *                  Gunnar Andre' Dalsnes
@@ -713,7 +713,7 @@
 NTAPI
 MmInitMemoryAreas(VOID)
 {
-   DPRINT("MmInitMemoryAreas()\n",0);
+   DPRINT("MmInitMemoryAreas()\n");
    return(STATUS_SUCCESS);
 }
 

Modified: branches/cache_manager_rewrite/mm/mm.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/mm.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/mm.c (original)
+++ branches/cache_manager_rewrite/mm/mm.c Wed Jan  2 22:12:58 2008
@@ -193,17 +193,6 @@
       CPRINT("Page fault at high IRQL was %d, address %x\n", KeGetCurrentIrql(), Address);
       return(STATUS_UNSUCCESSFUL);
    }
-   if (PsGetCurrentProcess() == NULL)
-   {
-      /* Allow this! It lets us page alloc much earlier! It won't be needed 
-       * after my init patch anyways
-       */
-      DPRINT("No current process\n");
-      if (Address < (ULONG_PTR)MmSystemRangeStart)
-      {
-         return(STATUS_ACCESS_VIOLATION);
-      }
-   }
 
    /*
     * Find the memory area for the faulting address
@@ -274,7 +263,7 @@
             break;
 
          case MEMORY_AREA_SHARED_DATA:
-	    Pfn = MmSharedDataPagePhysicalAddress.QuadPart >> PAGE_SHIFT;
+            Pfn = MmSharedDataPagePhysicalAddress.LowPart >> PAGE_SHIFT;
             Status =
                MmCreateVirtualMapping(PsGetCurrentProcess(),
                                       (PVOID)PAGE_ROUND_DOWN(Address),
@@ -310,12 +299,14 @@
     /* Cute little hack for ROS */
     if ((ULONG_PTR)Address >= (ULONG_PTR)MmSystemRangeStart)
     {
+#ifdef _M_IX86
         /* Check for an invalid page directory in kernel mode */
         if (Mmi386MakeKernelPageTableGlobal(Address))
         {
             /* All is well with the world */
             return STATUS_SUCCESS;
         }
+#endif
     }
 
     /* Keep same old ReactOS Behaviour */
@@ -399,70 +390,6 @@
    return (FALSE);
 }
 
-/*
- * @implemented
- */
-PVOID
-NTAPI
-MmGetSystemRoutineAddress(IN PUNICODE_STRING SystemRoutineName)
-{
-    PVOID ProcAddress;
-    ANSI_STRING AnsiRoutineName;
-    NTSTATUS Status;
-    PLIST_ENTRY NextEntry;
-    extern LIST_ENTRY ModuleListHead;
-    PLDR_DATA_TABLE_ENTRY LdrEntry;
-    BOOLEAN Found = FALSE;
-    UNICODE_STRING KernelName = RTL_CONSTANT_STRING(L"ntoskrnl.exe");
-    UNICODE_STRING HalName = RTL_CONSTANT_STRING(L"hal.dll");
-
-    /* Convert routine to ansi name */
-    Status = RtlUnicodeStringToAnsiString(&AnsiRoutineName,
-                                          SystemRoutineName,
-                                          TRUE);
-    if (!NT_SUCCESS(Status)) return NULL;
-
-    /* Loop the loaded module list */
-    NextEntry = ModuleListHead.Flink;
-    while (NextEntry != &ModuleListHead)
-    {
-        /* Get the entry */
-        LdrEntry = CONTAINING_RECORD(NextEntry,
-                                     LDR_DATA_TABLE_ENTRY,
-                                     InLoadOrderLinks);
-
-        /* Check if it's the kernel or HAL */
-        if (RtlEqualUnicodeString(&KernelName, &LdrEntry->BaseDllName, TRUE))
-        {
-            /* Found it */
-            Found = TRUE;
-        }
-        else if (RtlEqualUnicodeString(&HalName, &LdrEntry->BaseDllName, TRUE))
-        {
-            /* Found it */
-            Found = TRUE;
-        }
-
-        /* Check if we found a valid binary */
-        if (Found)
-        {
-            /* Find the procedure name */
-            Status = LdrGetProcedureAddress(LdrEntry->DllBase,
-                                            &AnsiRoutineName,
-                                            0,
-                                            &ProcAddress);
-            break;
-        }
-
-        /* Keep looping */
-        NextEntry = NextEntry->Flink;
-    }
-
-    /* Free the string and return */
-    RtlFreeAnsiString(&AnsiRoutineName);
-    return (NT_SUCCESS(Status) ? ProcAddress : NULL);
-}
-
 NTSTATUS
 NTAPI
 NtGetWriteWatch(IN HANDLE ProcessHandle,

Modified: branches/cache_manager_rewrite/mm/mminit.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/mminit.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/mminit.c (original)
+++ branches/cache_manager_rewrite/mm/mminit.c Wed Jan  2 22:12:58 2008
@@ -19,6 +19,7 @@
 /*
  * Compiler defined symbols
  */
+#if 0
 extern unsigned int _image_base__;
 extern unsigned int _text_start__;
 extern unsigned int _text_end__;
@@ -27,7 +28,7 @@
 extern unsigned int _init_end__;
 
 extern unsigned int _bss_end__;
-
+#endif
 
 static BOOLEAN IsThisAnNtAsSystem = FALSE;
 MM_SYSTEMSIZE MmSystemSize = MmSmallSystem;
@@ -37,14 +38,9 @@
 PVOID MiNonPagedPoolStart;
 ULONG MiNonPagedPoolLength;
 
+ULONG MmNumberOfPhysicalPages;
+
 VOID INIT_FUNCTION NTAPI MmInitVirtualMemory(ULONG_PTR LastKernelAddress, ULONG KernelLength);
-
-#if defined (ALLOC_PRAGMA)
-#pragma alloc_text(INIT, MmInitVirtualMemory)
-#pragma alloc_text(INIT, MmInit1)
-#pragma alloc_text(INIT, MmInit2)
-#pragma alloc_text(INIT, MmInit3)
-#endif
 
 /* FUNCTIONS ****************************************************************/
 
@@ -83,10 +79,8 @@
 {
    PVOID BaseAddress;
    ULONG Length;
-   ULONG ParamLength = KernelLength;
    NTSTATUS Status;
    PHYSICAL_ADDRESS BoundaryAddressMultiple;
-   PFN_TYPE Pfn;
    PMEMORY_AREA MArea;
 
    DPRINT("MmInitVirtualMemory(%x, %x)\n",LastKernelAddress, KernelLength);
@@ -96,14 +90,19 @@
 
    MmInitMemoryAreas();
 
-   /* Start the paged and nonpaged pool at a 4MB boundary. */ 
-   MiNonPagedPoolStart = (PVOID)ROUND_UP((ULONG_PTR)LastKernelAddress + PAGE_SIZE, 0x400000);
+   /*
+    * FreeLDR Marks 6MB "in use" at the start of the kernel base,
+    * so start the non-paged pool at a boundary of 6MB from where
+    * the last driver was loaded. This should be the end of the
+    * FreeLDR-marked region.
+    */
+   MiNonPagedPoolStart = (PVOID)ROUND_UP((ULONG_PTR)LastKernelAddress + PAGE_SIZE, 0x600000);
    MiNonPagedPoolLength = MM_NONPAGED_POOL_SIZE;
 
    MmPagedPoolBase = (PVOID)ROUND_UP((ULONG_PTR)MiNonPagedPoolStart + MiNonPagedPoolLength + PAGE_SIZE, 0x400000);
    MmPagedPoolSize = MM_PAGED_POOL_SIZE;
 
-   DPRINT("NonPagedPool %x - %x, PagedPool %x - %x\n", MiNonPagedPoolStart, (ULONG_PTR)MiNonPagedPoolStart + MiNonPagedPoolLength - 1, 
+   DPRINT("NonPagedPool %x - %x, PagedPool %x - %x\n", MiNonPagedPoolStart, (ULONG_PTR)MiNonPagedPoolStart + MiNonPagedPoolLength - 1,
            MmPagedPoolBase, (ULONG_PTR)MmPagedPoolBase + MmPagedPoolSize - 1);
 
    MiInitializeNonPagedPool();
@@ -159,73 +158,6 @@
                       0,
                       BoundaryAddressMultiple);
 
-   BaseAddress = (PVOID)&_image_base__;
-   Length = PAGE_ROUND_UP(((ULONG_PTR)&_text_end__)) - (ULONG_PTR)&_image_base__;
-   ParamLength = ParamLength - Length;
-
-   /*
-    * No need to lock the address space at this point since no
-    * other threads are running.
-    */
-   MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                      MEMORY_AREA_SYSTEM,
-                      &BaseAddress,
-                      Length,
-                      PAGE_EXECUTE_READ,
-                      &MArea,
-                      TRUE,
-                      0,
-                      BoundaryAddressMultiple);
-
-   BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG_PTR)&_text_end__));
-   ASSERT(BaseAddress == (PVOID)&_init_start__);
-   Length = PAGE_ROUND_UP(((ULONG_PTR)&_init_end__)) -
-            PAGE_ROUND_UP(((ULONG_PTR)&_text_end__));
-   ParamLength = ParamLength - Length;
-
-   MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                      MEMORY_AREA_SYSTEM,
-                      &BaseAddress,
-                      Length,
-                      PAGE_EXECUTE_READ,
-                      &MArea,
-                      TRUE,
-                      0,
-                      BoundaryAddressMultiple);
-
-   Length = PAGE_ROUND_UP(((ULONG_PTR)&_bss_end__)) -
-            PAGE_ROUND_UP(((ULONG_PTR)&_init_end__));
-   ParamLength = ParamLength - Length;
-   DPRINT("Length %x\n",Length);
-   BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG_PTR)&_init_end__));
-   DPRINT("BaseAddress %x\n",BaseAddress);
-
-   /*
-    * No need to lock the address space at this point since we are
-    * the only thread running.
-    */
-   MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                      MEMORY_AREA_SYSTEM,
-                      &BaseAddress,
-                      Length,
-                      PAGE_READWRITE,
-                      &MArea,
-                      TRUE,
-                      0,
-                      BoundaryAddressMultiple);
-
-   BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG_PTR)&_bss_end__));
-   Length = LastKernelAddress - (ULONG_PTR)BaseAddress;
-   MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                      MEMORY_AREA_SYSTEM,
-                      &BaseAddress,
-                      Length,
-                      PAGE_READWRITE,
-                      &MArea,
-                      TRUE,
-                      0,
-                      BoundaryAddressMultiple);
-
    BaseAddress = MiNonPagedPoolStart;
    MmCreateMemoryArea(MmGetKernelAddressSpace(),
                       MEMORY_AREA_SYSTEM,
@@ -264,18 +196,7 @@
                       TRUE,
                       0,
                       BoundaryAddressMultiple);
-   Status = MmRequestPageMemoryConsumer(MC_NPPOOL, TRUE, &Pfn);
-   MmSharedDataPagePhysicalAddress.QuadPart = Pfn << PAGE_SHIFT;
-   Status = MmCreateVirtualMapping(NULL,
-                                   (PVOID)KI_USER_SHARED_DATA,
-                                   PAGE_READWRITE,
-                                   &Pfn,
-                                   1);
-   if (!NT_SUCCESS(Status))
-   {
-      DbgPrint("Unable to create virtual mapping\n");
-      KEBUGCHECK(0);
-   }
+   MmSharedDataPagePhysicalAddress.QuadPart = 2 << PAGE_SHIFT;
    RtlZeroMemory(BaseAddress, Length);
 
    /*
@@ -283,6 +204,35 @@
     */
    MmInitializeMemoryConsumer(MC_USER, MmTrimUserMemory);
 }
+
+PCHAR
+MemType[]  = {
+    "ExceptionBlock    ", // ?
+   "SystemBlock       ", // ?
+   "Free              ",
+   "Bad               ", // used
+   "LoadedProgram     ", // == Free
+   "FirmwareTemporary ", // == Free
+   "FirmwarePermanent ", // == Bad
+   "OsloaderHeap      ", // used
+   "OsloaderStack     ", // == Free
+   "SystemCode        ",
+   "HalCode           ",
+   "BootDriver        ", // not used
+   "ConsoleInDriver   ", // ?
+   "ConsoleOutDriver  ", // ?
+   "StartupDpcStack   ", // ?
+   "StartupKernelStack", // ?
+   "StartupPanicStack ", // ?
+   "StartupPcrPage    ", // ?
+   "StartupPdrPage    ", // ?
+   "RegistryData      ", // used
+   "MemoryData        ", // not used
+   "NlsData           ", // used
+   "SpecialMemory     ", // == Bad
+   "BBTMemory         ",
+   "LoaderReserve     "// == Bad
+};
 
 VOID
 INIT_FUNCTION
@@ -306,6 +256,25 @@
           FirstKrnlPhysAddr,
           LastKrnlPhysAddr,
           LastKernelAddress);
+
+    /* Dump memory descriptors */
+    {
+        PLIST_ENTRY NextEntry;
+        PMEMORY_ALLOCATION_DESCRIPTOR Md;
+        ULONG TotalPages = 0;
+
+        DPRINT("Base\t\tLength\t\tType\n");
+        for (NextEntry = KeLoaderBlock->MemoryDescriptorListHead.Flink;
+             NextEntry != &KeLoaderBlock->MemoryDescriptorListHead;
+             NextEntry = NextEntry->Flink)
+        {
+            Md = CONTAINING_RECORD(NextEntry, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
+            DPRINT("%08lX\t%08lX\t%s\n", Md->BasePage, Md->PageCount, MemType[Md->MemoryType]);
+            TotalPages += Md->PageCount;
+        }
+
+        DPRINT("Total: %08lX (%d MB)\n", TotalPages, (TotalPages * PAGE_SIZE) / 1024 / 1024);
+    }
 
    /* Set the page directory */
    PsGetCurrentProcess()->Pcb.DirectoryTableBase.LowPart = (ULONG)MmGetPageDirectory();
@@ -350,6 +319,7 @@
    }
 
    /* Set memory limits */
+   MmSystemRangeStart = (PVOID)KSEG0_BASE;
    MmUserProbeAddress = (ULONG_PTR)MmSystemRangeStart - 0x10000;
    MmHighestUserAddress = (PVOID)(MmUserProbeAddress - 1);
 
@@ -379,6 +349,7 @@
     * Free physical memory not used by the kernel
     */
    MmStats.NrTotalPages = MmFreeLdrMemHigher/4;
+   MmNumberOfPhysicalPages = MmStats.NrTotalPages;
    if (!MmStats.NrTotalPages)
    {
       DbgPrint("Memory not detected, default to 8 MB\n");
@@ -451,47 +422,78 @@
    MmInitializeMdlImplementation();
 }
 
-VOID
+BOOLEAN RmapReady, PageOpReady, SectionsReady, PagingReady;
+extern KMUTANT MmSystemLoadLock;
+
+BOOLEAN
 NTAPI
-INIT_FUNCTION
-MmInit2(VOID)
-{
-   MmInitializeRmapList();
-   MmInitializePageOp();
-   MmInitSectionImplementation();
-   MmInitPagingFile();
-}
-
-VOID 
-MmInitSectionImplementation2(VOID);
-
-VOID
-INIT_FUNCTION
-NTAPI
-MmInit3(VOID)
-{
-   /*
-    * Unmap low memory
-    */
-#ifdef CONFIG_SMP
-   /* In SMP mode we can unmap the low memory
-      if all processors are started. */
-   MmDeletePageTable(NULL, 0);
-#endif
-
-   MmCreatePhysicalMemorySection();
-   MiInitBalancerThread();
-
-   /*
-    * Initialise the modified page writer.
-    */
-   if (!strstr(KeLoaderBlock->LoadOptions, "MININT")) MmInitMpwThread();
-
-   /* FIXME: Read parameters from memory */
-   MmInitSectionImplementation2();
-
-
-}
+MmInitSystem(IN ULONG Phase,
+             IN PLOADER_PARAMETER_BLOCK LoaderBlock)
+{
+    ULONG Flags = 0;
+    if (Phase == 0)
+    {
+        /* Initialize the Loader Lock */
+        KeInitializeMutant(&MmSystemLoadLock, FALSE);
+
+        /* Initialize the address space for the system process */
+        MmInitializeProcessAddressSpace(PsGetCurrentProcess(),
+                                        NULL,
+                                        NULL,
+                                        &Flags,
+                                        NULL);
+
+        /* Reload boot drivers */
+        MiReloadBootLoadedDrivers(LoaderBlock);
+
+        /* Initialize the loaded module list */
+        MiInitializeLoadedModuleList(LoaderBlock);
+
+        /* We're done, for now */
+        DPRINT("Mm0: COMPLETE\n");
+    }
+    else if (Phase == 1)
+    {
+        MmInitializeRmapList();
+        RmapReady = TRUE;
+        MmInitializePageOp();
+        PageOpReady = TRUE;
+        MmInitSectionImplementation();
+        SectionsReady = TRUE;
+        MmInitPagingFile();
+        PagingReady = TRUE;
+        MmCreatePhysicalMemorySection();
+
+        /* Setup shared user data settings that NT does as well */
+        ASSERT(SharedUserData->NumberOfPhysicalPages == 0);
+        SharedUserData->NumberOfPhysicalPages = MmStats.NrTotalPages;
+        SharedUserData->LargePageMinimum = 0;
+
+        /* For now, we assume that we're always Workstation */
+        SharedUserData->NtProductType = NtProductWinNt;
+    }
+    else if (Phase == 2)
+    {
+        /*
+        * Unmap low memory
+        */
+        MiInitBalancerThread();
+
+        /*
+        * Initialise the modified page writer.
+        */
+        MmInitMpwThread();
+
+        /* Initialize the balance set manager */
+        MmInitBsmThread();
+
+        /* FIXME: Read parameters from memory */
+    }
+
+    return TRUE;
+}
+
+#if 0
 
 VOID static
 MiFreeInitMemoryPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
@@ -516,3 +518,4 @@
                          NULL);
    MmUnlockAddressSpace(MmGetKernelAddressSpace());
 }
+#endif

Modified: branches/cache_manager_rewrite/mm/mpw.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/mpw.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/mpw.c (original)
+++ branches/cache_manager_rewrite/mm/mpw.c Wed Jan  2 22:12:58 2008
@@ -123,3 +123,26 @@
 
    return(STATUS_SUCCESS);
 }
+
+NTSTATUS
+NTAPI
+MmInitBsmThread(VOID)
+{
+    NTSTATUS Status;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    HANDLE ThreadHandle;
+
+    /* Create the thread */
+    InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
+    Status = PsCreateSystemThread(&ThreadHandle,
+                                  THREAD_ALL_ACCESS,
+                                  &ObjectAttributes,
+                                  NULL,
+                                  NULL,
+                                  KeBalanceSetManager,
+                                  NULL);
+
+    /* Close the handle and return status */
+    ZwClose(ThreadHandle);
+    return Status;
+}

Modified: branches/cache_manager_rewrite/mm/npool.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/npool.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/npool.c (original)
+++ branches/cache_manager_rewrite/mm/npool.c Wed Jan  2 22:12:58 2008
@@ -83,7 +83,7 @@
     LIST_ENTRY TagListEntry;
 #if defined(NPOOL_REDZONE_CHECK) || defined(NPOOL_REDZONE_CHECK_FULL)
     ULONG UserSize;
-#endif 
+#endif
     BOOLEAN Dumped;
 } HDR_USED, *PHDR_USED;
 
@@ -911,7 +911,7 @@
    }
    DbgPrint("***************** Dump Complete ***************\n");
    KeReleaseSpinLock(&MmNpoolLock, oldIrql);
-#endif 
+#endif
 }
 
 #ifdef ENABLE_VALIDATE_POOL
@@ -1327,7 +1327,7 @@
              break;
          }
       }
-      
+
       if (best)
       {
          if (size < PAGE_SIZE)
@@ -1346,7 +1346,7 @@
          }
       }
       p = avl_get_next(FreeBlockListRoot, p);
-   }         
+   }
    /*
     * We didn't find anything suitable at all.
     */
@@ -1487,7 +1487,7 @@
          HiOK = FALSE;
       }
    }
-   
+
    if (!HiOK || !LoOK)
    {
       c[0] = (CHAR)((hdr->Tag >> 24) & 0xFF);
@@ -1514,7 +1514,7 @@
       KEBUGCHECK(0);
    }
 }
-#endif 
+#endif
 
 #ifdef NPOOL_REDZONE_CHECK_FULL
 void check_redzone_list(void)

Modified: branches/cache_manager_rewrite/mm/pagefile.c
URL: http://svn.reactos.org/svn/reactos/branches/cache_manager_rewrite/mm/pagefile.c?rev=31567&r1=31566&r2=31567&view=diff
==============================================================================
--- branches/cache_manager_rewrite/mm/pagefile.c (original)
+++ branches/cache_manager_rewrite/mm/pagefile.c Wed Jan  2 22:12:58 2008
@@ -323,6 +323,8 @@
    return(Status);
 }
 
+extern BOOLEAN PagingReady;
+
 VOID
 INIT_FUNCTION
 NTAPI
@@ -366,11 +368,16 @@
    KIRQL oldIrql;
    ULONG MiAvailSwapPages;
 
+   if (!PagingReady)
+   {
+       DPRINT1("PAGING USED TOO SOON!!!\n");
+       while (TRUE);
+   }
    KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
    MiAvailSwapPages =
       (MiFreeSwapPages * MM_PAGEFILE_COMMIT_RATIO) + MM_PAGEFILE_COMMIT_GRACE;
    MiReservedSwapPages = MiReservedSwapPages + Nr;
-   if (MM_PAGEFILE_COMMIT_RATIO != 0 && MiAvailSwapPages < MiReservedSwapPages)
+   if ((MM_PAGEFILE_COMMIT_RATIO != 0) && (MiAvailSwapPages < MiReservedSwapPages))
    {
       KeReleaseSpinLock(&PagingFileListLock, oldIrql);
       return(FALSE);
@@ -385,6 +392,11 @@
 {
    KIRQL oldIrql;
 
+   if (!PagingReady)
+   {
+       DPRINT1("PAGING USED TOO SOON!!!\n");
+       while (TRUE);
+   }
    KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
    MiReservedSwapPages = MiReservedSwapPages - Nr;
    KeReleaseSpinLock(&PagingFileListLock, oldIrql);
@@ -396,6 +408,11 @@
    KIRQL oldIrql;
    ULONG i, j;
 
+   if (!PagingReady)
+   {
+       DPRINT1("PAGING USED TOO SOON!!!\n");
+       while (TRUE);
+   }
    KeAcquireSpinLock(&PagingFile->AllocMapLock, &oldIrql);
 
    for (i = 0; i < PagingFile->AllocMapSize; i++)
@@ -425,6 +442,11 @@
    ULONG off;
    KIRQL oldIrql;
 
+   if (!PagingReady)
+   {
+       DPRINT1("PAGING USED TOO SOON!!!\n");
+       while (TRUE);
+   }
    i = FILE_FROM_ENTRY(Entry);
    off = OFFSET_FROM_ENTRY(Entry);
 
@@ -469,6 +491,11 @@
    ULONG off;
    SWAPENTRY entry;
 
+   if (!PagingReady)
+   {
+       DPRINT1("PAGING USED TOO SOON!!!\n");
+       while (TRUE);
+   }
    KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
 
    if (MiFreeSwapPages == 0)
@@ -553,12 +580,14 @@
    Headers->Type = MmCoreDumpType;
    if (TrapFrame != NULL)
    {
+#ifdef _M_IX86
       if (!(TrapFrame->EFlags & (1 << 17)))
       {
          memcpy(&Headers->TrapFrame, TrapFrame,
                 sizeof(KTRAP_FRAME) - (4 * sizeof(ULONG)));
       }
       else
+#endif
       {
          memcpy(&Headers->TrapFrame, TrapFrame, sizeof(KTRAP_FRAME));
       }
@@ -672,7 +701,7 @@
    UNICODE_STRING DiskDumpName = RTL_CONSTANT_STRING(L"DiskDump");
    ANSI_STRING ProcName;
    PIO_STACK_LOCATION StackPtr;
-   PLDR_DATA_TABLE_ENTRY ModuleObject;
+   PLDR_DATA_TABLE_ENTRY ModuleObject = NULL;
 
    Status = ZwFsControlFile(PageFileHandle,
                             0,




More information about the Ros-diffs mailing list