2 UEFI Memory page management functions.
4 Copyright (c) 2007 - 2013, Intel Corporation. All rights reserved.<BR>
5 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_STATISTICS
;
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_STATISTICS 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
;
74 EFI_PHYSICAL_ADDRESS mDefaultBaseAddress
= MAX_ADDRESS
;
76 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
77 { EfiReservedMemoryType
, 0 },
80 { EfiBootServicesCode
, 0 },
81 { EfiBootServicesData
, 0 },
82 { EfiRuntimeServicesCode
, 0 },
83 { EfiRuntimeServicesData
, 0 },
84 { EfiConventionalMemory
, 0 },
85 { EfiUnusableMemory
, 0 },
86 { EfiACPIReclaimMemory
, 0 },
87 { EfiACPIMemoryNVS
, 0 },
88 { EfiMemoryMappedIO
, 0 },
89 { EfiMemoryMappedIOPortSpace
, 0 },
91 { EfiMaxMemoryType
, 0 }
94 // Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated
95 // and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a
96 // address assigned by DXE core.
98 GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady
= FALSE
;
101 Enter critical section by gaining lock on gMemoryLock.
105 CoreAcquireMemoryLock (
109 CoreAcquireLock (&gMemoryLock
);
115 Exit critical section by releasing lock on gMemoryLock.
119 CoreReleaseMemoryLock (
123 CoreReleaseLock (&gMemoryLock
);
130 Internal function. Removes a descriptor entry.
132 @param Entry The entry to remove
136 RemoveMemoryMapEntry (
137 IN OUT MEMORY_MAP
*Entry
140 RemoveEntryList (&Entry
->Link
);
141 Entry
->Link
.ForwardLink
= NULL
;
143 if (Entry
->FromPages
) {
145 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
147 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
152 Internal function. Adds a ranges to the memory map.
153 The range must not already exist in the map.
155 @param Type The type of memory range to add
156 @param Start The starting address in the memory range Must be
158 @param End The last address in the range Must be the last
160 @param Attribute The attributes of the memory range to add
165 IN EFI_MEMORY_TYPE Type
,
166 IN EFI_PHYSICAL_ADDRESS Start
,
167 IN EFI_PHYSICAL_ADDRESS End
,
174 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
175 ASSERT (End
> Start
) ;
177 ASSERT_LOCKED (&gMemoryLock
);
179 DEBUG ((DEBUG_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
182 // Memory map being altered so updated key
187 // UEFI 2.0 added an event group for notificaiton on memory map changes.
188 // So we need to signal this Event Group every time the memory map changes.
189 // If we are in EFI 1.10 compatability mode no event groups will be
190 // found and nothing will happen we we call this function. These events
191 // will get signaled but since a lock is held around the call to this
192 // function the notificaiton events will only be called after this funciton
193 // returns and the lock is released.
195 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
198 // Look for adjoining memory descriptor
201 // Two memory descriptors can only be merged if they have the same Type
202 // and the same Attribute
205 Link
= gMemoryMap
.ForwardLink
;
206 while (Link
!= &gMemoryMap
) {
207 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
208 Link
= Link
->ForwardLink
;
210 if (Entry
->Type
!= Type
) {
214 if (Entry
->Attribute
!= Attribute
) {
218 if (Entry
->End
+ 1 == Start
) {
220 Start
= Entry
->Start
;
221 RemoveMemoryMapEntry (Entry
);
223 } else if (Entry
->Start
== End
+ 1) {
226 RemoveMemoryMapEntry (Entry
);
234 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
235 mMapStack
[mMapDepth
].FromPages
= FALSE
;
236 mMapStack
[mMapDepth
].Type
= Type
;
237 mMapStack
[mMapDepth
].Start
= Start
;
238 mMapStack
[mMapDepth
].End
= End
;
239 mMapStack
[mMapDepth
].VirtualStart
= 0;
240 mMapStack
[mMapDepth
].Attribute
= Attribute
;
241 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
244 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
250 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
251 If the list is emtry, then allocate a new page to refuel the list.
252 Please Note this algorithm to allocate the memory map descriptor has a property
253 that the memory allocated for memory entries always grows, and will never really be freed
254 For example, if the current boot uses 2000 memory map entries at the maximum point, but
255 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
256 memory map entries is still allocated from EfiBootServicesMemory.
259 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
263 AllocateMemoryMapEntry (
267 MEMORY_MAP
* FreeDescriptorEntries
;
271 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
273 // The list is empty, to allocate one page to refuel the list
275 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
276 if(FreeDescriptorEntries
!= NULL
) {
278 // Enque the free memmory map entries into the list
280 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
281 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
282 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
289 // dequeue the first descriptor from the list
291 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
292 RemoveEntryList (&Entry
->Link
);
299 Internal function. Moves any memory descriptors that are on the
300 temporary descriptor stack to heap.
304 CoreFreeMemoryMapStack (
312 ASSERT_LOCKED (&gMemoryLock
);
315 // If already freeing the map stack, then return
317 if (mFreeMapStack
!= 0) {
322 // Move the temporary memory descriptor stack into pool
326 while (mMapDepth
!= 0) {
328 // Deque an memory map entry from mFreeMemoryMapEntryList
330 Entry
= AllocateMemoryMapEntry ();
335 // Update to proper entry
339 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
342 // Move this entry to general memory
344 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
345 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
347 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
348 Entry
->FromPages
= TRUE
;
351 // Find insertion location
353 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
354 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
355 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
360 InsertTailList (Link2
, &Entry
->Link
);
364 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
365 // so here no need to move it to memory.
367 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
375 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
379 PromoteMemoryResource (
384 EFI_GCD_MAP_ENTRY
*Entry
;
387 DEBUG ((DEBUG_PAGE
, "Promote the memory resource\n"));
389 CoreAcquireGcdMemoryLock ();
392 Link
= mGcdMemorySpaceMap
.ForwardLink
;
393 while (Link
!= &mGcdMemorySpaceMap
) {
395 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
397 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
398 Entry
->EndAddress
< MAX_ADDRESS
&&
399 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
400 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
402 // Update the GCD map
404 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
405 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
406 Entry
->ImageHandle
= gDxeCoreImageHandle
;
407 Entry
->DeviceHandle
= NULL
;
410 // Add to allocable system memory resource
414 EfiConventionalMemory
,
417 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
419 CoreFreeMemoryMapStack ();
424 Link
= Link
->ForwardLink
;
427 CoreReleaseGcdMemoryLock ();
432 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD
433 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the
434 size of boot time and runtime code.
438 CoreLoadingFixedAddressHook (
442 UINT32 RuntimeCodePageNumber
;
443 UINT32 BootTimeCodePageNumber
;
444 EFI_PHYSICAL_ADDRESS RuntimeCodeBase
;
445 EFI_PHYSICAL_ADDRESS BootTimeCodeBase
;
449 // Make sure these 2 areas are not initialzied.
451 if (!gLoadFixedAddressCodeMemoryReady
) {
452 RuntimeCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
453 BootTimeCodePageNumber
= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
454 RuntimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
- EFI_PAGES_TO_SIZE (RuntimeCodePageNumber
));
455 BootTimeCodeBase
= (EFI_PHYSICAL_ADDRESS
)(RuntimeCodeBase
- EFI_PAGES_TO_SIZE (BootTimeCodePageNumber
));
457 // Try to allocate runtime memory.
459 Status
= CoreAllocatePages (
461 EfiRuntimeServicesCode
,
462 RuntimeCodePageNumber
,
465 if (EFI_ERROR(Status
)) {
467 // Runtime memory allocation failed
472 // Try to allocate boot memory.
474 Status
= CoreAllocatePages (
477 BootTimeCodePageNumber
,
480 if (EFI_ERROR(Status
)) {
482 // boot memory allocation failed. Free Runtime code range and will try the allocation again when
483 // new memory range is installed.
487 RuntimeCodePageNumber
491 gLoadFixedAddressCodeMemoryReady
= TRUE
;
497 Called to initialize the memory map and add descriptors to
498 the current descriptor list.
499 The first descriptor that is added must be general usable
500 memory as the addition allocates heap.
502 @param Type The type of memory to add
503 @param Start The starting address in the memory range Must be
505 @param NumberOfPages The number of pages in the range
506 @param Attribute Attributes of the memory to add
508 @return None. The range is added to the memory map
512 CoreAddMemoryDescriptor (
513 IN EFI_MEMORY_TYPE Type
,
514 IN EFI_PHYSICAL_ADDRESS Start
,
515 IN UINT64 NumberOfPages
,
519 EFI_PHYSICAL_ADDRESS End
;
524 if ((Start
& EFI_PAGE_MASK
) != 0) {
528 if (Type
>= EfiMaxMemoryType
&& Type
<= 0x7fffffff) {
531 CoreAcquireMemoryLock ();
532 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
533 CoreAddRange (Type
, Start
, End
, Attribute
);
534 CoreFreeMemoryMapStack ();
535 CoreReleaseMemoryLock ();
538 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type
540 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
541 CoreLoadingFixedAddressHook();
545 // Check to see if the statistics for the different memory types have already been established
547 if (mMemoryTypeInformationInitialized
) {
553 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
555 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
557 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
559 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
560 if ((UINT32
)Type
> EfiMaxMemoryType
) {
563 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
565 // Allocate pages for the current memory type from the top of available memory
567 Status
= CoreAllocatePages (
570 gMemoryTypeInformation
[Index
].NumberOfPages
,
571 &mMemoryTypeStatistics
[Type
].BaseAddress
573 if (EFI_ERROR (Status
)) {
575 // If an error occurs allocating the pages for the current memory type, then
576 // free all the pages allocates for the previous memory types and return. This
577 // operation with be retied when/if more memory is added to the system
579 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
581 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
583 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
584 if ((UINT32
)Type
> EfiMaxMemoryType
) {
588 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
590 mMemoryTypeStatistics
[Type
].BaseAddress
,
591 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
593 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
594 mMemoryTypeStatistics
[Type
].MaximumAddress
= MAX_ADDRESS
;
601 // Compute the address at the top of the current statistics
603 mMemoryTypeStatistics
[Type
].MaximumAddress
=
604 mMemoryTypeStatistics
[Type
].BaseAddress
+
605 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
608 // If the current base address is the lowest address so far, then update the default
611 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
612 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
618 // There was enough system memory for all the the memory types were allocated. So,
619 // those memory areas can be freed for future allocations, and all future memory
620 // allocations can occur within their respective bins
622 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
624 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
626 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
627 if ((UINT32
)Type
> EfiMaxMemoryType
) {
630 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
632 mMemoryTypeStatistics
[Type
].BaseAddress
,
633 gMemoryTypeInformation
[Index
].NumberOfPages
635 mMemoryTypeStatistics
[Type
].NumberOfPages
= gMemoryTypeInformation
[Index
].NumberOfPages
;
636 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
641 // If the number of pages reserved for a memory type is 0, then all allocations for that type
642 // should be in the default range.
644 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
645 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
646 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
647 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
650 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
651 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== MAX_ADDRESS
) {
652 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
656 mMemoryTypeInformationInitialized
= TRUE
;
661 Internal function. Converts a memory range to the specified type.
662 The range must exist in the memory map.
664 @param Start The first address of the range Must be page
666 @param NumberOfPages The number of pages to convert
667 @param NewType The new type for the memory range
669 @retval EFI_INVALID_PARAMETER Invalid parameter
670 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
671 range or convertion not allowed.
672 @retval EFI_SUCCESS Successfully converts the memory range to the
679 IN UINT64 NumberOfPages
,
680 IN EFI_MEMORY_TYPE NewType
684 UINT64 NumberOfBytes
;
692 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
693 End
= Start
+ NumberOfBytes
- 1;
695 ASSERT (NumberOfPages
);
696 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
697 ASSERT (End
> Start
) ;
698 ASSERT_LOCKED (&gMemoryLock
);
700 if (NumberOfPages
== 0 || ((Start
& EFI_PAGE_MASK
) != 0) || (Start
> (Start
+ NumberOfBytes
))) {
701 return EFI_INVALID_PARAMETER
;
705 // Convert the entire range
708 while (Start
< End
) {
711 // Find the entry that the covers the range
713 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
714 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
716 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
721 if (Link
== &gMemoryMap
) {
722 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
723 return EFI_NOT_FOUND
;
727 // Convert range to the end, or to the end of the descriptor
728 // if that's all we've got
732 ASSERT (Entry
!= NULL
);
733 if (Entry
->End
< End
) {
734 RangeEnd
= Entry
->End
;
737 DEBUG ((DEBUG_PAGE
, "ConvertRange: %lx-%lx to %d\n", Start
, RangeEnd
, NewType
));
740 // Debug code - verify conversion is allowed
742 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
743 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "ConvertPages: Incompatible memory types\n"));
744 return EFI_NOT_FOUND
;
748 // Update counters for the number of pages allocated to each memory type
750 if ((UINT32
)Entry
->Type
< EfiMaxMemoryType
) {
751 if ((Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) ||
752 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
753 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
754 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
756 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
761 if ((UINT32
)NewType
< EfiMaxMemoryType
) {
762 if ((Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) ||
763 (Start
>= mDefaultBaseAddress
&& Start
<= mDefaultMaximumAddress
) ) {
764 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
765 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
> gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
766 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
772 // Pull range out of descriptor
774 if (Entry
->Start
== Start
) {
779 Entry
->Start
= RangeEnd
+ 1;
781 } else if (Entry
->End
== RangeEnd
) {
786 Entry
->End
= Start
- 1;
791 // Pull it out of the center, clip current
797 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
798 mMapStack
[mMapDepth
].FromPages
= FALSE
;
799 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
800 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
801 mMapStack
[mMapDepth
].End
= Entry
->End
;
804 // Inherit Attribute from the Memory Descriptor that is being clipped
806 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
808 Entry
->End
= Start
- 1;
809 ASSERT (Entry
->Start
< Entry
->End
);
811 Entry
= &mMapStack
[mMapDepth
];
812 InsertTailList (&gMemoryMap
, &Entry
->Link
);
815 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
819 // The new range inherits the same Attribute as the Entry
820 //it is being cut out of
822 Attribute
= Entry
->Attribute
;
825 // If the descriptor is empty, then remove it from the map
827 if (Entry
->Start
== Entry
->End
+ 1) {
828 RemoveMemoryMapEntry (Entry
);
833 // Add our new range in
835 CoreAddRange (NewType
, Start
, RangeEnd
, Attribute
);
836 if (NewType
== EfiConventionalMemory
) {
838 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
839 // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees
840 // that the page starting at address 0 is always filled with zeros.
843 if (RangeEnd
> EFI_PAGE_SIZE
) {
844 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) EFI_PAGE_SIZE
, (UINTN
) (RangeEnd
- EFI_PAGE_SIZE
+ 1));
847 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
) Start
, (UINTN
) (RangeEnd
- Start
+ 1));
852 // Move any map descriptor stack to general pool
854 CoreFreeMemoryMapStack ();
857 // Bump the starting address, and convert the next range
859 Start
= RangeEnd
+ 1;
863 // Converted the whole range, done
872 Internal function. Finds a consecutive free page range below
873 the requested address.
875 @param MaxAddress The address that the range must be below
876 @param MinAddress The address that the range must be above
877 @param NumberOfPages Number of pages needed
878 @param NewType The type of memory the range is going to be
880 @param Alignment Bits to align with
882 @return The base address of the range, or 0 if the range was not found
887 IN UINT64 MaxAddress
,
888 IN UINT64 MinAddress
,
889 IN UINT64 NumberOfPages
,
890 IN EFI_MEMORY_TYPE NewType
,
894 UINT64 NumberOfBytes
;
898 UINT64 DescNumberOfBytes
;
902 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
906 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
909 // If MaxAddress is not aligned to the end of a page
913 // Change MaxAddress to be 1 page lower
915 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
918 // Set MaxAddress to a page boundary
920 MaxAddress
&= ~EFI_PAGE_MASK
;
923 // Set MaxAddress to end of the page
925 MaxAddress
|= EFI_PAGE_MASK
;
928 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
931 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
932 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
935 // If it's not a free entry, don't bother with it
937 if (Entry
->Type
!= EfiConventionalMemory
) {
941 DescStart
= Entry
->Start
;
942 DescEnd
= Entry
->End
;
945 // If desc is past max allowed address or below min allowed address, skip it
947 if ((DescStart
>= MaxAddress
) || (DescEnd
< MinAddress
)) {
952 // If desc ends past max allowed address, clip the end
954 if (DescEnd
>= MaxAddress
) {
955 DescEnd
= MaxAddress
;
958 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
961 // Compute the number of bytes we can used from this
962 // descriptor, and see it's enough to satisfy the request
964 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
966 if (DescNumberOfBytes
>= NumberOfBytes
) {
968 // If the start of the allocated range is below the min address allowed, skip it
970 if ((DescEnd
- NumberOfBytes
+ 1) < MinAddress
) {
975 // If this is the best match so far remember it
977 if (DescEnd
> Target
) {
984 // If this is a grow down, adjust target to be the allocation base
986 Target
-= NumberOfBytes
- 1;
989 // If we didn't find a match, return 0
991 if ((Target
& EFI_PAGE_MASK
) != 0) {
1000 Internal function. Finds a consecutive free page range below
1001 the requested address
1003 @param MaxAddress The address that the range must be below
1004 @param NoPages Number of pages needed
1005 @param NewType The type of memory the range is going to be
1007 @param Alignment Bits to align with
1009 @return The base address of the range, or 0 if the range was not found.
1014 IN UINT64 MaxAddress
,
1016 IN EFI_MEMORY_TYPE NewType
,
1023 // Attempt to find free pages in the preferred bin based on the requested memory type
1025 if ((UINT32
)NewType
< EfiMaxMemoryType
&& MaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1026 Start
= CoreFindFreePagesI (
1027 mMemoryTypeStatistics
[NewType
].MaximumAddress
,
1028 mMemoryTypeStatistics
[NewType
].BaseAddress
,
1039 // Attempt to find free pages in the default allocation bin
1041 if (MaxAddress
>= mDefaultMaximumAddress
) {
1042 Start
= CoreFindFreePagesI (mDefaultMaximumAddress
, 0, NoPages
, NewType
, Alignment
);
1044 if (Start
< mDefaultBaseAddress
) {
1045 mDefaultBaseAddress
= Start
;
1052 // The allocation did not succeed in any of the prefered bins even after
1053 // promoting resources. Attempt to find free pages anywhere is the requested
1054 // address range. If this allocation fails, then there are not enough
1055 // resources anywhere to satisfy the request.
1057 Start
= CoreFindFreePagesI (MaxAddress
, 0, NoPages
, NewType
, Alignment
);
1063 // If allocations from the preferred bins fail, then attempt to promote memory resources.
1065 if (!PromoteMemoryResource ()) {
1070 // If any memory resources were promoted, then re-attempt the allocation
1072 return FindFreePages (MaxAddress
, NoPages
, NewType
, Alignment
);
1077 Allocates pages from the memory map.
1079 @param Type The type of allocation to perform
1080 @param MemoryType The type of memory to turn the allocated pages
1082 @param NumberOfPages The number of pages to allocate
1083 @param Memory A pointer to receive the base allocated memory
1086 @return Status. On success, Memory is filled in with the base address allocated
1087 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1089 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1090 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1091 @retval EFI_SUCCESS Pages successfully allocated.
1097 IN EFI_ALLOCATE_TYPE Type
,
1098 IN EFI_MEMORY_TYPE MemoryType
,
1099 IN UINTN NumberOfPages
,
1100 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1108 if ((UINT32
)Type
>= MaxAllocateType
) {
1109 return EFI_INVALID_PARAMETER
;
1112 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
1113 MemoryType
== EfiConventionalMemory
) {
1114 return EFI_INVALID_PARAMETER
;
1117 if (Memory
== NULL
) {
1118 return EFI_INVALID_PARAMETER
;
1121 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1123 if (MemoryType
== EfiACPIReclaimMemory
||
1124 MemoryType
== EfiACPIMemoryNVS
||
1125 MemoryType
== EfiRuntimeServicesCode
||
1126 MemoryType
== EfiRuntimeServicesData
) {
1128 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1131 if (Type
== AllocateAddress
) {
1132 if ((*Memory
& (Alignment
- 1)) != 0) {
1133 return EFI_NOT_FOUND
;
1137 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1138 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1141 // If this is for below a particular address, then
1146 // The max address is the max natively addressable address for the processor
1148 MaxAddress
= MAX_ADDRESS
;
1150 if (Type
== AllocateMaxAddress
) {
1154 CoreAcquireMemoryLock ();
1157 // If not a specific address, then find an address to allocate
1159 if (Type
!= AllocateAddress
) {
1160 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1162 Status
= EFI_OUT_OF_RESOURCES
;
1168 // Convert pages from FreeMemory to the requested type
1170 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1173 CoreReleaseMemoryLock ();
1175 if (!EFI_ERROR (Status
)) {
1184 Frees previous allocated pages.
1186 @param Memory Base address of memory being freed
1187 @param NumberOfPages The number of pages to free
1189 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1190 @retval EFI_INVALID_PARAMETER Address not aligned
1191 @return EFI_SUCCESS -Pages successfully freed.
1197 IN EFI_PHYSICAL_ADDRESS Memory
,
1198 IN UINTN NumberOfPages
1209 CoreAcquireMemoryLock ();
1212 // Find the entry that the covers the range
1215 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1216 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1217 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1221 if (Link
== &gMemoryMap
) {
1222 Status
= EFI_NOT_FOUND
;
1226 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1228 ASSERT (Entry
!= NULL
);
1229 if (Entry
->Type
== EfiACPIReclaimMemory
||
1230 Entry
->Type
== EfiACPIMemoryNVS
||
1231 Entry
->Type
== EfiRuntimeServicesCode
||
1232 Entry
->Type
== EfiRuntimeServicesData
) {
1234 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1238 if ((Memory
& (Alignment
- 1)) != 0) {
1239 Status
= EFI_INVALID_PARAMETER
;
1243 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1244 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1246 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1248 if (EFI_ERROR (Status
)) {
1253 CoreReleaseMemoryLock ();
1258 This function checks to see if the last memory map descriptor in a memory map
1259 can be merged with any of the other memory map descriptors in a memorymap.
1260 Memory descriptors may be merged if they are adjacent and have the same type
1263 @param MemoryMap A pointer to the start of the memory map.
1264 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.
1265 @param DescriptorSize The size, in bytes, of an individual
1266 EFI_MEMORY_DESCRIPTOR.
1268 @return A pointer to the next available descriptor in MemoryMap
1271 EFI_MEMORY_DESCRIPTOR
*
1272 MergeMemoryMapDescriptor (
1273 IN EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1274 IN EFI_MEMORY_DESCRIPTOR
*MemoryMapDescriptor
,
1275 IN UINTN DescriptorSize
1279 // Traverse the array of descriptors in MemoryMap
1281 for (; MemoryMap
!= MemoryMapDescriptor
; MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, DescriptorSize
)) {
1283 // Check to see if the Type fields are identical.
1285 if (MemoryMap
->Type
!= MemoryMapDescriptor
->Type
) {
1290 // Check to see if the Attribute fields are identical.
1292 if (MemoryMap
->Attribute
!= MemoryMapDescriptor
->Attribute
) {
1297 // Check to see if MemoryMapDescriptor is immediately above MemoryMap
1299 if (MemoryMap
->PhysicalStart
+ EFI_PAGES_TO_SIZE ((UINTN
)MemoryMap
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1301 // Merge MemoryMapDescriptor into MemoryMap
1303 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1306 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1308 return MemoryMapDescriptor
;
1312 // Check to see if MemoryMapDescriptor is immediately below MemoryMap
1314 if (MemoryMap
->PhysicalStart
- EFI_PAGES_TO_SIZE ((UINTN
)MemoryMapDescriptor
->NumberOfPages
) == MemoryMapDescriptor
->PhysicalStart
) {
1316 // Merge MemoryMapDescriptor into MemoryMap
1318 MemoryMap
->PhysicalStart
= MemoryMapDescriptor
->PhysicalStart
;
1319 MemoryMap
->VirtualStart
= MemoryMapDescriptor
->VirtualStart
;
1320 MemoryMap
->NumberOfPages
+= MemoryMapDescriptor
->NumberOfPages
;
1323 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1325 return MemoryMapDescriptor
;
1330 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.
1332 // Return the slot immediately after MemoryMapDescriptor as the next available
1333 // slot in the MemoryMap array
1335 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor
, DescriptorSize
);
1339 This function returns a copy of the current memory map. The map is an array of
1340 memory descriptors, each of which describes a contiguous block of memory.
1342 @param MemoryMapSize A pointer to the size, in bytes, of the
1343 MemoryMap buffer. On input, this is the size of
1344 the buffer allocated by the caller. On output,
1345 it is the size of the buffer returned by the
1346 firmware if the buffer was large enough, or the
1347 size of the buffer needed to contain the map if
1348 the buffer was too small.
1349 @param MemoryMap A pointer to the buffer in which firmware places
1350 the current memory map.
1351 @param MapKey A pointer to the location in which firmware
1352 returns the key for the current memory map.
1353 @param DescriptorSize A pointer to the location in which firmware
1354 returns the size, in bytes, of an individual
1355 EFI_MEMORY_DESCRIPTOR.
1356 @param DescriptorVersion A pointer to the location in which firmware
1357 returns the version number associated with the
1358 EFI_MEMORY_DESCRIPTOR.
1360 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1362 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1363 buffer size needed to hold the memory map is
1364 returned in MemoryMapSize.
1365 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1371 IN OUT UINTN
*MemoryMapSize
,
1372 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1374 OUT UINTN
*DescriptorSize
,
1375 OUT UINT32
*DescriptorVersion
1381 UINTN NumberOfRuntimeEntries
;
1384 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1385 EFI_MEMORY_TYPE Type
;
1386 EFI_MEMORY_DESCRIPTOR
*MemoryMapStart
;
1389 // Make sure the parameters are valid
1391 if (MemoryMapSize
== NULL
) {
1392 return EFI_INVALID_PARAMETER
;
1395 CoreAcquireGcdMemoryLock ();
1398 // Count the number of Reserved and MMIO entries that are marked for runtime use
1400 NumberOfRuntimeEntries
= 0;
1401 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1402 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1403 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1404 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1405 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1406 NumberOfRuntimeEntries
++;
1411 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1414 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1415 // prevent people from having pointer math bugs in their code.
1416 // now you have to use *DescriptorSize to make things work.
1418 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1420 if (DescriptorSize
!= NULL
) {
1421 *DescriptorSize
= Size
;
1424 if (DescriptorVersion
!= NULL
) {
1425 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1428 CoreAcquireMemoryLock ();
1431 // Compute the buffer size needed to fit the entire map
1433 BufferSize
= Size
* NumberOfRuntimeEntries
;
1434 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1438 if (*MemoryMapSize
< BufferSize
) {
1439 Status
= EFI_BUFFER_TOO_SMALL
;
1443 if (MemoryMap
== NULL
) {
1444 Status
= EFI_INVALID_PARAMETER
;
1451 ZeroMem (MemoryMap
, BufferSize
);
1452 MemoryMapStart
= MemoryMap
;
1453 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1454 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1455 ASSERT (Entry
->VirtualStart
== 0);
1458 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1460 MemoryMap
->Type
= Entry
->Type
;
1461 MemoryMap
->PhysicalStart
= Entry
->Start
;
1462 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1463 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1465 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1466 // memory type bin and needs to be converted to the same memory type as the rest of the
1467 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1468 // improves the chances for a successful S4 resume in the presence of minor page allocation
1469 // differences across reboots.
1471 if (MemoryMap
->Type
== EfiConventionalMemory
) {
1472 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
1473 if (mMemoryTypeStatistics
[Type
].Special
&&
1474 mMemoryTypeStatistics
[Type
].NumberOfPages
> 0 &&
1475 Entry
->Start
>= mMemoryTypeStatistics
[Type
].BaseAddress
&&
1476 Entry
->End
<= mMemoryTypeStatistics
[Type
].MaximumAddress
) {
1477 MemoryMap
->Type
= Type
;
1481 MemoryMap
->Attribute
= Entry
->Attribute
;
1482 if (MemoryMap
->Type
< EfiMaxMemoryType
) {
1483 if (mMemoryTypeStatistics
[MemoryMap
->Type
].Runtime
) {
1484 MemoryMap
->Attribute
|= EFI_MEMORY_RUNTIME
;
1489 // Check to see if the new Memory Map Descriptor can be merged with an
1490 // existing descriptor if they are adjacent and have the same attributes
1492 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1495 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1496 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1497 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1498 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1499 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1501 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries
1502 // that are marked for runtime use
1504 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1505 MemoryMap
->VirtualStart
= 0;
1506 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1507 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1509 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1510 MemoryMap
->Type
= EfiReservedMemoryType
;
1511 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1512 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1513 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1515 MemoryMap
->Type
= EfiMemoryMappedIO
;
1520 // Check to see if the new Memory Map Descriptor can be merged with an
1521 // existing descriptor if they are adjacent and have the same attributes
1523 MemoryMap
= MergeMemoryMapDescriptor (MemoryMapStart
, MemoryMap
, Size
);
1529 // Compute the size of the buffer actually used after all memory map descriptor merge operations
1531 BufferSize
= ((UINT8
*)MemoryMap
- (UINT8
*)MemoryMapStart
);
1533 Status
= EFI_SUCCESS
;
1537 // Update the map key finally
1539 if (MapKey
!= NULL
) {
1540 *MapKey
= mMemoryMapKey
;
1543 CoreReleaseMemoryLock ();
1545 CoreReleaseGcdMemoryLock ();
1547 *MemoryMapSize
= BufferSize
;
1554 Internal function. Used by the pool functions to allocate pages
1555 to back pool allocation requests.
1557 @param PoolType The type of memory for the new pool pages
1558 @param NumberOfPages No of pages to allocate
1559 @param Alignment Bits to align.
1561 @return The allocated memory, or NULL
1565 CoreAllocatePoolPages (
1566 IN EFI_MEMORY_TYPE PoolType
,
1567 IN UINTN NumberOfPages
,
1574 // Find the pages to convert
1576 Start
= FindFreePages (MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1579 // Convert it to boot services data
1582 DEBUG ((DEBUG_ERROR
| DEBUG_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32
)NumberOfPages
));
1584 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1587 return (VOID
*)(UINTN
) Start
;
1592 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1594 @param Memory The base address to free
1595 @param NumberOfPages The number of pages to free
1600 IN EFI_PHYSICAL_ADDRESS Memory
,
1601 IN UINTN NumberOfPages
1604 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1610 Make sure the memory map is following all the construction rules,
1611 it is the last time to check memory map error before exit boot services.
1613 @param MapKey Memory map key
1615 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1617 @retval EFI_SUCCESS Valid memory map.
1621 CoreTerminateMemoryMap (
1629 Status
= EFI_SUCCESS
;
1631 CoreAcquireMemoryLock ();
1633 if (MapKey
== mMemoryMapKey
) {
1636 // Make sure the memory map is following all the construction rules
1637 // This is the last chance we will be able to display any messages on
1638 // the console devices.
1641 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1642 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1643 if ((Entry
->Attribute
& EFI_MEMORY_RUNTIME
) != 0) {
1644 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1645 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1646 Status
= EFI_INVALID_PARAMETER
;
1649 if ((Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1650 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1651 Status
= EFI_INVALID_PARAMETER
;
1654 if (((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) != 0) {
1655 DEBUG((DEBUG_ERROR
| DEBUG_PAGE
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1656 Status
= EFI_INVALID_PARAMETER
;
1663 // The map key they gave us matches what we expect. Fall through and
1664 // return success. In an ideal world we would clear out all of
1665 // EfiBootServicesCode and EfiBootServicesData. However this function
1666 // is not the last one called by ExitBootServices(), so we have to
1667 // preserve the memory contents.
1670 Status
= EFI_INVALID_PARAMETER
;
1674 CoreReleaseMemoryLock ();