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 (
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
439 @return None. The range is added to the memory map
444 IN EFI_MEMORY_TYPE Type
,
445 IN EFI_PHYSICAL_ADDRESS Start
,
446 IN EFI_PHYSICAL_ADDRESS End
,
453 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
454 ASSERT (End
> Start
) ;
456 ASSERT_LOCKED (&gMemoryLock
);
458 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
461 // Memory map being altered so updated key
466 // UEFI 2.0 added an event group for notificaiton on memory map changes.
467 // So we need to signal this Event Group every time the memory map changes.
468 // If we are in EFI 1.10 compatability mode no event groups will be
469 // found and nothing will happen we we call this function. These events
470 // will get signaled but since a lock is held around the call to this
471 // function the notificaiton events will only be called after this funciton
472 // returns and the lock is released.
474 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
477 // Look for adjoining memory descriptor
480 // Two memory descriptors can only be merged if they have the same Type
481 // and the same Attribute
484 Link
= gMemoryMap
.ForwardLink
;
485 while (Link
!= &gMemoryMap
) {
486 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
487 Link
= Link
->ForwardLink
;
489 if (Entry
->Type
!= Type
) {
493 if (Entry
->Attribute
!= Attribute
) {
497 if (Entry
->End
+ 1 == Start
) {
499 Start
= Entry
->Start
;
500 RemoveMemoryMapEntry (Entry
);
502 } else if (Entry
->Start
== End
+ 1) {
505 RemoveMemoryMapEntry (Entry
);
513 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
514 mMapStack
[mMapDepth
].FromPages
= FALSE
;
515 mMapStack
[mMapDepth
].Type
= Type
;
516 mMapStack
[mMapDepth
].Start
= Start
;
517 mMapStack
[mMapDepth
].End
= End
;
518 mMapStack
[mMapDepth
].VirtualStart
= 0;
519 mMapStack
[mMapDepth
].Attribute
= Attribute
;
520 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
523 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
530 Internal function. Moves any memory descriptors that are on the
531 temporary descriptor stack to heap.
535 CoreFreeMemoryMapStack (
543 ASSERT_LOCKED (&gMemoryLock
);
546 // If already freeing the map stack, then return
548 if (mFreeMapStack
!= 0) {
553 // Move the temporary memory descriptor stack into pool
557 while (mMapDepth
!= 0) {
559 // Deque an memory map entry from mFreeMemoryMapEntryList
561 Entry
= AllocateMemoryMapEntry ();
566 // Update to proper entry
570 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
573 // Move this entry to general memory
575 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
576 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
578 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
579 Entry
->FromPages
= TRUE
;
582 // Find insertion location
584 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
585 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
586 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
591 InsertTailList (Link2
, &Entry
->Link
);
595 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
596 // so here no need to move it to memory.
598 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
607 Internal function. Removes a descriptor entry.
609 @param Entry The entry to remove
613 RemoveMemoryMapEntry (
614 IN OUT MEMORY_MAP
*Entry
617 RemoveEntryList (&Entry
->Link
);
618 Entry
->Link
.ForwardLink
= NULL
;
620 if (Entry
->FromPages
) {
622 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
624 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
630 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
631 If the list is emtry, then allocate a new page to refuel the list.
632 Please Note this algorithm to allocate the memory map descriptor has a property
633 that the memory allocated for memory entries always grows, and will never really be freed
634 For example, if the current boot uses 2000 memory map entries at the maximum point, but
635 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
636 memory map entries is still allocated from EfiBootServicesMemory.
639 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
643 AllocateMemoryMapEntry (
647 MEMORY_MAP
* FreeDescriptorEntries
;
651 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
653 // The list is empty, to allocate one page to refuel the list
655 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
656 if(FreeDescriptorEntries
!= NULL
) {
658 // Enque the free memmory map entries into the list
660 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
661 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
662 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
669 // dequeue the first descriptor from the list
671 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
672 RemoveEntryList (&Entry
->Link
);
679 Internal function. Converts a memory range to the specified type.
680 The range must exist in the memory map.
682 @param Start The first address of the range Must be page
684 @param NumberOfPages The number of pages to convert
685 @param NewType The new type for the memory range
687 @retval EFI_INVALID_PARAMETER Invalid parameter
688 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
689 range or convertion not allowed.
690 @retval EFI_SUCCESS Successfully converts the memory range to the
697 IN UINT64 NumberOfPages
,
698 IN EFI_MEMORY_TYPE NewType
702 UINT64 NumberOfBytes
;
710 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
711 End
= Start
+ NumberOfBytes
- 1;
713 ASSERT (NumberOfPages
);
714 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
715 ASSERT (End
> Start
) ;
716 ASSERT_LOCKED (&gMemoryLock
);
718 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
> (Start
+ NumberOfBytes
))) {
719 return EFI_INVALID_PARAMETER
;
723 // Convert the entire range
726 while (Start
< End
) {
729 // Find the entry that the covers the range
731 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
732 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
734 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
739 if (Link
== &gMemoryMap
) {
740 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
741 return EFI_NOT_FOUND
;
745 // Convert range to the end, or to the end of the descriptor
746 // if that's all we've got
749 if (Entry
->End
< End
) {
750 RangeEnd
= Entry
->End
;
753 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to %d\n", Start
, RangeEnd
, NewType
));
756 // Debug code - verify conversion is allowed
758 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
759 DEBUG ((DEBUG_ERROR
, "ConvertPages: Incompatible memory types\n"));
760 return EFI_NOT_FOUND
;
764 // Update counters for the number of pages allocated to each memory type
766 if (Entry
->Type
>= 0 && Entry
->Type
< EfiMaxMemoryType
) {
767 if (Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&&
768 Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) {
769 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
770 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
772 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
777 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
) {
778 if (Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
779 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
780 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
>
781 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
782 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
788 // Pull range out of descriptor
790 if (Entry
->Start
== Start
) {
795 Entry
->Start
= RangeEnd
+ 1;
797 } else if (Entry
->End
== RangeEnd
) {
802 Entry
->End
= Start
- 1;
807 // Pull it out of the center, clip current
813 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
814 mMapStack
[mMapDepth
].FromPages
= FALSE
;
815 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
816 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
817 mMapStack
[mMapDepth
].End
= Entry
->End
;
820 // Inherit Attribute from the Memory Descriptor that is being clipped
822 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
824 Entry
->End
= Start
- 1;
825 ASSERT (Entry
->Start
< Entry
->End
);
827 Entry
= &mMapStack
[mMapDepth
];
828 InsertTailList (&gMemoryMap
, &Entry
->Link
);
831 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
835 // The new range inherits the same Attribute as the Entry
836 //it is being cut out of
838 Attribute
= Entry
->Attribute
;
841 // If the descriptor is empty, then remove it from the map
843 if (Entry
->Start
== Entry
->End
+ 1) {
844 RemoveMemoryMapEntry (Entry
);
849 // Add our new range in
851 CoreAddRange (NewType
, Start
, RangeEnd
, Attribute
);
854 // Move any map descriptor stack to general pool
856 CoreFreeMemoryMapStack ();
859 // Bump the starting address, and convert the next range
861 Start
= RangeEnd
+ 1;
865 // Converted the whole range, done
874 Internal function. Finds a consecutive free page range below
875 the requested address.
877 @param MaxAddress The address that the range must be below
878 @param NumberOfPages Number of pages needed
879 @param NewType The type of memory the range is going to be
881 @param Alignment Bits to align with
883 @return The base address of the range, or 0 if the range was not found
888 IN UINT64 MaxAddress
,
889 IN UINT64 NumberOfPages
,
890 IN EFI_MEMORY_TYPE NewType
,
894 UINT64 NumberOfBytes
;
898 UINT64 DescNumberOfBytes
;
902 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
906 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
909 // If MaxAddress is not aligned to the end of a page
913 // Change MaxAddress to be 1 page lower
915 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
918 // Set MaxAddress to a page boundary
920 MaxAddress
&= ~EFI_PAGE_MASK
;
923 // Set MaxAddress to end of the page
925 MaxAddress
|= EFI_PAGE_MASK
;
928 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
931 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
932 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
935 // If it's not a free entry, don't bother with it
937 if (Entry
->Type
!= EfiConventionalMemory
) {
941 DescStart
= Entry
->Start
;
942 DescEnd
= Entry
->End
;
945 // If desc is past max allowed address, skip it
947 if (DescStart
>= MaxAddress
) {
952 // If desc ends past max allowed address, clip the end
954 if (DescEnd
>= MaxAddress
) {
955 DescEnd
= MaxAddress
;
958 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
961 // Compute the number of bytes we can used from this
962 // descriptor, and see it's enough to satisfy the request
964 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
966 if (DescNumberOfBytes
>= NumberOfBytes
) {
969 // If this is the best match so far remember it
971 if (DescEnd
> Target
) {
978 // If this is a grow down, adjust target to be the allocation base
980 Target
-= NumberOfBytes
- 1;
983 // If we didn't find a match, return 0
985 if ((Target
& EFI_PAGE_MASK
) != 0) {
994 Internal function. Finds a consecutive free page range below
995 the requested address
997 @param MaxAddress The address that the range must be below
998 @param NoPages Number of pages needed
999 @param NewType The type of memory the range is going to be
1001 @param Alignment Bits to align with
1003 @return The base address of the range, or 0 if the range was not found.
1008 IN UINT64 MaxAddress
,
1010 IN EFI_MEMORY_TYPE NewType
,
1014 UINT64 NewMaxAddress
;
1017 NewMaxAddress
= MaxAddress
;
1019 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
&& NewMaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1020 NewMaxAddress
= mMemoryTypeStatistics
[NewType
].MaximumAddress
;
1022 if (NewMaxAddress
> mDefaultMaximumAddress
) {
1023 NewMaxAddress
= mDefaultMaximumAddress
;
1027 Start
= CoreFindFreePagesI (NewMaxAddress
, NoPages
, NewType
, Alignment
);
1029 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1032 // Here means there may be no enough memory to use, so try to go through
1033 // all the memory descript to promote the untested memory directly
1035 PromoteMemoryResource ();
1038 // Allocate memory again after the memory resource re-arranged
1040 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1050 Allocates pages from the memory map.
1052 @param Type The type of allocation to perform
1053 @param MemoryType The type of memory to turn the allocated pages
1055 @param NumberOfPages The number of pages to allocate
1056 @param Memory A pointer to receive the base allocated memory
1059 @return Status. On success, Memory is filled in with the base address allocated
1060 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1062 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1063 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1064 @retval EFI_SUCCESS Pages successfully allocated.
1070 IN EFI_ALLOCATE_TYPE Type
,
1071 IN EFI_MEMORY_TYPE MemoryType
,
1072 IN UINTN NumberOfPages
,
1073 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1081 if (Type
< AllocateAnyPages
|| Type
>= (UINTN
) MaxAllocateType
) {
1082 return EFI_INVALID_PARAMETER
;
1085 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
1086 MemoryType
== EfiConventionalMemory
) {
1087 return EFI_INVALID_PARAMETER
;
1090 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1092 if (MemoryType
== EfiACPIReclaimMemory
||
1093 MemoryType
== EfiACPIMemoryNVS
||
1094 MemoryType
== EfiRuntimeServicesCode
||
1095 MemoryType
== EfiRuntimeServicesData
) {
1097 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1100 if (Type
== AllocateAddress
) {
1101 if ((*Memory
& (Alignment
- 1)) != 0) {
1102 return EFI_NOT_FOUND
;
1106 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1107 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1110 // If this is for below a particular address, then
1115 // The max address is the max natively addressable address for the processor
1117 MaxAddress
= EFI_MAX_ADDRESS
;
1119 if (Type
== AllocateMaxAddress
) {
1123 CoreAcquireMemoryLock ();
1126 // If not a specific address, then find an address to allocate
1128 if (Type
!= AllocateAddress
) {
1129 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1131 Status
= EFI_OUT_OF_RESOURCES
;
1137 // Convert pages from FreeMemory to the requested type
1139 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1142 CoreReleaseMemoryLock ();
1144 if (!EFI_ERROR (Status
)) {
1153 Frees previous allocated pages.
1155 @param Memory Base address of memory being freed
1156 @param NumberOfPages The number of pages to free
1158 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1159 @retval EFI_INVALID_PARAMETER Address not aligned
1160 @return EFI_SUCCESS -Pages successfully freed.
1166 IN EFI_PHYSICAL_ADDRESS Memory
,
1167 IN UINTN NumberOfPages
1178 CoreAcquireMemoryLock ();
1181 // Find the entry that the covers the range
1184 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1185 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1186 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1190 if (Link
== &gMemoryMap
) {
1191 CoreReleaseMemoryLock ();
1192 return EFI_NOT_FOUND
;
1195 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1197 if (Entry
->Type
== EfiACPIReclaimMemory
||
1198 Entry
->Type
== EfiACPIMemoryNVS
||
1199 Entry
->Type
== EfiRuntimeServicesCode
||
1200 Entry
->Type
== EfiRuntimeServicesData
) {
1202 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1206 if ((Memory
& (Alignment
- 1)) != 0) {
1207 CoreReleaseMemoryLock ();
1208 return EFI_INVALID_PARAMETER
;
1211 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1212 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1214 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1216 CoreReleaseMemoryLock ();
1218 if (EFI_ERROR (Status
)) {
1223 // Destroy the contents
1225 if (Memory
< EFI_MAX_ADDRESS
) {
1226 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
)Memory
, NumberOfPages
<< EFI_PAGE_SHIFT
);
1234 This function returns a copy of the current memory map. The map is an array of
1235 memory descriptors, each of which describes a contiguous block of memory.
1237 @param MemoryMapSize A pointer to the size, in bytes, of the
1238 MemoryMap buffer. On input, this is the size of
1239 the buffer allocated by the caller. On output,
1240 it is the size of the buffer returned by the
1241 firmware if the buffer was large enough, or the
1242 size of the buffer needed to contain the map if
1243 the buffer was too small.
1244 @param MemoryMap A pointer to the buffer in which firmware places
1245 the current memory map.
1246 @param MapKey A pointer to the location in which firmware
1247 returns the key for the current memory map.
1248 @param DescriptorSize A pointer to the location in which firmware
1249 returns the size, in bytes, of an individual
1250 EFI_MEMORY_DESCRIPTOR.
1251 @param DescriptorVersion A pointer to the location in which firmware
1252 returns the version number associated with the
1253 EFI_MEMORY_DESCRIPTOR.
1255 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1257 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1258 buffer size needed to hold the memory map is
1259 returned in MemoryMapSize.
1260 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1266 IN OUT UINTN
*MemoryMapSize
,
1267 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1269 OUT UINTN
*DescriptorSize
,
1270 OUT UINT32
*DescriptorVersion
1276 UINTN NumberOfRuntimeEntries
;
1279 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1280 EFI_MEMORY_TYPE Type
;
1283 // Make sure the parameters are valid
1285 if (MemoryMapSize
== NULL
) {
1286 return EFI_INVALID_PARAMETER
;
1289 CoreAcquireGcdMemoryLock ();
1292 // Count the number of Reserved and MMIO entries that are marked for runtime use
1294 NumberOfRuntimeEntries
= 0;
1295 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1296 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1297 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1298 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1299 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1300 NumberOfRuntimeEntries
++;
1305 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1308 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1309 // prevent people from having pointer math bugs in their code.
1310 // now you have to use *DescriptorSize to make things work.
1312 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1314 if (DescriptorSize
!= NULL
) {
1315 *DescriptorSize
= Size
;
1318 if (DescriptorVersion
!= NULL
) {
1319 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1322 CoreAcquireMemoryLock ();
1325 // Compute the buffer size needed to fit the entire map
1327 BufferSize
= Size
* NumberOfRuntimeEntries
;
1328 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1332 if (*MemoryMapSize
< BufferSize
) {
1333 Status
= EFI_BUFFER_TOO_SMALL
;
1337 if (MemoryMap
== NULL
) {
1338 Status
= EFI_INVALID_PARAMETER
;
1345 ZeroMem (MemoryMap
, BufferSize
);
1346 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1347 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1348 ASSERT (Entry
->VirtualStart
== 0);
1351 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1353 MemoryMap
->Type
= Entry
->Type
;
1354 MemoryMap
->PhysicalStart
= Entry
->Start
;
1355 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1356 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1358 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1359 // memory type bin and needs to be converted to the same memory type as the rest of the
1360 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1361 // improves the chances for a successful S4 resume in the presence of minor page allocation
1362 // differences across reboots.
1364 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1365 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1366 if (mMemoryTypeStatistics
[Type
].Special
&&
1367 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1368 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1369 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1370 MemoryMap
->Type
= Type
;
1374 MemoryMap
->Attribute
= Entry
->Attribute
;
1375 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1376 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1379 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1382 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1383 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1384 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1385 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1386 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1388 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1389 MemoryMap
->VirtualStart
= 0;
1390 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1391 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1393 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1394 MemoryMap
->Type
= EfiReservedMemoryType
;
1395 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1396 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1397 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1399 MemoryMap
->Type
= EfiMemoryMappedIO
;
1403 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1408 Status
= EFI_SUCCESS
;
1412 CoreReleaseMemoryLock ();
1414 CoreReleaseGcdMemoryLock ();
1417 // Update the map key finally
1419 if (MapKey
!= NULL
) {
1420 *MapKey
= mMemoryMapKey
;
1423 *MemoryMapSize
= BufferSize
;
1430 Internal function. Used by the pool functions to allocate pages
1431 to back pool allocation requests.
1433 @param PoolType The type of memory for the new pool pages
1434 @param NumberOfPages No of pages to allocate
1435 @param Alignment Bits to align.
1437 @return The allocated memory, or NULL
1441 CoreAllocatePoolPages (
1442 IN EFI_MEMORY_TYPE PoolType
,
1443 IN UINTN NumberOfPages
,
1450 // Find the pages to convert
1452 Start
= FindFreePages (EFI_MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1455 // Convert it to boot services data
1458 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages
));
1460 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1463 return (VOID
*)(UINTN
) Start
;
1468 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1470 @param Memory The base address to free
1471 @param NumberOfPages The number of pages to free
1476 IN EFI_PHYSICAL_ADDRESS Memory
,
1477 IN UINTN NumberOfPages
1480 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1486 Make sure the memory map is following all the construction rules,
1487 it is the last time to check memory map error before exit boot services.
1489 @param MapKey Memory map key
1491 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1493 @retval EFI_SUCCESS Valid memory map.
1497 CoreTerminateMemoryMap (
1505 Status
= EFI_SUCCESS
;
1507 CoreAcquireMemoryLock ();
1509 if (MapKey
== mMemoryMapKey
) {
1512 // Make sure the memory map is following all the construction rules
1513 // This is the last chance we will be able to display any messages on
1514 // the console devices.
1517 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1518 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1519 if (Entry
->Attribute
& EFI_MEMORY_RUNTIME
) {
1520 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1521 DEBUG((DEBUG_ERROR
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1522 CoreReleaseMemoryLock ();
1523 return EFI_INVALID_PARAMETER
;
1525 if (Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1526 DEBUG((DEBUG_ERROR
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1527 CoreReleaseMemoryLock ();
1528 return EFI_INVALID_PARAMETER
;
1530 if ((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1531 DEBUG((DEBUG_ERROR
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1532 CoreReleaseMemoryLock ();
1533 return EFI_INVALID_PARAMETER
;
1539 // The map key they gave us matches what we expect. Fall through and
1540 // return success. In an ideal world we would clear out all of
1541 // EfiBootServicesCode and EfiBootServicesData. However this function
1542 // is not the last one called by ExitBootServices(), so we have to
1543 // preserve the memory contents.
1546 Status
= EFI_INVALID_PARAMETER
;
1549 CoreReleaseMemoryLock ();