Skip to content

Commit

Permalink
Ring3: Refactored out AllocateCoreCopy() BootService.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Feb 2, 2024
1 parent 50016e9 commit 3cba245
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 103 deletions.
3 changes: 1 addition & 2 deletions MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ EFI_BOOT_SERVICES mBootServices = {
(EFI_COPY_MEM)CopyMem, // CopyMem
(EFI_SET_MEM)SetMem, // SetMem
(EFI_CREATE_EVENT_EX)CoreCreateEventEx, // CreateEventEx
(EFI_ALLOCATE_RING3_PAGES)AllocateRing3Pages,
(EFI_ALLOCATE_CORE_COPY)AllocateCopyPool
(EFI_ALLOCATE_RING3_PAGES)AllocateRing3Pages
};

EFI_DXE_SERVICES mDxeServices = {
Expand Down
67 changes: 28 additions & 39 deletions MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
**/

#include <Base.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Uefi.h>

#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>

VOID
EFIAPI
Expand All @@ -34,15 +34,6 @@ InternalEnterUserImage (
IN UINT16 DataSelector
);

typedef enum {
SysCallReadMemory = 0,
SysCallAllocateRing3Pages = 1,
SysCallAllocateCoreCopy = 2,
SysCallLocateProtocol = 3,
SysCallOpenProtocol = 4,
SysCallMax
} SYS_CALL_TYPE;

UINTN
EFIAPI
CallBootService (
Expand All @@ -57,10 +48,9 @@ CallBootService (
VOID * Arg4;
VOID * Arg5;
UINT32 Arg6;
EFI_ALLOCATE_RING3_PAGES Func1;
EFI_ALLOCATE_CORE_COPY Func2;
EFI_LOCATE_PROTOCOL Func3;
EFI_OPEN_PROTOCOL Func4;

EFI_GUID *CoreProtocol;

// Stack:
// rcx - Rip for SYSCALL
// r8 - Argument 1
Expand All @@ -69,57 +59,56 @@ CallBootService (
// r11 - User data segment selector <- CoreRbp
// rsp - User Rsp
switch (Type) {
case SysCallReadMemory:
return *(UINTN *)FunctionAddress;

case SysCallAllocateRing3Pages:
Func1 = (EFI_ALLOCATE_RING3_PAGES)*FunctionAddress;
Status = Func1 (
*((UINTN *)CoreRbp + 3),
&Pointer
);
Status = gBS->AllocateRing3Pages (*((UINTN *)CoreRbp + 3), &Pointer);
DisableSMAP ();
*(UINTN *)(*((UINTN *)CoreRbp + 1)) = (UINTN)Pointer;
EnableSMAP ();
return (UINTN)Status;

case SysCallAllocateCoreCopy:
case SysCallLocateProtocol:
DisableSMAP ();
Func2 = (EFI_ALLOCATE_CORE_COPY)*FunctionAddress;
Status = (UINTN)Func2 (
*((UINTN *)CoreRbp + 3),
(VOID *)*((UINTN *)CoreRbp + 1)
);
CoreProtocol = AllocateCopyPool (sizeof (EFI_GUID), (VOID *)*((UINTN *)CoreRbp + 3));
EnableSMAP ();
return (UINTN)Status;
if (CoreProtocol == NULL) {
DEBUG ((DEBUG_ERROR, "Ring0: Failed to allocate core copy of the Protocol variable.\n"));
return EFI_OUT_OF_RESOURCES;
}

case SysCallLocateProtocol:
Func3 = (EFI_LOCATE_PROTOCOL)*FunctionAddress;
Status = Func3 (
(VOID *)*((UINTN *)CoreRbp + 3),
Status = gBS->LocateProtocol (
CoreProtocol,
(VOID *)*((UINTN *)CoreRbp + 1),
&Pointer
);

FreePool (CoreProtocol);
DisableSMAP ();
*((UINTN *)UserRsp + 5) = (UINTN)Pointer;
EnableSMAP ();
return (UINTN)Status;

case SysCallOpenProtocol:
DisableSMAP ();
CoreProtocol = AllocateCopyPool (sizeof (EFI_GUID), (VOID *)*((UINTN *)CoreRbp + 1));
Arg4 = (VOID *)*((UINTN *)UserRsp + 6);
Arg5 = (VOID *)*((UINTN *)UserRsp + 7);
Arg6 = (UINT32)*((UINTN *)UserRsp + 8);
EnableSMAP ();
Func4 = (EFI_OPEN_PROTOCOL)*FunctionAddress;
Status = Func4 (
if (CoreProtocol == NULL) {
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 + 1),
CoreProtocol,
&Pointer,
Arg4,
Arg5,
Arg6
);

FreePool (CoreProtocol);
DisableSMAP ();
*((UINTN *)UserRsp + 5) = (UINTN)Pointer;
EnableSMAP ();
Expand Down
15 changes: 7 additions & 8 deletions MdePkg/Include/Uefi/UefiSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,6 @@ EFI_STATUS
IN OUT VOID **Memory
);

typedef
VOID *
(EFIAPI *EFI_ALLOCATE_CORE_COPY)(
IN UINTN AllocationSize,
IN CONST VOID *Buffer
);

/**
Frees memory pages.
Expand Down Expand Up @@ -1975,9 +1968,15 @@ typedef struct {
EFI_SET_MEM SetMem;
EFI_CREATE_EVENT_EX CreateEventEx;
EFI_ALLOCATE_RING3_PAGES AllocateRing3Pages;
EFI_ALLOCATE_CORE_COPY AllocateCoreCopy;
} 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
/// EFI System Table.
Expand Down
9 changes: 0 additions & 9 deletions MdePkg/Library/Ring3UefiBootServicesTableLib/Ring3.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
**/

typedef enum {
SysCallReadMemory = 0,
SysCallAllocateRing3Pages = 1,
SysCallAllocateCoreCopy = 2,
SysCallLocateProtocol = 3,
SysCallOpenProtocol = 4,
SysCallMax
} SYS_CALL_TYPE;

UINTN
EFIAPI
SysCall (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ EFI_BOOT_SERVICES mBootServices = {
};

EFI_BOOT_SERVICES *gBS = &mBootServices;
EFI_BOOT_SERVICES *mCoreBS = NULL;

EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mCoreDevicePathUtilitiesProtocol = NULL;
EFI_LOADED_IMAGE_PROTOCOL *mCoreLoadedImageProtocol = NULL;
Expand All @@ -92,16 +91,6 @@ UefiBootServicesTableLibConstructor (
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;
}

Expand Down Expand Up @@ -452,26 +441,14 @@ Ring3OpenProtocol (
)
{
EFI_STATUS Status;
EFI_GUID *CoreProtocol;

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 (
SysCallOpenProtocol,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, OpenProtocol),
0,
CoreUserHandle,
CoreProtocol,
Protocol,
Interface,
CoreImageHandle,
CoreControllerHandle,
Expand All @@ -482,14 +459,12 @@ Ring3OpenProtocol (
return Status;
}

// TODO: FreePool (CoreProtocol);

if (CompareGuid (Protocol, &gEfiLoadedImageProtocolGuid)) {
mCoreLoadedImageProtocol = (EFI_LOADED_IMAGE_PROTOCOL *)*Interface;

Status = (EFI_STATUS)SysCall (
SysCallAllocateRing3Pages,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, AllocateRing3Pages),
0,
EFI_SIZE_TO_PAGES (sizeof (EFI_LOADED_IMAGE_PROTOCOL)),
(VOID **)&UserProtocol
);
Expand Down Expand Up @@ -579,25 +554,13 @@ Ring3LocateProtocol (
)
{
EFI_STATUS Status;
EFI_GUID *CoreProtocol;

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 (
SysCallLocateProtocol,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, LocateProtocol),
CoreProtocol,
0,
Protocol,
CoreRegistration,
Interface
);
Expand All @@ -606,14 +569,12 @@ Ring3LocateProtocol (
return Status;
}

// TODO: FreePool (CoreProtocol);

if (CompareGuid (Protocol, &gEfiDevicePathUtilitiesProtocolGuid)) {
mCoreDevicePathUtilitiesProtocol = (EFI_DEVICE_PATH_UTILITIES_PROTOCOL *)*Interface;

Status = (EFI_STATUS)SysCall (
SysCallAllocateRing3Pages,
(UINTN)mCoreBS + OFFSET_OF (EFI_BOOT_SERVICES, AllocateRing3Pages),
0,
EFI_SIZE_TO_PAGES (sizeof (EFI_DEVICE_PATH_UTILITIES_PROTOCOL)),
(VOID **)&UserProtocol
);
Expand Down

0 comments on commit 3cba245

Please sign in to comment.