[NTOS:MM] Use MI_MAKE_HARDWARE_PTE & friends in legacy Mm
authorJérôme Gardou <jerome.gardou@reactos.org>
Thu, 1 Apr 2021 09:45:10 +0000 (11:45 +0200)
committerJérôme Gardou <zefklop@users.noreply.github.com>
Thu, 8 Apr 2021 13:40:37 +0000 (15:40 +0200)
ntoskrnl/mm/i386/page.c

index 35b0921..8b3ed37 100644 (file)
@@ -15,9 +15,6 @@
 
 #include <mm/ARM3/miarm.h>
 
-#define ADDR_TO_PDE_OFFSET MiAddressToPdeOffset
-#define ADDR_TO_PAGE_TABLE(v)  (((ULONG)(v)) / (1024 * PAGE_SIZE))
-
 /* GLOBALS *****************************************************************/
 
 #define PA_BIT_PRESENT   (0)
@@ -134,47 +131,6 @@ ULONG MmProtectToValue[32] =
 
 /* FUNCTIONS ***************************************************************/
 
-static ULONG
-ProtectToPTE(ULONG flProtect)
-{
-    ULONG Attributes = 0;
-
-    if (flProtect & (PAGE_NOACCESS|PAGE_GUARD))
-    {
-        Attributes = 0;
-    }
-    else if (flProtect & PAGE_IS_WRITABLE)
-    {
-        Attributes = PA_PRESENT | PA_READWRITE;
-    }
-    else if (flProtect & (PAGE_IS_READABLE | PAGE_IS_EXECUTABLE))
-    {
-        Attributes = PA_PRESENT;
-    }
-    else
-    {
-        DPRINT1("Unknown main protection type.\n");
-        KeBugCheck(MEMORY_MANAGEMENT);
-    }
-
-    if (flProtect & PAGE_SYSTEM)
-    {
-    }
-    else
-    {
-        Attributes = Attributes | PA_USER;
-    }
-    if (flProtect & PAGE_NOCACHE)
-    {
-        Attributes = Attributes | PA_CD;
-    }
-    if (flProtect & PAGE_WRITETHROUGH)
-    {
-        Attributes = Attributes | PA_WT;
-    }
-    return(Attributes);
-}
-
 NTSTATUS
 NTAPI
 MiFillSystemPageDirectory(IN PVOID Base,
@@ -591,15 +547,22 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process,
                              ULONG flProtect,
                              PFN_NUMBER Page)
 {
-    ULONG Attributes;
+    ULONG ProtectionMask;
     PMMPTE PointerPte;
-    ULONG Pte;
+    MMPTE TempPte;
+    ULONG_PTR Pte;
 
     DPRINT("MmCreateVirtualMappingUnsafe(%p, %p, %lu, %x)\n",
            Process, Address, flProtect, Page);
 
     ASSERT(((ULONG_PTR)Address % PAGE_SIZE) == 0);
 
+    ProtectionMask = MiMakeProtectionMask(flProtect);
+    /* Caller must have checked ! */
+    ASSERT(ProtectionMask != MM_INVALID_PROTECTION);
+    ASSERT(ProtectionMask != MM_NOACCESS);
+    ASSERT(ProtectionMask != MM_ZERO_ACCESS);
+
     /* Make sure our PDE is valid, and that everything is going fine */
     if (Process == NULL)
     {
@@ -626,22 +589,18 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process,
         MiMakePdeExistAndMakeValid(MiAddressToPde(Address), Process, MM_NOIRQL);
     }
 
-    Attributes = ProtectToPTE(flProtect);
-    Attributes &= 0xfff;
+    PointerPte = MiAddressToPte(Address);
+
     if (Address >= MmSystemRangeStart)
     {
-        Attributes &= ~PA_USER;
+        MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte, PointerPte, ProtectionMask, Page);
     }
     else
     {
-        Attributes |= PA_USER;
+        MI_MAKE_HARDWARE_PTE_USER(&TempPte, PointerPte, ProtectionMask, Page);
     }
 
-    /* This must be for a valid address */
-    ASSERT(FlagOn(Attributes, PA_PRESENT));
-
-    PointerPte = MiAddressToPte(Address);
-    Pte = InterlockedExchangePte(PointerPte, PFN_TO_PTE(Page) | Attributes);
+    Pte = InterlockedExchangePte(PointerPte, TempPte.u.Long);
     /* There should not have been anything valid here */
     if (Pte != 0)
     {
@@ -752,9 +711,10 @@ VOID
 NTAPI
 MmSetPageProtect(PEPROCESS Process, PVOID Address, ULONG flProtect)
 {
-    ULONG Attributes = 0;
+    ULONG ProtectionMask;
     PMMPTE PointerPte;
-    ULONG Pte;
+    MMPTE TempPte;
+    ULONG_PTR Pte;
 
     DPRINT("MmSetPageProtect(Process %p  Address %p  flProtect %x)\n",
            Process, Address, flProtect);
@@ -764,17 +724,22 @@ MmSetPageProtect(PEPROCESS Process, PVOID Address, ULONG flProtect)
 
     ASSERT(Process == PsGetCurrentProcess());
 
+    ProtectionMask = MiMakeProtectionMask(flProtect);
+    /* Caller must have checked ! */
+    ASSERT(ProtectionMask != MM_INVALID_PROTECTION);
+
     MiLockProcessWorkingSetUnsafe(Process, PsGetCurrentThread());
 
     MiMakePdeExistAndMakeValid(MiAddressToPde(Address), Process, MM_NOIRQL);
 
-    Attributes = ProtectToPTE(flProtect);
+    PointerPte = MiAddressToPte(Address);
 
-    Attributes &= 0xfff;
-    Attributes |= PA_USER;
+    MI_MAKE_HARDWARE_PTE_USER(&TempPte, PointerPte, ProtectionMask, PFN_FROM_PTE(PointerPte));
+    /* Keep dirty & accessed bits */
+    TempPte.u.Hard.Accessed = PointerPte->u.Hard.Accessed;
+    TempPte.u.Hard.Dirty = PointerPte->u.Hard.Dirty;
 
-    PointerPte = MiAddressToPte(Address);
-    Pte = InterlockedExchangePte(PointerPte, PAGE_MASK(PointerPte->u.Long) | Attributes | (PointerPte->u.Long & (PA_ACCESSED|PA_DIRTY)));
+    Pte = InterlockedExchangePte(PointerPte, TempPte.u.Long);
 
     // We should be able to bring a page back from PAGE_NOACCESS
     if ((Pte & 0x800) || !(Pte >> PAGE_SHIFT))
@@ -783,7 +748,7 @@ MmSetPageProtect(PEPROCESS Process, PVOID Address, ULONG flProtect)
         KeBugCheck(MEMORY_MANAGEMENT);
     }
 
-    if ((Pte & 0xFFF) != Attributes)
+    if (Pte != TempPte.u.Long)
         KeInvalidateTlbEntry(Address);
 
     MiUnlockProcessWorkingSetUnsafe(Process, PsGetCurrentThread());