Ring3: Added MemoryPoolLib into DxeRing3.

This commit is contained in:
Mikhail Krichanov 2024-02-27 19:08:41 +03:00
parent 68dce7677d
commit 07188c19a8
7 changed files with 173 additions and 88 deletions

View File

@ -7,6 +7,8 @@
#include <Uefi.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryPoolLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include "Ring3.h"
@ -23,8 +25,8 @@ EFI_BOOT_SERVICES mBootServices = {
(EFI_ALLOCATE_PAGES)Ring3AllocatePages, // AllocatePages
(EFI_FREE_PAGES)Ring3FreePages, // FreePages
(EFI_GET_MEMORY_MAP)Ring3GetMemoryMap, // GetMemoryMap
(EFI_ALLOCATE_POOL)Ring3AllocatePool, // AllocatePool
(EFI_FREE_POOL)Ring3FreePool, // FreePool
(EFI_ALLOCATE_POOL)CoreAllocatePool, // AllocatePool
(EFI_FREE_POOL)CoreFreePool, // FreePool
(EFI_CREATE_EVENT)Ring3CreateEvent, // CreateEvent
(EFI_SET_TIMER)Ring3SetTimer, // SetTimer
(EFI_WAIT_FOR_EVENT)Ring3WaitForEvent, // WaitForEvent
@ -177,5 +179,9 @@ Ring3Initialization (
Ring3Data->EntryPoint = (VOID *)Ring3EntryPoint;
Ring3Data->BootServices = &mBootServices;
gBS = &mBootServices;
CoreInitializePool ();
return EFI_SUCCESS;
}

View File

@ -32,13 +32,16 @@
[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
[LibraryClasses]
BaseLib
BaseMemoryLib
DebugLib
MemoryPoolLib
UefiBootServicesTableLib
UefiDriverEntryPoint
[Protocols]
gEfiDevicePathUtilitiesProtocolGuid ## SOMETIMES_CONSUMES
gEfiLoadedImageProtocolGuid ## SOMETIMES_CONSUMES

View File

@ -8,18 +8,25 @@
#include <Uefi.h>
#include <Guid/MemoryProfile.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include "Ring3.h"
BOOLEAN mOnGuarding = FALSE;
EFI_TPL
EFIAPI
Ring3RaiseTpl (
IN EFI_TPL NewTpl
)
{
return NewTpl;
return (EFI_TPL)SysCall (
SysCallRaiseTpl,
NewTpl
);
}
VOID
@ -28,7 +35,10 @@ Ring3RestoreTpl (
IN EFI_TPL NewTpl
)
{
SysCall (
SysCallRestoreTpl,
NewTpl
);
}
EFI_STATUS
@ -40,7 +50,20 @@ Ring3AllocatePages (
IN OUT EFI_PHYSICAL_ADDRESS *Memory
)
{
return EFI_UNSUPPORTED;
EFI_STATUS Status;
Status = SysCall (
SysCallAllocatePages,
Type,
MemoryType,
NumberOfPages,
Memory
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Ring3: Failed to allocate %d pages.\n", NumberOfPages));
}
return Status;
}
EFI_STATUS
@ -50,7 +73,18 @@ Ring3FreePages (
IN UINTN NumberOfPages
)
{
return EFI_UNSUPPORTED;
EFI_STATUS Status;
Status = SysCall (
SysCallFreePages,
Memory,
NumberOfPages
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Ring3: Failed to free %d pages.\n", NumberOfPages));
}
return Status;
}
EFI_STATUS
@ -66,48 +100,6 @@ Ring3GetMemoryMap (
return EFI_UNSUPPORTED;
}
EFI_STATUS
EFIAPI
Ring3AllocatePool (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN Size,
OUT VOID **Buffer
)
{
EFI_STATUS Status;
Status = SysCall (
SysCallAllocatePool,
PoolType,
Size,
Buffer
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Ring3: Failed to allocate %d bytes.\n", Size));
}
return Status;
}
EFI_STATUS
EFIAPI
Ring3FreePool (
IN VOID *Buffer
)
{
EFI_STATUS Status;
Status = SysCall (
SysCallFreePool,
Buffer
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Ring3: Failed to free buffer.\n"));
}
return Status;
}
EFI_STATUS
EFIAPI
Ring3CreateEvent (
@ -614,3 +606,69 @@ Ring3CreateEventEx (
{
return EFI_UNSUPPORTED;
}
EFI_STATUS
EFIAPI
CoreUpdateProfile (
IN EFI_PHYSICAL_ADDRESS CallerAddress,
IN MEMORY_PROFILE_ACTION Action,
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Size, // Valid for AllocatePages/FreePages/AllocatePool
IN VOID *Buffer,
IN CHAR8 *ActionString OPTIONAL
)
{
return EFI_SUCCESS;
}
VOID
InstallMemoryAttributesTableOnMemoryAllocation (
IN EFI_MEMORY_TYPE MemoryType
)
{
return;
}
BOOLEAN
EFIAPI
IsMemoryGuarded (
IN EFI_PHYSICAL_ADDRESS Address
)
{
return FALSE;
}
VOID *
CoreAllocatePoolPagesI (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN NoPages,
IN UINTN Granularity,
IN BOOLEAN NeedGuard
)
{
EFI_PHYSICAL_ADDRESS Memory;
Ring3AllocatePages (AllocateAnyPages, EfiRing3MemoryType, NoPages, &Memory);
return (VOID *)Memory;
}
VOID
CoreFreePoolPagesI (
IN EFI_MEMORY_TYPE PoolType,
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN NoPages
)
{
Ring3FreePages (Memory, NoPages);
}
VOID
CoreFreePoolPagesWithGuard (
IN EFI_MEMORY_TYPE PoolType,
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN NoPages
)
{
CoreFreePoolPagesI (PoolType, Memory, NoPages);
}

View File

@ -1735,6 +1735,13 @@ CoreFreePages (
EFI_STATUS Status;
EFI_MEMORY_TYPE MemoryType;
ApplyMemoryProtectionPolicy (
EfiMaxMemoryType,
EfiConventionalMemory,
Memory,
EFI_PAGES_TO_SIZE (NumberOfPages)
);
Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);
if (!EFI_ERROR (Status)) {
GuardFreedPagesChecked (Memory, NumberOfPages);
@ -1747,12 +1754,6 @@ CoreFreePages (
NULL
);
InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);
ApplyMemoryProtectionPolicy (
MemoryType,
EfiConventionalMemory,
Memory,
EFI_PAGES_TO_SIZE (NumberOfPages)
);
}
return Status;

View File

@ -950,9 +950,6 @@ ApplyMemoryProtectionPolicy (
// policy is the same between OldType and NewType
return EFI_SUCCESS;
}
} else if (NewAttributes == 0) {
// newly added region of a type that does not require protection
return EFI_SUCCESS;
}
return gCpu->SetMemoryAttributes (gCpu, Memory, Length, NewAttributes);

View File

@ -291,34 +291,6 @@ CallBootService (
return Status;
case SysCallAllocatePool:
//
// Argument 1: EFI_MEMORY_TYPE PoolType
// Argument 2: UINTN Size
// Argument 3: VOID **Buffer
//
DisableSMAP ();
Status = gBS->AllocatePool (
EfiRing3MemoryType,
CoreRbp->Argument2,
(VOID **)CoreRbp->Argument3
);
EnableSMAP ();
return Status;
case SysCallFreePool:
//
// Argument 1: VOID *Buffer
//
DisableSMAP ();
Status = gBS->FreePool (
(VOID *)CoreRbp->Argument1
);
EnableSMAP ();
return Status;
case SysCallCloseProtocol:
//
// Argument 1: EFI_HANDLE CoreUserHandle
@ -379,6 +351,52 @@ CallBootService (
return Status;
case SysCallAllocatePages:
//
// Argument 1: EFI_ALLOCATE_TYPE Type
// Argument 2: EFI_MEMORY_TYPE MemoryType
// Argument 3: UINTN NumberOfPages
// Argument 4: EFI_PHYSICAL_ADDRESS *Memory
//
Status = gBS->AllocatePages (
(EFI_ALLOCATE_TYPE)CoreRbp->Argument1,
(EFI_MEMORY_TYPE)CoreRbp->Argument2,
CoreRbp->Argument3,
(EFI_PHYSICAL_ADDRESS *)&Argument4
);
DisableSMAP ();
*(EFI_PHYSICAL_ADDRESS *)UserRsp->Arguments[4] = (EFI_PHYSICAL_ADDRESS)Argument4;
EnableSMAP ();
return Status;
case SysCallFreePages:
//
// Argument 1: EFI_PHYSICAL_ADDRESS Memory
// Argument 2: UINTN NumberOfPages
//
return gBS->FreePages (
(EFI_PHYSICAL_ADDRESS)CoreRbp->Argument1,
CoreRbp->Argument2
);
case SysCallRaiseTpl:
//
// Argument 1: EFI_TPL NewTpl
//
return (EFI_STATUS)gBS->RaiseTPL (
(EFI_TPL)CoreRbp->Argument1
);
case SysCallRestoreTpl:
//
// Argument 1: EFI_TPL NewTpl
//
gBS->RestoreTPL ((EFI_TPL)CoreRbp->Argument1);
return EFI_SUCCESS;
case SysCallBlockIoReset:
//
// Argument 1: EFI_BLOCK_IO_PROTOCOL *This

View File

@ -2018,10 +2018,12 @@ typedef enum {
SysCallLocateProtocol,
SysCallOpenProtocol,
SysCallInstallMultipleProtocolInterfaces,
SysCallAllocatePool,
SysCallFreePool,
SysCallCloseProtocol,
SysCallHandleProtocol,
SysCallAllocatePages,
SysCallFreePages,
SysCallRaiseTpl,
SysCallRestoreTpl,
//
// Protocols
//