2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/mm/pagealloc.c
5 * PURPOSE: Boot Library Memory Manager Page Allocator
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
14 typedef struct _BL_PA_REQUEST
16 BL_ADDRESS_RANGE BaseRange
;
17 BL_ADDRESS_RANGE VirtualRange
;
23 } BL_PA_REQUEST
, *PBL_PA_REQUEST
;
25 /* DATA VARIABLES ************************************************************/
27 ULONGLONG PapMaximumPhysicalPage
, PapMinimumPhysicalPage
;
29 ULONG PapMinimumAllocationCount
;
31 BOOLEAN PapInitializationStatus
;
33 BL_MEMORY_DESCRIPTOR_LIST MmMdlMappedAllocated
;
34 BL_MEMORY_DESCRIPTOR_LIST MmMdlMappedUnallocated
;
35 BL_MEMORY_DESCRIPTOR_LIST MmMdlFwAllocationTracker
;
36 BL_MEMORY_DESCRIPTOR_LIST MmMdlUnmappedAllocated
;
37 BL_MEMORY_DESCRIPTOR_LIST MmMdlUnmappedUnallocated
;
38 BL_MEMORY_DESCRIPTOR_LIST MmMdlReservedAllocated
;
39 BL_MEMORY_DESCRIPTOR_LIST MmMdlBadMemory
;
40 BL_MEMORY_DESCRIPTOR_LIST MmMdlTruncatedMemory
;
41 BL_MEMORY_DESCRIPTOR_LIST MmMdlPersistentMemory
;
42 BL_MEMORY_DESCRIPTOR_LIST MmMdlCompleteBadMemory
;
43 BL_MEMORY_DESCRIPTOR_LIST MmMdlFreeVirtual
;
44 BL_MEMORY_DESCRIPTOR_LIST MmMdlMappingTrackers
;
46 /* FUNCTIONS *****************************************************************/
49 BlpMmInitializeConstraints (
54 ULONGLONG LowestAddressValid
, HighestAddressValid
;
56 /* Check for LOWMEM */
57 Status
= BlGetBootOptionInteger(BlpApplicationEntry
.BcdData
,
58 BcdLibraryInteger_AvoidLowPhysicalMemory
,
60 if (NT_SUCCESS(Status
))
62 EfiPrintf(L
"/LOWMEM not supported\r\n");
63 return STATUS_NOT_IMPLEMENTED
;
66 /* Check for MAXMEM */
67 Status
= BlGetBootOptionInteger(BlpApplicationEntry
.BcdData
,
68 BcdLibraryInteger_TruncatePhysicalMemory
,
69 &HighestAddressValid
);
70 if (NT_SUCCESS(Status
))
72 EfiPrintf(L
"/MAXMEM not supported\r\n");
73 return STATUS_NOT_IMPLEMENTED
;
76 /* Return back to the caller */
77 return STATUS_SUCCESS
;
81 MmPapAllocateRegionFromMdl (
82 _In_ PBL_MEMORY_DESCRIPTOR_LIST NewList
,
83 _Out_opt_ PBL_MEMORY_DESCRIPTOR Descriptor
,
84 _In_ PBL_MEMORY_DESCRIPTOR_LIST CurrentList
,
85 _In_ PBL_PA_REQUEST Request
,
86 _In_ BL_MEMORY_TYPE Type
90 BL_MEMORY_DESCRIPTOR LocalDescriptor
= {{0}};
91 PBL_MEMORY_DESCRIPTOR FoundDescriptor
, TempDescriptor
;
92 PLIST_ENTRY ListHead
, NextEntry
;
93 BOOLEAN TopDown
, GotFwPages
;
94 EFI_PHYSICAL_ADDRESS EfiAddress
;
95 ULONGLONG LocalEndPage
, FoundEndPage
, LocalVirtualEndPage
;
97 /* Check if any parameters were not passed in correctly */
98 if ( !(CurrentList
) || !(Request
) || (!(NewList
) && !(Descriptor
)))
100 return STATUS_INVALID_PARAMETER
;
103 /* Set failure by default */
104 Status
= STATUS_NO_MEMORY
;
106 /* Take the head and next entry in the list, as appropriate */
107 ListHead
= CurrentList
->First
;
108 if (Request
->Type
& BL_MM_REQUEST_TOP_DOWN_TYPE
)
110 NextEntry
= ListHead
->Blink
;
115 NextEntry
= ListHead
->Flink
;
119 /* Loop through the list */
121 while (NextEntry
!= ListHead
)
123 /* Grab a descriptor */
124 FoundDescriptor
= CONTAINING_RECORD(NextEntry
,
125 BL_MEMORY_DESCRIPTOR
,
128 /* See if it matches the request */
129 if (MmMdFindSatisfyingRegion(FoundDescriptor
,
133 &Request
->VirtualRange
,
139 /* It does, get out */
143 /* It doesn't, move to the next appropriate entry */
146 NextEntry
= NextEntry
->Blink
;
150 NextEntry
= NextEntry
->Flink
;
154 /* Check if we exhausted the list */
155 if (NextEntry
== ListHead
)
157 EfiPrintf(L
"No matching memory found\r\n");
161 /* Copy all the flags that are not request flag */
162 LocalDescriptor
.Flags
= (Request
->Flags
& 0xFFFF0000) |
163 (LocalDescriptor
.Flags
& 0x0000FFFF);
165 /* Are we using the physical memory list, and are we OK with using firmware? */
166 if ((CurrentList
== &MmMdlUnmappedUnallocated
) &&
167 !((Request
->Flags
& BlMemoryNonFirmware
) ||
168 (LocalDescriptor
.Flags
& BlMemoryNonFirmware
)))
170 /* Allocate the requested address from EFI */
171 EfiAddress
= LocalDescriptor
.BasePage
<< PAGE_SHIFT
;
172 Status
= EfiAllocatePages(AllocateAddress
,
173 (ULONG
)LocalDescriptor
.PageCount
,
175 if (!NT_SUCCESS(Status
))
177 EfiPrintf(L
"EFI memory allocation failure\r\n");
181 /* Remember we got memory from EFI */
185 /* Remove the descriptor from the original list it was on */
186 MmMdRemoveDescriptorFromList(CurrentList
, FoundDescriptor
);
188 /* Are we allocating from the virtual memory list? */
189 if (CurrentList
== &MmMdlMappedUnallocated
)
191 EfiPrintf(L
"not yet implemented in %S\r\n", __FUNCTION__
);
193 return STATUS_NOT_IMPLEMENTED
;
196 /* Does the memory we received not exactly fall onto the beginning of its descriptor? */
197 if (LocalDescriptor
.BasePage
!= FoundDescriptor
->BasePage
)
199 TempDescriptor
= MmMdInitByteGranularDescriptor(FoundDescriptor
->Flags
,
200 FoundDescriptor
->Type
,
201 FoundDescriptor
->BasePage
,
202 FoundDescriptor
->VirtualPage
,
203 LocalDescriptor
.BasePage
-
204 FoundDescriptor
->BasePage
);
205 Status
= MmMdAddDescriptorToList(CurrentList
, TempDescriptor
, 0);
206 if (!NT_SUCCESS(Status
))
212 /* Does the memory we received not exactly fall onto the end of its descriptor? */
213 LocalEndPage
= LocalDescriptor
.PageCount
+ LocalDescriptor
.BasePage
;
214 FoundEndPage
= FoundDescriptor
->PageCount
+ FoundDescriptor
->BasePage
;
215 LocalVirtualEndPage
= LocalDescriptor
.VirtualPage
?
216 LocalDescriptor
.VirtualPage
+ LocalDescriptor
.PageCount
: 0;
217 if (LocalEndPage
!= FoundEndPage
)
219 TempDescriptor
= MmMdInitByteGranularDescriptor(FoundDescriptor
->Flags
,
220 FoundDescriptor
->Type
,
223 FoundEndPage
- LocalEndPage
);
224 Status
= MmMdAddDescriptorToList(CurrentList
, TempDescriptor
, 0);
225 if (!NT_SUCCESS(Status
))
231 /* We got the memory we needed */
232 Status
= STATUS_SUCCESS
;
234 /* Are we supposed to insert it into a new list? */
237 /* Copy the allocated region descriptor into the one we found */
238 FoundDescriptor
->BaseAddress
= LocalDescriptor
.BaseAddress
;
239 FoundDescriptor
->VirtualPage
= LocalDescriptor
.VirtualPage
;
240 FoundDescriptor
->PageCount
= LocalDescriptor
.PageCount
;
241 FoundDescriptor
->Type
= Type
;
242 FoundDescriptor
->Flags
= LocalDescriptor
.Flags
;
244 /* Remember if it came from EFI */
247 FoundDescriptor
->Flags
|= BlMemoryFirmware
;
250 /* Add the descriptor to the requested list */
251 Status
= MmMdAddDescriptorToList(NewList
, FoundDescriptor
, 0);
255 /* Free the descriptor, nobody wants to know about it anymore */
256 MmMdFreeDescriptor(FoundDescriptor
);
259 /* Return the allocation region back */
260 RtlCopyMemory(Descriptor
, &LocalDescriptor
, sizeof(LocalDescriptor
));
266 _In_ PBL_MEMORY_DESCRIPTOR_LIST NewList
,
267 _In_ PBL_MEMORY_DESCRIPTOR Descriptor
,
268 _In_ PBL_MEMORY_DESCRIPTOR_LIST CurrentList
,
269 _In_ PBL_PA_REQUEST Request
,
270 _In_ BL_MEMORY_TYPE MemoryType
275 /* Heap and page directory/table pages have a special flag */
276 if ((MemoryType
>= BlLoaderHeap
) && (MemoryType
<= BlLoaderReferencePage
))
278 Request
->Flags
|= BlMemorySpecial
;
281 /* Try to find a free region of RAM matching this range and request */
282 Request
->MemoryType
= BlConventionalMemory
;
283 Status
= MmPapAllocateRegionFromMdl(NewList
,
288 if (Status
== STATUS_NOT_FOUND
)
290 /* Need to re-synchronize the memory map and check other lists */
291 EfiPrintf(L
"No RAM found -- backup plan not yet implemented\r\n");
294 /* Did we get the region we wanted? */
295 if (NT_SUCCESS(Status
))
297 /* All good, return back */
301 /* Nope, we have to hunt for it elsewhere */
302 EfiPrintf(L
"TODO\r\n");
307 MmPapAllocatePhysicalPagesInRange (
308 _Inout_ PPHYSICAL_ADDRESS BaseAddress
,
309 _In_ BL_MEMORY_TYPE MemoryType
,
310 _In_ ULONGLONG Pages
,
311 _In_ ULONG Attributes
,
312 _In_ ULONG Alignment
,
313 _In_ PBL_MEMORY_DESCRIPTOR_LIST NewList
,
314 _In_opt_ PBL_ADDRESS_RANGE Range
,
319 BL_PA_REQUEST Request
;
320 BL_MEMORY_DESCRIPTOR Descriptor
;
322 /* Increase nesting depth */
323 ++MmDescriptorCallTreeCount
;
325 /* Bail out if no address was specified */
328 Status
= STATUS_INVALID_PARAMETER
;
332 /* Bail out if no page count was passed in, or a bad list was specified */
334 ((NewList
!= &MmMdlUnmappedAllocated
) &&
335 (NewList
!= &MmMdlPersistentMemory
)))
337 Status
= STATUS_INVALID_PARAMETER
;
341 /* Bail out if the passed in range is invalid */
342 if ((Range
) && (Range
->Minimum
>= Range
->Maximum
))
344 Status
= STATUS_INVALID_PARAMETER
;
348 /* Adjust alignment as needed */
354 /* Clear the virtual range */
355 Request
.VirtualRange
.Minimum
= 0;
356 Request
.VirtualRange
.Maximum
= 0;
358 /* Check if a fixed allocation was requested*/
359 if (Attributes
& BlMemoryFixed
)
361 /* Force the only available range to be the passed in address */
362 Request
.BaseRange
.Minimum
= BaseAddress
->QuadPart
>> PAGE_SHIFT
;
363 Request
.BaseRange
.Maximum
= Request
.BaseRange
.Minimum
+ Pages
- 1;
367 /* Otherwise, a manual range was specified, use it */
368 Request
.BaseRange
.Minimum
= Range
->Minimum
>> PAGE_SHIFT
;
369 Request
.BaseRange
.Maximum
= Request
.BaseRange
.Minimum
+
370 (Range
->Maximum
>> PAGE_SHIFT
) - 1;
374 /* Otherwise, use any possible range of pages */
375 Request
.BaseRange
.Minimum
= PapMinimumPhysicalPage
;
376 Request
.BaseRange
.Maximum
= MAXULONG
>> PAGE_SHIFT
;
379 /* Check if no type was specified, or if it was invalid */
381 (RangeType
& ~(BL_MM_REQUEST_TOP_DOWN_TYPE
| BL_MM_REQUEST_DEFAULT_TYPE
)))
383 /* Use default type */
384 Request
.Type
= BL_MM_REQUEST_DEFAULT_TYPE
;
388 /* Use the requested type */
389 Request
.Type
= RangeType
;
392 /* Capture the other request parameters */
393 Request
.Alignment
= Alignment
;
394 Request
.Pages
= Pages
;
395 Request
.Flags
= Attributes
;
396 Status
= MmPaAllocatePages(NewList
,
398 &MmMdlUnmappedUnallocated
,
401 if (NT_SUCCESS(Status
))
403 /* We got a descriptor back, return its address */
404 BaseAddress
->QuadPart
= Descriptor
.BasePage
<< PAGE_SHIFT
;
408 /* Restore the nesting depth */
409 MmMdFreeGlobalDescriptors();
410 --MmDescriptorCallTreeCount
;
415 MmPapAllocatePagesInRange (
416 _Inout_ PVOID
* PhysicalAddress
,
417 _In_ BL_MEMORY_TYPE MemoryType
,
418 _In_ ULONGLONG Pages
,
419 _In_ ULONG Attributes
,
420 _In_ ULONG Alignment
,
421 _In_opt_ PBL_ADDRESS_RANGE Range
,
426 PHYSICAL_ADDRESS BaseAddress
;
428 /* Increment nesting depth */
429 ++MmDescriptorCallTreeCount
;
431 /* Check for missing parameters or invalid range */
432 if (!(PhysicalAddress
) ||
434 ((Range
) && (Range
->Minimum
>= Range
->Maximum
)))
436 Status
= STATUS_INVALID_PARAMETER
;
440 /* What translation mode are we using? */
441 if (MmTranslationType
!= BlNone
)
443 /* We don't support virtual memory yet @TODO */
444 EfiPrintf(L
"not yet implemented in %S\r\n", __FUNCTION__
);
446 Status
= STATUS_NOT_IMPLEMENTED
;
451 /* Check if this is a fixed allocation */
452 BaseAddress
.QuadPart
= (Attributes
& BlMemoryFixed
) ? (ULONG_PTR
)*PhysicalAddress
: 0;
454 /* Allocate the pages */
455 Status
= MmPapAllocatePhysicalPagesInRange(&BaseAddress
,
460 (&MmMdlMappedAllocated
!=
461 &MmMdlPersistentMemory
) ?
462 &MmMdlUnmappedAllocated
:
463 &MmMdlMappedAllocated
,
467 /* Return the allocated address */
468 *PhysicalAddress
= (PVOID
)BaseAddress
.LowPart
;
472 /* Restore the nesting depth */
473 MmMdFreeGlobalDescriptors();
474 --MmDescriptorCallTreeCount
;
480 __in PBL_MEMORY_DATA BootMemoryData
,
481 __in ULONG MinimumAllocationCount
485 ULONG ExistingDescriptors
, FinalOffset
;
486 PBL_MEMORY_DESCRIPTOR Descriptor
, NewDescriptor
;
488 /* Initialize physical allocator variables */
489 PapMaximumPhysicalPage
= 0xFFFFFFFFFFFFF;
490 PapMinimumAllocationCount
= MinimumAllocationCount
;
491 PapMinimumPhysicalPage
= 0;
493 /* Initialize all the lists */
494 MmMdInitializeListHead(&MmMdlMappedAllocated
);
495 MmMdInitializeListHead(&MmMdlMappedUnallocated
);
496 MmMdInitializeListHead(&MmMdlFwAllocationTracker
);
497 MmMdInitializeListHead(&MmMdlUnmappedAllocated
);
498 MmMdInitializeListHead(&MmMdlReservedAllocated
);
499 MmMdInitializeListHead(&MmMdlBadMemory
);
500 MmMdInitializeListHead(&MmMdlTruncatedMemory
);
501 MmMdInitializeListHead(&MmMdlPersistentMemory
);
502 MmMdInitializeListHead(&MmMdlUnmappedUnallocated
);
503 MmMdInitializeListHead(&MmMdlCompleteBadMemory
);
505 /* Get the BIOS memory map */
506 Status
= MmFwGetMemoryMap(&MmMdlUnmappedUnallocated
,
507 BL_MM_FLAG_USE_FIRMWARE_FOR_MEMORY_MAP_BUFFERS
|
508 BL_MM_FLAG_REQUEST_COALESCING
);
509 if (NT_SUCCESS(Status
))
512 PLIST_ENTRY listHead
, nextEntry
;
514 /* Loop the NT firmware memory list */
515 EfiPrintf(L
"NT MEMORY MAP\n\r\n");
516 listHead
= &MmMdlUnmappedUnallocated
.ListHead
;
517 nextEntry
= listHead
->Flink
;
518 while (listHead
!= nextEntry
)
520 Descriptor
= CONTAINING_RECORD(nextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
522 EfiPrintf(L
"Type: %08lX Flags: %08lX Base: 0x%016I64X End: 0x%016I64X\r\n",
525 Descriptor
->BasePage
<< PAGE_SHIFT
,
526 (Descriptor
->BasePage
+ Descriptor
->PageCount
) << PAGE_SHIFT
);
528 nextEntry
= nextEntry
->Flink
;
533 * Because BL supports cross x86-x64 application launches and a LIST_ENTRY
534 * is of variable size, care must be taken here to ensure that we see a
535 * consistent view of descriptors. BL uses some offset magic to figure out
536 * where the data actually starts, since everything is ULONGLONG past the
539 FinalOffset
= BootMemoryData
->MdListOffset
+ BootMemoryData
->DescriptorOffset
;
540 Descriptor
= (PBL_MEMORY_DESCRIPTOR
)((ULONG_PTR
)BootMemoryData
+ FinalOffset
-
541 FIELD_OFFSET(BL_MEMORY_DESCRIPTOR
, BasePage
));
543 /* Scan all of them */
544 ExistingDescriptors
= BootMemoryData
->DescriptorCount
;
545 while (ExistingDescriptors
!= 0)
547 /* Remove this region from our free memory MDL */
548 //EfiPrintf(L"Handling existing descriptor: %llx %llx\r\n", Descriptor->BasePage, Descriptor->PageCount);
549 Status
= MmMdRemoveRegionFromMdlEx(&MmMdlUnmappedUnallocated
,
550 BL_MM_REMOVE_PHYSICAL_REGION_FLAG
,
551 Descriptor
->BasePage
,
552 Descriptor
->PageCount
,
554 if (!NT_SUCCESS(Status
))
556 return STATUS_INVALID_PARAMETER
;
559 /* Build a descriptor for it */
560 NewDescriptor
= MmMdInitByteGranularDescriptor(Descriptor
->Flags
,
562 Descriptor
->BasePage
,
563 Descriptor
->VirtualPage
,
564 Descriptor
->PageCount
);
567 return STATUS_NO_MEMORY
;
570 /* And add this region to the reserved & allocated MDL */
571 Status
= MmMdAddDescriptorToList(&MmMdlReservedAllocated
, NewDescriptor
, 0);
572 if (!NT_SUCCESS(Status
))
577 /* Move on to the next descriptor */
578 ExistingDescriptors
--;
579 Descriptor
= (PBL_MEMORY_DESCRIPTOR
)((ULONG_PTR
)Descriptor
+ BootMemoryData
->DescriptorSize
);
582 /* We are done, so check for any RAM constraints which will make us truncate memory */
583 Status
= BlpMmInitializeConstraints();
584 if (NT_SUCCESS(Status
))
586 /* The Page Allocator has initialized */
587 PapInitializationStatus
= TRUE
;
588 Status
= STATUS_SUCCESS
;
597 BlMmAllocatePhysicalPages(
598 _In_ PPHYSICAL_ADDRESS Address
,
599 _In_ BL_MEMORY_TYPE MemoryType
,
600 _In_ ULONGLONG PageCount
,
601 _In_ ULONG Attributes
,
605 /* Call the physical allocator */
606 return MmPapAllocatePhysicalPagesInRange(Address
,
611 &MmMdlUnmappedAllocated
,
617 MmPapFreePhysicalPages (
618 _In_ ULONG WhichList
,
619 _In_ ULONGLONG PageCount
,
620 _In_ PHYSICAL_ADDRESS Address
623 PBL_MEMORY_DESCRIPTOR Descriptor
;
625 ULONG DescriptorFlags
, Flags
;
626 BOOLEAN DontFree
, HasPageData
;
627 BL_LIBRARY_PARAMETERS LibraryParameters
;
630 /* Set some defaults */
631 Flags
= BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
;
635 /* Only page-aligned addresses are accepted */
636 if (Address
.QuadPart
& (PAGE_SIZE
- 1))
638 EfiPrintf(L
"free mem fail 1\r\n");
639 return STATUS_INVALID_PARAMETER
;
642 /* Try to find the descriptor containing this address */
643 Page
= Address
.QuadPart
>> PAGE_SHIFT
;
644 Descriptor
= MmMdFindDescriptor(WhichList
,
645 BL_MM_REMOVE_PHYSICAL_REGION_FLAG
,
649 EfiPrintf(L
"free mem fail 2\r\n");
650 return STATUS_INVALID_PARAMETER
;
653 /* If a page count was given, it must match, unless it's coalesced */
654 DescriptorFlags
= Descriptor
->Flags
;
655 if (!(DescriptorFlags
& BlMemoryCoalesced
) &&
656 (PageCount
) && (PageCount
!= Descriptor
->PageCount
))
658 EfiPrintf(L
"free mem fail 3\r\n");
659 return STATUS_INVALID_PARAMETER
;
662 /* Check if this is persistent memory in teardown status */
663 if ((PapInitializationStatus
== 2) &&
664 (DescriptorFlags
& BlMemoryPersistent
))
666 /* Then we should keep it */
671 /* Mark it as non-persistent, since we're freeing it */
672 Descriptor
->Flags
&= ~BlMemoryPersistent
;
675 /* Check if this memory contains paging data */
676 if ((Descriptor
->Type
== BlLoaderPageDirectory
) ||
677 (Descriptor
->Type
== BlLoaderReferencePage
))
682 /* Check if a page count was given */
685 /* The pages must fit within the descriptor */
686 if ((PageCount
+ Page
- Descriptor
->BasePage
) > Descriptor
->PageCount
)
688 EfiPrintf(L
"free mem fail 4\r\n");
689 return STATUS_INVALID_PARAMETER
;
694 /* No page count given, so the address must be at the beginning then */
695 if (Descriptor
->BasePage
!= Page
)
697 EfiPrintf(L
"free mem fail 5\r\n");
698 return STATUS_INVALID_PARAMETER
;
701 /* And we'll use the page count in the descriptor */
702 PageCount
= Descriptor
->PageCount
;
705 /* Copy library parameters since we will read them */
706 RtlCopyMemory(&LibraryParameters
,
707 &BlpLibraryParameters
,
708 sizeof(LibraryParameters
));
710 /* Check if this is teardown */
711 if (PapInitializationStatus
== 2)
713 EfiPrintf(L
"Case 2 not yet handled!\r\n");
714 return STATUS_NOT_SUPPORTED
;
718 /* Caller wants memory to be freed -- should we zero it? */
719 if (!(HasPageData
) &&
720 (LibraryParameters
.LibraryFlags
&
721 BL_LIBRARY_FLAG_ZERO_HEAP_ALLOCATIONS_ON_FREE
))
723 EfiPrintf(L
"Freeing zero data not yet handled!\r\n");
724 return STATUS_NOT_SUPPORTED
;
728 /* Now call into firmware to actually free the physical pages */
729 Status
= MmFwFreePages(Page
, PageCount
);
730 if (!NT_SUCCESS(Status
))
732 EfiPrintf(L
"free mem fail 6\r\n");
736 /* Remove the firmware flags */
737 Descriptor
->Flags
&= ~(BlMemoryNonFirmware
|
741 /* If we're not actually freeing, don't coalesce with anyone nearby */
744 Flags
|= BL_MM_ADD_DESCRIPTOR_NEVER_COALESCE_FLAG
;
747 /* Check if the entire allocation is being freed */
748 if (PageCount
== Descriptor
->PageCount
)
750 /* Remove the descriptor from the allocated list */
751 MmMdRemoveDescriptorFromList(&MmMdlUnmappedAllocated
, Descriptor
);
753 /* Mark the entire descriptor as free */
754 Descriptor
->Type
= BlConventionalMemory
;
758 /* Init a descriptor for what we're actually freeing */
759 Descriptor
= MmMdInitByteGranularDescriptor(Descriptor
->Flags
,
760 BlConventionalMemory
,
766 return STATUS_NO_MEMORY
;
769 /* Remove the region from the existing descriptor */
770 Status
= MmMdRemoveRegionFromMdlEx(&MmMdlUnmappedAllocated
,
771 BL_MM_REMOVE_PHYSICAL_REGION_FLAG
,
775 if (!NT_SUCCESS(Status
))
781 /* Add the new descriptor into in the list (or the old, repurposed one) */
782 Descriptor
->Flags
&= ~BlMemoryCoalesced
;
783 return MmMdAddDescriptorToList(&MmMdlUnmappedUnallocated
, Descriptor
, Flags
);
787 BlMmFreePhysicalPages (
788 _In_ PHYSICAL_ADDRESS Address
791 /* Call the physical allocator */
792 return MmPapFreePhysicalPages(BL_MM_INCLUDE_UNMAPPED_ALLOCATED
, 0, Address
);
801 PHYSICAL_ADDRESS PhysicalAddress
;
803 /* Handle virtual memory scenario */
804 if (MmTranslationType
!= BlNone
)
806 EfiPrintf(L
"Unimplemented virtual path\r\n");
807 return STATUS_SUCCESS
;
810 /* Physical memory should be in the unmapped allocated list */
811 if (WhichList
!= BL_MM_INCLUDE_PERSISTENT_MEMORY
)
813 WhichList
= BL_MM_INCLUDE_UNMAPPED_ALLOCATED
;
816 /* Free it from there */
817 PhysicalAddress
.QuadPart
= (ULONG_PTR
)Address
;
818 return MmPapFreePhysicalPages(WhichList
, 0, PhysicalAddress
);
823 _In_ PLIST_ENTRY MemoryMap
,
824 _In_ PBL_IMAGE_PARAMETERS MemoryParameters
,
825 _In_ ULONG WhichTypes
,
829 BL_MEMORY_DESCRIPTOR_LIST FirmwareMdList
, FullMdList
;
830 BOOLEAN DoFirmware
, DoPersistent
, DoTruncated
, DoBad
;
831 BOOLEAN DoReserved
, DoUnmapUnalloc
, DoUnmapAlloc
;
832 BOOLEAN DoMapAlloc
, DoMapUnalloc
, DoFirmware2
;
833 ULONG LoopCount
, MdListCount
, MdListSize
, Used
;
836 /* Initialize the firmware list if we use it */
837 MmMdInitializeListHead(&FirmwareMdList
);
839 /* Make sure we got our input parameters */
840 if (!(MemoryMap
) || !(MemoryParameters
))
842 return STATUS_INVALID_PARAMETER
;
845 /* Either ask for firmware memory, or don't. Not neither */
846 if ((WhichTypes
& ~BL_MM_INCLUDE_NO_FIRMWARE_MEMORY
) &&
847 (WhichTypes
& ~BL_MM_INCLUDE_ONLY_FIRMWARE_MEMORY
))
849 return STATUS_INVALID_PARAMETER
;
852 /* Either ask for firmware memory, or don't. Not both */
853 if ((WhichTypes
& BL_MM_INCLUDE_NO_FIRMWARE_MEMORY
) &&
854 (WhichTypes
& BL_MM_INCLUDE_ONLY_FIRMWARE_MEMORY
))
856 return STATUS_INVALID_PARAMETER
;
859 /* Initialize the memory map list */
860 InitializeListHead(MemoryMap
);
862 /* Check which types of memory to dump */
863 DoFirmware
= WhichTypes
& BL_MM_INCLUDE_FIRMWARE_MEMORY
;
864 DoPersistent
= WhichTypes
& BL_MM_INCLUDE_PERSISTENT_MEMORY
;
865 DoTruncated
= WhichTypes
& BL_MM_INCLUDE_TRUNCATED_MEMORY
;
866 DoBad
= WhichTypes
& BL_MM_INCLUDE_BAD_MEMORY
;
867 DoReserved
= WhichTypes
& BL_MM_INCLUDE_RESERVED_ALLOCATED
;
868 DoUnmapUnalloc
= WhichTypes
& BL_MM_INCLUDE_UNMAPPED_UNALLOCATED
;
869 DoUnmapAlloc
= WhichTypes
& BL_MM_INCLUDE_UNMAPPED_ALLOCATED
;
870 DoMapAlloc
= WhichTypes
& BL_MM_INCLUDE_MAPPED_ALLOCATED
;
871 DoMapUnalloc
= WhichTypes
& BL_MM_INCLUDE_MAPPED_UNALLOCATED
;
872 DoFirmware2
= WhichTypes
& BL_MM_INCLUDE_FIRMWARE_MEMORY_2
;
874 /* Begin the attempt loop */
878 /* Count how many entries we will need */
880 if (DoMapAlloc
) MdListCount
= MmMdCountList(&MmMdlMappedAllocated
);
881 if (DoMapUnalloc
) MdListCount
+= MmMdCountList(&MmMdlMappedUnallocated
);
882 if (DoUnmapAlloc
) MdListCount
+= MmMdCountList(&MmMdlUnmappedAllocated
);
883 if (DoUnmapUnalloc
) MdListCount
+= MmMdCountList(&MmMdlUnmappedUnallocated
);
884 if (DoReserved
) MdListCount
+= MmMdCountList(&MmMdlReservedAllocated
);
885 if (DoBad
) MdListCount
+= MmMdCountList(&MmMdlBadMemory
);
886 if (DoTruncated
) MdListCount
+= MmMdCountList(&MmMdlTruncatedMemory
);
887 if (DoPersistent
) MdListCount
+= MmMdCountList(&MmMdlPersistentMemory
);
889 /* Plus firmware entries */
892 /* Free the previous entries, if any */
893 MmMdFreeList(&FirmwareMdList
);
895 /* Get the firmware map, coalesced */
896 Status
= MmFwGetMemoryMap(&FirmwareMdList
,
897 BL_MM_FLAG_REQUEST_COALESCING
);
898 if (!NT_SUCCESS(Status
))
903 /* We overwrite, since this type is exclusive */
904 MdListCount
= MmMdCountList(&FirmwareMdList
);
907 /* Plus firmware entries-2 */
910 /* Free the previous entries, if any */
911 MmMdFreeList(&FirmwareMdList
);
913 /* Get the firmware map, uncoalesced */
914 Status
= MmFwGetMemoryMap(&FirmwareMdList
, 0);
915 if (!NT_SUCCESS(Status
))
920 /* We overwrite, since this type is exclusive */
921 MdListCount
= MmMdCountList(&FirmwareMdList
);
924 /* If there's no descriptors, we're done */
927 Status
= STATUS_SUCCESS
;
931 /* Check if the buffer we have is big enough */
932 if (MemoryParameters
->BufferSize
>=
933 (sizeof(BL_MEMORY_DESCRIPTOR
) * MdListCount
))
938 /* It's not, allocate it, with a slack of 4 extra descriptors */
939 MdListSize
= sizeof(BL_MEMORY_DESCRIPTOR
) * (MdListCount
+ 4);
941 /* Except if we weren't asked to */
942 if (!(Flags
& BL_MM_ADD_DESCRIPTOR_ALLOCATE_FLAG
))
944 MemoryParameters
->BufferSize
= MdListSize
;
945 Status
= STATUS_BUFFER_TOO_SMALL
;
949 /* Has it been less than 4 times we've tried this? */
950 if (++LoopCount
<= 4)
952 /* Free the previous attempt, if any */
953 if (MemoryParameters
->BufferSize
)
955 BlMmFreeHeap(MemoryParameters
->Buffer
);
958 /* Allocate a new buffer */
959 MemoryParameters
->BufferSize
= MdListSize
;
960 MemoryParameters
->Buffer
= BlMmAllocateHeap(MdListSize
);
961 if (MemoryParameters
->Buffer
)
968 /* If we got here, we're out of memory after 4 attempts */
969 Status
= STATUS_NO_MEMORY
;
973 /* We should have a buffer by now... */
974 if (MemoryParameters
->Buffer
)
977 RtlZeroMemory(MemoryParameters
->Buffer
,
978 MdListCount
* sizeof(BL_MEMORY_DESCRIPTOR
));
981 /* Initialize our list of descriptors */
982 MmMdInitializeList(&FullMdList
, 0, MemoryMap
);
985 /* Handle mapped, allocated */
988 Status
= MmMdCopyList(&FullMdList
,
989 &MmMdlMappedAllocated
,
990 MemoryParameters
->Buffer
,
996 /* Handle mapped, unallocated */
999 Status
= MmMdCopyList(&FullMdList
,
1000 &MmMdlMappedUnallocated
,
1001 MemoryParameters
->Buffer
,
1007 /* Handle unmapped, allocated */
1010 Status
= MmMdCopyList(&FullMdList
,
1011 &MmMdlUnmappedAllocated
,
1012 MemoryParameters
->Buffer
,
1018 /* Handle unmapped, unallocated */
1021 Status
= MmMdCopyList(&FullMdList
,
1022 &MmMdlUnmappedUnallocated
,
1023 MemoryParameters
->Buffer
,
1029 /* Handle reserved, allocated */
1032 Status
= MmMdCopyList(&FullMdList
,
1033 &MmMdlReservedAllocated
,
1034 MemoryParameters
->Buffer
,
1043 Status
= MmMdCopyList(&FullMdList
,
1045 MemoryParameters
->Buffer
,
1051 /* Handle truncated */
1054 Status
= MmMdCopyList(&FullMdList
,
1055 &MmMdlTruncatedMemory
,
1056 MemoryParameters
->Buffer
,
1062 /* Handle persistent */
1065 Status
= MmMdCopyList(&FullMdList
,
1066 &MmMdlPersistentMemory
,
1067 MemoryParameters
->Buffer
,
1073 /* Handle firmware */
1076 Status
= MmMdCopyList(&FullMdList
,
1078 MemoryParameters
->Buffer
,
1084 /* Handle firmware2 */
1087 Status
= MmMdCopyList(&FullMdList
,
1089 MemoryParameters
->Buffer
,
1095 /* Add up the final size */
1096 Status
= RtlULongLongToULong(Used
* sizeof(BL_MEMORY_DESCRIPTOR
),
1097 &MemoryParameters
->ActualSize
);
1100 MmMdFreeList(&FirmwareMdList
);
1105 MmPaReleaseSelfMapPages (
1106 _In_ PHYSICAL_ADDRESS Address
1109 PBL_MEMORY_DESCRIPTOR Descriptor
;
1113 /* Only page-aligned addresses are accepted */
1114 if (Address
.QuadPart
& (PAGE_SIZE
- 1))
1116 EfiPrintf(L
"free mem fail 1\r\n");
1117 return STATUS_INVALID_PARAMETER
;
1120 /* Get the base page, and find a descriptor that matches */
1121 BasePage
= Address
.QuadPart
>> PAGE_SHIFT
;
1122 Descriptor
= MmMdFindDescriptor(BL_MM_INCLUDE_UNMAPPED_UNALLOCATED
,
1123 BL_MM_REMOVE_PHYSICAL_REGION_FLAG
,
1125 if (!(Descriptor
) || (Descriptor
->BasePage
!= BasePage
))
1127 return STATUS_INVALID_PARAMETER
;
1130 /* Free the physical pages */
1131 Status
= MmFwFreePages(BasePage
, Descriptor
->PageCount
);
1132 if (!NT_SUCCESS(Status
))
1137 /* Remove the firmware flags */
1138 Descriptor
->Flags
&= ~(BlMemoryNonFirmware
|
1140 BlMemoryPersistent
);
1142 /* Set it as free memory */
1143 Descriptor
->Type
= BlConventionalMemory
;
1145 /* Create a new descriptor that's free memory, covering the old range */
1146 Descriptor
= MmMdInitByteGranularDescriptor(0,
1147 BlConventionalMemory
,
1150 Descriptor
->PageCount
);
1153 return STATUS_NO_MEMORY
;
1156 /* Insert it into the virtual free list */
1157 return MmMdAddDescriptorToList(&MmMdlFreeVirtual
,
1159 BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
|
1160 BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG
);
1164 MmPaReserveSelfMapPages (
1165 _Inout_ PPHYSICAL_ADDRESS PhysicalAddress
,
1166 _In_ ULONG Alignment
,
1167 _In_ ULONG PageCount
1171 BL_PA_REQUEST Request
;
1172 BL_MEMORY_DESCRIPTOR Descriptor
;
1174 /* Increment descriptor usage count */
1175 ++MmDescriptorCallTreeCount
;
1177 /* Bail if we don't have an address */
1178 if (!PhysicalAddress
)
1180 Status
= STATUS_INVALID_PARAMETER
;
1184 /* Make a request for the required number of self-map pages */
1185 Request
.BaseRange
.Minimum
= PapMinimumPhysicalPage
;
1186 Request
.BaseRange
.Maximum
= 0xFFFFFFFF >> PAGE_SHIFT
;
1187 Request
.VirtualRange
.Minimum
= 0;
1188 Request
.VirtualRange
.Maximum
= 0;
1189 Request
.Pages
= PageCount
;
1190 Request
.Alignment
= Alignment
;
1191 Request
.Type
= BL_MM_REQUEST_DEFAULT_TYPE
;
1193 Status
= MmPaAllocatePages(&MmMdlUnmappedUnallocated
,
1195 &MmMdlUnmappedUnallocated
,
1198 if (!NT_SUCCESS(Status
))
1203 /* Remove this region from free virtual memory */
1204 Status
= MmMdRemoveRegionFromMdlEx(&MmMdlFreeVirtual
,
1205 BL_MM_REMOVE_VIRTUAL_REGION_FLAG
,
1206 Descriptor
.BasePage
,
1207 Descriptor
.PageCount
,
1209 if (!NT_SUCCESS(Status
))
1214 /* Return the physical address */
1215 PhysicalAddress
->QuadPart
= Descriptor
.BasePage
<< PAGE_SHIFT
;
1218 /* Free global descriptors and reduce the count by one */
1219 MmMdFreeGlobalDescriptors();
1220 --MmDescriptorCallTreeCount
;