Make the address space mutex unsafe, because sometimes it is acquired at > APC_LEVEL...
[reactos.git] / reactos / ntoskrnl / mm / aspace.c
index c2df1d7..b79d67b 100644 (file)
-/* $Id: aspace.c,v 1.16 2004/03/04 00:07:01 navaraf Exp $
- * 
+/* $Id$
+ *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS kernel
  * FILE:            ntoskrnl/mm/aspace.c
  * PURPOSE:         Manages address spaces
- * PROGRAMMER:      David Welch (welch@cwcom.net)
- * UPDATE HISTORY:
- *                  Created 22/05/98
+ *
+ * PROGRAMMERS:     David Welch (welch@cwcom.net)
  */
 
 /* INCLUDES *****************************************************************/
 
-#include <ddk/ntddk.h>
-#include <internal/mm.h>
-#include <internal/ps.h>
-#include <internal/pool.h>
-
+#include <ntoskrnl.h>
 #include <internal/debug.h>
 
 /* GLOBALS ******************************************************************/
 
 STATIC MADDRESS_SPACE KernelAddressSpace;
 
-#define TAG_PTRC      TAG('P', 'T', 'R', 'C')
-
 /* FUNCTIONS *****************************************************************/
 
-VOID 
+VOID
+NTAPI
 MmLockAddressSpace(PMADDRESS_SPACE AddressSpace)
 {
-  /*
-   * Don't bother with locking if we are the first thread.
-   */
-  if (KeGetCurrentThread() == NULL)
-    {
+   /*
+    * Don't bother with locking if we are the first thread.
+    */
+   if (KeGetCurrentThread() == NULL)
+   {
       return;
-    }
-  ExAcquireFastMutex(&AddressSpace->Lock);
+   }
+   ExEnterCriticalRegionAndAcquireFastMutexUnsafe(&AddressSpace->Lock);
 }
 
-VOID 
+VOID
+NTAPI
 MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace)
 {
-  /*
-   * Don't bother locking if we are the first thread.
-   */
-  if (KeGetCurrentThread() == NULL)
-    {
+   /*
+    * Don't bother locking if we are the first thread.
+    */
+   if (KeGetCurrentThread() == NULL)
+   {
       return;
-    }
-  ExReleaseFastMutex(&AddressSpace->Lock);
+   }
+   ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(&AddressSpace->Lock);
 }
 
-VOID INIT_FUNCTION
+VOID
+INIT_FUNCTION
+NTAPI
 MmInitializeKernelAddressSpace(VOID)
 {
    MmInitializeAddressSpace(NULL, &KernelAddressSpace);
 }
 
-PMADDRESS_SPACE MmGetCurrentAddressSpace(VOID)
+PMADDRESS_SPACE
+NTAPI
+MmGetCurrentAddressSpace(VOID)
 {
    return(&PsGetCurrentProcess()->AddressSpace);
 }
 
-PMADDRESS_SPACE MmGetKernelAddressSpace(VOID)
+PMADDRESS_SPACE
+NTAPI
+MmGetKernelAddressSpace(VOID)
 {
    return(&KernelAddressSpace);
 }
 
-NTSTATUS 
+NTSTATUS
+NTAPI
 MmInitializeAddressSpace(PEPROCESS Process,
-                        PMADDRESS_SPACE AddressSpace)
+                         PMADDRESS_SPACE AddressSpace)
 {
-   InitializeListHead(&AddressSpace->MAreaListHead);
+   AddressSpace->MemoryAreaRoot = NULL;
    ExInitializeFastMutex(&AddressSpace->Lock);
    if (Process != NULL)
-     {
-       AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS;
-     }
+   {
+      AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS;
+   }
    else
-     {
-       AddressSpace->LowestAddress = KERNEL_BASE;
-     }
+   {
+      AddressSpace->LowestAddress = MmSystemRangeStart;
+   }
    AddressSpace->Process = Process;
    if (Process != NULL)
-     {
-       AddressSpace->PageTableRefCountTable = 
-         ExAllocatePoolWithTag(NonPagedPool, 768 * sizeof(USHORT),
-                               TAG_PTRC);
-       RtlZeroMemory(AddressSpace->PageTableRefCountTable, 768 * sizeof(USHORT));
-       AddressSpace->PageTableRefCountTableSize = 768;
-     }
+   {
+      ULONG Count;
+      Count = MiGetUserPageDirectoryCount();
+      AddressSpace->PageTableRefCountTable =
+         ExAllocatePoolWithTag(NonPagedPool, Count * sizeof(USHORT),
+                               TAG_PTRC);
+      RtlZeroMemory(AddressSpace->PageTableRefCountTable, Count * sizeof(USHORT));
+      AddressSpace->PageTableRefCountTableSize = Count;
+   }
    else
-     {
-       AddressSpace->PageTableRefCountTable = NULL;
-       AddressSpace->PageTableRefCountTableSize = 0;
-     }
+   {
+      AddressSpace->PageTableRefCountTable = NULL;
+      AddressSpace->PageTableRefCountTableSize = 0;
+   }
    return(STATUS_SUCCESS);
 }
 
-NTSTATUS 
+NTSTATUS
+NTAPI
 MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace)
 {
-  if (AddressSpace->PageTableRefCountTable != NULL)
-    {
+   if (AddressSpace->PageTableRefCountTable != NULL)
+   {
       ExFreePool(AddressSpace->PageTableRefCountTable);
-    }
+   }
    return(STATUS_SUCCESS);
 }