b79d67b02d4374ea91c370edf83687e03d2c52b9
[reactos.git] / reactos / 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 *
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #include <internal/debug.h>
15
16 /* GLOBALS ******************************************************************/
17
18 STATIC MADDRESS_SPACE KernelAddressSpace;
19
20 /* FUNCTIONS *****************************************************************/
21
22 VOID
23 NTAPI
24 MmLockAddressSpace(PMADDRESS_SPACE AddressSpace)
25 {
26 /*
27 * Don't bother with locking if we are the first thread.
28 */
29 if (KeGetCurrentThread() == NULL)
30 {
31 return;
32 }
33 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(&AddressSpace->Lock);
34 }
35
36 VOID
37 NTAPI
38 MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace)
39 {
40 /*
41 * Don't bother locking if we are the first thread.
42 */
43 if (KeGetCurrentThread() == NULL)
44 {
45 return;
46 }
47 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(&AddressSpace->Lock);
48 }
49
50 VOID
51 INIT_FUNCTION
52 NTAPI
53 MmInitializeKernelAddressSpace(VOID)
54 {
55 MmInitializeAddressSpace(NULL, &KernelAddressSpace);
56 }
57
58 PMADDRESS_SPACE
59 NTAPI
60 MmGetCurrentAddressSpace(VOID)
61 {
62 return(&PsGetCurrentProcess()->AddressSpace);
63 }
64
65 PMADDRESS_SPACE
66 NTAPI
67 MmGetKernelAddressSpace(VOID)
68 {
69 return(&KernelAddressSpace);
70 }
71
72 NTSTATUS
73 NTAPI
74 MmInitializeAddressSpace(PEPROCESS Process,
75 PMADDRESS_SPACE AddressSpace)
76 {
77 AddressSpace->MemoryAreaRoot = NULL;
78 ExInitializeFastMutex(&AddressSpace->Lock);
79 if (Process != NULL)
80 {
81 AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS;
82 }
83 else
84 {
85 AddressSpace->LowestAddress = MmSystemRangeStart;
86 }
87 AddressSpace->Process = Process;
88 if (Process != NULL)
89 {
90 ULONG Count;
91 Count = MiGetUserPageDirectoryCount();
92 AddressSpace->PageTableRefCountTable =
93 ExAllocatePoolWithTag(NonPagedPool, Count * sizeof(USHORT),
94 TAG_PTRC);
95 RtlZeroMemory(AddressSpace->PageTableRefCountTable, Count * sizeof(USHORT));
96 AddressSpace->PageTableRefCountTableSize = Count;
97 }
98 else
99 {
100 AddressSpace->PageTableRefCountTable = NULL;
101 AddressSpace->PageTableRefCountTableSize = 0;
102 }
103 return(STATUS_SUCCESS);
104 }
105
106 NTSTATUS
107 NTAPI
108 MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace)
109 {
110 if (AddressSpace->PageTableRefCountTable != NULL)
111 {
112 ExFreePool(AddressSpace->PageTableRefCountTable);
113 }
114 return(STATUS_SUCCESS);
115 }
116
117 /* EOF */