X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxe%2FMem%2FPage.c;h=18780070c5eaf8df44a27756933ba236c8c3800a;hp=179376dc31c6bc50a07845a5d80a3aafc259bc55;hb=84edd20bd0756ef5719835498d4283435d6b5e77;hpb=d9177625f69f65107e298cee9bf3dc54bd06f9c1 diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/Page.c index 179376dc31..18780070c5 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -1,8 +1,8 @@ /** @file UEFI Memory page management functions. -Copyright (c) 2007 - 2008, Intel Corporation.
-All rights reserved. This program and the accompanying materials +Copyright (c) 2007 - 2014, 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 @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ #include "DxeMain.h" +#include "Imem.h" #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE) @@ -27,47 +28,50 @@ typedef struct { UINTN InformationIndex; BOOLEAN Special; BOOLEAN Runtime; -} EFI_MEMORY_TYPE_STAISTICS; +} EFI_MEMORY_TYPE_STATISTICS; // // MemoryMap - The current memory map // 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 +EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = { + { 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_PHYSICAL_ADDRESS mDefaultBaseAddress = MAX_ADDRESS; EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = { { EfiReservedMemoryType, 0 }, @@ -86,7 +90,12 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = { { EfiPalCode, 0 }, { EfiMaxMemoryType, 0 } }; - +// +// 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. +// +GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady = FALSE; /** Enter critical section by gaining lock on gMemoryLock. @@ -168,7 +177,20 @@ CoreAddRange ( ASSERT_LOCKED (&gMemoryLock); DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type)); - + + // + // If memory of type EfiConventionalMemory is being added that includes the page + // starting at address 0, then zero the page starting at address 0. This has + // two benifits. It helps find NULL pointer bugs and it also maximizes + // compatibility with operating systems that may evaluate memory in this page + // for legacy data structures. If memory of any other type is added starting + // at address 0, then do not zero the page at address 0 because the page is being + // used for other purposes. + // + if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) { + SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0); + } + // // Memory map being altered so updated key // @@ -366,25 +388,27 @@ CoreFreeMemoryMapStack ( Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable. **/ -VOID +BOOLEAN PromoteMemoryResource ( VOID ) { - LIST_ENTRY *Link; - EFI_GCD_MAP_ENTRY *Entry; + LIST_ENTRY *Link; + EFI_GCD_MAP_ENTRY *Entry; + BOOLEAN Promoted; DEBUG ((DEBUG_PAGE, "Promote the memory resource\n")); CoreAcquireGcdMemoryLock (); + Promoted = FALSE; 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)) { // @@ -407,6 +431,7 @@ PromoteMemoryResource ( ); CoreFreeMemoryMapStack (); + Promoted = TRUE; } Link = Link->ForwardLink; @@ -414,9 +439,72 @@ PromoteMemoryResource ( CoreReleaseGcdMemoryLock (); - return; + return Promoted; } +/** + 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 +CoreLoadingFixedAddressHook ( + VOID + ) +{ + 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 @@ -445,7 +533,7 @@ CoreAddMemoryDescriptor ( EFI_STATUS Status; UINTN Index; UINTN FreeIndex; - + if ((Start & EFI_PAGE_MASK) != 0) { return; } @@ -453,13 +541,19 @@ CoreAddMemoryDescriptor ( 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 // @@ -467,6 +561,7 @@ CoreAddMemoryDescriptor ( return; } + // // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array // @@ -475,10 +570,9 @@ CoreAddMemoryDescriptor ( // Make sure the memory type in the gMemoryTypeInformation[] array is valid // Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type); - if (Type < 0 || Type > EfiMaxMemoryType) { + if ((UINT32)Type > EfiMaxMemoryType) { continue; } - if (gMemoryTypeInformation[Index].NumberOfPages != 0) { // // Allocate pages for the current memory type from the top of available memory @@ -500,7 +594,7 @@ CoreAddMemoryDescriptor ( // Make sure the memory type in the gMemoryTypeInformation[] array is valid // Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type); - if (Type < 0 || Type > EfiMaxMemoryType) { + if ((UINT32)Type > EfiMaxMemoryType) { continue; } @@ -510,7 +604,7 @@ CoreAddMemoryDescriptor ( gMemoryTypeInformation[FreeIndex].NumberOfPages ); mMemoryTypeStatistics[Type].BaseAddress = 0; - mMemoryTypeStatistics[Type].MaximumAddress = EFI_MAX_ADDRESS; + mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS; } } return; @@ -543,10 +637,9 @@ CoreAddMemoryDescriptor ( // Make sure the memory type in the gMemoryTypeInformation[] array is valid // Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type); - if (Type < 0 || Type > EfiMaxMemoryType) { + if ((UINT32)Type > EfiMaxMemoryType) { continue; } - if (gMemoryTypeInformation[Index].NumberOfPages != 0) { CoreFreePages ( mMemoryTypeStatistics[Type].BaseAddress, @@ -568,7 +661,7 @@ CoreAddMemoryDescriptor ( } } mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0; - if (mMemoryTypeStatistics[Type].MaximumAddress == EFI_MAX_ADDRESS) { + if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) { mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress; } } @@ -648,6 +741,8 @@ CoreConvertPages ( // if that's all we've got // RangeEnd = End; + + ASSERT (Entry != NULL); if (Entry->End < End) { RangeEnd = Entry->End; } @@ -665,9 +760,9 @@ CoreConvertPages ( // // 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 && - Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) { + if ((UINT32)Entry->Type < EfiMaxMemoryType) { + if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) || + (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) { if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) { mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0; } else { @@ -676,11 +771,11 @@ CoreConvertPages ( } } - if (NewType >= 0 && NewType < EfiMaxMemoryType) { - if (Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) { + if ((UINT32)NewType < EfiMaxMemoryType) { + if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) || + (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) { mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages; - if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > - gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) { + if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) { gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages; } } @@ -751,6 +846,20 @@ CoreConvertPages ( // Add our new range in // CoreAddRange (NewType, Start, RangeEnd, Attribute); + if (NewType == EfiConventionalMemory) { + // + // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this + // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees + // that the page starting at address 0 is always filled with zeros. + // + if (Start == 0) { + if (RangeEnd > EFI_PAGE_SIZE) { + DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1)); + } + } else { + DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1)); + } + } // // Move any map descriptor stack to general pool @@ -777,6 +886,7 @@ CoreConvertPages ( the requested address. @param MaxAddress The address that the range must be below + @param MinAddress The address that the range must be above @param NumberOfPages Number of pages needed @param NewType The type of memory the range is going to be turned into @@ -788,6 +898,7 @@ CoreConvertPages ( UINT64 CoreFindFreePagesI ( IN UINT64 MaxAddress, + IN UINT64 MinAddress, IN UINT64 NumberOfPages, IN EFI_MEMORY_TYPE NewType, IN UINTN Alignment @@ -819,7 +930,7 @@ CoreFindFreePagesI ( // // Set MaxAddress to a page boundary // - MaxAddress &= ~EFI_PAGE_MASK; + MaxAddress &= ~(UINT64)EFI_PAGE_MASK; // // Set MaxAddress to end of the page @@ -844,9 +955,9 @@ CoreFindFreePagesI ( DescEnd = Entry->End; // - // If desc is past max allowed address, skip it + // If desc is past max allowed address or below min allowed address, skip it // - if (DescStart >= MaxAddress) { + if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) { continue; } @@ -866,6 +977,12 @@ CoreFindFreePagesI ( DescNumberOfBytes = DescEnd - DescStart + 1; if (DescNumberOfBytes >= NumberOfBytes) { + // + // If the start of the allocated range is below the min address allowed, skip it + // + if ((DescEnd - NumberOfBytes + 1) < MinAddress) { + continue; + } // // If this is the best match so far remember it @@ -913,41 +1030,62 @@ FindFreePages ( IN UINTN Alignment ) { - UINT64 NewMaxAddress; - UINT64 Start; + UINT64 Start; - NewMaxAddress = MaxAddress; + // + // Attempt to find free pages in the preferred bin based on the requested memory type + // + if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) { + Start = CoreFindFreePagesI ( + mMemoryTypeStatistics[NewType].MaximumAddress, + mMemoryTypeStatistics[NewType].BaseAddress, + NoPages, + NewType, + Alignment + ); + if (Start != 0) { + return Start; + } + } - if (NewType >= 0 && NewType < EfiMaxMemoryType && NewMaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) { - NewMaxAddress = mMemoryTypeStatistics[NewType].MaximumAddress; - } else { - if (NewMaxAddress > mDefaultMaximumAddress) { - NewMaxAddress = mDefaultMaximumAddress; + // + // Attempt to find free pages in the default allocation bin + // + if (MaxAddress >= mDefaultMaximumAddress) { + Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment); + if (Start != 0) { + if (Start < mDefaultBaseAddress) { + mDefaultBaseAddress = Start; + } + return Start; } } - Start = CoreFindFreePagesI (NewMaxAddress, NoPages, NewType, Alignment); - if (Start == 0) { - Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment); - 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 - // - PromoteMemoryResource (); + // + // The allocation did not succeed in any of the prefered bins even after + // promoting resources. Attempt to find free pages anywhere is the requested + // address range. If this allocation fails, then there are not enough + // resources anywhere to satisfy the request. + // + Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment); + if (Start != 0) { + return Start; + } - // - // Allocate memory again after the memory resource re-arranged - // - Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment); - } + // + // If allocations from the preferred bins fail, then attempt to promote memory resources. + // + if (!PromoteMemoryResource ()) { + return 0; } - return Start; + // + // If any memory resources were promoted, then re-attempt the allocation + // + return FindFreePages (MaxAddress, NoPages, NewType, Alignment); } - /** Allocates pages from the memory map. @@ -968,7 +1106,7 @@ FindFreePages ( **/ EFI_STATUS EFIAPI -CoreAllocatePages ( +CoreInternalAllocatePages ( IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN UINTN NumberOfPages, @@ -980,7 +1118,7 @@ CoreAllocatePages ( UINT64 MaxAddress; UINTN Alignment; - if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) { + if ((UINT32)Type >= MaxAllocateType) { return EFI_INVALID_PARAMETER; } @@ -989,6 +1127,10 @@ CoreAllocatePages ( return EFI_INVALID_PARAMETER; } + if (Memory == NULL) { + return EFI_INVALID_PARAMETER; + } + Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT; if (MemoryType == EfiACPIReclaimMemory || @@ -1016,7 +1158,7 @@ CoreAllocatePages ( // // The max address is the max natively addressable address for the processor // - MaxAddress = EFI_MAX_ADDRESS; + MaxAddress = MAX_ADDRESS; if (Type == AllocateMaxAddress) { MaxAddress = Start; @@ -1050,6 +1192,41 @@ Done: return Status; } +/** + 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 ( + IN EFI_ALLOCATE_TYPE Type, + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN NumberOfPages, + OUT EFI_PHYSICAL_ADDRESS *Memory + ) +{ + EFI_STATUS Status; + + Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory); + if (!EFI_ERROR (Status)) { + CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePages, MemoryType, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) *Memory); + } + return Status; +} /** Frees previous allocated pages. @@ -1064,7 +1241,7 @@ Done: **/ EFI_STATUS EFIAPI -CoreFreePages ( +CoreInternalFreePages ( IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages ) @@ -1090,12 +1267,13 @@ CoreFreePages ( } } 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 || @@ -1106,8 +1284,8 @@ CoreFreePages ( } if ((Memory & (Alignment - 1)) != 0) { - CoreReleaseMemoryLock (); - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1; @@ -1115,23 +1293,123 @@ CoreFreePages ( Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory); + if (EFI_ERROR (Status)) { + goto Done; + } + +Done: CoreReleaseMemoryLock (); + return Status; +} - if (EFI_ERROR (Status)) { - return Status; +/** + 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 +EFIAPI +CoreFreePages ( + IN EFI_PHYSICAL_ADDRESS Memory, + IN UINTN NumberOfPages + ) +{ + EFI_STATUS Status; + + Status = CoreInternalFreePages (Memory, NumberOfPages); + if (!EFI_ERROR (Status)) { + CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePages, 0, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) Memory); } + return Status; +} +/** + This function checks to see if the last memory map descriptor in a memory map + can be merged with any of the other memory map descriptors in a memorymap. + Memory descriptors may be merged if they are adjacent and have the same type + and attributes. + + @param MemoryMap A pointer to the start of the memory map. + @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap. + @param DescriptorSize The size, in bytes, of an individual + EFI_MEMORY_DESCRIPTOR. + + @return A pointer to the next available descriptor in MemoryMap + +**/ +EFI_MEMORY_DESCRIPTOR * +MergeMemoryMapDescriptor ( + IN EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor, + IN UINTN DescriptorSize + ) +{ // - // Destroy the contents + // Traverse the array of descriptors in MemoryMap // - if (Memory < EFI_MAX_ADDRESS) { - DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT); + for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) { + // + // Check to see if the Type fields are identical. + // + if (MemoryMap->Type != MemoryMapDescriptor->Type) { + continue; + } + + // + // Check to see if the Attribute fields are identical. + // + if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) { + continue; + } + + // + // Check to see if MemoryMapDescriptor is immediately above MemoryMap + // + if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { + // + // Merge MemoryMapDescriptor into MemoryMap + // + MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages; + + // + // Return MemoryMapDescriptor as the next available slot int he MemoryMap array + // + return MemoryMapDescriptor; + } + + // + // Check to see if MemoryMapDescriptor is immediately below MemoryMap + // + if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { + // + // Merge MemoryMapDescriptor into MemoryMap + // + MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart; + MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart; + MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages; + + // + // Return MemoryMapDescriptor as the next available slot int he MemoryMap array + // + return MemoryMapDescriptor; + } } - return Status; + // + // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap. + // + // Return the slot immediately after MemoryMapDescriptor as the next available + // slot in the MemoryMap array + // + return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize); } - /** 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. @@ -1180,6 +1458,7 @@ CoreGetMemoryMap ( MEMORY_MAP *Entry; EFI_GCD_MAP_ENTRY *GcdMapEntry; EFI_MEMORY_TYPE Type; + EFI_MEMORY_DESCRIPTOR *MemoryMapStart; // // Make sure the parameters are valid @@ -1245,6 +1524,7 @@ CoreGetMemoryMap ( // Build the map // ZeroMem (MemoryMap, BufferSize); + MemoryMapStart = MemoryMap; for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) { Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); ASSERT (Entry->VirtualStart == 0); @@ -1274,11 +1554,17 @@ CoreGetMemoryMap ( } } MemoryMap->Attribute = Entry->Attribute; - if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) { - MemoryMap->Attribute |= EFI_MEMORY_RUNTIME; + if (MemoryMap->Type < EfiMaxMemoryType) { + if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) { + MemoryMap->Attribute |= EFI_MEMORY_RUNTIME; + } } - MemoryMap = NextMemoryDescriptor (MemoryMap, Size); + // + // Check to see if the new Memory Map Descriptor can be merged with an + // existing descriptor if they are adjacent and have the same attributes + // + MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size); } for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) { @@ -1286,7 +1572,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); @@ -1302,19 +1591,23 @@ CoreGetMemoryMap ( } } - MemoryMap = NextMemoryDescriptor (MemoryMap, Size); + // + // Check to see if the new Memory Map Descriptor can be merged with an + // existing descriptor if they are adjacent and have the same attributes + // + MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size); } } } + // + // Compute the size of the buffer actually used after all memory map descriptor merge operations + // + BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart); + Status = EFI_SUCCESS; Done: - - CoreReleaseMemoryLock (); - - CoreReleaseGcdMemoryLock (); - // // Update the map key finally // @@ -1322,6 +1615,10 @@ Done: *MapKey = mMemoryMapKey; } + CoreReleaseMemoryLock (); + + CoreReleaseGcdMemoryLock (); + *MemoryMapSize = BufferSize; return Status; @@ -1351,13 +1648,13 @@ 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 // if (Start == 0) { - DEBUG ((DEBUG_ERROR | DEBUG_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); } @@ -1418,18 +1715,18 @@ 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 | 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)) { + 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)) { + 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;