Sync with trunk (48237)
[reactos.git] / ntoskrnl / mm / i386 / page.c
index 1fcba4f..8945ff2 100644 (file)
@@ -11,7 +11,7 @@
 
 #include <ntoskrnl.h>
 #define NDEBUG
-#include <internal/debug.h>
+#include <debug.h>
 
 #if defined (ALLOC_PRAGMA)
 #pragma alloc_text(INIT, MmInitGlobalKernelPageDirectory)
@@ -62,42 +62,15 @@ __inline LARGE_INTEGER PTE_TO_PAGE(ULONG npage)
 
 BOOLEAN MmUnmapPageTable(PULONG Pt);
 
-ULONG_PTR
-NTAPI
-MiFlushTlbIpiRoutine(ULONG_PTR Address)
-{
-    if (Address == (ULONG_PTR)-1)
-    {
-        KeFlushCurrentTb();
-    }
-    else if (Address == (ULONG_PTR)-2)
-    {
-        KeFlushCurrentTb();
-    }
-    else
-    {
-        __invlpg((PVOID)Address);
-    }
-    return 0;
-}
-
 VOID
 MiFlushTlb(PULONG Pt, PVOID Address)
 {
     if ((Pt && MmUnmapPageTable(Pt)) || Address >= MmSystemRangeStart)
     {
-        __invlpg(Address);
+        KeInvalidateTlbEntry(Address);
     }
 }
 
-
-
-PULONG
-MmGetPageDirectory(VOID)
-{
-    return (PULONG)__readcr3();
-}
-
 static ULONG
 ProtectToPTE(ULONG flProtect)
 {
@@ -118,7 +91,7 @@ ProtectToPTE(ULONG flProtect)
     else
     {
         DPRINT1("Unknown main protection type.\n");
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     
     if (flProtect & PAGE_SYSTEM)
@@ -139,202 +112,23 @@ ProtectToPTE(ULONG flProtect)
     return(Attributes);
 }
 
-NTSTATUS
-NTAPI
-Mmi386ReleaseMmInfo(PEPROCESS Process)
-{
-    PUSHORT LdtDescriptor;
-    ULONG LdtBase;
-    PULONG PageDir;
-    ULONG i;
-    
-    DPRINT("Mmi386ReleaseMmInfo(Process %x)\n",Process);
-    
-    LdtDescriptor = (PUSHORT) &Process->Pcb.LdtDescriptor;
-    LdtBase = LdtDescriptor[1] |
-    ((LdtDescriptor[2] & 0xff) << 16) |
-    ((LdtDescriptor[3] & ~0xff) << 16);
-    
-    DPRINT("LdtBase: %x\n", LdtBase);
-    
-    if (LdtBase)
-    {
-        ExFreePool((PVOID) LdtBase);
-    }
-    
-    PageDir = MmCreateHyperspaceMapping(PTE_TO_PFN(Process->Pcb.DirectoryTableBase.u.LowPart));
-    for (i = 0; i < ADDR_TO_PDE_OFFSET(MmSystemRangeStart); i++)
-    {
-        if (PageDir[i] != 0)
-        {
-            MiZeroPage(PTE_TO_PFN(PageDir[i]));
-            MmReleasePageMemoryConsumer(MC_NPPOOL, PTE_TO_PFN(PageDir[i]));
-        }
-    }
-    MmReleasePageMemoryConsumer(MC_NPPOOL, PTE_TO_PFN(PageDir[ADDR_TO_PDE_OFFSET(HYPERSPACE)]));
-    MmDeleteHyperspaceMapping(PageDir);
-    MmReleasePageMemoryConsumer(MC_NPPOOL, PTE_TO_PFN(Process->Pcb.DirectoryTableBase.u.LowPart));
-    
-#if defined(__GNUC__)
-    
-    Process->Pcb.DirectoryTableBase.QuadPart = 0LL;
-#else
-    
-    Process->Pcb.DirectoryTableBase.QuadPart = 0;
-#endif
-    
-    DPRINT("Finished Mmi386ReleaseMmInfo()\n");
-    return(STATUS_SUCCESS);
-}
-
-NTSTATUS
-NTAPI
-MmInitializeHandBuiltProcess(IN PEPROCESS Process,
-                             IN PLARGE_INTEGER DirectoryTableBase)
-{
-    /* Share the directory base with the idle process */
-    *DirectoryTableBase = PsGetCurrentProcess()->Pcb.DirectoryTableBase;
-    
-    /* Initialize the Addresss Space */
-    KeInitializeGuardedMutex(&Process->AddressCreationLock);
-    Process->VadRoot.BalancedRoot.u1.Parent = NULL;
-    
-    /* The process now has an address space */
-    Process->HasAddressSpace = TRUE;
-    return STATUS_SUCCESS;
-}
-
-BOOLEAN
-STDCALL
-MmCreateProcessAddressSpace(IN ULONG MinWs,
-                            IN PEPROCESS Process,
-                            IN PLARGE_INTEGER DirectoryTableBase)
-{
-    NTSTATUS Status;
-    ULONG i, j;
-    PFN_TYPE Pfn[2];
-    PULONG PageDirectory;
-    
-    DPRINT("MmCopyMmInfo(Src %x, Dest %x)\n", MinWs, Process);
-    
-    for (i = 0; i < 2; i++)
-    {
-        Status = MmRequestPageMemoryConsumer(MC_NPPOOL, FALSE, &Pfn[i]);
-        if (!NT_SUCCESS(Status))
-        {
-            for (j = 0; j < i; j++)
-            {
-                MmReleasePageMemoryConsumer(MC_NPPOOL, Pfn[j]);
-            }
-            
-            return FALSE;
-        }
-    }
-    
-    PageDirectory = MmCreateHyperspaceMapping(Pfn[0]);
-    
-    memcpy(PageDirectory + ADDR_TO_PDE_OFFSET(MmSystemRangeStart),
-           MmGlobalKernelPageDirectory + ADDR_TO_PDE_OFFSET(MmSystemRangeStart),
-           (1024 - ADDR_TO_PDE_OFFSET(MmSystemRangeStart)) * sizeof(ULONG));
-    
-    DPRINT("Addr %x\n",ADDR_TO_PDE_OFFSET(PAGETABLE_MAP));
-    PageDirectory[ADDR_TO_PDE_OFFSET(PAGETABLE_MAP)] = PFN_TO_PTE(Pfn[0]) | PA_PRESENT | PA_READWRITE;
-    PageDirectory[ADDR_TO_PDE_OFFSET(HYPERSPACE)] = PFN_TO_PTE(Pfn[1]) | PA_PRESENT | PA_READWRITE;
-    
-    MmDeleteHyperspaceMapping(PageDirectory);
-    
-    DirectoryTableBase->QuadPart = PFN_TO_PTE(Pfn[0]);
-    DPRINT("Finished MmCopyMmInfo(): %I64x\n", DirectoryTableBase->QuadPart);
-    return TRUE;
-}
-
-VOID
-NTAPI
-MmDeletePageTable(PEPROCESS Process, PVOID Address)
-{
-    PEPROCESS CurrentProcess = PsGetCurrentProcess();
-    
-    if (Process != NULL && Process != CurrentProcess)
-    {
-        KeAttachProcess(&Process->Pcb);
-    }
-    
-    MiAddressToPde(Address)->u.Long = 0;
-    MiFlushTlb((PULONG)MiAddressToPde(Address),
-               MiAddressToPte(Address));
-    
-    if (Address >= MmSystemRangeStart)
-    {
-        KEBUGCHECK(0);
-        //       MmGlobalKernelPageDirectory[ADDR_TO_PDE_OFFSET(Address)] = 0;
-    }
-    if (Process != NULL && Process != CurrentProcess)
-    {
-        KeDetachProcess();
-    }
-}
-
-VOID
-NTAPI
-MmFreePageTable(PEPROCESS Process, PVOID Address)
-{
-    PEPROCESS CurrentProcess = PsGetCurrentProcess();
-    ULONG i;
-    PFN_TYPE Pfn;
-    PULONG PageTable;
-    
-    DPRINT("ProcessId %d, Address %x\n", Process->UniqueProcessId, Address);
-    if (Process != NULL && Process != CurrentProcess)
-    {
-        KeAttachProcess(&Process->Pcb);
-    }
-    
-    PageTable = (PULONG)PAGE_ROUND_DOWN((PVOID)MiAddressToPte(Address));
-    for (i = 0; i < 1024; i++)
-    {
-        if (PageTable[i] != 0)
-        {
-            DbgPrint("Page table entry not clear at %x/%x (is %x)\n",
-                     ((ULONG)Address / (4*1024*1024)), i, PageTable[i]);
-            KEBUGCHECK(0);
-        }
-    }
-    Pfn = MiAddressToPde(Address)->u.Hard.PageFrameNumber;
-    MiAddressToPde(Address)->u.Long = 0;
-    MiFlushTlb((PULONG)MiAddressToPde(Address), MiAddressToPte(Address));
-    
-    if (Address >= MmSystemRangeStart)
-    {
-        //    MmGlobalKernelPageDirectory[ADDR_TO_PDE_OFFSET(Address)] = 0;
-        KEBUGCHECK(0);
-    }
-    else
-    {
-        MmReleasePageMemoryConsumer(MC_NPPOOL, Pfn);
-    }
-    if (Process != NULL && Process != CurrentProcess)
-    {
-        KeDetachProcess();
-    }
-}
-
 static PULONG
 MmGetPageTableForProcess(PEPROCESS Process, PVOID Address, BOOLEAN Create)
 {
     ULONG PdeOffset = ADDR_TO_PDE_OFFSET(Address);
     NTSTATUS Status;
-    PFN_TYPE Pfn;
+    PFN_NUMBER Pfn;
     ULONG Entry;
     PULONG Pt, PageDir;
     
     if (Address < MmSystemRangeStart && Process && Process != PsGetCurrentProcess())
     {
-        PageDir = MmCreateHyperspaceMapping(PTE_TO_PFN(Process->Pcb.DirectoryTableBase.LowPart));
+        PageDir = MmCreateHyperspaceMapping(PTE_TO_PFN(Process->Pcb.DirectoryTableBase[0]));
         if (PageDir == NULL)
         {
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
-        if (0 == InterlockedCompareExchangeUL(&PageDir[PdeOffset], 0, 0))
+        if (0 == InterlockedCompareExchangePte(&PageDir[PdeOffset], 0, 0))
         {
             if (Create == FALSE)
             {
@@ -344,9 +138,9 @@ MmGetPageTableForProcess(PEPROCESS Process, PVOID Address, BOOLEAN Create)
             Status = MmRequestPageMemoryConsumer(MC_NPPOOL, FALSE, &Pfn);
             if (!NT_SUCCESS(Status) || Pfn == 0)
             {
-                KEBUGCHECK(0);
+                KeBugCheck(MEMORY_MANAGEMENT);
             }
-            Entry = InterlockedCompareExchangeUL(&PageDir[PdeOffset], PFN_TO_PTE(Pfn) | PA_PRESENT | PA_READWRITE | PA_USER, 0);
+            Entry = InterlockedCompareExchangePte(&PageDir[PdeOffset], PFN_TO_PTE(Pfn) | PA_PRESENT | PA_READWRITE | PA_USER, 0);
             if (Entry != 0)
             {
                 MmReleasePageMemoryConsumer(MC_NPPOOL, Pfn);
@@ -361,37 +155,36 @@ MmGetPageTableForProcess(PEPROCESS Process, PVOID Address, BOOLEAN Create)
         Pt = MmCreateHyperspaceMapping(Pfn);
         if (Pt == NULL)
         {
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
         return Pt + ADDR_TO_PTE_OFFSET(Address);
     }
     PageDir = (PULONG)MiAddressToPde(Address);
-    if (0 == InterlockedCompareExchangeUL(PageDir, 0, 0))
+    if (0 == InterlockedCompareExchangePte(PageDir, 0, 0))
     {
         if (Address >= MmSystemRangeStart)
         {
-            if (0 == InterlockedCompareExchangeUL(&MmGlobalKernelPageDirectory[PdeOffset], 0, 0))
+            if (0 == InterlockedCompareExchangePte(&MmGlobalKernelPageDirectory[PdeOffset], 0, 0))
             {
                 if (Create == FALSE)
                 {
                     return NULL;
                 }
-                Status = MmRequestPageMemoryConsumer(MC_NPPOOL, FALSE, &Pfn);
+                Status = MmRequestPageMemoryConsumer(MC_SYSTEM, FALSE, &Pfn);
                 if (!NT_SUCCESS(Status) || Pfn == 0)
                 {
-                    KEBUGCHECK(0);
+                    KeBugCheck(MEMORY_MANAGEMENT);
                 }
                 Entry = PFN_TO_PTE(Pfn) | PA_PRESENT | PA_READWRITE;
-                if (Ke386GlobalPagesEnabled)
-                {
-                    Entry |= PA_GLOBAL;
-                }
-                if(0 != InterlockedCompareExchangeUL(&MmGlobalKernelPageDirectory[PdeOffset], Entry, 0))
+                if(0 != InterlockedCompareExchangePte(&MmGlobalKernelPageDirectory[PdeOffset], Entry, 0))
                 {
-                    MmReleasePageMemoryConsumer(MC_NPPOOL, Pfn);
+                    MmReleasePageMemoryConsumer(MC_SYSTEM, Pfn);
                 }
+                InterlockedExchangePte(PageDir, MmGlobalKernelPageDirectory[PdeOffset]);
+                RtlZeroMemory(MiPteToAddress(PageDir), PAGE_SIZE);
+                return (PULONG)MiAddressToPte(Address);
             }
-            (void)InterlockedExchangeUL(PageDir, MmGlobalKernelPageDirectory[PdeOffset]);
+            InterlockedExchangePte(PageDir, MmGlobalKernelPageDirectory[PdeOffset]);
         }
         else
         {
@@ -402,9 +195,9 @@ MmGetPageTableForProcess(PEPROCESS Process, PVOID Address, BOOLEAN Create)
             Status = MmRequestPageMemoryConsumer(MC_NPPOOL, FALSE, &Pfn);
             if (!NT_SUCCESS(Status) || Pfn == 0)
             {
-                KEBUGCHECK(0);
+                KeBugCheck(MEMORY_MANAGEMENT);
             }
-            Entry = InterlockedCompareExchangeUL(PageDir, PFN_TO_PTE(Pfn) | PA_PRESENT | PA_READWRITE | PA_USER, 0);
+            Entry = InterlockedCompareExchangePte(PageDir, PFN_TO_PTE(Pfn) | PA_PRESENT | PA_READWRITE | PA_USER, 0);
             if (Entry != 0)
             {
                 MmReleasePageMemoryConsumer(MC_NPPOOL, Pfn);
@@ -443,7 +236,7 @@ static ULONG MmGetPageEntryForProcess(PEPROCESS Process, PVOID Address)
     return 0;
 }
 
-PFN_TYPE
+PFN_NUMBER
 NTAPI
 MmGetPfnForProcess(PEPROCESS Process,
                    PVOID Address)
@@ -459,7 +252,7 @@ MmGetPfnForProcess(PEPROCESS Process,
 
 VOID
 NTAPI
-MmDisableVirtualMapping(PEPROCESS Process, PVOID Address, BOOLEAN* WasDirty, PPFN_TYPE Page)
+MmDisableVirtualMapping(PEPROCESS Process, PVOID Address, BOOLEAN* WasDirty, PPFN_NUMBER Page)
 /*
  * FUNCTION: Delete a virtual mapping
  */
@@ -471,7 +264,7 @@ MmDisableVirtualMapping(PEPROCESS Process, PVOID Address, BOOLEAN* WasDirty, PPF
     Pt = MmGetPageTableForProcess(Process, Address, FALSE);
     if (Pt == NULL)
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     /*
      * Atomically disable the present bit and get the old value.
@@ -479,13 +272,13 @@ MmDisableVirtualMapping(PEPROCESS Process, PVOID Address, BOOLEAN* WasDirty, PPF
     do
     {
         Pte = *Pt;
-    } while (Pte != InterlockedCompareExchangeUL(Pt, Pte & ~PA_PRESENT, Pte));
+    } while (Pte != InterlockedCompareExchangePte(Pt, Pte & ~PA_PRESENT, Pte));
     
     MiFlushTlb(Pt, Address);
     WasValid = (PAGE_MASK(Pte) != 0);
     if (!WasValid)
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     
     /*
@@ -513,7 +306,7 @@ MmRawDeleteVirtualMapping(PVOID Address)
         /*
          * Set the entry to zero
          */
-        (void)InterlockedExchangeUL(Pt, 0);
+        InterlockedExchangePte(Pt, 0);
         MiFlushTlb(Pt, Address);
     }
 }
@@ -521,13 +314,13 @@ MmRawDeleteVirtualMapping(PVOID Address)
 VOID
 NTAPI
 MmDeleteVirtualMapping(PEPROCESS Process, PVOID Address, BOOLEAN FreePage,
-                       BOOLEAN* WasDirty, PPFN_TYPE Page)
+                       BOOLEAN* WasDirty, PPFN_NUMBER Page)
 /*
  * FUNCTION: Delete a virtual mapping
  */
 {
     BOOLEAN WasValid = FALSE;
-    PFN_TYPE Pfn;
+    PFN_NUMBER Pfn;
     ULONG Pte;
     PULONG Pt;
     
@@ -552,7 +345,7 @@ MmDeleteVirtualMapping(PEPROCESS Process, PVOID Address, BOOLEAN FreePage,
     /*
      * Atomically set the entry to zero and get the old value.
      */
-    Pte = InterlockedExchangeUL(Pt, 0);
+    Pte = InterlockedExchangePte(Pt, 0);
     
     MiFlushTlb(Pt, Address);
     
@@ -560,7 +353,6 @@ MmDeleteVirtualMapping(PEPROCESS Process, PVOID Address, BOOLEAN FreePage,
     if (WasValid)
     {
         Pfn = PTE_TO_PFN(Pte);
-        MmMarkPageUnmapped(Pfn);
     }
     else
     {
@@ -607,7 +399,7 @@ MmDeletePageFileMapping(PEPROCESS Process, PVOID Address,
     /*
      * Atomically set the entry to zero and get the old value.
      */
-    Pte = InterlockedExchangeUL(Pt, 0);
+    Pte = InterlockedExchangePte(Pt, 0);
     
     MiFlushTlb(Pt, Address);
     
@@ -640,42 +432,6 @@ MmIsDirtyPage(PEPROCESS Process, PVOID Address)
     return MmGetPageEntryForProcess(Process, Address) & PA_DIRTY ? TRUE : FALSE;
 }
 
-BOOLEAN
-NTAPI
-MmIsAccessedAndResetAccessPage(PEPROCESS Process, PVOID Address)
-{
-    PULONG Pt;
-    ULONG Pte;
-    
-    if (Address < MmSystemRangeStart && Process == NULL)
-    {
-        DPRINT1("MmIsAccessedAndResetAccessPage is called for user space without a process.\n");
-        KEBUGCHECK(0);
-    }
-    
-    Pt = MmGetPageTableForProcess(Process, Address, FALSE);
-    if (Pt == NULL)
-    {
-        KEBUGCHECK(0);
-    }
-    
-    do
-    {
-        Pte = *Pt;
-    } while (Pte != InterlockedCompareExchangeUL(Pt, Pte & ~PA_ACCESSED, Pte));
-    
-    if (Pte & PA_ACCESSED)
-    {
-        MiFlushTlb(Pt, Address);
-        return TRUE;
-    }
-    else
-    {
-        MmUnmapPageTable(Pt);
-        return FALSE;
-    }
-}
-
 VOID
 NTAPI
 MmSetCleanPage(PEPROCESS Process, PVOID Address)
@@ -686,20 +442,20 @@ MmSetCleanPage(PEPROCESS Process, PVOID Address)
     if (Address < MmSystemRangeStart && Process == NULL)
     {
         DPRINT1("MmSetCleanPage is called for user space without a process.\n");
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     
     Pt = MmGetPageTableForProcess(Process, Address, FALSE);
     
     if (Pt == NULL)
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     
     do
     {
         Pte = *Pt;
-    } while (Pte != InterlockedCompareExchangeUL(Pt, Pte & ~PA_DIRTY, Pte));
+    } while (Pte != InterlockedCompareExchangePte(Pt, Pte & ~PA_DIRTY, Pte));
     
     if (Pte & PA_DIRTY)
     {
@@ -721,19 +477,19 @@ MmSetDirtyPage(PEPROCESS Process, PVOID Address)
     if (Address < MmSystemRangeStart && Process == NULL)
     {
         DPRINT1("MmSetDirtyPage is called for user space without a process.\n");
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     
     Pt = MmGetPageTableForProcess(Process, Address, FALSE);
     if (Pt == NULL)
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     
     do
     {
         Pte = *Pt;
-    } while (Pte != InterlockedCompareExchangeUL(Pt, Pte | PA_DIRTY, Pte));
+    } while (Pte != InterlockedCompareExchangePte(Pt, Pte | PA_DIRTY, Pte));
     if (!(Pte & PA_DIRTY))
     {
         MiFlushTlb(Pt, Address);
@@ -754,13 +510,13 @@ MmEnableVirtualMapping(PEPROCESS Process, PVOID Address)
     Pt = MmGetPageTableForProcess(Process, Address, FALSE);
     if (Pt == NULL)
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     
     do
     {
         Pte = *Pt;
-    } while (Pte != InterlockedCompareExchangeUL(Pt, Pte | PA_PRESENT, Pte));
+    } while (Pte != InterlockedCompareExchangePte(Pt, Pte | PA_PRESENT, Pte));
     if (!(Pte & PA_PRESENT))
     {
         MiFlushTlb(Pt, Address);
@@ -775,7 +531,7 @@ BOOLEAN
 NTAPI
 MmIsPagePresent(PEPROCESS Process, PVOID Address)
 {
-    return MmGetPageEntryForProcess(Process, Address) & PA_PRESENT ? TRUE : FALSE;
+    return MmGetPageEntryForProcess(Process, Address) & PA_PRESENT;
 }
 
 BOOLEAN
@@ -784,88 +540,7 @@ MmIsPageSwapEntry(PEPROCESS Process, PVOID Address)
 {
     ULONG Entry;
     Entry = MmGetPageEntryForProcess(Process, Address);
-    return !(Entry & PA_PRESENT) && Entry != 0 ? TRUE : FALSE;
-}
-
-NTSTATUS
-NTAPI
-MmCreateVirtualMappingForKernel(PVOID Address,
-                                ULONG flProtect,
-                                PPFN_TYPE Pages,
-                                ULONG PageCount)
-{
-    ULONG Attributes;
-    ULONG i;
-    PVOID Addr;
-    ULONG PdeOffset, oldPdeOffset;
-    PULONG Pt;
-    ULONG Pte;
-    BOOLEAN NoExecute = FALSE;
-    
-    DPRINT("MmCreateVirtualMappingForKernel(%x, %x, %x, %d)\n",
-           Address, flProtect, Pages, PageCount);
-    
-    if (Address < MmSystemRangeStart)
-    {
-        DPRINT1("MmCreateVirtualMappingForKernel is called for user space\n");
-        KEBUGCHECK(0);
-    }
-    
-    Attributes = ProtectToPTE(flProtect);
-    if (Attributes & 0x80000000)
-    {
-        NoExecute = TRUE;
-    }
-    Attributes &= 0xfff;
-    if (Ke386GlobalPagesEnabled)
-    {
-        Attributes |= PA_GLOBAL;
-    }
-    
-    Addr = Address;
-    
-    oldPdeOffset = ADDR_TO_PDE_OFFSET(Addr);
-    Pt = MmGetPageTableForProcess(NULL, Addr, TRUE);
-    if (Pt == NULL)
-    {
-        KEBUGCHECK(0);
-    }
-    Pt--;
-    
-    for (i = 0; i < PageCount; i++, Addr = (PVOID)((ULONG_PTR)Addr + PAGE_SIZE))
-    {
-        if (!(Attributes & PA_PRESENT) && Pages[i] != 0)
-        {
-            DPRINT1("Setting physical address but not allowing access at address "
-                    "0x%.8X with attributes %x/%x.\n",
-                    Addr, Attributes, flProtect);
-            KEBUGCHECK(0);
-        }
-        
-        PdeOffset = ADDR_TO_PDE_OFFSET(Addr);
-        if (oldPdeOffset != PdeOffset)
-        {
-            Pt = MmGetPageTableForProcess(NULL, Addr, TRUE);
-            if (Pt == NULL)
-            {
-                KEBUGCHECK(0);
-            }
-        }
-        else
-        {
-            Pt++;
-        }
-        oldPdeOffset = PdeOffset;
-        
-        Pte = *Pt;
-        if (Pte != 0)
-        {
-            KEBUGCHECK(0);
-        }
-        (void)InterlockedExchangeUL(Pt, PFN_TO_PTE(Pages[i]) | Attributes);
-    }
-    
-    return(STATUS_SUCCESS);
+    return !(Entry & PA_PRESENT) && (Entry & 0x800) && Entry != 0;
 }
 
 NTSTATUS
@@ -880,29 +555,26 @@ MmCreatePageFileMapping(PEPROCESS Process,
     if (Process == NULL && Address < MmSystemRangeStart)
     {
         DPRINT1("No process\n");
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     if (Process != NULL && Address >= MmSystemRangeStart)
     {
         DPRINT1("Setting kernel address with process context\n");
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
+
     if (SwapEntry & (1 << 31))
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     
     Pt = MmGetPageTableForProcess(Process, Address, TRUE);
     if (Pt == NULL)
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
     Pte = *Pt;
-    if (PAGE_MASK((Pte)) != 0)
-    {
-        MmMarkPageUnmapped(PTE_TO_PFN((Pte)));
-    }
-    (void)InterlockedExchangeUL(Pt, SwapEntry << 1);
+    InterlockedExchangePte(Pt, SwapEntry << 1);
     if (Pte != 0)
     {
         MiFlushTlb(Pt, Address);
@@ -921,7 +593,7 @@ NTAPI
 MmCreateVirtualMappingUnsafe(PEPROCESS Process,
                              PVOID Address,
                              ULONG flProtect,
-                             PPFN_TYPE Pages,
+                             PPFN_NUMBER Pages,
                              ULONG PageCount)
 {
     ULONG Attributes;
@@ -940,13 +612,13 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process,
         if (Address < MmSystemRangeStart)
         {
             DPRINT1("No process\n");
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
         if (PageCount > 0x10000 ||
             (ULONG_PTR) Address / PAGE_SIZE + PageCount > 0x100000)
         {
-            DPRINT1("Page count to large\n");
-            KEBUGCHECK(0);
+            DPRINT1("Page count too large\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
     }
     else
@@ -954,14 +626,14 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process,
         if (Address >= MmSystemRangeStart)
         {
             DPRINT1("Setting kernel address with process context\n");
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
         if (PageCount > (ULONG_PTR)MmSystemRangeStart / PAGE_SIZE ||
             (ULONG_PTR) Address / PAGE_SIZE + PageCount >
             (ULONG_PTR)MmSystemRangeStart / PAGE_SIZE)
         {
-            DPRINT1("Page Count to large\n");
-            KEBUGCHECK(0);
+            DPRINT1("Page Count too large\n");
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
     }
     
@@ -974,10 +646,6 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process,
     if (Address >= MmSystemRangeStart)
     {
         Attributes &= ~PA_USER;
-        if (Ke386GlobalPagesEnabled)
-        {
-            Attributes |= PA_GLOBAL;
-        }
     }
     else
     {
@@ -993,7 +661,7 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process,
             DPRINT1("Setting physical address but not allowing access at address "
                     "0x%.8X with attributes %x/%x.\n",
                     Addr, Attributes, flProtect);
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
         PdeOffset = ADDR_TO_PDE_OFFSET(Addr);
         if (oldPdeOffset != PdeOffset)
@@ -1002,7 +670,7 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process,
             Pt = MmGetPageTableForProcess(Process, Addr, TRUE);
             if (Pt == NULL)
             {
-                KEBUGCHECK(0);
+                KeBugCheck(MEMORY_MANAGEMENT);
             }
         }
         else
@@ -1012,16 +680,12 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process,
         oldPdeOffset = PdeOffset;
         
         Pte = *Pt;
-        MmMarkPageMapped(Pages[i]);
-        if (PAGE_MASK((Pte)) != 0 && !((Pte) & PA_PRESENT))
-        {
-            KEBUGCHECK(0);
-        }
-        if (PAGE_MASK((Pte)) != 0)
+        if (PAGE_MASK(Pte) != 0 && !(Pte & PA_PRESENT) && (Pte & 0x800))
         {
-            MmMarkPageUnmapped(PTE_TO_PFN((Pte)));
+            DPRINT1("Bad PTE %lx\n", Pte);
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
-        (void)InterlockedExchangeUL(Pt, PFN_TO_PTE(Pages[i]) | Attributes);
+        InterlockedExchangePte(Pt, PFN_TO_PTE(Pages[i]) | Attributes);
         if (Pte != 0)
         {
             if (Address > MmSystemRangeStart ||
@@ -1044,7 +708,7 @@ NTAPI
 MmCreateVirtualMapping(PEPROCESS Process,
                        PVOID Address,
                        ULONG flProtect,
-                       PPFN_TYPE Pages,
+                       PPFN_NUMBER Pages,
                        ULONG PageCount)
 {
     ULONG i;
@@ -1054,7 +718,7 @@ MmCreateVirtualMapping(PEPROCESS Process,
         if (!MmIsPageInUse(Pages[i]))
         {
             DPRINT1("Page at address %x not in use\n", PFN_TO_PTE(Pages[i]));
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
         }
     }
     
@@ -1118,6 +782,7 @@ MmSetPageProtect(PEPROCESS Process, PVOID Address, ULONG flProtect)
            Process, Address, flProtect);
     
     Attributes = ProtectToPTE(flProtect);
+
     if (Attributes & 0x80000000)
     {
         NoExecute = TRUE;
@@ -1126,10 +791,6 @@ MmSetPageProtect(PEPROCESS Process, PVOID Address, ULONG flProtect)
     if (Address >= MmSystemRangeStart)
     {
         Attributes &= ~PA_USER;
-        if (Ke386GlobalPagesEnabled)
-        {
-            Attributes |= PA_GLOBAL;
-        }
     }
     else
     {
@@ -1139,16 +800,16 @@ MmSetPageProtect(PEPROCESS Process, PVOID Address, ULONG flProtect)
     Pt = MmGetPageTableForProcess(Process, Address, FALSE);
     if (Pt == NULL)
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
-    InterlockedExchange((PLONG)Pt, PAGE_MASK(*Pt) | Attributes | (*Pt & (PA_ACCESSED|PA_DIRTY)));
+    InterlockedExchangePte(Pt, PAGE_MASK(*Pt) | Attributes | (*Pt & (PA_ACCESSED|PA_DIRTY)));
     MiFlushTlb(Pt, Address);
 }
 
 /*
  * @implemented
  */
-PHYSICAL_ADDRESS STDCALL
+PHYSICAL_ADDRESS NTAPI
 MmGetPhysicalAddress(PVOID vaddr)
 /*
  * FUNCTION: Returns the physical address corresponding to a virtual address
@@ -1171,104 +832,6 @@ MmGetPhysicalAddress(PVOID vaddr)
     return p;
 }
 
-PVOID
-NTAPI
-MmCreateHyperspaceMapping(PFN_TYPE Page)
-{
-    PVOID Address;
-    ULONG i;
-    
-    ULONG Entry;
-    PULONG Pte;
-    Entry = PFN_TO_PTE(Page) | PA_PRESENT | PA_READWRITE;
-    Pte = (PULONG)MiAddressToPte(HYPERSPACE) + Page % 1024;
-    if (Page & 1024)
-    {
-        for (i = Page % 1024; i < 1024; i++, Pte++)
-        {
-            if (0 == InterlockedCompareExchange((PLONG)Pte, (LONG)Entry, 0))
-            {
-                break;
-            }
-        }
-        if (i >= 1024)
-        {
-            Pte = (PULONG)MiAddressToPte(HYPERSPACE);
-            for (i = 0; i < Page % 1024; i++, Pte++)
-            {
-                if (0 == InterlockedCompareExchange((PLONG)Pte, (LONG)Entry, 0))
-                {
-                    break;
-                }
-            }
-            if (i >= Page % 1024)
-            {
-                KEBUGCHECK(0);
-            }
-        }
-    }
-    else
-    {
-        for (i = Page % 1024; (LONG)i >= 0; i--, Pte--)
-        {
-            if (0 == InterlockedCompareExchange((PLONG)Pte, (LONG)Entry, 0))
-            {
-                break;
-            }
-        }
-        if ((LONG)i < 0)
-        {
-            Pte = (PULONG)MiAddressToPte(HYPERSPACE) + 1023;
-            for (i = 1023; i > Page % 1024; i--, Pte--)
-            {
-                if (0 == InterlockedCompareExchange((PLONG)Pte, (LONG)Entry, 0))
-                {
-                    break;
-                }
-            }
-            if (i <= Page % 1024)
-            {
-                KEBUGCHECK(0);
-            }
-        }
-    }
-    Address = (PVOID)((ULONG_PTR)HYPERSPACE + i * PAGE_SIZE);
-    __invlpg(Address);
-    return Address;
-}
-
-PFN_TYPE
-NTAPI
-MmChangeHyperspaceMapping(PVOID Address, PFN_TYPE NewPage)
-{
-    PFN_TYPE Pfn;
-    ULONG Entry;
-    
-    ASSERT (IS_HYPERSPACE(Address));
-    
-    Entry = InterlockedExchange((PLONG)MiAddressToPte(Address), PFN_TO_PTE(NewPage) | PA_PRESENT | PA_READWRITE);
-    Pfn = PTE_TO_PFN(Entry);
-    
-    __invlpg(Address);
-    return Pfn;
-}
-
-PFN_TYPE
-NTAPI
-MmDeleteHyperspaceMapping(PVOID Address)
-{
-    PFN_TYPE Pfn;
-    ULONG Entry;
-    
-    ASSERT (IS_HYPERSPACE(Address));
-    
-    Entry = InterlockedExchange((PLONG)MiAddressToPte(Address), 0);
-    Pfn = PTE_TO_PFN(Entry);
-    
-    __invlpg(Address);
-    return Pfn;
-}
-
 VOID
 NTAPI
 MmUpdatePageDir(PEPROCESS Process, PVOID Address, ULONG Size)
@@ -1276,17 +839,29 @@ MmUpdatePageDir(PEPROCESS Process, PVOID Address, ULONG Size)
     ULONG StartOffset, EndOffset, Offset;
     PULONG Pde;
     
+    //
+    // Check if the process isn't there anymore
+    // This is probably a bad sign, since it means the caller is setting cr3 to
+    // 0 or something...
+    //
+    if ((PTE_TO_PFN(Process->Pcb.DirectoryTableBase[0]) == 0) && (Process != PsGetCurrentProcess()))
+    {
+        DPRINT1("Process: %16s is dead: %p\n", Process->ImageFileName, Process->Pcb.DirectoryTableBase[0]);
+        ASSERT(FALSE);
+        return;
+    }
+
     if (Address < MmSystemRangeStart)
     {
-        KEBUGCHECK(0);
+        KeBugCheck(MEMORY_MANAGEMENT);
     }
-    
+
     StartOffset = ADDR_TO_PDE_OFFSET(Address);
     EndOffset = ADDR_TO_PDE_OFFSET((PVOID)((ULONG_PTR)Address + Size));
-    
+
     if (Process != NULL && Process != PsGetCurrentProcess())
     {
-        Pde = MmCreateHyperspaceMapping(PTE_TO_PFN(Process->Pcb.DirectoryTableBase.u.LowPart));
+        Pde = MmCreateHyperspaceMapping(PTE_TO_PFN(Process->Pcb.DirectoryTableBase[0]));
     }
     else
     {
@@ -1296,7 +871,7 @@ MmUpdatePageDir(PEPROCESS Process, PVOID Address, ULONG Size)
     {
         if (Offset != ADDR_TO_PDE_OFFSET(PAGETABLE_MAP))
         {
-            (void)InterlockedCompareExchangeUL(&Pde[Offset], MmGlobalKernelPageDirectory[Offset], 0);
+            InterlockedCompareExchangePte(&Pde[Offset], MmGlobalKernelPageDirectory[Offset], 0);
         }
     }
     if (Pde != (PULONG)PAGEDIRECTORY_MAP)
@@ -1322,57 +897,8 @@ MmInitGlobalKernelPageDirectory(VOID)
             0 == MmGlobalKernelPageDirectory[i] && 0 != CurrentPageDirectory[i])
         {
             MmGlobalKernelPageDirectory[i] = CurrentPageDirectory[i];
-            if (Ke386GlobalPagesEnabled)
-            {
-                MmGlobalKernelPageDirectory[i] |= PA_GLOBAL;
-                CurrentPageDirectory[i] |= PA_GLOBAL;
-            }
         }
     }
 }
 
-VOID
-INIT_FUNCTION
-NTAPI
-MiInitPageDirectoryMap(VOID)
-{
-    MEMORY_AREA* kernel_map_desc = NULL;
-    MEMORY_AREA* hyperspace_desc = NULL;
-    PHYSICAL_ADDRESS BoundaryAddressMultiple;
-    PVOID BaseAddress;
-    NTSTATUS Status;
-    
-    DPRINT("MiInitPageDirectoryMap()\n");
-    
-    BoundaryAddressMultiple.QuadPart = 0;
-    BaseAddress = (PVOID)PAGETABLE_MAP;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_SYSTEM,
-                                &BaseAddress,
-                                0x400000,
-                                PAGE_READWRITE,
-                                &kernel_map_desc,
-                                TRUE,
-                                0,
-                                BoundaryAddressMultiple);
-    if (!NT_SUCCESS(Status))
-    {
-        KEBUGCHECK(0);
-    }
-    BaseAddress = (PVOID)HYPERSPACE;
-    Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
-                                MEMORY_AREA_SYSTEM,
-                                &BaseAddress,
-                                0x400000,
-                                PAGE_READWRITE,
-                                &hyperspace_desc,
-                                TRUE,
-                                0,
-                                BoundaryAddressMultiple);
-    if (!NT_SUCCESS(Status))
-    {
-        KEBUGCHECK(0);
-    }
-}
-
 /* EOF */