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.
17 #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)
20 // Entry for tracking the memory regions for each memory type to help cooalese like memory types
23 EFI_PHYSICAL_ADDRESS BaseAddress
;
24 EFI_PHYSICAL_ADDRESS MaximumAddress
;
25 UINT64 CurrentNumberOfPages
;
27 UINTN InformationIndex
;
30 } EFI_MEMORY_TYPE_STAISTICS
;
33 // MemoryMap - The current memory map
35 UINTN mMemoryMapKey
= 0;
38 // mMapStack - space to use as temp storage to build new map descriptors
39 // mMapDepth - depth of new descriptor stack
42 #define MAX_MAP_DEPTH 6
44 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
45 UINTN mFreeMapStack
= 0;
47 // This list maintain the free memory map list
49 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
50 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
52 EFI_MEMORY_TYPE_STAISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
53 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiReservedMemoryType
54 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderCode
55 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderData
56 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesCode
57 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesData
58 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesCode
59 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesData
60 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiConventionalMemory
61 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiUnusableMemory
62 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIReclaimMemory
63 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIMemoryNVS
64 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIO
65 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIOPortSpace
66 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiPalCode
67 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
} // EfiMaxMemoryType
70 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress
= EFI_MAX_ADDRESS
;
72 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
73 { EfiReservedMemoryType
, 0 },
76 { EfiBootServicesCode
, 0 },
77 { EfiBootServicesData
, 0 },
78 { EfiRuntimeServicesCode
, 0 },
79 { EfiRuntimeServicesData
, 0 },
80 { EfiConventionalMemory
, 0 },
81 { EfiUnusableMemory
, 0 },
82 { EfiACPIReclaimMemory
, 0 },
83 { EfiACPIMemoryNVS
, 0 },
84 { EfiMemoryMappedIO
, 0 },
85 { EfiMemoryMappedIOPortSpace
, 0 },
87 { EfiMaxMemoryType
, 0 }
91 // Internal prototypes
94 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
98 PromoteMemoryResource (
103 Internal function. Adds a ranges to the memory map.
104 The range must not already exist in the map.
106 @param Type The type of memory range to add
107 @param Start The starting address in the memory range Must be
109 @param End The last address in the range Must be the last
111 @param Attribute The attributes of the memory range to add
116 IN EFI_MEMORY_TYPE Type
,
117 IN EFI_PHYSICAL_ADDRESS Start
,
118 IN EFI_PHYSICAL_ADDRESS End
,
123 Internal function. Moves any memory descriptors that are on the
124 temporary descriptor stack to heap.
128 CoreFreeMemoryMapStack (
133 Internal function. Converts a memory range to the specified type.
134 The range must exist in the memory map.
136 @param Start The first address of the range Must be page
138 @param NumberOfPages The number of pages to convert
139 @param NewType The new type for the memory range
141 @retval EFI_INVALID_PARAMETER Invalid parameter
142 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
143 range or convertion not allowed.
144 @retval EFI_SUCCESS Successfully converts the memory range to the
151 IN UINT64 NumberOfPages
,
152 IN EFI_MEMORY_TYPE NewType
156 Internal function. Removes a descriptor entry.
158 @param Entry The entry to remove
162 RemoveMemoryMapEntry (
163 IN OUT MEMORY_MAP
*Entry
167 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
168 If the list is emtry, then allocate a new page to refuel the list.
169 Please Note this algorithm to allocate the memory map descriptor has a property
170 that the memory allocated for memory entries always grows, and will never really be freed
171 For example, if the current boot uses 2000 memory map entries at the maximum point, but
172 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
173 memory map entries is still allocated from EfiBootServicesMemory.
176 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
180 AllocateMemoryMapEntry (
186 Enter critical section by gaining lock on gMemoryLock.
190 CoreAcquireMemoryLock (
194 CoreAcquireLock (&gMemoryLock
);
200 Exit critical section by releasing lock on gMemoryLock.
204 CoreReleaseMemoryLock (
208 CoreReleaseLock (&gMemoryLock
);
213 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
217 PromoteMemoryResource (
222 EFI_GCD_MAP_ENTRY
*Entry
;
224 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "Promote the memory resource\n"));
226 CoreAcquireGcdMemoryLock ();
228 Link
= mGcdMemorySpaceMap
.ForwardLink
;
229 while (Link
!= &mGcdMemorySpaceMap
) {
231 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
233 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
234 Entry
->EndAddress
< EFI_MAX_ADDRESS
&&
235 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
236 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
238 // Update the GCD map
240 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
241 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
242 Entry
->ImageHandle
= gDxeCoreImageHandle
;
243 Entry
->DeviceHandle
= NULL
;
246 // Add to allocable system memory resource
250 EfiConventionalMemory
,
253 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
255 CoreFreeMemoryMapStack ();
259 Link
= Link
->ForwardLink
;
262 CoreReleaseGcdMemoryLock ();
269 Called to initialize the memory map and add descriptors to
270 the current descriptor list.
271 The first descriptor that is added must be general usable
272 memory as the addition allocates heap.
274 @param Type The type of memory to add
275 @param Start The starting address in the memory range Must be
277 @param NumberOfPages The number of pages in the range
278 @param Attribute Attributes of the memory to add
280 @return None. The range is added to the memory map
284 CoreAddMemoryDescriptor (
285 IN EFI_MEMORY_TYPE Type
,
286 IN EFI_PHYSICAL_ADDRESS Start
,
287 IN UINT64 NumberOfPages
,
291 EFI_PHYSICAL_ADDRESS End
;
296 if ((Start
& EFI_PAGE_MASK
) != 0) {
300 if (Type
>= EfiMaxMemoryType
&& Type
<= 0x7fffffff) {
304 CoreAcquireMemoryLock ();
305 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
306 CoreAddRange (Type
, Start
, End
, Attribute
);
307 CoreFreeMemoryMapStack ();
308 CoreReleaseMemoryLock ();
311 // Check to see if the statistics for the different memory types have already been established
313 if (mMemoryTypeInformationInitialized
) {
318 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
320 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
322 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
324 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
325 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
329 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
331 // Allocate pages for the current memory type from the top of available memory
333 Status
= CoreAllocatePages (
336 gMemoryTypeInformation
[Index
].NumberOfPages
,
337 &mMemoryTypeStatistics
[Type
].BaseAddress
339 if (EFI_ERROR (Status
)) {
341 // If an error occurs allocating the pages for the current memory type, then
342 // free all the pages allocates for the previous memory types and return. This
343 // operation with be retied when/if more memory is added to the system
345 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
347 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
349 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
350 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
354 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
356 mMemoryTypeStatistics
[Type
].BaseAddress
,
357 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
359 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
360 mMemoryTypeStatistics
[Type
].MaximumAddress
= EFI_MAX_ADDRESS
;
367 // Compute the address at the top of the current statistics
369 mMemoryTypeStatistics
[Type
].MaximumAddress
=
370 mMemoryTypeStatistics
[Type
].BaseAddress
+
371 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
374 // If the current base address is the lowest address so far, then update the default
377 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
378 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
384 // There was enough system memory for all the the memory types were allocated. So,
385 // those memory areas can be freed for future allocations, and all future memory
386 // allocations can occur within their respective bins
388 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
390 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
392 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
393 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
397 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
399 mMemoryTypeStatistics
[Type
].BaseAddress
,
400 gMemoryTypeInformation
[Index
].NumberOfPages
402 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
403 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
408 // If the number of pages reserved for a memory type is 0, then all allocations for that type
409 // should be in the default range.
411 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
412 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
413 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
414 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
417 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
418 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== EFI_MAX_ADDRESS
) {
419 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
423 mMemoryTypeInformationInitialized
= TRUE
;
429 Internal function. Adds a ranges to the memory map.
430 The range must not already exist in the map.
432 @param Type The type of memory range to add
433 @param Start The starting address in the memory range Must be
435 @param End The last address in the range Must be the last
437 @param Attribute The attributes of the memory range to add
442 IN EFI_MEMORY_TYPE Type
,
443 IN EFI_PHYSICAL_ADDRESS Start
,
444 IN EFI_PHYSICAL_ADDRESS End
,
451 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
452 ASSERT (End
> Start
) ;
454 ASSERT_LOCKED (&gMemoryLock
);
456 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
459 // Memory map being altered so updated key
464 // UEFI 2.0 added an event group for notificaiton on memory map changes.
465 // So we need to signal this Event Group every time the memory map changes.
466 // If we are in EFI 1.10 compatability mode no event groups will be
467 // found and nothing will happen we we call this function. These events
468 // will get signaled but since a lock is held around the call to this
469 // function the notificaiton events will only be called after this funciton
470 // returns and the lock is released.
472 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
475 // Look for adjoining memory descriptor
478 // Two memory descriptors can only be merged if they have the same Type
479 // and the same Attribute
482 Link
= gMemoryMap
.ForwardLink
;
483 while (Link
!= &gMemoryMap
) {
484 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
485 Link
= Link
->ForwardLink
;
487 if (Entry
->Type
!= Type
) {
491 if (Entry
->Attribute
!= Attribute
) {
495 if (Entry
->End
+ 1 == Start
) {
497 Start
= Entry
->Start
;
498 RemoveMemoryMapEntry (Entry
);
500 } else if (Entry
->Start
== End
+ 1) {
503 RemoveMemoryMapEntry (Entry
);
511 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
512 mMapStack
[mMapDepth
].FromPages
= FALSE
;
513 mMapStack
[mMapDepth
].Type
= Type
;
514 mMapStack
[mMapDepth
].Start
= Start
;
515 mMapStack
[mMapDepth
].End
= End
;
516 mMapStack
[mMapDepth
].VirtualStart
= 0;
517 mMapStack
[mMapDepth
].Attribute
= Attribute
;
518 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
521 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
528 Internal function. Moves any memory descriptors that are on the
529 temporary descriptor stack to heap.
533 CoreFreeMemoryMapStack (
541 ASSERT_LOCKED (&gMemoryLock
);
544 // If already freeing the map stack, then return
546 if (mFreeMapStack
!= 0) {
551 // Move the temporary memory descriptor stack into pool
555 while (mMapDepth
!= 0) {
557 // Deque an memory map entry from mFreeMemoryMapEntryList
559 Entry
= AllocateMemoryMapEntry ();
564 // Update to proper entry
568 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
571 // Move this entry to general memory
573 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
574 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
576 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
577 Entry
->FromPages
= TRUE
;
580 // Find insertion location
582 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
583 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
584 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
589 InsertTailList (Link2
, &Entry
->Link
);
593 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
594 // so here no need to move it to memory.
596 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
605 Internal function. Removes a descriptor entry.
607 @param Entry The entry to remove
611 RemoveMemoryMapEntry (
612 IN OUT MEMORY_MAP
*Entry
615 RemoveEntryList (&Entry
->Link
);
616 Entry
->Link
.ForwardLink
= NULL
;
618 if (Entry
->FromPages
) {
620 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
622 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
628 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
629 If the list is emtry, then allocate a new page to refuel the list.
630 Please Note this algorithm to allocate the memory map descriptor has a property
631 that the memory allocated for memory entries always grows, and will never really be freed
632 For example, if the current boot uses 2000 memory map entries at the maximum point, but
633 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
634 memory map entries is still allocated from EfiBootServicesMemory.
637 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
641 AllocateMemoryMapEntry (
645 MEMORY_MAP
* FreeDescriptorEntries
;
649 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
651 // The list is empty, to allocate one page to refuel the list
653 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
654 if(FreeDescriptorEntries
!= NULL
) {
656 // Enque the free memmory map entries into the list
658 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
659 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
660 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
667 // dequeue the first descriptor from the list
669 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
670 RemoveEntryList (&Entry
->Link
);
677 Internal function. Converts a memory range to the specified type.
678 The range must exist in the memory map.
680 @param Start The first address of the range Must be page
682 @param NumberOfPages The number of pages to convert
683 @param NewType The new type for the memory range
685 @retval EFI_INVALID_PARAMETER Invalid parameter
686 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
687 range or convertion not allowed.
688 @retval EFI_SUCCESS Successfully converts the memory range to the
695 IN UINT64 NumberOfPages
,
696 IN EFI_MEMORY_TYPE NewType
700 UINT64 NumberOfBytes
;
708 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
709 End
= Start
+ NumberOfBytes
- 1;
711 ASSERT (NumberOfPages
);
712 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
713 ASSERT (End
> Start
) ;
714 ASSERT_LOCKED (&gMemoryLock
);
716 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
> (Start
+ NumberOfBytes
))) {
717 return EFI_INVALID_PARAMETER
;
721 // Convert the entire range
724 while (Start
< End
) {
727 // Find the entry that the covers the range
729 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
730 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
732 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
737 if (Link
== &gMemoryMap
) {
738 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
739 return EFI_NOT_FOUND
;
743 // Convert range to the end, or to the end of the descriptor
744 // if that's all we've got
747 if (Entry
->End
< End
) {
748 RangeEnd
= Entry
->End
;
751 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to %d\n", Start
, RangeEnd
, NewType
));
754 // Debug code - verify conversion is allowed
756 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
757 DEBUG ((DEBUG_ERROR
, "ConvertPages: Incompatible memory types\n"));
758 return EFI_NOT_FOUND
;
762 // Update counters for the number of pages allocated to each memory type
764 if (Entry
->Type
>= 0 && Entry
->Type
< EfiMaxMemoryType
) {
765 if (Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&&
766 Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) {
767 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
768 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
770 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
775 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
) {
776 if (Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
777 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
778 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
>
779 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
780 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
786 // Pull range out of descriptor
788 if (Entry
->Start
== Start
) {
793 Entry
->Start
= RangeEnd
+ 1;
795 } else if (Entry
->End
== RangeEnd
) {
800 Entry
->End
= Start
- 1;
805 // Pull it out of the center, clip current
811 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
812 mMapStack
[mMapDepth
].FromPages
= FALSE
;
813 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
814 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
815 mMapStack
[mMapDepth
].End
= Entry
->End
;
818 // Inherit Attribute from the Memory Descriptor that is being clipped
820 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
822 Entry
->End
= Start
- 1;
823 ASSERT (Entry
->Start
< Entry
->End
);
825 Entry
= &mMapStack
[mMapDepth
];
826 InsertTailList (&gMemoryMap
, &Entry
->Link
);
829 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
833 // The new range inherits the same Attribute as the Entry
834 //it is being cut out of
836 Attribute
= Entry
->Attribute
;
839 // If the descriptor is empty, then remove it from the map
841 if (Entry
->Start
== Entry
->End
+ 1) {
842 RemoveMemoryMapEntry (Entry
);
847 // Add our new range in
849 CoreAddRange (NewType
, Start
, RangeEnd
, Attribute
);
852 // Move any map descriptor stack to general pool
854 CoreFreeMemoryMapStack ();
857 // Bump the starting address, and convert the next range
859 Start
= RangeEnd
+ 1;
863 // Converted the whole range, done
872 Internal function. Finds a consecutive free page range below
873 the requested address.
875 @param MaxAddress The address that the range must be below
876 @param NumberOfPages Number of pages needed
877 @param NewType The type of memory the range is going to be
879 @param Alignment Bits to align with
881 @return The base address of the range, or 0 if the range was not found
886 IN UINT64 MaxAddress
,
887 IN UINT64 NumberOfPages
,
888 IN EFI_MEMORY_TYPE NewType
,
892 UINT64 NumberOfBytes
;
896 UINT64 DescNumberOfBytes
;
900 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
904 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
907 // If MaxAddress is not aligned to the end of a page
911 // Change MaxAddress to be 1 page lower
913 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
916 // Set MaxAddress to a page boundary
918 MaxAddress
&= ~EFI_PAGE_MASK
;
921 // Set MaxAddress to end of the page
923 MaxAddress
|= EFI_PAGE_MASK
;
926 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
929 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
930 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
933 // If it's not a free entry, don't bother with it
935 if (Entry
->Type
!= EfiConventionalMemory
) {
939 DescStart
= Entry
->Start
;
940 DescEnd
= Entry
->End
;
943 // If desc is past max allowed address, skip it
945 if (DescStart
>= MaxAddress
) {
950 // If desc ends past max allowed address, clip the end
952 if (DescEnd
>= MaxAddress
) {
953 DescEnd
= MaxAddress
;
956 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
959 // Compute the number of bytes we can used from this
960 // descriptor, and see it's enough to satisfy the request
962 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
964 if (DescNumberOfBytes
>= NumberOfBytes
) {
967 // If this is the best match so far remember it
969 if (DescEnd
> Target
) {
976 // If this is a grow down, adjust target to be the allocation base
978 Target
-= NumberOfBytes
- 1;
981 // If we didn't find a match, return 0
983 if ((Target
& EFI_PAGE_MASK
) != 0) {
992 Internal function. Finds a consecutive free page range below
993 the requested address
995 @param MaxAddress The address that the range must be below
996 @param NoPages Number of pages needed
997 @param NewType The type of memory the range is going to be
999 @param Alignment Bits to align with
1001 @return The base address of the range, or 0 if the range was not found.
1006 IN UINT64 MaxAddress
,
1008 IN EFI_MEMORY_TYPE NewType
,
1012 UINT64 NewMaxAddress
;
1015 NewMaxAddress
= MaxAddress
;
1017 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
&& NewMaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1018 NewMaxAddress
= mMemoryTypeStatistics
[NewType
].MaximumAddress
;
1020 if (NewMaxAddress
> mDefaultMaximumAddress
) {
1021 NewMaxAddress
= mDefaultMaximumAddress
;
1025 Start
= CoreFindFreePagesI (NewMaxAddress
, NoPages
, NewType
, Alignment
);
1027 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1030 // Here means there may be no enough memory to use, so try to go through
1031 // all the memory descript to promote the untested memory directly
1033 PromoteMemoryResource ();
1036 // Allocate memory again after the memory resource re-arranged
1038 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1048 Allocates pages from the memory map.
1050 @param Type The type of allocation to perform
1051 @param MemoryType The type of memory to turn the allocated pages
1053 @param NumberOfPages The number of pages to allocate
1054 @param Memory A pointer to receive the base allocated memory
1057 @return Status. On success, Memory is filled in with the base address allocated
1058 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1060 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1061 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1062 @retval EFI_SUCCESS Pages successfully allocated.
1068 IN EFI_ALLOCATE_TYPE Type
,
1069 IN EFI_MEMORY_TYPE MemoryType
,
1070 IN UINTN NumberOfPages
,
1071 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1079 if (Type
< AllocateAnyPages
|| Type
>= (UINTN
) MaxAllocateType
) {
1080 return EFI_INVALID_PARAMETER
;
1083 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
1084 MemoryType
== EfiConventionalMemory
) {
1085 return EFI_INVALID_PARAMETER
;
1088 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1090 if (MemoryType
== EfiACPIReclaimMemory
||
1091 MemoryType
== EfiACPIMemoryNVS
||
1092 MemoryType
== EfiRuntimeServicesCode
||
1093 MemoryType
== EfiRuntimeServicesData
) {
1095 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1098 if (Type
== AllocateAddress
) {
1099 if ((*Memory
& (Alignment
- 1)) != 0) {
1100 return EFI_NOT_FOUND
;
1104 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1105 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1108 // If this is for below a particular address, then
1113 // The max address is the max natively addressable address for the processor
1115 MaxAddress
= EFI_MAX_ADDRESS
;
1117 if (Type
== AllocateMaxAddress
) {
1121 CoreAcquireMemoryLock ();
1124 // If not a specific address, then find an address to allocate
1126 if (Type
!= AllocateAddress
) {
1127 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1129 Status
= EFI_OUT_OF_RESOURCES
;
1135 // Convert pages from FreeMemory to the requested type
1137 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1140 CoreReleaseMemoryLock ();
1142 if (!EFI_ERROR (Status
)) {
1151 Frees previous allocated pages.
1153 @param Memory Base address of memory being freed
1154 @param NumberOfPages The number of pages to free
1156 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1157 @retval EFI_INVALID_PARAMETER Address not aligned
1158 @return EFI_SUCCESS -Pages successfully freed.
1164 IN EFI_PHYSICAL_ADDRESS Memory
,
1165 IN UINTN NumberOfPages
1176 CoreAcquireMemoryLock ();
1179 // Find the entry that the covers the range
1182 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1183 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1184 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1188 if (Link
== &gMemoryMap
) {
1189 CoreReleaseMemoryLock ();
1190 return EFI_NOT_FOUND
;
1193 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1195 if (Entry
->Type
== EfiACPIReclaimMemory
||
1196 Entry
->Type
== EfiACPIMemoryNVS
||
1197 Entry
->Type
== EfiRuntimeServicesCode
||
1198 Entry
->Type
== EfiRuntimeServicesData
) {
1200 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1204 if ((Memory
& (Alignment
- 1)) != 0) {
1205 CoreReleaseMemoryLock ();
1206 return EFI_INVALID_PARAMETER
;
1209 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1210 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1212 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1214 CoreReleaseMemoryLock ();
1216 if (EFI_ERROR (Status
)) {
1221 // Destroy the contents
1223 if (Memory
< EFI_MAX_ADDRESS
) {
1224 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
)Memory
, NumberOfPages
<< EFI_PAGE_SHIFT
);
1232 This function returns a copy of the current memory map. The map is an array of
1233 memory descriptors, each of which describes a contiguous block of memory.
1235 @param MemoryMapSize A pointer to the size, in bytes, of the
1236 MemoryMap buffer. On input, this is the size of
1237 the buffer allocated by the caller. On output,
1238 it is the size of the buffer returned by the
1239 firmware if the buffer was large enough, or the
1240 size of the buffer needed to contain the map if
1241 the buffer was too small.
1242 @param MemoryMap A pointer to the buffer in which firmware places
1243 the current memory map.
1244 @param MapKey A pointer to the location in which firmware
1245 returns the key for the current memory map.
1246 @param DescriptorSize A pointer to the location in which firmware
1247 returns the size, in bytes, of an individual
1248 EFI_MEMORY_DESCRIPTOR.
1249 @param DescriptorVersion A pointer to the location in which firmware
1250 returns the version number associated with the
1251 EFI_MEMORY_DESCRIPTOR.
1253 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1255 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1256 buffer size needed to hold the memory map is
1257 returned in MemoryMapSize.
1258 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1264 IN OUT UINTN
*MemoryMapSize
,
1265 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1267 OUT UINTN
*DescriptorSize
,
1268 OUT UINT32
*DescriptorVersion
1274 UINTN NumberOfRuntimeEntries
;
1277 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1278 EFI_MEMORY_TYPE Type
;
1281 // Make sure the parameters are valid
1283 if (MemoryMapSize
== NULL
) {
1284 return EFI_INVALID_PARAMETER
;
1287 CoreAcquireGcdMemoryLock ();
1290 // Count the number of Reserved and MMIO entries that are marked for runtime use
1292 NumberOfRuntimeEntries
= 0;
1293 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1294 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1295 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1296 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1297 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1298 NumberOfRuntimeEntries
++;
1303 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1306 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1307 // prevent people from having pointer math bugs in their code.
1308 // now you have to use *DescriptorSize to make things work.
1310 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1312 if (DescriptorSize
!= NULL
) {
1313 *DescriptorSize
= Size
;
1316 if (DescriptorVersion
!= NULL
) {
1317 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1320 CoreAcquireMemoryLock ();
1323 // Compute the buffer size needed to fit the entire map
1325 BufferSize
= Size
* NumberOfRuntimeEntries
;
1326 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1330 if (*MemoryMapSize
< BufferSize
) {
1331 Status
= EFI_BUFFER_TOO_SMALL
;
1335 if (MemoryMap
== NULL
) {
1336 Status
= EFI_INVALID_PARAMETER
;
1343 ZeroMem (MemoryMap
, BufferSize
);
1344 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1345 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1346 ASSERT (Entry
->VirtualStart
== 0);
1349 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1351 MemoryMap
->Type
= Entry
->Type
;
1352 MemoryMap
->PhysicalStart
= Entry
->Start
;
1353 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1354 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1356 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1357 // memory type bin and needs to be converted to the same memory type as the rest of the
1358 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1359 // improves the chances for a successful S4 resume in the presence of minor page allocation
1360 // differences across reboots.
1362 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1363 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1364 if (mMemoryTypeStatistics
[Type
].Special
&&
1365 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1366 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1367 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1368 MemoryMap
->Type
= Type
;
1372 MemoryMap
->Attribute
= Entry
->Attribute
;
1373 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1374 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1377 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1380 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1381 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1382 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1383 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1384 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1386 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1387 MemoryMap
->VirtualStart
= 0;
1388 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1389 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1391 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1392 MemoryMap
->Type
= EfiReservedMemoryType
;
1393 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1394 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1395 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1397 MemoryMap
->Type
= EfiMemoryMappedIO
;
1401 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1406 Status
= EFI_SUCCESS
;
1410 CoreReleaseMemoryLock ();
1412 CoreReleaseGcdMemoryLock ();
1415 // Update the map key finally
1417 if (MapKey
!= NULL
) {
1418 *MapKey
= mMemoryMapKey
;
1421 *MemoryMapSize
= BufferSize
;
1428 Internal function. Used by the pool functions to allocate pages
1429 to back pool allocation requests.
1431 @param PoolType The type of memory for the new pool pages
1432 @param NumberOfPages No of pages to allocate
1433 @param Alignment Bits to align.
1435 @return The allocated memory, or NULL
1439 CoreAllocatePoolPages (
1440 IN EFI_MEMORY_TYPE PoolType
,
1441 IN UINTN NumberOfPages
,
1448 // Find the pages to convert
1450 Start
= FindFreePages (EFI_MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1453 // Convert it to boot services data
1456 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages
));
1458 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1461 return (VOID
*)(UINTN
) Start
;
1466 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1468 @param Memory The base address to free
1469 @param NumberOfPages The number of pages to free
1474 IN EFI_PHYSICAL_ADDRESS Memory
,
1475 IN UINTN NumberOfPages
1478 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1484 Make sure the memory map is following all the construction rules,
1485 it is the last time to check memory map error before exit boot services.
1487 @param MapKey Memory map key
1489 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1491 @retval EFI_SUCCESS Valid memory map.
1495 CoreTerminateMemoryMap (
1503 Status
= EFI_SUCCESS
;
1505 CoreAcquireMemoryLock ();
1507 if (MapKey
== mMemoryMapKey
) {
1510 // Make sure the memory map is following all the construction rules
1511 // This is the last chance we will be able to display any messages on
1512 // the console devices.
1515 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1516 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1517 if (Entry
->Attribute
& EFI_MEMORY_RUNTIME
) {
1518 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1519 DEBUG((DEBUG_ERROR
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1520 CoreReleaseMemoryLock ();
1521 return EFI_INVALID_PARAMETER
;
1523 if (Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1524 DEBUG((DEBUG_ERROR
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1525 CoreReleaseMemoryLock ();
1526 return EFI_INVALID_PARAMETER
;
1528 if ((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1529 DEBUG((DEBUG_ERROR
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1530 CoreReleaseMemoryLock ();
1531 return EFI_INVALID_PARAMETER
;
1537 // The map key they gave us matches what we expect. Fall through and
1538 // return success. In an ideal world we would clear out all of
1539 // EfiBootServicesCode and EfiBootServicesData. However this function
1540 // is not the last one called by ExitBootServices(), so we have to
1541 // preserve the memory contents.
1544 Status
= EFI_INVALID_PARAMETER
;
1547 CoreReleaseMemoryLock ();