[RSHELL]
[reactos.git] / ntoskrnl / mm / mmfault.c
index 269a165..602d708 100644 (file)
@@ -9,6 +9,7 @@
 /* INCLUDES *******************************************************************/
 
 #include <ntoskrnl.h>
+#include "../cache/section/newmm.h"
 #define NDEBUG
 #include <debug.h>
 
 
 /* PRIVATE FUNCTIONS **********************************************************/
 
-VOID
-FASTCALL
-MiSyncForProcessAttach(IN PKTHREAD Thread,
-                       IN PEPROCESS Process)
-{
-    PETHREAD Ethread = CONTAINING_RECORD(Thread, ETHREAD, Tcb);
-
-    /* Hack Sync because Mm is broken */
-    MmUpdatePageDir(Process, Ethread, sizeof(ETHREAD));
-    MmUpdatePageDir(Process, Ethread->ThreadsProcess, sizeof(EPROCESS));
-    MmUpdatePageDir(Process,
-                    (PVOID)Thread->StackLimit,
-                    Thread->LargeStack ?
-                    KERNEL_LARGE_STACK_SIZE : KERNEL_STACK_SIZE);
-}
-
-VOID
-FASTCALL
-MiSyncForContextSwitch(IN PKTHREAD Thread)
-{
-    PVOID Process = PsGetCurrentProcess();
-    PETHREAD Ethread = CONTAINING_RECORD(Thread, ETHREAD, Tcb);
-
-    /* Hack Sync because Mm is broken */
-    MmUpdatePageDir(Process, Ethread->ThreadsProcess, sizeof(EPROCESS));
-    MmUpdatePageDir(Process,
-                    (PVOID)Thread->StackLimit,
-                    Thread->LargeStack ?
-                    KERNEL_LARGE_STACK_SIZE : KERNEL_STACK_SIZE);
-}
-
 NTSTATUS
 NTAPI
 MmpAccessFault(KPROCESSOR_MODE Mode,
@@ -57,13 +27,12 @@ MmpAccessFault(KPROCESSOR_MODE Mode,
    PMMSUPPORT AddressSpace;
    MEMORY_AREA* MemoryArea;
    NTSTATUS Status;
-   BOOLEAN Locked = FromMdl;
 
    DPRINT("MmAccessFault(Mode %d, Address %x)\n", Mode, Address);
 
    if (KeGetCurrentIrql() >= DISPATCH_LEVEL)
    {
-      DPRINT1("Page fault at high IRQL was %d\n", KeGetCurrentIrql());
+      DPRINT1("Page fault at high IRQL was %u\n", KeGetCurrentIrql());
       return(STATUS_UNSUCCESSFUL);
    }
 
@@ -108,12 +77,17 @@ MmpAccessFault(KPROCESSOR_MODE Mode,
          case MEMORY_AREA_SECTION_VIEW:
             Status = MmAccessFaultSectionView(AddressSpace,
                                               MemoryArea,
-                                              (PVOID)Address,
-                                              Locked);
+                                              (PVOID)Address);
             break;
 
-         case MEMORY_AREA_VIRTUAL_MEMORY:
-            Status = STATUS_ACCESS_VIOLATION;
+         case MEMORY_AREA_CACHE:
+            // This code locks for itself to keep from having to break a lock
+            // passed in.
+            if (!FromMdl)
+               MmUnlockAddressSpace(AddressSpace);
+            Status = MmAccessFaultCacheSection(Mode, Address, FromMdl);
+            if (!FromMdl)
+               MmLockAddressSpace(AddressSpace);
             break;
 
          default:
@@ -140,13 +114,12 @@ MmNotPresentFault(KPROCESSOR_MODE Mode,
    PMMSUPPORT AddressSpace;
    MEMORY_AREA* MemoryArea;
    NTSTATUS Status;
-   BOOLEAN Locked = FromMdl;
 
    DPRINT("MmNotPresentFault(Mode %d, Address %x)\n", Mode, Address);
 
    if (KeGetCurrentIrql() >= DISPATCH_LEVEL)
    {
-      DPRINT1("Page fault at high IRQL was %d, address %x\n", KeGetCurrentIrql(), Address);
+      DPRINT1("Page fault at high IRQL was %u, address %x\n", KeGetCurrentIrql(), Address);
       return(STATUS_UNSUCCESSFUL);
    }
 
@@ -160,7 +133,7 @@ MmNotPresentFault(KPROCESSOR_MODE Mode,
        */
       if (Mode != KernelMode)
       {
-        DPRINT1("Address: %x\n", Address);
+         DPRINT1("Address: %x\n", Address);
          return(STATUS_ACCESS_VIOLATION);
       }
       AddressSpace = MmGetKernelAddressSpace();
@@ -196,14 +169,17 @@ MmNotPresentFault(KPROCESSOR_MODE Mode,
             Status = MmNotPresentFaultSectionView(AddressSpace,
                                                   MemoryArea,
                                                   (PVOID)Address,
-                                                  Locked);
+                                                  FromMdl);
             break;
 
-         case MEMORY_AREA_VIRTUAL_MEMORY:
-            Status = MmNotPresentFaultVirtualMemory(AddressSpace,
-                                                    MemoryArea,
-                                                    (PVOID)Address,
-                                                    Locked);
+         case MEMORY_AREA_CACHE:
+            // This code locks for itself to keep from having to break a lock
+            // passed in.
+            if (!FromMdl)
+               MmUnlockAddressSpace(AddressSpace);
+            Status = MmNotPresentFaultCacheSection(Mode, Address, FromMdl);
+            if (!FromMdl)
+               MmLockAddressSpace(AddressSpace);
             break;
 
          default:
@@ -230,7 +206,7 @@ MmAccessFault(IN BOOLEAN StoreInstruction,
               IN KPROCESSOR_MODE Mode,
               IN PVOID TrapInformation)
 {
-    PMEMORY_AREA MemoryArea;
+    PMEMORY_AREA MemoryArea = NULL;
 
     /* Cute little hack for ROS */
     if ((ULONG_PTR)Address >= (ULONG_PTR)MmSystemRangeStart)
@@ -244,28 +220,25 @@ MmAccessFault(IN BOOLEAN StoreInstruction,
         }
 #endif
     }
-    
-    /* 
-     * Check if this is an ARM3 memory area or if there's no memory area at all.
-     * The latter can happen early in the boot cycle when ARM3 paged pool is in
-     * use before having defined the memory areas proper.
-     * A proper fix would be to define memory areas in the ARM3 code, but we want
-     * to avoid adding this ReactOS-specific construct to ARM3 code.
-     * Either way, in the future, as ReactOS-paged pool is eliminated, this hack
-     * can go away.
-     */
-    MemoryArea = MmLocateMemoryAreaByAddress(MmGetKernelAddressSpace(), Address);
-    if (!(MemoryArea) && (Address <= MM_HIGHEST_USER_ADDRESS))
+
+    /* Is there a ReactOS address space yet? */
+    if (MmGetKernelAddressSpace())
     {
-        /* Could this be a VAD fault from user-mode? */
-        MemoryArea = MmLocateMemoryAreaByAddress(MmGetCurrentAddressSpace(), Address);
+        /* Check if this is an ARM3 memory area */
+        MemoryArea = MmLocateMemoryAreaByAddress(MmGetKernelAddressSpace(), Address);
+        if (!(MemoryArea) && (Address <= MM_HIGHEST_USER_ADDRESS))
+        {
+            /* Could this be a VAD fault from user-mode? */
+            MemoryArea = MmLocateMemoryAreaByAddress(MmGetCurrentAddressSpace(), Address);
+        }
     }
-    if ((!(MemoryArea) && ((ULONG_PTR)Address >= (ULONG_PTR)MmPagedPoolStart)) ||
-        ((MemoryArea) && (MemoryArea->Type == MEMORY_AREA_OWNED_BY_ARM3)))
+
+    /* 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()))
     {
-        //
-        // Hand it off to more competent hands...
-        //
+        /* This is an ARM3 fault */
         DPRINT("ARM3 fault %p\n", MemoryArea);
         return MmArmAccessFault(StoreInstruction, Address, Mode, TrapInformation);
     }