diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMain.h index f298c90baa..c825cb3e92 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.h +++ b/MdeModulePkg/Core/Dxe/DxeMain.h @@ -1173,11 +1173,11 @@ CoreAllocatePages ( IN OUT EFI_PHYSICAL_ADDRESS *Memory ); -EFI_STATUS +VOID * EFIAPI -AllocateRing3Pages ( - IN UINTN NumberOfPages, - IN OUT VOID **Memory +AllocateRing3CopyPages ( + IN VOID *MemoryCore, + IN UINT32 MemoryCoreSize ); /** diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeMain.inf index 7a6332a79c..914b08d1ce 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -162,6 +162,7 @@ gEfiHiiPackageListProtocolGuid ## SOMETIMES_PRODUCES gEfiSmmBase2ProtocolGuid ## SOMETIMES_CONSUMES gEdkiiPeCoffImageEmulatorProtocolGuid ## SOMETIMES_CONSUMES + gEfiDevicePathUtilitiesProtocolGuid ## SOMETIMES_CONSUMES # Arch Protocols gEfiBdsArchProtocolGuid ## CONSUMES diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c index 77bc48a286..376f0f056c 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c +++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c @@ -89,8 +89,7 @@ EFI_BOOT_SERVICES mBootServices = { (EFI_CALCULATE_CRC32)CoreEfiNotAvailableYetArg3, // CalculateCrc32 (EFI_COPY_MEM)CopyMem, // CopyMem (EFI_SET_MEM)SetMem, // SetMem - (EFI_CREATE_EVENT_EX)CoreCreateEventEx, // CreateEventEx - (EFI_ALLOCATE_RING3_PAGES)AllocateRing3Pages + (EFI_CREATE_EVENT_EX)CoreCreateEventEx // CreateEventEx }; EFI_DXE_SERVICES mDxeServices = { diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Image/Image.c index bcd07124ec..2cbcee6545 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -1565,25 +1565,25 @@ CoreLoadImage ( return Status; } -EFI_STATUS +VOID * EFIAPI -AllocateRing3Pages ( - IN UINTN NumberOfPages, - IN OUT VOID **Memory +AllocateRing3CopyPages ( + IN VOID *MemoryCore, + IN UINT32 MemoryCoreSize ) { - if (Memory == NULL) { - return EFI_INVALID_PARAMETER; + VOID *MemoryRing3; + + MemoryRing3 = AllocatePages (EFI_SIZE_TO_PAGES (MemoryCoreSize)); + if (MemoryRing3 == NULL) { + return NULL; } - *Memory = AllocatePages (NumberOfPages); - if (*Memory == NULL) { - return EFI_OUT_OF_RESOURCES; - } + CopyMem (MemoryRing3, MemoryCore, MemoryCoreSize); - SetUefiImageMemoryAttributes ((UINTN)*Memory, EFI_PAGES_TO_SIZE (NumberOfPages), EFI_MEMORY_USER); + SetUefiImageMemoryAttributes ((UINTN)MemoryRing3, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (MemoryCoreSize)), EFI_MEMORY_USER); - return EFI_SUCCESS; + return MemoryRing3; } /** diff --git a/MdeModulePkg/Core/Dxe/SysCall/BootServices.c b/MdeModulePkg/Core/Dxe/SysCall/BootServices.c index 915d14f493..98b3ab6662 100644 --- a/MdeModulePkg/Core/Dxe/SysCall/BootServices.c +++ b/MdeModulePkg/Core/Dxe/SysCall/BootServices.c @@ -5,11 +5,9 @@ **/ -#include +#include "DxeMain.h" -#include -#include -#include +#include VOID EFIAPI @@ -50,6 +48,7 @@ CallBootService ( UINT32 Arg6; EFI_GUID *CoreProtocol; + UINT32 MemoryCoreSize; // Stack: // rcx - Rip for SYSCALL @@ -59,13 +58,6 @@ CallBootService ( // r11 - User data segment selector <- CoreRbp // rsp - User Rsp switch (Type) { - case SysCallAllocateRing3Pages: - Status = gBS->AllocateRing3Pages (*((UINTN *)CoreRbp + 3), &Pointer); - DisableSMAP (); - *(UINTN *)(*((UINTN *)CoreRbp + 1)) = (UINTN)Pointer; - EnableSMAP (); - return (UINTN)Status; - case SysCallLocateProtocol: DisableSMAP (); CoreProtocol = AllocateCopyPool (sizeof (EFI_GUID), (VOID *)*((UINTN *)CoreRbp + 3)); @@ -81,10 +73,25 @@ CallBootService ( &Pointer ); - FreePool (CoreProtocol); + if (CompareGuid (CoreProtocol, &gEfiDevicePathUtilitiesProtocolGuid)) { + MemoryCoreSize = sizeof (EFI_DEVICE_PATH_UTILITIES_PROTOCOL); + } else { + MemoryCoreSize = 0; + } + + Pointer = AllocateRing3CopyPages (Pointer, MemoryCoreSize); + if (Pointer == NULL) { + DEBUG ((DEBUG_ERROR, "Ring0: Failed to allocate pages for Ring3 PROTOCOL structure.\n")); + FreePool (CoreProtocol); + return EFI_OUT_OF_RESOURCES; + } + DisableSMAP (); - *((UINTN *)UserRsp + 5) = (UINTN)Pointer; + *(UINTN *)(*((UINTN *)UserRsp + 5)) = (UINTN)Pointer; EnableSMAP (); + + FreePool (CoreProtocol); + return (UINTN)Status; case SysCallOpenProtocol: @@ -108,11 +115,27 @@ CallBootService ( Arg6 ); - FreePool (CoreProtocol); + if (CompareGuid (CoreProtocol, &gEfiLoadedImageProtocolGuid)) { + MemoryCoreSize = sizeof (EFI_LOADED_IMAGE_PROTOCOL); + } else { + MemoryCoreSize = 0; + } + + Pointer = AllocateRing3CopyPages (Pointer, MemoryCoreSize); + if (Pointer == NULL) { + DEBUG ((DEBUG_ERROR, "Ring0: Failed to allocate pages for Ring3 PROTOCOL structure.\n")); + FreePool (CoreProtocol); + return EFI_OUT_OF_RESOURCES; + } + DisableSMAP (); - *((UINTN *)UserRsp + 5) = (UINTN)Pointer; + *(UINTN *)(*((UINTN *)UserRsp + 5)) = (UINTN)Pointer; EnableSMAP (); + + FreePool (CoreProtocol); + return (UINTN)Status; + default: break; } diff --git a/MdePkg/Include/Uefi/UefiSpec.h b/MdePkg/Include/Uefi/UefiSpec.h index 084c29f812..bd795b7b9a 100644 --- a/MdePkg/Include/Uefi/UefiSpec.h +++ b/MdePkg/Include/Uefi/UefiSpec.h @@ -213,13 +213,6 @@ EFI_STATUS IN OUT EFI_PHYSICAL_ADDRESS *Memory ); -typedef -EFI_STATUS -(EFIAPI *EFI_ALLOCATE_RING3_PAGES)( - IN UINTN Pages, - IN OUT VOID **Memory - ); - /** Frees memory pages. @@ -2018,13 +2011,11 @@ typedef struct { EFI_COPY_MEM CopyMem; EFI_SET_MEM SetMem; EFI_CREATE_EVENT_EX CreateEventEx; - EFI_ALLOCATE_RING3_PAGES AllocateRing3Pages; } EFI_BOOT_SERVICES; typedef enum { SysCallLocateProtocol = 1, SysCallOpenProtocol = 2, - SysCallAllocateRing3Pages = 3, SysCallMax } SYS_CALL_TYPE; diff --git a/MdePkg/Library/Ring3UefiBootServicesTableLib/Ring3UefiBootServicesTableLib.c b/MdePkg/Library/Ring3UefiBootServicesTableLib/Ring3UefiBootServicesTableLib.c index b764b51080..cfd204559b 100644 --- a/MdePkg/Library/Ring3UefiBootServicesTableLib/Ring3UefiBootServicesTableLib.c +++ b/MdePkg/Library/Ring3UefiBootServicesTableLib/Ring3UefiBootServicesTableLib.c @@ -70,10 +70,7 @@ EFI_BOOT_SERVICES mBootServices = { (EFI_CREATE_EVENT_EX)Ring3CreateEventEx, // CreateEventEx }; -EFI_BOOT_SERVICES *gBS = &mBootServices; - -EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mCoreDevicePathUtilitiesProtocol = NULL; -EFI_LOADED_IMAGE_PROTOCOL *mCoreLoadedImageProtocol = NULL; +EFI_BOOT_SERVICES *gBS = &mBootServices; /** The function constructs Ring 3 wrappers for the EFI_BOOT_SERVICES. @@ -460,37 +457,12 @@ Ring3OpenProtocol ( } if (CompareGuid (Protocol, &gEfiLoadedImageProtocolGuid)) { - mCoreLoadedImageProtocol = (EFI_LOADED_IMAGE_PROTOCOL *)*Interface; + UserProtocol = (EFI_LOADED_IMAGE_PROTOCOL *)*Interface; - Status = (EFI_STATUS)SysCall ( - SysCallAllocateRing3Pages, - 0, - EFI_SIZE_TO_PAGES (sizeof (EFI_LOADED_IMAGE_PROTOCOL)), - (VOID **)&UserProtocol - ); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "Ring3: Failed to allocate pages for Ring3 EFI_LOADED_IMAGE_PROTOCOL structure.\n")); - return Status; - } + // TODO: Copy User changes to Core? Resembles InstallMultipleProtocolInterfaces(). - // TODO: Copy Core Interface fields with AllocateRing3Pages(). - - UserProtocol->Revision = 0; - UserProtocol->ParentHandle = NULL; - UserProtocol->SystemTable = NULL; - UserProtocol->DeviceHandle = NULL; - UserProtocol->FilePath = NULL; - UserProtocol->Reserved = 0; - UserProtocol->LoadOptionsSize = 0; - UserProtocol->LoadOptions = NULL; - UserProtocol->ImageBase = NULL; - UserProtocol->ImageSize = 0; - UserProtocol->ImageCodeType = 0; - UserProtocol->ImageDataType = 0; UserProtocol->Unload = NULL; - *Interface = UserProtocol; - return Status; } @@ -570,18 +542,7 @@ Ring3LocateProtocol ( } if (CompareGuid (Protocol, &gEfiDevicePathUtilitiesProtocolGuid)) { - mCoreDevicePathUtilitiesProtocol = (EFI_DEVICE_PATH_UTILITIES_PROTOCOL *)*Interface; - - Status = (EFI_STATUS)SysCall ( - SysCallAllocateRing3Pages, - 0, - EFI_SIZE_TO_PAGES (sizeof (EFI_DEVICE_PATH_UTILITIES_PROTOCOL)), - (VOID **)&UserProtocol - ); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "Ring3: Failed to allocate pages for Ring3 EFI_DEVICE_PATH_UTILITIES_PROTOCOL structure.\n")); - return Status; - } + UserProtocol = (EFI_DEVICE_PATH_UTILITIES_PROTOCOL *)*Interface; UserProtocol->GetDevicePathSize = NULL; UserProtocol->DuplicateDevicePath = NULL; @@ -592,8 +553,6 @@ Ring3LocateProtocol ( UserProtocol->IsDevicePathMultiInstance = NULL; UserProtocol->CreateDeviceNode = NULL; - *Interface = UserProtocol; - return Status; }