Skip to content

Commit

Permalink
Ring3: Refactored out CoreRsp and UserStackTop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Jan 16, 2025
1 parent 708ea60 commit cd65706
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 194 deletions.
22 changes: 15 additions & 7 deletions MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ typedef struct {
UINTN UserStackTop;
} LOADED_IMAGE_PRIVATE_DATA;

typedef struct {
VOID *CoreWrapper;
VOID *UserSpaceDriver;
UINTN UserPageTable;
UINTN UserStackTop;
UINTN SysCallStackTop;
UINTN ReturnSP;
LIST_ENTRY Link;
} USER_SPACE_DRIVER;

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

Expand Down Expand Up @@ -278,6 +288,7 @@ extern VOID *gRing3Interfaces;
extern VOID *gRing3EntryPoint;
extern UINTN gUserPageTable;
extern UINTN gCorePageTable;
extern LIST_ENTRY gUserSpaceDriversHead;

//
// Service Initialization Functions
Expand Down Expand Up @@ -2727,9 +2738,7 @@ EFI_STATUS
EFIAPI
CallBootService (
IN UINT8 Type,
IN UINTN *UserArguments,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop
IN UINTN *UserArguments
);

VOID
Expand All @@ -2747,10 +2756,9 @@ ForbidSupervisorAccessToUserMemory (
EFI_STATUS
EFIAPI
GoToRing3 (
IN UINT8 Number,
IN VOID *EntryPoint,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop,
IN UINT8 Number,
IN VOID *EntryPoint,
IN USER_SPACE_DRIVER *UserDriver,
...
);

Expand Down
13 changes: 11 additions & 2 deletions MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,7 @@ CoreStartImage (
UINTN SetJumpFlag;
EFI_HANDLE Handle;
UINT64 Attributes;
USER_SPACE_DRIVER *UserDriver;

Handle = ImageHandle;

Expand Down Expand Up @@ -1743,11 +1744,19 @@ CoreStartImage (

gUserPageTable = Image->UserPageTable;

UserDriver = AllocatePool (sizeof (USER_SPACE_DRIVER));
UserDriver->CoreWrapper = NULL;
UserDriver->UserSpaceDriver = (VOID *)Image->EntryPoint;
UserDriver->UserPageTable = Image->UserPageTable;
UserDriver->UserStackTop = Image->UserStackTop;
UserDriver->SysCallStackTop = Image->SysCallStackTop;

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

Image->Status = GoToRing3 (
2,
(VOID *)Image->EntryPoint,
Image->UserStackTop,
Image->SysCallStackTop,
UserDriver,
ImageHandle,
gRing3Data
);
Expand Down
14 changes: 8 additions & 6 deletions MdeModulePkg/Core/Dxe/SysCall/AARCH64/CoreBootServices.S
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
//
// Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
// Copyright (c) 2024 - 2025, Mikhail Krichanov. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
//
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -62,14 +62,15 @@ ASM_FUNC_ALIGN(SysCallBase, 4096)
// CallRing3 (
// IN RING3_CALL_DATA *Data,
// IN UINTN UserStackTop,
// IN UINTN SysCallStackTop
// IN UINTN SysCallStackTop,
// IN UINTN *ReturnSP
// );
//
// (x0) Data
// (x1) UserStackTop
// (x2) gRing3EntryPoint
// (x3) SysCallStackTop
// (x4) &CoreSp
// (x4) ReturnSP
// (x5) gUserPageTable
//------------------------------------------------------------------------------
ASM_FUNC(ArmCallRing3)
Expand Down Expand Up @@ -121,13 +122,14 @@ ASM_FUNC_ALIGN(SysCallEnd, 4096)
// VOID
// EFIAPI
// ReturnToCore (
// IN EFI_STATUS Status
// IN EFI_STATUS Status,
// IN UINTN ReturnSP
// );
//
// (x0) Status
// (x1) mCoreSp
// (x1) ReturnSP
//------------------------------------------------------------------------------
ASM_FUNC(ArmReturnToCore)
ASM_FUNC(ReturnToCore)
// Switch to Core Stack.
mov sp, x1
// Restore registers and Stack.
Expand Down
32 changes: 6 additions & 26 deletions MdeModulePkg/Core/Dxe/SysCall/AARCH64/InitializeAARCH64.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
Copyright (c) 2024 - 2025, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
**/
Expand All @@ -12,8 +12,6 @@

#include "DxeMain.h"

STATIC UINTN mCoreSp;
STATIC UINTN mUserStackTop;
STATIC UINTN mSysCallStackTop;
UINTN gUserPageTable;

Expand All @@ -24,26 +22,10 @@ ArmCallRing3 (
IN UINTN UserStackTop,
IN VOID *EntryPoint,
IN UINTN SysCallStackTop,
IN VOID *CoreStack,
IN UINTN *ReturnSP,
IN UINTN UserPageTable
);

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

VOID
EFIAPI
ReturnToCore (
IN EFI_STATUS Status
)
{
ArmReturnToCore (Status, mCoreSp);
}

STATIC
EFI_STATUS
EFIAPI
Expand Down Expand Up @@ -74,9 +56,7 @@ SysCallBootService (

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

CoreFreePages (Physical, EFI_SIZE_TO_PAGES (9 * sizeof (UINTN)));
Expand Down Expand Up @@ -172,18 +152,18 @@ EFIAPI
CallRing3 (
IN RING3_CALL_DATA *Data,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop
IN UINTN SysCallStackTop,
IN UINTN *ReturnSP
)
{
mUserStackTop = UserStackTop;
mSysCallStackTop = SysCallStackTop;

return ArmCallRing3 (
Data,
UserStackTop,
gRing3EntryPoint,
SysCallStackTop,
&mCoreSp,
ReturnSP,
gUserPageTable
);
}
16 changes: 9 additions & 7 deletions MdeModulePkg/Core/Dxe/SysCall/ARM/CoreBootServices.S
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
//
// Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
// Copyright (c) 2024 - 2025, Mikhail Krichanov. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
//
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -58,20 +58,21 @@ ASM_FUNC_ALIGN(SysCallBase, 4096)
// CallRing3 (
// IN RING3_CALL_DATA *Data,
// IN UINTN UserStackTop,
// IN UINTN SysCallStackTop
// IN UINTN SysCallStackTop,
// IN UINTN *ReturnSP
// );
//
// (r0) Data
// (r1) UserStackTop
// (r2) gRing3EntryPoint
// (r3) SysCallStackTop
//
// (On Core Stack) &CoreSp, gUserPageTable
// (On Core Stack) ReturnSP, gUserPageTable
//------------------------------------------------------------------------------
ASM_FUNC(ArmCallRing3)
// Save registers.
push {R4-R12, LR}
// R6 is &CoreSp
// R6 is ReturnSP
ldr R6, [SP, #0x28]
// R7 is gUserPageTable
ldr R7, [SP, #0x2C]
Expand Down Expand Up @@ -118,13 +119,14 @@ ASM_FUNC_ALIGN(SysCallEnd, 4096)
// VOID
// EFIAPI
// ReturnToCore (
// IN EFI_STATUS Status
// IN EFI_STATUS Status,
// IN UINTN ReturnSP
// );
//
// (r0) Status
// (r1) mCoreSp
// (r1) ReturnSP
//------------------------------------------------------------------------------
ASM_FUNC(ArmReturnToCore)
ASM_FUNC(ReturnToCore)
// Switch to Core Stack.
mov SP, R1

Expand Down
32 changes: 6 additions & 26 deletions MdeModulePkg/Core/Dxe/SysCall/ARM/InitializeARM.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
Copyright (c) 2024 - 2025, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
**/
Expand All @@ -11,8 +11,6 @@

#include "DxeMain.h"

STATIC UINTN mCoreSp;
STATIC UINTN mUserStackTop;
STATIC UINTN mSysCallStackTop;
UINTN gUserPageTable;

Expand All @@ -23,26 +21,10 @@ ArmCallRing3 (
IN UINTN UserStackTop,
IN VOID *EntryPoint,
IN UINTN SysCallStackTop,
IN VOID *CoreStack,
IN UINTN *ReturnSP,
IN UINTN UserPageTable
);

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

VOID
EFIAPI
ReturnToCore (
IN EFI_STATUS Status
)
{
ArmReturnToCore (Status, mCoreSp);
}

STATIC
EFI_STATUS
EFIAPI
Expand Down Expand Up @@ -80,9 +62,7 @@ SysCallBootService (

Status = CallBootService (
Type,
(UINTN *)((UINTN)Physical + sizeof (UINTN)),
mUserStackTop,
mSysCallStackTop
(UINTN *)((UINTN)Physical + sizeof (UINTN))
);
//
// TODO: Fix memory leak for ReturnToCore().
Expand Down Expand Up @@ -167,18 +147,18 @@ EFIAPI
CallRing3 (
IN RING3_CALL_DATA *Data,
IN UINTN UserStackTop,
IN UINTN SysCallStackTop
IN UINTN SysCallStackTop,
IN UINTN *ReturnSP
)
{
mUserStackTop = UserStackTop;
mSysCallStackTop = SysCallStackTop;

return ArmCallRing3 (
Data,
UserStackTop,
gRing3EntryPoint,
SysCallStackTop,
&mCoreSp,
ReturnSP,
gUserPageTable
);
}
Loading

0 comments on commit cd65706

Please sign in to comment.