- Fix compile issues caused by previous patch.
[reactos.git] / reactos / ntoskrnl / mm / region.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/region.c
6 * PURPOSE: No purpose listed.
7 *
8 * PROGRAMMERS: David Welch
9 */
10
11 /* INCLUDE *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <internal/debug.h>
16
17 /* FUNCTIONS *****************************************************************/
18
19 VOID STATIC
20 InsertAfterEntry(PLIST_ENTRY Previous,
21 PLIST_ENTRY Entry)
22 /*
23 * FUNCTION: Insert a list entry after another entry in the list
24 */
25 {
26 Previous->Flink->Blink = Entry;
27
28 Entry->Flink = Previous->Flink;
29 Entry->Blink = Previous;
30
31 Previous->Flink = Entry;
32 }
33
34 PMM_REGION STATIC
35 MmSplitRegion(PMM_REGION InitialRegion, PVOID InitialBaseAddress,
36 PVOID StartAddress, ULONG Length, ULONG NewType,
37 ULONG NewProtect, PMADDRESS_SPACE AddressSpace,
38 PMM_ALTER_REGION_FUNC AlterFunc)
39 {
40 PMM_REGION NewRegion1;
41 PMM_REGION NewRegion2;
42 ULONG InternalLength;
43
44 /* Allocate this in front otherwise the failure case is too difficult. */
45 NewRegion2 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
46 TAG_MM_REGION);
47 if (NewRegion2 == NULL)
48 {
49 return(NULL);
50 }
51
52 /* Create the new region. */
53 NewRegion1 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
54 TAG_MM_REGION);
55 if (NewRegion1 == NULL)
56 {
57 ExFreePool(NewRegion2);
58 return(NULL);
59 }
60 NewRegion1->Type = NewType;
61 NewRegion1->Protect = NewProtect;
62 InternalLength = ((char*)InitialBaseAddress + InitialRegion->Length) - (char*)StartAddress;
63 InternalLength = min(InternalLength, Length);
64 NewRegion1->Length = InternalLength;
65 InsertAfterEntry(&InitialRegion->RegionListEntry,
66 &NewRegion1->RegionListEntry);
67
68 /*
69 * Call our helper function to do the changes on the addresses contained
70 * in the initial region.
71 */
72 AlterFunc(AddressSpace, StartAddress, InternalLength, InitialRegion->Type,
73 InitialRegion->Protect, NewType, NewProtect);
74
75 /*
76 * If necessary create a new region for the portion of the initial region
77 * beyond the range of addresses to alter.
78 */
79 if (((char*)InitialBaseAddress + InitialRegion->Length) > ((char*)StartAddress + Length))
80 {
81 NewRegion2->Type = InitialRegion->Type;
82 NewRegion2->Protect = InitialRegion->Protect;
83 NewRegion2->Length = ((char*)InitialBaseAddress + InitialRegion->Length) -
84 ((char*)StartAddress + Length);
85 InsertAfterEntry(&NewRegion1->RegionListEntry,
86 &NewRegion2->RegionListEntry);
87 }
88 else
89 {
90 ExFreePool(NewRegion2);
91 }
92
93 /* Either remove or shrink the initial region. */
94 if (InitialBaseAddress == StartAddress)
95 {
96 RemoveEntryList(&InitialRegion->RegionListEntry);
97 ExFreePool(InitialRegion);
98 }
99 else
100 {
101 InitialRegion->Length = (char*)StartAddress - (char*)InitialBaseAddress;
102 }
103
104 return(NewRegion1);
105 }
106
107 NTSTATUS
108 NTAPI
109 MmAlterRegion(PMADDRESS_SPACE AddressSpace, PVOID BaseAddress,
110 PLIST_ENTRY RegionListHead, PVOID StartAddress, ULONG Length,
111 ULONG NewType, ULONG NewProtect, PMM_ALTER_REGION_FUNC AlterFunc)
112 {
113 PMM_REGION InitialRegion;
114 PVOID InitialBaseAddress = NULL;
115 PMM_REGION NewRegion;
116 PLIST_ENTRY CurrentEntry;
117 PMM_REGION CurrentRegion = NULL;
118 PVOID CurrentBaseAddress;
119 ULONG RemainingLength;
120
121 /*
122 * Find the first region containing part of the range of addresses to
123 * be altered.
124 */
125 InitialRegion = MmFindRegion(BaseAddress, RegionListHead, StartAddress,
126 &InitialBaseAddress);
127 if (((char*)StartAddress + Length) >
128 ((char*)InitialBaseAddress + InitialRegion->Length))
129 {
130 RemainingLength = ((char*)StartAddress + Length) -
131 ((char*)InitialBaseAddress + InitialRegion->Length);
132 }
133 else
134 {
135 RemainingLength = 0;
136 }
137
138 /*
139 * If necessary then split the region into the affected and unaffected parts.
140 */
141 if (InitialRegion->Type != NewType || InitialRegion->Protect != NewProtect)
142 {
143 NewRegion = MmSplitRegion(InitialRegion, InitialBaseAddress,
144 StartAddress, Length, NewType, NewProtect,
145 AddressSpace, AlterFunc);
146 if (NewRegion == NULL)
147 {
148 return(STATUS_NO_MEMORY);
149 }
150 }
151 else
152 {
153 NewRegion = InitialRegion;
154 }
155
156 /*
157 * Free any complete regions that are containing in the range of addresses
158 * and call the helper function to actually do the changes.
159 */
160 CurrentEntry = NewRegion->RegionListEntry.Flink;
161 CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
162 RegionListEntry);
163 CurrentBaseAddress = (char*)StartAddress + NewRegion->Length;
164 while (RemainingLength > 0 && CurrentRegion->Length <= RemainingLength &&
165 CurrentEntry != RegionListHead)
166 {
167 if (CurrentRegion->Type != NewType &&
168 CurrentRegion->Protect != NewProtect)
169 {
170 AlterFunc(AddressSpace, CurrentBaseAddress, CurrentRegion->Length,
171 CurrentRegion->Type, CurrentRegion->Protect,
172 NewType, NewProtect);
173 }
174
175 CurrentBaseAddress = (PVOID)((ULONG_PTR)CurrentBaseAddress + CurrentRegion->Length);
176 NewRegion->Length += CurrentRegion->Length;
177 RemainingLength -= CurrentRegion->Length;
178 CurrentEntry = CurrentEntry->Flink;
179 RemoveEntryList(&CurrentRegion->RegionListEntry);
180 ExFreePool(CurrentRegion);
181 CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
182 RegionListEntry);
183 }
184
185 /*
186 * Split any final region.
187 */
188 if (RemainingLength > 0 && CurrentEntry != RegionListHead)
189 {
190 CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
191 RegionListEntry);
192 if (CurrentRegion->Type != NewType &&
193 CurrentRegion->Protect != NewProtect)
194 {
195 AlterFunc(AddressSpace, CurrentBaseAddress, CurrentRegion->Length,
196 CurrentRegion->Type, CurrentRegion->Protect,
197 NewType, NewProtect);
198 }
199 NewRegion->Length += RemainingLength;
200 CurrentRegion->Length -= RemainingLength;
201 }
202
203 /*
204 * If the region after the new region has the same type then merge them.
205 */
206 if (NewRegion->RegionListEntry.Flink != RegionListHead)
207 {
208 CurrentEntry = NewRegion->RegionListEntry.Flink;
209 CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
210 RegionListEntry);
211 if (CurrentRegion->Type == NewRegion->Type &&
212 CurrentRegion->Protect == NewRegion->Protect)
213 {
214 NewRegion->Length += CurrentRegion->Length;
215 RemoveEntryList(&CurrentRegion->RegionListEntry);
216 ExFreePool(CurrentRegion);
217 }
218 }
219
220 /*
221 * If the region before the new region has the same type then merge them.
222 */
223 if (NewRegion->RegionListEntry.Blink != RegionListHead)
224 {
225 CurrentEntry = NewRegion->RegionListEntry.Blink;
226 CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
227 RegionListEntry);
228 if (CurrentRegion->Type == NewRegion->Type &&
229 CurrentRegion->Protect == NewRegion->Protect)
230 {
231 NewRegion->Length += CurrentRegion->Length;
232 RemoveEntryList(&CurrentRegion->RegionListEntry);
233 ExFreePool(CurrentRegion);
234 }
235 }
236
237 return(STATUS_SUCCESS);
238 }
239
240 VOID
241 NTAPI
242 MmInitialiseRegion(PLIST_ENTRY RegionListHead, ULONG Length, ULONG Type,
243 ULONG Protect)
244 {
245 PMM_REGION Region;
246
247 Region = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
248 TAG_MM_REGION);
249 Region->Type = Type;
250 Region->Protect = Protect;
251 Region->Length = Length;
252 InitializeListHead(RegionListHead);
253 InsertHeadList(RegionListHead, &Region->RegionListEntry);
254 }
255
256 PMM_REGION
257 NTAPI
258 MmFindRegion(PVOID BaseAddress, PLIST_ENTRY RegionListHead, PVOID Address,
259 PVOID* RegionBaseAddress)
260 {
261 PLIST_ENTRY current_entry;
262 PMM_REGION current;
263 PVOID StartAddress = BaseAddress;
264
265 current_entry = RegionListHead->Flink;
266 while (current_entry != RegionListHead)
267 {
268 current = CONTAINING_RECORD(current_entry, MM_REGION, RegionListEntry);
269
270 if (StartAddress <= Address &&
271 ((char*)StartAddress + current->Length) > (char*)Address)
272 {
273 if (RegionBaseAddress != NULL)
274 {
275 *RegionBaseAddress = StartAddress;
276 }
277 return(current);
278 }
279
280 current_entry = current_entry->Flink;
281
282 StartAddress = (PVOID)((ULONG_PTR)StartAddress + current->Length);
283
284 }
285 return(NULL);
286 }