Ring3: Refactored SysCallBootService() out of EFI_BOOT_SERVICES.

This commit is contained in:
Mikhail Krichanov 2024-05-23 21:30:10 +03:00
parent c5d244956e
commit 9bb63c464b
7 changed files with 98 additions and 82 deletions

View File

@ -23,4 +23,10 @@ DefaultExceptionHandler (
IN OUT EFI_SYSTEM_CONTEXT SystemContext
);
VOID
EFIAPI
InitializeSysCallHandler (
IN VOID *Handler
);
#endif // DEFAULT_EXCEPTION_HANDLER_LIB_H_

View File

@ -29,6 +29,14 @@
//
#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",
@ -36,7 +44,8 @@ STATIC CHAR8 *gExceptionTypeString[] = {
"SError"
};
STATIC BOOLEAN mRecursiveException;
STATIC BOOLEAN mRecursiveException;
STATIC EFI_SYS_CALL_BOOT_SERVICE mSysCallHandler;
CONST CHAR8 *
GetImageName (
@ -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
@ -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) {

View File

@ -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)
@ -2724,11 +2735,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

View File

@ -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 = {

View File

@ -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
@ -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 (
@ -52,6 +100,8 @@ InitializeMsr (
DEBUG ((DEBUG_ERROR, "Core: Failed to initialize MSRs for Ring3.\n"));
ASSERT (FALSE);
}
InitializeSysCallHandler ((VOID *)SysCallBootService);
}
VOID

View File

@ -8,8 +8,6 @@
#include "DxeMain.h"
#include "SupportedProtocols.h"
UINTN CoreSp;
LIST_ENTRY mProtocolsHead = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolsHead);
typedef struct {
@ -67,13 +65,6 @@ CallInstallMultipleProtocolInterfaces (
IN VOID *Function
);
VOID
EFIAPI
ReturnToCore (
IN EFI_STATUS Status,
IN UINTN CoreSp
);
VOID
EFIAPI
FreeProtocolsList (
@ -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
@ -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;
}

View File

@ -1831,14 +1831,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
//
@ -2019,7 +2011,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 {