summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Core/PiSmmCore/Page.c
diff options
context:
space:
mode:
Diffstat (limited to 'MdeModulePkg/Core/PiSmmCore/Page.c')
-rw-r--r--MdeModulePkg/Core/PiSmmCore/Page.c236
1 files changed, 132 insertions, 104 deletions
diff --git a/MdeModulePkg/Core/PiSmmCore/Page.c b/MdeModulePkg/Core/PiSmmCore/Page.c
index d886187d9a..255964c23a 100644
--- a/MdeModulePkg/Core/PiSmmCore/Page.c
+++ b/MdeModulePkg/Core/PiSmmCore/Page.c
@@ -17,36 +17,34 @@ LIST_ENTRY mSmmMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (mSmmMemoryMap);
// For GetMemoryMap()
//
-#define MEMORY_MAP_SIGNATURE SIGNATURE_32('m','m','a','p')
+#define MEMORY_MAP_SIGNATURE SIGNATURE_32('m','m','a','p')
typedef struct {
- UINTN Signature;
- LIST_ENTRY Link;
-
- BOOLEAN FromStack;
- EFI_MEMORY_TYPE Type;
- UINT64 Start;
- UINT64 End;
+ UINTN Signature;
+ LIST_ENTRY Link;
+ BOOLEAN FromStack;
+ EFI_MEMORY_TYPE Type;
+ UINT64 Start;
+ UINT64 End;
} MEMORY_MAP;
-LIST_ENTRY gMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (gMemoryMap);
-
+LIST_ENTRY gMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (gMemoryMap);
-#define MAX_MAP_DEPTH 6
+#define MAX_MAP_DEPTH 6
///
/// mMapDepth - depth of new descriptor stack
///
-UINTN mMapDepth = 0;
+UINTN mMapDepth = 0;
///
/// mMapStack - space to use as temp storage to build new map descriptors
///
-MEMORY_MAP mMapStack[MAX_MAP_DEPTH];
-UINTN mFreeMapStack = 0;
+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);
+LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);
/**
Allocates pages from the memory map.
@@ -91,16 +89,16 @@ AllocateMemoryMapEntry (
VOID
)
{
- EFI_PHYSICAL_ADDRESS Mem;
- EFI_STATUS Status;
- MEMORY_MAP* FreeDescriptorEntries;
- MEMORY_MAP* Entry;
- UINTN Index;
+ EFI_PHYSICAL_ADDRESS Mem;
+ EFI_STATUS Status;
+ MEMORY_MAP *FreeDescriptorEntries;
+ MEMORY_MAP *Entry;
+ UINTN Index;
- //DEBUG((DEBUG_INFO, "AllocateMemoryMapEntry\n"));
+ // DEBUG((DEBUG_INFO, "AllocateMemoryMapEntry\n"));
if (IsListEmpty (&mFreeMemoryMapEntryList)) {
- //DEBUG((DEBUG_INFO, "mFreeMemoryMapEntryList is empty\n"));
+ // DEBUG((DEBUG_INFO, "mFreeMemoryMapEntryList is empty\n"));
//
// The list is empty, to allocate one page to refuel the list
//
@@ -113,13 +111,13 @@ AllocateMemoryMapEntry (
FALSE
);
ASSERT_EFI_ERROR (Status);
- if(!EFI_ERROR (Status)) {
+ if (!EFI_ERROR (Status)) {
FreeDescriptorEntries = (MEMORY_MAP *)(UINTN)Mem;
- //DEBUG((DEBUG_INFO, "New FreeDescriptorEntries - 0x%x\n", FreeDescriptorEntries));
+ // DEBUG((DEBUG_INFO, "New FreeDescriptorEntries - 0x%x\n", FreeDescriptorEntries));
//
// Enqueue the free memory map entries into the list
//
- for (Index = 0; Index< RUNTIME_PAGE_ALLOCATION_GRANULARITY / sizeof(MEMORY_MAP); Index++) {
+ for (Index = 0; Index < RUNTIME_PAGE_ALLOCATION_GRANULARITY / sizeof (MEMORY_MAP); Index++) {
FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;
InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);
}
@@ -127,6 +125,7 @@ AllocateMemoryMapEntry (
return NULL;
}
}
+
//
// dequeue the first descriptor from the list
//
@@ -136,7 +135,6 @@ AllocateMemoryMapEntry (
return Entry;
}
-
/**
Internal function. Moves any memory descriptors that are on the
temporary descriptor stack to heap.
@@ -147,14 +145,14 @@ CoreFreeMemoryMapStack (
VOID
)
{
- MEMORY_MAP *Entry;
+ MEMORY_MAP *Entry;
//
// If already freeing the map stack, then return
//
if (mFreeMapStack != 0) {
ASSERT (FALSE);
- return ;
+ return;
}
//
@@ -175,8 +173,7 @@ CoreFreeMemoryMapStack (
mMapDepth -= 1;
if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {
-
- CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));
+ CopyMem (Entry, &mMapStack[mMapDepth], sizeof (MEMORY_MAP));
Entry->FromStack = FALSE;
//
@@ -203,25 +200,25 @@ CoreFreeMemoryMapStack (
**/
VOID
InsertNewEntry (
- IN LIST_ENTRY *Link,
- IN UINT64 Start,
- IN UINT64 End,
- IN EFI_MEMORY_TYPE Type,
- IN BOOLEAN Next,
- IN BOOLEAN AddRegion
+ IN LIST_ENTRY *Link,
+ IN UINT64 Start,
+ IN UINT64 End,
+ IN EFI_MEMORY_TYPE Type,
+ IN BOOLEAN Next,
+ IN BOOLEAN AddRegion
)
{
MEMORY_MAP *Entry;
- Entry = &mMapStack[mMapDepth];
+ Entry = &mMapStack[mMapDepth];
mMapDepth += 1;
ASSERT (mMapDepth < MAX_MAP_DEPTH);
Entry->FromStack = TRUE;
Entry->Signature = MEMORY_MAP_SIGNATURE;
- Entry->Type = Type;
- Entry->Start = Start;
- Entry->End = End;
+ Entry->Type = Type;
+ Entry->Start = Start;
+ Entry->End = End;
if (Next) {
InsertHeadList (Link, &Entry->Link);
} else {
@@ -263,17 +260,17 @@ ConvertSmmMemoryMapEntry (
IN BOOLEAN AddRegion
)
{
- LIST_ENTRY *Link;
- MEMORY_MAP *Entry;
- MEMORY_MAP *NextEntry;
- LIST_ENTRY *NextLink;
- MEMORY_MAP *PreviousEntry;
- LIST_ENTRY *PreviousLink;
- EFI_PHYSICAL_ADDRESS Start;
- EFI_PHYSICAL_ADDRESS End;
+ LIST_ENTRY *Link;
+ MEMORY_MAP *Entry;
+ MEMORY_MAP *NextEntry;
+ LIST_ENTRY *NextLink;
+ MEMORY_MAP *PreviousEntry;
+ LIST_ENTRY *PreviousLink;
+ EFI_PHYSICAL_ADDRESS Start;
+ EFI_PHYSICAL_ADDRESS End;
Start = Memory;
- End = Memory + EFI_PAGES_TO_SIZE(NumberOfPages) - 1;
+ End = Memory + EFI_PAGES_TO_SIZE (NumberOfPages) - 1;
//
// Exclude memory region
@@ -296,8 +293,9 @@ ConvertSmmMemoryMapEntry (
if (Entry->Start > End) {
if ((Entry->Start == End + 1) && (Entry->Type == Type)) {
Entry->Start = Start;
- return ;
+ return;
}
+
InsertNewEntry (
&Entry->Link,
Start,
@@ -306,7 +304,7 @@ ConvertSmmMemoryMapEntry (
FALSE,
AddRegion
);
- return ;
+ return;
}
if ((Entry->Start <= Start) && (Entry->End >= End)) {
@@ -331,6 +329,7 @@ ConvertSmmMemoryMapEntry (
AddRegion
);
}
+
if (Entry->End > End) {
//
// ---------------------------------------------------
@@ -351,12 +350,13 @@ ConvertSmmMemoryMapEntry (
AddRegion
);
}
+
//
// Update this node
//
Entry->Start = Start;
- Entry->End = End;
- Entry->Type = Type;
+ Entry->End = End;
+ Entry->Type = Type;
//
// Check adjacent
@@ -375,6 +375,7 @@ ConvertSmmMemoryMapEntry (
RemoveOldEntry (NextEntry);
}
}
+
PreviousLink = Entry->Link.BackLink;
if (PreviousLink != &gMemoryMap) {
PreviousEntry = CR (PreviousLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
@@ -390,7 +391,8 @@ ConvertSmmMemoryMapEntry (
}
}
}
- return ;
+
+ return;
}
}
@@ -409,9 +411,10 @@ ConvertSmmMemoryMapEntry (
Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
if ((Entry->End + 1 == Start) && (Entry->Type == Type)) {
Entry->End = End;
- return ;
+ return;
}
}
+
InsertNewEntry (
&gMemoryMap,
Start,
@@ -420,7 +423,7 @@ ConvertSmmMemoryMapEntry (
FALSE,
AddRegion
);
- return ;
+ return;
}
/**
@@ -433,20 +436,19 @@ GetSmmMemoryMapEntryCount (
VOID
)
{
- LIST_ENTRY *Link;
- UINTN Count;
+ LIST_ENTRY *Link;
+ UINTN Count;
Count = 0;
- Link = gMemoryMap.ForwardLink;
+ Link = gMemoryMap.ForwardLink;
while (Link != &gMemoryMap) {
- Link = Link->ForwardLink;
+ Link = Link->ForwardLink;
Count++;
}
+
return Count;
}
-
-
/**
Internal Function. Allocate n pages from given free page node.
@@ -472,10 +474,11 @@ InternalAllocPagesOnOneNode (
if (Top > Pages->NumberOfPages) {
Top = Pages->NumberOfPages;
}
+
Bottom = Top - NumberOfPages;
if (Top < Pages->NumberOfPages) {
- Node = (FREE_PAGE_LIST*)((UINTN)Pages + EFI_PAGES_TO_SIZE (Top));
+ Node = (FREE_PAGE_LIST *)((UINTN)Pages + EFI_PAGES_TO_SIZE (Top));
Node->NumberOfPages = Pages->NumberOfPages - Top;
InsertHeadList (&Pages->Link, &Node->Link);
}
@@ -511,11 +514,13 @@ InternalAllocMaxAddress (
for (Node = FreePageList->BackLink; Node != FreePageList; Node = Node->BackLink) {
Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
- if (Pages->NumberOfPages >= NumberOfPages &&
- (UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress) {
+ if ((Pages->NumberOfPages >= NumberOfPages) &&
+ ((UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress))
+ {
return InternalAllocPagesOnOneNode (Pages, NumberOfPages, MaxAddress);
}
}
+
return (UINTN)(-1);
}
@@ -545,15 +550,17 @@ InternalAllocAddress (
}
EndAddress = Address + EFI_PAGES_TO_SIZE (NumberOfPages);
- for (Node = FreePageList->BackLink; Node!= FreePageList; Node = Node->BackLink) {
+ for (Node = FreePageList->BackLink; Node != FreePageList; Node = Node->BackLink) {
Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
if ((UINTN)Pages <= Address) {
if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) < EndAddress) {
break;
}
+
return InternalAllocPagesOnOneNode (Pages, NumberOfPages, EndAddress);
}
}
+
return ~Address;
}
@@ -588,8 +595,9 @@ SmmInternalAllocatePagesEx (
{
UINTN RequestedAddress;
- if (MemoryType != EfiRuntimeServicesCode &&
- MemoryType != EfiRuntimeServicesData) {
+ if ((MemoryType != EfiRuntimeServicesCode) &&
+ (MemoryType != EfiRuntimeServicesData))
+ {
return EFI_INVALID_PARAMETER;
}
@@ -607,11 +615,11 @@ SmmInternalAllocatePagesEx (
case AllocateMaxAddress:
if (NeedGuard) {
*Memory = InternalAllocMaxAddressWithGuard (
- &mSmmMemoryMap,
- NumberOfPages,
- RequestedAddress,
- MemoryType
- );
+ &mSmmMemoryMap,
+ NumberOfPages,
+ RequestedAddress,
+ MemoryType
+ );
if (*Memory == (UINTN)-1) {
return EFI_OUT_OF_RESOURCES;
} else {
@@ -628,6 +636,7 @@ SmmInternalAllocatePagesEx (
if (*Memory == (UINTN)-1) {
return EFI_OUT_OF_RESOURCES;
}
+
break;
case AllocateAddress:
*Memory = InternalAllocAddress (
@@ -638,6 +647,7 @@ SmmInternalAllocatePagesEx (
if (*Memory != RequestedAddress) {
return EFI_NOT_FOUND;
}
+
break;
default:
return EFI_INVALID_PARAMETER;
@@ -648,7 +658,7 @@ SmmInternalAllocatePagesEx (
//
ConvertSmmMemoryMapEntry (MemoryType, *Memory, NumberOfPages, AddRegion);
if (!AddRegion) {
- CoreFreeMemoryMapStack();
+ CoreFreeMemoryMapStack ();
}
return EFI_SUCCESS;
@@ -682,8 +692,14 @@ SmmInternalAllocatePages (
IN BOOLEAN NeedGuard
)
{
- return SmmInternalAllocatePagesEx (Type, MemoryType, NumberOfPages, Memory,
- FALSE, NeedGuard);
+ return SmmInternalAllocatePagesEx (
+ Type,
+ MemoryType,
+ NumberOfPages,
+ Memory,
+ FALSE,
+ NeedGuard
+ );
}
/**
@@ -715,18 +731,24 @@ SmmAllocatePages (
BOOLEAN NeedGuard;
NeedGuard = IsPageTypeToGuard (MemoryType, Type);
- Status = SmmInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory,
- NeedGuard);
+ Status = SmmInternalAllocatePages (
+ Type,
+ MemoryType,
+ NumberOfPages,
+ Memory,
+ NeedGuard
+ );
if (!EFI_ERROR (Status)) {
SmmCoreUpdateProfile (
- (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),
+ (EFI_PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),
MemoryProfileActionAllocatePages,
MemoryType,
EFI_PAGES_TO_SIZE (NumberOfPages),
- (VOID *) (UINTN) *Memory,
+ (VOID *)(UINTN)*Memory,
NULL
);
}
+
return Status;
}
@@ -747,13 +769,15 @@ InternalMergeNodes (
Next = BASE_CR (First->Link.ForwardLink, FREE_PAGE_LIST, Link);
ASSERT (
- TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) >= First->NumberOfPages);
+ TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) >= First->NumberOfPages
+ );
if (TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) == First->NumberOfPages) {
First->NumberOfPages += Next->NumberOfPages;
RemoveEntryList (&Next->Link);
Next = First;
}
+
return Next;
}
@@ -784,17 +808,19 @@ SmmInternalFreePagesEx (
}
Pages = NULL;
- Node = mSmmMemoryMap.ForwardLink;
+ Node = mSmmMemoryMap.ForwardLink;
while (Node != &mSmmMemoryMap) {
Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
if (Memory < (UINTN)Pages) {
break;
}
+
Node = Node->ForwardLink;
}
- if (Node != &mSmmMemoryMap &&
- Memory + EFI_PAGES_TO_SIZE (NumberOfPages) > (UINTN)Pages) {
+ if ((Node != &mSmmMemoryMap) &&
+ (Memory + EFI_PAGES_TO_SIZE (NumberOfPages) > (UINTN)Pages))
+ {
return EFI_INVALID_PARAMETER;
}
@@ -805,7 +831,7 @@ SmmInternalFreePagesEx (
}
}
- Pages = (FREE_PAGE_LIST*)(UINTN)Memory;
+ Pages = (FREE_PAGE_LIST *)(UINTN)Memory;
Pages->NumberOfPages = NumberOfPages;
InsertTailList (Node, &Pages->Link);
@@ -824,7 +850,7 @@ SmmInternalFreePagesEx (
//
ConvertSmmMemoryMapEntry (EfiConventionalMemory, Memory, NumberOfPages, AddRegion);
if (!AddRegion) {
- CoreFreeMemoryMapStack();
+ CoreFreeMemoryMapStack ();
}
return EFI_SUCCESS;
@@ -853,6 +879,7 @@ SmmInternalFreePages (
if (IsGuarded) {
return SmmInternalFreePagesExWithGuard (Memory, NumberOfPages, FALSE);
}
+
return SmmInternalFreePagesEx (Memory, NumberOfPages, FALSE);
}
@@ -872,9 +899,9 @@ InMemMap (
IN UINTN NumberOfPages
)
{
- LIST_ENTRY *Link;
- MEMORY_MAP *Entry;
- EFI_PHYSICAL_ADDRESS Last;
+ LIST_ENTRY *Link;
+ MEMORY_MAP *Entry;
+ EFI_PHYSICAL_ADDRESS Last;
Last = Memory + EFI_PAGES_TO_SIZE (NumberOfPages) - 1;
@@ -912,22 +939,23 @@ SmmFreePages (
EFI_STATUS Status;
BOOLEAN IsGuarded;
- if (!InMemMap(Memory, NumberOfPages)) {
+ if (!InMemMap (Memory, NumberOfPages)) {
return EFI_NOT_FOUND;
}
IsGuarded = IsHeapGuardEnabled () && IsMemoryGuarded (Memory);
- Status = SmmInternalFreePages (Memory, NumberOfPages, IsGuarded);
+ Status = SmmInternalFreePages (Memory, NumberOfPages, IsGuarded);
if (!EFI_ERROR (Status)) {
SmmCoreUpdateProfile (
- (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),
+ (EFI_PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),
MemoryProfileActionFreePages,
EfiMaxMemoryType,
EFI_PAGES_TO_SIZE (NumberOfPages),
- (VOID *) (UINTN) Memory,
+ (VOID *)(UINTN)Memory,
NULL
);
}
+
return Status;
}
@@ -969,7 +997,7 @@ SmmAddMemoryRegion (
// Align range on an EFI_PAGE_SIZE boundary
//
AlignedMemBase = (UINTN)(MemBase + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
- MemLength -= AlignedMemBase - MemBase;
+ MemLength -= AlignedMemBase - MemBase;
if (Type == EfiConventionalMemory) {
SmmInternalFreePagesEx (AlignedMemBase, TRUNCATE_TO_PAGES ((UINTN)MemLength), TRUE);
} else {
@@ -1019,11 +1047,11 @@ SmmCoreGetMemoryMap (
OUT UINT32 *DescriptorVersion
)
{
- UINTN Count;
- LIST_ENTRY *Link;
- MEMORY_MAP *Entry;
- UINTN Size;
- UINTN BufferSize;
+ UINTN Count;
+ LIST_ENTRY *Link;
+ MEMORY_MAP *Entry;
+ UINTN Size;
+ UINTN BufferSize;
Size = sizeof (EFI_MEMORY_DESCRIPTOR);
@@ -1032,7 +1060,7 @@ SmmCoreGetMemoryMap (
// prevent people from having pointer math bugs in their code.
// now you have to use *DescriptorSize to make things work.
//
- Size += sizeof(UINT64) - (Size % sizeof (UINT64));
+ Size += sizeof (UINT64) - (Size % sizeof (UINT64));
if (DescriptorSize != NULL) {
*DescriptorSize = Size;
@@ -1042,7 +1070,7 @@ SmmCoreGetMemoryMap (
*DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;
}
- Count = GetSmmMemoryMapEntryCount ();
+ Count = GetSmmMemoryMapEntryCount ();
BufferSize = Size * Count;
if (*MemoryMapSize < BufferSize) {
*MemoryMapSize = BufferSize;
@@ -1060,9 +1088,9 @@ SmmCoreGetMemoryMap (
Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
Link = Link->ForwardLink;
- MemoryMap->Type = Entry->Type;
- MemoryMap->PhysicalStart = Entry->Start;
- MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);
+ MemoryMap->Type = Entry->Type;
+ MemoryMap->PhysicalStart = Entry->Start;
+ MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);
MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);
}