]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/EbcDxe/AArch64/EbcSupport.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / AArch64 / EbcSupport.c
index c5cc76d7bdcb8bdad9203c4c6a800f4040cc12dd..6622eace985faac258bc87186c4532684560c3d3 100644 (file)
@@ -6,23 +6,18 @@ Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>
 Copyright (c) 2015, The Linux Foundation. All rights reserved.<BR>\r
 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
 \r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "EbcInt.h"\r
 #include "EbcExecute.h"\r
+#include "EbcDebuggerHook.h"\r
 \r
 //\r
 // Amount of space that is not used in the stack\r
 //\r
-#define STACK_REMAIN_SIZE (1024 * 4)\r
+#define STACK_REMAIN_SIZE  (1024 * 4)\r
 \r
 #pragma pack(1)\r
 typedef struct {\r
@@ -33,7 +28,7 @@ typedef struct {
 } EBC_INSTRUCTION_BUFFER;\r
 #pragma pack()\r
 \r
-extern CONST EBC_INSTRUCTION_BUFFER       mEbcInstructionBufferTemplate;\r
+extern CONST EBC_INSTRUCTION_BUFFER  mEbcInstructionBufferTemplate;\r
 \r
 /**\r
   Begin executing an EBC image.\r
@@ -70,20 +65,19 @@ EbcLLExecuteEbcImageEntryPoint (
 **/\r
 VOID\r
 PushU64 (\r
-  IN VM_CONTEXT *VmPtr,\r
-  IN UINT64     Arg\r
+  IN VM_CONTEXT  *VmPtr,\r
+  IN UINT64      Arg\r
   )\r
 {\r
   //\r
   // Advance the VM stack down, and then copy the argument to the stack.\r
   // Hope it's aligned.\r
   //\r
-  VmPtr->Gpr[0] -= sizeof (UINT64);\r
-  *(UINT64 *) VmPtr->Gpr[0] = Arg;\r
+  VmPtr->Gpr[0]           -= sizeof (UINT64);\r
+  *(UINT64 *)VmPtr->Gpr[0] = Arg;\r
   return;\r
 }\r
 \r
-\r
 /**\r
   Begin executing an EBC image.\r
 \r
@@ -134,12 +128,12 @@ EbcInterpret (
   //\r
   // Now clear out our context\r
   //\r
-  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+  ZeroMem ((VOID *)&VmContext, sizeof (VM_CONTEXT));\r
 \r
   //\r
   // Set the VM instruction pointer to the correct location in memory.\r
   //\r
-  VmContext.Ip = (VMIP) Addr;\r
+  VmContext.Ip = (VMIP)Addr;\r
 \r
   //\r
   // Initialize the stack pointer for the EBC. Get the current system stack\r
@@ -150,14 +144,15 @@ EbcInterpret (
   // Adjust the VM's stack pointer down.\r
   //\r
 \r
-  Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
-  if (EFI_ERROR(Status)) {\r
+  Status = GetEBCStack ((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
+  if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
-  VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
-  VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];\r
-  VmContext.Gpr[0] -= sizeof (UINTN);\r
+\r
+  VmContext.StackTop        = (UINT8 *)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+  VmContext.Gpr[0]          = (UINT64)((UINT8 *)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN)VmContext.Gpr[0];\r
+  VmContext.Gpr[0]         -= sizeof (UINTN);\r
 \r
   //\r
   // Align the stack on a natural boundary.\r
@@ -167,46 +162,46 @@ EbcInterpret (
   //\r
   // Put a magic value in the stack gap, then adjust down again.\r
   //\r
-  *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
-  VmContext.StackMagicPtr             = (UINTN *) (UINTN) VmContext.Gpr[0];\r
+  *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)VM_STACK_KEY_VALUE;\r
+  VmContext.StackMagicPtr             = (UINTN *)(UINTN)VmContext.Gpr[0];\r
 \r
   //\r
   // The stack upper to LowStackTop is belong to the VM.\r
   //\r
-  VmContext.LowStackTop   = (UINTN) VmContext.Gpr[0];\r
+  VmContext.LowStackTop = (UINTN)VmContext.Gpr[0];\r
 \r
   //\r
   // For the worst case, assume there are 4 arguments passed in registers, store\r
   // them to VM's stack.\r
   //\r
-  PushU64 (&VmContext, (UINT64) Args9_16[7]);\r
-  PushU64 (&VmContext, (UINT64) Args9_16[6]);\r
-  PushU64 (&VmContext, (UINT64) Args9_16[5]);\r
-  PushU64 (&VmContext, (UINT64) Args9_16[4]);\r
-  PushU64 (&VmContext, (UINT64) Args9_16[3]);\r
-  PushU64 (&VmContext, (UINT64) Args9_16[2]);\r
-  PushU64 (&VmContext, (UINT64) Args9_16[1]);\r
-  PushU64 (&VmContext, (UINT64) Args9_16[0]);\r
-  PushU64 (&VmContext, (UINT64) Arg8);\r
-  PushU64 (&VmContext, (UINT64) Arg7);\r
-  PushU64 (&VmContext, (UINT64) Arg6);\r
-  PushU64 (&VmContext, (UINT64) Arg5);\r
-  PushU64 (&VmContext, (UINT64) Arg4);\r
-  PushU64 (&VmContext, (UINT64) Arg3);\r
-  PushU64 (&VmContext, (UINT64) Arg2);\r
-  PushU64 (&VmContext, (UINT64) Arg1);\r
+  PushU64 (&VmContext, (UINT64)Args9_16[7]);\r
+  PushU64 (&VmContext, (UINT64)Args9_16[6]);\r
+  PushU64 (&VmContext, (UINT64)Args9_16[5]);\r
+  PushU64 (&VmContext, (UINT64)Args9_16[4]);\r
+  PushU64 (&VmContext, (UINT64)Args9_16[3]);\r
+  PushU64 (&VmContext, (UINT64)Args9_16[2]);\r
+  PushU64 (&VmContext, (UINT64)Args9_16[1]);\r
+  PushU64 (&VmContext, (UINT64)Args9_16[0]);\r
+  PushU64 (&VmContext, (UINT64)Arg8);\r
+  PushU64 (&VmContext, (UINT64)Arg7);\r
+  PushU64 (&VmContext, (UINT64)Arg6);\r
+  PushU64 (&VmContext, (UINT64)Arg5);\r
+  PushU64 (&VmContext, (UINT64)Arg4);\r
+  PushU64 (&VmContext, (UINT64)Arg3);\r
+  PushU64 (&VmContext, (UINT64)Arg2);\r
+  PushU64 (&VmContext, (UINT64)Arg1);\r
 \r
   //\r
   // Interpreter assumes 64-bit return address is pushed on the stack.\r
   // AArch64 does not do this so pad the stack accordingly.\r
   //\r
-  PushU64 (&VmContext, (UINT64) 0);\r
-  PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
+  PushU64 (&VmContext, (UINT64)0);\r
+  PushU64 (&VmContext, (UINT64)0x1234567887654321ULL);\r
 \r
   //\r
   // For AArch64, this is where we say our return address is\r
   //\r
-  VmContext.StackRetAddr  = (UINT64) VmContext.Gpr[0];\r
+  VmContext.StackRetAddr = (UINT64)VmContext.Gpr[0];\r
 \r
   //\r
   // We need to keep track of where the EBC stack starts. This way, if the EBC\r
@@ -225,16 +220,16 @@ EbcInterpret (
   //\r
   // Begin executing the EBC code\r
   //\r
+  EbcDebuggerHookEbcInterpret (&VmContext);\r
   EbcExecute (&VmContext);\r
 \r
   //\r
   // Return the value in R[7] unless there was an error\r
   //\r
-  ReturnEBCStack(StackIndex);\r
-  return (UINT64) VmContext.Gpr[7];\r
+  ReturnEBCStack (StackIndex);\r
+  return (UINT64)VmContext.Gpr[7];\r
 }\r
 \r
-\r
 /**\r
   Begin executing an EBC image.\r
 \r
@@ -249,9 +244,9 @@ EbcInterpret (
 UINT64\r
 EFIAPI\r
 ExecuteEbcImageEntryPoint (\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE     *SystemTable,\r
-  IN UINTN                EntryPoint\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable,\r
+  IN UINTN             EntryPoint\r
   )\r
 {\r
   //\r
@@ -270,7 +265,7 @@ ExecuteEbcImageEntryPoint (
   //\r
   // Now clear out our context\r
   //\r
-  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+  ZeroMem ((VOID *)&VmContext, sizeof (VM_CONTEXT));\r
 \r
   //\r
   // Save the image handle so we can track the thunks created for this image\r
@@ -281,52 +276,52 @@ ExecuteEbcImageEntryPoint (
   //\r
   // Set the VM instruction pointer to the correct location in memory.\r
   //\r
-  VmContext.Ip = (VMIP) Addr;\r
+  VmContext.Ip = (VMIP)Addr;\r
 \r
   //\r
   // Initialize the stack pointer for the EBC. Get the current system stack\r
   // pointer and adjust it down by the max needed for the interpreter.\r
   //\r
 \r
-  Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
-  if (EFI_ERROR(Status)) {\r
+  Status = GetEBCStack (ImageHandle, &VmContext.StackPool, &StackIndex);\r
+  if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
-  VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
-  VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];\r
-  VmContext.Gpr[0] -= sizeof (UINTN);\r
 \r
+  VmContext.StackTop        = (UINT8 *)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+  VmContext.Gpr[0]          = (UINT64)((UINT8 *)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN)VmContext.Gpr[0];\r
+  VmContext.Gpr[0]         -= sizeof (UINTN);\r
 \r
   //\r
   // Put a magic value in the stack gap, then adjust down again\r
   //\r
-  *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
-  VmContext.StackMagicPtr             = (UINTN *) (UINTN) VmContext.Gpr[0];\r
+  *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)VM_STACK_KEY_VALUE;\r
+  VmContext.StackMagicPtr             = (UINTN *)(UINTN)VmContext.Gpr[0];\r
 \r
   //\r
   // Align the stack on a natural boundary\r
-  VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof(UINTN) - 1);\r
+  VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof (UINTN) - 1);\r
   //\r
-  VmContext.LowStackTop   = (UINTN) VmContext.Gpr[0];\r
+  VmContext.LowStackTop = (UINTN)VmContext.Gpr[0];\r
 \r
   //\r
   // Simply copy the image handle and system table onto the EBC stack.\r
   // Greatly simplifies things by not having to spill the args.\r
   //\r
-  PushU64 (&VmContext, (UINT64) SystemTable);\r
-  PushU64 (&VmContext, (UINT64) ImageHandle);\r
+  PushU64 (&VmContext, (UINT64)SystemTable);\r
+  PushU64 (&VmContext, (UINT64)ImageHandle);\r
 \r
   //\r
   // VM pushes 16-bytes for return address. Simulate that here.\r
   //\r
-  PushU64 (&VmContext, (UINT64) 0);\r
-  PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
+  PushU64 (&VmContext, (UINT64)0);\r
+  PushU64 (&VmContext, (UINT64)0x1234567887654321ULL);\r
 \r
   //\r
   // For AArch64, this is where we say our return address is\r
   //\r
-  VmContext.StackRetAddr  = (UINT64) VmContext.Gpr[0];\r
+  VmContext.StackRetAddr = (UINT64)VmContext.Gpr[0];\r
 \r
   //\r
   // Entry function needn't access high stack context, simply\r
@@ -336,16 +331,16 @@ ExecuteEbcImageEntryPoint (
   //\r
   // Begin executing the EBC code\r
   //\r
+  EbcDebuggerHookExecuteEbcImageEntryPoint (&VmContext);\r
   EbcExecute (&VmContext);\r
 \r
   //\r
   // Return the value in R[7] unless there was an error\r
   //\r
-  ReturnEBCStack(StackIndex);\r
-  return (UINT64) VmContext.Gpr[7];\r
+  ReturnEBCStack (StackIndex);\r
+  return (UINT64)VmContext.Gpr[7];\r
 }\r
 \r
-\r
 /**\r
   Create thunks for an EBC image entry point, or an EBC protocol service.\r
 \r
@@ -365,22 +360,22 @@ ExecuteEbcImageEntryPoint (
 **/\r
 EFI_STATUS\r
 EbcCreateThunks (\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN VOID                 *EbcEntryPoint,\r
-  OUT VOID                **Thunk,\r
-  IN  UINT32              Flags\r
+  IN EFI_HANDLE  ImageHandle,\r
+  IN VOID        *EbcEntryPoint,\r
+  OUT VOID       **Thunk,\r
+  IN  UINT32     Flags\r
   )\r
 {\r
-  EBC_INSTRUCTION_BUFFER       *InstructionBuffer;\r
+  EBC_INSTRUCTION_BUFFER  *InstructionBuffer;\r
 \r
   //\r
   // Check alignment of pointer to EBC code\r
   //\r
-  if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
+  if ((UINT32)(UINTN)EbcEntryPoint & 0x01) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  InstructionBuffer = AllocatePool (sizeof (EBC_INSTRUCTION_BUFFER));\r
+  InstructionBuffer = EbcAllocatePoolForThunk (sizeof (EBC_INSTRUCTION_BUFFER));\r
   if (InstructionBuffer == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -393,8 +388,11 @@ EbcCreateThunks (
   //\r
   // Copy whole thunk instruction buffer template\r
   //\r
-  CopyMem (InstructionBuffer, &mEbcInstructionBufferTemplate,\r
-    sizeof (EBC_INSTRUCTION_BUFFER));\r
+  CopyMem (\r
+    InstructionBuffer,\r
+    &mEbcInstructionBufferTemplate,\r
+    sizeof (EBC_INSTRUCTION_BUFFER)\r
+    );\r
 \r
   //\r
   // Patch EbcEntryPoint and EbcLLEbcInterpret\r
@@ -410,13 +408,15 @@ EbcCreateThunks (
   // Add the thunk to the list for this image. Do this last since the add\r
   // function flushes the cache for us.\r
   //\r
-  EbcAddImageThunk (ImageHandle, InstructionBuffer,\r
-    sizeof (EBC_INSTRUCTION_BUFFER));\r
+  EbcAddImageThunk (\r
+    ImageHandle,\r
+    InstructionBuffer,\r
+    sizeof (EBC_INSTRUCTION_BUFFER)\r
+    );\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   This function is called to execute an EBC CALLEX instruction.\r
   The function check the callee's content to see whether it is common native\r
@@ -434,34 +434,38 @@ EbcCreateThunks (
 **/\r
 VOID\r
 EbcLLCALLEX (\r
-  IN VM_CONTEXT   *VmPtr,\r
-  IN UINTN        FuncAddr,\r
-  IN UINTN        NewStackPointer,\r
-  IN VOID         *FramePtr,\r
-  IN UINT8        Size\r
+  IN VM_CONTEXT  *VmPtr,\r
+  IN UINTN       FuncAddr,\r
+  IN UINTN       NewStackPointer,\r
+  IN VOID        *FramePtr,\r
+  IN UINT8       Size\r
   )\r
 {\r
-  CONST EBC_INSTRUCTION_BUFFER *InstructionBuffer;\r
+  CONST EBC_INSTRUCTION_BUFFER  *InstructionBuffer;\r
 \r
   //\r
   // Processor specific code to check whether the callee is a thunk to EBC.\r
   //\r
   InstructionBuffer = (EBC_INSTRUCTION_BUFFER *)FuncAddr;\r
 \r
-  if (CompareMem (InstructionBuffer, &mEbcInstructionBufferTemplate,\r
-        sizeof(EBC_INSTRUCTION_BUFFER) - 2 * sizeof (UINT64)) == 0) {\r
+  if (CompareMem (\r
+        InstructionBuffer,\r
+        &mEbcInstructionBufferTemplate,\r
+        sizeof (EBC_INSTRUCTION_BUFFER) - 2 * sizeof (UINT64)\r
+        ) == 0)\r
+  {\r
     //\r
     // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
     // put our return address and frame pointer on the VM stack.\r
     // Then set the VM's IP to new EBC code.\r
     //\r
     VmPtr->Gpr[0] -= 8;\r
-    VmWriteMemN (VmPtr, (UINTN) VmPtr->Gpr[0], (UINTN) FramePtr);\r
-    VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->Gpr[0];\r
-    VmPtr->Gpr[0] -= 8;\r
-    VmWriteMem64 (VmPtr, (UINTN) VmPtr->Gpr[0], (UINT64) (UINTN) (VmPtr->Ip + Size));\r
+    VmWriteMemN (VmPtr, (UINTN)VmPtr->Gpr[0], (UINTN)FramePtr);\r
+    VmPtr->FramePtr = (VOID *)(UINTN)VmPtr->Gpr[0];\r
+    VmPtr->Gpr[0]  -= 8;\r
+    VmWriteMem64 (VmPtr, (UINTN)VmPtr->Gpr[0], (UINT64)(UINTN)(VmPtr->Ip + Size));\r
 \r
-    VmPtr->Ip = (VMIP) InstructionBuffer->EbcEntryPoint;\r
+    VmPtr->Ip = (VMIP)InstructionBuffer->EbcEntryPoint;\r
   } else {\r
     //\r
     // The callee is not a thunk to EBC, call native code,\r
@@ -475,4 +479,3 @@ EbcLLCALLEX (
     VmPtr->Ip += Size;\r
   }\r
 }\r
-\r