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)
9 /* INCLUDES ******************************************************************/
14 /* DATA VARIABLES ************************************************************/
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
;
21 /* FUNCTIONS *****************************************************************/
28 PBL_MEMORY_DESCRIPTOR Descriptor
;
30 PLIST_ENTRY NextEntry
;
32 /* Nothing to track if we're using physical memory */
33 if (MmTranslationType
== BlNone
)
35 return STATUS_SUCCESS
;
38 /* Initialize all the virtual lists */
39 MmMdInitializeListHead(&MmMdlMappingTrackers
);
40 MmMdlMappingTrackers
.Type
= BlMdTracker
;
41 MmMdInitializeListHead(&MmMdlFreeVirtual
);
42 MmMdlFreeVirtual
.Type
= BlMdVirtual
;
44 /* Initialize a 4GB free descriptor */
45 Descriptor
= MmMdInitByteGranularDescriptor(0,
49 ((ULONGLONG
)4 * 1024 * 1024 * 1024) >>
53 Status
= STATUS_NO_MEMORY
;
57 /* Add this 4GB region to the free virtual address space list */
58 Status
= MmMdAddDescriptorToList(&MmMdlFreeVirtual
,
60 BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
);
61 if (!NT_SUCCESS(Status
))
63 RtlZeroMemory(Descriptor
, sizeof(*Descriptor
));
67 /* Remove any reserved regions of virtual address space */
68 NextEntry
= MmMdlReservedAllocated
.First
->Flink
;
69 while (NextEntry
!= MmMdlReservedAllocated
.First
)
71 /* Grab the descriptor and see if it's mapped */
72 Descriptor
= CONTAINING_RECORD(NextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
73 if (Descriptor
->VirtualPage
)
75 EfiPrintf(L
"Need to handle reserved allocation: %llx %llx\r\n",
76 Descriptor
->VirtualPage
, Descriptor
->PageCount
);
78 Status
= STATUS_NOT_IMPLEMENTED
;
83 NextEntry
= NextEntry
->Flink
;
86 /* Set success if we made it */
87 Status
= STATUS_SUCCESS
;
90 /* Return back to caller */
102 ULONGLONG BadPageCount
;
104 /* First check if bad memory access is allowed */
106 Status
= BlGetBootOptionBoolean(BlpApplicationEntry
.BcdData
,
107 BcdLibraryBoolean_AllowBadMemoryAccess
,
109 if ((NT_SUCCESS(Status
)) && (AllowBad
))
111 /* No point checking the list if it is */
112 return STATUS_SUCCESS
;
115 /* Otherwise, check if there's a persisted bad page list */
116 Status
= BlpGetBootOptionIntegerList(BlpApplicationEntry
.BcdData
,
117 BcdLibraryIntegerList_BadMemoryList
,
121 if (NT_SUCCESS(Status
))
123 EfiPrintf(L
"Persistent bad page list not supported\r\n");
124 return STATUS_NOT_IMPLEMENTED
;
128 return STATUS_SUCCESS
;
132 BlMmMapPhysicalAddressEx (
133 _In_ PVOID
* VirtualAddress
,
136 _In_ PHYSICAL_ADDRESS PhysicalAddress
140 PVOID MappingAddress
;
141 PHYSICAL_ADDRESS MappedAddress
;
144 UCHAR CacheAttributes
;
146 /* Increase call depth */
147 ++MmDescriptorCallTreeCount
;
149 /* Check if any parameters are missing */
150 if (!(VirtualAddress
) || !(Size
))
152 Status
= STATUS_INVALID_PARAMETER
;
156 /* Check for fixed allocation without an actual address */
157 if ((Flags
& BlMemoryFixed
) &&
158 (PhysicalAddress
.QuadPart
== -1) &&
161 Status
= STATUS_INVALID_PARAMETER
;
165 /* Check for invalid requirement flag, if one is present */
166 if (((Flags
& BlMemoryValidAllocationAttributes
) != BlMemoryFixed
) &&
167 ((Flags
& BlMemoryValidAllocationAttributes
) != BlMemoryKernelRange
) &&
168 (Flags
& BlMemoryValidAllocationAttributes
))
170 Status
= STATUS_INVALID_PARAMETER
;
174 /* Check for invalid cache attribute flags */
175 if (((Flags
& BlMemoryValidCacheAttributeMask
) - 1) &
176 (Flags
& BlMemoryValidCacheAttributeMask
))
178 Status
= STATUS_INVALID_PARAMETER
;
182 /* Select an address to map this at */
183 Status
= MmSelectMappingAddress(&MappingAddress
,
186 Flags
& BlMemoryValidAllocationAttributes
,
189 if (!NT_SUCCESS(Status
))
194 /* Map the selected address, using the appropriate caching attributes */
195 MappedAddress
= PhysicalAddress
;
197 CacheAttributes
= ((Flags
& BlMemoryValidCacheAttributeMask
) != 0x20) ?
198 (Flags
& BlMemoryValidCacheAttributeMask
) : 0;
199 EfiPrintf(L
"Selected address: %p for %lx\r\n", MappingAddress
, MappedAddress
.LowPart
);
200 Status
= MmMapPhysicalAddress(&MappedAddress
,
204 if (!NT_SUCCESS(Status
))
209 /* Compute the final address where the mapping was made */
210 MappedBase
= (PVOID
)((ULONG_PTR
)MappingAddress
+
211 PhysicalAddress
.LowPart
-
212 MappedAddress
.LowPart
);
214 /* Check if we're in physical or virtual mode */
215 if (MmTranslationType
!= BlNone
)
217 /* We don't support virtual memory yet @TODO */
218 EfiPrintf(L
"not yet implemented in %S\r\n", __FUNCTION__
);
220 Status
= STATUS_NOT_IMPLEMENTED
;
224 /* Return the mapped virtual address */
225 Status
= STATUS_SUCCESS
;
226 *VirtualAddress
= MappedBase
;
229 /* Cleanup descriptors and reduce depth */
230 MmMdFreeGlobalDescriptors();
231 --MmDescriptorCallTreeCount
;
236 MmUnmapVirtualAddress (
237 _Inout_ PVOID
* VirtualAddress
,
238 _Inout_ PULONGLONG Size
243 /* Make sure parameters were passed in and are valid */
244 if ((VirtualAddress
) && (Size
) && (*Size
<= 0xFFFFFFFF))
246 /* Nothing to do if translation isn't active */
247 if (MmTranslationType
== BlNone
)
249 Status
= STATUS_SUCCESS
;
253 /* We don't support virtual memory yet @TODO */
254 EfiPrintf(L
"not yet implemented in %S\r\n", __FUNCTION__
);
256 Status
= STATUS_NOT_IMPLEMENTED
;
262 Status
= STATUS_INVALID_PARAMETER
;
270 BlMmUnmapVirtualAddressEx (
271 _In_ PVOID VirtualAddress
,
277 /* Increment call depth */
278 ++MmDescriptorCallTreeCount
;
280 /* Make sure all parameters are there */
281 if ((VirtualAddress
) && (Size
))
283 /* Unmap the virtual address */
284 Status
= MmUnmapVirtualAddress(&VirtualAddress
, &Size
);
286 /* Check if we actually had a virtual mapping active */
287 if ((NT_SUCCESS(Status
)) && (MmTranslationType
!= BlNone
))
289 /* We don't support virtual memory yet @TODO */
290 EfiPrintf(L
"not yet implemented in %S\r\n", __FUNCTION__
);
292 Status
= STATUS_NOT_IMPLEMENTED
;
298 Status
= STATUS_INVALID_PARAMETER
;
301 /* Cleanup descriptors and reduce depth */
302 MmMdFreeGlobalDescriptors();
303 --MmDescriptorCallTreeCount
;
308 BlMmTranslateVirtualAddress (
309 _In_ PVOID VirtualAddress
,
310 _Out_ PPHYSICAL_ADDRESS PhysicalAddress
313 /* Make sure arguments are present */
314 if (!(VirtualAddress
) || !(PhysicalAddress
))
319 /* Do the architecture-specific translation */
320 return MmArchTranslateVirtualAddress(VirtualAddress
, PhysicalAddress
, NULL
);
325 _In_ PBL_MEMORY_DATA MemoryData
,
326 _In_ BL_TRANSLATION_TYPE TranslationType
,
327 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
332 /* Take a reference */
333 MmDescriptorCallTreeCount
= 1;
335 /* Only support valid translation types */
336 if ((TranslationType
> BlPae
) || (LibraryParameters
->TranslationType
> BlPae
))
339 EfiPrintf(L
"Invalid translation types present\r\n");
340 Status
= STATUS_INVALID_PARAMETER
;
344 /* Initialize memory descriptors */
345 MmMdInitialize(0, LibraryParameters
);
347 /* Remember the page type we came in with */
348 MmOriginalTranslationType
= TranslationType
;
350 /* Initialize the physical page allocator */
351 Status
= MmPaInitialize(MemoryData
,
352 LibraryParameters
->MinimumAllocationCount
);
353 if (!NT_SUCCESS(Status
))
358 /* Initialize the memory tracker */
359 Status
= MmTrInitialize();
360 if (!NT_SUCCESS(Status
))
362 EfiPrintf(L
"TR Mm init failed: %lx\r\n", Status
);
368 /* Initialize paging, large pages, self-mapping, PAE, if needed */
369 Status
= MmArchInitialize(1,
372 LibraryParameters
->TranslationType
);
373 if (NT_SUCCESS(Status
))
375 /* Save the newly active transation type */
376 MmTranslationType
= LibraryParameters
->TranslationType
;
378 /* Initialize the heap allocator now */
379 Status
= MmHaInitialize(LibraryParameters
->MinimumHeapSize
,
380 LibraryParameters
->HeapAllocationAttributes
);
383 /* If Phase 1 init failed, bail out */
384 if (!NT_SUCCESS(Status
))
386 /* Kill everything set setup so far */
387 EfiPrintf(L
"Phase 1 Mm init failed: %lx\r\n", Status
);
395 /* Do we have too many descriptors? */
396 if (LibraryParameters
->DescriptorCount
> 512)
398 /* Switch to using a dynamic buffer instead */
399 EfiPrintf(L
"Warning: too many descriptors\r\n");
400 Status
= STATUS_NOT_IMPLEMENTED
;
402 //MmMdpSwitchToDynamicDescriptors(LibraryParameters->DescriptorCount);
405 /* Remove memory that the BCD says is bad */
406 BlMmRemoveBadMemory();
408 /* Now map all the memory regions as needed */
409 Status
= MmArchInitialize(2,
412 LibraryParameters
->TranslationType
);
413 if (NT_SUCCESS(Status
))
415 /* Initialize the block allocator */
416 Status
= MmBaInitialize();
419 /* Check if anything in phase 2 failed */
420 if (!NT_SUCCESS(Status
))
422 /* Go back to static descriptors and kill the heap */
423 EfiPrintf(L
"Phase 2 Mm init failed: %lx\r\n", Status
);
424 //MmMdpSwitchToStaticDescriptors();
425 //HapInitializationStatus = 0;
426 //++MmDescriptorCallTreeCount;
428 /* Destroy the Phase 1 initialization */
436 /* Free the memory descriptors and return the initialization state */
437 MmMdFreeGlobalDescriptors();
438 --MmDescriptorCallTreeCount
;