Ring3: Properly refactored MemoryPoolLib to support User spaces.

This commit is contained in:
Mikhail Krichanov 2025-01-30 15:45:49 +03:00
parent 7ecac413b2
commit c8964a8a73
5 changed files with 41 additions and 19 deletions

View File

@ -259,7 +259,7 @@ Ring3Initialization (
gBS = &mBootServices; gBS = &mBootServices;
gRT = &mRuntimeServices; gRT = &mRuntimeServices;
CoreInitializePool (); CoreInitializePool (FALSE);
return EFI_SUCCESS; return EFI_SUCCESS;
} }

View File

@ -18,7 +18,7 @@
BOOLEAN mOnGuarding = FALSE; BOOLEAN mOnGuarding = FALSE;
STATIC UINTN mMemoryTypes[EfiMaxMemoryType]; STATIC UINTN mMemoryTypes[MAX_MEMORY_TYPE];
STATIC UINTN mNumberOfUsers = 0; STATIC UINTN mNumberOfUsers = 0;
STATIC STATIC
@ -32,18 +32,16 @@ GetMemoryType (
for (Index = 0; Index < mNumberOfUsers; ++Index) { for (Index = 0; Index < mNumberOfUsers; ++Index) {
if (mMemoryTypes[Index] == UserPageTable) { if (mMemoryTypes[Index] == UserPageTable) {
break; return Index;
} }
} }
if (Index == mNumberOfUsers) { ASSERT (mNumberOfUsers < MAX_MEMORY_TYPE);
++mNumberOfUsers;
mMemoryTypes[Index] = UserPageTable;
}
ASSERT (mNumberOfUsers <= EfiMaxMemoryType); mMemoryTypes[mNumberOfUsers] = UserPageTable;
++mNumberOfUsers;
return Index; return (mNumberOfUsers - 1);
} }
STATIC STATIC

View File

@ -2269,7 +2269,7 @@ CoreInitializeMemoryServices (
// Initialize the spin locks and maps in the memory services. // Initialize the spin locks and maps in the memory services.
// Also fill in the memory services into the EFI Boot Services Table // Also fill in the memory services into the EFI Boot Services Table
// //
CoreInitializePool (); CoreInitializePool (TRUE);
// //
// Initialize Local Variables // Initialize Local Variables

View File

@ -36,13 +36,19 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#define GUARD_HEAP_TYPE_ALL \ #define GUARD_HEAP_TYPE_ALL \
(GUARD_HEAP_TYPE_PAGE|GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_FREED) (GUARD_HEAP_TYPE_PAGE|GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_FREED)
#define MAX_MEMORY_TYPE 100
/** /**
Called to initialize the pool. Called to initialize the pool.
@param IsCore Selects between the purposes of mPoolHead array.
In DxeCore each element describes EFI_MEMORY_TYPE.
In DxeRing3 each element describes User space.
**/ **/
VOID VOID
CoreInitializePool ( CoreInitializePool (
VOID IN BOOLEAN IsCore
); );
/** /**

View File

@ -80,7 +80,9 @@ typedef struct {
// //
// Pool header for each memory type. // Pool header for each memory type.
// //
POOL mPoolHead[EfiMaxMemoryType]; POOL mPoolHead[MAX_MEMORY_TYPE];
STATIC BOOLEAN mIsCore;
// //
// List of pool header to search for the appropriate memory type. // List of pool header to search for the appropriate memory type.
@ -185,12 +187,14 @@ GetPoolIndexFromSize (
**/ **/
VOID VOID
CoreInitializePool ( CoreInitializePool (
VOID IN BOOLEAN IsCore
) )
{ {
UINTN Type; UINTN Type;
UINTN Index; UINTN Index;
mIsCore = IsCore;
for (Type = 0; Type < EfiMaxMemoryType; Type++) { for (Type = 0; Type < EfiMaxMemoryType; Type++) {
mPoolHead[Type].Signature = 0; mPoolHead[Type].Signature = 0;
mPoolHead[Type].Used = 0; mPoolHead[Type].Used = 0;
@ -219,6 +223,14 @@ LookupPoolHead (
POOL *Pool; POOL *Pool;
UINTN Index; UINTN Index;
if (!mIsCore) {
if ((UINT32)MemoryType < MAX_MEMORY_TYPE) {
return &mPoolHead[MemoryType];
}
return NULL;
}
if ((UINT32)MemoryType < EfiMaxMemoryType) { if ((UINT32)MemoryType < EfiMaxMemoryType) {
return &mPoolHead[MemoryType]; return &mPoolHead[MemoryType];
} }
@ -285,8 +297,8 @@ CoreInternalAllocatePool (
// //
// If it's not a valid type, fail it // If it's not a valid type, fail it
// //
if (((PoolType >= EfiMaxMemoryType) && (PoolType < MEMORY_TYPE_OEM_RESERVED_MIN)) || if (mIsCore && (((PoolType >= EfiMaxMemoryType) && (PoolType < MEMORY_TYPE_OEM_RESERVED_MIN)) ||
(PoolType == EfiConventionalMemory) || (PoolType == EfiPersistentMemory) || (PoolType == EfiUnacceptedMemoryType)) (PoolType == EfiConventionalMemory) || (PoolType == EfiPersistentMemory) || (PoolType == EfiUnacceptedMemoryType)))
{ {
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
@ -395,10 +407,11 @@ CoreAllocatePoolI (
ASSERT_LOCKED (&mPoolMemoryLock); ASSERT_LOCKED (&mPoolMemoryLock);
if ((PoolType == EfiReservedMemoryType) || if (mIsCore &&
((PoolType == EfiReservedMemoryType) ||
(PoolType == EfiACPIMemoryNVS) || (PoolType == EfiACPIMemoryNVS) ||
(PoolType == EfiRuntimeServicesCode) || (PoolType == EfiRuntimeServicesCode) ||
(PoolType == EfiRuntimeServicesData)) (PoolType == EfiRuntimeServicesData)))
{ {
Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY; Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
} else { } else {
@ -724,10 +737,11 @@ CoreFreePoolI (
Pool->Used -= Size; Pool->Used -= Size;
DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64)Pool->Used)); DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64)Pool->Used));
if ((Head->Type == EfiReservedMemoryType) || if (mIsCore &&
((Head->Type == EfiReservedMemoryType) ||
(Head->Type == EfiACPIMemoryNVS) || (Head->Type == EfiACPIMemoryNVS) ||
(Head->Type == EfiRuntimeServicesCode) || (Head->Type == EfiRuntimeServicesCode) ||
(Head->Type == EfiRuntimeServicesData)) (Head->Type == EfiRuntimeServicesData)))
{ {
Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY; Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
} else { } else {
@ -898,6 +912,10 @@ IsMemoryTypeToGuard (
UINT64 TestBit; UINT64 TestBit;
UINT64 ConfigBit; UINT64 ConfigBit;
if (!mIsCore) {
return FALSE;
}
if (AllocateType == AllocateAddress) { if (AllocateType == AllocateAddress) {
return FALSE; return FALSE;
} }