2 UEFI Memory page management functions.
4 Copyright (c) 2007 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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_STAISTICS
;
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_STAISTICS 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
;
75 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
76 { EfiReservedMemoryType
, 0 },
79 { EfiBootServicesCode
, 0 },
80 { EfiBootServicesData
, 0 },
81 { EfiRuntimeServicesCode
, 0 },
82 { EfiRuntimeServicesData
, 0 },
83 { EfiConventionalMemory
, 0 },
84 { EfiUnusableMemory
, 0 },
85 { EfiACPIReclaimMemory
, 0 },
86 { EfiACPIMemoryNVS
, 0 },
87 { EfiMemoryMappedIO
, 0 },
88 { EfiMemoryMappedIOPortSpace
, 0 },
90 { EfiMaxMemoryType
, 0 }
93 // Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated
94 // and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a
95 // address assigned by DXE core.
97 GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady
= FALSE
;
100 Enter critical section by gaining lock on gMemoryLock.
104 CoreAcquireMemoryLock (
108 CoreAcquireLock (&gMemoryLock
);
114 Exit critical section by releasing lock on gMemoryLock.
118 CoreReleaseMemoryLock (
122 CoreReleaseLock (&gMemoryLock
);
129 Internal function. Removes a descriptor entry.
131 @param Entry The entry to remove
135 RemoveMemoryMapEntry (
136 IN OUT MEMORY_MAP
*Entry
139 RemoveEntryList (&Entry
->Link
);
140 Entry
->Link
.ForwardLink
= NULL
;
142 if (Entry
->FromPages
) {
144 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
146 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
151 Internal function. Adds a ranges to the memory map.
152 The range must not already exist in the map.
154 @param Type The type of memory range to add
155 @param Start The starting address in the memory range Must be
157 @param End The last address in the range Must be the last
159 @param Attribute The attributes of the memory range to add
164 IN EFI_MEMORY_TYPE Type
,
165 IN EFI_PHYSICAL_ADDRESS Start
,
166 IN EFI_PHYSICAL_ADDRESS End
,
173 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
174 ASSERT (End
> Start
) ;
176 ASSERT_LOCKED (&gMemoryLock
);
178 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
181 // Memory map being altered so updated key
186 // UEFI 2.0 added an event group for notificaiton on memory map changes.
187 // So we need to signal this Event Group every time the memory map changes.
188 // If we are in EFI 1.10 compatability mode no event groups will be
189 // found and nothing will happen we we call this function. These events
190 // will get signaled but since a lock is held around the call to this
191 // function the notificaiton events will only be called after this funciton
192 // returns and the lock is released.
194 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
197 // Look for adjoining memory descriptor
200 // Two memory descriptors can only be merged if they have the same Type
201 // and the same Attribute
204 Link
= gMemoryMap
.ForwardLink
;
205 while (Link
!= &gMemoryMap
) {
206 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
207 Link
= Link
->ForwardLink
;
209 if (Entry
->Type
!= Type
) {
213 if (Entry
->Attribute
!= Attribute
) {
217 if (Entry
->End
+ 1 == Start
) {
219 Start
= Entry
->Start
;
220 RemoveMemoryMapEntry (Entry
);
222 } else if (Entry
->Start
== End
+ 1) {
225 RemoveMemoryMapEntry (Entry
);
233 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
234 mMapStack
[mMapDepth
].FromPages
= FALSE
;
235 mMapStack
[mMapDepth
].Type
= Type
;
236 mMapStack
[mMapDepth
].Start
= Start
;
237 mMapStack
[mMapDepth
].End
= End
;
238 mMapStack
[mMapDepth
].VirtualStart
= 0;
239 mMapStack
[mMapDepth
].Attribute
= Attribute
;
240 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
243 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
249 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
250 If the list is emtry, then allocate a new page to refuel the list.
251 Please Note this algorithm to allocate the memory map descriptor has a property
252 that the memory allocated for memory entries always grows, and will never really be freed
253 For example, if the current boot uses 2000 memory map entries at the maximum point, but
254 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
255 memory map entries is still allocated from EfiBootServicesMemory.
258 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
262 AllocateMemoryMapEntry (
266 MEMORY_MAP
* FreeDescriptorEntries
;
270 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
272 // The list is empty, to allocate one page to refuel the list
274 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
275 if(FreeDescriptorEntries
!= NULL
) {
277 // Enque the free memmory map entries into the list
279 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
280 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
281 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
288 // dequeue the first descriptor from the list
290 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
291 RemoveEntryList (&Entry
->Link
);
298 Internal function. Moves any memory descriptors that are on the
299 temporary descriptor stack to heap.
303 CoreFreeMemoryMapStack (
311 ASSERT_LOCKED (&gMemoryLock
);
314 // If already freeing the map stack, then return
316 if (mFreeMapStack
!= 0) {
321 // Move the temporary memory descriptor stack into pool
325 while (mMapDepth
!= 0) {
327 // Deque an memory map entry from mFreeMemoryMapEntryList
329 Entry
= AllocateMemoryMapEntry ();
334 // Update to proper entry
338 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
341 // Move this entry to general memory
343 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
344 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
346 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
347 Entry
->FromPages
= TRUE
;
350 // Find insertion location
352 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
353 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
354 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
359 InsertTailList (Link2
, &Entry
->Link
);
363 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
364 // so here no need to move it to memory.
366 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
374 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
378 PromoteMemoryResource (
383 EFI_GCD_MAP_ENTRY
*Entry
;
385 DEBUG ((DEBUG_PAGE
, "Promote the memory resource\n"));
387 CoreAcquireGcdMemoryLock ();
389 Link
= mGcdMemorySpaceMap
.ForwardLink
;
390 while (Link
!= &mGcdMemorySpaceMap
) {
392 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
394 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
395 Entry
->EndAddress
< MAX_ADDRESS
&&
396 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
397 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
399 // Update the GCD map
401 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
402 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
403 Entry
->ImageHandle
= gDxeCoreImageHandle
;
404 Entry
->DeviceHandle
= NULL
;
407 // Add to allocable system memory resource
411 EfiConventionalMemory
,
414 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
416 CoreFreeMemoryMapStack ();
420 Link
= Link
->ForwardLink
;
423 CoreReleaseGcdMemoryLock ();
428 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD
429 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the
430 size of boot time and runtime code.
434 CoreLoadingFixedAddressHook (
438 UINT32 RuntimeCodePageNumber
;
439 UINT32 BootTimeCodePageNumber
;
440 EFI_PHYSICAL_ADDRESS RuntimeCodeBase
;
441 EFI_PHYSICAL_ADDRESS BootTimeCodeBase
;
445 // Make sure these 2 areas are not initialzied.
447 if (!gLoadFixedAddressCodeMemoryReady
) {
448 RuntimeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
449 BootTimeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
450 RuntimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- EFI_PAGES_TO_SIZE (RuntimeCodePageNumber
));
451 BootTimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(RuntimeCodeBase
- EFI_PAGES_TO_SIZE (BootTimeCodePageNumber
));
453 // Try to allocate runtime memory.
455 Status
= CoreAllocatePages (
457 EfiRuntimeServicesCode
,
458 RuntimeCodePageNumber
,
461 if (EFI_ERROR(Status
)) {
463 // Runtime memory allocation failed
468 // Try to allocate boot memory.
470 Status
= CoreAllocatePages (
473 BootTimeCodePageNumber
,
476 if (EFI_ERROR(Status
)) {
478 // boot memory allocation failed. Free Runtime code range and will try the allocation again when
479 // new memory range is installed.
483 RuntimeCodePageNumber
487 gLoadFixedAddressCodeMemoryReady
= TRUE
;
493 Called to initialize the memory map and add descriptors to
494 the current descriptor list.
495 The first descriptor that is added must be general usable
496 memory as the addition allocates heap.
498 @param Type The type of memory to add
499 @param Start The starting address in the memory range Must be
501 @param NumberOfPages The number of pages in the range
502 @param Attribute Attributes of the memory to add
504 @return None. The range is added to the memory map
508 CoreAddMemoryDescriptor (
509 IN EFI_MEMORY_TYPE Type
,
510 IN EFI_PHYSICAL_ADDRESS Start
,
511 IN UINT64 NumberOfPages
,
515 EFI_PHYSICAL_ADDRESS End
;
520 if ((Start
& EFI_PAGE_MASK
) != 0) {
524 if (Type
>= EfiMaxMemoryType
&& Type
<= 0x7fffffff) {
527 CoreAcquireMemoryLock ();
528 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
529 CoreAddRange (Type
, Start
, End
, Attribute
);
530 CoreFreeMemoryMapStack ();
531 CoreReleaseMemoryLock ();
534 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type
536 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
537 CoreLoadingFixedAddressHook();
541 // Check to see if the statistics for the different memory types have already been established
543 if (mMemoryTypeInformationInitialized
) {
549 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
551 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
553 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
555 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
556 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
559 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
561 // Allocate pages for the current memory type from the top of available memory
563 Status
= CoreAllocatePages (
566 gMemoryTypeInformation
[Index
].NumberOfPages
,
567 &mMemoryTypeStatistics
[Type
].BaseAddress
569 if (EFI_ERROR (Status
)) {
571 // If an error occurs allocating the pages for the current memory type, then
572 // free all the pages allocates for the previous memory types and return. This
573 // operation with be retied when/if more memory is added to the system
575 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
577 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
579 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
580 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
584 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
586 mMemoryTypeStatistics
[Type
].BaseAddress
,
587 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
589 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
590 mMemoryTypeStatistics
[Type
].MaximumAddress
= MAX_ADDRESS
;
597 // Compute the address at the top of the current statistics
599 mMemoryTypeStatistics
[Type
].MaximumAddress
=
600 mMemoryTypeStatistics
[Type
].BaseAddress
+
601 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
604 // If the current base address is the lowest address so far, then update the default
607 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
608 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
614 // There was enough system memory for all the the memory types were allocated. So,
615 // those memory areas can be freed for future allocations, and all future memory
616 // allocations can occur within their respective bins
618 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
620 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
622 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
623 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
626 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
628 mMemoryTypeStatistics
[Type
].BaseAddress
,
629 gMemoryTypeInformation
[Index
].NumberOfPages
631 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
632 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
637 // If the number of pages reserved for a memory type is 0, then all allocations for that type
638 // should be in the default range.
640 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
641 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
642 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
643 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
646 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
647 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== MAX_ADDRESS
) {
648 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
652 mMemoryTypeInformationInitialized
= TRUE
;
657 Internal function. Converts a memory range to the specified type.
658 The range must exist in the memory map.
660 @param Start The first address of the range Must be page
662 @param NumberOfPages The number of pages to convert
663 @param NewType The new type for the memory range
665 @retval EFI_INVALID_PARAMETER Invalid parameter
666 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
667 range or convertion not allowed.
668 @retval EFI_SUCCESS Successfully converts the memory range to the
675 IN UINT64 NumberOfPages
,
676 IN EFI_MEMORY_TYPE NewType
680 UINT64 NumberOfBytes
;
688 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
689 End
= Start
+ NumberOfBytes
- 1;
691 ASSERT (NumberOfPages
);
692 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
693 ASSERT (End
> Start
) ;
694 ASSERT_LOCKED (&gMemoryLock
);
696 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
> (Start
+ NumberOfBytes
))) {
697 return EFI_INVALID_PARAMETER
;
701 // Convert the entire range
704 while (Start
< End
) {
707 // Find the entry that the covers the range
709 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
710 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
712 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
717 if (Link
== &gMemoryMap
) {
718 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
719 return EFI_NOT_FOUND
;
723 // Convert range to the end, or to the end of the descriptor
724 // if that's all we've got
728 ASSERT (Entry
!= NULL
);
729 if (Entry
->End
< End
) {
730 RangeEnd
= Entry
->End
;
733 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to %d\n", Start
, RangeEnd
, NewType
));
736 // Debug code - verify conversion is allowed
738 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
739 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types\n"));
740 return EFI_NOT_FOUND
;
744 // Update counters for the number of pages allocated to each memory type
746 if (Entry
->Type
>= 0 && Entry
->Type
< EfiMaxMemoryType
) {
747 if (Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&&
748 Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) {
749 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
750 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
752 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
757 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
) {
758 if (Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
759 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
760 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
>
761 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
762 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
768 // Pull range out of descriptor
770 if (Entry
->Start
== Start
) {
775 Entry
->Start
= RangeEnd
+ 1;
777 } else if (Entry
->End
== RangeEnd
) {
782 Entry
->End
= Start
- 1;
787 // Pull it out of the center, clip current
793 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
794 mMapStack
[mMapDepth
].FromPages
= FALSE
;
795 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
796 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
797 mMapStack
[mMapDepth
].End
= Entry
->End
;
800 // Inherit Attribute from the Memory Descriptor that is being clipped
802 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
804 Entry
->End
= Start
- 1;
805 ASSERT (Entry
->Start
< Entry
->End
);
807 Entry
= &mMapStack
[mMapDepth
];
808 InsertTailList (&gMemoryMap
, &Entry
->Link
);
811 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
815 // The new range inherits the same Attribute as the Entry
816 //it is being cut out of
818 Attribute
= Entry
->Attribute
;
821 // If the descriptor is empty, then remove it from the map
823 if (Entry
->Start
== Entry
->End
+ 1) {
824 RemoveMemoryMapEntry (Entry
);
829 // Add our new range in
831 CoreAddRange (NewType
, Start
, RangeEnd
, Attribute
);
834 // Move any map descriptor stack to general pool
836 CoreFreeMemoryMapStack ();
839 // Bump the starting address, and convert the next range
841 Start
= RangeEnd
+ 1;
845 // Converted the whole range, done
854 Internal function. Finds a consecutive free page range below
855 the requested address.
857 @param MaxAddress The address that the range must be below
858 @param NumberOfPages Number of pages needed
859 @param NewType The type of memory the range is going to be
861 @param Alignment Bits to align with
863 @return The base address of the range, or 0 if the range was not found
868 IN UINT64 MaxAddress
,
869 IN UINT64 NumberOfPages
,
870 IN EFI_MEMORY_TYPE NewType
,
874 UINT64 NumberOfBytes
;
878 UINT64 DescNumberOfBytes
;
882 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
886 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
889 // If MaxAddress is not aligned to the end of a page
893 // Change MaxAddress to be 1 page lower
895 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
898 // Set MaxAddress to a page boundary
900 MaxAddress
&= ~EFI_PAGE_MASK
;
903 // Set MaxAddress to end of the page
905 MaxAddress
|= EFI_PAGE_MASK
;
908 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
911 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
912 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
915 // If it's not a free entry, don't bother with it
917 if (Entry
->Type
!= EfiConventionalMemory
) {
921 DescStart
= Entry
->Start
;
922 DescEnd
= Entry
->End
;
925 // If desc is past max allowed address, skip it
927 if (DescStart
>= MaxAddress
) {
932 // If desc ends past max allowed address, clip the end
934 if (DescEnd
>= MaxAddress
) {
935 DescEnd
= MaxAddress
;
938 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
941 // Compute the number of bytes we can used from this
942 // descriptor, and see it's enough to satisfy the request
944 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
946 if (DescNumberOfBytes
>= NumberOfBytes
) {
949 // If this is the best match so far remember it
951 if (DescEnd
> Target
) {
958 // If this is a grow down, adjust target to be the allocation base
960 Target
-= NumberOfBytes
- 1;
963 // If we didn't find a match, return 0
965 if ((Target
& EFI_PAGE_MASK
) != 0) {
974 Internal function. Finds a consecutive free page range below
975 the requested address
977 @param MaxAddress The address that the range must be below
978 @param NoPages Number of pages needed
979 @param NewType The type of memory the range is going to be
981 @param Alignment Bits to align with
983 @return The base address of the range, or 0 if the range was not found.
988 IN UINT64 MaxAddress
,
990 IN EFI_MEMORY_TYPE NewType
,
994 UINT64 NewMaxAddress
;
997 NewMaxAddress
= MaxAddress
;
999 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
&& NewMaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1000 NewMaxAddress
= mMemoryTypeStatistics
[NewType
].MaximumAddress
;
1002 if (NewMaxAddress
> mDefaultMaximumAddress
) {
1003 NewMaxAddress
= mDefaultMaximumAddress
;
1007 Start
= CoreFindFreePagesI (NewMaxAddress
, NoPages
, NewType
, Alignment
);
1009 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1012 // Here means there may be no enough memory to use, so try to go through
1013 // all the memory descript to promote the untested memory directly
1015 PromoteMemoryResource ();
1018 // Allocate memory again after the memory resource re-arranged
1020 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1030 Allocates pages from the memory map.
1032 @param Type The type of allocation to perform
1033 @param MemoryType The type of memory to turn the allocated pages
1035 @param NumberOfPages The number of pages to allocate
1036 @param Memory A pointer to receive the base allocated memory
1039 @return Status. On success, Memory is filled in with the base address allocated
1040 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1042 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1043 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1044 @retval EFI_SUCCESS Pages successfully allocated.
1050 IN EFI_ALLOCATE_TYPE Type
,
1051 IN EFI_MEMORY_TYPE MemoryType
,
1052 IN UINTN NumberOfPages
,
1053 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1061 if (Type
< AllocateAnyPages
|| Type
>= (UINTN
) MaxAllocateType
) {
1062 return EFI_INVALID_PARAMETER
;
1065 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
1066 MemoryType
== EfiConventionalMemory
) {
1067 return EFI_INVALID_PARAMETER
;
1070 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1072 if (MemoryType
== EfiACPIReclaimMemory
||
1073 MemoryType
== EfiACPIMemoryNVS
||
1074 MemoryType
== EfiRuntimeServicesCode
||
1075 MemoryType
== EfiRuntimeServicesData
) {
1077 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1080 if (Type
== AllocateAddress
) {
1081 if ((*Memory
& (Alignment
- 1)) != 0) {
1082 return EFI_NOT_FOUND
;
1086 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1087 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1090 // If this is for below a particular address, then
1095 // The max address is the max natively addressable address for the processor
1097 MaxAddress
= MAX_ADDRESS
;
1099 if (Type
== AllocateMaxAddress
) {
1103 CoreAcquireMemoryLock ();
1106 // If not a specific address, then find an address to allocate
1108 if (Type
!= AllocateAddress
) {
1109 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1111 Status
= EFI_OUT_OF_RESOURCES
;
1117 // Convert pages from FreeMemory to the requested type
1119 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1122 CoreReleaseMemoryLock ();
1124 if (!EFI_ERROR (Status
)) {
1133 Frees previous allocated pages.
1135 @param Memory Base address of memory being freed
1136 @param NumberOfPages The number of pages to free
1138 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1139 @retval EFI_INVALID_PARAMETER Address not aligned
1140 @return EFI_SUCCESS -Pages successfully freed.
1146 IN EFI_PHYSICAL_ADDRESS Memory
,
1147 IN UINTN NumberOfPages
1158 CoreAcquireMemoryLock ();
1161 // Find the entry that the covers the range
1164 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1165 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1166 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1170 if (Link
== &gMemoryMap
) {
1171 Status
= EFI_NOT_FOUND
;
1175 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1177 ASSERT (Entry
!= NULL
);
1178 if (Entry
->Type
== EfiACPIReclaimMemory
||
1179 Entry
->Type
== EfiACPIMemoryNVS
||
1180 Entry
->Type
== EfiRuntimeServicesCode
||
1181 Entry
->Type
== EfiRuntimeServicesData
) {
1183 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1187 if ((Memory
& (Alignment
- 1)) != 0) {
1188 Status
= EFI_INVALID_PARAMETER
;
1192 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1193 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1195 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1197 if (EFI_ERROR (Status
)) {
1202 // Destroy the contents
1204 if (Memory
< MAX_ADDRESS
) {
1205 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
)Memory
, NumberOfPages
<< EFI_PAGE_SHIFT
);
1209 CoreReleaseMemoryLock ();
1215 This function returns a copy of the current memory map. The map is an array of
1216 memory descriptors, each of which describes a contiguous block of memory.
1218 @param MemoryMapSize A pointer to the size, in bytes, of the
1219 MemoryMap buffer. On input, this is the size of
1220 the buffer allocated by the caller. On output,
1221 it is the size of the buffer returned by the
1222 firmware if the buffer was large enough, or the
1223 size of the buffer needed to contain the map if
1224 the buffer was too small.
1225 @param MemoryMap A pointer to the buffer in which firmware places
1226 the current memory map.
1227 @param MapKey A pointer to the location in which firmware
1228 returns the key for the current memory map.
1229 @param DescriptorSize A pointer to the location in which firmware
1230 returns the size, in bytes, of an individual
1231 EFI_MEMORY_DESCRIPTOR.
1232 @param DescriptorVersion A pointer to the location in which firmware
1233 returns the version number associated with the
1234 EFI_MEMORY_DESCRIPTOR.
1236 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1238 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1239 buffer size needed to hold the memory map is
1240 returned in MemoryMapSize.
1241 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1247 IN OUT UINTN
*MemoryMapSize
,
1248 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1250 OUT UINTN
*DescriptorSize
,
1251 OUT UINT32
*DescriptorVersion
1257 UINTN NumberOfRuntimeEntries
;
1260 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1261 EFI_MEMORY_TYPE Type
;
1264 // Make sure the parameters are valid
1266 if (MemoryMapSize
== NULL
) {
1267 return EFI_INVALID_PARAMETER
;
1270 CoreAcquireGcdMemoryLock ();
1273 // Count the number of Reserved and MMIO entries that are marked for runtime use
1275 NumberOfRuntimeEntries
= 0;
1276 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1277 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1278 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1279 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1280 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1281 NumberOfRuntimeEntries
++;
1286 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1289 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1290 // prevent people from having pointer math bugs in their code.
1291 // now you have to use *DescriptorSize to make things work.
1293 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1295 if (DescriptorSize
!= NULL
) {
1296 *DescriptorSize
= Size
;
1299 if (DescriptorVersion
!= NULL
) {
1300 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1303 CoreAcquireMemoryLock ();
1306 // Compute the buffer size needed to fit the entire map
1308 BufferSize
= Size
* NumberOfRuntimeEntries
;
1309 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1313 if (*MemoryMapSize
< BufferSize
) {
1314 Status
= EFI_BUFFER_TOO_SMALL
;
1318 if (MemoryMap
== NULL
) {
1319 Status
= EFI_INVALID_PARAMETER
;
1326 ZeroMem (MemoryMap
, BufferSize
);
1327 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1328 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1329 ASSERT (Entry
->VirtualStart
== 0);
1332 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1334 MemoryMap
->Type
= Entry
->Type
;
1335 MemoryMap
->PhysicalStart
= Entry
->Start
;
1336 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1337 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1339 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1340 // memory type bin and needs to be converted to the same memory type as the rest of the
1341 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1342 // improves the chances for a successful S4 resume in the presence of minor page allocation
1343 // differences across reboots.
1345 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1346 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1347 if (mMemoryTypeStatistics
[Type
].Special
&&
1348 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1349 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1350 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1351 MemoryMap
->Type
= Type
;
1355 MemoryMap
->Attribute
= Entry
->Attribute
;
1356 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1357 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1360 MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, Size
);
1363 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1364 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1365 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1366 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1367 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1369 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries
1370 // that are marked for runtime use
1372 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1373 MemoryMap
->VirtualStart
= 0;
1374 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1375 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1377 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1378 MemoryMap
->Type
= EfiReservedMemoryType
;
1379 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1380 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1381 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1383 MemoryMap
->Type
= EfiMemoryMappedIO
;
1387 MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, Size
);
1392 Status
= EFI_SUCCESS
;
1396 CoreReleaseMemoryLock ();
1398 CoreReleaseGcdMemoryLock ();
1401 // Update the map key finally
1403 if (MapKey
!= NULL
) {
1404 *MapKey
= mMemoryMapKey
;
1407 *MemoryMapSize
= BufferSize
;
1414 Internal function. Used by the pool functions to allocate pages
1415 to back pool allocation requests.
1417 @param PoolType The type of memory for the new pool pages
1418 @param NumberOfPages No of pages to allocate
1419 @param Alignment Bits to align.
1421 @return The allocated memory, or NULL
1425 CoreAllocatePoolPages (
1426 IN EFI_MEMORY_TYPE PoolType
,
1427 IN UINTN NumberOfPages
,
1434 // Find the pages to convert
1436 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1439 // Convert it to boot services data
1442 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1444 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1447 return (VOID
*)(UINTN
) Start
;
1452 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1454 @param Memory The base address to free
1455 @param NumberOfPages The number of pages to free
1460 IN EFI_PHYSICAL_ADDRESS Memory
,
1461 IN UINTN NumberOfPages
1464 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1470 Make sure the memory map is following all the construction rules,
1471 it is the last time to check memory map error before exit boot services.
1473 @param MapKey Memory map key
1475 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1477 @retval EFI_SUCCESS Valid memory map.
1481 CoreTerminateMemoryMap (
1489 Status
= EFI_SUCCESS
;
1491 CoreAcquireMemoryLock ();
1493 if (MapKey
== mMemoryMapKey
) {
1496 // Make sure the memory map is following all the construction rules
1497 // This is the last chance we will be able to display any messages on
1498 // the console devices.
1501 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1502 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1503 if ((Entry
->Attribute
& EFI_MEMORY_RUNTIME
) != 0) {
1504 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1505 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1506 Status
= EFI_INVALID_PARAMETER
;
1509 if ((Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1510 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1511 Status
= EFI_INVALID_PARAMETER
;
1514 if (((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1515 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1516 Status
= EFI_INVALID_PARAMETER
;
1523 // The map key they gave us matches what we expect. Fall through and
1524 // return success. In an ideal world we would clear out all of
1525 // EfiBootServicesCode and EfiBootServicesData. However this function
1526 // is not the last one called by ExitBootServices(), so we have to
1527 // preserve the memory contents.
1530 Status
= EFI_INVALID_PARAMETER
;
1534 CoreReleaseMemoryLock ();