X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxe%2FMem%2FPage.c;h=fa54e7f0e63f9b6e7b37e6a75bfe42d87feaf83b;hp=2bf457267a99b8e338a7cb5491811f5596eeb15c;hb=d20c09b28247c7e839072d3a21f542de95abb246;hpb=e94a9ff7271367e649ee4f9a86da1f1bea6d112e diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/Page.c index 2bf457267a..fa54e7f0e6 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -12,12 +12,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ -#include +#include "DxeMain.h" +#include "Imem.h" #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE) // -// Entry for tracking the memory regions for each memory type to help cooalese like memory types +// Entry for tracking the memory regions for each memory type to coalesce similar memory types // typedef struct { EFI_PHYSICAL_ADDRESS BaseAddress; @@ -34,40 +35,42 @@ typedef struct { // UINTN mMemoryMapKey = 0; -// -// mMapStack - space to use as temp storage to build new map descriptors -// mMapDepth - depth of new descriptor stack -// - #define MAX_MAP_DEPTH 6 + +/// +/// mMapDepth - depth of new descriptor stack +/// UINTN mMapDepth = 0; +/// +/// mMapStack - space to use as temp storage to build new map descriptors +/// MEMORY_MAP mMapStack[MAX_MAP_DEPTH]; UINTN mFreeMapStack = 0; -// -// This list maintain the free memory map list -// +/// +/// This list maintain the free memory map list +/// LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList); BOOLEAN mMemoryTypeInformationInitialized = FALSE; EFI_MEMORY_TYPE_STAISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = { - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode - { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode + { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType }; -EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = EFI_MAX_ADDRESS; +EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ADDRESS; EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = { { EfiReservedMemoryType, 0 }, @@ -87,100 +90,6 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = { { EfiMaxMemoryType, 0 } }; -// -// Internal prototypes -// -/** - Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable. - -**/ -VOID -PromoteMemoryResource ( - VOID - ); - -/** - Internal function. Adds a ranges to the memory map. - The range must not already exist in the map. - - @param Type The type of memory range to add - @param Start The starting address in the memory range Must be - paged aligned - @param End The last address in the range Must be the last - byte of a page - @param Attribute The attributes of the memory range to add - -**/ -VOID -CoreAddRange ( - IN EFI_MEMORY_TYPE Type, - IN EFI_PHYSICAL_ADDRESS Start, - IN EFI_PHYSICAL_ADDRESS End, - IN UINT64 Attribute - ); - -/** - Internal function. Moves any memory descriptors that are on the - temporary descriptor stack to heap. - -**/ -VOID -CoreFreeMemoryMapStack ( - VOID - ); - -/** - Internal function. Converts a memory range to the specified type. - The range must exist in the memory map. - - @param Start The first address of the range Must be page - aligned - @param NumberOfPages The number of pages to convert - @param NewType The new type for the memory range - - @retval EFI_INVALID_PARAMETER Invalid parameter - @retval EFI_NOT_FOUND Could not find a descriptor cover the specified - range or convertion not allowed. - @retval EFI_SUCCESS Successfully converts the memory range to the - specified type. - -**/ -EFI_STATUS -CoreConvertPages ( - IN UINT64 Start, - IN UINT64 NumberOfPages, - IN EFI_MEMORY_TYPE NewType - ); - -/** - Internal function. Removes a descriptor entry. - - @param Entry The entry to remove - -**/ -VOID -RemoveMemoryMapEntry ( - MEMORY_MAP *Entry - ); - -/** - Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList. - If the list is emtry, then allocate a new page to refuel the list. - Please Note this algorithm to allocate the memory map descriptor has a property - that the memory allocated for memory entries always grows, and will never really be freed - For example, if the current boot uses 2000 memory map entries at the maximum point, but - ends up with only 50 at the time the OS is booted, then the memory associated with the 1950 - memory map entries is still allocated from EfiBootServicesMemory. - - - @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList - -**/ -MEMORY_MAP * -AllocateMemoryMapEntry ( - VOID - ); - /** Enter critical section by gaining lock on gMemoryLock. @@ -209,234 +118,40 @@ CoreReleaseMemoryLock ( } -/** - Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable. - -**/ -VOID -PromoteMemoryResource ( - VOID - ) -{ - LIST_ENTRY *Link; - EFI_GCD_MAP_ENTRY *Entry; - - DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "Promote the memory resource\n")); - - CoreAcquireGcdMemoryLock (); - - Link = mGcdMemorySpaceMap.ForwardLink; - while (Link != &mGcdMemorySpaceMap) { - - Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE); - - if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved && - Entry->EndAddress < EFI_MAX_ADDRESS && - (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) == - (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) { - // - // Update the GCD map - // - Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory; - Entry->Capabilities |= EFI_MEMORY_TESTED; - Entry->ImageHandle = gDxeCoreImageHandle; - Entry->DeviceHandle = NULL; - - // - // Add to allocable system memory resource - // - - CoreAddRange ( - EfiConventionalMemory, - Entry->BaseAddress, - Entry->EndAddress, - Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME) - ); - CoreFreeMemoryMapStack (); - - } - - Link = Link->ForwardLink; - } - - CoreReleaseGcdMemoryLock (); - - return; -} /** - Called to initialize the memory map and add descriptors to - the current descriptor list. - The first descriptor that is added must be general usable - memory as the addition allocates heap. - - @param Type The type of memory to add - @param Start The starting address in the memory range Must be - page aligned - @param NumberOfPages The number of pages in the range - @param Attribute Attributes of the memory to add + Internal function. Removes a descriptor entry. - @return None. The range is added to the memory map + @param Entry The entry to remove **/ VOID -CoreAddMemoryDescriptor ( - IN EFI_MEMORY_TYPE Type, - IN EFI_PHYSICAL_ADDRESS Start, - IN UINT64 NumberOfPages, - IN UINT64 Attribute +RemoveMemoryMapEntry ( + IN OUT MEMORY_MAP *Entry ) { - EFI_PHYSICAL_ADDRESS End; - EFI_STATUS Status; - UINTN Index; - UINTN FreeIndex; - - if ((Start & EFI_PAGE_MASK) != 0) { - return; - } - - if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) { - return; - } - - CoreAcquireMemoryLock (); - End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1; - CoreAddRange (Type, Start, End, Attribute); - CoreFreeMemoryMapStack (); - CoreReleaseMemoryLock (); - - // - // Check to see if the statistics for the different memory types have already been established - // - if (mMemoryTypeInformationInitialized) { - return; - } - - // - // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array - // - for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { - // - // Make sure the memory type in the gMemoryTypeInformation[] array is valid - // - Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type); - if (Type < 0 || Type > EfiMaxMemoryType) { - continue; - } - - if (gMemoryTypeInformation[Index].NumberOfPages != 0) { - // - // Allocate pages for the current memory type from the top of available memory - // - Status = CoreAllocatePages ( - AllocateAnyPages, - Type, - gMemoryTypeInformation[Index].NumberOfPages, - &mMemoryTypeStatistics[Type].BaseAddress - ); - if (EFI_ERROR (Status)) { - // - // If an error occurs allocating the pages for the current memory type, then - // free all the pages allocates for the previous memory types and return. This - // operation with be retied when/if more memory is added to the system - // - for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) { - // - // Make sure the memory type in the gMemoryTypeInformation[] array is valid - // - Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type); - if (Type < 0 || Type > EfiMaxMemoryType) { - continue; - } - - if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) { - CoreFreePages ( - mMemoryTypeStatistics[Type].BaseAddress, - gMemoryTypeInformation[FreeIndex].NumberOfPages - ); - mMemoryTypeStatistics[Type].BaseAddress = 0; - mMemoryTypeStatistics[Type].MaximumAddress = EFI_MAX_ADDRESS; - } - } - return; - } - - // - // Compute the address at the top of the current statistics - // - mMemoryTypeStatistics[Type].MaximumAddress = - mMemoryTypeStatistics[Type].BaseAddress + - LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1; - - // - // If the current base address is the lowest address so far, then update the default - // maximum address - // - if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) { - mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1; - } - } - } + RemoveEntryList (&Entry->Link); + Entry->Link.ForwardLink = NULL; - // - // There was enough system memory for all the the memory types were allocated. So, - // those memory areas can be freed for future allocations, and all future memory - // allocations can occur within their respective bins - // - for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { + if (Entry->FromPages) { // - // Make sure the memory type in the gMemoryTypeInformation[] array is valid + // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList // - Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type); - if (Type < 0 || Type > EfiMaxMemoryType) { - continue; - } - - if (gMemoryTypeInformation[Index].NumberOfPages != 0) { - CoreFreePages ( - mMemoryTypeStatistics[Type].BaseAddress, - gMemoryTypeInformation[Index].NumberOfPages - ); - mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages; - gMemoryTypeInformation[Index].NumberOfPages = 0; - } - } - - // - // If the number of pages reserved for a memory type is 0, then all allocations for that type - // should be in the default range. - // - for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) { - for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { - if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) { - mMemoryTypeStatistics[Type].InformationIndex = Index; - } - } - mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0; - if (mMemoryTypeStatistics[Type].MaximumAddress == EFI_MAX_ADDRESS) { - mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress; - } + InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link); } - - mMemoryTypeInformationInitialized = TRUE; } - - /** Internal function. Adds a ranges to the memory map. The range must not already exist in the map. - @param Type The type of memory range to add - @param Start The starting address in the memory range Must be - paged aligned - @param End The last address in the range Must be the last - byte of a page - @param Attribute The attributes of the memory range to add - - @return None. The range is added to the memory map + @param Type The type of memory range to add + @param Start The starting address in the memory range Must be + paged aligned + @param End The last address in the range Must be the last + byte of a page + @param Attribute The attributes of the memory range to add **/ VOID @@ -454,7 +169,7 @@ CoreAddRange ( ASSERT (End > Start) ; ASSERT_LOCKED (&gMemoryLock); - + DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type)); // @@ -465,9 +180,9 @@ CoreAddRange ( // // UEFI 2.0 added an event group for notificaiton on memory map changes. // So we need to signal this Event Group every time the memory map changes. - // If we are in EFI 1.10 compatability mode no event groups will be + // If we are in EFI 1.10 compatability mode no event groups will be // found and nothing will happen we we call this function. These events - // will get signaled but since a lock is held around the call to this + // will get signaled but since a lock is held around the call to this // function the notificaiton events will only be called after this funciton // returns and the lock is released. // @@ -476,7 +191,7 @@ CoreAddRange ( // // Look for adjoining memory descriptor // - + // Two memory descriptors can only be merged if they have the same Type // and the same Attribute // @@ -495,19 +210,19 @@ CoreAddRange ( } if (Entry->End + 1 == Start) { - + Start = Entry->Start; RemoveMemoryMapEntry (Entry); } else if (Entry->Start == End + 1) { - + End = Entry->End; RemoveMemoryMapEntry (Entry); } } // - // Add descriptor + // Add descriptor // mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE; @@ -522,7 +237,55 @@ CoreAddRange ( mMapDepth += 1; ASSERT (mMapDepth < MAX_MAP_DEPTH); - return ; + return ; +} + +/** + Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList. + If the list is emtry, then allocate a new page to refuel the list. + Please Note this algorithm to allocate the memory map descriptor has a property + that the memory allocated for memory entries always grows, and will never really be freed + For example, if the current boot uses 2000 memory map entries at the maximum point, but + ends up with only 50 at the time the OS is booted, then the memory associated with the 1950 + memory map entries is still allocated from EfiBootServicesMemory. + + + @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList + +**/ +MEMORY_MAP * +AllocateMemoryMapEntry ( + VOID + ) +{ + MEMORY_MAP* FreeDescriptorEntries; + MEMORY_MAP* Entry; + UINTN Index; + + if (IsListEmpty (&mFreeMemoryMapEntryList)) { + // + // The list is empty, to allocate one page to refuel the list + // + FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION); + if(FreeDescriptorEntries != NULL) { + // + // Enque the free memmory map entries into the list + // + for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) { + FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE; + InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link); + } + } else { + return NULL; + } + } + // + // dequeue the first descriptor from the list + // + Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); + RemoveEntryList (&Entry->Link); + + return Entry; } @@ -556,10 +319,10 @@ CoreFreeMemoryMapStack ( while (mMapDepth != 0) { // - // Deque an memory map entry from mFreeMemoryMapEntryList + // Deque an memory map entry from mFreeMemoryMapEntryList // Entry = AllocateMemoryMapEntry (); - + ASSERT (Entry); // @@ -591,7 +354,7 @@ CoreFreeMemoryMapStack ( InsertTailList (Link2, &Entry->Link); } else { - // + // // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list, // so here no need to move it to memory. // @@ -602,92 +365,234 @@ CoreFreeMemoryMapStack ( mFreeMapStack -= 1; } - /** - Internal function. Removes a descriptor entry. - - @param Entry The entry to remove + Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable. **/ VOID -RemoveMemoryMapEntry ( - IN OUT MEMORY_MAP *Entry +PromoteMemoryResource ( + VOID ) { - RemoveEntryList (&Entry->Link); - Entry->Link.ForwardLink = NULL; + LIST_ENTRY *Link; + EFI_GCD_MAP_ENTRY *Entry; - if (Entry->FromPages) { - // - // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList - // - InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link); + DEBUG ((DEBUG_PAGE, "Promote the memory resource\n")); + + CoreAcquireGcdMemoryLock (); + + Link = mGcdMemorySpaceMap.ForwardLink; + while (Link != &mGcdMemorySpaceMap) { + + Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE); + + if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved && + Entry->EndAddress < MAX_ADDRESS && + (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) == + (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) { + // + // Update the GCD map + // + Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory; + Entry->Capabilities |= EFI_MEMORY_TESTED; + Entry->ImageHandle = gDxeCoreImageHandle; + Entry->DeviceHandle = NULL; + + // + // Add to allocable system memory resource + // + + CoreAddRange ( + EfiConventionalMemory, + Entry->BaseAddress, + Entry->EndAddress, + Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME) + ); + CoreFreeMemoryMapStack (); + + } + + Link = Link->ForwardLink; } + + CoreReleaseGcdMemoryLock (); + + return; } /** - Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList. - If the list is emtry, then allocate a new page to refuel the list. - Please Note this algorithm to allocate the memory map descriptor has a property - that the memory allocated for memory entries always grows, and will never really be freed - For example, if the current boot uses 2000 memory map entries at the maximum point, but - ends up with only 50 at the time the OS is booted, then the memory associated with the 1950 - memory map entries is still allocated from EfiBootServicesMemory. + Called to initialize the memory map and add descriptors to + the current descriptor list. + The first descriptor that is added must be general usable + memory as the addition allocates heap. + @param Type The type of memory to add + @param Start The starting address in the memory range Must be + page aligned + @param NumberOfPages The number of pages in the range + @param Attribute Attributes of the memory to add - @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList + @return None. The range is added to the memory map **/ -MEMORY_MAP * -AllocateMemoryMapEntry ( - VOID +VOID +CoreAddMemoryDescriptor ( + IN EFI_MEMORY_TYPE Type, + IN EFI_PHYSICAL_ADDRESS Start, + IN UINT64 NumberOfPages, + IN UINT64 Attribute ) { - MEMORY_MAP* FreeDescriptorEntries; - MEMORY_MAP* Entry; - UINTN Index; - - if (IsListEmpty (&mFreeMemoryMapEntryList)) { - // - // The list is empty, to allocate one page to refuel the list + EFI_PHYSICAL_ADDRESS End; + EFI_STATUS Status; + UINTN Index; + UINTN FreeIndex; + + if ((Start & EFI_PAGE_MASK) != 0) { + return; + } + + if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) { + return; + } + + CoreAcquireMemoryLock (); + End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1; + CoreAddRange (Type, Start, End, Attribute); + CoreFreeMemoryMapStack (); + CoreReleaseMemoryLock (); + + // + // Check to see if the statistics for the different memory types have already been established + // + if (mMemoryTypeInformationInitialized) { + return; + } + + // + // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array + // + for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { // - FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION); - if(FreeDescriptorEntries != NULL) { + // Make sure the memory type in the gMemoryTypeInformation[] array is valid + // + Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type); + if (Type < 0 || Type > EfiMaxMemoryType) { + continue; + } + + if (gMemoryTypeInformation[Index].NumberOfPages != 0) { // - // Enque the free memmory map entries into the list + // Allocate pages for the current memory type from the top of available memory // - for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) { - FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE; - InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link); - } - } else { - return NULL; + Status = CoreAllocatePages ( + AllocateAnyPages, + Type, + gMemoryTypeInformation[Index].NumberOfPages, + &mMemoryTypeStatistics[Type].BaseAddress + ); + if (EFI_ERROR (Status)) { + // + // If an error occurs allocating the pages for the current memory type, then + // free all the pages allocates for the previous memory types and return. This + // operation with be retied when/if more memory is added to the system + // + for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) { + // + // Make sure the memory type in the gMemoryTypeInformation[] array is valid + // + Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type); + if (Type < 0 || Type > EfiMaxMemoryType) { + continue; + } + + if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) { + CoreFreePages ( + mMemoryTypeStatistics[Type].BaseAddress, + gMemoryTypeInformation[FreeIndex].NumberOfPages + ); + mMemoryTypeStatistics[Type].BaseAddress = 0; + mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS; + } + } + return; + } + + // + // Compute the address at the top of the current statistics + // + mMemoryTypeStatistics[Type].MaximumAddress = + mMemoryTypeStatistics[Type].BaseAddress + + LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1; + + // + // If the current base address is the lowest address so far, then update the default + // maximum address + // + if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) { + mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1; + } } } + // - // dequeue the first descriptor from the list + // There was enough system memory for all the the memory types were allocated. So, + // those memory areas can be freed for future allocations, and all future memory + // allocations can occur within their respective bins // - Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); - RemoveEntryList (&Entry->Link); - - return Entry; -} + for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { + // + // Make sure the memory type in the gMemoryTypeInformation[] array is valid + // + Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type); + if (Type < 0 || Type > EfiMaxMemoryType) { + continue; + } + + if (gMemoryTypeInformation[Index].NumberOfPages != 0) { + CoreFreePages ( + mMemoryTypeStatistics[Type].BaseAddress, + gMemoryTypeInformation[Index].NumberOfPages + ); + mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages; + gMemoryTypeInformation[Index].NumberOfPages = 0; + } + } + + // + // If the number of pages reserved for a memory type is 0, then all allocations for that type + // should be in the default range. + // + for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) { + for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { + if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) { + mMemoryTypeStatistics[Type].InformationIndex = Index; + } + } + mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0; + if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) { + mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress; + } + } + + mMemoryTypeInformationInitialized = TRUE; +} /** Internal function. Converts a memory range to the specified type. The range must exist in the memory map. - @param Start The first address of the range Must be page - aligned - @param NumberOfPages The number of pages to convert - @param NewType The new type for the memory range + @param Start The first address of the range Must be page + aligned + @param NumberOfPages The number of pages to convert + @param NewType The new type for the memory range - @retval EFI_INVALID_PARAMETER Invalid parameter - @retval EFI_NOT_FOUND Could not find a descriptor cover the specified - range or convertion not allowed. - @retval EFI_SUCCESS Successfully converts the memory range to the + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_NOT_FOUND Could not find a descriptor cover the specified + range or convertion not allowed. + @retval EFI_SUCCESS Successfully converts the memory range to the specified type. **/ @@ -756,15 +661,15 @@ CoreConvertPages ( // Debug code - verify conversion is allowed // if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) { - DEBUG ((DEBUG_ERROR , "ConvertPages: Incompatible memory types\n")); + DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n")); return EFI_NOT_FOUND; - } + } // // Update counters for the number of pages allocated to each memory type // if (Entry->Type >= 0 && Entry->Type < EfiMaxMemoryType) { - if (Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && + if (Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) { if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) { mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0; @@ -777,7 +682,7 @@ CoreConvertPages ( if (NewType >= 0 && NewType < EfiMaxMemoryType) { if (Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) { mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages; - if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > + if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) { gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages; } @@ -788,14 +693,14 @@ CoreConvertPages ( // Pull range out of descriptor // if (Entry->Start == Start) { - + // // Clip start // Entry->Start = RangeEnd + 1; } else if (Entry->End == RangeEnd) { - + // // Clip end // @@ -806,7 +711,7 @@ CoreConvertPages ( // // Pull it out of the center, clip current // - + // // Add a new one // @@ -832,7 +737,7 @@ CoreConvertPages ( } // - // The new range inherits the same Attribute as the Entry + // The new range inherits the same Attribute as the Entry //it is being cut out of // Attribute = Entry->Attribute; @@ -844,7 +749,7 @@ CoreConvertPages ( RemoveMemoryMapEntry (Entry); Entry = NULL; } - + // // Add our new range in // @@ -874,11 +779,11 @@ CoreConvertPages ( Internal function. Finds a consecutive free page range below the requested address. - @param MaxAddress The address that the range must be below - @param NumberOfPages Number of pages needed - @param NewType The type of memory the range is going to be - turned into - @param Alignment Bits to align with + @param MaxAddress The address that the range must be below + @param NumberOfPages Number of pages needed + @param NewType The type of memory the range is going to be + turned into + @param Alignment Bits to align with @return The base address of the range, or 0 if the range was not found @@ -904,21 +809,21 @@ CoreFindFreePagesI ( } if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) { - + // // If MaxAddress is not aligned to the end of a page // - + // // Change MaxAddress to be 1 page lower // MaxAddress -= (EFI_PAGE_MASK + 1); - + // // Set MaxAddress to a page boundary // MaxAddress &= ~EFI_PAGE_MASK; - + // // Set MaxAddress to end of the page // @@ -930,7 +835,7 @@ CoreFindFreePagesI ( for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) { Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); - + // // If it's not a free entry, don't bother with it // @@ -958,7 +863,7 @@ CoreFindFreePagesI ( DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1; // - // Compute the number of bytes we can used from this + // Compute the number of bytes we can used from this // descriptor, and see it's enough to satisfy the request // DescNumberOfBytes = DescEnd - DescStart + 1; @@ -972,7 +877,7 @@ CoreFindFreePagesI ( Target = DescEnd; } } - } + } // // If this is a grow down, adjust target to be the allocation base @@ -994,11 +899,11 @@ CoreFindFreePagesI ( Internal function. Finds a consecutive free page range below the requested address - @param MaxAddress The address that the range must be below - @param NoPages Number of pages needed - @param NewType The type of memory the range is going to be - turned into - @param Alignment Bits to align with + @param MaxAddress The address that the range must be below + @param NoPages Number of pages needed + @param NewType The type of memory the range is going to be + turned into + @param Alignment Bits to align with @return The base address of the range, or 0 if the range was not found. @@ -1049,18 +954,18 @@ FindFreePages ( /** Allocates pages from the memory map. - @param Type The type of allocation to perform - @param MemoryType The type of memory to turn the allocated pages - into - @param NumberOfPages The number of pages to allocate - @param Memory A pointer to receive the base allocated memory - address + @param Type The type of allocation to perform + @param MemoryType The type of memory to turn the allocated pages + into + @param NumberOfPages The number of pages to allocate + @param Memory A pointer to receive the base allocated memory + address @return Status. On success, Memory is filled in with the base address allocated - @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in - spec. - @retval EFI_NOT_FOUND Could not allocate pages match the requirement. - @retval EFI_OUT_OF_RESOURCES No enough pages to allocate. + @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in + spec. + @retval EFI_NOT_FOUND Could not allocate pages match the requirement. + @retval EFI_OUT_OF_RESOURCES No enough pages to allocate. @retval EFI_SUCCESS Pages successfully allocated. **/ @@ -1107,21 +1012,21 @@ CoreAllocatePages ( NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1); // - // If this is for below a particular address, then + // If this is for below a particular address, then // Start = *Memory; - + // // The max address is the max natively addressable address for the processor // - MaxAddress = EFI_MAX_ADDRESS; - + MaxAddress = MAX_ADDRESS; + if (Type == AllocateMaxAddress) { MaxAddress = Start; } CoreAcquireMemoryLock (); - + // // If not a specific address, then find an address to allocate // @@ -1152,15 +1057,15 @@ Done: /** Frees previous allocated pages. - @param Memory Base address of memory being freed - @param NumberOfPages The number of pages to free + @param Memory Base address of memory being freed + @param NumberOfPages The number of pages to free - @retval EFI_NOT_FOUND Could not find the entry that covers the range - @retval EFI_INVALID_PARAMETER Address not aligned + @retval EFI_NOT_FOUND Could not find the entry that covers the range + @retval EFI_INVALID_PARAMETER Address not aligned @return EFI_SUCCESS -Pages successfully freed. **/ -EFI_STATUS +EFI_STATUS EFIAPI CoreFreePages ( IN EFI_PHYSICAL_ADDRESS Memory, @@ -1222,10 +1127,10 @@ CoreFreePages ( // // Destroy the contents // - if (Memory < EFI_MAX_ADDRESS) { + if (Memory < MAX_ADDRESS) { DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT); } - + return Status; } @@ -1234,29 +1139,29 @@ CoreFreePages ( This function returns a copy of the current memory map. The map is an array of memory descriptors, each of which describes a contiguous block of memory. - @param MemoryMapSize A pointer to the size, in bytes, of the - MemoryMap buffer. On input, this is the size of - the buffer allocated by the caller. On output, - it is the size of the buffer returned by the - firmware if the buffer was large enough, or the - size of the buffer needed to contain the map if - the buffer was too small. - @param MemoryMap A pointer to the buffer in which firmware places - the current memory map. - @param MapKey A pointer to the location in which firmware - returns the key for the current memory map. - @param DescriptorSize A pointer to the location in which firmware - returns the size, in bytes, of an individual - EFI_MEMORY_DESCRIPTOR. - @param DescriptorVersion A pointer to the location in which firmware - returns the version number associated with the - EFI_MEMORY_DESCRIPTOR. - - @retval EFI_SUCCESS The memory map was returned in the MemoryMap - buffer. - @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current - buffer size needed to hold the memory map is - returned in MemoryMapSize. + @param MemoryMapSize A pointer to the size, in bytes, of the + MemoryMap buffer. On input, this is the size of + the buffer allocated by the caller. On output, + it is the size of the buffer returned by the + firmware if the buffer was large enough, or the + size of the buffer needed to contain the map if + the buffer was too small. + @param MemoryMap A pointer to the buffer in which firmware places + the current memory map. + @param MapKey A pointer to the location in which firmware + returns the key for the current memory map. + @param DescriptorSize A pointer to the location in which firmware + returns the size, in bytes, of an individual + EFI_MEMORY_DESCRIPTOR. + @param DescriptorVersion A pointer to the location in which firmware + returns the version number associated with the + EFI_MEMORY_DESCRIPTOR. + + @retval EFI_SUCCESS The memory map was returned in the MemoryMap + buffer. + @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current + buffer size needed to hold the memory map is + returned in MemoryMapSize. @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. **/ @@ -1271,12 +1176,12 @@ CoreGetMemoryMap ( ) { EFI_STATUS Status; - UINTN Size; - UINTN BufferSize; + UINTN Size; + UINTN BufferSize; UINTN NumberOfRuntimeEntries; LIST_ENTRY *Link; - MEMORY_MAP *Entry; - EFI_GCD_MAP_ENTRY *GcdMapEntry; + MEMORY_MAP *Entry; + EFI_GCD_MAP_ENTRY *GcdMapEntry; EFI_MEMORY_TYPE Type; // @@ -1285,9 +1190,9 @@ CoreGetMemoryMap ( if (MemoryMapSize == NULL) { return EFI_INVALID_PARAMETER; } - + CoreAcquireGcdMemoryLock (); - + // // Count the number of Reserved and MMIO entries that are marked for runtime use // @@ -1314,7 +1219,7 @@ CoreGetMemoryMap ( if (DescriptorSize != NULL) { *DescriptorSize = Size; } - + if (DescriptorVersion != NULL) { *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION; } @@ -1356,8 +1261,8 @@ CoreGetMemoryMap ( MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT); // // If the memory type is EfiConventionalMemory, then determine if the range is part of a - // memory type bin and needs to be converted to the same memory type as the rest of the - // memory type bin in order to minimize EFI Memory Map changes across reboots. This + // memory type bin and needs to be converted to the same memory type as the rest of the + // memory type bin in order to minimize EFI Memory Map changes across reboots. This // improves the chances for a successful S4 resume in the presence of minor page allocation // differences across reboots. // @@ -1375,8 +1280,8 @@ CoreGetMemoryMap ( if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) { MemoryMap->Attribute |= EFI_MEMORY_RUNTIME; } - - MemoryMap = NextMemoryDescriptor (MemoryMap, Size); + + MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size); } for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) { @@ -1384,7 +1289,10 @@ CoreGetMemoryMap ( if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) || (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) { if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) { - + // + // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries + // that are marked for runtime use + // MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress; MemoryMap->VirtualStart = 0; MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT); @@ -1400,28 +1308,28 @@ CoreGetMemoryMap ( } } - MemoryMap = NextMemoryDescriptor (MemoryMap, Size); + MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size); } } } - + Status = EFI_SUCCESS; Done: CoreReleaseMemoryLock (); - + CoreReleaseGcdMemoryLock (); - - // - // Update the map key finally - // + + // + // Update the map key finally + // if (MapKey != NULL) { *MapKey = mMemoryMapKey; } - + *MemoryMapSize = BufferSize; - + return Status; } @@ -1430,9 +1338,9 @@ Done: Internal function. Used by the pool functions to allocate pages to back pool allocation requests. - @param PoolType The type of memory for the new pool pages - @param NumberOfPages No of pages to allocate - @param Alignment Bits to align. + @param PoolType The type of memory for the new pool pages + @param NumberOfPages No of pages to allocate + @param Alignment Bits to align. @return The allocated memory, or NULL @@ -1449,7 +1357,7 @@ CoreAllocatePoolPages ( // // Find the pages to convert // - Start = FindFreePages (EFI_MAX_ADDRESS, NumberOfPages, PoolType, Alignment); + Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment); // // Convert it to boot services data @@ -1467,7 +1375,7 @@ CoreAllocatePoolPages ( /** Internal function. Frees pool pages allocated via AllocatePoolPages () - @param Memory The base address to free + @param Memory The base address to free @param NumberOfPages The number of pages to free **/ @@ -1486,10 +1394,10 @@ CoreFreePoolPages ( Make sure the memory map is following all the construction rules, it is the last time to check memory map error before exit boot services. - @param MapKey Memory map key + @param MapKey Memory map key - @retval EFI_INVALID_PARAMETER Memory map not consistent with construction - rules. + @retval EFI_INVALID_PARAMETER Memory map not consistent with construction + rules. @retval EFI_SUCCESS Valid memory map. **/ @@ -1516,21 +1424,21 @@ CoreTerminateMemoryMap ( for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) { Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); - if (Entry->Attribute & EFI_MEMORY_RUNTIME) { + if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) { if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) { - DEBUG((DEBUG_ERROR, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n")); - CoreReleaseMemoryLock (); - return EFI_INVALID_PARAMETER; + DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n")); + Status = EFI_INVALID_PARAMETER; + goto Done; } - if (Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) { - DEBUG((DEBUG_ERROR, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n")); - CoreReleaseMemoryLock (); - return EFI_INVALID_PARAMETER; + if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) { + DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n")); + Status = EFI_INVALID_PARAMETER; + goto Done; } - if ((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) { - DEBUG((DEBUG_ERROR, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n")); - CoreReleaseMemoryLock (); - return EFI_INVALID_PARAMETER; + if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) { + DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n")); + Status = EFI_INVALID_PARAMETER; + goto Done; } } } @@ -1546,6 +1454,7 @@ CoreTerminateMemoryMap ( Status = EFI_INVALID_PARAMETER; } +Done: CoreReleaseMemoryLock (); return Status;