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
;
36 UINTN InformationIndex
;
37 } EFI_MEMORY_TYPE_STAISTICS
;
40 // MemoryMap - The current memory map
42 UINTN mMemoryMapKey
= 0;
45 // mMapStack - space to use as temp storage to build new map descriptors
46 // mMapDepth - depth of new descriptor stack
49 #define MAX_MAP_DEPTH 6
51 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
52 UINTN mFreeMapStack
= 0;
54 // This list maintain the free memory map list
56 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
57 BOOLEAN mMemoryTypeInformationInitialized
= FALSE
;
59 EFI_MEMORY_TYPE_STAISTICS mMemoryTypeStatistics
[EfiMaxMemoryType
+ 1] = {
60 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiReservedMemoryType
61 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiLoaderCode
62 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiLoaderData
63 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiBootServicesCode
64 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiBootServicesData
65 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiRuntimeServicesCode
66 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiRuntimeServicesData
67 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiConventionalMemory
68 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiUnusableMemory
69 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiACPIReclaimMemory
70 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiACPIMemoryNVS
71 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiMemoryMappedIO
72 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiMemoryMappedIOPortSpace
73 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
}, // EfiPalCode
74 { 0, EFI_MAX_ADDRESS
, 0, EfiMaxMemoryType
} // EfiMaxMemoryType
77 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress
= EFI_MAX_ADDRESS
;
79 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation
[EfiMaxMemoryType
+ 1] = {
80 { EfiReservedMemoryType
, 0 },
83 { EfiBootServicesCode
, 0 },
84 { EfiBootServicesData
, 0 },
85 { EfiRuntimeServicesCode
, 0 },
86 { EfiRuntimeServicesData
, 0 },
87 { EfiConventionalMemory
, 0 },
88 { EfiUnusableMemory
, 0 },
89 { EfiACPIReclaimMemory
, 0 },
90 { EfiACPIMemoryNVS
, 0 },
91 { EfiMemoryMappedIO
, 0 },
92 { EfiMemoryMappedIOPortSpace
, 0 },
94 { EfiMaxMemoryType
, 0 }
98 // Internal prototypes
102 PromoteMemoryResource (
109 IN EFI_MEMORY_TYPE Type
,
110 IN EFI_PHYSICAL_ADDRESS Start
,
111 IN EFI_PHYSICAL_ADDRESS End
,
117 CoreFreeMemoryMapStack (
125 IN UINT64 NumberOfPages
,
126 IN EFI_MEMORY_TYPE NewType
131 RemoveMemoryMapEntry (
137 AllocateMemoryMapEntry (
141 CoreAcquireMemoryLock (
148 Enter critical section by gaining lock on gMemoryLock
160 CoreAcquireLock (&gMemoryLock
);
165 CoreReleaseMemoryLock (
172 Exit critical section by releasing lock on gMemoryLock
184 CoreReleaseLock (&gMemoryLock
);
189 PromoteMemoryResource (
196 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.
209 EFI_GCD_MAP_ENTRY
*Entry
;
211 DEBUG ((EFI_D_ERROR
| EFI_D_PAGE
, "Promote the memory resource\n"));
213 CoreAcquireGcdMemoryLock ();
215 Link
= mGcdMemorySpaceMap
.ForwardLink
;
216 while (Link
!= &mGcdMemorySpaceMap
) {
218 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
220 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
&&
221 Entry
->EndAddress
< EFI_MAX_ADDRESS
&&
222 (Entry
->Capabilities
& (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
)) ==
223 (EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
)) {
225 // Update the GCD map
227 Entry
->GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
228 Entry
->Capabilities
|= EFI_MEMORY_TESTED
;
229 Entry
->ImageHandle
= gDxeCoreImageHandle
;
230 Entry
->DeviceHandle
= NULL
;
233 // Add to allocable system memory resource
237 EfiConventionalMemory
,
240 Entry
->Capabilities
& ~(EFI_MEMORY_PRESENT
| EFI_MEMORY_INITIALIZED
| EFI_MEMORY_TESTED
| EFI_MEMORY_RUNTIME
)
242 CoreFreeMemoryMapStack ();
246 Link
= Link
->ForwardLink
;
249 CoreReleaseGcdMemoryLock ();
255 CoreAddMemoryDescriptor (
256 IN EFI_MEMORY_TYPE Type
,
257 IN EFI_PHYSICAL_ADDRESS Start
,
258 IN UINT64 NumberOfPages
,
265 Called to initialize the memory map and add descriptors to
266 the current descriptor list.
268 The first descriptor that is added must be general usable
269 memory as the addition allocates heap.
273 Type - The type of memory to add
275 Start - The starting address in the memory range
278 NumberOfPages - The number of pages in the range
280 Attribute - Attributes of the memory to add
284 None. The range is added to the memory map
288 EFI_PHYSICAL_ADDRESS End
;
293 if ((Start
& EFI_PAGE_MASK
) != 0) {
297 if (Type
>= EfiMaxMemoryType
&& Type
<= 0x7fffffff) {
301 CoreAcquireMemoryLock ();
302 End
= Start
+ LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
303 CoreAddRange (Type
, Start
, End
, Attribute
);
304 CoreFreeMemoryMapStack ();
305 CoreReleaseMemoryLock ();
308 // Check to see if the statistics for the different memory types have already been established
310 if (mMemoryTypeInformationInitialized
) {
315 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
317 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
319 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
321 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
322 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
326 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
328 // Allocate pages for the current memory type from the top of available memory
330 Status
= CoreAllocatePages (
333 gMemoryTypeInformation
[Index
].NumberOfPages
,
334 &mMemoryTypeStatistics
[Type
].BaseAddress
336 if (EFI_ERROR (Status
)) {
338 // If an error occurs allocating the pages for the current memory type, then
339 // free all the pages allocates for the previous memory types and return. This
340 // operation with be retied when/if more memory is added to the system
342 for (FreeIndex
= 0; FreeIndex
< Index
; FreeIndex
++) {
344 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
346 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[FreeIndex
].Type
);
347 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
351 if (gMemoryTypeInformation
[FreeIndex
].NumberOfPages
!= 0) {
353 mMemoryTypeStatistics
[Type
].BaseAddress
,
354 gMemoryTypeInformation
[FreeIndex
].NumberOfPages
356 mMemoryTypeStatistics
[Type
].BaseAddress
= 0;
357 mMemoryTypeStatistics
[Type
].MaximumAddress
= EFI_MAX_ADDRESS
;
364 // Compute the address at the top of the current statistics
366 mMemoryTypeStatistics
[Type
].MaximumAddress
=
367 mMemoryTypeStatistics
[Type
].BaseAddress
+
368 LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
) - 1;
371 // If the current base address is the lowest address so far, then update the default
374 if (mMemoryTypeStatistics
[Type
].BaseAddress
< mDefaultMaximumAddress
) {
375 mDefaultMaximumAddress
= mMemoryTypeStatistics
[Type
].BaseAddress
- 1;
381 // There was enough system memory for all the the memory types were allocated. So,
382 // those memory areas can be freed for future allocations, and all future memory
383 // allocations can occur within their respective bins
385 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
387 // Make sure the memory type in the gMemoryTypeInformation[] array is valid
389 Type
= (EFI_MEMORY_TYPE
) (gMemoryTypeInformation
[Index
].Type
);
390 if (Type
< 0 || Type
> EfiMaxMemoryType
) {
394 if (gMemoryTypeInformation
[Index
].NumberOfPages
!= 0) {
396 mMemoryTypeStatistics
[Type
].BaseAddress
,
397 gMemoryTypeInformation
[Index
].NumberOfPages
399 gMemoryTypeInformation
[Index
].NumberOfPages
= 0;
404 // If the number of pages reserved for a memory type is 0, then all allocations for that type
405 // should be in the default range.
407 for (Type
= (EFI_MEMORY_TYPE
) 0; Type
< EfiMaxMemoryType
; Type
++) {
408 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
409 if (Type
== (EFI_MEMORY_TYPE
)gMemoryTypeInformation
[Index
].Type
) {
410 mMemoryTypeStatistics
[Type
].InformationIndex
= Index
;
413 mMemoryTypeStatistics
[Type
].CurrentNumberOfPages
= 0;
414 if (mMemoryTypeStatistics
[Type
].MaximumAddress
== EFI_MAX_ADDRESS
) {
415 mMemoryTypeStatistics
[Type
].MaximumAddress
= mDefaultMaximumAddress
;
419 mMemoryTypeInformationInitialized
= TRUE
;
426 IN EFI_MEMORY_TYPE Type
,
427 IN EFI_PHYSICAL_ADDRESS Start
,
428 IN EFI_PHYSICAL_ADDRESS End
,
435 Internal function. Adds a ranges to the memory map.
436 The range must not already exist in the map.
440 Type - The type of memory range to add
442 Start - The starting address in the memory range
443 Must be paged aligned
445 End - The last address in the range
446 Must be the last byte of a page
448 Attribute - The attributes of the memory range to add
452 None. The range is added to the memory map
459 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
460 ASSERT (End
> Start
) ;
462 ASSERT_LOCKED (&gMemoryLock
);
464 DEBUG ((EFI_D_PAGE
, "AddRange: %lx-%lx to %d\n", Start
, End
, Type
));
467 // Memory map being altered so updated key
472 // UEFI 2.0 added an event group for notificaiton on memory map changes.
473 // So we need to signal this Event Group every time the memory map changes.
474 // If we are in EFI 1.10 compatability mode no event groups will be
475 // found and nothing will happen we we call this function. These events
476 // will get signaled but since a lock is held around the call to this
477 // function the notificaiton events will only be called after this funciton
478 // returns and the lock is released.
480 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid
);
483 // Look for adjoining memory descriptor
486 // Two memory descriptors can only be merged if they have the same Type
487 // and the same Attribute
490 Link
= gMemoryMap
.ForwardLink
;
491 while (Link
!= &gMemoryMap
) {
492 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
493 Link
= Link
->ForwardLink
;
495 if (Entry
->Type
!= Type
) {
499 if (Entry
->Attribute
!= Attribute
) {
503 if (Entry
->End
+ 1 == Start
) {
505 Start
= Entry
->Start
;
506 RemoveMemoryMapEntry (Entry
);
508 } else if (Entry
->Start
== End
+ 1) {
511 RemoveMemoryMapEntry (Entry
);
519 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
520 mMapStack
[mMapDepth
].FromPages
= FALSE
;
521 mMapStack
[mMapDepth
].Type
= Type
;
522 mMapStack
[mMapDepth
].Start
= Start
;
523 mMapStack
[mMapDepth
].End
= End
;
524 mMapStack
[mMapDepth
].VirtualStart
= 0;
525 mMapStack
[mMapDepth
].Attribute
= Attribute
;
526 InsertTailList (&gMemoryMap
, &mMapStack
[mMapDepth
].Link
);
529 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
536 CoreFreeMemoryMapStack (
543 Internal function. Moves any memory descriptors that are on the
544 temporary descriptor stack to heap.
560 ASSERT_LOCKED (&gMemoryLock
);
563 // If already freeing the map stack, then return
570 // Move the temporary memory descriptor stack into pool
576 // Deque an memory map entry from mFreeMemoryMapEntryList
578 Entry
= AllocateMemoryMapEntry ();
583 // Update to proper entry
587 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
590 // Move this entry to general memory
592 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
593 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
595 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
596 Entry
->FromPages
= TRUE
;
599 // Find insertion location
601 for (Link2
= gMemoryMap
.ForwardLink
; Link2
!= &gMemoryMap
; Link2
= Link2
->ForwardLink
) {
602 Entry2
= CR (Link2
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
603 if (Entry2
->FromPages
&& Entry2
->Start
> Entry
->Start
) {
608 InsertTailList (Link2
, &Entry
->Link
);
612 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
613 // so here no need to move it to memory.
615 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
624 RemoveMemoryMapEntry (
631 Internal function. Removes a descriptor entry.
635 Entry - The entry to remove
643 RemoveEntryList (&Entry
->Link
);
644 Entry
->Link
.ForwardLink
= NULL
;
646 if (Entry
->FromPages
) {
648 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
650 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
655 AllocateMemoryMapEntry (
661 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
662 If the list is emtry, then allocate a new page to refuel the list.
663 Please Note this algorithm to allocate the memory map descriptor has a property
664 that the memory allocated for memory entries always grows, and will never really be freed
665 For example, if the current boot uses 2000 memory map entries at the maximum point, but
666 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
667 memory map entries is still allocated from EfiBootServicesMemory.
675 The Memory map descriptor dequed from the mFreeMemoryMapEntryList
679 MEMORY_MAP
* FreeDescriptorEntries
;
683 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
685 // The list is empty, to allocate one page to refuel the list
687 FreeDescriptorEntries
= CoreAllocatePoolPages (EfiBootServicesData
, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION
), DEFAULT_PAGE_ALLOCATION
);
688 if(FreeDescriptorEntries
!= NULL
) {
690 // Enque the free memmory map entries into the list
692 for (Index
= 0; Index
< DEFAULT_PAGE_ALLOCATION
/ sizeof(MEMORY_MAP
); Index
++) {
693 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
694 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
701 // dequeue the first descriptor from the list
703 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
704 RemoveEntryList (&Entry
->Link
);
713 IN UINT64 NumberOfPages
,
714 IN EFI_MEMORY_TYPE NewType
720 Internal function. Converts a memory range to the specified type.
721 The range must exist in the memory map.
725 Start - The first address of the range
728 NumberOfPages - The number of pages to convert
730 NewType - The new type for the memory range
734 EFI_INVALID_PARAMETER - Invalid parameter
736 EFI_NOT_FOUND - Could not find a descriptor cover the specified range
737 or convertion not allowed.
739 EFI_SUCCESS - Successfully converts the memory range to the specified type.
744 UINT64 NumberOfBytes
;
752 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
753 End
= Start
+ NumberOfBytes
- 1;
755 ASSERT (NumberOfPages
);
756 ASSERT ((Start
& EFI_PAGE_MASK
) == 0);
757 ASSERT (End
> Start
) ;
758 ASSERT_LOCKED (&gMemoryLock
);
760 if (NumberOfPages
== 0 || (Start
& EFI_PAGE_MASK
) || (Start
> (Start
+ NumberOfBytes
))) {
761 return EFI_INVALID_PARAMETER
;
765 // Convert the entire range
768 while (Start
< End
) {
771 // Find the entry that the covers the range
773 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
774 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
776 if (Entry
->Start
<= Start
&& Entry
->End
> Start
) {
781 if (Link
== &gMemoryMap
) {
782 DEBUG ((EFI_D_ERROR
| EFI_D_PAGE
, "ConvertPages: failed to find range %lx - %lx\n", Start
, End
));
783 return EFI_NOT_FOUND
;
787 // Convert range to the end, or to the end of the descriptor
788 // if that's all we've got
791 if (Entry
->End
< End
) {
792 RangeEnd
= Entry
->End
;
795 DEBUG ((EFI_D_PAGE
, "ConvertRange: %lx-%lx to %d\n", Start
, RangeEnd
, NewType
));
798 // Debug code - verify conversion is allowed
800 if (!(NewType
== EfiConventionalMemory
? 1 : 0) ^ (Entry
->Type
== EfiConventionalMemory
? 1 : 0)) {
801 DEBUG ((EFI_D_ERROR
, "ConvertPages: Incompatible memory types\n"));
802 return EFI_NOT_FOUND
;
806 // Update counters for the number of pages allocated to each memory type
808 if (Entry
->Type
>= 0 && Entry
->Type
< EfiMaxMemoryType
) {
809 if (Start
>= mMemoryTypeStatistics
[Entry
->Type
].BaseAddress
&&
810 Start
<= mMemoryTypeStatistics
[Entry
->Type
].MaximumAddress
) {
811 if (NumberOfPages
> mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
) {
812 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
= 0;
814 mMemoryTypeStatistics
[Entry
->Type
].CurrentNumberOfPages
-= NumberOfPages
;
819 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
) {
820 if (Start
>= mMemoryTypeStatistics
[NewType
].BaseAddress
&& Start
<= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
821 mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
+= NumberOfPages
;
822 if (mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
>
823 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
) {
824 gMemoryTypeInformation
[mMemoryTypeStatistics
[NewType
].InformationIndex
].NumberOfPages
= (UINT32
)mMemoryTypeStatistics
[NewType
].CurrentNumberOfPages
;
830 // Pull range out of descriptor
832 if (Entry
->Start
== Start
) {
837 Entry
->Start
= RangeEnd
+ 1;
839 } else if (Entry
->End
== RangeEnd
) {
844 Entry
->End
= Start
- 1;
849 // Pull it out of the center, clip current
855 mMapStack
[mMapDepth
].Signature
= MEMORY_MAP_SIGNATURE
;
856 mMapStack
[mMapDepth
].FromPages
= FALSE
;
857 mMapStack
[mMapDepth
].Type
= Entry
->Type
;
858 mMapStack
[mMapDepth
].Start
= RangeEnd
+1;
859 mMapStack
[mMapDepth
].End
= Entry
->End
;
862 // Inherit Attribute from the Memory Descriptor that is being clipped
864 mMapStack
[mMapDepth
].Attribute
= Entry
->Attribute
;
866 Entry
->End
= Start
- 1;
867 ASSERT (Entry
->Start
< Entry
->End
);
869 Entry
= &mMapStack
[mMapDepth
];
870 InsertTailList (&gMemoryMap
, &Entry
->Link
);
873 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
877 // The new range inherits the same Attribute as the Entry
878 //it is being cut out of
880 Attribute
= Entry
->Attribute
;
883 // If the descriptor is empty, then remove it from the map
885 if (Entry
->Start
== Entry
->End
+ 1) {
886 RemoveMemoryMapEntry (Entry
);
891 // Add our new range in
893 CoreAddRange (NewType
, Start
, RangeEnd
, Attribute
);
896 // Move any map descriptor stack to general pool
898 CoreFreeMemoryMapStack ();
901 // Bump the starting address, and convert the next range
903 Start
= RangeEnd
+ 1;
907 // Converted the whole range, done
917 IN UINT64 MaxAddress
,
918 IN UINT64 NumberOfPages
,
919 IN EFI_MEMORY_TYPE NewType
,
926 Internal function. Finds a consecutive free page range below
927 the requested address.
931 MaxAddress - The address that the range must be below
933 NumberOfPages - Number of pages needed
935 NewType - The type of memory the range is going to be turned into
937 Alignment - Bits to align with
941 The base address of the range, or 0 if the range was not found
945 UINT64 NumberOfBytes
;
949 UINT64 DescNumberOfBytes
;
953 if ((MaxAddress
< EFI_PAGE_MASK
) ||(NumberOfPages
== 0)) {
957 if ((MaxAddress
& EFI_PAGE_MASK
) != EFI_PAGE_MASK
) {
960 // If MaxAddress is not aligned to the end of a page
964 // Change MaxAddress to be 1 page lower
966 MaxAddress
-= (EFI_PAGE_MASK
+ 1);
969 // Set MaxAddress to a page boundary
971 MaxAddress
&= ~EFI_PAGE_MASK
;
974 // Set MaxAddress to end of the page
976 MaxAddress
|= EFI_PAGE_MASK
;
979 NumberOfBytes
= LShiftU64 (NumberOfPages
, EFI_PAGE_SHIFT
);
982 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
983 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
986 // If it's not a free entry, don't bother with it
988 if (Entry
->Type
!= EfiConventionalMemory
) {
992 DescStart
= Entry
->Start
;
993 DescEnd
= Entry
->End
;
996 // If desc is past max allowed address, skip it
998 if (DescStart
>= MaxAddress
) {
1003 // If desc ends past max allowed address, clip the end
1005 if (DescEnd
>= MaxAddress
) {
1006 DescEnd
= MaxAddress
;
1009 DescEnd
= ((DescEnd
+ 1) & (~(Alignment
- 1))) - 1;
1012 // Compute the number of bytes we can used from this
1013 // descriptor, and see it's enough to satisfy the request
1015 DescNumberOfBytes
= DescEnd
- DescStart
+ 1;
1017 if (DescNumberOfBytes
>= NumberOfBytes
) {
1020 // If this is the best match so far remember it
1022 if (DescEnd
> Target
) {
1029 // If this is a grow down, adjust target to be the allocation base
1031 Target
-= NumberOfBytes
- 1;
1034 // If we didn't find a match, return 0
1036 if ((Target
& EFI_PAGE_MASK
) != 0) {
1046 IN UINT64 MaxAddress
,
1048 IN EFI_MEMORY_TYPE NewType
,
1053 Routine Description:
1055 Internal function. Finds a consecutive free page range below
1056 the requested address
1060 MaxAddress - The address that the range must be below
1062 NoPages - Number of pages needed
1064 NewType - The type of memory the range is going to be turned into
1066 Alignment - Bits to align with
1070 The base address of the range, or 0 if the range was not found.
1074 UINT64 NewMaxAddress
;
1077 NewMaxAddress
= MaxAddress
;
1079 if (NewType
>= 0 && NewType
< EfiMaxMemoryType
&& NewMaxAddress
>= mMemoryTypeStatistics
[NewType
].MaximumAddress
) {
1080 NewMaxAddress
= mMemoryTypeStatistics
[NewType
].MaximumAddress
;
1082 if (NewMaxAddress
> mDefaultMaximumAddress
) {
1083 NewMaxAddress
= mDefaultMaximumAddress
;
1087 Start
= CoreFindFreePagesI (NewMaxAddress
, NoPages
, NewType
, Alignment
);
1089 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1092 // Here means there may be no enough memory to use, so try to go through
1093 // all the memory descript to promote the untested memory directly
1095 PromoteMemoryResource ();
1098 // Allocate memory again after the memory resource re-arranged
1100 Start
= CoreFindFreePagesI (MaxAddress
, NoPages
, NewType
, Alignment
);
1111 IN EFI_ALLOCATE_TYPE Type
,
1112 IN EFI_MEMORY_TYPE MemoryType
,
1113 IN UINTN NumberOfPages
,
1114 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
1118 Routine Description:
1120 Allocates pages from the memory map.
1124 Type - The type of allocation to perform
1126 MemoryType - The type of memory to turn the allocated pages into
1128 NumberOfPages - The number of pages to allocate
1130 Memory - A pointer to receive the base allocated memory address
1134 Status. On success, Memory is filled in with the base address allocated
1136 EFI_INVALID_PARAMETER - Parameters violate checking rules defined in spec.
1138 EFI_NOT_FOUND - Could not allocate pages match the requirement.
1140 EFI_OUT_OF_RESOURCES - No enough pages to allocate.
1142 EFI_SUCCESS - Pages successfully allocated.
1151 if (Type
< AllocateAnyPages
|| Type
>= (UINTN
) MaxAllocateType
) {
1152 return EFI_INVALID_PARAMETER
;
1155 if ((MemoryType
>= EfiMaxMemoryType
&& MemoryType
<= 0x7fffffff) ||
1156 MemoryType
== EfiConventionalMemory
) {
1157 return EFI_INVALID_PARAMETER
;
1160 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1162 if (MemoryType
== EfiACPIReclaimMemory
||
1163 MemoryType
== EfiACPIMemoryNVS
||
1164 MemoryType
== EfiRuntimeServicesCode
||
1165 MemoryType
== EfiRuntimeServicesData
) {
1167 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1170 if (Type
== AllocateAddress
) {
1171 if ((*Memory
& (Alignment
- 1)) != 0) {
1172 return EFI_NOT_FOUND
;
1176 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1177 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1180 // If this is for below a particular address, then
1185 // The max address is the max natively addressable address for the processor
1187 MaxAddress
= EFI_MAX_ADDRESS
;
1189 if (Type
== AllocateMaxAddress
) {
1193 CoreAcquireMemoryLock ();
1196 // If not a specific address, then find an address to allocate
1198 if (Type
!= AllocateAddress
) {
1199 Start
= FindFreePages (MaxAddress
, NumberOfPages
, MemoryType
, Alignment
);
1201 Status
= EFI_OUT_OF_RESOURCES
;
1207 // Convert pages from FreeMemory to the requested type
1209 Status
= CoreConvertPages (Start
, NumberOfPages
, MemoryType
);
1212 CoreReleaseMemoryLock ();
1214 if (!EFI_ERROR (Status
)) {
1227 IN EFI_PHYSICAL_ADDRESS Memory
,
1228 IN UINTN NumberOfPages
1232 Routine Description:
1234 Frees previous allocated pages.
1238 Memory - Base address of memory being freed
1240 NumberOfPages - The number of pages to free
1244 EFI_NOT_FOUND - Could not find the entry that covers the range
1246 EFI_INVALID_PARAMETER - Address not aligned
1248 EFI_SUCCESS -Pages successfully freed.
1260 CoreAcquireMemoryLock ();
1263 // Find the entry that the covers the range
1266 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1267 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1268 if (Entry
->Start
<= Memory
&& Entry
->End
> Memory
) {
1272 if (Link
== &gMemoryMap
) {
1273 CoreReleaseMemoryLock ();
1274 return EFI_NOT_FOUND
;
1277 Alignment
= EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT
;
1279 if (Entry
->Type
== EfiACPIReclaimMemory
||
1280 Entry
->Type
== EfiACPIMemoryNVS
||
1281 Entry
->Type
== EfiRuntimeServicesCode
||
1282 Entry
->Type
== EfiRuntimeServicesData
) {
1284 Alignment
= EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
;
1288 if ((Memory
& (Alignment
- 1)) != 0) {
1289 CoreReleaseMemoryLock ();
1290 return EFI_INVALID_PARAMETER
;
1293 NumberOfPages
+= EFI_SIZE_TO_PAGES (Alignment
) - 1;
1294 NumberOfPages
&= ~(EFI_SIZE_TO_PAGES (Alignment
) - 1);
1296 Status
= CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1298 CoreReleaseMemoryLock ();
1300 if (EFI_ERROR (Status
)) {
1305 // Destroy the contents
1307 if (Memory
< EFI_MAX_ADDRESS
) {
1308 DEBUG_CLEAR_MEMORY ((VOID
*)(UINTN
)Memory
, NumberOfPages
<< EFI_PAGE_SHIFT
);
1319 IN OUT UINTN
*MemoryMapSize
,
1320 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1322 OUT UINTN
*DescriptorSize
,
1323 OUT UINT32
*DescriptorVersion
1327 Routine Description:
1329 This function returns a copy of the current memory map. The map is an array of
1330 memory descriptors, each of which describes a contiguous block of memory.
1334 MemoryMapSize - A pointer to the size, in bytes, of the MemoryMap buffer. On
1335 input, this is the size of the buffer allocated by the caller.
1336 On output, it is the size of the buffer returned by the firmware
1337 if the buffer was large enough, or the size of the buffer needed
1338 to contain the map if the buffer was too small.
1339 MemoryMap - A pointer to the buffer in which firmware places the current memory map.
1340 MapKey - A pointer to the location in which firmware returns the key for the
1342 DescriptorSize - A pointer to the location in which firmware returns the size, in
1343 bytes, of an individual EFI_MEMORY_DESCRIPTOR.
1344 DescriptorVersion - A pointer to the location in which firmware returns the version
1345 number associated with the EFI_MEMORY_DESCRIPTOR.
1349 EFI_SUCCESS - The memory map was returned in the MemoryMap buffer.
1350 EFI_BUFFER_TOO_SMALL - The MemoryMap buffer was too small. The current buffer size
1351 needed to hold the memory map is returned in MemoryMapSize.
1352 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
1359 UINTN NumberOfRuntimeEntries
;
1362 EFI_GCD_MAP_ENTRY
*GcdMapEntry
;
1365 // Make sure the parameters are valid
1367 if (MemoryMapSize
== NULL
) {
1368 return EFI_INVALID_PARAMETER
;
1371 CoreAcquireGcdMemoryLock ();
1374 // Count the number of Reserved and MMIO entries that are marked for runtime use
1376 NumberOfRuntimeEntries
= 0;
1377 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1378 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1379 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1380 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1381 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1382 NumberOfRuntimeEntries
++;
1387 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1390 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1391 // prevent people from having pointer math bugs in their code.
1392 // now you have to use *DescriptorSize to make things work.
1394 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1396 if (DescriptorSize
!= NULL
) {
1397 *DescriptorSize
= Size
;
1400 if (DescriptorVersion
!= NULL
) {
1401 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1404 CoreAcquireMemoryLock ();
1407 // Compute the buffer size needed to fit the entire map
1409 BufferSize
= Size
* NumberOfRuntimeEntries
;
1410 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1414 if (*MemoryMapSize
< BufferSize
) {
1415 Status
= EFI_BUFFER_TOO_SMALL
;
1419 if (MemoryMap
== NULL
) {
1420 Status
= EFI_INVALID_PARAMETER
;
1427 ZeroMem (MemoryMap
, Size
);
1428 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1429 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1430 ASSERT (Entry
->VirtualStart
== 0);
1432 MemoryMap
->Type
= Entry
->Type
;
1433 MemoryMap
->PhysicalStart
= Entry
->Start
;
1434 MemoryMap
->VirtualStart
= Entry
->VirtualStart
;
1435 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1437 switch (Entry
->Type
) {
1438 case EfiRuntimeServicesCode
:
1439 case EfiRuntimeServicesData
:
1441 MemoryMap
->Attribute
= Entry
->Attribute
| EFI_MEMORY_RUNTIME
;
1445 MemoryMap
->Attribute
= Entry
->Attribute
;
1449 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1452 for (Link
= mGcdMemorySpaceMap
.ForwardLink
; Link
!= &mGcdMemorySpaceMap
; Link
= Link
->ForwardLink
) {
1453 GcdMapEntry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1454 if ((GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) ||
1455 (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
)) {
1456 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
1458 MemoryMap
->PhysicalStart
= GcdMapEntry
->BaseAddress
;
1459 MemoryMap
->VirtualStart
= 0;
1460 MemoryMap
->NumberOfPages
= RShiftU64 ((GcdMapEntry
->EndAddress
- GcdMapEntry
->BaseAddress
+ 1), EFI_PAGE_SHIFT
);
1461 MemoryMap
->Attribute
= GcdMapEntry
->Attributes
& ~EFI_MEMORY_PORT_IO
;
1463 if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeReserved
) {
1464 MemoryMap
->Type
= EfiReservedMemoryType
;
1465 } else if (GcdMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
1466 if ((GcdMapEntry
->Attributes
& EFI_MEMORY_PORT_IO
) == EFI_MEMORY_PORT_IO
) {
1467 MemoryMap
->Type
= EfiMemoryMappedIOPortSpace
;
1469 MemoryMap
->Type
= EfiMemoryMappedIO
;
1473 MemoryMap
= NextMemoryDescriptor (MemoryMap
, Size
);
1478 Status
= EFI_SUCCESS
;
1482 CoreReleaseMemoryLock ();
1484 CoreReleaseGcdMemoryLock ();
1487 // Update the map key finally
1489 if (MapKey
!= NULL
) {
1490 *MapKey
= mMemoryMapKey
;
1493 *MemoryMapSize
= BufferSize
;
1499 CoreAllocatePoolPages (
1500 IN EFI_MEMORY_TYPE PoolType
,
1501 IN UINTN NumberOfPages
,
1506 Routine Description:
1508 Internal function. Used by the pool functions to allocate pages
1509 to back pool allocation requests.
1513 PoolType - The type of memory for the new pool pages
1515 NumberOfPages - No of pages to allocate
1517 Alignment - Bits to align.
1521 The allocated memory, or NULL
1528 // Find the pages to convert
1530 Start
= FindFreePages (EFI_MAX_ADDRESS
, NumberOfPages
, PoolType
, Alignment
);
1533 // Convert it to boot services data
1536 DEBUG ((EFI_D_ERROR
| EFI_D_PAGE
, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages
));
1538 CoreConvertPages (Start
, NumberOfPages
, PoolType
);
1541 return (VOID
*)(UINTN
)Start
;
1546 IN EFI_PHYSICAL_ADDRESS Memory
,
1547 IN UINTN NumberOfPages
1551 Routine Description:
1553 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1557 Memory - The base address to free
1559 NumberOfPages - The number of pages to free
1567 CoreConvertPages (Memory
, NumberOfPages
, EfiConventionalMemory
);
1572 CoreTerminateMemoryMap (
1577 Routine Description:
1579 Make sure the memory map is following all the construction rules,
1580 it is the last time to check memory map error before exit boot services.
1584 MapKey - Memory map key
1588 EFI_INVALID_PARAMETER - Memory map not consistent with construction rules.
1590 EFI_SUCCESS - Valid memory map.
1598 Status
= EFI_SUCCESS
;
1600 CoreAcquireMemoryLock ();
1602 if (MapKey
== mMemoryMapKey
) {
1605 // Make sure the memory map is following all the construction rules
1606 // This is the last chance we will be able to display any messages on
1607 // the console devices.
1610 for (Link
= gMemoryMap
.ForwardLink
; Link
!= &gMemoryMap
; Link
= Link
->ForwardLink
) {
1611 Entry
= CR(Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1612 if (Entry
->Attribute
& EFI_MEMORY_RUNTIME
) {
1613 if (Entry
->Type
== EfiACPIReclaimMemory
|| Entry
->Type
== EfiACPIMemoryNVS
) {
1614 DEBUG((EFI_D_ERROR
, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1615 CoreReleaseMemoryLock ();
1616 return EFI_INVALID_PARAMETER
;
1618 if (Entry
->Start
& (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1619 DEBUG((EFI_D_ERROR
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1620 CoreReleaseMemoryLock ();
1621 return EFI_INVALID_PARAMETER
;
1623 if ((Entry
->End
+ 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
- 1)) {
1624 DEBUG((EFI_D_ERROR
, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1625 CoreReleaseMemoryLock ();
1626 return EFI_INVALID_PARAMETER
;
1632 // The map key they gave us matches what we expect. Fall through and
1633 // return success. In an ideal world we would clear out all of
1634 // EfiBootServicesCode and EfiBootServicesData. However this function
1635 // is not the last one called by ExitBootServices(), so we have to
1636 // preserve the memory contents.
1639 Status
= EFI_INVALID_PARAMETER
;
1642 CoreReleaseMemoryLock ();