Skip to content

Commit

Permalink
Ring3: Refactored User and SysCall stacks allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Jan 10, 2025
1 parent cd2cf69 commit 90ecffb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 71 deletions.
16 changes: 10 additions & 6 deletions MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
///
#define DEPEX_STACK_SIZE_INCREMENT 0x1000

#define USER_STACK_SIZE 0x20000
#define STACK_SIZE 0x20000
#define RING3_INTERFACES_PAGES 20

typedef struct {
Expand Down Expand Up @@ -230,6 +230,8 @@ typedef struct {
VOID *HiiData;
BOOLEAN IsUserImage;
UINTN UserPageTable;
UINTN SysCallStackTop;
UINTN UserStackTop;
} LOADED_IMAGE_PRIVATE_DATA;

typedef struct {
Expand Down Expand Up @@ -284,10 +286,8 @@ extern LOADED_IMAGE_PRIVATE_DATA * mCurrentImage;

extern RING3_DATA *gRing3Data;
extern VOID *gRing3Interfaces;
extern VOID *gCoreSysCallStackBase;
extern VOID *gCoreSysCallStackTop;
extern VOID *gRing3CallStackBase;
extern VOID *gRing3CallStackTop;
extern UINTN gCoreSysCallStackTop;
extern UINTN gRing3CallStackTop;
extern VOID *gRing3EntryPoint;
extern UINTN gUserPageTable;
extern UINTN gCorePageTable;
Expand Down Expand Up @@ -2786,7 +2786,11 @@ FreeProtocolsList (
UINTN
EFIAPI
InitializeUserPageTable (
IN LOADED_IMAGE_PRIVATE_DATA *Image
IN LOADED_IMAGE_PRIVATE_DATA *Image,
IN UINTN SysCallStackBase,
IN UINTN SysCallStackSize,
IN UINTN UserStackBase,
IN UINTN UserStackSize
);

#endif
10 changes: 0 additions & 10 deletions MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,16 +791,6 @@ CoreExitBootServices (
RING3_INTERFACES_PAGES
);

CoreFreePages (
(EFI_PHYSICAL_ADDRESS)(UINTN)gCoreSysCallStackBase,
EFI_SIZE_TO_PAGES (USER_STACK_SIZE)
);

CoreFreePages (
(EFI_PHYSICAL_ADDRESS)(UINTN)gRing3CallStackBase,
EFI_SIZE_TO_PAGES (USER_STACK_SIZE)
);

FreeProtocolsList ();
}

Expand Down
44 changes: 42 additions & 2 deletions MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,30 @@ CoreUnloadAndCloseImage (
CoreFreePool (Image);
}

STATIC
UINTN
EFIAPI
AllocateStack (
IN UINTN Size,
OUT UINTN *Base
)
{
UINTN TopOfStack;

ASSERT (Base != NULL);
ASSERT (IS_ALIGNED (Size, EFI_PAGE_SIZE));

*Base = (UINTN)AllocatePages (EFI_SIZE_TO_PAGES (Size));
ASSERT (*Base != 0);
//
// Compute the top of the allocated stack. Pre-allocate a UINTN for safety.
//
TopOfStack = *Base + Size - CPU_STACK_ALIGNMENT;
TopOfStack = ALIGN_VALUE (TopOfStack, CPU_STACK_ALIGNMENT);

return TopOfStack;
}

/**
Loads an EFI image into memory and returns a handle to the image.
Expand Down Expand Up @@ -1108,6 +1132,8 @@ CoreLoadImageCommon (
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
UINT8 ImageOrigin;
EFI_FV_FILE_ATTRIBUTES FileAttributes;
UINTN SysCallStackBase;
UINTN UserStackBase;

SecurityStatus = EFI_SUCCESS;

Expand Down Expand Up @@ -1445,7 +1471,19 @@ CoreLoadImageCommon (
ProtectUefiImage (&Image->Info, ImageOrigin, &ImageContext, Image->IsUserImage);

if ((gRing3Data != NULL) && Image->IsUserImage) {
Image->UserPageTable = InitializeUserPageTable (Image);
Image->SysCallStackTop = AllocateStack (STACK_SIZE, &SysCallStackBase);
SetUefiImageMemoryAttributes (SysCallStackBase, STACK_SIZE, EFI_MEMORY_XP);

Image->UserStackTop = AllocateStack (STACK_SIZE, &UserStackBase);
SetUefiImageMemoryAttributes (UserStackBase, STACK_SIZE, EFI_MEMORY_XP | EFI_MEMORY_USER);

Image->UserPageTable = InitializeUserPageTable (
Image,
SysCallStackBase,
STACK_SIZE,
UserStackBase,
STACK_SIZE
);
}

RegisterMemoryProfileImage (
Expand Down Expand Up @@ -1703,7 +1741,9 @@ CoreStartImage (
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(UINTN)Image->EntryPoint, &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);

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

Image->Status = GoToRing3 (
2,
Expand Down
4 changes: 2 additions & 2 deletions MdeModulePkg/Core/Dxe/SysCall/AARCH64/InitializeAARCH64.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ EFI_STATUS
EFIAPI
ArmCallRing3 (
IN RING3_CALL_DATA *Data,
IN VOID *StackPointer,
IN UINTN StackPointer,
IN VOID *EntryPoint,
IN VOID *SysCallStack,
IN UINTN SysCallStack,
IN VOID *CoreStack,
IN UINTN UserPageTable
);
Expand Down
4 changes: 2 additions & 2 deletions MdeModulePkg/Core/Dxe/SysCall/ARM/InitializeARM.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ EFI_STATUS
EFIAPI
ArmCallRing3 (
IN RING3_CALL_DATA *Data,
IN VOID *StackPointer,
IN UINTN StackPointer,
IN VOID *EntryPoint,
IN VOID *SysCallStack,
IN UINTN SysCallStack,
IN VOID *CoreStack,
IN UINTN UserPageTable
);
Expand Down
62 changes: 13 additions & 49 deletions MdeModulePkg/Core/Dxe/SysCall/Initialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

#include "DxeMain.h"

VOID *gCoreSysCallStackTop;
VOID *gCoreSysCallStackBase;
VOID *gRing3CallStackTop;
VOID *gRing3CallStackBase;
UINTN gCoreSysCallStackTop;
UINTN gRing3CallStackTop;
VOID *gRing3EntryPoint;
RING3_DATA *gRing3Data;
VOID *gRing3Interfaces;
Expand Down Expand Up @@ -48,8 +46,6 @@ InitializeRing3 (
)
{
EFI_STATUS Status;
VOID *TopOfStack;
UINTN SizeOfStack;
EFI_PHYSICAL_ADDRESS Physical;
UINTN Index;
EFI_CONFIGURATION_TABLE *Conf;
Expand Down Expand Up @@ -157,42 +153,6 @@ InitializeRing3 (
EFI_MEMORY_XP | EFI_MEMORY_USER
);

SizeOfStack = EFI_SIZE_TO_PAGES (USER_STACK_SIZE) * EFI_PAGE_SIZE;

//
// Allocate 128KB for the Core SysCall Stack.
//
gCoreSysCallStackBase = AllocatePages (EFI_SIZE_TO_PAGES (USER_STACK_SIZE));
ASSERT (gCoreSysCallStackBase != NULL);

//
// Compute the top of the allocated stack. Pre-allocate a UINTN for safety.
//
TopOfStack = (VOID *)((UINTN)gCoreSysCallStackBase + SizeOfStack - CPU_STACK_ALIGNMENT);
TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);

gCoreSysCallStackTop = TopOfStack;

SetUefiImageMemoryAttributes ((UINTN)gCoreSysCallStackBase, SizeOfStack, EFI_MEMORY_XP);
DEBUG ((DEBUG_ERROR, "Core: gCoreSysCallStackTop = %p\n", gCoreSysCallStackTop));

//
// Allocate 128KB for the User Stack.
//
gRing3CallStackBase = AllocatePages (EFI_SIZE_TO_PAGES (USER_STACK_SIZE));
ASSERT (gRing3CallStackBase != NULL);

//
// Compute the top of the allocated stack. Pre-allocate a UINTN for safety.
//
TopOfStack = (VOID *)((UINTN)gRing3CallStackBase + SizeOfStack - CPU_STACK_ALIGNMENT);
TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);

gRing3CallStackTop = TopOfStack;

SetUefiImageMemoryAttributes ((UINTN)gRing3CallStackBase, SizeOfStack, EFI_MEMORY_XP | EFI_MEMORY_USER);
DEBUG ((DEBUG_ERROR, "Core: gRing3CallStackTop = %p\n", gRing3CallStackTop));

InitializeMsr (
gRing3Data->SystemTable.ConfigurationTable,
gRing3Data->SystemTable.NumberOfTableEntries
Expand All @@ -206,7 +166,11 @@ InitializeRing3 (
UINTN
EFIAPI
InitializeUserPageTable (
IN LOADED_IMAGE_PRIVATE_DATA *Image
IN LOADED_IMAGE_PRIVATE_DATA *Image,
IN UINTN SysCallStackBase,
IN UINTN SysCallStackSize,
IN UINTN UserStackBase,
IN UINTN UserStackSize
)
{
UINTN UserPageTable;
Expand All @@ -222,7 +186,7 @@ InitializeUserPageTable (
MakeUserPageTableTemplate (&UserPageTable, &UserPageTableSize);

//
// Map gRing3Data, gRing3Interfaces, gRing3CallStackBase, DxeRing3
// Map gRing3Data, gRing3Interfaces, UserStackBase, DxeRing3
//
gCpu->SetUserMemoryAttributes (
gCpu,
Expand All @@ -243,8 +207,8 @@ InitializeUserPageTable (
gCpu->SetUserMemoryAttributes (
gCpu,
UserPageTable,
(UINTN)gRing3CallStackBase,
EFI_SIZE_TO_PAGES (USER_STACK_SIZE) * EFI_PAGE_SIZE,
UserStackBase,
UserStackSize,
EFI_MEMORY_XP | EFI_MEMORY_USER
);

Expand All @@ -264,7 +228,7 @@ InitializeUserPageTable (
}

//
// Map CoreBootServices, gCoreSysCallStackBase
// Map CoreBootServices, SysCallStackBase
//
gCpu->SetUserMemoryAttributes (
gCpu,
Expand All @@ -277,8 +241,8 @@ InitializeUserPageTable (
gCpu->SetUserMemoryAttributes (
gCpu,
UserPageTable,
(UINTN)gCoreSysCallStackBase,
EFI_SIZE_TO_PAGES (USER_STACK_SIZE) * EFI_PAGE_SIZE,
SysCallStackBase,
SysCallStackSize,
EFI_MEMORY_XP
);

Expand Down

0 comments on commit 90ecffb

Please sign in to comment.