From 72c747b365e88d6a324f8c04b223d8c8148e6a84 Mon Sep 17 00:00:00 2001 From: Mikhail Krichanov Date: Thu, 23 Jan 2025 18:31:22 +0300 Subject: [PATCH] Ring3: Refactored out MAX_LIST. --- .../Core/Dxe/DxeRing3/Ring3UefiBootServices.c | 53 +++++++++++-------- MdeModulePkg/Core/Dxe/SysCall/BootServices.c | 23 ++++---- .../Core/Dxe/SysCall/SupportedProtocols.c | 1 + MdePkg/Include/Uefi/UefiSpec.h | 1 - 4 files changed, 45 insertions(+), 33 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c b/MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c index bae3b0036b..6cb6fd063d 100644 --- a/MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c +++ b/MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c @@ -659,33 +659,44 @@ Ring3InstallMultipleProtocolInterfaces ( ... ) { - VA_LIST Marker; - VOID *Argument; - VOID *ArgList[MAX_LIST]; - UINTN Index; + EFI_STATUS Status; + VA_LIST Marker; + VOID **Arguments; + UINTN NumberOfArguments; + UINTN Index; VA_START (Marker, Handle); - for (Index = 0; Index < MAX_LIST; ++Index) { - Argument = VA_ARG (Marker, VOID *); - ArgList[Index] = Argument; - - if (Argument == NULL) { - break; - } + NumberOfArguments = 1; + while (VA_ARG (Marker, VOID *) != NULL) { + ++NumberOfArguments; } VA_END (Marker); - if (Index == MAX_LIST) { - DEBUG ((DEBUG_ERROR, "Ring3: Too many arguments\n")); - return EFI_INVALID_PARAMETER; + Status = CoreAllocatePool ( + EfiRing3MemoryType, + NumberOfArguments * sizeof (VOID *), + (VOID **)&Arguments + ); + if (EFI_ERROR (Status)) { + return Status; } - return SysCall ( - SysCallInstallMultipleProtocolInterfaces, - 2, - Handle, - ArgList - ); + VA_START (Marker, Handle); + for (Index = 0; Index < NumberOfArguments; ++Index) { + Arguments[Index] = VA_ARG (Marker, VOID *); + } + VA_END (Marker); + + Status = SysCall ( + SysCallInstallMultipleProtocolInterfaces, + 3, + Handle, + NumberOfArguments, + Arguments + ); + + CoreFreePool (Arguments); + return Status; } EFI_STATUS @@ -796,5 +807,5 @@ CoreFreePoolPagesWithGuard ( IN UINTN NoPages ) { - CoreFreePoolPagesI (PoolType, Memory, NoPages); + CoreFreePoolPagesI (EfiRing3MemoryType, Memory, NoPages); } diff --git a/MdeModulePkg/Core/Dxe/SysCall/BootServices.c b/MdeModulePkg/Core/Dxe/SysCall/BootServices.c index 0b96d43299..e6dbe42a4d 100644 --- a/MdeModulePkg/Core/Dxe/SysCall/BootServices.c +++ b/MdeModulePkg/Core/Dxe/SysCall/BootServices.c @@ -345,6 +345,7 @@ FreeUserSpaceDriver ( } RemoveEntryList (&UserDriver->Link); + FreePool (UserDriver); } EFI_STATUS @@ -367,7 +368,7 @@ CallBootService ( UINTN Argument6; UINT32 Index; VOID **UserArgList; - VOID *CoreArgList[MAX_LIST]; + VOID **CoreArgList; EFI_HANDLE CoreHandle; UINT32 PagesNumber; EFI_PHYSICAL_ADDRESS Ring3Pages; @@ -491,24 +492,25 @@ CallBootService ( case SysCallInstallMultipleProtocolInterfaces: // // Argument 1: EFI_HANDLE *Handle - // ... + // Argument 2: UINTN NumberOfArguments + // Argument 3: VOID **UserArgList // gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)Arguments[1], &Attributes); ASSERT ((Attributes & EFI_MEMORY_USER) != 0); gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(Arguments[1] + sizeof (EFI_HANDLE *) - 1), &Attributes); ASSERT ((Attributes & EFI_MEMORY_USER) != 0); - gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)Arguments[2], &Attributes); + gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)Arguments[3], &Attributes); ASSERT ((Attributes & EFI_MEMORY_USER) != 0); - gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(Arguments[2] + sizeof (VOID **) - 1), &Attributes); + gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(Arguments[3] + Arguments[2] * sizeof (VOID *) - 1), &Attributes); ASSERT ((Attributes & EFI_MEMORY_USER) != 0); + CoreArgList = AllocatePool (Arguments[2] * sizeof (VOID *)); + AllowSupervisorAccessToUserMemory (); CoreHandle = *(EFI_HANDLE *)Arguments[1]; - UserArgList = (VOID **)Arguments[2]; + UserArgList = (VOID **)Arguments[3]; for (Index = 0; UserArgList[Index] != NULL; Index += 2) { - gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)((UINTN)&UserArgList[Index + 2] - 1), &Attributes); - ASSERT ((Attributes & EFI_MEMORY_USER) != 0); gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)(UINTN)UserArgList[Index], &Attributes); ASSERT ((Attributes & EFI_MEMORY_USER) != 0); gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)((UINTN)UserArgList[Index] + sizeof (EFI_GUID) - 1), &Attributes); @@ -524,6 +526,7 @@ CallBootService ( Index -= 2; } + FreePool (CoreArgList); FreePool (Arguments); return Status; } @@ -547,13 +550,10 @@ CallBootService ( NewDriver->UserStackTop = UserDriver->UserStackTop; InsertTailList (&gUserSpaceDriversHead, &NewDriver->Link); - - gCpu->GetMemoryAttributes (gCpu, (EFI_PHYSICAL_ADDRESS)((UINTN)&UserArgList[Index + 2] + sizeof (VOID *) - 1), &Attributes); - ASSERT ((Attributes & EFI_MEMORY_USER) != 0); } ForbidSupervisorAccessToUserMemory (); - ASSERT (Index < MAX_LIST); + ASSERT (Index == (Arguments[2] - 1)); CoreArgList[Index] = NULL; for (Index = 0; CoreArgList[Index] != NULL; Index += 2) { @@ -593,6 +593,7 @@ CallBootService ( (VOID *)gBS->InstallMultipleProtocolInterfaces ); + FreePool (CoreArgList); FreePool (Arguments); return Status; diff --git a/MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.c b/MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.c index 549d12b626..ced0053ae3 100644 --- a/MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.c +++ b/MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.c @@ -237,6 +237,7 @@ CoreFileClose ( FreePool (UserDriver->CoreWrapper); RemoveEntryList (&UserDriver->Link); + FreePool (UserDriver); gUserPageTable = OldPageTable; diff --git a/MdePkg/Include/Uefi/UefiSpec.h b/MdePkg/Include/Uefi/UefiSpec.h index 557a0a801b..9b2fb726ee 100644 --- a/MdePkg/Include/Uefi/UefiSpec.h +++ b/MdePkg/Include/Uefi/UefiSpec.h @@ -2051,7 +2051,6 @@ typedef enum { SysCallMax } SYS_CALL_TYPE; -#define MAX_LIST 32 #define SC_FREE_PAGES 7 #define SC_BLOCK_IO_READ 14 #define SC_BLOCK_IO_WRITE 15