X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FCore%2FPiSmmCore%2FPool.c;h=ea28484d68d9cf00fb8680a91f5dfcd1be48598c;hb=fbe12b79aef4c2706e90078cc75b94dcf7926ba8;hp=9e86d93e1865ed249a28c73863dd6ade768bf2da;hpb=e42e94041f7c71a5e2e57154bd568f3c14fd6eec;p=mirror_edk2.git diff --git a/MdeModulePkg/Core/PiSmmCore/Pool.c b/MdeModulePkg/Core/PiSmmCore/Pool.c index 9e86d93e18..ea28484d68 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 - 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 @@ -42,6 +42,11 @@ typedef struct { } FREE_POOL_HEADER; LIST_ENTRY mSmmPoolLists[MAX_POOL_INDEX]; +// +// 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. +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase = 0; /** Called to initialize the memory service. @@ -56,7 +61,10 @@ SmmInitializeMemoryServices ( IN EFI_SMRAM_DESCRIPTOR *SmramRanges ) { - UINTN Index; + UINTN Index; + UINT64 SmmCodeSize; + UINTN CurrentSmramRangesIndex; + UINT64 MaxSize; // // Initialize Pool list @@ -64,7 +72,38 @@ 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++) { + 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 +115,7 @@ SmmInitializeMemoryServices ( SmramRanges[Index].RegionState ); } + } /** @@ -97,11 +137,12 @@ InternalAllocPoolByIndex ( EFI_STATUS Status; FREE_POOL_HEADER *Hdr; + 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 = EFI_OUT_OF_RESOURCES; + return EFI_OUT_OF_RESOURCES; } } else if (!IsListEmpty (&mSmmPoolLists[PoolIndex])) { Hdr = BASE_CR (GetFirstNode (&mSmmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link); @@ -144,8 +185,9 @@ 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); return EFI_SUCCESS; } @@ -182,11 +224,6 @@ 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); @@ -203,7 +240,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++; }