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.
19 // Entry for tracking the memory regions for each memory type to coalesce similar memory types
22 EFI_PHYSICAL_ADDRESS BaseAddress
;
23 EFI_PHYSICAL_ADDRESS MaximumAddress
;
24 UINT64 CurrentNumberOfPages
;
26 UINTN InformationIndex
;
29 } EFI_MEMORY_TYPE_STATISTICS
;
32 // MemoryMap - The current memory map
34 UINTN mMemoryMapKey
= 0;
36 #define MAX_MAP_DEPTH 6
39 /// mMapDepth - depth of new descriptor stack
43 /// mMapStack - space to use as temp storage to build new map descriptors
45 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
46 UINTN mFreeMapStack
= 0;
48 /// This list maintain the free memory map list
50 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
51 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
53 EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
54 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiReservedMemoryType
55 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderCode
56 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderData
57 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesCode
58 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesData
59 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesCode
60 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesData
61 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiConventionalMemory
62 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiUnusableMemory
63 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIReclaimMemory
64 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIMemoryNVS
65 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIO
66 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIOPortSpace
67 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiPalCode
68 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiPersistentMemory
69 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
} // EfiMaxMemoryType
72 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress
= MAX_ADDRESS
;
73 EFI_PHYSICAL_ADDRESS mDefaultBaseAddress
= MAX_ADDRESS
;
75 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
76 { EfiReservedMemoryType
, 0 },
79 { EfiBootServicesCode
, 0 },
80 { EfiBootServicesData
, 0 },
81 { EfiRuntimeServicesCode
, 0 },
82 { EfiRuntimeServicesData
, 0 },
83 { EfiConventionalMemory
, 0 },
84 { EfiUnusableMemory
, 0 },
85 { EfiACPIReclaimMemory
, 0 },
86 { EfiACPIMemoryNVS
, 0 },
87 { EfiMemoryMappedIO
, 0 },
88 { EfiMemoryMappedIOPortSpace
, 0 },
90 { EfiPersistentMemory
, 0 },
91 { EfiMaxMemoryType
, 0 }
94 // Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated
95 // and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a
96 // address assigned by DXE core.
98 GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady
= FALSE
;
101 Enter critical section by gaining lock on gMemoryLock.
105 CoreAcquireMemoryLock (
109 CoreAcquireLock (&gMemoryLock
);
115 Exit critical section by releasing lock on gMemoryLock.
119 CoreReleaseMemoryLock (
123 CoreReleaseLock (&gMemoryLock
);
130 Internal function. Removes a descriptor entry.
132 @param Entry The entry to remove
136 RemoveMemoryMapEntry (
137 IN OUT MEMORY_MAP
*Entry
140 RemoveEntryList (&Entry
->Link
);
141 Entry
->Link
.ForwardLink
= NULL
;
143 if (Entry
->FromPages
) {
145 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
147 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
152 Internal function. Adds a ranges to the memory map.
153 The range must not already exist in the map.
155 @param Type The type of memory range to add
156 @param Start The starting address in the memory range Must be
158 @param End The last address in the range Must be the last
160 @param Attribute The attributes of the memory range to add
165 IN EFI_MEMORY_TYPE Type
,
166 IN EFI_PHYSICAL_ADDRESS Start
,
167 IN EFI_PHYSICAL_ADDRESS End
,
174 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
175 ASSERT (End
> Start
) ;
177 ASSERT_LOCKED (&gMemoryLock
);
179 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
182 // If memory of type EfiConventionalMemory is being added that includes the page
183 // starting at address 0, then zero the page starting at address 0. This has
184 // two benifits. It helps find NULL pointer bugs and it also maximizes
185 // compatibility with operating systems that may evaluate memory in this page
186 // for legacy data structures. If memory of any other type is added starting
187 // at address 0, then do not zero the page at address 0 because the page is being
188 // used for other purposes.
190 if (Type
== EfiConventionalMemory
&& Start
== 0 && (End
>= EFI_PAGE_SIZE
- 1)) {
191 SetMem ((VOID
*)(UINTN
)Start
, EFI_PAGE_SIZE
, 0);
195 // Memory map being altered so updated key
200 // UEFI 2.0 added an event group for notificaiton on memory map changes.
201 // So we need to signal this Event Group every time the memory map changes.
202 // If we are in EFI 1.10 compatability mode no event groups will be
203 // found and nothing will happen we we call this function. These events
204 // will get signaled but since a lock is held around the call to this
205 // function the notificaiton events will only be called after this function
206 // returns and the lock is released.
208 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
211 // Look for adjoining memory descriptor
214 // Two memory descriptors can only be merged if they have the same Type
215 // and the same Attribute
218 Link
= gMemoryMap
.ForwardLink
;
219 while (Link
!= &gMemoryMap
) {
220 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
221 Link
= Link
->ForwardLink
;
223 if (Entry
->Type
!= Type
) {
227 if (Entry
->Attribute
!= Attribute
) {
231 if (Entry
->End
+ 1 == Start
) {
233 Start
= Entry
->Start
;
234 RemoveMemoryMapEntry (Entry
);
236 } else if (Entry
->Start
== End
+ 1) {
239 RemoveMemoryMapEntry (Entry
);
247 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
248 mMapStack
[mMapDepth
].FromPages
= FALSE
;
249 mMapStack
[mMapDepth
].Type
= Type
;
250 mMapStack
[mMapDepth
].Start
= Start
;
251 mMapStack
[mMapDepth
].End
= End
;
252 mMapStack
[mMapDepth
].VirtualStart
= 0;
253 mMapStack
[mMapDepth
].Attribute
= Attribute
;
254 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
257 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
263 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
264 If the list is emtry, then allocate a new page to refuel the list.
265 Please Note this algorithm to allocate the memory map descriptor has a property
266 that the memory allocated for memory entries always grows, and will never really be freed
267 For example, if the current boot uses 2000 memory map entries at the maximum point, but
268 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
269 memory map entries is still allocated from EfiBootServicesMemory.
272 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
276 AllocateMemoryMapEntry (
280 MEMORY_MAP
* FreeDescriptorEntries
;
284 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
286 // The list is empty, to allocate one page to refuel the list
288 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
,
289 EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION_GRANULARITY
),
290 DEFAULT_PAGE_ALLOCATION_GRANULARITY
);
291 if (FreeDescriptorEntries
!= NULL
) {
293 // Enque the free memmory map entries into the list
295 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION_GRANULARITY
/ 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 ();
556 ApplyMemoryProtectionPolicy (EfiMaxMemoryType
, Type
, Start
,
557 LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
));
560 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type
562 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
563 CoreLoadingFixedAddressHook();
567 // Check to see if the statistics for the different memory types have already been established
569 if (mMemoryTypeInformationInitialized
) {
575 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
577 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
579 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
581 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
582 if ((UINT32
)Type
> EfiMaxMemoryType
) {
585 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
587 // Allocate pages for the current memory type from the top of available memory
589 Status
= CoreAllocatePages (
592 gMemoryTypeInformation
[Index
].NumberOfPages
,
593 &mMemoryTypeStatistics
[Type
].BaseAddress
595 if (EFI_ERROR (Status
)) {
597 // If an error occurs allocating the pages for the current memory type, then
598 // free all the pages allocates for the previous memory types and return. This
599 // operation with be retied when/if more memory is added to the system
601 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
603 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
605 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
606 if ((UINT32
)Type
> EfiMaxMemoryType
) {
610 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
612 mMemoryTypeStatistics
[Type
].BaseAddress
,
613 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
615 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
616 mMemoryTypeStatistics
[Type
].MaximumAddress
= MAX_ADDRESS
;
623 // Compute the address at the top of the current statistics
625 mMemoryTypeStatistics
[Type
].MaximumAddress
=
626 mMemoryTypeStatistics
[Type
].BaseAddress
+
627 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
630 // If the current base address is the lowest address so far, then update the default
633 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
634 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
640 // There was enough system memory for all the the memory types were allocated. So,
641 // those memory areas can be freed for future allocations, and all future memory
642 // allocations can occur within their respective bins
644 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
646 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
648 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
649 if ((UINT32
)Type
> EfiMaxMemoryType
) {
652 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
654 mMemoryTypeStatistics
[Type
].BaseAddress
,
655 gMemoryTypeInformation
[Index
].NumberOfPages
657 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
658 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
663 // If the number of pages reserved for a memory type is 0, then all allocations for that type
664 // should be in the default range.
666 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
667 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
668 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
669 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
672 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
673 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== MAX_ADDRESS
) {
674 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
678 mMemoryTypeInformationInitialized
= TRUE
;
683 Internal function. Converts a memory range to the specified type or attributes.
684 The range must exist in the memory map. Either ChangingType or
685 ChangingAttributes must be set, but not both.
687 @param Start The first address of the range Must be page
689 @param NumberOfPages The number of pages to convert
690 @param ChangingType Boolean indicating that type value should be changed
691 @param NewType The new type for the memory range
692 @param ChangingAttributes Boolean indicating that attributes value should be changed
693 @param NewAttributes The new attributes for the memory range
695 @retval EFI_INVALID_PARAMETER Invalid parameter
696 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
697 range or convertion not allowed.
698 @retval EFI_SUCCESS Successfully converts the memory range to the
705 IN UINT64 NumberOfPages
,
706 IN BOOLEAN ChangingType
,
707 IN EFI_MEMORY_TYPE NewType
,
708 IN BOOLEAN ChangingAttributes
,
709 IN UINT64 NewAttributes
713 UINT64 NumberOfBytes
;
717 EFI_MEMORY_TYPE MemType
;
722 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
723 End
= Start
+ NumberOfBytes
- 1;
725 ASSERT (NumberOfPages
);
726 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
727 ASSERT (End
> Start
) ;
728 ASSERT_LOCKED (&gMemoryLock
);
729 ASSERT ( (ChangingType
== FALSE
) || (ChangingAttributes
== FALSE
) );
731 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
>= End
)) {
732 return EFI_INVALID_PARAMETER
;
736 // Convert the entire range
739 while (Start
< End
) {
742 // Find the entry that the covers the range
744 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
745 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
747 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
752 if (Link
== &gMemoryMap
) {
753 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
754 return EFI_NOT_FOUND
;
758 // Convert range to the end, or to the end of the descriptor
759 // if that's all we've got
763 ASSERT (Entry
!= NULL
);
764 if (Entry
->End
< End
) {
765 RangeEnd
= Entry
->End
;
769 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to type %d\n", Start
, RangeEnd
, NewType
));
771 if (ChangingAttributes
) {
772 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to attr %lx\n", Start
, RangeEnd
, NewAttributes
));
777 // Debug code - verify conversion is allowed
779 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
780 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types\n"));
781 return EFI_NOT_FOUND
;
785 // Update counters for the number of pages allocated to each memory type
787 if ((UINT32
)Entry
->Type
< EfiMaxMemoryType
) {
788 if ((Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) ||
789 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
790 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
791 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
793 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
798 if ((UINT32
)NewType
< EfiMaxMemoryType
) {
799 if ((Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) ||
800 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
801 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
802 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
> gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
803 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
810 // Pull range out of descriptor
812 if (Entry
->Start
== Start
) {
817 Entry
->Start
= RangeEnd
+ 1;
819 } else if (Entry
->End
== RangeEnd
) {
824 Entry
->End
= Start
- 1;
829 // Pull it out of the center, clip current
835 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
836 mMapStack
[mMapDepth
].FromPages
= FALSE
;
837 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
838 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
839 mMapStack
[mMapDepth
].End
= Entry
->End
;
842 // Inherit Attribute from the Memory Descriptor that is being clipped
844 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
846 Entry
->End
= Start
- 1;
847 ASSERT (Entry
->Start
< Entry
->End
);
849 Entry
= &mMapStack
[mMapDepth
];
850 InsertTailList (&gMemoryMap
, &Entry
->Link
);
853 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
857 // The new range inherits the same Attribute as the Entry
858 // it is being cut out of unless attributes are being changed
861 Attribute
= Entry
->Attribute
;
864 Attribute
= NewAttributes
;
865 MemType
= Entry
->Type
;
869 // If the descriptor is empty, then remove it from the map
871 if (Entry
->Start
== Entry
->End
+ 1) {
872 RemoveMemoryMapEntry (Entry
);
877 // Add our new range in
879 CoreAddRange (MemType
, Start
, RangeEnd
, Attribute
);
880 if (ChangingType
&& (MemType
== EfiConventionalMemory
)) {
882 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
883 // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees
884 // that the page starting at address 0 is always filled with zeros.
887 if (RangeEnd
> EFI_PAGE_SIZE
) {
888 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) EFI_PAGE_SIZE
, (UINTN
) (RangeEnd
- EFI_PAGE_SIZE
+ 1));
891 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) Start
, (UINTN
) (RangeEnd
- Start
+ 1));
896 // Move any map descriptor stack to general pool
898 CoreFreeMemoryMapStack ();
901 // Bump the starting address, and convert the next range
903 Start
= RangeEnd
+ 1;
907 // Converted the whole range, done
915 Internal function. Converts a memory range to the specified type.
916 The range must exist in the memory map.
918 @param Start The first address of the range Must be page
920 @param NumberOfPages The number of pages to convert
921 @param NewType The new type for the memory range
923 @retval EFI_INVALID_PARAMETER Invalid parameter
924 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
925 range or convertion not allowed.
926 @retval EFI_SUCCESS Successfully converts the memory range to the
933 IN UINT64 NumberOfPages
,
934 IN EFI_MEMORY_TYPE NewType
937 return CoreConvertPagesEx(Start
, NumberOfPages
, TRUE
, NewType
, FALSE
, 0);
942 Internal function. Converts a memory range to use new attributes.
944 @param Start The first address of the range Must be page
946 @param NumberOfPages The number of pages to convert
947 @param NewAttributes The new attributes value for the range.
951 CoreUpdateMemoryAttributes (
952 IN EFI_PHYSICAL_ADDRESS Start
,
953 IN UINT64 NumberOfPages
,
954 IN UINT64 NewAttributes
957 CoreAcquireMemoryLock ();
960 // Update the attributes to the new value
962 CoreConvertPagesEx(Start
, NumberOfPages
, FALSE
, (EFI_MEMORY_TYPE
)0, TRUE
, NewAttributes
);
964 CoreReleaseMemoryLock ();
969 Internal function. Finds a consecutive free page range below
970 the requested address.
972 @param MaxAddress The address that the range must be below
973 @param MinAddress The address that the range must be above
974 @param NumberOfPages Number of pages needed
975 @param NewType The type of memory the range is going to be
977 @param Alignment Bits to align with
979 @return The base address of the range, or 0 if the range was not found
984 IN UINT64 MaxAddress
,
985 IN UINT64 MinAddress
,
986 IN UINT64 NumberOfPages
,
987 IN EFI_MEMORY_TYPE NewType
,
991 UINT64 NumberOfBytes
;
995 UINT64 DescNumberOfBytes
;
999 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
1003 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
1006 // If MaxAddress is not aligned to the end of a page
1010 // Change MaxAddress to be 1 page lower
1012 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
1015 // Set MaxAddress to a page boundary
1017 MaxAddress
&= ~(UINT64
)EFI_PAGE_MASK
;
1020 // Set MaxAddress to end of the page
1022 MaxAddress
|= EFI_PAGE_MASK
;
1025 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1028 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1029 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1032 // If it's not a free entry, don't bother with it
1034 if (Entry
->Type
!= EfiConventionalMemory
) {
1038 DescStart
= Entry
->Start
;
1039 DescEnd
= Entry
->End
;
1042 // If desc is past max allowed address or below min allowed address, skip it
1044 if ((DescStart
>= MaxAddress
) || (DescEnd
< MinAddress
)) {
1049 // If desc ends past max allowed address, clip the end
1051 if (DescEnd
>= MaxAddress
) {
1052 DescEnd
= MaxAddress
;
1055 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
1057 // Skip if DescEnd is less than DescStart after alignment clipping
1058 if (DescEnd
< DescStart
) {
1063 // Compute the number of bytes we can used from this
1064 // descriptor, and see it's enough to satisfy the request
1066 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
1068 if (DescNumberOfBytes
>= NumberOfBytes
) {
1070 // If the start of the allocated range is below the min address allowed, skip it
1072 if ((DescEnd
- NumberOfBytes
+ 1) < MinAddress
) {
1077 // If this is the best match so far remember it
1079 if (DescEnd
> Target
) {
1086 // If this is a grow down, adjust target to be the allocation base
1088 Target
-= NumberOfBytes
- 1;
1091 // If we didn't find a match, return 0
1093 if ((Target
& EFI_PAGE_MASK
) != 0) {
1102 Internal function. Finds a consecutive free page range below
1103 the requested address
1105 @param MaxAddress The address that the range must be below
1106 @param NoPages Number of pages needed
1107 @param NewType The type of memory the range is going to be
1109 @param Alignment Bits to align with
1111 @return The base address of the range, or 0 if the range was not found.
1116 IN UINT64 MaxAddress
,
1118 IN EFI_MEMORY_TYPE NewType
,
1125 // Attempt to find free pages in the preferred bin based on the requested memory type
1127 if ((UINT32
)NewType
< EfiMaxMemoryType
&& MaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1128 Start
= CoreFindFreePagesI (
1129 mMemoryTypeStatistics
[NewType
].MaximumAddress
,
1130 mMemoryTypeStatistics
[NewType
].BaseAddress
,
1141 // Attempt to find free pages in the default allocation bin
1143 if (MaxAddress
>= mDefaultMaximumAddress
) {
1144 Start
= CoreFindFreePagesI (mDefaultMaximumAddress
, 0, NoPages
, NewType
, Alignment
);
1146 if (Start
< mDefaultBaseAddress
) {
1147 mDefaultBaseAddress
= Start
;
1154 // The allocation did not succeed in any of the prefered bins even after
1155 // promoting resources. Attempt to find free pages anywhere is the requested
1156 // address range. If this allocation fails, then there are not enough
1157 // resources anywhere to satisfy the request.
1159 Start
= CoreFindFreePagesI (MaxAddress
, 0, NoPages
, NewType
, Alignment
);
1165 // If allocations from the preferred bins fail, then attempt to promote memory resources.
1167 if (!PromoteMemoryResource ()) {
1172 // If any memory resources were promoted, then re-attempt the allocation
1174 return FindFreePages (MaxAddress
, NoPages
, NewType
, Alignment
);
1179 Allocates pages from the memory map.
1181 @param Type The type of allocation to perform
1182 @param MemoryType The type of memory to turn the allocated pages
1184 @param NumberOfPages The number of pages to allocate
1185 @param Memory A pointer to receive the base allocated memory
1188 @return Status. On success, Memory is filled in with the base address allocated
1189 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1191 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1192 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1193 @retval EFI_SUCCESS Pages successfully allocated.
1198 CoreInternalAllocatePages (
1199 IN EFI_ALLOCATE_TYPE Type
,
1200 IN EFI_MEMORY_TYPE MemoryType
,
1201 IN UINTN NumberOfPages
,
1202 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1207 UINT64 NumberOfBytes
;
1212 if ((UINT32
)Type
>= MaxAllocateType
) {
1213 return EFI_INVALID_PARAMETER
;
1216 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
< MEMORY_TYPE_OEM_RESERVED_MIN
) ||
1217 (MemoryType
== EfiConventionalMemory
) || (MemoryType
== EfiPersistentMemory
)) {
1218 return EFI_INVALID_PARAMETER
;
1221 if (Memory
== NULL
) {
1222 return EFI_INVALID_PARAMETER
;
1225 Alignment
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
1227 if (MemoryType
== EfiACPIReclaimMemory
||
1228 MemoryType
== EfiACPIMemoryNVS
||
1229 MemoryType
== EfiRuntimeServicesCode
||
1230 MemoryType
== EfiRuntimeServicesData
) {
1232 Alignment
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
1235 if (Type
== AllocateAddress
) {
1236 if ((*Memory
& (Alignment
- 1)) != 0) {
1237 return EFI_NOT_FOUND
;
1241 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1242 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1245 // If this is for below a particular address, then
1250 // The max address is the max natively addressable address for the processor
1252 MaxAddress
= MAX_ADDRESS
;
1255 // Check for Type AllocateAddress,
1256 // if NumberOfPages is 0 or
1257 // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or
1258 // if (Start + NumberOfBytes) rolls over 0 or
1259 // if Start is above MAX_ADDRESS or
1260 // if End is above MAX_ADDRESS,
1261 // return EFI_NOT_FOUND.
1263 if (Type
== AllocateAddress
) {
1264 if ((NumberOfPages
== 0) ||
1265 (NumberOfPages
> RShiftU64 (MaxAddress
, EFI_PAGE_SHIFT
))) {
1266 return EFI_NOT_FOUND
;
1268 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1269 End
= Start
+ NumberOfBytes
- 1;
1271 if ((Start
>= End
) ||
1272 (Start
> MaxAddress
) ||
1273 (End
> MaxAddress
)) {
1274 return EFI_NOT_FOUND
;
1278 if (Type
== AllocateMaxAddress
) {
1282 CoreAcquireMemoryLock ();
1285 // If not a specific address, then find an address to allocate
1287 if (Type
!= AllocateAddress
) {
1288 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1290 Status
= EFI_OUT_OF_RESOURCES
;
1296 // Convert pages from FreeMemory to the requested type
1298 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1301 CoreReleaseMemoryLock ();
1303 if (!EFI_ERROR (Status
)) {
1311 Allocates pages from the memory map.
1313 @param Type The type of allocation to perform
1314 @param MemoryType The type of memory to turn the allocated pages
1316 @param NumberOfPages The number of pages to allocate
1317 @param Memory A pointer to receive the base allocated memory
1320 @return Status. On success, Memory is filled in with the base address allocated
1321 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1323 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1324 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1325 @retval EFI_SUCCESS Pages successfully allocated.
1331 IN EFI_ALLOCATE_TYPE Type
,
1332 IN EFI_MEMORY_TYPE MemoryType
,
1333 IN UINTN NumberOfPages
,
1334 OUT EFI_PHYSICAL_ADDRESS
*Memory
1339 Status
= CoreInternalAllocatePages (Type
, MemoryType
, NumberOfPages
, Memory
);
1340 if (!EFI_ERROR (Status
)) {
1342 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1343 MemoryProfileActionAllocatePages
,
1345 EFI_PAGES_TO_SIZE (NumberOfPages
),
1346 (VOID
*) (UINTN
) *Memory
,
1349 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType
);
1350 ApplyMemoryProtectionPolicy (EfiConventionalMemory
, MemoryType
, *Memory
,
1351 EFI_PAGES_TO_SIZE (NumberOfPages
));
1357 Frees previous allocated pages.
1359 @param Memory Base address of memory being freed
1360 @param NumberOfPages The number of pages to free
1361 @param MemoryType Pointer to memory type
1363 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1364 @retval EFI_INVALID_PARAMETER Address not aligned
1365 @return EFI_SUCCESS -Pages successfully freed.
1370 CoreInternalFreePages (
1371 IN EFI_PHYSICAL_ADDRESS Memory
,
1372 IN UINTN NumberOfPages
,
1373 OUT EFI_MEMORY_TYPE
*MemoryType OPTIONAL
1384 CoreAcquireMemoryLock ();
1387 // Find the entry that the covers the range
1390 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1391 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1392 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1396 if (Link
== &gMemoryMap
) {
1397 Status
= EFI_NOT_FOUND
;
1401 Alignment
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
1403 ASSERT (Entry
!= NULL
);
1404 if (Entry
->Type
== EfiACPIReclaimMemory
||
1405 Entry
->Type
== EfiACPIMemoryNVS
||
1406 Entry
->Type
== EfiRuntimeServicesCode
||
1407 Entry
->Type
== EfiRuntimeServicesData
) {
1409 Alignment
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
1413 if ((Memory
& (Alignment
- 1)) != 0) {
1414 Status
= EFI_INVALID_PARAMETER
;
1418 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1419 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1421 if (MemoryType
!= NULL
) {
1422 *MemoryType
= Entry
->Type
;
1425 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1427 if (EFI_ERROR (Status
)) {
1432 CoreReleaseMemoryLock ();
1437 Frees previous allocated pages.
1439 @param Memory Base address of memory being freed
1440 @param NumberOfPages The number of pages to free
1442 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1443 @retval EFI_INVALID_PARAMETER Address not aligned
1444 @return EFI_SUCCESS -Pages successfully freed.
1450 IN EFI_PHYSICAL_ADDRESS Memory
,
1451 IN UINTN NumberOfPages
1455 EFI_MEMORY_TYPE MemoryType
;
1457 Status
= CoreInternalFreePages (Memory
, NumberOfPages
, &MemoryType
);
1458 if (!EFI_ERROR (Status
)) {
1460 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1461 MemoryProfileActionFreePages
,
1463 EFI_PAGES_TO_SIZE (NumberOfPages
),
1464 (VOID
*) (UINTN
) Memory
,
1467 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType
);
1468 ApplyMemoryProtectionPolicy (MemoryType
, EfiConventionalMemory
, Memory
,
1469 EFI_PAGES_TO_SIZE (NumberOfPages
));
1475 This function checks to see if the last memory map descriptor in a memory map
1476 can be merged with any of the other memory map descriptors in a memorymap.
1477 Memory descriptors may be merged if they are adjacent and have the same type
1480 @param MemoryMap A pointer to the start of the memory map.
1481 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.
1482 @param DescriptorSize The size, in bytes, of an individual
1483 EFI_MEMORY_DESCRIPTOR.
1485 @return A pointer to the next available descriptor in MemoryMap
1488 EFI_MEMORY_DESCRIPTOR
*
1489 MergeMemoryMapDescriptor (
1490 IN EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1491 IN EFI_MEMORY_DESCRIPTOR
*MemoryMapDescriptor
,
1492 IN UINTN DescriptorSize
1496 // Traverse the array of descriptors in MemoryMap
1498 for (; MemoryMap
!= MemoryMapDescriptor
; MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, DescriptorSize
)) {
1500 // Check to see if the Type fields are identical.
1502 if (MemoryMap
->Type
!= MemoryMapDescriptor
->Type
) {
1507 // Check to see if the Attribute fields are identical.
1509 if (MemoryMap
->Attribute
!= MemoryMapDescriptor
->Attribute
) {
1514 // Check to see if MemoryMapDescriptor is immediately above MemoryMap
1516 if (MemoryMap
->PhysicalStart
+ EFI_PAGES_TO_SIZE ((UINTN
)MemoryMap
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1518 // Merge MemoryMapDescriptor into MemoryMap
1520 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1523 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1525 return MemoryMapDescriptor
;
1529 // Check to see if MemoryMapDescriptor is immediately below MemoryMap
1531 if (MemoryMap
->PhysicalStart
- EFI_PAGES_TO_SIZE ((UINTN
)MemoryMapDescriptor
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1533 // Merge MemoryMapDescriptor into MemoryMap
1535 MemoryMap
->PhysicalStart
= MemoryMapDescriptor
->PhysicalStart
;
1536 MemoryMap
->VirtualStart
= MemoryMapDescriptor
->VirtualStart
;
1537 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1540 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1542 return MemoryMapDescriptor
;
1547 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.
1549 // Return the slot immediately after MemoryMapDescriptor as the next available
1550 // slot in the MemoryMap array
1552 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor
, DescriptorSize
);
1556 This function returns a copy of the current memory map. The map is an array of
1557 memory descriptors, each of which describes a contiguous block of memory.
1559 @param MemoryMapSize A pointer to the size, in bytes, of the
1560 MemoryMap buffer. On input, this is the size of
1561 the buffer allocated by the caller. On output,
1562 it is the size of the buffer returned by the
1563 firmware if the buffer was large enough, or the
1564 size of the buffer needed to contain the map if
1565 the buffer was too small.
1566 @param MemoryMap A pointer to the buffer in which firmware places
1567 the current memory map.
1568 @param MapKey A pointer to the location in which firmware
1569 returns the key for the current memory map.
1570 @param DescriptorSize A pointer to the location in which firmware
1571 returns the size, in bytes, of an individual
1572 EFI_MEMORY_DESCRIPTOR.
1573 @param DescriptorVersion A pointer to the location in which firmware
1574 returns the version number associated with the
1575 EFI_MEMORY_DESCRIPTOR.
1577 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1579 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1580 buffer size needed to hold the memory map is
1581 returned in MemoryMapSize.
1582 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1588 IN OUT UINTN
*MemoryMapSize
,
1589 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1591 OUT UINTN
*DescriptorSize
,
1592 OUT UINT32
*DescriptorVersion
1598 UINTN NumberOfEntries
;
1601 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1602 EFI_GCD_MAP_ENTRY MergeGcdMapEntry
;
1603 EFI_MEMORY_TYPE Type
;
1604 EFI_MEMORY_DESCRIPTOR
*MemoryMapStart
;
1607 // Make sure the parameters are valid
1609 if (MemoryMapSize
== NULL
) {
1610 return EFI_INVALID_PARAMETER
;
1613 CoreAcquireGcdMemoryLock ();
1616 // Count the number of Reserved and runtime MMIO entries
1617 // And, count the number of Persistent entries.
1619 NumberOfEntries
= 0;
1620 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1621 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1622 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypePersistentMemory
) ||
1623 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1624 ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
1625 ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
))) {
1630 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1633 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1634 // prevent people from having pointer math bugs in their code.
1635 // now you have to use *DescriptorSize to make things work.
1637 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1639 if (DescriptorSize
!= NULL
) {
1640 *DescriptorSize
= Size
;
1643 if (DescriptorVersion
!= NULL
) {
1644 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1647 CoreAcquireMemoryLock ();
1650 // Compute the buffer size needed to fit the entire map
1652 BufferSize
= Size
* NumberOfEntries
;
1653 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1657 if (*MemoryMapSize
< BufferSize
) {
1658 Status
= EFI_BUFFER_TOO_SMALL
;
1662 if (MemoryMap
== NULL
) {
1663 Status
= EFI_INVALID_PARAMETER
;
1670 ZeroMem (MemoryMap
, BufferSize
);
1671 MemoryMapStart
= MemoryMap
;
1672 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1673 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1674 ASSERT (Entry
->VirtualStart
== 0);
1677 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1679 MemoryMap
->Type
= Entry
->Type
;
1680 MemoryMap
->PhysicalStart
= Entry
->Start
;
1681 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1682 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1684 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1685 // memory type bin and needs to be converted to the same memory type as the rest of the
1686 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1687 // improves the chances for a successful S4 resume in the presence of minor page allocation
1688 // differences across reboots.
1690 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1691 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1692 if (mMemoryTypeStatistics
[Type
].Special
&&
1693 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1694 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1695 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1696 MemoryMap
->Type
= Type
;
1700 MemoryMap
->Attribute
= Entry
->Attribute
;
1701 if (MemoryMap
->Type
< EfiMaxMemoryType
) {
1702 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1703 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1708 // Check to see if the new Memory Map Descriptor can be merged with an
1709 // existing descriptor if they are adjacent and have the same attributes
1711 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1715 ZeroMem (&MergeGcdMapEntry
, sizeof (MergeGcdMapEntry
));
1717 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; ; Link
= Link
->ForwardLink
) {
1718 if (Link
!= &mGcdMemorySpaceMap
) {
1720 // Merge adjacent same type and attribute GCD memory range
1722 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1724 if ((MergeGcdMapEntry
.Capabilities
== GcdMapEntry
->Capabilities
) &&
1725 (MergeGcdMapEntry
.Attributes
== GcdMapEntry
->Attributes
) &&
1726 (MergeGcdMapEntry
.GcdMemoryType
== GcdMapEntry
->GcdMemoryType
) &&
1727 (MergeGcdMapEntry
.GcdIoType
== GcdMapEntry
->GcdIoType
)) {
1728 MergeGcdMapEntry
.EndAddress
= GcdMapEntry
->EndAddress
;
1733 if ((MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1734 ((MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
1735 ((MergeGcdMapEntry
.Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
))) {
1737 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1738 // it will be recorded as page PhysicalStart and NumberOfPages.
1740 ASSERT ((MergeGcdMapEntry
.BaseAddress
& EFI_PAGE_MASK
) == 0);
1741 ASSERT (((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1) & EFI_PAGE_MASK
) == 0);
1744 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries
1746 MemoryMap
->PhysicalStart
= MergeGcdMapEntry
.BaseAddress
;
1747 MemoryMap
->VirtualStart
= 0;
1748 MemoryMap
->NumberOfPages
= RShiftU64 ((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1749 MemoryMap
->Attribute
= (MergeGcdMapEntry
.Attributes
& ~EFI_MEMORY_PORT_IO
) |
1750 (MergeGcdMapEntry
.Capabilities
& (EFI_MEMORY_RP
| EFI_MEMORY_WP
| EFI_MEMORY_XP
| EFI_MEMORY_RO
|
1751 EFI_MEMORY_UC
| EFI_MEMORY_UCE
| EFI_MEMORY_WC
| EFI_MEMORY_WT
| EFI_MEMORY_WB
));
1753 if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1754 MemoryMap
->Type
= EfiReservedMemoryType
;
1755 } else if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1756 if ((MergeGcdMapEntry
.Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1757 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1759 MemoryMap
->Type
= EfiMemoryMappedIO
;
1764 // Check to see if the new Memory Map Descriptor can be merged with an
1765 // existing descriptor if they are adjacent and have the same attributes
1767 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1770 if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypePersistentMemory
) {
1772 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1773 // it will be recorded as page PhysicalStart and NumberOfPages.
1775 ASSERT ((MergeGcdMapEntry
.BaseAddress
& EFI_PAGE_MASK
) == 0);
1776 ASSERT (((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1) & EFI_PAGE_MASK
) == 0);
1779 // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries
1781 MemoryMap
->PhysicalStart
= MergeGcdMapEntry
.BaseAddress
;
1782 MemoryMap
->VirtualStart
= 0;
1783 MemoryMap
->NumberOfPages
= RShiftU64 ((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1784 MemoryMap
->Attribute
= MergeGcdMapEntry
.Attributes
| EFI_MEMORY_NV
|
1785 (MergeGcdMapEntry
.Capabilities
& (EFI_MEMORY_RP
| EFI_MEMORY_WP
| EFI_MEMORY_XP
| EFI_MEMORY_RO
|
1786 EFI_MEMORY_UC
| EFI_MEMORY_UCE
| EFI_MEMORY_WC
| EFI_MEMORY_WT
| EFI_MEMORY_WB
));
1787 MemoryMap
->Type
= EfiPersistentMemory
;
1790 // Check to see if the new Memory Map Descriptor can be merged with an
1791 // existing descriptor if they are adjacent and have the same attributes
1793 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1795 if (Link
== &mGcdMemorySpaceMap
) {
1797 // break loop when arrive at head.
1801 if (GcdMapEntry
!= NULL
) {
1803 // Copy new GCD map entry for the following GCD range merge
1805 CopyMem (&MergeGcdMapEntry
, GcdMapEntry
, sizeof (MergeGcdMapEntry
));
1810 // Compute the size of the buffer actually used after all memory map descriptor merge operations
1812 BufferSize
= ((UINT8
*)MemoryMap
- (UINT8
*)MemoryMapStart
);
1814 Status
= EFI_SUCCESS
;
1818 // Update the map key finally
1820 if (MapKey
!= NULL
) {
1821 *MapKey
= mMemoryMapKey
;
1824 CoreReleaseMemoryLock ();
1826 CoreReleaseGcdMemoryLock ();
1828 *MemoryMapSize
= BufferSize
;
1835 Internal function. Used by the pool functions to allocate pages
1836 to back pool allocation requests.
1838 @param PoolType The type of memory for the new pool pages
1839 @param NumberOfPages No of pages to allocate
1840 @param Alignment Bits to align.
1842 @return The allocated memory, or NULL
1846 CoreAllocatePoolPages (
1847 IN EFI_MEMORY_TYPE PoolType
,
1848 IN UINTN NumberOfPages
,
1855 // Find the pages to convert
1857 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1860 // Convert it to boot services data
1863 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1865 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1868 return (VOID
*)(UINTN
) Start
;
1873 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1875 @param Memory The base address to free
1876 @param NumberOfPages The number of pages to free
1881 IN EFI_PHYSICAL_ADDRESS Memory
,
1882 IN UINTN NumberOfPages
1885 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1891 Make sure the memory map is following all the construction rules,
1892 it is the last time to check memory map error before exit boot services.
1894 @param MapKey Memory map key
1896 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1898 @retval EFI_SUCCESS Valid memory map.
1902 CoreTerminateMemoryMap (
1910 Status
= EFI_SUCCESS
;
1912 CoreAcquireMemoryLock ();
1914 if (MapKey
== mMemoryMapKey
) {
1917 // Make sure the memory map is following all the construction rules
1918 // This is the last chance we will be able to display any messages on
1919 // the console devices.
1922 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1923 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1924 if (Entry
->Type
< EfiMaxMemoryType
) {
1925 if (mMemoryTypeStatistics
[Entry
->Type
].Runtime
) {
1926 ASSERT (Entry
->Type
!= EfiACPIReclaimMemory
);
1927 ASSERT (Entry
->Type
!= EfiACPIMemoryNVS
);
1928 if ((Entry
->Start
& (RUNTIME_PAGE_ALLOCATION_GRANULARITY
- 1)) != 0) {
1929 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1930 Status
= EFI_INVALID_PARAMETER
;
1933 if (((Entry
->End
+ 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY
- 1)) != 0) {
1934 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1935 Status
= EFI_INVALID_PARAMETER
;
1943 // The map key they gave us matches what we expect. Fall through and
1944 // return success. In an ideal world we would clear out all of
1945 // EfiBootServicesCode and EfiBootServicesData. However this function
1946 // is not the last one called by ExitBootServices(), so we have to
1947 // preserve the memory contents.
1950 Status
= EFI_INVALID_PARAMETER
;
1954 CoreReleaseMemoryLock ();