832750b1d62f0e028a53eda85bf61f2a4b08cf1a
[reactos.git] / reactos / boot / environ / lib / mm / mm.c
1 /*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/mm/mm.c
5 * PURPOSE: Boot Library Memory Manager Core
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "bl.h"
12 #include "bcd.h"
13
14 /* DATA VARIABLES ************************************************************/
15
16 /* This is a bug in Windows, but is required for MmTrInitialize to load */
17 BL_TRANSLATION_TYPE MmTranslationType = BlMax;
18 BL_TRANSLATION_TYPE MmOriginalTranslationType;
19 ULONG MmDescriptorCallTreeCount;
20
21 /* FUNCTIONS *****************************************************************/
22
23 NTSTATUS
24 MmTrInitialize (
25 VOID
26 )
27 {
28 PBL_MEMORY_DESCRIPTOR Descriptor;
29 NTSTATUS Status;
30 PLIST_ENTRY NextEntry;
31
32 /* Nothing to track if we're using physical memory */
33 if (MmTranslationType == BlNone)
34 {
35 return STATUS_SUCCESS;
36 }
37
38 /* Initialize all the virtual lists */
39 MmMdInitializeListHead(&MmMdlMappingTrackers);
40 MmMdlMappingTrackers.Type = BlMdTracker;
41 MmMdInitializeListHead(&MmMdlFreeVirtual);
42 MmMdlFreeVirtual.Type = BlMdVirtual;
43
44 /* Initialize a 4GB free descriptor */
45 Descriptor = MmMdInitByteGranularDescriptor(0,
46 BlConventionalMemory,
47 0,
48 0,
49 ((ULONGLONG)4 * 1024 * 1024 * 1024) >>
50 PAGE_SHIFT);
51 if (!Descriptor)
52 {
53 Status = STATUS_NO_MEMORY;
54 goto Quickie;
55 }
56
57 /* Add this 4GB region to the free virtual address space list */
58 Status = MmMdAddDescriptorToList(&MmMdlFreeVirtual,
59 Descriptor,
60 BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG);
61 if (!NT_SUCCESS(Status))
62 {
63 RtlZeroMemory(Descriptor, sizeof(*Descriptor));
64 goto Quickie;
65 }
66
67 /* Remove any reserved regions of virtual address space */
68 NextEntry = MmMdlReservedAllocated.First->Flink;
69 while (NextEntry != MmMdlReservedAllocated.First)
70 {
71 /* Grab the descriptor and see if it's mapped */
72 Descriptor = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
73 if (Descriptor->VirtualPage)
74 {
75 EfiPrintf(L"Need to handle reserved allocation: %llx %llx\r\n",
76 Descriptor->VirtualPage, Descriptor->PageCount);
77 EfiStall(100000);
78 Status = STATUS_NOT_IMPLEMENTED;
79 goto Quickie;
80 }
81
82 /* Next entry */
83 NextEntry = NextEntry->Flink;
84 }
85
86 /* Set success if we made it */
87 Status = STATUS_SUCCESS;
88
89 Quickie:
90 /* Return back to caller */
91 return Status;
92 }
93
94 NTSTATUS
95 BlMmRemoveBadMemory (
96 VOID
97 )
98 {
99 BOOLEAN AllowBad;
100 NTSTATUS Status;
101 PULONGLONG BadPages;
102 ULONGLONG BadPageCount;
103
104 /* First check if bad memory access is allowed */
105 AllowBad = FALSE;
106 Status = BlGetBootOptionBoolean(BlpApplicationEntry.BcdData,
107 BcdLibraryBoolean_AllowBadMemoryAccess,
108 &AllowBad);
109 if ((NT_SUCCESS(Status)) && (AllowBad))
110 {
111 /* No point checking the list if it is */
112 return STATUS_SUCCESS;
113 }
114
115 /* Otherwise, check if there's a persisted bad page list */
116 Status = BlpGetBootOptionIntegerList(BlpApplicationEntry.BcdData,
117 BcdLibraryIntegerList_BadMemoryList,
118 &BadPages,
119 &BadPageCount,
120 TRUE);
121 if (NT_SUCCESS(Status))
122 {
123 EfiPrintf(L"Persistent bad page list not supported\r\n");
124 return STATUS_NOT_IMPLEMENTED;
125 }
126
127 /* All done here */
128 return STATUS_SUCCESS;
129 }
130
131 NTSTATUS
132 BlMmMapPhysicalAddressEx (
133 _In_ PVOID* VirtualAddress,
134 _In_ ULONG Flags,
135 _In_ ULONGLONG Size,
136 _In_ PHYSICAL_ADDRESS PhysicalAddress
137 )
138 {
139 NTSTATUS Status;
140 PVOID MappingAddress;
141 PHYSICAL_ADDRESS MappedAddress;
142 PVOID MappedBase;
143 ULONGLONG MapSize;
144 UCHAR CacheAttributes;
145
146 /* Increase call depth */
147 ++MmDescriptorCallTreeCount;
148
149 /* Check if any parameters are missing */
150 if (!(VirtualAddress) || !(Size))
151 {
152 Status = STATUS_INVALID_PARAMETER;
153 goto Quickie;
154 }
155
156 /* Check for fixed allocation without an actual address */
157 if ((Flags & BlMemoryFixed) &&
158 (PhysicalAddress.QuadPart == -1) &&
159 !(*VirtualAddress))
160 {
161 Status = STATUS_INVALID_PARAMETER;
162 goto Quickie;
163 }
164
165 /* Check for invalid requirement flag, if one is present */
166 if (((Flags & BlMemoryValidAllocationAttributes) != BlMemoryFixed) &&
167 ((Flags & BlMemoryValidAllocationAttributes) != BlMemoryNonFixed) &&
168 (Flags & BlMemoryValidAllocationAttributes))
169 {
170 Status = STATUS_INVALID_PARAMETER;
171 goto Quickie;
172 }
173
174 /* Check for invalid cache attribute flags */
175 if (((Flags & BlMemoryValidCacheAttributeMask) - 1) &
176 (Flags & BlMemoryValidCacheAttributeMask))
177 {
178 Status = STATUS_INVALID_PARAMETER;
179 goto Quickie;
180 }
181
182 /* Select an address to map this at */
183 Status = MmSelectMappingAddress(&MappingAddress,
184 Size,
185 Flags & BlMemoryValidAllocationAttributes,
186 Flags,
187 PhysicalAddress);
188 if (!NT_SUCCESS(Status))
189 {
190 goto Quickie;
191 }
192
193 /* Map the selected address, using the appropriate caching attributes */
194 MappedAddress = PhysicalAddress;
195 MapSize = Size;
196 CacheAttributes = ((Flags & BlMemoryValidCacheAttributeMask) != 0x20) ?
197 (Flags & BlMemoryValidCacheAttributeMask) : 0;
198 Status = MmMapPhysicalAddress(&MappedAddress,
199 &MappingAddress,
200 &MapSize,
201 CacheAttributes);
202 if (!NT_SUCCESS(Status))
203 {
204 goto Quickie;
205 }
206
207 /* Compute the final address where the mapping was made */
208 MappedBase = (PVOID)((ULONG_PTR)MappingAddress +
209 PhysicalAddress.LowPart -
210 MappedAddress.LowPart);
211
212 /* Check if we're in physical or virtual mode */
213 if (MmTranslationType != BlNone)
214 {
215 /* We don't support virtual memory yet @TODO */
216 EfiPrintf(L"not yet implemented in %S\r\n", __FUNCTION__);
217 EfiStall(1000000);
218 Status = STATUS_NOT_IMPLEMENTED;
219 goto Quickie;
220 }
221
222 /* Return the mapped virtual address */
223 Status = STATUS_SUCCESS;
224 *VirtualAddress = MappedBase;
225
226 Quickie:
227 /* Cleanup descriptors and reduce depth */
228 MmMdFreeGlobalDescriptors();
229 --MmDescriptorCallTreeCount;
230 return Status;
231 }
232
233 NTSTATUS
234 MmUnmapVirtualAddress (
235 _Inout_ PVOID* VirtualAddress,
236 _Inout_ PULONGLONG Size
237 )
238 {
239 NTSTATUS Status;
240
241 /* Make sure parameters were passed in and are valid */
242 if ((VirtualAddress) && (Size) && (*Size <= 0xFFFFFFFF))
243 {
244 /* Nothing to do if translation isn't active */
245 if (MmTranslationType == BlNone)
246 {
247 Status = STATUS_SUCCESS;
248 }
249 else
250 {
251 /* We don't support virtual memory yet @TODO */
252 EfiPrintf(L"not yet implemented in %S\r\n", __FUNCTION__);
253 EfiStall(1000000);
254 Status = STATUS_NOT_IMPLEMENTED;
255 }
256 }
257 else
258 {
259 /* Fail */
260 Status = STATUS_INVALID_PARAMETER;
261 }
262
263 /* All done */
264 return Status;
265 }
266
267 NTSTATUS
268 BlMmUnmapVirtualAddressEx (
269 _In_ PVOID VirtualAddress,
270 _In_ ULONGLONG Size
271 )
272 {
273 NTSTATUS Status;
274
275 /* Increment call depth */
276 ++MmDescriptorCallTreeCount;
277
278 /* Make sure all parameters are there */
279 if ((VirtualAddress) && (Size))
280 {
281 /* Unmap the virtual address */
282 Status = MmUnmapVirtualAddress(&VirtualAddress, &Size);
283
284 /* Check if we actually had a virtual mapping active */
285 if ((NT_SUCCESS(Status)) && (MmTranslationType != BlNone))
286 {
287 /* We don't support virtual memory yet @TODO */
288 EfiPrintf(L"not yet implemented in %S\r\n", __FUNCTION__);
289 EfiStall(1000000);
290 Status = STATUS_NOT_IMPLEMENTED;
291 }
292 }
293 else
294 {
295 /* Fail */
296 Status = STATUS_INVALID_PARAMETER;
297 }
298
299 /* Cleanup descriptors and reduce depth */
300 MmMdFreeGlobalDescriptors();
301 --MmDescriptorCallTreeCount;
302 return Status;
303 }
304
305 BOOLEAN
306 BlMmTranslateVirtualAddress (
307 _In_ PVOID VirtualAddress,
308 _Out_ PPHYSICAL_ADDRESS PhysicalAddress
309 )
310 {
311 /* Make sure arguments are present */
312 if (!(VirtualAddress) || !(PhysicalAddress))
313 {
314 return FALSE;
315 }
316
317 /* Do the architecture-specific translation */
318 return MmArchTranslateVirtualAddress(VirtualAddress, PhysicalAddress, NULL);
319 }
320
321 NTSTATUS
322 BlpMmInitialize (
323 _In_ PBL_MEMORY_DATA MemoryData,
324 _In_ BL_TRANSLATION_TYPE TranslationType,
325 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
326 )
327 {
328 NTSTATUS Status;
329
330 /* Take a reference */
331 MmDescriptorCallTreeCount = 1;
332
333 /* Only support valid translation types */
334 if ((TranslationType > BlPae) || (LibraryParameters->TranslationType > BlPae))
335 {
336 /* Bail out */
337 EfiPrintf(L"Invalid translation types present\r\n");
338 Status = STATUS_INVALID_PARAMETER;
339 goto Quickie;
340 }
341
342 /* Initialize memory descriptors */
343 MmMdInitialize(0, LibraryParameters);
344
345 /* Remember the page type we came in with */
346 MmOriginalTranslationType = TranslationType;
347
348 /* Initialize the physical page allocator */
349 Status = MmPaInitialize(MemoryData,
350 LibraryParameters->MinimumAllocationCount);
351 if (!NT_SUCCESS(Status))
352 {
353 goto Quickie;
354 }
355
356 /* Initialize the memory tracker */
357 Status = MmTrInitialize();
358 if (!NT_SUCCESS(Status))
359 {
360 EfiPrintf(L"TR Mm init failed: %lx\r\n", Status);
361 //MmArchDestroy();
362 //MmPaDestroy(1);
363 goto Quickie;
364 }
365
366 /* Initialize paging, large pages, self-mapping, PAE, if needed */
367 Status = MmArchInitialize(1,
368 MemoryData,
369 TranslationType,
370 LibraryParameters->TranslationType);
371 if (NT_SUCCESS(Status))
372 {
373 /* Save the newly active transation type */
374 MmTranslationType = LibraryParameters->TranslationType;
375
376 /* Initialize the heap allocator now */
377 Status = MmHaInitialize(LibraryParameters->MinimumHeapSize,
378 LibraryParameters->HeapAllocationAttributes);
379 }
380
381 /* If Phase 1 init failed, bail out */
382 if (!NT_SUCCESS(Status))
383 {
384 /* Kill everything set setup so far */
385 EfiPrintf(L"Phase 1 Mm init failed: %lx\r\n", Status);
386 //MmPaDestroy(0);
387 //MmTrDestroy();
388 //MmArchDestroy();
389 //MmPaDestroy(1);
390 goto Quickie;
391 }
392
393 /* Do we have too many descriptors? */
394 if (LibraryParameters->DescriptorCount > 512)
395 {
396 /* Switch to using a dynamic buffer instead */
397 EfiPrintf(L"Warning: too many descriptors\r\n");
398 Status = STATUS_NOT_IMPLEMENTED;
399 goto Quickie;
400 //MmMdpSwitchToDynamicDescriptors(LibraryParameters->DescriptorCount);
401 }
402
403 /* Remove memory that the BCD says is bad */
404 BlMmRemoveBadMemory();
405
406 /* Now map all the memory regions as needed */
407 Status = MmArchInitialize(2,
408 MemoryData,
409 TranslationType,
410 LibraryParameters->TranslationType);
411 if (NT_SUCCESS(Status))
412 {
413 /* Initialize the block allocator */
414 Status = MmBaInitialize();
415 }
416
417 /* Check if anything in phase 2 failed */
418 if (!NT_SUCCESS(Status))
419 {
420 /* Go back to static descriptors and kill the heap */
421 EfiPrintf(L"Phase 2 Mm init failed: %lx\r\n", Status);
422 //MmMdpSwitchToStaticDescriptors();
423 //HapInitializationStatus = 0;
424 //++MmDescriptorCallTreeCount;
425
426 /* Destroy the Phase 1 initialization */
427 //MmPaDestroy(0);
428 //MmTrDestroy();
429 //MmArchDestroy();
430 //MmPaDestroy(1);
431 }
432
433 Quickie:
434 /* Free the memory descriptors and return the initialization state */
435 MmMdFreeGlobalDescriptors();
436 --MmDescriptorCallTreeCount;
437 return Status;
438 }