X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FPiSmmCore%2FPool.c;h=34dcc93f1ab4cedfe080308c719d3276782e9f41;hp=d7f80f4f8c2448b776a5c2f84066607d23c55d6b;hb=84edd20bd0756ef5719835498d4283435d6b5e77;hpb=52c0d06b94665def4977e13ea329dccb17f46da5 diff --git a/MdeModulePkg/Core/PiSmmCore/Pool.c b/MdeModulePkg/Core/PiSmmCore/Pool.c index d7f80f4f8c..34dcc93f1a 100644 --- a/MdeModulePkg/Core/PiSmmCore/Pool.c +++ b/MdeModulePkg/Core/PiSmmCore/Pool.c @@ -1,7 +1,7 @@ /** @file SMM Memory pool management functions. - Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 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 @@ -14,34 +14,12 @@ #include "PiSmmCore.h" +LIST_ENTRY mSmmPoolLists[MAX_POOL_INDEX]; // -// MIN_POOL_SHIFT must not be less than 5 -// -#define MIN_POOL_SHIFT 6 -#define MIN_POOL_SIZE (1 << MIN_POOL_SHIFT) - -// -// MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1 -// -#define MAX_POOL_SHIFT (EFI_PAGE_SHIFT - 1) -#define MAX_POOL_SIZE (1 << MAX_POOL_SHIFT) - -// -// MAX_POOL_INDEX are calculated by maximum and minimum pool sizes +// To cache the SMRAM base since when Loading modules At fixed address feature is enabled, +// all module is assigned an offset relative the SMRAM base in build time. // -#define MAX_POOL_INDEX (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1) - -typedef struct { - UINTN Size; - BOOLEAN Available; -} POOL_HEADER; - -typedef struct { - POOL_HEADER Header; - LIST_ENTRY Link; -} FREE_POOL_HEADER; - -LIST_ENTRY mSmmPoolLists[MAX_POOL_INDEX]; +GLOBAL_REMOVE_IF_UNREFERENCED EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase = 0; /** Called to initialize the memory service. @@ -56,7 +34,10 @@ SmmInitializeMemoryServices ( IN EFI_SMRAM_DESCRIPTOR *SmramRanges ) { - UINTN Index; + UINTN Index; + UINT64 SmmCodeSize; + UINTN CurrentSmramRangesIndex; + UINT64 MaxSize; // // Initialize Pool list @@ -64,7 +45,45 @@ SmmInitializeMemoryServices ( for (Index = sizeof (mSmmPoolLists) / sizeof (*mSmmPoolLists); Index > 0;) { InitializeListHead (&mSmmPoolLists[--Index]); } - + CurrentSmramRangesIndex = 0; + // + // If Loadding Module At fixed Address feature is enabled, cache the SMRAM base here + // + if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) { + // + // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber + // + SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT); + + // + // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size + // + for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < SmramRangeCount; Index++) { + // + // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization + // + if ((SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) { + continue; + } + + if (SmramRanges[Index].CpuStart >= BASE_1MB) { + if ((SmramRanges[Index].CpuStart + SmramRanges[Index].PhysicalSize) <= BASE_4GB) { + if (SmramRanges[Index].PhysicalSize >= MaxSize) { + MaxSize = SmramRanges[Index].PhysicalSize; + CurrentSmramRangesIndex = Index; + } + } + } + } + gLoadModuleAtFixAddressSmramBase = SmramRanges[CurrentSmramRangesIndex].CpuStart; + + // + // cut out a memory range from this SMRAM range with the size SmmCodeSize to hold SMM driver code + // A notable thing is that SMM core is already loaded into this range. + // + SmramRanges[CurrentSmramRangesIndex].CpuStart = SmramRanges[CurrentSmramRangesIndex].CpuStart + SmmCodeSize; + SmramRanges[CurrentSmramRangesIndex].PhysicalSize = SmramRanges[CurrentSmramRangesIndex].PhysicalSize - SmmCodeSize; + } // // Initialize free SMRAM regions // @@ -76,6 +95,7 @@ SmmInitializeMemoryServices ( SmramRanges[Index].RegionState ); } + } /** @@ -94,16 +114,18 @@ InternalAllocPoolByIndex ( OUT FREE_POOL_HEADER **FreePoolHdr ) { - EFI_STATUS Status; - FREE_POOL_HEADER *Hdr; + EFI_STATUS Status; + FREE_POOL_HEADER *Hdr; + EFI_PHYSICAL_ADDRESS Address; ASSERT (PoolIndex <= MAX_POOL_INDEX); Status = EFI_SUCCESS; if (PoolIndex == MAX_POOL_INDEX) { - Hdr = (FREE_POOL_HEADER *)AllocatePages (EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1)); - if (Hdr == NULL) { + Status = SmmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address); + if (EFI_ERROR (Status)) { return EFI_OUT_OF_RESOURCES; } + Hdr = (FREE_POOL_HEADER *) (UINTN) Address; } else if (!IsListEmpty (&mSmmPoolLists[PoolIndex])) { Hdr = BASE_CR (GetFirstNode (&mSmmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link); RemoveEntryList (&Hdr->Link); @@ -145,7 +167,7 @@ InternalFreePoolByIndex ( ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0); ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE); - PoolIndex = HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT; + PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT); FreePoolHdr->Header.Available = TRUE; ASSERT (PoolIndex < MAX_POOL_INDEX); InsertHeadList (&mSmmPoolLists[PoolIndex], &FreePoolHdr->Link); @@ -167,7 +189,7 @@ InternalFreePoolByIndex ( **/ EFI_STATUS EFIAPI -SmmAllocatePool ( +SmmInternalAllocatePool ( IN EFI_MEMORY_TYPE PoolType, IN UINTN Size, OUT VOID **Buffer @@ -184,15 +206,10 @@ SmmAllocatePool ( return EFI_INVALID_PARAMETER; } - if (Size == 0) { - *Buffer = NULL; - return EFI_SUCCESS; - } - Size += sizeof (*PoolHdr); if (Size > MAX_POOL_SIZE) { Size = EFI_SIZE_TO_PAGES (Size); - Status = SmmAllocatePages (AllocateAnyPages, PoolType, Size, &Address); + Status = SmmInternalAllocatePages (AllocateAnyPages, PoolType, Size, &Address); if (EFI_ERROR (Status)) { return Status; } @@ -205,7 +222,7 @@ SmmAllocatePool ( } Size = (Size + MIN_POOL_SIZE - 1) >> MIN_POOL_SHIFT; - PoolIndex = HighBitSet32 ((UINT32)Size); + PoolIndex = (UINTN) HighBitSet32 ((UINT32)Size); if ((Size & (Size - 1)) != 0) { PoolIndex++; } @@ -215,6 +232,36 @@ SmmAllocatePool ( return Status; } +/** + Allocate pool of a particular type. + + @param PoolType Type of pool to allocate. + @param Size The amount of pool to allocate. + @param Buffer The address to return a pointer to the allocated + pool. + + @retval EFI_INVALID_PARAMETER PoolType not valid. + @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed. + @retval EFI_SUCCESS Pool successfully allocated. + +**/ +EFI_STATUS +EFIAPI +SmmAllocatePool ( + IN EFI_MEMORY_TYPE PoolType, + IN UINTN Size, + OUT VOID **Buffer + ) +{ + EFI_STATUS Status; + + Status = SmmInternalAllocatePool (PoolType, Size, Buffer); + if (!EFI_ERROR (Status)) { + SmmCoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer); + } + return Status; +} + /** Frees pool. @@ -226,7 +273,7 @@ SmmAllocatePool ( **/ EFI_STATUS EFIAPI -SmmFreePool ( +SmmInternalFreePool ( IN VOID *Buffer ) { @@ -242,10 +289,34 @@ SmmFreePool ( if (FreePoolHdr->Header.Size > MAX_POOL_SIZE) { ASSERT (((UINTN)FreePoolHdr & EFI_PAGE_MASK) == 0); ASSERT ((FreePoolHdr->Header.Size & EFI_PAGE_MASK) == 0); - return SmmFreePages ( + return SmmInternalFreePages ( (EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr, EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size) ); } return InternalFreePoolByIndex (FreePoolHdr); } + +/** + Frees pool. + + @param Buffer The allocated pool entry to free. + + @retval EFI_INVALID_PARAMETER Buffer is not a valid value. + @retval EFI_SUCCESS Pool successfully freed. + +**/ +EFI_STATUS +EFIAPI +SmmFreePool ( + IN VOID *Buffer + ) +{ + EFI_STATUS Status; + + Status = SmmInternalFreePool (Buffer); + if (!EFI_ERROR (Status)) { + SmmCoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePool, 0, 0, Buffer); + } + return Status; +}