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.
17 #include "HeapGuard.h"
20 // Entry for tracking the memory regions for each memory type to coalesce similar memory types
23 EFI_PHYSICAL_ADDRESS BaseAddress
;
24 EFI_PHYSICAL_ADDRESS MaximumAddress
;
25 UINT64 CurrentNumberOfPages
;
27 UINTN InformationIndex
;
30 } EFI_MEMORY_TYPE_STATISTICS
;
33 // MemoryMap - The current memory map
35 UINTN mMemoryMapKey
= 0;
37 #define MAX_MAP_DEPTH 6
40 /// mMapDepth - depth of new descriptor stack
44 /// mMapStack - space to use as temp storage to build new map descriptors
46 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
47 UINTN mFreeMapStack
= 0;
49 /// This list maintain the free memory map list
51 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
52 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
54 EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
55 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiReservedMemoryType
56 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderCode
57 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderData
58 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesCode
59 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesData
60 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesCode
61 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesData
62 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiConventionalMemory
63 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiUnusableMemory
64 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIReclaimMemory
65 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIMemoryNVS
66 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIO
67 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIOPortSpace
68 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiPalCode
69 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiPersistentMemory
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 { EfiPersistentMemory
, 0 },
92 { EfiMaxMemoryType
, 0 }
95 // Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated
96 // and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a
97 // address assigned by DXE core.
99 GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady
= FALSE
;
102 Enter critical section by gaining lock on gMemoryLock.
106 CoreAcquireMemoryLock (
110 CoreAcquireLock (&gMemoryLock
);
116 Exit critical section by releasing lock on gMemoryLock.
120 CoreReleaseMemoryLock (
124 CoreReleaseLock (&gMemoryLock
);
131 Internal function. Removes a descriptor entry.
133 @param Entry The entry to remove
137 RemoveMemoryMapEntry (
138 IN OUT MEMORY_MAP
*Entry
141 RemoveEntryList (&Entry
->Link
);
142 Entry
->Link
.ForwardLink
= NULL
;
144 if (Entry
->FromPages
) {
146 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
148 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
153 Internal function. Adds a ranges to the memory map.
154 The range must not already exist in the map.
156 @param Type The type of memory range to add
157 @param Start The starting address in the memory range Must be
159 @param End The last address in the range Must be the last
161 @param Attribute The attributes of the memory range to add
166 IN EFI_MEMORY_TYPE Type
,
167 IN EFI_PHYSICAL_ADDRESS Start
,
168 IN EFI_PHYSICAL_ADDRESS End
,
175 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
176 ASSERT (End
> Start
) ;
178 ASSERT_LOCKED (&gMemoryLock
);
180 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
183 // If memory of type EfiConventionalMemory is being added that includes the page
184 // starting at address 0, then zero the page starting at address 0. This has
185 // two benifits. It helps find NULL pointer bugs and it also maximizes
186 // compatibility with operating systems that may evaluate memory in this page
187 // for legacy data structures. If memory of any other type is added starting
188 // at address 0, then do not zero the page at address 0 because the page is being
189 // used for other purposes.
191 if (Type
== EfiConventionalMemory
&& Start
== 0 && (End
>= EFI_PAGE_SIZE
- 1)) {
192 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask
) & BIT0
) == 0) {
193 SetMem ((VOID
*)(UINTN
)Start
, EFI_PAGE_SIZE
, 0);
198 // Memory map being altered so updated key
203 // UEFI 2.0 added an event group for notificaiton on memory map changes.
204 // So we need to signal this Event Group every time the memory map changes.
205 // If we are in EFI 1.10 compatability mode no event groups will be
206 // found and nothing will happen we we call this function. These events
207 // will get signaled but since a lock is held around the call to this
208 // function the notificaiton events will only be called after this function
209 // returns and the lock is released.
211 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
214 // Look for adjoining memory descriptor
217 // Two memory descriptors can only be merged if they have the same Type
218 // and the same Attribute
221 Link
= gMemoryMap
.ForwardLink
;
222 while (Link
!= &gMemoryMap
) {
223 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
224 Link
= Link
->ForwardLink
;
226 if (Entry
->Type
!= Type
) {
230 if (Entry
->Attribute
!= Attribute
) {
234 if (Entry
->End
+ 1 == Start
) {
236 Start
= Entry
->Start
;
237 RemoveMemoryMapEntry (Entry
);
239 } else if (Entry
->Start
== End
+ 1) {
242 RemoveMemoryMapEntry (Entry
);
250 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
251 mMapStack
[mMapDepth
].FromPages
= FALSE
;
252 mMapStack
[mMapDepth
].Type
= Type
;
253 mMapStack
[mMapDepth
].Start
= Start
;
254 mMapStack
[mMapDepth
].End
= End
;
255 mMapStack
[mMapDepth
].VirtualStart
= 0;
256 mMapStack
[mMapDepth
].Attribute
= Attribute
;
257 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
260 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
266 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
267 If the list is emtry, then allocate a new page to refuel the list.
268 Please Note this algorithm to allocate the memory map descriptor has a property
269 that the memory allocated for memory entries always grows, and will never really be freed
270 For example, if the current boot uses 2000 memory map entries at the maximum point, but
271 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
272 memory map entries is still allocated from EfiBootServicesMemory.
275 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
279 AllocateMemoryMapEntry (
283 MEMORY_MAP
* FreeDescriptorEntries
;
287 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
289 // The list is empty, to allocate one page to refuel the list
291 FreeDescriptorEntries
= CoreAllocatePoolPages (
293 EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION_GRANULARITY
),
294 DEFAULT_PAGE_ALLOCATION_GRANULARITY
,
297 if (FreeDescriptorEntries
!= NULL
) {
299 // Enque the free memmory map entries into the list
301 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION_GRANULARITY
/ sizeof(MEMORY_MAP
); Index
++) {
302 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
303 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
310 // dequeue the first descriptor from the list
312 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
313 RemoveEntryList (&Entry
->Link
);
320 Internal function. Moves any memory descriptors that are on the
321 temporary descriptor stack to heap.
325 CoreFreeMemoryMapStack (
333 ASSERT_LOCKED (&gMemoryLock
);
336 // If already freeing the map stack, then return
338 if (mFreeMapStack
!= 0) {
343 // Move the temporary memory descriptor stack into pool
347 while (mMapDepth
!= 0) {
349 // Deque an memory map entry from mFreeMemoryMapEntryList
351 Entry
= AllocateMemoryMapEntry ();
356 // Update to proper entry
360 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
363 // Move this entry to general memory
365 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
366 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
368 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
369 Entry
->FromPages
= TRUE
;
372 // Find insertion location
374 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
375 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
376 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
381 InsertTailList (Link2
, &Entry
->Link
);
385 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
386 // so here no need to move it to memory.
388 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
396 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
400 PromoteMemoryResource (
405 EFI_GCD_MAP_ENTRY
*Entry
;
408 DEBUG ((DEBUG_PAGE
, "Promote the memory resource\n"));
410 CoreAcquireGcdMemoryLock ();
413 Link
= mGcdMemorySpaceMap
.ForwardLink
;
414 while (Link
!= &mGcdMemorySpaceMap
) {
416 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
418 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
419 Entry
->EndAddress
< MAX_ADDRESS
&&
420 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
421 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
423 // Update the GCD map
425 if ((Entry
->Capabilities
& EFI_MEMORY_MORE_RELIABLE
) == EFI_MEMORY_MORE_RELIABLE
) {
426 Entry
->GcdMemoryType
= EfiGcdMemoryTypeMoreReliable
;
428 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
430 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
431 Entry
->ImageHandle
= gDxeCoreImageHandle
;
432 Entry
->DeviceHandle
= NULL
;
435 // Add to allocable system memory resource
439 EfiConventionalMemory
,
442 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
444 CoreFreeMemoryMapStack ();
449 Link
= Link
->ForwardLink
;
452 CoreReleaseGcdMemoryLock ();
457 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD
458 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the
459 size of boot time and runtime code.
463 CoreLoadingFixedAddressHook (
467 UINT32 RuntimeCodePageNumber
;
468 UINT32 BootTimeCodePageNumber
;
469 EFI_PHYSICAL_ADDRESS RuntimeCodeBase
;
470 EFI_PHYSICAL_ADDRESS BootTimeCodeBase
;
474 // Make sure these 2 areas are not initialzied.
476 if (!gLoadFixedAddressCodeMemoryReady
) {
477 RuntimeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
478 BootTimeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
479 RuntimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- EFI_PAGES_TO_SIZE (RuntimeCodePageNumber
));
480 BootTimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(RuntimeCodeBase
- EFI_PAGES_TO_SIZE (BootTimeCodePageNumber
));
482 // Try to allocate runtime memory.
484 Status
= CoreAllocatePages (
486 EfiRuntimeServicesCode
,
487 RuntimeCodePageNumber
,
490 if (EFI_ERROR(Status
)) {
492 // Runtime memory allocation failed
497 // Try to allocate boot memory.
499 Status
= CoreAllocatePages (
502 BootTimeCodePageNumber
,
505 if (EFI_ERROR(Status
)) {
507 // boot memory allocation failed. Free Runtime code range and will try the allocation again when
508 // new memory range is installed.
512 RuntimeCodePageNumber
516 gLoadFixedAddressCodeMemoryReady
= TRUE
;
522 Called to initialize the memory map and add descriptors to
523 the current descriptor list.
524 The first descriptor that is added must be general usable
525 memory as the addition allocates heap.
527 @param Type The type of memory to add
528 @param Start The starting address in the memory range Must be
530 @param NumberOfPages The number of pages in the range
531 @param Attribute Attributes of the memory to add
533 @return None. The range is added to the memory map
537 CoreAddMemoryDescriptor (
538 IN EFI_MEMORY_TYPE Type
,
539 IN EFI_PHYSICAL_ADDRESS Start
,
540 IN UINT64 NumberOfPages
,
544 EFI_PHYSICAL_ADDRESS End
;
549 if ((Start
& EFI_PAGE_MASK
) != 0) {
553 if (Type
>= EfiMaxMemoryType
&& Type
< MEMORY_TYPE_OEM_RESERVED_MIN
) {
556 CoreAcquireMemoryLock ();
557 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
558 CoreAddRange (Type
, Start
, End
, Attribute
);
559 CoreFreeMemoryMapStack ();
560 CoreReleaseMemoryLock ();
562 ApplyMemoryProtectionPolicy (EfiMaxMemoryType
, Type
, Start
,
563 LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
));
566 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type
568 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
569 CoreLoadingFixedAddressHook();
573 // Check to see if the statistics for the different memory types have already been established
575 if (mMemoryTypeInformationInitialized
) {
581 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
583 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
585 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
587 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
588 if ((UINT32
)Type
> EfiMaxMemoryType
) {
591 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
593 // Allocate pages for the current memory type from the top of available memory
595 Status
= CoreAllocatePages (
598 gMemoryTypeInformation
[Index
].NumberOfPages
,
599 &mMemoryTypeStatistics
[Type
].BaseAddress
601 if (EFI_ERROR (Status
)) {
603 // If an error occurs allocating the pages for the current memory type, then
604 // free all the pages allocates for the previous memory types and return. This
605 // operation with be retied when/if more memory is added to the system
607 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
609 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
611 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
612 if ((UINT32
)Type
> EfiMaxMemoryType
) {
616 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
618 mMemoryTypeStatistics
[Type
].BaseAddress
,
619 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
621 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
622 mMemoryTypeStatistics
[Type
].MaximumAddress
= MAX_ADDRESS
;
629 // Compute the address at the top of the current statistics
631 mMemoryTypeStatistics
[Type
].MaximumAddress
=
632 mMemoryTypeStatistics
[Type
].BaseAddress
+
633 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
636 // If the current base address is the lowest address so far, then update the default
639 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
640 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
646 // There was enough system memory for all the the memory types were allocated. So,
647 // those memory areas can be freed for future allocations, and all future memory
648 // allocations can occur within their respective bins
650 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
652 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
654 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
655 if ((UINT32
)Type
> EfiMaxMemoryType
) {
658 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
660 mMemoryTypeStatistics
[Type
].BaseAddress
,
661 gMemoryTypeInformation
[Index
].NumberOfPages
663 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
664 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
669 // If the number of pages reserved for a memory type is 0, then all allocations for that type
670 // should be in the default range.
672 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
673 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
674 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
675 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
678 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
679 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== MAX_ADDRESS
) {
680 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
684 mMemoryTypeInformationInitialized
= TRUE
;
689 Internal function. Converts a memory range to the specified type or attributes.
690 The range must exist in the memory map. Either ChangingType or
691 ChangingAttributes must be set, but not both.
693 @param Start The first address of the range Must be page
695 @param NumberOfPages The number of pages to convert
696 @param ChangingType Boolean indicating that type value should be changed
697 @param NewType The new type for the memory range
698 @param ChangingAttributes Boolean indicating that attributes value should be changed
699 @param NewAttributes The new attributes for the memory range
701 @retval EFI_INVALID_PARAMETER Invalid parameter
702 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
703 range or convertion not allowed.
704 @retval EFI_SUCCESS Successfully converts the memory range to the
711 IN UINT64 NumberOfPages
,
712 IN BOOLEAN ChangingType
,
713 IN EFI_MEMORY_TYPE NewType
,
714 IN BOOLEAN ChangingAttributes
,
715 IN UINT64 NewAttributes
719 UINT64 NumberOfBytes
;
723 EFI_MEMORY_TYPE MemType
;
728 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
729 End
= Start
+ NumberOfBytes
- 1;
731 ASSERT (NumberOfPages
);
732 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
733 ASSERT (End
> Start
) ;
734 ASSERT_LOCKED (&gMemoryLock
);
735 ASSERT ( (ChangingType
== FALSE
) || (ChangingAttributes
== FALSE
) );
737 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
>= End
)) {
738 return EFI_INVALID_PARAMETER
;
742 // Convert the entire range
745 while (Start
< End
) {
748 // Find the entry that the covers the range
750 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
751 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
753 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
758 if (Link
== &gMemoryMap
) {
759 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
760 return EFI_NOT_FOUND
;
764 // If we are converting the type of the range from EfiConventionalMemory to
765 // another type, we have to ensure that the entire range is covered by a
768 if (ChangingType
&& (NewType
!= EfiConventionalMemory
)) {
769 if (Entry
->End
< End
) {
770 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: range %lx - %lx covers multiple entries\n", Start
, End
));
771 return EFI_NOT_FOUND
;
775 // Convert range to the end, or to the end of the descriptor
776 // if that's all we've got
780 ASSERT (Entry
!= NULL
);
781 if (Entry
->End
< End
) {
782 RangeEnd
= Entry
->End
;
786 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to type %d\n", Start
, RangeEnd
, NewType
));
788 if (ChangingAttributes
) {
789 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to attr %lx\n", Start
, RangeEnd
, NewAttributes
));
794 // Debug code - verify conversion is allowed
796 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
797 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types, "));
798 if (Entry
->Type
== EfiConventionalMemory
) {
799 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "the pages to free have been freed\n"));
801 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "the pages to allocate have been allocated\n"));
803 return EFI_NOT_FOUND
;
807 // Update counters for the number of pages allocated to each memory type
809 if ((UINT32
)Entry
->Type
< EfiMaxMemoryType
) {
810 if ((Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) ||
811 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
812 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
813 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
815 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
820 if ((UINT32
)NewType
< EfiMaxMemoryType
) {
821 if ((Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) ||
822 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
823 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
824 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
> gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
825 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
832 // Pull range out of descriptor
834 if (Entry
->Start
== Start
) {
839 Entry
->Start
= RangeEnd
+ 1;
841 } else if (Entry
->End
== RangeEnd
) {
846 Entry
->End
= Start
- 1;
851 // Pull it out of the center, clip current
857 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
858 mMapStack
[mMapDepth
].FromPages
= FALSE
;
859 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
860 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
861 mMapStack
[mMapDepth
].End
= Entry
->End
;
864 // Inherit Attribute from the Memory Descriptor that is being clipped
866 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
868 Entry
->End
= Start
- 1;
869 ASSERT (Entry
->Start
< Entry
->End
);
871 Entry
= &mMapStack
[mMapDepth
];
872 InsertTailList (&gMemoryMap
, &Entry
->Link
);
875 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
879 // The new range inherits the same Attribute as the Entry
880 // it is being cut out of unless attributes are being changed
883 Attribute
= Entry
->Attribute
;
886 Attribute
= NewAttributes
;
887 MemType
= Entry
->Type
;
891 // If the descriptor is empty, then remove it from the map
893 if (Entry
->Start
== Entry
->End
+ 1) {
894 RemoveMemoryMapEntry (Entry
);
899 // Add our new range in
901 CoreAddRange (MemType
, Start
, RangeEnd
, Attribute
);
902 if (ChangingType
&& (MemType
== EfiConventionalMemory
)) {
905 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
906 // macro will ASSERT() if address is 0. Instead, CoreAddRange()
907 // guarantees that the page starting at address 0 is always filled
910 if (RangeEnd
> EFI_PAGE_SIZE
) {
911 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) EFI_PAGE_SIZE
, (UINTN
) (RangeEnd
- EFI_PAGE_SIZE
+ 1));
915 // If Heap Guard is enabled, the page at the top and/or bottom of
916 // this memory block to free might be inaccessible. Skipping them
917 // to avoid page fault exception.
922 StartToClear
= Start
;
923 EndToClear
= RangeEnd
+ 1;
924 if (PcdGet8 (PcdHeapGuardPropertyMask
) & (BIT1
|BIT0
)) {
925 if (IsGuardPage(StartToClear
)) {
926 StartToClear
+= EFI_PAGE_SIZE
;
928 if (IsGuardPage (EndToClear
- 1)) {
929 EndToClear
-= EFI_PAGE_SIZE
;
933 if (EndToClear
> StartToClear
) {
935 (VOID
*)(UINTN
)StartToClear
,
936 (UINTN
)(EndToClear
- StartToClear
)
943 // Move any map descriptor stack to general pool
945 CoreFreeMemoryMapStack ();
948 // Bump the starting address, and convert the next range
950 Start
= RangeEnd
+ 1;
954 // Converted the whole range, done
962 Internal function. Converts a memory range to the specified type.
963 The range must exist in the memory map.
965 @param Start The first address of the range Must be page
967 @param NumberOfPages The number of pages to convert
968 @param NewType The new type for the memory range
970 @retval EFI_INVALID_PARAMETER Invalid parameter
971 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
972 range or convertion not allowed.
973 @retval EFI_SUCCESS Successfully converts the memory range to the
980 IN UINT64 NumberOfPages
,
981 IN EFI_MEMORY_TYPE NewType
984 return CoreConvertPagesEx(Start
, NumberOfPages
, TRUE
, NewType
, FALSE
, 0);
989 Internal function. Converts a memory range to use new attributes.
991 @param Start The first address of the range Must be page
993 @param NumberOfPages The number of pages to convert
994 @param NewAttributes The new attributes value for the range.
998 CoreUpdateMemoryAttributes (
999 IN EFI_PHYSICAL_ADDRESS Start
,
1000 IN UINT64 NumberOfPages
,
1001 IN UINT64 NewAttributes
1004 CoreAcquireMemoryLock ();
1007 // Update the attributes to the new value
1009 CoreConvertPagesEx(Start
, NumberOfPages
, FALSE
, (EFI_MEMORY_TYPE
)0, TRUE
, NewAttributes
);
1011 CoreReleaseMemoryLock ();
1016 Internal function. Finds a consecutive free page range below
1017 the requested address.
1019 @param MaxAddress The address that the range must be below
1020 @param MinAddress The address that the range must be above
1021 @param NumberOfPages Number of pages needed
1022 @param NewType The type of memory the range is going to be
1024 @param Alignment Bits to align with
1025 @param NeedGuard Flag to indicate Guard page is needed or not
1027 @return The base address of the range, or 0 if the range was not found
1031 CoreFindFreePagesI (
1032 IN UINT64 MaxAddress
,
1033 IN UINT64 MinAddress
,
1034 IN UINT64 NumberOfPages
,
1035 IN EFI_MEMORY_TYPE NewType
,
1037 IN BOOLEAN NeedGuard
1040 UINT64 NumberOfBytes
;
1044 UINT64 DescNumberOfBytes
;
1048 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
1052 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
1055 // If MaxAddress is not aligned to the end of a page
1059 // Change MaxAddress to be 1 page lower
1061 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
1064 // Set MaxAddress to a page boundary
1066 MaxAddress
&= ~(UINT64
)EFI_PAGE_MASK
;
1069 // Set MaxAddress to end of the page
1071 MaxAddress
|= EFI_PAGE_MASK
;
1074 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1077 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1078 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1081 // If it's not a free entry, don't bother with it
1083 if (Entry
->Type
!= EfiConventionalMemory
) {
1087 DescStart
= Entry
->Start
;
1088 DescEnd
= Entry
->End
;
1091 // If desc is past max allowed address or below min allowed address, skip it
1093 if ((DescStart
>= MaxAddress
) || (DescEnd
< MinAddress
)) {
1098 // If desc ends past max allowed address, clip the end
1100 if (DescEnd
>= MaxAddress
) {
1101 DescEnd
= MaxAddress
;
1104 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
1106 // Skip if DescEnd is less than DescStart after alignment clipping
1107 if (DescEnd
< DescStart
) {
1112 // Compute the number of bytes we can used from this
1113 // descriptor, and see it's enough to satisfy the request
1115 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
1117 if (DescNumberOfBytes
>= NumberOfBytes
) {
1119 // If the start of the allocated range is below the min address allowed, skip it
1121 if ((DescEnd
- NumberOfBytes
+ 1) < MinAddress
) {
1126 // If this is the best match so far remember it
1128 if (DescEnd
> Target
) {
1130 DescEnd
= AdjustMemoryS (
1131 DescEnd
+ 1 - DescNumberOfBytes
,
1146 // If this is a grow down, adjust target to be the allocation base
1148 Target
-= NumberOfBytes
- 1;
1151 // If we didn't find a match, return 0
1153 if ((Target
& EFI_PAGE_MASK
) != 0) {
1162 Internal function. Finds a consecutive free page range below
1163 the requested address
1165 @param MaxAddress The address that the range must be below
1166 @param NoPages Number of pages needed
1167 @param NewType The type of memory the range is going to be
1169 @param Alignment Bits to align with
1170 @param NeedGuard Flag to indicate Guard page is needed or not
1172 @return The base address of the range, or 0 if the range was not found.
1177 IN UINT64 MaxAddress
,
1179 IN EFI_MEMORY_TYPE NewType
,
1181 IN BOOLEAN NeedGuard
1187 // Attempt to find free pages in the preferred bin based on the requested memory type
1189 if ((UINT32
)NewType
< EfiMaxMemoryType
&& MaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1190 Start
= CoreFindFreePagesI (
1191 mMemoryTypeStatistics
[NewType
].MaximumAddress
,
1192 mMemoryTypeStatistics
[NewType
].BaseAddress
,
1204 // Attempt to find free pages in the default allocation bin
1206 if (MaxAddress
>= mDefaultMaximumAddress
) {
1207 Start
= CoreFindFreePagesI (mDefaultMaximumAddress
, 0, NoPages
, NewType
,
1208 Alignment
, NeedGuard
);
1210 if (Start
< mDefaultBaseAddress
) {
1211 mDefaultBaseAddress
= Start
;
1218 // The allocation did not succeed in any of the prefered bins even after
1219 // promoting resources. Attempt to find free pages anywhere is the requested
1220 // address range. If this allocation fails, then there are not enough
1221 // resources anywhere to satisfy the request.
1223 Start
= CoreFindFreePagesI (MaxAddress
, 0, NoPages
, NewType
, Alignment
,
1230 // If allocations from the preferred bins fail, then attempt to promote memory resources.
1232 if (!PromoteMemoryResource ()) {
1237 // If any memory resources were promoted, then re-attempt the allocation
1239 return FindFreePages (MaxAddress
, NoPages
, NewType
, Alignment
, NeedGuard
);
1244 Allocates pages from the memory map.
1246 @param Type The type of allocation to perform
1247 @param MemoryType The type of memory to turn the allocated pages
1249 @param NumberOfPages The number of pages to allocate
1250 @param Memory A pointer to receive the base allocated memory
1252 @param NeedGuard Flag to indicate Guard page is needed or not
1254 @return Status. On success, Memory is filled in with the base address allocated
1255 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1257 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1258 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1259 @retval EFI_SUCCESS Pages successfully allocated.
1264 CoreInternalAllocatePages (
1265 IN EFI_ALLOCATE_TYPE Type
,
1266 IN EFI_MEMORY_TYPE MemoryType
,
1267 IN UINTN NumberOfPages
,
1268 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
,
1269 IN BOOLEAN NeedGuard
1274 UINT64 NumberOfBytes
;
1279 if ((UINT32
)Type
>= MaxAllocateType
) {
1280 return EFI_INVALID_PARAMETER
;
1283 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
< MEMORY_TYPE_OEM_RESERVED_MIN
) ||
1284 (MemoryType
== EfiConventionalMemory
) || (MemoryType
== EfiPersistentMemory
)) {
1285 return EFI_INVALID_PARAMETER
;
1288 if (Memory
== NULL
) {
1289 return EFI_INVALID_PARAMETER
;
1292 Alignment
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
1294 if (MemoryType
== EfiACPIReclaimMemory
||
1295 MemoryType
== EfiACPIMemoryNVS
||
1296 MemoryType
== EfiRuntimeServicesCode
||
1297 MemoryType
== EfiRuntimeServicesData
) {
1299 Alignment
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
1302 if (Type
== AllocateAddress
) {
1303 if ((*Memory
& (Alignment
- 1)) != 0) {
1304 return EFI_NOT_FOUND
;
1308 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1309 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1312 // If this is for below a particular address, then
1317 // The max address is the max natively addressable address for the processor
1319 MaxAddress
= MAX_ADDRESS
;
1322 // Check for Type AllocateAddress,
1323 // if NumberOfPages is 0 or
1324 // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or
1325 // if (Start + NumberOfBytes) rolls over 0 or
1326 // if Start is above MAX_ADDRESS or
1327 // if End is above MAX_ADDRESS,
1328 // return EFI_NOT_FOUND.
1330 if (Type
== AllocateAddress
) {
1331 if ((NumberOfPages
== 0) ||
1332 (NumberOfPages
> RShiftU64 (MaxAddress
, EFI_PAGE_SHIFT
))) {
1333 return EFI_NOT_FOUND
;
1335 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
1336 End
= Start
+ NumberOfBytes
- 1;
1338 if ((Start
>= End
) ||
1339 (Start
> MaxAddress
) ||
1340 (End
> MaxAddress
)) {
1341 return EFI_NOT_FOUND
;
1345 if (Type
== AllocateMaxAddress
) {
1349 CoreAcquireMemoryLock ();
1352 // If not a specific address, then find an address to allocate
1354 if (Type
!= AllocateAddress
) {
1355 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
,
1358 Status
= EFI_OUT_OF_RESOURCES
;
1364 // Convert pages from FreeMemory to the requested type
1367 Status
= CoreConvertPagesWithGuard(Start
, NumberOfPages
, MemoryType
);
1369 Status
= CoreConvertPages(Start
, NumberOfPages
, MemoryType
);
1373 CoreReleaseMemoryLock ();
1375 if (!EFI_ERROR (Status
)) {
1377 SetGuardForMemory (Start
, NumberOfPages
);
1386 Allocates pages from the memory map.
1388 @param Type The type of allocation to perform
1389 @param MemoryType The type of memory to turn the allocated pages
1391 @param NumberOfPages The number of pages to allocate
1392 @param Memory A pointer to receive the base allocated memory
1395 @return Status. On success, Memory is filled in with the base address allocated
1396 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1398 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1399 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1400 @retval EFI_SUCCESS Pages successfully allocated.
1406 IN EFI_ALLOCATE_TYPE Type
,
1407 IN EFI_MEMORY_TYPE MemoryType
,
1408 IN UINTN NumberOfPages
,
1409 OUT EFI_PHYSICAL_ADDRESS
*Memory
1415 NeedGuard
= IsPageTypeToGuard (MemoryType
, Type
) && !mOnGuarding
;
1416 Status
= CoreInternalAllocatePages (Type
, MemoryType
, NumberOfPages
, Memory
,
1418 if (!EFI_ERROR (Status
)) {
1420 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1421 MemoryProfileActionAllocatePages
,
1423 EFI_PAGES_TO_SIZE (NumberOfPages
),
1424 (VOID
*) (UINTN
) *Memory
,
1427 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType
);
1428 ApplyMemoryProtectionPolicy (EfiConventionalMemory
, MemoryType
, *Memory
,
1429 EFI_PAGES_TO_SIZE (NumberOfPages
));
1435 Frees previous allocated pages.
1437 @param Memory Base address of memory being freed
1438 @param NumberOfPages The number of pages to free
1439 @param MemoryType Pointer to memory type
1441 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1442 @retval EFI_INVALID_PARAMETER Address not aligned
1443 @return EFI_SUCCESS -Pages successfully freed.
1448 CoreInternalFreePages (
1449 IN EFI_PHYSICAL_ADDRESS Memory
,
1450 IN UINTN NumberOfPages
,
1451 OUT EFI_MEMORY_TYPE
*MemoryType OPTIONAL
1463 CoreAcquireMemoryLock ();
1466 // Find the entry that the covers the range
1470 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1471 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1472 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1476 if (Link
== &gMemoryMap
) {
1477 Status
= EFI_NOT_FOUND
;
1481 Alignment
= DEFAULT_PAGE_ALLOCATION_GRANULARITY
;
1483 ASSERT (Entry
!= NULL
);
1484 if (Entry
->Type
== EfiACPIReclaimMemory
||
1485 Entry
->Type
== EfiACPIMemoryNVS
||
1486 Entry
->Type
== EfiRuntimeServicesCode
||
1487 Entry
->Type
== EfiRuntimeServicesData
) {
1489 Alignment
= RUNTIME_PAGE_ALLOCATION_GRANULARITY
;
1493 if ((Memory
& (Alignment
- 1)) != 0) {
1494 Status
= EFI_INVALID_PARAMETER
;
1498 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1499 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1501 if (MemoryType
!= NULL
) {
1502 *MemoryType
= Entry
->Type
;
1505 IsGuarded
= IsPageTypeToGuard (Entry
->Type
, AllocateAnyPages
) &&
1506 IsMemoryGuarded (Memory
);
1508 Status
= CoreConvertPagesWithGuard (Memory
, NumberOfPages
,
1509 EfiConventionalMemory
);
1511 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1515 CoreReleaseMemoryLock ();
1517 UnsetGuardForMemory(Memory
, NumberOfPages
);
1523 Frees previous allocated pages.
1525 @param Memory Base address of memory being freed
1526 @param NumberOfPages The number of pages to free
1528 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1529 @retval EFI_INVALID_PARAMETER Address not aligned
1530 @return EFI_SUCCESS -Pages successfully freed.
1536 IN EFI_PHYSICAL_ADDRESS Memory
,
1537 IN UINTN NumberOfPages
1541 EFI_MEMORY_TYPE MemoryType
;
1543 Status
= CoreInternalFreePages (Memory
, NumberOfPages
, &MemoryType
);
1544 if (!EFI_ERROR (Status
)) {
1546 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1547 MemoryProfileActionFreePages
,
1549 EFI_PAGES_TO_SIZE (NumberOfPages
),
1550 (VOID
*) (UINTN
) Memory
,
1553 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType
);
1554 ApplyMemoryProtectionPolicy (MemoryType
, EfiConventionalMemory
, Memory
,
1555 EFI_PAGES_TO_SIZE (NumberOfPages
));
1561 This function checks to see if the last memory map descriptor in a memory map
1562 can be merged with any of the other memory map descriptors in a memorymap.
1563 Memory descriptors may be merged if they are adjacent and have the same type
1566 @param MemoryMap A pointer to the start of the memory map.
1567 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.
1568 @param DescriptorSize The size, in bytes, of an individual
1569 EFI_MEMORY_DESCRIPTOR.
1571 @return A pointer to the next available descriptor in MemoryMap
1574 EFI_MEMORY_DESCRIPTOR
*
1575 MergeMemoryMapDescriptor (
1576 IN EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1577 IN EFI_MEMORY_DESCRIPTOR
*MemoryMapDescriptor
,
1578 IN UINTN DescriptorSize
1582 // Traverse the array of descriptors in MemoryMap
1584 for (; MemoryMap
!= MemoryMapDescriptor
; MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, DescriptorSize
)) {
1586 // Check to see if the Type fields are identical.
1588 if (MemoryMap
->Type
!= MemoryMapDescriptor
->Type
) {
1593 // Check to see if the Attribute fields are identical.
1595 if (MemoryMap
->Attribute
!= MemoryMapDescriptor
->Attribute
) {
1600 // Check to see if MemoryMapDescriptor is immediately above MemoryMap
1602 if (MemoryMap
->PhysicalStart
+ EFI_PAGES_TO_SIZE ((UINTN
)MemoryMap
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1604 // Merge MemoryMapDescriptor into MemoryMap
1606 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1609 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1611 return MemoryMapDescriptor
;
1615 // Check to see if MemoryMapDescriptor is immediately below MemoryMap
1617 if (MemoryMap
->PhysicalStart
- EFI_PAGES_TO_SIZE ((UINTN
)MemoryMapDescriptor
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1619 // Merge MemoryMapDescriptor into MemoryMap
1621 MemoryMap
->PhysicalStart
= MemoryMapDescriptor
->PhysicalStart
;
1622 MemoryMap
->VirtualStart
= MemoryMapDescriptor
->VirtualStart
;
1623 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1626 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1628 return MemoryMapDescriptor
;
1633 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.
1635 // Return the slot immediately after MemoryMapDescriptor as the next available
1636 // slot in the MemoryMap array
1638 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor
, DescriptorSize
);
1642 This function returns a copy of the current memory map. The map is an array of
1643 memory descriptors, each of which describes a contiguous block of memory.
1645 @param MemoryMapSize A pointer to the size, in bytes, of the
1646 MemoryMap buffer. On input, this is the size of
1647 the buffer allocated by the caller. On output,
1648 it is the size of the buffer returned by the
1649 firmware if the buffer was large enough, or the
1650 size of the buffer needed to contain the map if
1651 the buffer was too small.
1652 @param MemoryMap A pointer to the buffer in which firmware places
1653 the current memory map.
1654 @param MapKey A pointer to the location in which firmware
1655 returns the key for the current memory map.
1656 @param DescriptorSize A pointer to the location in which firmware
1657 returns the size, in bytes, of an individual
1658 EFI_MEMORY_DESCRIPTOR.
1659 @param DescriptorVersion A pointer to the location in which firmware
1660 returns the version number associated with the
1661 EFI_MEMORY_DESCRIPTOR.
1663 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1665 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1666 buffer size needed to hold the memory map is
1667 returned in MemoryMapSize.
1668 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1674 IN OUT UINTN
*MemoryMapSize
,
1675 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1677 OUT UINTN
*DescriptorSize
,
1678 OUT UINT32
*DescriptorVersion
1684 UINTN NumberOfEntries
;
1687 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1688 EFI_GCD_MAP_ENTRY MergeGcdMapEntry
;
1689 EFI_MEMORY_TYPE Type
;
1690 EFI_MEMORY_DESCRIPTOR
*MemoryMapStart
;
1691 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
1694 // Make sure the parameters are valid
1696 if (MemoryMapSize
== NULL
) {
1697 return EFI_INVALID_PARAMETER
;
1700 CoreAcquireGcdMemoryLock ();
1703 // Count the number of Reserved and runtime MMIO entries
1704 // And, count the number of Persistent entries.
1706 NumberOfEntries
= 0;
1707 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1708 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1709 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypePersistent
) ||
1710 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1711 ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
1712 ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
))) {
1717 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1720 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1721 // prevent people from having pointer math bugs in their code.
1722 // now you have to use *DescriptorSize to make things work.
1724 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1726 if (DescriptorSize
!= NULL
) {
1727 *DescriptorSize
= Size
;
1730 if (DescriptorVersion
!= NULL
) {
1731 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1734 CoreAcquireMemoryLock ();
1737 // Compute the buffer size needed to fit the entire map
1739 BufferSize
= Size
* NumberOfEntries
;
1740 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1744 if (*MemoryMapSize
< BufferSize
) {
1745 Status
= EFI_BUFFER_TOO_SMALL
;
1749 if (MemoryMap
== NULL
) {
1750 Status
= EFI_INVALID_PARAMETER
;
1757 ZeroMem (MemoryMap
, BufferSize
);
1758 MemoryMapStart
= MemoryMap
;
1759 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1760 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1761 ASSERT (Entry
->VirtualStart
== 0);
1764 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1766 MemoryMap
->Type
= Entry
->Type
;
1767 MemoryMap
->PhysicalStart
= Entry
->Start
;
1768 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1769 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1771 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1772 // memory type bin and needs to be converted to the same memory type as the rest of the
1773 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1774 // improves the chances for a successful S4 resume in the presence of minor page allocation
1775 // differences across reboots.
1777 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1778 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1779 if (mMemoryTypeStatistics
[Type
].Special
&&
1780 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1781 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1782 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1783 MemoryMap
->Type
= Type
;
1787 MemoryMap
->Attribute
= Entry
->Attribute
;
1788 if (MemoryMap
->Type
< EfiMaxMemoryType
) {
1789 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1790 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1795 // Check to see if the new Memory Map Descriptor can be merged with an
1796 // existing descriptor if they are adjacent and have the same attributes
1798 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1802 ZeroMem (&MergeGcdMapEntry
, sizeof (MergeGcdMapEntry
));
1804 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; ; Link
= Link
->ForwardLink
) {
1805 if (Link
!= &mGcdMemorySpaceMap
) {
1807 // Merge adjacent same type and attribute GCD memory range
1809 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1811 if ((MergeGcdMapEntry
.Capabilities
== GcdMapEntry
->Capabilities
) &&
1812 (MergeGcdMapEntry
.Attributes
== GcdMapEntry
->Attributes
) &&
1813 (MergeGcdMapEntry
.GcdMemoryType
== GcdMapEntry
->GcdMemoryType
) &&
1814 (MergeGcdMapEntry
.GcdIoType
== GcdMapEntry
->GcdIoType
)) {
1815 MergeGcdMapEntry
.EndAddress
= GcdMapEntry
->EndAddress
;
1820 if ((MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1821 ((MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
1822 ((MergeGcdMapEntry
.Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
))) {
1824 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1825 // it will be recorded as page PhysicalStart and NumberOfPages.
1827 ASSERT ((MergeGcdMapEntry
.BaseAddress
& EFI_PAGE_MASK
) == 0);
1828 ASSERT (((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1) & EFI_PAGE_MASK
) == 0);
1831 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries
1833 MemoryMap
->PhysicalStart
= MergeGcdMapEntry
.BaseAddress
;
1834 MemoryMap
->VirtualStart
= 0;
1835 MemoryMap
->NumberOfPages
= RShiftU64 ((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1836 MemoryMap
->Attribute
= (MergeGcdMapEntry
.Attributes
& ~EFI_MEMORY_PORT_IO
) |
1837 (MergeGcdMapEntry
.Capabilities
& (EFI_MEMORY_RP
| EFI_MEMORY_WP
| EFI_MEMORY_XP
| EFI_MEMORY_RO
|
1838 EFI_MEMORY_UC
| EFI_MEMORY_UCE
| EFI_MEMORY_WC
| EFI_MEMORY_WT
| EFI_MEMORY_WB
));
1840 if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1841 MemoryMap
->Type
= EfiReservedMemoryType
;
1842 } else if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1843 if ((MergeGcdMapEntry
.Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1844 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1846 MemoryMap
->Type
= EfiMemoryMappedIO
;
1851 // Check to see if the new Memory Map Descriptor can be merged with an
1852 // existing descriptor if they are adjacent and have the same attributes
1854 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1857 if (MergeGcdMapEntry
.GcdMemoryType
== EfiGcdMemoryTypePersistent
) {
1859 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1860 // it will be recorded as page PhysicalStart and NumberOfPages.
1862 ASSERT ((MergeGcdMapEntry
.BaseAddress
& EFI_PAGE_MASK
) == 0);
1863 ASSERT (((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1) & EFI_PAGE_MASK
) == 0);
1866 // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries
1868 MemoryMap
->PhysicalStart
= MergeGcdMapEntry
.BaseAddress
;
1869 MemoryMap
->VirtualStart
= 0;
1870 MemoryMap
->NumberOfPages
= RShiftU64 ((MergeGcdMapEntry
.EndAddress
- MergeGcdMapEntry
.BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1871 MemoryMap
->Attribute
= MergeGcdMapEntry
.Attributes
| EFI_MEMORY_NV
|
1872 (MergeGcdMapEntry
.Capabilities
& (EFI_MEMORY_RP
| EFI_MEMORY_WP
| EFI_MEMORY_XP
| EFI_MEMORY_RO
|
1873 EFI_MEMORY_UC
| EFI_MEMORY_UCE
| EFI_MEMORY_WC
| EFI_MEMORY_WT
| EFI_MEMORY_WB
));
1874 MemoryMap
->Type
= EfiPersistentMemory
;
1877 // Check to see if the new Memory Map Descriptor can be merged with an
1878 // existing descriptor if they are adjacent and have the same attributes
1880 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1882 if (Link
== &mGcdMemorySpaceMap
) {
1884 // break loop when arrive at head.
1888 if (GcdMapEntry
!= NULL
) {
1890 // Copy new GCD map entry for the following GCD range merge
1892 CopyMem (&MergeGcdMapEntry
, GcdMapEntry
, sizeof (MergeGcdMapEntry
));
1897 // Compute the size of the buffer actually used after all memory map descriptor merge operations
1899 BufferSize
= ((UINT8
*)MemoryMap
- (UINT8
*)MemoryMapStart
);
1902 // Note: Some OSs will treat EFI_MEMORY_DESCRIPTOR.Attribute as really
1903 // set attributes and change memory paging attribute accordingly.
1904 // But current EFI_MEMORY_DESCRIPTOR.Attribute is assigned by
1905 // value from Capabilities in GCD memory map. This might cause
1906 // boot problems. Clearing all paging related capabilities can
1907 // workaround it. Following code is supposed to be removed once
1908 // the usage of EFI_MEMORY_DESCRIPTOR.Attribute is clarified in
1909 // UEFI spec and adopted by both EDK-II Core and all supported
1912 MemoryMapEnd
= MemoryMap
;
1913 MemoryMap
= MemoryMapStart
;
1914 while (MemoryMap
< MemoryMapEnd
) {
1915 MemoryMap
->Attribute
&= ~(UINT64
)(EFI_MEMORY_RP
| EFI_MEMORY_RO
|
1917 MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, Size
);
1919 MergeMemoryMap (MemoryMapStart
, &BufferSize
, Size
);
1920 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)MemoryMapStart
+ BufferSize
);
1922 Status
= EFI_SUCCESS
;
1926 // Update the map key finally
1928 if (MapKey
!= NULL
) {
1929 *MapKey
= mMemoryMapKey
;
1932 CoreReleaseMemoryLock ();
1934 CoreReleaseGcdMemoryLock ();
1936 *MemoryMapSize
= BufferSize
;
1939 if (PcdGet8 (PcdHeapGuardPropertyMask
) & (BIT1
|BIT0
)) {
1940 DumpGuardedMemoryBitmap ();
1949 Internal function. Used by the pool functions to allocate pages
1950 to back pool allocation requests.
1952 @param PoolType The type of memory for the new pool pages
1953 @param NumberOfPages No of pages to allocate
1954 @param Alignment Bits to align.
1955 @param NeedGuard Flag to indicate Guard page is needed or not
1957 @return The allocated memory, or NULL
1961 CoreAllocatePoolPages (
1962 IN EFI_MEMORY_TYPE PoolType
,
1963 IN UINTN NumberOfPages
,
1965 IN BOOLEAN NeedGuard
1971 // Find the pages to convert
1973 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
,
1977 // Convert it to boot services data
1980 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1983 CoreConvertPagesWithGuard (Start
, NumberOfPages
, PoolType
);
1985 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1989 return (VOID
*)(UINTN
) Start
;
1994 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1996 @param Memory The base address to free
1997 @param NumberOfPages The number of pages to free
2002 IN EFI_PHYSICAL_ADDRESS Memory
,
2003 IN UINTN NumberOfPages
2006 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
2012 Make sure the memory map is following all the construction rules,
2013 it is the last time to check memory map error before exit boot services.
2015 @param MapKey Memory map key
2017 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
2019 @retval EFI_SUCCESS Valid memory map.
2023 CoreTerminateMemoryMap (
2031 Status
= EFI_SUCCESS
;
2033 CoreAcquireMemoryLock ();
2035 if (MapKey
== mMemoryMapKey
) {
2038 // Make sure the memory map is following all the construction rules
2039 // This is the last chance we will be able to display any messages on
2040 // the console devices.
2043 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
2044 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
2045 if (Entry
->Type
< EfiMaxMemoryType
) {
2046 if (mMemoryTypeStatistics
[Entry
->Type
].Runtime
) {
2047 ASSERT (Entry
->Type
!= EfiACPIReclaimMemory
);
2048 ASSERT (Entry
->Type
!= EfiACPIMemoryNVS
);
2049 if ((Entry
->Start
& (RUNTIME_PAGE_ALLOCATION_GRANULARITY
- 1)) != 0) {
2050 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
2051 Status
= EFI_INVALID_PARAMETER
;
2054 if (((Entry
->End
+ 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY
- 1)) != 0) {
2055 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
2056 Status
= EFI_INVALID_PARAMETER
;
2064 // The map key they gave us matches what we expect. Fall through and
2065 // return success. In an ideal world we would clear out all of
2066 // EfiBootServicesCode and EfiBootServicesData. However this function
2067 // is not the last one called by ExitBootServices(), so we have to
2068 // preserve the memory contents.
2071 Status
= EFI_INVALID_PARAMETER
;
2075 CoreReleaseMemoryLock ();