2 UEFI Memory page management functions.
4 Copyright (c) 2007 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)
21 // Entry for tracking the memory regions for each memory type to coalesce similar memory types
24 EFI_PHYSICAL_ADDRESS BaseAddress
;
25 EFI_PHYSICAL_ADDRESS MaximumAddress
;
26 UINT64 CurrentNumberOfPages
;
28 UINTN InformationIndex
;
31 } EFI_MEMORY_TYPE_STAISTICS
;
34 // MemoryMap - The current memory map
36 UINTN mMemoryMapKey
= 0;
38 #define MAX_MAP_DEPTH 6
41 /// mMapDepth - depth of new descriptor stack
45 /// mMapStack - space to use as temp storage to build new map descriptors
47 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
48 UINTN mFreeMapStack
= 0;
50 /// This list maintain the free memory map list
52 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
53 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
55 EFI_MEMORY_TYPE_STAISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
56 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiReservedMemoryType
57 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderCode
58 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderData
59 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesCode
60 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesData
61 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesCode
62 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesData
63 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiConventionalMemory
64 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiUnusableMemory
65 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIReclaimMemory
66 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIMemoryNVS
67 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIO
68 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIOPortSpace
69 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiPalCode
70 { 0, MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
} // EfiMaxMemoryType
73 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress
= MAX_ADDRESS
;
75 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
76 { EfiReservedMemoryType
, 0 },
79 { EfiBootServicesCode
, 0 },
80 { EfiBootServicesData
, 0 },
81 { EfiRuntimeServicesCode
, 0 },
82 { EfiRuntimeServicesData
, 0 },
83 { EfiConventionalMemory
, 0 },
84 { EfiUnusableMemory
, 0 },
85 { EfiACPIReclaimMemory
, 0 },
86 { EfiACPIMemoryNVS
, 0 },
87 { EfiMemoryMappedIO
, 0 },
88 { EfiMemoryMappedIOPortSpace
, 0 },
90 { EfiMaxMemoryType
, 0 }
95 Enter critical section by gaining lock on gMemoryLock.
99 CoreAcquireMemoryLock (
103 CoreAcquireLock (&gMemoryLock
);
109 Exit critical section by releasing lock on gMemoryLock.
113 CoreReleaseMemoryLock (
117 CoreReleaseLock (&gMemoryLock
);
124 Internal function. Removes a descriptor entry.
126 @param Entry The entry to remove
130 RemoveMemoryMapEntry (
131 IN OUT MEMORY_MAP
*Entry
134 RemoveEntryList (&Entry
->Link
);
135 Entry
->Link
.ForwardLink
= NULL
;
137 if (Entry
->FromPages
) {
139 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
141 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
146 Internal function. Adds a ranges to the memory map.
147 The range must not already exist in the map.
149 @param Type The type of memory range to add
150 @param Start The starting address in the memory range Must be
152 @param End The last address in the range Must be the last
154 @param Attribute The attributes of the memory range to add
159 IN EFI_MEMORY_TYPE Type
,
160 IN EFI_PHYSICAL_ADDRESS Start
,
161 IN EFI_PHYSICAL_ADDRESS End
,
168 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
169 ASSERT (End
> Start
) ;
171 ASSERT_LOCKED (&gMemoryLock
);
173 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
176 // Memory map being altered so updated key
181 // UEFI 2.0 added an event group for notificaiton on memory map changes.
182 // So we need to signal this Event Group every time the memory map changes.
183 // If we are in EFI 1.10 compatability mode no event groups will be
184 // found and nothing will happen we we call this function. These events
185 // will get signaled but since a lock is held around the call to this
186 // function the notificaiton events will only be called after this funciton
187 // returns and the lock is released.
189 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
192 // Look for adjoining memory descriptor
195 // Two memory descriptors can only be merged if they have the same Type
196 // and the same Attribute
199 Link
= gMemoryMap
.ForwardLink
;
200 while (Link
!= &gMemoryMap
) {
201 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
202 Link
= Link
->ForwardLink
;
204 if (Entry
->Type
!= Type
) {
208 if (Entry
->Attribute
!= Attribute
) {
212 if (Entry
->End
+ 1 == Start
) {
214 Start
= Entry
->Start
;
215 RemoveMemoryMapEntry (Entry
);
217 } else if (Entry
->Start
== End
+ 1) {
220 RemoveMemoryMapEntry (Entry
);
228 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
229 mMapStack
[mMapDepth
].FromPages
= FALSE
;
230 mMapStack
[mMapDepth
].Type
= Type
;
231 mMapStack
[mMapDepth
].Start
= Start
;
232 mMapStack
[mMapDepth
].End
= End
;
233 mMapStack
[mMapDepth
].VirtualStart
= 0;
234 mMapStack
[mMapDepth
].Attribute
= Attribute
;
235 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
238 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
244 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
245 If the list is emtry, then allocate a new page to refuel the list.
246 Please Note this algorithm to allocate the memory map descriptor has a property
247 that the memory allocated for memory entries always grows, and will never really be freed
248 For example, if the current boot uses 2000 memory map entries at the maximum point, but
249 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
250 memory map entries is still allocated from EfiBootServicesMemory.
253 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
257 AllocateMemoryMapEntry (
261 MEMORY_MAP
* FreeDescriptorEntries
;
265 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
267 // The list is empty, to allocate one page to refuel the list
269 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
270 if(FreeDescriptorEntries
!= NULL
) {
272 // Enque the free memmory map entries into the list
274 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
275 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
276 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
283 // dequeue the first descriptor from the list
285 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
286 RemoveEntryList (&Entry
->Link
);
293 Internal function. Moves any memory descriptors that are on the
294 temporary descriptor stack to heap.
298 CoreFreeMemoryMapStack (
306 ASSERT_LOCKED (&gMemoryLock
);
309 // If already freeing the map stack, then return
311 if (mFreeMapStack
!= 0) {
316 // Move the temporary memory descriptor stack into pool
320 while (mMapDepth
!= 0) {
322 // Deque an memory map entry from mFreeMemoryMapEntryList
324 Entry
= AllocateMemoryMapEntry ();
329 // Update to proper entry
333 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
336 // Move this entry to general memory
338 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
339 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
341 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
342 Entry
->FromPages
= TRUE
;
345 // Find insertion location
347 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
348 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
349 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
354 InsertTailList (Link2
, &Entry
->Link
);
358 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
359 // so here no need to move it to memory.
361 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
369 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
373 PromoteMemoryResource (
378 EFI_GCD_MAP_ENTRY
*Entry
;
380 DEBUG ((DEBUG_PAGE
, "Promote the memory resource\n"));
382 CoreAcquireGcdMemoryLock ();
384 Link
= mGcdMemorySpaceMap
.ForwardLink
;
385 while (Link
!= &mGcdMemorySpaceMap
) {
387 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
389 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
390 Entry
->EndAddress
< MAX_ADDRESS
&&
391 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
392 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
394 // Update the GCD map
396 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
397 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
398 Entry
->ImageHandle
= gDxeCoreImageHandle
;
399 Entry
->DeviceHandle
= NULL
;
402 // Add to allocable system memory resource
406 EfiConventionalMemory
,
409 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
411 CoreFreeMemoryMapStack ();
415 Link
= Link
->ForwardLink
;
418 CoreReleaseGcdMemoryLock ();
425 Called to initialize the memory map and add descriptors to
426 the current descriptor list.
427 The first descriptor that is added must be general usable
428 memory as the addition allocates heap.
430 @param Type The type of memory to add
431 @param Start The starting address in the memory range Must be
433 @param NumberOfPages The number of pages in the range
434 @param Attribute Attributes of the memory to add
436 @return None. The range is added to the memory map
440 CoreAddMemoryDescriptor (
441 IN EFI_MEMORY_TYPE Type
,
442 IN EFI_PHYSICAL_ADDRESS Start
,
443 IN UINT64 NumberOfPages
,
447 EFI_PHYSICAL_ADDRESS End
;
452 if ((Start
& EFI_PAGE_MASK
) != 0) {
456 if (Type
>= EfiMaxMemoryType
&& Type
<= 0x7fffffff) {
460 CoreAcquireMemoryLock ();
461 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
462 CoreAddRange (Type
, Start
, End
, Attribute
);
463 CoreFreeMemoryMapStack ();
464 CoreReleaseMemoryLock ();
467 // Check to see if the statistics for the different memory types have already been established
469 if (mMemoryTypeInformationInitialized
) {
474 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
476 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
478 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
480 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
481 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
485 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
487 // Allocate pages for the current memory type from the top of available memory
489 Status
= CoreAllocatePages (
492 gMemoryTypeInformation
[Index
].NumberOfPages
,
493 &mMemoryTypeStatistics
[Type
].BaseAddress
495 if (EFI_ERROR (Status
)) {
497 // If an error occurs allocating the pages for the current memory type, then
498 // free all the pages allocates for the previous memory types and return. This
499 // operation with be retied when/if more memory is added to the system
501 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
503 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
505 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
506 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
510 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
512 mMemoryTypeStatistics
[Type
].BaseAddress
,
513 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
515 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
516 mMemoryTypeStatistics
[Type
].MaximumAddress
= MAX_ADDRESS
;
523 // Compute the address at the top of the current statistics
525 mMemoryTypeStatistics
[Type
].MaximumAddress
=
526 mMemoryTypeStatistics
[Type
].BaseAddress
+
527 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
530 // If the current base address is the lowest address so far, then update the default
533 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
534 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
540 // There was enough system memory for all the the memory types were allocated. So,
541 // those memory areas can be freed for future allocations, and all future memory
542 // allocations can occur within their respective bins
544 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
546 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
548 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
549 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
553 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
555 mMemoryTypeStatistics
[Type
].BaseAddress
,
556 gMemoryTypeInformation
[Index
].NumberOfPages
558 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
559 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
564 // If the number of pages reserved for a memory type is 0, then all allocations for that type
565 // should be in the default range.
567 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
568 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
569 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
570 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
573 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
574 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== MAX_ADDRESS
) {
575 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
579 mMemoryTypeInformationInitialized
= TRUE
;
584 Internal function. Converts a memory range to the specified type.
585 The range must exist in the memory map.
587 @param Start The first address of the range Must be page
589 @param NumberOfPages The number of pages to convert
590 @param NewType The new type for the memory range
592 @retval EFI_INVALID_PARAMETER Invalid parameter
593 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
594 range or convertion not allowed.
595 @retval EFI_SUCCESS Successfully converts the memory range to the
602 IN UINT64 NumberOfPages
,
603 IN EFI_MEMORY_TYPE NewType
607 UINT64 NumberOfBytes
;
615 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
616 End
= Start
+ NumberOfBytes
- 1;
618 ASSERT (NumberOfPages
);
619 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
620 ASSERT (End
> Start
) ;
621 ASSERT_LOCKED (&gMemoryLock
);
623 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
> (Start
+ NumberOfBytes
))) {
624 return EFI_INVALID_PARAMETER
;
628 // Convert the entire range
631 while (Start
< End
) {
634 // Find the entry that the covers the range
636 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
637 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
639 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
644 if (Link
== &gMemoryMap
) {
645 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
646 return EFI_NOT_FOUND
;
650 // Convert range to the end, or to the end of the descriptor
651 // if that's all we've got
654 if (Entry
->End
< End
) {
655 RangeEnd
= Entry
->End
;
658 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to %d\n", Start
, RangeEnd
, NewType
));
661 // Debug code - verify conversion is allowed
663 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
664 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types\n"));
665 return EFI_NOT_FOUND
;
669 // Update counters for the number of pages allocated to each memory type
671 if (Entry
->Type
>= 0 && Entry
->Type
< EfiMaxMemoryType
) {
672 if (Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&&
673 Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) {
674 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
675 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
677 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
682 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
) {
683 if (Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
684 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
685 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
>
686 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
687 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
693 // Pull range out of descriptor
695 if (Entry
->Start
== Start
) {
700 Entry
->Start
= RangeEnd
+ 1;
702 } else if (Entry
->End
== RangeEnd
) {
707 Entry
->End
= Start
- 1;
712 // Pull it out of the center, clip current
718 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
719 mMapStack
[mMapDepth
].FromPages
= FALSE
;
720 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
721 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
722 mMapStack
[mMapDepth
].End
= Entry
->End
;
725 // Inherit Attribute from the Memory Descriptor that is being clipped
727 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
729 Entry
->End
= Start
- 1;
730 ASSERT (Entry
->Start
< Entry
->End
);
732 Entry
= &mMapStack
[mMapDepth
];
733 InsertTailList (&gMemoryMap
, &Entry
->Link
);
736 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
740 // The new range inherits the same Attribute as the Entry
741 //it is being cut out of
743 Attribute
= Entry
->Attribute
;
746 // If the descriptor is empty, then remove it from the map
748 if (Entry
->Start
== Entry
->End
+ 1) {
749 RemoveMemoryMapEntry (Entry
);
754 // Add our new range in
756 CoreAddRange (NewType
, Start
, RangeEnd
, Attribute
);
759 // Move any map descriptor stack to general pool
761 CoreFreeMemoryMapStack ();
764 // Bump the starting address, and convert the next range
766 Start
= RangeEnd
+ 1;
770 // Converted the whole range, done
779 Internal function. Finds a consecutive free page range below
780 the requested address.
782 @param MaxAddress The address that the range must be below
783 @param NumberOfPages Number of pages needed
784 @param NewType The type of memory the range is going to be
786 @param Alignment Bits to align with
788 @return The base address of the range, or 0 if the range was not found
793 IN UINT64 MaxAddress
,
794 IN UINT64 NumberOfPages
,
795 IN EFI_MEMORY_TYPE NewType
,
799 UINT64 NumberOfBytes
;
803 UINT64 DescNumberOfBytes
;
807 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
811 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
814 // If MaxAddress is not aligned to the end of a page
818 // Change MaxAddress to be 1 page lower
820 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
823 // Set MaxAddress to a page boundary
825 MaxAddress
&= ~EFI_PAGE_MASK
;
828 // Set MaxAddress to end of the page
830 MaxAddress
|= EFI_PAGE_MASK
;
833 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
836 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
837 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
840 // If it's not a free entry, don't bother with it
842 if (Entry
->Type
!= EfiConventionalMemory
) {
846 DescStart
= Entry
->Start
;
847 DescEnd
= Entry
->End
;
850 // If desc is past max allowed address, skip it
852 if (DescStart
>= MaxAddress
) {
857 // If desc ends past max allowed address, clip the end
859 if (DescEnd
>= MaxAddress
) {
860 DescEnd
= MaxAddress
;
863 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
866 // Compute the number of bytes we can used from this
867 // descriptor, and see it's enough to satisfy the request
869 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
871 if (DescNumberOfBytes
>= NumberOfBytes
) {
874 // If this is the best match so far remember it
876 if (DescEnd
> Target
) {
883 // If this is a grow down, adjust target to be the allocation base
885 Target
-= NumberOfBytes
- 1;
888 // If we didn't find a match, return 0
890 if ((Target
& EFI_PAGE_MASK
) != 0) {
899 Internal function. Finds a consecutive free page range below
900 the requested address
902 @param MaxAddress The address that the range must be below
903 @param NoPages Number of pages needed
904 @param NewType The type of memory the range is going to be
906 @param Alignment Bits to align with
908 @return The base address of the range, or 0 if the range was not found.
913 IN UINT64 MaxAddress
,
915 IN EFI_MEMORY_TYPE NewType
,
919 UINT64 NewMaxAddress
;
922 NewMaxAddress
= MaxAddress
;
924 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
&& NewMaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
925 NewMaxAddress
= mMemoryTypeStatistics
[NewType
].MaximumAddress
;
927 if (NewMaxAddress
> mDefaultMaximumAddress
) {
928 NewMaxAddress
= mDefaultMaximumAddress
;
932 Start
= CoreFindFreePagesI (NewMaxAddress
, NoPages
, NewType
, Alignment
);
934 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
937 // Here means there may be no enough memory to use, so try to go through
938 // all the memory descript to promote the untested memory directly
940 PromoteMemoryResource ();
943 // Allocate memory again after the memory resource re-arranged
945 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
955 Allocates pages from the memory map.
957 @param Type The type of allocation to perform
958 @param MemoryType The type of memory to turn the allocated pages
960 @param NumberOfPages The number of pages to allocate
961 @param Memory A pointer to receive the base allocated memory
964 @return Status. On success, Memory is filled in with the base address allocated
965 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
967 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
968 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
969 @retval EFI_SUCCESS Pages successfully allocated.
975 IN EFI_ALLOCATE_TYPE Type
,
976 IN EFI_MEMORY_TYPE MemoryType
,
977 IN UINTN NumberOfPages
,
978 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
986 if (Type
< AllocateAnyPages
|| Type
>= (UINTN
) MaxAllocateType
) {
987 return EFI_INVALID_PARAMETER
;
990 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
991 MemoryType
== EfiConventionalMemory
) {
992 return EFI_INVALID_PARAMETER
;
995 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
997 if (MemoryType
== EfiACPIReclaimMemory
||
998 MemoryType
== EfiACPIMemoryNVS
||
999 MemoryType
== EfiRuntimeServicesCode
||
1000 MemoryType
== EfiRuntimeServicesData
) {
1002 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1005 if (Type
== AllocateAddress
) {
1006 if ((*Memory
& (Alignment
- 1)) != 0) {
1007 return EFI_NOT_FOUND
;
1011 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1012 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1015 // If this is for below a particular address, then
1020 // The max address is the max natively addressable address for the processor
1022 MaxAddress
= MAX_ADDRESS
;
1024 if (Type
== AllocateMaxAddress
) {
1028 CoreAcquireMemoryLock ();
1031 // If not a specific address, then find an address to allocate
1033 if (Type
!= AllocateAddress
) {
1034 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1036 Status
= EFI_OUT_OF_RESOURCES
;
1042 // Convert pages from FreeMemory to the requested type
1044 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1047 CoreReleaseMemoryLock ();
1049 if (!EFI_ERROR (Status
)) {
1058 Frees previous allocated pages.
1060 @param Memory Base address of memory being freed
1061 @param NumberOfPages The number of pages to free
1063 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1064 @retval EFI_INVALID_PARAMETER Address not aligned
1065 @return EFI_SUCCESS -Pages successfully freed.
1071 IN EFI_PHYSICAL_ADDRESS Memory
,
1072 IN UINTN NumberOfPages
1083 CoreAcquireMemoryLock ();
1086 // Find the entry that the covers the range
1089 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1090 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1091 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1095 if (Link
== &gMemoryMap
) {
1096 CoreReleaseMemoryLock ();
1097 return EFI_NOT_FOUND
;
1100 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1102 if (Entry
->Type
== EfiACPIReclaimMemory
||
1103 Entry
->Type
== EfiACPIMemoryNVS
||
1104 Entry
->Type
== EfiRuntimeServicesCode
||
1105 Entry
->Type
== EfiRuntimeServicesData
) {
1107 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1111 if ((Memory
& (Alignment
- 1)) != 0) {
1112 CoreReleaseMemoryLock ();
1113 return EFI_INVALID_PARAMETER
;
1116 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1117 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1119 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1121 CoreReleaseMemoryLock ();
1123 if (EFI_ERROR (Status
)) {
1128 // Destroy the contents
1130 if (Memory
< MAX_ADDRESS
) {
1131 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
)Memory
, NumberOfPages
<< EFI_PAGE_SHIFT
);
1139 This function returns a copy of the current memory map. The map is an array of
1140 memory descriptors, each of which describes a contiguous block of memory.
1142 @param MemoryMapSize A pointer to the size, in bytes, of the
1143 MemoryMap buffer. On input, this is the size of
1144 the buffer allocated by the caller. On output,
1145 it is the size of the buffer returned by the
1146 firmware if the buffer was large enough, or the
1147 size of the buffer needed to contain the map if
1148 the buffer was too small.
1149 @param MemoryMap A pointer to the buffer in which firmware places
1150 the current memory map.
1151 @param MapKey A pointer to the location in which firmware
1152 returns the key for the current memory map.
1153 @param DescriptorSize A pointer to the location in which firmware
1154 returns the size, in bytes, of an individual
1155 EFI_MEMORY_DESCRIPTOR.
1156 @param DescriptorVersion A pointer to the location in which firmware
1157 returns the version number associated with the
1158 EFI_MEMORY_DESCRIPTOR.
1160 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1162 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1163 buffer size needed to hold the memory map is
1164 returned in MemoryMapSize.
1165 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1171 IN OUT UINTN
*MemoryMapSize
,
1172 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1174 OUT UINTN
*DescriptorSize
,
1175 OUT UINT32
*DescriptorVersion
1181 UINTN NumberOfRuntimeEntries
;
1184 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1185 EFI_MEMORY_TYPE Type
;
1188 // Make sure the parameters are valid
1190 if (MemoryMapSize
== NULL
) {
1191 return EFI_INVALID_PARAMETER
;
1194 CoreAcquireGcdMemoryLock ();
1197 // Count the number of Reserved and MMIO entries that are marked for runtime use
1199 NumberOfRuntimeEntries
= 0;
1200 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1201 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1202 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1203 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1204 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1205 NumberOfRuntimeEntries
++;
1210 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1213 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1214 // prevent people from having pointer math bugs in their code.
1215 // now you have to use *DescriptorSize to make things work.
1217 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1219 if (DescriptorSize
!= NULL
) {
1220 *DescriptorSize
= Size
;
1223 if (DescriptorVersion
!= NULL
) {
1224 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1227 CoreAcquireMemoryLock ();
1230 // Compute the buffer size needed to fit the entire map
1232 BufferSize
= Size
* NumberOfRuntimeEntries
;
1233 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1237 if (*MemoryMapSize
< BufferSize
) {
1238 Status
= EFI_BUFFER_TOO_SMALL
;
1242 if (MemoryMap
== NULL
) {
1243 Status
= EFI_INVALID_PARAMETER
;
1250 ZeroMem (MemoryMap
, BufferSize
);
1251 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1252 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1253 ASSERT (Entry
->VirtualStart
== 0);
1256 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1258 MemoryMap
->Type
= Entry
->Type
;
1259 MemoryMap
->PhysicalStart
= Entry
->Start
;
1260 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1261 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1263 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1264 // memory type bin and needs to be converted to the same memory type as the rest of the
1265 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1266 // improves the chances for a successful S4 resume in the presence of minor page allocation
1267 // differences across reboots.
1269 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1270 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1271 if (mMemoryTypeStatistics
[Type
].Special
&&
1272 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1273 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1274 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1275 MemoryMap
->Type
= Type
;
1279 MemoryMap
->Attribute
= Entry
->Attribute
;
1280 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1281 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1284 MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, Size
);
1287 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1288 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1289 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1290 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1291 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1293 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries
1294 // that are marked for runtime use
1296 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1297 MemoryMap
->VirtualStart
= 0;
1298 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1299 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1301 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1302 MemoryMap
->Type
= EfiReservedMemoryType
;
1303 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1304 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1305 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1307 MemoryMap
->Type
= EfiMemoryMappedIO
;
1311 MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, Size
);
1316 Status
= EFI_SUCCESS
;
1320 CoreReleaseMemoryLock ();
1322 CoreReleaseGcdMemoryLock ();
1325 // Update the map key finally
1327 if (MapKey
!= NULL
) {
1328 *MapKey
= mMemoryMapKey
;
1331 *MemoryMapSize
= BufferSize
;
1338 Internal function. Used by the pool functions to allocate pages
1339 to back pool allocation requests.
1341 @param PoolType The type of memory for the new pool pages
1342 @param NumberOfPages No of pages to allocate
1343 @param Alignment Bits to align.
1345 @return The allocated memory, or NULL
1349 CoreAllocatePoolPages (
1350 IN EFI_MEMORY_TYPE PoolType
,
1351 IN UINTN NumberOfPages
,
1358 // Find the pages to convert
1360 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1363 // Convert it to boot services data
1366 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1368 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1371 return (VOID
*)(UINTN
) Start
;
1376 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1378 @param Memory The base address to free
1379 @param NumberOfPages The number of pages to free
1384 IN EFI_PHYSICAL_ADDRESS Memory
,
1385 IN UINTN NumberOfPages
1388 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1394 Make sure the memory map is following all the construction rules,
1395 it is the last time to check memory map error before exit boot services.
1397 @param MapKey Memory map key
1399 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1401 @retval EFI_SUCCESS Valid memory map.
1405 CoreTerminateMemoryMap (
1413 Status
= EFI_SUCCESS
;
1415 CoreAcquireMemoryLock ();
1417 if (MapKey
== mMemoryMapKey
) {
1420 // Make sure the memory map is following all the construction rules
1421 // This is the last chance we will be able to display any messages on
1422 // the console devices.
1425 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1426 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1427 if ((Entry
->Attribute
& EFI_MEMORY_RUNTIME
) != 0) {
1428 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1429 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1430 Status
= EFI_INVALID_PARAMETER
;
1433 if ((Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1434 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1435 Status
= EFI_INVALID_PARAMETER
;
1438 if (((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1439 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1440 Status
= EFI_INVALID_PARAMETER
;
1447 // The map key they gave us matches what we expect. Fall through and
1448 // return success. In an ideal world we would clear out all of
1449 // EfiBootServicesCode and EfiBootServicesData. However this function
1450 // is not the last one called by ExitBootServices(), so we have to
1451 // preserve the memory contents.
1454 Status
= EFI_INVALID_PARAMETER
;
1458 CoreReleaseMemoryLock ();