Skip to content

Commit

Permalink
Ring3: Added SysCallGetVariable wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Mar 11, 2024
1 parent 73e36a1 commit 239b883
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 16 deletions.
1 change: 1 addition & 0 deletions MdeModulePkg/Core/Dxe/DxeMain.inf
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
gEfiEndOfDxeEventGroupGuid ## SOMETIMES_CONSUMES ## Event
gEfiHobMemoryAllocStackGuid ## SOMETIMES_CONSUMES ## SystemTable
gUefiImageLoaderImageContextGuid ## CONSUMES ## HOB
gEfiGlobalVariableGuid ## SOMETIMES_CONSUMES ## SysCall

[Ppis]
gEfiVectorHandoffInfoPpiGuid ## UNDEFINED # HOB
Expand Down
7 changes: 5 additions & 2 deletions MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryPoolLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>

#include "Ring3.h"

Expand Down Expand Up @@ -200,10 +201,12 @@ Ring3Initialization (

Ring3Data = (RING3_DATA *)SystemTable;

Ring3Data->EntryPoint = (VOID *)Ring3EntryPoint;
Ring3Data->BootServices = &mBootServices;
Ring3Data->EntryPoint = (VOID *)Ring3EntryPoint;
Ring3Data->BootServices = &mBootServices;
Ring3Data->RuntimeServices = &mRuntimeServices;

gBS = &mBootServices;
gRT = &mRuntimeServices;

CoreInitializePool ();

Expand Down
1 change: 1 addition & 0 deletions MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.inf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
MemoryPoolLib
UefiBootServicesTableLib
UefiDriverEntryPoint
UefiRuntimeServicesTableLib

[Protocols]
gEfiDevicePathUtilitiesProtocolGuid ## SOMETIMES_CONSUMES
Expand Down
11 changes: 8 additions & 3 deletions MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiRuntimeServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ Ring3GetVariable (
OUT VOID *Data OPTIONAL
)
{
DEBUG ((DEBUG_ERROR, "Ring3: GetVariable is not supported\n"));

return EFI_UNSUPPORTED;
return SysCall (
SysCallGetVariable,
VariableName,
VendorGuid,
Attributes,
DataSize,
Data
);
}

EFI_STATUS
Expand Down
3 changes: 2 additions & 1 deletion MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,8 @@ InitializeRing3 (

gRing3EntryPoint = gRing3Data->EntryPoint;

gRing3Data->SystemTable.BootServices = gRing3Data->BootServices;
gRing3Data->SystemTable.BootServices = gRing3Data->BootServices;
gRing3Data->SystemTable.RuntimeServices = gRing3Data->RuntimeServices;

Status = CoreAllocatePages (
AllocateAnyPages,
Expand Down
108 changes: 101 additions & 7 deletions MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ FindGuid (
*Core = &gEfiUnicodeCollationProtocolGuid;
*CoreSize = sizeof (EFI_UNICODE_COLLATION_PROTOCOL);

} else if (CompareGuid (Ring3, &gEfiGlobalVariableGuid)) {

*Core = &gEfiGlobalVariableGuid;

} else {
DEBUG ((DEBUG_ERROR, "Ring0: Unknown protocol - %g.\n", Ring3));
return EFI_NOT_FOUND;
Expand Down Expand Up @@ -533,8 +537,8 @@ CallBootService (
//
// Argument 1: EFI_LOCATE_SEARCH_TYPE SearchType
// Argument 2: EFI_GUID *Protocol OPTIONAL
// Argument 3: VOID *SearchKey OPTIONAL,
// Argument 4: UINTN *NumberHandles,
// Argument 3: VOID *SearchKey OPTIONAL
// Argument 4: UINTN *NumberHandles
// Argument 5: EFI_HANDLE **Buffer
//
if ((EFI_GUID *)CoreRbp->Argument2 != NULL) {
Expand Down Expand Up @@ -581,11 +585,11 @@ CallBootService (
PagesNumber = EFI_SIZE_TO_PAGES (Argument4 * sizeof (EFI_HANDLE *));

Status = CoreAllocatePages (
AllocateAnyPages,
EfiRing3MemoryType,
PagesNumber,
(EFI_PHYSICAL_ADDRESS *)&Ring3Pages
);
AllocateAnyPages,
EfiRing3MemoryType,
PagesNumber,
(EFI_PHYSICAL_ADDRESS *)&Ring3Pages
);
if (EFI_ERROR (Status)) {
return Status;
}
Expand All @@ -600,6 +604,96 @@ CallBootService (

return StatusBS;

case SysCallGetVariable:
//
// Argument 1: CHAR16 *VariableName
// Argument 2: EFI_GUID *VendorGuid
// Argument 3: UINT32 *Attributes OPTIONAL
// Argument 4: UINTN *DataSize
// Argument 5: VOID *Data OPTIONAL
//
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)CoreRbp->Argument1, &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)CoreRbp->Argument2, &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(CoreRbp->Argument2 + sizeof (EFI_GUID) - 1), &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);
if ((UINT32 *)CoreRbp->Argument3 != NULL) {
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)CoreRbp->Argument3, &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(CoreRbp->Argument3 + sizeof (UINT32) - 1), &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);
}
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)((UINTN)UserRsp + 7 * sizeof (UINTN) - 1), &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);

DisableSMAP ();
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(CoreRbp->Argument1 + StrSize ((CHAR16 *)CoreRbp->Argument1) - 1), &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);

Argument6 = (UINTN)AllocateCopyPool (StrSize ((CHAR16 *)CoreRbp->Argument1), (CHAR16 *)CoreRbp->Argument1);
if ((VOID *)Argument6 == NULL) {
EnableSMAP ();
return EFI_OUT_OF_RESOURCES;
}

Status = FindGuid ((EFI_GUID *)CoreRbp->Argument2, &CoreProtocol, &MemoryCoreSize);
if (EFI_ERROR (Status)) {
EnableSMAP ();
FreePool ((VOID *)Argument6);
return Status;
}

gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)UserRsp->Arguments[4], &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(UserRsp->Arguments[4] + sizeof (UINTN) - 1), &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);

Argument4 = *(UINTN *)UserRsp->Arguments[4];

if ((VOID *)UserRsp->Arguments[5] != NULL) {
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)UserRsp->Arguments[5], &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);
gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(UserRsp->Arguments[5] + Argument4 - 1), &Attributes);
ASSERT ((Attributes & EFI_MEMORY_USER) != 0);

Argument5 = (UINTN)AllocatePool (Argument4);
if ((VOID *)Argument5 == NULL) {
EnableSMAP ();
FreePool ((VOID *)Argument6);
return EFI_OUT_OF_RESOURCES;
}
}
EnableSMAP ();

Status = gRT->GetVariable (
(CHAR16 *)Argument6,
CoreProtocol,
(UINT32 *)&Attributes,
&Argument4,
(VOID *)Argument5
);

DisableSMAP ();
if ((VOID *)UserRsp->Arguments[5] != NULL) {
CopyMem ((VOID *)UserRsp->Arguments[5], (VOID *)Argument5, Argument4);
}

*(UINTN *)UserRsp->Arguments[4] = Argument4;

if ((UINT32 *)CoreRbp->Argument3 != NULL) {
*(UINT32 *)CoreRbp->Argument3 = (UINT32)Attributes;
}
EnableSMAP ();

FreePool ((VOID *)Argument6);

if ((VOID *)Argument5 != NULL) {
FreePool ((VOID *)Argument5);
}

return Status;

case SysCallBlockIoReset:
//
// Argument 1: EFI_BLOCK_IO_PROTOCOL *This
Expand Down
8 changes: 5 additions & 3 deletions MdePkg/Include/Uefi/UefiSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,7 @@ typedef enum {
//
// RuntimeServices
//
SysCallGetVariable,
//
// Protocols
//
Expand Down Expand Up @@ -2082,9 +2083,10 @@ typedef struct {
} EFI_SYSTEM_TABLE;

typedef struct {
EFI_SYSTEM_TABLE SystemTable;
VOID *EntryPoint;
EFI_BOOT_SERVICES *BootServices;
EFI_SYSTEM_TABLE SystemTable;
VOID *EntryPoint;
EFI_BOOT_SERVICES *BootServices;
EFI_RUNTIME_SERVICES *RuntimeServices;
} RING3_DATA;

typedef struct {
Expand Down

0 comments on commit 239b883

Please sign in to comment.