2 UEFI Memory page management functions.
4 Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)
21 // Entry for tracking the memory regions for each memory type to coalesce similar memory types
24 EFI_PHYSICAL_ADDRESS BaseAddress
;
25 EFI_PHYSICAL_ADDRESS MaximumAddress
;
26 UINT64 CurrentNumberOfPages
;
28 UINTN InformationIndex
;
31 } EFI_MEMORY_TYPE_STATISTICS
;
34 // MemoryMap - The current memory map
36 UINTN mMemoryMapKey
= 0;
38 #define MAX_MAP_DEPTH 6
41 /// mMapDepth - depth of new descriptor stack
45 /// mMapStack - space to use as temp storage to build new map descriptors
47 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
48 UINTN mFreeMapStack
= 0;
50 /// This list maintain the free memory map list
52 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
53 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
55 EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
56 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiReservedMemoryType
57 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderCode
58 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderData
59 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesCode
60 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesData
61 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesCode
62 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesData
63 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiConventionalMemory
64 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiUnusableMemory
65 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIReclaimMemory
66 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIMemoryNVS
67 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIO
68 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIOPortSpace
69 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiPalCode
70 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
} // EfiMaxMemoryType
73 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress
= MAX_ADDRESS
;
74 EFI_PHYSICAL_ADDRESS mDefaultBaseAddress
= MAX_ADDRESS
;
76 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
77 { EfiReservedMemoryType
, 0 },
80 { EfiBootServicesCode
, 0 },
81 { EfiBootServicesData
, 0 },
82 { EfiRuntimeServicesCode
, 0 },
83 { EfiRuntimeServicesData
, 0 },
84 { EfiConventionalMemory
, 0 },
85 { EfiUnusableMemory
, 0 },
86 { EfiACPIReclaimMemory
, 0 },
87 { EfiACPIMemoryNVS
, 0 },
88 { EfiMemoryMappedIO
, 0 },
89 { EfiMemoryMappedIOPortSpace
, 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 funciton
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
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
289 if(FreeDescriptorEntries
!= NULL
) {
291 // Enque the free memmory map entries into the list
293 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
294 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
295 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
302 // dequeue the first descriptor from the list
304 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
305 RemoveEntryList (&Entry
->Link
);
312 Internal function. Moves any memory descriptors that are on the
313 temporary descriptor stack to heap.
317 CoreFreeMemoryMapStack (
325 ASSERT_LOCKED (&gMemoryLock
);
328 // If already freeing the map stack, then return
330 if (mFreeMapStack
!= 0) {
335 // Move the temporary memory descriptor stack into pool
339 while (mMapDepth
!= 0) {
341 // Deque an memory map entry from mFreeMemoryMapEntryList
343 Entry
= AllocateMemoryMapEntry ();
348 // Update to proper entry
352 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
355 // Move this entry to general memory
357 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
358 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
360 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
361 Entry
->FromPages
= TRUE
;
364 // Find insertion location
366 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
367 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
368 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
373 InsertTailList (Link2
, &Entry
->Link
);
377 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
378 // so here no need to move it to memory.
380 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
388 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
392 PromoteMemoryResource (
397 EFI_GCD_MAP_ENTRY
*Entry
;
400 DEBUG ((DEBUG_PAGE
, "Promote the memory resource\n"));
402 CoreAcquireGcdMemoryLock ();
405 Link
= mGcdMemorySpaceMap
.ForwardLink
;
406 while (Link
!= &mGcdMemorySpaceMap
) {
408 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
410 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
411 Entry
->EndAddress
< MAX_ADDRESS
&&
412 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
413 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
415 // Update the GCD map
417 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
418 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
419 Entry
->ImageHandle
= gDxeCoreImageHandle
;
420 Entry
->DeviceHandle
= NULL
;
423 // Add to allocable system memory resource
427 EfiConventionalMemory
,
430 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
432 CoreFreeMemoryMapStack ();
437 Link
= Link
->ForwardLink
;
440 CoreReleaseGcdMemoryLock ();
445 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD
446 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the
447 size of boot time and runtime code.
451 CoreLoadingFixedAddressHook (
455 UINT32 RuntimeCodePageNumber
;
456 UINT32 BootTimeCodePageNumber
;
457 EFI_PHYSICAL_ADDRESS RuntimeCodeBase
;
458 EFI_PHYSICAL_ADDRESS BootTimeCodeBase
;
462 // Make sure these 2 areas are not initialzied.
464 if (!gLoadFixedAddressCodeMemoryReady
) {
465 RuntimeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
466 BootTimeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
467 RuntimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- EFI_PAGES_TO_SIZE (RuntimeCodePageNumber
));
468 BootTimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(RuntimeCodeBase
- EFI_PAGES_TO_SIZE (BootTimeCodePageNumber
));
470 // Try to allocate runtime memory.
472 Status
= CoreAllocatePages (
474 EfiRuntimeServicesCode
,
475 RuntimeCodePageNumber
,
478 if (EFI_ERROR(Status
)) {
480 // Runtime memory allocation failed
485 // Try to allocate boot memory.
487 Status
= CoreAllocatePages (
490 BootTimeCodePageNumber
,
493 if (EFI_ERROR(Status
)) {
495 // boot memory allocation failed. Free Runtime code range and will try the allocation again when
496 // new memory range is installed.
500 RuntimeCodePageNumber
504 gLoadFixedAddressCodeMemoryReady
= TRUE
;
510 Called to initialize the memory map and add descriptors to
511 the current descriptor list.
512 The first descriptor that is added must be general usable
513 memory as the addition allocates heap.
515 @param Type The type of memory to add
516 @param Start The starting address in the memory range Must be
518 @param NumberOfPages The number of pages in the range
519 @param Attribute Attributes of the memory to add
521 @return None. The range is added to the memory map
525 CoreAddMemoryDescriptor (
526 IN EFI_MEMORY_TYPE Type
,
527 IN EFI_PHYSICAL_ADDRESS Start
,
528 IN UINT64 NumberOfPages
,
532 EFI_PHYSICAL_ADDRESS End
;
537 if ((Start
& EFI_PAGE_MASK
) != 0) {
541 if (Type
>= EfiMaxMemoryType
&& Type
<= 0x7fffffff) {
544 CoreAcquireMemoryLock ();
545 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
546 CoreAddRange (Type
, Start
, End
, Attribute
);
547 CoreFreeMemoryMapStack ();
548 CoreReleaseMemoryLock ();
551 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type
553 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
554 CoreLoadingFixedAddressHook();
558 // Check to see if the statistics for the different memory types have already been established
560 if (mMemoryTypeInformationInitialized
) {
566 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
568 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
570 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
572 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
573 if ((UINT32
)Type
> EfiMaxMemoryType
) {
576 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
578 // Allocate pages for the current memory type from the top of available memory
580 Status
= CoreAllocatePages (
583 gMemoryTypeInformation
[Index
].NumberOfPages
,
584 &mMemoryTypeStatistics
[Type
].BaseAddress
586 if (EFI_ERROR (Status
)) {
588 // If an error occurs allocating the pages for the current memory type, then
589 // free all the pages allocates for the previous memory types and return. This
590 // operation with be retied when/if more memory is added to the system
592 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
594 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
596 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
597 if ((UINT32
)Type
> EfiMaxMemoryType
) {
601 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
603 mMemoryTypeStatistics
[Type
].BaseAddress
,
604 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
606 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
607 mMemoryTypeStatistics
[Type
].MaximumAddress
= MAX_ADDRESS
;
614 // Compute the address at the top of the current statistics
616 mMemoryTypeStatistics
[Type
].MaximumAddress
=
617 mMemoryTypeStatistics
[Type
].BaseAddress
+
618 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
621 // If the current base address is the lowest address so far, then update the default
624 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
625 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
631 // There was enough system memory for all the the memory types were allocated. So,
632 // those memory areas can be freed for future allocations, and all future memory
633 // allocations can occur within their respective bins
635 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
637 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
639 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
640 if ((UINT32
)Type
> EfiMaxMemoryType
) {
643 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
645 mMemoryTypeStatistics
[Type
].BaseAddress
,
646 gMemoryTypeInformation
[Index
].NumberOfPages
648 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
649 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
654 // If the number of pages reserved for a memory type is 0, then all allocations for that type
655 // should be in the default range.
657 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
658 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
659 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
660 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
663 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
664 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== MAX_ADDRESS
) {
665 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
669 mMemoryTypeInformationInitialized
= TRUE
;
674 Internal function. Converts a memory range to the specified type or attributes.
675 The range must exist in the memory map. Either ChangingType or
676 ChangingAttributes must be set, but not both.
678 @param Start The first address of the range Must be page
680 @param NumberOfPages The number of pages to convert
681 @param ChangingType Boolean indicating that type value should be changed
682 @param NewType The new type for the memory range
683 @param ChangingAttributes Boolean indicating that attributes value should be changed
684 @param NewAttributes The new attributes for the memory range
686 @retval EFI_INVALID_PARAMETER Invalid parameter
687 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
688 range or convertion not allowed.
689 @retval EFI_SUCCESS Successfully converts the memory range to the
696 IN UINT64 NumberOfPages
,
697 IN BOOLEAN ChangingType
,
698 IN EFI_MEMORY_TYPE NewType
,
699 IN BOOLEAN ChangingAttributes
,
700 IN UINT64 NewAttributes
704 UINT64 NumberOfBytes
;
708 EFI_MEMORY_TYPE MemType
;
713 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
714 End
= Start
+ NumberOfBytes
- 1;
716 ASSERT (NumberOfPages
);
717 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
718 ASSERT (End
> Start
) ;
719 ASSERT_LOCKED (&gMemoryLock
);
720 ASSERT ( (ChangingType
== FALSE
) || (ChangingAttributes
== FALSE
) );
722 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
> (Start
+ NumberOfBytes
))) {
723 return EFI_INVALID_PARAMETER
;
727 // Convert the entire range
730 while (Start
< End
) {
733 // Find the entry that the covers the range
735 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
736 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
738 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
743 if (Link
== &gMemoryMap
) {
744 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
745 return EFI_NOT_FOUND
;
749 // Convert range to the end, or to the end of the descriptor
750 // if that's all we've got
754 ASSERT (Entry
!= NULL
);
755 if (Entry
->End
< End
) {
756 RangeEnd
= Entry
->End
;
760 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to type %d\n", Start
, RangeEnd
, NewType
));
762 if (ChangingAttributes
) {
763 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to attr %lx\n", Start
, RangeEnd
, NewAttributes
));
768 // Debug code - verify conversion is allowed
770 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
771 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types\n"));
772 return EFI_NOT_FOUND
;
776 // Update counters for the number of pages allocated to each memory type
778 if ((UINT32
)Entry
->Type
< EfiMaxMemoryType
) {
779 if ((Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) ||
780 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
781 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
782 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
784 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
789 if ((UINT32
)NewType
< EfiMaxMemoryType
) {
790 if ((Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) ||
791 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
792 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
793 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
> gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
794 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
801 // Pull range out of descriptor
803 if (Entry
->Start
== Start
) {
808 Entry
->Start
= RangeEnd
+ 1;
810 } else if (Entry
->End
== RangeEnd
) {
815 Entry
->End
= Start
- 1;
820 // Pull it out of the center, clip current
826 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
827 mMapStack
[mMapDepth
].FromPages
= FALSE
;
828 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
829 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
830 mMapStack
[mMapDepth
].End
= Entry
->End
;
833 // Inherit Attribute from the Memory Descriptor that is being clipped
835 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
837 Entry
->End
= Start
- 1;
838 ASSERT (Entry
->Start
< Entry
->End
);
840 Entry
= &mMapStack
[mMapDepth
];
841 InsertTailList (&gMemoryMap
, &Entry
->Link
);
844 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
848 // The new range inherits the same Attribute as the Entry
849 // it is being cut out of unless attributes are being changed
852 Attribute
= Entry
->Attribute
;
855 Attribute
= NewAttributes
;
856 MemType
= Entry
->Type
;
860 // If the descriptor is empty, then remove it from the map
862 if (Entry
->Start
== Entry
->End
+ 1) {
863 RemoveMemoryMapEntry (Entry
);
868 // Add our new range in
870 CoreAddRange (MemType
, Start
, RangeEnd
, Attribute
);
871 if (ChangingType
&& (MemType
== EfiConventionalMemory
)) {
873 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
874 // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees
875 // that the page starting at address 0 is always filled with zeros.
878 if (RangeEnd
> EFI_PAGE_SIZE
) {
879 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) EFI_PAGE_SIZE
, (UINTN
) (RangeEnd
- EFI_PAGE_SIZE
+ 1));
882 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) Start
, (UINTN
) (RangeEnd
- Start
+ 1));
887 // Move any map descriptor stack to general pool
889 CoreFreeMemoryMapStack ();
892 // Bump the starting address, and convert the next range
894 Start
= RangeEnd
+ 1;
898 // Converted the whole range, done
906 Internal function. Converts a memory range to the specified type.
907 The range must exist in the memory map.
909 @param Start The first address of the range Must be page
911 @param NumberOfPages The number of pages to convert
912 @param NewType The new type for the memory range
914 @retval EFI_INVALID_PARAMETER Invalid parameter
915 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
916 range or convertion not allowed.
917 @retval EFI_SUCCESS Successfully converts the memory range to the
924 IN UINT64 NumberOfPages
,
925 IN EFI_MEMORY_TYPE NewType
928 return CoreConvertPagesEx(Start
, NumberOfPages
, TRUE
, NewType
, FALSE
, 0);
933 Internal function. Converts a memory range to use new attributes.
935 @param Start The first address of the range Must be page
937 @param NumberOfPages The number of pages to convert
938 @param NewAttributes The new attributes value for the range.
942 CoreUpdateMemoryAttributes (
943 IN EFI_PHYSICAL_ADDRESS Start
,
944 IN UINT64 NumberOfPages
,
945 IN UINT64 NewAttributes
948 CoreAcquireMemoryLock ();
951 // Update the attributes to the new value
953 CoreConvertPagesEx(Start
, NumberOfPages
, FALSE
, (EFI_MEMORY_TYPE
)0, TRUE
, NewAttributes
);
955 CoreReleaseMemoryLock ();
960 Internal function. Finds a consecutive free page range below
961 the requested address.
963 @param MaxAddress The address that the range must be below
964 @param MinAddress The address that the range must be above
965 @param NumberOfPages Number of pages needed
966 @param NewType The type of memory the range is going to be
968 @param Alignment Bits to align with
970 @return The base address of the range, or 0 if the range was not found
975 IN UINT64 MaxAddress
,
976 IN UINT64 MinAddress
,
977 IN UINT64 NumberOfPages
,
978 IN EFI_MEMORY_TYPE NewType
,
982 UINT64 NumberOfBytes
;
986 UINT64 DescNumberOfBytes
;
990 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
994 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
997 // If MaxAddress is not aligned to the end of a page
1001 // Change MaxAddress to be 1 page lower
1003 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
1006 // Set MaxAddress to a page boundary
1008 MaxAddress
&= ~(UINT64
)EFI_PAGE_MASK
;
1011 // Set MaxAddress to end of the page
1013 MaxAddress
|= EFI_PAGE_MASK
;
1016 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1019 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1020 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1023 // If it's not a free entry, don't bother with it
1025 if (Entry
->Type
!= EfiConventionalMemory
) {
1029 DescStart
= Entry
->Start
;
1030 DescEnd
= Entry
->End
;
1033 // If desc is past max allowed address or below min allowed address, skip it
1035 if ((DescStart
>= MaxAddress
) || (DescEnd
< MinAddress
)) {
1040 // If desc ends past max allowed address, clip the end
1042 if (DescEnd
>= MaxAddress
) {
1043 DescEnd
= MaxAddress
;
1046 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
1049 // Compute the number of bytes we can used from this
1050 // descriptor, and see it's enough to satisfy the request
1052 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
1054 if (DescNumberOfBytes
>= NumberOfBytes
) {
1056 // If the start of the allocated range is below the min address allowed, skip it
1058 if ((DescEnd
- NumberOfBytes
+ 1) < MinAddress
) {
1063 // If this is the best match so far remember it
1065 if (DescEnd
> Target
) {
1072 // If this is a grow down, adjust target to be the allocation base
1074 Target
-= NumberOfBytes
- 1;
1077 // If we didn't find a match, return 0
1079 if ((Target
& EFI_PAGE_MASK
) != 0) {
1088 Internal function. Finds a consecutive free page range below
1089 the requested address
1091 @param MaxAddress The address that the range must be below
1092 @param NoPages Number of pages needed
1093 @param NewType The type of memory the range is going to be
1095 @param Alignment Bits to align with
1097 @return The base address of the range, or 0 if the range was not found.
1102 IN UINT64 MaxAddress
,
1104 IN EFI_MEMORY_TYPE NewType
,
1111 // Attempt to find free pages in the preferred bin based on the requested memory type
1113 if ((UINT32
)NewType
< EfiMaxMemoryType
&& MaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1114 Start
= CoreFindFreePagesI (
1115 mMemoryTypeStatistics
[NewType
].MaximumAddress
,
1116 mMemoryTypeStatistics
[NewType
].BaseAddress
,
1127 // Attempt to find free pages in the default allocation bin
1129 if (MaxAddress
>= mDefaultMaximumAddress
) {
1130 Start
= CoreFindFreePagesI (mDefaultMaximumAddress
, 0, NoPages
, NewType
, Alignment
);
1132 if (Start
< mDefaultBaseAddress
) {
1133 mDefaultBaseAddress
= Start
;
1140 // The allocation did not succeed in any of the prefered bins even after
1141 // promoting resources. Attempt to find free pages anywhere is the requested
1142 // address range. If this allocation fails, then there are not enough
1143 // resources anywhere to satisfy the request.
1145 Start
= CoreFindFreePagesI (MaxAddress
, 0, NoPages
, NewType
, Alignment
);
1151 // If allocations from the preferred bins fail, then attempt to promote memory resources.
1153 if (!PromoteMemoryResource ()) {
1158 // If any memory resources were promoted, then re-attempt the allocation
1160 return FindFreePages (MaxAddress
, NoPages
, NewType
, Alignment
);
1165 Allocates pages from the memory map.
1167 @param Type The type of allocation to perform
1168 @param MemoryType The type of memory to turn the allocated pages
1170 @param NumberOfPages The number of pages to allocate
1171 @param Memory A pointer to receive the base allocated memory
1174 @return Status. On success, Memory is filled in with the base address allocated
1175 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1177 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1178 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1179 @retval EFI_SUCCESS Pages successfully allocated.
1184 CoreInternalAllocatePages (
1185 IN EFI_ALLOCATE_TYPE Type
,
1186 IN EFI_MEMORY_TYPE MemoryType
,
1187 IN UINTN NumberOfPages
,
1188 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1196 if ((UINT32
)Type
>= MaxAllocateType
) {
1197 return EFI_INVALID_PARAMETER
;
1200 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
1201 MemoryType
== EfiConventionalMemory
) {
1202 return EFI_INVALID_PARAMETER
;
1205 if (Memory
== NULL
) {
1206 return EFI_INVALID_PARAMETER
;
1209 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1211 if (MemoryType
== EfiACPIReclaimMemory
||
1212 MemoryType
== EfiACPIMemoryNVS
||
1213 MemoryType
== EfiRuntimeServicesCode
||
1214 MemoryType
== EfiRuntimeServicesData
) {
1216 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1219 if (Type
== AllocateAddress
) {
1220 if ((*Memory
& (Alignment
- 1)) != 0) {
1221 return EFI_NOT_FOUND
;
1225 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1226 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1229 // If this is for below a particular address, then
1234 // The max address is the max natively addressable address for the processor
1236 MaxAddress
= MAX_ADDRESS
;
1238 if (Type
== AllocateMaxAddress
) {
1242 CoreAcquireMemoryLock ();
1245 // If not a specific address, then find an address to allocate
1247 if (Type
!= AllocateAddress
) {
1248 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1250 Status
= EFI_OUT_OF_RESOURCES
;
1256 // Convert pages from FreeMemory to the requested type
1258 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1261 CoreReleaseMemoryLock ();
1263 if (!EFI_ERROR (Status
)) {
1271 Allocates pages from the memory map.
1273 @param Type The type of allocation to perform
1274 @param MemoryType The type of memory to turn the allocated pages
1276 @param NumberOfPages The number of pages to allocate
1277 @param Memory A pointer to receive the base allocated memory
1280 @return Status. On success, Memory is filled in with the base address allocated
1281 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1283 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1284 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1285 @retval EFI_SUCCESS Pages successfully allocated.
1291 IN EFI_ALLOCATE_TYPE Type
,
1292 IN EFI_MEMORY_TYPE MemoryType
,
1293 IN UINTN NumberOfPages
,
1294 OUT EFI_PHYSICAL_ADDRESS
*Memory
1299 Status
= CoreInternalAllocatePages (Type
, MemoryType
, NumberOfPages
, Memory
);
1300 if (!EFI_ERROR (Status
)) {
1301 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionAllocatePages
, MemoryType
, EFI_PAGES_TO_SIZE (NumberOfPages
), (VOID
*) (UINTN
) *Memory
);
1307 Frees previous allocated pages.
1309 @param Memory Base address of memory being freed
1310 @param NumberOfPages The number of pages to free
1312 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1313 @retval EFI_INVALID_PARAMETER Address not aligned
1314 @return EFI_SUCCESS -Pages successfully freed.
1319 CoreInternalFreePages (
1320 IN EFI_PHYSICAL_ADDRESS Memory
,
1321 IN UINTN NumberOfPages
1332 CoreAcquireMemoryLock ();
1335 // Find the entry that the covers the range
1338 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1339 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1340 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1344 if (Link
== &gMemoryMap
) {
1345 Status
= EFI_NOT_FOUND
;
1349 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1351 ASSERT (Entry
!= NULL
);
1352 if (Entry
->Type
== EfiACPIReclaimMemory
||
1353 Entry
->Type
== EfiACPIMemoryNVS
||
1354 Entry
->Type
== EfiRuntimeServicesCode
||
1355 Entry
->Type
== EfiRuntimeServicesData
) {
1357 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1361 if ((Memory
& (Alignment
- 1)) != 0) {
1362 Status
= EFI_INVALID_PARAMETER
;
1366 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1367 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1369 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1371 if (EFI_ERROR (Status
)) {
1376 CoreReleaseMemoryLock ();
1381 Frees previous allocated pages.
1383 @param Memory Base address of memory being freed
1384 @param NumberOfPages The number of pages to free
1386 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1387 @retval EFI_INVALID_PARAMETER Address not aligned
1388 @return EFI_SUCCESS -Pages successfully freed.
1394 IN EFI_PHYSICAL_ADDRESS Memory
,
1395 IN UINTN NumberOfPages
1400 Status
= CoreInternalFreePages (Memory
, NumberOfPages
);
1401 if (!EFI_ERROR (Status
)) {
1402 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0), MemoryProfileActionFreePages
, (EFI_MEMORY_TYPE
) 0, EFI_PAGES_TO_SIZE (NumberOfPages
), (VOID
*) (UINTN
) Memory
);
1408 This function checks to see if the last memory map descriptor in a memory map
1409 can be merged with any of the other memory map descriptors in a memorymap.
1410 Memory descriptors may be merged if they are adjacent and have the same type
1413 @param MemoryMap A pointer to the start of the memory map.
1414 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.
1415 @param DescriptorSize The size, in bytes, of an individual
1416 EFI_MEMORY_DESCRIPTOR.
1418 @return A pointer to the next available descriptor in MemoryMap
1421 EFI_MEMORY_DESCRIPTOR
*
1422 MergeMemoryMapDescriptor (
1423 IN EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1424 IN EFI_MEMORY_DESCRIPTOR
*MemoryMapDescriptor
,
1425 IN UINTN DescriptorSize
1429 // Traverse the array of descriptors in MemoryMap
1431 for (; MemoryMap
!= MemoryMapDescriptor
; MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, DescriptorSize
)) {
1433 // Check to see if the Type fields are identical.
1435 if (MemoryMap
->Type
!= MemoryMapDescriptor
->Type
) {
1440 // Check to see if the Attribute fields are identical.
1442 if (MemoryMap
->Attribute
!= MemoryMapDescriptor
->Attribute
) {
1447 // Check to see if MemoryMapDescriptor is immediately above MemoryMap
1449 if (MemoryMap
->PhysicalStart
+ EFI_PAGES_TO_SIZE ((UINTN
)MemoryMap
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1451 // Merge MemoryMapDescriptor into MemoryMap
1453 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1456 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1458 return MemoryMapDescriptor
;
1462 // Check to see if MemoryMapDescriptor is immediately below MemoryMap
1464 if (MemoryMap
->PhysicalStart
- EFI_PAGES_TO_SIZE ((UINTN
)MemoryMapDescriptor
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1466 // Merge MemoryMapDescriptor into MemoryMap
1468 MemoryMap
->PhysicalStart
= MemoryMapDescriptor
->PhysicalStart
;
1469 MemoryMap
->VirtualStart
= MemoryMapDescriptor
->VirtualStart
;
1470 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1473 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1475 return MemoryMapDescriptor
;
1480 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.
1482 // Return the slot immediately after MemoryMapDescriptor as the next available
1483 // slot in the MemoryMap array
1485 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor
, DescriptorSize
);
1489 This function returns a copy of the current memory map. The map is an array of
1490 memory descriptors, each of which describes a contiguous block of memory.
1492 @param MemoryMapSize A pointer to the size, in bytes, of the
1493 MemoryMap buffer. On input, this is the size of
1494 the buffer allocated by the caller. On output,
1495 it is the size of the buffer returned by the
1496 firmware if the buffer was large enough, or the
1497 size of the buffer needed to contain the map if
1498 the buffer was too small.
1499 @param MemoryMap A pointer to the buffer in which firmware places
1500 the current memory map.
1501 @param MapKey A pointer to the location in which firmware
1502 returns the key for the current memory map.
1503 @param DescriptorSize A pointer to the location in which firmware
1504 returns the size, in bytes, of an individual
1505 EFI_MEMORY_DESCRIPTOR.
1506 @param DescriptorVersion A pointer to the location in which firmware
1507 returns the version number associated with the
1508 EFI_MEMORY_DESCRIPTOR.
1510 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1512 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1513 buffer size needed to hold the memory map is
1514 returned in MemoryMapSize.
1515 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1521 IN OUT UINTN
*MemoryMapSize
,
1522 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1524 OUT UINTN
*DescriptorSize
,
1525 OUT UINT32
*DescriptorVersion
1531 UINTN NumberOfRuntimeEntries
;
1534 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1535 EFI_MEMORY_TYPE Type
;
1536 EFI_MEMORY_DESCRIPTOR
*MemoryMapStart
;
1539 // Make sure the parameters are valid
1541 if (MemoryMapSize
== NULL
) {
1542 return EFI_INVALID_PARAMETER
;
1545 CoreAcquireGcdMemoryLock ();
1548 // Count the number of Reserved and MMIO entries that are marked for runtime use
1550 NumberOfRuntimeEntries
= 0;
1551 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1552 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1553 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1554 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1555 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1556 NumberOfRuntimeEntries
++;
1561 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1564 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1565 // prevent people from having pointer math bugs in their code.
1566 // now you have to use *DescriptorSize to make things work.
1568 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1570 if (DescriptorSize
!= NULL
) {
1571 *DescriptorSize
= Size
;
1574 if (DescriptorVersion
!= NULL
) {
1575 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1578 CoreAcquireMemoryLock ();
1581 // Compute the buffer size needed to fit the entire map
1583 BufferSize
= Size
* NumberOfRuntimeEntries
;
1584 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1588 if (*MemoryMapSize
< BufferSize
) {
1589 Status
= EFI_BUFFER_TOO_SMALL
;
1593 if (MemoryMap
== NULL
) {
1594 Status
= EFI_INVALID_PARAMETER
;
1601 ZeroMem (MemoryMap
, BufferSize
);
1602 MemoryMapStart
= MemoryMap
;
1603 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1604 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1605 ASSERT (Entry
->VirtualStart
== 0);
1608 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1610 MemoryMap
->Type
= Entry
->Type
;
1611 MemoryMap
->PhysicalStart
= Entry
->Start
;
1612 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1613 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1615 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1616 // memory type bin and needs to be converted to the same memory type as the rest of the
1617 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1618 // improves the chances for a successful S4 resume in the presence of minor page allocation
1619 // differences across reboots.
1621 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1622 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1623 if (mMemoryTypeStatistics
[Type
].Special
&&
1624 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1625 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1626 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1627 MemoryMap
->Type
= Type
;
1631 MemoryMap
->Attribute
= Entry
->Attribute
;
1632 if (MemoryMap
->Type
< EfiMaxMemoryType
) {
1633 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1634 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1639 // Check to see if the new Memory Map Descriptor can be merged with an
1640 // existing descriptor if they are adjacent and have the same attributes
1642 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1645 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1646 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1647 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1648 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1649 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1651 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries
1652 // that are marked for runtime use
1654 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1655 MemoryMap
->VirtualStart
= 0;
1656 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1657 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1659 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1660 MemoryMap
->Type
= EfiReservedMemoryType
;
1661 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1662 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1663 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1665 MemoryMap
->Type
= EfiMemoryMappedIO
;
1670 // Check to see if the new Memory Map Descriptor can be merged with an
1671 // existing descriptor if they are adjacent and have the same attributes
1673 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1679 // Compute the size of the buffer actually used after all memory map descriptor merge operations
1681 BufferSize
= ((UINT8
*)MemoryMap
- (UINT8
*)MemoryMapStart
);
1683 Status
= EFI_SUCCESS
;
1687 // Update the map key finally
1689 if (MapKey
!= NULL
) {
1690 *MapKey
= mMemoryMapKey
;
1693 CoreReleaseMemoryLock ();
1695 CoreReleaseGcdMemoryLock ();
1697 *MemoryMapSize
= BufferSize
;
1704 Internal function. Used by the pool functions to allocate pages
1705 to back pool allocation requests.
1707 @param PoolType The type of memory for the new pool pages
1708 @param NumberOfPages No of pages to allocate
1709 @param Alignment Bits to align.
1711 @return The allocated memory, or NULL
1715 CoreAllocatePoolPages (
1716 IN EFI_MEMORY_TYPE PoolType
,
1717 IN UINTN NumberOfPages
,
1724 // Find the pages to convert
1726 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1729 // Convert it to boot services data
1732 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1734 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1737 return (VOID
*)(UINTN
) Start
;
1742 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1744 @param Memory The base address to free
1745 @param NumberOfPages The number of pages to free
1750 IN EFI_PHYSICAL_ADDRESS Memory
,
1751 IN UINTN NumberOfPages
1754 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1760 Make sure the memory map is following all the construction rules,
1761 it is the last time to check memory map error before exit boot services.
1763 @param MapKey Memory map key
1765 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1767 @retval EFI_SUCCESS Valid memory map.
1771 CoreTerminateMemoryMap (
1779 Status
= EFI_SUCCESS
;
1781 CoreAcquireMemoryLock ();
1783 if (MapKey
== mMemoryMapKey
) {
1786 // Make sure the memory map is following all the construction rules
1787 // This is the last chance we will be able to display any messages on
1788 // the console devices.
1791 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1792 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1793 if (Entry
->Type
< EfiMaxMemoryType
) {
1794 if (mMemoryTypeStatistics
[Entry
->Type
].Runtime
) {
1795 ASSERT (Entry
->Type
!= EfiACPIReclaimMemory
);
1796 ASSERT (Entry
->Type
!= EfiACPIMemoryNVS
);
1797 if ((Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1798 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1799 Status
= EFI_INVALID_PARAMETER
;
1802 if (((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1803 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1804 Status
= EFI_INVALID_PARAMETER
;
1812 // The map key they gave us matches what we expect. Fall through and
1813 // return success. In an ideal world we would clear out all of
1814 // EfiBootServicesCode and EfiBootServicesData. However this function
1815 // is not the last one called by ExitBootServices(), so we have to
1816 // preserve the memory contents.
1819 Status
= EFI_INVALID_PARAMETER
;
1823 CoreReleaseMemoryLock ();