Skip to content

Commit

Permalink
Ring3: Refactored AllocateRing3CopyPages() and mUserDriverBinding.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Feb 16, 2024
1 parent a388a3e commit ebd30e0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
7 changes: 4 additions & 3 deletions MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -1170,9 +1170,10 @@ CoreAllocatePages (

VOID *
EFIAPI
AllocateRing3CopyPages (
IN VOID *MemoryCore,
IN UINT32 MemoryCoreSize
AllocateRing3Copy (
IN VOID *Source,
IN UINT32 AllocationSize,
IN UINT32 CopySize
);

/**
Expand Down
28 changes: 18 additions & 10 deletions MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1571,21 +1571,24 @@ CoreLoadImage (

VOID *
EFIAPI
AllocateRing3CopyPages (
IN VOID *MemoryCore,
IN UINT32 MemoryCoreSize
AllocateRing3Copy (
IN VOID *Source,
IN UINT32 AllocationSize,
IN UINT32 CopySize
)
{
VOID *MemoryRing3;
EFI_STATUS Status;
VOID *MemoryRing3;

MemoryRing3 = AllocatePages (EFI_SIZE_TO_PAGES (MemoryCoreSize));
if (MemoryRing3 == NULL) {
Status = CoreAllocatePool (EfiRing3MemoryType, AllocationSize, &MemoryRing3);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Core: Failed to allocate %d bytes for Ring3.\n", AllocationSize));
return NULL;
}

CopyMem (MemoryRing3, MemoryCore, MemoryCoreSize);
ASSERT (CopySize <= AllocationSize);

SetUefiImageMemoryAttributes ((UINTN)MemoryRing3, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (MemoryCoreSize)), EFI_MEMORY_USER);
CopyMem (MemoryRing3, Source, CopySize);

return MemoryRing3;
}
Expand Down Expand Up @@ -1613,8 +1616,13 @@ InitializeRing3 (
//
// Set Ring3 EntryPoint and BootServices.
//
mRing3Data = AllocateRing3CopyPages ((VOID *)Image->Info.SystemTable, sizeof (RING3_DATA));

mRing3Data = AllocateRing3Copy (
(VOID *)Image->Info.SystemTable,
sizeof (RING3_DATA),
sizeof (EFI_SYSTEM_TABLE)
);
ASSERT (mRing3Data != NULL);

Image->Status = Image->EntryPoint (ImageHandle, (EFI_SYSTEM_TABLE *)mRing3Data);

gRing3EntryPoint = mRing3Data->EntryPoint;
Expand Down
16 changes: 9 additions & 7 deletions MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ CallBootService (
&Interface
);

Interface = AllocateRing3CopyPages (Interface, MemoryCoreSize);
DisableSMAP ();
Interface = AllocateRing3Copy (Interface, MemoryCoreSize, MemoryCoreSize);
if (Interface == NULL) {
DEBUG ((DEBUG_ERROR, "Ring0: Failed to allocate pages for Ring3 PROTOCOL structure.\n"));
EnableSMAP ();
return EFI_OUT_OF_RESOURCES;
}

DisableSMAP ();
*(VOID **)CoreRbp->Argument3 = Interface;
EnableSMAP ();

Expand Down Expand Up @@ -137,13 +138,14 @@ CallBootService (
Argument6
);

Interface = AllocateRing3CopyPages (Interface, MemoryCoreSize);
DisableSMAP ();
Interface = AllocateRing3Copy (Interface, MemoryCoreSize, MemoryCoreSize);
if (Interface == NULL) {
DEBUG ((DEBUG_ERROR, "Ring0: Failed to allocate pages for Ring3 PROTOCOL structure.\n"));
EnableSMAP ();
return EFI_OUT_OF_RESOURCES;
}

DisableSMAP ();
*(VOID **)CoreRbp->Argument3 = Interface;
EnableSMAP ();

Expand Down Expand Up @@ -184,9 +186,9 @@ CallBootService (
if (CompareGuid ((EFI_GUID *)CoreArgList[Index], &gEfiDriverBindingProtocolGuid)) {
CoreDriverBinding = (EFI_DRIVER_BINDING_PROTOCOL *)CoreArgList[Index + 1];

mUserDriverBindingSupported = CoreDriverBinding->Supported;
mUserDriverBindingStart = CoreDriverBinding->Start;
mUserDriverBindingStop = CoreDriverBinding->Stop;
mRing3DriverBindingProtocol.Supported = CoreDriverBinding->Supported;
mRing3DriverBindingProtocol.Start = CoreDriverBinding->Start;
mRing3DriverBindingProtocol.Stop = CoreDriverBinding->Stop;

CoreDriverBinding->Supported = CoreDriverBindingSupported;
CoreDriverBinding->Start = CoreDriverBindingStart;
Expand Down
10 changes: 4 additions & 6 deletions MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

#include "DxeMain.h"

EFI_DRIVER_BINDING_SUPPORTED mUserDriverBindingSupported;
EFI_DRIVER_BINDING_START mUserDriverBindingStart;
EFI_DRIVER_BINDING_STOP mUserDriverBindingStop;
EFI_DRIVER_BINDING_PROTOCOL mRing3DriverBindingProtocol;

EFI_STATUS
EFIAPI
Expand Down Expand Up @@ -59,7 +57,7 @@ CoreDriverBindingSupported (
{
return GoToRing3 (
3,
(VOID *)mUserDriverBindingSupported,
(VOID *)mRing3DriverBindingProtocol.Supported,
This,
ControllerHandle,
RemainingDevicePath
Expand All @@ -76,7 +74,7 @@ CoreDriverBindingStart (
{
return GoToRing3 (
3,
(VOID *)mUserDriverBindingStart,
(VOID *)mRing3DriverBindingProtocol.Start,
This,
ControllerHandle,
RemainingDevicePath
Expand All @@ -94,7 +92,7 @@ CoreDriverBindingStop (
{
return GoToRing3 (
4,
(VOID *)mUserDriverBindingStop,
(VOID *)mRing3DriverBindingProtocol.Stop,
This,
ControllerHandle,
NumberOfChildren,
Expand Down
4 changes: 1 addition & 3 deletions MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
**/

extern EFI_DRIVER_BINDING_SUPPORTED mUserDriverBindingSupported;
extern EFI_DRIVER_BINDING_START mUserDriverBindingStart;
extern EFI_DRIVER_BINDING_STOP mUserDriverBindingStop;
extern EFI_DRIVER_BINDING_PROTOCOL mRing3DriverBindingProtocol;

EFI_STATUS
EFIAPI
Expand Down

0 comments on commit ebd30e0

Please sign in to comment.