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;
39 // mMapStack - space to use as temp storage to build new map descriptors
40 // mMapDepth - depth of new descriptor stack
43 #define MAX_MAP_DEPTH 6
45 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
46 UINTN mFreeMapStack
= 0;
48 // This list maintain the free memory map list
50 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
51 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
53 EFI_MEMORY_TYPE_STAISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
54 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiReservedMemoryType
55 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderCode
56 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiLoaderData
57 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesCode
58 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiBootServicesData
59 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesCode
60 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiRuntimeServicesData
61 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiConventionalMemory
62 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiUnusableMemory
63 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIReclaimMemory
64 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, FALSE
}, // EfiACPIMemoryNVS
65 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIO
66 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
}, // EfiMemoryMappedIOPortSpace
67 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, TRUE
, TRUE
}, // EfiPalCode
68 { 0, EFI_MAX_ADDRESS
, 0, 0, EfiMaxMemoryType
, FALSE
, FALSE
} // EfiMaxMemoryType
71 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress
= EFI_MAX_ADDRESS
;
73 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
74 { EfiReservedMemoryType
, 0 },
77 { EfiBootServicesCode
, 0 },
78 { EfiBootServicesData
, 0 },
79 { EfiRuntimeServicesCode
, 0 },
80 { EfiRuntimeServicesData
, 0 },
81 { EfiConventionalMemory
, 0 },
82 { EfiUnusableMemory
, 0 },
83 { EfiACPIReclaimMemory
, 0 },
84 { EfiACPIMemoryNVS
, 0 },
85 { EfiMemoryMappedIO
, 0 },
86 { EfiMemoryMappedIOPortSpace
, 0 },
88 { EfiMaxMemoryType
, 0 }
93 Enter critical section by gaining lock on gMemoryLock.
97 CoreAcquireMemoryLock (
101 CoreAcquireLock (&gMemoryLock
);
107 Exit critical section by releasing lock on gMemoryLock.
111 CoreReleaseMemoryLock (
115 CoreReleaseLock (&gMemoryLock
);
122 Internal function. Removes a descriptor entry.
124 @param Entry The entry to remove
128 RemoveMemoryMapEntry (
129 IN OUT MEMORY_MAP
*Entry
132 RemoveEntryList (&Entry
->Link
);
133 Entry
->Link
.ForwardLink
= NULL
;
135 if (Entry
->FromPages
) {
137 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
139 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
144 Internal function. Adds a ranges to the memory map.
145 The range must not already exist in the map.
147 @param Type The type of memory range to add
148 @param Start The starting address in the memory range Must be
150 @param End The last address in the range Must be the last
152 @param Attribute The attributes of the memory range to add
157 IN EFI_MEMORY_TYPE Type
,
158 IN EFI_PHYSICAL_ADDRESS Start
,
159 IN EFI_PHYSICAL_ADDRESS End
,
166 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
167 ASSERT (End
> Start
) ;
169 ASSERT_LOCKED (&gMemoryLock
);
171 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
174 // Memory map being altered so updated key
179 // UEFI 2.0 added an event group for notificaiton on memory map changes.
180 // So we need to signal this Event Group every time the memory map changes.
181 // If we are in EFI 1.10 compatability mode no event groups will be
182 // found and nothing will happen we we call this function. These events
183 // will get signaled but since a lock is held around the call to this
184 // function the notificaiton events will only be called after this funciton
185 // returns and the lock is released.
187 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
190 // Look for adjoining memory descriptor
193 // Two memory descriptors can only be merged if they have the same Type
194 // and the same Attribute
197 Link
= gMemoryMap
.ForwardLink
;
198 while (Link
!= &gMemoryMap
) {
199 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
200 Link
= Link
->ForwardLink
;
202 if (Entry
->Type
!= Type
) {
206 if (Entry
->Attribute
!= Attribute
) {
210 if (Entry
->End
+ 1 == Start
) {
212 Start
= Entry
->Start
;
213 RemoveMemoryMapEntry (Entry
);
215 } else if (Entry
->Start
== End
+ 1) {
218 RemoveMemoryMapEntry (Entry
);
226 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
227 mMapStack
[mMapDepth
].FromPages
= FALSE
;
228 mMapStack
[mMapDepth
].Type
= Type
;
229 mMapStack
[mMapDepth
].Start
= Start
;
230 mMapStack
[mMapDepth
].End
= End
;
231 mMapStack
[mMapDepth
].VirtualStart
= 0;
232 mMapStack
[mMapDepth
].Attribute
= Attribute
;
233 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
236 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
242 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
243 If the list is emtry, then allocate a new page to refuel the list.
244 Please Note this algorithm to allocate the memory map descriptor has a property
245 that the memory allocated for memory entries always grows, and will never really be freed
246 For example, if the current boot uses 2000 memory map entries at the maximum point, but
247 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
248 memory map entries is still allocated from EfiBootServicesMemory.
251 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
255 AllocateMemoryMapEntry (
259 MEMORY_MAP
* FreeDescriptorEntries
;
263 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
265 // The list is empty, to allocate one page to refuel the list
267 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
268 if(FreeDescriptorEntries
!= NULL
) {
270 // Enque the free memmory map entries into the list
272 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
273 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
274 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
281 // dequeue the first descriptor from the list
283 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
284 RemoveEntryList (&Entry
->Link
);
291 Internal function. Moves any memory descriptors that are on the
292 temporary descriptor stack to heap.
296 CoreFreeMemoryMapStack (
304 ASSERT_LOCKED (&gMemoryLock
);
307 // If already freeing the map stack, then return
309 if (mFreeMapStack
!= 0) {
314 // Move the temporary memory descriptor stack into pool
318 while (mMapDepth
!= 0) {
320 // Deque an memory map entry from mFreeMemoryMapEntryList
322 Entry
= AllocateMemoryMapEntry ();
327 // Update to proper entry
331 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
334 // Move this entry to general memory
336 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
337 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
339 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
340 Entry
->FromPages
= TRUE
;
343 // Find insertion location
345 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
346 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
347 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
352 InsertTailList (Link2
, &Entry
->Link
);
356 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
357 // so here no need to move it to memory.
359 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
367 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
371 PromoteMemoryResource (
376 EFI_GCD_MAP_ENTRY
*Entry
;
378 DEBUG ((DEBUG_PAGE
, "Promote the memory resource\n"));
380 CoreAcquireGcdMemoryLock ();
382 Link
= mGcdMemorySpaceMap
.ForwardLink
;
383 while (Link
!= &mGcdMemorySpaceMap
) {
385 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
387 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
388 Entry
->EndAddress
< EFI_MAX_ADDRESS
&&
389 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
390 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
392 // Update the GCD map
394 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
395 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
396 Entry
->ImageHandle
= gDxeCoreImageHandle
;
397 Entry
->DeviceHandle
= NULL
;
400 // Add to allocable system memory resource
404 EfiConventionalMemory
,
407 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
409 CoreFreeMemoryMapStack ();
413 Link
= Link
->ForwardLink
;
416 CoreReleaseGcdMemoryLock ();
423 Called to initialize the memory map and add descriptors to
424 the current descriptor list.
425 The first descriptor that is added must be general usable
426 memory as the addition allocates heap.
428 @param Type The type of memory to add
429 @param Start The starting address in the memory range Must be
431 @param NumberOfPages The number of pages in the range
432 @param Attribute Attributes of the memory to add
434 @return None. The range is added to the memory map
438 CoreAddMemoryDescriptor (
439 IN EFI_MEMORY_TYPE Type
,
440 IN EFI_PHYSICAL_ADDRESS Start
,
441 IN UINT64 NumberOfPages
,
445 EFI_PHYSICAL_ADDRESS End
;
450 if ((Start
& EFI_PAGE_MASK
) != 0) {
454 if (Type
>= EfiMaxMemoryType
&& Type
<= 0x7fffffff) {
458 CoreAcquireMemoryLock ();
459 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
460 CoreAddRange (Type
, Start
, End
, Attribute
);
461 CoreFreeMemoryMapStack ();
462 CoreReleaseMemoryLock ();
465 // Check to see if the statistics for the different memory types have already been established
467 if (mMemoryTypeInformationInitialized
) {
472 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
474 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
476 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
478 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
479 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
483 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
485 // Allocate pages for the current memory type from the top of available memory
487 Status
= CoreAllocatePages (
490 gMemoryTypeInformation
[Index
].NumberOfPages
,
491 &mMemoryTypeStatistics
[Type
].BaseAddress
493 if (EFI_ERROR (Status
)) {
495 // If an error occurs allocating the pages for the current memory type, then
496 // free all the pages allocates for the previous memory types and return. This
497 // operation with be retied when/if more memory is added to the system
499 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
501 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
503 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
504 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
508 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
510 mMemoryTypeStatistics
[Type
].BaseAddress
,
511 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
513 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
514 mMemoryTypeStatistics
[Type
].MaximumAddress
= EFI_MAX_ADDRESS
;
521 // Compute the address at the top of the current statistics
523 mMemoryTypeStatistics
[Type
].MaximumAddress
=
524 mMemoryTypeStatistics
[Type
].BaseAddress
+
525 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
528 // If the current base address is the lowest address so far, then update the default
531 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
532 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
538 // There was enough system memory for all the the memory types were allocated. So,
539 // those memory areas can be freed for future allocations, and all future memory
540 // allocations can occur within their respective bins
542 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
544 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
546 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
547 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
551 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
553 mMemoryTypeStatistics
[Type
].BaseAddress
,
554 gMemoryTypeInformation
[Index
].NumberOfPages
556 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
557 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
562 // If the number of pages reserved for a memory type is 0, then all allocations for that type
563 // should be in the default range.
565 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
566 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
567 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
568 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
571 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
572 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== EFI_MAX_ADDRESS
) {
573 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
577 mMemoryTypeInformationInitialized
= TRUE
;
582 Internal function. Converts a memory range to the specified type.
583 The range must exist in the memory map.
585 @param Start The first address of the range Must be page
587 @param NumberOfPages The number of pages to convert
588 @param NewType The new type for the memory range
590 @retval EFI_INVALID_PARAMETER Invalid parameter
591 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
592 range or convertion not allowed.
593 @retval EFI_SUCCESS Successfully converts the memory range to the
600 IN UINT64 NumberOfPages
,
601 IN EFI_MEMORY_TYPE NewType
605 UINT64 NumberOfBytes
;
613 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
614 End
= Start
+ NumberOfBytes
- 1;
616 ASSERT (NumberOfPages
);
617 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
618 ASSERT (End
> Start
) ;
619 ASSERT_LOCKED (&gMemoryLock
);
621 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
> (Start
+ NumberOfBytes
))) {
622 return EFI_INVALID_PARAMETER
;
626 // Convert the entire range
629 while (Start
< End
) {
632 // Find the entry that the covers the range
634 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
635 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
637 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
642 if (Link
== &gMemoryMap
) {
643 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
644 return EFI_NOT_FOUND
;
648 // Convert range to the end, or to the end of the descriptor
649 // if that's all we've got
652 if (Entry
->End
< End
) {
653 RangeEnd
= Entry
->End
;
656 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to %d\n", Start
, RangeEnd
, NewType
));
659 // Debug code - verify conversion is allowed
661 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
662 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types\n"));
663 return EFI_NOT_FOUND
;
667 // Update counters for the number of pages allocated to each memory type
669 if (Entry
->Type
>= 0 && Entry
->Type
< EfiMaxMemoryType
) {
670 if (Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&&
671 Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) {
672 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
673 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
675 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
680 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
) {
681 if (Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
682 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
683 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
>
684 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
685 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
691 // Pull range out of descriptor
693 if (Entry
->Start
== Start
) {
698 Entry
->Start
= RangeEnd
+ 1;
700 } else if (Entry
->End
== RangeEnd
) {
705 Entry
->End
= Start
- 1;
710 // Pull it out of the center, clip current
716 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
717 mMapStack
[mMapDepth
].FromPages
= FALSE
;
718 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
719 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
720 mMapStack
[mMapDepth
].End
= Entry
->End
;
723 // Inherit Attribute from the Memory Descriptor that is being clipped
725 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
727 Entry
->End
= Start
- 1;
728 ASSERT (Entry
->Start
< Entry
->End
);
730 Entry
= &mMapStack
[mMapDepth
];
731 InsertTailList (&gMemoryMap
, &Entry
->Link
);
734 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
738 // The new range inherits the same Attribute as the Entry
739 //it is being cut out of
741 Attribute
= Entry
->Attribute
;
744 // If the descriptor is empty, then remove it from the map
746 if (Entry
->Start
== Entry
->End
+ 1) {
747 RemoveMemoryMapEntry (Entry
);
752 // Add our new range in
754 CoreAddRange (NewType
, Start
, RangeEnd
, Attribute
);
757 // Move any map descriptor stack to general pool
759 CoreFreeMemoryMapStack ();
762 // Bump the starting address, and convert the next range
764 Start
= RangeEnd
+ 1;
768 // Converted the whole range, done
777 Internal function. Finds a consecutive free page range below
778 the requested address.
780 @param MaxAddress The address that the range must be below
781 @param NumberOfPages Number of pages needed
782 @param NewType The type of memory the range is going to be
784 @param Alignment Bits to align with
786 @return The base address of the range, or 0 if the range was not found
791 IN UINT64 MaxAddress
,
792 IN UINT64 NumberOfPages
,
793 IN EFI_MEMORY_TYPE NewType
,
797 UINT64 NumberOfBytes
;
801 UINT64 DescNumberOfBytes
;
805 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
809 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
812 // If MaxAddress is not aligned to the end of a page
816 // Change MaxAddress to be 1 page lower
818 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
821 // Set MaxAddress to a page boundary
823 MaxAddress
&= ~EFI_PAGE_MASK
;
826 // Set MaxAddress to end of the page
828 MaxAddress
|= EFI_PAGE_MASK
;
831 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
834 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
835 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
838 // If it's not a free entry, don't bother with it
840 if (Entry
->Type
!= EfiConventionalMemory
) {
844 DescStart
= Entry
->Start
;
845 DescEnd
= Entry
->End
;
848 // If desc is past max allowed address, skip it
850 if (DescStart
>= MaxAddress
) {
855 // If desc ends past max allowed address, clip the end
857 if (DescEnd
>= MaxAddress
) {
858 DescEnd
= MaxAddress
;
861 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
864 // Compute the number of bytes we can used from this
865 // descriptor, and see it's enough to satisfy the request
867 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
869 if (DescNumberOfBytes
>= NumberOfBytes
) {
872 // If this is the best match so far remember it
874 if (DescEnd
> Target
) {
881 // If this is a grow down, adjust target to be the allocation base
883 Target
-= NumberOfBytes
- 1;
886 // If we didn't find a match, return 0
888 if ((Target
& EFI_PAGE_MASK
) != 0) {
897 Internal function. Finds a consecutive free page range below
898 the requested address
900 @param MaxAddress The address that the range must be below
901 @param NoPages Number of pages needed
902 @param NewType The type of memory the range is going to be
904 @param Alignment Bits to align with
906 @return The base address of the range, or 0 if the range was not found.
911 IN UINT64 MaxAddress
,
913 IN EFI_MEMORY_TYPE NewType
,
917 UINT64 NewMaxAddress
;
920 NewMaxAddress
= MaxAddress
;
922 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
&& NewMaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
923 NewMaxAddress
= mMemoryTypeStatistics
[NewType
].MaximumAddress
;
925 if (NewMaxAddress
> mDefaultMaximumAddress
) {
926 NewMaxAddress
= mDefaultMaximumAddress
;
930 Start
= CoreFindFreePagesI (NewMaxAddress
, NoPages
, NewType
, Alignment
);
932 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
935 // Here means there may be no enough memory to use, so try to go through
936 // all the memory descript to promote the untested memory directly
938 PromoteMemoryResource ();
941 // Allocate memory again after the memory resource re-arranged
943 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
953 Allocates pages from the memory map.
955 @param Type The type of allocation to perform
956 @param MemoryType The type of memory to turn the allocated pages
958 @param NumberOfPages The number of pages to allocate
959 @param Memory A pointer to receive the base allocated memory
962 @return Status. On success, Memory is filled in with the base address allocated
963 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
965 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
966 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
967 @retval EFI_SUCCESS Pages successfully allocated.
973 IN EFI_ALLOCATE_TYPE Type
,
974 IN EFI_MEMORY_TYPE MemoryType
,
975 IN UINTN NumberOfPages
,
976 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
984 if (Type
< AllocateAnyPages
|| Type
>= (UINTN
) MaxAllocateType
) {
985 return EFI_INVALID_PARAMETER
;
988 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
989 MemoryType
== EfiConventionalMemory
) {
990 return EFI_INVALID_PARAMETER
;
993 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
995 if (MemoryType
== EfiACPIReclaimMemory
||
996 MemoryType
== EfiACPIMemoryNVS
||
997 MemoryType
== EfiRuntimeServicesCode
||
998 MemoryType
== EfiRuntimeServicesData
) {
1000 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1003 if (Type
== AllocateAddress
) {
1004 if ((*Memory
& (Alignment
- 1)) != 0) {
1005 return EFI_NOT_FOUND
;
1009 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1010 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1013 // If this is for below a particular address, then
1018 // The max address is the max natively addressable address for the processor
1020 MaxAddress
= EFI_MAX_ADDRESS
;
1022 if (Type
== AllocateMaxAddress
) {
1026 CoreAcquireMemoryLock ();
1029 // If not a specific address, then find an address to allocate
1031 if (Type
!= AllocateAddress
) {
1032 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1034 Status
= EFI_OUT_OF_RESOURCES
;
1040 // Convert pages from FreeMemory to the requested type
1042 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1045 CoreReleaseMemoryLock ();
1047 if (!EFI_ERROR (Status
)) {
1056 Frees previous allocated pages.
1058 @param Memory Base address of memory being freed
1059 @param NumberOfPages The number of pages to free
1061 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1062 @retval EFI_INVALID_PARAMETER Address not aligned
1063 @return EFI_SUCCESS -Pages successfully freed.
1069 IN EFI_PHYSICAL_ADDRESS Memory
,
1070 IN UINTN NumberOfPages
1081 CoreAcquireMemoryLock ();
1084 // Find the entry that the covers the range
1087 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1088 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1089 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1093 if (Link
== &gMemoryMap
) {
1094 CoreReleaseMemoryLock ();
1095 return EFI_NOT_FOUND
;
1098 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1100 if (Entry
->Type
== EfiACPIReclaimMemory
||
1101 Entry
->Type
== EfiACPIMemoryNVS
||
1102 Entry
->Type
== EfiRuntimeServicesCode
||
1103 Entry
->Type
== EfiRuntimeServicesData
) {
1105 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1109 if ((Memory
& (Alignment
- 1)) != 0) {
1110 CoreReleaseMemoryLock ();
1111 return EFI_INVALID_PARAMETER
;
1114 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1115 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1117 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1119 CoreReleaseMemoryLock ();
1121 if (EFI_ERROR (Status
)) {
1126 // Destroy the contents
1128 if (Memory
< EFI_MAX_ADDRESS
) {
1129 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
)Memory
, NumberOfPages
<< EFI_PAGE_SHIFT
);
1137 This function returns a copy of the current memory map. The map is an array of
1138 memory descriptors, each of which describes a contiguous block of memory.
1140 @param MemoryMapSize A pointer to the size, in bytes, of the
1141 MemoryMap buffer. On input, this is the size of
1142 the buffer allocated by the caller. On output,
1143 it is the size of the buffer returned by the
1144 firmware if the buffer was large enough, or the
1145 size of the buffer needed to contain the map if
1146 the buffer was too small.
1147 @param MemoryMap A pointer to the buffer in which firmware places
1148 the current memory map.
1149 @param MapKey A pointer to the location in which firmware
1150 returns the key for the current memory map.
1151 @param DescriptorSize A pointer to the location in which firmware
1152 returns the size, in bytes, of an individual
1153 EFI_MEMORY_DESCRIPTOR.
1154 @param DescriptorVersion A pointer to the location in which firmware
1155 returns the version number associated with the
1156 EFI_MEMORY_DESCRIPTOR.
1158 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1160 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1161 buffer size needed to hold the memory map is
1162 returned in MemoryMapSize.
1163 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1169 IN OUT UINTN
*MemoryMapSize
,
1170 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1172 OUT UINTN
*DescriptorSize
,
1173 OUT UINT32
*DescriptorVersion
1179 UINTN NumberOfRuntimeEntries
;
1182 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1183 EFI_MEMORY_TYPE Type
;
1186 // Make sure the parameters are valid
1188 if (MemoryMapSize
== NULL
) {
1189 return EFI_INVALID_PARAMETER
;
1192 CoreAcquireGcdMemoryLock ();
1195 // Count the number of Reserved and MMIO entries that are marked for runtime use
1197 NumberOfRuntimeEntries
= 0;
1198 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1199 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1200 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1201 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1202 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1203 NumberOfRuntimeEntries
++;
1208 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1211 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1212 // prevent people from having pointer math bugs in their code.
1213 // now you have to use *DescriptorSize to make things work.
1215 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1217 if (DescriptorSize
!= NULL
) {
1218 *DescriptorSize
= Size
;
1221 if (DescriptorVersion
!= NULL
) {
1222 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1225 CoreAcquireMemoryLock ();
1228 // Compute the buffer size needed to fit the entire map
1230 BufferSize
= Size
* NumberOfRuntimeEntries
;
1231 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1235 if (*MemoryMapSize
< BufferSize
) {
1236 Status
= EFI_BUFFER_TOO_SMALL
;
1240 if (MemoryMap
== NULL
) {
1241 Status
= EFI_INVALID_PARAMETER
;
1248 ZeroMem (MemoryMap
, BufferSize
);
1249 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1250 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1251 ASSERT (Entry
->VirtualStart
== 0);
1254 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1256 MemoryMap
->Type
= Entry
->Type
;
1257 MemoryMap
->PhysicalStart
= Entry
->Start
;
1258 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1259 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1261 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1262 // memory type bin and needs to be converted to the same memory type as the rest of the
1263 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1264 // improves the chances for a successful S4 resume in the presence of minor page allocation
1265 // differences across reboots.
1267 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1268 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1269 if (mMemoryTypeStatistics
[Type
].Special
&&
1270 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1271 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1272 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1273 MemoryMap
->Type
= Type
;
1277 MemoryMap
->Attribute
= Entry
->Attribute
;
1278 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1279 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1282 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1285 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1286 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1287 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1288 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1289 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1291 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1292 MemoryMap
->VirtualStart
= 0;
1293 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1294 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1296 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1297 MemoryMap
->Type
= EfiReservedMemoryType
;
1298 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1299 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1300 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1302 MemoryMap
->Type
= EfiMemoryMappedIO
;
1306 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1311 Status
= EFI_SUCCESS
;
1315 CoreReleaseMemoryLock ();
1317 CoreReleaseGcdMemoryLock ();
1320 // Update the map key finally
1322 if (MapKey
!= NULL
) {
1323 *MapKey
= mMemoryMapKey
;
1326 *MemoryMapSize
= BufferSize
;
1333 Internal function. Used by the pool functions to allocate pages
1334 to back pool allocation requests.
1336 @param PoolType The type of memory for the new pool pages
1337 @param NumberOfPages No of pages to allocate
1338 @param Alignment Bits to align.
1340 @return The allocated memory, or NULL
1344 CoreAllocatePoolPages (
1345 IN EFI_MEMORY_TYPE PoolType
,
1346 IN UINTN NumberOfPages
,
1353 // Find the pages to convert
1355 Start
= FindFreePages (EFI_MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1358 // Convert it to boot services data
1361 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages
));
1363 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1366 return (VOID
*)(UINTN
) Start
;
1371 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1373 @param Memory The base address to free
1374 @param NumberOfPages The number of pages to free
1379 IN EFI_PHYSICAL_ADDRESS Memory
,
1380 IN UINTN NumberOfPages
1383 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1389 Make sure the memory map is following all the construction rules,
1390 it is the last time to check memory map error before exit boot services.
1392 @param MapKey Memory map key
1394 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1396 @retval EFI_SUCCESS Valid memory map.
1400 CoreTerminateMemoryMap (
1408 Status
= EFI_SUCCESS
;
1410 CoreAcquireMemoryLock ();
1412 if (MapKey
== mMemoryMapKey
) {
1415 // Make sure the memory map is following all the construction rules
1416 // This is the last chance we will be able to display any messages on
1417 // the console devices.
1420 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1421 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1422 if (Entry
->Attribute
& EFI_MEMORY_RUNTIME
) {
1423 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1424 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1425 Status
= EFI_INVALID_PARAMETER
;
1428 if (Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1429 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1430 Status
= EFI_INVALID_PARAMETER
;
1433 if ((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1434 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1435 Status
= EFI_INVALID_PARAMETER
;
1442 // The map key they gave us matches what we expect. Fall through and
1443 // return success. In an ideal world we would clear out all of
1444 // EfiBootServicesCode and EfiBootServicesData. However this function
1445 // is not the last one called by ExitBootServices(), so we have to
1446 // preserve the memory contents.
1449 Status
= EFI_INVALID_PARAMETER
;
1453 CoreReleaseMemoryLock ();