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
) != BlMemoryNonFixed
) &&
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
,
185 Flags
& BlMemoryValidAllocationAttributes
,
188 if (!NT_SUCCESS(Status
))
193 /* Map the selected address, using the appropriate caching attributes */
194 MappedAddress
= PhysicalAddress
;
196 CacheAttributes
= ((Flags
& BlMemoryValidCacheAttributeMask
) != 0x20) ?
197 (Flags
& BlMemoryValidCacheAttributeMask
) : 0;
198 Status
= MmMapPhysicalAddress(&MappedAddress
,
202 if (!NT_SUCCESS(Status
))
207 /* Compute the final address where the mapping was made */
208 MappedBase
= (PVOID
)((ULONG_PTR
)MappingAddress
+
209 PhysicalAddress
.LowPart
-
210 MappedAddress
.LowPart
);
212 /* Check if we're in physical or virtual mode */
213 if (MmTranslationType
!= BlNone
)
215 /* We don't support virtual memory yet @TODO */
216 EfiPrintf(L
"not yet implemented in %S\r\n", __FUNCTION__
);
218 Status
= STATUS_NOT_IMPLEMENTED
;
222 /* Return the mapped virtual address */
223 Status
= STATUS_SUCCESS
;
224 *VirtualAddress
= MappedBase
;
227 /* Cleanup descriptors and reduce depth */
228 MmMdFreeGlobalDescriptors();
229 --MmDescriptorCallTreeCount
;
234 MmUnmapVirtualAddress (
235 _Inout_ PVOID
* VirtualAddress
,
236 _Inout_ PULONGLONG Size
241 /* Make sure parameters were passed in and are valid */
242 if ((VirtualAddress
) && (Size
) && (*Size
<= 0xFFFFFFFF))
244 /* Nothing to do if translation isn't active */
245 if (MmTranslationType
== BlNone
)
247 Status
= STATUS_SUCCESS
;
251 /* We don't support virtual memory yet @TODO */
252 EfiPrintf(L
"not yet implemented in %S\r\n", __FUNCTION__
);
254 Status
= STATUS_NOT_IMPLEMENTED
;
260 Status
= STATUS_INVALID_PARAMETER
;
268 BlMmUnmapVirtualAddressEx (
269 _In_ PVOID VirtualAddress
,
275 /* Increment call depth */
276 ++MmDescriptorCallTreeCount
;
278 /* Make sure all parameters are there */
279 if ((VirtualAddress
) && (Size
))
281 /* Unmap the virtual address */
282 Status
= MmUnmapVirtualAddress(&VirtualAddress
, &Size
);
284 /* Check if we actually had a virtual mapping active */
285 if ((NT_SUCCESS(Status
)) && (MmTranslationType
!= BlNone
))
287 /* We don't support virtual memory yet @TODO */
288 EfiPrintf(L
"not yet implemented in %S\r\n", __FUNCTION__
);
290 Status
= STATUS_NOT_IMPLEMENTED
;
296 Status
= STATUS_INVALID_PARAMETER
;
299 /* Cleanup descriptors and reduce depth */
300 MmMdFreeGlobalDescriptors();
301 --MmDescriptorCallTreeCount
;
306 BlMmTranslateVirtualAddress (
307 _In_ PVOID VirtualAddress
,
308 _Out_ PPHYSICAL_ADDRESS PhysicalAddress
311 /* Make sure arguments are present */
312 if (!(VirtualAddress
) || !(PhysicalAddress
))
317 /* Do the architecture-specific translation */
318 return MmArchTranslateVirtualAddress(VirtualAddress
, PhysicalAddress
, NULL
);
323 _In_ PBL_MEMORY_DATA MemoryData
,
324 _In_ BL_TRANSLATION_TYPE TranslationType
,
325 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
330 /* Take a reference */
331 MmDescriptorCallTreeCount
= 1;
333 /* Only support valid translation types */
334 if ((TranslationType
> BlPae
) || (LibraryParameters
->TranslationType
> BlPae
))
337 EfiPrintf(L
"Invalid translation types present\r\n");
338 Status
= STATUS_INVALID_PARAMETER
;
342 /* Initialize memory descriptors */
343 MmMdInitialize(0, LibraryParameters
);
345 /* Remember the page type we came in with */
346 MmOriginalTranslationType
= TranslationType
;
348 /* Initialize the physical page allocator */
349 Status
= MmPaInitialize(MemoryData
,
350 LibraryParameters
->MinimumAllocationCount
);
351 if (!NT_SUCCESS(Status
))
356 /* Initialize the memory tracker */
357 Status
= MmTrInitialize();
358 if (!NT_SUCCESS(Status
))
360 EfiPrintf(L
"TR Mm init failed: %lx\r\n", Status
);
366 /* Initialize paging, large pages, self-mapping, PAE, if needed */
367 Status
= MmArchInitialize(1,
370 LibraryParameters
->TranslationType
);
371 if (NT_SUCCESS(Status
))
373 /* Save the newly active transation type */
374 MmTranslationType
= LibraryParameters
->TranslationType
;
376 /* Initialize the heap allocator now */
377 Status
= MmHaInitialize(LibraryParameters
->MinimumHeapSize
,
378 LibraryParameters
->HeapAllocationAttributes
);
381 /* If Phase 1 init failed, bail out */
382 if (!NT_SUCCESS(Status
))
384 /* Kill everything set setup so far */
385 EfiPrintf(L
"Phase 1 Mm init failed: %lx\r\n", Status
);
393 /* Do we have too many descriptors? */
394 if (LibraryParameters
->DescriptorCount
> 512)
396 /* Switch to using a dynamic buffer instead */
397 EfiPrintf(L
"Warning: too many descriptors\r\n");
398 Status
= STATUS_NOT_IMPLEMENTED
;
400 //MmMdpSwitchToDynamicDescriptors(LibraryParameters->DescriptorCount);
403 /* Remove memory that the BCD says is bad */
404 BlMmRemoveBadMemory();
406 /* Now map all the memory regions as needed */
407 Status
= MmArchInitialize(2,
410 LibraryParameters
->TranslationType
);
411 if (NT_SUCCESS(Status
))
413 /* Initialize the block allocator */
414 Status
= MmBaInitialize();
417 /* Check if anything in phase 2 failed */
418 if (!NT_SUCCESS(Status
))
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;
426 /* Destroy the Phase 1 initialization */
434 /* Free the memory descriptors and return the initialization state */
435 MmMdFreeGlobalDescriptors();
436 --MmDescriptorCallTreeCount
;