:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / ntoskrnl / mm / aspace.c
1 /* $Id$
2  * 
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            ntoskrnl/mm/aspace.c
6  * PURPOSE:         Manages address spaces
7  * PROGRAMMER:      David Welch (welch@cwcom.net)
8  * UPDATE HISTORY:
9  *                  Created 22/05/98
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/mm.h>
16 #include <internal/ps.h>
17 #include <internal/pool.h>
18
19 #include <internal/debug.h>
20
21 /* GLOBALS ******************************************************************/
22
23 STATIC MADDRESS_SPACE KernelAddressSpace;
24
25 #define TAG_PTRC      TAG('P', 'T', 'R', 'C')
26
27 /* FUNCTIONS *****************************************************************/
28
29 VOID 
30 MmLockAddressSpace(PMADDRESS_SPACE AddressSpace)
31 {
32   /*
33    * Don't bother with locking if we are the first thread.
34    */
35   if (KeGetCurrentThread() == NULL)
36     {
37       return;
38     }
39   ExAcquireFastMutex(&AddressSpace->Lock);
40 }
41
42 VOID 
43 MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace)
44 {
45   /*
46    * Don't bother locking if we are the first thread.
47    */
48   if (KeGetCurrentThread() == NULL)
49     {
50       return;
51     }
52   ExReleaseFastMutex(&AddressSpace->Lock);
53 }
54
55 VOID 
56 MmInitializeKernelAddressSpace(VOID)
57 {
58    MmInitializeAddressSpace(NULL, &KernelAddressSpace);
59 }
60
61 PMADDRESS_SPACE MmGetCurrentAddressSpace(VOID)
62 {
63    return(&PsGetCurrentProcess()->AddressSpace);
64 }
65
66 PMADDRESS_SPACE MmGetKernelAddressSpace(VOID)
67 {
68    return(&KernelAddressSpace);
69 }
70
71 NTSTATUS 
72 MmInitializeAddressSpace(PEPROCESS Process,
73                          PMADDRESS_SPACE AddressSpace)
74 {
75    InitializeListHead(&AddressSpace->MAreaListHead);
76    ExInitializeFastMutex(&AddressSpace->Lock);
77    if (Process != NULL)
78      {
79         AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS;
80      }
81    else
82      {
83         AddressSpace->LowestAddress = KERNEL_BASE;
84      }
85    AddressSpace->Process = Process;
86    if (Process != NULL)
87      {
88         AddressSpace->PageTableRefCountTable = 
89           ExAllocatePoolWithTag(NonPagedPool, 768 * sizeof(USHORT),
90                                 TAG_PTRC);
91         AddressSpace->PageTableRefCountTableSize = 768;
92      }
93    else
94      {
95         AddressSpace->PageTableRefCountTable = NULL;
96         AddressSpace->PageTableRefCountTableSize = 0;
97      }
98    return(STATUS_SUCCESS);
99 }
100
101 NTSTATUS 
102 MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace)
103 {
104   if (AddressSpace->PageTableRefCountTable != NULL)
105     {
106       ExFreePool(AddressSpace->PageTableRefCountTable);
107     }
108    return(STATUS_SUCCESS);
109 }