2 UEFI Memory page management functions.
4 Copyright (c) 2007 - 2017, 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 // If we are converting the type of the range from EfiConventionalMemory to
759 // another type, we have to ensure that the entire range is covered by a
762 if (ChangingType
&& (NewType
!= EfiConventionalMemory
)) {
763 if (Entry
->End
< End
) {
764 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: range %lx - %lx covers multiple entries\n", Start
, End
));
765 return EFI_NOT_FOUND
;
769 // Convert range to the end, or to the end of the descriptor
770 // if that's all we've got
774 ASSERT (Entry
!= NULL
);
775 if (Entry
->End
< End
) {
776 RangeEnd
= Entry
->End
;
780 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to type %d\n", Start
, RangeEnd
, NewType
));
782 if (ChangingAttributes
) {
783 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to attr %lx\n", Start
, RangeEnd
, NewAttributes
));
788 // Debug code - verify conversion is allowed
790 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
791 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types, "));
792 if (Entry
->Type
== EfiConventionalMemory
) {
793 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "the pages to free have been freed\n"));
795 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "the pages to allocate have been allocated\n"));
797 return EFI_NOT_FOUND
;
801 // Update counters for the number of pages allocated to each memory type
803 if ((UINT32
)Entry
->Type
< EfiMaxMemoryType
) {
804 if ((Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) ||
805 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
806 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
807 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
809 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
814 if ((UINT32
)NewType
< EfiMaxMemoryType
) {
815 if ((Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) ||
816 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
817 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
818 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
> gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
819 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
826 // Pull range out of descriptor
828 if (Entry
->Start
== Start
) {
833 Entry
->Start
= RangeEnd
+ 1;
835 } else if (Entry
->End
== RangeEnd
) {
840 Entry
->End
= Start
- 1;
845 // Pull it out of the center, clip current
851 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
852 mMapStack
[mMapDepth
].FromPages
= FALSE
;
853 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
854 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
855 mMapStack
[mMapDepth
].End
= Entry
->End
;
858 // Inherit Attribute from the Memory Descriptor that is being clipped
860 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
862 Entry
->End
= Start
- 1;
863 ASSERT (Entry
->Start
< Entry
->End
);
865 Entry
= &mMapStack
[mMapDepth
];
866 InsertTailList (&gMemoryMap
, &Entry
->Link
);
869 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
873 // The new range inherits the same Attribute as the Entry
874 // it is being cut out of unless attributes are being changed
877 Attribute
= Entry
->Attribute
;
880 Attribute
= NewAttributes
;
881 MemType
= Entry
->Type
;
885 // If the descriptor is empty, then remove it from the map
887 if (Entry
->Start
== Entry
->End
+ 1) {
888 RemoveMemoryMapEntry (Entry
);
893 // Add our new range in
895 CoreAddRange (MemType
, Start
, RangeEnd
, Attribute
);
896 if (ChangingType
&& (MemType
== EfiConventionalMemory
)) {
898 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
899 // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees
900 // that the page starting at address 0 is always filled with zeros.
903 if (RangeEnd
> EFI_PAGE_SIZE
) {
904 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) EFI_PAGE_SIZE
, (UINTN
) (RangeEnd
- EFI_PAGE_SIZE
+ 1));
907 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) Start
, (UINTN
) (RangeEnd
- Start
+ 1));
912 // Move any map descriptor stack to general pool
914 CoreFreeMemoryMapStack ();
917 // Bump the starting address, and convert the next range
919 Start
= RangeEnd
+ 1;
923 // Converted the whole range, done
931 Internal function. Converts a memory range to the specified type.
932 The range must exist in the memory map.
934 @param Start The first address of the range Must be page
936 @param NumberOfPages The number of pages to convert
937 @param NewType The new type for the memory range
939 @retval EFI_INVALID_PARAMETER Invalid parameter
940 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
941 range or convertion not allowed.
942 @retval EFI_SUCCESS Successfully converts the memory range to the
949 IN UINT64 NumberOfPages
,
950 IN EFI_MEMORY_TYPE NewType
953 return CoreConvertPagesEx(Start
, NumberOfPages
, TRUE
, NewType
, FALSE
, 0);
958 Internal function. Converts a memory range to use new attributes.
960 @param Start The first address of the range Must be page
962 @param NumberOfPages The number of pages to convert
963 @param NewAttributes The new attributes value for the range.
967 CoreUpdateMemoryAttributes (
968 IN EFI_PHYSICAL_ADDRESS Start
,
969 IN UINT64 NumberOfPages
,
970 IN UINT64 NewAttributes
973 CoreAcquireMemoryLock ();
976 // Update the attributes to the new value
978 CoreConvertPagesEx(Start
, NumberOfPages
, FALSE
, (EFI_MEMORY_TYPE
)0, TRUE
, NewAttributes
);
980 CoreReleaseMemoryLock ();
985 Internal function. Finds a consecutive free page range below
986 the requested address.
988 @param MaxAddress The address that the range must be below
989 @param MinAddress The address that the range must be above
990 @param NumberOfPages Number of pages needed
991 @param NewType The type of memory the range is going to be
993 @param Alignment Bits to align with
995 @return The base address of the range, or 0 if the range was not found
1000 IN UINT64 MaxAddress
,
1001 IN UINT64 MinAddress
,
1002 IN UINT64 NumberOfPages
,
1003 IN EFI_MEMORY_TYPE NewType
,
1007 UINT64 NumberOfBytes
;
1011 UINT64 DescNumberOfBytes
;
1015 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
1019 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
1022 // If MaxAddress is not aligned to the end of a page
1026 // Change MaxAddress to be 1 page lower
1028 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
1031 // Set MaxAddress to a page boundary
1033 MaxAddress
&= ~(UINT64
)EFI_PAGE_MASK
;
1036 // Set MaxAddress to end of the page
1038 MaxAddress
|= EFI_PAGE_MASK
;
1041 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1044 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1045 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1048 // If it's not a free entry, don't bother with it
1050 if (Entry
->Type
!= EfiConventionalMemory
) {
1054 DescStart
= Entry
->Start
;
1055 DescEnd
= Entry
->End
;
1058 // If desc is past max allowed address or below min allowed address, skip it
1060 if ((DescStart
>= MaxAddress
) || (DescEnd
< MinAddress
)) {
1065 // If desc ends past max allowed address, clip the end
1067 if (DescEnd
>= MaxAddress
) {
1068 DescEnd
= MaxAddress
;
1071 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
1073 // Skip if DescEnd is less than DescStart after alignment clipping
1074 if (DescEnd
< DescStart
) {
1079 // Compute the number of bytes we can used from this
1080 // descriptor, and see it's enough to satisfy the request
1082 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
1084 if (DescNumberOfBytes
>= NumberOfBytes
) {
1086 // If the start of the allocated range is below the min address allowed, skip it
1088 if ((DescEnd
- NumberOfBytes
+ 1) < MinAddress
) {
1093 // If this is the best match so far remember it
1095 if (DescEnd
> Target
) {
1102 // If this is a grow down, adjust target to be the allocation base
1104 Target
-= NumberOfBytes
- 1;
1107 // If we didn't find a match, return 0
1109 if ((Target
& EFI_PAGE_MASK
) != 0) {
1118 Internal function. Finds a consecutive free page range below
1119 the requested address
1121 @param MaxAddress The address that the range must be below
1122 @param NoPages Number of pages needed
1123 @param NewType The type of memory the range is going to be
1125 @param Alignment Bits to align with
1127 @return The base address of the range, or 0 if the range was not found.
1132 IN UINT64 MaxAddress
,
1134 IN EFI_MEMORY_TYPE NewType
,
1141 // Attempt to find free pages in the preferred bin based on the requested memory type
1143 if ((UINT32
)NewType
< EfiMaxMemoryType
&& MaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1144 Start
= CoreFindFreePagesI (
1145 mMemoryTypeStatistics
[NewType
].MaximumAddress
,
1146 mMemoryTypeStatistics
[NewType
].BaseAddress
,
1157 // Attempt to find free pages in the default allocation bin
1159 if (MaxAddress
>= mDefaultMaximumAddress
) {
1160 Start
= CoreFindFreePagesI (mDefaultMaximumAddress
, 0, NoPages
, NewType
, Alignment
);
1162 if (Start
< mDefaultBaseAddress
) {
1163 mDefaultBaseAddress
= Start
;
1170 // The allocation did not succeed in any of the prefered bins even after
1171 // promoting resources. Attempt to find free pages anywhere is the requested
1172 // address range. If this allocation fails, then there are not enough
1173 // resources anywhere to satisfy the request.
1175 Start
= CoreFindFreePagesI (MaxAddress
, 0, NoPages
, NewType
, Alignment
);
1181 // If allocations from the preferred bins fail, then attempt to promote memory resources.
1183 if (!PromoteMemoryResource ()) {
1188 // If any memory resources were promoted, then re-attempt the allocation
1190 return FindFreePages (MaxAddress
, NoPages
, NewType
, Alignment
);
1195 Allocates pages from the memory map.
1197 @param Type The type of allocation to perform
1198 @param MemoryType The type of memory to turn the allocated pages
1200 @param NumberOfPages The number of pages to allocate
1201 @param Memory A pointer to receive the base allocated memory
1204 @return Status. On success, Memory is filled in with the base address allocated
1205 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1207 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1208 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1209 @retval EFI_SUCCESS Pages successfully allocated.
1214 CoreInternalAllocatePages (
1215 IN EFI_ALLOCATE_TYPE Type
,
1216 IN EFI_MEMORY_TYPE MemoryType
,
1217 IN UINTN NumberOfPages
,
1218 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1223 UINT64 NumberOfBytes
;
1228 if ((UINT32
)Type
>= MaxAllocateType
) {
1229 return EFI_INVALID_PARAMETER
;
1232 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
< MEMORY_TYPE_OEM_RESERVED_MIN
) ||
1233 (MemoryType
== EfiConventionalMemory
) || (MemoryType
== EfiPersistentMemory
)) {
1234 return EFI_INVALID_PARAMETER
;
1237 if (Memory
== NULL
) {
1238 return EFI_INVALID_PARAMETER
;
1241 Alignment
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
1243 if (MemoryType
== EfiACPIReclaimMemory
||
1244 MemoryType
== EfiACPIMemoryNVS
||
1245 MemoryType
== EfiRuntimeServicesCode
||
1246 MemoryType
== EfiRuntimeServicesData
) {
1248 Alignment
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
1251 if (Type
== AllocateAddress
) {
1252 if ((*Memory
& (Alignment
- 1)) != 0) {
1253 return EFI_NOT_FOUND
;
1257 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1258 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1261 // If this is for below a particular address, then
1266 // The max address is the max natively addressable address for the processor
1268 MaxAddress
= MAX_ADDRESS
;
1271 // Check for Type AllocateAddress,
1272 // if NumberOfPages is 0 or
1273 // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or
1274 // if (Start + NumberOfBytes) rolls over 0 or
1275 // if Start is above MAX_ADDRESS or
1276 // if End is above MAX_ADDRESS,
1277 // return EFI_NOT_FOUND.
1279 if (Type
== AllocateAddress
) {
1280 if ((NumberOfPages
== 0) ||
1281 (NumberOfPages
> RShiftU64 (MaxAddress
, EFI_PAGE_SHIFT
))) {
1282 return EFI_NOT_FOUND
;
1284 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1285 End
= Start
+ NumberOfBytes
- 1;
1287 if ((Start
>= End
) ||
1288 (Start
> MaxAddress
) ||
1289 (End
> MaxAddress
)) {
1290 return EFI_NOT_FOUND
;
1294 if (Type
== AllocateMaxAddress
) {
1298 CoreAcquireMemoryLock ();
1301 // If not a specific address, then find an address to allocate
1303 if (Type
!= AllocateAddress
) {
1304 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1306 Status
= EFI_OUT_OF_RESOURCES
;
1312 // Convert pages from FreeMemory to the requested type
1314 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1317 CoreReleaseMemoryLock ();
1319 if (!EFI_ERROR (Status
)) {
1327 Allocates pages from the memory map.
1329 @param Type The type of allocation to perform
1330 @param MemoryType The type of memory to turn the allocated pages
1332 @param NumberOfPages The number of pages to allocate
1333 @param Memory A pointer to receive the base allocated memory
1336 @return Status. On success, Memory is filled in with the base address allocated
1337 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1339 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1340 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1341 @retval EFI_SUCCESS Pages successfully allocated.
1347 IN EFI_ALLOCATE_TYPE Type
,
1348 IN EFI_MEMORY_TYPE MemoryType
,
1349 IN UINTN NumberOfPages
,
1350 OUT EFI_PHYSICAL_ADDRESS
*Memory
1355 Status
= CoreInternalAllocatePages (Type
, MemoryType
, NumberOfPages
, Memory
);
1356 if (!EFI_ERROR (Status
)) {
1358 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1359 MemoryProfileActionAllocatePages
,
1361 EFI_PAGES_TO_SIZE (NumberOfPages
),
1362 (VOID
*) (UINTN
) *Memory
,
1365 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType
);
1366 ApplyMemoryProtectionPolicy (EfiConventionalMemory
, MemoryType
, *Memory
,
1367 EFI_PAGES_TO_SIZE (NumberOfPages
));
1373 Frees previous allocated pages.
1375 @param Memory Base address of memory being freed
1376 @param NumberOfPages The number of pages to free
1377 @param MemoryType Pointer to memory type
1379 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1380 @retval EFI_INVALID_PARAMETER Address not aligned
1381 @return EFI_SUCCESS -Pages successfully freed.
1386 CoreInternalFreePages (
1387 IN EFI_PHYSICAL_ADDRESS Memory
,
1388 IN UINTN NumberOfPages
,
1389 OUT EFI_MEMORY_TYPE
*MemoryType OPTIONAL
1400 CoreAcquireMemoryLock ();
1403 // Find the entry that the covers the range
1406 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1407 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1408 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1412 if (Link
== &gMemoryMap
) {
1413 Status
= EFI_NOT_FOUND
;
1417 Alignment
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
1419 ASSERT (Entry
!= NULL
);
1420 if (Entry
->Type
== EfiACPIReclaimMemory
||
1421 Entry
->Type
== EfiACPIMemoryNVS
||
1422 Entry
->Type
== EfiRuntimeServicesCode
||
1423 Entry
->Type
== EfiRuntimeServicesData
) {
1425 Alignment
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
1429 if ((Memory
& (Alignment
- 1)) != 0) {
1430 Status
= EFI_INVALID_PARAMETER
;
1434 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1435 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1437 if (MemoryType
!= NULL
) {
1438 *MemoryType
= Entry
->Type
;
1441 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1443 if (EFI_ERROR (Status
)) {
1448 CoreReleaseMemoryLock ();
1453 Frees previous allocated pages.
1455 @param Memory Base address of memory being freed
1456 @param NumberOfPages The number of pages to free
1458 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1459 @retval EFI_INVALID_PARAMETER Address not aligned
1460 @return EFI_SUCCESS -Pages successfully freed.
1466 IN EFI_PHYSICAL_ADDRESS Memory
,
1467 IN UINTN NumberOfPages
1471 EFI_MEMORY_TYPE MemoryType
;
1473 Status
= CoreInternalFreePages (Memory
, NumberOfPages
, &MemoryType
);
1474 if (!EFI_ERROR (Status
)) {
1476 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1477 MemoryProfileActionFreePages
,
1479 EFI_PAGES_TO_SIZE (NumberOfPages
),
1480 (VOID
*) (UINTN
) Memory
,
1483 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType
);
1484 ApplyMemoryProtectionPolicy (MemoryType
, EfiConventionalMemory
, Memory
,
1485 EFI_PAGES_TO_SIZE (NumberOfPages
));
1491 This function checks to see if the last memory map descriptor in a memory map
1492 can be merged with any of the other memory map descriptors in a memorymap.
1493 Memory descriptors may be merged if they are adjacent and have the same type
1496 @param MemoryMap A pointer to the start of the memory map.
1497 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.
1498 @param DescriptorSize The size, in bytes, of an individual
1499 EFI_MEMORY_DESCRIPTOR.
1501 @return A pointer to the next available descriptor in MemoryMap
1504 EFI_MEMORY_DESCRIPTOR
*
1505 MergeMemoryMapDescriptor (
1506 IN EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1507 IN EFI_MEMORY_DESCRIPTOR
*MemoryMapDescriptor
,
1508 IN UINTN DescriptorSize
1512 // Traverse the array of descriptors in MemoryMap
1514 for (; MemoryMap
!= MemoryMapDescriptor
; MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, DescriptorSize
)) {
1516 // Check to see if the Type fields are identical.
1518 if (MemoryMap
->Type
!= MemoryMapDescriptor
->Type
) {
1523 // Check to see if the Attribute fields are identical.
1525 if (MemoryMap
->Attribute
!= MemoryMapDescriptor
->Attribute
) {
1530 // Check to see if MemoryMapDescriptor is immediately above MemoryMap
1532 if (MemoryMap
->PhysicalStart
+ EFI_PAGES_TO_SIZE ((UINTN
)MemoryMap
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1534 // Merge MemoryMapDescriptor into MemoryMap
1536 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1539 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1541 return MemoryMapDescriptor
;
1545 // Check to see if MemoryMapDescriptor is immediately below MemoryMap
1547 if (MemoryMap
->PhysicalStart
- EFI_PAGES_TO_SIZE ((UINTN
)MemoryMapDescriptor
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1549 // Merge MemoryMapDescriptor into MemoryMap
1551 MemoryMap
->PhysicalStart
= MemoryMapDescriptor
->PhysicalStart
;
1552 MemoryMap
->VirtualStart
= MemoryMapDescriptor
->VirtualStart
;
1553 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1556 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1558 return MemoryMapDescriptor
;
1563 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.
1565 // Return the slot immediately after MemoryMapDescriptor as the next available
1566 // slot in the MemoryMap array
1568 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor
, DescriptorSize
);
1572 This function returns a copy of the current memory map. The map is an array of
1573 memory descriptors, each of which describes a contiguous block of memory.
1575 @param MemoryMapSize A pointer to the size, in bytes, of the
1576 MemoryMap buffer. On input, this is the size of
1577 the buffer allocated by the caller. On output,
1578 it is the size of the buffer returned by the
1579 firmware if the buffer was large enough, or the
1580 size of the buffer needed to contain the map if
1581 the buffer was too small.
1582 @param MemoryMap A pointer to the buffer in which firmware places
1583 the current memory map.
1584 @param MapKey A pointer to the location in which firmware
1585 returns the key for the current memory map.
1586 @param DescriptorSize A pointer to the location in which firmware
1587 returns the size, in bytes, of an individual
1588 EFI_MEMORY_DESCRIPTOR.
1589 @param DescriptorVersion A pointer to the location in which firmware
1590 returns the version number associated with the
1591 EFI_MEMORY_DESCRIPTOR.
1593 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1595 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1596 buffer size needed to hold the memory map is
1597 returned in MemoryMapSize.
1598 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1604 IN OUT UINTN
*MemoryMapSize
,
1605 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1607 OUT UINTN
*DescriptorSize
,
1608 OUT UINT32
*DescriptorVersion
1614 UINTN NumberOfEntries
;
1617 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1618 EFI_GCD_MAP_ENTRY MergeGcdMapEntry
;
1619 EFI_MEMORY_TYPE Type
;
1620 EFI_MEMORY_DESCRIPTOR
*MemoryMapStart
;
1623 // Make sure the parameters are valid
1625 if (MemoryMapSize
== NULL
) {
1626 return EFI_INVALID_PARAMETER
;
1629 CoreAcquireGcdMemoryLock ();
1632 // Count the number of Reserved and runtime MMIO entries
1633 // And, count the number of Persistent entries.
1635 NumberOfEntries
= 0;
1636 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1637 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1638 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypePersistentMemory
) ||
1639 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1640 ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
1641 ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
))) {
1646 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1649 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1650 // prevent people from having pointer math bugs in their code.
1651 // now you have to use *DescriptorSize to make things work.
1653 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1655 if (DescriptorSize
!= NULL
) {
1656 *DescriptorSize
= Size
;
1659 if (DescriptorVersion
!= NULL
) {
1660 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1663 CoreAcquireMemoryLock ();
1666 // Compute the buffer size needed to fit the entire map
1668 BufferSize
= Size
* NumberOfEntries
;
1669 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1673 if (*MemoryMapSize
< BufferSize
) {
1674 Status
= EFI_BUFFER_TOO_SMALL
;
1678 if (MemoryMap
== NULL
) {
1679 Status
= EFI_INVALID_PARAMETER
;
1686 ZeroMem (MemoryMap
, BufferSize
);
1687 MemoryMapStart
= MemoryMap
;
1688 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1689 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1690 ASSERT (Entry
->VirtualStart
== 0);
1693 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1695 MemoryMap
->Type
= Entry
->Type
;
1696 MemoryMap
->PhysicalStart
= Entry
->Start
;
1697 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1698 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1700 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1701 // memory type bin and needs to be converted to the same memory type as the rest of the
1702 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1703 // improves the chances for a successful S4 resume in the presence of minor page allocation
1704 // differences across reboots.
1706 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1707 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1708 if (mMemoryTypeStatistics
[Type
].Special
&&
1709 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1710 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1711 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1712 MemoryMap
->Type
= Type
;
1716 MemoryMap
->Attribute
= Entry
->Attribute
;
1717 if (MemoryMap
->Type
< EfiMaxMemoryType
) {
1718 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1719 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1724 // Check to see if the new Memory Map Descriptor can be merged with an
1725 // existing descriptor if they are adjacent and have the same attributes
1727 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1731 ZeroMem (&MergeGcdMapEntry
, sizeof (MergeGcdMapEntry
));
1733 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; ; Link
= Link
->ForwardLink
) {
1734 if (Link
!= &mGcdMemorySpaceMap
) {
1736 // Merge adjacent same type and attribute GCD memory range
1738 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1740 if ((MergeGcdMapEntry
.Capabilities
== GcdMapEntry
->Capabilities
) &&
1741 (MergeGcdMapEntry
.Attributes
== GcdMapEntry
->Attributes
) &&
1742 (MergeGcdMapEntry
.GcdMemoryType
== GcdMapEntry
->GcdMemoryType
) &&
1743 (MergeGcdMapEntry
.GcdIoType
== GcdMapEntry
->GcdIoType
)) {
1744 MergeGcdMapEntry
.EndAddress
= GcdMapEntry
->EndAddress
;
1749 if ((MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1750 ((MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
1751 ((MergeGcdMapEntry
.Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
))) {
1753 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1754 // it will be recorded as page PhysicalStart and NumberOfPages.
1756 ASSERT ((MergeGcdMapEntry
.BaseAddress
& EFI_PAGE_MASK
) == 0);
1757 ASSERT (((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1) & EFI_PAGE_MASK
) == 0);
1760 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries
1762 MemoryMap
->PhysicalStart
= MergeGcdMapEntry
.BaseAddress
;
1763 MemoryMap
->VirtualStart
= 0;
1764 MemoryMap
->NumberOfPages
= RShiftU64 ((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1765 MemoryMap
->Attribute
= (MergeGcdMapEntry
.Attributes
& ~EFI_MEMORY_PORT_IO
) |
1766 (MergeGcdMapEntry
.Capabilities
& (EFI_MEMORY_RP
| EFI_MEMORY_WP
| EFI_MEMORY_XP
| EFI_MEMORY_RO
|
1767 EFI_MEMORY_UC
| EFI_MEMORY_UCE
| EFI_MEMORY_WC
| EFI_MEMORY_WT
| EFI_MEMORY_WB
));
1769 if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1770 MemoryMap
->Type
= EfiReservedMemoryType
;
1771 } else if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1772 if ((MergeGcdMapEntry
.Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1773 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1775 MemoryMap
->Type
= EfiMemoryMappedIO
;
1780 // Check to see if the new Memory Map Descriptor can be merged with an
1781 // existing descriptor if they are adjacent and have the same attributes
1783 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1786 if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypePersistentMemory
) {
1788 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1789 // it will be recorded as page PhysicalStart and NumberOfPages.
1791 ASSERT ((MergeGcdMapEntry
.BaseAddress
& EFI_PAGE_MASK
) == 0);
1792 ASSERT (((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1) & EFI_PAGE_MASK
) == 0);
1795 // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries
1797 MemoryMap
->PhysicalStart
= MergeGcdMapEntry
.BaseAddress
;
1798 MemoryMap
->VirtualStart
= 0;
1799 MemoryMap
->NumberOfPages
= RShiftU64 ((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1800 MemoryMap
->Attribute
= MergeGcdMapEntry
.Attributes
| EFI_MEMORY_NV
|
1801 (MergeGcdMapEntry
.Capabilities
& (EFI_MEMORY_RP
| EFI_MEMORY_WP
| EFI_MEMORY_XP
| EFI_MEMORY_RO
|
1802 EFI_MEMORY_UC
| EFI_MEMORY_UCE
| EFI_MEMORY_WC
| EFI_MEMORY_WT
| EFI_MEMORY_WB
));
1803 MemoryMap
->Type
= EfiPersistentMemory
;
1806 // Check to see if the new Memory Map Descriptor can be merged with an
1807 // existing descriptor if they are adjacent and have the same attributes
1809 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1811 if (Link
== &mGcdMemorySpaceMap
) {
1813 // break loop when arrive at head.
1817 if (GcdMapEntry
!= NULL
) {
1819 // Copy new GCD map entry for the following GCD range merge
1821 CopyMem (&MergeGcdMapEntry
, GcdMapEntry
, sizeof (MergeGcdMapEntry
));
1826 // Compute the size of the buffer actually used after all memory map descriptor merge operations
1828 BufferSize
= ((UINT8
*)MemoryMap
- (UINT8
*)MemoryMapStart
);
1830 Status
= EFI_SUCCESS
;
1834 // Update the map key finally
1836 if (MapKey
!= NULL
) {
1837 *MapKey
= mMemoryMapKey
;
1840 CoreReleaseMemoryLock ();
1842 CoreReleaseGcdMemoryLock ();
1844 *MemoryMapSize
= BufferSize
;
1851 Internal function. Used by the pool functions to allocate pages
1852 to back pool allocation requests.
1854 @param PoolType The type of memory for the new pool pages
1855 @param NumberOfPages No of pages to allocate
1856 @param Alignment Bits to align.
1858 @return The allocated memory, or NULL
1862 CoreAllocatePoolPages (
1863 IN EFI_MEMORY_TYPE PoolType
,
1864 IN UINTN NumberOfPages
,
1871 // Find the pages to convert
1873 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1876 // Convert it to boot services data
1879 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1881 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1884 return (VOID
*)(UINTN
) Start
;
1889 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1891 @param Memory The base address to free
1892 @param NumberOfPages The number of pages to free
1897 IN EFI_PHYSICAL_ADDRESS Memory
,
1898 IN UINTN NumberOfPages
1901 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1907 Make sure the memory map is following all the construction rules,
1908 it is the last time to check memory map error before exit boot services.
1910 @param MapKey Memory map key
1912 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1914 @retval EFI_SUCCESS Valid memory map.
1918 CoreTerminateMemoryMap (
1926 Status
= EFI_SUCCESS
;
1928 CoreAcquireMemoryLock ();
1930 if (MapKey
== mMemoryMapKey
) {
1933 // Make sure the memory map is following all the construction rules
1934 // This is the last chance we will be able to display any messages on
1935 // the console devices.
1938 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1939 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1940 if (Entry
->Type
< EfiMaxMemoryType
) {
1941 if (mMemoryTypeStatistics
[Entry
->Type
].Runtime
) {
1942 ASSERT (Entry
->Type
!= EfiACPIReclaimMemory
);
1943 ASSERT (Entry
->Type
!= EfiACPIMemoryNVS
);
1944 if ((Entry
->Start
& (RUNTIME_PAGE_ALLOCATION_GRANULARITY
- 1)) != 0) {
1945 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1946 Status
= EFI_INVALID_PARAMETER
;
1949 if (((Entry
->End
+ 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY
- 1)) != 0) {
1950 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1951 Status
= EFI_INVALID_PARAMETER
;
1959 // The map key they gave us matches what we expect. Fall through and
1960 // return success. In an ideal world we would clear out all of
1961 // EfiBootServicesCode and EfiBootServicesData. However this function
1962 // is not the last one called by ExitBootServices(), so we have to
1963 // preserve the memory contents.
1966 Status
= EFI_INVALID_PARAMETER
;
1970 CoreReleaseMemoryLock ();