2 UEFI Memory page management functions.
4 Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)
21 // Entry for tracking the memory regions for each memory type to coalesce similar memory types
24 EFI_PHYSICAL_ADDRESS BaseAddress
;
25 EFI_PHYSICAL_ADDRESS MaximumAddress
;
26 UINT64 CurrentNumberOfPages
;
28 UINTN InformationIndex
;
31 } EFI_MEMORY_TYPE_STATISTICS
;
34 // MemoryMap - The current memory map
36 UINTN mMemoryMapKey
= 0;
38 #define MAX_MAP_DEPTH 6
41 /// mMapDepth - depth of new descriptor stack
45 /// mMapStack - space to use as temp storage to build new map descriptors
47 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
48 UINTN mFreeMapStack
= 0;
50 /// This list maintain the free memory map list
52 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
53 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
55 EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
56 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiReservedMemoryType
57 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderCode
58 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderData
59 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesCode
60 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesData
61 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesCode
62 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesData
63 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiConventionalMemory
64 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiUnusableMemory
65 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIReclaimMemory
66 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIMemoryNVS
67 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIO
68 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIOPortSpace
69 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiPalCode
70 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiPersistentMemory
71 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
} // EfiMaxMemoryType
74 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress
= MAX_ADDRESS
;
75 EFI_PHYSICAL_ADDRESS mDefaultBaseAddress
= MAX_ADDRESS
;
77 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
78 { EfiReservedMemoryType
, 0 },
81 { EfiBootServicesCode
, 0 },
82 { EfiBootServicesData
, 0 },
83 { EfiRuntimeServicesCode
, 0 },
84 { EfiRuntimeServicesData
, 0 },
85 { EfiConventionalMemory
, 0 },
86 { EfiUnusableMemory
, 0 },
87 { EfiACPIReclaimMemory
, 0 },
88 { EfiACPIMemoryNVS
, 0 },
89 { EfiMemoryMappedIO
, 0 },
90 { EfiMemoryMappedIOPortSpace
, 0 },
92 { EfiPersistentMemory
, 0 },
93 { EfiMaxMemoryType
, 0 }
96 // Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated
97 // and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a
98 // address assigned by DXE core.
100 GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady
= FALSE
;
103 Enter critical section by gaining lock on gMemoryLock.
107 CoreAcquireMemoryLock (
111 CoreAcquireLock (&gMemoryLock
);
117 Exit critical section by releasing lock on gMemoryLock.
121 CoreReleaseMemoryLock (
125 CoreReleaseLock (&gMemoryLock
);
132 Internal function. Removes a descriptor entry.
134 @param Entry The entry to remove
138 RemoveMemoryMapEntry (
139 IN OUT MEMORY_MAP
*Entry
142 RemoveEntryList (&Entry
->Link
);
143 Entry
->Link
.ForwardLink
= NULL
;
145 if (Entry
->FromPages
) {
147 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
149 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
154 Internal function. Adds a ranges to the memory map.
155 The range must not already exist in the map.
157 @param Type The type of memory range to add
158 @param Start The starting address in the memory range Must be
160 @param End The last address in the range Must be the last
162 @param Attribute The attributes of the memory range to add
167 IN EFI_MEMORY_TYPE Type
,
168 IN EFI_PHYSICAL_ADDRESS Start
,
169 IN EFI_PHYSICAL_ADDRESS End
,
176 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
177 ASSERT (End
> Start
) ;
179 ASSERT_LOCKED (&gMemoryLock
);
181 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
184 // If memory of type EfiConventionalMemory is being added that includes the page
185 // starting at address 0, then zero the page starting at address 0. This has
186 // two benifits. It helps find NULL pointer bugs and it also maximizes
187 // compatibility with operating systems that may evaluate memory in this page
188 // for legacy data structures. If memory of any other type is added starting
189 // at address 0, then do not zero the page at address 0 because the page is being
190 // used for other purposes.
192 if (Type
== EfiConventionalMemory
&& Start
== 0 && (End
>= EFI_PAGE_SIZE
- 1)) {
193 SetMem ((VOID
*)(UINTN
)Start
, EFI_PAGE_SIZE
, 0);
197 // Memory map being altered so updated key
202 // UEFI 2.0 added an event group for notificaiton on memory map changes.
203 // So we need to signal this Event Group every time the memory map changes.
204 // If we are in EFI 1.10 compatability mode no event groups will be
205 // found and nothing will happen we we call this function. These events
206 // will get signaled but since a lock is held around the call to this
207 // function the notificaiton events will only be called after this function
208 // returns and the lock is released.
210 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
213 // Look for adjoining memory descriptor
216 // Two memory descriptors can only be merged if they have the same Type
217 // and the same Attribute
220 Link
= gMemoryMap
.ForwardLink
;
221 while (Link
!= &gMemoryMap
) {
222 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
223 Link
= Link
->ForwardLink
;
225 if (Entry
->Type
!= Type
) {
229 if (Entry
->Attribute
!= Attribute
) {
233 if (Entry
->End
+ 1 == Start
) {
235 Start
= Entry
->Start
;
236 RemoveMemoryMapEntry (Entry
);
238 } else if (Entry
->Start
== End
+ 1) {
241 RemoveMemoryMapEntry (Entry
);
249 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
250 mMapStack
[mMapDepth
].FromPages
= FALSE
;
251 mMapStack
[mMapDepth
].Type
= Type
;
252 mMapStack
[mMapDepth
].Start
= Start
;
253 mMapStack
[mMapDepth
].End
= End
;
254 mMapStack
[mMapDepth
].VirtualStart
= 0;
255 mMapStack
[mMapDepth
].Attribute
= Attribute
;
256 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
259 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
265 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
266 If the list is emtry, then allocate a new page to refuel the list.
267 Please Note this algorithm to allocate the memory map descriptor has a property
268 that the memory allocated for memory entries always grows, and will never really be freed
269 For example, if the current boot uses 2000 memory map entries at the maximum point, but
270 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
271 memory map entries is still allocated from EfiBootServicesMemory.
274 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
278 AllocateMemoryMapEntry (
282 MEMORY_MAP
* FreeDescriptorEntries
;
286 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
288 // The list is empty, to allocate one page to refuel the list
290 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
291 if(FreeDescriptorEntries
!= NULL
) {
293 // Enque the free memmory map entries into the list
295 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
296 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
297 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
304 // dequeue the first descriptor from the list
306 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
307 RemoveEntryList (&Entry
->Link
);
314 Internal function. Moves any memory descriptors that are on the
315 temporary descriptor stack to heap.
319 CoreFreeMemoryMapStack (
327 ASSERT_LOCKED (&gMemoryLock
);
330 // If already freeing the map stack, then return
332 if (mFreeMapStack
!= 0) {
337 // Move the temporary memory descriptor stack into pool
341 while (mMapDepth
!= 0) {
343 // Deque an memory map entry from mFreeMemoryMapEntryList
345 Entry
= AllocateMemoryMapEntry ();
350 // Update to proper entry
354 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
357 // Move this entry to general memory
359 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
360 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
362 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
363 Entry
->FromPages
= TRUE
;
366 // Find insertion location
368 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
369 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
370 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
375 InsertTailList (Link2
, &Entry
->Link
);
379 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
380 // so here no need to move it to memory.
382 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
390 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
394 PromoteMemoryResource (
399 EFI_GCD_MAP_ENTRY
*Entry
;
402 DEBUG ((DEBUG_PAGE
, "Promote the memory resource\n"));
404 CoreAcquireGcdMemoryLock ();
407 Link
= mGcdMemorySpaceMap
.ForwardLink
;
408 while (Link
!= &mGcdMemorySpaceMap
) {
410 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
412 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
413 Entry
->EndAddress
< MAX_ADDRESS
&&
414 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
415 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
417 // Update the GCD map
419 if ((Entry
->Capabilities
& EFI_MEMORY_MORE_RELIABLE
) == EFI_MEMORY_MORE_RELIABLE
) {
420 Entry
->GcdMemoryType
= EfiGcdMemoryTypeMoreReliable
;
422 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
424 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
425 Entry
->ImageHandle
= gDxeCoreImageHandle
;
426 Entry
->DeviceHandle
= NULL
;
429 // Add to allocable system memory resource
433 EfiConventionalMemory
,
436 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
438 CoreFreeMemoryMapStack ();
443 Link
= Link
->ForwardLink
;
446 CoreReleaseGcdMemoryLock ();
451 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD
452 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the
453 size of boot time and runtime code.
457 CoreLoadingFixedAddressHook (
461 UINT32 RuntimeCodePageNumber
;
462 UINT32 BootTimeCodePageNumber
;
463 EFI_PHYSICAL_ADDRESS RuntimeCodeBase
;
464 EFI_PHYSICAL_ADDRESS BootTimeCodeBase
;
468 // Make sure these 2 areas are not initialzied.
470 if (!gLoadFixedAddressCodeMemoryReady
) {
471 RuntimeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
472 BootTimeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
473 RuntimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- EFI_PAGES_TO_SIZE (RuntimeCodePageNumber
));
474 BootTimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(RuntimeCodeBase
- EFI_PAGES_TO_SIZE (BootTimeCodePageNumber
));
476 // Try to allocate runtime memory.
478 Status
= CoreAllocatePages (
480 EfiRuntimeServicesCode
,
481 RuntimeCodePageNumber
,
484 if (EFI_ERROR(Status
)) {
486 // Runtime memory allocation failed
491 // Try to allocate boot memory.
493 Status
= CoreAllocatePages (
496 BootTimeCodePageNumber
,
499 if (EFI_ERROR(Status
)) {
501 // boot memory allocation failed. Free Runtime code range and will try the allocation again when
502 // new memory range is installed.
506 RuntimeCodePageNumber
510 gLoadFixedAddressCodeMemoryReady
= TRUE
;
516 Called to initialize the memory map and add descriptors to
517 the current descriptor list.
518 The first descriptor that is added must be general usable
519 memory as the addition allocates heap.
521 @param Type The type of memory to add
522 @param Start The starting address in the memory range Must be
524 @param NumberOfPages The number of pages in the range
525 @param Attribute Attributes of the memory to add
527 @return None. The range is added to the memory map
531 CoreAddMemoryDescriptor (
532 IN EFI_MEMORY_TYPE Type
,
533 IN EFI_PHYSICAL_ADDRESS Start
,
534 IN UINT64 NumberOfPages
,
538 EFI_PHYSICAL_ADDRESS End
;
543 if ((Start
& EFI_PAGE_MASK
) != 0) {
547 if (Type
>= EfiMaxMemoryType
&& Type
< MEMORY_TYPE_OEM_RESERVED_MIN
) {
550 CoreAcquireMemoryLock ();
551 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
552 CoreAddRange (Type
, Start
, End
, Attribute
);
553 CoreFreeMemoryMapStack ();
554 CoreReleaseMemoryLock ();
557 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type
559 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
560 CoreLoadingFixedAddressHook();
564 // Check to see if the statistics for the different memory types have already been established
566 if (mMemoryTypeInformationInitialized
) {
572 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
574 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
576 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
578 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
579 if ((UINT32
)Type
> EfiMaxMemoryType
) {
582 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
584 // Allocate pages for the current memory type from the top of available memory
586 Status
= CoreAllocatePages (
589 gMemoryTypeInformation
[Index
].NumberOfPages
,
590 &mMemoryTypeStatistics
[Type
].BaseAddress
592 if (EFI_ERROR (Status
)) {
594 // If an error occurs allocating the pages for the current memory type, then
595 // free all the pages allocates for the previous memory types and return. This
596 // operation with be retied when/if more memory is added to the system
598 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
600 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
602 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
603 if ((UINT32
)Type
> EfiMaxMemoryType
) {
607 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
609 mMemoryTypeStatistics
[Type
].BaseAddress
,
610 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
612 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
613 mMemoryTypeStatistics
[Type
].MaximumAddress
= MAX_ADDRESS
;
620 // Compute the address at the top of the current statistics
622 mMemoryTypeStatistics
[Type
].MaximumAddress
=
623 mMemoryTypeStatistics
[Type
].BaseAddress
+
624 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
627 // If the current base address is the lowest address so far, then update the default
630 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
631 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
637 // There was enough system memory for all the the memory types were allocated. So,
638 // those memory areas can be freed for future allocations, and all future memory
639 // allocations can occur within their respective bins
641 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
643 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
645 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
646 if ((UINT32
)Type
> EfiMaxMemoryType
) {
649 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
651 mMemoryTypeStatistics
[Type
].BaseAddress
,
652 gMemoryTypeInformation
[Index
].NumberOfPages
654 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
655 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
660 // If the number of pages reserved for a memory type is 0, then all allocations for that type
661 // should be in the default range.
663 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
664 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
665 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
666 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
669 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
670 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== MAX_ADDRESS
) {
671 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
675 mMemoryTypeInformationInitialized
= TRUE
;
680 Internal function. Converts a memory range to the specified type or attributes.
681 The range must exist in the memory map. Either ChangingType or
682 ChangingAttributes must be set, but not both.
684 @param Start The first address of the range Must be page
686 @param NumberOfPages The number of pages to convert
687 @param ChangingType Boolean indicating that type value should be changed
688 @param NewType The new type for the memory range
689 @param ChangingAttributes Boolean indicating that attributes value should be changed
690 @param NewAttributes The new attributes for the memory range
692 @retval EFI_INVALID_PARAMETER Invalid parameter
693 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
694 range or convertion not allowed.
695 @retval EFI_SUCCESS Successfully converts the memory range to the
702 IN UINT64 NumberOfPages
,
703 IN BOOLEAN ChangingType
,
704 IN EFI_MEMORY_TYPE NewType
,
705 IN BOOLEAN ChangingAttributes
,
706 IN UINT64 NewAttributes
710 UINT64 NumberOfBytes
;
714 EFI_MEMORY_TYPE MemType
;
719 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
720 End
= Start
+ NumberOfBytes
- 1;
722 ASSERT (NumberOfPages
);
723 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
724 ASSERT (End
> Start
) ;
725 ASSERT_LOCKED (&gMemoryLock
);
726 ASSERT ( (ChangingType
== FALSE
) || (ChangingAttributes
== FALSE
) );
728 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
>= End
)) {
729 return EFI_INVALID_PARAMETER
;
733 // Convert the entire range
736 while (Start
< End
) {
739 // Find the entry that the covers the range
741 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
742 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
744 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
749 if (Link
== &gMemoryMap
) {
750 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
751 return EFI_NOT_FOUND
;
755 // Convert range to the end, or to the end of the descriptor
756 // if that's all we've got
760 ASSERT (Entry
!= NULL
);
761 if (Entry
->End
< End
) {
762 RangeEnd
= Entry
->End
;
766 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to type %d\n", Start
, RangeEnd
, NewType
));
768 if (ChangingAttributes
) {
769 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to attr %lx\n", Start
, RangeEnd
, NewAttributes
));
774 // Debug code - verify conversion is allowed
776 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
777 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types\n"));
778 return EFI_NOT_FOUND
;
782 // Update counters for the number of pages allocated to each memory type
784 if ((UINT32
)Entry
->Type
< EfiMaxMemoryType
) {
785 if ((Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) ||
786 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
787 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
788 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
790 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
795 if ((UINT32
)NewType
< EfiMaxMemoryType
) {
796 if ((Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) ||
797 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
798 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
799 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
> gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
800 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
807 // Pull range out of descriptor
809 if (Entry
->Start
== Start
) {
814 Entry
->Start
= RangeEnd
+ 1;
816 } else if (Entry
->End
== RangeEnd
) {
821 Entry
->End
= Start
- 1;
826 // Pull it out of the center, clip current
832 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
833 mMapStack
[mMapDepth
].FromPages
= FALSE
;
834 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
835 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
836 mMapStack
[mMapDepth
].End
= Entry
->End
;
839 // Inherit Attribute from the Memory Descriptor that is being clipped
841 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
843 Entry
->End
= Start
- 1;
844 ASSERT (Entry
->Start
< Entry
->End
);
846 Entry
= &mMapStack
[mMapDepth
];
847 InsertTailList (&gMemoryMap
, &Entry
->Link
);
850 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
854 // The new range inherits the same Attribute as the Entry
855 // it is being cut out of unless attributes are being changed
858 Attribute
= Entry
->Attribute
;
861 Attribute
= NewAttributes
;
862 MemType
= Entry
->Type
;
866 // If the descriptor is empty, then remove it from the map
868 if (Entry
->Start
== Entry
->End
+ 1) {
869 RemoveMemoryMapEntry (Entry
);
874 // Add our new range in
876 CoreAddRange (MemType
, Start
, RangeEnd
, Attribute
);
877 if (ChangingType
&& (MemType
== EfiConventionalMemory
)) {
879 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
880 // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees
881 // that the page starting at address 0 is always filled with zeros.
884 if (RangeEnd
> EFI_PAGE_SIZE
) {
885 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) EFI_PAGE_SIZE
, (UINTN
) (RangeEnd
- EFI_PAGE_SIZE
+ 1));
888 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) Start
, (UINTN
) (RangeEnd
- Start
+ 1));
893 // Move any map descriptor stack to general pool
895 CoreFreeMemoryMapStack ();
898 // Bump the starting address, and convert the next range
900 Start
= RangeEnd
+ 1;
904 // Converted the whole range, done
912 Internal function. Converts a memory range to the specified type.
913 The range must exist in the memory map.
915 @param Start The first address of the range Must be page
917 @param NumberOfPages The number of pages to convert
918 @param NewType The new type for the memory range
920 @retval EFI_INVALID_PARAMETER Invalid parameter
921 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
922 range or convertion not allowed.
923 @retval EFI_SUCCESS Successfully converts the memory range to the
930 IN UINT64 NumberOfPages
,
931 IN EFI_MEMORY_TYPE NewType
934 return CoreConvertPagesEx(Start
, NumberOfPages
, TRUE
, NewType
, FALSE
, 0);
939 Internal function. Converts a memory range to use new attributes.
941 @param Start The first address of the range Must be page
943 @param NumberOfPages The number of pages to convert
944 @param NewAttributes The new attributes value for the range.
948 CoreUpdateMemoryAttributes (
949 IN EFI_PHYSICAL_ADDRESS Start
,
950 IN UINT64 NumberOfPages
,
951 IN UINT64 NewAttributes
954 CoreAcquireMemoryLock ();
957 // Update the attributes to the new value
959 CoreConvertPagesEx(Start
, NumberOfPages
, FALSE
, (EFI_MEMORY_TYPE
)0, TRUE
, NewAttributes
);
961 CoreReleaseMemoryLock ();
966 Internal function. Finds a consecutive free page range below
967 the requested address.
969 @param MaxAddress The address that the range must be below
970 @param MinAddress The address that the range must be above
971 @param NumberOfPages Number of pages needed
972 @param NewType The type of memory the range is going to be
974 @param Alignment Bits to align with
976 @return The base address of the range, or 0 if the range was not found
981 IN UINT64 MaxAddress
,
982 IN UINT64 MinAddress
,
983 IN UINT64 NumberOfPages
,
984 IN EFI_MEMORY_TYPE NewType
,
988 UINT64 NumberOfBytes
;
992 UINT64 DescNumberOfBytes
;
996 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
1000 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
1003 // If MaxAddress is not aligned to the end of a page
1007 // Change MaxAddress to be 1 page lower
1009 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
1012 // Set MaxAddress to a page boundary
1014 MaxAddress
&= ~(UINT64
)EFI_PAGE_MASK
;
1017 // Set MaxAddress to end of the page
1019 MaxAddress
|= EFI_PAGE_MASK
;
1022 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1025 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1026 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1029 // If it's not a free entry, don't bother with it
1031 if (Entry
->Type
!= EfiConventionalMemory
) {
1035 DescStart
= Entry
->Start
;
1036 DescEnd
= Entry
->End
;
1039 // If desc is past max allowed address or below min allowed address, skip it
1041 if ((DescStart
>= MaxAddress
) || (DescEnd
< MinAddress
)) {
1046 // If desc ends past max allowed address, clip the end
1048 if (DescEnd
>= MaxAddress
) {
1049 DescEnd
= MaxAddress
;
1052 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
1054 // Skip if DescEnd is less than DescStart after alignment clipping
1055 if (DescEnd
< DescStart
) {
1060 // Compute the number of bytes we can used from this
1061 // descriptor, and see it's enough to satisfy the request
1063 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
1065 if (DescNumberOfBytes
>= NumberOfBytes
) {
1067 // If the start of the allocated range is below the min address allowed, skip it
1069 if ((DescEnd
- NumberOfBytes
+ 1) < MinAddress
) {
1074 // If this is the best match so far remember it
1076 if (DescEnd
> Target
) {
1083 // If this is a grow down, adjust target to be the allocation base
1085 Target
-= NumberOfBytes
- 1;
1088 // If we didn't find a match, return 0
1090 if ((Target
& EFI_PAGE_MASK
) != 0) {
1099 Internal function. Finds a consecutive free page range below
1100 the requested address
1102 @param MaxAddress The address that the range must be below
1103 @param NoPages Number of pages needed
1104 @param NewType The type of memory the range is going to be
1106 @param Alignment Bits to align with
1108 @return The base address of the range, or 0 if the range was not found.
1113 IN UINT64 MaxAddress
,
1115 IN EFI_MEMORY_TYPE NewType
,
1122 // Attempt to find free pages in the preferred bin based on the requested memory type
1124 if ((UINT32
)NewType
< EfiMaxMemoryType
&& MaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1125 Start
= CoreFindFreePagesI (
1126 mMemoryTypeStatistics
[NewType
].MaximumAddress
,
1127 mMemoryTypeStatistics
[NewType
].BaseAddress
,
1138 // Attempt to find free pages in the default allocation bin
1140 if (MaxAddress
>= mDefaultMaximumAddress
) {
1141 Start
= CoreFindFreePagesI (mDefaultMaximumAddress
, 0, NoPages
, NewType
, Alignment
);
1143 if (Start
< mDefaultBaseAddress
) {
1144 mDefaultBaseAddress
= Start
;
1151 // The allocation did not succeed in any of the prefered bins even after
1152 // promoting resources. Attempt to find free pages anywhere is the requested
1153 // address range. If this allocation fails, then there are not enough
1154 // resources anywhere to satisfy the request.
1156 Start
= CoreFindFreePagesI (MaxAddress
, 0, NoPages
, NewType
, Alignment
);
1162 // If allocations from the preferred bins fail, then attempt to promote memory resources.
1164 if (!PromoteMemoryResource ()) {
1169 // If any memory resources were promoted, then re-attempt the allocation
1171 return FindFreePages (MaxAddress
, NoPages
, NewType
, Alignment
);
1176 Allocates pages from the memory map.
1178 @param Type The type of allocation to perform
1179 @param MemoryType The type of memory to turn the allocated pages
1181 @param NumberOfPages The number of pages to allocate
1182 @param Memory A pointer to receive the base allocated memory
1185 @return Status. On success, Memory is filled in with the base address allocated
1186 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1188 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1189 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1190 @retval EFI_SUCCESS Pages successfully allocated.
1195 CoreInternalAllocatePages (
1196 IN EFI_ALLOCATE_TYPE Type
,
1197 IN EFI_MEMORY_TYPE MemoryType
,
1198 IN UINTN NumberOfPages
,
1199 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1204 UINT64 NumberOfBytes
;
1209 if ((UINT32
)Type
>= MaxAllocateType
) {
1210 return EFI_INVALID_PARAMETER
;
1213 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
< MEMORY_TYPE_OEM_RESERVED_MIN
) ||
1214 (MemoryType
== EfiConventionalMemory
) || (MemoryType
== EfiPersistentMemory
)) {
1215 return EFI_INVALID_PARAMETER
;
1218 if (Memory
== NULL
) {
1219 return EFI_INVALID_PARAMETER
;
1222 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1224 if (MemoryType
== EfiACPIReclaimMemory
||
1225 MemoryType
== EfiACPIMemoryNVS
||
1226 MemoryType
== EfiRuntimeServicesCode
||
1227 MemoryType
== EfiRuntimeServicesData
) {
1229 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1232 if (Type
== AllocateAddress
) {
1233 if ((*Memory
& (Alignment
- 1)) != 0) {
1234 return EFI_NOT_FOUND
;
1238 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1239 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1242 // If this is for below a particular address, then
1247 // The max address is the max natively addressable address for the processor
1249 MaxAddress
= MAX_ADDRESS
;
1252 // Check for Type AllocateAddress,
1253 // if NumberOfPages is 0 or
1254 // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or
1255 // if (Start + NumberOfBytes) rolls over 0 or
1256 // if Start is above MAX_ADDRESS or
1257 // if End is above MAX_ADDRESS,
1258 // return EFI_NOT_FOUND.
1260 if (Type
== AllocateAddress
) {
1261 if ((NumberOfPages
== 0) ||
1262 (NumberOfPages
> RShiftU64 (MaxAddress
, EFI_PAGE_SHIFT
))) {
1263 return EFI_NOT_FOUND
;
1265 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1266 End
= Start
+ NumberOfBytes
- 1;
1268 if ((Start
>= End
) ||
1269 (Start
> MaxAddress
) ||
1270 (End
> MaxAddress
)) {
1271 return EFI_NOT_FOUND
;
1275 if (Type
== AllocateMaxAddress
) {
1279 CoreAcquireMemoryLock ();
1282 // If not a specific address, then find an address to allocate
1284 if (Type
!= AllocateAddress
) {
1285 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1287 Status
= EFI_OUT_OF_RESOURCES
;
1293 // Convert pages from FreeMemory to the requested type
1295 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1298 CoreReleaseMemoryLock ();
1300 if (!EFI_ERROR (Status
)) {
1308 Allocates pages from the memory map.
1310 @param Type The type of allocation to perform
1311 @param MemoryType The type of memory to turn the allocated pages
1313 @param NumberOfPages The number of pages to allocate
1314 @param Memory A pointer to receive the base allocated memory
1317 @return Status. On success, Memory is filled in with the base address allocated
1318 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1320 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1321 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1322 @retval EFI_SUCCESS Pages successfully allocated.
1328 IN EFI_ALLOCATE_TYPE Type
,
1329 IN EFI_MEMORY_TYPE MemoryType
,
1330 IN UINTN NumberOfPages
,
1331 OUT EFI_PHYSICAL_ADDRESS
*Memory
1336 Status
= CoreInternalAllocatePages (Type
, MemoryType
, NumberOfPages
, Memory
);
1337 if (!EFI_ERROR (Status
)) {
1339 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1340 MemoryProfileActionAllocatePages
,
1342 EFI_PAGES_TO_SIZE (NumberOfPages
),
1343 (VOID
*) (UINTN
) *Memory
,
1346 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType
);
1352 Frees previous allocated pages.
1354 @param Memory Base address of memory being freed
1355 @param NumberOfPages The number of pages to free
1356 @param MemoryType Pointer to memory type
1358 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1359 @retval EFI_INVALID_PARAMETER Address not aligned
1360 @return EFI_SUCCESS -Pages successfully freed.
1365 CoreInternalFreePages (
1366 IN EFI_PHYSICAL_ADDRESS Memory
,
1367 IN UINTN NumberOfPages
,
1368 OUT EFI_MEMORY_TYPE
*MemoryType OPTIONAL
1379 CoreAcquireMemoryLock ();
1382 // Find the entry that the covers the range
1385 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1386 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1387 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1391 if (Link
== &gMemoryMap
) {
1392 Status
= EFI_NOT_FOUND
;
1396 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1398 ASSERT (Entry
!= NULL
);
1399 if (Entry
->Type
== EfiACPIReclaimMemory
||
1400 Entry
->Type
== EfiACPIMemoryNVS
||
1401 Entry
->Type
== EfiRuntimeServicesCode
||
1402 Entry
->Type
== EfiRuntimeServicesData
) {
1404 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1408 if ((Memory
& (Alignment
- 1)) != 0) {
1409 Status
= EFI_INVALID_PARAMETER
;
1413 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1414 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1416 if (MemoryType
!= NULL
) {
1417 *MemoryType
= Entry
->Type
;
1420 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1422 if (EFI_ERROR (Status
)) {
1427 CoreReleaseMemoryLock ();
1432 Frees previous allocated pages.
1434 @param Memory Base address of memory being freed
1435 @param NumberOfPages The number of pages to free
1437 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1438 @retval EFI_INVALID_PARAMETER Address not aligned
1439 @return EFI_SUCCESS -Pages successfully freed.
1445 IN EFI_PHYSICAL_ADDRESS Memory
,
1446 IN UINTN NumberOfPages
1450 EFI_MEMORY_TYPE MemoryType
;
1452 Status
= CoreInternalFreePages (Memory
, NumberOfPages
, &MemoryType
);
1453 if (!EFI_ERROR (Status
)) {
1455 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1456 MemoryProfileActionFreePages
,
1458 EFI_PAGES_TO_SIZE (NumberOfPages
),
1459 (VOID
*) (UINTN
) Memory
,
1462 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType
);
1468 This function checks to see if the last memory map descriptor in a memory map
1469 can be merged with any of the other memory map descriptors in a memorymap.
1470 Memory descriptors may be merged if they are adjacent and have the same type
1473 @param MemoryMap A pointer to the start of the memory map.
1474 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.
1475 @param DescriptorSize The size, in bytes, of an individual
1476 EFI_MEMORY_DESCRIPTOR.
1478 @return A pointer to the next available descriptor in MemoryMap
1481 EFI_MEMORY_DESCRIPTOR
*
1482 MergeMemoryMapDescriptor (
1483 IN EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1484 IN EFI_MEMORY_DESCRIPTOR
*MemoryMapDescriptor
,
1485 IN UINTN DescriptorSize
1489 // Traverse the array of descriptors in MemoryMap
1491 for (; MemoryMap
!= MemoryMapDescriptor
; MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, DescriptorSize
)) {
1493 // Check to see if the Type fields are identical.
1495 if (MemoryMap
->Type
!= MemoryMapDescriptor
->Type
) {
1500 // Check to see if the Attribute fields are identical.
1502 if (MemoryMap
->Attribute
!= MemoryMapDescriptor
->Attribute
) {
1507 // Check to see if MemoryMapDescriptor is immediately above MemoryMap
1509 if (MemoryMap
->PhysicalStart
+ EFI_PAGES_TO_SIZE ((UINTN
)MemoryMap
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1511 // Merge MemoryMapDescriptor into MemoryMap
1513 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1516 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1518 return MemoryMapDescriptor
;
1522 // Check to see if MemoryMapDescriptor is immediately below MemoryMap
1524 if (MemoryMap
->PhysicalStart
- EFI_PAGES_TO_SIZE ((UINTN
)MemoryMapDescriptor
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1526 // Merge MemoryMapDescriptor into MemoryMap
1528 MemoryMap
->PhysicalStart
= MemoryMapDescriptor
->PhysicalStart
;
1529 MemoryMap
->VirtualStart
= MemoryMapDescriptor
->VirtualStart
;
1530 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1533 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1535 return MemoryMapDescriptor
;
1540 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.
1542 // Return the slot immediately after MemoryMapDescriptor as the next available
1543 // slot in the MemoryMap array
1545 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor
, DescriptorSize
);
1549 This function returns a copy of the current memory map. The map is an array of
1550 memory descriptors, each of which describes a contiguous block of memory.
1552 @param MemoryMapSize A pointer to the size, in bytes, of the
1553 MemoryMap buffer. On input, this is the size of
1554 the buffer allocated by the caller. On output,
1555 it is the size of the buffer returned by the
1556 firmware if the buffer was large enough, or the
1557 size of the buffer needed to contain the map if
1558 the buffer was too small.
1559 @param MemoryMap A pointer to the buffer in which firmware places
1560 the current memory map.
1561 @param MapKey A pointer to the location in which firmware
1562 returns the key for the current memory map.
1563 @param DescriptorSize A pointer to the location in which firmware
1564 returns the size, in bytes, of an individual
1565 EFI_MEMORY_DESCRIPTOR.
1566 @param DescriptorVersion A pointer to the location in which firmware
1567 returns the version number associated with the
1568 EFI_MEMORY_DESCRIPTOR.
1570 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1572 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1573 buffer size needed to hold the memory map is
1574 returned in MemoryMapSize.
1575 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1581 IN OUT UINTN
*MemoryMapSize
,
1582 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1584 OUT UINTN
*DescriptorSize
,
1585 OUT UINT32
*DescriptorVersion
1591 UINTN NumberOfEntries
;
1594 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1595 EFI_GCD_MAP_ENTRY MergeGcdMapEntry
;
1596 EFI_MEMORY_TYPE Type
;
1597 EFI_MEMORY_DESCRIPTOR
*MemoryMapStart
;
1600 // Make sure the parameters are valid
1602 if (MemoryMapSize
== NULL
) {
1603 return EFI_INVALID_PARAMETER
;
1606 CoreAcquireGcdMemoryLock ();
1609 // Count the number of Reserved and runtime MMIO entries
1610 // And, count the number of Persistent entries.
1612 NumberOfEntries
= 0;
1613 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1614 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1615 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypePersistentMemory
) ||
1616 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1617 ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
1618 ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
))) {
1623 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1626 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1627 // prevent people from having pointer math bugs in their code.
1628 // now you have to use *DescriptorSize to make things work.
1630 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1632 if (DescriptorSize
!= NULL
) {
1633 *DescriptorSize
= Size
;
1636 if (DescriptorVersion
!= NULL
) {
1637 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1640 CoreAcquireMemoryLock ();
1643 // Compute the buffer size needed to fit the entire map
1645 BufferSize
= Size
* NumberOfEntries
;
1646 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1650 if (*MemoryMapSize
< BufferSize
) {
1651 Status
= EFI_BUFFER_TOO_SMALL
;
1655 if (MemoryMap
== NULL
) {
1656 Status
= EFI_INVALID_PARAMETER
;
1663 ZeroMem (MemoryMap
, BufferSize
);
1664 MemoryMapStart
= MemoryMap
;
1665 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1666 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1667 ASSERT (Entry
->VirtualStart
== 0);
1670 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1672 MemoryMap
->Type
= Entry
->Type
;
1673 MemoryMap
->PhysicalStart
= Entry
->Start
;
1674 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1675 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1677 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1678 // memory type bin and needs to be converted to the same memory type as the rest of the
1679 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1680 // improves the chances for a successful S4 resume in the presence of minor page allocation
1681 // differences across reboots.
1683 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1684 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1685 if (mMemoryTypeStatistics
[Type
].Special
&&
1686 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1687 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1688 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1689 MemoryMap
->Type
= Type
;
1693 MemoryMap
->Attribute
= Entry
->Attribute
;
1694 if (MemoryMap
->Type
< EfiMaxMemoryType
) {
1695 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1696 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1701 // Check to see if the new Memory Map Descriptor can be merged with an
1702 // existing descriptor if they are adjacent and have the same attributes
1704 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1708 ZeroMem (&MergeGcdMapEntry
, sizeof (MergeGcdMapEntry
));
1710 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; ; Link
= Link
->ForwardLink
) {
1711 if (Link
!= &mGcdMemorySpaceMap
) {
1713 // Merge adjacent same type and attribute GCD memory range
1715 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1717 if ((MergeGcdMapEntry
.Capabilities
== GcdMapEntry
->Capabilities
) &&
1718 (MergeGcdMapEntry
.Attributes
== GcdMapEntry
->Attributes
) &&
1719 (MergeGcdMapEntry
.GcdMemoryType
== GcdMapEntry
->GcdMemoryType
) &&
1720 (MergeGcdMapEntry
.GcdIoType
== GcdMapEntry
->GcdIoType
)) {
1721 MergeGcdMapEntry
.EndAddress
= GcdMapEntry
->EndAddress
;
1726 if ((MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1727 ((MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
1728 ((MergeGcdMapEntry
.Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
))) {
1730 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1731 // it will be recorded as page PhysicalStart and NumberOfPages.
1733 ASSERT ((MergeGcdMapEntry
.BaseAddress
& EFI_PAGE_MASK
) == 0);
1734 ASSERT (((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1) & EFI_PAGE_MASK
) == 0);
1737 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries
1739 MemoryMap
->PhysicalStart
= MergeGcdMapEntry
.BaseAddress
;
1740 MemoryMap
->VirtualStart
= 0;
1741 MemoryMap
->NumberOfPages
= RShiftU64 ((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1742 MemoryMap
->Attribute
= (MergeGcdMapEntry
.Attributes
& ~EFI_MEMORY_PORT_IO
) |
1743 (MergeGcdMapEntry
.Capabilities
& (EFI_MEMORY_RP
| EFI_MEMORY_WP
| EFI_MEMORY_XP
| EFI_MEMORY_RO
|
1744 EFI_MEMORY_UC
| EFI_MEMORY_UCE
| EFI_MEMORY_WC
| EFI_MEMORY_WT
| EFI_MEMORY_WB
));
1746 if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1747 MemoryMap
->Type
= EfiReservedMemoryType
;
1748 } else if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1749 if ((MergeGcdMapEntry
.Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1750 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1752 MemoryMap
->Type
= EfiMemoryMappedIO
;
1757 // Check to see if the new Memory Map Descriptor can be merged with an
1758 // existing descriptor if they are adjacent and have the same attributes
1760 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1763 if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypePersistentMemory
) {
1765 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1766 // it will be recorded as page PhysicalStart and NumberOfPages.
1768 ASSERT ((MergeGcdMapEntry
.BaseAddress
& EFI_PAGE_MASK
) == 0);
1769 ASSERT (((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1) & EFI_PAGE_MASK
) == 0);
1772 // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries
1774 MemoryMap
->PhysicalStart
= MergeGcdMapEntry
.BaseAddress
;
1775 MemoryMap
->VirtualStart
= 0;
1776 MemoryMap
->NumberOfPages
= RShiftU64 ((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1777 MemoryMap
->Attribute
= MergeGcdMapEntry
.Attributes
| EFI_MEMORY_NV
|
1778 (MergeGcdMapEntry
.Capabilities
& (EFI_MEMORY_RP
| EFI_MEMORY_WP
| EFI_MEMORY_XP
| EFI_MEMORY_RO
|
1779 EFI_MEMORY_UC
| EFI_MEMORY_UCE
| EFI_MEMORY_WC
| EFI_MEMORY_WT
| EFI_MEMORY_WB
));
1780 MemoryMap
->Type
= EfiPersistentMemory
;
1783 // Check to see if the new Memory Map Descriptor can be merged with an
1784 // existing descriptor if they are adjacent and have the same attributes
1786 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1788 if (Link
== &mGcdMemorySpaceMap
) {
1790 // break loop when arrive at head.
1794 if (GcdMapEntry
!= NULL
) {
1796 // Copy new GCD map entry for the following GCD range merge
1798 CopyMem (&MergeGcdMapEntry
, GcdMapEntry
, sizeof (MergeGcdMapEntry
));
1803 // Compute the size of the buffer actually used after all memory map descriptor merge operations
1805 BufferSize
= ((UINT8
*)MemoryMap
- (UINT8
*)MemoryMapStart
);
1807 Status
= EFI_SUCCESS
;
1811 // Update the map key finally
1813 if (MapKey
!= NULL
) {
1814 *MapKey
= mMemoryMapKey
;
1817 CoreReleaseMemoryLock ();
1819 CoreReleaseGcdMemoryLock ();
1821 *MemoryMapSize
= BufferSize
;
1828 Internal function. Used by the pool functions to allocate pages
1829 to back pool allocation requests.
1831 @param PoolType The type of memory for the new pool pages
1832 @param NumberOfPages No of pages to allocate
1833 @param Alignment Bits to align.
1835 @return The allocated memory, or NULL
1839 CoreAllocatePoolPages (
1840 IN EFI_MEMORY_TYPE PoolType
,
1841 IN UINTN NumberOfPages
,
1848 // Find the pages to convert
1850 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1853 // Convert it to boot services data
1856 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1858 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1861 return (VOID
*)(UINTN
) Start
;
1866 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1868 @param Memory The base address to free
1869 @param NumberOfPages The number of pages to free
1874 IN EFI_PHYSICAL_ADDRESS Memory
,
1875 IN UINTN NumberOfPages
1878 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1884 Make sure the memory map is following all the construction rules,
1885 it is the last time to check memory map error before exit boot services.
1887 @param MapKey Memory map key
1889 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1891 @retval EFI_SUCCESS Valid memory map.
1895 CoreTerminateMemoryMap (
1903 Status
= EFI_SUCCESS
;
1905 CoreAcquireMemoryLock ();
1907 if (MapKey
== mMemoryMapKey
) {
1910 // Make sure the memory map is following all the construction rules
1911 // This is the last chance we will be able to display any messages on
1912 // the console devices.
1915 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1916 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1917 if (Entry
->Type
< EfiMaxMemoryType
) {
1918 if (mMemoryTypeStatistics
[Entry
->Type
].Runtime
) {
1919 ASSERT (Entry
->Type
!= EfiACPIReclaimMemory
);
1920 ASSERT (Entry
->Type
!= EfiACPIMemoryNVS
);
1921 if ((Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1922 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1923 Status
= EFI_INVALID_PARAMETER
;
1926 if (((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1927 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1928 Status
= EFI_INVALID_PARAMETER
;
1936 // The map key they gave us matches what we expect. Fall through and
1937 // return success. In an ideal world we would clear out all of
1938 // EfiBootServicesCode and EfiBootServicesData. However this function
1939 // is not the last one called by ExitBootServices(), so we have to
1940 // preserve the memory contents.
1943 Status
= EFI_INVALID_PARAMETER
;
1947 CoreReleaseMemoryLock ();