Skip to content

Commit

Permalink
Ring3: Refactored out AllocateRing3Copy().
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Feb 29, 2024
1 parent 1cbd136 commit 759d0b9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 70 deletions.
8 changes: 0 additions & 8 deletions MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -1165,14 +1165,6 @@ CoreAllocatePages (
IN OUT EFI_PHYSICAL_ADDRESS *Memory
);

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

/**
Frees previous allocated pages.
Expand Down
24 changes: 0 additions & 24 deletions MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1570,30 +1570,6 @@ CoreLoadImage (
return Status;
}

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

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

ASSERT (CopySize <= AllocationSize);

CopyMem (MemoryRing3, Source, CopySize);

return MemoryRing3;
}

EFI_STATUS
EFIAPI
InitializeRing3 (
Expand Down
12 changes: 7 additions & 5 deletions MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ PrepareRing3Interface (

Ring3Limit = (UINTN)gRing3Interfaces + EFI_PAGES_TO_SIZE (RING3_INTERFACES_PAGES);

ASSERT ((mRing3InterfacePointer + sizeof (EFI_GUID) + CoreSize) <= Ring3Limit);

CopyMem ((VOID *)mRing3InterfacePointer, (VOID *)Guid, sizeof (EFI_GUID));
mRing3InterfacePointer += sizeof (EFI_GUID);
ASSERT ((mRing3InterfacePointer + CoreSize) <= Ring3Limit);

Ring3Interface = (VOID *)mRing3InterfacePointer;

Expand Down Expand Up @@ -265,7 +262,12 @@ CallBootService (
Status = FindGuid ((EFI_GUID *)UserArgList[Index], (EFI_GUID **)&CoreArgList[Index], &MemoryCoreSize);
if (EFI_ERROR (Status)) {
EnableSMAP ();
//TODO: Free CoreArgList.

while (Index > 0) {
FreePool (CoreArgList[Index - 1]);
Index -= 2;
}

return Status;
}

Expand Down
67 changes: 34 additions & 33 deletions MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ GoToRing3 (
return Status;
}

STATIC
EFIAPI
VOID *
Ring3Copy (
IN VOID *Core,
IN UINT32 Size
)
{
EFI_STATUS Status;
VOID *Ring3;

Status = CoreAllocatePages (
AllocateAnyPages,
EfiRing3MemoryType,
1,
(EFI_PHYSICAL_ADDRESS *)&Ring3
);
if (EFI_ERROR (Status)) {
return NULL;
}

DisableSMAP ();
CopyMem (Ring3, Core, Size);
EnableSMAP ();

return Ring3;
}

EFI_STATUS
EFIAPI
CoreDriverBindingSupported (
Expand All @@ -87,17 +115,10 @@ CoreDriverBindingSupported (
{
EFI_STATUS Status;

DisableSMAP ();
This = AllocateRing3Copy (
This,
sizeof (EFI_DRIVER_BINDING_PROTOCOL),
sizeof (EFI_DRIVER_BINDING_PROTOCOL)
);
This = Ring3Copy (This, sizeof (EFI_DRIVER_BINDING_PROTOCOL));
if (This == NULL) {
EnableSMAP ();
return EFI_OUT_OF_RESOURCES;
}
EnableSMAP ();

Status = GoToRing3 (
3,
Expand All @@ -107,9 +128,7 @@ CoreDriverBindingSupported (
RemainingDevicePath
);

DisableSMAP ();
FreePool (This);
EnableSMAP ();
CoreFreePages ((EFI_PHYSICAL_ADDRESS)This, 1);

return Status;
}
Expand All @@ -124,17 +143,10 @@ CoreDriverBindingStart (
{
EFI_STATUS Status;

DisableSMAP ();
This = AllocateRing3Copy (
This,
sizeof (EFI_DRIVER_BINDING_PROTOCOL),
sizeof (EFI_DRIVER_BINDING_PROTOCOL)
);
This = Ring3Copy (This, sizeof (EFI_DRIVER_BINDING_PROTOCOL));
if (This == NULL) {
EnableSMAP ();
return EFI_OUT_OF_RESOURCES;
}
EnableSMAP ();

Status = GoToRing3 (
3,
Expand All @@ -144,9 +156,7 @@ CoreDriverBindingStart (
RemainingDevicePath
);

DisableSMAP ();
FreePool (This);
EnableSMAP ();
CoreFreePages ((EFI_PHYSICAL_ADDRESS)This, 1);

return Status;
}
Expand All @@ -162,17 +172,10 @@ CoreDriverBindingStop (
{
EFI_STATUS Status;

DisableSMAP ();
This = AllocateRing3Copy (
This,
sizeof (EFI_DRIVER_BINDING_PROTOCOL),
sizeof (EFI_DRIVER_BINDING_PROTOCOL)
);
This = Ring3Copy (This, sizeof (EFI_DRIVER_BINDING_PROTOCOL));
if (This == NULL) {
EnableSMAP ();
return EFI_OUT_OF_RESOURCES;
}
EnableSMAP ();

Status = GoToRing3 (
4,
Expand All @@ -183,9 +186,7 @@ CoreDriverBindingStop (
ChildHandleBuffer
);

DisableSMAP ();
FreePool (This);
EnableSMAP ();
CoreFreePages ((EFI_PHYSICAL_ADDRESS)This, 1);

return Status;
}
Expand Down

0 comments on commit 759d0b9

Please sign in to comment.