2 UEFI Memory page management functions.
4 Copyright (c) 2007 - 2010, 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_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
);
832 if (NewType
== EfiConventionalMemory
) {
833 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) Start
, (UINTN
) (RangeEnd
- Start
+ 1));
837 // Move any map descriptor stack to general pool
839 CoreFreeMemoryMapStack ();
842 // Bump the starting address, and convert the next range
844 Start
= RangeEnd
+ 1;
848 // Converted the whole range, done
857 Internal function. Finds a consecutive free page range below
858 the requested address.
860 @param MaxAddress The address that the range must be below
861 @param NumberOfPages Number of pages needed
862 @param NewType The type of memory the range is going to be
864 @param Alignment Bits to align with
866 @return The base address of the range, or 0 if the range was not found
871 IN UINT64 MaxAddress
,
872 IN UINT64 NumberOfPages
,
873 IN EFI_MEMORY_TYPE NewType
,
877 UINT64 NumberOfBytes
;
881 UINT64 DescNumberOfBytes
;
885 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
889 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
892 // If MaxAddress is not aligned to the end of a page
896 // Change MaxAddress to be 1 page lower
898 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
901 // Set MaxAddress to a page boundary
903 MaxAddress
&= ~EFI_PAGE_MASK
;
906 // Set MaxAddress to end of the page
908 MaxAddress
|= EFI_PAGE_MASK
;
911 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
914 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
915 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
918 // If it's not a free entry, don't bother with it
920 if (Entry
->Type
!= EfiConventionalMemory
) {
924 DescStart
= Entry
->Start
;
925 DescEnd
= Entry
->End
;
928 // If desc is past max allowed address, skip it
930 if (DescStart
>= MaxAddress
) {
935 // If desc ends past max allowed address, clip the end
937 if (DescEnd
>= MaxAddress
) {
938 DescEnd
= MaxAddress
;
941 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
944 // Compute the number of bytes we can used from this
945 // descriptor, and see it's enough to satisfy the request
947 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
949 if (DescNumberOfBytes
>= NumberOfBytes
) {
952 // If this is the best match so far remember it
954 if (DescEnd
> Target
) {
961 // If this is a grow down, adjust target to be the allocation base
963 Target
-= NumberOfBytes
- 1;
966 // If we didn't find a match, return 0
968 if ((Target
& EFI_PAGE_MASK
) != 0) {
977 Internal function. Finds a consecutive free page range below
978 the requested address
980 @param MaxAddress The address that the range must be below
981 @param NoPages Number of pages needed
982 @param NewType The type of memory the range is going to be
984 @param Alignment Bits to align with
986 @return The base address of the range, or 0 if the range was not found.
991 IN UINT64 MaxAddress
,
993 IN EFI_MEMORY_TYPE NewType
,
997 UINT64 NewMaxAddress
;
1000 NewMaxAddress
= MaxAddress
;
1002 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
&& NewMaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1003 NewMaxAddress
= mMemoryTypeStatistics
[NewType
].MaximumAddress
;
1005 if (NewMaxAddress
> mDefaultMaximumAddress
) {
1006 NewMaxAddress
= mDefaultMaximumAddress
;
1010 Start
= CoreFindFreePagesI (NewMaxAddress
, NoPages
, NewType
, Alignment
);
1012 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1015 // Here means there may be no enough memory to use, so try to go through
1016 // all the memory descript to promote the untested memory directly
1018 PromoteMemoryResource ();
1021 // Allocate memory again after the memory resource re-arranged
1023 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1033 Allocates pages from the memory map.
1035 @param Type The type of allocation to perform
1036 @param MemoryType The type of memory to turn the allocated pages
1038 @param NumberOfPages The number of pages to allocate
1039 @param Memory A pointer to receive the base allocated memory
1042 @return Status. On success, Memory is filled in with the base address allocated
1043 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1045 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1046 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1047 @retval EFI_SUCCESS Pages successfully allocated.
1053 IN EFI_ALLOCATE_TYPE Type
,
1054 IN EFI_MEMORY_TYPE MemoryType
,
1055 IN UINTN NumberOfPages
,
1056 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1064 if (Type
< AllocateAnyPages
|| Type
>= (UINTN
) MaxAllocateType
) {
1065 return EFI_INVALID_PARAMETER
;
1068 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
1069 MemoryType
== EfiConventionalMemory
) {
1070 return EFI_INVALID_PARAMETER
;
1073 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1075 if (MemoryType
== EfiACPIReclaimMemory
||
1076 MemoryType
== EfiACPIMemoryNVS
||
1077 MemoryType
== EfiRuntimeServicesCode
||
1078 MemoryType
== EfiRuntimeServicesData
) {
1080 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1083 if (Type
== AllocateAddress
) {
1084 if ((*Memory
& (Alignment
- 1)) != 0) {
1085 return EFI_NOT_FOUND
;
1089 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1090 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1093 // If this is for below a particular address, then
1098 // The max address is the max natively addressable address for the processor
1100 MaxAddress
= MAX_ADDRESS
;
1102 if (Type
== AllocateMaxAddress
) {
1106 CoreAcquireMemoryLock ();
1109 // If not a specific address, then find an address to allocate
1111 if (Type
!= AllocateAddress
) {
1112 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1114 Status
= EFI_OUT_OF_RESOURCES
;
1120 // Convert pages from FreeMemory to the requested type
1122 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1125 CoreReleaseMemoryLock ();
1127 if (!EFI_ERROR (Status
)) {
1136 Frees previous allocated pages.
1138 @param Memory Base address of memory being freed
1139 @param NumberOfPages The number of pages to free
1141 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1142 @retval EFI_INVALID_PARAMETER Address not aligned
1143 @return EFI_SUCCESS -Pages successfully freed.
1149 IN EFI_PHYSICAL_ADDRESS Memory
,
1150 IN UINTN NumberOfPages
1161 CoreAcquireMemoryLock ();
1164 // Find the entry that the covers the range
1167 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1168 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1169 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1173 if (Link
== &gMemoryMap
) {
1174 Status
= EFI_NOT_FOUND
;
1178 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1180 ASSERT (Entry
!= NULL
);
1181 if (Entry
->Type
== EfiACPIReclaimMemory
||
1182 Entry
->Type
== EfiACPIMemoryNVS
||
1183 Entry
->Type
== EfiRuntimeServicesCode
||
1184 Entry
->Type
== EfiRuntimeServicesData
) {
1186 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1190 if ((Memory
& (Alignment
- 1)) != 0) {
1191 Status
= EFI_INVALID_PARAMETER
;
1195 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1196 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1198 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1200 if (EFI_ERROR (Status
)) {
1205 CoreReleaseMemoryLock ();
1211 This function returns a copy of the current memory map. The map is an array of
1212 memory descriptors, each of which describes a contiguous block of memory.
1214 @param MemoryMapSize A pointer to the size, in bytes, of the
1215 MemoryMap buffer. On input, this is the size of
1216 the buffer allocated by the caller. On output,
1217 it is the size of the buffer returned by the
1218 firmware if the buffer was large enough, or the
1219 size of the buffer needed to contain the map if
1220 the buffer was too small.
1221 @param MemoryMap A pointer to the buffer in which firmware places
1222 the current memory map.
1223 @param MapKey A pointer to the location in which firmware
1224 returns the key for the current memory map.
1225 @param DescriptorSize A pointer to the location in which firmware
1226 returns the size, in bytes, of an individual
1227 EFI_MEMORY_DESCRIPTOR.
1228 @param DescriptorVersion A pointer to the location in which firmware
1229 returns the version number associated with the
1230 EFI_MEMORY_DESCRIPTOR.
1232 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1234 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1235 buffer size needed to hold the memory map is
1236 returned in MemoryMapSize.
1237 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1243 IN OUT UINTN
*MemoryMapSize
,
1244 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1246 OUT UINTN
*DescriptorSize
,
1247 OUT UINT32
*DescriptorVersion
1253 UINTN NumberOfRuntimeEntries
;
1256 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1257 EFI_MEMORY_TYPE Type
;
1260 // Make sure the parameters are valid
1262 if (MemoryMapSize
== NULL
) {
1263 return EFI_INVALID_PARAMETER
;
1266 CoreAcquireGcdMemoryLock ();
1269 // Count the number of Reserved and MMIO entries that are marked for runtime use
1271 NumberOfRuntimeEntries
= 0;
1272 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1273 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1274 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1275 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1276 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1277 NumberOfRuntimeEntries
++;
1282 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1285 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1286 // prevent people from having pointer math bugs in their code.
1287 // now you have to use *DescriptorSize to make things work.
1289 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1291 if (DescriptorSize
!= NULL
) {
1292 *DescriptorSize
= Size
;
1295 if (DescriptorVersion
!= NULL
) {
1296 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1299 CoreAcquireMemoryLock ();
1302 // Compute the buffer size needed to fit the entire map
1304 BufferSize
= Size
* NumberOfRuntimeEntries
;
1305 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1309 if (*MemoryMapSize
< BufferSize
) {
1310 Status
= EFI_BUFFER_TOO_SMALL
;
1314 if (MemoryMap
== NULL
) {
1315 Status
= EFI_INVALID_PARAMETER
;
1322 ZeroMem (MemoryMap
, BufferSize
);
1323 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1324 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1325 ASSERT (Entry
->VirtualStart
== 0);
1328 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1330 MemoryMap
->Type
= Entry
->Type
;
1331 MemoryMap
->PhysicalStart
= Entry
->Start
;
1332 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1333 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1335 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1336 // memory type bin and needs to be converted to the same memory type as the rest of the
1337 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1338 // improves the chances for a successful S4 resume in the presence of minor page allocation
1339 // differences across reboots.
1341 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1342 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1343 if (mMemoryTypeStatistics
[Type
].Special
&&
1344 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1345 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1346 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1347 MemoryMap
->Type
= Type
;
1351 MemoryMap
->Attribute
= Entry
->Attribute
;
1352 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1353 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1356 MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, Size
);
1359 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1360 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1361 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1362 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1363 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1365 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries
1366 // that are marked for runtime use
1368 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1369 MemoryMap
->VirtualStart
= 0;
1370 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1371 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1373 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1374 MemoryMap
->Type
= EfiReservedMemoryType
;
1375 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1376 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1377 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1379 MemoryMap
->Type
= EfiMemoryMappedIO
;
1383 MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, Size
);
1388 Status
= EFI_SUCCESS
;
1392 CoreReleaseMemoryLock ();
1394 CoreReleaseGcdMemoryLock ();
1397 // Update the map key finally
1399 if (MapKey
!= NULL
) {
1400 *MapKey
= mMemoryMapKey
;
1403 *MemoryMapSize
= BufferSize
;
1410 Internal function. Used by the pool functions to allocate pages
1411 to back pool allocation requests.
1413 @param PoolType The type of memory for the new pool pages
1414 @param NumberOfPages No of pages to allocate
1415 @param Alignment Bits to align.
1417 @return The allocated memory, or NULL
1421 CoreAllocatePoolPages (
1422 IN EFI_MEMORY_TYPE PoolType
,
1423 IN UINTN NumberOfPages
,
1430 // Find the pages to convert
1432 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1435 // Convert it to boot services data
1438 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1440 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1443 return (VOID
*)(UINTN
) Start
;
1448 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1450 @param Memory The base address to free
1451 @param NumberOfPages The number of pages to free
1456 IN EFI_PHYSICAL_ADDRESS Memory
,
1457 IN UINTN NumberOfPages
1460 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1466 Make sure the memory map is following all the construction rules,
1467 it is the last time to check memory map error before exit boot services.
1469 @param MapKey Memory map key
1471 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1473 @retval EFI_SUCCESS Valid memory map.
1477 CoreTerminateMemoryMap (
1485 Status
= EFI_SUCCESS
;
1487 CoreAcquireMemoryLock ();
1489 if (MapKey
== mMemoryMapKey
) {
1492 // Make sure the memory map is following all the construction rules
1493 // This is the last chance we will be able to display any messages on
1494 // the console devices.
1497 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1498 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1499 if ((Entry
->Attribute
& EFI_MEMORY_RUNTIME
) != 0) {
1500 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1501 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1502 Status
= EFI_INVALID_PARAMETER
;
1505 if ((Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1506 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1507 Status
= EFI_INVALID_PARAMETER
;
1510 if (((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1511 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1512 Status
= EFI_INVALID_PARAMETER
;
1519 // The map key they gave us matches what we expect. Fall through and
1520 // return success. In an ideal world we would clear out all of
1521 // EfiBootServicesCode and EfiBootServicesData. However this function
1522 // is not the last one called by ExitBootServices(), so we have to
1523 // preserve the memory contents.
1526 Status
= EFI_INVALID_PARAMETER
;
1530 CoreReleaseMemoryLock ();