Skip to content

Commit

Permalink
Ring3: Refactored out gCoreSysCallStackTop and gRing3CallStackTop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Jan 13, 2025
1 parent efcbf19 commit befe005
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 117 deletions.
4 changes: 2 additions & 2 deletions ArmVirtPkg/ArmVirtQemuFvMain.fdf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ APRIORI DXE {
#
INF MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxe.inf
INF MdeModulePkg/Universal/Disk/PartitionDxe/PartitionDxe.inf
INF FatPkg/EnhancedFatDxe/Fat.inf
INF USER FatPkg/EnhancedFatDxe/Fat.inf
INF USER MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.inf
INF USER MdeModulePkg/Universal/Disk/UnicodeCollation/EnglishDxe/EnglishDxe.inf
INF MdeModulePkg/Universal/Disk/UnicodeCollation/EnglishDxe/EnglishDxe.inf
INF MdeModulePkg/Universal/Disk/UdfDxe/UdfDxe.inf
INF OvmfPkg/VirtioFsDxe/VirtioFsDxe.inf

Expand Down
14 changes: 4 additions & 10 deletions MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ extern LOADED_IMAGE_PRIVATE_DATA * mCurrentImage;

extern RING3_DATA *gRing3Data;
extern VOID *gRing3Interfaces;
extern UINTN gCoreSysCallStackTop;
extern UINTN gRing3CallStackTop;
extern VOID *gRing3EntryPoint;
extern UINTN gUserPageTable;
extern UINTN gCorePageTable;
Expand Down Expand Up @@ -2744,12 +2742,6 @@ CallBootService (
IN RING3_STACK *UserRsp
);

EFI_STATUS
EFIAPI
CallRing3 (
IN RING3_CALL_DATA *Data
);

VOID
EFIAPI
AllowSupervisorAccessToUserMemory (
Expand All @@ -2765,8 +2757,10 @@ ForbidSupervisorAccessToUserMemory (
EFI_STATUS
EFIAPI
GoToRing3 (
IN UINT8 Number,
IN VOID *EntryPoint,
IN UINT8 Number,
IN VOID *EntryPoint,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop,
...
);

Expand Down
16 changes: 8 additions & 8 deletions MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,16 +1741,16 @@ CoreStartImage (
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(UINTN)Image->EntryPoint, &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);

gUserPageTable = Image->UserPageTable;
gRing3CallStackTop = Image->UserStackTop;
gCoreSysCallStackTop = Image->SysCallStackTop;
gUserPageTable = Image->UserPageTable;

Image->Status = GoToRing3 (
2,
(VOID *)Image->EntryPoint,
ImageHandle,
gRing3Data
);
2,
(VOID *)Image->EntryPoint,
Image->UserStackTop,
Image->SysCallStackTop,
ImageHandle,
gRing3Data
);
}
} else {
Image->Status = Image->EntryPoint (ImageHandle, Image->Info.SystemTable);
Expand Down
8 changes: 5 additions & 3 deletions MdeModulePkg/Core/Dxe/SysCall/AARCH64/CoreBootServices.S
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ ASM_FUNC_ALIGN(SysCallBase, 4096)
// EFI_STATUS
// EFIAPI
// CallRing3 (
// IN RING3_CALL_DATA *Data
// IN RING3_CALL_DATA *Data,
// IN UINTN UserStackTop,
// IN UINTN SysCallStackTop
// );
//
// (x0) Data
// (x1) gRing3CallStackTop
// (x1) UserStackTop
// (x2) gRing3EntryPoint
// (x3) gCoreSysCallStackTop
// (x3) SysCallStackTop
// (x4) &CoreSp
// (x5) gUserPageTable
//------------------------------------------------------------------------------
Expand Down
11 changes: 9 additions & 2 deletions MdeModulePkg/Core/Dxe/SysCall/AARCH64/InitializeAARCH64.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

STATIC UINTN mCoreSp;
UINTN gUserPageTable;
UINTN mRing3CallStackTop;
UINTN mCoreSysCallStackTop;

EFI_STATUS
EFIAPI
Expand Down Expand Up @@ -167,8 +169,13 @@ ForbidSupervisorAccessToUserMemory (
EFI_STATUS
EFIAPI
CallRing3 (
IN RING3_CALL_DATA *Data
IN RING3_CALL_DATA *Data,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop
)
{
return ArmCallRing3 (Data, gRing3CallStackTop, gRing3EntryPoint, gCoreSysCallStackTop, &mCoreSp, gUserPageTable);
mRing3CallStackTop = UserStackTop;
mCoreSysCallStackTop = SysCallStackTop;

return ArmCallRing3 (Data, UserStackTop, gRing3EntryPoint, SysCallStackTop, &mCoreSp, gUserPageTable);
}
10 changes: 6 additions & 4 deletions MdeModulePkg/Core/Dxe/SysCall/ARM/CoreBootServices.S
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ ASM_FUNC_ALIGN(SysCallBase, 4096)
// EFI_STATUS
// EFIAPI
// CallRing3 (
// IN RING3_CALL_DATA *Data
// IN RING3_CALL_DATA *Data,
// IN UINTN UserStackTop,
// IN UINTN SysCallStackTop
// );
//
// (r0) Data
// (r1) gRing3CallStackTop
// (r1) UserStackTop
// (r2) gRing3EntryPoint
// (r3) gCoreSysCallStackTop
// (r3) SysCallStackTop
//
// (On Core Stack) &CoreSp, gUserPageTable
//------------------------------------------------------------------------------
Expand All @@ -84,7 +86,7 @@ ASM_FUNC(ArmCallRing3)
cpsid if
isb

// Set SP_usr to gRing3CallStackTop.
// Set SP_usr to UserStackTop.
push {R1}
mov R1, SP
ldmia R1, {SP}^
Expand Down
11 changes: 9 additions & 2 deletions MdeModulePkg/Core/Dxe/SysCall/ARM/InitializeARM.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

STATIC UINTN mCoreSp;
UINTN gUserPageTable;
UINTN mRing3CallStackTop;
UINTN mCoreSysCallStackTop;

EFI_STATUS
EFIAPI
Expand Down Expand Up @@ -162,8 +164,13 @@ ForbidSupervisorAccessToUserMemory (
EFI_STATUS
EFIAPI
CallRing3 (
IN RING3_CALL_DATA *Data
IN RING3_CALL_DATA *Data,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop
)
{
return ArmCallRing3 (Data, gRing3CallStackTop, gRing3EntryPoint, gCoreSysCallStackTop, &mCoreSp, gUserPageTable);
mRing3CallStackTop = UserStackTop;
mCoreSysCallStackTop = SysCallStackTop;

return ArmCallRing3 (Data, UserStackTop, gRing3EntryPoint, SysCallStackTop, &mCoreSp, gUserPageTable);
}
5 changes: 5 additions & 0 deletions MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "DxeMain.h"
#include "SupportedProtocols.h"

extern UINTN mRing3CallStackTop;
extern UINTN mCoreSysCallStackTop;

LIST_ENTRY mProtocolsHead = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolsHead);

typedef struct {
Expand Down Expand Up @@ -479,6 +482,8 @@ CallBootService (
UserDriver->CoreWrapper = CoreArgList[Index + 1];
UserDriver->UserSpaceDriver = UserArgList[Index + 1];
UserDriver->UserPageTable = gUserPageTable;
UserDriver->SysCallStackTop = mCoreSysCallStackTop;
UserDriver->UserStackTop = mRing3CallStackTop;

InsertTailList (&mUserSpaceDriversHead, &UserDriver->Link);

Expand Down
25 changes: 21 additions & 4 deletions MdeModulePkg/Core/Dxe/SysCall/IA32/CoreBootServices.nasm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <Register/Intel/ArchitecturalMsr.h>

extern ASM_PFX(CallBootService)
extern ASM_PFX(gCoreSysCallStackTop)
extern ASM_PFX(gRing3CallStackTop)
extern ASM_PFX(gRing3EntryPoint)

extern ASM_PFX(AsmReadMsr64)
Expand Down Expand Up @@ -174,7 +172,9 @@ ASM_PFX(CoreBootServices):
; EFI_STATUS
; EFIAPI
; CallRing3 (
; IN RING3_CALL_DATA *Data
; IN RING3_CALL_DATA *Data,
; IN UINTN UserStackTop,
; IN UINTN SysCallStackTop
; );
;
; (On User Stack) Data
Expand All @@ -191,8 +191,17 @@ ASM_PFX(CallRing3):
; Save Core Stack pointer.
mov [ASM_PFX(CoreEsp)], esp

mov ebx, [esp + 4 * 6] ; UserStackTop
mov [ASM_PFX(mRing3CallStackTop)], ebx
mov ebx, [esp + 4 * 7] ; SysCallStackTop
mov [ASM_PFX(mCoreSysCallStackTop)], ebx
mov edx, 0
mov eax, ebx
mov ecx, MSR_IA32_SYSENTER_ESP
wrmsr

push dword [ASM_PFX(gRing3EntryPoint)]
push dword [ASM_PFX(gRing3CallStackTop)]
push dword [ASM_PFX(mRing3CallStackTop)]

SetRing3DataSegmentSelectors

Expand Down Expand Up @@ -249,3 +258,11 @@ ASM_PFX(gUserPageTable):
ALIGN 4096
ASM_PFX(CoreEsp):
resd 1

global ASM_PFX(mRing3CallStackTop)
ASM_PFX(mRing3CallStackTop):
resd 1

global ASM_PFX(mCoreSysCallStackTop)
ASM_PFX(mCoreSysCallStackTop):
resd 1
3 changes: 0 additions & 3 deletions MdeModulePkg/Core/Dxe/SysCall/IA32/InitializeIA32.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,5 @@ InitializeMsr (
Msr = (UINT64)(UINTN)CoreBootServices;
AsmWriteMsr64 (MSR_IA32_SYSENTER_EIP, Msr);

Msr = (UINT64)(UINTN)gCoreSysCallStackTop;
AsmWriteMsr64 (MSR_IA32_SYSENTER_ESP, Msr);

gCorePageTable = AsmReadCr3 ();
}
2 changes: 0 additions & 2 deletions MdeModulePkg/Core/Dxe/SysCall/Initialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include "DxeMain.h"

UINTN gCoreSysCallStackTop;
UINTN gRing3CallStackTop;
VOID *gRing3EntryPoint;
RING3_DATA *gRing3Data;
VOID *gRing3Interfaces;
Expand Down
Loading

0 comments on commit befe005

Please sign in to comment.