Core/Dxe: Changed line ending to Windows style.

This commit is contained in:
Mikhail Krichanov 2024-09-02 12:26:15 +03:00
parent 5b59ec5e07
commit d8aa9c3ea7
23 changed files with 7026 additions and 7026 deletions

View File

@ -70,21 +70,21 @@
SysCall/SupportedProtocols.h SysCall/SupportedProtocols.h
SysCall/SupportedProtocols.c SysCall/SupportedProtocols.c
[Sources.IA32] [Sources.IA32]
SysCall/IA32/InitializeMsr.c SysCall/IA32/InitializeMsr.c
SysCall/IA32/CoreBootServices.nasm SysCall/IA32/CoreBootServices.nasm
[Sources.X64] [Sources.X64]
SysCall/X64/InitializeMsr.c SysCall/X64/InitializeMsr.c
SysCall/X64/CoreBootServices.nasm SysCall/X64/CoreBootServices.nasm
[Sources.ARM] [Sources.ARM]
SysCall/ARM/InitializeMsr.c SysCall/ARM/InitializeMsr.c
SysCall/ARM/CoreBootServices.S SysCall/ARM/CoreBootServices.S
[Sources.AARCH64] [Sources.AARCH64]
SysCall/AARCH64/InitializeMsr.c SysCall/AARCH64/InitializeMsr.c
SysCall/AARCH64/CoreBootServices.S SysCall/AARCH64/CoreBootServices.S
[Packages] [Packages]
MdePkg/MdePkg.dec MdePkg/MdePkg.dec
@ -93,7 +93,7 @@
OvmfPkg/OvmfPkg.dec OvmfPkg/OvmfPkg.dec
ArmPkg/ArmPkg.dec ArmPkg/ArmPkg.dec
ArmVirtPkg/ArmVirtPkg.dec ArmVirtPkg/ArmVirtPkg.dec
[LibraryClasses] [LibraryClasses]
BaseMemoryLib BaseMemoryLib
CacheMaintenanceLib CacheMaintenanceLib

View File

@ -1,32 +1,32 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Copyright (c) 2024, Mikhail Krichanov. All rights reserved. // Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <AsmMacroIoLibV8.h> #include <AsmMacroIoLibV8.h>
.extern ASM_PFX(Ring3Call) .extern ASM_PFX(Ring3Call)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// EFI_STATUS // EFI_STATUS
// EFIAPI // EFIAPI
// SysCall ( // SysCall (
// IN UINT8 Type, // IN UINT8 Type,
// ... // ...
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(SysCall) ASM_FUNC(SysCall)
svc #0 svc #0
ret ret
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VOID // VOID
// EFIAPI // EFIAPI
// Ring3EntryPoint ( // Ring3EntryPoint (
// IN RING3_CALL_DATA *Data // IN RING3_CALL_DATA *Data
// ); // );
// //
// (x0) Data // (x0) Data
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(Ring3EntryPoint) ASM_FUNC(Ring3EntryPoint)
b ASM_PFX(Ring3Call) b ASM_PFX(Ring3Call)

View File

@ -1,32 +1,32 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Copyright (c) 2024, Mikhail Krichanov. All rights reserved. // Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <AsmMacroIoLib.h> #include <AsmMacroIoLib.h>
.extern ASM_PFX(Ring3Call) .extern ASM_PFX(Ring3Call)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// EFI_STATUS // EFI_STATUS
// EFIAPI // EFIAPI
// SysCall ( // SysCall (
// IN UINT8 Type, // IN UINT8 Type,
// ... // ...
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(SysCall) ASM_FUNC(SysCall)
svc #0 svc #0
bx LR bx LR
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VOID // VOID
// EFIAPI // EFIAPI
// Ring3EntryPoint ( // Ring3EntryPoint (
// IN RING3_CALL_DATA *Data // IN RING3_CALL_DATA *Data
// ); // );
// //
// (r0) Data // (r0) Data
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(Ring3EntryPoint) ASM_FUNC(Ring3EntryPoint)
b ASM_PFX(Ring3Call) b ASM_PFX(Ring3Call)

View File

@ -1,265 +1,265 @@
/** @file /** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved. Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause SPDX-License-Identifier: BSD-3-Clause
**/ **/
#include <Uefi.h> #include <Uefi.h>
#include <Library/BaseMemoryLib.h> #include <Library/BaseMemoryLib.h>
#include <Library/MemoryPoolLib.h> #include <Library/MemoryPoolLib.h>
#include <Library/UefiBootServicesTableLib.h> #include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h> #include <Library/UefiRuntimeServicesTableLib.h>
#include "Ring3.h" #include "Ring3.h"
EFI_BOOT_SERVICES mBootServices = { EFI_BOOT_SERVICES mBootServices = {
{ {
EFI_BOOT_SERVICES_SIGNATURE, // Signature EFI_BOOT_SERVICES_SIGNATURE, // Signature
EFI_BOOT_SERVICES_REVISION, // Revision EFI_BOOT_SERVICES_REVISION, // Revision
sizeof (EFI_BOOT_SERVICES), // HeaderSize sizeof (EFI_BOOT_SERVICES), // HeaderSize
0, // CRC32 0, // CRC32
0 // Reserved 0 // Reserved
}, },
(EFI_RAISE_TPL)Ring3RaiseTpl, // RaiseTPL (EFI_RAISE_TPL)Ring3RaiseTpl, // RaiseTPL
(EFI_RESTORE_TPL)Ring3RestoreTpl, // RestoreTPL (EFI_RESTORE_TPL)Ring3RestoreTpl, // RestoreTPL
(EFI_ALLOCATE_PAGES)Ring3AllocatePages, // AllocatePages (EFI_ALLOCATE_PAGES)Ring3AllocatePages, // AllocatePages
(EFI_FREE_PAGES)Ring3FreePages, // FreePages (EFI_FREE_PAGES)Ring3FreePages, // FreePages
(EFI_GET_MEMORY_MAP)Ring3GetMemoryMap, // GetMemoryMap (EFI_GET_MEMORY_MAP)Ring3GetMemoryMap, // GetMemoryMap
(EFI_ALLOCATE_POOL)CoreAllocatePool, // AllocatePool (EFI_ALLOCATE_POOL)CoreAllocatePool, // AllocatePool
(EFI_FREE_POOL)CoreFreePool, // FreePool (EFI_FREE_POOL)CoreFreePool, // FreePool
(EFI_CREATE_EVENT)Ring3CreateEvent, // CreateEvent (EFI_CREATE_EVENT)Ring3CreateEvent, // CreateEvent
(EFI_SET_TIMER)Ring3SetTimer, // SetTimer (EFI_SET_TIMER)Ring3SetTimer, // SetTimer
(EFI_WAIT_FOR_EVENT)Ring3WaitForEvent, // WaitForEvent (EFI_WAIT_FOR_EVENT)Ring3WaitForEvent, // WaitForEvent
(EFI_SIGNAL_EVENT)Ring3SignalEvent, // SignalEvent (EFI_SIGNAL_EVENT)Ring3SignalEvent, // SignalEvent
(EFI_CLOSE_EVENT)Ring3CloseEvent, // CloseEvent (EFI_CLOSE_EVENT)Ring3CloseEvent, // CloseEvent
(EFI_CHECK_EVENT)Ring3CheckEvent, // CheckEvent (EFI_CHECK_EVENT)Ring3CheckEvent, // CheckEvent
(EFI_INSTALL_PROTOCOL_INTERFACE)Ring3InstallProtocolInterface, // InstallProtocolInterface (EFI_INSTALL_PROTOCOL_INTERFACE)Ring3InstallProtocolInterface, // InstallProtocolInterface
(EFI_REINSTALL_PROTOCOL_INTERFACE)Ring3ReinstallProtocolInterface, // ReinstallProtocolInterface (EFI_REINSTALL_PROTOCOL_INTERFACE)Ring3ReinstallProtocolInterface, // ReinstallProtocolInterface
(EFI_UNINSTALL_PROTOCOL_INTERFACE)Ring3UninstallProtocolInterface, // UninstallProtocolInterface (EFI_UNINSTALL_PROTOCOL_INTERFACE)Ring3UninstallProtocolInterface, // UninstallProtocolInterface
(EFI_HANDLE_PROTOCOL)Ring3HandleProtocol, // HandleProtocol (EFI_HANDLE_PROTOCOL)Ring3HandleProtocol, // HandleProtocol
(VOID *)NULL, // Reserved (VOID *)NULL, // Reserved
(EFI_REGISTER_PROTOCOL_NOTIFY)Ring3RegisterProtocolNotify, // RegisterProtocolNotify (EFI_REGISTER_PROTOCOL_NOTIFY)Ring3RegisterProtocolNotify, // RegisterProtocolNotify
(EFI_LOCATE_HANDLE)Ring3LocateHandle, // LocateHandle (EFI_LOCATE_HANDLE)Ring3LocateHandle, // LocateHandle
(EFI_LOCATE_DEVICE_PATH)Ring3LocateDevicePath, // LocateDevicePath (EFI_LOCATE_DEVICE_PATH)Ring3LocateDevicePath, // LocateDevicePath
(EFI_INSTALL_CONFIGURATION_TABLE)Ring3InstallConfigurationTable, // InstallConfigurationTable (EFI_INSTALL_CONFIGURATION_TABLE)Ring3InstallConfigurationTable, // InstallConfigurationTable
(EFI_IMAGE_LOAD)Ring3LoadImage, // LoadImage (EFI_IMAGE_LOAD)Ring3LoadImage, // LoadImage
(EFI_IMAGE_START)Ring3StartImage, // StartImage (EFI_IMAGE_START)Ring3StartImage, // StartImage
(EFI_EXIT)Ring3Exit, // Exit (EFI_EXIT)Ring3Exit, // Exit
(EFI_IMAGE_UNLOAD)Ring3UnloadImage, // UnloadImage (EFI_IMAGE_UNLOAD)Ring3UnloadImage, // UnloadImage
(EFI_EXIT_BOOT_SERVICES)Ring3ExitBootServices, // ExitBootServices (EFI_EXIT_BOOT_SERVICES)Ring3ExitBootServices, // ExitBootServices
(EFI_GET_NEXT_MONOTONIC_COUNT)Ring3GetNextMonotonicCount, // GetNextMonotonicCount (EFI_GET_NEXT_MONOTONIC_COUNT)Ring3GetNextMonotonicCount, // GetNextMonotonicCount
(EFI_STALL)Ring3Stall, // Stall (EFI_STALL)Ring3Stall, // Stall
(EFI_SET_WATCHDOG_TIMER)Ring3SetWatchdogTimer, // SetWatchdogTimer (EFI_SET_WATCHDOG_TIMER)Ring3SetWatchdogTimer, // SetWatchdogTimer
(EFI_CONNECT_CONTROLLER)Ring3ConnectController, // ConnectController (EFI_CONNECT_CONTROLLER)Ring3ConnectController, // ConnectController
(EFI_DISCONNECT_CONTROLLER)Ring3DisconnectController, // DisconnectController (EFI_DISCONNECT_CONTROLLER)Ring3DisconnectController, // DisconnectController
(EFI_OPEN_PROTOCOL)Ring3OpenProtocol, // OpenProtocol (EFI_OPEN_PROTOCOL)Ring3OpenProtocol, // OpenProtocol
(EFI_CLOSE_PROTOCOL)Ring3CloseProtocol, // CloseProtocol (EFI_CLOSE_PROTOCOL)Ring3CloseProtocol, // CloseProtocol
(EFI_OPEN_PROTOCOL_INFORMATION)Ring3OpenProtocolInformation, // OpenProtocolInformation (EFI_OPEN_PROTOCOL_INFORMATION)Ring3OpenProtocolInformation, // OpenProtocolInformation
(EFI_PROTOCOLS_PER_HANDLE)Ring3ProtocolsPerHandle, // ProtocolsPerHandle (EFI_PROTOCOLS_PER_HANDLE)Ring3ProtocolsPerHandle, // ProtocolsPerHandle
(EFI_LOCATE_HANDLE_BUFFER)Ring3LocateHandleBuffer, // LocateHandleBuffer (EFI_LOCATE_HANDLE_BUFFER)Ring3LocateHandleBuffer, // LocateHandleBuffer
(EFI_LOCATE_PROTOCOL)Ring3LocateProtocol, // LocateProtocol (EFI_LOCATE_PROTOCOL)Ring3LocateProtocol, // LocateProtocol
(EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES)Ring3InstallMultipleProtocolInterfaces, // InstallMultipleProtocolInterfaces (EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES)Ring3InstallMultipleProtocolInterfaces, // InstallMultipleProtocolInterfaces
(EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES)Ring3UninstallMultipleProtocolInterfaces, // UninstallMultipleProtocolInterfaces (EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES)Ring3UninstallMultipleProtocolInterfaces, // UninstallMultipleProtocolInterfaces
(EFI_CALCULATE_CRC32)Ring3CalculateCrc32, // CalculateCrc32 (EFI_CALCULATE_CRC32)Ring3CalculateCrc32, // CalculateCrc32
(EFI_COPY_MEM)CopyMem, // CopyMem (EFI_COPY_MEM)CopyMem, // CopyMem
(EFI_SET_MEM)SetMem, // SetMem (EFI_SET_MEM)SetMem, // SetMem
(EFI_CREATE_EVENT_EX)Ring3CreateEventEx, // CreateEventEx (EFI_CREATE_EVENT_EX)Ring3CreateEventEx, // CreateEventEx
}; };
EFI_RUNTIME_SERVICES mRuntimeServices = { EFI_RUNTIME_SERVICES mRuntimeServices = {
{ {
EFI_RUNTIME_SERVICES_SIGNATURE, // Signature EFI_RUNTIME_SERVICES_SIGNATURE, // Signature
EFI_RUNTIME_SERVICES_REVISION, // Revision EFI_RUNTIME_SERVICES_REVISION, // Revision
sizeof (EFI_RUNTIME_SERVICES), // HeaderSize sizeof (EFI_RUNTIME_SERVICES), // HeaderSize
0, // CRC32 0, // CRC32
0 // Reserved 0 // Reserved
}, },
(EFI_GET_TIME)Ring3GetTime, // GetTime (EFI_GET_TIME)Ring3GetTime, // GetTime
(EFI_SET_TIME)Ring3SetTime, // SetTime (EFI_SET_TIME)Ring3SetTime, // SetTime
(EFI_GET_WAKEUP_TIME)Ring3GetWakeupTime, // GetWakeupTime (EFI_GET_WAKEUP_TIME)Ring3GetWakeupTime, // GetWakeupTime
(EFI_SET_WAKEUP_TIME)Ring3SetWakeupTime, // SetWakeupTime (EFI_SET_WAKEUP_TIME)Ring3SetWakeupTime, // SetWakeupTime
(EFI_SET_VIRTUAL_ADDRESS_MAP)Ring3SetVirtualAddressMap, // SetVirtualAddressMap (EFI_SET_VIRTUAL_ADDRESS_MAP)Ring3SetVirtualAddressMap, // SetVirtualAddressMap
(EFI_CONVERT_POINTER)Ring3ConvertPointer, // ConvertPointer (EFI_CONVERT_POINTER)Ring3ConvertPointer, // ConvertPointer
(EFI_GET_VARIABLE)Ring3GetVariable, // GetVariable (EFI_GET_VARIABLE)Ring3GetVariable, // GetVariable
(EFI_GET_NEXT_VARIABLE_NAME)Ring3GetNextVariableName, // GetNextVariableName (EFI_GET_NEXT_VARIABLE_NAME)Ring3GetNextVariableName, // GetNextVariableName
(EFI_SET_VARIABLE)Ring3SetVariable, // SetVariable (EFI_SET_VARIABLE)Ring3SetVariable, // SetVariable
(EFI_GET_NEXT_HIGH_MONO_COUNT)Ring3GetNextHighMonotonicCount, // GetNextHighMonotonicCount (EFI_GET_NEXT_HIGH_MONO_COUNT)Ring3GetNextHighMonotonicCount, // GetNextHighMonotonicCount
(EFI_RESET_SYSTEM)Ring3ResetSystem, // ResetSystem (EFI_RESET_SYSTEM)Ring3ResetSystem, // ResetSystem
(EFI_UPDATE_CAPSULE)Ring3UpdateCapsule, // UpdateCapsule (EFI_UPDATE_CAPSULE)Ring3UpdateCapsule, // UpdateCapsule
(EFI_QUERY_CAPSULE_CAPABILITIES)Ring3QueryCapsuleCapabilities, // QueryCapsuleCapabilities (EFI_QUERY_CAPSULE_CAPABILITIES)Ring3QueryCapsuleCapabilities, // QueryCapsuleCapabilities
(EFI_QUERY_VARIABLE_INFO)Ring3QueryVariableInfo // QueryVariableInfo (EFI_QUERY_VARIABLE_INFO)Ring3QueryVariableInfo // QueryVariableInfo
}; };
VOID VOID
EFIAPI EFIAPI
Ring3EntryPoint ( Ring3EntryPoint (
IN RING3_CALL_DATA *Data IN RING3_CALL_DATA *Data
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_0)( (EFIAPI *FUNCTION_0)(
VOID VOID
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_1)( (EFIAPI *FUNCTION_1)(
IN UINTN Argument1 IN UINTN Argument1
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_2)( (EFIAPI *FUNCTION_2)(
IN UINTN Argument1, IN UINTN Argument1,
IN UINTN Argument2 IN UINTN Argument2
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_3)( (EFIAPI *FUNCTION_3)(
IN UINTN Argument1, IN UINTN Argument1,
IN UINTN Argument2, IN UINTN Argument2,
IN UINTN Argument3 IN UINTN Argument3
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_4)( (EFIAPI *FUNCTION_4)(
IN UINTN Argument1, IN UINTN Argument1,
IN UINTN Argument2, IN UINTN Argument2,
IN UINTN Argument3, IN UINTN Argument3,
IN UINTN Argument4 IN UINTN Argument4
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_5)( (EFIAPI *FUNCTION_5)(
IN UINTN Argument1, IN UINTN Argument1,
IN UINTN Argument2, IN UINTN Argument2,
IN UINTN Argument3, IN UINTN Argument3,
IN UINTN Argument4, IN UINTN Argument4,
IN UINTN Argument5 IN UINTN Argument5
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_6)( (EFIAPI *FUNCTION_6)(
IN UINTN Argument1, IN UINTN Argument1,
IN UINTN Argument2, IN UINTN Argument2,
IN UINTN Argument3, IN UINTN Argument3,
IN UINTN Argument4, IN UINTN Argument4,
IN UINTN Argument5, IN UINTN Argument5,
IN UINTN Argument6 IN UINTN Argument6
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_7)( (EFIAPI *FUNCTION_7)(
IN UINTN Argument1, IN UINTN Argument1,
IN UINTN Argument2, IN UINTN Argument2,
IN UINTN Argument3, IN UINTN Argument3,
IN UINTN Argument4, IN UINTN Argument4,
IN UINTN Argument5, IN UINTN Argument5,
IN UINTN Argument6, IN UINTN Argument6,
IN UINTN Argument7 IN UINTN Argument7
); );
typedef typedef
EFI_STATUS EFI_STATUS
(EFIAPI *FUNCTION_8)( (EFIAPI *FUNCTION_8)(
IN UINTN Argument1, IN UINTN Argument1,
IN UINTN Argument2, IN UINTN Argument2,
IN UINTN Argument3, IN UINTN Argument3,
IN UINTN Argument4, IN UINTN Argument4,
IN UINTN Argument5, IN UINTN Argument5,
IN UINTN Argument6, IN UINTN Argument6,
IN UINTN Argument7, IN UINTN Argument7,
IN UINTN Argument8 IN UINTN Argument8
); );
VOID VOID
EFIAPI EFIAPI
Ring3Call ( Ring3Call (
IN RING3_CALL_DATA *Data IN RING3_CALL_DATA *Data
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
FUNCTION_0 Function0; FUNCTION_0 Function0;
FUNCTION_1 Function1; FUNCTION_1 Function1;
FUNCTION_2 Function2; FUNCTION_2 Function2;
FUNCTION_3 Function3; FUNCTION_3 Function3;
FUNCTION_4 Function4; FUNCTION_4 Function4;
FUNCTION_5 Function5; FUNCTION_5 Function5;
FUNCTION_6 Function6; FUNCTION_6 Function6;
FUNCTION_7 Function7; FUNCTION_7 Function7;
FUNCTION_8 Function8; FUNCTION_8 Function8;
switch (Data->NumberOfArguments) { switch (Data->NumberOfArguments) {
case 0: case 0:
Function0 = (FUNCTION_0)Data->EntryPoint; Function0 = (FUNCTION_0)Data->EntryPoint;
Status = Function0 (); Status = Function0 ();
break; break;
case 1: case 1:
Function1 = (FUNCTION_1)Data->EntryPoint; Function1 = (FUNCTION_1)Data->EntryPoint;
Status = Function1 (Data->Arguments[0]); Status = Function1 (Data->Arguments[0]);
break; break;
case 2: case 2:
Function2 = (FUNCTION_2)Data->EntryPoint; Function2 = (FUNCTION_2)Data->EntryPoint;
Status = Function2 (Data->Arguments[0], Data->Arguments[1]); Status = Function2 (Data->Arguments[0], Data->Arguments[1]);
break; break;
case 3: case 3:
Function3 = (FUNCTION_3)Data->EntryPoint; Function3 = (FUNCTION_3)Data->EntryPoint;
Status = Function3 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2]); Status = Function3 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2]);
break; break;
case 4: case 4:
Function4 = (FUNCTION_4)Data->EntryPoint; Function4 = (FUNCTION_4)Data->EntryPoint;
Status = Function4 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3]); Status = Function4 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3]);
break; break;
case 5: case 5:
Function5 = (FUNCTION_5)Data->EntryPoint; Function5 = (FUNCTION_5)Data->EntryPoint;
Status = Function5 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3], Data->Arguments[4]); Status = Function5 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3], Data->Arguments[4]);
break; break;
case 6: case 6:
Function6 = (FUNCTION_6)Data->EntryPoint; Function6 = (FUNCTION_6)Data->EntryPoint;
Status = Function6 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3], Data->Arguments[4], Data->Arguments[5]); Status = Function6 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3], Data->Arguments[4], Data->Arguments[5]);
break; break;
case 7: case 7:
Function7 = (FUNCTION_7)Data->EntryPoint; Function7 = (FUNCTION_7)Data->EntryPoint;
Status = Function7 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3], Data->Arguments[4], Data->Arguments[5], Data->Arguments[6]); Status = Function7 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3], Data->Arguments[4], Data->Arguments[5], Data->Arguments[6]);
break; break;
case 8: case 8:
Function8 = (FUNCTION_8)Data->EntryPoint; Function8 = (FUNCTION_8)Data->EntryPoint;
Status = Function8 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3], Data->Arguments[4], Data->Arguments[5], Data->Arguments[6], Data->Arguments[7]); Status = Function8 (Data->Arguments[0], Data->Arguments[1], Data->Arguments[2], Data->Arguments[3], Data->Arguments[4], Data->Arguments[5], Data->Arguments[6], Data->Arguments[7]);
break; break;
default: default:
Status = EFI_UNSUPPORTED; Status = EFI_UNSUPPORTED;
break; break;
} }
SysCall (SysCallReturnToCore, Status); SysCall (SysCallReturnToCore, Status);
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3Initialization ( Ring3Initialization (
IN EFI_HANDLE ImageHandle, IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable IN EFI_SYSTEM_TABLE *SystemTable
) )
{ {
RING3_DATA *Ring3Data; RING3_DATA *Ring3Data;
Ring3Data = (RING3_DATA *)SystemTable; Ring3Data = (RING3_DATA *)SystemTable;
Ring3Data->EntryPoint = (VOID *)Ring3EntryPoint; Ring3Data->EntryPoint = (VOID *)Ring3EntryPoint;
Ring3Data->BootServices = &mBootServices; Ring3Data->BootServices = &mBootServices;
Ring3Data->RuntimeServices = &mRuntimeServices; Ring3Data->RuntimeServices = &mRuntimeServices;
gBS = &mBootServices; gBS = &mBootServices;
gRT = &mRuntimeServices; gRT = &mRuntimeServices;
CoreInitializePool (); CoreInitializePool ();
return EFI_SUCCESS; return EFI_SUCCESS;
} }

View File

@ -1,66 +1,66 @@
## @file ## @file
# #
# Ring3 driver for SysCalls. # Ring3 driver for SysCalls.
# #
# Copyright (c) 2024, Mikhail Krichanov. All rights reserved. # Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
# #
## ##
[Defines] [Defines]
INF_VERSION = 0x00010005 INF_VERSION = 0x00010005
BASE_NAME = DxeRing3 BASE_NAME = DxeRing3
FILE_GUID = 88EA50C2-0DEA-4F13-B691-B506554E632B FILE_GUID = 88EA50C2-0DEA-4F13-B691-B506554E632B
MODULE_TYPE = DXE_DRIVER MODULE_TYPE = DXE_DRIVER
VERSION_STRING = 1.0 VERSION_STRING = 1.0
ENTRY_POINT = Ring3Initialization ENTRY_POINT = Ring3Initialization
# #
# The following information is for reference only and not required by the build tools. # The following information is for reference only and not required by the build tools.
# #
# VALID_ARCHITECTURES = IA32 X64 ARM AARCH64 # VALID_ARCHITECTURES = IA32 X64 ARM AARCH64
# #
[Sources] [Sources]
Ring3.h Ring3.h
DxeRing3.c DxeRing3.c
Ring3UefiBootServices.c Ring3UefiBootServices.c
Ring3UefiRuntimeServices.c Ring3UefiRuntimeServices.c
Ring3Protocols.c Ring3Protocols.c
[Sources.IA32] [Sources.IA32]
IA32/SysCall.nasm IA32/SysCall.nasm
[Sources.X64] [Sources.X64]
X64/SysCall.nasm X64/SysCall.nasm
[Sources.ARM] [Sources.ARM]
ARM/SysCall.S ARM/SysCall.S
[Sources.AARCH64] [Sources.AARCH64]
AARCH64/SysCall.S AARCH64/SysCall.S
[Packages] [Packages]
MdePkg/MdePkg.dec MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec MdeModulePkg/MdeModulePkg.dec
ArmPkg/ArmPkg.dec ArmPkg/ArmPkg.dec
[LibraryClasses] [LibraryClasses]
BaseLib BaseLib
BaseMemoryLib BaseMemoryLib
DebugLib DebugLib
MemoryPoolLib MemoryPoolLib
UefiBootServicesTableLib UefiBootServicesTableLib
UefiDriverEntryPoint UefiDriverEntryPoint
UefiRuntimeServicesTableLib UefiRuntimeServicesTableLib
[Protocols] [Protocols]
gEfiDevicePathUtilitiesProtocolGuid ## SOMETIMES_CONSUMES gEfiDevicePathUtilitiesProtocolGuid ## SOMETIMES_CONSUMES
gEfiLoadedImageProtocolGuid ## SOMETIMES_CONSUMES gEfiLoadedImageProtocolGuid ## SOMETIMES_CONSUMES
gEfiDiskIoProtocolGuid ## SOMETIMES_CONSUMES gEfiDiskIoProtocolGuid ## SOMETIMES_CONSUMES
gEfiBlockIoProtocolGuid ## SOMETIMES_CONSUMES gEfiBlockIoProtocolGuid ## SOMETIMES_CONSUMES
gEfiDevicePathProtocolGuid ## SOMETIMES_CONSUMES gEfiDevicePathProtocolGuid ## SOMETIMES_CONSUMES
gEfiUnicodeCollationProtocolGuid ## SOMETIMES_CONSUMES gEfiUnicodeCollationProtocolGuid ## SOMETIMES_CONSUMES
[Depex] [Depex]
TRUE TRUE

View File

@ -1,42 +1,42 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; Copyright (c) 2024, Mikhail Krichanov. All rights reserved. ; Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
; SPDX-License-Identifier: BSD-3-Clause ; SPDX-License-Identifier: BSD-3-Clause
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
extern ASM_PFX(Ring3Call) extern ASM_PFX(Ring3Call)
DEFAULT REL DEFAULT REL
SECTION .text SECTION .text
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; EFI_STATUS ; EFI_STATUS
; EFIAPI ; EFIAPI
; SysCall ( ; SysCall (
; IN UINT8 Type, ; IN UINT8 Type,
; ... ; ...
; ); ; );
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(SysCall) global ASM_PFX(SysCall)
ASM_PFX(SysCall): ASM_PFX(SysCall):
mov edx, esp mov edx, esp
mov ecx, [esp + 4] ; Type mov ecx, [esp + 4] ; Type
lea eax, [userReturnAddress] lea eax, [userReturnAddress]
sysenter sysenter
userReturnAddress: userReturnAddress:
ret ret
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; VOID ; VOID
; EFIAPI ; EFIAPI
; Ring3EntryPoint ( ; Ring3EntryPoint (
; IN RING3_CALL_DATA *Data ; IN RING3_CALL_DATA *Data
; ); ; );
; ;
; (eax) Data ; (eax) Data
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(Ring3EntryPoint) global ASM_PFX(Ring3EntryPoint)
ASM_PFX(Ring3EntryPoint): ASM_PFX(Ring3EntryPoint):
push eax push eax
call ASM_PFX(Ring3Call) call ASM_PFX(Ring3Call)

File diff suppressed because it is too large Load Diff

View File

@ -1,210 +1,210 @@
/** @file /** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved. Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause SPDX-License-Identifier: BSD-3-Clause
**/ **/
#include "Ring3.h" #include "Ring3.h"
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3BlockIoReset ( Ring3BlockIoReset (
IN EFI_BLOCK_IO_PROTOCOL *This, IN EFI_BLOCK_IO_PROTOCOL *This,
IN BOOLEAN ExtendedVerification IN BOOLEAN ExtendedVerification
) )
{ {
return SysCall ( return SysCall (
SysCallBlockIoReset, SysCallBlockIoReset,
This, This,
ExtendedVerification ExtendedVerification
); );
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3BlockIoRead ( Ring3BlockIoRead (
IN EFI_BLOCK_IO_PROTOCOL *This, IN EFI_BLOCK_IO_PROTOCOL *This,
IN UINT32 MediaId, IN UINT32 MediaId,
IN EFI_LBA Lba, IN EFI_LBA Lba,
IN UINTN BufferSize, IN UINTN BufferSize,
OUT VOID *Buffer OUT VOID *Buffer
) )
{ {
return SysCall ( return SysCall (
SysCallBlockIoRead, SysCallBlockIoRead,
This, This,
MediaId, MediaId,
BufferSize, BufferSize,
Buffer, Buffer,
Lba Lba
); );
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3BlockIoWrite ( Ring3BlockIoWrite (
IN EFI_BLOCK_IO_PROTOCOL *This, IN EFI_BLOCK_IO_PROTOCOL *This,
IN UINT32 MediaId, IN UINT32 MediaId,
IN EFI_LBA Lba, IN EFI_LBA Lba,
IN UINTN BufferSize, IN UINTN BufferSize,
IN VOID *Buffer IN VOID *Buffer
) )
{ {
return SysCall ( return SysCall (
SysCallBlockIoWrite, SysCallBlockIoWrite,
This, This,
MediaId, MediaId,
BufferSize, BufferSize,
Buffer, Buffer,
Lba Lba
); );
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3BlockIoFlush ( Ring3BlockIoFlush (
IN EFI_BLOCK_IO_PROTOCOL *This IN EFI_BLOCK_IO_PROTOCOL *This
) )
{ {
return SysCall ( return SysCall (
SysCallBlockIoFlush, SysCallBlockIoFlush,
This This
); );
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3DiskIoRead ( Ring3DiskIoRead (
IN EFI_DISK_IO_PROTOCOL *This, IN EFI_DISK_IO_PROTOCOL *This,
IN UINT32 MediaId, IN UINT32 MediaId,
IN UINT64 Offset, IN UINT64 Offset,
IN UINTN BufferSize, IN UINTN BufferSize,
OUT VOID *Buffer OUT VOID *Buffer
) )
{ {
return SysCall ( return SysCall (
SysCallDiskIoRead, SysCallDiskIoRead,
This, This,
MediaId, MediaId,
BufferSize, BufferSize,
Buffer, Buffer,
Offset Offset
); );
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3DiskIoWrite ( Ring3DiskIoWrite (
IN EFI_DISK_IO_PROTOCOL *This, IN EFI_DISK_IO_PROTOCOL *This,
IN UINT32 MediaId, IN UINT32 MediaId,
IN UINT64 Offset, IN UINT64 Offset,
IN UINTN BufferSize, IN UINTN BufferSize,
IN VOID *Buffer IN VOID *Buffer
) )
{ {
return SysCall ( return SysCall (
SysCallDiskIoWrite, SysCallDiskIoWrite,
This, This,
MediaId, MediaId,
BufferSize, BufferSize,
Buffer, Buffer,
Offset Offset
); );
} }
INTN INTN
EFIAPI EFIAPI
Ring3UnicodeStriColl ( Ring3UnicodeStriColl (
IN EFI_UNICODE_COLLATION_PROTOCOL *This, IN EFI_UNICODE_COLLATION_PROTOCOL *This,
IN CHAR16 *Str1, IN CHAR16 *Str1,
IN CHAR16 *Str2 IN CHAR16 *Str2
) )
{ {
return (INTN)SysCall ( return (INTN)SysCall (
SysCallUnicodeStriColl, SysCallUnicodeStriColl,
This, This,
Str1, Str1,
Str2 Str2
); );
} }
BOOLEAN BOOLEAN
EFIAPI EFIAPI
Ring3UnicodeMetaiMatch ( Ring3UnicodeMetaiMatch (
IN EFI_UNICODE_COLLATION_PROTOCOL *This, IN EFI_UNICODE_COLLATION_PROTOCOL *This,
IN CHAR16 *String, IN CHAR16 *String,
IN CHAR16 *Pattern IN CHAR16 *Pattern
) )
{ {
return (BOOLEAN)SysCall ( return (BOOLEAN)SysCall (
SysCallUnicodeMetaiMatch, SysCallUnicodeMetaiMatch,
This, This,
String, String,
Pattern Pattern
); );
} }
VOID VOID
EFIAPI EFIAPI
Ring3UnicodeStrLwr ( Ring3UnicodeStrLwr (
IN EFI_UNICODE_COLLATION_PROTOCOL *This, IN EFI_UNICODE_COLLATION_PROTOCOL *This,
IN OUT CHAR16 *Str IN OUT CHAR16 *Str
) )
{ {
SysCall ( SysCall (
SysCallUnicodeStrLwr, SysCallUnicodeStrLwr,
This, This,
Str Str
); );
} }
VOID VOID
EFIAPI EFIAPI
Ring3UnicodeStrUpr ( Ring3UnicodeStrUpr (
IN EFI_UNICODE_COLLATION_PROTOCOL *This, IN EFI_UNICODE_COLLATION_PROTOCOL *This,
IN OUT CHAR16 *Str IN OUT CHAR16 *Str
) )
{ {
SysCall ( SysCall (
SysCallUnicodeStrUpr, SysCallUnicodeStrUpr,
This, This,
Str Str
); );
} }
VOID VOID
EFIAPI EFIAPI
Ring3UnicodeFatToStr ( Ring3UnicodeFatToStr (
IN EFI_UNICODE_COLLATION_PROTOCOL *This, IN EFI_UNICODE_COLLATION_PROTOCOL *This,
IN UINTN FatSize, IN UINTN FatSize,
IN CHAR8 *Fat, IN CHAR8 *Fat,
OUT CHAR16 *String OUT CHAR16 *String
) )
{ {
SysCall ( SysCall (
SysCallUnicodeFatToStr, SysCallUnicodeFatToStr,
This, This,
FatSize, FatSize,
Fat, Fat,
String String
); );
} }
BOOLEAN BOOLEAN
EFIAPI EFIAPI
Ring3UnicodeStrToFat ( Ring3UnicodeStrToFat (
IN EFI_UNICODE_COLLATION_PROTOCOL *This, IN EFI_UNICODE_COLLATION_PROTOCOL *This,
IN CHAR16 *String, IN CHAR16 *String,
IN UINTN FatSize, IN UINTN FatSize,
OUT CHAR8 *Fat OUT CHAR8 *Fat
) )
{ {
return (BOOLEAN)SysCall ( return (BOOLEAN)SysCall (
SysCallUnicodeStrToFat, SysCallUnicodeStrToFat,
This, This,
String, String,
FatSize, FatSize,
Fat Fat
); );
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,194 +1,194 @@
#include <Uefi.h> #include <Uefi.h>
#include <Library/BaseMemoryLib.h> #include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h> #include <Library/DebugLib.h>
#include "Ring3.h" #include "Ring3.h"
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3GetTime ( Ring3GetTime (
OUT EFI_TIME *Time, OUT EFI_TIME *Time,
OUT EFI_TIME_CAPABILITIES *Capabilities OPTIONAL OUT EFI_TIME_CAPABILITIES *Capabilities OPTIONAL
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: GetTime is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: GetTime is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3SetTime ( Ring3SetTime (
IN EFI_TIME *Time IN EFI_TIME *Time
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: SetTime is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: SetTime is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3GetWakeupTime ( Ring3GetWakeupTime (
OUT BOOLEAN *Enabled, OUT BOOLEAN *Enabled,
OUT BOOLEAN *Pending, OUT BOOLEAN *Pending,
OUT EFI_TIME *Time OUT EFI_TIME *Time
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: GetWakeupTime is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: GetWakeupTime is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3SetWakeupTime ( Ring3SetWakeupTime (
IN BOOLEAN Enable, IN BOOLEAN Enable,
IN EFI_TIME *Time OPTIONAL IN EFI_TIME *Time OPTIONAL
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: SetWakeupTime is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: SetWakeupTime is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3SetVirtualAddressMap ( Ring3SetVirtualAddressMap (
IN UINTN MemoryMapSize, IN UINTN MemoryMapSize,
IN UINTN DescriptorSize, IN UINTN DescriptorSize,
IN UINT32 DescriptorVersion, IN UINT32 DescriptorVersion,
IN EFI_MEMORY_DESCRIPTOR *VirtualMap IN EFI_MEMORY_DESCRIPTOR *VirtualMap
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: SetVirtualAddressMap is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: SetVirtualAddressMap is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3ConvertPointer ( Ring3ConvertPointer (
IN UINTN DebugDisposition, IN UINTN DebugDisposition,
IN OUT VOID **Address IN OUT VOID **Address
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: ConvertPointer is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: ConvertPointer is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3GetVariable ( Ring3GetVariable (
IN CHAR16 *VariableName, IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid, IN EFI_GUID *VendorGuid,
OUT UINT32 *Attributes OPTIONAL, OUT UINT32 *Attributes OPTIONAL,
IN OUT UINTN *DataSize, IN OUT UINTN *DataSize,
OUT VOID *Data OPTIONAL OUT VOID *Data OPTIONAL
) )
{ {
return SysCall ( return SysCall (
SysCallGetVariable, SysCallGetVariable,
VariableName, VariableName,
VendorGuid, VendorGuid,
Attributes, Attributes,
DataSize, DataSize,
Data Data
); );
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3GetNextVariableName ( Ring3GetNextVariableName (
IN OUT UINTN *VariableNameSize, IN OUT UINTN *VariableNameSize,
IN OUT CHAR16 *VariableName, IN OUT CHAR16 *VariableName,
IN OUT EFI_GUID *VendorGuid IN OUT EFI_GUID *VendorGuid
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: GetNextVariableName is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: GetNextVariableName is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3SetVariable ( Ring3SetVariable (
IN CHAR16 *VariableName, IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid, IN EFI_GUID *VendorGuid,
IN UINT32 Attributes, IN UINT32 Attributes,
IN UINTN DataSize, IN UINTN DataSize,
IN VOID *Data IN VOID *Data
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: SetVariable is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: SetVariable is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3GetNextHighMonotonicCount ( Ring3GetNextHighMonotonicCount (
OUT UINT32 *HighCount OUT UINT32 *HighCount
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: GetNextHighMonotonicCount is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: GetNextHighMonotonicCount is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
VOID VOID
EFIAPI EFIAPI
Ring3ResetSystem ( Ring3ResetSystem (
IN EFI_RESET_TYPE ResetType, IN EFI_RESET_TYPE ResetType,
IN EFI_STATUS ResetStatus, IN EFI_STATUS ResetStatus,
IN UINTN DataSize, IN UINTN DataSize,
IN VOID *ResetData OPTIONAL IN VOID *ResetData OPTIONAL
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: ResetSystem is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: ResetSystem is not supported\n"));
return; return;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3UpdateCapsule ( Ring3UpdateCapsule (
IN EFI_CAPSULE_HEADER **CapsuleHeaderArray, IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
IN UINTN CapsuleCount, IN UINTN CapsuleCount,
IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: UpdateCapsule is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: UpdateCapsule is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3QueryCapsuleCapabilities ( Ring3QueryCapsuleCapabilities (
IN EFI_CAPSULE_HEADER **CapsuleHeaderArray, IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
IN UINTN CapsuleCount, IN UINTN CapsuleCount,
OUT UINT64 *MaximumCapsuleSize, OUT UINT64 *MaximumCapsuleSize,
OUT EFI_RESET_TYPE *ResetType OUT EFI_RESET_TYPE *ResetType
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: QueryCapsuleCapabilities is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: QueryCapsuleCapabilities is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
Ring3QueryVariableInfo ( Ring3QueryVariableInfo (
IN UINT32 Attributes, IN UINT32 Attributes,
OUT UINT64 *MaximumVariableStorageSize, OUT UINT64 *MaximumVariableStorageSize,
OUT UINT64 *RemainingVariableStorageSize, OUT UINT64 *RemainingVariableStorageSize,
OUT UINT64 *MaximumVariableSize OUT UINT64 *MaximumVariableSize
) )
{ {
DEBUG ((DEBUG_ERROR, "Ring3: QueryVariableInfo is not supported\n")); DEBUG ((DEBUG_ERROR, "Ring3: QueryVariableInfo is not supported\n"));
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }

View File

@ -1,44 +1,44 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; Copyright (c) 2024, Mikhail Krichanov. All rights reserved. ; Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
; SPDX-License-Identifier: BSD-3-Clause ; SPDX-License-Identifier: BSD-3-Clause
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
extern ASM_PFX(Ring3Call) extern ASM_PFX(Ring3Call)
DEFAULT REL DEFAULT REL
SECTION .text SECTION .text
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; EFI_STATUS ; EFI_STATUS
; EFIAPI ; EFIAPI
; SysCall ( ; SysCall (
; IN UINT8 Type, ; IN UINT8 Type,
; ... ; ...
; ); ; );
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(SysCall) global ASM_PFX(SysCall)
ASM_PFX(SysCall): ASM_PFX(SysCall):
; Save Type for CoreBootServices(). ; Save Type for CoreBootServices().
mov r10, rcx mov r10, rcx
; SYSCALL saves RFLAGS into R11 and the RIP of the next instruction into RCX. ; SYSCALL saves RFLAGS into R11 and the RIP of the next instruction into RCX.
syscall syscall
; SYSRET copies the value in RCX into RIP and loads RFLAGS from R11. ; SYSRET copies the value in RCX into RIP and loads RFLAGS from R11.
ret ret
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; VOID ; VOID
; EFIAPI ; EFIAPI
; Ring3EntryPoint ( ; Ring3EntryPoint (
; IN RING3_CALL_DATA *Data ; IN RING3_CALL_DATA *Data
; ); ; );
; ;
; (rcx) RIP of Ring3EntryPoint saved for SYSRET in CallRing3(). ; (rcx) RIP of Ring3EntryPoint saved for SYSRET in CallRing3().
; (rdx) Data ; (rdx) Data
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(Ring3EntryPoint) global ASM_PFX(Ring3EntryPoint)
ASM_PFX(Ring3EntryPoint): ASM_PFX(Ring3EntryPoint):
mov rcx, rdx mov rcx, rdx
call ASM_PFX(Ring3Call) call ASM_PFX(Ring3Call)

View File

@ -1,169 +1,169 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// //
// Copyright (c) 2024, Mikhail Krichanov. All rights reserved. // Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
// //
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <AsmMacroIoLibV8.h> #include <AsmMacroIoLibV8.h>
.cpu cortex-a76 .cpu cortex-a76
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// EFI_STATUS // EFI_STATUS
// EFIAPI // EFIAPI
// CallInstallMultipleProtocolInterfaces ( // CallInstallMultipleProtocolInterfaces (
// IN EFI_HANDLE *Handle, // IN EFI_HANDLE *Handle,
// IN VOID **ArgList, // IN VOID **ArgList,
// IN UINT32 ArgListSize, // IN UINT32 ArgListSize,
// IN VOID *Function // IN VOID *Function
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(CallInstallMultipleProtocolInterfaces) ASM_FUNC(CallInstallMultipleProtocolInterfaces)
stp x29, x30, [sp, #-0x10]! stp x29, x30, [sp, #-0x10]!
mov x29, sp mov x29, sp
// Save function input. // Save function input.
mov x9, x1 mov x9, x1
mov x10, x2 mov x10, x2
mov x11, x3 mov x11, x3
// Prepare registers for call. // Prepare registers for call.
ldp x1, x2, [x9] ldp x1, x2, [x9]
ldp x3, x4, [x9, #0x10] ldp x3, x4, [x9, #0x10]
ldp x5, x6, [x9, #0x20] ldp x5, x6, [x9, #0x20]
ldr x7, [x9, #0x30] ldr x7, [x9, #0x30]
// Prepare stack for call. // Prepare stack for call.
cmp x10, #7 cmp x10, #7
b.le call b.le call
add x9, x9, x10, LSL #3 add x9, x9, x10, LSL #3
sub x10, x10, #7 sub x10, x10, #7
tst x10, #1 tst x10, #1
b.eq copy b.eq copy
// To align stack on 16 bytes. // To align stack on 16 bytes.
add x9, x9, #0x8 add x9, x9, #0x8
add x10, x10, #1 add x10, x10, #1
copy: copy:
ldp x12, x13, [x9, #-0x10]! ldp x12, x13, [x9, #-0x10]!
stp x12, x13, [sp, #-0x10]! stp x12, x13, [sp, #-0x10]!
subs x10, x10, #2 subs x10, x10, #2
b.ne copy b.ne copy
call: call:
blr x11 blr x11
mov sp, x29 mov sp, x29
ldp x29, x30, [sp] ldp x29, x30, [sp]
add sp, sp, #0x10 add sp, sp, #0x10
ret ret
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// EFI_STATUS // EFI_STATUS
// EFIAPI // EFIAPI
// CallRing3 ( // CallRing3 (
// IN RING3_CALL_DATA *Data // IN RING3_CALL_DATA *Data
// ); // );
// //
// (x0) Data // (x0) Data
// (x1) gRing3CallStackTop // (x1) gRing3CallStackTop
// (x2) gRing3EntryPoint // (x2) gRing3EntryPoint
// (x3) gCoreSysCallStackTop // (x3) gCoreSysCallStackTop
// (x4) &CoreSp // (x4) &CoreSp
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(ArmCallRing3) ASM_FUNC(ArmCallRing3)
// Save registers. // Save registers.
sub sp, sp, #0x100 sub sp, sp, #0x100
stp q8, q9, [sp, #0xe0] stp q8, q9, [sp, #0xe0]
stp q10, q11, [sp, #0xc0] stp q10, q11, [sp, #0xc0]
stp q12, q13, [sp, #0xa0] stp q12, q13, [sp, #0xa0]
stp q14, q15, [sp, #0x80] stp q14, q15, [sp, #0x80]
stp xzr, x16, [sp, #0x70] stp xzr, x16, [sp, #0x70]
stp x17, x18, [sp, #0x60] stp x17, x18, [sp, #0x60]
stp x19, x20, [sp, #0x50] stp x19, x20, [sp, #0x50]
stp x21, x22, [sp, #0x40] stp x21, x22, [sp, #0x40]
stp x23, x24, [sp, #0x30] stp x23, x24, [sp, #0x30]
stp x25, x26, [sp, #0x20] stp x25, x26, [sp, #0x20]
stp x27, x28, [sp, #0x10] stp x27, x28, [sp, #0x10]
stp x29, x30, [sp] stp x29, x30, [sp]
// Disable interrupts. // Disable interrupts.
msr daifset, #0xf msr daifset, #0xf
isb isb
// Prepare Ring3 SP and EntryPoint. // Prepare Ring3 SP and EntryPoint.
msr sp_el0, x1 msr sp_el0, x1
msr elr_el1, x2 msr elr_el1, x2
// Save Core SP and switch to CoreSysCall Stack. // Save Core SP and switch to CoreSysCall Stack.
mov x5, sp mov x5, sp
str x5, [x4] str x5, [x4]
mov sp, x3 mov sp, x3
// Copy PSTATE to SPSR. // Copy PSTATE to SPSR.
mrs x1, nzcv mrs x1, nzcv
mrs x2, pan mrs x2, pan
orr x1, x1, x2 orr x1, x1, x2
// //
// M[3:0], bits [3:0] AArch64 Exception level and selected Stack Pointer. // M[3:0], bits [3:0] AArch64 Exception level and selected Stack Pointer.
// 0b0000 - EL0. // 0b0000 - EL0.
// 0b0100 - EL1 with SP_EL0 (ELt). // 0b0100 - EL1 with SP_EL0 (ELt).
// 0b0101 - EL1 with SP_EL1 (EL1h). // 0b0101 - EL1 with SP_EL1 (EL1h).
// //
msr spsr_el1, x1 msr spsr_el1, x1
isb isb
dsb sy dsb sy
eret eret
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VOID // VOID
// EFIAPI // EFIAPI
// ReturnToCore ( // ReturnToCore (
// IN EFI_STATUS Status, // IN EFI_STATUS Status,
// IN UINTN CoreSp // IN UINTN CoreSp
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(ReturnToCore) ASM_FUNC(ReturnToCore)
// Zero Exception Syndrome Register to prevent QEMU from random crashing. // Zero Exception Syndrome Register to prevent QEMU from random crashing.
msr esr_el1, xzr msr esr_el1, xzr
msr spsr_el1, xzr msr spsr_el1, xzr
msr elr_el1, xzr msr elr_el1, xzr
msr far_el1, xzr msr far_el1, xzr
// Switch to Core Stack. // Switch to Core Stack.
mov sp, x1 mov sp, x1
// Restore registers and Stack. // Restore registers and Stack.
ldp q8, q9, [sp, #0xe0] ldp q8, q9, [sp, #0xe0]
ldp q10, q11, [sp, #0xc0] ldp q10, q11, [sp, #0xc0]
ldp q12, q13, [sp, #0xa0] ldp q12, q13, [sp, #0xa0]
ldp q14, q15, [sp, #0x80] ldp q14, q15, [sp, #0x80]
ldr x16, [sp, #0x78] ldr x16, [sp, #0x78]
ldp x17, x18, [sp, #0x60] ldp x17, x18, [sp, #0x60]
ldp x19, x20, [sp, #0x50] ldp x19, x20, [sp, #0x50]
ldp x21, x22, [sp, #0x40] ldp x21, x22, [sp, #0x40]
ldp x23, x24, [sp, #0x30] ldp x23, x24, [sp, #0x30]
ldp x25, x26, [sp, #0x20] ldp x25, x26, [sp, #0x20]
ldp x27, x28, [sp, #0x10] ldp x27, x28, [sp, #0x10]
ldp x29, x30, [sp] ldp x29, x30, [sp]
add sp, sp, #0x100 add sp, sp, #0x100
// Enable interrupts. // Enable interrupts.
msr daifclr, #0xf msr daifclr, #0xf
isb isb
ret ret
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VOID // VOID
// EFIAPI // EFIAPI
// ArmSetPan ( // ArmSetPan (
// VOID // VOID
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(ArmSetPan) ASM_FUNC(ArmSetPan)
msr pan, #1 msr pan, #1
ret ret
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VOID // VOID
// EFIAPI // EFIAPI
// ArmClearPan ( // ArmClearPan (
// VOID // VOID
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(ArmClearPan) ASM_FUNC(ArmClearPan)
msr pan, #0 msr pan, #0
ret ret

View File

@ -1,210 +1,210 @@
/** @file /** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved. Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause SPDX-License-Identifier: BSD-3-Clause
**/ **/
#include <Chipset/AArch64.h> #include <Chipset/AArch64.h>
#include <Guid/EarlyPL011BaseAddress.h> #include <Guid/EarlyPL011BaseAddress.h>
#include <Library/ArmLib.h> #include <Library/ArmLib.h>
#include <Library/DefaultExceptionHandlerLib.h> #include <Library/DefaultExceptionHandlerLib.h>
#include "DxeMain.h" #include "DxeMain.h"
STATIC UINTN mCoreSp; STATIC UINTN mCoreSp;
UINTN gUartBaseAddress; UINTN gUartBaseAddress;
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
ArmCallRing3 ( ArmCallRing3 (
IN RING3_CALL_DATA *Data, IN RING3_CALL_DATA *Data,
IN VOID *StackPointer, IN VOID *StackPointer,
IN VOID *EntryPoint, IN VOID *EntryPoint,
IN VOID *SysCallStack, IN VOID *SysCallStack,
IN VOID *CoreStack IN VOID *CoreStack
); );
VOID VOID
EFIAPI EFIAPI
ReturnToCore ( ReturnToCore (
IN EFI_STATUS Status, IN EFI_STATUS Status,
IN UINTN CoreSp IN UINTN CoreSp
); );
VOID VOID
EFIAPI EFIAPI
ArmSetPan ( ArmSetPan (
VOID VOID
); );
VOID VOID
EFIAPI EFIAPI
ArmClearPan ( ArmClearPan (
VOID VOID
); );
STATIC STATIC
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
SysCallBootService ( SysCallBootService (
IN UINT8 Type, IN UINT8 Type,
IN VOID *CoreRbp, IN VOID *CoreRbp,
IN VOID *UserRsp IN VOID *UserRsp
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Physical; EFI_PHYSICAL_ADDRESS Physical;
if (Type == SysCallReturnToCore) { if (Type == SysCallReturnToCore) {
ReturnToCore (*(EFI_STATUS *)CoreRbp, mCoreSp); ReturnToCore (*(EFI_STATUS *)CoreRbp, mCoreSp);
} }
Status = CoreAllocatePages ( Status = CoreAllocatePages (
AllocateAnyPages, AllocateAnyPages,
EfiRing3MemoryType, EfiRing3MemoryType,
EFI_SIZE_TO_PAGES (9 * sizeof (UINTN)), EFI_SIZE_TO_PAGES (9 * sizeof (UINTN)),
&Physical &Physical
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return Status; return Status;
} }
DisableSMAP (); DisableSMAP ();
CopyMem ((VOID *)((UINTN)Physical + sizeof (UINTN)), (VOID *)UserRsp, 8 * sizeof (UINTN)); CopyMem ((VOID *)((UINTN)Physical + sizeof (UINTN)), (VOID *)UserRsp, 8 * sizeof (UINTN));
SetUefiImageMemoryAttributes ( SetUefiImageMemoryAttributes (
gUartBaseAddress, gUartBaseAddress,
EFI_PAGE_SIZE, EFI_PAGE_SIZE,
EFI_MEMORY_XP EFI_MEMORY_XP
); );
EnableSMAP (); EnableSMAP ();
Status = CallBootService ( Status = CallBootService (
Type, Type,
(CORE_STACK *)CoreRbp, (CORE_STACK *)CoreRbp,
(RING3_STACK *)(UINTN)Physical (RING3_STACK *)(UINTN)Physical
); );
SetUefiImageMemoryAttributes ( SetUefiImageMemoryAttributes (
gUartBaseAddress, gUartBaseAddress,
EFI_PAGE_SIZE, EFI_PAGE_SIZE,
EFI_MEMORY_XP | EFI_MEMORY_USER EFI_MEMORY_XP | EFI_MEMORY_USER
); );
CoreFreePages (Physical, EFI_SIZE_TO_PAGES (9 * sizeof (UINTN))); CoreFreePages (Physical, EFI_SIZE_TO_PAGES (9 * sizeof (UINTN)));
return Status; return Status;
} }
VOID VOID
EFIAPI EFIAPI
InitializeMsr ( InitializeMsr (
IN OUT EFI_CONFIGURATION_TABLE *Table, IN OUT EFI_CONFIGURATION_TABLE *Table,
IN UINTN NumberOfEntries IN UINTN NumberOfEntries
) )
{ {
UINTN Tcr; UINTN Tcr;
UINTN Index; UINTN Index;
EARLY_PL011_BASE_ADDRESS *UartBase; EARLY_PL011_BASE_ADDRESS *UartBase;
EFI_PHYSICAL_ADDRESS Physical; EFI_PHYSICAL_ADDRESS Physical;
EFI_HOB_GENERIC_HEADER *Ring3Hob; EFI_HOB_GENERIC_HEADER *Ring3Hob;
UINT16 HobLength; UINT16 HobLength;
EFI_STATUS Status; EFI_STATUS Status;
// //
// If HCR_EL2.NV is 1 and the current Exception level is EL1, // If HCR_EL2.NV is 1 and the current Exception level is EL1,
// then EL1 read accesses to the CurrentEL register return a value of 0x2 in bits[3:2]. // then EL1 read accesses to the CurrentEL register return a value of 0x2 in bits[3:2].
// CurrentEL == 1 -> HCR_EL2.NV == 0 // CurrentEL == 1 -> HCR_EL2.NV == 0
// //
// If stage 1 is enabled and stage 1 Base permissions use Direct permissions, // If stage 1 is enabled and stage 1 Base permissions use Direct permissions,
// then GCS access is not permitted and UnprivGCS and PrivGCS are not present. // then GCS access is not permitted and UnprivGCS and PrivGCS are not present.
// //
// Disable Hierarchical permissions just in case. // Disable Hierarchical permissions just in case.
// //
Tcr = ArmGetTCR (); Tcr = ArmGetTCR ();
Tcr |= TCR_EL1_HPD0_MASK | TCR_EL1_HPD1_MASK; Tcr |= TCR_EL1_HPD0_MASK | TCR_EL1_HPD1_MASK;
ArmSetTCR (Tcr); ArmSetTCR (Tcr);
// //
// Problem 1: Uart is memory maped. // Problem 1: Uart is memory maped.
// //
for (Index = 0; Index < NumberOfEntries; ++Index) { for (Index = 0; Index < NumberOfEntries; ++Index) {
if (CompareGuid (&gEfiHobListGuid, &(Table[Index].VendorGuid))) { if (CompareGuid (&gEfiHobListGuid, &(Table[Index].VendorGuid))) {
UartBase = GET_GUID_HOB_DATA (Table[Index].VendorTable); UartBase = GET_GUID_HOB_DATA (Table[Index].VendorTable);
gUartBaseAddress = UartBase->DebugAddress; gUartBaseAddress = UartBase->DebugAddress;
// //
// Copy Hob into Ring3. // Copy Hob into Ring3.
// //
Status = CoreAllocatePages ( Status = CoreAllocatePages (
AllocateAnyPages, AllocateAnyPages,
EfiRing3MemoryType, EfiRing3MemoryType,
1, 1,
&Physical &Physical
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3Hob.\n")); DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3Hob.\n"));
ASSERT (FALSE); ASSERT (FALSE);
} }
DEBUG ((DEBUG_ERROR, "UartBaseAddress = %p.\n", gUartBaseAddress)); DEBUG ((DEBUG_ERROR, "UartBaseAddress = %p.\n", gUartBaseAddress));
Ring3Hob = (EFI_HOB_GENERIC_HEADER *)(UINTN)Physical; Ring3Hob = (EFI_HOB_GENERIC_HEADER *)(UINTN)Physical;
HobLength = (UINT16)((sizeof (EFI_HOB_GUID_TYPE) + sizeof (EARLY_PL011_BASE_ADDRESS) + 0x7) & (~0x7)); HobLength = (UINT16)((sizeof (EFI_HOB_GUID_TYPE) + sizeof (EARLY_PL011_BASE_ADDRESS) + 0x7) & (~0x7));
Ring3Hob->HobType = EFI_HOB_TYPE_GUID_EXTENSION; Ring3Hob->HobType = EFI_HOB_TYPE_GUID_EXTENSION;
Ring3Hob->HobLength = HobLength; Ring3Hob->HobLength = HobLength;
Ring3Hob->Reserved = 0; Ring3Hob->Reserved = 0;
CopyGuid (&((EFI_HOB_GUID_TYPE *)Ring3Hob)->Name, &gEarlyPL011BaseAddressGuid); CopyGuid (&((EFI_HOB_GUID_TYPE *)Ring3Hob)->Name, &gEarlyPL011BaseAddressGuid);
Ring3Hob = (EFI_HOB_GENERIC_HEADER *)((UINTN)Ring3Hob + HobLength); Ring3Hob = (EFI_HOB_GENERIC_HEADER *)((UINTN)Ring3Hob + HobLength);
Ring3Hob->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST; Ring3Hob->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
Ring3Hob->HobLength = sizeof (EFI_HOB_GENERIC_HEADER); Ring3Hob->HobLength = sizeof (EFI_HOB_GENERIC_HEADER);
Ring3Hob->Reserved = 0; Ring3Hob->Reserved = 0;
Table[Index].VendorTable = (VOID *)(UINTN)Physical; Table[Index].VendorTable = (VOID *)(UINTN)Physical;
UartBase = GET_GUID_HOB_DATA (Table[Index].VendorTable); UartBase = GET_GUID_HOB_DATA (Table[Index].VendorTable);
UartBase->DebugAddress = gUartBaseAddress; UartBase->DebugAddress = gUartBaseAddress;
} }
} }
if (ArmHasPan ()) { if (ArmHasPan ()) {
// //
// Enable Privileged Access Never feature. // Enable Privileged Access Never feature.
// //
ArmSetPan (); ArmSetPan ();
} }
InitializeSysCallHandler ((VOID *)SysCallBootService); InitializeSysCallHandler ((VOID *)SysCallBootService);
} }
VOID VOID
EFIAPI EFIAPI
DisableSMAP ( DisableSMAP (
VOID VOID
) )
{ {
if (ArmHasPan ()) { if (ArmHasPan ()) {
ArmClearPan (); ArmClearPan ();
} }
} }
VOID VOID
EFIAPI EFIAPI
EnableSMAP ( EnableSMAP (
VOID VOID
) )
{ {
if (ArmHasPan ()) { if (ArmHasPan ()) {
ArmSetPan (); ArmSetPan ();
} }
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
CallRing3 ( CallRing3 (
IN RING3_CALL_DATA *Data IN RING3_CALL_DATA *Data
) )
{ {
return ArmCallRing3 (Data, gRing3CallStackTop, gRing3EntryPoint, gCoreSysCallStackTop, &mCoreSp); return ArmCallRing3 (Data, gRing3CallStackTop, gRing3EntryPoint, gCoreSysCallStackTop, &mCoreSp);
} }

View File

@ -1,148 +1,148 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// //
// Copyright (c) 2024, Mikhail Krichanov. All rights reserved. // Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
// //
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <AsmMacroIoLib.h> #include <AsmMacroIoLib.h>
.arch armv8.1a .arch armv8.1a
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// EFI_STATUS // EFI_STATUS
// EFIAPI // EFIAPI
// CallInstallMultipleProtocolInterfaces ( // CallInstallMultipleProtocolInterfaces (
// IN EFI_HANDLE *Handle, // IN EFI_HANDLE *Handle,
// IN VOID **ArgList, // IN VOID **ArgList,
// IN UINT32 ArgListSize, // IN UINT32 ArgListSize,
// IN VOID *Function // IN VOID *Function
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(CallInstallMultipleProtocolInterfaces) ASM_FUNC(CallInstallMultipleProtocolInterfaces)
push {R4-R8, LR} push {R4-R8, LR}
mov R7, SP mov R7, SP
// Save function input. // Save function input.
mov R4, R3 mov R4, R3
mov R5, R1 mov R5, R1
mov R6, R2 mov R6, R2
// Prepare registers for call. // Prepare registers for call.
ldmfd R5, {R1-R3} ldmfd R5, {R1-R3}
// Prepare stack for call. // Prepare stack for call.
cmp R6, #3 cmp R6, #3
ble call ble call
add R5, R5, R6, LSL #2 add R5, R5, R6, LSL #2
sub R6, R6, #3 sub R6, R6, #3
tst R6, #1 tst R6, #1
beq copy beq copy
// To align stack on 8 bytes. // To align stack on 8 bytes.
add R5, R5, #0x4 add R5, R5, #0x4
add R6, R6, #1 add R6, R6, #1
copy: copy:
sub R5, R5, #0x4 sub R5, R5, #0x4
ldmfd R5, {R8} ldmfd R5, {R8}
push {R8} push {R8}
subs R6, R6, #1 subs R6, R6, #1
bne copy bne copy
call: call:
blx R4 blx R4
mov SP, R7 mov SP, R7
pop {R4-R8, LR} pop {R4-R8, LR}
bx LR bx LR
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// EFI_STATUS // EFI_STATUS
// EFIAPI // EFIAPI
// CallRing3 ( // CallRing3 (
// IN RING3_CALL_DATA *Data // IN RING3_CALL_DATA *Data
// ); // );
// //
// (r0) Data // (r0) Data
// (r1) gRing3CallStackTop // (r1) gRing3CallStackTop
// (r2) gRing3EntryPoint // (r2) gRing3EntryPoint
// (r3) gCoreSysCallStackTop // (r3) gCoreSysCallStackTop
// //
// (On Core Stack) &CoreSp // (On Core Stack) &CoreSp
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(ArmCallRing3) ASM_FUNC(ArmCallRing3)
// Save registers. // Save registers.
push {R4-R12, LR} push {R4-R12, LR}
// R6 is &CoreSp // R6 is &CoreSp
ldr R6, [SP, #0x28] ldr R6, [SP, #0x28]
#if (FixedPcdGet32(PcdVFPEnabled)) #if (FixedPcdGet32(PcdVFPEnabled))
// Save vstm registers in case they are used in optimizations. // Save vstm registers in case they are used in optimizations.
vpush {d0-d15} vpush {d0-d15}
#endif #endif
// Disable interrupts. // Disable interrupts.
mrs R4, CPSR mrs R4, CPSR
cpsid if cpsid if
isb isb
// Set SP_usr to gRing3CallStackTop. // Set SP_usr to gRing3CallStackTop.
push {R1} push {R1}
mov R1, SP mov R1, SP
ldmia R1, {SP}^ ldmia R1, {SP}^
pop {R1} pop {R1}
// Set SPSR M[3:0] bits to User mode. // Set SPSR M[3:0] bits to User mode.
and R4, R4, #0xFFFFFFF0 and R4, R4, #0xFFFFFFF0
// Save Core SP and switch to CoreSysCall Stack. // Save Core SP and switch to CoreSysCall Stack.
mov R5, SP mov R5, SP
str R5, [R6] str R5, [R6]
mov SP, R3 mov SP, R3
push {R4} push {R4}
push {R2} push {R2}
rfefd SP rfefd SP
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VOID // VOID
// EFIAPI // EFIAPI
// ReturnToCore ( // ReturnToCore (
// IN EFI_STATUS Status, // IN EFI_STATUS Status,
// IN UINTN CoreSp // IN UINTN CoreSp
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(ReturnToCore) ASM_FUNC(ReturnToCore)
// Switch to Core Stack. // Switch to Core Stack.
mov SP, R1 mov SP, R1
// Restore registers and Stack. // Restore registers and Stack.
#if (FixedPcdGet32(PcdVFPEnabled)) #if (FixedPcdGet32(PcdVFPEnabled))
vpop {d0-d15} vpop {d0-d15}
#endif #endif
pop {R4-R12, LR} pop {R4-R12, LR}
// Enable interrupts. // Enable interrupts.
cpsie if cpsie if
isb isb
bx LR bx LR
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VOID // VOID
// EFIAPI // EFIAPI
// ArmSetPan ( // ArmSetPan (
// VOID // VOID
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(ArmSetPan) ASM_FUNC(ArmSetPan)
setpan #1 setpan #1
bx LR bx LR
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VOID // VOID
// EFIAPI // EFIAPI
// ArmClearPan ( // ArmClearPan (
// VOID // VOID
// ); // );
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ASM_FUNC(ArmClearPan) ASM_FUNC(ArmClearPan)
setpan #0 setpan #0
bx LR bx LR

View File

@ -1,145 +1,145 @@
/** @file /** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved. Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause SPDX-License-Identifier: BSD-3-Clause
**/ **/
#include <Library/ArmLib.h> #include <Library/ArmLib.h>
#include <Library/DefaultExceptionHandlerLib.h> #include <Library/DefaultExceptionHandlerLib.h>
#include "DxeMain.h" #include "DxeMain.h"
STATIC UINTN mCoreSp; STATIC UINTN mCoreSp;
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
ArmCallRing3 ( ArmCallRing3 (
IN RING3_CALL_DATA *Data, IN RING3_CALL_DATA *Data,
IN VOID *StackPointer, IN VOID *StackPointer,
IN VOID *EntryPoint, IN VOID *EntryPoint,
IN VOID *SysCallStack, IN VOID *SysCallStack,
IN VOID *CoreStack IN VOID *CoreStack
); );
VOID VOID
EFIAPI EFIAPI
ReturnToCore ( ReturnToCore (
IN EFI_STATUS Status, IN EFI_STATUS Status,
IN UINTN CoreSp IN UINTN CoreSp
); );
VOID VOID
EFIAPI EFIAPI
ArmSetPan ( ArmSetPan (
VOID VOID
); );
VOID VOID
EFIAPI EFIAPI
ArmClearPan ( ArmClearPan (
VOID VOID
); );
STATIC STATIC
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
SysCallBootService ( SysCallBootService (
IN UINT8 Type, IN UINT8 Type,
IN VOID *CoreRbp, IN VOID *CoreRbp,
IN VOID *UserRsp IN VOID *UserRsp
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Physical; EFI_PHYSICAL_ADDRESS Physical;
if (Type == SysCallReturnToCore) { if (Type == SysCallReturnToCore) {
ReturnToCore (*(EFI_STATUS *)CoreRbp, mCoreSp); ReturnToCore (*(EFI_STATUS *)CoreRbp, mCoreSp);
} }
Status = CoreAllocatePages ( Status = CoreAllocatePages (
AllocateAnyPages, AllocateAnyPages,
EfiRing3MemoryType, EfiRing3MemoryType,
EFI_SIZE_TO_PAGES (9 * sizeof (UINTN)), EFI_SIZE_TO_PAGES (9 * sizeof (UINTN)),
&Physical &Physical
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return Status; return Status;
} }
DisableSMAP (); DisableSMAP ();
// //
// First 3 arguments are passed through R1-R3 and copied to SysCall Stack. // First 3 arguments are passed through R1-R3 and copied to SysCall Stack.
// //
CopyMem ((VOID *)((UINTN)Physical + 2 * sizeof (UINTN)), (VOID *)CoreRbp, 3 * sizeof (UINTN)); CopyMem ((VOID *)((UINTN)Physical + 2 * sizeof (UINTN)), (VOID *)CoreRbp, 3 * sizeof (UINTN));
// //
// All remaining arguments are on User Stack. // All remaining arguments are on User Stack.
// //
CopyMem ((VOID *)((UINTN)Physical + 5 * sizeof (UINTN)), (VOID *)UserRsp, 4 * sizeof (UINTN)); CopyMem ((VOID *)((UINTN)Physical + 5 * sizeof (UINTN)), (VOID *)UserRsp, 4 * sizeof (UINTN));
EnableSMAP (); EnableSMAP ();
Status = CallBootService ( Status = CallBootService (
Type, Type,
(CORE_STACK *)CoreRbp, (CORE_STACK *)CoreRbp,
(RING3_STACK *)(UINTN)Physical (RING3_STACK *)(UINTN)Physical
); );
CoreFreePages (Physical, EFI_SIZE_TO_PAGES (9 * sizeof (UINTN))); CoreFreePages (Physical, EFI_SIZE_TO_PAGES (9 * sizeof (UINTN)));
return Status; return Status;
} }
VOID VOID
EFIAPI EFIAPI
InitializeMsr ( InitializeMsr (
IN OUT EFI_CONFIGURATION_TABLE *Table, IN OUT EFI_CONFIGURATION_TABLE *Table,
IN UINTN NumberOfEntries IN UINTN NumberOfEntries
) )
{ {
// //
// TODO: EFI_CONFIGURATION_TABLE, HOB_DATA, Uart are accessible to User. // TODO: EFI_CONFIGURATION_TABLE, HOB_DATA, Uart are accessible to User.
// Fix PageTable initialization. // Fix PageTable initialization.
// //
if (ArmHasPan ()) { if (ArmHasPan ()) {
// //
// Enable Privileged Access Never feature. // Enable Privileged Access Never feature.
// //
ArmSetPan (); ArmSetPan ();
} }
InitializeSysCallHandler (SysCallBootService); InitializeSysCallHandler (SysCallBootService);
} }
// //
// TODO: Refactoring. // TODO: Refactoring.
// //
VOID VOID
EFIAPI EFIAPI
DisableSMAP ( DisableSMAP (
VOID VOID
) )
{ {
if (ArmHasPan ()) { if (ArmHasPan ()) {
ArmClearPan (); ArmClearPan ();
} }
} }
VOID VOID
EFIAPI EFIAPI
EnableSMAP ( EnableSMAP (
VOID VOID
) )
{ {
if (ArmHasPan ()) { if (ArmHasPan ()) {
ArmSetPan (); ArmSetPan ();
} }
} }
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
CallRing3 ( CallRing3 (
IN RING3_CALL_DATA *Data IN RING3_CALL_DATA *Data
) )
{ {
return ArmCallRing3 (Data, gRing3CallStackTop, gRing3EntryPoint, gCoreSysCallStackTop, &mCoreSp); return ArmCallRing3 (Data, gRing3CallStackTop, gRing3EntryPoint, gCoreSysCallStackTop, &mCoreSp);
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,223 +1,223 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; ;
; Copyright (c) 2024, Mikhail Krichanov. All rights reserved. ; Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
; SPDX-License-Identifier: BSD-3-Clause ; SPDX-License-Identifier: BSD-3-Clause
; ;
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
#include <Register/Intel/ArchitecturalMsr.h> #include <Register/Intel/ArchitecturalMsr.h>
extern ASM_PFX(CallBootService) extern ASM_PFX(CallBootService)
extern ASM_PFX(gCoreSysCallStackTop) extern ASM_PFX(gCoreSysCallStackTop)
extern ASM_PFX(gRing3CallStackTop) extern ASM_PFX(gRing3CallStackTop)
extern ASM_PFX(gRing3EntryPoint) extern ASM_PFX(gRing3EntryPoint)
extern ASM_PFX(AsmReadMsr64) extern ASM_PFX(AsmReadMsr64)
DEFAULT REL DEFAULT REL
SECTION .text SECTION .text
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; VOID ; VOID
; EFIAPI ; EFIAPI
; DisableSMAP ( ; DisableSMAP (
; VOID ; VOID
; ); ; );
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(DisableSMAP) global ASM_PFX(DisableSMAP)
ASM_PFX(DisableSMAP): ASM_PFX(DisableSMAP):
pushfd pushfd
pop eax pop eax
or eax, 0x40000 ; Set AC (bit 18) or eax, 0x40000 ; Set AC (bit 18)
push eax push eax
popfd popfd
ret ret
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; VOID ; VOID
; EFIAPI ; EFIAPI
; EnableSMAP ( ; EnableSMAP (
; VOID ; VOID
; ); ; );
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(EnableSMAP) global ASM_PFX(EnableSMAP)
ASM_PFX(EnableSMAP): ASM_PFX(EnableSMAP):
pushfd pushfd
pop eax pop eax
and eax, ~0x40000 ; Clear AC (bit 18) and eax, ~0x40000 ; Clear AC (bit 18)
push eax push eax
popfd popfd
ret ret
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; EFI_STATUS ; EFI_STATUS
; EFIAPI ; EFIAPI
; CallInstallMultipleProtocolInterfaces ( ; CallInstallMultipleProtocolInterfaces (
; IN EFI_HANDLE *Handle, ; IN EFI_HANDLE *Handle,
; IN VOID **ArgList, ; IN VOID **ArgList,
; IN UINT32 ArgListSize, ; IN UINT32 ArgListSize,
; IN VOID *Function ; IN VOID *Function
; ); ; );
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(CallInstallMultipleProtocolInterfaces) global ASM_PFX(CallInstallMultipleProtocolInterfaces)
ASM_PFX(CallInstallMultipleProtocolInterfaces): ASM_PFX(CallInstallMultipleProtocolInterfaces):
push ebp push ebp
mov ebp, esp mov ebp, esp
; Prepare stack for call. ; Prepare stack for call.
mov eax, [ebp + 3 * 4] ; eax = ArgList mov eax, [ebp + 3 * 4] ; eax = ArgList
mov ecx, [ebp + 4 * 4] ; ecx = ArgListSize mov ecx, [ebp + 4 * 4] ; ecx = ArgListSize
lea eax, [eax + ecx * 4] lea eax, [eax + ecx * 4]
copy: copy:
sub eax, 4 sub eax, 4
push dword [eax] push dword [eax]
sub ecx, 1 sub ecx, 1
jnz copy jnz copy
push dword [ebp + 2 * 4] push dword [ebp + 2 * 4]
call [ebp + 5 * 4] call [ebp + 5 * 4]
; Step over Function arguments. ; Step over Function arguments.
mov esp, ebp mov esp, ebp
pop ebp pop ebp
ret ret
%macro SetRing3DataSegmentSelectors 0 %macro SetRing3DataSegmentSelectors 0
push dword MSR_IA32_SYSENTER_CS push dword MSR_IA32_SYSENTER_CS
call ASM_PFX(AsmReadMsr64) call ASM_PFX(AsmReadMsr64)
; eax = RING0_CODE32_SEL ; eax = RING0_CODE32_SEL
add eax, 24 ; GDT: RING0_CODE32, RING0_DATA32, RING3_CODE32, RING3_DATA32 add eax, 24 ; GDT: RING0_CODE32, RING0_DATA32, RING3_CODE32, RING3_DATA32
or eax, 3 ; RPL = 3 or eax, 3 ; RPL = 3
mov ds, ax mov ds, ax
mov es, ax mov es, ax
mov fs, ax mov fs, ax
mov gs, ax mov gs, ax
pop eax pop eax
%endmacro %endmacro
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; EFI_STATUS ; EFI_STATUS
; EFIAPI ; EFIAPI
; CoreBootServices ( ; CoreBootServices (
; IN UINT8 Type, ; IN UINT8 Type,
; ... ; ...
; ); ; );
; ;
; (eax) User return address. ; (eax) User return address.
; (ecx) Type. ; (ecx) Type.
; (edx) User Stack Pointer. ; (edx) User Stack Pointer.
; ;
; (On User Stack) Argument 1, 2, ... ; (On User Stack) Argument 1, 2, ...
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(CoreBootServices) global ASM_PFX(CoreBootServices)
ASM_PFX(CoreBootServices): ASM_PFX(CoreBootServices):
; Save User return address and Stack pointers. ; Save User return address and Stack pointers.
push edx push edx
push ebp push ebp
push eax push eax
; Switch from User to Core data segment selectors. ; Switch from User to Core data segment selectors.
mov ax, ss mov ax, ss
mov ds, ax mov ds, ax
mov es, ax mov es, ax
mov fs, ax mov fs, ax
mov gs, ax mov gs, ax
; Special case for SysCallReturnToCore. ; Special case for SysCallReturnToCore.
cmp ecx, 0 cmp ecx, 0
je coreReturnAddress je coreReturnAddress
; Prepare CallBootService arguments. ; Prepare CallBootService arguments.
call ASM_PFX(DisableSMAP) call ASM_PFX(DisableSMAP)
mov eax, [edx + 4 * 4] ; User Argument 3 mov eax, [edx + 4 * 4] ; User Argument 3
push eax push eax
mov eax, [edx + 3 * 4] ; User Argument 2 mov eax, [edx + 3 * 4] ; User Argument 2
push eax push eax
mov eax, [edx + 2 * 4] ; User Argument 1 mov eax, [edx + 2 * 4] ; User Argument 1
push eax push eax
call ASM_PFX(EnableSMAP) call ASM_PFX(EnableSMAP)
mov ebp, esp mov ebp, esp
push edx push edx
push ebp push ebp
push ecx push ecx
sti sti
call ASM_PFX(CallBootService) call ASM_PFX(CallBootService)
push eax push eax
cli cli
SetRing3DataSegmentSelectors SetRing3DataSegmentSelectors
pop eax pop eax
; Step over User Arguments [1..3] and CallBootService input. ; Step over User Arguments [1..3] and CallBootService input.
add esp, 4*6 add esp, 4*6
; Prepare SYSEXIT arguments. ; Prepare SYSEXIT arguments.
pop edx ; User return address. pop edx ; User return address.
pop ebp pop ebp
pop ecx ; User Stack Pointer. pop ecx ; User Stack Pointer.
sti sti
sysexit sysexit
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; EFI_STATUS ; EFI_STATUS
; EFIAPI ; EFIAPI
; CallRing3 ( ; CallRing3 (
; IN RING3_CALL_DATA *Data ; IN RING3_CALL_DATA *Data
; ); ; );
; ;
; (On User Stack) Data ; (On User Stack) Data
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(CallRing3) global ASM_PFX(CallRing3)
ASM_PFX(CallRing3): ASM_PFX(CallRing3):
cli cli
; Save nonvolatile registers EBX, EBP, EDI, ESI, ESP. ; Save nonvolatile registers EBX, EBP, EDI, ESI, ESP.
push ebx push ebx
push ebp push ebp
push edi push edi
push esi push esi
; Save Core Stack pointer. ; Save Core Stack pointer.
mov [ASM_PFX(CoreEsp)], esp mov [ASM_PFX(CoreEsp)], esp
push dword [ASM_PFX(gRing3EntryPoint)] push dword [ASM_PFX(gRing3EntryPoint)]
push dword [ASM_PFX(gRing3CallStackTop)] push dword [ASM_PFX(gRing3CallStackTop)]
SetRing3DataSegmentSelectors SetRing3DataSegmentSelectors
; Prepare SYSEXIT arguments. ; Prepare SYSEXIT arguments.
pop ecx pop ecx
pop edx pop edx
mov eax, [esp + 4 * 5] ; Data mov eax, [esp + 4 * 5] ; Data
; Switch to User Stack. ; Switch to User Stack.
mov ebp, ecx mov ebp, ecx
; Pass control to user image ; Pass control to user image
sti sti
sysexit sysexit
coreReturnAddress: coreReturnAddress:
mov esp, [ASM_PFX(CoreEsp)] mov esp, [ASM_PFX(CoreEsp)]
pop esi pop esi
pop edi pop edi
pop ebp pop ebp
pop ebx pop ebx
call ASM_PFX(DisableSMAP) call ASM_PFX(DisableSMAP)
mov eax, [edx + 2 * 4] ; User Argument 1 mov eax, [edx + 2 * 4] ; User Argument 1
push eax push eax
call ASM_PFX(EnableSMAP) call ASM_PFX(EnableSMAP)
pop eax pop eax
sti sti
ret ret
SECTION .data SECTION .data
ASM_PFX(CoreEsp): ASM_PFX(CoreEsp):
resd 1 resd 1

View File

@ -1,64 +1,64 @@
/** @file /** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved. Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause SPDX-License-Identifier: BSD-3-Clause
**/ **/
#include "DxeMain.h" #include "DxeMain.h"
#include <Register/Intel/ArchitecturalMsr.h> #include <Register/Intel/ArchitecturalMsr.h>
VOID VOID
EFIAPI EFIAPI
InitializeMsr ( InitializeMsr (
IN OUT EFI_CONFIGURATION_TABLE *Table, IN OUT EFI_CONFIGURATION_TABLE *Table,
IN UINTN NumberOfEntries IN UINTN NumberOfEntries
) )
{ {
UINT64 Msr; UINT64 Msr;
IA32_CR4 Cr4; IA32_CR4 Cr4;
IA32_EFLAGS32 Eflags; IA32_EFLAGS32 Eflags;
UINT32 Ebx; UINT32 Ebx;
UINT32 Edx; UINT32 Edx;
Ebx = 0; Ebx = 0;
Edx = 0; Edx = 0;
// //
// Forbid supervisor-mode accesses to any user-mode pages. // Forbid supervisor-mode accesses to any user-mode pages.
// //
AsmCpuidEx (0x07, 0x0, NULL, &Ebx, NULL, NULL); AsmCpuidEx (0x07, 0x0, NULL, &Ebx, NULL, NULL);
if (((Ebx & BIT20) != 0) && ((Ebx & BIT7) != 0)) { if (((Ebx & BIT20) != 0) && ((Ebx & BIT7) != 0)) {
Cr4.UintN = AsmReadCr4 (); Cr4.UintN = AsmReadCr4 ();
Cr4.Bits.SMAP = 1; Cr4.Bits.SMAP = 1;
Cr4.Bits.SMEP = 1; Cr4.Bits.SMEP = 1;
AsmWriteCr4 (Cr4.UintN); AsmWriteCr4 (Cr4.UintN);
Eflags.UintN = AsmReadEflags (); Eflags.UintN = AsmReadEflags ();
Eflags.Bits.AC = 0; Eflags.Bits.AC = 0;
AsmWriteEflags (Eflags.UintN); AsmWriteEflags (Eflags.UintN);
} }
// //
// SYSENTER and SYSEXIT must be supported. // SYSENTER and SYSEXIT must be supported.
// //
AsmCpuidEx (0x01, 0x0, NULL, NULL, NULL, &Edx); AsmCpuidEx (0x01, 0x0, NULL, NULL, NULL, &Edx);
if ((Edx & BIT11) == 0) { if ((Edx & BIT11) == 0) {
DEBUG ((DEBUG_ERROR, "Core: SYSENTER and SYSEXIT are not supported.\n")); DEBUG ((DEBUG_ERROR, "Core: SYSENTER and SYSEXIT are not supported.\n"));
CpuDeadLoop (); CpuDeadLoop ();
} }
// //
// Initialize MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_EIP and // Initialize MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_EIP and
// MSR_IA32_SYSENTER_ESP for SYSENTER and SYSEXIT. // MSR_IA32_SYSENTER_ESP for SYSENTER and SYSEXIT.
// //
Msr = RING0_CODE32_SEL; Msr = RING0_CODE32_SEL;
AsmWriteMsr64 (MSR_IA32_SYSENTER_CS, Msr); AsmWriteMsr64 (MSR_IA32_SYSENTER_CS, Msr);
Msr = (UINT64)(UINTN)CoreBootServices; Msr = (UINT64)(UINTN)CoreBootServices;
AsmWriteMsr64 (MSR_IA32_SYSENTER_EIP, Msr); AsmWriteMsr64 (MSR_IA32_SYSENTER_EIP, Msr);
Msr = (UINT64)(UINTN)gCoreSysCallStackTop; Msr = (UINT64)(UINTN)gCoreSysCallStackTop;
AsmWriteMsr64 (MSR_IA32_SYSENTER_ESP, Msr); AsmWriteMsr64 (MSR_IA32_SYSENTER_ESP, Msr);
} }

View File

@ -1,151 +1,151 @@
/** @file /** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved. Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause SPDX-License-Identifier: BSD-3-Clause
**/ **/
#include "DxeMain.h" #include "DxeMain.h"
VOID *gCoreSysCallStackTop; VOID *gCoreSysCallStackTop;
VOID *gCoreSysCallStackBase; VOID *gCoreSysCallStackBase;
VOID *gRing3CallStackTop; VOID *gRing3CallStackTop;
VOID *gRing3CallStackBase; VOID *gRing3CallStackBase;
VOID *gRing3EntryPoint; VOID *gRing3EntryPoint;
RING3_DATA *gRing3Data; RING3_DATA *gRing3Data;
VOID *gRing3Interfaces; VOID *gRing3Interfaces;
VOID VOID
EFIAPI EFIAPI
InitializeMsr ( InitializeMsr (
IN OUT EFI_CONFIGURATION_TABLE *Table, IN OUT EFI_CONFIGURATION_TABLE *Table,
IN UINTN NumberOfEntries IN UINTN NumberOfEntries
); );
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
InitializeRing3 ( InitializeRing3 (
IN EFI_HANDLE ImageHandle, IN EFI_HANDLE ImageHandle,
IN LOADED_IMAGE_PRIVATE_DATA *Image IN LOADED_IMAGE_PRIVATE_DATA *Image
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
VOID *TopOfStack; VOID *TopOfStack;
UINTN SizeOfStack; UINTN SizeOfStack;
EFI_PHYSICAL_ADDRESS Physical; EFI_PHYSICAL_ADDRESS Physical;
UINTN Index; UINTN Index;
EFI_CONFIGURATION_TABLE *Conf; EFI_CONFIGURATION_TABLE *Conf;
// //
// Set Ring3 EntryPoint and BootServices. // Set Ring3 EntryPoint and BootServices.
// //
Status = CoreAllocatePages ( Status = CoreAllocatePages (
AllocateAnyPages, AllocateAnyPages,
EfiRing3MemoryType, EfiRing3MemoryType,
EFI_SIZE_TO_PAGES (sizeof (RING3_DATA)), EFI_SIZE_TO_PAGES (sizeof (RING3_DATA)),
&Physical &Physical
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3Data.\n")); DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3Data.\n"));
return Status; return Status;
} }
gRing3Data = (RING3_DATA *)(UINTN)Physical; gRing3Data = (RING3_DATA *)(UINTN)Physical;
CopyMem ((VOID *)gRing3Data, (VOID *)Image->Info.SystemTable, sizeof (EFI_SYSTEM_TABLE)); CopyMem ((VOID *)gRing3Data, (VOID *)Image->Info.SystemTable, sizeof (EFI_SYSTEM_TABLE));
Status = CoreAllocatePages ( Status = CoreAllocatePages (
AllocateAnyPages, AllocateAnyPages,
EfiRing3MemoryType, EfiRing3MemoryType,
EFI_SIZE_TO_PAGES (gRing3Data->SystemTable.NumberOfTableEntries * sizeof (EFI_CONFIGURATION_TABLE)), EFI_SIZE_TO_PAGES (gRing3Data->SystemTable.NumberOfTableEntries * sizeof (EFI_CONFIGURATION_TABLE)),
&Physical &Physical
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3 ConfigurationTable.\n")); DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3 ConfigurationTable.\n"));
return Status; return Status;
} }
Conf = (EFI_CONFIGURATION_TABLE *)(UINTN)Physical; Conf = (EFI_CONFIGURATION_TABLE *)(UINTN)Physical;
for (Index = 0; Index < gRing3Data->SystemTable.NumberOfTableEntries; ++Index) { for (Index = 0; Index < gRing3Data->SystemTable.NumberOfTableEntries; ++Index) {
Conf->VendorGuid = gRing3Data->SystemTable.ConfigurationTable[Index].VendorGuid; Conf->VendorGuid = gRing3Data->SystemTable.ConfigurationTable[Index].VendorGuid;
Conf->VendorTable = gRing3Data->SystemTable.ConfigurationTable[Index].VendorTable; Conf->VendorTable = gRing3Data->SystemTable.ConfigurationTable[Index].VendorTable;
++Conf; ++Conf;
} }
gRing3Data->SystemTable.ConfigurationTable = (EFI_CONFIGURATION_TABLE *)(UINTN)Physical; gRing3Data->SystemTable.ConfigurationTable = (EFI_CONFIGURATION_TABLE *)(UINTN)Physical;
// //
// Initialize DxeRing3 with Supervisor privileges. // Initialize DxeRing3 with Supervisor privileges.
// //
ChangeUefiImageRing (&Image->Info, Image->LoadedImageDevicePath, FALSE); ChangeUefiImageRing (&Image->Info, Image->LoadedImageDevicePath, FALSE);
Status = Image->EntryPoint (ImageHandle, (EFI_SYSTEM_TABLE *)gRing3Data); Status = Image->EntryPoint (ImageHandle, (EFI_SYSTEM_TABLE *)gRing3Data);
ChangeUefiImageRing (&Image->Info, Image->LoadedImageDevicePath, TRUE); ChangeUefiImageRing (&Image->Info, Image->LoadedImageDevicePath, TRUE);
gRing3EntryPoint = gRing3Data->EntryPoint; gRing3EntryPoint = gRing3Data->EntryPoint;
gRing3Data->SystemTable.BootServices = gRing3Data->BootServices; gRing3Data->SystemTable.BootServices = gRing3Data->BootServices;
gRing3Data->SystemTable.RuntimeServices = gRing3Data->RuntimeServices; gRing3Data->SystemTable.RuntimeServices = gRing3Data->RuntimeServices;
Status = CoreAllocatePages ( Status = CoreAllocatePages (
AllocateAnyPages, AllocateAnyPages,
EfiRing3MemoryType, EfiRing3MemoryType,
RING3_INTERFACES_PAGES, RING3_INTERFACES_PAGES,
&Physical &Physical
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3Interfaces.\n")); DEBUG ((DEBUG_ERROR, "Core: Failed to allocate memory for Ring3Interfaces.\n"));
CoreFreePages ( CoreFreePages (
(EFI_PHYSICAL_ADDRESS)(UINTN)gRing3Data, (EFI_PHYSICAL_ADDRESS)(UINTN)gRing3Data,
EFI_SIZE_TO_PAGES (sizeof (RING3_DATA)) EFI_SIZE_TO_PAGES (sizeof (RING3_DATA))
); );
return Status; return Status;
} }
gRing3Interfaces = (VOID *)(UINTN)Physical; gRing3Interfaces = (VOID *)(UINTN)Physical;
SizeOfStack = EFI_SIZE_TO_PAGES (USER_STACK_SIZE) * EFI_PAGE_SIZE; SizeOfStack = EFI_SIZE_TO_PAGES (USER_STACK_SIZE) * EFI_PAGE_SIZE;
// //
// Allocate 128KB for the Core SysCall Stack. // Allocate 128KB for the Core SysCall Stack.
// //
gCoreSysCallStackBase = AllocatePages (EFI_SIZE_TO_PAGES (USER_STACK_SIZE)); gCoreSysCallStackBase = AllocatePages (EFI_SIZE_TO_PAGES (USER_STACK_SIZE));
ASSERT (gCoreSysCallStackBase != NULL); ASSERT (gCoreSysCallStackBase != NULL);
// //
// Compute the top of the allocated stack. Pre-allocate a UINTN for safety. // Compute the top of the allocated stack. Pre-allocate a UINTN for safety.
// //
TopOfStack = (VOID *)((UINTN)gCoreSysCallStackBase + SizeOfStack - CPU_STACK_ALIGNMENT); TopOfStack = (VOID *)((UINTN)gCoreSysCallStackBase + SizeOfStack - CPU_STACK_ALIGNMENT);
TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT); TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);
gCoreSysCallStackTop = TopOfStack; gCoreSysCallStackTop = TopOfStack;
SetUefiImageMemoryAttributes ((UINTN)gCoreSysCallStackBase, SizeOfStack, EFI_MEMORY_XP); SetUefiImageMemoryAttributes ((UINTN)gCoreSysCallStackBase, SizeOfStack, EFI_MEMORY_XP);
DEBUG ((DEBUG_ERROR, "Core: gCoreSysCallStackTop = %p\n", gCoreSysCallStackTop)); DEBUG ((DEBUG_ERROR, "Core: gCoreSysCallStackTop = %p\n", gCoreSysCallStackTop));
// //
// Allocate 128KB for the User Stack. // Allocate 128KB for the User Stack.
// //
gRing3CallStackBase = AllocatePages (EFI_SIZE_TO_PAGES (USER_STACK_SIZE)); gRing3CallStackBase = AllocatePages (EFI_SIZE_TO_PAGES (USER_STACK_SIZE));
ASSERT (gRing3CallStackBase != NULL); ASSERT (gRing3CallStackBase != NULL);
// //
// Compute the top of the allocated stack. Pre-allocate a UINTN for safety. // Compute the top of the allocated stack. Pre-allocate a UINTN for safety.
// //
TopOfStack = (VOID *)((UINTN)gRing3CallStackBase + SizeOfStack - CPU_STACK_ALIGNMENT); TopOfStack = (VOID *)((UINTN)gRing3CallStackBase + SizeOfStack - CPU_STACK_ALIGNMENT);
TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT); TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);
gRing3CallStackTop = TopOfStack; gRing3CallStackTop = TopOfStack;
SetUefiImageMemoryAttributes ((UINTN)gRing3CallStackBase, SizeOfStack, EFI_MEMORY_XP | EFI_MEMORY_USER); SetUefiImageMemoryAttributes ((UINTN)gRing3CallStackBase, SizeOfStack, EFI_MEMORY_XP | EFI_MEMORY_USER);
DEBUG ((DEBUG_ERROR, "Core: gRing3CallStackTop = %p\n", gRing3CallStackTop)); DEBUG ((DEBUG_ERROR, "Core: gRing3CallStackTop = %p\n", gRing3CallStackTop));
InitializeMsr ( InitializeMsr (
gRing3Data->SystemTable.ConfigurationTable, gRing3Data->SystemTable.ConfigurationTable,
gRing3Data->SystemTable.NumberOfTableEntries gRing3Data->SystemTable.NumberOfTableEntries
); );
return Status; return Status;
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,48 +1,48 @@
/** @file /** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved. Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause SPDX-License-Identifier: BSD-3-Clause
**/ **/
#include <Protocol/BlockIo.h> #include <Protocol/BlockIo.h>
#include <Protocol/ComponentName.h> #include <Protocol/ComponentName.h>
#include <Protocol/DevicePathUtilities.h> #include <Protocol/DevicePathUtilities.h>
#include <Protocol/DiskIo.h> #include <Protocol/DiskIo.h>
#include <Protocol/UnicodeCollation.h> #include <Protocol/UnicodeCollation.h>
extern EFI_DRIVER_BINDING_PROTOCOL mRing3DriverBindingProtocol; extern EFI_DRIVER_BINDING_PROTOCOL mRing3DriverBindingProtocol;
extern EFI_SIMPLE_FILE_SYSTEM_PROTOCOL mRing3SimpleFileSystemProtocol; extern EFI_SIMPLE_FILE_SYSTEM_PROTOCOL mRing3SimpleFileSystemProtocol;
extern EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *mRing3SimpleFileSystemPointer; extern EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *mRing3SimpleFileSystemPointer;
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
CoreDriverBindingSupported ( CoreDriverBindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle, IN EFI_HANDLE ControllerHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
); );
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
CoreDriverBindingStart ( CoreDriverBindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle, IN EFI_HANDLE ControllerHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
); );
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
CoreDriverBindingStop ( CoreDriverBindingStop (
IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle, IN EFI_HANDLE ControllerHandle,
IN UINTN NumberOfChildren, IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer OPTIONAL IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
); );
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
CoreOpenVolume ( CoreOpenVolume (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This, IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
OUT EFI_FILE_PROTOCOL **Root OUT EFI_FILE_PROTOCOL **Root
); );

View File

@ -1,246 +1,246 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; ;
; Copyright (c) 2024, Mikhail Krichanov. All rights reserved. ; Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
; SPDX-License-Identifier: BSD-3-Clause ; SPDX-License-Identifier: BSD-3-Clause
; ;
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
#include <Register/Intel/ArchitecturalMsr.h> #include <Register/Intel/ArchitecturalMsr.h>
extern ASM_PFX(CallBootService) extern ASM_PFX(CallBootService)
extern ASM_PFX(gCoreSysCallStackTop) extern ASM_PFX(gCoreSysCallStackTop)
extern ASM_PFX(gRing3CallStackTop) extern ASM_PFX(gRing3CallStackTop)
extern ASM_PFX(gRing3EntryPoint) extern ASM_PFX(gRing3EntryPoint)
DEFAULT REL DEFAULT REL
SECTION .text SECTION .text
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; VOID ; VOID
; EFIAPI ; EFIAPI
; DisableSMAP ( ; DisableSMAP (
; VOID ; VOID
; ); ; );
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(DisableSMAP) global ASM_PFX(DisableSMAP)
ASM_PFX(DisableSMAP): ASM_PFX(DisableSMAP):
pushfq pushfq
pop r10 pop r10
or r10, 0x40000 ; Set AC (bit 18) or r10, 0x40000 ; Set AC (bit 18)
push r10 push r10
popfq popfq
ret ret
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; VOID ; VOID
; EFIAPI ; EFIAPI
; EnableSMAP ( ; EnableSMAP (
; VOID ; VOID
; ); ; );
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(EnableSMAP) global ASM_PFX(EnableSMAP)
ASM_PFX(EnableSMAP): ASM_PFX(EnableSMAP):
pushfq pushfq
pop r10 pop r10
and r10, ~0x40000 ; Clear AC (bit 18) and r10, ~0x40000 ; Clear AC (bit 18)
push r10 push r10
popfq popfq
ret ret
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; EFI_STATUS ; EFI_STATUS
; EFIAPI ; EFIAPI
; CallInstallMultipleProtocolInterfaces ( ; CallInstallMultipleProtocolInterfaces (
; IN EFI_HANDLE *Handle, ; IN EFI_HANDLE *Handle,
; IN VOID **ArgList, ; IN VOID **ArgList,
; IN UINT32 ArgListSize, ; IN UINT32 ArgListSize,
; IN VOID *Function ; IN VOID *Function
; ); ; );
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(CallInstallMultipleProtocolInterfaces) global ASM_PFX(CallInstallMultipleProtocolInterfaces)
ASM_PFX(CallInstallMultipleProtocolInterfaces): ASM_PFX(CallInstallMultipleProtocolInterfaces):
push r12 push r12
; Save function input. ; Save function input.
mov rax, rdx mov rax, rdx
mov r10, r8 mov r10, r8
mov r11, r9 mov r11, r9
; Prepare registers for call. ; Prepare registers for call.
mov rdx, [rax] mov rdx, [rax]
mov r8, [rax + 8] mov r8, [rax + 8]
mov r9, [rax + 8*2] mov r9, [rax + 8*2]
; Prepare stack for call. ; Prepare stack for call.
lea rax, [rax + r10 * 8] lea rax, [rax + r10 * 8]
mov r12, r10 mov r12, r10
copy: copy:
sub rax, 8 sub rax, 8
push qword [rax] push qword [rax]
sub r10, 1 sub r10, 1
jnz copy jnz copy
push rcx push rcx
call r11 call r11
; Step over Function arguments. ; Step over Function arguments.
pop rcx pop rcx
lea rsp, [rsp + r12 * 8] lea rsp, [rsp + r12 * 8]
pop r12 pop r12
ret ret
%macro SetRing3DataSegmentSelectors 0 %macro SetRing3DataSegmentSelectors 0
mov rcx, MSR_IA32_STAR mov rcx, MSR_IA32_STAR
rdmsr rdmsr
shl rdx, 0x20 shl rdx, 0x20
or rax, rdx or rax, rdx
; rax = ((RING3_CODE64_SEL - 16) << 16 | RING0_CODE64_SEL) << 32 ; rax = ((RING3_CODE64_SEL - 16) << 16 | RING0_CODE64_SEL) << 32
shr rax, 48 shr rax, 48
add rax, 8 add rax, 8
mov ds, ax mov ds, ax
mov es, ax mov es, ax
mov fs, ax mov fs, ax
mov gs, ax mov gs, ax
%endmacro %endmacro
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; EFI_STATUS ; EFI_STATUS
; EFIAPI ; EFIAPI
; CoreBootServices ( ; CoreBootServices (
; IN UINT8 Type, ; IN UINT8 Type,
; ... ; ...
; ); ; );
; ;
; (rcx) RIP of the next instruction saved by SYSCALL in SysCall(). ; (rcx) RIP of the next instruction saved by SYSCALL in SysCall().
; (rdx) Argument 1 of the called function. ; (rdx) Argument 1 of the called function.
; (r8) Argument 2 of the called function. ; (r8) Argument 2 of the called function.
; (r9) Argument 3 of the called function. ; (r9) Argument 3 of the called function.
; (r10) Type. ; (r10) Type.
; (r11) RFLAGS saved by SYSCALL in SysCall(). ; (r11) RFLAGS saved by SYSCALL in SysCall().
; ;
; (On User Stack) Argument 4, 5, ... ; (On User Stack) Argument 4, 5, ...
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(CoreBootServices) global ASM_PFX(CoreBootServices)
ASM_PFX(CoreBootServices): ASM_PFX(CoreBootServices):
; Switch from User to Core data segment selectors. ; Switch from User to Core data segment selectors.
mov ax, ss mov ax, ss
mov ds, ax mov ds, ax
mov es, ax mov es, ax
mov fs, ax mov fs, ax
mov gs, ax mov gs, ax
; Special case for SysCallReturnToCore. ; Special case for SysCallReturnToCore.
cmp r10, 0 cmp r10, 0
je coreReturnAddress je coreReturnAddress
; Save User Stack pointers and switch to Core SysCall Stack. ; Save User Stack pointers and switch to Core SysCall Stack.
mov rax, [ASM_PFX(gCoreSysCallStackTop)] mov rax, [ASM_PFX(gCoreSysCallStackTop)]
sub rax, 8 sub rax, 8
mov [rax], rsp mov [rax], rsp
mov rsp, rax mov rsp, rax
push rbp push rbp
; Save return address for SYSRET. ; Save return address for SYSRET.
push rcx push rcx
; Save User RFLAGS for SYSRET. ; Save User RFLAGS for SYSRET.
push r11 push r11
; Save User Arguments [1..3]. ; Save User Arguments [1..3].
push r9 push r9
push r8 push r8
push rdx push rdx
mov rbp, rsp mov rbp, rsp
; Prepare CallBootService arguments. ; Prepare CallBootService arguments.
mov rcx, r10 mov rcx, r10
mov rdx, rbp mov rdx, rbp
mov r8, [rbp + 8*6] mov r8, [rbp + 8*6]
sti sti
call ASM_PFX(CallBootService) call ASM_PFX(CallBootService)
push rax push rax
cli cli
SetRing3DataSegmentSelectors SetRing3DataSegmentSelectors
pop rax pop rax
; Step over Arguments [1..3]. ; Step over Arguments [1..3].
add rsp, 8*3 add rsp, 8*3
; Prepare SYSRET arguments. ; Prepare SYSRET arguments.
pop r11 pop r11
pop rcx pop rcx
; Switch to User Stack. ; Switch to User Stack.
pop rbp pop rbp
pop rsp pop rsp
; SYSCALL saves RFLAGS into R11 and the RIP of the next instruction into RCX. ; SYSCALL saves RFLAGS into R11 and the RIP of the next instruction into RCX.
o64 sysret o64 sysret
; SYSRET copies the value in RCX into RIP and loads RFLAGS from R11. ; SYSRET copies the value in RCX into RIP and loads RFLAGS from R11.
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; EFI_STATUS ; EFI_STATUS
; EFIAPI ; EFIAPI
; CallRing3 ( ; CallRing3 (
; IN RING3_CALL_DATA *Data ; IN RING3_CALL_DATA *Data
; ); ; );
; ;
; (rcx) Data ; (rcx) Data
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(CallRing3) global ASM_PFX(CallRing3)
ASM_PFX(CallRing3): ASM_PFX(CallRing3):
pushfq pushfq
pop r11 pop r11
cli cli
; Save nonvolatile registers RBX, RBP, RDI, RSI, RSP, R12, R13, R14, and R15. ; Save nonvolatile registers RBX, RBP, RDI, RSI, RSP, R12, R13, R14, and R15.
push rbx push rbx
push rbp push rbp
push rdi push rdi
push rsi push rsi
push r12 push r12
push r13 push r13
push r14 push r14
push r15 push r15
; Save Core Stack pointer. ; Save Core Stack pointer.
mov [ASM_PFX(CoreRsp)], rsp mov [ASM_PFX(CoreRsp)], rsp
; Save input Arguments. ; Save input Arguments.
mov r8, [ASM_PFX(gRing3CallStackTop)] mov r8, [ASM_PFX(gRing3CallStackTop)]
mov r9, [ASM_PFX(gRing3EntryPoint)] mov r9, [ASM_PFX(gRing3EntryPoint)]
mov r10, rcx mov r10, rcx
SetRing3DataSegmentSelectors SetRing3DataSegmentSelectors
; Prepare SYSRET arguments. ; Prepare SYSRET arguments.
mov rdx, r10 mov rdx, r10
mov rcx, r9 mov rcx, r9
; Switch to User Stack. ; Switch to User Stack.
mov rsp, r8 mov rsp, r8
mov rbp, rsp mov rbp, rsp
; Pass control to user image ; Pass control to user image
o64 sysret o64 sysret
coreReturnAddress: coreReturnAddress:
mov rsp, [ASM_PFX(CoreRsp)] mov rsp, [ASM_PFX(CoreRsp)]
pop r15 pop r15
pop r14 pop r14
pop r13 pop r13
pop r12 pop r12
pop rsi pop rsi
pop rdi pop rdi
pop rbp pop rbp
pop rbx pop rbx
mov rax, rdx mov rax, rdx
sti sti
ret ret
SECTION .data SECTION .data
ASM_PFX(CoreRsp): ASM_PFX(CoreRsp):
resq 1 resq 1

View File

@ -1,70 +1,70 @@
/** @file /** @file
Copyright (c) 2024, Mikhail Krichanov. All rights reserved. Copyright (c) 2024, Mikhail Krichanov. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause SPDX-License-Identifier: BSD-3-Clause
**/ **/
#include "DxeMain.h" #include "DxeMain.h"
#include <Register/Intel/ArchitecturalMsr.h> #include <Register/Intel/ArchitecturalMsr.h>
VOID VOID
EFIAPI EFIAPI
InitializeMsr ( InitializeMsr (
IN OUT EFI_CONFIGURATION_TABLE *Table, IN OUT EFI_CONFIGURATION_TABLE *Table,
IN UINTN NumberOfEntries IN UINTN NumberOfEntries
) )
{ {
UINT64 Msr; UINT64 Msr;
IA32_CR4 Cr4; IA32_CR4 Cr4;
IA32_EFLAGS32 Eflags; IA32_EFLAGS32 Eflags;
UINT32 Ebx; UINT32 Ebx;
UINT32 Edx; UINT32 Edx;
MSR_IA32_EFER_REGISTER MsrEfer; MSR_IA32_EFER_REGISTER MsrEfer;
Ebx = 0; Ebx = 0;
Edx = 0; Edx = 0;
// //
// Forbid supervisor-mode accesses to any user-mode pages. // Forbid supervisor-mode accesses to any user-mode pages.
// //
AsmCpuidEx (0x07, 0x0, NULL, &Ebx, NULL, NULL); AsmCpuidEx (0x07, 0x0, NULL, &Ebx, NULL, NULL);
if (((Ebx & BIT20) != 0) && ((Ebx & BIT7) != 0)) { if (((Ebx & BIT20) != 0) && ((Ebx & BIT7) != 0)) {
Cr4.UintN = AsmReadCr4 (); Cr4.UintN = AsmReadCr4 ();
Cr4.Bits.SMAP = 1; Cr4.Bits.SMAP = 1;
Cr4.Bits.SMEP = 1; Cr4.Bits.SMEP = 1;
AsmWriteCr4 (Cr4.UintN); AsmWriteCr4 (Cr4.UintN);
Eflags.UintN = AsmReadEflags (); Eflags.UintN = AsmReadEflags ();
Eflags.Bits.AC = 0; Eflags.Bits.AC = 0;
AsmWriteEflags (Eflags.UintN); AsmWriteEflags (Eflags.UintN);
} }
// //
// Enable SYSCALL and SYSRET. // Enable SYSCALL and SYSRET.
// //
AsmCpuidEx (0x80000001, 0x0, NULL, NULL, NULL, &Edx); AsmCpuidEx (0x80000001, 0x0, NULL, NULL, NULL, &Edx);
if ((Edx & BIT11) != 0) { if ((Edx & BIT11) != 0) {
MsrEfer.Uint64 = AsmReadMsr64 (MSR_IA32_EFER); MsrEfer.Uint64 = AsmReadMsr64 (MSR_IA32_EFER);
MsrEfer.Bits.SCE = 1; MsrEfer.Bits.SCE = 1;
AsmWriteMsr64 (MSR_IA32_EFER, MsrEfer.Uint64); AsmWriteMsr64 (MSR_IA32_EFER, MsrEfer.Uint64);
} else { } else {
DEBUG ((DEBUG_ERROR, "Core: SYSCALL and SYSRET are not supported.\n")); DEBUG ((DEBUG_ERROR, "Core: SYSCALL and SYSRET are not supported.\n"));
CpuDeadLoop (); CpuDeadLoop ();
} }
// //
// Initialize MSR_IA32_STAR, MSR_IA32_LSTAR and MSR_IA32_FMASK for SYSCALL and SYSRET. // Initialize MSR_IA32_STAR, MSR_IA32_LSTAR and MSR_IA32_FMASK for SYSCALL and SYSRET.
// //
Msr = (((((UINT64)RING3_CODE64_SEL - 16) | 3) << 16) | (UINT64)RING0_CODE64_SEL) << 32; Msr = (((((UINT64)RING3_CODE64_SEL - 16) | 3) << 16) | (UINT64)RING0_CODE64_SEL) << 32;
AsmWriteMsr64 (MSR_IA32_STAR, Msr); AsmWriteMsr64 (MSR_IA32_STAR, Msr);
Msr = (UINT64)(UINTN)CoreBootServices; Msr = (UINT64)(UINTN)CoreBootServices;
AsmWriteMsr64 (MSR_IA32_LSTAR, Msr); AsmWriteMsr64 (MSR_IA32_LSTAR, Msr);
// //
// Disable maskable interrupts at SYSCALL. // Disable maskable interrupts at SYSCALL.
// //
Msr = (UINT64)BIT9; Msr = (UINT64)BIT9;
AsmWriteMsr64 (MSR_IA32_FMASK, Msr); AsmWriteMsr64 (MSR_IA32_FMASK, Msr);
} }