Skip to content

Commit

Permalink
SysCall: Refactored out mRing3CallStackTop and mCoreSysCallStackTop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Jan 14, 2025
1 parent 08df5dd commit 708ea60
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 53 deletions.
4 changes: 3 additions & 1 deletion MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,9 @@ EFI_STATUS
EFIAPI
CallBootService (
IN UINT8 Type,
IN UINTN *UserArguments
IN UINTN *UserArguments,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop
);

VOID
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Core/Dxe/SysCall/AARCH64/CoreBootServices.S
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ASM_FUNC(ArmCallRing3)
// Disable interrupts.
msr daifset, #0xf
isb
// Save Core SP and switch to CoreSysCall Stack.
// Save Core SP and switch to SysCallStackTop.
mov x6, sp
str x6, [x4]
mov sp, x3
Expand Down
27 changes: 18 additions & 9 deletions MdeModulePkg/Core/Dxe/SysCall/AARCH64/InitializeAARCH64.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
#include "DxeMain.h"

STATIC UINTN mCoreSp;
STATIC UINTN mUserStackTop;
STATIC UINTN mSysCallStackTop;
UINTN gUserPageTable;
UINTN mRing3CallStackTop;
UINTN mCoreSysCallStackTop;

EFI_STATUS
EFIAPI
ArmCallRing3 (
IN RING3_CALL_DATA *Data,
IN UINTN StackPointer,
IN UINTN UserStackTop,
IN VOID *EntryPoint,
IN UINTN SysCallStack,
IN UINTN SysCallStackTop,
IN VOID *CoreStack,
IN UINTN UserPageTable
);
Expand Down Expand Up @@ -74,7 +74,9 @@ SysCallBootService (

Status = CallBootService (
Type,
(UINTN *)((UINTN)Physical + sizeof (UINTN))
(UINTN *)((UINTN)Physical + sizeof (UINTN)),
mUserStackTop,
mSysCallStackTop
);

CoreFreePages (Physical, EFI_SIZE_TO_PAGES (9 * sizeof (UINTN)));
Expand Down Expand Up @@ -173,8 +175,15 @@ CallRing3 (
IN UINTN SysCallStackTop
)
{
mRing3CallStackTop = UserStackTop;
mCoreSysCallStackTop = SysCallStackTop;

return ArmCallRing3 (Data, UserStackTop, gRing3EntryPoint, SysCallStackTop, &mCoreSp, gUserPageTable);
mUserStackTop = UserStackTop;
mSysCallStackTop = SysCallStackTop;

return ArmCallRing3 (
Data,
UserStackTop,
gRing3EntryPoint,
SysCallStackTop,
&mCoreSp,
gUserPageTable
);
}
2 changes: 1 addition & 1 deletion MdeModulePkg/Core/Dxe/SysCall/ARM/CoreBootServices.S
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ ASM_FUNC(ArmCallRing3)
// Set SPSR M[3:0] bits to User mode.
and R4, R4, #0xFFFFFFF0

// Save Core SP and switch to CoreSysCall Stack.
// Save Core SP and switch to SysCallStackTop.
mov R5, SP
str R5, [R6]
mov SP, R3
Expand Down
27 changes: 18 additions & 9 deletions MdeModulePkg/Core/Dxe/SysCall/ARM/InitializeARM.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
#include "DxeMain.h"

STATIC UINTN mCoreSp;
STATIC UINTN mUserStackTop;
STATIC UINTN mSysCallStackTop;
UINTN gUserPageTable;
UINTN mRing3CallStackTop;
UINTN mCoreSysCallStackTop;

EFI_STATUS
EFIAPI
ArmCallRing3 (
IN RING3_CALL_DATA *Data,
IN UINTN StackPointer,
IN UINTN UserStackTop,
IN VOID *EntryPoint,
IN UINTN SysCallStack,
IN UINTN SysCallStackTop,
IN VOID *CoreStack,
IN UINTN UserPageTable
);
Expand Down Expand Up @@ -80,7 +80,9 @@ SysCallBootService (

Status = CallBootService (
Type,
(UINTN *)((UINTN)Physical + sizeof (UINTN))
(UINTN *)((UINTN)Physical + sizeof (UINTN)),
mUserStackTop,
mSysCallStackTop
);
//
// TODO: Fix memory leak for ReturnToCore().
Expand Down Expand Up @@ -168,8 +170,15 @@ CallRing3 (
IN UINTN SysCallStackTop
)
{
mRing3CallStackTop = UserStackTop;
mCoreSysCallStackTop = SysCallStackTop;

return ArmCallRing3 (Data, UserStackTop, gRing3EntryPoint, SysCallStackTop, &mCoreSp, gUserPageTable);
mUserStackTop = UserStackTop;
mSysCallStackTop = SysCallStackTop;

return ArmCallRing3 (
Data,
UserStackTop,
gRing3EntryPoint,
SysCallStackTop,
&mCoreSp,
gUserPageTable
);
}
11 changes: 5 additions & 6 deletions MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#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 @@ -311,7 +308,9 @@ EFI_STATUS
EFIAPI
CallBootService (
IN UINT8 Type,
IN UINTN *UserArguments
IN UINTN *UserArguments,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop
)
{
EFI_STATUS Status;
Expand Down Expand Up @@ -504,8 +503,8 @@ CallBootService (
UserDriver->CoreWrapper = CoreArgList[Index + 1];
UserDriver->UserSpaceDriver = UserArgList[Index + 1];
UserDriver->UserPageTable = gUserPageTable;
UserDriver->SysCallStackTop = mCoreSysCallStackTop;
UserDriver->UserStackTop = mRing3CallStackTop;
UserDriver->UserStackTop = UserStackTop;
UserDriver->SysCallStackTop = SysCallStackTop;

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

Expand Down
27 changes: 12 additions & 15 deletions MdeModulePkg/Core/Dxe/SysCall/IA32/CoreBootServices.nasm
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ ASM_PFX(CoreBootServices):

; Prepare CallBootService arguments.
mov ebp, esp
push dword [ASM_PFX(SysCallStackTop)]
push dword [ASM_PFX(UserStackTop)]
add edx, 4 ; User Arguments[]
push edx
push ecx ; Type
Expand All @@ -150,7 +152,7 @@ ASM_PFX(CoreBootServices):
pop eax

; Step over CallBootService input.
add esp, 4*2
mov esp, ebp

; Prepare SYSEXIT arguments.
pop edx ; User return address.
Expand All @@ -169,7 +171,7 @@ ASM_PFX(CoreBootServices):
; IN UINTN SysCallStackTop
; );
;
; (On User Stack) Data
; (On User Stack) Data, UserStackTop, SysCallStackTop
;------------------------------------------------------------------------------
global ASM_PFX(CallRing3)
ASM_PFX(CallRing3):
Expand All @@ -183,23 +185,20 @@ 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 ebx, [esp + 4 * 6]
mov [ASM_PFX(UserStackTop)], ebx
mov ebx, [esp + 4 * 7]
mov [ASM_PFX(SysCallStackTop)], ebx
mov edx, 0
mov eax, ebx
mov ecx, MSR_IA32_SYSENTER_ESP
wrmsr

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

SetRing3DataSegmentSelectors

; Prepare SYSEXIT arguments.
pop ecx
pop edx
mov ecx, [ASM_PFX(UserStackTop)]
mov edx, [ASM_PFX(gRing3EntryPoint)]
mov eax, [esp + 4 * 5] ; Data

; Switch to User Stack.
Expand Down Expand Up @@ -251,10 +250,8 @@ ALIGN 4096
ASM_PFX(CoreEsp):
resd 1

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

global ASM_PFX(mCoreSysCallStackTop)
ASM_PFX(mCoreSysCallStackTop):
ASM_PFX(SysCallStackTop):
resd 1
20 changes: 9 additions & 11 deletions MdeModulePkg/Core/Dxe/SysCall/X64/CoreBootServices.nasm
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ ASM_PFX(CoreBootServices):
mov gs, ax

; Save User Stack pointers and switch to Core SysCall Stack.
mov rax, [ASM_PFX(mCoreSysCallStackTop)]
mov rax, [ASM_PFX(SysCallStackTop)]
sub rax, 8
mov [rax], rsp
mov rsp, rax
Expand All @@ -162,6 +162,8 @@ ASM_PFX(CoreBootServices):
mov rcx, r10 ; Type
mov rdx, [rbp + 8*3]
add rdx, 8 ; User Arguments[]
mov r8, [ASM_PFX(UserStackTop)]
mov r9, [ASM_PFX(SysCallStackTop)]

sti
call ASM_PFX(CallBootService)
Expand Down Expand Up @@ -221,20 +223,18 @@ ASM_PFX(CallRing3):
mov [ASM_PFX(CoreRsp)], rsp

; Save input Arguments.
mov [ASM_PFX(mRing3CallStackTop)], rdx
mov [ASM_PFX(mCoreSysCallStackTop)], r8
mov r8, [ASM_PFX(mRing3CallStackTop)]
mov r9, [ASM_PFX(gRing3EntryPoint)]
mov [ASM_PFX(UserStackTop)], rdx
mov [ASM_PFX(SysCallStackTop)], r8
mov r10, rcx

SetRing3DataSegmentSelectors

; Prepare SYSRET arguments.
mov rdx, r10
mov rcx, r9
mov rcx, [ASM_PFX(gRing3EntryPoint)]

; Switch to User Stack.
mov rsp, r8
mov rsp, [ASM_PFX(UserStackTop)]
mov rbp, rsp

mov r8, [ASM_PFX(gUserPageTable)]
Expand Down Expand Up @@ -284,10 +284,8 @@ ALIGN 4096
ASM_PFX(CoreRsp):
resq 1

global ASM_PFX(mRing3CallStackTop)
ASM_PFX(mRing3CallStackTop):
ASM_PFX(UserStackTop):
resq 1

global ASM_PFX(mCoreSysCallStackTop)
ASM_PFX(mCoreSysCallStackTop):
ASM_PFX(SysCallStackTop):
resq 1

0 comments on commit 708ea60

Please sign in to comment.