- Fix KiDispatchException to unmask KI_EXCEPTION_INTERNAL when setting the exception...
[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 #if defined (ALLOC_PRAGMA)
17 #pragma alloc_text(INIT, MmInitializeKernelAddressSpace)
18 #endif
19
20
21 /* GLOBALS ******************************************************************/
22
23 static MADDRESS_SPACE KernelAddressSpace;
24 FAST_MUTEX KernelAddressSpaceLock;
25
26 /* FUNCTIONS *****************************************************************/
27
28 VOID
29 NTAPI
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
40 if (AddressSpace->Process)
41 {
42 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(&AddressSpace->Process->AddressCreationLock);
43 }
44 else
45 {
46 ExEnterCriticalRegionAndAcquireFastMutexUnsafe(&KernelAddressSpaceLock);
47 }
48 }
49
50 VOID
51 NTAPI
52 MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace)
53 {
54 /*
55 * Don't bother locking if we are the first thread.
56 */
57 if (KeGetCurrentThread() == NULL)
58 {
59 return;
60 }
61 if (AddressSpace->Process)
62 {
63 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(&AddressSpace->Process->AddressCreationLock);
64 }
65 else
66 {
67 ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(&KernelAddressSpaceLock);
68 }
69 }
70
71 VOID
72 INIT_FUNCTION
73 NTAPI
74 MmInitializeKernelAddressSpace(VOID)
75 {
76 MmInitializeAddressSpace(NULL, &KernelAddressSpace);
77 }
78
79 PMADDRESS_SPACE
80 NTAPI
81 MmGetCurrentAddressSpace(VOID)
82 {
83 return((PMADDRESS_SPACE)&(PsGetCurrentProcess())->VadRoot);
84 }
85
86 PMADDRESS_SPACE
87 NTAPI
88 MmGetKernelAddressSpace(VOID)
89 {
90 return(&KernelAddressSpace);
91 }
92
93 NTSTATUS
94 NTAPI
95 MmInitializeAddressSpace(PEPROCESS Process,
96 PMADDRESS_SPACE AddressSpace)
97 {
98 AddressSpace->MemoryAreaRoot = NULL;
99 if (Process)
100 {
101 ExInitializeFastMutex(&Process->AddressCreationLock);
102 }
103 else
104 {
105 ExInitializeFastMutex(&KernelAddressSpaceLock);
106 }
107 if (Process != NULL)
108 {
109 AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS;
110 }
111 else
112 {
113 AddressSpace->LowestAddress = MmSystemRangeStart;
114 }
115 AddressSpace->Process = Process;
116 if (Process != NULL)
117 {
118 ULONG Count;
119 Count = MiGetUserPageDirectoryCount();
120 AddressSpace->PageTableRefCountTable =
121 ExAllocatePoolWithTag(NonPagedPool, Count * sizeof(USHORT),
122 TAG_PTRC);
123 RtlZeroMemory(AddressSpace->PageTableRefCountTable, Count * sizeof(USHORT));
124 AddressSpace->PageTableRefCountTableSize = Count;
125 }
126 else
127 {
128 AddressSpace->PageTableRefCountTable = NULL;
129 AddressSpace->PageTableRefCountTableSize = 0;
130 }
131 return(STATUS_SUCCESS);
132 }
133
134 NTSTATUS
135 NTAPI
136 MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace)
137 {
138 if (AddressSpace->PageTableRefCountTable != NULL)
139 {
140 ExFreePool(AddressSpace->PageTableRefCountTable);
141 }
142 return(STATUS_SUCCESS);
143 }
144
145 /* EOF */