Ring3: Added Ring3AllocatePool() and Ring3FreePool().

This commit is contained in:
Mikhail Krichanov 2025-01-28 17:50:18 +03:00
parent 31fef0a12e
commit a615429b73
6 changed files with 114 additions and 10 deletions

View File

@ -26,8 +26,8 @@ EFI_BOOT_SERVICES mBootServices = {
(EFI_ALLOCATE_PAGES)Ring3AllocatePages, // AllocatePages
(EFI_FREE_PAGES)Ring3FreePages, // FreePages
(EFI_GET_MEMORY_MAP)Ring3GetMemoryMap, // GetMemoryMap
(EFI_ALLOCATE_POOL)CoreAllocatePool, // AllocatePool
(EFI_FREE_POOL)CoreFreePool, // FreePool
(EFI_ALLOCATE_POOL)Ring3AllocatePool, // AllocatePool
(EFI_FREE_POOL)Ring3FreePool, // FreePool
(EFI_CREATE_EVENT)Ring3CreateEvent, // CreateEvent
(EFI_SET_TIMER)Ring3SetTimer, // SetTimer
(EFI_WAIT_FOR_EVENT)Ring3WaitForEvent, // WaitForEvent

View File

@ -132,6 +132,44 @@ Ring3GetMemoryMap (
OUT UINT32 *DescriptorVersion
);
/**
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 Buffer is NULL.
PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
PoolType is EfiPersistentMemory.
@retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
@retval EFI_SUCCESS Pool successfully allocated.
**/
EFI_STATUS
EFIAPI
Ring3AllocatePool (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN Size,
OUT VOID **Buffer
);
/**
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
Ring3FreePool (
IN VOID *Buffer
);
/**
Creates an event.

View File

@ -18,6 +18,34 @@
BOOLEAN mOnGuarding = FALSE;
STATIC UINTN mMemoryTypes[EfiMaxMemoryType];
STATIC UINTN mNumberOfUsers = 0;
STATIC
UINTN
EFIAPI
GetMemoryType (
IN UINTN UserPageTable
)
{
UINTN Index;
for (Index = 0; Index < mNumberOfUsers; ++Index) {
if (mMemoryTypes[Index] == UserPageTable) {
break;
}
}
if (Index == mNumberOfUsers) {
++mNumberOfUsers;
mMemoryTypes[Index] = UserPageTable;
}
ASSERT (mNumberOfUsers <= EfiMaxMemoryType);
return Index;
}
STATIC
EFI_STATUS
EFIAPI
@ -179,6 +207,32 @@ Ring3GetMemoryMap (
return EFI_UNSUPPORTED;
}
EFI_STATUS
EFIAPI
Ring3AllocatePool (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN Size,
OUT VOID **Buffer
)
{
UINTN CurrentUser;
CurrentUser = (UINTN)SysCall (SysCallGetUserPageTable, 0);
PoolType = GetMemoryType (CurrentUser);
return CoreAllocatePool (PoolType, Size, Buffer);
}
EFI_STATUS
EFIAPI
Ring3FreePool (
IN VOID *Buffer
)
{
return CoreFreePool (Buffer);
}
EFI_STATUS
EFIAPI
Ring3CreateEvent (
@ -606,7 +660,7 @@ Ring3LocateHandleBuffer (
&& (Buffer != NULL) && (*Buffer != NULL)) {
PoolSize = *NumberHandles * sizeof (EFI_HANDLE *);
Status = CoreAllocatePool (EfiRing3MemoryType, PoolSize, &Pool);
Status = Ring3AllocatePool (EfiRing3MemoryType, PoolSize, &Pool);
if (EFI_ERROR (Status)) {
return Status;
}
@ -672,7 +726,7 @@ Ring3InstallMultipleProtocolInterfaces (
}
VA_END (Marker);
Status = CoreAllocatePool (
Status = Ring3AllocatePool (
EfiRing3MemoryType,
NumberOfArguments * sizeof (VOID *),
(VOID **)&Arguments
@ -695,8 +749,8 @@ Ring3InstallMultipleProtocolInterfaces (
Arguments
);
CoreFreePool (Arguments);
return Status;
Ring3FreePool (Arguments);
return Status;
}
EFI_STATUS

View File

@ -63,6 +63,7 @@ EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiPersistentMemory
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiUnacceptedMemoryType
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiRing3MemoryType
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType
};
@ -86,6 +87,7 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {
{ EfiPalCode, 0 },
{ EfiPersistentMemory, 0 },
{ EfiGcdMemoryTypeUnaccepted, 0 },
{ EfiRing3MemoryType, 0 },
{ EfiMaxMemoryType, 0 }
};
//

View File

@ -317,12 +317,11 @@ FreeUserSpaceDriver (
UserDriver = BASE_CR (Link, USER_SPACE_DRIVER, Link);
if (UserDriver->CoreWrapper == CoreWrapper) {
break;
RemoveEntryList (&UserDriver->Link);
FreePool (UserDriver);
return;
}
}
RemoveEntryList (&UserDriver->Link);
FreePool (UserDriver);
}
EFI_STATUS
@ -1457,6 +1456,13 @@ CallBootService (
FreePool ((VOID *)Argument5);
}
break;
case SysCallGetUserPageTable:
//
// No Arguments
//
Status = (EFI_STATUS)gUserPageTable;
break;
default:
DEBUG ((DEBUG_ERROR, "Ring0: Unknown syscall type.\n"));

View File

@ -2048,6 +2048,10 @@ typedef enum {
SysCallUnicodeStrUpr,
SysCallUnicodeFatToStr,
SysCallUnicodeStrToFat,
//
// Helper functions
//
SysCallGetUserPageTable,
SysCallMax
} SYS_CALL_TYPE;