]> 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 23261a0701431f58ce0c22cc46d5948e6cd1972f..6622eace985faac258bc87186c4532684560c3d3 100644 (file)
@@ -2,67 +2,33 @@
   This module contains EBC support routines that are customized based on\r
   the target AArch64 processor.\r
 \r
-Copyright (c) 2015, The Linux Foundation. All rights reserved.\r
+Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>\r
+Copyright (c) 2015, The Linux Foundation. All rights reserved.<BR>\r
 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\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
-//\r
-// This is instruction buffer used to create EBC thunk\r
-//\r
-#define EBC_MAGIC_SIGNATURE                0xCA112EBCCA112EBCull\r
-#define EBC_ENTRYPOINT_SIGNATURE           0xAFAFAFAFAFAFAFAFull\r
-#define EBC_LL_EBC_ENTRYPOINT_SIGNATURE    0xFAFAFAFAFAFAFAFAull\r
-UINT8  mInstructionBufferTemplate[] = {\r
-  0x03,  0x00, 0x00, 0x14, //b pc+16\r
-  //\r
-  // Add a magic code here to help the VM recognize the thunk..\r
-  //\r
-    (UINT8)(EBC_MAGIC_SIGNATURE & 0xFF),\r
-    (UINT8)((EBC_MAGIC_SIGNATURE >> 8) & 0xFF),\r
-    (UINT8)((EBC_MAGIC_SIGNATURE >> 16) & 0xFF),\r
-    (UINT8)((EBC_MAGIC_SIGNATURE >> 24) & 0xFF),\r
-    (UINT8)((EBC_MAGIC_SIGNATURE >> 32) & 0xFF),\r
-    (UINT8)((EBC_MAGIC_SIGNATURE >> 40) & 0xFF),\r
-    (UINT8)((EBC_MAGIC_SIGNATURE >> 48) & 0xFF),\r
-    (UINT8)((EBC_MAGIC_SIGNATURE >> 56) & 0xFF),\r
-  0x69, 0x00, 0x00, 0x58, //ldr x9, #32\r
-  0x8A, 0x00, 0x00, 0x58, //ldr x10, #40\r
-  0x05, 0x00, 0x00, 0x14, //b pc+32\r
-    (UINT8)(EBC_ENTRYPOINT_SIGNATURE & 0xFF),\r
-    (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 8) & 0xFF),\r
-    (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 16) & 0xFF),\r
-    (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 24) & 0xFF),\r
-    (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 32) & 0xFF),\r
-    (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 40) & 0xFF),\r
-    (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 48) & 0xFF),\r
-    (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 56) & 0xFF),\r
-    (UINT8)(EBC_LL_EBC_ENTRYPOINT_SIGNATURE & 0xFF),\r
-    (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 8) & 0xFF),\r
-    (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 16) & 0xFF),\r
-    (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 24) & 0xFF),\r
-    (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 32) & 0xFF),\r
-    (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 40) & 0xFF),\r
-    (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 48) & 0xFF),\r
-    (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 56) & 0xFF),\r
-  0x40, 0x01, 0x1F, 0xD6 //br x10\r
-\r
-};\r
+#pragma pack(1)\r
+typedef struct {\r
+  UINT32    Instr[3];\r
+  UINT32    Magic;\r
+  UINT64    EbcEntryPoint;\r
+  UINT64    EbcLlEntryPoint;\r
+} EBC_INSTRUCTION_BUFFER;\r
+#pragma pack()\r
+\r
+extern CONST EBC_INSTRUCTION_BUFFER  mEbcInstructionBufferTemplate;\r
 \r
 /**\r
   Begin executing an EBC image.\r
@@ -99,26 +65,24 @@ 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
   This is a thunk function.\r
 \r
-  @param  EntryPoint            The entrypoint of EBC code.\r
   @param  Arg1                  The 1st argument.\r
   @param  Arg2                  The 2nd argument.\r
   @param  Arg3                  The 3rd argument.\r
@@ -127,14 +91,8 @@ PushU64 (
   @param  Arg6                  The 6th argument.\r
   @param  Arg7                  The 7th argument.\r
   @param  Arg8                  The 8th argument.\r
-  @param  Arg9                  The 9th argument.\r
-  @param  Arg10                 The 10th argument.\r
-  @param  Arg11                 The 11th argument.\r
-  @param  Arg12                 The 12th argument.\r
-  @param  Arg13                 The 13th argument.\r
-  @param  Arg14                 The 14th argument.\r
-  @param  Arg15                 The 15th argument.\r
-  @param  Arg16                 The 16th argument.\r
+  @param  EntryPoint            The entrypoint of EBC code.\r
+  @param  Args9_16[]            Array containing arguments #9 to #16.\r
 \r
   @return The value returned by the EBC application we're going to run.\r
 \r
@@ -142,23 +100,16 @@ PushU64 (
 UINT64\r
 EFIAPI\r
 EbcInterpret (\r
-  IN UINTN      EntryPoint,\r
-  IN UINTN      Arg1,\r
-  IN UINTN      Arg2,\r
-  IN UINTN      Arg3,\r
-  IN UINTN      Arg4,\r
-  IN UINTN      Arg5,\r
-  IN UINTN      Arg6,\r
-  IN UINTN      Arg7,\r
-  IN UINTN      Arg8,\r
-  IN UINTN      Arg9,\r
-  IN UINTN      Arg10,\r
-  IN UINTN      Arg11,\r
-  IN UINTN      Arg12,\r
-  IN UINTN      Arg13,\r
-  IN UINTN      Arg14,\r
-  IN UINTN      Arg15,\r
-  IN UINTN      Arg16\r
+  IN UINTN        Arg1,\r
+  IN UINTN        Arg2,\r
+  IN UINTN        Arg3,\r
+  IN UINTN        Arg4,\r
+  IN UINTN        Arg5,\r
+  IN UINTN        Arg6,\r
+  IN UINTN        Arg7,\r
+  IN UINTN        Arg8,\r
+  IN UINTN        EntryPoint,\r
+  IN CONST UINTN  Args9_16[]\r
   )\r
 {\r
   //\r
@@ -177,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
@@ -193,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
@@ -210,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) Arg16);\r
-  PushU64 (&VmContext, (UINT64) Arg15);\r
-  PushU64 (&VmContext, (UINT64) Arg14);\r
-  PushU64 (&VmContext, (UINT64) Arg13);\r
-  PushU64 (&VmContext, (UINT64) Arg12);\r
-  PushU64 (&VmContext, (UINT64) Arg11);\r
-  PushU64 (&VmContext, (UINT64) Arg10);\r
-  PushU64 (&VmContext, (UINT64) Arg9);\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
@@ -268,23 +220,23 @@ 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
-  @param  EntryPoint       The entrypoint of EBC code.\r
   @param  ImageHandle      image handle for the EBC application we're executing\r
   @param  SystemTable      standard system table passed into an driver's entry\r
                            point\r
+  @param  EntryPoint       The entrypoint of EBC code.\r
 \r
   @return The value returned by the EBC application we're going to run.\r
 \r
@@ -292,9 +244,9 @@ EbcInterpret (
 UINT64\r
 EFIAPI\r
 ExecuteEbcImageEntryPoint (\r
-  IN UINTN                EntryPoint,\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable,\r
+  IN UINTN             EntryPoint\r
   )\r
 {\r
   //\r
@@ -313,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
@@ -324,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
@@ -379,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
@@ -408,74 +360,63 @@ 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
-  UINT8       *Ptr;\r
-  UINT8       *ThunkBase;\r
-  UINT32      Index;\r
-  INT32       ThunkSize;\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
-  ThunkSize = sizeof(mInstructionBufferTemplate);\r
-\r
-  Ptr = AllocatePool (sizeof(mInstructionBufferTemplate));\r
-\r
-  if (Ptr == NULL) {\r
+  InstructionBuffer = EbcAllocatePoolForThunk (sizeof (EBC_INSTRUCTION_BUFFER));\r
+  if (InstructionBuffer == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  //\r
-  //  Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
-  //\r
-  // Save the start address so we can add a pointer to it to a list later.\r
-  //\r
-  ThunkBase = Ptr;\r
 \r
   //\r
   // Give them the address of our buffer we're going to fix up\r
   //\r
-  *Thunk = (VOID *) Ptr;\r
+  *Thunk = InstructionBuffer;\r
 \r
   //\r
   // Copy whole thunk instruction buffer template\r
   //\r
-  CopyMem (Ptr, mInstructionBufferTemplate, sizeof(mInstructionBufferTemplate));\r
+  CopyMem (\r
+    InstructionBuffer,\r
+    &mEbcInstructionBufferTemplate,\r
+    sizeof (EBC_INSTRUCTION_BUFFER)\r
+    );\r
 \r
   //\r
   // Patch EbcEntryPoint and EbcLLEbcInterpret\r
   //\r
-  for (Index = 0; Index < sizeof(mInstructionBufferTemplate) - sizeof(UINTN); Index++) {\r
-    if (*(UINTN *)&Ptr[Index] == EBC_ENTRYPOINT_SIGNATURE) {\r
-      *(UINTN *)&Ptr[Index] = (UINTN)EbcEntryPoint;\r
-    }\r
-    if (*(UINTN *)&Ptr[Index] == EBC_LL_EBC_ENTRYPOINT_SIGNATURE) {\r
-      if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r
-        *(UINTN *)&Ptr[Index] = (UINTN)EbcLLExecuteEbcImageEntryPoint;\r
-      } else {\r
-        *(UINTN *)&Ptr[Index] = (UINTN)EbcLLEbcInterpret;\r
-      }\r
-    }\r
+  InstructionBuffer->EbcEntryPoint = (UINT64)EbcEntryPoint;\r
+  if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r
+    InstructionBuffer->EbcLlEntryPoint = (UINT64)EbcLLExecuteEbcImageEntryPoint;\r
+  } else {\r
+    InstructionBuffer->EbcLlEntryPoint = (UINT64)EbcLLEbcInterpret;\r
   }\r
 \r
   //\r
   // 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, (VOID *) ThunkBase, ThunkSize);\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
@@ -493,60 +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
-  UINTN    IsThunk;\r
-  UINTN    TargetEbcAddr;\r
-  UINT8    InstructionBuffer[sizeof(mInstructionBufferTemplate)];\r
-  UINTN    Index;\r
-  UINTN    IndexOfEbcEntrypoint;\r
-\r
-  IsThunk       = 1;\r
-  TargetEbcAddr = 0;\r
-  IndexOfEbcEntrypoint = 0;\r
+  CONST EBC_INSTRUCTION_BUFFER  *InstructionBuffer;\r
 \r
   //\r
   // Processor specific code to check whether the callee is a thunk to EBC.\r
   //\r
-  CopyMem (InstructionBuffer, (VOID *)FuncAddr, sizeof(InstructionBuffer));\r
-  //\r
-  // Fill the signature according to mInstructionBufferTemplate\r
-  //\r
-  for (Index = 0; Index < sizeof(mInstructionBufferTemplate) - sizeof(UINTN); Index++) {\r
-    if (*(UINTN *)&mInstructionBufferTemplate[Index] == EBC_ENTRYPOINT_SIGNATURE) {\r
-      *(UINTN *)&InstructionBuffer[Index] = EBC_ENTRYPOINT_SIGNATURE;\r
-      IndexOfEbcEntrypoint = Index;\r
-    }\r
-    if (*(UINTN *)&mInstructionBufferTemplate[Index] == EBC_LL_EBC_ENTRYPOINT_SIGNATURE) {\r
-      *(UINTN *)&InstructionBuffer[Index] = EBC_LL_EBC_ENTRYPOINT_SIGNATURE;\r
-    }\r
-  }\r
-  //\r
-  // Check if we need thunk to native\r
-  //\r
-  if (CompareMem (InstructionBuffer, mInstructionBufferTemplate, sizeof(mInstructionBufferTemplate)) != 0) {\r
-    IsThunk = 0;\r
-  }\r
+  InstructionBuffer = (EBC_INSTRUCTION_BUFFER *)FuncAddr;\r
 \r
-  if (IsThunk == 1){\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
-    CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + IndexOfEbcEntrypoint, sizeof(UINTN));\r
-    VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
+    VmPtr->Ip = (VMIP)InstructionBuffer->EbcEntryPoint;\r
   } else {\r
     //\r
     // The callee is not a thunk to EBC, call native code,\r
@@ -560,4 +479,3 @@ EbcLLCALLEX (
     VmPtr->Ip += Size;\r
   }\r
 }\r
-\r