3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI Memory page management
27 #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)
30 // Entry for tracking the memory regions for each memory type to help cooalese like memory types
33 EFI_PHYSICAL_ADDRESS BaseAddress
;
34 EFI_PHYSICAL_ADDRESS MaximumAddress
;
35 UINT64 CurrentNumberOfPages
;
37 UINTN InformationIndex
;
40 } EFI_MEMORY_TYPE_STAISTICS
;
43 // MemoryMap - The current memory map
45 UINTN mMemoryMapKey
= 0;
48 // mMapStack - space to use as temp storage to build new map descriptors
49 // mMapDepth - depth of new descriptor stack
52 #define MAX_MAP_DEPTH 6
54 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
55 UINTN mFreeMapStack
= 0;
57 // This list maintain the free memory map list
59 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
60 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
62 EFI_MEMORY_TYPE_STAISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
63 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiReservedMemoryType
64 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderCode
65 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderData
66 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesCode
67 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesData
68 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesCode
69 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesData
70 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiConventionalMemory
71 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiUnusableMemory
72 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIReclaimMemory
73 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIMemoryNVS
74 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIO
75 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIOPortSpace
76 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiPalCode
77 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
} // EfiMaxMemoryType
80 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress
= EFI_MAX_ADDRESS
;
82 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
83 { EfiReservedMemoryType
, 0 },
86 { EfiBootServicesCode
, 0 },
87 { EfiBootServicesData
, 0 },
88 { EfiRuntimeServicesCode
, 0 },
89 { EfiRuntimeServicesData
, 0 },
90 { EfiConventionalMemory
, 0 },
91 { EfiUnusableMemory
, 0 },
92 { EfiACPIReclaimMemory
, 0 },
93 { EfiACPIMemoryNVS
, 0 },
94 { EfiMemoryMappedIO
, 0 },
95 { EfiMemoryMappedIOPortSpace
, 0 },
97 { EfiMaxMemoryType
, 0 }
101 // Internal prototypes
105 PromoteMemoryResource (
112 IN EFI_MEMORY_TYPE Type
,
113 IN EFI_PHYSICAL_ADDRESS Start
,
114 IN EFI_PHYSICAL_ADDRESS End
,
120 CoreFreeMemoryMapStack (
128 IN UINT64 NumberOfPages
,
129 IN EFI_MEMORY_TYPE NewType
134 RemoveMemoryMapEntry (
140 AllocateMemoryMapEntry (
145 CoreAcquireMemoryLock (
152 Enter critical section by gaining lock on gMemoryLock
164 CoreAcquireLock (&gMemoryLock
);
169 CoreReleaseMemoryLock (
176 Exit critical section by releasing lock on gMemoryLock
188 CoreReleaseLock (&gMemoryLock
);
193 PromoteMemoryResource (
200 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
213 EFI_GCD_MAP_ENTRY
*Entry
;
215 DEBUG ((EFI_D_ERROR
| EFI_D_PAGE
, "Promote the memory resource\n"));
217 CoreAcquireGcdMemoryLock ();
219 Link
= mGcdMemorySpaceMap
.ForwardLink
;
220 while (Link
!= &mGcdMemorySpaceMap
) {
222 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
224 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
225 Entry
->EndAddress
< EFI_MAX_ADDRESS
&&
226 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
227 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
229 // Update the GCD map
231 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
232 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
233 Entry
->ImageHandle
= gDxeCoreImageHandle
;
234 Entry
->DeviceHandle
= NULL
;
237 // Add to allocable system memory resource
241 EfiConventionalMemory
,
244 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
246 CoreFreeMemoryMapStack ();
250 Link
= Link
->ForwardLink
;
253 CoreReleaseGcdMemoryLock ();
259 CoreAddMemoryDescriptor (
260 IN EFI_MEMORY_TYPE Type
,
261 IN EFI_PHYSICAL_ADDRESS Start
,
262 IN UINT64 NumberOfPages
,
269 Called to initialize the memory map and add descriptors to
270 the current descriptor list.
272 The first descriptor that is added must be general usable
273 memory as the addition allocates heap.
277 Type - The type of memory to add
279 Start - The starting address in the memory range
282 NumberOfPages - The number of pages in the range
284 Attribute - Attributes of the memory to add
288 None. The range is added to the memory map
292 EFI_PHYSICAL_ADDRESS End
;
297 if ((Start
& EFI_PAGE_MASK
) != 0) {
301 if (Type
>= EfiMaxMemoryType
&& Type
<= 0x7fffffff) {
305 CoreAcquireMemoryLock ();
306 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
307 CoreAddRange (Type
, Start
, End
, Attribute
);
308 CoreFreeMemoryMapStack ();
309 CoreReleaseMemoryLock ();
312 // Check to see if the statistics for the different memory types have already been established
314 if (mMemoryTypeInformationInitialized
) {
319 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
321 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
323 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
325 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
326 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
330 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
332 // Allocate pages for the current memory type from the top of available memory
334 Status
= CoreAllocatePages (
337 gMemoryTypeInformation
[Index
].NumberOfPages
,
338 &mMemoryTypeStatistics
[Type
].BaseAddress
340 if (EFI_ERROR (Status
)) {
342 // If an error occurs allocating the pages for the current memory type, then
343 // free all the pages allocates for the previous memory types and return. This
344 // operation with be retied when/if more memory is added to the system
346 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
348 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
350 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
351 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
355 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
357 mMemoryTypeStatistics
[Type
].BaseAddress
,
358 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
360 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
361 mMemoryTypeStatistics
[Type
].MaximumAddress
= EFI_MAX_ADDRESS
;
368 // Compute the address at the top of the current statistics
370 mMemoryTypeStatistics
[Type
].MaximumAddress
=
371 mMemoryTypeStatistics
[Type
].BaseAddress
+
372 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
375 // If the current base address is the lowest address so far, then update the default
378 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
379 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
385 // There was enough system memory for all the the memory types were allocated. So,
386 // those memory areas can be freed for future allocations, and all future memory
387 // allocations can occur within their respective bins
389 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
391 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
393 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
394 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
398 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
400 mMemoryTypeStatistics
[Type
].BaseAddress
,
401 gMemoryTypeInformation
[Index
].NumberOfPages
403 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
404 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
409 // If the number of pages reserved for a memory type is 0, then all allocations for that type
410 // should be in the default range.
412 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
413 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
414 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
415 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
418 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
419 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== EFI_MAX_ADDRESS
) {
420 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
424 mMemoryTypeInformationInitialized
= TRUE
;
431 IN EFI_MEMORY_TYPE Type
,
432 IN EFI_PHYSICAL_ADDRESS Start
,
433 IN EFI_PHYSICAL_ADDRESS End
,
440 Internal function. Adds a ranges to the memory map.
441 The range must not already exist in the map.
445 Type - The type of memory range to add
447 Start - The starting address in the memory range
448 Must be paged aligned
450 End - The last address in the range
451 Must be the last byte of a page
453 Attribute - The attributes of the memory range to add
457 None. The range is added to the memory map
464 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
465 ASSERT (End
> Start
) ;
467 ASSERT_LOCKED (&gMemoryLock
);
469 DEBUG ((EFI_D_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
472 // Memory map being altered so updated key
477 // UEFI 2.0 added an event group for notificaiton on memory map changes.
478 // So we need to signal this Event Group every time the memory map changes.
479 // If we are in EFI 1.10 compatability mode no event groups will be
480 // found and nothing will happen we we call this function. These events
481 // will get signaled but since a lock is held around the call to this
482 // function the notificaiton events will only be called after this funciton
483 // returns and the lock is released.
485 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
488 // Look for adjoining memory descriptor
491 // Two memory descriptors can only be merged if they have the same Type
492 // and the same Attribute
495 Link
= gMemoryMap
.ForwardLink
;
496 while (Link
!= &gMemoryMap
) {
497 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
498 Link
= Link
->ForwardLink
;
500 if (Entry
->Type
!= Type
) {
504 if (Entry
->Attribute
!= Attribute
) {
508 if (Entry
->End
+ 1 == Start
) {
510 Start
= Entry
->Start
;
511 RemoveMemoryMapEntry (Entry
);
513 } else if (Entry
->Start
== End
+ 1) {
516 RemoveMemoryMapEntry (Entry
);
524 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
525 mMapStack
[mMapDepth
].FromPages
= FALSE
;
526 mMapStack
[mMapDepth
].Type
= Type
;
527 mMapStack
[mMapDepth
].Start
= Start
;
528 mMapStack
[mMapDepth
].End
= End
;
529 mMapStack
[mMapDepth
].VirtualStart
= 0;
530 mMapStack
[mMapDepth
].Attribute
= Attribute
;
531 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
534 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
541 CoreFreeMemoryMapStack (
548 Internal function. Moves any memory descriptors that are on the
549 temporary descriptor stack to heap.
565 ASSERT_LOCKED (&gMemoryLock
);
568 // If already freeing the map stack, then return
575 // Move the temporary memory descriptor stack into pool
581 // Deque an memory map entry from mFreeMemoryMapEntryList
583 Entry
= AllocateMemoryMapEntry ();
588 // Update to proper entry
592 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
595 // Move this entry to general memory
597 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
598 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
600 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
601 Entry
->FromPages
= TRUE
;
604 // Find insertion location
606 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
607 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
608 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
613 InsertTailList (Link2
, &Entry
->Link
);
617 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
618 // so here no need to move it to memory.
620 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
629 RemoveMemoryMapEntry (
636 Internal function. Removes a descriptor entry.
640 Entry - The entry to remove
648 RemoveEntryList (&Entry
->Link
);
649 Entry
->Link
.ForwardLink
= NULL
;
651 if (Entry
->FromPages
) {
653 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
655 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
661 AllocateMemoryMapEntry (
668 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
669 If the list is emtry, then allocate a new page to refuel the list.
670 Please Note this algorithm to allocate the memory map descriptor has a property
671 that the memory allocated for memory entries always grows, and will never really be freed
672 For example, if the current boot uses 2000 memory map entries at the maximum point, but
673 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
674 memory map entries is still allocated from EfiBootServicesMemory.
682 The Memory map descriptor dequed from the mFreeMemoryMapEntryList
686 MEMORY_MAP
* FreeDescriptorEntries
;
690 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
692 // The list is empty, to allocate one page to refuel the list
694 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
695 if(FreeDescriptorEntries
!= NULL
) {
697 // Enque the free memmory map entries into the list
699 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
700 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
701 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
708 // dequeue the first descriptor from the list
710 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
711 RemoveEntryList (&Entry
->Link
);
720 IN UINT64 NumberOfPages
,
721 IN EFI_MEMORY_TYPE NewType
727 Internal function. Converts a memory range to the specified type.
728 The range must exist in the memory map.
732 Start - The first address of the range
735 NumberOfPages - The number of pages to convert
737 NewType - The new type for the memory range
741 EFI_INVALID_PARAMETER - Invalid parameter
743 EFI_NOT_FOUND - Could not find a descriptor cover the specified range
744 or convertion not allowed.
746 EFI_SUCCESS - Successfully converts the memory range to the specified type.
751 UINT64 NumberOfBytes
;
759 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
760 End
= Start
+ NumberOfBytes
- 1;
762 ASSERT (NumberOfPages
);
763 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
764 ASSERT (End
> Start
) ;
765 ASSERT_LOCKED (&gMemoryLock
);
767 if (NumberOfPages
== 0 || (Start
& EFI_PAGE_MASK
) || (Start
> (Start
+ NumberOfBytes
))) {
768 return EFI_INVALID_PARAMETER
;
772 // Convert the entire range
775 while (Start
< End
) {
778 // Find the entry that the covers the range
780 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
781 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
783 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
788 if (Link
== &gMemoryMap
) {
789 DEBUG ((EFI_D_ERROR
| EFI_D_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
790 return EFI_NOT_FOUND
;
794 // Convert range to the end, or to the end of the descriptor
795 // if that's all we've got
798 if (Entry
->End
< End
) {
799 RangeEnd
= Entry
->End
;
802 DEBUG ((EFI_D_PAGE
, "ConvertRange: %lx-%lx to %d\n", Start
, RangeEnd
, NewType
));
805 // Debug code - verify conversion is allowed
807 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
808 DEBUG ((EFI_D_ERROR
, "ConvertPages: Incompatible memory types\n"));
809 return EFI_NOT_FOUND
;
813 // Update counters for the number of pages allocated to each memory type
815 if (Entry
->Type
>= 0 && Entry
->Type
< EfiMaxMemoryType
) {
816 if (Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&&
817 Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) {
818 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
819 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
821 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
826 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
) {
827 if (Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
828 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
829 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
>
830 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
831 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
837 // Pull range out of descriptor
839 if (Entry
->Start
== Start
) {
844 Entry
->Start
= RangeEnd
+ 1;
846 } else if (Entry
->End
== RangeEnd
) {
851 Entry
->End
= Start
- 1;
856 // Pull it out of the center, clip current
862 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
863 mMapStack
[mMapDepth
].FromPages
= FALSE
;
864 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
865 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
866 mMapStack
[mMapDepth
].End
= Entry
->End
;
869 // Inherit Attribute from the Memory Descriptor that is being clipped
871 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
873 Entry
->End
= Start
- 1;
874 ASSERT (Entry
->Start
< Entry
->End
);
876 Entry
= &mMapStack
[mMapDepth
];
877 InsertTailList (&gMemoryMap
, &Entry
->Link
);
880 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
884 // The new range inherits the same Attribute as the Entry
885 //it is being cut out of
887 Attribute
= Entry
->Attribute
;
890 // If the descriptor is empty, then remove it from the map
892 if (Entry
->Start
== Entry
->End
+ 1) {
893 RemoveMemoryMapEntry (Entry
);
898 // Add our new range in
900 CoreAddRange (NewType
, Start
, RangeEnd
, Attribute
);
903 // Move any map descriptor stack to general pool
905 CoreFreeMemoryMapStack ();
908 // Bump the starting address, and convert the next range
910 Start
= RangeEnd
+ 1;
914 // Converted the whole range, done
924 IN UINT64 MaxAddress
,
925 IN UINT64 NumberOfPages
,
926 IN EFI_MEMORY_TYPE NewType
,
933 Internal function. Finds a consecutive free page range below
934 the requested address.
938 MaxAddress - The address that the range must be below
940 NumberOfPages - Number of pages needed
942 NewType - The type of memory the range is going to be turned into
944 Alignment - Bits to align with
948 The base address of the range, or 0 if the range was not found
952 UINT64 NumberOfBytes
;
956 UINT64 DescNumberOfBytes
;
960 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
964 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
967 // If MaxAddress is not aligned to the end of a page
971 // Change MaxAddress to be 1 page lower
973 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
976 // Set MaxAddress to a page boundary
978 MaxAddress
&= ~EFI_PAGE_MASK
;
981 // Set MaxAddress to end of the page
983 MaxAddress
|= EFI_PAGE_MASK
;
986 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
989 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
990 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
993 // If it's not a free entry, don't bother with it
995 if (Entry
->Type
!= EfiConventionalMemory
) {
999 DescStart
= Entry
->Start
;
1000 DescEnd
= Entry
->End
;
1003 // If desc is past max allowed address, skip it
1005 if (DescStart
>= MaxAddress
) {
1010 // If desc ends past max allowed address, clip the end
1012 if (DescEnd
>= MaxAddress
) {
1013 DescEnd
= MaxAddress
;
1016 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
1019 // Compute the number of bytes we can used from this
1020 // descriptor, and see it's enough to satisfy the request
1022 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
1024 if (DescNumberOfBytes
>= NumberOfBytes
) {
1027 // If this is the best match so far remember it
1029 if (DescEnd
> Target
) {
1036 // If this is a grow down, adjust target to be the allocation base
1038 Target
-= NumberOfBytes
- 1;
1041 // If we didn't find a match, return 0
1043 if ((Target
& EFI_PAGE_MASK
) != 0) {
1053 IN UINT64 MaxAddress
,
1055 IN EFI_MEMORY_TYPE NewType
,
1060 Routine Description:
1062 Internal function. Finds a consecutive free page range below
1063 the requested address
1067 MaxAddress - The address that the range must be below
1069 NoPages - Number of pages needed
1071 NewType - The type of memory the range is going to be turned into
1073 Alignment - Bits to align with
1077 The base address of the range, or 0 if the range was not found.
1081 UINT64 NewMaxAddress
;
1084 NewMaxAddress
= MaxAddress
;
1086 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
&& NewMaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1087 NewMaxAddress
= mMemoryTypeStatistics
[NewType
].MaximumAddress
;
1089 if (NewMaxAddress
> mDefaultMaximumAddress
) {
1090 NewMaxAddress
= mDefaultMaximumAddress
;
1094 Start
= CoreFindFreePagesI (NewMaxAddress
, NoPages
, NewType
, Alignment
);
1096 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1099 // Here means there may be no enough memory to use, so try to go through
1100 // all the memory descript to promote the untested memory directly
1102 PromoteMemoryResource ();
1105 // Allocate memory again after the memory resource re-arranged
1107 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1118 IN EFI_ALLOCATE_TYPE Type
,
1119 IN EFI_MEMORY_TYPE MemoryType
,
1120 IN UINTN NumberOfPages
,
1121 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1125 Routine Description:
1127 Allocates pages from the memory map.
1131 Type - The type of allocation to perform
1133 MemoryType - The type of memory to turn the allocated pages into
1135 NumberOfPages - The number of pages to allocate
1137 Memory - A pointer to receive the base allocated memory address
1141 Status. On success, Memory is filled in with the base address allocated
1143 EFI_INVALID_PARAMETER - Parameters violate checking rules defined in spec.
1145 EFI_NOT_FOUND - Could not allocate pages match the requirement.
1147 EFI_OUT_OF_RESOURCES - No enough pages to allocate.
1149 EFI_SUCCESS - Pages successfully allocated.
1158 if (Type
< AllocateAnyPages
|| Type
>= (UINTN
) MaxAllocateType
) {
1159 return EFI_INVALID_PARAMETER
;
1162 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
1163 MemoryType
== EfiConventionalMemory
) {
1164 return EFI_INVALID_PARAMETER
;
1167 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1169 if (MemoryType
== EfiACPIReclaimMemory
||
1170 MemoryType
== EfiACPIMemoryNVS
||
1171 MemoryType
== EfiRuntimeServicesCode
||
1172 MemoryType
== EfiRuntimeServicesData
) {
1174 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1177 if (Type
== AllocateAddress
) {
1178 if ((*Memory
& (Alignment
- 1)) != 0) {
1179 return EFI_NOT_FOUND
;
1183 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1184 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1187 // If this is for below a particular address, then
1192 // The max address is the max natively addressable address for the processor
1194 MaxAddress
= EFI_MAX_ADDRESS
;
1196 if (Type
== AllocateMaxAddress
) {
1200 CoreAcquireMemoryLock ();
1203 // If not a specific address, then find an address to allocate
1205 if (Type
!= AllocateAddress
) {
1206 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1208 Status
= EFI_OUT_OF_RESOURCES
;
1214 // Convert pages from FreeMemory to the requested type
1216 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1219 CoreReleaseMemoryLock ();
1221 if (!EFI_ERROR (Status
)) {
1234 IN EFI_PHYSICAL_ADDRESS Memory
,
1235 IN UINTN NumberOfPages
1239 Routine Description:
1241 Frees previous allocated pages.
1245 Memory - Base address of memory being freed
1247 NumberOfPages - The number of pages to free
1251 EFI_NOT_FOUND - Could not find the entry that covers the range
1253 EFI_INVALID_PARAMETER - Address not aligned
1255 EFI_SUCCESS -Pages successfully freed.
1267 CoreAcquireMemoryLock ();
1270 // Find the entry that the covers the range
1273 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1274 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1275 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1279 if (Link
== &gMemoryMap
) {
1280 CoreReleaseMemoryLock ();
1281 return EFI_NOT_FOUND
;
1284 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1286 if (Entry
->Type
== EfiACPIReclaimMemory
||
1287 Entry
->Type
== EfiACPIMemoryNVS
||
1288 Entry
->Type
== EfiRuntimeServicesCode
||
1289 Entry
->Type
== EfiRuntimeServicesData
) {
1291 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1295 if ((Memory
& (Alignment
- 1)) != 0) {
1296 CoreReleaseMemoryLock ();
1297 return EFI_INVALID_PARAMETER
;
1300 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1301 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1303 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1305 CoreReleaseMemoryLock ();
1307 if (EFI_ERROR (Status
)) {
1312 // Destroy the contents
1314 if (Memory
< EFI_MAX_ADDRESS
) {
1315 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
)Memory
, NumberOfPages
<< EFI_PAGE_SHIFT
);
1326 IN OUT UINTN
*MemoryMapSize
,
1327 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1329 OUT UINTN
*DescriptorSize
,
1330 OUT UINT32
*DescriptorVersion
1334 Routine Description:
1336 This function returns a copy of the current memory map. The map is an array of
1337 memory descriptors, each of which describes a contiguous block of memory.
1341 MemoryMapSize - A pointer to the size, in bytes, of the MemoryMap buffer. On
1342 input, this is the size of the buffer allocated by the caller.
1343 On output, it is the size of the buffer returned by the firmware
1344 if the buffer was large enough, or the size of the buffer needed
1345 to contain the map if the buffer was too small.
1346 MemoryMap - A pointer to the buffer in which firmware places the current memory map.
1347 MapKey - A pointer to the location in which firmware returns the key for the
1349 DescriptorSize - A pointer to the location in which firmware returns the size, in
1350 bytes, of an individual EFI_MEMORY_DESCRIPTOR.
1351 DescriptorVersion - A pointer to the location in which firmware returns the version
1352 number associated with the EFI_MEMORY_DESCRIPTOR.
1356 EFI_SUCCESS - The memory map was returned in the MemoryMap buffer.
1357 EFI_BUFFER_TOO_SMALL - The MemoryMap buffer was too small. The current buffer size
1358 needed to hold the memory map is returned in MemoryMapSize.
1359 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
1366 UINTN NumberOfRuntimeEntries
;
1369 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1370 EFI_MEMORY_TYPE Type
;
1373 // Make sure the parameters are valid
1375 if (MemoryMapSize
== NULL
) {
1376 return EFI_INVALID_PARAMETER
;
1379 CoreAcquireGcdMemoryLock ();
1382 // Count the number of Reserved and MMIO entries that are marked for runtime use
1384 NumberOfRuntimeEntries
= 0;
1385 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1386 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1387 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1388 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1389 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1390 NumberOfRuntimeEntries
++;
1395 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1398 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1399 // prevent people from having pointer math bugs in their code.
1400 // now you have to use *DescriptorSize to make things work.
1402 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1404 if (DescriptorSize
!= NULL
) {
1405 *DescriptorSize
= Size
;
1408 if (DescriptorVersion
!= NULL
) {
1409 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1412 CoreAcquireMemoryLock ();
1415 // Compute the buffer size needed to fit the entire map
1417 BufferSize
= Size
* NumberOfRuntimeEntries
;
1418 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1422 if (*MemoryMapSize
< BufferSize
) {
1423 Status
= EFI_BUFFER_TOO_SMALL
;
1427 if (MemoryMap
== NULL
) {
1428 Status
= EFI_INVALID_PARAMETER
;
1435 ZeroMem (MemoryMap
, BufferSize
);
1436 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1437 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1438 ASSERT (Entry
->VirtualStart
== 0);
1441 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1443 MemoryMap
->Type
= Entry
->Type
;
1444 MemoryMap
->PhysicalStart
= Entry
->Start
;
1445 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1446 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1448 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1449 // memory type bin and needs to be converted to the same memory type as the rest of the
1450 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1451 // improves the chances for a successful S4 resume in the presence of minor page allocation
1452 // differences across reboots.
1454 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1455 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1456 if (mMemoryTypeStatistics
[Type
].Special
&&
1457 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1458 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1459 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1460 MemoryMap
->Type
= Type
;
1464 MemoryMap
->Attribute
= Entry
->Attribute
;
1465 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1466 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1469 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1472 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1473 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1474 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1475 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1476 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1478 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1479 MemoryMap
->VirtualStart
= 0;
1480 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1481 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1483 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1484 MemoryMap
->Type
= EfiReservedMemoryType
;
1485 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1486 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1487 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1489 MemoryMap
->Type
= EfiMemoryMappedIO
;
1493 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1498 Status
= EFI_SUCCESS
;
1502 CoreReleaseMemoryLock ();
1504 CoreReleaseGcdMemoryLock ();
1507 // Update the map key finally
1509 if (MapKey
!= NULL
) {
1510 *MapKey
= mMemoryMapKey
;
1513 *MemoryMapSize
= BufferSize
;
1519 CoreAllocatePoolPages (
1520 IN EFI_MEMORY_TYPE PoolType
,
1521 IN UINTN NumberOfPages
,
1526 Routine Description:
1528 Internal function. Used by the pool functions to allocate pages
1529 to back pool allocation requests.
1533 PoolType - The type of memory for the new pool pages
1535 NumberOfPages - No of pages to allocate
1537 Alignment - Bits to align.
1541 The allocated memory, or NULL
1548 // Find the pages to convert
1550 Start
= FindFreePages (EFI_MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1553 // Convert it to boot services data
1556 DEBUG ((EFI_D_ERROR
| EFI_D_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages
));
1558 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1561 return (VOID
*)(UINTN
)Start
;
1566 IN EFI_PHYSICAL_ADDRESS Memory
,
1567 IN UINTN NumberOfPages
1571 Routine Description:
1573 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1577 Memory - The base address to free
1579 NumberOfPages - The number of pages to free
1587 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1592 CoreTerminateMemoryMap (
1597 Routine Description:
1599 Make sure the memory map is following all the construction rules,
1600 it is the last time to check memory map error before exit boot services.
1604 MapKey - Memory map key
1608 EFI_INVALID_PARAMETER - Memory map not consistent with construction rules.
1610 EFI_SUCCESS - Valid memory map.
1618 Status
= EFI_SUCCESS
;
1620 CoreAcquireMemoryLock ();
1622 if (MapKey
== mMemoryMapKey
) {
1625 // Make sure the memory map is following all the construction rules
1626 // This is the last chance we will be able to display any messages on
1627 // the console devices.
1630 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1631 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1632 if (Entry
->Attribute
& EFI_MEMORY_RUNTIME
) {
1633 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1634 DEBUG((EFI_D_ERROR
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1635 CoreReleaseMemoryLock ();
1636 return EFI_INVALID_PARAMETER
;
1638 if (Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1639 DEBUG((EFI_D_ERROR
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1640 CoreReleaseMemoryLock ();
1641 return EFI_INVALID_PARAMETER
;
1643 if ((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1644 DEBUG((EFI_D_ERROR
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1645 CoreReleaseMemoryLock ();
1646 return EFI_INVALID_PARAMETER
;
1652 // The map key they gave us matches what we expect. Fall through and
1653 // return success. In an ideal world we would clear out all of
1654 // EfiBootServicesCode and EfiBootServicesData. However this function
1655 // is not the last one called by ExitBootServices(), so we have to
1656 // preserve the memory contents.
1659 Status
= EFI_INVALID_PARAMETER
;
1662 CoreReleaseMemoryLock ();