[NTOSKRNL]
authorTimo Kreuzer <timo.kreuzer@reactos.org>
Sun, 5 Oct 2014 06:33:34 +0000 (06:33 +0000)
committerTimo Kreuzer <timo.kreuzer@reactos.org>
Sun, 5 Oct 2014 06:33:34 +0000 (06:33 +0000)
Apply indentation of 4 spaces, no code change.

svn path=/trunk/; revision=64531

reactos/ntoskrnl/mm/balance.c
reactos/ntoskrnl/mm/freelist.c
reactos/ntoskrnl/mm/marea.c
reactos/ntoskrnl/mm/mmfault.c
reactos/ntoskrnl/mm/mminit.c
reactos/ntoskrnl/mm/pagefile.c
reactos/ntoskrnl/mm/region.c
reactos/ntoskrnl/mm/rmap.c
reactos/ntoskrnl/mm/section.c

index 3296b22..7e1a3fb 100644 (file)
@@ -26,9 +26,9 @@
 /* TYPES ********************************************************************/
 typedef struct _MM_ALLOCATION_REQUEST
 {
-   PFN_NUMBER Page;
-   LIST_ENTRY ListEntry;
-   KEVENT Event;
+    PFN_NUMBER Page;
+    LIST_ENTRY ListEntry;
+    KEVENT Event;
 }
 MM_ALLOCATION_REQUEST, *PMM_ALLOCATION_REQUEST;
 /* GLOBALS ******************************************************************/
@@ -52,15 +52,15 @@ INIT_FUNCTION
 NTAPI
 MmInitializeBalancer(ULONG NrAvailablePages, ULONG NrSystemPages)
 {
-   memset(MiMemoryConsumers, 0, sizeof(MiMemoryConsumers));
-   InitializeListHead(&AllocationListHead);
-   KeInitializeSpinLock(&AllocationListLock);
+    memset(MiMemoryConsumers, 0, sizeof(MiMemoryConsumers));
+    InitializeListHead(&AllocationListHead);
+    KeInitializeSpinLock(&AllocationListLock);
 
-   MiNrTotalPages = NrAvailablePages;
+    MiNrTotalPages = NrAvailablePages;
 
-   /* Set up targets. */
-   MiMinimumAvailablePages = 128;
-   MiMinimumPagesPerRun = 256;
+    /* Set up targets. */
+    MiMinimumAvailablePages = 128;
+    MiMinimumPagesPerRun = 256;
     if ((NrAvailablePages + NrSystemPages) >= 8192)
     {
         MiMemoryConsumers[MC_CACHE].PagesTarget = NrAvailablePages / 4 * 3;
@@ -73,17 +73,17 @@ MmInitializeBalancer(ULONG NrAvailablePages, ULONG NrSystemPages)
     {
         MiMemoryConsumers[MC_CACHE].PagesTarget = NrAvailablePages / 8;
     }
-   MiMemoryConsumers[MC_USER].PagesTarget = NrAvailablePages - MiMinimumAvailablePages;
+    MiMemoryConsumers[MC_USER].PagesTarget = NrAvailablePages - MiMinimumAvailablePages;
 }
 
 VOID
 INIT_FUNCTION
 NTAPI
-MmInitializeMemoryConsumer(ULONG Consumer,
-                           NTSTATUS (*Trim)(ULONG Target, ULONG Priority,
-                                            PULONG NrFreed))
+MmInitializeMemoryConsumer(
+    ULONG Consumer,
+    NTSTATUS (*Trim)(ULONG Target, ULONG Priority, PULONG NrFreed))
 {
-   MiMemoryConsumers[Consumer].Trim = Trim;
+    MiMemoryConsumers[Consumer].Trim = Trim;
 }
 
 VOID
@@ -96,42 +96,42 @@ NTSTATUS
 NTAPI
 MmReleasePageMemoryConsumer(ULONG Consumer, PFN_NUMBER Page)
 {
-   PMM_ALLOCATION_REQUEST Request;
-   PLIST_ENTRY Entry;
-   KIRQL OldIrql;
-
-   if (Page == 0)
-   {
-      DPRINT1("Tried to release page zero.\n");
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   if (MmGetReferenceCountPage(Page) == 1)
-   {
-      if(Consumer == MC_USER) MmRemoveLRUUserPage(Page);
-      (void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
-      if ((Entry = ExInterlockedRemoveHeadList(&AllocationListHead, &AllocationListLock)) == NULL)
-      {
-         OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-         MmDereferencePage(Page);
-         KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
-      }
-      else
-      {
-         Request = CONTAINING_RECORD(Entry, MM_ALLOCATION_REQUEST, ListEntry);
-         MiZeroPhysicalPage(Page);
-         Request->Page = Page;
-         KeSetEvent(&Request->Event, IO_NO_INCREMENT, FALSE);
-      }
-   }
-   else
-   {
-      OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-      MmDereferencePage(Page);
-      KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
-   }
-
-   return(STATUS_SUCCESS);
+    PMM_ALLOCATION_REQUEST Request;
+    PLIST_ENTRY Entry;
+    KIRQL OldIrql;
+
+    if (Page == 0)
+    {
+        DPRINT1("Tried to release page zero.\n");
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    if (MmGetReferenceCountPage(Page) == 1)
+    {
+        if(Consumer == MC_USER) MmRemoveLRUUserPage(Page);
+        (void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
+        if ((Entry = ExInterlockedRemoveHeadList(&AllocationListHead, &AllocationListLock)) == NULL)
+        {
+            OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+            MmDereferencePage(Page);
+            KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
+        }
+        else
+        {
+            Request = CONTAINING_RECORD(Entry, MM_ALLOCATION_REQUEST, ListEntry);
+            MiZeroPhysicalPage(Page);
+            Request->Page = Page;
+            KeSetEvent(&Request->Event, IO_NO_INCREMENT, FALSE);
+        }
+    }
+    else
+    {
+        OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+        MmDereferencePage(Page);
+        KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
+    }
+
+    return(STATUS_SUCCESS);
 }
 
 ULONG
@@ -230,8 +230,8 @@ MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG NrFreedPages)
 static BOOLEAN
 MiIsBalancerThread(VOID)
 {
-   return (MiBalancerThreadHandle != NULL) &&
-          (PsGetCurrentThreadId() == MiBalancerThreadId.UniqueThread);
+    return (MiBalancerThreadHandle != NULL) &&
+           (PsGetCurrentThreadId() == MiBalancerThreadId.UniqueThread);
 }
 
 VOID
@@ -257,181 +257,182 @@ NTAPI
 MmRequestPageMemoryConsumer(ULONG Consumer, BOOLEAN CanWait,
                             PPFN_NUMBER AllocatedPage)
 {
-   ULONG PagesUsed;
-   PFN_NUMBER Page;
-   KIRQL OldIrql;
-
-   /*
-    * Make sure we don't exceed our individual target.
-    */
-   PagesUsed = InterlockedIncrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
-   if (PagesUsed > MiMemoryConsumers[Consumer].PagesTarget &&
-       !MiIsBalancerThread())
-   {
-      MmRebalanceMemoryConsumers();
-   }
-
-   /*
-    * Allocate always memory for the non paged pool and for the pager thread.
-    */
-   if ((Consumer == MC_SYSTEM) || MiIsBalancerThread())
-   {
-      OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-      Page = MmAllocPage(Consumer);
-      KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
-      if (Page == 0)
-      {
-         KeBugCheck(NO_PAGES_AVAILABLE);
-      }
-      if (Consumer == MC_USER) MmInsertLRULastUserPage(Page);
-      *AllocatedPage = Page;
-      if (MmAvailablePages < MiMinimumAvailablePages)
-          MmRebalanceMemoryConsumers();
-      return(STATUS_SUCCESS);
-   }
-
-   /*
-    * Make sure we don't exceed global targets.
-    */
-   if (MmAvailablePages < MiMinimumAvailablePages)
-   {
-      MM_ALLOCATION_REQUEST Request;
-
-      if (!CanWait)
-      {
-         (void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
-         MmRebalanceMemoryConsumers();
-         return(STATUS_NO_MEMORY);
-      }
-
-      /* Insert an allocation request. */
-      Request.Page = 0;
-      KeInitializeEvent(&Request.Event, NotificationEvent, FALSE);
-
-      ExInterlockedInsertTailList(&AllocationListHead, &Request.ListEntry, &AllocationListLock);
-      MmRebalanceMemoryConsumers();
-
-      KeWaitForSingleObject(&Request.Event,
-                            0,
-                            KernelMode,
-                            FALSE,
-                            NULL);
-
-      Page = Request.Page;
-      if (Page == 0)
-      {
-         KeBugCheck(NO_PAGES_AVAILABLE);
-      }
-
-      if(Consumer == MC_USER) MmInsertLRULastUserPage(Page);
-      *AllocatedPage = Page;
-
-      if (MmAvailablePages < MiMinimumAvailablePages)
-      {
-          MmRebalanceMemoryConsumers();
-      }
-
-      return(STATUS_SUCCESS);
-   }
-
-   /*
-    * Actually allocate the page.
-    */
-   OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-   Page = MmAllocPage(Consumer);
-   KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
-   if (Page == 0)
-   {
-      KeBugCheck(NO_PAGES_AVAILABLE);
-   }
-   if(Consumer == MC_USER) MmInsertLRULastUserPage(Page);
-   *AllocatedPage = Page;
-
-   if (MmAvailablePages < MiMinimumAvailablePages)
-   {
-       MmRebalanceMemoryConsumers();
-   }
-
-   return(STATUS_SUCCESS);
+    ULONG PagesUsed;
+    PFN_NUMBER Page;
+    KIRQL OldIrql;
+
+    /*
+     * Make sure we don't exceed our individual target.
+     */
+    PagesUsed = InterlockedIncrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
+    if (PagesUsed > MiMemoryConsumers[Consumer].PagesTarget &&
+            !MiIsBalancerThread())
+    {
+        MmRebalanceMemoryConsumers();
+    }
+
+    /*
+     * Allocate always memory for the non paged pool and for the pager thread.
+     */
+    if ((Consumer == MC_SYSTEM) || MiIsBalancerThread())
+    {
+        OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+        Page = MmAllocPage(Consumer);
+        KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
+        if (Page == 0)
+        {
+            KeBugCheck(NO_PAGES_AVAILABLE);
+        }
+        if (Consumer == MC_USER) MmInsertLRULastUserPage(Page);
+        *AllocatedPage = Page;
+        if (MmAvailablePages < MiMinimumAvailablePages)
+            MmRebalanceMemoryConsumers();
+        return(STATUS_SUCCESS);
+    }
+
+    /*
+     * Make sure we don't exceed global targets.
+     */
+    if (MmAvailablePages < MiMinimumAvailablePages)
+    {
+        MM_ALLOCATION_REQUEST Request;
+
+        if (!CanWait)
+        {
+            (void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
+            MmRebalanceMemoryConsumers();
+            return(STATUS_NO_MEMORY);
+        }
+
+        /* Insert an allocation request. */
+        Request.Page = 0;
+        KeInitializeEvent(&Request.Event, NotificationEvent, FALSE);
+
+        ExInterlockedInsertTailList(&AllocationListHead, &Request.ListEntry, &AllocationListLock);
+        MmRebalanceMemoryConsumers();
+
+        KeWaitForSingleObject(&Request.Event,
+                              0,
+                              KernelMode,
+                              FALSE,
+                              NULL);
+
+        Page = Request.Page;
+        if (Page == 0)
+        {
+            KeBugCheck(NO_PAGES_AVAILABLE);
+        }
+
+        if(Consumer == MC_USER) MmInsertLRULastUserPage(Page);
+        *AllocatedPage = Page;
+
+        if (MmAvailablePages < MiMinimumAvailablePages)
+        {
+            MmRebalanceMemoryConsumers();
+        }
+
+        return(STATUS_SUCCESS);
+    }
+
+    /*
+     * Actually allocate the page.
+     */
+    OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+    Page = MmAllocPage(Consumer);
+    KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
+    if (Page == 0)
+    {
+        KeBugCheck(NO_PAGES_AVAILABLE);
+    }
+    if(Consumer == MC_USER) MmInsertLRULastUserPage(Page);
+    *AllocatedPage = Page;
+
+    if (MmAvailablePages < MiMinimumAvailablePages)
+    {
+        MmRebalanceMemoryConsumers();
+    }
+
+    return(STATUS_SUCCESS);
 }
 
 
 VOID NTAPI
 MiBalancerThread(PVOID Unused)
 {
-   PVOID WaitObjects[2];
-   NTSTATUS Status;
-   ULONG i;
-
-   WaitObjects[0] = &MiBalancerEvent;
-   WaitObjects[1] = &MiBalancerTimer;
-
-   while (1)
-   {
-      Status = KeWaitForMultipleObjects(2,
-                                        WaitObjects,
-                                        WaitAny,
-                                        Executive,
-                                        KernelMode,
-                                        FALSE,
-                                        NULL,
-                                        NULL);
-
-      if (Status == STATUS_WAIT_0 || Status == STATUS_WAIT_1)
-      {
-        ULONG InitialTarget = 0;
+    PVOID WaitObjects[2];
+    NTSTATUS Status;
+    ULONG i;
 
-#if (_MI_PAGING_LEVELS == 2)
-        if (!MiIsBalancerThread())
+    WaitObjects[0] = &MiBalancerEvent;
+    WaitObjects[1] = &MiBalancerTimer;
+
+    while (1)
+    {
+        Status = KeWaitForMultipleObjects(2,
+                                          WaitObjects,
+                                          WaitAny,
+                                          Executive,
+                                          KernelMode,
+                                          FALSE,
+                                          NULL,
+                                          NULL);
+
+        if (Status == STATUS_WAIT_0 || Status == STATUS_WAIT_1)
         {
-            /* Clean up the unused PDEs */
-            ULONG_PTR Address;
-            PEPROCESS Process = PsGetCurrentProcess();
-
-            /* Acquire PFN lock */
-            KIRQL OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-            PMMPDE pointerPde;
-            for (Address = (ULONG_PTR)MI_LOWEST_VAD_ADDRESS;
-                 Address < (ULONG_PTR)MM_HIGHEST_VAD_ADDRESS;
-                 Address += (PAGE_SIZE * PTE_COUNT))
+            ULONG InitialTarget = 0;
+
+#if (_MI_PAGING_LEVELS == 2)
+            if (!MiIsBalancerThread())
             {
-                if (MiQueryPageTableReferences((PVOID)Address) == 0)
+                /* Clean up the unused PDEs */
+                ULONG_PTR Address;
+                PEPROCESS Process = PsGetCurrentProcess();
+
+                /* Acquire PFN lock */
+                KIRQL OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+                PMMPDE pointerPde;
+                for (Address = (ULONG_PTR)MI_LOWEST_VAD_ADDRESS;
+                        Address < (ULONG_PTR)MM_HIGHEST_VAD_ADDRESS;
+                        Address += (PAGE_SIZE * PTE_COUNT))
                 {
-                    pointerPde = MiAddressToPde(Address);
-                    if (pointerPde->u.Hard.Valid)
-                        MiDeletePte(pointerPde, MiPdeToPte(pointerPde), Process, NULL);
-                    ASSERT(pointerPde->u.Hard.Valid == 0);
+                    if (MiQueryPageTableReferences((PVOID)Address) == 0)
+                    {
+                        pointerPde = MiAddressToPde(Address);
+                        if (pointerPde->u.Hard.Valid)
+                            MiDeletePte(pointerPde, MiPdeToPte(pointerPde), Process, NULL);
+                        ASSERT(pointerPde->u.Hard.Valid == 0);
+                    }
                 }
+                /* Release lock */
+                KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
             }
-            /* Release lock */
-            KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
-        }
 #endif
-          do
-          {
-              ULONG OldTarget = InitialTarget;
-
-              /* Trim each consumer */
-              for (i = 0; i < MC_MAXIMUM; i++)
-              {
-                  InitialTarget = MiTrimMemoryConsumer(i, InitialTarget);
-              }
-
-              /* No pages left to swap! */
-              if (InitialTarget != 0 &&
-                  InitialTarget == OldTarget)
-              {
-                  /* Game over */
-                  KeBugCheck(NO_PAGES_AVAILABLE);
-              }
-          } while (InitialTarget != 0);
-      }
-      else
-      {
-         DPRINT1("KeWaitForMultipleObjects failed, status = %x\n", Status);
-         KeBugCheck(MEMORY_MANAGEMENT);
-      }
-   }
+            do
+            {
+                ULONG OldTarget = InitialTarget;
+
+                /* Trim each consumer */
+                for (i = 0; i < MC_MAXIMUM; i++)
+                {
+                    InitialTarget = MiTrimMemoryConsumer(i, InitialTarget);
+                }
+
+                /* No pages left to swap! */
+                if (InitialTarget != 0 &&
+                        InitialTarget == OldTarget)
+                {
+                    /* Game over */
+                    KeBugCheck(NO_PAGES_AVAILABLE);
+                }
+            }
+            while (InitialTarget != 0);
+        }
+        else
+        {
+            DPRINT1("KeWaitForMultipleObjects failed, status = %x\n", Status);
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+    }
 }
 
 VOID
@@ -439,44 +440,44 @@ INIT_FUNCTION
 NTAPI
 MiInitBalancerThread(VOID)
 {
-   KPRIORITY Priority;
-   NTSTATUS Status;
+    KPRIORITY Priority;
+    NTSTATUS Status;
 #if !defined(__GNUC__)
 
-   LARGE_INTEGER dummyJunkNeeded;
-   dummyJunkNeeded.QuadPart = -20000000; /* 2 sec */
-   ;
+    LARGE_INTEGER dummyJunkNeeded;
+    dummyJunkNeeded.QuadPart = -20000000; /* 2 sec */
+    ;
 #endif
 
 
-   KeInitializeEvent(&MiBalancerEvent, SynchronizationEvent, FALSE);
-   KeInitializeTimerEx(&MiBalancerTimer, SynchronizationTimer);
-   KeSetTimerEx(&MiBalancerTimer,
+    KeInitializeEvent(&MiBalancerEvent, SynchronizationEvent, FALSE);
+    KeInitializeTimerEx(&MiBalancerTimer, SynchronizationTimer);
+    KeSetTimerEx(&MiBalancerTimer,
 #if defined(__GNUC__)
-                (LARGE_INTEGER)(LONGLONG)-20000000LL,     /* 2 sec */
+                 (LARGE_INTEGER)(LONGLONG)-20000000LL,     /* 2 sec */
 #else
-                dummyJunkNeeded,
+                 dummyJunkNeeded,
 #endif
-                2000,         /* 2 sec */
-                NULL);
-
-   Status = PsCreateSystemThread(&MiBalancerThreadHandle,
-                                 THREAD_ALL_ACCESS,
-                                 NULL,
-                                 NULL,
-                                 &MiBalancerThreadId,
-                                 MiBalancerThread,
-                                 NULL);
-   if (!NT_SUCCESS(Status))
-   {
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   Priority = LOW_REALTIME_PRIORITY + 1;
-   NtSetInformationThread(MiBalancerThreadHandle,
-                          ThreadPriority,
-                          &Priority,
-                          sizeof(Priority));
+                 2000,         /* 2 sec */
+                 NULL);
+
+    Status = PsCreateSystemThread(&MiBalancerThreadHandle,
+                                  THREAD_ALL_ACCESS,
+                                  NULL,
+                                  NULL,
+                                  &MiBalancerThreadId,
+                                  MiBalancerThread,
+                                  NULL);
+    if (!NT_SUCCESS(Status))
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    Priority = LOW_REALTIME_PRIORITY + 1;
+    NtSetInformationThread(MiBalancerThreadHandle,
+                           ThreadPriority,
+                           &Priority,
+                           sizeof(Priority));
 
 }
 
index e65ba48..568ba13 100644 (file)
@@ -448,76 +448,76 @@ VOID
 NTAPI
 MmSetSavedSwapEntryPage(PFN_NUMBER Pfn,  SWAPENTRY SwapEntry)
 {
-   KIRQL oldIrql;
-   PMMPFN Pfn1;
+    KIRQL oldIrql;
+    PMMPFN Pfn1;
 
-   Pfn1 = MiGetPfnEntry(Pfn);
-   ASSERT(Pfn1);
-   ASSERT_IS_ROS_PFN(Pfn1);
+    Pfn1 = MiGetPfnEntry(Pfn);
+    ASSERT(Pfn1);
+    ASSERT_IS_ROS_PFN(Pfn1);
 
-   oldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-   Pfn1->u1.SwapEntry = SwapEntry;
-   KeReleaseQueuedSpinLock(LockQueuePfnLock, oldIrql);
+    oldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+    Pfn1->u1.SwapEntry = SwapEntry;
+    KeReleaseQueuedSpinLock(LockQueuePfnLock, oldIrql);
 }
 
 SWAPENTRY
 NTAPI
 MmGetSavedSwapEntryPage(PFN_NUMBER Pfn)
 {
-   SWAPENTRY SwapEntry;
-   KIRQL oldIrql;
-   PMMPFN Pfn1;
+    SWAPENTRY SwapEntry;
+    KIRQL oldIrql;
+    PMMPFN Pfn1;
 
-   Pfn1 = MiGetPfnEntry(Pfn);
-   ASSERT(Pfn1);
-   ASSERT_IS_ROS_PFN(Pfn1);
+    Pfn1 = MiGetPfnEntry(Pfn);
+    ASSERT(Pfn1);
+    ASSERT_IS_ROS_PFN(Pfn1);
 
-   oldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-   SwapEntry = Pfn1->u1.SwapEntry;
-   KeReleaseQueuedSpinLock(LockQueuePfnLock, oldIrql);
+    oldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+    SwapEntry = Pfn1->u1.SwapEntry;
+    KeReleaseQueuedSpinLock(LockQueuePfnLock, oldIrql);
 
-   return(SwapEntry);
+    return(SwapEntry);
 }
 
 VOID
 NTAPI
 MmReferencePage(PFN_NUMBER Pfn)
 {
-   PMMPFN Pfn1;
+    PMMPFN Pfn1;
 
-   DPRINT("MmReferencePage(PysicalAddress %x)\n", Pfn << PAGE_SHIFT);
+    DPRINT("MmReferencePage(PysicalAddress %x)\n", Pfn << PAGE_SHIFT);
 
-   ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
-   ASSERT(Pfn != 0);
-   ASSERT(Pfn <= MmHighestPhysicalPage);
+    ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
+    ASSERT(Pfn != 0);
+    ASSERT(Pfn <= MmHighestPhysicalPage);
 
-   Pfn1 = MiGetPfnEntry(Pfn);
-   ASSERT(Pfn1);
-   ASSERT_IS_ROS_PFN(Pfn1);
+    Pfn1 = MiGetPfnEntry(Pfn);
+    ASSERT(Pfn1);
+    ASSERT_IS_ROS_PFN(Pfn1);
 
-   ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
-   Pfn1->u3.e2.ReferenceCount++;
+    ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
+    Pfn1->u3.e2.ReferenceCount++;
 }
 
 ULONG
 NTAPI
 MmGetReferenceCountPage(PFN_NUMBER Pfn)
 {
-   KIRQL oldIrql;
-   ULONG RCount;
-   PMMPFN Pfn1;
+    KIRQL oldIrql;
+    ULONG RCount;
+    PMMPFN Pfn1;
 
-   DPRINT("MmGetReferenceCountPage(PhysicalAddress %x)\n", Pfn << PAGE_SHIFT);
+    DPRINT("MmGetReferenceCountPage(PhysicalAddress %x)\n", Pfn << PAGE_SHIFT);
 
-   oldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-   Pfn1 = MiGetPfnEntry(Pfn);
-   ASSERT(Pfn1);
-   ASSERT_IS_ROS_PFN(Pfn1);
+    oldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+    Pfn1 = MiGetPfnEntry(Pfn);
+    ASSERT(Pfn1);
+    ASSERT_IS_ROS_PFN(Pfn1);
 
-   RCount = Pfn1->u3.e2.ReferenceCount;
+    RCount = Pfn1->u3.e2.ReferenceCount;
 
-   KeReleaseQueuedSpinLock(LockQueuePfnLock, oldIrql);
-   return(RCount);
+    KeReleaseQueuedSpinLock(LockQueuePfnLock, oldIrql);
+    return(RCount);
 }
 
 BOOLEAN
@@ -531,17 +531,17 @@ VOID
 NTAPI
 MmDereferencePage(PFN_NUMBER Pfn)
 {
-   PMMPFN Pfn1;
-   DPRINT("MmDereferencePage(PhysicalAddress %x)\n", Pfn << PAGE_SHIFT);
+    PMMPFN Pfn1;
+    DPRINT("MmDereferencePage(PhysicalAddress %x)\n", Pfn << PAGE_SHIFT);
 
-   Pfn1 = MiGetPfnEntry(Pfn);
-   ASSERT(Pfn1);
-   ASSERT_IS_ROS_PFN(Pfn1);
+    Pfn1 = MiGetPfnEntry(Pfn);
+    ASSERT(Pfn1);
+    ASSERT_IS_ROS_PFN(Pfn1);
 
-   ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
-   Pfn1->u3.e2.ReferenceCount--;
-   if (Pfn1->u3.e2.ReferenceCount == 0)
-   {
+    ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
+    Pfn1->u3.e2.ReferenceCount--;
+    if (Pfn1->u3.e2.ReferenceCount == 0)
+    {
         /* Mark the page temporarily as valid, we're going to make it free soon */
         Pfn1->u3.e1.PageLocation = ActiveAndValid;
 
@@ -551,37 +551,37 @@ MmDereferencePage(PFN_NUMBER Pfn)
         /* Bring it back into the free list */
         DPRINT("Legacy free: %lx\n", Pfn);
         MiInsertPageInFreeList(Pfn);
-   }
+    }
 }
 
 PFN_NUMBER
 NTAPI
 MmAllocPage(ULONG Type)
 {
-   PFN_NUMBER PfnOffset;
-   PMMPFN Pfn1;
+    PFN_NUMBER PfnOffset;
+    PMMPFN Pfn1;
 
-   PfnOffset = MiRemoveZeroPage(MI_GET_NEXT_COLOR());
+    PfnOffset = MiRemoveZeroPage(MI_GET_NEXT_COLOR());
 
-   if (!PfnOffset)
-   {
-       DPRINT1("MmAllocPage(): Out of memory\n");
-       return 0;
-   }
+    if (!PfnOffset)
+    {
+        DPRINT1("MmAllocPage(): Out of memory\n");
+        return 0;
+    }
 
-   DPRINT("Legacy allocate: %lx\n", PfnOffset);
-   Pfn1 = MiGetPfnEntry(PfnOffset);
-   Pfn1->u3.e2.ReferenceCount = 1;
-   Pfn1->u3.e1.PageLocation = ActiveAndValid;
+    DPRINT("Legacy allocate: %lx\n", PfnOffset);
+    Pfn1 = MiGetPfnEntry(PfnOffset);
+    Pfn1->u3.e2.ReferenceCount = 1;
+    Pfn1->u3.e1.PageLocation = ActiveAndValid;
 
-   /* This marks the PFN as a ReactOS PFN */
-   Pfn1->u4.AweAllocation = TRUE;
+    /* This marks the PFN as a ReactOS PFN */
+    Pfn1->u4.AweAllocation = TRUE;
 
-   /* Allocate the extra ReactOS Data and zero it out */
-   Pfn1->u1.SwapEntry = 0;
-   Pfn1->RmapListHead = NULL;
+    /* Allocate the extra ReactOS Data and zero it out */
+    Pfn1->u1.SwapEntry = 0;
+    Pfn1->RmapListHead = NULL;
 
-   return PfnOffset;
+    return PfnOffset;
 }
 
 /* EOF */
index 12a7afa..39fc28e 100644 (file)
@@ -65,10 +65,10 @@ ULONG MiStaticMemoryAreaCount;
 
 static PMEMORY_AREA MmIterateFirstNode(PMEMORY_AREA Node)
 {
-   while (Node->LeftChild != NULL)
-      Node = Node->LeftChild;
+    while (Node->LeftChild != NULL)
+        Node = Node->LeftChild;
 
-   return Node;
+    return Node;
 }
 
 /**
@@ -82,28 +82,28 @@ static PMEMORY_AREA MmIterateFirstNode(PMEMORY_AREA Node)
 
 static PMEMORY_AREA MmIterateNextNode(PMEMORY_AREA Node)
 {
-   if (Node->RightChild != NULL)
-   {
-      Node = Node->RightChild;
-      while (Node->LeftChild != NULL)
-         Node = Node->LeftChild;
-   }
-   else
-   {
-      PMEMORY_AREA TempNode = NULL;
-
-      do
-      {
-         /* Check if we're at the end of tree. */
-         if (Node->Parent == NULL)
-            return NULL;
+    if (Node->RightChild != NULL)
+    {
+        Node = Node->RightChild;
+        while (Node->LeftChild != NULL)
+            Node = Node->LeftChild;
+    }
+    else
+    {
+        PMEMORY_AREA TempNode = NULL;
 
-         TempNode = Node;
-         Node = Node->Parent;
-      }
-      while (TempNode == Node->RightChild);
-   }
-   return Node;
+        do
+        {
+            /* Check if we're at the end of tree. */
+            if (Node->Parent == NULL)
+                return NULL;
+
+            TempNode = Node;
+            Node = Node->Parent;
+        }
+        while (TempNode == Node->RightChild);
+    }
+    return Node;
 }
 
 /**
@@ -118,10 +118,10 @@ static PMEMORY_AREA MmIterateNextNode(PMEMORY_AREA Node)
 
 static PMEMORY_AREA MmIterateLastNode(PMEMORY_AREA Node)
 {
-   while (Node->RightChild != NULL)
-      Node = Node->RightChild;
+    while (Node->RightChild != NULL)
+        Node = Node->RightChild;
 
-   return Node;
+    return Node;
 }
 
 /**
@@ -135,108 +135,108 @@ static PMEMORY_AREA MmIterateLastNode(PMEMORY_AREA Node)
 
 static PMEMORY_AREA MmIteratePrevNode(PMEMORY_AREA Node)
 {
-   if (Node->LeftChild != NULL)
-   {
-      Node = Node->LeftChild;
-      while (Node->RightChild != NULL)
-         Node = Node->RightChild;
-   }
-   else
-   {
-      PMEMORY_AREA TempNode = NULL;
-
-      do
-      {
-         /* Check if we're at the end of tree. */
-         if (Node->Parent == NULL)
-            return NULL;
+    if (Node->LeftChild != NULL)
+    {
+        Node = Node->LeftChild;
+        while (Node->RightChild != NULL)
+            Node = Node->RightChild;
+    }
+    else
+    {
+        PMEMORY_AREA TempNode = NULL;
+
+        do
+        {
+            /* Check if we're at the end of tree. */
+            if (Node->Parent == NULL)
+                return NULL;
 
-         TempNode = Node;
-         Node = Node->Parent;
-      }
-      while (TempNode == Node->LeftChild);
-   }
-   return Node;
+            TempNode = Node;
+            Node = Node->Parent;
+        }
+        while (TempNode == Node->LeftChild);
+    }
+    return Node;
 }
 
 PMEMORY_AREA NTAPI
 MmLocateMemoryAreaByAddress(
-   PMMSUPPORT AddressSpace,
-   PVOID Address)
+    PMMSUPPORT AddressSpace,
+    PVOID Address)
 {
-   PMEMORY_AREA Node = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
+    PMEMORY_AREA Node = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
 
-   DPRINT("MmLocateMemoryAreaByAddress(AddressSpace %p, Address %p)\n",
+    DPRINT("MmLocateMemoryAreaByAddress(AddressSpace %p, Address %p)\n",
            AddressSpace, Address);
 
-   while (Node != NULL)
-   {
-      if (Address < Node->StartingAddress)
-         Node = Node->LeftChild;
-      else if (Address >= Node->EndingAddress)
-         Node = Node->RightChild;
-      else
-      {
-         DPRINT("MmLocateMemoryAreaByAddress(%p): %p [%p - %p]\n",
-                Address, Node, Node->StartingAddress, Node->EndingAddress);
-         return Node;
-      }
-   }
-
-   DPRINT("MmLocateMemoryAreaByAddress(%p): 0\n", Address);
-   return NULL;
+    while (Node != NULL)
+    {
+        if (Address < Node->StartingAddress)
+            Node = Node->LeftChild;
+        else if (Address >= Node->EndingAddress)
+            Node = Node->RightChild;
+        else
+        {
+            DPRINT("MmLocateMemoryAreaByAddress(%p): %p [%p - %p]\n",
+                   Address, Node, Node->StartingAddress, Node->EndingAddress);
+            return Node;
+        }
+    }
+
+    DPRINT("MmLocateMemoryAreaByAddress(%p): 0\n", Address);
+    return NULL;
 }
 
 PMEMORY_AREA NTAPI
 MmLocateMemoryAreaByRegion(
-   PMMSUPPORT AddressSpace,
-   PVOID Address,
-   ULONG_PTR Length)
+    PMMSUPPORT AddressSpace,
+    PVOID Address,
+    ULONG_PTR Length)
 {
-   PMEMORY_AREA Node;
-   PVOID Extent = (PVOID)((ULONG_PTR)Address + Length);
-
-   /* Special case for empty tree. */
-   if (AddressSpace->WorkingSetExpansionLinks.Flink == NULL)
-      return NULL;
-
-   /* Traverse the tree from left to right. */
-   for (Node = MmIterateFirstNode((PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink);
-        Node != NULL;
-        Node = MmIterateNextNode(Node))
-   {
-      if (Node->StartingAddress >= Address &&
-          Node->StartingAddress < Extent)
-      {
-         DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
-                Address, (ULONG_PTR)Address + Length, Node->StartingAddress,
-                Node->EndingAddress);
-         return Node;
-      }
-      if (Node->EndingAddress > Address &&
-          Node->EndingAddress < Extent)
-      {
-         DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
-                Address, (ULONG_PTR)Address + Length, Node->StartingAddress,
-                Node->EndingAddress);
-         return Node;
-      }
-      if (Node->StartingAddress <= Address &&
-          Node->EndingAddress >= Extent)
-      {
-         DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
-                Address, (ULONG_PTR)Address + Length, Node->StartingAddress,
-                Node->EndingAddress);
-         return Node;
-      }
-      if (Node->StartingAddress >= Extent)
-      {
-         DPRINT("Finished MmLocateMemoryAreaByRegion() = NULL\n");
-         return NULL;
-      }
-   }
-
-   return NULL;
+    PMEMORY_AREA Node;
+    PVOID Extent = (PVOID)((ULONG_PTR)Address + Length);
+
+    /* Special case for empty tree. */
+    if (AddressSpace->WorkingSetExpansionLinks.Flink == NULL)
+        return NULL;
+
+    /* Traverse the tree from left to right. */
+    for (Node = MmIterateFirstNode((PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink);
+            Node != NULL;
+            Node = MmIterateNextNode(Node))
+    {
+        if (Node->StartingAddress >= Address &&
+                Node->StartingAddress < Extent)
+        {
+            DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
+                   Address, (ULONG_PTR)Address + Length, Node->StartingAddress,
+                   Node->EndingAddress);
+            return Node;
+        }
+        if (Node->EndingAddress > Address &&
+                Node->EndingAddress < Extent)
+        {
+            DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
+                   Address, (ULONG_PTR)Address + Length, Node->StartingAddress,
+                   Node->EndingAddress);
+            return Node;
+        }
+        if (Node->StartingAddress <= Address &&
+                Node->EndingAddress >= Extent)
+        {
+            DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
+                   Address, (ULONG_PTR)Address + Length, Node->StartingAddress,
+                   Node->EndingAddress);
+            return Node;
+        }
+        if (Node->StartingAddress >= Extent)
+        {
+            DPRINT("Finished MmLocateMemoryAreaByRegion() = NULL\n");
+            return NULL;
+        }
+    }
+
+    return NULL;
 }
 
 /**
@@ -248,33 +248,33 @@ MmLocateMemoryAreaByRegion(
 
 static VOID
 MmCompressHelper(
-   PMMSUPPORT AddressSpace,
-   ULONG Count)
+    PMMSUPPORT AddressSpace,
+    ULONG Count)
 {
-   PMEMORY_AREA Root = NULL;
-   PMEMORY_AREA Red = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
-   PMEMORY_AREA Black = Red->LeftChild;
-
-   while (Count--)
-   {
-      if (Root)
-         Root->LeftChild = Black;
-      else
-         AddressSpace->WorkingSetExpansionLinks.Flink = (PVOID)Black;
-      Black->Parent = Root;
-      Red->LeftChild = Black->RightChild;
-      if (Black->RightChild)
-         Black->RightChild->Parent = Red;
-      Black->RightChild = Red;
-      Red->Parent = Black;
-      Root = Black;
-
-      if (Count)
-      {
-         Red = Root->LeftChild;
-         Black = Red->LeftChild;
-      }
-   }
+    PMEMORY_AREA Root = NULL;
+    PMEMORY_AREA Red = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
+    PMEMORY_AREA Black = Red->LeftChild;
+
+    while (Count--)
+    {
+        if (Root)
+            Root->LeftChild = Black;
+        else
+            AddressSpace->WorkingSetExpansionLinks.Flink = (PVOID)Black;
+        Black->Parent = Root;
+        Red->LeftChild = Black->RightChild;
+        if (Black->RightChild)
+            Black->RightChild->Parent = Red;
+        Black->RightChild = Red;
+        Red->Parent = Black;
+        Root = Black;
+
+        if (Count)
+        {
+            Red = Root->LeftChild;
+            Black = Red->LeftChild;
+        }
+    }
 }
 
 /**
@@ -287,77 +287,77 @@ MmCompressHelper(
 
 static VOID
 MmRebalanceTree(
-   PMMSUPPORT AddressSpace)
+    PMMSUPPORT AddressSpace)
 {
-   PMEMORY_AREA PreviousNode;
-   PMEMORY_AREA CurrentNode;
-   PMEMORY_AREA TempNode;
-   ULONG NodeCount = 0;
-   ULONG Vine;   /* Number of nodes in main vine. */
-   ULONG Leaves; /* Nodes in incomplete bottom level, if any. */
-   INT Height;   /* Height of produced balanced tree. */
-
-   /* Transform the tree into Vine. */
-
-   PreviousNode = NULL;
-   CurrentNode = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
-   while (CurrentNode != NULL)
-   {
-      if (CurrentNode->RightChild == NULL)
-      {
-         PreviousNode = CurrentNode;
-         CurrentNode = CurrentNode->LeftChild;
-         NodeCount++;
-      }
-      else
-      {
-         TempNode = CurrentNode->RightChild;
-
-         CurrentNode->RightChild = TempNode->LeftChild;
-         if (TempNode->LeftChild)
-            TempNode->LeftChild->Parent = CurrentNode;
-
-         TempNode->LeftChild = CurrentNode;
-         CurrentNode->Parent = TempNode;
-
-         CurrentNode = TempNode;
-
-         if (PreviousNode != NULL)
-            PreviousNode->LeftChild = TempNode;
-         else
-            AddressSpace->WorkingSetExpansionLinks.Flink = (PVOID)TempNode;
-         TempNode->Parent = PreviousNode;
-      }
-   }
-
-   /* Transform Vine back into a balanced tree. */
-
-   Leaves = NodeCount + 1;
-   for (;;)
-   {
-      ULONG Next = Leaves & (Leaves - 1);
-      if (Next == 0)
-         break;
-      Leaves = Next;
-   }
-   Leaves = NodeCount + 1 - Leaves;
-
-   MmCompressHelper(AddressSpace, Leaves);
-
-   Vine = NodeCount - Leaves;
-   Height = 1 + (Leaves > 0);
-   while (Vine > 1)
-   {
-      MmCompressHelper(AddressSpace, Vine / 2);
-      Vine /= 2;
-      Height++;
-   }
+    PMEMORY_AREA PreviousNode;
+    PMEMORY_AREA CurrentNode;
+    PMEMORY_AREA TempNode;
+    ULONG NodeCount = 0;
+    ULONG Vine;   /* Number of nodes in main vine. */
+    ULONG Leaves; /* Nodes in incomplete bottom level, if any. */
+    INT Height;   /* Height of produced balanced tree. */
+
+    /* Transform the tree into Vine. */
+
+    PreviousNode = NULL;
+    CurrentNode = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
+    while (CurrentNode != NULL)
+    {
+        if (CurrentNode->RightChild == NULL)
+        {
+            PreviousNode = CurrentNode;
+            CurrentNode = CurrentNode->LeftChild;
+            NodeCount++;
+        }
+        else
+        {
+            TempNode = CurrentNode->RightChild;
+
+            CurrentNode->RightChild = TempNode->LeftChild;
+            if (TempNode->LeftChild)
+                TempNode->LeftChild->Parent = CurrentNode;
+
+            TempNode->LeftChild = CurrentNode;
+            CurrentNode->Parent = TempNode;
+
+            CurrentNode = TempNode;
+
+            if (PreviousNode != NULL)
+                PreviousNode->LeftChild = TempNode;
+            else
+                AddressSpace->WorkingSetExpansionLinks.Flink = (PVOID)TempNode;
+            TempNode->Parent = PreviousNode;
+        }
+    }
+
+    /* Transform Vine back into a balanced tree. */
+
+    Leaves = NodeCount + 1;
+    for (;;)
+    {
+        ULONG Next = Leaves & (Leaves - 1);
+        if (Next == 0)
+            break;
+        Leaves = Next;
+    }
+    Leaves = NodeCount + 1 - Leaves;
+
+    MmCompressHelper(AddressSpace, Leaves);
+
+    Vine = NodeCount - Leaves;
+    Height = 1 + (Leaves > 0);
+    while (Vine > 1)
+    {
+        MmCompressHelper(AddressSpace, Vine / 2);
+        Vine /= 2;
+        Height++;
+    }
 }
 
 VOID
 NTAPI
 MiInsertVad(IN PMMVAD Vad,
-IN PEPROCESS Process);
+            IN PEPROCESS Process);
 
 ULONG
 NTAPI
@@ -367,29 +367,29 @@ MiMakeProtectionMask(
 
 static VOID
 MmInsertMemoryArea(
-   PMMSUPPORT AddressSpace,
-   PMEMORY_AREA marea)
+    PMMSUPPORT AddressSpace,
+    PMEMORY_AREA marea)
 {
-   PMEMORY_AREA Node;
-   PMEMORY_AREA PreviousNode;
-   ULONG Depth = 0;
-   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
-
-   /* Build a lame VAD if this is a user-space allocation */
-   if ((marea->EndingAddress < MmSystemRangeStart) && (marea->Type != MEMORY_AREA_OWNED_BY_ARM3))
-   {
-       PMMVAD Vad;
-
-       ASSERT(marea->Type == MEMORY_AREA_SECTION_VIEW || marea->Type == MEMORY_AREA_CACHE);
-       Vad = ExAllocatePoolWithTag(NonPagedPool, sizeof(MMVAD), TAG_MVAD);
-       ASSERT(Vad);
-       RtlZeroMemory(Vad, sizeof(MMVAD));
-       Vad->StartingVpn = PAGE_ROUND_DOWN(marea->StartingAddress) >> PAGE_SHIFT;
-       /*
-        * For some strange reason, it is perfectly valid to create a MAREA from 0x1000 to... 0x1000.
-        * In a normal OS/Memory Manager, this would be retarded, but ReactOS allows this (how it works
-        * I don't even want to know).
-        */
+    PMEMORY_AREA Node;
+    PMEMORY_AREA PreviousNode;
+    ULONG Depth = 0;
+    PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+
+    /* Build a lame VAD if this is a user-space allocation */
+    if ((marea->EndingAddress < MmSystemRangeStart) && (marea->Type != MEMORY_AREA_OWNED_BY_ARM3))
+    {
+        PMMVAD Vad;
+
+        ASSERT(marea->Type == MEMORY_AREA_SECTION_VIEW || marea->Type == MEMORY_AREA_CACHE);
+        Vad = ExAllocatePoolWithTag(NonPagedPool, sizeof(MMVAD), TAG_MVAD);
+        ASSERT(Vad);
+        RtlZeroMemory(Vad, sizeof(MMVAD));
+        Vad->StartingVpn = PAGE_ROUND_DOWN(marea->StartingAddress) >> PAGE_SHIFT;
+        /*
+         * For some strange reason, it is perfectly valid to create a MAREA from 0x1000 to... 0x1000.
+         * In a normal OS/Memory Manager, this would be retarded, but ReactOS allows this (how it works
+         * I don't even want to know).
+         */
         if (marea->EndingAddress != marea->StartingAddress)
         {
             Vad->EndingVpn = PAGE_ROUND_DOWN((ULONG_PTR)marea->EndingAddress - 1) >> PAGE_SHIFT;
@@ -398,68 +398,68 @@ MmInsertMemoryArea(
         {
             Vad->EndingVpn = Vad->StartingVpn;
         }
-       Vad->u.VadFlags.Spare = 1;
-       Vad->u.VadFlags.Protection = MiMakeProtectionMask(marea->Protect);
-
-       /* Insert the VAD */
-       MiInsertVad(Vad, Process);
-       marea->Vad = Vad;
-   }
-   else
-   {
-       marea->Vad = NULL;
-   }
-
-   if (AddressSpace->WorkingSetExpansionLinks.Flink == NULL)
-   {
-      AddressSpace->WorkingSetExpansionLinks.Flink = (PVOID)marea;
-      marea->LeftChild = marea->RightChild = marea->Parent = NULL;
-      return;
-   }
-
-   Node = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
-   do
-   {
-      DPRINT("marea->EndingAddress: %p Node->StartingAddress: %p\n",
-             marea->EndingAddress, Node->StartingAddress);
-      DPRINT("marea->StartingAddress: %p Node->EndingAddress: %p\n",
-             marea->StartingAddress, Node->EndingAddress);
-      ASSERT(marea->EndingAddress <= Node->StartingAddress ||
-             marea->StartingAddress >= Node->EndingAddress);
-      ASSERT(marea->StartingAddress != Node->StartingAddress);
-
-      PreviousNode = Node;
-
-      if (marea->StartingAddress < Node->StartingAddress)
-         Node = Node->LeftChild;
-      else
-         Node = Node->RightChild;
-
-      if (Node)
-      {
-         Depth++;
-         if (Depth == 22)
-         {
-            MmRebalanceTree(AddressSpace);
-            PreviousNode = Node->Parent;
-         }
-      }
-   }
-   while (Node != NULL);
-
-   marea->LeftChild = marea->RightChild = NULL;
-   marea->Parent = PreviousNode;
-   if (marea->StartingAddress < PreviousNode->StartingAddress)
-      PreviousNode->LeftChild = marea;
-   else
-      PreviousNode->RightChild = marea;
+        Vad->u.VadFlags.Spare = 1;
+        Vad->u.VadFlags.Protection = MiMakeProtectionMask(marea->Protect);
+
+        /* Insert the VAD */
+        MiInsertVad(Vad, Process);
+        marea->Vad = Vad;
+    }
+    else
+    {
+        marea->Vad = NULL;
+    }
+
+    if (AddressSpace->WorkingSetExpansionLinks.Flink == NULL)
+    {
+        AddressSpace->WorkingSetExpansionLinks.Flink = (PVOID)marea;
+        marea->LeftChild = marea->RightChild = marea->Parent = NULL;
+        return;
+    }
+
+    Node = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
+    do
+    {
+        DPRINT("marea->EndingAddress: %p Node->StartingAddress: %p\n",
+               marea->EndingAddress, Node->StartingAddress);
+        DPRINT("marea->StartingAddress: %p Node->EndingAddress: %p\n",
+               marea->StartingAddress, Node->EndingAddress);
+        ASSERT(marea->EndingAddress <= Node->StartingAddress ||
+               marea->StartingAddress >= Node->EndingAddress);
+        ASSERT(marea->StartingAddress != Node->StartingAddress);
+
+        PreviousNode = Node;
+
+        if (marea->StartingAddress < Node->StartingAddress)
+            Node = Node->LeftChild;
+        else
+            Node = Node->RightChild;
+
+        if (Node)
+        {
+            Depth++;
+            if (Depth == 22)
+            {
+                MmRebalanceTree(AddressSpace);
+                PreviousNode = Node->Parent;
+            }
+        }
+    }
+    while (Node != NULL);
+
+    marea->LeftChild = marea->RightChild = NULL;
+    marea->Parent = PreviousNode;
+    if (marea->StartingAddress < PreviousNode->StartingAddress)
+        PreviousNode->LeftChild = marea;
+    else
+        PreviousNode->RightChild = marea;
 }
 
 static PVOID
 MmFindGapBottomUp(
-   PMMSUPPORT AddressSpace,
-   ULONG_PTR Length,
-   ULONG_PTR Granularity)
+    PMMSUPPORT AddressSpace,
+    ULONG_PTR Length,
+    ULONG_PTR Granularity)
 {
     ULONG_PTR LowestAddress, HighestAddress, Candidate;
     PMEMORY_AREA Root, Node;
@@ -528,9 +528,9 @@ MmFindGapBottomUp(
 
 static PVOID
 MmFindGapTopDown(
-   PMMSUPPORT AddressSpace,
-   ULONG_PTR Length,
-   ULONG_PTR Granularity)
+    PMMSUPPORT AddressSpace,
+    ULONG_PTR Length,
+    ULONG_PTR Granularity)
 {
     ULONG_PTR LowestAddress, HighestAddress, Candidate;
     PMEMORY_AREA Root, Node;
@@ -599,88 +599,88 @@ MmFindGapTopDown(
 
 PVOID NTAPI
 MmFindGap(
-   PMMSUPPORT AddressSpace,
-   ULONG_PTR Length,
-   ULONG_PTR Granularity,
-   BOOLEAN TopDown)
+    PMMSUPPORT AddressSpace,
+    ULONG_PTR Length,
+    ULONG_PTR Granularity,
+    BOOLEAN TopDown)
 {
-   if (TopDown)
-      return MmFindGapTopDown(AddressSpace, Length, Granularity);
+    if (TopDown)
+        return MmFindGapTopDown(AddressSpace, Length, Granularity);
 
-   return MmFindGapBottomUp(AddressSpace, Length, Granularity);
+    return MmFindGapBottomUp(AddressSpace, Length, Granularity);
 }
 
 ULONG_PTR NTAPI
 MmFindGapAtAddress(
-   PMMSUPPORT AddressSpace,
-   PVOID Address)
+    PMMSUPPORT AddressSpace,
+    PVOID Address)
 {
-   PMEMORY_AREA Node = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
-   PMEMORY_AREA RightNeighbour = NULL;
-   PVOID LowestAddress  = MmGetAddressSpaceOwner(AddressSpace) ? MM_LOWEST_USER_ADDRESS : MmSystemRangeStart;
-   PVOID HighestAddress = MmGetAddressSpaceOwner(AddressSpace) ?
-                          (PVOID)((ULONG_PTR)MmSystemRangeStart - 1) : (PVOID)MAXULONG_PTR;
-
-   Address = MM_ROUND_DOWN(Address, PAGE_SIZE);
-
-   if (LowestAddress < MmSystemRangeStart)
-   {
-      if (Address >= MmSystemRangeStart)
-      {
-         return 0;
-      }
-   }
-   else
-   {
-      if (Address < LowestAddress)
-      {
-         return 0;
-      }
-   }
-
-   while (Node != NULL)
-   {
-      if (Address < Node->StartingAddress)
-      {
-         RightNeighbour = Node;
-         Node = Node->LeftChild;
-      }
-      else if (Address >= Node->EndingAddress)
-      {
-         Node = Node->RightChild;
-      }
-      else
-      {
-         DPRINT("MmFindGapAtAddress: 0\n");
-         return 0;
-      }
-   }
-
-   if (RightNeighbour)
-   {
-      DPRINT("MmFindGapAtAddress: %p [%p]\n", Address,
-             (ULONG_PTR)RightNeighbour->StartingAddress - (ULONG_PTR)Address);
-      return (ULONG_PTR)RightNeighbour->StartingAddress - (ULONG_PTR)Address;
-   }
-   else
-   {
-      DPRINT("MmFindGapAtAddress: %p [%p]\n", Address,
-             (ULONG_PTR)HighestAddress - (ULONG_PTR)Address);
-      return (ULONG_PTR)HighestAddress - (ULONG_PTR)Address;
-   }
+    PMEMORY_AREA Node = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
+    PMEMORY_AREA RightNeighbour = NULL;
+    PVOID LowestAddress  = MmGetAddressSpaceOwner(AddressSpace) ? MM_LOWEST_USER_ADDRESS : MmSystemRangeStart;
+    PVOID HighestAddress = MmGetAddressSpaceOwner(AddressSpace) ?
+                           (PVOID)((ULONG_PTR)MmSystemRangeStart - 1) : (PVOID)MAXULONG_PTR;
+
+    Address = MM_ROUND_DOWN(Address, PAGE_SIZE);
+
+    if (LowestAddress < MmSystemRangeStart)
+    {
+        if (Address >= MmSystemRangeStart)
+        {
+            return 0;
+        }
+    }
+    else
+    {
+        if (Address < LowestAddress)
+        {
+            return 0;
+        }
+    }
+
+    while (Node != NULL)
+    {
+        if (Address < Node->StartingAddress)
+        {
+            RightNeighbour = Node;
+            Node = Node->LeftChild;
+        }
+        else if (Address >= Node->EndingAddress)
+        {
+            Node = Node->RightChild;
+        }
+        else
+        {
+            DPRINT("MmFindGapAtAddress: 0\n");
+            return 0;
+        }
+    }
+
+    if (RightNeighbour)
+    {
+        DPRINT("MmFindGapAtAddress: %p [%p]\n", Address,
+               (ULONG_PTR)RightNeighbour->StartingAddress - (ULONG_PTR)Address);
+        return (ULONG_PTR)RightNeighbour->StartingAddress - (ULONG_PTR)Address;
+    }
+    else
+    {
+        DPRINT("MmFindGapAtAddress: %p [%p]\n", Address,
+               (ULONG_PTR)HighestAddress - (ULONG_PTR)Address);
+        return (ULONG_PTR)HighestAddress - (ULONG_PTR)Address;
+    }
 }
 
 VOID
 NTAPI
 MiRemoveNode(IN PMMADDRESS_NODE Node,
-IN PMM_AVL_TABLE Table);
+             IN PMM_AVL_TABLE Table);
 
 #if DBG
 
 static
 VOID
 MiRosCheckMemoryAreasRecursive(
-   PMEMORY_AREA Node)
+    PMEMORY_AREA Node)
 {
     /* Check if the allocation is ok */
     ExpCheckPoolAllocation(Node, NonPagedPool, 'ERAM');
@@ -693,7 +693,7 @@ MiRosCheckMemoryAreasRecursive(
     ASSERT((ULONG_PTR)Node->StartingAddress < (ULONG_PTR)Node->EndingAddress);
     ASSERT((Node->Type == 0) ||
            (Node->Type == MEMORY_AREA_CACHE) ||
-          // (Node->Type == MEMORY_AREA_CACHE_SEGMENT) ||
+           // (Node->Type == MEMORY_AREA_CACHE_SEGMENT) ||
            (Node->Type == MEMORY_AREA_SECTION_VIEW) ||
            (Node->Type == MEMORY_AREA_OWNED_BY_ARM3) ||
            (Node->Type == (MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC)));
@@ -708,7 +708,7 @@ MiRosCheckMemoryAreasRecursive(
 VOID
 NTAPI
 MiRosCheckMemoryAreas(
-   PMMSUPPORT AddressSpace)
+    PMMSUPPORT AddressSpace)
 {
     PMEMORY_AREA RootNode;
     PEPROCESS AddressSpaceOwner;
@@ -796,54 +796,54 @@ MiDeletePte(IN PMMPTE PointerPte,
 
 NTSTATUS NTAPI
 MmFreeMemoryArea(
-   PMMSUPPORT AddressSpace,
-   PMEMORY_AREA MemoryArea,
-   PMM_FREE_PAGE_FUNC FreePage,
-   PVOID FreePageContext)
+    PMMSUPPORT AddressSpace,
+    PMEMORY_AREA MemoryArea,
+    PMM_FREE_PAGE_FUNC FreePage,
+    PVOID FreePageContext)
 {
-   PMEMORY_AREA *ParentReplace;
-   ULONG_PTR Address;
-   PVOID EndAddress;
+    PMEMORY_AREA *ParentReplace;
+    ULONG_PTR Address;
+    PVOID EndAddress;
 
-   /* Make sure we own the address space lock! */
-   ASSERT(CONTAINING_RECORD(AddressSpace, EPROCESS, Vm)->AddressCreationLock.Owner == KeGetCurrentThread());
+    /* Make sure we own the address space lock! */
+    ASSERT(CONTAINING_RECORD(AddressSpace, EPROCESS, Vm)->AddressCreationLock.Owner == KeGetCurrentThread());
 
     /* Check magic */
     ASSERT(MemoryArea->Magic == 'erAM');
 
-   if (MemoryArea->Type != MEMORY_AREA_OWNED_BY_ARM3)
-   {
-       PEPROCESS CurrentProcess = PsGetCurrentProcess();
-       PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
-
-       if (Process != NULL &&
-           Process != CurrentProcess)
-       {
-          KeAttachProcess(&Process->Pcb);
-       }
-
-       EndAddress = MM_ROUND_UP(MemoryArea->EndingAddress, PAGE_SIZE);
-       for (Address = (ULONG_PTR)MemoryArea->StartingAddress;
-            Address < (ULONG_PTR)EndAddress;
-            Address += PAGE_SIZE)
-       {
-             BOOLEAN Dirty = FALSE;
-             SWAPENTRY SwapEntry = 0;
-             PFN_NUMBER Page = 0;
-
-             if (MmIsPageSwapEntry(Process, (PVOID)Address))
-             {
+    if (MemoryArea->Type != MEMORY_AREA_OWNED_BY_ARM3)
+    {
+        PEPROCESS CurrentProcess = PsGetCurrentProcess();
+        PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+
+        if (Process != NULL &&
+                Process != CurrentProcess)
+        {
+            KeAttachProcess(&Process->Pcb);
+        }
+
+        EndAddress = MM_ROUND_UP(MemoryArea->EndingAddress, PAGE_SIZE);
+        for (Address = (ULONG_PTR)MemoryArea->StartingAddress;
+                Address < (ULONG_PTR)EndAddress;
+                Address += PAGE_SIZE)
+        {
+            BOOLEAN Dirty = FALSE;
+            SWAPENTRY SwapEntry = 0;
+            PFN_NUMBER Page = 0;
+
+            if (MmIsPageSwapEntry(Process, (PVOID)Address))
+            {
                 MmDeletePageFileMapping(Process, (PVOID)Address, &SwapEntry);
-             }
-             else
-             {
+            }
+            else
+            {
                 MmDeleteVirtualMapping(Process, (PVOID)Address, FALSE, &Dirty, &Page);
-             }
-             if (FreePage != NULL)
-             {
+            }
+            if (FreePage != NULL)
+            {
                 FreePage(FreePageContext, MemoryArea, (PVOID)Address,
                          Page, SwapEntry, (BOOLEAN)Dirty);
-             }
+            }
 #if (_MI_PAGING_LEVELS == 2)
             /* Remove page table reference */
             ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
@@ -862,90 +862,90 @@ MmFreeMemoryArea(
                 }
             }
 #endif
-       }
-
-       if (Process != NULL &&
-           Process != CurrentProcess)
-       {
-          KeDetachProcess();
-       }
-
-       if (MemoryArea->Vad)
-       {
-           ASSERT(MemoryArea->EndingAddress < MmSystemRangeStart);
-           ASSERT(MemoryArea->Type == MEMORY_AREA_SECTION_VIEW || MemoryArea->Type == MEMORY_AREA_CACHE);
-
-           /* MmCleanProcessAddressSpace might have removed it (and this would be MmDeleteProcessAdressSpace) */
-           ASSERT(((PMMVAD)MemoryArea->Vad)->u.VadFlags.Spare != 0);
-           if (((PMMVAD)MemoryArea->Vad)->u.VadFlags.Spare == 1)
-           {
-               MiRemoveNode(MemoryArea->Vad, &Process->VadRoot);
-           }
-
-           ExFreePoolWithTag(MemoryArea->Vad, TAG_MVAD);
-           MemoryArea->Vad = NULL;
-       }
+        }
+
+        if (Process != NULL &&
+                Process != CurrentProcess)
+        {
+            KeDetachProcess();
+        }
+
+        if (MemoryArea->Vad)
+        {
+            ASSERT(MemoryArea->EndingAddress < MmSystemRangeStart);
+            ASSERT(MemoryArea->Type == MEMORY_AREA_SECTION_VIEW || MemoryArea->Type == MEMORY_AREA_CACHE);
+
+            /* MmCleanProcessAddressSpace might have removed it (and this would be MmDeleteProcessAdressSpace) */
+            ASSERT(((PMMVAD)MemoryArea->Vad)->u.VadFlags.Spare != 0);
+            if (((PMMVAD)MemoryArea->Vad)->u.VadFlags.Spare == 1)
+            {
+                MiRemoveNode(MemoryArea->Vad, &Process->VadRoot);
+            }
+
+            ExFreePoolWithTag(MemoryArea->Vad, TAG_MVAD);
+            MemoryArea->Vad = NULL;
+        }
     }
 
-   /* Remove the tree item. */
-   {
-      if (MemoryArea->Parent != NULL)
-      {
-         if (MemoryArea->Parent->LeftChild == MemoryArea)
-            ParentReplace = &MemoryArea->Parent->LeftChild;
-         else
-            ParentReplace = &MemoryArea->Parent->RightChild;
-      }
-      else
-         ParentReplace = (PMEMORY_AREA*)&AddressSpace->WorkingSetExpansionLinks.Flink;
-
-      if (MemoryArea->RightChild == NULL)
-      {
-         *ParentReplace = MemoryArea->LeftChild;
-         if (MemoryArea->LeftChild)
-            MemoryArea->LeftChild->Parent = MemoryArea->Parent;
-      }
-      else
-      {
-         if (MemoryArea->RightChild->LeftChild == NULL)
-         {
-            MemoryArea->RightChild->LeftChild = MemoryArea->LeftChild;
+    /* Remove the tree item. */
+    {
+        if (MemoryArea->Parent != NULL)
+        {
+            if (MemoryArea->Parent->LeftChild == MemoryArea)
+                ParentReplace = &MemoryArea->Parent->LeftChild;
+            else
+                ParentReplace = &MemoryArea->Parent->RightChild;
+        }
+        else
+            ParentReplace = (PMEMORY_AREA*)&AddressSpace->WorkingSetExpansionLinks.Flink;
+
+        if (MemoryArea->RightChild == NULL)
+        {
+            *ParentReplace = MemoryArea->LeftChild;
             if (MemoryArea->LeftChild)
-               MemoryArea->LeftChild->Parent = MemoryArea->RightChild;
+                MemoryArea->LeftChild->Parent = MemoryArea->Parent;
+        }
+        else
+        {
+            if (MemoryArea->RightChild->LeftChild == NULL)
+            {
+                MemoryArea->RightChild->LeftChild = MemoryArea->LeftChild;
+                if (MemoryArea->LeftChild)
+                    MemoryArea->LeftChild->Parent = MemoryArea->RightChild;
 
-            *ParentReplace = MemoryArea->RightChild;
-            MemoryArea->RightChild->Parent = MemoryArea->Parent;
-         }
-         else
-         {
-            PMEMORY_AREA LowestNode;
+                *ParentReplace = MemoryArea->RightChild;
+                MemoryArea->RightChild->Parent = MemoryArea->Parent;
+            }
+            else
+            {
+                PMEMORY_AREA LowestNode;
 
-            LowestNode = MemoryArea->RightChild->LeftChild;
-            while (LowestNode->LeftChild != NULL)
-               LowestNode = LowestNode->LeftChild;
+                LowestNode = MemoryArea->RightChild->LeftChild;
+                while (LowestNode->LeftChild != NULL)
+                    LowestNode = LowestNode->LeftChild;
 
-            LowestNode->Parent->LeftChild = LowestNode->RightChild;
-            if (LowestNode->RightChild)
-               LowestNode->RightChild->Parent = LowestNode->Parent;
+                LowestNode->Parent->LeftChild = LowestNode->RightChild;
+                if (LowestNode->RightChild)
+                    LowestNode->RightChild->Parent = LowestNode->Parent;
 
-            LowestNode->LeftChild = MemoryArea->LeftChild;
-            if (MemoryArea->LeftChild)
-               MemoryArea->LeftChild->Parent = LowestNode;
+                LowestNode->LeftChild = MemoryArea->LeftChild;
+                if (MemoryArea->LeftChild)
+                    MemoryArea->LeftChild->Parent = LowestNode;
 
-            LowestNode->RightChild = MemoryArea->RightChild;
-            MemoryArea->RightChild->Parent = LowestNode;
+                LowestNode->RightChild = MemoryArea->RightChild;
+                MemoryArea->RightChild->Parent = LowestNode;
 
-            *ParentReplace = LowestNode;
-            LowestNode->Parent = MemoryArea->Parent;
-         }
-      }
-   }
+                *ParentReplace = LowestNode;
+                LowestNode->Parent = MemoryArea->Parent;
+            }
+        }
+    }
 
-   ExFreePoolWithTag(MemoryArea, TAG_MAREA);
+    ExFreePoolWithTag(MemoryArea, TAG_MAREA);
 
-   DPRINT("MmFreeMemoryAreaByNode() succeeded\n");
+    DPRINT("MmFreeMemoryAreaByNode() succeeded\n");
 
-   return STATUS_SUCCESS;
+    return STATUS_SUCCESS;
 }
 
 /**
@@ -985,55 +985,55 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
                    ULONG AllocationFlags,
                    ULONG Granularity)
 {
-   ULONG_PTR tmpLength;
-   PMEMORY_AREA MemoryArea;
-   ULONG_PTR EndingAddress;
-
-   DPRINT("MmCreateMemoryArea(Type 0x%lx, BaseAddress %p, "
-          "*BaseAddress %p, Length %p, AllocationFlags %x, "
-          "FixedAddress %x, Result %p)\n",
-          Type, BaseAddress, *BaseAddress, Length, AllocationFlags,
-          FixedAddress, Result);
-
-   if ((*BaseAddress) == 0 && !FixedAddress)
-   {
-      tmpLength = (ULONG_PTR)MM_ROUND_UP(Length, PAGE_SIZE);
-      *BaseAddress = MmFindGap(AddressSpace,
-                               tmpLength,
-                               Granularity,
-                               (AllocationFlags & MEM_TOP_DOWN) == MEM_TOP_DOWN);
-      if ((*BaseAddress) == 0)
-      {
-         DPRINT("No suitable gap\n");
-         return STATUS_NO_MEMORY;
-      }
-   }
-   else
-   {
-      EndingAddress = ((ULONG_PTR)*BaseAddress + Length - 1) | (PAGE_SIZE - 1);
-      *BaseAddress = ALIGN_DOWN_POINTER_BY(*BaseAddress, Granularity);
-      tmpLength = EndingAddress + 1 - (ULONG_PTR)*BaseAddress;
-
-      if (!MmGetAddressSpaceOwner(AddressSpace) && *BaseAddress < MmSystemRangeStart)
-      {
-         return STATUS_ACCESS_VIOLATION;
-      }
-
-      if (MmGetAddressSpaceOwner(AddressSpace) &&
-          (ULONG_PTR)(*BaseAddress) + tmpLength > (ULONG_PTR)MmSystemRangeStart)
-      {
-         DPRINT("Memory area for user mode address space exceeds MmSystemRangeStart\n");
-         return STATUS_ACCESS_VIOLATION;
-      }
-
-      if (MmLocateMemoryAreaByRegion(AddressSpace,
-                                     *BaseAddress,
-                                     tmpLength) != NULL)
-      {
-         DPRINT("Memory area already occupied\n");
-         return STATUS_CONFLICTING_ADDRESSES;
-      }
-   }
+    ULONG_PTR tmpLength;
+    PMEMORY_AREA MemoryArea;
+    ULONG_PTR EndingAddress;
+
+    DPRINT("MmCreateMemoryArea(Type 0x%lx, BaseAddress %p, "
+           "*BaseAddress %p, Length %p, AllocationFlags %x, "
+           "FixedAddress %x, Result %p)\n",
+           Type, BaseAddress, *BaseAddress, Length, AllocationFlags,
+           FixedAddress, Result);
+
+    if ((*BaseAddress) == 0 && !FixedAddress)
+    {
+        tmpLength = (ULONG_PTR)MM_ROUND_UP(Length, PAGE_SIZE);
+        *BaseAddress = MmFindGap(AddressSpace,
+                                 tmpLength,
+                                 Granularity,
+                                 (AllocationFlags & MEM_TOP_DOWN) == MEM_TOP_DOWN);
+        if ((*BaseAddress) == 0)
+        {
+            DPRINT("No suitable gap\n");
+            return STATUS_NO_MEMORY;
+        }
+    }
+    else
+    {
+        EndingAddress = ((ULONG_PTR)*BaseAddress + Length - 1) | (PAGE_SIZE - 1);
+        *BaseAddress = ALIGN_DOWN_POINTER_BY(*BaseAddress, Granularity);
+        tmpLength = EndingAddress + 1 - (ULONG_PTR)*BaseAddress;
+
+        if (!MmGetAddressSpaceOwner(AddressSpace) && *BaseAddress < MmSystemRangeStart)
+        {
+            return STATUS_ACCESS_VIOLATION;
+        }
+
+        if (MmGetAddressSpaceOwner(AddressSpace) &&
+                (ULONG_PTR)(*BaseAddress) + tmpLength > (ULONG_PTR)MmSystemRangeStart)
+        {
+            DPRINT("Memory area for user mode address space exceeds MmSystemRangeStart\n");
+            return STATUS_ACCESS_VIOLATION;
+        }
+
+        if (MmLocateMemoryAreaByRegion(AddressSpace,
+                                       *BaseAddress,
+                                       tmpLength) != NULL)
+        {
+            DPRINT("Memory area already occupied\n");
+            return STATUS_CONFLICTING_ADDRESSES;
+        }
+    }
 
     //
     // Is this a static memory area?
@@ -1057,28 +1057,28 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
                                            TAG_MAREA);
     }
 
-   if (!MemoryArea)
-   {
-      DPRINT1("Not enough memory.\n");
-      return STATUS_NO_MEMORY;
-   }
+    if (!MemoryArea)
+    {
+        DPRINT1("Not enough memory.\n");
+        return STATUS_NO_MEMORY;
+    }
 
-   RtlZeroMemory(MemoryArea, sizeof(MEMORY_AREA));
-   MemoryArea->Type = Type;
-   MemoryArea->StartingAddress = *BaseAddress;
-   MemoryArea->EndingAddress = (PVOID)((ULONG_PTR)*BaseAddress + tmpLength);
-   MemoryArea->Protect = Protect;
-   MemoryArea->Flags = AllocationFlags;
-   //MemoryArea->LockCount = 0;
-   MemoryArea->Magic = 'erAM';
-   MemoryArea->DeleteInProgress = FALSE;
+    RtlZeroMemory(MemoryArea, sizeof(MEMORY_AREA));
+    MemoryArea->Type = Type;
+    MemoryArea->StartingAddress = *BaseAddress;
+    MemoryArea->EndingAddress = (PVOID)((ULONG_PTR)*BaseAddress + tmpLength);
+    MemoryArea->Protect = Protect;
+    MemoryArea->Flags = AllocationFlags;
+    //MemoryArea->LockCount = 0;
+    MemoryArea->Magic = 'erAM';
+    MemoryArea->DeleteInProgress = FALSE;
 
-   MmInsertMemoryArea(AddressSpace, MemoryArea);
+    MmInsertMemoryArea(AddressSpace, MemoryArea);
 
-   *Result = MemoryArea;
+    *Result = MemoryArea;
 
-   DPRINT("MmCreateMemoryArea() succeeded (%p)\n", *BaseAddress);
-   return STATUS_SUCCESS;
+    DPRINT("MmCreateMemoryArea() succeeded (%p)\n", *BaseAddress);
+    return STATUS_SUCCESS;
 }
 
 VOID NTAPI
@@ -1087,32 +1087,32 @@ MmMapMemoryArea(PVOID BaseAddress,
                 ULONG Consumer,
                 ULONG Protection)
 {
-   ULONG i;
-   NTSTATUS Status;
-
-   ASSERT(((ULONG_PTR)BaseAddress % PAGE_SIZE) == 0);
-
-   for (i = 0; i < PAGE_ROUND_UP(Length) / PAGE_SIZE; i++)
-   {
-      PFN_NUMBER Page;
-
-      Status = MmRequestPageMemoryConsumer(Consumer, TRUE, &Page);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Unable to allocate page\n");
-         KeBugCheck(MEMORY_MANAGEMENT);
-      }
-      Status = MmCreateVirtualMapping (NULL,
-                                       (PVOID)((ULONG_PTR)BaseAddress + (i * PAGE_SIZE)),
-                                       Protection,
-                                       &Page,
-                                       1);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Unable to create virtual mapping\n");
-         KeBugCheck(MEMORY_MANAGEMENT);
-      }
-   }
+    ULONG i;
+    NTSTATUS Status;
+
+    ASSERT(((ULONG_PTR)BaseAddress % PAGE_SIZE) == 0);
+
+    for (i = 0; i < PAGE_ROUND_UP(Length) / PAGE_SIZE; i++)
+    {
+        PFN_NUMBER Page;
+
+        Status = MmRequestPageMemoryConsumer(Consumer, TRUE, &Page);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("Unable to allocate page\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+        Status = MmCreateVirtualMapping (NULL,
+                                         (PVOID)((ULONG_PTR)BaseAddress + (i * PAGE_SIZE)),
+                                         Protection,
+                                         &Page,
+                                         1);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("Unable to create virtual mapping\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+    }
 }
 
 VOID
@@ -1123,46 +1123,46 @@ NTSTATUS
 NTAPI
 MmDeleteProcessAddressSpace(PEPROCESS Process)
 {
-   PVOID Address;
-   PMEMORY_AREA MemoryArea;
+    PVOID Address;
+    PMEMORY_AREA MemoryArea;
 
-   DPRINT("MmDeleteProcessAddressSpace(Process %p (%s))\n", Process,
-          Process->ImageFileName);
+    DPRINT("MmDeleteProcessAddressSpace(Process %p (%s))\n", Process,
+           Process->ImageFileName);
 
 #ifndef _M_AMD64
-   RemoveEntryList(&Process->MmProcessLinks);
+    RemoveEntryList(&Process->MmProcessLinks);
 #endif
-   MmLockAddressSpace(&Process->Vm);
-
-   while ((MemoryArea = (PMEMORY_AREA)Process->Vm.WorkingSetExpansionLinks.Flink) != NULL)
-   {
-      switch (MemoryArea->Type)
-      {
-         case MEMORY_AREA_SECTION_VIEW:
-             Address = (PVOID)MemoryArea->StartingAddress;
-             MmUnlockAddressSpace(&Process->Vm);
-             MmUnmapViewOfSection(Process, Address);
-             MmLockAddressSpace(&Process->Vm);
-             break;
-
-         case MEMORY_AREA_CACHE:
-             Address = (PVOID)MemoryArea->StartingAddress;
-             MmUnlockAddressSpace(&Process->Vm);
-             MmUnmapViewOfCacheSegment(&Process->Vm, Address);
-             MmLockAddressSpace(&Process->Vm);
-             break;
-
-         case MEMORY_AREA_OWNED_BY_ARM3:
-             MmFreeMemoryArea(&Process->Vm,
-                              MemoryArea,
-                              NULL,
-                              NULL);
-             break;
-
-         default:
+    MmLockAddressSpace(&Process->Vm);
+
+    while ((MemoryArea = (PMEMORY_AREA)Process->Vm.WorkingSetExpansionLinks.Flink) != NULL)
+    {
+        switch (MemoryArea->Type)
+        {
+        case MEMORY_AREA_SECTION_VIEW:
+            Address = (PVOID)MemoryArea->StartingAddress;
+            MmUnlockAddressSpace(&Process->Vm);
+            MmUnmapViewOfSection(Process, Address);
+            MmLockAddressSpace(&Process->Vm);
+            break;
+
+        case MEMORY_AREA_CACHE:
+            Address = (PVOID)MemoryArea->StartingAddress;
+            MmUnlockAddressSpace(&Process->Vm);
+            MmUnmapViewOfCacheSegment(&Process->Vm, Address);
+            MmLockAddressSpace(&Process->Vm);
+            break;
+
+        case MEMORY_AREA_OWNED_BY_ARM3:
+            MmFreeMemoryArea(&Process->Vm,
+                             MemoryArea,
+                             NULL,
+                             NULL);
+            break;
+
+        default:
             KeBugCheck(MEMORY_MANAGEMENT);
-      }
-   }
+        }
+    }
 
 #if (_MI_PAGING_LEVELS == 2)
     {
@@ -1175,8 +1175,8 @@ MmDeleteProcessAddressSpace(PEPROCESS Process)
         OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
 
         for (Address = MI_LOWEST_VAD_ADDRESS;
-             Address < MM_HIGHEST_VAD_ADDRESS;
-             Address =(PVOID)((ULONG_PTR)Address + (PAGE_SIZE * PTE_COUNT)))
+                Address < MM_HIGHEST_VAD_ADDRESS;
+                Address =(PVOID)((ULONG_PTR)Address + (PAGE_SIZE * PTE_COUNT)))
         {
             /* At this point all references should be dead */
             if (MiQueryPageTableReferences(Address) != 0)
@@ -1202,11 +1202,11 @@ MmDeleteProcessAddressSpace(PEPROCESS Process)
     }
 #endif
 
-   MmUnlockAddressSpace(&Process->Vm);
+    MmUnlockAddressSpace(&Process->Vm);
 
-   DPRINT("Finished MmDeleteProcessAddressSpace()\n");
-   MmDeleteProcessAddressSpace2(Process);
-   return(STATUS_SUCCESS);
+    DPRINT("Finished MmDeleteProcessAddressSpace()\n");
+    MmDeleteProcessAddressSpace2(Process);
+    return(STATUS_SUCCESS);
 }
 
 /* EOF */
index 602d708..2390854 100644 (file)
 NTSTATUS
 NTAPI
 MmpAccessFault(KPROCESSOR_MODE Mode,
-                  ULONG_PTR Address,
-                  BOOLEAN FromMdl)
+               ULONG_PTR Address,
+               BOOLEAN FromMdl)
 {
-   PMMSUPPORT AddressSpace;
-   MEMORY_AREA* MemoryArea;
-   NTSTATUS Status;
+    PMMSUPPORT AddressSpace;
+    MEMORY_AREA* MemoryArea;
+    NTSTATUS Status;
 
-   DPRINT("MmAccessFault(Mode %d, Address %x)\n", Mode, Address);
+    DPRINT("MmAccessFault(Mode %d, Address %x)\n", Mode, Address);
 
-   if (KeGetCurrentIrql() >= DISPATCH_LEVEL)
-   {
-      DPRINT1("Page fault at high IRQL was %u\n", KeGetCurrentIrql());
-      return(STATUS_UNSUCCESSFUL);
-   }
+    if (KeGetCurrentIrql() >= DISPATCH_LEVEL)
+    {
+        DPRINT1("Page fault at high IRQL was %u\n", KeGetCurrentIrql());
+        return(STATUS_UNSUCCESSFUL);
+    }
 
-   /*
-    * Find the memory area for the faulting address
-    */
-   if (Address >= (ULONG_PTR)MmSystemRangeStart)
-   {
-      /*
-       * Check permissions
-       */
-      if (Mode != KernelMode)
-      {
-         DPRINT1("MmAccessFault(Mode %d, Address %x)\n", Mode, Address);
-         return(STATUS_ACCESS_VIOLATION);
-      }
-      AddressSpace = MmGetKernelAddressSpace();
-   }
-   else
-   {
-      AddressSpace = &PsGetCurrentProcess()->Vm;
-   }
+    /*
+     * Find the memory area for the faulting address
+     */
+    if (Address >= (ULONG_PTR)MmSystemRangeStart)
+    {
+        /*
+         * Check permissions
+         */
+        if (Mode != KernelMode)
+        {
+            DPRINT1("MmAccessFault(Mode %d, Address %x)\n", Mode, Address);
+            return(STATUS_ACCESS_VIOLATION);
+        }
+        AddressSpace = MmGetKernelAddressSpace();
+    }
+    else
+    {
+        AddressSpace = &PsGetCurrentProcess()->Vm;
+    }
 
-   if (!FromMdl)
-   {
-      MmLockAddressSpace(AddressSpace);
-   }
-   do
-   {
-      MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, (PVOID)Address);
-      if (MemoryArea == NULL || MemoryArea->DeleteInProgress)
-      {
-         if (!FromMdl)
-         {
-            MmUnlockAddressSpace(AddressSpace);
-         }
-         return (STATUS_ACCESS_VIOLATION);
-      }
+    if (!FromMdl)
+    {
+        MmLockAddressSpace(AddressSpace);
+    }
+    do
+    {
+        MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, (PVOID)Address);
+        if (MemoryArea == NULL || MemoryArea->DeleteInProgress)
+        {
+            if (!FromMdl)
+            {
+                MmUnlockAddressSpace(AddressSpace);
+            }
+            return (STATUS_ACCESS_VIOLATION);
+        }
 
-      switch (MemoryArea->Type)
-      {
-         case MEMORY_AREA_SECTION_VIEW:
+        switch (MemoryArea->Type)
+        {
+        case MEMORY_AREA_SECTION_VIEW:
             Status = MmAccessFaultSectionView(AddressSpace,
                                               MemoryArea,
                                               (PVOID)Address);
             break;
 
-         case MEMORY_AREA_CACHE:
+        case MEMORY_AREA_CACHE:
             // This code locks for itself to keep from having to break a lock
             // passed in.
             if (!FromMdl)
-               MmUnlockAddressSpace(AddressSpace);
+                MmUnlockAddressSpace(AddressSpace);
             Status = MmAccessFaultCacheSection(Mode, Address, FromMdl);
             if (!FromMdl)
-               MmLockAddressSpace(AddressSpace);
+                MmLockAddressSpace(AddressSpace);
             break;
 
-         default:
+        default:
             Status = STATUS_ACCESS_VIOLATION;
             break;
-      }
-   }
-   while (Status == STATUS_MM_RESTART_OPERATION);
+        }
+    }
+    while (Status == STATUS_MM_RESTART_OPERATION);
 
-   DPRINT("Completed page fault handling\n");
-   if (!FromMdl)
-   {
-      MmUnlockAddressSpace(AddressSpace);
-   }
-   return(Status);
+    DPRINT("Completed page fault handling\n");
+    if (!FromMdl)
+    {
+        MmUnlockAddressSpace(AddressSpace);
+    }
+    return(Status);
 }
 
 NTSTATUS
 NTAPI
 MmNotPresentFault(KPROCESSOR_MODE Mode,
-                           ULONG_PTR Address,
-                           BOOLEAN FromMdl)
+                  ULONG_PTR Address,
+                  BOOLEAN FromMdl)
 {
-   PMMSUPPORT AddressSpace;
-   MEMORY_AREA* MemoryArea;
-   NTSTATUS Status;
+    PMMSUPPORT AddressSpace;
+    MEMORY_AREA* MemoryArea;
+    NTSTATUS Status;
 
-   DPRINT("MmNotPresentFault(Mode %d, Address %x)\n", Mode, Address);
+    DPRINT("MmNotPresentFault(Mode %d, Address %x)\n", Mode, Address);
 
-   if (KeGetCurrentIrql() >= DISPATCH_LEVEL)
-   {
-      DPRINT1("Page fault at high IRQL was %u, address %x\n", KeGetCurrentIrql(), Address);
-      return(STATUS_UNSUCCESSFUL);
-   }
+    if (KeGetCurrentIrql() >= DISPATCH_LEVEL)
+    {
+        DPRINT1("Page fault at high IRQL was %u, address %x\n", KeGetCurrentIrql(), Address);
+        return(STATUS_UNSUCCESSFUL);
+    }
 
-   /*
-    * Find the memory area for the faulting address
-    */
-   if (Address >= (ULONG_PTR)MmSystemRangeStart)
-   {
-      /*
-       * Check permissions
-       */
-      if (Mode != KernelMode)
-      {
-         DPRINT1("Address: %x\n", Address);
-         return(STATUS_ACCESS_VIOLATION);
-      }
-      AddressSpace = MmGetKernelAddressSpace();
-   }
-   else
-   {
-      AddressSpace = &PsGetCurrentProcess()->Vm;
-   }
+    /*
+     * Find the memory area for the faulting address
+     */
+    if (Address >= (ULONG_PTR)MmSystemRangeStart)
+    {
+        /*
+         * Check permissions
+         */
+        if (Mode != KernelMode)
+        {
+            DPRINT1("Address: %x\n", Address);
+            return(STATUS_ACCESS_VIOLATION);
+        }
+        AddressSpace = MmGetKernelAddressSpace();
+    }
+    else
+    {
+        AddressSpace = &PsGetCurrentProcess()->Vm;
+    }
 
-   if (!FromMdl)
-   {
-      MmLockAddressSpace(AddressSpace);
-   }
+    if (!FromMdl)
+    {
+        MmLockAddressSpace(AddressSpace);
+    }
 
-   /*
-    * Call the memory area specific fault handler
-    */
-   do
-   {
-      MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, (PVOID)Address);
-      if (MemoryArea == NULL || MemoryArea->DeleteInProgress)
-      {
-         if (!FromMdl)
-         {
-            MmUnlockAddressSpace(AddressSpace);
-         }
-         return (STATUS_ACCESS_VIOLATION);
-      }
+    /*
+     * Call the memory area specific fault handler
+     */
+    do
+    {
+        MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, (PVOID)Address);
+        if (MemoryArea == NULL || MemoryArea->DeleteInProgress)
+        {
+            if (!FromMdl)
+            {
+                MmUnlockAddressSpace(AddressSpace);
+            }
+            return (STATUS_ACCESS_VIOLATION);
+        }
 
-      switch (MemoryArea->Type)
-      {
-         case MEMORY_AREA_SECTION_VIEW:
+        switch (MemoryArea->Type)
+        {
+        case MEMORY_AREA_SECTION_VIEW:
             Status = MmNotPresentFaultSectionView(AddressSpace,
                                                   MemoryArea,
                                                   (PVOID)Address,
                                                   FromMdl);
             break;
 
-         case MEMORY_AREA_CACHE:
+        case MEMORY_AREA_CACHE:
             // This code locks for itself to keep from having to break a lock
             // passed in.
             if (!FromMdl)
-               MmUnlockAddressSpace(AddressSpace);
+                MmUnlockAddressSpace(AddressSpace);
             Status = MmNotPresentFaultCacheSection(Mode, Address, FromMdl);
             if (!FromMdl)
-               MmLockAddressSpace(AddressSpace);
+                MmLockAddressSpace(AddressSpace);
             break;
 
-         default:
+        default:
             Status = STATUS_ACCESS_VIOLATION;
             break;
-      }
-   }
-   while (Status == STATUS_MM_RESTART_OPERATION);
+        }
+    }
+    while (Status == STATUS_MM_RESTART_OPERATION);
 
-   DPRINT("Completed page fault handling\n");
-   if (!FromMdl)
-   {
-      MmUnlockAddressSpace(AddressSpace);
-   }
-   return(Status);
+    DPRINT("Completed page fault handling\n");
+    if (!FromMdl)
+    {
+        MmUnlockAddressSpace(AddressSpace);
+    }
+    return(Status);
 }
 
 extern BOOLEAN Mmi386MakeKernelPageTableGlobal(PVOID Address);
@@ -235,8 +235,8 @@ MmAccessFault(IN BOOLEAN StoreInstruction,
 
     /* Is this an ARM3 memory area, or is there no address space yet? */
     if (((MemoryArea) && (MemoryArea->Type == MEMORY_AREA_OWNED_BY_ARM3)) ||
-        (!(MemoryArea) && ((ULONG_PTR)Address >= (ULONG_PTR)MmPagedPoolStart)) ||
-        (!MmGetKernelAddressSpace()))
+            (!(MemoryArea) && ((ULONG_PTR)Address >= (ULONG_PTR)MmPagedPoolStart)) ||
+            (!MmGetKernelAddressSpace()))
     {
         /* This is an ARM3 fault */
         DPRINT("ARM3 fault %p\n", MemoryArea);
index 910f397..41732e3 100644 (file)
@@ -299,36 +299,36 @@ 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;
+    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
@@ -336,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,
-                                 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
@@ -430,8 +430,8 @@ MmInitSystem(IN ULONG Phase,
     // by the fault handler.
     //
     MmSharedUserDataPte = ExAllocatePoolWithTag(PagedPool,
-                                                sizeof(MMPTE),
-                                                '  mM');
+                          sizeof(MMPTE),
+                          '  mM');
     if (!MmSharedUserDataPte) return FALSE;
 
     //
index a1ffb08..e5b88a5 100644 (file)
@@ -44,23 +44,23 @@ MiFindExportedRoutineByName(IN PVOID DllBase,
 
 typedef struct _PAGINGFILE
 {
-   LIST_ENTRY PagingFileListEntry;
-   PFILE_OBJECT FileObject;
-   LARGE_INTEGER MaximumSize;
-   LARGE_INTEGER CurrentSize;
-   PFN_NUMBER FreePages;
-   PFN_NUMBER UsedPages;
-   PULONG AllocMap;
-   KSPIN_LOCK AllocMapLock;
-   ULONG AllocMapSize;
-   PRETRIEVAL_POINTERS_BUFFER RetrievalPointers;
+    LIST_ENTRY PagingFileListEntry;
+    PFILE_OBJECT FileObject;
+    LARGE_INTEGER MaximumSize;
+    LARGE_INTEGER CurrentSize;
+    PFN_NUMBER FreePages;
+    PFN_NUMBER UsedPages;
+    PULONG AllocMap;
+    KSPIN_LOCK AllocMapLock;
+    ULONG AllocMapSize;
+    PRETRIEVAL_POINTERS_BUFFER RetrievalPointers;
 }
 PAGINGFILE, *PPAGINGFILE;
 
 typedef struct _RETRIEVEL_DESCRIPTOR_LIST
 {
-   struct _RETRIEVEL_DESCRIPTOR_LIST* Next;
-   RETRIEVAL_POINTERS_BUFFER RetrievalPointers;
+    struct _RETRIEVEL_DESCRIPTOR_LIST* Next;
+    RETRIEVAL_POINTERS_BUFFER RetrievalPointers;
 }
 RETRIEVEL_DESCRIPTOR_LIST, *PRETRIEVEL_DESCRIPTOR_LIST;
 
@@ -153,67 +153,67 @@ VOID
 NTAPI
 MmShowOutOfSpaceMessagePagingFile(VOID)
 {
-   if (!MmSwapSpaceMessage)
-   {
-      DPRINT1("MM: Out of swap space.\n");
-      MmSwapSpaceMessage = TRUE;
-   }
+    if (!MmSwapSpaceMessage)
+    {
+        DPRINT1("MM: Out of swap space.\n");
+        MmSwapSpaceMessage = TRUE;
+    }
 }
 
 static LARGE_INTEGER
 MmGetOffsetPageFile(PRETRIEVAL_POINTERS_BUFFER RetrievalPointers, LARGE_INTEGER Offset)
 {
-   /* Simple binary search */
-   ULONG first, last, mid;
-   first = 0;
-   last = RetrievalPointers->ExtentCount - 1;
-   while (first <= last)
-   {
-      mid = (last - first) / 2 + first;
-      if (Offset.QuadPart < RetrievalPointers->Extents[mid].NextVcn.QuadPart)
-      {
-         if (mid == 0)
-         {
-            Offset.QuadPart += RetrievalPointers->Extents[0].Lcn.QuadPart - RetrievalPointers->StartingVcn.QuadPart;
-            return Offset;
-         }
-         else
-         {
-            if (Offset.QuadPart >= RetrievalPointers->Extents[mid-1].NextVcn.QuadPart)
+    /* Simple binary search */
+    ULONG first, last, mid;
+    first = 0;
+    last = RetrievalPointers->ExtentCount - 1;
+    while (first <= last)
+    {
+        mid = (last - first) / 2 + first;
+        if (Offset.QuadPart < RetrievalPointers->Extents[mid].NextVcn.QuadPart)
+        {
+            if (mid == 0)
             {
-               Offset.QuadPart += RetrievalPointers->Extents[mid].Lcn.QuadPart - RetrievalPointers->Extents[mid-1].NextVcn.QuadPart;
-               return Offset;
+                Offset.QuadPart += RetrievalPointers->Extents[0].Lcn.QuadPart - RetrievalPointers->StartingVcn.QuadPart;
+                return Offset;
             }
-            last = mid - 1;
-         }
-      }
-      else
-      {
-         if (mid == RetrievalPointers->ExtentCount - 1)
-         {
-            break;
-         }
-         if (Offset.QuadPart < RetrievalPointers->Extents[mid+1].NextVcn.QuadPart)
-         {
-            Offset.QuadPart += RetrievalPointers->Extents[mid+1].Lcn.QuadPart  - RetrievalPointers->Extents[mid].NextVcn.QuadPart;
-            return Offset;
-         }
-         first = mid + 1;
-      }
-   }
-   KeBugCheck(MEMORY_MANAGEMENT);
+            else
+            {
+                if (Offset.QuadPart >= RetrievalPointers->Extents[mid-1].NextVcn.QuadPart)
+                {
+                    Offset.QuadPart += RetrievalPointers->Extents[mid].Lcn.QuadPart - RetrievalPointers->Extents[mid-1].NextVcn.QuadPart;
+                    return Offset;
+                }
+                last = mid - 1;
+            }
+        }
+        else
+        {
+            if (mid == RetrievalPointers->ExtentCount - 1)
+            {
+                break;
+            }
+            if (Offset.QuadPart < RetrievalPointers->Extents[mid+1].NextVcn.QuadPart)
+            {
+                Offset.QuadPart += RetrievalPointers->Extents[mid+1].Lcn.QuadPart  - RetrievalPointers->Extents[mid].NextVcn.QuadPart;
+                return Offset;
+            }
+            first = mid + 1;
+        }
+    }
+    KeBugCheck(MEMORY_MANAGEMENT);
 #if defined(__GNUC__)
 
-   return (LARGE_INTEGER)0LL;
+    return (LARGE_INTEGER)0LL;
 #else
 
-   {
-      const LARGE_INTEGER dummy =
-         {
+    {
+        const LARGE_INTEGER dummy =
+        {
             0
-         };
-      return dummy;
-   }
+        };
+        return dummy;
+    }
 #endif
 }
 
@@ -221,57 +221,57 @@ NTSTATUS
 NTAPI
 MmWriteToSwapPage(SWAPENTRY SwapEntry, PFN_NUMBER Page)
 {
-   ULONG i;
-   ULONG_PTR offset;
-   LARGE_INTEGER file_offset;
-   IO_STATUS_BLOCK Iosb;
-   NTSTATUS Status;
-   KEVENT Event;
-   UCHAR MdlBase[sizeof(MDL) + sizeof(ULONG)];
-   PMDL Mdl = (PMDL)MdlBase;
-
-   DPRINT("MmWriteToSwapPage\n");
-
-   if (SwapEntry == 0)
-   {
-      KeBugCheck(MEMORY_MANAGEMENT);
-      return(STATUS_UNSUCCESSFUL);
-   }
-
-   i = FILE_FROM_ENTRY(SwapEntry);
-   offset = OFFSET_FROM_ENTRY(SwapEntry);
-
-   if (PagingFileList[i]->FileObject == NULL ||
-         PagingFileList[i]->FileObject->DeviceObject == NULL)
-   {
-      DPRINT1("Bad paging file 0x%.8X\n", SwapEntry);
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   MmInitializeMdl(Mdl, NULL, PAGE_SIZE);
-   MmBuildMdlFromPages(Mdl, &Page);
-   Mdl->MdlFlags |= MDL_PAGES_LOCKED;
-
-   file_offset.QuadPart = offset * PAGE_SIZE;
-   file_offset = MmGetOffsetPageFile(PagingFileList[i]->RetrievalPointers, file_offset);
-
-   KeInitializeEvent(&Event, NotificationEvent, FALSE);
-   Status = IoSynchronousPageWrite(PagingFileList[i]->FileObject,
-                                   Mdl,
-                                   &file_offset,
-                                   &Event,
-                                   &Iosb);
-   if (Status == STATUS_PENDING)
-   {
-      KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
-      Status = Iosb.Status;
-   }
-
-   if (Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA)
-   {
-      MmUnmapLockedPages (Mdl->MappedSystemVa, Mdl);
-   }
-   return(Status);
+    ULONG i;
+    ULONG_PTR offset;
+    LARGE_INTEGER file_offset;
+    IO_STATUS_BLOCK Iosb;
+    NTSTATUS Status;
+    KEVENT Event;
+    UCHAR MdlBase[sizeof(MDL) + sizeof(ULONG)];
+    PMDL Mdl = (PMDL)MdlBase;
+
+    DPRINT("MmWriteToSwapPage\n");
+
+    if (SwapEntry == 0)
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+        return(STATUS_UNSUCCESSFUL);
+    }
+
+    i = FILE_FROM_ENTRY(SwapEntry);
+    offset = OFFSET_FROM_ENTRY(SwapEntry);
+
+    if (PagingFileList[i]->FileObject == NULL ||
+            PagingFileList[i]->FileObject->DeviceObject == NULL)
+    {
+        DPRINT1("Bad paging file 0x%.8X\n", SwapEntry);
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    MmInitializeMdl(Mdl, NULL, PAGE_SIZE);
+    MmBuildMdlFromPages(Mdl, &Page);
+    Mdl->MdlFlags |= MDL_PAGES_LOCKED;
+
+    file_offset.QuadPart = offset * PAGE_SIZE;
+    file_offset = MmGetOffsetPageFile(PagingFileList[i]->RetrievalPointers, file_offset);
+
+    KeInitializeEvent(&Event, NotificationEvent, FALSE);
+    Status = IoSynchronousPageWrite(PagingFileList[i]->FileObject,
+                                    Mdl,
+                                    &file_offset,
+                                    &Event,
+                                    &Iosb);
+    if (Status == STATUS_PENDING)
+    {
+        KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
+        Status = Iosb.Status;
+    }
+
+    if (Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA)
+    {
+        MmUnmapLockedPages (Mdl->MappedSystemVa, Mdl);
+    }
+    return(Status);
 }
 
 
@@ -279,65 +279,65 @@ NTSTATUS
 NTAPI
 MmReadFromSwapPage(SWAPENTRY SwapEntry, PFN_NUMBER Page)
 {
-       return MiReadPageFile(Page, FILE_FROM_ENTRY(SwapEntry), OFFSET_FROM_ENTRY(SwapEntry));
+    return MiReadPageFile(Page, FILE_FROM_ENTRY(SwapEntry), OFFSET_FROM_ENTRY(SwapEntry));
 }
 
 NTSTATUS
 NTAPI
 MiReadPageFile(
-       _In_ PFN_NUMBER Page,
-       _In_ ULONG PageFileIndex,
-       _In_ ULONG_PTR PageFileOffset)
+    _In_ PFN_NUMBER Page,
+    _In_ ULONG PageFileIndex,
+    _In_ ULONG_PTR PageFileOffset)
 {
-   LARGE_INTEGER file_offset;
-   IO_STATUS_BLOCK Iosb;
-   NTSTATUS Status;
-   KEVENT Event;
-   UCHAR MdlBase[sizeof(MDL) + sizeof(ULONG)];
-   PMDL Mdl = (PMDL)MdlBase;
-   PPAGINGFILE PagingFile;
-
-   DPRINT("MiReadSwapFile\n");
-
-   if (PageFileOffset == 0)
-   {
-      KeBugCheck(MEMORY_MANAGEMENT);
-      return(STATUS_UNSUCCESSFUL);
-   }
-
-   ASSERT(PageFileIndex < MAX_PAGING_FILES);
-
-   PagingFile = PagingFileList[PageFileIndex];
-
-   if (PagingFile->FileObject == NULL || PagingFile->FileObject->DeviceObject == NULL)
-   {
-      DPRINT1("Bad paging file %u\n", PageFileIndex);
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   MmInitializeMdl(Mdl, NULL, PAGE_SIZE);
-   MmBuildMdlFromPages(Mdl, &Page);
-   Mdl->MdlFlags |= MDL_PAGES_LOCKED;
-
-   file_offset.QuadPart = PageFileOffset * PAGE_SIZE;
-   file_offset = MmGetOffsetPageFile(PagingFile->RetrievalPointers, file_offset);
-
-   KeInitializeEvent(&Event, NotificationEvent, FALSE);
-   Status = IoPageRead(PagingFile->FileObject,
-                       Mdl,
-                       &file_offset,
-                       &Event,
-                       &Iosb);
-   if (Status == STATUS_PENDING)
-   {
-      KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
-      Status = Iosb.Status;
-   }
-   if (Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA)
-   {
-      MmUnmapLockedPages (Mdl->MappedSystemVa, Mdl);
-   }
-   return(Status);
+    LARGE_INTEGER file_offset;
+    IO_STATUS_BLOCK Iosb;
+    NTSTATUS Status;
+    KEVENT Event;
+    UCHAR MdlBase[sizeof(MDL) + sizeof(ULONG)];
+    PMDL Mdl = (PMDL)MdlBase;
+    PPAGINGFILE PagingFile;
+
+    DPRINT("MiReadSwapFile\n");
+
+    if (PageFileOffset == 0)
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+        return(STATUS_UNSUCCESSFUL);
+    }
+
+    ASSERT(PageFileIndex < MAX_PAGING_FILES);
+
+    PagingFile = PagingFileList[PageFileIndex];
+
+    if (PagingFile->FileObject == NULL || PagingFile->FileObject->DeviceObject == NULL)
+    {
+        DPRINT1("Bad paging file %u\n", PageFileIndex);
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    MmInitializeMdl(Mdl, NULL, PAGE_SIZE);
+    MmBuildMdlFromPages(Mdl, &Page);
+    Mdl->MdlFlags |= MDL_PAGES_LOCKED;
+
+    file_offset.QuadPart = PageFileOffset * PAGE_SIZE;
+    file_offset = MmGetOffsetPageFile(PagingFile->RetrievalPointers, file_offset);
+
+    KeInitializeEvent(&Event, NotificationEvent, FALSE);
+    Status = IoPageRead(PagingFile->FileObject,
+                        Mdl,
+                        &file_offset,
+                        &Event,
+                        &Iosb);
+    if (Status == STATUS_PENDING)
+    {
+        KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
+        Status = Iosb.Status;
+    }
+    if (Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA)
+    {
+        MmUnmapLockedPages (Mdl->MappedSystemVa, Mdl);
+    }
+    return(Status);
 }
 
 VOID
@@ -345,135 +345,135 @@ INIT_FUNCTION
 NTAPI
 MmInitPagingFile(VOID)
 {
-   ULONG i;
+    ULONG i;
 
-   KeInitializeSpinLock(&PagingFileListLock);
+    KeInitializeSpinLock(&PagingFileListLock);
 
-   MiFreeSwapPages = 0;
-   MiUsedSwapPages = 0;
-   MiReservedSwapPages = 0;
+    MiFreeSwapPages = 0;
+    MiUsedSwapPages = 0;
+    MiReservedSwapPages = 0;
 
-   for (i = 0; i < MAX_PAGING_FILES; i++)
-   {
-      PagingFileList[i] = NULL;
-   }
-   MmNumberOfPagingFiles = 0;
+    for (i = 0; i < MAX_PAGING_FILES; i++)
+    {
+        PagingFileList[i] = NULL;
+    }
+    MmNumberOfPagingFiles = 0;
 }
 
 static ULONG
 MiAllocPageFromPagingFile(PPAGINGFILE PagingFile)
 {
-   KIRQL oldIrql;
-   ULONG i, j;
-
-   KeAcquireSpinLock(&PagingFile->AllocMapLock, &oldIrql);
-
-   for (i = 0; i < PagingFile->AllocMapSize; i++)
-   {
-      for (j = 0; j < 32; j++)
-      {
-         if (!(PagingFile->AllocMap[i] & (1 << j)))
-         {
-            PagingFile->AllocMap[i] |= (1 << j);
-            PagingFile->UsedPages++;
-            PagingFile->FreePages--;
-            KeReleaseSpinLock(&PagingFile->AllocMapLock, oldIrql);
-            return((i * 32) + j);
-         }
-      }
-   }
-
-   KeReleaseSpinLock(&PagingFile->AllocMapLock, oldIrql);
-   return(0xFFFFFFFF);
+    KIRQL oldIrql;
+    ULONG i, j;
+
+    KeAcquireSpinLock(&PagingFile->AllocMapLock, &oldIrql);
+
+    for (i = 0; i < PagingFile->AllocMapSize; i++)
+    {
+        for (j = 0; j < 32; j++)
+        {
+            if (!(PagingFile->AllocMap[i] & (1 << j)))
+            {
+                PagingFile->AllocMap[i] |= (1 << j);
+                PagingFile->UsedPages++;
+                PagingFile->FreePages--;
+                KeReleaseSpinLock(&PagingFile->AllocMapLock, oldIrql);
+                return((i * 32) + j);
+            }
+        }
+    }
+
+    KeReleaseSpinLock(&PagingFile->AllocMapLock, oldIrql);
+    return(0xFFFFFFFF);
 }
 
 VOID
 NTAPI
 MmFreeSwapPage(SWAPENTRY Entry)
 {
-   ULONG i;
-   ULONG_PTR off;
-   KIRQL oldIrql;
+    ULONG i;
+    ULONG_PTR off;
+    KIRQL oldIrql;
 
-   i = FILE_FROM_ENTRY(Entry);
-   off = OFFSET_FROM_ENTRY(Entry);
+    i = FILE_FROM_ENTRY(Entry);
+    off = OFFSET_FROM_ENTRY(Entry);
 
-   KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
-   if (PagingFileList[i] == NULL)
-   {
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   KeAcquireSpinLockAtDpcLevel(&PagingFileList[i]->AllocMapLock);
+    KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
+    if (PagingFileList[i] == NULL)
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    KeAcquireSpinLockAtDpcLevel(&PagingFileList[i]->AllocMapLock);
 
-   PagingFileList[i]->AllocMap[off >> 5] &= (~(1 << (off % 32)));
+    PagingFileList[i]->AllocMap[off >> 5] &= (~(1 << (off % 32)));
 
-   PagingFileList[i]->FreePages++;
-   PagingFileList[i]->UsedPages--;
+    PagingFileList[i]->FreePages++;
+    PagingFileList[i]->UsedPages--;
 
-   MiFreeSwapPages++;
-   MiUsedSwapPages--;
+    MiFreeSwapPages++;
+    MiUsedSwapPages--;
 
-   KeReleaseSpinLockFromDpcLevel(&PagingFileList[i]->AllocMapLock);
-   KeReleaseSpinLock(&PagingFileListLock, oldIrql);
+    KeReleaseSpinLockFromDpcLevel(&PagingFileList[i]->AllocMapLock);
+    KeReleaseSpinLock(&PagingFileListLock, oldIrql);
 }
 
 SWAPENTRY
 NTAPI
 MmAllocSwapPage(VOID)
 {
-   KIRQL oldIrql;
-   ULONG i;
-   ULONG off;
-   SWAPENTRY entry;
-
-   KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
-
-   if (MiFreeSwapPages == 0)
-   {
-      KeReleaseSpinLock(&PagingFileListLock, oldIrql);
-      return(0);
-   }
-
-   for (i = 0; i < MAX_PAGING_FILES; i++)
-   {
-      if (PagingFileList[i] != NULL &&
-            PagingFileList[i]->FreePages >= 1)
-      {
-         off = MiAllocPageFromPagingFile(PagingFileList[i]);
-         if (off == 0xFFFFFFFF)
-         {
-            KeBugCheck(MEMORY_MANAGEMENT);
+    KIRQL oldIrql;
+    ULONG i;
+    ULONG off;
+    SWAPENTRY entry;
+
+    KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
+
+    if (MiFreeSwapPages == 0)
+    {
+        KeReleaseSpinLock(&PagingFileListLock, oldIrql);
+        return(0);
+    }
+
+    for (i = 0; i < MAX_PAGING_FILES; i++)
+    {
+        if (PagingFileList[i] != NULL &&
+                PagingFileList[i]->FreePages >= 1)
+        {
+            off = MiAllocPageFromPagingFile(PagingFileList[i]);
+            if (off == 0xFFFFFFFF)
+            {
+                KeBugCheck(MEMORY_MANAGEMENT);
+                KeReleaseSpinLock(&PagingFileListLock, oldIrql);
+                return(STATUS_UNSUCCESSFUL);
+            }
+            MiUsedSwapPages++;
+            MiFreeSwapPages--;
             KeReleaseSpinLock(&PagingFileListLock, oldIrql);
-            return(STATUS_UNSUCCESSFUL);
-         }
-         MiUsedSwapPages++;
-         MiFreeSwapPages--;
-         KeReleaseSpinLock(&PagingFileListLock, oldIrql);
-
-         entry = ENTRY_FROM_FILE_OFFSET(i, off);
-         return(entry);
-      }
-   }
-
-   KeReleaseSpinLock(&PagingFileListLock, oldIrql);
-   KeBugCheck(MEMORY_MANAGEMENT);
-   return(0);
+
+            entry = ENTRY_FROM_FILE_OFFSET(i, off);
+            return(entry);
+        }
+    }
+
+    KeReleaseSpinLock(&PagingFileListLock, oldIrql);
+    KeBugCheck(MEMORY_MANAGEMENT);
+    return(0);
 }
 
 static PRETRIEVEL_DESCRIPTOR_LIST FASTCALL
 MmAllocRetrievelDescriptorList(ULONG Pairs)
 {
-   ULONG Size;
-   PRETRIEVEL_DESCRIPTOR_LIST RetDescList;
+    ULONG Size;
+    PRETRIEVEL_DESCRIPTOR_LIST RetDescList;
 
-   Size = sizeof(RETRIEVEL_DESCRIPTOR_LIST) + Pairs * 2 * sizeof(LARGE_INTEGER);
-   RetDescList = ExAllocatePool(NonPagedPool, Size);
-   if (RetDescList)
-   {
-      RtlZeroMemory(RetDescList, Size);
-   }
+    Size = sizeof(RETRIEVEL_DESCRIPTOR_LIST) + Pairs * 2 * sizeof(LARGE_INTEGER);
+    RetDescList = ExAllocatePool(NonPagedPool, Size);
+    if (RetDescList)
+    {
+        RtlZeroMemory(RetDescList, Size);
+    }
 
-   return RetDescList;
+    return RetDescList;
 }
 
 NTSTATUS NTAPI
@@ -482,341 +482,341 @@ NtCreatePagingFile(IN PUNICODE_STRING FileName,
                    IN PLARGE_INTEGER MaximumSize,
                    IN ULONG Reserved)
 {
-   NTSTATUS Status;
-   OBJECT_ATTRIBUTES ObjectAttributes;
-   HANDLE FileHandle;
-   IO_STATUS_BLOCK IoStatus;
-   PFILE_OBJECT FileObject;
-   PPAGINGFILE PagingFile;
-   KIRQL oldIrql;
-   ULONG AllocMapSize;
-   FILE_FS_SIZE_INFORMATION FsSizeInformation;
-   PRETRIEVEL_DESCRIPTOR_LIST RetDescList;
-   PRETRIEVEL_DESCRIPTOR_LIST CurrentRetDescList;
-   ULONG i;
-   ULONG BytesPerAllocationUnit;
-   LARGE_INTEGER Vcn;
-   ULONG ExtentCount;
-   LARGE_INTEGER MaxVcn;
-   ULONG Count;
-   ULONG Size;
-   KPROCESSOR_MODE PreviousMode;
-   UNICODE_STRING CapturedFileName;
-   LARGE_INTEGER SafeInitialSize, SafeMaximumSize;
-
-   DPRINT("NtCreatePagingFile(FileName %wZ, InitialSize %I64d)\n",
-          FileName, InitialSize->QuadPart);
-
-   if (MmNumberOfPagingFiles >= MAX_PAGING_FILES)
-   {
-      return(STATUS_TOO_MANY_PAGING_FILES);
-   }
-
-   PreviousMode = ExGetPreviousMode();
-
-   if (PreviousMode != KernelMode)
-   {
-      _SEH2_TRY
-      {
-         SafeInitialSize = ProbeForReadLargeInteger(InitialSize);
-         SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
-      }
-      _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
-      {
-          /* Return the exception code */
-          _SEH2_YIELD(return _SEH2_GetExceptionCode());
-      }
-      _SEH2_END;
-   }
-   else
-   {
-      SafeInitialSize = *InitialSize;
-      SafeMaximumSize = *MaximumSize;
-   }
-
-   /* Pagefiles can't be larger than 4GB and ofcourse the minimum should be
-      smaller than the maximum */
-   if (0 != SafeInitialSize.u.HighPart)
-   {
-      return STATUS_INVALID_PARAMETER_2;
-   }
-   if (0 != SafeMaximumSize.u.HighPart)
-   {
-      return STATUS_INVALID_PARAMETER_3;
-   }
-   if (SafeMaximumSize.u.LowPart < SafeInitialSize.u.LowPart)
-   {
-      return STATUS_INVALID_PARAMETER_MIX;
-   }
-
-   Status = ProbeAndCaptureUnicodeString(&CapturedFileName,
-                                         PreviousMode,
-                                         FileName);
-   if (!NT_SUCCESS(Status))
-   {
-      return(Status);
-   }
-
-   InitializeObjectAttributes(&ObjectAttributes,
-                              &CapturedFileName,
-                              0,
-                              NULL,
-                              NULL);
-
-   Status = IoCreateFile(&FileHandle,
-                         FILE_ALL_ACCESS,
-                         &ObjectAttributes,
-                         &IoStatus,
-                         NULL,
-                         0,
-                         0,
-                         FILE_OPEN_IF,
-                         FILE_SYNCHRONOUS_IO_NONALERT,
-                         NULL,
-                         0,
-                         CreateFileTypeNone,
-                         NULL,
-                         SL_OPEN_PAGING_FILE | IO_NO_PARAMETER_CHECKING);
-
-   ReleaseCapturedUnicodeString(&CapturedFileName,
-                                PreviousMode);
-   if (!NT_SUCCESS(Status))
-   {
-      return(Status);
-   }
-
-   Status = ZwQueryVolumeInformationFile(FileHandle,
-                                         &IoStatus,
-                                         &FsSizeInformation,
-                                         sizeof(FILE_FS_SIZE_INFORMATION),
-                                         FileFsSizeInformation);
-   if (!NT_SUCCESS(Status))
-   {
-      ZwClose(FileHandle);
-      return Status;
-   }
-
-   BytesPerAllocationUnit = FsSizeInformation.SectorsPerAllocationUnit *
-                            FsSizeInformation.BytesPerSector;
-   /* FIXME: If we have 2048 BytesPerAllocationUnit (FAT16 < 128MB) there is
-    * a problem if the paging file is fragmented. Suppose the first cluster
-    * of the paging file is cluster 3042 but cluster 3043 is NOT part of the
-    * paging file but of another file. We can't write a complete page (4096
-    * bytes) to the physical location of cluster 3042 then. */
-   if (BytesPerAllocationUnit % PAGE_SIZE)
-   {
-      DPRINT1("BytesPerAllocationUnit %lu is not a multiple of PAGE_SIZE %d\n",
-              BytesPerAllocationUnit, PAGE_SIZE);
-      ZwClose(FileHandle);
-      return STATUS_UNSUCCESSFUL;
-   }
-
-   Status = ZwSetInformationFile(FileHandle,
-                                 &IoStatus,
-                                 &SafeInitialSize,
-                                 sizeof(LARGE_INTEGER),
-                                 FileAllocationInformation);
-   if (!NT_SUCCESS(Status))
-   {
-      ZwClose(FileHandle);
-      return(Status);
-   }
-
-   Status = ObReferenceObjectByHandle(FileHandle,
-                                      FILE_ALL_ACCESS,
-                                      IoFileObjectType,
-                                      PreviousMode,
-                                      (PVOID*)&FileObject,
-                                      NULL);
-   if (!NT_SUCCESS(Status))
-   {
-      ZwClose(FileHandle);
-      return(Status);
-   }
-
-   CurrentRetDescList = RetDescList = MmAllocRetrievelDescriptorList(PAIRS_PER_RUN);
-
-   if (CurrentRetDescList == NULL)
-   {
-      ObDereferenceObject(FileObject);
-      ZwClose(FileHandle);
-      return(STATUS_NO_MEMORY);
-   }
+    NTSTATUS Status;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    HANDLE FileHandle;
+    IO_STATUS_BLOCK IoStatus;
+    PFILE_OBJECT FileObject;
+    PPAGINGFILE PagingFile;
+    KIRQL oldIrql;
+    ULONG AllocMapSize;
+    FILE_FS_SIZE_INFORMATION FsSizeInformation;
+    PRETRIEVEL_DESCRIPTOR_LIST RetDescList;
+    PRETRIEVEL_DESCRIPTOR_LIST CurrentRetDescList;
+    ULONG i;
+    ULONG BytesPerAllocationUnit;
+    LARGE_INTEGER Vcn;
+    ULONG ExtentCount;
+    LARGE_INTEGER MaxVcn;
+    ULONG Count;
+    ULONG Size;
+    KPROCESSOR_MODE PreviousMode;
+    UNICODE_STRING CapturedFileName;
+    LARGE_INTEGER SafeInitialSize, SafeMaximumSize;
+
+    DPRINT("NtCreatePagingFile(FileName %wZ, InitialSize %I64d)\n",
+           FileName, InitialSize->QuadPart);
+
+    if (MmNumberOfPagingFiles >= MAX_PAGING_FILES)
+    {
+        return(STATUS_TOO_MANY_PAGING_FILES);
+    }
+
+    PreviousMode = ExGetPreviousMode();
+
+    if (PreviousMode != KernelMode)
+    {
+        _SEH2_TRY
+        {
+            SafeInitialSize = ProbeForReadLargeInteger(InitialSize);
+            SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
+        }
+        _SEH2_END;
+    }
+    else
+    {
+        SafeInitialSize = *InitialSize;
+        SafeMaximumSize = *MaximumSize;
+    }
+
+    /* Pagefiles can't be larger than 4GB and ofcourse the minimum should be
+       smaller than the maximum */
+    if (0 != SafeInitialSize.u.HighPart)
+    {
+        return STATUS_INVALID_PARAMETER_2;
+    }
+    if (0 != SafeMaximumSize.u.HighPart)
+    {
+        return STATUS_INVALID_PARAMETER_3;
+    }
+    if (SafeMaximumSize.u.LowPart < SafeInitialSize.u.LowPart)
+    {
+        return STATUS_INVALID_PARAMETER_MIX;
+    }
+
+    Status = ProbeAndCaptureUnicodeString(&CapturedFileName,
+                                          PreviousMode,
+                                          FileName);
+    if (!NT_SUCCESS(Status))
+    {
+        return(Status);
+    }
+
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &CapturedFileName,
+                               0,
+                               NULL,
+                               NULL);
+
+    Status = IoCreateFile(&FileHandle,
+                          FILE_ALL_ACCESS,
+                          &ObjectAttributes,
+                          &IoStatus,
+                          NULL,
+                          0,
+                          0,
+                          FILE_OPEN_IF,
+                          FILE_SYNCHRONOUS_IO_NONALERT,
+                          NULL,
+                          0,
+                          CreateFileTypeNone,
+                          NULL,
+                          SL_OPEN_PAGING_FILE | IO_NO_PARAMETER_CHECKING);
+
+    ReleaseCapturedUnicodeString(&CapturedFileName,
+                                 PreviousMode);
+    if (!NT_SUCCESS(Status))
+    {
+        return(Status);
+    }
+
+    Status = ZwQueryVolumeInformationFile(FileHandle,
+                                          &IoStatus,
+                                          &FsSizeInformation,
+                                          sizeof(FILE_FS_SIZE_INFORMATION),
+                                          FileFsSizeInformation);
+    if (!NT_SUCCESS(Status))
+    {
+        ZwClose(FileHandle);
+        return Status;
+    }
+
+    BytesPerAllocationUnit = FsSizeInformation.SectorsPerAllocationUnit *
+                             FsSizeInformation.BytesPerSector;
+    /* FIXME: If we have 2048 BytesPerAllocationUnit (FAT16 < 128MB) there is
+     * a problem if the paging file is fragmented. Suppose the first cluster
+     * of the paging file is cluster 3042 but cluster 3043 is NOT part of the
+     * paging file but of another file. We can't write a complete page (4096
+     * bytes) to the physical location of cluster 3042 then. */
+    if (BytesPerAllocationUnit % PAGE_SIZE)
+    {
+        DPRINT1("BytesPerAllocationUnit %lu is not a multiple of PAGE_SIZE %d\n",
+                BytesPerAllocationUnit, PAGE_SIZE);
+        ZwClose(FileHandle);
+        return STATUS_UNSUCCESSFUL;
+    }
+
+    Status = ZwSetInformationFile(FileHandle,
+                                  &IoStatus,
+                                  &SafeInitialSize,
+                                  sizeof(LARGE_INTEGER),
+                                  FileAllocationInformation);
+    if (!NT_SUCCESS(Status))
+    {
+        ZwClose(FileHandle);
+        return(Status);
+    }
+
+    Status = ObReferenceObjectByHandle(FileHandle,
+                                       FILE_ALL_ACCESS,
+                                       IoFileObjectType,
+                                       PreviousMode,
+                                       (PVOID*)&FileObject,
+                                       NULL);
+    if (!NT_SUCCESS(Status))
+    {
+        ZwClose(FileHandle);
+        return(Status);
+    }
+
+    CurrentRetDescList = RetDescList = MmAllocRetrievelDescriptorList(PAIRS_PER_RUN);
+
+    if (CurrentRetDescList == NULL)
+    {
+        ObDereferenceObject(FileObject);
+        ZwClose(FileHandle);
+        return(STATUS_NO_MEMORY);
+    }
 
 #if defined(__GNUC__)
-   Vcn.QuadPart = 0LL;
+    Vcn.QuadPart = 0LL;
 #else
 
-   Vcn.QuadPart = 0;
+    Vcn.QuadPart = 0;
 #endif
 
-   ExtentCount = 0;
-   MaxVcn.QuadPart = (SafeInitialSize.QuadPart + BytesPerAllocationUnit - 1) / BytesPerAllocationUnit;
-   while(1)
-   {
-      Status = ZwFsControlFile(FileHandle,
-                               0,
-                               NULL,
-                               NULL,
-                               &IoStatus,
-                               FSCTL_GET_RETRIEVAL_POINTERS,
-                               &Vcn,
-                               sizeof(LARGE_INTEGER),
-                               &CurrentRetDescList->RetrievalPointers,
-                               sizeof(RETRIEVAL_POINTERS_BUFFER) + PAIRS_PER_RUN * 2 * sizeof(LARGE_INTEGER));
-      if (!NT_SUCCESS(Status))
-      {
-         while (RetDescList)
-         {
-            CurrentRetDescList = RetDescList;
-            RetDescList = RetDescList->Next;
-            ExFreePool(CurrentRetDescList);
-         }
-         ObDereferenceObject(FileObject);
-         ZwClose(FileHandle);
-         return(Status);
-      }
-      ExtentCount += CurrentRetDescList->RetrievalPointers.ExtentCount;
-      if (CurrentRetDescList->RetrievalPointers.Extents[CurrentRetDescList->RetrievalPointers.ExtentCount-1].NextVcn.QuadPart < MaxVcn.QuadPart)
-      {
-         CurrentRetDescList->Next = MmAllocRetrievelDescriptorList(PAIRS_PER_RUN);
-         if (CurrentRetDescList->Next == NULL)
-         {
+    ExtentCount = 0;
+    MaxVcn.QuadPart = (SafeInitialSize.QuadPart + BytesPerAllocationUnit - 1) / BytesPerAllocationUnit;
+    while(1)
+    {
+        Status = ZwFsControlFile(FileHandle,
+                                 0,
+                                 NULL,
+                                 NULL,
+                                 &IoStatus,
+                                 FSCTL_GET_RETRIEVAL_POINTERS,
+                                 &Vcn,
+                                 sizeof(LARGE_INTEGER),
+                                 &CurrentRetDescList->RetrievalPointers,
+                                 sizeof(RETRIEVAL_POINTERS_BUFFER) + PAIRS_PER_RUN * 2 * sizeof(LARGE_INTEGER));
+        if (!NT_SUCCESS(Status))
+        {
             while (RetDescList)
             {
-               CurrentRetDescList = RetDescList;
-               RetDescList = RetDescList->Next;
-               ExFreePool(CurrentRetDescList);
+                CurrentRetDescList = RetDescList;
+                RetDescList = RetDescList->Next;
+                ExFreePool(CurrentRetDescList);
             }
             ObDereferenceObject(FileObject);
             ZwClose(FileHandle);
-            return(STATUS_NO_MEMORY);
-         }
-         Vcn = CurrentRetDescList->RetrievalPointers.Extents[CurrentRetDescList->RetrievalPointers.ExtentCount-1].NextVcn;
-         CurrentRetDescList = CurrentRetDescList->Next;
-      }
-      else
-      {
-         break;
-      }
-   }
-
-   PagingFile = ExAllocatePool(NonPagedPool, sizeof(*PagingFile));
-   if (PagingFile == NULL)
-   {
-      while (RetDescList)
-      {
-         CurrentRetDescList = RetDescList;
-         RetDescList = RetDescList->Next;
-         ExFreePool(CurrentRetDescList);
-      }
-      ObDereferenceObject(FileObject);
-      ZwClose(FileHandle);
-      return(STATUS_NO_MEMORY);
-   }
-
-   RtlZeroMemory(PagingFile, sizeof(*PagingFile));
-
-   PagingFile->FileObject = FileObject;
-   PagingFile->MaximumSize.QuadPart = SafeMaximumSize.QuadPart;
-   PagingFile->CurrentSize.QuadPart = SafeInitialSize.QuadPart;
-   PagingFile->FreePages = (ULONG)(SafeInitialSize.QuadPart / PAGE_SIZE);
-   PagingFile->UsedPages = 0;
-   KeInitializeSpinLock(&PagingFile->AllocMapLock);
-
-   AllocMapSize = (PagingFile->FreePages / 32) + 1;
-   PagingFile->AllocMap = ExAllocatePool(NonPagedPool,
-                                         AllocMapSize * sizeof(ULONG));
-   PagingFile->AllocMapSize = AllocMapSize;
-
-   if (PagingFile->AllocMap == NULL)
-   {
-      while (RetDescList)
-      {
-         CurrentRetDescList = RetDescList;
-         RetDescList = RetDescList->Next;
-         ExFreePool(CurrentRetDescList);
-      }
-      ExFreePool(PagingFile);
-      ObDereferenceObject(FileObject);
-      ZwClose(FileHandle);
-      return(STATUS_NO_MEMORY);
-   }
-   DPRINT("ExtentCount: %lu\n", ExtentCount);
-   Size = sizeof(RETRIEVAL_POINTERS_BUFFER) + ExtentCount * 2 * sizeof(LARGE_INTEGER);
-   PagingFile->RetrievalPointers = ExAllocatePool(NonPagedPool, Size);
-   if (PagingFile->RetrievalPointers == NULL)
-   {
-      while (RetDescList)
-      {
-         CurrentRetDescList = RetDescList;
-         RetDescList = RetDescList->Next;
-         ExFreePool(CurrentRetDescList);
-      }
-      ExFreePool(PagingFile->AllocMap);
-      ExFreePool(PagingFile);
-      ObDereferenceObject(FileObject);
-      ZwClose(FileHandle);
-      return(STATUS_NO_MEMORY);
-   }
-
-   RtlZeroMemory(PagingFile->AllocMap, AllocMapSize * sizeof(ULONG));
-   RtlZeroMemory(PagingFile->RetrievalPointers, Size);
-
-   Count = 0;
-   PagingFile->RetrievalPointers->ExtentCount = ExtentCount;
-   PagingFile->RetrievalPointers->StartingVcn = RetDescList->RetrievalPointers.StartingVcn;
-   CurrentRetDescList = RetDescList;
-   while (CurrentRetDescList)
-   {
-      memcpy(&PagingFile->RetrievalPointers->Extents[Count],
-             CurrentRetDescList->RetrievalPointers.Extents,
-             CurrentRetDescList->RetrievalPointers.ExtentCount * 2 * sizeof(LARGE_INTEGER));
-      Count += CurrentRetDescList->RetrievalPointers.ExtentCount;
-      RetDescList = CurrentRetDescList;
-      CurrentRetDescList = CurrentRetDescList->Next;
-      ExFreePool(RetDescList);
-   }
-
-   if (PagingFile->RetrievalPointers->ExtentCount != ExtentCount ||
-         PagingFile->RetrievalPointers->Extents[ExtentCount - 1].NextVcn.QuadPart != MaxVcn.QuadPart)
-   {
-      ExFreePool(PagingFile->RetrievalPointers);
-      ExFreePool(PagingFile->AllocMap);
-      ExFreePool(PagingFile);
-      ObDereferenceObject(FileObject);
-      ZwClose(FileHandle);
-      return(STATUS_UNSUCCESSFUL);
-   }
-
-   /*
-    * Change the entries from lcn's to volume offset's.
-    */
-   PagingFile->RetrievalPointers->StartingVcn.QuadPart *= BytesPerAllocationUnit;
-   for (i = 0; i < ExtentCount; i++)
-   {
-      PagingFile->RetrievalPointers->Extents[i].Lcn.QuadPart *= BytesPerAllocationUnit;
-      PagingFile->RetrievalPointers->Extents[i].NextVcn.QuadPart *= BytesPerAllocationUnit;
-   }
-
-   KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
-   for (i = 0; i < MAX_PAGING_FILES; i++)
-   {
-      if (PagingFileList[i] == NULL)
-      {
-         PagingFileList[i] = PagingFile;
-         break;
-      }
-   }
-   MiFreeSwapPages = MiFreeSwapPages + PagingFile->FreePages;
-   MmNumberOfPagingFiles++;
-   KeReleaseSpinLock(&PagingFileListLock, oldIrql);
-
-   ZwClose(FileHandle);
-
-   MmSwapSpaceMessage = FALSE;
-
-   return(STATUS_SUCCESS);
+            return(Status);
+        }
+        ExtentCount += CurrentRetDescList->RetrievalPointers.ExtentCount;
+        if (CurrentRetDescList->RetrievalPointers.Extents[CurrentRetDescList->RetrievalPointers.ExtentCount-1].NextVcn.QuadPart < MaxVcn.QuadPart)
+        {
+            CurrentRetDescList->Next = MmAllocRetrievelDescriptorList(PAIRS_PER_RUN);
+            if (CurrentRetDescList->Next == NULL)
+            {
+                while (RetDescList)
+                {
+                    CurrentRetDescList = RetDescList;
+                    RetDescList = RetDescList->Next;
+                    ExFreePool(CurrentRetDescList);
+                }
+                ObDereferenceObject(FileObject);
+                ZwClose(FileHandle);
+                return(STATUS_NO_MEMORY);
+            }
+            Vcn = CurrentRetDescList->RetrievalPointers.Extents[CurrentRetDescList->RetrievalPointers.ExtentCount-1].NextVcn;
+            CurrentRetDescList = CurrentRetDescList->Next;
+        }
+        else
+        {
+            break;
+        }
+    }
+
+    PagingFile = ExAllocatePool(NonPagedPool, sizeof(*PagingFile));
+    if (PagingFile == NULL)
+    {
+        while (RetDescList)
+        {
+            CurrentRetDescList = RetDescList;
+            RetDescList = RetDescList->Next;
+            ExFreePool(CurrentRetDescList);
+        }
+        ObDereferenceObject(FileObject);
+        ZwClose(FileHandle);
+        return(STATUS_NO_MEMORY);
+    }
+
+    RtlZeroMemory(PagingFile, sizeof(*PagingFile));
+
+    PagingFile->FileObject = FileObject;
+    PagingFile->MaximumSize.QuadPart = SafeMaximumSize.QuadPart;
+    PagingFile->CurrentSize.QuadPart = SafeInitialSize.QuadPart;
+    PagingFile->FreePages = (ULONG)(SafeInitialSize.QuadPart / PAGE_SIZE);
+    PagingFile->UsedPages = 0;
+    KeInitializeSpinLock(&PagingFile->AllocMapLock);
+
+    AllocMapSize = (PagingFile->FreePages / 32) + 1;
+    PagingFile->AllocMap = ExAllocatePool(NonPagedPool,
+                                          AllocMapSize * sizeof(ULONG));
+    PagingFile->AllocMapSize = AllocMapSize;
+
+    if (PagingFile->AllocMap == NULL)
+    {
+        while (RetDescList)
+        {
+            CurrentRetDescList = RetDescList;
+            RetDescList = RetDescList->Next;
+            ExFreePool(CurrentRetDescList);
+        }
+        ExFreePool(PagingFile);
+        ObDereferenceObject(FileObject);
+        ZwClose(FileHandle);
+        return(STATUS_NO_MEMORY);
+    }
+    DPRINT("ExtentCount: %lu\n", ExtentCount);
+    Size = sizeof(RETRIEVAL_POINTERS_BUFFER) + ExtentCount * 2 * sizeof(LARGE_INTEGER);
+    PagingFile->RetrievalPointers = ExAllocatePool(NonPagedPool, Size);
+    if (PagingFile->RetrievalPointers == NULL)
+    {
+        while (RetDescList)
+        {
+            CurrentRetDescList = RetDescList;
+            RetDescList = RetDescList->Next;
+            ExFreePool(CurrentRetDescList);
+        }
+        ExFreePool(PagingFile->AllocMap);
+        ExFreePool(PagingFile);
+        ObDereferenceObject(FileObject);
+        ZwClose(FileHandle);
+        return(STATUS_NO_MEMORY);
+    }
+
+    RtlZeroMemory(PagingFile->AllocMap, AllocMapSize * sizeof(ULONG));
+    RtlZeroMemory(PagingFile->RetrievalPointers, Size);
+
+    Count = 0;
+    PagingFile->RetrievalPointers->ExtentCount = ExtentCount;
+    PagingFile->RetrievalPointers->StartingVcn = RetDescList->RetrievalPointers.StartingVcn;
+    CurrentRetDescList = RetDescList;
+    while (CurrentRetDescList)
+    {
+        memcpy(&PagingFile->RetrievalPointers->Extents[Count],
+               CurrentRetDescList->RetrievalPointers.Extents,
+               CurrentRetDescList->RetrievalPointers.ExtentCount * 2 * sizeof(LARGE_INTEGER));
+        Count += CurrentRetDescList->RetrievalPointers.ExtentCount;
+        RetDescList = CurrentRetDescList;
+        CurrentRetDescList = CurrentRetDescList->Next;
+        ExFreePool(RetDescList);
+    }
+
+    if (PagingFile->RetrievalPointers->ExtentCount != ExtentCount ||
+            PagingFile->RetrievalPointers->Extents[ExtentCount - 1].NextVcn.QuadPart != MaxVcn.QuadPart)
+    {
+        ExFreePool(PagingFile->RetrievalPointers);
+        ExFreePool(PagingFile->AllocMap);
+        ExFreePool(PagingFile);
+        ObDereferenceObject(FileObject);
+        ZwClose(FileHandle);
+        return(STATUS_UNSUCCESSFUL);
+    }
+
+    /*
+     * Change the entries from lcn's to volume offset's.
+     */
+    PagingFile->RetrievalPointers->StartingVcn.QuadPart *= BytesPerAllocationUnit;
+    for (i = 0; i < ExtentCount; i++)
+    {
+        PagingFile->RetrievalPointers->Extents[i].Lcn.QuadPart *= BytesPerAllocationUnit;
+        PagingFile->RetrievalPointers->Extents[i].NextVcn.QuadPart *= BytesPerAllocationUnit;
+    }
+
+    KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
+    for (i = 0; i < MAX_PAGING_FILES; i++)
+    {
+        if (PagingFileList[i] == NULL)
+        {
+            PagingFileList[i] = PagingFile;
+            break;
+        }
+    }
+    MiFreeSwapPages = MiFreeSwapPages + PagingFile->FreePages;
+    MmNumberOfPagingFiles++;
+    KeReleaseSpinLock(&PagingFileListLock, oldIrql);
+
+    ZwClose(FileHandle);
+
+    MmSwapSpaceMessage = FALSE;
+
+    return(STATUS_SUCCESS);
 }
 
 /* EOF */
index eb71540..d62fcb4 100644 (file)
@@ -22,12 +22,12 @@ InsertAfterEntry(PLIST_ENTRY Previous,
  * FUNCTION: Insert a list entry after another entry in the list
  */
 {
-   Previous->Flink->Blink = Entry;
+    Previous->Flink->Blink = Entry;
 
-   Entry->Flink = Previous->Flink;
-   Entry->Blink = Previous;
+    Entry->Flink = Previous->Flink;
+    Entry->Blink = Previous;
 
-   Previous->Flink = Entry;
+    Previous->Flink = Entry;
 }
 
 static PMM_REGION
@@ -36,71 +36,71 @@ MmSplitRegion(PMM_REGION InitialRegion, PVOID InitialBaseAddress,
               ULONG NewProtect, PMMSUPPORT AddressSpace,
               PMM_ALTER_REGION_FUNC AlterFunc)
 {
-   PMM_REGION NewRegion1;
-   PMM_REGION NewRegion2;
-   SIZE_T InternalLength;
+    PMM_REGION NewRegion1;
+    PMM_REGION NewRegion2;
+    SIZE_T InternalLength;
 
-   /* Allocate this in front otherwise the failure case is too difficult. */
-   NewRegion2 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
-                                      TAG_MM_REGION);
-   if (NewRegion2 == NULL)
-   {
-      return(NULL);
-   }
+    /* Allocate this in front otherwise the failure case is too difficult. */
+    NewRegion2 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
+                                       TAG_MM_REGION);
+    if (NewRegion2 == NULL)
+    {
+        return(NULL);
+    }
 
-   /* Create the new region. */
-   NewRegion1 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
-                                      TAG_MM_REGION);
-   if (NewRegion1 == NULL)
-   {
-      ExFreePoolWithTag(NewRegion2, TAG_MM_REGION);
-      return(NULL);
-   }
-   NewRegion1->Type = NewType;
-   NewRegion1->Protect = NewProtect;
-   InternalLength = ((char*)InitialBaseAddress + InitialRegion->Length) - (char*)StartAddress;
-   InternalLength = min(InternalLength, Length);
-   NewRegion1->Length = InternalLength;
-   InsertAfterEntry(&InitialRegion->RegionListEntry,
-                    &NewRegion1->RegionListEntry);
+    /* Create the new region. */
+    NewRegion1 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
+                                       TAG_MM_REGION);
+    if (NewRegion1 == NULL)
+    {
+        ExFreePoolWithTag(NewRegion2, TAG_MM_REGION);
+        return(NULL);
+    }
+    NewRegion1->Type = NewType;
+    NewRegion1->Protect = NewProtect;
+    InternalLength = ((char*)InitialBaseAddress + InitialRegion->Length) - (char*)StartAddress;
+    InternalLength = min(InternalLength, Length);
+    NewRegion1->Length = InternalLength;
+    InsertAfterEntry(&InitialRegion->RegionListEntry,
+                     &NewRegion1->RegionListEntry);
 
-   /*
-    * Call our helper function to do the changes on the addresses contained
-    * in the initial region.
-    */
-   AlterFunc(AddressSpace, StartAddress, InternalLength, InitialRegion->Type,
-             InitialRegion->Protect, NewType, NewProtect);
+    /*
+     * Call our helper function to do the changes on the addresses contained
+     * in the initial region.
+     */
+    AlterFunc(AddressSpace, StartAddress, InternalLength, InitialRegion->Type,
+              InitialRegion->Protect, NewType, NewProtect);
 
-   /*
-    * If necessary create a new region for the portion of the initial region
-    * beyond the range of addresses to alter.
-    */
-   if (((char*)InitialBaseAddress + InitialRegion->Length) > ((char*)StartAddress + Length))
-   {
-      NewRegion2->Type = InitialRegion->Type;
-      NewRegion2->Protect = InitialRegion->Protect;
-      NewRegion2->Length = ((char*)InitialBaseAddress + InitialRegion->Length) -
-                           ((char*)StartAddress + Length);
-      InsertAfterEntry(&NewRegion1->RegionListEntry,
-                       &NewRegion2->RegionListEntry);
-   }
-   else
-   {
-      ExFreePoolWithTag(NewRegion2, TAG_MM_REGION);
-   }
+    /*
+     * If necessary create a new region for the portion of the initial region
+     * beyond the range of addresses to alter.
+     */
+    if (((char*)InitialBaseAddress + InitialRegion->Length) > ((char*)StartAddress + Length))
+    {
+        NewRegion2->Type = InitialRegion->Type;
+        NewRegion2->Protect = InitialRegion->Protect;
+        NewRegion2->Length = ((char*)InitialBaseAddress + InitialRegion->Length) -
+                             ((char*)StartAddress + Length);
+        InsertAfterEntry(&NewRegion1->RegionListEntry,
+                         &NewRegion2->RegionListEntry);
+    }
+    else
+    {
+        ExFreePoolWithTag(NewRegion2, TAG_MM_REGION);
+    }
 
-   /* Either remove or shrink the initial region. */
-   if (InitialBaseAddress == StartAddress)
-   {
-      RemoveEntryList(&InitialRegion->RegionListEntry);
-      ExFreePoolWithTag(InitialRegion, TAG_MM_REGION);
-   }
-   else
-   {
-      InitialRegion->Length = (char*)StartAddress - (char*)InitialBaseAddress;
-   }
+    /* Either remove or shrink the initial region. */
+    if (InitialBaseAddress == StartAddress)
+    {
+        RemoveEntryList(&InitialRegion->RegionListEntry);
+        ExFreePoolWithTag(InitialRegion, TAG_MM_REGION);
+    }
+    else
+    {
+        InitialRegion->Length = (char*)StartAddress - (char*)InitialBaseAddress;
+    }
 
-   return(NewRegion1);
+    return(NewRegion1);
 }
 
 NTSTATUS
@@ -109,129 +109,129 @@ MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
               PLIST_ENTRY RegionListHead, PVOID StartAddress, SIZE_T Length,
               ULONG NewType, ULONG NewProtect, PMM_ALTER_REGION_FUNC AlterFunc)
 {
-   PMM_REGION InitialRegion;
-   PVOID InitialBaseAddress = NULL;
-   PMM_REGION NewRegion;
-   PLIST_ENTRY CurrentEntry;
-   PMM_REGION CurrentRegion = NULL;
-   PVOID CurrentBaseAddress;
-   SIZE_T RemainingLength;
+    PMM_REGION InitialRegion;
+    PVOID InitialBaseAddress = NULL;
+    PMM_REGION NewRegion;
+    PLIST_ENTRY CurrentEntry;
+    PMM_REGION CurrentRegion = NULL;
+    PVOID CurrentBaseAddress;
+    SIZE_T RemainingLength;
 
-   /*
-    * Find the first region containing part of the range of addresses to
-    * be altered.
-    */
-   InitialRegion = MmFindRegion(BaseAddress, RegionListHead, StartAddress,
-                                &InitialBaseAddress);
-   /*
-    * If necessary then split the region into the affected and unaffected parts.
-    */
-   if (InitialRegion->Type != NewType || InitialRegion->Protect != NewProtect)
-   {
-      NewRegion = MmSplitRegion(InitialRegion, InitialBaseAddress,
-                                StartAddress, Length, NewType, NewProtect,
-                                AddressSpace, AlterFunc);
-      if (NewRegion == NULL)
-      {
-         return(STATUS_NO_MEMORY);
-      }
-      if(NewRegion->Length < Length)
-         RemainingLength = Length - NewRegion->Length;
-      else
-         RemainingLength = 0;
-   }
-   else
-   {
-      NewRegion = InitialRegion;
-      if(((ULONG_PTR)InitialBaseAddress + NewRegion->Length) <
-            ((ULONG_PTR)StartAddress + Length))
-         RemainingLength = ((ULONG_PTR)StartAddress + Length) - ((ULONG_PTR)InitialBaseAddress + NewRegion->Length);
-      else
-         RemainingLength = 0;
-   }
+    /*
+     * Find the first region containing part of the range of addresses to
+     * be altered.
+     */
+    InitialRegion = MmFindRegion(BaseAddress, RegionListHead, StartAddress,
+                                 &InitialBaseAddress);
+    /*
+     * If necessary then split the region into the affected and unaffected parts.
+     */
+    if (InitialRegion->Type != NewType || InitialRegion->Protect != NewProtect)
+    {
+        NewRegion = MmSplitRegion(InitialRegion, InitialBaseAddress,
+                                  StartAddress, Length, NewType, NewProtect,
+                                  AddressSpace, AlterFunc);
+        if (NewRegion == NULL)
+        {
+            return(STATUS_NO_MEMORY);
+        }
+        if(NewRegion->Length < Length)
+            RemainingLength = Length - NewRegion->Length;
+        else
+            RemainingLength = 0;
+    }
+    else
+    {
+        NewRegion = InitialRegion;
+        if(((ULONG_PTR)InitialBaseAddress + NewRegion->Length) <
+                ((ULONG_PTR)StartAddress + Length))
+            RemainingLength = ((ULONG_PTR)StartAddress + Length) - ((ULONG_PTR)InitialBaseAddress + NewRegion->Length);
+        else
+            RemainingLength = 0;
+    }
 
-   /*
-    * Free any complete regions that are containing in the range of addresses
-    * and call the helper function to actually do the changes.
-    */
-   CurrentEntry = NewRegion->RegionListEntry.Flink;
-   CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
-                                     RegionListEntry);
-   CurrentBaseAddress = (char*)StartAddress + NewRegion->Length;
-   while (RemainingLength > 0 && CurrentRegion->Length <= RemainingLength &&
-          CurrentEntry != RegionListHead)
-   {
-      if (CurrentRegion->Type != NewType ||
-            CurrentRegion->Protect != NewProtect)
-      {
-         AlterFunc(AddressSpace, CurrentBaseAddress, CurrentRegion->Length,
-                   CurrentRegion->Type, CurrentRegion->Protect,
-                   NewType, NewProtect);
-      }
+    /*
+     * Free any complete regions that are containing in the range of addresses
+     * and call the helper function to actually do the changes.
+     */
+    CurrentEntry = NewRegion->RegionListEntry.Flink;
+    CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
+                                      RegionListEntry);
+    CurrentBaseAddress = (char*)StartAddress + NewRegion->Length;
+    while (RemainingLength > 0 && CurrentRegion->Length <= RemainingLength &&
+            CurrentEntry != RegionListHead)
+    {
+        if (CurrentRegion->Type != NewType ||
+                CurrentRegion->Protect != NewProtect)
+        {
+            AlterFunc(AddressSpace, CurrentBaseAddress, CurrentRegion->Length,
+                      CurrentRegion->Type, CurrentRegion->Protect,
+                      NewType, NewProtect);
+        }
 
-      CurrentBaseAddress = (PVOID)((ULONG_PTR)CurrentBaseAddress + CurrentRegion->Length);
-      NewRegion->Length += CurrentRegion->Length;
-      RemainingLength -= CurrentRegion->Length;
-      CurrentEntry = CurrentEntry->Flink;
-      RemoveEntryList(&CurrentRegion->RegionListEntry);
-      ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
-      CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
-                                        RegionListEntry);
-   }
+        CurrentBaseAddress = (PVOID)((ULONG_PTR)CurrentBaseAddress + CurrentRegion->Length);
+        NewRegion->Length += CurrentRegion->Length;
+        RemainingLength -= CurrentRegion->Length;
+        CurrentEntry = CurrentEntry->Flink;
+        RemoveEntryList(&CurrentRegion->RegionListEntry);
+        ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
+        CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
+                                          RegionListEntry);
+    }
 
-   /*
-    * Split any final region.
-    */
-   if (RemainingLength > 0 && CurrentEntry != RegionListHead)
-   {
-      CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
-                                        RegionListEntry);
-      if (CurrentRegion->Type != NewType ||
-            CurrentRegion->Protect != NewProtect)
-      {
-         AlterFunc(AddressSpace, CurrentBaseAddress, RemainingLength,
-                   CurrentRegion->Type, CurrentRegion->Protect,
-                   NewType, NewProtect);
-      }
-      NewRegion->Length += RemainingLength;
-      CurrentRegion->Length -= RemainingLength;
-   }
+    /*
+     * Split any final region.
+     */
+    if (RemainingLength > 0 && CurrentEntry != RegionListHead)
+    {
+        CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
+                                          RegionListEntry);
+        if (CurrentRegion->Type != NewType ||
+                CurrentRegion->Protect != NewProtect)
+        {
+            AlterFunc(AddressSpace, CurrentBaseAddress, RemainingLength,
+                      CurrentRegion->Type, CurrentRegion->Protect,
+                      NewType, NewProtect);
+        }
+        NewRegion->Length += RemainingLength;
+        CurrentRegion->Length -= RemainingLength;
+    }
 
-   /*
-    * If the region after the new region has the same type then merge them.
-    */
-   if (NewRegion->RegionListEntry.Flink != RegionListHead)
-   {
-      CurrentEntry = NewRegion->RegionListEntry.Flink;
-      CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
-                                        RegionListEntry);
-      if (CurrentRegion->Type == NewRegion->Type &&
-            CurrentRegion->Protect == NewRegion->Protect)
-      {
-         NewRegion->Length += CurrentRegion->Length;
-         RemoveEntryList(&CurrentRegion->RegionListEntry);
-         ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
-      }
-   }
+    /*
+     * If the region after the new region has the same type then merge them.
+     */
+    if (NewRegion->RegionListEntry.Flink != RegionListHead)
+    {
+        CurrentEntry = NewRegion->RegionListEntry.Flink;
+        CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
+                                          RegionListEntry);
+        if (CurrentRegion->Type == NewRegion->Type &&
+                CurrentRegion->Protect == NewRegion->Protect)
+        {
+            NewRegion->Length += CurrentRegion->Length;
+            RemoveEntryList(&CurrentRegion->RegionListEntry);
+            ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
+        }
+    }
 
-   /*
-    * If the region before the new region has the same type then merge them.
-    */
-   if (NewRegion->RegionListEntry.Blink != RegionListHead)
-   {
-      CurrentEntry = NewRegion->RegionListEntry.Blink;
-      CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
-                                        RegionListEntry);
-      if (CurrentRegion->Type == NewRegion->Type &&
-            CurrentRegion->Protect == NewRegion->Protect)
-      {
-         NewRegion->Length += CurrentRegion->Length;
-         RemoveEntryList(&CurrentRegion->RegionListEntry);
-         ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
-      }
-   }
+    /*
+     * If the region before the new region has the same type then merge them.
+     */
+    if (NewRegion->RegionListEntry.Blink != RegionListHead)
+    {
+        CurrentEntry = NewRegion->RegionListEntry.Blink;
+        CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
+                                          RegionListEntry);
+        if (CurrentRegion->Type == NewRegion->Type &&
+                CurrentRegion->Protect == NewRegion->Protect)
+        {
+            NewRegion->Length += CurrentRegion->Length;
+            RemoveEntryList(&CurrentRegion->RegionListEntry);
+            ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
+        }
+    }
 
-   return(STATUS_SUCCESS);
+    return(STATUS_SUCCESS);
 }
 
 VOID
@@ -239,17 +239,17 @@ NTAPI
 MmInitializeRegion(PLIST_ENTRY RegionListHead, SIZE_T Length, ULONG Type,
                    ULONG Protect)
 {
-   PMM_REGION Region;
+    PMM_REGION Region;
 
-   Region = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
-                                  TAG_MM_REGION);
-   if (!Region) return;
+    Region = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
+                                   TAG_MM_REGION);
+    if (!Region) return;
 
-   Region->Type = Type;
-   Region->Protect = Protect;
-   Region->Length = Length;
-   InitializeListHead(RegionListHead);
-   InsertHeadList(RegionListHead, &Region->RegionListEntry);
+    Region->Type = Type;
+    Region->Protect = Protect;
+    Region->Length = Length;
+    InitializeListHead(RegionListHead);
+    InsertHeadList(RegionListHead, &Region->RegionListEntry);
 }
 
 PMM_REGION
@@ -257,29 +257,29 @@ NTAPI
 MmFindRegion(PVOID BaseAddress, PLIST_ENTRY RegionListHead, PVOID Address,
              PVOID* RegionBaseAddress)
 {
-   PLIST_ENTRY current_entry;
-   PMM_REGION current;
-   PVOID StartAddress = BaseAddress;
+    PLIST_ENTRY current_entry;
+    PMM_REGION current;
+    PVOID StartAddress = BaseAddress;
 
-   current_entry = RegionListHead->Flink;
-   while (current_entry != RegionListHead)
-   {
-      current = CONTAINING_RECORD(current_entry, MM_REGION, RegionListEntry);
+    current_entry = RegionListHead->Flink;
+    while (current_entry != RegionListHead)
+    {
+        current = CONTAINING_RECORD(current_entry, MM_REGION, RegionListEntry);
 
-      if (StartAddress <= Address &&
-            ((char*)StartAddress + current->Length) > (char*)Address)
-      {
-         if (RegionBaseAddress != NULL)
-         {
-            *RegionBaseAddress = StartAddress;
-         }
-         return(current);
-      }
+        if (StartAddress <= Address &&
+                ((char*)StartAddress + current->Length) > (char*)Address)
+        {
+            if (RegionBaseAddress != NULL)
+            {
+                *RegionBaseAddress = StartAddress;
+            }
+            return(current);
+        }
 
-      current_entry = current_entry->Flink;
+        current_entry = current_entry->Flink;
 
-      StartAddress = (PVOID)((ULONG_PTR)StartAddress + current->Length);
+        StartAddress = (PVOID)((ULONG_PTR)StartAddress + current->Length);
 
-   }
-   return(NULL);
+    }
+    return(NULL);
 }
index d177f3c..b5034d8 100644 (file)
@@ -42,224 +42,224 @@ INIT_FUNCTION
 NTAPI
 MmInitializeRmapList(VOID)
 {
-   ExInitializeFastMutex(&RmapListLock);
-   ExInitializeNPagedLookasideList (&RmapLookasideList,
-                                    NULL,
-                                    RmapListFree,
-                                    0,
-                                    sizeof(MM_RMAP_ENTRY),
-                                    TAG_RMAP,
-                                    50);
+    ExInitializeFastMutex(&RmapListLock);
+    ExInitializeNPagedLookasideList (&RmapLookasideList,
+                                     NULL,
+                                     RmapListFree,
+                                     0,
+                                     sizeof(MM_RMAP_ENTRY),
+                                     TAG_RMAP,
+                                     50);
 }
 
 NTSTATUS
 NTAPI
 MmPageOutPhysicalAddress(PFN_NUMBER Page)
 {
-   PMM_RMAP_ENTRY entry;
-   PMEMORY_AREA MemoryArea;
-   PMMSUPPORT AddressSpace;
-   ULONG Type;
-   PVOID Address;
-   PEPROCESS Process;
-   ULONGLONG Offset;
-   NTSTATUS Status = STATUS_SUCCESS;
-
-   ExAcquireFastMutex(&RmapListLock);
-   entry = MmGetRmapListHeadPage(Page);
+    PMM_RMAP_ENTRY entry;
+    PMEMORY_AREA MemoryArea;
+    PMMSUPPORT AddressSpace;
+    ULONG Type;
+    PVOID Address;
+    PEPROCESS Process;
+    ULONGLONG Offset;
+    NTSTATUS Status = STATUS_SUCCESS;
+
+    ExAcquireFastMutex(&RmapListLock);
+    entry = MmGetRmapListHeadPage(Page);
 
 #ifdef NEWCC
-   // Special case for NEWCC: we can have a page that's only in a segment
-   // page table
-   if (entry && RMAP_IS_SEGMENT(entry->Address) && entry->Next == NULL)
-   {
-       /* NEWCC does locking itself */
-       ExReleaseFastMutex(&RmapListLock);
-       return MmpPageOutPhysicalAddress(Page);
-   }
+    // Special case for NEWCC: we can have a page that's only in a segment
+    // page table
+    if (entry && RMAP_IS_SEGMENT(entry->Address) && entry->Next == NULL)
+    {
+        /* NEWCC does locking itself */
+        ExReleaseFastMutex(&RmapListLock);
+        return MmpPageOutPhysicalAddress(Page);
+    }
 #endif
 
-   while (entry && RMAP_IS_SEGMENT(entry->Address))
-       entry = entry->Next;
-
-   if (entry == NULL)
-   {
-      ExReleaseFastMutex(&RmapListLock);
-      return(STATUS_UNSUCCESSFUL);
-   }
-
-   Process = entry->Process;
-
-   Address = entry->Address;
-
-   if ((((ULONG_PTR)Address) & 0xFFF) != 0)
-   {
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   if (Address < MmSystemRangeStart)
-   {
-       if (!ExAcquireRundownProtection(&Process->RundownProtect))
-       {
-          ExReleaseFastMutex(&RmapListLock);
-          return STATUS_PROCESS_IS_TERMINATING;
-       }
-
-      Status = ObReferenceObjectByPointer(Process, PROCESS_ALL_ACCESS, NULL, KernelMode);
-      ExReleaseFastMutex(&RmapListLock);
-      if (!NT_SUCCESS(Status))
-      {
-         ExReleaseRundownProtection(&Process->RundownProtect);
-         return Status;
-      }
-      AddressSpace = &Process->Vm;
-   }
-   else
-   {
-      ExReleaseFastMutex(&RmapListLock);
-      AddressSpace = MmGetKernelAddressSpace();
-   }
-
-   MmLockAddressSpace(AddressSpace);
-   MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, Address);
-   if (MemoryArea == NULL || MemoryArea->DeleteInProgress)
-   {
-      MmUnlockAddressSpace(AddressSpace);
-      if (Address < MmSystemRangeStart)
-      {
-         ExReleaseRundownProtection(&Process->RundownProtect);
-         ObDereferenceObject(Process);
-      }
-      return(STATUS_UNSUCCESSFUL);
-   }
-   Type = MemoryArea->Type;
-   if (Type == MEMORY_AREA_SECTION_VIEW)
-   {
-      ULONG_PTR Entry;
-      Offset = MemoryArea->Data.SectionData.ViewOffset.QuadPart +
-               ((ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress);
-
-      MmLockSectionSegment(MemoryArea->Data.SectionData.Segment);
-
-      /*
-       * Get or create a pageop
-       */
-      Entry = MmGetPageEntrySectionSegment(MemoryArea->Data.SectionData.Segment,
-                                           (PLARGE_INTEGER)&Offset);
-      if (Entry && IS_SWAP_FROM_SSE(Entry) && SWAPENTRY_FROM_SSE(Entry) == MM_WAIT_ENTRY)
-      {
-         MmUnlockSectionSegment(MemoryArea->Data.SectionData.Segment);
-         MmUnlockAddressSpace(AddressSpace);
-         if (Address < MmSystemRangeStart)
-         {
+    while (entry && RMAP_IS_SEGMENT(entry->Address))
+        entry = entry->Next;
+
+    if (entry == NULL)
+    {
+        ExReleaseFastMutex(&RmapListLock);
+        return(STATUS_UNSUCCESSFUL);
+    }
+
+    Process = entry->Process;
+
+    Address = entry->Address;
+
+    if ((((ULONG_PTR)Address) & 0xFFF) != 0)
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    if (Address < MmSystemRangeStart)
+    {
+        if (!ExAcquireRundownProtection(&Process->RundownProtect))
+        {
+            ExReleaseFastMutex(&RmapListLock);
+            return STATUS_PROCESS_IS_TERMINATING;
+        }
+
+        Status = ObReferenceObjectByPointer(Process, PROCESS_ALL_ACCESS, NULL, KernelMode);
+        ExReleaseFastMutex(&RmapListLock);
+        if (!NT_SUCCESS(Status))
+        {
+            ExReleaseRundownProtection(&Process->RundownProtect);
+            return Status;
+        }
+        AddressSpace = &Process->Vm;
+    }
+    else
+    {
+        ExReleaseFastMutex(&RmapListLock);
+        AddressSpace = MmGetKernelAddressSpace();
+    }
+
+    MmLockAddressSpace(AddressSpace);
+    MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, Address);
+    if (MemoryArea == NULL || MemoryArea->DeleteInProgress)
+    {
+        MmUnlockAddressSpace(AddressSpace);
+        if (Address < MmSystemRangeStart)
+        {
             ExReleaseRundownProtection(&Process->RundownProtect);
             ObDereferenceObject(Process);
-         }
-         return(STATUS_UNSUCCESSFUL);
-      }
-
-      MmSetPageEntrySectionSegment(MemoryArea->Data.SectionData.Segment, (PLARGE_INTEGER)&Offset, MAKE_SWAP_SSE(MM_WAIT_ENTRY));
-
-      /*
-       * Release locks now we have a page op.
-       */
-      MmUnlockSectionSegment(MemoryArea->Data.SectionData.Segment);
-      MmUnlockAddressSpace(AddressSpace);
-
-      /*
-       * Do the actual page out work.
-       */
-      Status = MmPageOutSectionView(AddressSpace, MemoryArea, Address, Entry);
-   }
-   else if (Type == MEMORY_AREA_CACHE)
-   {
-       /* NEWCC does locking itself */
-       MmUnlockAddressSpace(AddressSpace);
-       Status = MmpPageOutPhysicalAddress(Page);
-   }
-   else
-   {
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   if (Address < MmSystemRangeStart)
-   {
-      ExReleaseRundownProtection(&Process->RundownProtect);
-      ObDereferenceObject(Process);
-   }
-   return(Status);
+        }
+        return(STATUS_UNSUCCESSFUL);
+    }
+    Type = MemoryArea->Type;
+    if (Type == MEMORY_AREA_SECTION_VIEW)
+    {
+        ULONG_PTR Entry;
+        Offset = MemoryArea->Data.SectionData.ViewOffset.QuadPart +
+                 ((ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress);
+
+        MmLockSectionSegment(MemoryArea->Data.SectionData.Segment);
+
+        /*
+         * Get or create a pageop
+         */
+        Entry = MmGetPageEntrySectionSegment(MemoryArea->Data.SectionData.Segment,
+                                             (PLARGE_INTEGER)&Offset);
+        if (Entry && IS_SWAP_FROM_SSE(Entry) && SWAPENTRY_FROM_SSE(Entry) == MM_WAIT_ENTRY)
+        {
+            MmUnlockSectionSegment(MemoryArea->Data.SectionData.Segment);
+            MmUnlockAddressSpace(AddressSpace);
+            if (Address < MmSystemRangeStart)
+            {
+                ExReleaseRundownProtection(&Process->RundownProtect);
+                ObDereferenceObject(Process);
+            }
+            return(STATUS_UNSUCCESSFUL);
+        }
+
+        MmSetPageEntrySectionSegment(MemoryArea->Data.SectionData.Segment, (PLARGE_INTEGER)&Offset, MAKE_SWAP_SSE(MM_WAIT_ENTRY));
+
+        /*
+         * Release locks now we have a page op.
+         */
+        MmUnlockSectionSegment(MemoryArea->Data.SectionData.Segment);
+        MmUnlockAddressSpace(AddressSpace);
+
+        /*
+         * Do the actual page out work.
+         */
+        Status = MmPageOutSectionView(AddressSpace, MemoryArea, Address, Entry);
+    }
+    else if (Type == MEMORY_AREA_CACHE)
+    {
+        /* NEWCC does locking itself */
+        MmUnlockAddressSpace(AddressSpace);
+        Status = MmpPageOutPhysicalAddress(Page);
+    }
+    else
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    if (Address < MmSystemRangeStart)
+    {
+        ExReleaseRundownProtection(&Process->RundownProtect);
+        ObDereferenceObject(Process);
+    }
+    return(Status);
 }
 
 VOID
 NTAPI
 MmSetCleanAllRmaps(PFN_NUMBER Page)
 {
-   PMM_RMAP_ENTRY current_entry;
-
-   ExAcquireFastMutex(&RmapListLock);
-   current_entry = MmGetRmapListHeadPage(Page);
-   if (current_entry == NULL)
-   {
-      DPRINT1("MmIsDirtyRmap: No rmaps.\n");
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   while (current_entry != NULL)
-   {
-      if (!RMAP_IS_SEGMENT(current_entry->Address))
-         MmSetCleanPage(current_entry->Process, current_entry->Address);
-      current_entry = current_entry->Next;
-   }
-   ExReleaseFastMutex(&RmapListLock);
+    PMM_RMAP_ENTRY current_entry;
+
+    ExAcquireFastMutex(&RmapListLock);
+    current_entry = MmGetRmapListHeadPage(Page);
+    if (current_entry == NULL)
+    {
+        DPRINT1("MmIsDirtyRmap: No rmaps.\n");
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    while (current_entry != NULL)
+    {
+        if (!RMAP_IS_SEGMENT(current_entry->Address))
+            MmSetCleanPage(current_entry->Process, current_entry->Address);
+        current_entry = current_entry->Next;
+    }
+    ExReleaseFastMutex(&RmapListLock);
 }
 
 VOID
 NTAPI
 MmSetDirtyAllRmaps(PFN_NUMBER Page)
 {
-   PMM_RMAP_ENTRY current_entry;
-
-   ExAcquireFastMutex(&RmapListLock);
-   current_entry = MmGetRmapListHeadPage(Page);
-   if (current_entry == NULL)
-   {
-      DPRINT1("MmIsDirtyRmap: No rmaps.\n");
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   while (current_entry != NULL)
-   {
-      if (!RMAP_IS_SEGMENT(current_entry->Address))
-         MmSetDirtyPage(current_entry->Process, current_entry->Address);
-      current_entry = current_entry->Next;
-   }
-   ExReleaseFastMutex(&RmapListLock);
+    PMM_RMAP_ENTRY current_entry;
+
+    ExAcquireFastMutex(&RmapListLock);
+    current_entry = MmGetRmapListHeadPage(Page);
+    if (current_entry == NULL)
+    {
+        DPRINT1("MmIsDirtyRmap: No rmaps.\n");
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    while (current_entry != NULL)
+    {
+        if (!RMAP_IS_SEGMENT(current_entry->Address))
+            MmSetDirtyPage(current_entry->Process, current_entry->Address);
+        current_entry = current_entry->Next;
+    }
+    ExReleaseFastMutex(&RmapListLock);
 }
 
 BOOLEAN
 NTAPI
 MmIsDirtyPageRmap(PFN_NUMBER Page)
 {
-   PMM_RMAP_ENTRY current_entry;
-
-   ExAcquireFastMutex(&RmapListLock);
-   current_entry = MmGetRmapListHeadPage(Page);
-   if (current_entry == NULL)
-   {
-      ExReleaseFastMutex(&RmapListLock);
-      return(FALSE);
-   }
-   while (current_entry != NULL)
-   {
-      if (
-          !RMAP_IS_SEGMENT(current_entry->Address) &&
-          MmIsDirtyPage(current_entry->Process, current_entry->Address))
-      {
-         ExReleaseFastMutex(&RmapListLock);
-         return(TRUE);
-      }
-      current_entry = current_entry->Next;
-   }
-   ExReleaseFastMutex(&RmapListLock);
-   return(FALSE);
+    PMM_RMAP_ENTRY current_entry;
+
+    ExAcquireFastMutex(&RmapListLock);
+    current_entry = MmGetRmapListHeadPage(Page);
+    if (current_entry == NULL)
+    {
+        ExReleaseFastMutex(&RmapListLock);
+        return(FALSE);
+    }
+    while (current_entry != NULL)
+    {
+        if (
+            !RMAP_IS_SEGMENT(current_entry->Address) &&
+            MmIsDirtyPage(current_entry->Process, current_entry->Address))
+        {
+            ExReleaseFastMutex(&RmapListLock);
+            return(TRUE);
+        }
+        current_entry = current_entry->Next;
+    }
+    ExReleaseFastMutex(&RmapListLock);
+    return(FALSE);
 }
 
 VOID
@@ -267,75 +267,75 @@ NTAPI
 MmInsertRmap(PFN_NUMBER Page, PEPROCESS Process,
              PVOID Address)
 {
-   PMM_RMAP_ENTRY current_entry;
-   PMM_RMAP_ENTRY new_entry;
-   ULONG PrevSize;
-   if (!RMAP_IS_SEGMENT(Address))
-      Address = (PVOID)PAGE_ROUND_DOWN(Address);
-
-   new_entry = ExAllocateFromNPagedLookasideList(&RmapLookasideList);
-   if (new_entry == NULL)
-   {
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   new_entry->Address = Address;
-   new_entry->Process = (PEPROCESS)Process;
+    PMM_RMAP_ENTRY current_entry;
+    PMM_RMAP_ENTRY new_entry;
+    ULONG PrevSize;
+    if (!RMAP_IS_SEGMENT(Address))
+        Address = (PVOID)PAGE_ROUND_DOWN(Address);
+
+    new_entry = ExAllocateFromNPagedLookasideList(&RmapLookasideList);
+    if (new_entry == NULL)
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    new_entry->Address = Address;
+    new_entry->Process = (PEPROCESS)Process;
 #if DBG
 #ifdef __GNUC__
-   new_entry->Caller = __builtin_return_address(0);
+    new_entry->Caller = __builtin_return_address(0);
 #else
-   new_entry->Caller = _ReturnAddress();
+    new_entry->Caller = _ReturnAddress();
 #endif
 #endif
 
-   if (
-       !RMAP_IS_SEGMENT(Address) &&
-       MmGetPfnForProcess(Process, Address) != Page)
-   {
-      DPRINT1("Insert rmap (%d, 0x%.8X) 0x%.8X which doesn't match physical "
-              "address 0x%.8X\n", Process ? Process->UniqueProcessId : 0,
-              Address,
-              MmGetPfnForProcess(Process, Address) << PAGE_SHIFT,
-              Page << PAGE_SHIFT);
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   ExAcquireFastMutex(&RmapListLock);
-   current_entry = MmGetRmapListHeadPage(Page);
-   new_entry->Next = current_entry;
+    if (
+        !RMAP_IS_SEGMENT(Address) &&
+        MmGetPfnForProcess(Process, Address) != Page)
+    {
+        DPRINT1("Insert rmap (%d, 0x%.8X) 0x%.8X which doesn't match physical "
+                "address 0x%.8X\n", Process ? Process->UniqueProcessId : 0,
+                Address,
+                MmGetPfnForProcess(Process, Address) << PAGE_SHIFT,
+                Page << PAGE_SHIFT);
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    ExAcquireFastMutex(&RmapListLock);
+    current_entry = MmGetRmapListHeadPage(Page);
+    new_entry->Next = current_entry;
 #if DBG
-   while (current_entry)
-   {
-      if (current_entry->Address == new_entry->Address && current_entry->Process == new_entry->Process)
-      {
-          DbgPrint("MmInsertRmap tries to add a second rmap entry for address %p\n    current caller ",
-                   current_entry->Address);
-          DbgPrint("%p", new_entry->Caller);
-          DbgPrint("\n    previous caller ");
-          DbgPrint("%p", current_entry->Caller);
-          DbgPrint("\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-      current_entry = current_entry->Next;
-   }
+    while (current_entry)
+    {
+        if (current_entry->Address == new_entry->Address && current_entry->Process == new_entry->Process)
+        {
+            DbgPrint("MmInsertRmap tries to add a second rmap entry for address %p\n    current caller ",
+                     current_entry->Address);
+            DbgPrint("%p", new_entry->Caller);
+            DbgPrint("\n    previous caller ");
+            DbgPrint("%p", current_entry->Caller);
+            DbgPrint("\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+        current_entry = current_entry->Next;
+    }
 #endif
-   MmSetRmapListHeadPage(Page, new_entry);
-   ExReleaseFastMutex(&RmapListLock);
-   if (!RMAP_IS_SEGMENT(Address))
-   {
-      if (Process == NULL)
-      {
-         Process = PsInitialSystemProcess;
-      }
-      if (Process)
-      {
-         PrevSize = InterlockedExchangeAddUL(&Process->Vm.WorkingSetSize, PAGE_SIZE);
-         if (PrevSize >= Process->Vm.PeakWorkingSetSize)
-         {
-            Process->Vm.PeakWorkingSetSize = PrevSize + PAGE_SIZE;
-         }
-      }
-   }
+    MmSetRmapListHeadPage(Page, new_entry);
+    ExReleaseFastMutex(&RmapListLock);
+    if (!RMAP_IS_SEGMENT(Address))
+    {
+        if (Process == NULL)
+        {
+            Process = PsInitialSystemProcess;
+        }
+        if (Process)
+        {
+            PrevSize = InterlockedExchangeAddUL(&Process->Vm.WorkingSetSize, PAGE_SIZE);
+            if (PrevSize >= Process->Vm.PeakWorkingSetSize)
+            {
+                Process->Vm.PeakWorkingSetSize = PrevSize + PAGE_SIZE;
+            }
+        }
+    }
 }
 
 VOID
@@ -344,47 +344,47 @@ MmDeleteAllRmaps(PFN_NUMBER Page, PVOID Context,
                  VOID (*DeleteMapping)(PVOID Context, PEPROCESS Process,
                                        PVOID Address))
 {
-   PMM_RMAP_ENTRY current_entry;
-   PMM_RMAP_ENTRY previous_entry;
-   PEPROCESS Process;
-
-   ExAcquireFastMutex(&RmapListLock);
-   current_entry = MmGetRmapListHeadPage(Page);
-   if (current_entry == NULL)
-   {
-      DPRINT1("MmDeleteAllRmaps: No rmaps.\n");
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   MmSetRmapListHeadPage(Page, NULL);
-   ExReleaseFastMutex(&RmapListLock);
-
-   while (current_entry != NULL)
-   {
-      previous_entry = current_entry;
-      current_entry = current_entry->Next;
-      if (!RMAP_IS_SEGMENT(previous_entry->Address))
-      {
-         if (DeleteMapping)
-         {
-            DeleteMapping(Context, previous_entry->Process,
-                          previous_entry->Address);
-         }
-         Process = previous_entry->Process;
-         ExFreeToNPagedLookasideList(&RmapLookasideList, previous_entry);
-         if (Process == NULL)
-         {
-            Process = PsInitialSystemProcess;
-         }
-         if (Process)
-         {
-            (void)InterlockedExchangeAddUL(&Process->Vm.WorkingSetSize, -PAGE_SIZE);
-         }
-      }
-      else
-      {
-         ExFreeToNPagedLookasideList(&RmapLookasideList, previous_entry);
-      }
-   }
+    PMM_RMAP_ENTRY current_entry;
+    PMM_RMAP_ENTRY previous_entry;
+    PEPROCESS Process;
+
+    ExAcquireFastMutex(&RmapListLock);
+    current_entry = MmGetRmapListHeadPage(Page);
+    if (current_entry == NULL)
+    {
+        DPRINT1("MmDeleteAllRmaps: No rmaps.\n");
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    MmSetRmapListHeadPage(Page, NULL);
+    ExReleaseFastMutex(&RmapListLock);
+
+    while (current_entry != NULL)
+    {
+        previous_entry = current_entry;
+        current_entry = current_entry->Next;
+        if (!RMAP_IS_SEGMENT(previous_entry->Address))
+        {
+            if (DeleteMapping)
+            {
+                DeleteMapping(Context, previous_entry->Process,
+                              previous_entry->Address);
+            }
+            Process = previous_entry->Process;
+            ExFreeToNPagedLookasideList(&RmapLookasideList, previous_entry);
+            if (Process == NULL)
+            {
+                Process = PsInitialSystemProcess;
+            }
+            if (Process)
+            {
+                (void)InterlockedExchangeAddUL(&Process->Vm.WorkingSetSize, -PAGE_SIZE);
+            }
+        }
+        else
+        {
+            ExFreeToNPagedLookasideList(&RmapLookasideList, previous_entry);
+        }
+    }
 }
 
 VOID
@@ -392,44 +392,44 @@ NTAPI
 MmDeleteRmap(PFN_NUMBER Page, PEPROCESS Process,
              PVOID Address)
 {
-   PMM_RMAP_ENTRY current_entry, previous_entry;
-
-   ExAcquireFastMutex(&RmapListLock);
-   previous_entry = NULL;
-   current_entry = MmGetRmapListHeadPage(Page);
-
-   while (current_entry != NULL)
-   {
-      if (current_entry->Process == (PEPROCESS)Process &&
-            current_entry->Address == Address)
-      {
-         if (previous_entry == NULL)
-         {
-            MmSetRmapListHeadPage(Page, current_entry->Next);
-         }
-         else
-         {
-            previous_entry->Next = current_entry->Next;
-         }
-         ExReleaseFastMutex(&RmapListLock);
-         ExFreeToNPagedLookasideList(&RmapLookasideList, current_entry);
-         if (!RMAP_IS_SEGMENT(Address))
-         {
-            if (Process == NULL)
+    PMM_RMAP_ENTRY current_entry, previous_entry;
+
+    ExAcquireFastMutex(&RmapListLock);
+    previous_entry = NULL;
+    current_entry = MmGetRmapListHeadPage(Page);
+
+    while (current_entry != NULL)
+    {
+        if (current_entry->Process == (PEPROCESS)Process &&
+                current_entry->Address == Address)
+        {
+            if (previous_entry == NULL)
             {
-               Process = PsInitialSystemProcess;
+                MmSetRmapListHeadPage(Page, current_entry->Next);
             }
-            if (Process)
+            else
+            {
+                previous_entry->Next = current_entry->Next;
+            }
+            ExReleaseFastMutex(&RmapListLock);
+            ExFreeToNPagedLookasideList(&RmapLookasideList, current_entry);
+            if (!RMAP_IS_SEGMENT(Address))
             {
-               (void)InterlockedExchangeAddUL(&Process->Vm.WorkingSetSize, -PAGE_SIZE);
+                if (Process == NULL)
+                {
+                    Process = PsInitialSystemProcess;
+                }
+                if (Process)
+                {
+                    (void)InterlockedExchangeAddUL(&Process->Vm.WorkingSetSize, -PAGE_SIZE);
+                }
             }
-         }
-         return;
-      }
-      previous_entry = current_entry;
-      current_entry = current_entry->Next;
-   }
-   KeBugCheck(MEMORY_MANAGEMENT);
+            return;
+        }
+        previous_entry = current_entry;
+        current_entry = current_entry->Next;
+    }
+    KeBugCheck(MEMORY_MANAGEMENT);
 }
 
 /*
@@ -450,27 +450,27 @@ PVOID
 NTAPI
 MmGetSegmentRmap(PFN_NUMBER Page, PULONG RawOffset)
 {
-   PCACHE_SECTION_PAGE_TABLE Result = NULL;
-   PMM_RMAP_ENTRY current_entry;//, previous_entry;
-
-   ExAcquireFastMutex(&RmapListLock);
-   //previous_entry = NULL;
-   current_entry = MmGetRmapListHeadPage(Page);
-   while (current_entry != NULL)
-   {
-      if (RMAP_IS_SEGMENT(current_entry->Address))
-      {
-         Result = (PCACHE_SECTION_PAGE_TABLE)current_entry->Process;
-         *RawOffset = (ULONG_PTR)current_entry->Address & ~RMAP_SEGMENT_MASK;
-         InterlockedIncrementUL(&Result->Segment->ReferenceCount);
-         ExReleaseFastMutex(&RmapListLock);
-         return Result;
-      }
-      //previous_entry = current_entry;
-      current_entry = current_entry->Next;
-   }
-   ExReleaseFastMutex(&RmapListLock);
-   return NULL;
+    PCACHE_SECTION_PAGE_TABLE Result = NULL;
+    PMM_RMAP_ENTRY current_entry;//, previous_entry;
+
+    ExAcquireFastMutex(&RmapListLock);
+    //previous_entry = NULL;
+    current_entry = MmGetRmapListHeadPage(Page);
+    while (current_entry != NULL)
+    {
+        if (RMAP_IS_SEGMENT(current_entry->Address))
+        {
+            Result = (PCACHE_SECTION_PAGE_TABLE)current_entry->Process;
+            *RawOffset = (ULONG_PTR)current_entry->Address & ~RMAP_SEGMENT_MASK;
+            InterlockedIncrementUL(&Result->Segment->ReferenceCount);
+            ExReleaseFastMutex(&RmapListLock);
+            return Result;
+        }
+        //previous_entry = current_entry;
+        current_entry = current_entry->Next;
+    }
+    ExReleaseFastMutex(&RmapListLock);
+    return NULL;
 }
 
 /*
@@ -483,29 +483,29 @@ VOID
 NTAPI
 MmDeleteSectionAssociation(PFN_NUMBER Page)
 {
-   PMM_RMAP_ENTRY current_entry, previous_entry;
-
-   ExAcquireFastMutex(&RmapListLock);
-   previous_entry = NULL;
-   current_entry = MmGetRmapListHeadPage(Page);
-   while (current_entry != NULL)
-   {
-      if (RMAP_IS_SEGMENT(current_entry->Address))
-      {
-         if (previous_entry == NULL)
-         {
-            MmSetRmapListHeadPage(Page, current_entry->Next);
-         }
-         else
-         {
-            previous_entry->Next = current_entry->Next;
-         }
-         ExReleaseFastMutex(&RmapListLock);
-         ExFreeToNPagedLookasideList(&RmapLookasideList, current_entry);
-         return;
-      }
-      previous_entry = current_entry;
-      current_entry = current_entry->Next;
-   }
-   ExReleaseFastMutex(&RmapListLock);
+    PMM_RMAP_ENTRY current_entry, previous_entry;
+
+    ExAcquireFastMutex(&RmapListLock);
+    previous_entry = NULL;
+    current_entry = MmGetRmapListHeadPage(Page);
+    while (current_entry != NULL)
+    {
+        if (RMAP_IS_SEGMENT(current_entry->Address))
+        {
+            if (previous_entry == NULL)
+            {
+                MmSetRmapListHeadPage(Page, current_entry->Next);
+            }
+            else
+            {
+                previous_entry->Next = current_entry->Next;
+            }
+            ExReleaseFastMutex(&RmapListLock);
+            ExFreeToNPagedLookasideList(&RmapLookasideList, current_entry);
+            return;
+        }
+        previous_entry = current_entry;
+        current_entry = current_entry->Next;
+    }
+    ExReleaseFastMutex(&RmapListLock);
 }
index fc7c343..587b35e 100644 (file)
@@ -69,20 +69,20 @@ extern MMSESSION MmSession;
 NTSTATUS
 NTAPI
 MiMapViewInSystemSpace(IN PVOID Section,
-IN PVOID Session,
-OUT PVOID *MappedBase,
-IN OUT PSIZE_T ViewSize);
+                       IN PVOID Session,
+                       OUT PVOID *MappedBase,
+                       IN OUT PSIZE_T ViewSize);
 
 NTSTATUS
 NTAPI
 MmCreateArm3Section(OUT PVOID *SectionObject,
-IN ACCESS_MASK DesiredAccess,
-IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
-IN PLARGE_INTEGER InputMaximumSize,
-IN ULONG SectionPageProtection,
-IN ULONG AllocationAttributes,
-IN HANDLE FileHandle OPTIONAL,
-IN PFILE_OBJECT FileObject OPTIONAL);
+                    IN ACCESS_MASK DesiredAccess,
+                    IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
+                    IN PLARGE_INTEGER InputMaximumSize,
+                    IN ULONG SectionPageProtection,
+                    IN ULONG AllocationAttributes,
+                    IN HANDLE FileHandle OPTIONAL,
+                    IN PFILE_OBJECT FileObject OPTIONAL);
 
 NTSTATUS
 NTAPI
@@ -121,13 +121,13 @@ C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Si
 
 typedef struct
 {
-   PROS_SECTION_OBJECT Section;
-   PMM_SECTION_SEGMENT Segment;
-   LARGE_INTEGER Offset;
-   BOOLEAN WasDirty;
-   BOOLEAN Private;
-   PEPROCESS CallingProcess;
-   ULONG_PTR SectionEntry;
+    PROS_SECTION_OBJECT Section;
+    PMM_SECTION_SEGMENT Segment;
+    LARGE_INTEGER Offset;
+    BOOLEAN WasDirty;
+    BOOLEAN Private;
+    PEPROCESS CallingProcess;
+    ULONG_PTR SectionEntry;
 }
 MM_SECTION_PAGEOUT_CONTEXT;
 
@@ -163,16 +163,18 @@ static ULONG SectionCharacteristicsToProtect[16] =
 
 extern ULONG MmMakeFileAccess [];
 ACCESS_MASK NTAPI MiArm3GetCorrectFileAccessMask(IN ACCESS_MASK SectionPageProtection);
-static GENERIC_MAPPING MmpSectionMapping = {
-         STANDARD_RIGHTS_READ | SECTION_MAP_READ | SECTION_QUERY,
-         STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
-         STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
-         SECTION_ALL_ACCESS};
+static GENERIC_MAPPING MmpSectionMapping =
+{
+    STANDARD_RIGHTS_READ | SECTION_MAP_READ | SECTION_QUERY,
+    STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
+    STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
+    SECTION_ALL_ACCESS
+};
 
 static const INFORMATION_CLASS_INFO ExSectionInfoClass[] =
 {
-  ICI_SQ_SAME( sizeof(SECTION_BASIC_INFORMATION), sizeof(ULONG), ICIF_QUERY ), /* SectionBasicInformation */
-  ICI_SQ_SAME( sizeof(SECTION_IMAGE_INFORMATION), sizeof(ULONG), ICIF_QUERY ), /* SectionImageInformation */
+    ICI_SQ_SAME( sizeof(SECTION_BASIC_INFORMATION), sizeof(ULONG), ICIF_QUERY ), /* SectionBasicInformation */
+    ICI_SQ_SAME( sizeof(SECTION_IMAGE_INFORMATION), sizeof(ULONG), ICIF_QUERY ), /* SectionImageInformation */
 };
 
 /* FUNCTIONS *****************************************************************/
@@ -266,7 +268,7 @@ NTSTATUS NTAPI PeFmtCreateSection(IN CONST VOID * FileHeader,
      * need to read the header from the file
      */
     if(FileHeaderSize < cbFileHeaderOffsetSize ||
-       (UINT_PTR)pinhNtHeader % TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) != 0)
+            (UINT_PTR)pinhNtHeader % TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) != 0)
     {
         ULONG cbNtHeaderSize;
         ULONG cbReadSize;
@@ -358,16 +360,16 @@ l_ReadHeaderFromFile:
 
     switch(piohOptHeader->Magic)
     {
-        case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
-        case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
-            break;
+    case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
+    case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
+        break;
 
-        default:
-            DIE(("Unrecognized optional header, Magic is %X\n", piohOptHeader->Magic));
+    default:
+        DIE(("Unrecognized optional header, Magic is %X\n", piohOptHeader->Magic));
     }
 
     if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SectionAlignment) &&
-        RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, FileAlignment))
+            RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, FileAlignment))
     {
         /* See [1], section 3.4.2 */
         if(piohOptHeader->SectionAlignment < PAGE_SIZE)
@@ -395,153 +397,153 @@ l_ReadHeaderFromFile:
 
     switch(piohOptHeader->Magic)
     {
-        /* PE32 */
-        case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
-        {
-            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, ImageBase))
-                ImageBase = piohOptHeader->ImageBase;
+    /* PE32 */
+    case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
+    {
+        if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, ImageBase))
+            ImageBase = piohOptHeader->ImageBase;
 
-            if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfImage))
-                ImageSectionObject->ImageInformation.ImageFileSize = piohOptHeader->SizeOfImage;
+        if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfImage))
+            ImageSectionObject->ImageInformation.ImageFileSize = piohOptHeader->SizeOfImage;
 
-            if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackReserve))
-                ImageSectionObject->ImageInformation.MaximumStackSize = piohOptHeader->SizeOfStackReserve;
+        if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackReserve))
+            ImageSectionObject->ImageInformation.MaximumStackSize = piohOptHeader->SizeOfStackReserve;
 
-            if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackCommit))
-                ImageSectionObject->ImageInformation.CommittedStackSize = piohOptHeader->SizeOfStackCommit;
+        if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackCommit))
+            ImageSectionObject->ImageInformation.CommittedStackSize = piohOptHeader->SizeOfStackCommit;
 
-            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Subsystem))
-            {
-                ImageSectionObject->ImageInformation.SubSystemType = piohOptHeader->Subsystem;
+        if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Subsystem))
+        {
+            ImageSectionObject->ImageInformation.SubSystemType = piohOptHeader->Subsystem;
 
-                if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
+            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
                     RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MajorSubsystemVersion))
-                {
-                    ImageSectionObject->ImageInformation.SubSystemMinorVersion = piohOptHeader->MinorSubsystemVersion;
-                    ImageSectionObject->ImageInformation.SubSystemMajorVersion = piohOptHeader->MajorSubsystemVersion;
-                }
+            {
+                ImageSectionObject->ImageInformation.SubSystemMinorVersion = piohOptHeader->MinorSubsystemVersion;
+                ImageSectionObject->ImageInformation.SubSystemMajorVersion = piohOptHeader->MajorSubsystemVersion;
             }
+        }
 
-            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
-            {
-                ImageSectionObject->ImageInformation.TransferAddress = (PVOID) (ImageBase +
+        if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+        {
+            ImageSectionObject->ImageInformation.TransferAddress = (PVOID) (ImageBase +
                     piohOptHeader->AddressOfEntryPoint);
-            }
+        }
 
-            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfCode))
-                ImageSectionObject->ImageInformation.ImageContainsCode = piohOptHeader->SizeOfCode != 0;
-            else
-                ImageSectionObject->ImageInformation.ImageContainsCode = TRUE;
+        if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfCode))
+            ImageSectionObject->ImageInformation.ImageContainsCode = piohOptHeader->SizeOfCode != 0;
+        else
+            ImageSectionObject->ImageInformation.ImageContainsCode = TRUE;
 
-            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+        if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+        {
+            if (piohOptHeader->AddressOfEntryPoint == 0)
             {
-                if (piohOptHeader->AddressOfEntryPoint == 0)
-                {
-                    ImageSectionObject->ImageInformation.ImageContainsCode = FALSE;
-                }
+                ImageSectionObject->ImageInformation.ImageContainsCode = FALSE;
             }
+        }
 
-            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, LoaderFlags))
-                ImageSectionObject->ImageInformation.LoaderFlags = piohOptHeader->LoaderFlags;
-
-            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, DllCharacteristics))
-            {
-                ImageSectionObject->ImageInformation.DllCharacteristics = piohOptHeader->DllCharacteristics;
+        if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, LoaderFlags))
+            ImageSectionObject->ImageInformation.LoaderFlags = piohOptHeader->LoaderFlags;
 
-                /*
-                 * Since we don't really implement SxS yet and LD doesn't supoprt /ALLOWISOLATION:NO, hard-code
-                 * this flag here, which will prevent the loader and other code from doing any .manifest or SxS
-                 * magic to any binary.
-                 *
-                 * This will break applications that depend on SxS when running with real Windows Kernel32/SxS/etc
-                 * but honestly that's not tested. It will also break them when running no ReactOS once we implement
-                 * the SxS support -- at which point, duh, this should be removed.
-                 *
-                 * But right now, any app depending on SxS is already broken anyway, so this flag only helps.
-                 */
-                ImageSectionObject->ImageInformation.DllCharacteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
-            }
+        if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, DllCharacteristics))
+        {
+            ImageSectionObject->ImageInformation.DllCharacteristics = piohOptHeader->DllCharacteristics;
 
-            break;
+            /*
+             * Since we don't really implement SxS yet and LD doesn't supoprt /ALLOWISOLATION:NO, hard-code
+             * this flag here, which will prevent the loader and other code from doing any .manifest or SxS
+             * magic to any binary.
+             *
+             * This will break applications that depend on SxS when running with real Windows Kernel32/SxS/etc
+             * but honestly that's not tested. It will also break them when running no ReactOS once we implement
+             * the SxS support -- at which point, duh, this should be removed.
+             *
+             * But right now, any app depending on SxS is already broken anyway, so this flag only helps.
+             */
+            ImageSectionObject->ImageInformation.DllCharacteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
         }
+
+        break;
+    }
 #ifdef _WIN64
-        /* PE64 */
-        case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
-        {
-            const IMAGE_OPTIONAL_HEADER64 * pioh64OptHeader;
+    /* PE64 */
+    case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
+    {
+        const IMAGE_OPTIONAL_HEADER64 * pioh64OptHeader;
 
-            pioh64OptHeader = (const IMAGE_OPTIONAL_HEADER64 *)piohOptHeader;
+        pioh64OptHeader = (const IMAGE_OPTIONAL_HEADER64 *)piohOptHeader;
 
-            if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, ImageBase))
-            {
-                ImageBase = pioh64OptHeader->ImageBase;
-                if(pioh64OptHeader->ImageBase > MAXULONG_PTR)
-                    DIE(("ImageBase exceeds the address space\n"));
-            }
+        if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, ImageBase))
+        {
+            ImageBase = pioh64OptHeader->ImageBase;
+            if(pioh64OptHeader->ImageBase > MAXULONG_PTR)
+                DIE(("ImageBase exceeds the address space\n"));
+        }
 
-            if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfImage))
-            {
-                if(pioh64OptHeader->SizeOfImage > MAXULONG_PTR)
-                    DIE(("SizeOfImage exceeds the address space\n"));
+        if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfImage))
+        {
+            if(pioh64OptHeader->SizeOfImage > MAXULONG_PTR)
+                DIE(("SizeOfImage exceeds the address space\n"));
 
-                ImageSectionObject->ImageInformation.ImageFileSize = pioh64OptHeader->SizeOfImage;
-            }
+            ImageSectionObject->ImageInformation.ImageFileSize = pioh64OptHeader->SizeOfImage;
+        }
 
-            if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackReserve))
-            {
-                if(pioh64OptHeader->SizeOfStackReserve > MAXULONG_PTR)
-                    DIE(("SizeOfStackReserve exceeds the address space\n"));
+        if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackReserve))
+        {
+            if(pioh64OptHeader->SizeOfStackReserve > MAXULONG_PTR)
+                DIE(("SizeOfStackReserve exceeds the address space\n"));
 
-                ImageSectionObject->ImageInformation.MaximumStackSize = (ULONG_PTR) pioh64OptHeader->SizeOfStackReserve;
-            }
+            ImageSectionObject->ImageInformation.MaximumStackSize = (ULONG_PTR) pioh64OptHeader->SizeOfStackReserve;
+        }
 
-            if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackCommit))
-            {
-                if(pioh64OptHeader->SizeOfStackCommit > MAXULONG_PTR)
-                    DIE(("SizeOfStackCommit exceeds the address space\n"));
+        if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackCommit))
+        {
+            if(pioh64OptHeader->SizeOfStackCommit > MAXULONG_PTR)
+                DIE(("SizeOfStackCommit exceeds the address space\n"));
 
-                ImageSectionObject->ImageInformation.CommittedStackSize = (ULONG_PTR) pioh64OptHeader->SizeOfStackCommit;
-            }
+            ImageSectionObject->ImageInformation.CommittedStackSize = (ULONG_PTR) pioh64OptHeader->SizeOfStackCommit;
+        }
 
-            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, Subsystem))
-            {
-                ImageSectionObject->ImageInformation.SubSystemType = pioh64OptHeader->Subsystem;
+        if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, Subsystem))
+        {
+            ImageSectionObject->ImageInformation.SubSystemType = pioh64OptHeader->Subsystem;
 
-                if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
+            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
                     RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, MajorSubsystemVersion))
-                {
-                    ImageSectionObject->ImageInformation.SubSystemMinorVersion = pioh64OptHeader->MinorSubsystemVersion;
-                    ImageSectionObject->ImageInformation.SubSystemMajorVersion = pioh64OptHeader->MajorSubsystemVersion;
-                }
+            {
+                ImageSectionObject->ImageInformation.SubSystemMinorVersion = pioh64OptHeader->MinorSubsystemVersion;
+                ImageSectionObject->ImageInformation.SubSystemMajorVersion = pioh64OptHeader->MajorSubsystemVersion;
             }
+        }
 
-            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, AddressOfEntryPoint))
-            {
-                ImageSectionObject->ImageInformation.TransferAddress = (PVOID) (ImageBase +
+        if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+        {
+            ImageSectionObject->ImageInformation.TransferAddress = (PVOID) (ImageBase +
                     pioh64OptHeader->AddressOfEntryPoint);
-            }
+        }
 
-            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfCode))
-                ImageSectionObject->ImageInformation.ImageContainsCode = pioh64OptHeader->SizeOfCode != 0;
-            else
-                ImageSectionObject->ImageInformation.ImageContainsCode = TRUE;
+        if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfCode))
+            ImageSectionObject->ImageInformation.ImageContainsCode = pioh64OptHeader->SizeOfCode != 0;
+        else
+            ImageSectionObject->ImageInformation.ImageContainsCode = TRUE;
 
-            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+        if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+        {
+            if (pioh64OptHeader->AddressOfEntryPoint == 0)
             {
-                if (pioh64OptHeader->AddressOfEntryPoint == 0)
-                {
-                    ImageSectionObject->ImageInformation.ImageContainsCode = FALSE;
-                }
+                ImageSectionObject->ImageInformation.ImageContainsCode = FALSE;
             }
+        }
 
-            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, LoaderFlags))
-                ImageSectionObject->ImageInformation.LoaderFlags = pioh64OptHeader->LoaderFlags;
+        if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, LoaderFlags))
+            ImageSectionObject->ImageInformation.LoaderFlags = pioh64OptHeader->LoaderFlags;
 
-            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, DllCharacteristics))
-                ImageSectionObject->ImageInformation.DllCharacteristics = pioh64OptHeader->DllCharacteristics;
+        if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, DllCharacteristics))
+            ImageSectionObject->ImageInformation.DllCharacteristics = pioh64OptHeader->DllCharacteristics;
 
-            break;
-        }
+        break;
+    }
 #endif // _WIN64
     }
 
@@ -623,7 +625,7 @@ l_ReadHeaderFromFile:
      * read the headers from the file
      */
     if(FileHeaderSize < cbSectionHeadersOffsetSize ||
-       (UINT_PTR)pishSectionHeaders % TYPE_ALIGNMENT(IMAGE_SECTION_HEADER) != 0)
+            (UINT_PTR)pishSectionHeaders % TYPE_ALIGNMENT(IMAGE_SECTION_HEADER) != 0)
     {
         PVOID pData;
         ULONG cbReadSize;
@@ -793,53 +795,53 @@ NTSTATUS
 MmspWaitForFileLock(PFILE_OBJECT File)
 {
     return STATUS_SUCCESS;
-   //return KeWaitForSingleObject(&File->Lock, 0, KernelMode, FALSE, NULL);
+    //return KeWaitForSingleObject(&File->Lock, 0, KernelMode, FALSE, NULL);
 }
 
 VOID
 NTAPI
 MmFreeSectionSegments(PFILE_OBJECT FileObject)
 {
-   if (FileObject->SectionObjectPointer->ImageSectionObject != NULL)
-   {
-      PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
-      PMM_SECTION_SEGMENT SectionSegments;
-      ULONG NrSegments;
-      ULONG i;
-
-      ImageSectionObject = (PMM_IMAGE_SECTION_OBJECT)FileObject->SectionObjectPointer->ImageSectionObject;
-      NrSegments = ImageSectionObject->NrSegments;
-      SectionSegments = ImageSectionObject->Segments;
-      for (i = 0; i < NrSegments; i++)
-      {
-         if (SectionSegments[i].ReferenceCount != 0)
-         {
-            DPRINT1("Image segment %lu still referenced (was %lu)\n", i,
-                    SectionSegments[i].ReferenceCount);
+    if (FileObject->SectionObjectPointer->ImageSectionObject != NULL)
+    {
+        PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
+        PMM_SECTION_SEGMENT SectionSegments;
+        ULONG NrSegments;
+        ULONG i;
+
+        ImageSectionObject = (PMM_IMAGE_SECTION_OBJECT)FileObject->SectionObjectPointer->ImageSectionObject;
+        NrSegments = ImageSectionObject->NrSegments;
+        SectionSegments = ImageSectionObject->Segments;
+        for (i = 0; i < NrSegments; i++)
+        {
+            if (SectionSegments[i].ReferenceCount != 0)
+            {
+                DPRINT1("Image segment %lu still referenced (was %lu)\n", i,
+                        SectionSegments[i].ReferenceCount);
+                KeBugCheck(MEMORY_MANAGEMENT);
+            }
+            MmFreePageTablesSectionSegment(&SectionSegments[i], NULL);
+        }
+        ExFreePool(ImageSectionObject->Segments);
+        ExFreePool(ImageSectionObject);
+        FileObject->SectionObjectPointer->ImageSectionObject = NULL;
+    }
+    if (FileObject->SectionObjectPointer->DataSectionObject != NULL)
+    {
+        PMM_SECTION_SEGMENT Segment;
+
+        Segment = (PMM_SECTION_SEGMENT)FileObject->SectionObjectPointer->
+                  DataSectionObject;
+
+        if (Segment->ReferenceCount != 0)
+        {
+            DPRINT1("Data segment still referenced\n");
             KeBugCheck(MEMORY_MANAGEMENT);
-         }
-         MmFreePageTablesSectionSegment(&SectionSegments[i], NULL);
-      }
-      ExFreePool(ImageSectionObject->Segments);
-      ExFreePool(ImageSectionObject);
-      FileObject->SectionObjectPointer->ImageSectionObject = NULL;
-   }
-   if (FileObject->SectionObjectPointer->DataSectionObject != NULL)
-   {
-      PMM_SECTION_SEGMENT Segment;
-
-      Segment = (PMM_SECTION_SEGMENT)FileObject->SectionObjectPointer->
-                DataSectionObject;
-
-      if (Segment->ReferenceCount != 0)
-      {
-         DPRINT1("Data segment still referenced\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-      MmFreePageTablesSectionSegment(Segment, NULL);
-      ExFreePool(Segment);
-      FileObject->SectionObjectPointer->DataSectionObject = NULL;
-   }
+        }
+        MmFreePageTablesSectionSegment(Segment, NULL);
+        ExFreePool(Segment);
+        FileObject->SectionObjectPointer->DataSectionObject = NULL;
+    }
 }
 
 VOID
@@ -847,25 +849,25 @@ NTAPI
 MmSharePageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
                                PLARGE_INTEGER Offset)
 {
-   ULONG_PTR Entry;
-
-   Entry = MmGetPageEntrySectionSegment(Segment, Offset);
-   if (Entry == 0)
-   {
-      DPRINT1("Entry == 0 for MmSharePageEntrySectionSegment\n");
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   if (SHARE_COUNT_FROM_SSE(Entry) == MAX_SHARE_COUNT)
-   {
-      DPRINT1("Maximum share count reached\n");
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   if (IS_SWAP_FROM_SSE(Entry))
-   {
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   Entry = MAKE_SSE(PAGE_FROM_SSE(Entry), SHARE_COUNT_FROM_SSE(Entry) + 1);
-   MmSetPageEntrySectionSegment(Segment, Offset, Entry);
+    ULONG_PTR Entry;
+
+    Entry = MmGetPageEntrySectionSegment(Segment, Offset);
+    if (Entry == 0)
+    {
+        DPRINT1("Entry == 0 for MmSharePageEntrySectionSegment\n");
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    if (SHARE_COUNT_FROM_SSE(Entry) == MAX_SHARE_COUNT)
+    {
+        DPRINT1("Maximum share count reached\n");
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    if (IS_SWAP_FROM_SSE(Entry))
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    Entry = MAKE_SSE(PAGE_FROM_SSE(Entry), SHARE_COUNT_FROM_SSE(Entry) + 1);
+    MmSetPageEntrySectionSegment(Segment, Offset, Entry);
 }
 
 BOOLEAN
@@ -877,163 +879,163 @@ MmUnsharePageEntrySectionSegment(PROS_SECTION_OBJECT Section,
                                  BOOLEAN PageOut,
                                  ULONG_PTR *InEntry)
 {
-   ULONG_PTR Entry = InEntry ? *InEntry : MmGetPageEntrySectionSegment(Segment, Offset);
-   BOOLEAN IsDirectMapped = FALSE;
-
-   if (Entry == 0)
-   {
-      DPRINT1("Entry == 0 for MmUnsharePageEntrySectionSegment\n");
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   if (SHARE_COUNT_FROM_SSE(Entry) == 0)
-   {
-       DPRINT1("Zero share count for unshare (Seg %p Offset %x Page %x)\n", Segment, Offset->LowPart, PFN_FROM_SSE(Entry));
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   if (IS_SWAP_FROM_SSE(Entry))
-   {
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   Entry = MAKE_SSE(PAGE_FROM_SSE(Entry), SHARE_COUNT_FROM_SSE(Entry) - 1);
-   /*
-    * If we reducing the share count of this entry to zero then set the entry
-    * to zero and tell the cache the page is no longer mapped.
-    */
-   if (SHARE_COUNT_FROM_SSE(Entry) == 0)
-   {
-      PFILE_OBJECT FileObject;
+    ULONG_PTR Entry = InEntry ? *InEntry : MmGetPageEntrySectionSegment(Segment, Offset);
+    BOOLEAN IsDirectMapped = FALSE;
+
+    if (Entry == 0)
+    {
+        DPRINT1("Entry == 0 for MmUnsharePageEntrySectionSegment\n");
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    if (SHARE_COUNT_FROM_SSE(Entry) == 0)
+    {
+        DPRINT1("Zero share count for unshare (Seg %p Offset %x Page %x)\n", Segment, Offset->LowPart, PFN_FROM_SSE(Entry));
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    if (IS_SWAP_FROM_SSE(Entry))
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    Entry = MAKE_SSE(PAGE_FROM_SSE(Entry), SHARE_COUNT_FROM_SSE(Entry) - 1);
+    /*
+     * If we reducing the share count of this entry to zero then set the entry
+     * to zero and tell the cache the page is no longer mapped.
+     */
+    if (SHARE_COUNT_FROM_SSE(Entry) == 0)
+    {
+        PFILE_OBJECT FileObject;
 #ifndef NEWCC
-      PROS_SHARED_CACHE_MAP SharedCacheMap;
+        PROS_SHARED_CACHE_MAP SharedCacheMap;
 #endif
-      SWAPENTRY SavedSwapEntry;
-      PFN_NUMBER Page;
-      BOOLEAN IsImageSection;
-      LARGE_INTEGER FileOffset;
+        SWAPENTRY SavedSwapEntry;
+        PFN_NUMBER Page;
+        BOOLEAN IsImageSection;
+        LARGE_INTEGER FileOffset;
 
-      FileOffset.QuadPart = Offset->QuadPart + Segment->Image.FileOffset;
+        FileOffset.QuadPart = Offset->QuadPart + Segment->Image.FileOffset;
 
-      IsImageSection = Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
+        IsImageSection = Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
 
-      Page = PFN_FROM_SSE(Entry);
-      FileObject = Section->FileObject;
-      if (FileObject != NULL &&
-            !(Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
-      {
+        Page = PFN_FROM_SSE(Entry);
+        FileObject = Section->FileObject;
+        if (FileObject != NULL &&
+                !(Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
+        {
 
 #ifndef NEWCC
-         if ((FileOffset.QuadPart % PAGE_SIZE) == 0 &&
-               (Offset->QuadPart + PAGE_SIZE <= Segment->RawLength.QuadPart || !IsImageSection))
-         {
-            NTSTATUS Status;
-            SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
-            IsDirectMapped = TRUE;
+            if ((FileOffset.QuadPart % PAGE_SIZE) == 0 &&
+                    (Offset->QuadPart + PAGE_SIZE <= Segment->RawLength.QuadPart || !IsImageSection))
+            {
+                NTSTATUS Status;
+                SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
+                IsDirectMapped = TRUE;
 #ifndef NEWCC
-            Status = CcRosUnmapVacb(SharedCacheMap, FileOffset.QuadPart, Dirty);
+                Status = CcRosUnmapVacb(SharedCacheMap, FileOffset.QuadPart, Dirty);
 #else
-            Status = STATUS_SUCCESS;
+                Status = STATUS_SUCCESS;
 #endif
-            if (!NT_SUCCESS(Status))
-            {
-               DPRINT1("CcRosUnmapVacb failed, status = %x\n", Status);
-               KeBugCheck(MEMORY_MANAGEMENT);
+                if (!NT_SUCCESS(Status))
+                {
+                    DPRINT1("CcRosUnmapVacb failed, status = %x\n", Status);
+                    KeBugCheck(MEMORY_MANAGEMENT);
+                }
             }
-         }
 #endif
-      }
-
-      SavedSwapEntry = MmGetSavedSwapEntryPage(Page);
-      if (SavedSwapEntry == 0)
-      {
-         if (!PageOut &&
-               ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
-                (Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)))
-         {
-            /*
-             * FIXME:
-             *   Try to page out this page and set the swap entry
-             *   within the section segment. There exist no rmap entry
-             *   for this page. The pager thread can't page out a
-             *   page without a rmap entry.
-             */
-            MmSetPageEntrySectionSegment(Segment, Offset, Entry);
-            if (InEntry) *InEntry = Entry;
-            MiSetPageEvent(NULL, NULL);
-         }
-         else
-         {
-            MmSetPageEntrySectionSegment(Segment, Offset, 0);
-            if (InEntry) *InEntry = 0;
-            MiSetPageEvent(NULL, NULL);
-            if (!IsDirectMapped)
+        }
+
+        SavedSwapEntry = MmGetSavedSwapEntryPage(Page);
+        if (SavedSwapEntry == 0)
+        {
+            if (!PageOut &&
+                    ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
+                     (Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)))
             {
-               MmReleasePageMemoryConsumer(MC_USER, Page);
+                /*
+                 * FIXME:
+                 *   Try to page out this page and set the swap entry
+                 *   within the section segment. There exist no rmap entry
+                 *   for this page. The pager thread can't page out a
+                 *   page without a rmap entry.
+                 */
+                MmSetPageEntrySectionSegment(Segment, Offset, Entry);
+                if (InEntry) *InEntry = Entry;
+                MiSetPageEvent(NULL, NULL);
             }
-         }
-      }
-      else
-      {
-         if ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
-               (Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
-         {
-            if (!PageOut)
+            else
             {
-               if (Dirty)
-               {
-                  /*
-                   * FIXME:
-                   *   We hold all locks. Nobody can do something with the current
-                   *   process and the current segment (also not within an other process).
-                   */
-                  NTSTATUS Status;
-                  Status = MmWriteToSwapPage(SavedSwapEntry, Page);
-                  if (!NT_SUCCESS(Status))
-                  {
-                     DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", Status);
-                      KeBugCheck(MEMORY_MANAGEMENT);
-                  }
-               }
-               MmSetPageEntrySectionSegment(Segment, Offset, MAKE_SWAP_SSE(SavedSwapEntry));
-               if (InEntry) *InEntry = MAKE_SWAP_SSE(SavedSwapEntry);
-               MmSetSavedSwapEntryPage(Page, 0);
-               MiSetPageEvent(NULL, NULL);
+                MmSetPageEntrySectionSegment(Segment, Offset, 0);
+                if (InEntry) *InEntry = 0;
+                MiSetPageEvent(NULL, NULL);
+                if (!IsDirectMapped)
+                {
+                    MmReleasePageMemoryConsumer(MC_USER, Page);
+                }
             }
-            MmReleasePageMemoryConsumer(MC_USER, Page);
-         }
-         else
-         {
-            DPRINT1("Found a swapentry for a non private page in an image or data file sgment\n");
-            KeBugCheck(MEMORY_MANAGEMENT);
-         }
-      }
-   }
-   else
-   {
-      if (InEntry)
-          *InEntry = Entry;
-      else
-          MmSetPageEntrySectionSegment(Segment, Offset, Entry);
-   }
-   return(SHARE_COUNT_FROM_SSE(Entry) > 0);
+        }
+        else
+        {
+            if ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
+                    (Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
+            {
+                if (!PageOut)
+                {
+                    if (Dirty)
+                    {
+                        /*
+                         * FIXME:
+                         *   We hold all locks. Nobody can do something with the current
+                         *   process and the current segment (also not within an other process).
+                         */
+                        NTSTATUS Status;
+                        Status = MmWriteToSwapPage(SavedSwapEntry, Page);
+                        if (!NT_SUCCESS(Status))
+                        {
+                            DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", Status);
+                            KeBugCheck(MEMORY_MANAGEMENT);
+                        }
+                    }
+                    MmSetPageEntrySectionSegment(Segment, Offset, MAKE_SWAP_SSE(SavedSwapEntry));
+                    if (InEntry) *InEntry = MAKE_SWAP_SSE(SavedSwapEntry);
+                    MmSetSavedSwapEntryPage(Page, 0);
+                    MiSetPageEvent(NULL, NULL);
+                }
+                MmReleasePageMemoryConsumer(MC_USER, Page);
+            }
+            else
+            {
+                DPRINT1("Found a swapentry for a non private page in an image or data file sgment\n");
+                KeBugCheck(MEMORY_MANAGEMENT);
+            }
+        }
+    }
+    else
+    {
+        if (InEntry)
+            *InEntry = Entry;
+        else
+            MmSetPageEntrySectionSegment(Segment, Offset, Entry);
+    }
+    return(SHARE_COUNT_FROM_SSE(Entry) > 0);
 }
 
 BOOLEAN MiIsPageFromCache(PMEMORY_AREA MemoryArea,
-                       LONGLONG SegOffset)
+                          LONGLONG SegOffset)
 {
 #ifndef NEWCC
-   if (!(MemoryArea->Data.SectionData.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
-   {
-      PROS_SHARED_CACHE_MAP SharedCacheMap;
-      PROS_VACB Vacb;
-      SharedCacheMap = MemoryArea->Data.SectionData.Section->FileObject->SectionObjectPointer->SharedCacheMap;
-      Vacb = CcRosLookupVacb(SharedCacheMap, SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset);
-      if (Vacb)
-      {
-         CcRosReleaseVacb(SharedCacheMap, Vacb, Vacb->Valid, FALSE, TRUE);
-         return TRUE;
-      }
-   }
+    if (!(MemoryArea->Data.SectionData.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
+    {
+        PROS_SHARED_CACHE_MAP SharedCacheMap;
+        PROS_VACB Vacb;
+        SharedCacheMap = MemoryArea->Data.SectionData.Section->FileObject->SectionObjectPointer->SharedCacheMap;
+        Vacb = CcRosLookupVacb(SharedCacheMap, SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset);
+        if (Vacb)
+        {
+            CcRosReleaseVacb(SharedCacheMap, Vacb, Vacb->Valid, FALSE, TRUE);
+            return TRUE;
+        }
+    }
 #endif
-   return FALSE;
+    return FALSE;
 }
 
 NTSTATUS
@@ -1073,149 +1075,108 @@ MiReadPage(PMEMORY_AREA MemoryArea,
  *       Page - Variable that receives a page contains the read data.
  */
 {
-   LONGLONG BaseOffset;
-   LONGLONG FileOffset;
-   PVOID BaseAddress;
-   BOOLEAN UptoDate;
-   PROS_VACB Vacb;
-   PFILE_OBJECT FileObject;
-   NTSTATUS Status;
-   LONGLONG RawLength;
-   PROS_SHARED_CACHE_MAP SharedCacheMap;
-   BOOLEAN IsImageSection;
-   LONGLONG Length;
-
-   FileObject = MemoryArea->Data.SectionData.Section->FileObject;
-   SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
-   RawLength = MemoryArea->Data.SectionData.Segment->RawLength.QuadPart;
-   FileOffset = SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset;
-   IsImageSection = MemoryArea->Data.SectionData.Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
-
-   ASSERT(SharedCacheMap);
-
-   DPRINT("%S %I64x\n", FileObject->FileName.Buffer, FileOffset);
-
-   /*
-    * If the file system is letting us go directly to the cache and the
-    * memory area was mapped at an offset in the file which is page aligned
-    * then get the related VACB.
-    */
-   if (((FileOffset % PAGE_SIZE) == 0) &&
-       ((SegOffset + PAGE_SIZE <= RawLength) || !IsImageSection) &&
-       !(MemoryArea->Data.SectionData.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
-   {
-
-      /*
-       * Get the related VACB; we use a lower level interface than
-       * filesystems do because it is safe for us to use an offset with an
-       * alignment less than the file system block size.
-       */
-      Status = CcRosGetVacb(SharedCacheMap,
-                            FileOffset,
-                            &BaseOffset,
-                            &BaseAddress,
-                            &UptoDate,
-                            &Vacb);
-      if (!NT_SUCCESS(Status))
-      {
-         return(Status);
-      }
-      if (!UptoDate)
-      {
-         /*
-          * If the VACB isn't up to date then call the file
-          * system to read in the data.
-          */
-         Status = CcReadVirtualAddress(Vacb);
-         if (!NT_SUCCESS(Status))
-         {
-            CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
-            return Status;
-         }
-      }
-
-      /* Probe the page, since it's PDE might not be synced */
-      (void)*((volatile char*)BaseAddress + FileOffset - BaseOffset);
-
-      /*
-       * Retrieve the page from the view that we actually want.
-       */
-      (*Page) = MmGetPhysicalAddress((char*)BaseAddress +
-                                     FileOffset - BaseOffset).LowPart >> PAGE_SHIFT;
-
-      CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, TRUE);
-   }
-   else
-   {
-      PEPROCESS Process;
-      KIRQL Irql;
-      PVOID PageAddr;
-      LONGLONG VacbOffset;
-
-      /*
-       * Allocate a page, this is rather complicated by the possibility
-       * we might have to move other things out of memory
-       */
-      MI_SET_USAGE(MI_USAGE_SECTION);
-      MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName);
-      Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, Page);
-      if (!NT_SUCCESS(Status))
-      {
-         return(Status);
-      }
-      Status = CcRosGetVacb(SharedCacheMap,
-                            FileOffset,
-                            &BaseOffset,
-                            &BaseAddress,
-                            &UptoDate,
-                            &Vacb);
-      if (!NT_SUCCESS(Status))
-      {
-         return(Status);
-      }
-      if (!UptoDate)
-      {
-         /*
-          * If the VACB isn't up to date then call the file
-          * system to read in the data.
-          */
-         Status = CcReadVirtualAddress(Vacb);
-         if (!NT_SUCCESS(Status))
-         {
-            CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
-            return Status;
-         }
-      }
-
-      Process = PsGetCurrentProcess();
-      PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
-      VacbOffset = BaseOffset + VACB_MAPPING_GRANULARITY - FileOffset;
-      Length = RawLength - SegOffset;
-      if (Length <= VacbOffset && Length <= PAGE_SIZE)
-      {
-         memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, Length);
-      }
-      else if (VacbOffset >= PAGE_SIZE)
-      {
-         memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, PAGE_SIZE);
-      }
-      else
-      {
-         memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, VacbOffset);
-         MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
-         CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, FALSE);
-         Status = CcRosGetVacb(SharedCacheMap,
-                               FileOffset + VacbOffset,
-                               &BaseOffset,
-                               &BaseAddress,
-                               &UptoDate,
-                               &Vacb);
-         if (!NT_SUCCESS(Status))
-         {
+    LONGLONG BaseOffset;
+    LONGLONG FileOffset;
+    PVOID BaseAddress;
+    BOOLEAN UptoDate;
+    PROS_VACB Vacb;
+    PFILE_OBJECT FileObject;
+    NTSTATUS Status;
+    LONGLONG RawLength;
+    PROS_SHARED_CACHE_MAP SharedCacheMap;
+    BOOLEAN IsImageSection;
+    LONGLONG Length;
+
+    FileObject = MemoryArea->Data.SectionData.Section->FileObject;
+    SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
+    RawLength = MemoryArea->Data.SectionData.Segment->RawLength.QuadPart;
+    FileOffset = SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset;
+    IsImageSection = MemoryArea->Data.SectionData.Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
+
+    ASSERT(SharedCacheMap);
+
+    DPRINT("%S %I64x\n", FileObject->FileName.Buffer, FileOffset);
+
+    /*
+     * If the file system is letting us go directly to the cache and the
+     * memory area was mapped at an offset in the file which is page aligned
+     * then get the related VACB.
+     */
+    if (((FileOffset % PAGE_SIZE) == 0) &&
+            ((SegOffset + PAGE_SIZE <= RawLength) || !IsImageSection) &&
+            !(MemoryArea->Data.SectionData.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
+    {
+
+        /*
+         * Get the related VACB; we use a lower level interface than
+         * filesystems do because it is safe for us to use an offset with an
+         * alignment less than the file system block size.
+         */
+        Status = CcRosGetVacb(SharedCacheMap,
+                              FileOffset,
+                              &BaseOffset,
+                              &BaseAddress,
+                              &UptoDate,
+                              &Vacb);
+        if (!NT_SUCCESS(Status))
+        {
+            return(Status);
+        }
+        if (!UptoDate)
+        {
+            /*
+             * If the VACB isn't up to date then call the file
+             * system to read in the data.
+             */
+            Status = CcReadVirtualAddress(Vacb);
+            if (!NT_SUCCESS(Status))
+            {
+                CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
+                return Status;
+            }
+        }
+
+        /* Probe the page, since it's PDE might not be synced */
+        (void)*((volatile char*)BaseAddress + FileOffset - BaseOffset);
+
+        /*
+         * Retrieve the page from the view that we actually want.
+         */
+        (*Page) = MmGetPhysicalAddress((char*)BaseAddress +
+                                       FileOffset - BaseOffset).LowPart >> PAGE_SHIFT;
+
+        CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, TRUE);
+    }
+    else
+    {
+        PEPROCESS Process;
+        KIRQL Irql;
+        PVOID PageAddr;
+        LONGLONG VacbOffset;
+
+        /*
+         * Allocate a page, this is rather complicated by the possibility
+         * we might have to move other things out of memory
+         */
+        MI_SET_USAGE(MI_USAGE_SECTION);
+        MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName);
+        Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, Page);
+        if (!NT_SUCCESS(Status))
+        {
+            return(Status);
+        }
+        Status = CcRosGetVacb(SharedCacheMap,
+                              FileOffset,
+                              &BaseOffset,
+                              &BaseAddress,
+                              &UptoDate,
+                              &Vacb);
+        if (!NT_SUCCESS(Status))
+        {
             return(Status);
-         }
-         if (!UptoDate)
-         {
+        }
+        if (!UptoDate)
+        {
             /*
              * If the VACB isn't up to date then call the file
              * system to read in the data.
@@ -1223,24 +1184,65 @@ MiReadPage(PMEMORY_AREA MemoryArea,
             Status = CcReadVirtualAddress(Vacb);
             if (!NT_SUCCESS(Status))
             {
-               CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
-               return Status;
+                CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
+                return Status;
+            }
+        }
+
+        Process = PsGetCurrentProcess();
+        PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
+        VacbOffset = BaseOffset + VACB_MAPPING_GRANULARITY - FileOffset;
+        Length = RawLength - SegOffset;
+        if (Length <= VacbOffset && Length <= PAGE_SIZE)
+        {
+            memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, Length);
+        }
+        else if (VacbOffset >= PAGE_SIZE)
+        {
+            memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, PAGE_SIZE);
+        }
+        else
+        {
+            memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, VacbOffset);
+            MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
+            CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, FALSE);
+            Status = CcRosGetVacb(SharedCacheMap,
+                                  FileOffset + VacbOffset,
+                                  &BaseOffset,
+                                  &BaseAddress,
+                                  &UptoDate,
+                                  &Vacb);
+            if (!NT_SUCCESS(Status))
+            {
+                return(Status);
+            }
+            if (!UptoDate)
+            {
+                /*
+                 * If the VACB isn't up to date then call the file
+                 * system to read in the data.
+                 */
+                Status = CcReadVirtualAddress(Vacb);
+                if (!NT_SUCCESS(Status))
+                {
+                    CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
+                    return Status;
+                }
+            }
+            PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
+            if (Length < PAGE_SIZE)
+            {
+                memcpy((char*)PageAddr + VacbOffset, BaseAddress, Length - VacbOffset);
             }
-         }
-         PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
-         if (Length < PAGE_SIZE)
-         {
-            memcpy((char*)PageAddr + VacbOffset, BaseAddress, Length - VacbOffset);
-         }
-         else
-         {
-            memcpy((char*)PageAddr + VacbOffset, BaseAddress, PAGE_SIZE - VacbOffset);
-         }
-      }
-      MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
-      CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, FALSE);
-   }
-   return(STATUS_SUCCESS);
+            else
+            {
+                memcpy((char*)PageAddr + VacbOffset, BaseAddress, PAGE_SIZE - VacbOffset);
+            }
+        }
+        MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
+        CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, FALSE);
+    }
+    return(STATUS_SUCCESS);
 }
 #else
 NTSTATUS
@@ -1256,22 +1258,22 @@ MiReadPage(PMEMORY_AREA MemoryArea,
  *       Page - Variable that receives a page contains the read data.
  */
 {
-   MM_REQUIRED_RESOURCES Resources;
-   NTSTATUS Status;
+    MM_REQUIRED_RESOURCES Resources;
+    NTSTATUS Status;
 
-   RtlZeroMemory(&Resources, sizeof(MM_REQUIRED_RESOURCES));
+    RtlZeroMemory(&Resources, sizeof(MM_REQUIRED_RESOURCES));
 
-   Resources.Context = MemoryArea->Data.SectionData.Section->FileObject;
-   Resources.FileOffset.QuadPart = SegOffset +
-       MemoryArea->Data.SectionData.Segment->Image.FileOffset;
-   Resources.Consumer = MC_USER;
-   Resources.Amount = PAGE_SIZE;
+    Resources.Context = MemoryArea->Data.SectionData.Section->FileObject;
+    Resources.FileOffset.QuadPart = SegOffset +
+                                    MemoryArea->Data.SectionData.Segment->Image.FileOffset;
+    Resources.Consumer = MC_USER;
+    Resources.Amount = PAGE_SIZE;
 
-   DPRINT("%S, offset 0x%x, len 0x%x, page 0x%x\n", ((PFILE_OBJECT)Resources.Context)->FileName.Buffer, Resources.FileOffset.LowPart, Resources.Amount, Resources.Page[0]);
+    DPRINT("%S, offset 0x%x, len 0x%x, page 0x%x\n", ((PFILE_OBJECT)Resources.Context)->FileName.Buffer, Resources.FileOffset.LowPart, Resources.Amount, Resources.Page[0]);
 
-   Status = MiReadFilePage(MmGetKernelAddressSpace(), MemoryArea, &Resources);
-   *Page = Resources.Page[0];
-   return Status;
+    Status = MiReadFilePage(MmGetKernelAddressSpace(), MemoryArea, &Resources);
+    *Page = Resources.Page[0];
+    return Status;
 }
 #endif
 
@@ -1282,857 +1284,913 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
                              PVOID Address,
                              BOOLEAN Locked)
 {
-   LARGE_INTEGER Offset;
-   PFN_NUMBER Page;
-   NTSTATUS Status;
-   PROS_SECTION_OBJECT Section;
-   PMM_SECTION_SEGMENT Segment;
-   ULONG_PTR Entry;
-   ULONG_PTR Entry1;
-   ULONG Attributes;
-   PMM_REGION Region;
-   BOOLEAN HasSwapEntry;
-   PVOID PAddress;
-   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
-   SWAPENTRY SwapEntry;
-
-   /*
-    * There is a window between taking the page fault and locking the
-    * address space when another thread could load the page so we check
-    * that.
-    */
-   if (MmIsPagePresent(Process, Address))
-   {
-      return(STATUS_SUCCESS);
-   }
-
-   if (MmIsDisabledPage(Process, Address))
-   {
-       return(STATUS_ACCESS_VIOLATION);
-   }
-
-   /*
-    * Check for the virtual memory area being deleted.
-    */
-   if (MemoryArea->DeleteInProgress)
-   {
-      return(STATUS_UNSUCCESSFUL);
-   }
-
-   PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
-   Offset.QuadPart = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress
-            + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
-
-   Segment = MemoryArea->Data.SectionData.Segment;
-   Section = MemoryArea->Data.SectionData.Section;
-   Region = MmFindRegion(MemoryArea->StartingAddress,
-                         &MemoryArea->Data.SectionData.RegionListHead,
-                         Address, NULL);
-   ASSERT(Region != NULL);
-   /*
-    * Lock the segment
-    */
-   MmLockSectionSegment(Segment);
-   Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
-   /*
-    * Check if this page needs to be mapped COW
-    */
-   if ((Segment->WriteCopy) &&
-       (Region->Protect == PAGE_READWRITE ||
-       Region->Protect == PAGE_EXECUTE_READWRITE))
-   {
-      Attributes = Region->Protect == PAGE_READWRITE ? PAGE_READONLY : PAGE_EXECUTE_READ;
-   }
-   else
-   {
-      Attributes = Region->Protect;
-   }
-
-   /*
-    * Check if someone else is already handling this fault, if so wait
-    * for them
-    */
-   if (Entry && IS_SWAP_FROM_SSE(Entry) && SWAPENTRY_FROM_SSE(Entry) == MM_WAIT_ENTRY)
-   {
-      MmUnlockSectionSegment(Segment);
-      MmUnlockAddressSpace(AddressSpace);
-      MiWaitForPageEvent(NULL, NULL);
-      MmLockAddressSpace(AddressSpace);
-      DPRINT("Address 0x%p\n", Address);
-      return(STATUS_MM_RESTART_OPERATION);
-   }
-
-   HasSwapEntry = MmIsPageSwapEntry(Process, Address);
-
-   if (HasSwapEntry)
-   {
-      SWAPENTRY DummyEntry;
-
-      /*
-       * Is it a wait entry?
-       */
-      MmGetPageFileMapping(Process, Address, &SwapEntry);
-
-      if (SwapEntry == MM_WAIT_ENTRY)
-      {
-         MmUnlockSectionSegment(Segment);
-         MmUnlockAddressSpace(AddressSpace);
-         MiWaitForPageEvent(NULL, NULL);
-         MmLockAddressSpace(AddressSpace);
-         return STATUS_MM_RESTART_OPERATION;
-      }
-
-      /*
-       * Must be private page we have swapped out.
-       */
-
-      /*
-       * Sanity check
-       */
-      if (Segment->Flags & MM_PAGEFILE_SEGMENT)
-      {
-         DPRINT1("Found a swaped out private page in a pagefile section.\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-
-      MmUnlockSectionSegment(Segment);
-      MmDeletePageFileMapping(Process, Address, &SwapEntry);
-      MmCreatePageFileMapping(Process, Address, MM_WAIT_ENTRY);
-
-      MmUnlockAddressSpace(AddressSpace);
-      MI_SET_USAGE(MI_USAGE_SECTION);
-      if (Process) MI_SET_PROCESS2(Process->ImageFileName);
-      if (!Process) MI_SET_PROCESS2("Kernel Section");
-      Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
-      if (!NT_SUCCESS(Status))
-      {
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-
-      Status = MmReadFromSwapPage(SwapEntry, Page);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("MmReadFromSwapPage failed, status = %x\n", Status);
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-      MmLockAddressSpace(AddressSpace);
-      MmDeletePageFileMapping(Process, PAddress, &DummyEntry);
-      Status = MmCreateVirtualMapping(Process,
-                                      PAddress,
-                                      Region->Protect,
-                                      &Page,
-                                      1);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT("MmCreateVirtualMapping failed, not out of memory\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-         return(Status);
-      }
-
-      /*
-       * Store the swap entry for later use.
-       */
-      MmSetSavedSwapEntryPage(Page, SwapEntry);
-
-      /*
-       * Add the page to the process's working set
-       */
-      MmInsertRmap(Page, Process, Address);
-      /*
-       * Finish the operation
-       */
-      MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%p\n", Address);
-      return(STATUS_SUCCESS);
-   }
-
-   /*
-    * Satisfying a page fault on a map of /Device/PhysicalMemory is easy
-    */
-   if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
-   {
-      MmUnlockSectionSegment(Segment);
-      /*
-       * Just map the desired physical page
-       */
-      Page = (PFN_NUMBER)(Offset.QuadPart >> PAGE_SHIFT);
-      Status = MmCreateVirtualMappingUnsafe(Process,
-                                            PAddress,
-                                            Region->Protect,
-                                            &Page,
-                                            1);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT("MmCreateVirtualMappingUnsafe failed, not out of memory\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-         return(Status);
-      }
-
-      /*
-       * Cleanup and release locks
-       */
-      MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%p\n", Address);
-      return(STATUS_SUCCESS);
-   }
-
-   /*
-    * Get the entry corresponding to the offset within the section
-    */
-   Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
-
-   if (Entry == 0)
-   {
-      SWAPENTRY FakeSwapEntry;
-
-      /*
-       * If the entry is zero (and it can't change because we have
-       * locked the segment) then we need to load the page.
-       */
-
-      /*
-       * Release all our locks and read in the page from disk
-       */
-      MmSetPageEntrySectionSegment(Segment, &Offset, MAKE_SWAP_SSE(MM_WAIT_ENTRY));
-      MmUnlockSectionSegment(Segment);
-      MmCreatePageFileMapping(Process, PAddress, MM_WAIT_ENTRY);
-      MmUnlockAddressSpace(AddressSpace);
-
-      if ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
-          ((Offset.QuadPart >= (LONGLONG)PAGE_ROUND_UP(Segment->RawLength.QuadPart) &&
-          (Section->AllocationAttributes & SEC_IMAGE))))
-      {
-         MI_SET_USAGE(MI_USAGE_SECTION);
-         if (Process) MI_SET_PROCESS2(Process->ImageFileName);
-         if (!Process) MI_SET_PROCESS2("Kernel Section");
-         Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
-         if (!NT_SUCCESS(Status))
-         {
-            DPRINT1("MmRequestPageMemoryConsumer failed (Status %x)\n", Status);
-         }
-
-      }
-      else
-      {
-         Status = MiReadPage(MemoryArea, Offset.QuadPart, &Page);
-         if (!NT_SUCCESS(Status))
-         {
-            DPRINT1("MiReadPage failed (Status %x)\n", Status);
-         }
-      }
-      if (!NT_SUCCESS(Status))
-      {
-         /*
-          * FIXME: What do we know in this case?
-          */
-         /*
-          * Cleanup and release locks
-          */
-         MmLockAddressSpace(AddressSpace);
-         MiSetPageEvent(Process, Address);
-         DPRINT("Address 0x%p\n", Address);
-         return(Status);
-      }
-
-      /*
-       * Mark the offset within the section as having valid, in-memory
-       * data
-       */
-      MmLockAddressSpace(AddressSpace);
-      MmLockSectionSegment(Segment);
-      Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
-      MmSetPageEntrySectionSegment(Segment, &Offset, Entry);
-      MmUnlockSectionSegment(Segment);
-
-      MmDeletePageFileMapping(Process, PAddress, &FakeSwapEntry);
-      DPRINT("CreateVirtualMapping Page %x Process %p PAddress %p Attributes %x\n",
-              Page, Process, PAddress, Attributes);
-      Status = MmCreateVirtualMapping(Process,
-                                      PAddress,
-                                      Attributes,
-                                      &Page,
-                                      1);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Unable to create virtual mapping\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-      ASSERT(MmIsPagePresent(Process, PAddress));
-      MmInsertRmap(Page, Process, Address);
-
-      MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%p\n", Address);
-      return(STATUS_SUCCESS);
-   }
-   else if (IS_SWAP_FROM_SSE(Entry))
-   {
-      SWAPENTRY SwapEntry;
-
-      SwapEntry = SWAPENTRY_FROM_SSE(Entry);
-
-      /*
-      * Release all our locks and read in the page from disk
-      */
-      MmUnlockSectionSegment(Segment);
-
-      MmUnlockAddressSpace(AddressSpace);
-      MI_SET_USAGE(MI_USAGE_SECTION);
-      if (Process) MI_SET_PROCESS2(Process->ImageFileName);
-      if (!Process) MI_SET_PROCESS2("Kernel Section");
-      Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
-      if (!NT_SUCCESS(Status))
-      {
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-
-      Status = MmReadFromSwapPage(SwapEntry, Page);
-      if (!NT_SUCCESS(Status))
-      {
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-
-      /*
-       * Relock the address space and segment
-       */
-      MmLockAddressSpace(AddressSpace);
-      MmLockSectionSegment(Segment);
-
-      /*
-       * Check the entry. No one should change the status of a page
-       * that has a pending page-in.
-       */
-      Entry1 = MmGetPageEntrySectionSegment(Segment, &Offset);
-      if (Entry != Entry1)
-      {
-          DPRINT1("Someone changed ppte entry while we slept (%x vs %x)\n", Entry, Entry1);
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-
-      /*
-       * Mark the offset within the section as having valid, in-memory
-       * data
-       */
-      Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
-      MmSetPageEntrySectionSegment(Segment, &Offset, Entry);
-      MmUnlockSectionSegment(Segment);
-
-      /*
-       * Save the swap entry.
-       */
-      MmSetSavedSwapEntryPage(Page, SwapEntry);
-      Status = MmCreateVirtualMapping(Process,
-                                      PAddress,
-                                      Region->Protect,
-                                      &Page,
-                                      1);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Unable to create virtual mapping\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-      MmInsertRmap(Page, Process, Address);
-      MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%p\n", Address);
-      return(STATUS_SUCCESS);
-   }
-   else
-   {
-      /*
-       * If the section offset is already in-memory and valid then just
-       * take another reference to the page
-       */
-
-      Page = PFN_FROM_SSE(Entry);
-
-      MmSharePageEntrySectionSegment(Segment, &Offset);
-      MmUnlockSectionSegment(Segment);
-
-      Status = MmCreateVirtualMapping(Process,
-                                      PAddress,
-                                      Attributes,
-                                      &Page,
-                                      1);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Unable to create virtual mapping\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-      MmInsertRmap(Page, Process, Address);
-      MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%p\n", Address);
-      return(STATUS_SUCCESS);
-   }
-}
+    LARGE_INTEGER Offset;
+    PFN_NUMBER Page;
+    NTSTATUS Status;
+    PROS_SECTION_OBJECT Section;
+    PMM_SECTION_SEGMENT Segment;
+    ULONG_PTR Entry;
+    ULONG_PTR Entry1;
+    ULONG Attributes;
+    PMM_REGION Region;
+    BOOLEAN HasSwapEntry;
+    PVOID PAddress;
+    PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+    SWAPENTRY SwapEntry;
 
-NTSTATUS
-NTAPI
-MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
-                         MEMORY_AREA* MemoryArea,
-                         PVOID Address)
-{
-   PMM_SECTION_SEGMENT Segment;
-   PROS_SECTION_OBJECT Section;
-   PFN_NUMBER OldPage;
-   PFN_NUMBER NewPage;
-   NTSTATUS Status;
-   PVOID PAddress;
-   LARGE_INTEGER Offset;
-   PMM_REGION Region;
-   ULONG_PTR Entry;
-   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
-   SWAPENTRY SwapEntry;
-
-   DPRINT("MmAccessFaultSectionView(%p, %p, %p)\n", AddressSpace, MemoryArea, Address);
-
-   /*
-    * Check if the page has already been set readwrite
-    */
-   if (MmGetPageProtect(Process, Address) & PAGE_READWRITE)
-   {
-      DPRINT("Address 0x%p\n", Address);
-      return(STATUS_SUCCESS);
-   }
-
-   /*
-    * Find the offset of the page
-    */
-   PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
-   Offset.QuadPart = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress
-            + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
-
-   Segment = MemoryArea->Data.SectionData.Segment;
-   Section = MemoryArea->Data.SectionData.Section;
-   Region = MmFindRegion(MemoryArea->StartingAddress,
-                         &MemoryArea->Data.SectionData.RegionListHead,
-                         Address, NULL);
-   ASSERT(Region != NULL);
-   /*
-    * Lock the segment
-    */
-   MmLockSectionSegment(Segment);
-
-   OldPage = MmGetPfnForProcess(Process, Address);
-   Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
-
-   MmUnlockSectionSegment(Segment);
-
-   /*
-    * Check if we are doing COW
-    */
-   if (!((Segment->WriteCopy) &&
-         (Region->Protect == PAGE_READWRITE ||
-          Region->Protect == PAGE_EXECUTE_READWRITE)))
-   {
-      DPRINT("Address 0x%p\n", Address);
-      return(STATUS_ACCESS_VIOLATION);
-   }
-
-   if (IS_SWAP_FROM_SSE(Entry) ||
-       PFN_FROM_SSE(Entry) != OldPage)
-   {
-      /* This is a private page. We must only change the page protection. */
-      MmSetPageProtect(Process, Address, Region->Protect);
-      return(STATUS_SUCCESS);
-   }
-
-   if(OldPage == 0)
-      DPRINT("OldPage == 0!\n");
-
-   /*
-    * Get or create a pageop
-    */
-   MmLockSectionSegment(Segment);
-   Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
-
-   /*
-    * Wait for any other operations to complete
-    */
-   if (Entry == SWAPENTRY_FROM_SSE(MM_WAIT_ENTRY))
-   {
-       MmUnlockSectionSegment(Segment);
-       MmUnlockAddressSpace(AddressSpace);
-       MiWaitForPageEvent(NULL, NULL);
-       /*
-        * Restart the operation
-        */
-       MmLockAddressSpace(AddressSpace);
-       DPRINT("Address 0x%p\n", Address);
-       return(STATUS_MM_RESTART_OPERATION);
-   }
-
-   MmDeleteRmap(OldPage, Process, PAddress);
-   MmDeleteVirtualMapping(Process, PAddress, FALSE, NULL, NULL);
-   MmCreatePageFileMapping(Process, PAddress, MM_WAIT_ENTRY);
-
-   /*
-    * Release locks now we have the pageop
-    */
-   MmUnlockSectionSegment(Segment);
-   MmUnlockAddressSpace(AddressSpace);
-
-   /*
-    * Allocate a page
-    */
-   MI_SET_USAGE(MI_USAGE_SECTION);
-   if (Process) MI_SET_PROCESS2(Process->ImageFileName);
-   if (!Process) MI_SET_PROCESS2("Kernel Section");
-   Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &NewPage);
-   if (!NT_SUCCESS(Status))
-   {
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   /*
-    * Copy the old page
-    */
-   MiCopyFromUserPage(NewPage, OldPage);
-
-   MmLockAddressSpace(AddressSpace);
-
-   /*
-    * Set the PTE to point to the new page
-    */
-   MmDeletePageFileMapping(Process, PAddress, &SwapEntry);
-   Status = MmCreateVirtualMapping(Process,
-                                   PAddress,
-                                   Region->Protect,
-                                   &NewPage,
-                                   1);
-    if (!NT_SUCCESS(Status))
+    /*
+     * There is a window between taking the page fault and locking the
+     * address space when another thread could load the page so we check
+     * that.
+     */
+    if (MmIsPagePresent(Process, Address))
     {
-        DPRINT1("MmCreateVirtualMapping failed, unable to create virtual mapping, not out of memory\n");
-        KeBugCheck(MEMORY_MANAGEMENT);
-        return(Status);
+        return(STATUS_SUCCESS);
     }
 
-   /*
-    * Unshare the old page.
-    */
-   DPRINT("Swapping page (Old %x New %x)\n", OldPage, NewPage);
-   MmInsertRmap(NewPage, Process, PAddress);
-   MmLockSectionSegment(Segment);
-   MmUnsharePageEntrySectionSegment(Section, Segment, &Offset, FALSE, FALSE, NULL);
-   MmUnlockSectionSegment(Segment);
+    if (MmIsDisabledPage(Process, Address))
+    {
+        return(STATUS_ACCESS_VIOLATION);
+    }
 
-   MiSetPageEvent(Process, Address);
-   DPRINT("Address 0x%p\n", Address);
-   return(STATUS_SUCCESS);
-}
+    /*
+     * Check for the virtual memory area being deleted.
+     */
+    if (MemoryArea->DeleteInProgress)
+    {
+        return(STATUS_UNSUCCESSFUL);
+    }
 
-VOID
-MmPageOutDeleteMapping(PVOID Context, PEPROCESS Process, PVOID Address)
-{
-   MM_SECTION_PAGEOUT_CONTEXT* PageOutContext;
-   BOOLEAN WasDirty;
-   PFN_NUMBER Page = 0;
-
-   PageOutContext = (MM_SECTION_PAGEOUT_CONTEXT*)Context;
-   if (Process)
-   {
-      MmLockAddressSpace(&Process->Vm);
-   }
-
-   MmDeleteVirtualMapping(Process,
-                          Address,
-                          FALSE,
-                          &WasDirty,
-                          &Page);
-   if (WasDirty)
-   {
-      PageOutContext->WasDirty = TRUE;
-   }
-   if (!PageOutContext->Private)
-   {
-      MmLockSectionSegment(PageOutContext->Segment);
-      MmUnsharePageEntrySectionSegment((PROS_SECTION_OBJECT)PageOutContext->Section,
-                                       PageOutContext->Segment,
-                                       &PageOutContext->Offset,
-                                       PageOutContext->WasDirty,
-                                       TRUE,
-                                       &PageOutContext->SectionEntry);
-      MmUnlockSectionSegment(PageOutContext->Segment);
-   }
-   if (Process)
-   {
-      MmUnlockAddressSpace(&Process->Vm);
-   }
-
-   if (PageOutContext->Private)
-   {
-      MmReleasePageMemoryConsumer(MC_USER, Page);
-   }
-}
+    PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
+    Offset.QuadPart = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress
+                      + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
 
-NTSTATUS
-NTAPI
-MmPageOutSectionView(PMMSUPPORT AddressSpace,
-                     MEMORY_AREA* MemoryArea,
-                     PVOID Address, ULONG_PTR Entry)
-{
-   PFN_NUMBER Page;
-   MM_SECTION_PAGEOUT_CONTEXT Context;
-   SWAPENTRY SwapEntry;
-   ULONGLONG FileOffset;
-   NTSTATUS Status;
-   PFILE_OBJECT FileObject;
-#ifndef NEWCC
-   PROS_SHARED_CACHE_MAP SharedCacheMap = NULL;
-#endif
-   BOOLEAN DirectMapped;
-   BOOLEAN IsImageSection;
-   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
-   KIRQL OldIrql;
+    Segment = MemoryArea->Data.SectionData.Segment;
+    Section = MemoryArea->Data.SectionData.Section;
+    Region = MmFindRegion(MemoryArea->StartingAddress,
+                          &MemoryArea->Data.SectionData.RegionListHead,
+                          Address, NULL);
+    ASSERT(Region != NULL);
+    /*
+     * Lock the segment
+     */
+    MmLockSectionSegment(Segment);
+    Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
+    /*
+     * Check if this page needs to be mapped COW
+     */
+    if ((Segment->WriteCopy) &&
+            (Region->Protect == PAGE_READWRITE ||
+             Region->Protect == PAGE_EXECUTE_READWRITE))
+    {
+        Attributes = Region->Protect == PAGE_READWRITE ? PAGE_READONLY : PAGE_EXECUTE_READ;
+    }
+    else
+    {
+        Attributes = Region->Protect;
+    }
+
+    /*
+     * Check if someone else is already handling this fault, if so wait
+     * for them
+     */
+    if (Entry && IS_SWAP_FROM_SSE(Entry) && SWAPENTRY_FROM_SSE(Entry) == MM_WAIT_ENTRY)
+    {
+        MmUnlockSectionSegment(Segment);
+        MmUnlockAddressSpace(AddressSpace);
+        MiWaitForPageEvent(NULL, NULL);
+        MmLockAddressSpace(AddressSpace);
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_MM_RESTART_OPERATION);
+    }
+
+    HasSwapEntry = MmIsPageSwapEntry(Process, Address);
+
+    if (HasSwapEntry)
+    {
+        SWAPENTRY DummyEntry;
+
+        /*
+         * Is it a wait entry?
+         */
+        MmGetPageFileMapping(Process, Address, &SwapEntry);
+
+        if (SwapEntry == MM_WAIT_ENTRY)
+        {
+            MmUnlockSectionSegment(Segment);
+            MmUnlockAddressSpace(AddressSpace);
+            MiWaitForPageEvent(NULL, NULL);
+            MmLockAddressSpace(AddressSpace);
+            return STATUS_MM_RESTART_OPERATION;
+        }
+
+        /*
+         * Must be private page we have swapped out.
+         */
+
+        /*
+         * Sanity check
+         */
+        if (Segment->Flags & MM_PAGEFILE_SEGMENT)
+        {
+            DPRINT1("Found a swaped out private page in a pagefile section.\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+
+        MmUnlockSectionSegment(Segment);
+        MmDeletePageFileMapping(Process, Address, &SwapEntry);
+        MmCreatePageFileMapping(Process, Address, MM_WAIT_ENTRY);
+
+        MmUnlockAddressSpace(AddressSpace);
+        MI_SET_USAGE(MI_USAGE_SECTION);
+        if (Process) MI_SET_PROCESS2(Process->ImageFileName);
+        if (!Process) MI_SET_PROCESS2("Kernel Section");
+        Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
+        if (!NT_SUCCESS(Status))
+        {
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+
+        Status = MmReadFromSwapPage(SwapEntry, Page);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("MmReadFromSwapPage failed, status = %x\n", Status);
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+        MmLockAddressSpace(AddressSpace);
+        MmDeletePageFileMapping(Process, PAddress, &DummyEntry);
+        Status = MmCreateVirtualMapping(Process,
+                                        PAddress,
+                                        Region->Protect,
+                                        &Page,
+                                        1);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT("MmCreateVirtualMapping failed, not out of memory\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+            return(Status);
+        }
+
+        /*
+         * Store the swap entry for later use.
+         */
+        MmSetSavedSwapEntryPage(Page, SwapEntry);
+
+        /*
+         * Add the page to the process's working set
+         */
+        MmInsertRmap(Page, Process, Address);
+        /*
+         * Finish the operation
+         */
+        MiSetPageEvent(Process, Address);
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_SUCCESS);
+    }
+
+    /*
+     * Satisfying a page fault on a map of /Device/PhysicalMemory is easy
+     */
+    if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
+    {
+        MmUnlockSectionSegment(Segment);
+        /*
+         * Just map the desired physical page
+         */
+        Page = (PFN_NUMBER)(Offset.QuadPart >> PAGE_SHIFT);
+        Status = MmCreateVirtualMappingUnsafe(Process,
+                                              PAddress,
+                                              Region->Protect,
+                                              &Page,
+                                              1);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT("MmCreateVirtualMappingUnsafe failed, not out of memory\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+            return(Status);
+        }
+
+        /*
+         * Cleanup and release locks
+         */
+        MiSetPageEvent(Process, Address);
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_SUCCESS);
+    }
+
+    /*
+     * Get the entry corresponding to the offset within the section
+     */
+    Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
+
+    if (Entry == 0)
+    {
+        SWAPENTRY FakeSwapEntry;
+
+        /*
+         * If the entry is zero (and it can't change because we have
+         * locked the segment) then we need to load the page.
+         */
+
+        /*
+         * Release all our locks and read in the page from disk
+         */
+        MmSetPageEntrySectionSegment(Segment, &Offset, MAKE_SWAP_SSE(MM_WAIT_ENTRY));
+        MmUnlockSectionSegment(Segment);
+        MmCreatePageFileMapping(Process, PAddress, MM_WAIT_ENTRY);
+        MmUnlockAddressSpace(AddressSpace);
+
+        if ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
+                ((Offset.QuadPart >= (LONGLONG)PAGE_ROUND_UP(Segment->RawLength.QuadPart) &&
+                  (Section->AllocationAttributes & SEC_IMAGE))))
+        {
+            MI_SET_USAGE(MI_USAGE_SECTION);
+            if (Process) MI_SET_PROCESS2(Process->ImageFileName);
+            if (!Process) MI_SET_PROCESS2("Kernel Section");
+            Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT1("MmRequestPageMemoryConsumer failed (Status %x)\n", Status);
+            }
+
+        }
+        else
+        {
+            Status = MiReadPage(MemoryArea, Offset.QuadPart, &Page);
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT1("MiReadPage failed (Status %x)\n", Status);
+            }
+        }
+        if (!NT_SUCCESS(Status))
+        {
+            /*
+             * FIXME: What do we know in this case?
+             */
+            /*
+             * Cleanup and release locks
+             */
+            MmLockAddressSpace(AddressSpace);
+            MiSetPageEvent(Process, Address);
+            DPRINT("Address 0x%p\n", Address);
+            return(Status);
+        }
+
+        /*
+         * Mark the offset within the section as having valid, in-memory
+         * data
+         */
+        MmLockAddressSpace(AddressSpace);
+        MmLockSectionSegment(Segment);
+        Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
+        MmSetPageEntrySectionSegment(Segment, &Offset, Entry);
+        MmUnlockSectionSegment(Segment);
+
+        MmDeletePageFileMapping(Process, PAddress, &FakeSwapEntry);
+        DPRINT("CreateVirtualMapping Page %x Process %p PAddress %p Attributes %x\n",
+               Page, Process, PAddress, Attributes);
+        Status = MmCreateVirtualMapping(Process,
+                                        PAddress,
+                                        Attributes,
+                                        &Page,
+                                        1);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("Unable to create virtual mapping\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+        ASSERT(MmIsPagePresent(Process, PAddress));
+        MmInsertRmap(Page, Process, Address);
+
+        MiSetPageEvent(Process, Address);
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_SUCCESS);
+    }
+    else if (IS_SWAP_FROM_SSE(Entry))
+    {
+        SWAPENTRY SwapEntry;
+
+        SwapEntry = SWAPENTRY_FROM_SSE(Entry);
+
+        /*
+        * Release all our locks and read in the page from disk
+        */
+        MmUnlockSectionSegment(Segment);
+
+        MmUnlockAddressSpace(AddressSpace);
+        MI_SET_USAGE(MI_USAGE_SECTION);
+        if (Process) MI_SET_PROCESS2(Process->ImageFileName);
+        if (!Process) MI_SET_PROCESS2("Kernel Section");
+        Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
+        if (!NT_SUCCESS(Status))
+        {
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+
+        Status = MmReadFromSwapPage(SwapEntry, Page);
+        if (!NT_SUCCESS(Status))
+        {
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+
+        /*
+         * Relock the address space and segment
+         */
+        MmLockAddressSpace(AddressSpace);
+        MmLockSectionSegment(Segment);
+
+        /*
+         * Check the entry. No one should change the status of a page
+         * that has a pending page-in.
+         */
+        Entry1 = MmGetPageEntrySectionSegment(Segment, &Offset);
+        if (Entry != Entry1)
+        {
+            DPRINT1("Someone changed ppte entry while we slept (%x vs %x)\n", Entry, Entry1);
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+
+        /*
+         * Mark the offset within the section as having valid, in-memory
+         * data
+         */
+        Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
+        MmSetPageEntrySectionSegment(Segment, &Offset, Entry);
+        MmUnlockSectionSegment(Segment);
+
+        /*
+         * Save the swap entry.
+         */
+        MmSetSavedSwapEntryPage(Page, SwapEntry);
+        Status = MmCreateVirtualMapping(Process,
+                                        PAddress,
+                                        Region->Protect,
+                                        &Page,
+                                        1);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("Unable to create virtual mapping\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+        MmInsertRmap(Page, Process, Address);
+        MiSetPageEvent(Process, Address);
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_SUCCESS);
+    }
+    else
+    {
+        /*
+         * If the section offset is already in-memory and valid then just
+         * take another reference to the page
+         */
+
+        Page = PFN_FROM_SSE(Entry);
+
+        MmSharePageEntrySectionSegment(Segment, &Offset);
+        MmUnlockSectionSegment(Segment);
+
+        Status = MmCreateVirtualMapping(Process,
+                                        PAddress,
+                                        Attributes,
+                                        &Page,
+                                        1);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("Unable to create virtual mapping\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+        MmInsertRmap(Page, Process, Address);
+        MiSetPageEvent(Process, Address);
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_SUCCESS);
+    }
+}
+
+NTSTATUS
+NTAPI
+MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
+                         MEMORY_AREA* MemoryArea,
+                         PVOID Address)
+{
+    PMM_SECTION_SEGMENT Segment;
+    PROS_SECTION_OBJECT Section;
+    PFN_NUMBER OldPage;
+    PFN_NUMBER NewPage;
+    NTSTATUS Status;
+    PVOID PAddress;
+    LARGE_INTEGER Offset;
+    PMM_REGION Region;
+    ULONG_PTR Entry;
+    PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+    SWAPENTRY SwapEntry;
+
+    DPRINT("MmAccessFaultSectionView(%p, %p, %p)\n", AddressSpace, MemoryArea, Address);
+
+    /*
+     * Check if the page has already been set readwrite
+     */
+    if (MmGetPageProtect(Process, Address) & PAGE_READWRITE)
+    {
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_SUCCESS);
+    }
+
+    /*
+     * Find the offset of the page
+     */
+    PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
+    Offset.QuadPart = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress
+                      + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
+
+    Segment = MemoryArea->Data.SectionData.Segment;
+    Section = MemoryArea->Data.SectionData.Section;
+    Region = MmFindRegion(MemoryArea->StartingAddress,
+                          &MemoryArea->Data.SectionData.RegionListHead,
+                          Address, NULL);
+    ASSERT(Region != NULL);
+    /*
+     * Lock the segment
+     */
+    MmLockSectionSegment(Segment);
+
+    OldPage = MmGetPfnForProcess(Process, Address);
+    Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
+
+    MmUnlockSectionSegment(Segment);
+
+    /*
+     * Check if we are doing COW
+     */
+    if (!((Segment->WriteCopy) &&
+            (Region->Protect == PAGE_READWRITE ||
+             Region->Protect == PAGE_EXECUTE_READWRITE)))
+    {
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_ACCESS_VIOLATION);
+    }
+
+    if (IS_SWAP_FROM_SSE(Entry) ||
+            PFN_FROM_SSE(Entry) != OldPage)
+    {
+        /* This is a private page. We must only change the page protection. */
+        MmSetPageProtect(Process, Address, Region->Protect);
+        return(STATUS_SUCCESS);
+    }
+
+    if(OldPage == 0)
+        DPRINT("OldPage == 0!\n");
+
+    /*
+     * Get or create a pageop
+     */
+    MmLockSectionSegment(Segment);
+    Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
+
+    /*
+     * Wait for any other operations to complete
+     */
+    if (Entry == SWAPENTRY_FROM_SSE(MM_WAIT_ENTRY))
+    {
+        MmUnlockSectionSegment(Segment);
+        MmUnlockAddressSpace(AddressSpace);
+        MiWaitForPageEvent(NULL, NULL);
+        /*
+         * Restart the operation
+         */
+        MmLockAddressSpace(AddressSpace);
+        DPRINT("Address 0x%p\n", Address);
+        return(STATUS_MM_RESTART_OPERATION);
+    }
+
+    MmDeleteRmap(OldPage, Process, PAddress);
+    MmDeleteVirtualMapping(Process, PAddress, FALSE, NULL, NULL);
+    MmCreatePageFileMapping(Process, PAddress, MM_WAIT_ENTRY);
+
+    /*
+     * Release locks now we have the pageop
+     */
+    MmUnlockSectionSegment(Segment);
+    MmUnlockAddressSpace(AddressSpace);
+
+    /*
+     * Allocate a page
+     */
+    MI_SET_USAGE(MI_USAGE_SECTION);
+    if (Process) MI_SET_PROCESS2(Process->ImageFileName);
+    if (!Process) MI_SET_PROCESS2("Kernel Section");
+    Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &NewPage);
+    if (!NT_SUCCESS(Status))
+    {
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    /*
+     * Copy the old page
+     */
+    MiCopyFromUserPage(NewPage, OldPage);
+
+    MmLockAddressSpace(AddressSpace);
+
+    /*
+     * Set the PTE to point to the new page
+     */
+    MmDeletePageFileMapping(Process, PAddress, &SwapEntry);
+    Status = MmCreateVirtualMapping(Process,
+                                    PAddress,
+                                    Region->Protect,
+                                    &NewPage,
+                                    1);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("MmCreateVirtualMapping failed, unable to create virtual mapping, not out of memory\n");
+        KeBugCheck(MEMORY_MANAGEMENT);
+        return(Status);
+    }
+
+    /*
+     * Unshare the old page.
+     */
+    DPRINT("Swapping page (Old %x New %x)\n", OldPage, NewPage);
+    MmInsertRmap(NewPage, Process, PAddress);
+    MmLockSectionSegment(Segment);
+    MmUnsharePageEntrySectionSegment(Section, Segment, &Offset, FALSE, FALSE, NULL);
+    MmUnlockSectionSegment(Segment);
+
+    MiSetPageEvent(Process, Address);
+    DPRINT("Address 0x%p\n", Address);
+    return(STATUS_SUCCESS);
+}
+
+VOID
+MmPageOutDeleteMapping(PVOID Context, PEPROCESS Process, PVOID Address)
+{
+    MM_SECTION_PAGEOUT_CONTEXT* PageOutContext;
+    BOOLEAN WasDirty;
+    PFN_NUMBER Page = 0;
+
+    PageOutContext = (MM_SECTION_PAGEOUT_CONTEXT*)Context;
+    if (Process)
+    {
+        MmLockAddressSpace(&Process->Vm);
+    }
+
+    MmDeleteVirtualMapping(Process,
+                           Address,
+                           FALSE,
+                           &WasDirty,
+                           &Page);
+    if (WasDirty)
+    {
+        PageOutContext->WasDirty = TRUE;
+    }
+    if (!PageOutContext->Private)
+    {
+        MmLockSectionSegment(PageOutContext->Segment);
+        MmUnsharePageEntrySectionSegment((PROS_SECTION_OBJECT)PageOutContext->Section,
+                                         PageOutContext->Segment,
+                                         &PageOutContext->Offset,
+                                         PageOutContext->WasDirty,
+                                         TRUE,
+                                         &PageOutContext->SectionEntry);
+        MmUnlockSectionSegment(PageOutContext->Segment);
+    }
+    if (Process)
+    {
+        MmUnlockAddressSpace(&Process->Vm);
+    }
 
-   Address = (PVOID)PAGE_ROUND_DOWN(Address);
+    if (PageOutContext->Private)
+    {
+        MmReleasePageMemoryConsumer(MC_USER, Page);
+    }
+}
 
-   /*
-    * Get the segment and section.
-    */
-   Context.Segment = MemoryArea->Data.SectionData.Segment;
-   Context.Section = MemoryArea->Data.SectionData.Section;
-   Context.SectionEntry = Entry;
-   Context.CallingProcess = Process;
+NTSTATUS
+NTAPI
+MmPageOutSectionView(PMMSUPPORT AddressSpace,
+                     MEMORY_AREA* MemoryArea,
+                     PVOID Address, ULONG_PTR Entry)
+{
+    PFN_NUMBER Page;
+    MM_SECTION_PAGEOUT_CONTEXT Context;
+    SWAPENTRY SwapEntry;
+    ULONGLONG FileOffset;
+    NTSTATUS Status;
+    PFILE_OBJECT FileObject;
+#ifndef NEWCC
+    PROS_SHARED_CACHE_MAP SharedCacheMap = NULL;
+#endif
+    BOOLEAN DirectMapped;
+    BOOLEAN IsImageSection;
+    PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+    KIRQL OldIrql;
 
-   Context.Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
-                    + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
-   FileOffset = Context.Offset.QuadPart + Context.Segment->Image.FileOffset;
+    Address = (PVOID)PAGE_ROUND_DOWN(Address);
 
-   IsImageSection = Context.Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
+    /*
+     * Get the segment and section.
+     */
+    Context.Segment = MemoryArea->Data.SectionData.Segment;
+    Context.Section = MemoryArea->Data.SectionData.Section;
+    Context.SectionEntry = Entry;
+    Context.CallingProcess = Process;
+
+    Context.Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
+                              + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
+    FileOffset = Context.Offset.QuadPart + Context.Segment->Image.FileOffset;
 
-   FileObject = Context.Section->FileObject;
-   DirectMapped = FALSE;
+    IsImageSection = Context.Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
 
-   MmLockSectionSegment(Context.Segment);
+    FileObject = Context.Section->FileObject;
+    DirectMapped = FALSE;
+
+    MmLockSectionSegment(Context.Segment);
 
 #ifndef NEWCC
-   if (FileObject != NULL &&
-       !(Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
-   {
-      SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
-
-      /*
-       * If the file system is letting us go directly to the cache and the
-       * memory area was mapped at an offset in the file which is page aligned
-       * then note this is a direct mapped page.
-       */
-      if ((FileOffset % PAGE_SIZE) == 0 &&
-            (Context.Offset.QuadPart + PAGE_SIZE <= Context.Segment->RawLength.QuadPart || !IsImageSection))
-      {
-         DirectMapped = TRUE;
-      }
-   }
+    if (FileObject != NULL &&
+            !(Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
+    {
+        SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
+
+        /*
+         * If the file system is letting us go directly to the cache and the
+         * memory area was mapped at an offset in the file which is page aligned
+         * then note this is a direct mapped page.
+         */
+        if ((FileOffset % PAGE_SIZE) == 0 &&
+                (Context.Offset.QuadPart + PAGE_SIZE <= Context.Segment->RawLength.QuadPart || !IsImageSection))
+        {
+            DirectMapped = TRUE;
+        }
+    }
 #endif
 
 
-   /*
-    * This should never happen since mappings of physical memory are never
-    * placed in the rmap lists.
-    */
-   if (Context.Section->AllocationAttributes & SEC_PHYSICALMEMORY)
-   {
-      DPRINT1("Trying to page out from physical memory section address 0x%p "
-              "process %p\n", Address,
-              Process ? Process->UniqueProcessId : 0);
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   /*
-    * Get the section segment entry and the physical address.
-    */
-   if (!MmIsPagePresent(Process, Address))
-   {
-      DPRINT1("Trying to page out not-present page at (%p,0x%p).\n",
-              Process ? Process->UniqueProcessId : 0, Address);
-       KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   Page = MmGetPfnForProcess(Process, Address);
-   SwapEntry = MmGetSavedSwapEntryPage(Page);
-
-   /*
-    * Check the reference count to ensure this page can be paged out
-    */
-   if (MmGetReferenceCountPage(Page) != 1)
-   {
-       DPRINT("Cannot page out locked section page: 0x%lu (RefCount: %lu)\n",
+    /*
+     * This should never happen since mappings of physical memory are never
+     * placed in the rmap lists.
+     */
+    if (Context.Section->AllocationAttributes & SEC_PHYSICALMEMORY)
+    {
+        DPRINT1("Trying to page out from physical memory section address 0x%p "
+                "process %p\n", Address,
+                Process ? Process->UniqueProcessId : 0);
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    /*
+     * Get the section segment entry and the physical address.
+     */
+    if (!MmIsPagePresent(Process, Address))
+    {
+        DPRINT1("Trying to page out not-present page at (%p,0x%p).\n",
+                Process ? Process->UniqueProcessId : 0, Address);
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    Page = MmGetPfnForProcess(Process, Address);
+    SwapEntry = MmGetSavedSwapEntryPage(Page);
+
+    /*
+     * Check the reference count to ensure this page can be paged out
+     */
+    if (MmGetReferenceCountPage(Page) != 1)
+    {
+        DPRINT("Cannot page out locked section page: 0x%lu (RefCount: %lu)\n",
                Page, MmGetReferenceCountPage(Page));
-       MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
-       MmUnlockSectionSegment(Context.Segment);
-       return STATUS_UNSUCCESSFUL;
-   }
-
-   /*
-    * Prepare the context structure for the rmap delete call.
-    */
-   MmUnlockSectionSegment(Context.Segment);
-   Context.WasDirty = FALSE;
-   if (Context.Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
-         IS_SWAP_FROM_SSE(Entry) ||
-         PFN_FROM_SSE(Entry) != Page)
-   {
-      Context.Private = TRUE;
-   }
-   else
-   {
-      Context.Private = FALSE;
-   }
-
-   /*
-    * Take an additional reference to the page or the VACB.
-    */
-   if (DirectMapped && !Context.Private)
-   {
-      if(!MiIsPageFromCache(MemoryArea, Context.Offset.QuadPart))
-      {
-         DPRINT1("Direct mapped non private page is not associated with the cache.\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-   }
-   else
-   {
-      OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
-      MmReferencePage(Page);
-      KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
-   }
-
-   MmDeleteAllRmaps(Page, (PVOID)&Context, MmPageOutDeleteMapping);
-
-   /* Since we passed in a surrogate, we'll get back the page entry
-    * state in our context.  This is intended to make intermediate
-    * decrements of share count not release the wait entry.
-    */
-   Entry = Context.SectionEntry;
-
-   /*
-    * If this wasn't a private page then we should have reduced the entry to
-    * zero by deleting all the rmaps.
-    */
-   if (!Context.Private && Entry != 0)
-   {
-      if (!(Context.Segment->Flags & MM_PAGEFILE_SEGMENT) &&
-            !(Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
-      {
-          KeBugCheckEx(MEMORY_MANAGEMENT, Entry, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
-      }
-   }
-
-   /*
-    * If the page wasn't dirty then we can just free it as for a readonly page.
-    * Since we unmapped all the mappings above we know it will not suddenly
-    * become dirty.
-    * If the page is from a pagefile section and has no swap entry,
-    * we can't free the page at this point.
-    */
-   SwapEntry = MmGetSavedSwapEntryPage(Page);
-   if (Context.Segment->Flags & MM_PAGEFILE_SEGMENT)
-   {
-      if (Context.Private)
-      {
-         DPRINT1("Found a %s private page (address %p) in a pagefile segment.\n",
-                 Context.WasDirty ? "dirty" : "clean", Address);
-         KeBugCheckEx(MEMORY_MANAGEMENT, SwapEntry, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
-      }
-      if (!Context.WasDirty && SwapEntry != 0)
-      {
-         MmSetSavedSwapEntryPage(Page, 0);
-         MmLockSectionSegment(Context.Segment);
-         MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, MAKE_SWAP_SSE(SwapEntry));
-         MmUnlockSectionSegment(Context.Segment);
-         MmReleasePageMemoryConsumer(MC_USER, Page);
-         MiSetPageEvent(NULL, NULL);
-         return(STATUS_SUCCESS);
-      }
-   }
-   else if (Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)
-   {
-      if (Context.Private)
-      {
-         DPRINT1("Found a %s private page (address %p) in a shared section segment.\n",
-                 Context.WasDirty ? "dirty" : "clean", Address);
-         KeBugCheckEx(MEMORY_MANAGEMENT, Page, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
-      }
-      if (!Context.WasDirty || SwapEntry != 0)
-      {
-         MmSetSavedSwapEntryPage(Page, 0);
-         if (SwapEntry != 0)
-         {
+        MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
+        MmUnlockSectionSegment(Context.Segment);
+        return STATUS_UNSUCCESSFUL;
+    }
+
+    /*
+     * Prepare the context structure for the rmap delete call.
+     */
+    MmUnlockSectionSegment(Context.Segment);
+    Context.WasDirty = FALSE;
+    if (Context.Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
+            IS_SWAP_FROM_SSE(Entry) ||
+            PFN_FROM_SSE(Entry) != Page)
+    {
+        Context.Private = TRUE;
+    }
+    else
+    {
+        Context.Private = FALSE;
+    }
+
+    /*
+     * Take an additional reference to the page or the VACB.
+     */
+    if (DirectMapped && !Context.Private)
+    {
+        if(!MiIsPageFromCache(MemoryArea, Context.Offset.QuadPart))
+        {
+            DPRINT1("Direct mapped non private page is not associated with the cache.\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
+        }
+    }
+    else
+    {
+        OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+        MmReferencePage(Page);
+        KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
+    }
+
+    MmDeleteAllRmaps(Page, (PVOID)&Context, MmPageOutDeleteMapping);
+
+    /* Since we passed in a surrogate, we'll get back the page entry
+     * state in our context.  This is intended to make intermediate
+     * decrements of share count not release the wait entry.
+     */
+    Entry = Context.SectionEntry;
+
+    /*
+     * If this wasn't a private page then we should have reduced the entry to
+     * zero by deleting all the rmaps.
+     */
+    if (!Context.Private && Entry != 0)
+    {
+        if (!(Context.Segment->Flags & MM_PAGEFILE_SEGMENT) &&
+                !(Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
+        {
+            KeBugCheckEx(MEMORY_MANAGEMENT, Entry, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
+        }
+    }
+
+    /*
+     * If the page wasn't dirty then we can just free it as for a readonly page.
+     * Since we unmapped all the mappings above we know it will not suddenly
+     * become dirty.
+     * If the page is from a pagefile section and has no swap entry,
+     * we can't free the page at this point.
+     */
+    SwapEntry = MmGetSavedSwapEntryPage(Page);
+    if (Context.Segment->Flags & MM_PAGEFILE_SEGMENT)
+    {
+        if (Context.Private)
+        {
+            DPRINT1("Found a %s private page (address %p) in a pagefile segment.\n",
+                    Context.WasDirty ? "dirty" : "clean", Address);
+            KeBugCheckEx(MEMORY_MANAGEMENT, SwapEntry, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
+        }
+        if (!Context.WasDirty && SwapEntry != 0)
+        {
+            MmSetSavedSwapEntryPage(Page, 0);
             MmLockSectionSegment(Context.Segment);
             MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, MAKE_SWAP_SSE(SwapEntry));
             MmUnlockSectionSegment(Context.Segment);
-         }
-         MmReleasePageMemoryConsumer(MC_USER, Page);
-         MiSetPageEvent(NULL, NULL);
-         return(STATUS_SUCCESS);
-      }
-   }
-   else if (!Context.Private && DirectMapped)
-   {
-      if (SwapEntry != 0)
-      {
-         DPRINT1("Found a swapentry for a non private and direct mapped page (address %p)\n",
-                 Address);
-         KeBugCheckEx(MEMORY_MANAGEMENT, STATUS_UNSUCCESSFUL, SwapEntry, (ULONG_PTR)Process, (ULONG_PTR)Address);
-      }
+            MmReleasePageMemoryConsumer(MC_USER, Page);
+            MiSetPageEvent(NULL, NULL);
+            return(STATUS_SUCCESS);
+        }
+    }
+    else if (Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)
+    {
+        if (Context.Private)
+        {
+            DPRINT1("Found a %s private page (address %p) in a shared section segment.\n",
+                    Context.WasDirty ? "dirty" : "clean", Address);
+            KeBugCheckEx(MEMORY_MANAGEMENT, Page, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
+        }
+        if (!Context.WasDirty || SwapEntry != 0)
+        {
+            MmSetSavedSwapEntryPage(Page, 0);
+            if (SwapEntry != 0)
+            {
+                MmLockSectionSegment(Context.Segment);
+                MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, MAKE_SWAP_SSE(SwapEntry));
+                MmUnlockSectionSegment(Context.Segment);
+            }
+            MmReleasePageMemoryConsumer(MC_USER, Page);
+            MiSetPageEvent(NULL, NULL);
+            return(STATUS_SUCCESS);
+        }
+    }
+    else if (!Context.Private && DirectMapped)
+    {
+        if (SwapEntry != 0)
+        {
+            DPRINT1("Found a swapentry for a non private and direct mapped page (address %p)\n",
+                    Address);
+            KeBugCheckEx(MEMORY_MANAGEMENT, STATUS_UNSUCCESSFUL, SwapEntry, (ULONG_PTR)Process, (ULONG_PTR)Address);
+        }
 #ifndef NEWCC
-      Status = CcRosUnmapVacb(SharedCacheMap, FileOffset, FALSE);
+        Status = CcRosUnmapVacb(SharedCacheMap, FileOffset, FALSE);
 #else
-      Status = STATUS_SUCCESS;
+        Status = STATUS_SUCCESS;
 #endif
 #ifndef NEWCC
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("CcRosUnmapVacb failed, status = %x\n", Status);
-         KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)SharedCacheMap, (ULONG_PTR)FileOffset, (ULONG_PTR)Address);
-      }
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("CcRosUnmapVacb failed, status = %x\n", Status);
+            KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)SharedCacheMap, (ULONG_PTR)FileOffset, (ULONG_PTR)Address);
+        }
 #endif
-      MiSetPageEvent(NULL, NULL);
-      return(STATUS_SUCCESS);
-   }
-   else if (!Context.WasDirty && !DirectMapped && !Context.Private)
-   {
-      if (SwapEntry != 0)
-      {
-         DPRINT1("Found a swap entry for a non dirty, non private and not direct mapped page (address %p)\n",
-                 Address);
-         KeBugCheckEx(MEMORY_MANAGEMENT, SwapEntry, Page, (ULONG_PTR)Process, (ULONG_PTR)Address);
-      }
-      MmReleasePageMemoryConsumer(MC_USER, Page);
-      MiSetPageEvent(NULL, NULL);
-      return(STATUS_SUCCESS);
-   }
-   else if (!Context.WasDirty && Context.Private && SwapEntry != 0)
-   {
-      DPRINT("Not dirty and private and not swapped (%p:%p)\n", Process, Address);
-      MmSetSavedSwapEntryPage(Page, 0);
-      MmLockAddressSpace(AddressSpace);
-      Status = MmCreatePageFileMapping(Process,
-                                       Address,
-                                       SwapEntry);
-      MmUnlockAddressSpace(AddressSpace);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Status %x Swapping out %p:%p\n", Status, Process, Address);
-         KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)Process, (ULONG_PTR)Address, SwapEntry);
-      }
-      MmReleasePageMemoryConsumer(MC_USER, Page);
-      MiSetPageEvent(NULL, NULL);
-      return(STATUS_SUCCESS);
-   }
-
-   /*
-    * If necessary, allocate an entry in the paging file for this page
-    */
-   if (SwapEntry == 0)
-   {
-      SwapEntry = MmAllocSwapPage();
-      if (SwapEntry == 0)
-      {
-         MmShowOutOfSpaceMessagePagingFile();
-         MmLockAddressSpace(AddressSpace);
-         /*
-          * For private pages restore the old mappings.
-          */
-         if (Context.Private)
-         {
+        MiSetPageEvent(NULL, NULL);
+        return(STATUS_SUCCESS);
+    }
+    else if (!Context.WasDirty && !DirectMapped && !Context.Private)
+    {
+        if (SwapEntry != 0)
+        {
+            DPRINT1("Found a swap entry for a non dirty, non private and not direct mapped page (address %p)\n",
+                    Address);
+            KeBugCheckEx(MEMORY_MANAGEMENT, SwapEntry, Page, (ULONG_PTR)Process, (ULONG_PTR)Address);
+        }
+        MmReleasePageMemoryConsumer(MC_USER, Page);
+        MiSetPageEvent(NULL, NULL);
+        return(STATUS_SUCCESS);
+    }
+    else if (!Context.WasDirty && Context.Private && SwapEntry != 0)
+    {
+        DPRINT("Not dirty and private and not swapped (%p:%p)\n", Process, Address);
+        MmSetSavedSwapEntryPage(Page, 0);
+        MmLockAddressSpace(AddressSpace);
+        Status = MmCreatePageFileMapping(Process,
+                                         Address,
+                                         SwapEntry);
+        MmUnlockAddressSpace(AddressSpace);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("Status %x Swapping out %p:%p\n", Status, Process, Address);
+            KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)Process, (ULONG_PTR)Address, SwapEntry);
+        }
+        MmReleasePageMemoryConsumer(MC_USER, Page);
+        MiSetPageEvent(NULL, NULL);
+        return(STATUS_SUCCESS);
+    }
+
+    /*
+     * If necessary, allocate an entry in the paging file for this page
+     */
+    if (SwapEntry == 0)
+    {
+        SwapEntry = MmAllocSwapPage();
+        if (SwapEntry == 0)
+        {
+            MmShowOutOfSpaceMessagePagingFile();
+            MmLockAddressSpace(AddressSpace);
+            /*
+             * For private pages restore the old mappings.
+             */
+            if (Context.Private)
+            {
+                Status = MmCreateVirtualMapping(Process,
+                                                Address,
+                                                MemoryArea->Protect,
+                                                &Page,
+                                                1);
+                MmSetDirtyPage(Process, Address);
+                MmInsertRmap(Page,
+                             Process,
+                             Address);
+            }
+            else
+            {
+                ULONG_PTR OldEntry;
+                /*
+                 * For non-private pages if the page wasn't direct mapped then
+                 * set it back into the section segment entry so we don't loose
+                 * our copy. Otherwise it will be handled by the cache manager.
+                 */
+                Status = MmCreateVirtualMapping(Process,
+                                                Address,
+                                                MemoryArea->Protect,
+                                                &Page,
+                                                1);
+                MmSetDirtyPage(Process, Address);
+                MmInsertRmap(Page,
+                             Process,
+                             Address);
+                // If we got here, the previous entry should have been a wait
+                Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
+                MmLockSectionSegment(Context.Segment);
+                OldEntry = MmGetPageEntrySectionSegment(Context.Segment, &Context.Offset);
+                ASSERT(OldEntry == 0 || OldEntry == MAKE_SWAP_SSE(MM_WAIT_ENTRY));
+                MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
+                MmUnlockSectionSegment(Context.Segment);
+            }
+            MmUnlockAddressSpace(AddressSpace);
+            MiSetPageEvent(NULL, NULL);
+            return(STATUS_PAGEFILE_QUOTA);
+        }
+    }
+
+    /*
+     * Write the page to the pagefile
+     */
+    Status = MmWriteToSwapPage(SwapEntry, Page);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
+                Status);
+        /*
+         * As above: undo our actions.
+         * FIXME: Also free the swap page.
+         */
+        MmLockAddressSpace(AddressSpace);
+        if (Context.Private)
+        {
             Status = MmCreateVirtualMapping(Process,
                                             Address,
                                             MemoryArea->Protect,
@@ -2142,15 +2200,9 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
             MmInsertRmap(Page,
                          Process,
                          Address);
-         }
-         else
-         {
-            ULONG_PTR OldEntry;
-            /*
-             * For non-private pages if the page wasn't direct mapped then
-             * set it back into the section segment entry so we don't loose
-             * our copy. Otherwise it will be handled by the cache manager.
-             */
+        }
+        else
+        {
             Status = MmCreateVirtualMapping(Process,
                                             Address,
                                             MemoryArea->Protect,
@@ -2160,111 +2212,61 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
             MmInsertRmap(Page,
                          Process,
                          Address);
-            // If we got here, the previous entry should have been a wait
             Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
-            MmLockSectionSegment(Context.Segment);
-            OldEntry = MmGetPageEntrySectionSegment(Context.Segment, &Context.Offset);
-            ASSERT(OldEntry == 0 || OldEntry == MAKE_SWAP_SSE(MM_WAIT_ENTRY));
             MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
-            MmUnlockSectionSegment(Context.Segment);
-         }
-         MmUnlockAddressSpace(AddressSpace);
-         MiSetPageEvent(NULL, NULL);
-         return(STATUS_PAGEFILE_QUOTA);
-      }
-   }
-
-   /*
-    * Write the page to the pagefile
-    */
-   Status = MmWriteToSwapPage(SwapEntry, Page);
-   if (!NT_SUCCESS(Status))
-   {
-      DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
-              Status);
-      /*
-       * As above: undo our actions.
-       * FIXME: Also free the swap page.
-       */
-      MmLockAddressSpace(AddressSpace);
-      if (Context.Private)
-      {
-         Status = MmCreateVirtualMapping(Process,
-                                         Address,
-                                         MemoryArea->Protect,
-                                         &Page,
-                                         1);
-         MmSetDirtyPage(Process, Address);
-         MmInsertRmap(Page,
-                      Process,
-                      Address);
-      }
-      else
-      {
-         Status = MmCreateVirtualMapping(Process,
+        }
+        MmUnlockAddressSpace(AddressSpace);
+        MiSetPageEvent(NULL, NULL);
+        return(STATUS_UNSUCCESSFUL);
+    }
+
+    /*
+     * Otherwise we have succeeded.
+     */
+    DPRINT("MM: Wrote section page 0x%.8X to swap!\n", Page << PAGE_SHIFT);
+    MmSetSavedSwapEntryPage(Page, 0);
+    if (Context.Segment->Flags & MM_PAGEFILE_SEGMENT ||
+            Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)
+    {
+        MmLockSectionSegment(Context.Segment);
+        MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, MAKE_SWAP_SSE(SwapEntry));
+        MmUnlockSectionSegment(Context.Segment);
+    }
+    else
+    {
+        MmReleasePageMemoryConsumer(MC_USER, Page);
+    }
+
+    if (Context.Private)
+    {
+        MmLockAddressSpace(AddressSpace);
+        MmLockSectionSegment(Context.Segment);
+        Status = MmCreatePageFileMapping(Process,
                                          Address,
-                                         MemoryArea->Protect,
-                                         &Page,
-                                         1);
-         MmSetDirtyPage(Process, Address);
-         MmInsertRmap(Page,
-                      Process,
-                      Address);
-         Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
-         MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
-      }
-      MmUnlockAddressSpace(AddressSpace);
-      MiSetPageEvent(NULL, NULL);
-      return(STATUS_UNSUCCESSFUL);
-   }
-
-   /*
-    * Otherwise we have succeeded.
-    */
-   DPRINT("MM: Wrote section page 0x%.8X to swap!\n", Page << PAGE_SHIFT);
-   MmSetSavedSwapEntryPage(Page, 0);
-   if (Context.Segment->Flags & MM_PAGEFILE_SEGMENT ||
-         Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)
-   {
-      MmLockSectionSegment(Context.Segment);
-      MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, MAKE_SWAP_SSE(SwapEntry));
-      MmUnlockSectionSegment(Context.Segment);
-   }
-   else
-   {
-      MmReleasePageMemoryConsumer(MC_USER, Page);
-   }
-
-   if (Context.Private)
-   {
-      MmLockAddressSpace(AddressSpace);
-      MmLockSectionSegment(Context.Segment);
-      Status = MmCreatePageFileMapping(Process,
-                                       Address,
-                                       SwapEntry);
-      /* We had placed a wait entry upon entry ... replace it before leaving */
-      MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
-      MmUnlockSectionSegment(Context.Segment);
-      MmUnlockAddressSpace(AddressSpace);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Status %x Creating page file mapping for %p:%p\n", Status, Process, Address);
-         KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)Process, (ULONG_PTR)Address, SwapEntry);
-      }
-   }
-   else
-   {
-      MmLockAddressSpace(AddressSpace);
-      MmLockSectionSegment(Context.Segment);
-      Entry = MAKE_SWAP_SSE(SwapEntry);
-      /* We had placed a wait entry upon entry ... replace it before leaving */
-      MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
-      MmUnlockSectionSegment(Context.Segment);
-      MmUnlockAddressSpace(AddressSpace);
-   }
-
-   MiSetPageEvent(NULL, NULL);
-   return(STATUS_SUCCESS);
+                                         SwapEntry);
+        /* We had placed a wait entry upon entry ... replace it before leaving */
+        MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
+        MmUnlockSectionSegment(Context.Segment);
+        MmUnlockAddressSpace(AddressSpace);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("Status %x Creating page file mapping for %p:%p\n", Status, Process, Address);
+            KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)Process, (ULONG_PTR)Address, SwapEntry);
+        }
+    }
+    else
+    {
+        MmLockAddressSpace(AddressSpace);
+        MmLockSectionSegment(Context.Segment);
+        Entry = MAKE_SWAP_SSE(SwapEntry);
+        /* We had placed a wait entry upon entry ... replace it before leaving */
+        MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
+        MmUnlockSectionSegment(Context.Segment);
+        MmUnlockAddressSpace(AddressSpace);
+    }
+
+    MiSetPageEvent(NULL, NULL);
+    return(STATUS_SUCCESS);
 }
 
 NTSTATUS
@@ -2274,148 +2276,148 @@ MmWritePageSectionView(PMMSUPPORT AddressSpace,
                        PVOID Address,
                        ULONG PageEntry)
 {
-   LARGE_INTEGER Offset;
-   PROS_SECTION_OBJECT Section;
-   PMM_SECTION_SEGMENT Segment;
-   PFN_NUMBER Page;
-   SWAPENTRY SwapEntry;
-   ULONG_PTR Entry;
-   BOOLEAN Private;
-   NTSTATUS Status;
-   PFILE_OBJECT FileObject;
-   PROS_SHARED_CACHE_MAP SharedCacheMap = NULL;
-   BOOLEAN DirectMapped;
-   BOOLEAN IsImageSection;
-   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
-
-   Address = (PVOID)PAGE_ROUND_DOWN(Address);
-
-   Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
-            + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
-
-   /*
-    * Get the segment and section.
-    */
-   Segment = MemoryArea->Data.SectionData.Segment;
-   Section = MemoryArea->Data.SectionData.Section;
-   IsImageSection = Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
-
-   FileObject = Section->FileObject;
-   DirectMapped = FALSE;
-   if (FileObject != NULL &&
-         !(Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
-   {
-      SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
-
-      /*
-       * If the file system is letting us go directly to the cache and the
-       * memory area was mapped at an offset in the file which is page aligned
-       * then note this is a direct mapped page.
-       */
-      if (((Offset.QuadPart + Segment->Image.FileOffset) % PAGE_SIZE) == 0 &&
-            (Offset.QuadPart + PAGE_SIZE <= Segment->RawLength.QuadPart || !IsImageSection))
-      {
-         DirectMapped = TRUE;
-      }
-   }
-
-   /*
-    * This should never happen since mappings of physical memory are never
-    * placed in the rmap lists.
-    */
-   if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
-   {
-      DPRINT1("Trying to write back page from physical memory mapped at %p "
-              "process %p\n", Address,
-              Process ? Process->UniqueProcessId : 0);
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-
-   /*
-    * Get the section segment entry and the physical address.
-    */
-   Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
-   if (!MmIsPagePresent(Process, Address))
-   {
-      DPRINT1("Trying to page out not-present page at (%p,0x%p).\n",
-              Process ? Process->UniqueProcessId : 0, Address);
-      KeBugCheck(MEMORY_MANAGEMENT);
-   }
-   Page = MmGetPfnForProcess(Process, Address);
-   SwapEntry = MmGetSavedSwapEntryPage(Page);
-
-   /*
-    * Check for a private (COWed) page.
-    */
-   if (Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
-         IS_SWAP_FROM_SSE(Entry) ||
-         PFN_FROM_SSE(Entry) != Page)
-   {
-      Private = TRUE;
-   }
-   else
-   {
-      Private = FALSE;
-   }
-
-   /*
-    * Speculatively set all mappings of the page to clean.
-    */
-   MmSetCleanAllRmaps(Page);
-
-   /*
-    * If this page was direct mapped from the cache then the cache manager
-    * will take care of writing it back to disk.
-    */
-   if (DirectMapped && !Private)
-   {
-      //LARGE_INTEGER SOffset;
-      ASSERT(SwapEntry == 0);
-      //SOffset.QuadPart = Offset.QuadPart + Segment->Image.FileOffset;
+    LARGE_INTEGER Offset;
+    PROS_SECTION_OBJECT Section;
+    PMM_SECTION_SEGMENT Segment;
+    PFN_NUMBER Page;
+    SWAPENTRY SwapEntry;
+    ULONG_PTR Entry;
+    BOOLEAN Private;
+    NTSTATUS Status;
+    PFILE_OBJECT FileObject;
+    PROS_SHARED_CACHE_MAP SharedCacheMap = NULL;
+    BOOLEAN DirectMapped;
+    BOOLEAN IsImageSection;
+    PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+
+    Address = (PVOID)PAGE_ROUND_DOWN(Address);
+
+    Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
+                      + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
+
+    /*
+     * Get the segment and section.
+     */
+    Segment = MemoryArea->Data.SectionData.Segment;
+    Section = MemoryArea->Data.SectionData.Section;
+    IsImageSection = Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
+
+    FileObject = Section->FileObject;
+    DirectMapped = FALSE;
+    if (FileObject != NULL &&
+            !(Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
+    {
+        SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
+
+        /*
+         * If the file system is letting us go directly to the cache and the
+         * memory area was mapped at an offset in the file which is page aligned
+         * then note this is a direct mapped page.
+         */
+        if (((Offset.QuadPart + Segment->Image.FileOffset) % PAGE_SIZE) == 0 &&
+                (Offset.QuadPart + PAGE_SIZE <= Segment->RawLength.QuadPart || !IsImageSection))
+        {
+            DirectMapped = TRUE;
+        }
+    }
+
+    /*
+     * This should never happen since mappings of physical memory are never
+     * placed in the rmap lists.
+     */
+    if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
+    {
+        DPRINT1("Trying to write back page from physical memory mapped at %p "
+                "process %p\n", Address,
+                Process ? Process->UniqueProcessId : 0);
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+
+    /*
+     * Get the section segment entry and the physical address.
+     */
+    Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
+    if (!MmIsPagePresent(Process, Address))
+    {
+        DPRINT1("Trying to page out not-present page at (%p,0x%p).\n",
+                Process ? Process->UniqueProcessId : 0, Address);
+        KeBugCheck(MEMORY_MANAGEMENT);
+    }
+    Page = MmGetPfnForProcess(Process, Address);
+    SwapEntry = MmGetSavedSwapEntryPage(Page);
+
+    /*
+     * Check for a private (COWed) page.
+     */
+    if (Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
+            IS_SWAP_FROM_SSE(Entry) ||
+            PFN_FROM_SSE(Entry) != Page)
+    {
+        Private = TRUE;
+    }
+    else
+    {
+        Private = FALSE;
+    }
+
+    /*
+     * Speculatively set all mappings of the page to clean.
+     */
+    MmSetCleanAllRmaps(Page);
+
+    /*
+     * If this page was direct mapped from the cache then the cache manager
+     * will take care of writing it back to disk.
+     */
+    if (DirectMapped && !Private)
+    {
+        //LARGE_INTEGER SOffset;
+        ASSERT(SwapEntry == 0);
+        //SOffset.QuadPart = Offset.QuadPart + Segment->Image.FileOffset;
 #ifndef NEWCC
-      CcRosMarkDirtyVacb(SharedCacheMap, Offset.QuadPart);
+        CcRosMarkDirtyVacb(SharedCacheMap, Offset.QuadPart);
 #endif
-      MmLockSectionSegment(Segment);
-      MmSetPageEntrySectionSegment(Segment, &Offset, PageEntry);
-      MmUnlockSectionSegment(Segment);
-      MiSetPageEvent(NULL, NULL);
-      return(STATUS_SUCCESS);
-   }
-
-   /*
-    * If necessary, allocate an entry in the paging file for this page
-    */
-   if (SwapEntry == 0)
-   {
-      SwapEntry = MmAllocSwapPage();
-      if (SwapEntry == 0)
-      {
-         MmSetDirtyAllRmaps(Page);
-         MiSetPageEvent(NULL, NULL);
-         return(STATUS_PAGEFILE_QUOTA);
-      }
-      MmSetSavedSwapEntryPage(Page, SwapEntry);
-   }
-
-   /*
-    * Write the page to the pagefile
-    */
-   Status = MmWriteToSwapPage(SwapEntry, Page);
-   if (!NT_SUCCESS(Status))
-   {
-      DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
-              Status);
-      MmSetDirtyAllRmaps(Page);
-      MiSetPageEvent(NULL, NULL);
-      return(STATUS_UNSUCCESSFUL);
-   }
-
-   /*
-    * Otherwise we have succeeded.
-    */
-   DPRINT("MM: Wrote section page 0x%.8X to swap!\n", Page << PAGE_SHIFT);
-   MiSetPageEvent(NULL, NULL);
-   return(STATUS_SUCCESS);
+        MmLockSectionSegment(Segment);
+        MmSetPageEntrySectionSegment(Segment, &Offset, PageEntry);
+        MmUnlockSectionSegment(Segment);
+        MiSetPageEvent(NULL, NULL);
+        return(STATUS_SUCCESS);
+    }
+
+    /*
+     * If necessary, allocate an entry in the paging file for this page
+     */
+    if (SwapEntry == 0)
+    {
+        SwapEntry = MmAllocSwapPage();
+        if (SwapEntry == 0)
+        {
+            MmSetDirtyAllRmaps(Page);
+            MiSetPageEvent(NULL, NULL);
+            return(STATUS_PAGEFILE_QUOTA);
+        }
+        MmSetSavedSwapEntryPage(Page, SwapEntry);
+    }
+
+    /*
+     * Write the page to the pagefile
+     */
+    Status = MmWriteToSwapPage(SwapEntry, Page);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
+                Status);
+        MmSetDirtyAllRmaps(Page);
+        MiSetPageEvent(NULL, NULL);
+        return(STATUS_UNSUCCESSFUL);
+    }
+
+    /*
+     * Otherwise we have succeeded.
+     */
+    DPRINT("MM: Wrote section page 0x%.8X to swap!\n", Page << PAGE_SHIFT);
+    MiSetPageEvent(NULL, NULL);
+    return(STATUS_SUCCESS);
 }
 
 static VOID
@@ -2427,76 +2429,78 @@ MmAlterViewAttributes(PMMSUPPORT AddressSpace,
                       ULONG NewType,
                       ULONG NewProtect)
 {
-   PMEMORY_AREA MemoryArea;
-   PMM_SECTION_SEGMENT Segment;
-   BOOLEAN DoCOW = FALSE;
-   ULONG i;
-   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
-
-   MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, BaseAddress);
-   ASSERT(MemoryArea != NULL);
-   Segment = MemoryArea->Data.SectionData.Segment;
-   MmLockSectionSegment(Segment);
-
-   if ((Segment->WriteCopy) &&
-         (NewProtect == PAGE_READWRITE || NewProtect == PAGE_EXECUTE_READWRITE))
-   {
-      DoCOW = TRUE;
-   }
-
-   if (OldProtect != NewProtect)
-   {
-      for (i = 0; i < PAGE_ROUND_UP(RegionSize) / PAGE_SIZE; i++)
-      {
-         SWAPENTRY SwapEntry;
-         PVOID Address = (char*)BaseAddress + (i * PAGE_SIZE);
-         ULONG Protect = NewProtect;
-
-         /* Wait for a wait entry to disappear */
-         do {
-             MmGetPageFileMapping(Process, Address, &SwapEntry);
-             if (SwapEntry != MM_WAIT_ENTRY)
-                 break;
-             MiWaitForPageEvent(Process, Address);
-         } while (TRUE);
-
-         /*
-          * If we doing COW for this segment then check if the page is
-          * already private.
-          */
-         if (DoCOW && MmIsPagePresent(Process, Address))
-         {
-            LARGE_INTEGER Offset;
-            ULONG_PTR Entry;
-            PFN_NUMBER Page;
-
-            Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
-                     + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
-            Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
+    PMEMORY_AREA MemoryArea;
+    PMM_SECTION_SEGMENT Segment;
+    BOOLEAN DoCOW = FALSE;
+    ULONG i;
+    PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+
+    MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, BaseAddress);
+    ASSERT(MemoryArea != NULL);
+    Segment = MemoryArea->Data.SectionData.Segment;
+    MmLockSectionSegment(Segment);
+
+    if ((Segment->WriteCopy) &&
+            (NewProtect == PAGE_READWRITE || NewProtect == PAGE_EXECUTE_READWRITE))
+    {
+        DoCOW = TRUE;
+    }
+
+    if (OldProtect != NewProtect)
+    {
+        for (i = 0; i < PAGE_ROUND_UP(RegionSize) / PAGE_SIZE; i++)
+        {
+            SWAPENTRY SwapEntry;
+            PVOID Address = (char*)BaseAddress + (i * PAGE_SIZE);
+            ULONG Protect = NewProtect;
+
+            /* Wait for a wait entry to disappear */
+            do
+            {
+                MmGetPageFileMapping(Process, Address, &SwapEntry);
+                if (SwapEntry != MM_WAIT_ENTRY)
+                    break;
+                MiWaitForPageEvent(Process, Address);
+            }
+            while (TRUE);
+
             /*
-             * An MM_WAIT_ENTRY is ok in this case...  It'll just count as
-             * IS_SWAP_FROM_SSE and we'll do the right thing.
+             * If we doing COW for this segment then check if the page is
+             * already private.
              */
-            Page = MmGetPfnForProcess(Process, Address);
-
-            Protect = PAGE_READONLY;
-            if (Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
-                  IS_SWAP_FROM_SSE(Entry) ||
-                  PFN_FROM_SSE(Entry) != Page)
+            if (DoCOW && MmIsPagePresent(Process, Address))
             {
-               Protect = NewProtect;
+                LARGE_INTEGER Offset;
+                ULONG_PTR Entry;
+                PFN_NUMBER Page;
+
+                Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
+                                  + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
+                Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
+                /*
+                 * An MM_WAIT_ENTRY is ok in this case...  It'll just count as
+                 * IS_SWAP_FROM_SSE and we'll do the right thing.
+                 */
+                Page = MmGetPfnForProcess(Process, Address);
+
+                Protect = PAGE_READONLY;
+                if (Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
+                        IS_SWAP_FROM_SSE(Entry) ||
+                        PFN_FROM_SSE(Entry) != Page)
+                {
+                    Protect = NewProtect;
+                }
             }
-         }
 
-         if (MmIsPagePresent(Process, Address) || MmIsDisabledPage(Process, Address))
-         {
-            MmSetPageProtect(Process, Address,
-                             Protect);
-         }
-      }
-   }
+            if (MmIsPagePresent(Process, Address) || MmIsDisabledPage(Process, Address))
+            {
+                MmSetPageProtect(Process, Address,
+                                 Protect);
+            }
+        }
+    }
 
-   MmUnlockSectionSegment(Segment);
+    MmUnlockSectionSegment(Segment);
 }
 
 NTSTATUS
@@ -2508,32 +2512,32 @@ MmProtectSectionView(PMMSUPPORT AddressSpace,
                      ULONG Protect,
                      PULONG OldProtect)
 {
-   PMM_REGION Region;
-   NTSTATUS Status;
-   ULONG_PTR MaxLength;
-
-   MaxLength = (ULONG_PTR)MemoryArea->EndingAddress - (ULONG_PTR)BaseAddress;
-   if (Length > MaxLength)
-      Length = (ULONG)MaxLength;
-
-   Region = MmFindRegion(MemoryArea->StartingAddress,
-                         &MemoryArea->Data.SectionData.RegionListHead,
-                         BaseAddress, NULL);
-   ASSERT(Region != NULL);
-
-   if ((MemoryArea->Flags & SEC_NO_CHANGE) &&
-       Region->Protect != Protect)
-   {
-      return STATUS_INVALID_PAGE_PROTECTION;
-   }
-
-   *OldProtect = Region->Protect;
-   Status = MmAlterRegion(AddressSpace, MemoryArea->StartingAddress,
+    PMM_REGION Region;
+    NTSTATUS Status;
+    ULONG_PTR MaxLength;
+
+    MaxLength = (ULONG_PTR)MemoryArea->EndingAddress - (ULONG_PTR)BaseAddress;
+    if (Length > MaxLength)
+        Length = (ULONG)MaxLength;
+
+    Region = MmFindRegion(MemoryArea->StartingAddress,
                           &MemoryArea->Data.SectionData.RegionListHead,
-                          BaseAddress, Length, Region->Type, Protect,
-                          MmAlterViewAttributes);
+                          BaseAddress, NULL);
+    ASSERT(Region != NULL);
+
+    if ((MemoryArea->Flags & SEC_NO_CHANGE) &&
+            Region->Protect != Protect)
+    {
+        return STATUS_INVALID_PAGE_PROTECTION;
+    }
+
+    *OldProtect = Region->Protect;
+    Status = MmAlterRegion(AddressSpace, MemoryArea->StartingAddress,
+                           &MemoryArea->Data.SectionData.RegionListHead,
+             &nbs