[RSHELL]
[reactos.git] / ntoskrnl / mm / marea.c
index a621abf..759be7a 100644 (file)
 
 #include <ntoskrnl.h>
 #define NDEBUG
+#include "../cache/section/newmm.h"
 #include <debug.h>
 
+#include "ARM3/miarm.h"
+
 MEMORY_AREA MiStaticMemoryAreas[MI_STATIC_MEMORY_AREAS];
 ULONG MiStaticMemoryAreaCount;
 
@@ -370,13 +373,15 @@ MmInsertMemoryArea(
    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))
    {
-       ASSERT(marea->Type == MEMORY_AREA_VIRTUAL_MEMORY || marea->Type == MEMORY_AREA_SECTION_VIEW);
        PMMVAD Vad;
-       Vad = ExAllocatePoolWithTag(NonPagedPool, sizeof(MMVAD), 'Fake');
+
+       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;
@@ -396,7 +401,9 @@ MmInsertMemoryArea(
        Vad->u.VadFlags.Spare = 1;
        Vad->u.VadFlags.PrivateMemory = 1;
        Vad->u.VadFlags.Protection = MiMakeProtectionMask(marea->Protect);
-       MiInsertVad(Vad, MmGetAddressSpaceOwner(AddressSpace));
+
+       /* Insert the VAD */
+       MiInsertVad(Vad, Process);
        marea->Vad = Vad;
    }
    else
@@ -455,78 +462,68 @@ MmFindGapBottomUp(
    ULONG_PTR Length,
    ULONG_PTR Granularity)
 {
-   PVOID LowestAddress  = MmGetAddressSpaceOwner(AddressSpace) ? MM_LOWEST_USER_ADDRESS : MmSystemRangeStart;
-   PVOID HighestAddress = MmGetAddressSpaceOwner(AddressSpace) ?
-                          (PVOID)((ULONG_PTR)MmSystemRangeStart - 1) : (PVOID)MAXULONG_PTR;
-   PVOID AlignedAddress;
-   PMEMORY_AREA Node;
-   PMEMORY_AREA FirstNode;
-   PMEMORY_AREA PreviousNode;
+    ULONG_PTR LowestAddress, HighestAddress, Candidate;
+    PMEMORY_AREA Root, Node;
 
-   DPRINT("LowestAddress: %p HighestAddress: %p\n",
-          LowestAddress, HighestAddress);
+    /* Get the margins of the address space */
+    if (MmGetAddressSpaceOwner(AddressSpace) != NULL)
+    {
+        LowestAddress = (ULONG_PTR)MM_LOWEST_USER_ADDRESS;
+        HighestAddress = (ULONG_PTR)MmHighestUserAddress;
+    }
+    else
+    {
+        LowestAddress = (ULONG_PTR)MmSystemRangeStart;
+        HighestAddress = MAXULONG_PTR;
+    }
 
-   AlignedAddress = MM_ROUND_UP(LowestAddress, Granularity);
+    /* Start with the lowest address */
+    Candidate = LowestAddress;
 
-   /* Special case for empty tree. */
-   if (AddressSpace->WorkingSetExpansionLinks.Flink == NULL)
-   {
-      if ((ULONG_PTR)HighestAddress - (ULONG_PTR)AlignedAddress >= Length)
-      {
-         DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress);
-         return AlignedAddress;
-      }
-      DPRINT("MmFindGapBottomUp: 0\n");
-      return 0;
-   }
+    /* Check for overflow */
+    if ((Candidate + Length) < Candidate) return NULL;
 
-   /* Go to the node with lowest address in the tree. */
-   FirstNode = Node = MmIterateFirstNode((PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink);
+    /* Get the root of the address space tree */
+    Root = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
 
-   /* Traverse the tree from left to right. */
-   PreviousNode = Node;
-   for (;;)
-   {
-      Node = MmIterateNextNode(Node);
-      if (Node == NULL)
-         break;
+    /* Go to the node with lowest address in the tree. */
+    Node = Root ? MmIterateFirstNode(Root) : NULL;
+    while (Node && ((ULONG_PTR)Node->EndingAddress < LowestAddress))
+    {
+        Node = MmIterateNextNode(Node);
+    }
 
-      AlignedAddress = MM_ROUND_UP(PreviousNode->EndingAddress, Granularity);
-      if (AlignedAddress >= LowestAddress)
-      {
-          if (Node->StartingAddress > AlignedAddress &&
-              (ULONG_PTR)Node->StartingAddress - (ULONG_PTR)AlignedAddress >= Length)
-          {
-             DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress);
-             ASSERT(AlignedAddress >= LowestAddress);
-             return AlignedAddress;
-          }
-      }
-      PreviousNode = Node;
-   }
+    /* Traverse the tree from low to high addresses */
+    while (Node && ((ULONG_PTR)Node->EndingAddress < HighestAddress))
+    {
+        /* Check if the memory area fits before the current node */
+        if ((ULONG_PTR)Node->StartingAddress >= (Candidate + Length))
+        {
+            DPRINT("MmFindGapBottomUp: %p\n", Candidate);
+            ASSERT(Candidate >= LowestAddress);
+            return (PVOID)Candidate;
+        }
 
-   /* Check if there is enough space after the last memory area. */
-   AlignedAddress = MM_ROUND_UP(PreviousNode->EndingAddress, Granularity);
-   if ((ULONG_PTR)HighestAddress > (ULONG_PTR)AlignedAddress &&
-       (ULONG_PTR)HighestAddress - (ULONG_PTR)AlignedAddress >= Length)
-   {
-      DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress);
-      ASSERT(AlignedAddress >= LowestAddress);
-      return AlignedAddress;
-   }
+        /* Calculate next possible adress above this node */
+        Candidate = ALIGN_UP_BY((ULONG_PTR)Node->EndingAddress, Granularity);
 
-   /* Check if there is enough space before the first memory area. */
-   AlignedAddress = MM_ROUND_UP(LowestAddress, Granularity);
-   if (FirstNode->StartingAddress > AlignedAddress &&
-       (ULONG_PTR)FirstNode->StartingAddress - (ULONG_PTR)AlignedAddress >= Length)
-   {
-      DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress);
-      ASSERT(AlignedAddress >= LowestAddress);
-      return AlignedAddress;
-   }
+        /* Check for overflow */
+        if ((Candidate + Length) < (ULONG_PTR)Node->EndingAddress) return NULL;
+
+        /* Go to the next higher node */
+        Node = MmIterateNextNode(Node);
+    }
 
-   DPRINT("MmFindGapBottomUp: 0\n");
-   return 0;
+    /* Check if there is enough space after the last memory area. */
+    if ((Candidate + Length) <= HighestAddress)
+    {
+        DPRINT("MmFindGapBottomUp: %p\n", Candidate);
+        ASSERT(Candidate >= LowestAddress);
+        return (PVOID)Candidate;
+    }
+
+    DPRINT("MmFindGapBottomUp: 0\n");
+    return NULL;
 }
 
 
@@ -536,81 +533,68 @@ MmFindGapTopDown(
    ULONG_PTR Length,
    ULONG_PTR Granularity)
 {
-   PVOID LowestAddress  = MmGetAddressSpaceOwner(AddressSpace) ? MM_LOWEST_USER_ADDRESS : MmSystemRangeStart;
-   PVOID HighestAddress = MmGetAddressSpaceOwner(AddressSpace) ?
-                          (PVOID)((ULONG_PTR)MmSystemRangeStart - 1) : (PVOID)MAXULONG_PTR;
-   PVOID AlignedAddress;
-   PMEMORY_AREA Node;
-   PMEMORY_AREA PreviousNode;
-
-   DPRINT("LowestAddress: %p HighestAddress: %p\n",
-          LowestAddress, HighestAddress);
-
-   AlignedAddress = MM_ROUND_DOWN((ULONG_PTR)HighestAddress - Length + 1, Granularity);
-
-   /* Check for overflow. */
-   if (AlignedAddress > HighestAddress)
-      return NULL;
-
-   /* Special case for empty tree. */
-   if (AddressSpace->WorkingSetExpansionLinks.Flink == NULL)
-   {
-      if (AlignedAddress >= LowestAddress)
-      {
-         DPRINT("MmFindGapTopDown: %p\n", AlignedAddress);
-         return AlignedAddress;
-      }
-      DPRINT("MmFindGapTopDown: 0\n");
-      return 0;
-   }
+    ULONG_PTR LowestAddress, HighestAddress, Candidate;
+    PMEMORY_AREA Root, Node;
 
-   /* Go to the node with highest address in the tree. */
-   Node = MmIterateLastNode((PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink);
+    /* Get the margins of the address space */
+    if (MmGetAddressSpaceOwner(AddressSpace) != NULL)
+    {
+        LowestAddress = (ULONG_PTR)MM_LOWEST_USER_ADDRESS;
+        HighestAddress = (ULONG_PTR)MmHighestUserAddress;
+    }
+    else
+    {
+        LowestAddress = (ULONG_PTR)MmSystemRangeStart;
+        HighestAddress = MAXULONG_PTR;
+    }
 
-   /* Check if there is enough space after the last memory area. */
-   if (Node->EndingAddress <= AlignedAddress)
-   {
-      DPRINT("MmFindGapTopDown: %p\n", AlignedAddress);
-      return AlignedAddress;
-   }
+    /* Calculate the highest candidate */
+    Candidate = ALIGN_DOWN_BY(HighestAddress + 1 - Length, Granularity);
 
-   /* Traverse the tree from left to right. */
-   PreviousNode = Node;
-   for (;;)
-   {
-      Node = MmIteratePrevNode(Node);
-      if (Node == NULL)
-         break;
+    /* Check for overflow. */
+    if (Candidate > HighestAddress) return NULL;
 
-      AlignedAddress = MM_ROUND_DOWN((ULONG_PTR)PreviousNode->StartingAddress - Length + 1, Granularity);
+    /* Get the root of the address space tree */
+    Root = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
 
-      /* Check for overflow. */
-      if (AlignedAddress > PreviousNode->StartingAddress)
-         return NULL;
+    /* Go to the node with highest address in the tree. */
+    Node = Root ? MmIterateLastNode(Root) : NULL;
+    while (Node && ((ULONG_PTR)Node->StartingAddress > HighestAddress))
+    {
+        Node = MmIteratePrevNode(Node);
+    }
 
-      if (Node->EndingAddress <= AlignedAddress)
-      {
-         DPRINT("MmFindGapTopDown: %p\n", AlignedAddress);
-         return AlignedAddress;
-      }
+    /* Traverse the tree from high to low addresses */
+    while (Node && ((ULONG_PTR)Node->StartingAddress > LowestAddress))
+    {
+        /* Check if the memory area fits after the current node */
+        if ((ULONG_PTR)Node->EndingAddress <= Candidate)
+        {
+            DPRINT("MmFindGapTopDown: %p\n", Candidate);
+            return (PVOID)Candidate;
+        }
 
-      PreviousNode = Node;
-   }
+        /* Calculate next possible adress below this node */
+        Candidate = ALIGN_DOWN_BY((ULONG_PTR)Node->StartingAddress - Length,
+                                  Granularity);
 
-   AlignedAddress = MM_ROUND_DOWN((ULONG_PTR)PreviousNode->StartingAddress - Length + 1, Granularity);
+        /* Check for overflow. */
+        if (Candidate > (ULONG_PTR)Node->StartingAddress)
+            return NULL;
 
-   /* Check for overflow. */
-   if (AlignedAddress > PreviousNode->StartingAddress)
-      return NULL;
+        /* Go to the next lower node */
+        Node = MmIteratePrevNode(Node);
+    }
 
-   if (AlignedAddress >= LowestAddress)
-   {
-      DPRINT("MmFindGapTopDown: %p\n", AlignedAddress);
-      return AlignedAddress;
-   }
+    /* Check if the last candidate is inside the given range */
+    if (Candidate >= LowestAddress)
+    {
+        DPRINT("MmFindGapTopDown: %p\n", Candidate);
+        return (PVOID)Candidate;
+    }
 
-   DPRINT("MmFindGapTopDown: 0\n");
-   return 0;
+    DPRINT("MmFindGapTopDown: 0\n");
+    return NULL;
 }
 
 
@@ -692,6 +676,100 @@ NTAPI
 MiRemoveNode(IN PMMADDRESS_NODE Node,
 IN PMM_AVL_TABLE Table);
 
+#if DBG
+
+static
+VOID
+MiRosCheckMemoryAreasRecursive(
+   PMEMORY_AREA Node)
+{
+    /* Check if the allocation is ok */
+    ExpCheckPoolAllocation(Node, NonPagedPool, 'ERAM');
+
+    /* Check some fields */
+    ASSERT(Node->Magic == 'erAM');
+    ASSERT(PAGE_ALIGN(Node->StartingAddress) == Node->StartingAddress);
+    ASSERT(Node->EndingAddress != NULL);
+    ASSERT(PAGE_ALIGN(Node->EndingAddress) == Node->EndingAddress);
+    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_SECTION_VIEW) ||
+           (Node->Type == MEMORY_AREA_OWNED_BY_ARM3) ||
+           (Node->Type == (MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC)));
+
+    /* Recursively check children */
+    if (Node->LeftChild != NULL)
+        MiRosCheckMemoryAreasRecursive(Node->LeftChild);
+    if (Node->RightChild != NULL)
+        MiRosCheckMemoryAreasRecursive(Node->RightChild);
+}
+
+VOID
+NTAPI
+MiRosCheckMemoryAreas(
+   PMMSUPPORT AddressSpace)
+{
+    PMEMORY_AREA RootNode;
+    PEPROCESS AddressSpaceOwner;
+    BOOLEAN NeedReleaseLock;
+
+    NeedReleaseLock = FALSE;
+
+    /* Get the address space owner */
+    AddressSpaceOwner = CONTAINING_RECORD(AddressSpace, EPROCESS, Vm);
+
+    /* Check if we already own the address space lock */
+    if (AddressSpaceOwner->AddressCreationLock.Owner != KeGetCurrentThread())
+    {
+        /* We must own it! */
+        MmLockAddressSpace(AddressSpace);
+        NeedReleaseLock = TRUE;
+    }
+
+    /* Check all memory areas */
+    RootNode = (PMEMORY_AREA)AddressSpace->WorkingSetExpansionLinks.Flink;
+    MiRosCheckMemoryAreasRecursive(RootNode);
+
+    /* Release the lock, if we acquired it */
+    if (NeedReleaseLock)
+    {
+        MmUnlockAddressSpace(AddressSpace);
+    }
+}
+
+extern KGUARDED_MUTEX PspActiveProcessMutex;
+
+VOID
+NTAPI
+MiCheckAllProcessMemoryAreas(VOID)
+{
+    PEPROCESS Process;
+    PLIST_ENTRY Entry;
+
+    /* Acquire the Active Process Lock */
+    KeAcquireGuardedMutex(&PspActiveProcessMutex);
+
+    /* Loop the process list */
+    Entry = PsActiveProcessHead.Flink;
+    while (Entry != &PsActiveProcessHead)
+    {
+        /* Get the process */
+        Process = CONTAINING_RECORD(Entry, EPROCESS, ActiveProcessLinks);
+
+        /* Check memory areas */
+        MiRosCheckMemoryAreas(&Process->Vm);
+
+        Entry = Entry->Flink;
+    }
+
+    /* Release the lock */
+    KeReleaseGuardedMutex(&PspActiveProcessMutex);
+}
+
+#endif
+
 /**
  * @name MmFreeMemoryArea
  *
@@ -710,6 +788,12 @@ IN PMM_AVL_TABLE Table);
  *
  * @remarks Lock the address space before calling this function.
  */
+VOID
+NTAPI
+MiDeletePte(IN PMMPTE PointerPte,
+            IN PVOID VirtualAddress,
+            IN PEPROCESS CurrentProcess,
+            IN PMMPTE PrototypePte);
 
 NTSTATUS NTAPI
 MmFreeMemoryArea(
@@ -721,12 +805,18 @@ MmFreeMemoryArea(
    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());
+
+    /* 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)
        {
@@ -755,6 +845,24 @@ MmFreeMemoryArea(
                 FreePage(FreePageContext, MemoryArea, (PVOID)Address,
                          Page, SwapEntry, (BOOLEAN)Dirty);
              }
+#if (_MI_PAGING_LEVELS == 2)
+            /* Remove page table reference */
+            ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
+            if ((SwapEntry || Page) && ((PVOID)Address < MmSystemRangeStart))
+            {
+                ASSERT(AddressSpace != MmGetKernelAddressSpace());
+                if (MiQueryPageTableReferences((PVOID)Address) == 0)
+                {
+                    /* No PTE relies on this PDE. Release it */
+                    KIRQL OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+                    PMMPDE PointerPde = MiAddressToPde(Address);
+                    ASSERT(PointerPde->u.Hard.Valid == 1);
+                    MiDeletePte(PointerPde, MiPdeToPte(PointerPde), Process, NULL);
+                    ASSERT(PointerPde->u.Hard.Valid == 0);
+                    KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
+                }
+            }
+#endif
        }
 
        if (Process != NULL &&
@@ -766,16 +874,16 @@ MmFreeMemoryArea(
        if (MemoryArea->Vad)
        {
            ASSERT(MemoryArea->EndingAddress < MmSystemRangeStart);
-           ASSERT(MemoryArea->Type == MEMORY_AREA_VIRTUAL_MEMORY || MemoryArea->Type == MEMORY_AREA_SECTION_VIEW);
-           
+           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);
            }
-           
-           ExFreePool(MemoryArea->Vad);
+
+           ExFreePoolWithTag(MemoryArea->Vad, TAG_MVAD);
            MemoryArea->Vad = NULL;
        }
     }
@@ -876,23 +984,20 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
                    PMEMORY_AREA *Result,
                    BOOLEAN FixedAddress,
                    ULONG AllocationFlags,
-                   PHYSICAL_ADDRESS BoundaryAddressMultiple)
+                   ULONG Granularity)
 {
-   PVOID EndAddress;
-   ULONG Granularity;
-   ULONG tmpLength;
+   ULONG_PTR tmpLength;
    PMEMORY_AREA MemoryArea;
 
-   DPRINT("MmCreateMemoryArea(Type %d, BaseAddress %p, "
+   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);
 
-   Granularity = (MEMORY_AREA_VIRTUAL_MEMORY == Type ? MM_VIRTMEM_GRANULARITY : PAGE_SIZE);
    if ((*BaseAddress) == 0 && !FixedAddress)
    {
-      tmpLength = PAGE_ROUND_UP(Length);
+      tmpLength = (ULONG_PTR)MM_ROUND_UP(Length, Granularity);
       *BaseAddress = MmFindGap(AddressSpace,
                                tmpLength,
                                Granularity,
@@ -907,6 +1012,7 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
    {
       tmpLength = Length + ((ULONG_PTR) *BaseAddress
                          - (ULONG_PTR) MM_ROUND_DOWN(*BaseAddress, Granularity));
+      tmpLength = (ULONG_PTR)MM_ROUND_UP(tmpLength, Granularity);
       *BaseAddress = MM_ROUND_DOWN(*BaseAddress, Granularity);
 
       if (!MmGetAddressSpaceOwner(AddressSpace) && *BaseAddress < MmSystemRangeStart)
@@ -917,15 +1023,10 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
       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 (BoundaryAddressMultiple.QuadPart != 0)
-      {
-         EndAddress = ((char*)(*BaseAddress)) + tmpLength-1;
-         ASSERT(((ULONG_PTR)*BaseAddress/BoundaryAddressMultiple.QuadPart) == ((DWORD_PTR)EndAddress/BoundaryAddressMultiple.QuadPart));
-      }
-
       if (MmLocateMemoryAreaByRegion(AddressSpace,
                                      *BaseAddress,
                                      tmpLength) != NULL)
@@ -934,7 +1035,7 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
          return STATUS_CONFLICTING_ADDRESSES;
       }
    }
-    
+
     //
     // Is this a static memory area?
     //
@@ -957,7 +1058,11 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
                                            TAG_MAREA);
     }
 
-    if (!MemoryArea) return STATUS_NO_MEMORY;
+   if (!MemoryArea)
+   {
+      DPRINT1("Not enough memory.\n");
+      return STATUS_NO_MEMORY;
+   }
 
    RtlZeroMemory(MemoryArea, sizeof(MEMORY_AREA));
    MemoryArea->Type = Type;
@@ -966,7 +1071,7 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
    MemoryArea->Protect = Protect;
    MemoryArea->Flags = AllocationFlags;
    //MemoryArea->LockCount = 0;
-   MemoryArea->PageOpCount = 0;
+   MemoryArea->Magic = 'erAM';
    MemoryArea->DeleteInProgress = FALSE;
 
    MmInsertMemoryArea(AddressSpace, MemoryArea);
@@ -979,13 +1084,15 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
 
 VOID NTAPI
 MmMapMemoryArea(PVOID BaseAddress,
-                ULONG Length,
+                SIZE_T Length,
                 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;
@@ -1009,6 +1116,10 @@ MmMapMemoryArea(PVOID BaseAddress,
    }
 }
 
+VOID
+NTAPI
+MmDeleteProcessAddressSpace2(IN PEPROCESS Process);
+
 NTSTATUS
 NTAPI
 MmDeleteProcessAddressSpace(PEPROCESS Process)
@@ -1016,11 +1127,12 @@ MmDeleteProcessAddressSpace(PEPROCESS Process)
    PVOID Address;
    PMEMORY_AREA MemoryArea;
 
-   DPRINT("MmDeleteProcessAddressSpace(Process %x (%s))\n", Process,
+   DPRINT("MmDeleteProcessAddressSpace(Process %p (%s))\n", Process,
           Process->ImageFileName);
 
+#ifndef _M_AMD64
    RemoveEntryList(&Process->MmProcessLinks);
-
+#endif
    MmLockAddressSpace(&Process->Vm);
 
    while ((MemoryArea = (PMEMORY_AREA)Process->Vm.WorkingSetExpansionLinks.Flink) != NULL)
@@ -1034,8 +1146,11 @@ MmDeleteProcessAddressSpace(PEPROCESS Process)
              MmLockAddressSpace(&Process->Vm);
              break;
 
-         case MEMORY_AREA_VIRTUAL_MEMORY:
-             MmFreeVirtualMemory(Process, MemoryArea);
+         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:
@@ -1050,9 +1165,48 @@ MmDeleteProcessAddressSpace(PEPROCESS Process)
       }
    }
 
+#if (_MI_PAGING_LEVELS == 2)
+    {
+        KIRQL OldIrql;
+        PMMPDE pointerPde;
+        /* Attach to Process */
+        KeAttachProcess(&Process->Pcb);
+
+        /* Acquire PFN lock */
+        OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+
+        for (Address = MI_LOWEST_VAD_ADDRESS;
+             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)
+            {
+                DPRINT1("Process %p, Address %p, UsedPageTableEntries %lu\n",
+                        Process,
+                        Address,
+                        MiQueryPageTableReferences(Address));
+                ASSERT(MiQueryPageTableReferences(Address) == 0);
+            }
+            pointerPde = MiAddressToPde(Address);
+            /* Unlike in ARM3, we don't necesarrily free the PDE page as soon as reference reaches 0,
+             * so we must clean up a bit when process closes */
+            if (pointerPde->u.Hard.Valid)
+                MiDeletePte(pointerPde, MiPdeToPte(pointerPde), Process, NULL);
+            ASSERT(pointerPde->u.Hard.Valid == 0);
+        }
+        /* Release lock */
+        KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
+
+        /* Detach */
+        KeDetachProcess();
+    }
+#endif
+
    MmUnlockAddressSpace(&Process->Vm);
 
    DPRINT("Finished MmReleaseMmInfo()\n");
+   MmDeleteProcessAddressSpace2(Process);
    return(STATUS_SUCCESS);
 }