From 07188c19a85ccba9a558b4c732c50238ac0502f5 Mon Sep 17 00:00:00 2001 From: Mikhail Krichanov Date: Tue, 27 Feb 2024 19:08:41 +0300 Subject: [PATCH] Ring3: Added MemoryPoolLib into DxeRing3. --- MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.c | 10 +- MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.inf | 5 +- .../Core/Dxe/DxeRing3/Ring3UefiBootServices.c | 150 ++++++++++++------ MdeModulePkg/Core/Dxe/Mem/Page.c | 13 +- MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c | 3 - MdeModulePkg/Core/Dxe/SysCall/BootServices.c | 74 +++++---- MdePkg/Include/Uefi/UefiSpec.h | 6 +- 7 files changed, 173 insertions(+), 88 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.c b/MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.c index ec2405c08e..20659b11b3 100644 --- a/MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.c +++ b/MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.c @@ -7,6 +7,8 @@ #include #include +#include +#include #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; } diff --git a/MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.inf b/MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.inf index c2b0c004de..1e29851e32 100644 --- a/MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.inf +++ b/MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.inf @@ -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 diff --git a/MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c b/MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c index 5e26a67fea..2fc863a1e4 100644 --- a/MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c +++ b/MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c @@ -8,18 +8,25 @@ #include +#include + #include #include #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); +} diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/Page.c index b868ae7e31..d3e18b168a 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -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; diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c index 1627b99868..6436566576 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c @@ -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); diff --git a/MdeModulePkg/Core/Dxe/SysCall/BootServices.c b/MdeModulePkg/Core/Dxe/SysCall/BootServices.c index c081e1c0e1..5cb08ce4fa 100644 --- a/MdeModulePkg/Core/Dxe/SysCall/BootServices.c +++ b/MdeModulePkg/Core/Dxe/SysCall/BootServices.c @@ -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 diff --git a/MdePkg/Include/Uefi/UefiSpec.h b/MdePkg/Include/Uefi/UefiSpec.h index eabe704fa1..a5b0fe0525 100644 --- a/MdePkg/Include/Uefi/UefiSpec.h +++ b/MdePkg/Include/Uefi/UefiSpec.h @@ -2018,10 +2018,12 @@ typedef enum { SysCallLocateProtocol, SysCallOpenProtocol, SysCallInstallMultipleProtocolInterfaces, - SysCallAllocatePool, - SysCallFreePool, SysCallCloseProtocol, SysCallHandleProtocol, + SysCallAllocatePages, + SysCallFreePages, + SysCallRaiseTpl, + SysCallRestoreTpl, // // Protocols //