Skip to content

Commit

Permalink
Ring3: Fixed some page faults caused by wrong memory attribution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed May 23, 2024
1 parent 91c4729 commit 999108e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
9 changes: 9 additions & 0 deletions MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,15 @@ CoreExitBootServices (
// Free resources allocated for Ring3.
//
if (gRing3Data != NULL) {
DisableSMAP ();
if (gRing3Data->SystemTable.ConfigurationTable != NULL) {
CoreFreePages (
(EFI_PHYSICAL_ADDRESS)(UINTN)gRing3Data->SystemTable.ConfigurationTable,
EFI_SIZE_TO_PAGES (gRing3Data->SystemTable.NumberOfTableEntries * sizeof (EFI_CONFIGURATION_TABLE))
);
}
EnableSMAP ();

CoreFreePages (
(EFI_PHYSICAL_ADDRESS)(UINTN)gRing3Data,
EFI_SIZE_TO_PAGES (sizeof (RING3_DATA))
Expand Down
31 changes: 26 additions & 5 deletions MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -1400,9 +1400,30 @@ SysCallBootService (
IN VOID *UserRsp
)
{
return CallBootService (
Type,
(CORE_STACK *)CoreRbp,
(RING3_STACK *)UserRsp
);
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Physical;

Status = CoreAllocatePages (
AllocateAnyPages,
EfiRing3MemoryType,
EFI_SIZE_TO_PAGES (8 * sizeof (UINTN)),
&Physical
);
if (EFI_ERROR (Status)) {
return Status;
}

DisableSMAP ();
CopyMem ((VOID *)(UINTN)Physical, (VOID *)UserRsp, 8 * sizeof (UINTN));
EnableSMAP ();

Status = CallBootService (
Type,
(CORE_STACK *)CoreRbp,
(RING3_STACK *)(UINTN)Physical
);

CoreFreePages (Physical, EFI_SIZE_TO_PAGES (8 * sizeof (UINTN)));

return Status;
}
24 changes: 24 additions & 0 deletions MdeModulePkg/Core/Dxe/SysCall/Initialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ InitializeRing3 (
VOID *TopOfStack;
UINTN SizeOfStack;
EFI_PHYSICAL_ADDRESS Physical;
UINTN Index;
EFI_CONFIGURATION_TABLE *Conf;

//
// Set Ring3 EntryPoint and BootServices.
Expand All @@ -50,6 +52,28 @@ InitializeRing3 (
gRing3Data = (RING3_DATA *)(UINTN)Physical;

CopyMem ((VOID *)gRing3Data, (VOID *)Image->Info.SystemTable, sizeof (EFI_SYSTEM_TABLE));

Status = CoreAllocatePages (
AllocateAnyPages,
EfiRing3MemoryType,
EFI_SIZE_TO_PAGES (gRing3Data->SystemTable.NumberOfTableEntries * sizeof (EFI_CONFIGURATION_TABLE)),
&Physical
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3 ConfigurationTable.\n"));
return Status;
}

Conf = (EFI_CONFIGURATION_TABLE *)(UINTN)Physical;

for (Index = 0; Index < gRing3Data->SystemTable.NumberOfTableEntries; ++Index) {
Conf->VendorGuid = gRing3Data->SystemTable.ConfigurationTable[Index].VendorGuid;
Conf->VendorTable = gRing3Data->SystemTable.ConfigurationTable[Index].VendorTable;
++Conf;
}

gRing3Data->SystemTable.ConfigurationTable = (EFI_CONFIGURATION_TABLE *)(UINTN)Physical;

//
// Initialize DxeRing3 with Supervisor privileges.
//
Expand Down
4 changes: 4 additions & 0 deletions MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ GoToRing3 (
VA_END (Marker);
EnableSMAP ();

#if defined (MDE_CPU_X64) || defined (MDE_CPU_IA32)
if (Number == 2) {
//
// Necessary fix for ProcessLibraryConstructorList() -> DxeCcProbeLibConstructor()
Expand All @@ -68,16 +69,19 @@ GoToRing3 (
EFI_MEMORY_XP | EFI_MEMORY_USER
);
}
#endif

Status = CallRing3 (Input);

#if defined (MDE_CPU_X64) || defined (MDE_CPU_IA32)
if (Number == 2) {
SetUefiImageMemoryAttributes (
FixedPcdGet32 (PcdOvmfWorkAreaBase),
FixedPcdGet32 (PcdOvmfWorkAreaSize),
EFI_MEMORY_XP
);
}
#endif

CoreFreePages (Ring3Pages, PagesNumber);

Expand Down

0 comments on commit 999108e

Please sign in to comment.