1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/** @file
SMM Memory pool management functions.
Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "PiSmmCore.h"
LIST_ENTRY mSmmPoolLists[SmmPoolTypeMax][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;
/**
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.
@param SmramRangeCount Number of SMRAM Regions
@param SmramRanges Pointer to SMRAM Descriptors
**/
VOID
SmmInitializeMemoryServices (
IN UINTN SmramRangeCount,
IN EFI_SMRAM_DESCRIPTOR *SmramRanges
)
{
UINTN Index;
EFI_STATUS Status;
UINTN SmmPoolTypeIndex;
EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *LMFAConfigurationTable;
//
// Initialize Pool list
//
for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
for (Index = 0; Index < ARRAY_SIZE (mSmmPoolLists[SmmPoolTypeIndex]); Index++) {
InitializeListHead (&mSmmPoolLists[SmmPoolTypeIndex][Index]);
}
}
Status = EfiGetSystemConfigurationTable (
&gLoadFixedAddressConfigurationTableGuid,
(VOID **)&LMFAConfigurationTable
);
if (!EFI_ERROR (Status) && (LMFAConfigurationTable != NULL)) {
gLoadModuleAtFixAddressSmramBase = LMFAConfigurationTable->SmramBase;
}
//
// Add Free SMRAM regions
// Need add Free memory at first, to let gSmmMemoryMap record data
//
for (Index = 0; Index < SmramRangeCount; Index++) {
if ((SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
continue;
}
SmmAddMemoryRegion (
SmramRanges[Index].CpuStart,
SmramRanges[Index].PhysicalSize,
EfiConventionalMemory,
SmramRanges[Index].RegionState
);
}
//
// Add the allocated SMRAM regions
//
for (Index = 0; Index < SmramRangeCount; Index++) {
if ((SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) == 0) {
continue;
}
SmmAddMemoryRegion (
SmramRanges[Index].CpuStart,
SmramRanges[Index].PhysicalSize,
EfiConventionalMemory,
SmramRanges[Index].RegionState
);
}
}
/**
Internal Function. Allocate a pool by specified PoolIndex.
@param PoolType Type of pool to allocate.
@param PoolIndex Index which indicate the Pool size.
@param FreePoolHdr The returned Free pool.
@retval EFI_OUT_OF_RESOURCES Allocation failed.
@retval EFI_SUCCESS Pool successfully allocated.
**/
EFI_STATUS
InternalAllocPoolByIndex (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN PoolIndex,
OUT FREE_POOL_HEADER **FreePoolHdr
)
{
EFI_STATUS Status;
FREE_POOL_HEADER *Hdr;
POOL_TAIL *Tail;
EFI_PHYSICAL_ADDRESS Address;
SMM_POOL_TYPE SmmPoolType;
Address = 0;
SmmPoolType = UefiMemoryTypeToSmmPoolType (PoolType);
ASSERT (PoolIndex <= MAX_POOL_INDEX);
Status = EFI_SUCCESS;
Hdr = NULL;
if (PoolIndex == MAX_POOL_INDEX) {
Status = SmmInternalAllocatePages (
AllocateAnyPages,
PoolType,
EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1),
&Address,
FALSE
);
if (EFI_ERROR (Status)) {
return EFI_OUT_OF_RESOURCES;
}
Hdr = (FREE_POOL_HEADER *)(UINTN)Address;
} else if (!IsListEmpty (&mSmmPoolLists[SmmPoolType][PoolIndex])) {
Hdr = BASE_CR (GetFirstNode (&mSmmPoolLists[SmmPoolType][PoolIndex]), FREE_POOL_HEADER, Link);
RemoveEntryList (&Hdr->Link);
} else {
Status = InternalAllocPoolByIndex (PoolType, PoolIndex + 1, &Hdr);
if (!EFI_ERROR (Status)) {
Hdr->Header.Signature = 0;
Hdr->Header.Size >>= 1;
Hdr->Header.Available = TRUE;
Hdr->Header.Type = 0;
Tail = HEAD_TO_TAIL (&Hdr->Header);
Tail->Signature = 0;
Tail->Size = 0;
InsertHeadList (&mSmmPoolLists[SmmPoolType][PoolIndex], &Hdr->Link);
Hdr = (FREE_POOL_HEADER *)((UINT8 *)Hdr + Hdr->Header.Size);
}
}
if (!EFI_ERROR (Status)) {
Hdr->Header.Signature = POOL_HEAD_SIGNATURE;
Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex;
Hdr->Header.Available = FALSE;
Hdr->Header.Type = PoolType;
Tail = HEAD_TO_TAIL (&Hdr->Header);
Tail->Signature = POOL_TAIL_SIGNATURE;
Tail->Size = Hdr->Header.Size;
}
*FreePoolHdr = Hdr;
return Status;
}
/**
Internal Function. Free a pool by specified PoolIndex.
@param FreePoolHdr The pool to free.
@param PoolTail The pointer to the pool tail.
@retval EFI_SUCCESS Pool successfully freed.
**/
EFI_STATUS
InternalFreePoolByIndex (
IN FREE_POOL_HEADER *FreePoolHdr,
IN POOL_TAIL *PoolTail
)
{
UINTN PoolIndex;
SMM_POOL_TYPE SmmPoolType;
ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0);
ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);
ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);
SmmPoolType = UefiMemoryTypeToSmmPoolType (FreePoolHdr->Header.Type);
PoolIndex = (UINTN)(HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT);
FreePoolHdr->Header.Signature = 0;
FreePoolHdr->Header.Available = TRUE;
FreePoolHdr->Header.Type = 0;
PoolTail->Signature = 0;
PoolTail->Size = 0;
ASSERT (PoolIndex < MAX_POOL_INDEX);
InsertHeadList (&mSmmPoolLists[SmmPoolType][PoolIndex], &FreePoolHdr->Link);
return EFI_SUCCESS;
}
/**
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
SmmInternalAllocatePool (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN Size,
OUT VOID **Buffer
)
{
POOL_HEADER *PoolHdr;
POOL_TAIL *PoolTail;
FREE_POOL_HEADER *FreePoolHdr;
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Address;
UINTN PoolIndex;
BOOLEAN HasPoolTail;
BOOLEAN NeedGuard;
UINTN NoPages;
Address = 0;
if ((PoolType != EfiRuntimeServicesCode) &&
(PoolType != EfiRuntimeServicesData))
{
return EFI_INVALID_PARAMETER;
}
NeedGuard = IsPoolTypeToGuard (PoolType);
HasPoolTail = !(NeedGuard &&
((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0));
//
// Adjust the size by the pool header & tail overhead
//
Size += POOL_OVERHEAD;
if ((Size > MAX_POOL_SIZE) || NeedGuard) {
if (!HasPoolTail) {
Size -= sizeof (POOL_TAIL);
}
NoPages = EFI_SIZE_TO_PAGES (Size);
Status = SmmInternalAllocatePages (
AllocateAnyPages,
PoolType,
NoPages,
&Address,
NeedGuard
);
if (EFI_ERROR (Status)) {
return Status;
}
if (NeedGuard) {
ASSERT (VerifyMemoryGuard (Address, NoPages) == TRUE);
Address = (EFI_PHYSICAL_ADDRESS)(UINTN)AdjustPoolHeadA (
Address,
NoPages,
Size
);
}
PoolHdr = (POOL_HEADER *)(UINTN)Address;
PoolHdr->Signature = POOL_HEAD_SIGNATURE;
PoolHdr->Size = EFI_PAGES_TO_SIZE (NoPages);
PoolHdr->Available = FALSE;
PoolHdr->Type = PoolType;
if (HasPoolTail) {
PoolTail = HEAD_TO_TAIL (PoolHdr);
PoolTail->Signature = POOL_TAIL_SIGNATURE;
PoolTail->Size = PoolHdr->Size;
}
*Buffer = PoolHdr + 1;
return Status;
}
Size = (Size + MIN_POOL_SIZE - 1) >> MIN_POOL_SHIFT;
PoolIndex = (UINTN)HighBitSet32 ((UINT32)Size);
if ((Size & (Size - 1)) != 0) {
PoolIndex++;
}
Status = InternalAllocPoolByIndex (PoolType, PoolIndex, &FreePoolHdr);
if (!EFI_ERROR (Status)) {
*Buffer = &FreePoolHdr->Header + 1;
}
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,
NULL
);
}
return Status;
}
/**
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
SmmInternalFreePool (
IN VOID *Buffer
)
{
FREE_POOL_HEADER *FreePoolHdr;
POOL_TAIL *PoolTail;
BOOLEAN HasPoolTail;
BOOLEAN MemoryGuarded;
if (Buffer == NULL) {
return EFI_INVALID_PARAMETER;
}
FreePoolHdr = (FREE_POOL_HEADER *)((POOL_HEADER *)Buffer - 1);
ASSERT (FreePoolHdr->Header.Signature == POOL_HEAD_SIGNATURE);
ASSERT (!FreePoolHdr->Header.Available);
if (FreePoolHdr->Header.Signature != POOL_HEAD_SIGNATURE) {
return EFI_INVALID_PARAMETER;
}
MemoryGuarded = IsHeapGuardEnabled () &&
IsMemoryGuarded ((EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr);
HasPoolTail = !(MemoryGuarded &&
((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0));
if (HasPoolTail) {
PoolTail = HEAD_TO_TAIL (&FreePoolHdr->Header);
ASSERT (PoolTail->Signature == POOL_TAIL_SIGNATURE);
ASSERT (FreePoolHdr->Header.Size == PoolTail->Size);
if (PoolTail->Signature != POOL_TAIL_SIGNATURE) {
return EFI_INVALID_PARAMETER;
}
if (FreePoolHdr->Header.Size != PoolTail->Size) {
return EFI_INVALID_PARAMETER;
}
} else {
PoolTail = NULL;
}
if (MemoryGuarded) {
Buffer = AdjustPoolHeadF ((EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr);
return SmmInternalFreePages (
(EFI_PHYSICAL_ADDRESS)(UINTN)Buffer,
EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size),
TRUE
);
}
if (FreePoolHdr->Header.Size > MAX_POOL_SIZE) {
ASSERT (((UINTN)FreePoolHdr & EFI_PAGE_MASK) == 0);
ASSERT ((FreePoolHdr->Header.Size & EFI_PAGE_MASK) == 0);
return SmmInternalFreePages (
(EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr,
EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size),
FALSE
);
}
return InternalFreePoolByIndex (FreePoolHdr, PoolTail);
}
/**
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,
EfiMaxMemoryType,
0,
Buffer,
NULL
);
}
return Status;
}
|