MdeModulePkg/PiSmmCore: AllocatePool should use MemoryType.

PiSmmCore supports page level protection based upon the Memory Type
(EfiRuntimeServicesCode/EfiRuntimeServicesData) and PE image.

However, the Memory Type information is ignored in AllocatePool().
If a caller calls AllocatePool with EfiRuntimeServicesCode,
the final memory is still allocated as EfiRuntimeServicesData.

This patch supports AllocatePool with EfiRuntimeServicesCode.

Cc: Jeff Fan <jeff.fan@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Jeff Fan <jeff.fan@intel.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
This commit is contained in:
Jiewen Yao 2016-11-30 23:08:51 +08:00
parent 432f1d83f7
commit 5f4d3e1794
3 changed files with 122 additions and 67 deletions

View File

@ -1111,6 +1111,7 @@ extern LIST_ENTRY mSmmMemoryMap;
typedef struct { typedef struct {
UINTN Size; UINTN Size;
BOOLEAN Available; BOOLEAN Available;
EFI_MEMORY_TYPE Type;
} POOL_HEADER; } POOL_HEADER;
typedef struct { typedef struct {
@ -1118,6 +1119,12 @@ typedef struct {
LIST_ENTRY Link; LIST_ENTRY Link;
} FREE_POOL_HEADER; } FREE_POOL_HEADER;
extern LIST_ENTRY mSmmPoolLists[MAX_POOL_INDEX]; typedef enum {
SmmPoolTypeCode,
SmmPoolTypeData,
SmmPoolTypeMax,
} SMM_POOL_TYPE;
extern LIST_ENTRY mSmmPoolLists[SmmPoolTypeMax][MAX_POOL_INDEX];
#endif #endif

View File

@ -14,13 +14,37 @@
#include "PiSmmCore.h" #include "PiSmmCore.h"
LIST_ENTRY mSmmPoolLists[MAX_POOL_INDEX]; LIST_ENTRY mSmmPoolLists[SmmPoolTypeMax][MAX_POOL_INDEX];
// //
// To cache the SMRAM base since when Loading modules At fixed address feature is enabled, // 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. // all module is assigned an offset relative the SMRAM base in build time.
// //
GLOBAL_REMOVE_IF_UNREFERENCED EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase = 0; GLOBAL_REMOVE_IF_UNREFERENCED EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase = 0;
/**
Convert a UEFI memory type to SMM pool type.
@param[in] MemoryType Type of pool to allocate.
@return SMM pool type
**/
SMM_POOL_TYPE
UefiMemoryTypeToSmmPoolType (
IN EFI_MEMORY_TYPE MemoryType
)
{
ASSERT ((MemoryType == EfiRuntimeServicesCode) || (MemoryType == EfiRuntimeServicesData));
switch (MemoryType) {
case EfiRuntimeServicesCode:
return SmmPoolTypeCode;
case EfiRuntimeServicesData:
return SmmPoolTypeData;
default:
return SmmPoolTypeMax;
}
}
/** /**
Called to initialize the memory service. Called to initialize the memory service.
@ -38,12 +62,15 @@ SmmInitializeMemoryServices (
UINT64 SmmCodeSize; UINT64 SmmCodeSize;
UINTN CurrentSmramRangesIndex; UINTN CurrentSmramRangesIndex;
UINT64 MaxSize; UINT64 MaxSize;
UINTN SmmPoolTypeIndex;
// //
// Initialize Pool list // Initialize Pool list
// //
for (Index = ARRAY_SIZE (mSmmPoolLists); Index > 0;) { for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
InitializeListHead (&mSmmPoolLists[--Index]); for (Index = 0; Index < ARRAY_SIZE (mSmmPoolLists[SmmPoolTypeIndex]); Index++) {
InitializeListHead (&mSmmPoolLists[SmmPoolTypeIndex][Index]);
}
} }
CurrentSmramRangesIndex = 0; CurrentSmramRangesIndex = 0;
// //
@ -120,6 +147,7 @@ SmmInitializeMemoryServices (
/** /**
Internal Function. Allocate a pool by specified PoolIndex. Internal Function. Allocate a pool by specified PoolIndex.
@param PoolType Type of pool to allocate.
@param PoolIndex Index which indicate the Pool size. @param PoolIndex Index which indicate the Pool size.
@param FreePoolHdr The returned Free pool. @param FreePoolHdr The returned Free pool.
@ -129,6 +157,7 @@ SmmInitializeMemoryServices (
**/ **/
EFI_STATUS EFI_STATUS
InternalAllocPoolByIndex ( InternalAllocPoolByIndex (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN PoolIndex, IN UINTN PoolIndex,
OUT FREE_POOL_HEADER **FreePoolHdr OUT FREE_POOL_HEADER **FreePoolHdr
) )
@ -136,25 +165,29 @@ InternalAllocPoolByIndex (
EFI_STATUS Status; EFI_STATUS Status;
FREE_POOL_HEADER *Hdr; FREE_POOL_HEADER *Hdr;
EFI_PHYSICAL_ADDRESS Address; EFI_PHYSICAL_ADDRESS Address;
SMM_POOL_TYPE SmmPoolType;
SmmPoolType = UefiMemoryTypeToSmmPoolType(PoolType);
ASSERT (PoolIndex <= MAX_POOL_INDEX); ASSERT (PoolIndex <= MAX_POOL_INDEX);
Status = EFI_SUCCESS; Status = EFI_SUCCESS;
Hdr = NULL; Hdr = NULL;
if (PoolIndex == MAX_POOL_INDEX) { if (PoolIndex == MAX_POOL_INDEX) {
Status = SmmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address); Status = SmmInternalAllocatePages (AllocateAnyPages, PoolType, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return EFI_OUT_OF_RESOURCES; return EFI_OUT_OF_RESOURCES;
} }
Hdr = (FREE_POOL_HEADER *) (UINTN) Address; Hdr = (FREE_POOL_HEADER *) (UINTN) Address;
} else if (!IsListEmpty (&mSmmPoolLists[PoolIndex])) { } else if (!IsListEmpty (&mSmmPoolLists[SmmPoolType][PoolIndex])) {
Hdr = BASE_CR (GetFirstNode (&mSmmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link); Hdr = BASE_CR (GetFirstNode (&mSmmPoolLists[SmmPoolType][PoolIndex]), FREE_POOL_HEADER, Link);
RemoveEntryList (&Hdr->Link); RemoveEntryList (&Hdr->Link);
} else { } else {
Status = InternalAllocPoolByIndex (PoolIndex + 1, &Hdr); Status = InternalAllocPoolByIndex (PoolType, PoolIndex + 1, &Hdr);
if (!EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
Hdr->Header.Size >>= 1; Hdr->Header.Size >>= 1;
Hdr->Header.Available = TRUE; Hdr->Header.Available = TRUE;
InsertHeadList (&mSmmPoolLists[PoolIndex], &Hdr->Link); Hdr->Header.Type = PoolType;
InsertHeadList (&mSmmPoolLists[SmmPoolType][PoolIndex], &Hdr->Link);
Hdr = (FREE_POOL_HEADER*)((UINT8*)Hdr + Hdr->Header.Size); Hdr = (FREE_POOL_HEADER*)((UINT8*)Hdr + Hdr->Header.Size);
} }
} }
@ -162,6 +195,7 @@ InternalAllocPoolByIndex (
if (!EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex; Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex;
Hdr->Header.Available = FALSE; Hdr->Header.Available = FALSE;
Hdr->Header.Type = PoolType;
} }
*FreePoolHdr = Hdr; *FreePoolHdr = Hdr;
@ -182,15 +216,18 @@ InternalFreePoolByIndex (
) )
{ {
UINTN PoolIndex; UINTN PoolIndex;
SMM_POOL_TYPE SmmPoolType;
ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0); ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0);
ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0); ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);
ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE); ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);
SmmPoolType = UefiMemoryTypeToSmmPoolType(FreePoolHdr->Header.Type);
PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT); PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT);
FreePoolHdr->Header.Available = TRUE; FreePoolHdr->Header.Available = TRUE;
ASSERT (PoolIndex < MAX_POOL_INDEX); ASSERT (PoolIndex < MAX_POOL_INDEX);
InsertHeadList (&mSmmPoolLists[PoolIndex], &FreePoolHdr->Link); InsertHeadList (&mSmmPoolLists[SmmPoolType][PoolIndex], &FreePoolHdr->Link);
return EFI_SUCCESS; return EFI_SUCCESS;
} }
@ -237,6 +274,7 @@ SmmInternalAllocatePool (
PoolHdr = (POOL_HEADER*)(UINTN)Address; PoolHdr = (POOL_HEADER*)(UINTN)Address;
PoolHdr->Size = EFI_PAGES_TO_SIZE (Size); PoolHdr->Size = EFI_PAGES_TO_SIZE (Size);
PoolHdr->Available = FALSE; PoolHdr->Available = FALSE;
PoolHdr->Type = PoolType;
*Buffer = PoolHdr + 1; *Buffer = PoolHdr + 1;
return Status; return Status;
} }
@ -247,7 +285,7 @@ SmmInternalAllocatePool (
PoolIndex++; PoolIndex++;
} }
Status = InternalAllocPoolByIndex (PoolIndex, &FreePoolHdr); Status = InternalAllocPoolByIndex (PoolType, PoolIndex, &FreePoolHdr);
if (!EFI_ERROR(Status)) { if (!EFI_ERROR(Status)) {
*Buffer = &FreePoolHdr->Header + 1; *Buffer = &FreePoolHdr->Header + 1;
} }

View File

@ -1596,6 +1596,7 @@ SmramProfileGetDataSize (
FREE_POOL_HEADER *Pool; FREE_POOL_HEADER *Pool;
UINTN PoolListIndex; UINTN PoolListIndex;
UINTN Index; UINTN Index;
UINTN SmmPoolTypeIndex;
ContextData = GetSmramProfileContext (); ContextData = GetSmramProfileContext ();
if (ContextData == NULL) { if (ContextData == NULL) {
@ -1638,8 +1639,9 @@ SmramProfileGetDataSize (
Node = Node->BackLink) { Node = Node->BackLink) {
Index++; Index++;
} }
for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) { for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) {
FreePoolList = &mSmmPoolLists[PoolListIndex]; FreePoolList = &mSmmPoolLists[SmmPoolTypeIndex][PoolListIndex];
for (Node = FreePoolList->BackLink; for (Node = FreePoolList->BackLink;
Node != FreePoolList; Node != FreePoolList;
Node = Node->BackLink) { Node = Node->BackLink) {
@ -1649,7 +1651,7 @@ SmramProfileGetDataSize (
} }
} }
} }
}
TotalSize += (sizeof (MEMORY_PROFILE_FREE_MEMORY) + Index * sizeof (MEMORY_PROFILE_DESCRIPTOR)); TotalSize += (sizeof (MEMORY_PROFILE_FREE_MEMORY) + Index * sizeof (MEMORY_PROFILE_DESCRIPTOR));
TotalSize += (sizeof (MEMORY_PROFILE_MEMORY_RANGE) + mFullSmramRangeCount * sizeof (MEMORY_PROFILE_DESCRIPTOR)); TotalSize += (sizeof (MEMORY_PROFILE_MEMORY_RANGE) + mFullSmramRangeCount * sizeof (MEMORY_PROFILE_DESCRIPTOR));
@ -1698,6 +1700,7 @@ SmramProfileCopyData (
UINT64 RemainingSize; UINT64 RemainingSize;
UINTN PdbSize; UINTN PdbSize;
UINTN ActionStringSize; UINTN ActionStringSize;
UINTN SmmPoolTypeIndex;
ContextData = GetSmramProfileContext (); ContextData = GetSmramProfileContext ();
if (ContextData == NULL) { if (ContextData == NULL) {
@ -1785,8 +1788,9 @@ SmramProfileCopyData (
Node = Node->BackLink) { Node = Node->BackLink) {
Index++; Index++;
} }
for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) { for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) {
FreePoolList = &mSmmPoolLists[MAX_POOL_INDEX - PoolListIndex - 1]; FreePoolList = &mSmmPoolLists[SmmPoolTypeIndex][MAX_POOL_INDEX - PoolListIndex - 1];
for (Node = FreePoolList->BackLink; for (Node = FreePoolList->BackLink;
Node != FreePoolList; Node != FreePoolList;
Node = Node->BackLink) { Node = Node->BackLink) {
@ -1796,6 +1800,7 @@ SmramProfileCopyData (
} }
} }
} }
}
FreeMemory->FreeMemoryEntryCount = Index; FreeMemory->FreeMemoryEntryCount = Index;
RemainingSize -= sizeof (MEMORY_PROFILE_FREE_MEMORY); RemainingSize -= sizeof (MEMORY_PROFILE_FREE_MEMORY);
@ -1827,8 +1832,9 @@ SmramProfileCopyData (
} }
Offset += sizeof (MEMORY_PROFILE_DESCRIPTOR); Offset += sizeof (MEMORY_PROFILE_DESCRIPTOR);
} }
for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) { for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) {
FreePoolList = &mSmmPoolLists[MAX_POOL_INDEX - PoolListIndex - 1]; FreePoolList = &mSmmPoolLists[SmmPoolTypeIndex][MAX_POOL_INDEX - PoolListIndex - 1];
for (Node = FreePoolList->BackLink; for (Node = FreePoolList->BackLink;
Node != FreePoolList; Node != FreePoolList;
Node = Node->BackLink) { Node = Node->BackLink) {
@ -1853,6 +1859,7 @@ SmramProfileCopyData (
} }
} }
} }
}
if (*ProfileOffset < (Offset + sizeof (MEMORY_PROFILE_MEMORY_RANGE))) { if (*ProfileOffset < (Offset + sizeof (MEMORY_PROFILE_MEMORY_RANGE))) {
if (RemainingSize >= sizeof (MEMORY_PROFILE_MEMORY_RANGE)) { if (RemainingSize >= sizeof (MEMORY_PROFILE_MEMORY_RANGE)) {
@ -2577,6 +2584,7 @@ DumpFreePoolList (
UINTN PoolListIndex; UINTN PoolListIndex;
MEMORY_PROFILE_CONTEXT_DATA *ContextData; MEMORY_PROFILE_CONTEXT_DATA *ContextData;
BOOLEAN SmramProfileGettingStatus; BOOLEAN SmramProfileGettingStatus;
UINTN SmmPoolTypeIndex;
ContextData = GetSmramProfileContext (); ContextData = GetSmramProfileContext ();
if (ContextData == NULL) { if (ContextData == NULL) {
@ -2586,23 +2594,25 @@ DumpFreePoolList (
SmramProfileGettingStatus = mSmramProfileGettingStatus; SmramProfileGettingStatus = mSmramProfileGettingStatus;
mSmramProfileGettingStatus = TRUE; mSmramProfileGettingStatus = TRUE;
DEBUG ((EFI_D_INFO, "======= SmramProfile begin =======\n")); DEBUG ((DEBUG_INFO, "======= SmramProfile begin =======\n"));
for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) { for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) {
DEBUG ((EFI_D_INFO, "FreePoolList (%d):\n", PoolListIndex)); DEBUG ((DEBUG_INFO, "FreePoolList(%d)(%d):\n", SmmPoolTypeIndex, PoolListIndex));
FreePoolList = &mSmmPoolLists[PoolListIndex]; FreePoolList = &mSmmPoolLists[SmmPoolTypeIndex][PoolListIndex];
for (Node = FreePoolList->BackLink, Index = 0; for (Node = FreePoolList->BackLink, Index = 0;
Node != FreePoolList; Node != FreePoolList;
Node = Node->BackLink, Index++) { Node = Node->BackLink, Index++) {
Pool = BASE_CR (Node, FREE_POOL_HEADER, Link); Pool = BASE_CR (Node, FREE_POOL_HEADER, Link);
DEBUG ((EFI_D_INFO, " Index - 0x%x\n", Index)); DEBUG ((DEBUG_INFO, " Index - 0x%x\n", Index));
DEBUG ((EFI_D_INFO, " PhysicalStart - 0x%016lx\n", (PHYSICAL_ADDRESS) (UINTN) Pool)); DEBUG ((DEBUG_INFO, " PhysicalStart - 0x%016lx\n", (PHYSICAL_ADDRESS) (UINTN) Pool));
DEBUG ((EFI_D_INFO, " Size - 0x%08x\n", Pool->Header.Size)); DEBUG ((DEBUG_INFO, " Size - 0x%08x\n", Pool->Header.Size));
DEBUG ((EFI_D_INFO, " Available - 0x%02x\n", Pool->Header.Available)); DEBUG ((DEBUG_INFO, " Available - 0x%02x\n", Pool->Header.Available));
}
} }
} }
DEBUG ((EFI_D_INFO, "======= SmramProfile end =======\n")); DEBUG ((DEBUG_INFO, "======= SmramProfile end =======\n"));
mSmramProfileGettingStatus = SmramProfileGettingStatus; mSmramProfileGettingStatus = SmramProfileGettingStatus;
} }