X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxe%2FMem%2FPage.c;h=5995f97c61b7e6874a5e52f1f6cae01a80e9f25e;hp=daf00f237d0f8a3b77431dad27d372d0eb2ded8e;hb=b295dfd486e34c28fa5166a783aa992d004fae3c;hpb=504214c4870e9183418014634268ce630eb5332a diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/Page.c index daf00f237d..5995f97c61 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -1,24 +1,24 @@ /** @file - UEFI Memory page management functions. -Copyright (c) 2007 - 2008, Intel Corporation -All rights reserved. This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +Copyright (c) 2007 - 2010, Intel Corporation.
+All rights reserved. This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +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; @@ -35,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 -// -LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList); -BOOLEAN mMemoryTypeInformationInitialized = FALSE; +/// +/// 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,133 +89,310 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = { { EfiPalCode, 0 }, { EfiMaxMemoryType, 0 } }; - // -// Internal prototypes +// Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated +// and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a +// address assigned by DXE core. // -STATIC -VOID -PromoteMemoryResource ( - VOID -); +GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady = FALSE; -STATIC -VOID -CoreAddRange ( - IN EFI_MEMORY_TYPE Type, - IN EFI_PHYSICAL_ADDRESS Start, - IN EFI_PHYSICAL_ADDRESS End, - IN UINT64 Attribute - ); +/** + Enter critical section by gaining lock on gMemoryLock. -STATIC +**/ VOID -CoreFreeMemoryMapStack ( +CoreAcquireMemoryLock ( VOID - ); + ) +{ + CoreAcquireLock (&gMemoryLock); +} -STATIC -EFI_STATUS -CoreConvertPages ( - IN UINT64 Start, - IN UINT64 NumberOfPages, - IN EFI_MEMORY_TYPE NewType - ); -STATIC -VOID -RemoveMemoryMapEntry ( - MEMORY_MAP *Entry - ); - -STATIC -MEMORY_MAP * -AllocateMemoryMapEntry ( - VOID - ); - + +/** + Exit critical section by releasing lock on gMemoryLock. + +**/ VOID -CoreAcquireMemoryLock ( +CoreReleaseMemoryLock ( VOID ) -/*++ - -Routine Description: +{ + CoreReleaseLock (&gMemoryLock); +} - Enter critical section by gaining lock on gMemoryLock -Arguments: - None -Returns: +/** + Internal function. Removes a descriptor entry. - None + @param Entry The entry to remove ---*/ +**/ +VOID +RemoveMemoryMapEntry ( + IN OUT MEMORY_MAP *Entry + ) { - CoreAcquireLock (&gMemoryLock); + RemoveEntryList (&Entry->Link); + Entry->Link.ForwardLink = NULL; + + if (Entry->FromPages) { + // + // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList + // + InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link); + } } +/** + 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 -CoreReleaseMemoryLock ( - VOID +CoreAddRange ( + IN EFI_MEMORY_TYPE Type, + IN EFI_PHYSICAL_ADDRESS Start, + IN EFI_PHYSICAL_ADDRESS End, + IN UINT64 Attribute ) -/*++ +{ + LIST_ENTRY *Link; + MEMORY_MAP *Entry; + + ASSERT ((Start & EFI_PAGE_MASK) == 0); + ASSERT (End > Start) ; + + ASSERT_LOCKED (&gMemoryLock); + + DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type)); + + // + // Memory map being altered so updated key + // + mMemoryMapKey += 1; + + // + // 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 + // 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 + // function the notificaiton events will only be called after this funciton + // returns and the lock is released. + // + CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid); + + // + // Look for adjoining memory descriptor + // + + // Two memory descriptors can only be merged if they have the same Type + // and the same Attribute + // + + Link = gMemoryMap.ForwardLink; + while (Link != &gMemoryMap) { + Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); + Link = Link->ForwardLink; + + if (Entry->Type != Type) { + continue; + } + + if (Entry->Attribute != Attribute) { + continue; + } + + if (Entry->End + 1 == Start) { + + Start = Entry->Start; + RemoveMemoryMapEntry (Entry); -Routine Description: + } else if (Entry->Start == End + 1) { - Exit critical section by releasing lock on gMemoryLock + End = Entry->End; + RemoveMemoryMapEntry (Entry); + } + } -Arguments: + // + // Add descriptor + // - None + mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE; + mMapStack[mMapDepth].FromPages = FALSE; + mMapStack[mMapDepth].Type = Type; + mMapStack[mMapDepth].Start = Start; + mMapStack[mMapDepth].End = End; + mMapStack[mMapDepth].VirtualStart = 0; + mMapStack[mMapDepth].Attribute = Attribute; + InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link); + + mMapDepth += 1; + ASSERT (mMapDepth < MAX_MAP_DEPTH); + + return ; +} -Returns: +/** + 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. - None ---*/ + @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList + +**/ +MEMORY_MAP * +AllocateMemoryMapEntry ( + VOID + ) { - CoreReleaseLock (&gMemoryLock); + 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; } -STATIC + +/** + Internal function. Moves any memory descriptors that are on the + temporary descriptor stack to heap. + +**/ VOID -PromoteMemoryResource ( +CoreFreeMemoryMapStack ( VOID ) -/*++ +{ + MEMORY_MAP *Entry; + MEMORY_MAP *Entry2; + LIST_ENTRY *Link2; -Routine Description: + ASSERT_LOCKED (&gMemoryLock); - Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable. + // + // If already freeing the map stack, then return + // + if (mFreeMapStack != 0) { + return ; + } + + // + // Move the temporary memory descriptor stack into pool + // + mFreeMapStack += 1; + + while (mMapDepth != 0) { + // + // Deque an memory map entry from mFreeMemoryMapEntryList + // + Entry = AllocateMemoryMapEntry (); -Arguments: + ASSERT (Entry); - None + // + // Update to proper entry + // + mMapDepth -= 1; -Returns: + if (mMapStack[mMapDepth].Link.ForwardLink != NULL) { - None + // + // Move this entry to general memory + // + RemoveEntryList (&mMapStack[mMapDepth].Link); + mMapStack[mMapDepth].Link.ForwardLink = NULL; + + CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP)); + Entry->FromPages = TRUE; ---*/ + // + // Find insertion location + // + for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) { + Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); + if (Entry2->FromPages && Entry2->Start > Entry->Start) { + break; + } + } + + 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. + // + InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link); + } + } + + mFreeMapStack -= 1; +} + +/** + 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 ((EFI_D_ERROR | EFI_D_PAGE, "Promote the memory resource\n")); - + 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 < EFI_MAX_ADDRESS && + Entry->EndAddress < MAX_ADDRESS && (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) == (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) { // @@ -226,65 +405,118 @@ Returns: // // Add to allocable system memory resource - // + // CoreAddRange ( - EfiConventionalMemory, - Entry->BaseAddress, - Entry->EndAddress, + 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; } +/** + This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD + PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the + size of boot time and runtime code. +**/ VOID -CoreAddMemoryDescriptor ( - IN EFI_MEMORY_TYPE Type, - IN EFI_PHYSICAL_ADDRESS Start, - IN UINT64 NumberOfPages, - IN UINT64 Attribute +CoreLoadingFixedAddressHook ( + VOID ) -/*++ - -Routine Description: - +{ + UINT32 RuntimeCodePageNumber; + UINT32 BootTimeCodePageNumber; + EFI_PHYSICAL_ADDRESS RuntimeCodeBase; + EFI_PHYSICAL_ADDRESS BootTimeCodeBase; + EFI_STATUS Status; + + // + // Make sure these 2 areas are not initialzied. + // + if (!gLoadFixedAddressCodeMemoryReady) { + RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber); + BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber); + RuntimeCodeBase = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber)); + BootTimeCodeBase = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber)); + // + // Try to allocate runtime memory. + // + Status = CoreAllocatePages ( + AllocateAddress, + EfiRuntimeServicesCode, + RuntimeCodePageNumber, + &RuntimeCodeBase + ); + if (EFI_ERROR(Status)) { + // + // Runtime memory allocation failed + // + return; + } + // + // Try to allocate boot memory. + // + Status = CoreAllocatePages ( + AllocateAddress, + EfiBootServicesCode, + BootTimeCodePageNumber, + &BootTimeCodeBase + ); + if (EFI_ERROR(Status)) { + // + // boot memory allocation failed. Free Runtime code range and will try the allocation again when + // new memory range is installed. + // + CoreFreePages ( + RuntimeCodeBase, + RuntimeCodePageNumber + ); + return; + } + gLoadFixedAddressCodeMemoryReady = TRUE; + } + 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. -Arguments: - - Type - The type of memory to add - - Start - The starting address in the memory range - Must be page aligned - - NumberOfPages - The number of pages in the range - - Attribute - Attributes of the memory to add - -Returns: + @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 - None. The range is added to the memory map + @return None. The range is added to the memory map ---*/ +**/ +VOID +CoreAddMemoryDescriptor ( + IN EFI_MEMORY_TYPE Type, + IN EFI_PHYSICAL_ADDRESS Start, + IN UINT64 NumberOfPages, + IN UINT64 Attribute + ) { EFI_PHYSICAL_ADDRESS End; EFI_STATUS Status; UINTN Index; UINTN FreeIndex; - + if ((Start & EFI_PAGE_MASK) != 0) { return; } @@ -292,13 +524,19 @@ Returns: if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) { return; } - CoreAcquireMemoryLock (); End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1; CoreAddRange (Type, Start, End, Attribute); CoreFreeMemoryMapStack (); CoreReleaseMemoryLock (); + // + // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type + // + if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) { + CoreLoadingFixedAddressHook(); + } + // // Check to see if the statistics for the different memory types have already been established // @@ -306,6 +544,7 @@ Returns: return; } + // // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array // @@ -317,7 +556,6 @@ Returns: if (Type < 0 || Type > EfiMaxMemoryType) { continue; } - if (gMemoryTypeInformation[Index].NumberOfPages != 0) { // // Allocate pages for the current memory type from the top of available memory @@ -330,7 +568,7 @@ Returns: ); if (EFI_ERROR (Status)) { // - // If an error occurs allocating the pages for the current memory type, then + // 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 // @@ -345,11 +583,11 @@ Returns: if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) { CoreFreePages ( - mMemoryTypeStatistics[Type].BaseAddress, + mMemoryTypeStatistics[Type].BaseAddress, gMemoryTypeInformation[FreeIndex].NumberOfPages ); mMemoryTypeStatistics[Type].BaseAddress = 0; - mMemoryTypeStatistics[Type].MaximumAddress = EFI_MAX_ADDRESS; + mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS; } } return; @@ -358,12 +596,12 @@ Returns: // // Compute the address at the top of the current statistics // - mMemoryTypeStatistics[Type].MaximumAddress = - mMemoryTypeStatistics[Type].BaseAddress + + 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 + // If the current base address is the lowest address so far, then update the default // maximum address // if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) { @@ -385,10 +623,9 @@ Returns: if (Type < 0 || Type > EfiMaxMemoryType) { continue; } - if (gMemoryTypeInformation[Index].NumberOfPages != 0) { CoreFreePages ( - mMemoryTypeStatistics[Type].BaseAddress, + mMemoryTypeStatistics[Type].BaseAddress, gMemoryTypeInformation[Index].NumberOfPages ); mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages; @@ -407,336 +644,37 @@ Returns: } } mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0; - if (mMemoryTypeStatistics[Type].MaximumAddress == EFI_MAX_ADDRESS) { - mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress; - } - } - - mMemoryTypeInformationInitialized = TRUE; -} - - -STATIC -VOID -CoreAddRange ( - IN EFI_MEMORY_TYPE Type, - IN EFI_PHYSICAL_ADDRESS Start, - IN EFI_PHYSICAL_ADDRESS End, - IN UINT64 Attribute - ) -/*++ - -Routine Description: - - Internal function. Adds a ranges to the memory map. - The range must not already exist in the map. - -Arguments: - - Type - The type of memory range to add - - Start - The starting address in the memory range - Must be paged aligned - - End - The last address in the range - Must be the last byte of a page - - Attribute - The attributes of the memory range to add - -Returns: - - None. The range is added to the memory map - ---*/ -{ - LIST_ENTRY *Link; - MEMORY_MAP *Entry; - - ASSERT ((Start & EFI_PAGE_MASK) == 0); - ASSERT (End > Start) ; - - ASSERT_LOCKED (&gMemoryLock); - - DEBUG ((EFI_D_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type)); - - // - // Memory map being altered so updated key - // - mMemoryMapKey += 1; - - // - // 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 - // 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 - // function the notificaiton events will only be called after this funciton - // returns and the lock is released. - // - CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid); - - // - // Look for adjoining memory descriptor - // - - // Two memory descriptors can only be merged if they have the same Type - // and the same Attribute - // - - Link = gMemoryMap.ForwardLink; - while (Link != &gMemoryMap) { - Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); - Link = Link->ForwardLink; - - if (Entry->Type != Type) { - continue; - } - - if (Entry->Attribute != Attribute) { - continue; - } - - if (Entry->End + 1 == Start) { - - Start = Entry->Start; - RemoveMemoryMapEntry (Entry); - - } else if (Entry->Start == End + 1) { - - End = Entry->End; - RemoveMemoryMapEntry (Entry); - } - } - - // - // Add descriptor - // - - mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE; - mMapStack[mMapDepth].FromPages = FALSE; - mMapStack[mMapDepth].Type = Type; - mMapStack[mMapDepth].Start = Start; - mMapStack[mMapDepth].End = End; - mMapStack[mMapDepth].VirtualStart = 0; - mMapStack[mMapDepth].Attribute = Attribute; - InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link); - - mMapDepth += 1; - ASSERT (mMapDepth < MAX_MAP_DEPTH); - - return ; -} - -STATIC -VOID -CoreFreeMemoryMapStack ( - VOID - ) -/*++ - -Routine Description: - - Internal function. Moves any memory descriptors that are on the - temporary descriptor stack to heap. - -Arguments: - - None - -Returns: - - None - ---*/ -{ - MEMORY_MAP *Entry; - MEMORY_MAP *Entry2; - LIST_ENTRY *Link2; - - ASSERT_LOCKED (&gMemoryLock); - - // - // If already freeing the map stack, then return - // - if (mFreeMapStack) { - return ; - } - - // - // Move the temporary memory descriptor stack into pool - // - mFreeMapStack += 1; - - while (mMapDepth) { - // - // Deque an memory map entry from mFreeMemoryMapEntryList - // - Entry = AllocateMemoryMapEntry (); - - ASSERT (Entry); - - // - // Update to proper entry - // - mMapDepth -= 1; - - if (mMapStack[mMapDepth].Link.ForwardLink != NULL) { - - // - // Move this entry to general memory - // - RemoveEntryList (&mMapStack[mMapDepth].Link); - mMapStack[mMapDepth].Link.ForwardLink = NULL; - - CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP)); - Entry->FromPages = TRUE; - - // - // Find insertion location - // - for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) { - Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); - if (Entry2->FromPages && Entry2->Start > Entry->Start) { - break; - } - } - - 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. - // - InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link); + if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) { + mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress; } } - mFreeMapStack -= 1; -} - -STATIC -VOID -RemoveMemoryMapEntry ( - MEMORY_MAP *Entry - ) -/*++ - -Routine Description: - - Internal function. Removes a descriptor entry. - -Arguments: - - Entry - The entry to remove - -Returns: - - None - ---*/ -{ - RemoveEntryList (&Entry->Link); - Entry->Link.ForwardLink = NULL; - - if (Entry->FromPages) { - // - // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList - // - InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link); - } + mMemoryTypeInformationInitialized = TRUE; } -STATIC -MEMORY_MAP * -AllocateMemoryMapEntry ( - VOID - ) -/*++ - -Routine Description: - - 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. - -Arguments: - - NONE -Returns: +/** + Internal function. Converts a memory range to the specified type. + The range must exist in the memory map. - The Memory map descriptor dequed from the mFreeMemoryMapEntryList + @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 ---*/ -{ - 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; -} + @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. -STATIC +**/ EFI_STATUS CoreConvertPages ( IN UINT64 Start, IN UINT64 NumberOfPages, IN EFI_MEMORY_TYPE NewType ) -/*++ - -Routine Description: - - Internal function. Converts a memory range to the specified type. - The range must exist in the memory map. - -Arguments: - - Start - The first address of the range - Must be page aligned - - NumberOfPages - The number of pages to convert - - NewType - The new type for the memory range - -Returns: - - EFI_INVALID_PARAMETER - Invalid parameter - - EFI_NOT_FOUND - Could not find a descriptor cover the specified range - or convertion not allowed. - - EFI_SUCCESS - Successfully converts the memory range to the specified type. - ---*/ { UINT64 NumberOfBytes; @@ -755,7 +693,7 @@ Returns: ASSERT (End > Start) ; ASSERT_LOCKED (&gMemoryLock); - if (NumberOfPages == 0 || (Start & EFI_PAGE_MASK ) || (Start > (Start + NumberOfBytes))) { + if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start > (Start + NumberOfBytes))) { return EFI_INVALID_PARAMETER; } @@ -777,7 +715,7 @@ Returns: } if (Link == &gMemoryMap) { - DEBUG ((EFI_D_ERROR | EFI_D_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End)); + DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End)); return EFI_NOT_FOUND; } @@ -786,25 +724,27 @@ Returns: // if that's all we've got // RangeEnd = End; + + ASSERT (Entry != NULL); if (Entry->End < End) { RangeEnd = Entry->End; } - DEBUG ((EFI_D_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType)); + DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType)); // // Debug code - verify conversion is allowed // if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) { - DEBUG ((EFI_D_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; @@ -817,7 +757,7 @@ Returns: 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; } @@ -828,14 +768,14 @@ Returns: // Pull range out of descriptor // if (Entry->Start == Start) { - + // // Clip start // Entry->Start = RangeEnd + 1; } else if (Entry->End == RangeEnd) { - + // // Clip end // @@ -846,7 +786,7 @@ Returns: // // Pull it out of the center, clip current // - + // // Add a new one // @@ -872,7 +812,7 @@ Returns: } // - // 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; @@ -884,11 +824,14 @@ Returns: RemoveMemoryMapEntry (Entry); Entry = NULL; } - + // // Add our new range in // CoreAddRange (NewType, Start, RangeEnd, Attribute); + if (NewType == EfiConventionalMemory) { + DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1)); + } // // Move any map descriptor stack to general pool @@ -909,7 +852,20 @@ Returns: } -STATIC + +/** + 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 + + @return The base address of the range, or 0 if the range was not found + +**/ UINT64 CoreFindFreePagesI ( IN UINT64 MaxAddress, @@ -917,28 +873,6 @@ CoreFindFreePagesI ( IN EFI_MEMORY_TYPE NewType, IN UINTN Alignment ) -/*++ - -Routine Description: - - Internal function. Finds a consecutive free page range below - the requested address. - -Arguments: - - MaxAddress - The address that the range must be below - - NumberOfPages - Number of pages needed - - NewType - The type of memory the range is going to be turned into - - Alignment - Bits to align with - -Returns: - - The base address of the range, or 0 if the range was not found - ---*/ { UINT64 NumberOfBytes; UINT64 Target; @@ -953,21 +887,21 @@ Returns: } 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 // @@ -979,7 +913,7 @@ Returns: 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 // @@ -1007,7 +941,7 @@ Returns: 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; @@ -1021,7 +955,7 @@ Returns: Target = DescEnd; } } - } + } // // If this is a grow down, adjust target to be the allocation base @@ -1038,7 +972,20 @@ Returns: return Target; } -STATIC + +/** + 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 + + @return The base address of the range, or 0 if the range was not found. + +**/ UINT64 FindFreePages ( IN UINT64 MaxAddress, @@ -1046,28 +993,6 @@ FindFreePages ( IN EFI_MEMORY_TYPE NewType, IN UINTN Alignment ) -/*++ - -Routine Description: - - Internal function. Finds a consecutive free page range below - the requested address - -Arguments: - - MaxAddress - The address that the range must be below - - NoPages - Number of pages needed - - NewType - The type of memory the range is going to be turned into - - Alignment - Bits to align with - -Returns: - - The base address of the range, or 0 if the range was not found. - ---*/ { UINT64 NewMaxAddress; UINT64 Start; @@ -1083,9 +1008,9 @@ Returns: } Start = CoreFindFreePagesI (NewMaxAddress, NoPages, NewType, Alignment); - if (!Start) { + if (Start == 0) { Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment); - if (!Start) { + if (Start == 0) { // // Here means there may be no enough memory to use, so try to go through // all the memory descript to promote the untested memory directly @@ -1103,6 +1028,25 @@ Returns: } + +/** + 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 + + @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_SUCCESS Pages successfully allocated. + +**/ EFI_STATUS EFIAPI CoreAllocatePages ( @@ -1111,35 +1055,6 @@ CoreAllocatePages ( IN UINTN NumberOfPages, IN OUT EFI_PHYSICAL_ADDRESS *Memory ) -/*++ - -Routine Description: - - Allocates pages from the memory map. - -Arguments: - - Type - The type of allocation to perform - - MemoryType - The type of memory to turn the allocated pages into - - NumberOfPages - The number of pages to allocate - - Memory - A pointer to receive the base allocated memory address - -Returns: - - Status. On success, Memory is filled in with the base address allocated - - EFI_INVALID_PARAMETER - Parameters violate checking rules defined in spec. - - EFI_NOT_FOUND - Could not allocate pages match the requirement. - - EFI_OUT_OF_RESOURCES - No enough pages to allocate. - - EFI_SUCCESS - Pages successfully allocated. - ---*/ { EFI_STATUS Status; UINT64 Start; @@ -1175,21 +1090,21 @@ Returns: 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 // @@ -1217,35 +1132,23 @@ Done: } +/** + Frees previous allocated pages. + + @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 + @return EFI_SUCCESS -Pages successfully freed. -EFI_STATUS +**/ +EFI_STATUS EFIAPI CoreFreePages ( IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages ) -/*++ - -Routine Description: - - Frees previous allocated pages. - -Arguments: - - Memory - Base address of memory being freed - - NumberOfPages - The number of pages to free - -Returns: - - EFI_NOT_FOUND - Could not find the entry that covers the range - - EFI_INVALID_PARAMETER - Address not aligned - - EFI_SUCCESS -Pages successfully freed. - ---*/ { EFI_STATUS Status; LIST_ENTRY *Link; @@ -1268,12 +1171,13 @@ Returns: } } if (Link == &gMemoryMap) { - CoreReleaseMemoryLock (); - return EFI_NOT_FOUND; + Status = EFI_NOT_FOUND; + goto Done; } Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT; + ASSERT (Entry != NULL); if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS || Entry->Type == EfiRuntimeServicesCode || @@ -1284,8 +1188,8 @@ Returns: } if ((Memory & (Alignment - 1)) != 0) { - CoreReleaseMemoryLock (); - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1; @@ -1293,24 +1197,46 @@ Returns: Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory); - CoreReleaseMemoryLock (); - if (EFI_ERROR (Status)) { - return Status; + goto Done; } - // - // Destroy the contents - // - if (Memory < EFI_MAX_ADDRESS) { - DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT); - } - +Done: + CoreReleaseMemoryLock (); return Status; } +/** + 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. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. +**/ EFI_STATUS EFIAPI CoreGetMemoryMap ( @@ -1320,44 +1246,14 @@ CoreGetMemoryMap ( OUT UINTN *DescriptorSize, OUT UINT32 *DescriptorVersion ) -/*++ - -Routine Description: - - 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. - -Arguments: - - 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. - MemoryMap - A pointer to the buffer in which firmware places the current memory map. - MapKey - A pointer to the location in which firmware returns the key for the - current memory map. - DescriptorSize - A pointer to the location in which firmware returns the size, in - bytes, of an individual EFI_MEMORY_DESCRIPTOR. - DescriptorVersion - A pointer to the location in which firmware returns the version - number associated with the EFI_MEMORY_DESCRIPTOR. - -Returns: - - EFI_SUCCESS - The memory map was returned in the MemoryMap buffer. - EFI_BUFFER_TOO_SMALL - The MemoryMap buffer was too small. The current buffer size - needed to hold the memory map is returned in MemoryMapSize. - EFI_INVALID_PARAMETER - One of the parameters has an invalid value. - ---*/ { 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; // @@ -1366,9 +1262,9 @@ Returns: if (MemoryMapSize == NULL) { return EFI_INVALID_PARAMETER; } - + CoreAcquireGcdMemoryLock (); - + // // Count the number of Reserved and MMIO entries that are marked for runtime use // @@ -1395,7 +1291,7 @@ Returns: if (DescriptorSize != NULL) { *DescriptorSize = Size; } - + if (DescriptorVersion != NULL) { *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION; } @@ -1437,8 +1333,8 @@ Returns: 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. // @@ -1447,7 +1343,7 @@ Returns: if (mMemoryTypeStatistics[Type].Special && mMemoryTypeStatistics[Type].NumberOfPages > 0 && Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress && - Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress ) { + Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) { MemoryMap->Type = Type; } } @@ -1456,8 +1352,8 @@ Returns: 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) { @@ -1465,7 +1361,10 @@ Returns: 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); @@ -1481,126 +1380,103 @@ Returns: } } - 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; } -VOID * -CoreAllocatePoolPages ( - IN EFI_MEMORY_TYPE PoolType, - IN UINTN NumberOfPages, - IN UINTN Alignment - ) -/*++ - -Routine Description: +/** Internal function. Used by the pool functions to allocate pages to back pool allocation requests. -Arguments: + @param PoolType The type of memory for the new pool pages + @param NumberOfPages No of pages to allocate + @param Alignment Bits to align. - PoolType - The type of memory for the new pool pages + @return The allocated memory, or NULL - NumberOfPages - No of pages to allocate - - Alignment - Bits to align. - -Returns: - - The allocated memory, or NULL - ---*/ +**/ +VOID * +CoreAllocatePoolPages ( + IN EFI_MEMORY_TYPE PoolType, + IN UINTN NumberOfPages, + IN UINTN Alignment + ) { UINT64 Start; // // 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 // if (Start == 0) { - DEBUG ((EFI_D_ERROR | EFI_D_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages)); + DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages)); } else { CoreConvertPages (Start, NumberOfPages, PoolType); } - return (VOID *)(UINTN)Start; + return (VOID *)(UINTN) Start; } + +/** + Internal function. Frees pool pages allocated via AllocatePoolPages () + + @param Memory The base address to free + @param NumberOfPages The number of pages to free + +**/ VOID CoreFreePoolPages ( IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages ) -/*++ - -Routine Description: - - Internal function. Frees pool pages allocated via AllocatePoolPages () - -Arguments: - - Memory - The base address to free - - NumberOfPages - The number of pages to free - -Returns: - - None - ---*/ { CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory); } -EFI_STATUS -CoreTerminateMemoryMap ( - IN UINTN MapKey - ) -/*++ - -Routine Description: - Make sure the memory map is following all the construction rules, +/** + 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. -Arguments: - - MapKey - Memory map key - -Returns: + @param MapKey Memory map key - EFI_INVALID_PARAMETER - Memory map not consistent with construction rules. - - EFI_SUCCESS - Valid memory map. + @retval EFI_INVALID_PARAMETER Memory map not consistent with construction + rules. + @retval EFI_SUCCESS Valid memory map. ---*/ +**/ +EFI_STATUS +CoreTerminateMemoryMap ( + IN UINTN MapKey + ) { EFI_STATUS Status; LIST_ENTRY *Link; @@ -1620,21 +1496,21 @@ Returns: 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((EFI_D_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((EFI_D_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((EFI_D_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; } } } @@ -1650,6 +1526,7 @@ Returns: Status = EFI_INVALID_PARAMETER; } +Done: CoreReleaseMemoryLock (); return Status; @@ -1662,3 +1539,4 @@ Returns: +