2 SMM Memory pool management functions.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 LIST_ENTRY mSmmPoolLists
[SmmPoolTypeMax
][MAX_POOL_INDEX
];
13 // To cache the SMRAM base since when Loading modules At fixed address feature is enabled,
14 // all module is assigned an offset relative the SMRAM base in build time.
16 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase
= 0;
19 Convert a UEFI memory type to SMM pool type.
21 @param[in] MemoryType Type of pool to allocate.
26 UefiMemoryTypeToSmmPoolType (
27 IN EFI_MEMORY_TYPE MemoryType
30 ASSERT ((MemoryType
== EfiRuntimeServicesCode
) || (MemoryType
== EfiRuntimeServicesData
));
32 case EfiRuntimeServicesCode
:
33 return SmmPoolTypeCode
;
34 case EfiRuntimeServicesData
:
35 return SmmPoolTypeData
;
37 return SmmPoolTypeMax
;
42 Called to initialize the memory service.
44 @param SmramRangeCount Number of SMRAM Regions
45 @param SmramRanges Pointer to SMRAM Descriptors
49 SmmInitializeMemoryServices (
50 IN UINTN SmramRangeCount
,
51 IN EFI_SMRAM_DESCRIPTOR
*SmramRanges
56 UINTN SmmPoolTypeIndex
;
57 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*LMFAConfigurationTable
;
60 // Initialize Pool list
62 for (SmmPoolTypeIndex
= 0; SmmPoolTypeIndex
< SmmPoolTypeMax
; SmmPoolTypeIndex
++) {
63 for (Index
= 0; Index
< ARRAY_SIZE (mSmmPoolLists
[SmmPoolTypeIndex
]); Index
++) {
64 InitializeListHead (&mSmmPoolLists
[SmmPoolTypeIndex
][Index
]);
68 Status
= EfiGetSystemConfigurationTable (
69 &gLoadFixedAddressConfigurationTableGuid
,
70 (VOID
**)&LMFAConfigurationTable
72 if (!EFI_ERROR (Status
) && (LMFAConfigurationTable
!= NULL
)) {
73 gLoadModuleAtFixAddressSmramBase
= LMFAConfigurationTable
->SmramBase
;
77 // Add Free SMRAM regions
78 // Need add Free memory at first, to let gSmmMemoryMap record data
80 for (Index
= 0; Index
< SmramRangeCount
; Index
++) {
81 if ((SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
86 SmramRanges
[Index
].CpuStart
,
87 SmramRanges
[Index
].PhysicalSize
,
88 EfiConventionalMemory
,
89 SmramRanges
[Index
].RegionState
94 // Add the allocated SMRAM regions
96 for (Index
= 0; Index
< SmramRangeCount
; Index
++) {
97 if ((SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) == 0) {
102 SmramRanges
[Index
].CpuStart
,
103 SmramRanges
[Index
].PhysicalSize
,
104 EfiConventionalMemory
,
105 SmramRanges
[Index
].RegionState
111 Internal Function. Allocate a pool by specified PoolIndex.
113 @param PoolType Type of pool to allocate.
114 @param PoolIndex Index which indicate the Pool size.
115 @param FreePoolHdr The returned Free pool.
117 @retval EFI_OUT_OF_RESOURCES Allocation failed.
118 @retval EFI_SUCCESS Pool successfully allocated.
122 InternalAllocPoolByIndex (
123 IN EFI_MEMORY_TYPE PoolType
,
125 OUT FREE_POOL_HEADER
**FreePoolHdr
129 FREE_POOL_HEADER
*Hdr
;
131 EFI_PHYSICAL_ADDRESS Address
;
132 SMM_POOL_TYPE SmmPoolType
;
135 SmmPoolType
= UefiMemoryTypeToSmmPoolType (PoolType
);
137 ASSERT (PoolIndex
<= MAX_POOL_INDEX
);
138 Status
= EFI_SUCCESS
;
140 if (PoolIndex
== MAX_POOL_INDEX
) {
141 Status
= SmmInternalAllocatePages (
144 EFI_SIZE_TO_PAGES (MAX_POOL_SIZE
<< 1),
148 if (EFI_ERROR (Status
)) {
149 return EFI_OUT_OF_RESOURCES
;
152 Hdr
= (FREE_POOL_HEADER
*)(UINTN
)Address
;
153 } else if (!IsListEmpty (&mSmmPoolLists
[SmmPoolType
][PoolIndex
])) {
154 Hdr
= BASE_CR (GetFirstNode (&mSmmPoolLists
[SmmPoolType
][PoolIndex
]), FREE_POOL_HEADER
, Link
);
155 RemoveEntryList (&Hdr
->Link
);
157 Status
= InternalAllocPoolByIndex (PoolType
, PoolIndex
+ 1, &Hdr
);
158 if (!EFI_ERROR (Status
)) {
159 Hdr
->Header
.Signature
= 0;
160 Hdr
->Header
.Size
>>= 1;
161 Hdr
->Header
.Available
= TRUE
;
162 Hdr
->Header
.Type
= 0;
163 Tail
= HEAD_TO_TAIL (&Hdr
->Header
);
166 InsertHeadList (&mSmmPoolLists
[SmmPoolType
][PoolIndex
], &Hdr
->Link
);
167 Hdr
= (FREE_POOL_HEADER
*)((UINT8
*)Hdr
+ Hdr
->Header
.Size
);
171 if (!EFI_ERROR (Status
)) {
172 Hdr
->Header
.Signature
= POOL_HEAD_SIGNATURE
;
173 Hdr
->Header
.Size
= MIN_POOL_SIZE
<< PoolIndex
;
174 Hdr
->Header
.Available
= FALSE
;
175 Hdr
->Header
.Type
= PoolType
;
176 Tail
= HEAD_TO_TAIL (&Hdr
->Header
);
177 Tail
->Signature
= POOL_TAIL_SIGNATURE
;
178 Tail
->Size
= Hdr
->Header
.Size
;
186 Internal Function. Free a pool by specified PoolIndex.
188 @param FreePoolHdr The pool to free.
189 @param PoolTail The pointer to the pool tail.
191 @retval EFI_SUCCESS Pool successfully freed.
195 InternalFreePoolByIndex (
196 IN FREE_POOL_HEADER
*FreePoolHdr
,
197 IN POOL_TAIL
*PoolTail
201 SMM_POOL_TYPE SmmPoolType
;
203 ASSERT ((FreePoolHdr
->Header
.Size
& (FreePoolHdr
->Header
.Size
- 1)) == 0);
204 ASSERT (((UINTN
)FreePoolHdr
& (FreePoolHdr
->Header
.Size
- 1)) == 0);
205 ASSERT (FreePoolHdr
->Header
.Size
>= MIN_POOL_SIZE
);
207 SmmPoolType
= UefiMemoryTypeToSmmPoolType (FreePoolHdr
->Header
.Type
);
209 PoolIndex
= (UINTN
)(HighBitSet32 ((UINT32
)FreePoolHdr
->Header
.Size
) - MIN_POOL_SHIFT
);
210 FreePoolHdr
->Header
.Signature
= 0;
211 FreePoolHdr
->Header
.Available
= TRUE
;
212 FreePoolHdr
->Header
.Type
= 0;
213 PoolTail
->Signature
= 0;
215 ASSERT (PoolIndex
< MAX_POOL_INDEX
);
216 InsertHeadList (&mSmmPoolLists
[SmmPoolType
][PoolIndex
], &FreePoolHdr
->Link
);
221 Allocate pool of a particular type.
223 @param PoolType Type of pool to allocate.
224 @param Size The amount of pool to allocate.
225 @param Buffer The address to return a pointer to the allocated
228 @retval EFI_INVALID_PARAMETER PoolType not valid.
229 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
230 @retval EFI_SUCCESS Pool successfully allocated.
235 SmmInternalAllocatePool (
236 IN EFI_MEMORY_TYPE PoolType
,
241 POOL_HEADER
*PoolHdr
;
243 FREE_POOL_HEADER
*FreePoolHdr
;
245 EFI_PHYSICAL_ADDRESS Address
;
253 if ((PoolType
!= EfiRuntimeServicesCode
) &&
254 (PoolType
!= EfiRuntimeServicesData
))
256 return EFI_INVALID_PARAMETER
;
259 NeedGuard
= IsPoolTypeToGuard (PoolType
);
260 HasPoolTail
= !(NeedGuard
&&
261 ((PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) == 0));
264 // Adjust the size by the pool header & tail overhead
266 Size
+= POOL_OVERHEAD
;
267 if ((Size
> MAX_POOL_SIZE
) || NeedGuard
) {
269 Size
-= sizeof (POOL_TAIL
);
272 NoPages
= EFI_SIZE_TO_PAGES (Size
);
273 Status
= SmmInternalAllocatePages (
280 if (EFI_ERROR (Status
)) {
285 ASSERT (VerifyMemoryGuard (Address
, NoPages
) == TRUE
);
286 Address
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AdjustPoolHeadA (
293 PoolHdr
= (POOL_HEADER
*)(UINTN
)Address
;
294 PoolHdr
->Signature
= POOL_HEAD_SIGNATURE
;
295 PoolHdr
->Size
= EFI_PAGES_TO_SIZE (NoPages
);
296 PoolHdr
->Available
= FALSE
;
297 PoolHdr
->Type
= PoolType
;
300 PoolTail
= HEAD_TO_TAIL (PoolHdr
);
301 PoolTail
->Signature
= POOL_TAIL_SIGNATURE
;
302 PoolTail
->Size
= PoolHdr
->Size
;
305 *Buffer
= PoolHdr
+ 1;
309 Size
= (Size
+ MIN_POOL_SIZE
- 1) >> MIN_POOL_SHIFT
;
310 PoolIndex
= (UINTN
)HighBitSet32 ((UINT32
)Size
);
311 if ((Size
& (Size
- 1)) != 0) {
315 Status
= InternalAllocPoolByIndex (PoolType
, PoolIndex
, &FreePoolHdr
);
316 if (!EFI_ERROR (Status
)) {
317 *Buffer
= &FreePoolHdr
->Header
+ 1;
324 Allocate pool of a particular type.
326 @param PoolType Type of pool to allocate.
327 @param Size The amount of pool to allocate.
328 @param Buffer The address to return a pointer to the allocated
331 @retval EFI_INVALID_PARAMETER PoolType not valid.
332 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
333 @retval EFI_SUCCESS Pool successfully allocated.
339 IN EFI_MEMORY_TYPE PoolType
,
346 Status
= SmmInternalAllocatePool (PoolType
, Size
, Buffer
);
347 if (!EFI_ERROR (Status
)) {
348 SmmCoreUpdateProfile (
349 (EFI_PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
350 MemoryProfileActionAllocatePool
,
364 @param Buffer The allocated pool entry to free.
366 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
367 @retval EFI_SUCCESS Pool successfully freed.
372 SmmInternalFreePool (
376 FREE_POOL_HEADER
*FreePoolHdr
;
379 BOOLEAN MemoryGuarded
;
381 if (Buffer
== NULL
) {
382 return EFI_INVALID_PARAMETER
;
385 FreePoolHdr
= (FREE_POOL_HEADER
*)((POOL_HEADER
*)Buffer
- 1);
386 ASSERT (FreePoolHdr
->Header
.Signature
== POOL_HEAD_SIGNATURE
);
387 ASSERT (!FreePoolHdr
->Header
.Available
);
388 if (FreePoolHdr
->Header
.Signature
!= POOL_HEAD_SIGNATURE
) {
389 return EFI_INVALID_PARAMETER
;
392 MemoryGuarded
= IsHeapGuardEnabled () &&
393 IsMemoryGuarded ((EFI_PHYSICAL_ADDRESS
)(UINTN
)FreePoolHdr
);
394 HasPoolTail
= !(MemoryGuarded
&&
395 ((PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) == 0));
398 PoolTail
= HEAD_TO_TAIL (&FreePoolHdr
->Header
);
399 ASSERT (PoolTail
->Signature
== POOL_TAIL_SIGNATURE
);
400 ASSERT (FreePoolHdr
->Header
.Size
== PoolTail
->Size
);
401 if (PoolTail
->Signature
!= POOL_TAIL_SIGNATURE
) {
402 return EFI_INVALID_PARAMETER
;
405 if (FreePoolHdr
->Header
.Size
!= PoolTail
->Size
) {
406 return EFI_INVALID_PARAMETER
;
413 Buffer
= AdjustPoolHeadF ((EFI_PHYSICAL_ADDRESS
)(UINTN
)FreePoolHdr
);
414 return SmmInternalFreePages (
415 (EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
,
416 EFI_SIZE_TO_PAGES (FreePoolHdr
->Header
.Size
),
421 if (FreePoolHdr
->Header
.Size
> MAX_POOL_SIZE
) {
422 ASSERT (((UINTN
)FreePoolHdr
& EFI_PAGE_MASK
) == 0);
423 ASSERT ((FreePoolHdr
->Header
.Size
& EFI_PAGE_MASK
) == 0);
424 return SmmInternalFreePages (
425 (EFI_PHYSICAL_ADDRESS
)(UINTN
)FreePoolHdr
,
426 EFI_SIZE_TO_PAGES (FreePoolHdr
->Header
.Size
),
431 return InternalFreePoolByIndex (FreePoolHdr
, PoolTail
);
437 @param Buffer The allocated pool entry to free.
439 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
440 @retval EFI_SUCCESS Pool successfully freed.
451 Status
= SmmInternalFreePool (Buffer
);
452 if (!EFI_ERROR (Status
)) {
453 SmmCoreUpdateProfile (
454 (EFI_PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
455 MemoryProfileActionFreePool
,