Skip to content

Commit

Permalink
Ring3: Refactored SysCallBootService() out of EFI_BOOT_SERVICES.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed May 23, 2024
1 parent 2416073 commit 737945d
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 82 deletions.
6 changes: 6 additions & 0 deletions ArmPkg/Include/Library/DefaultExceptionHandlerLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ DefaultExceptionHandler (
IN OUT EFI_SYSTEM_CONTEXT SystemContext
);

VOID
EFIAPI
InitializeSysCallHandler (
IN VOID *Handler
);

#endif // DEFAULT_EXCEPTION_HANDLER_LIB_H_
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@
//
#define MAX_PRINT_CHARS 100

typedef
EFI_STATUS
(EFIAPI *EFI_SYS_CALL_BOOT_SERVICE)(
IN UINT8 Type,
IN VOID *CoreRbp,
IN VOID *UserRsp
);

STATIC CHAR8 *gExceptionTypeString[] = {
"Synchronous",
"IRQ",
"FIQ",
"SError"
};

STATIC BOOLEAN mRecursiveException;
STATIC BOOLEAN mRecursiveException;
STATIC EFI_SYS_CALL_BOOT_SERVICE mSysCallHandler;

CONST CHAR8 *
GetImageName (
Expand Down Expand Up @@ -177,6 +186,15 @@ BaseName (
return Str;
}

VOID
EFIAPI
InitializeSysCallHandler (
IN VOID *Handler
)
{
mSysCallHandler = (EFI_SYS_CALL_BOOT_SERVICE)Handler;
}

/**
This is the default action to take on an unexpected exception
Expand All @@ -199,11 +217,11 @@ DefaultExceptionHandler (
INT32 Offset;

if (AARCH64_ESR_EC (SystemContext.SystemContextAArch64->ESR) == AARCH64_ESR_EC_SVC64) {
return gBS->SysCallBootService (
SystemContext.SystemContextAArch64->X0,
&(SystemContext.SystemContextAArch64->X1),
&(SystemContext.SystemContextAArch64->X0)
);
return mSysCallHandler (
SystemContext.SystemContextAArch64->X0,
&(SystemContext.SystemContextAArch64->X1),
&(SystemContext.SystemContextAArch64->X0)
);
}

if (mRecursiveException) {
Expand Down
21 changes: 16 additions & 5 deletions MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ typedef struct {
BOOLEAN IsRing3EntryPoint;
} LOADED_IMAGE_PRIVATE_DATA;

typedef struct {
UINTN Argument1;
UINTN Argument2;
UINTN Argument3;
} CORE_STACK;

typedef struct {
UINTN Rip;
UINTN Arguments[];
} RING3_STACK;

#define LOADED_IMAGE_PRIVATE_DATA_FROM_THIS(a) \
CR(a, LOADED_IMAGE_PRIVATE_DATA, Info, LOADED_IMAGE_PRIVATE_DATA_SIGNATURE)

Expand Down Expand Up @@ -2718,11 +2729,11 @@ CoreBootServices (

EFI_STATUS
EFIAPI
SysCallBootService (
IN UINT8 Type,
IN VOID *CoreRbp,
IN VOID *UserRsp
);
CallBootService (
IN UINT8 Type,
IN CORE_STACK *CoreRbp,
IN RING3_STACK *UserRsp
);

EFI_STATUS
EFIAPI
Expand Down
3 changes: 1 addition & 2 deletions MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_SYS_CALL_BOOT_SERVICE)SysCallBootService
(EFI_CREATE_EVENT_EX)CoreCreateEventEx // CreateEventEx
};

EFI_DXE_SERVICES mDxeServices = {
Expand Down
52 changes: 51 additions & 1 deletion MdeModulePkg/Core/Dxe/SysCall/AARCH64/InitializeMsr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

#include <Chipset/AArch64.h>
#include <Library/ArmLib.h>
#include <Library/DefaultExceptionHandlerLib.h>

#include "DxeMain.h"

extern UINTN CoreSp;
UINTN CoreSp;

EFI_STATUS
EFIAPI
Expand All @@ -22,6 +23,53 @@ ArmCallRing3 (
IN VOID *CoreStack
);

VOID
EFIAPI
ReturnToCore (
IN EFI_STATUS Status,
IN UINTN CoreSp
);

EFI_STATUS
EFIAPI
SysCallBootService (
IN UINT8 Type,
IN VOID *CoreRbp,
IN VOID *UserRsp
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Physical;

if (Type == SysCallReturnToCore) {
ReturnToCore (*(EFI_STATUS *)CoreRbp, CoreSp);
}

Status = CoreAllocatePages (
AllocateAnyPages,
EfiRing3MemoryType,
EFI_SIZE_TO_PAGES (8 * sizeof (UINTN)),
&Physical
);
if (EFI_ERROR (Status)) {
return Status;
}

DisableSMAP ();
CopyMem ((VOID *)(UINTN)Physical, (VOID *)UserRsp, 8 * sizeof (UINTN));
EnableSMAP ();

Status = CallBootService (
Type,
(CORE_STACK *)CoreRbp,
(RING3_STACK *)(UINTN)Physical
);

CoreFreePages (Physical, EFI_SIZE_TO_PAGES (8 * sizeof (UINTN)));

return Status;
}

VOID
EFIAPI
InitializeMsr (
Expand Down Expand Up @@ -52,6 +100,8 @@ InitializeMsr (
DEBUG ((DEBUG_ERROR, "Core: Failed to initialize MSRs for Ring3.\n"));
ASSERT (FALSE);
}

InitializeSysCallHandler ((VOID *)SysCallBootService);
}

VOID
Expand Down
59 changes: 0 additions & 59 deletions MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include "DxeMain.h"
#include "SupportedProtocols.h"

UINTN CoreSp;

LIST_ENTRY mProtocolsHead = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolsHead);

typedef struct {
Expand Down Expand Up @@ -67,13 +65,6 @@ CallInstallMultipleProtocolInterfaces (
IN VOID *Function
);

VOID
EFIAPI
ReturnToCore (
IN EFI_STATUS Status,
IN UINTN CoreSp
);

VOID
EFIAPI
FreeProtocolsList (
Expand Down Expand Up @@ -270,16 +261,6 @@ PrepareRing3Interface (
return Ring3Interface;
}

typedef struct {
UINTN Argument1;
UINTN Argument2;
UINTN Argument3;
} CORE_STACK;

typedef struct {
UINTN Rip;
UINTN Arguments[];
} RING3_STACK;
//
// Stack:
// rsp - User Rsp
Expand Down Expand Up @@ -1400,43 +1381,3 @@ CallBootService (

return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
SysCallBootService (
IN UINT8 Type,
IN VOID *CoreRbp,
IN VOID *UserRsp
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Physical;

if (Type == SysCallReturnToCore) {
ReturnToCore (*(EFI_STATUS *)CoreRbp, CoreSp);
}

Status = CoreAllocatePages (
AllocateAnyPages,
EfiRing3MemoryType,
EFI_SIZE_TO_PAGES (8 * sizeof (UINTN)),
&Physical
);
if (EFI_ERROR (Status)) {
return Status;
}

DisableSMAP ();
CopyMem ((VOID *)(UINTN)Physical, (VOID *)UserRsp, 8 * sizeof (UINTN));
EnableSMAP ();

Status = CallBootService (
Type,
(CORE_STACK *)CoreRbp,
(RING3_STACK *)(UINTN)Physical
);

CoreFreePages (Physical, EFI_SIZE_TO_PAGES (8 * sizeof (UINTN)));

return Status;
}
9 changes: 0 additions & 9 deletions MdePkg/Include/Uefi/UefiSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -1780,14 +1780,6 @@ EFI_STATUS
OUT UINT64 *MaximumVariableSize
);

typedef
EFI_STATUS
(EFIAPI *EFI_SYS_CALL_BOOT_SERVICE)(
IN UINT8 Type,
IN VOID *CoreRbp,
IN VOID *UserRsp
);

//
// Firmware should stop at a firmware user interface on next boot
//
Expand Down Expand Up @@ -1968,7 +1960,6 @@ typedef struct {
EFI_COPY_MEM CopyMem;
EFI_SET_MEM SetMem;
EFI_CREATE_EVENT_EX CreateEventEx;
EFI_SYS_CALL_BOOT_SERVICE SysCallBootService;
} EFI_BOOT_SERVICES;

typedef enum {
Expand Down

0 comments on commit 737945d

Please sign in to comment.