Ring3: Refactored out AllocateCoreCopy() BootService.

This commit is contained in:
Mikhail Krichanov 2024-02-02 12:26:12 +03:00
parent 926f1b1b69
commit 78fac6c238
5 changed files with 44 additions and 105 deletions

View File

@ -90,8 +90,7 @@ EFI_BOOT_SERVICES mBootServices = {
(EFI_COPY_MEM)CopyMem, // CopyMem (EFI_COPY_MEM)CopyMem, // CopyMem
(EFI_SET_MEM)SetMem, // SetMem (EFI_SET_MEM)SetMem, // SetMem
(EFI_CREATE_EVENT_EX)CoreCreateEventEx, // CreateEventEx (EFI_CREATE_EVENT_EX)CoreCreateEventEx, // CreateEventEx
(EFI_ALLOCATE_RING3_PAGES)AllocateRing3Pages, (EFI_ALLOCATE_RING3_PAGES)AllocateRing3Pages
(EFI_ALLOCATE_CORE_COPY)AllocateCopyPool
}; };
EFI_DXE_SERVICES mDxeServices = { EFI_DXE_SERVICES mDxeServices = {

View File

@ -5,11 +5,11 @@
**/ **/
#include <Base.h> #include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h> #include <Library/DebugLib.h>
#include <Library/PcdLib.h> #include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
VOID VOID
EFIAPI EFIAPI
@ -34,15 +34,6 @@ InternalEnterUserImage (
IN UINT16 DataSelector IN UINT16 DataSelector
); );
typedef enum {
SysCallReadMemory = 0,
SysCallAllocateRing3Pages = 1,
SysCallAllocateCoreCopy = 2,
SysCallLocateProtocol = 3,
SysCallOpenProtocol = 4,
SysCallMax
} SYS_CALL_TYPE;
UINTN UINTN
EFIAPI EFIAPI
CallBootService ( CallBootService (
@ -57,10 +48,9 @@ CallBootService (
VOID * Arg4; VOID * Arg4;
VOID * Arg5; VOID * Arg5;
UINT32 Arg6; UINT32 Arg6;
EFI_ALLOCATE_RING3_PAGES Func1;
EFI_ALLOCATE_CORE_COPY Func2; EFI_GUID *CoreProtocol;
EFI_LOCATE_PROTOCOL Func3;
EFI_OPEN_PROTOCOL Func4;
// Stack: // Stack:
// rcx - Rip for SYSCALL // rcx - Rip for SYSCALL
// r8 - Argument 1 // r8 - Argument 1
@ -69,37 +59,29 @@ CallBootService (
// r11 - User data segment selector <- CoreRbp // r11 - User data segment selector <- CoreRbp
// rsp - User Rsp // rsp - User Rsp
switch (Type) { switch (Type) {
case SysCallReadMemory:
return *(UINTN *)FunctionAddress;
case SysCallAllocateRing3Pages: case SysCallAllocateRing3Pages:
Func1 = (EFI_ALLOCATE_RING3_PAGES)*FunctionAddress; Status = gBS->AllocateRing3Pages (*((UINTN *)CoreRbp + 3), &Pointer);
Status = Func1 (
*((UINTN *)CoreRbp + 3),
&Pointer
);
DisableSMAP (); DisableSMAP ();
*(UINTN *)(*((UINTN *)CoreRbp + 1)) = (UINTN)Pointer; *(UINTN *)(*((UINTN *)CoreRbp + 1)) = (UINTN)Pointer;
EnableSMAP (); EnableSMAP ();
return (UINTN)Status; return (UINTN)Status;
case SysCallAllocateCoreCopy:
DisableSMAP ();
Func2 = (EFI_ALLOCATE_CORE_COPY)*FunctionAddress;
Status = (UINTN)Func2 (
*((UINTN *)CoreRbp + 3),
(VOID *)*((UINTN *)CoreRbp + 1)
);
EnableSMAP ();
return (UINTN)Status;
case SysCallLocateProtocol: case SysCallLocateProtocol:
Func3 = (EFI_LOCATE_PROTOCOL)*FunctionAddress; DisableSMAP ();
Status = Func3 ( CoreProtocol = AllocateCopyPool (sizeof (EFI_GUID), (VOID *)*((UINTN *)CoreRbp + 3));
(VOID *)*((UINTN *)CoreRbp + 3), EnableSMAP ();
if (CoreProtocol == NULL) {
DEBUG ((DEBUG_ERROR, "Ring0: Failed to allocate core copy of the Protocol variable.\n"));
return EFI_OUT_OF_RESOURCES;
}
Status = gBS->LocateProtocol (
CoreProtocol,
(VOID *)*((UINTN *)CoreRbp + 1), (VOID *)*((UINTN *)CoreRbp + 1),
&Pointer &Pointer
); );
FreePool (CoreProtocol);
DisableSMAP (); DisableSMAP ();
*((UINTN *)UserRsp + 5) = (UINTN)Pointer; *((UINTN *)UserRsp + 5) = (UINTN)Pointer;
EnableSMAP (); EnableSMAP ();
@ -107,19 +89,26 @@ CallBootService (
case SysCallOpenProtocol: case SysCallOpenProtocol:
DisableSMAP (); DisableSMAP ();
CoreProtocol = AllocateCopyPool (sizeof (EFI_GUID), (VOID *)*((UINTN *)CoreRbp + 1));
Arg4 = (VOID *)*((UINTN *)UserRsp + 6); Arg4 = (VOID *)*((UINTN *)UserRsp + 6);
Arg5 = (VOID *)*((UINTN *)UserRsp + 7); Arg5 = (VOID *)*((UINTN *)UserRsp + 7);
Arg6 = (UINT32)*((UINTN *)UserRsp + 8); Arg6 = (UINT32)*((UINTN *)UserRsp + 8);
EnableSMAP (); EnableSMAP ();
Func4 = (EFI_OPEN_PROTOCOL)*FunctionAddress; if (CoreProtocol == NULL) {
Status = Func4 ( DEBUG ((DEBUG_ERROR, "Ring0: Failed to allocate core copy of the Protocol variable.\n"));
return EFI_OUT_OF_RESOURCES;
}
Status = gBS->OpenProtocol (
(VOID *)*((UINTN *)CoreRbp + 3), (VOID *)*((UINTN *)CoreRbp + 3),
(VOID *)*((UINTN *)CoreRbp + 1), CoreProtocol,
&Pointer, &Pointer,
Arg4, Arg4,
Arg5, Arg5,
Arg6 Arg6
); );
FreePool (CoreProtocol);
DisableSMAP (); DisableSMAP ();
*((UINTN *)UserRsp + 5) = (UINTN)Pointer; *((UINTN *)UserRsp + 5) = (UINTN)Pointer;
EnableSMAP (); EnableSMAP ();

View File

@ -229,13 +229,6 @@ EFI_STATUS
IN OUT VOID **Memory IN OUT VOID **Memory
); );
typedef
VOID *
(EFIAPI *EFI_ALLOCATE_CORE_COPY)(
IN UINTN AllocationSize,
IN CONST VOID *Buffer
);
/** /**
Frees memory pages. Frees memory pages.
@ -2038,9 +2031,15 @@ typedef struct {
EFI_SET_MEM SetMem; EFI_SET_MEM SetMem;
EFI_CREATE_EVENT_EX CreateEventEx; EFI_CREATE_EVENT_EX CreateEventEx;
EFI_ALLOCATE_RING3_PAGES AllocateRing3Pages; EFI_ALLOCATE_RING3_PAGES AllocateRing3Pages;
EFI_ALLOCATE_CORE_COPY AllocateCoreCopy;
} EFI_BOOT_SERVICES; } EFI_BOOT_SERVICES;
typedef enum {
SysCallLocateProtocol = 1,
SysCallOpenProtocol = 2,
SysCallAllocateRing3Pages = 3,
SysCallMax
} SYS_CALL_TYPE;
/// ///
/// Contains a set of GUID/pointer pairs comprised of the ConfigurationTable field in the /// Contains a set of GUID/pointer pairs comprised of the ConfigurationTable field in the
/// EFI System Table. /// EFI System Table.

View File

@ -5,15 +5,6 @@
**/ **/
typedef enum {
SysCallReadMemory = 0,
SysCallAllocateRing3Pages = 1,
SysCallAllocateCoreCopy = 2,
SysCallLocateProtocol = 3,
SysCallOpenProtocol = 4,
SysCallMax
} SYS_CALL_TYPE;
UINTN UINTN
EFIAPI EFIAPI
SysCall ( SysCall (

View File

@ -71,7 +71,6 @@ EFI_BOOT_SERVICES mBootServices = {
}; };
EFI_BOOT_SERVICES *gBS = &mBootServices; EFI_BOOT_SERVICES *gBS = &mBootServices;
EFI_BOOT_SERVICES *mCoreBS = NULL;
EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mCoreDevicePathUtilitiesProtocol = NULL; EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mCoreDevicePathUtilitiesProtocol = NULL;
EFI_LOADED_IMAGE_PROTOCOL *mCoreLoadedImageProtocol = NULL; EFI_LOADED_IMAGE_PROTOCOL *mCoreLoadedImageProtocol = NULL;
@ -92,16 +91,6 @@ UefiBootServicesTableLibConstructor (
IN EFI_SYSTEM_TABLE *SystemTable IN EFI_SYSTEM_TABLE *SystemTable
) )
{ {
//
// Cache pointer to the EFI Boot Services Table
//
mCoreBS = (EFI_BOOT_SERVICES *)SysCall (
SysCallReadMemory,
(UINTN)SystemTable + OFFSET_OF (EFI_SYSTEM_TABLE, BootServices)
);
ASSERT (mCoreBS != NULL);
DEBUG ((DEBUG_ERROR, "User: BootServices = 0x%lx\n", (UINTN)mCoreBS));
return EFI_SUCCESS; return EFI_SUCCESS;
} }
@ -452,26 +441,14 @@ Ring3OpenProtocol (
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
EFI_GUID *CoreProtocol;
EFI_LOADED_IMAGE_PROTOCOL *UserProtocol; EFI_LOADED_IMAGE_PROTOCOL *UserProtocol;
CoreProtocol = (VOID *)SysCall (
SysCallAllocateCoreCopy,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, AllocateCoreCopy),
sizeof (EFI_GUID),
Protocol
);
if (CoreProtocol == NULL) {
DEBUG ((DEBUG_ERROR, "Ring3: Failed to allocate core copy of the Protocol variable.\n"));
return EFI_OUT_OF_RESOURCES;
}
Status = (EFI_STATUS)SysCall ( Status = (EFI_STATUS)SysCall (
SysCallOpenProtocol, SysCallOpenProtocol,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, OpenProtocol), 0,
CoreUserHandle, CoreUserHandle,
CoreProtocol, Protocol,
Interface, Interface,
CoreImageHandle, CoreImageHandle,
CoreControllerHandle, CoreControllerHandle,
@ -482,14 +459,12 @@ Ring3OpenProtocol (
return Status; return Status;
} }
// TODO: FreePool (CoreProtocol);
if (CompareGuid (Protocol, &gEfiLoadedImageProtocolGuid)) { if (CompareGuid (Protocol, &gEfiLoadedImageProtocolGuid)) {
mCoreLoadedImageProtocol = (EFI_LOADED_IMAGE_PROTOCOL *)*Interface; mCoreLoadedImageProtocol = (EFI_LOADED_IMAGE_PROTOCOL *)*Interface;
Status = (EFI_STATUS)SysCall ( Status = (EFI_STATUS)SysCall (
SysCallAllocateRing3Pages, SysCallAllocateRing3Pages,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, AllocateRing3Pages), 0,
EFI_SIZE_TO_PAGES (sizeof (EFI_LOADED_IMAGE_PROTOCOL)), EFI_SIZE_TO_PAGES (sizeof (EFI_LOADED_IMAGE_PROTOCOL)),
(VOID **)&UserProtocol (VOID **)&UserProtocol
); );
@ -579,25 +554,13 @@ Ring3LocateProtocol (
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
EFI_GUID *CoreProtocol;
EFI_DEVICE_PATH_UTILITIES_PROTOCOL *UserProtocol; EFI_DEVICE_PATH_UTILITIES_PROTOCOL *UserProtocol;
CoreProtocol = (VOID *)SysCall (
SysCallAllocateCoreCopy,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, AllocateCoreCopy),
sizeof (EFI_GUID),
Protocol
);
if (CoreProtocol == NULL) {
DEBUG ((DEBUG_ERROR, "Ring3: Failed to allocate core copy of the Protocol variable.\n"));
return EFI_OUT_OF_RESOURCES;
}
Status = (EFI_STATUS)SysCall ( Status = (EFI_STATUS)SysCall (
SysCallLocateProtocol, SysCallLocateProtocol,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, LocateProtocol), 0,
CoreProtocol, Protocol,
CoreRegistration, CoreRegistration,
Interface Interface
); );
@ -606,14 +569,12 @@ Ring3LocateProtocol (
return Status; return Status;
} }
// TODO: FreePool (CoreProtocol);
if (CompareGuid (Protocol, &gEfiDevicePathUtilitiesProtocolGuid)) { if (CompareGuid (Protocol, &gEfiDevicePathUtilitiesProtocolGuid)) {
mCoreDevicePathUtilitiesProtocol = (EFI_DEVICE_PATH_UTILITIES_PROTOCOL *)*Interface; mCoreDevicePathUtilitiesProtocol = (EFI_DEVICE_PATH_UTILITIES_PROTOCOL *)*Interface;
Status = (EFI_STATUS)SysCall ( Status = (EFI_STATUS)SysCall (
SysCallAllocateRing3Pages, SysCallAllocateRing3Pages,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, AllocateRing3Pages), 0,
EFI_SIZE_TO_PAGES (sizeof (EFI_DEVICE_PATH_UTILITIES_PROTOCOL)), EFI_SIZE_TO_PAGES (sizeof (EFI_DEVICE_PATH_UTILITIES_PROTOCOL)),
(VOID **)&UserProtocol (VOID **)&UserProtocol
); );