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 MmSelectMappingAddress (
133 _Out_ PVOID
* MappingAddress
,
135 _In_ ULONG AllocationAttributes
,
137 _In_ PHYSICAL_ADDRESS PhysicalAddress
140 /* Are we in physical mode? */
141 if (MmTranslationType
== BlNone
)
143 /* Just return the physical address as the mapping address */
144 *MappingAddress
= (PVOID
)PhysicalAddress
.LowPart
;
145 return STATUS_SUCCESS
;
148 /* Have to allocate physical pages */
149 EfiPrintf(L
"VM Todo\r\n");
150 return STATUS_NOT_IMPLEMENTED
;
154 MmMapPhysicalAddress (
155 _Inout_ PPHYSICAL_ADDRESS PhysicalAddress
,
156 _Out_ PVOID VirtualAddress
,
157 _Inout_ PULONGLONG Size
,
158 _In_ ULONG CacheAttributes
161 ULONGLONG MappingSize
;
163 /* Fail if any parameters are missing */
164 if (!(PhysicalAddress
) || !(VirtualAddress
) || !(Size
))
166 return STATUS_INVALID_PARAMETER
;
169 /* Fail if the size is over 32-bits */
171 if (MappingSize
> 0xFFFFFFFF)
173 return STATUS_INVALID_PARAMETER
;
176 /* Nothing to do if we're in physical mode */
177 if (MmTranslationType
== BlNone
)
179 return STATUS_SUCCESS
;
182 /* Can't use virtual memory in real mode */
183 if (CurrentExecutionContext
->Mode
== BlRealMode
)
185 return STATUS_UNSUCCESSFUL
;
188 EfiPrintf(L
"VM todo\r\n");
189 return STATUS_NOT_IMPLEMENTED
;
193 BlMmMapPhysicalAddressEx (
194 _In_ PVOID
* VirtualAddress
,
197 _In_ PHYSICAL_ADDRESS PhysicalAddress
201 PVOID MappingAddress
;
202 PHYSICAL_ADDRESS MappedAddress
;
205 UCHAR CacheAttributes
;
207 /* Increase call depth */
208 ++MmDescriptorCallTreeCount
;
210 /* Check if any parameters are missing */
211 if (!(VirtualAddress
) || !(Size
))
213 Status
= STATUS_INVALID_PARAMETER
;
217 /* Check for fixed allocation without an actual address */
218 if ((Flags
& BlMemoryFixed
) &&
219 (PhysicalAddress
.QuadPart
== -1) &&
222 Status
= STATUS_INVALID_PARAMETER
;
226 /* Check for invalid requirement flag, if one is present */
227 if (((Flags
& BlMemoryValidAllocationAttributes
) != BlMemoryFixed
) &&
228 ((Flags
& BlMemoryValidAllocationAttributes
) != BlMemoryNonFixed
) &&
229 (Flags
& BlMemoryValidAllocationAttributes
))
231 Status
= STATUS_INVALID_PARAMETER
;
235 /* Check for invalid cache attribute flags */
236 if (((Flags
& BlMemoryValidCacheAttributeMask
) - 1) &
237 (Flags
& BlMemoryValidCacheAttributeMask
))
239 Status
= STATUS_INVALID_PARAMETER
;
243 /* Select an address to map this at */
244 Status
= MmSelectMappingAddress(&MappingAddress
,
246 Flags
& BlMemoryValidAllocationAttributes
,
249 if (!NT_SUCCESS(Status
))
254 /* Map the selected address, using the appropriate caching attributes */
255 MappedAddress
= PhysicalAddress
;
257 CacheAttributes
= ((Flags
& BlMemoryValidCacheAttributeMask
) != 0x20) ?
258 (Flags
& BlMemoryValidCacheAttributeMask
) : 0;
259 Status
= MmMapPhysicalAddress(&MappedAddress
,
263 if (!NT_SUCCESS(Status
))
268 /* Compute the final address where the mapping was made */
269 MappedBase
= (PVOID
)((ULONG_PTR
)MappingAddress
+
270 PhysicalAddress
.LowPart
-
271 MappedAddress
.LowPart
);
273 /* Check if we're in physical or virtual mode */
274 if (MmTranslationType
!= BlNone
)
276 /* For virtual memory, there's more to do */
277 EfiPrintf(L
"VM not supported for mapping\r\n");
278 Status
= STATUS_NOT_IMPLEMENTED
;
282 /* Return the mapped virtual address */
283 Status
= STATUS_SUCCESS
;
284 *VirtualAddress
= MappedBase
;
287 /* Cleanup descriptors and reduce depth */
288 MmMdFreeGlobalDescriptors();
289 --MmDescriptorCallTreeCount
;
294 MmUnmapVirtualAddress (
295 _Inout_ PVOID
* VirtualAddress
,
296 _Inout_ PULONGLONG Size
301 /* Make sure parameters were passed in and are valid */
302 if ((VirtualAddress
) && (Size
) && (*Size
<= 0xFFFFFFFF))
304 /* Nothing to do if translation isn't active */
305 if (MmTranslationType
== BlNone
)
307 Status
= STATUS_SUCCESS
;
311 Status
= STATUS_NOT_IMPLEMENTED
;
316 Status
= STATUS_INVALID_PARAMETER
;
324 BlMmUnmapVirtualAddressEx (
325 _In_ PVOID VirtualAddress
,
331 /* Increment call depth */
332 ++MmDescriptorCallTreeCount
;
334 /* Make sure all parameters are there */
335 if ((VirtualAddress
) && (Size
))
337 /* Unmap the virtual address */
338 Status
= MmUnmapVirtualAddress(&VirtualAddress
, &Size
);
340 /* Check if we actually had a virtual mapping active */
341 if ((NT_SUCCESS(Status
)) && (MmTranslationType
!= BlNone
))
344 EfiPrintf(L
"unhandled virtual path\r\n");
345 Status
= STATUS_NOT_IMPLEMENTED
;
351 Status
= STATUS_INVALID_PARAMETER
;
354 /* Cleanup descriptors and reduce depth */
355 MmMdFreeGlobalDescriptors();
356 --MmDescriptorCallTreeCount
;
361 BlMmTranslateVirtualAddress (
362 _In_ PVOID VirtualAddress
,
363 _Out_ PPHYSICAL_ADDRESS PhysicalAddress
366 /* Make sure arguments are present */
367 if (!(VirtualAddress
) || !(PhysicalAddress
))
372 /* Do the architecture-specific translation */
373 return MmArchTranslateVirtualAddress(VirtualAddress
, PhysicalAddress
, NULL
);
378 _In_ PBL_MEMORY_DATA MemoryData
,
379 _In_ BL_TRANSLATION_TYPE TranslationType
,
380 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
385 /* Take a reference */
386 MmDescriptorCallTreeCount
= 1;
388 /* Only support valid translation types */
389 if ((TranslationType
> BlPae
) || (LibraryParameters
->TranslationType
> BlPae
))
392 EfiPrintf(L
"Invalid translation types present\r\n");
393 Status
= STATUS_INVALID_PARAMETER
;
397 /* Initialize memory descriptors */
398 MmMdInitialize(0, LibraryParameters
);
400 /* Remember the page type we came in with */
401 MmOriginalTranslationType
= TranslationType
;
403 /* Initialize the physical page allocator */
404 Status
= MmPaInitialize(MemoryData
,
405 LibraryParameters
->MinimumAllocationCount
);
406 if (!NT_SUCCESS(Status
))
411 /* Initialize the memory tracker */
412 Status
= MmTrInitialize();
413 if (!NT_SUCCESS(Status
))
415 EfiPrintf(L
"TR Mm init failed: %lx\r\n", Status
);
421 /* Initialize paging, large pages, self-mapping, PAE, if needed */
422 Status
= MmArchInitialize(1,
425 LibraryParameters
->TranslationType
);
426 if (NT_SUCCESS(Status
))
428 /* Save the newly active transation type */
429 MmTranslationType
= LibraryParameters
->TranslationType
;
431 /* Initialize the heap allocator now */
432 Status
= MmHaInitialize(LibraryParameters
->MinimumHeapSize
,
433 LibraryParameters
->HeapAllocationAttributes
);
436 /* If Phase 1 init failed, bail out */
437 if (!NT_SUCCESS(Status
))
439 /* Kill everything set setup so far */
440 EfiPrintf(L
"Phase 1 Mm init failed: %lx\r\n", Status
);
448 /* Do we have too many descriptors? */
449 if (LibraryParameters
->DescriptorCount
> 512)
451 /* Switch to using a dynamic buffer instead */
452 EfiPrintf(L
"Warning: too many descriptors\r\n");
453 Status
= STATUS_NOT_IMPLEMENTED
;
455 //MmMdpSwitchToDynamicDescriptors(LibraryParameters->DescriptorCount);
458 /* Remove memory that the BCD says is bad */
459 BlMmRemoveBadMemory();
461 /* Now map all the memory regions as needed */
462 Status
= MmArchInitialize(2,
465 LibraryParameters
->TranslationType
);
466 if (NT_SUCCESS(Status
))
468 /* Initialize the block allocator */
469 Status
= MmBaInitialize();
472 /* Check if anything in phase 2 failed */
473 if (!NT_SUCCESS(Status
))
475 /* Go back to static descriptors and kill the heap */
476 EfiPrintf(L
"Phase 2 Mm init failed: %lx\r\n", Status
);
477 //MmMdpSwitchToStaticDescriptors();
478 //HapInitializationStatus = 0;
479 //++MmDescriptorCallTreeCount;
481 /* Destroy the Phase 1 initialization */
489 /* Free the memory descriptors and return the initialization state */
490 MmMdFreeGlobalDescriptors();
491 --MmDescriptorCallTreeCount
;