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
"Virtual memory not yet supported\r\n");
192 return STATUS_NOT_IMPLEMENTED
;
195 /* Does the memory we received not exactly fall onto the beginning of its descriptor? */
196 if (LocalDescriptor
.BasePage
!= FoundDescriptor
->BasePage
)
198 TempDescriptor
= MmMdInitByteGranularDescriptor(FoundDescriptor
->Flags
,
199 FoundDescriptor
->Type
,
200 FoundDescriptor
->BasePage
,
201 FoundDescriptor
->VirtualPage
,
202 LocalDescriptor
.BasePage
-
203 FoundDescriptor
->BasePage
);
204 Status
= MmMdAddDescriptorToList(CurrentList
, TempDescriptor
, 0);
205 if (!NT_SUCCESS(Status
))
211 /* Does the memory we received not exactly fall onto the end of its descriptor? */
212 LocalEndPage
= LocalDescriptor
.PageCount
+ LocalDescriptor
.BasePage
;
213 FoundEndPage
= FoundDescriptor
->PageCount
+ FoundDescriptor
->BasePage
;
214 LocalVirtualEndPage
= LocalDescriptor
.VirtualPage
?
215 LocalDescriptor
.VirtualPage
+ LocalDescriptor
.PageCount
: 0;
216 if (LocalEndPage
!= FoundEndPage
)
218 TempDescriptor
= MmMdInitByteGranularDescriptor(FoundDescriptor
->Flags
,
219 FoundDescriptor
->Type
,
222 FoundEndPage
- LocalEndPage
);
223 Status
= MmMdAddDescriptorToList(CurrentList
, TempDescriptor
, 0);
224 if (!NT_SUCCESS(Status
))
230 /* We got the memory we needed */
231 Status
= STATUS_SUCCESS
;
233 /* Are we supposed to insert it into a new list? */
236 /* Copy the allocated region descriptor into the one we found */
237 FoundDescriptor
->BaseAddress
= LocalDescriptor
.BaseAddress
;
238 FoundDescriptor
->VirtualPage
= LocalDescriptor
.VirtualPage
;
239 FoundDescriptor
->PageCount
= LocalDescriptor
.PageCount
;
240 FoundDescriptor
->Type
= Type
;
241 FoundDescriptor
->Flags
= LocalDescriptor
.Flags
;
243 /* Remember if it came from EFI */
246 FoundDescriptor
->Flags
|= BlMemoryFirmware
;
249 /* Add the descriptor to the requested list */
250 Status
= MmMdAddDescriptorToList(NewList
, FoundDescriptor
, 0);
254 /* Free the descriptor, nobody wants to know about it anymore */
255 MmMdFreeDescriptor(FoundDescriptor
);
258 /* Return the allocation region back */
259 RtlCopyMemory(Descriptor
, &LocalDescriptor
, sizeof(LocalDescriptor
));
265 _In_ PBL_MEMORY_DESCRIPTOR_LIST NewList
,
266 _In_ PBL_MEMORY_DESCRIPTOR Descriptor
,
267 _In_ PBL_MEMORY_DESCRIPTOR_LIST CurrentList
,
268 _In_ PBL_PA_REQUEST Request
,
269 _In_ BL_MEMORY_TYPE MemoryType
274 /* Heap and page directory/table pages have a special flag */
275 if ((MemoryType
>= BlLoaderHeap
) && (MemoryType
<= BlLoaderReferencePage
))
277 Request
->Flags
|= BlMemorySpecial
;
280 /* Try to find a free region of RAM matching this range and request */
281 Request
->MemoryType
= BlConventionalMemory
;
282 Status
= MmPapAllocateRegionFromMdl(NewList
,
287 if (Status
== STATUS_NOT_FOUND
)
289 /* Need to re-synchronize the memory map and check other lists */
290 EfiPrintf(L
"No RAM found -- backup plan not yet implemented\r\n");
293 /* Did we get the region we wanted? */
294 if (NT_SUCCESS(Status
))
296 /* All good, return back */
300 /* Nope, we have to hunt for it elsewhere */
301 EfiPrintf(L
"TODO\r\n");
306 MmPapAllocatePhysicalPagesInRange (
307 _Inout_ PPHYSICAL_ADDRESS BaseAddress
,
308 _In_ BL_MEMORY_TYPE MemoryType
,
309 _In_ ULONGLONG Pages
,
310 _In_ ULONG Attributes
,
311 _In_ ULONG Alignment
,
312 _In_ PBL_MEMORY_DESCRIPTOR_LIST NewList
,
313 _In_opt_ PBL_ADDRESS_RANGE Range
,
318 BL_PA_REQUEST Request
;
319 BL_MEMORY_DESCRIPTOR Descriptor
;
321 /* Increase nesting depth */
322 ++MmDescriptorCallTreeCount
;
324 /* Bail out if no address was specified */
327 Status
= STATUS_INVALID_PARAMETER
;
331 /* Bail out if no page count was passed in, or a bad list was specified */
333 ((NewList
!= &MmMdlUnmappedAllocated
) &&
334 (NewList
!= &MmMdlPersistentMemory
)))
336 Status
= STATUS_INVALID_PARAMETER
;
340 /* Bail out if the passed in range is invalid */
341 if ((Range
) && (Range
->Minimum
>= Range
->Maximum
))
343 Status
= STATUS_INVALID_PARAMETER
;
347 /* Adjust alignment as needed */
353 /* Clear the virtual range */
354 Request
.VirtualRange
.Minimum
= 0;
355 Request
.VirtualRange
.Maximum
= 0;
357 /* Check if a fixed allocation was requested*/
358 if (Attributes
& BlMemoryFixed
)
360 /* Force the only available range to be the passed in address */
361 Request
.BaseRange
.Minimum
= BaseAddress
->QuadPart
>> PAGE_SHIFT
;
362 Request
.BaseRange
.Maximum
= Request
.BaseRange
.Minimum
+ Pages
- 1;
366 /* Otherwise, a manual range was specified, use it */
367 Request
.BaseRange
.Minimum
= Range
->Minimum
>> PAGE_SHIFT
;
368 Request
.BaseRange
.Maximum
= Request
.BaseRange
.Minimum
+
369 (Range
->Maximum
>> PAGE_SHIFT
) - 1;
373 /* Otherwise, use any possible range of pages */
374 Request
.BaseRange
.Minimum
= PapMinimumPhysicalPage
;
375 Request
.BaseRange
.Maximum
= MAXULONG
>> PAGE_SHIFT
;
378 /* Check if no type was specified, or if it was invalid */
380 (RangeType
& ~(BL_MM_REQUEST_TOP_DOWN_TYPE
| BL_MM_REQUEST_DEFAULT_TYPE
)))
382 /* Use default type */
383 Request
.Type
= BL_MM_REQUEST_DEFAULT_TYPE
;
387 /* Use the requested type */
388 Request
.Type
= RangeType
;
391 /* Capture the other request parameters */
392 Request
.Alignment
= Alignment
;
393 Request
.Pages
= Pages
;
394 Request
.Flags
= Attributes
;
395 Status
= MmPaAllocatePages(NewList
,
397 &MmMdlUnmappedUnallocated
,
400 if (NT_SUCCESS(Status
))
402 /* We got a descriptor back, return its address */
403 BaseAddress
->QuadPart
= Descriptor
.BasePage
<< PAGE_SHIFT
;
407 /* Restore the nesting depth */
408 MmMdFreeGlobalDescriptors();
409 --MmDescriptorCallTreeCount
;
414 MmPapAllocatePagesInRange (
415 _Inout_ PVOID
* PhysicalAddress
,
416 _In_ BL_MEMORY_TYPE MemoryType
,
417 _In_ ULONGLONG Pages
,
418 _In_ ULONG Attributes
,
419 _In_ ULONG Alignment
,
420 _In_opt_ PBL_ADDRESS_RANGE Range
,
425 PHYSICAL_ADDRESS BaseAddress
;
427 /* Increment nesting depth */
428 ++MmDescriptorCallTreeCount
;
430 /* Check for missing parameters or invalid range */
431 if (!(PhysicalAddress
) ||
433 ((Range
) && (Range
->Minimum
>= Range
->Maximum
)))
435 Status
= STATUS_INVALID_PARAMETER
;
439 /* What translation mode are we using? */
440 if (MmTranslationType
!= BlNone
)
442 /* We don't support virtual memory yet */
443 Status
= STATUS_NOT_IMPLEMENTED
;
448 /* Check if this is a fixed allocation */
449 BaseAddress
.QuadPart
= (Attributes
& BlMemoryFixed
) ? (ULONG_PTR
)*PhysicalAddress
: 0;
451 /* Allocate the pages */
452 Status
= MmPapAllocatePhysicalPagesInRange(&BaseAddress
,
457 (&MmMdlMappedAllocated
!=
458 &MmMdlPersistentMemory
) ?
459 &MmMdlUnmappedAllocated
:
460 &MmMdlMappedAllocated
,
464 /* Return the allocated address */
465 *PhysicalAddress
= (PVOID
)BaseAddress
.LowPart
;
469 /* Restore the nesting depth */
470 MmMdFreeGlobalDescriptors();
471 --MmDescriptorCallTreeCount
;
477 __in PBL_MEMORY_DATA BootMemoryData
,
478 __in ULONG MinimumAllocationCount
482 ULONG ExistingDescriptors
, FinalOffset
;
483 PBL_MEMORY_DESCRIPTOR Descriptor
, NewDescriptor
;
485 /* Initialize physical allocator variables */
486 PapMaximumPhysicalPage
= 0xFFFFFFFFFFFFF;
487 PapMinimumAllocationCount
= MinimumAllocationCount
;
488 PapMinimumPhysicalPage
= 0;
490 /* Initialize all the lists */
491 MmMdInitializeListHead(&MmMdlMappedAllocated
);
492 MmMdInitializeListHead(&MmMdlMappedUnallocated
);
493 MmMdInitializeListHead(&MmMdlFwAllocationTracker
);
494 MmMdInitializeListHead(&MmMdlUnmappedAllocated
);
495 MmMdInitializeListHead(&MmMdlReservedAllocated
);
496 MmMdInitializeListHead(&MmMdlBadMemory
);
497 MmMdInitializeListHead(&MmMdlTruncatedMemory
);
498 MmMdInitializeListHead(&MmMdlPersistentMemory
);
499 MmMdInitializeListHead(&MmMdlUnmappedUnallocated
);
500 MmMdInitializeListHead(&MmMdlCompleteBadMemory
);
502 /* Get the BIOS memory map */
503 Status
= MmFwGetMemoryMap(&MmMdlUnmappedUnallocated
,
504 BL_MM_FLAG_USE_FIRMWARE_FOR_MEMORY_MAP_BUFFERS
|
505 BL_MM_FLAG_REQUEST_COALESCING
);
506 if (NT_SUCCESS(Status
))
509 PLIST_ENTRY listHead
, nextEntry
;
511 /* Loop the NT firmware memory list */
512 EfiPrintf(L
"NT MEMORY MAP\n\r\n");
513 listHead
= &MmMdlUnmappedUnallocated
.ListHead
;
514 nextEntry
= listHead
->Flink
;
515 while (listHead
!= nextEntry
)
517 Descriptor
= CONTAINING_RECORD(nextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
519 EfiPrintf(L
"Type: %08lX Flags: %08lX Base: 0x%016I64X End: 0x%016I64X\r\n",
522 Descriptor
->BasePage
<< PAGE_SHIFT
,
523 (Descriptor
->BasePage
+ Descriptor
->PageCount
) << PAGE_SHIFT
);
525 nextEntry
= nextEntry
->Flink
;
530 * Because BL supports cross x86-x64 application launches and a LIST_ENTRY
531 * is of variable size, care must be taken here to ensure that we see a
532 * consistent view of descriptors. BL uses some offset magic to figure out
533 * where the data actually starts, since everything is ULONGLONG past the
536 FinalOffset
= BootMemoryData
->MdListOffset
+ BootMemoryData
->DescriptorOffset
;
537 Descriptor
= (PBL_MEMORY_DESCRIPTOR
)((ULONG_PTR
)BootMemoryData
+ FinalOffset
-
538 FIELD_OFFSET(BL_MEMORY_DESCRIPTOR
, BasePage
));
540 /* Scan all of them */
541 ExistingDescriptors
= BootMemoryData
->DescriptorCount
;
542 while (ExistingDescriptors
!= 0)
544 /* Remove this region from our free memory MDL */
545 Status
= MmMdRemoveRegionFromMdlEx(&MmMdlUnmappedUnallocated
,
546 BL_MM_REMOVE_PHYSICAL_REGION_FLAG
,
547 Descriptor
->BasePage
,
548 Descriptor
->PageCount
,
550 if (!NT_SUCCESS(Status
))
552 return STATUS_INVALID_PARAMETER
;
555 /* Build a descriptor for it */
556 NewDescriptor
= MmMdInitByteGranularDescriptor(Descriptor
->Flags
,
558 Descriptor
->BasePage
,
559 Descriptor
->VirtualPage
,
560 Descriptor
->PageCount
);
563 return STATUS_NO_MEMORY
;
566 /* And add this region to the reserved & allocated MDL */
567 Status
= MmMdAddDescriptorToList(&MmMdlReservedAllocated
, NewDescriptor
, 0);
568 if (!NT_SUCCESS(Status
))
573 /* Move on to the next descriptor */
574 ExistingDescriptors
--;
575 Descriptor
= (PBL_MEMORY_DESCRIPTOR
)((ULONG_PTR
)Descriptor
+ BootMemoryData
->DescriptorSize
);
578 /* We are done, so check for any RAM constraints which will make us truncate memory */
579 Status
= BlpMmInitializeConstraints();
580 if (NT_SUCCESS(Status
))
582 /* The Page Allocator has initialized */
583 PapInitializationStatus
= TRUE
;
584 Status
= STATUS_SUCCESS
;
593 BlMmAllocatePhysicalPages(
594 _In_ PPHYSICAL_ADDRESS Address
,
595 _In_ BL_MEMORY_TYPE MemoryType
,
596 _In_ ULONGLONG PageCount
,
597 _In_ ULONG Attributes
,
601 /* Call the physical allocator */
602 return MmPapAllocatePhysicalPagesInRange(Address
,
607 &MmMdlUnmappedAllocated
,
613 MmPapFreePhysicalPages (
614 _In_ ULONG WhichList
,
615 _In_ ULONGLONG PageCount
,
616 _In_ PHYSICAL_ADDRESS Address
619 PBL_MEMORY_DESCRIPTOR Descriptor
;
621 ULONG DescriptorFlags
, Flags
;
622 BOOLEAN DontFree
, HasPageData
;
623 BL_LIBRARY_PARAMETERS LibraryParameters
;
626 /* Set some defaults */
627 Flags
= BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
;
631 /* Only page-aligned addresses are accepted */
632 if (Address
.QuadPart
& (PAGE_SIZE
- 1))
634 EfiPrintf(L
"free mem fail 1\r\n");
635 return STATUS_INVALID_PARAMETER
;
638 /* Try to find the descriptor containing this address */
639 Page
= Address
.QuadPart
>> PAGE_SHIFT
;
640 Descriptor
= MmMdFindDescriptor(WhichList
,
641 BL_MM_REMOVE_PHYSICAL_REGION_FLAG
,
645 EfiPrintf(L
"free mem fail 2\r\n");
646 return STATUS_INVALID_PARAMETER
;
649 /* If a page count was given, it must match, unless it's coalesced */
650 DescriptorFlags
= Descriptor
->Flags
;
651 if (!(DescriptorFlags
& BlMemoryCoalesced
) &&
652 (PageCount
) && (PageCount
!= Descriptor
->PageCount
))
654 EfiPrintf(L
"free mem fail 3\r\n");
655 return STATUS_INVALID_PARAMETER
;
658 /* Check if this is persistent memory in teardown status */
659 if ((PapInitializationStatus
== 2) &&
660 (DescriptorFlags
& BlMemoryPersistent
))
662 /* Then we should keep it */
667 /* Mark it as non-persistent, since we're freeing it */
668 Descriptor
->Flags
&= ~BlMemoryPersistent
;
671 /* Check if this memory contains paging data */
672 if ((Descriptor
->Type
== BlLoaderPageDirectory
) ||
673 (Descriptor
->Type
== BlLoaderReferencePage
))
678 /* Check if a page count was given */
681 /* The pages must fit within the descriptor */
682 if ((PageCount
+ Page
- Descriptor
->BasePage
) > Descriptor
->PageCount
)
684 EfiPrintf(L
"free mem fail 4\r\n");
685 return STATUS_INVALID_PARAMETER
;
690 /* No page count given, so the address must be at the beginning then */
691 if (Descriptor
->BasePage
!= Page
)
693 EfiPrintf(L
"free mem fail 5\r\n");
694 return STATUS_INVALID_PARAMETER
;
697 /* And we'll use the page count in the descriptor */
698 PageCount
= Descriptor
->PageCount
;
701 /* Copy library parameters since we will read them */
702 RtlCopyMemory(&LibraryParameters
,
703 &BlpLibraryParameters
,
704 sizeof(LibraryParameters
));
706 /* Check if this is teardown */
707 if (PapInitializationStatus
== 2)
709 EfiPrintf(L
"Case 2 not yet handled!\r\n");
710 return STATUS_NOT_SUPPORTED
;
714 /* Caller wants memory to be freed -- should we zero it? */
715 if (!(HasPageData
) &&
716 (LibraryParameters
.LibraryFlags
&
717 BL_LIBRARY_FLAG_ZERO_HEAP_ALLOCATIONS_ON_FREE
))
719 EfiPrintf(L
"Freeing zero data not yet handled!\r\n");
720 return STATUS_NOT_SUPPORTED
;
724 /* Now call into firmware to actually free the physical pages */
725 Status
= MmFwFreePages(Page
, PageCount
);
726 if (!NT_SUCCESS(Status
))
728 EfiPrintf(L
"free mem fail 6\r\n");
732 /* Remove the firmware flags */
733 Descriptor
->Flags
&= ~(BlMemoryNonFirmware
|
737 /* If we're not actually freeing, don't coalesce with anyone nearby */
740 Flags
|= BL_MM_ADD_DESCRIPTOR_NEVER_COALESCE_FLAG
;
743 /* Check if the entire allocation is being free*/
744 if (PageCount
== Descriptor
->PageCount
)
746 /* Remove the descriptor from the allocated list */
747 MmMdRemoveDescriptorFromList(&MmMdlUnmappedAllocated
, Descriptor
);
749 /* Mark the entire descriptor as free */
750 Descriptor
->Type
= BlConventionalMemory
;
754 /* Init a descriptor for what we're actually freeing */
755 Descriptor
= MmMdInitByteGranularDescriptor(Descriptor
->Flags
,
756 BlConventionalMemory
,
762 return STATUS_NO_MEMORY
;
765 /* Remove the region from the existing descriptor */
766 Status
= MmMdRemoveRegionFromMdlEx(&MmMdlUnmappedAllocated
,
767 BL_MM_REMOVE_PHYSICAL_REGION_FLAG
,
771 if (!NT_SUCCESS(Status
))
777 /* Add the new descriptor into in the list (or the old, repurposed one) */
778 Descriptor
->Flags
&= ~BlMemoryCoalesced
;
779 return MmMdAddDescriptorToList(&MmMdlUnmappedUnallocated
, Descriptor
, Flags
);
783 BlMmFreePhysicalPages (
784 _In_ PHYSICAL_ADDRESS Address
787 /* Call the physical allocator */
788 return MmPapFreePhysicalPages(BL_MM_INCLUDE_UNMAPPED_ALLOCATED
, 0, Address
);
797 PHYSICAL_ADDRESS PhysicalAddress
;
799 /* Handle virtual memory scenario */
800 if (MmTranslationType
!= BlNone
)
802 EfiPrintf(L
"Unimplemented virtual path\r\n");
803 return STATUS_SUCCESS
;
806 /* Physical memory should be in the unmapped allocated list */
807 if (WhichList
!= BL_MM_INCLUDE_PERSISTENT_MEMORY
)
809 WhichList
= BL_MM_INCLUDE_UNMAPPED_ALLOCATED
;
812 /* Free it from there */
813 PhysicalAddress
.QuadPart
= (ULONG_PTR
)Address
;
814 return MmPapFreePhysicalPages(WhichList
, 0, PhysicalAddress
);
819 _In_ PLIST_ENTRY MemoryMap
,
820 _In_ PBL_IMAGE_PARAMETERS MemoryParameters
,
821 _In_ ULONG WhichTypes
,
825 BL_MEMORY_DESCRIPTOR_LIST FirmwareMdList
, FullMdList
;
826 BOOLEAN DoFirmware
, DoPersistent
, DoTruncated
, DoBad
;
827 BOOLEAN DoReserved
, DoUnmapUnalloc
, DoUnmapAlloc
;
828 BOOLEAN DoMapAlloc
, DoMapUnalloc
, DoFirmware2
;
829 ULONG LoopCount
, MdListCount
, MdListSize
, Used
;
832 /* Initialize the firmware list if we use it */
833 MmMdInitializeListHead(&FirmwareMdList
);
835 /* Make sure we got our input parameters */
836 if (!(MemoryMap
) || !(MemoryParameters
))
838 return STATUS_INVALID_PARAMETER
;
841 /* Either ask for firmware memory, or don't. Not neither */
842 if ((WhichTypes
& ~BL_MM_INCLUDE_NO_FIRMWARE_MEMORY
) &&
843 (WhichTypes
& ~BL_MM_INCLUDE_ONLY_FIRMWARE_MEMORY
))
845 return STATUS_INVALID_PARAMETER
;
848 /* Either ask for firmware memory, or don't. Not both */
849 if ((WhichTypes
& BL_MM_INCLUDE_NO_FIRMWARE_MEMORY
) &&
850 (WhichTypes
& BL_MM_INCLUDE_ONLY_FIRMWARE_MEMORY
))
852 return STATUS_INVALID_PARAMETER
;
855 /* Initialize the memory map list */
856 InitializeListHead(MemoryMap
);
858 /* Check which types of memory to dump */
859 DoFirmware
= WhichTypes
& BL_MM_INCLUDE_FIRMWARE_MEMORY
;
860 DoPersistent
= WhichTypes
& BL_MM_INCLUDE_PERSISTENT_MEMORY
;
861 DoTruncated
= WhichTypes
& BL_MM_INCLUDE_TRUNCATED_MEMORY
;
862 DoBad
= WhichTypes
& BL_MM_INCLUDE_BAD_MEMORY
;
863 DoReserved
= WhichTypes
& BL_MM_INCLUDE_RESERVED_ALLOCATED
;
864 DoUnmapUnalloc
= WhichTypes
& BL_MM_INCLUDE_UNMAPPED_UNALLOCATED
;
865 DoUnmapAlloc
= WhichTypes
& BL_MM_INCLUDE_UNMAPPED_ALLOCATED
;
866 DoMapAlloc
= WhichTypes
& BL_MM_INCLUDE_MAPPED_ALLOCATED
;
867 DoMapUnalloc
= WhichTypes
& BL_MM_INCLUDE_MAPPED_UNALLOCATED
;
868 DoFirmware2
= WhichTypes
& BL_MM_INCLUDE_FIRMWARE_MEMORY_2
;
870 /* Begin the attempt loop */
874 /* Count how many entries we will need */
876 if (DoMapAlloc
) MdListCount
= MmMdCountList(&MmMdlMappedAllocated
);
877 if (DoMapUnalloc
) MdListCount
+= MmMdCountList(&MmMdlMappedUnallocated
);
878 if (DoUnmapAlloc
) MdListCount
+= MmMdCountList(&MmMdlUnmappedAllocated
);
879 if (DoUnmapUnalloc
) MdListCount
+= MmMdCountList(&MmMdlUnmappedUnallocated
);
880 if (DoReserved
) MdListCount
+= MmMdCountList(&MmMdlReservedAllocated
);
881 if (DoBad
) MdListCount
+= MmMdCountList(&MmMdlBadMemory
);
882 if (DoTruncated
) MdListCount
+= MmMdCountList(&MmMdlTruncatedMemory
);
883 if (DoPersistent
) MdListCount
+= MmMdCountList(&MmMdlPersistentMemory
);
885 /* Plus firmware entries */
888 /* Free the previous entries, if any */
889 MmMdFreeList(&FirmwareMdList
);
891 /* Get the firmware map, coalesced */
892 Status
= MmFwGetMemoryMap(&FirmwareMdList
,
893 BL_MM_FLAG_REQUEST_COALESCING
);
894 if (!NT_SUCCESS(Status
))
899 /* We overwrite, since this type is exclusive */
900 MdListCount
= MmMdCountList(&FirmwareMdList
);
903 /* Plus firmware entries-2 */
906 /* Free the previous entries, if any */
907 MmMdFreeList(&FirmwareMdList
);
909 /* Get the firmware map, uncoalesced */
910 Status
= MmFwGetMemoryMap(&FirmwareMdList
, 0);
911 if (!NT_SUCCESS(Status
))
916 /* We overwrite, since this type is exclusive */
917 MdListCount
= MmMdCountList(&FirmwareMdList
);
920 /* If there's no descriptors, we're done */
923 Status
= STATUS_SUCCESS
;
927 /* Check if the buffer we have is big enough */
928 if (MemoryParameters
->BufferSize
>=
929 (sizeof(BL_MEMORY_DESCRIPTOR
) * MdListCount
))
934 /* It's not, allocate it, with a slack of 4 extra descriptors */
935 MdListSize
= sizeof(BL_MEMORY_DESCRIPTOR
) * (MdListCount
+ 4);
937 /* Except if we weren't asked to */
938 if (!(Flags
& BL_MM_ADD_DESCRIPTOR_ALLOCATE_FLAG
))
940 MemoryParameters
->BufferSize
= MdListSize
;
941 Status
= STATUS_BUFFER_TOO_SMALL
;
945 /* Has it been less than 4 times we've tried this? */
946 if (++LoopCount
<= 4)
948 /* Free the previous attempt, if any */
949 if (MemoryParameters
->BufferSize
)
951 BlMmFreeHeap(MemoryParameters
->Buffer
);
954 /* Allocate a new buffer */
955 MemoryParameters
->BufferSize
= MdListSize
;
956 MemoryParameters
->Buffer
= BlMmAllocateHeap(MdListSize
);
957 if (MemoryParameters
->Buffer
)
964 /* If we got here, we're out of memory after 4 attempts */
965 Status
= STATUS_NO_MEMORY
;
969 /* We should have a buffer by now... */
970 if (MemoryParameters
->Buffer
)
973 RtlZeroMemory(MemoryParameters
->Buffer
,
974 MdListCount
* sizeof(BL_MEMORY_DESCRIPTOR
));
977 /* Initialize our list of descriptors */
978 MmMdInitializeList(&FullMdList
, 0, MemoryMap
);
981 /* Handle mapped, allocated */
984 Status
= MmMdCopyList(&FullMdList
,
985 &MmMdlMappedAllocated
,
986 MemoryParameters
->Buffer
,
992 /* Handle mapped, unallocated */
995 Status
= MmMdCopyList(&FullMdList
,
996 &MmMdlMappedUnallocated
,
997 MemoryParameters
->Buffer
,
1003 /* Handle unmapped, allocated */
1006 Status
= MmMdCopyList(&FullMdList
,
1007 &MmMdlUnmappedAllocated
,
1008 MemoryParameters
->Buffer
,
1014 /* Handle unmapped, unallocated */
1017 Status
= MmMdCopyList(&FullMdList
,
1018 &MmMdlUnmappedUnallocated
,
1019 MemoryParameters
->Buffer
,
1025 /* Handle reserved, allocated */
1028 Status
= MmMdCopyList(&FullMdList
,
1029 &MmMdlReservedAllocated
,
1030 MemoryParameters
->Buffer
,
1039 Status
= MmMdCopyList(&FullMdList
,
1041 MemoryParameters
->Buffer
,
1047 /* Handle truncated */
1050 Status
= MmMdCopyList(&FullMdList
,
1051 &MmMdlTruncatedMemory
,
1052 MemoryParameters
->Buffer
,
1058 /* Handle persistent */
1061 Status
= MmMdCopyList(&FullMdList
,
1062 &MmMdlPersistentMemory
,
1063 MemoryParameters
->Buffer
,
1069 /* Handle firmware */
1072 Status
= MmMdCopyList(&FullMdList
,
1074 MemoryParameters
->Buffer
,
1080 /* Handle firmware2 */
1083 Status
= MmMdCopyList(&FullMdList
,
1085 MemoryParameters
->Buffer
,
1091 /* Add up the final size */
1092 Status
= RtlULongLongToULong(Used
* sizeof(BL_MEMORY_DESCRIPTOR
),
1093 &MemoryParameters
->ActualSize
);
1096 MmMdFreeList(&FirmwareMdList
);
1101 MmPaReleaseSelfMapPages (
1102 _In_ PHYSICAL_ADDRESS Address
1105 PBL_MEMORY_DESCRIPTOR Descriptor
;
1109 /* Only page-aligned addresses are accepted */
1110 if (Address
.QuadPart
& (PAGE_SIZE
- 1))
1112 EfiPrintf(L
"free mem fail 1\r\n");
1113 return STATUS_INVALID_PARAMETER
;
1116 /* Get the base page, and find a descriptor that matches */
1117 BasePage
= Address
.QuadPart
>> PAGE_SHIFT
;
1118 Descriptor
= MmMdFindDescriptor(BL_MM_INCLUDE_UNMAPPED_UNALLOCATED
,
1119 BL_MM_REMOVE_PHYSICAL_REGION_FLAG
,
1121 if (!(Descriptor
) || (Descriptor
->BasePage
!= BasePage
))
1123 return STATUS_INVALID_PARAMETER
;
1126 /* Free the physical pages */
1127 Status
= MmFwFreePages(BasePage
, Descriptor
->PageCount
);
1128 if (!NT_SUCCESS(Status
))
1133 /* Remove the firmware flags */
1134 Descriptor
->Flags
&= ~(BlMemoryNonFirmware
|
1136 BlMemoryPersistent
);
1138 /* Set it as free memory */
1139 Descriptor
->Type
= BlConventionalMemory
;
1141 /* Create a new descriptor that's free memory, covering the old range */
1142 Descriptor
= MmMdInitByteGranularDescriptor(0,
1143 BlConventionalMemory
,
1146 Descriptor
->PageCount
);
1149 return STATUS_NO_MEMORY
;
1152 /* Insert it into the virtual free list */
1153 return MmMdAddDescriptorToList(&MmMdlFreeVirtual
,
1155 BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
|
1156 BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG
);
1160 MmPaReserveSelfMapPages (
1161 _Inout_ PPHYSICAL_ADDRESS PhysicalAddress
,
1162 _In_ ULONG Alignment
,
1163 _In_ ULONG PageCount
1167 BL_PA_REQUEST Request
;
1168 BL_MEMORY_DESCRIPTOR Descriptor
;
1170 /* Increment descriptor usage count */
1171 ++MmDescriptorCallTreeCount
;
1173 /* Bail if we don't have an address */
1174 if (!PhysicalAddress
)
1176 Status
= STATUS_INVALID_PARAMETER
;
1180 /* Make a request for the required number of self-map pages */
1181 Request
.BaseRange
.Minimum
= PapMinimumPhysicalPage
;
1182 Request
.BaseRange
.Maximum
= 0xFFFFFFFF >> PAGE_SHIFT
;
1183 Request
.VirtualRange
.Minimum
= 0;
1184 Request
.VirtualRange
.Maximum
= 0;
1185 Request
.Pages
= PageCount
;
1186 Request
.Alignment
= Alignment
;
1187 Request
.Type
= BL_MM_REQUEST_DEFAULT_TYPE
;
1189 Status
= MmPaAllocatePages(&MmMdlUnmappedUnallocated
,
1191 &MmMdlUnmappedUnallocated
,
1194 if (!NT_SUCCESS(Status
))
1199 /* Remove this region from free virtual memory */
1200 Status
= MmMdRemoveRegionFromMdlEx(&MmMdlFreeVirtual
,
1201 BL_MM_REMOVE_VIRTUAL_REGION_FLAG
,
1202 Descriptor
.BasePage
,
1203 Descriptor
.PageCount
,
1205 if (!NT_SUCCESS(Status
))
1210 /* Return the physical address */
1211 PhysicalAddress
->QuadPart
= Descriptor
.BasePage
<< PAGE_SHIFT
;
1214 /* Free global descriptors and reduce the count by one */
1215 MmMdFreeGlobalDescriptors();
1216 --MmDescriptorCallTreeCount
;