Skip to content

Commit

Permalink
Ring3: Properly refactored MemoryPoolLib to support User spaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Jan 30, 2025
1 parent 9216e41 commit b6a7e2b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ Ring3Initialization (
gBS = &mBootServices;
gRT = &mRuntimeServices;

CoreInitializePool ();
CoreInitializePool (FALSE);

return EFI_SUCCESS;
}
14 changes: 6 additions & 8 deletions MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

BOOLEAN mOnGuarding = FALSE;

STATIC UINTN mMemoryTypes[EfiMaxMemoryType];
STATIC UINTN mMemoryTypes[MAX_MEMORY_TYPE];
STATIC UINTN mNumberOfUsers = 0;

STATIC
Expand All @@ -32,18 +32,16 @@ GetMemoryType (

for (Index = 0; Index < mNumberOfUsers; ++Index) {
if (mMemoryTypes[Index] == UserPageTable) {
break;
return Index;
}
}

if (Index == mNumberOfUsers) {
++mNumberOfUsers;
mMemoryTypes[Index] = UserPageTable;
}
ASSERT (mNumberOfUsers < MAX_MEMORY_TYPE);

ASSERT (mNumberOfUsers <= EfiMaxMemoryType);
mMemoryTypes[mNumberOfUsers] = UserPageTable;
++mNumberOfUsers;

return Index;
return (mNumberOfUsers - 1);
}

STATIC
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Core/Dxe/Gcd/Gcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,7 @@ CoreInitializeMemoryServices (
// Initialize the spin locks and maps in the memory services.
// Also fill in the memory services into the EFI Boot Services Table
//
CoreInitializePool ();
CoreInitializePool (TRUE);

//
// Initialize Local Variables
Expand Down
8 changes: 7 additions & 1 deletion MdeModulePkg/Include/Library/MemoryPoolLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#define GUARD_HEAP_TYPE_ALL \
(GUARD_HEAP_TYPE_PAGE|GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_FREED)

#define MAX_MEMORY_TYPE 100

/**
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
CoreInitializePool (
VOID
IN BOOLEAN IsCore
);

/**
Expand Down
34 changes: 26 additions & 8 deletions MdeModulePkg/Library/MemoryPoolLib/Pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ typedef struct {
//
// 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.
Expand Down Expand Up @@ -185,12 +187,14 @@ GetPoolIndexFromSize (
**/
VOID
CoreInitializePool (
VOID
IN BOOLEAN IsCore
)
{
UINTN Type;
UINTN Index;

mIsCore = IsCore;

for (Type = 0; Type < EfiMaxMemoryType; Type++) {
mPoolHead[Type].Signature = 0;
mPoolHead[Type].Used = 0;
Expand Down Expand Up @@ -219,6 +223,14 @@ LookupPoolHead (
POOL *Pool;
UINTN Index;

if (!mIsCore) {
if ((UINT32)MemoryType < MAX_MEMORY_TYPE) {
return &mPoolHead[MemoryType];
}

return NULL;
}

if ((UINT32)MemoryType < EfiMaxMemoryType) {
return &mPoolHead[MemoryType];
}
Expand Down Expand Up @@ -285,8 +297,8 @@ CoreInternalAllocatePool (
//
// If it's not a valid type, fail it
//
if (((PoolType >= EfiMaxMemoryType) && (PoolType < MEMORY_TYPE_OEM_RESERVED_MIN)) ||
(PoolType == EfiConventionalMemory) || (PoolType == EfiPersistentMemory) || (PoolType == EfiUnacceptedMemoryType))
if (mIsCore && (((PoolType >= EfiMaxMemoryType) && (PoolType < MEMORY_TYPE_OEM_RESERVED_MIN)) ||
(PoolType == EfiConventionalMemory) || (PoolType == EfiPersistentMemory) || (PoolType == EfiUnacceptedMemoryType)))
{
return EFI_INVALID_PARAMETER;
}
Expand Down Expand Up @@ -395,10 +407,11 @@ CoreAllocatePoolI (

ASSERT_LOCKED (&mPoolMemoryLock);

if ((PoolType == EfiReservedMemoryType) ||
if (mIsCore &&
((PoolType == EfiReservedMemoryType) ||
(PoolType == EfiACPIMemoryNVS) ||
(PoolType == EfiRuntimeServicesCode) ||
(PoolType == EfiRuntimeServicesData))
(PoolType == EfiRuntimeServicesData)))
{
Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
} else {
Expand Down Expand Up @@ -724,10 +737,11 @@ CoreFreePoolI (
Pool->Used -= Size;
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 == EfiRuntimeServicesCode) ||
(Head->Type == EfiRuntimeServicesData))
(Head->Type == EfiRuntimeServicesData)))
{
Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
} else {
Expand Down Expand Up @@ -898,6 +912,10 @@ IsMemoryTypeToGuard (
UINT64 TestBit;
UINT64 ConfigBit;

if (!mIsCore) {
return FALSE;
}

if (AllocateType == AllocateAddress) {
return FALSE;
}
Expand Down

0 comments on commit b6a7e2b

Please sign in to comment.