]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/EbcDxe AARCH64: use a fixed size thunk structure
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Wed, 17 Aug 2016 14:24:52 +0000 (16:24 +0200)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Fri, 2 Sep 2016 06:59:21 +0000 (07:59 +0100)
The thunk generation is needlessly complex, given that it attempts to
deal with variable length instructions, which don't exist on AArch64.

So replace it with a simple template coded in assembler, with a matching
struct definition in C. That way, we can create and manipulate the thunks
easily without looping over the instructions looking for 'magic' numbers.

Also, use x16 rather than x9, since it is the architectural register to
use for thunks/veneers.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org>
Reviewed-by: Feng Tian <feng.tian@intel.com>
MdeModulePkg/Universal/EbcDxe/AArch64/EbcLowLevel.S
MdeModulePkg/Universal/EbcDxe/AArch64/EbcSupport.c

index 17f379248a6280cf98d8f3183232248856f21248..b4b8531f1a017b8b8b3099deb549d89e7ebaf829 100644 (file)
@@ -3,8 +3,10 @@
 //  This code provides low level routines that support the Virtual Machine\r
 //  for option ROMs.\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) 2007 - 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
@@ -19,6 +21,8 @@ ASM_GLOBAL ASM_PFX(EbcLLCALLEXNative)
 ASM_GLOBAL ASM_PFX(EbcLLEbcInterpret)\r
 ASM_GLOBAL ASM_PFX(EbcLLExecuteEbcImageEntryPoint)\r
 \r
+ASM_GLOBAL ASM_PFX(mEbcInstructionBufferTemplate)\r
+\r
 //****************************************************************************\r
 // EbcLLCALLEX\r
 //\r
@@ -61,7 +65,7 @@ ASM_PFX(EbcLLCALLEXNative):
 //\r
 // This function is called by the thunk code to handle an Native to EBC call\r
 // This can handle up to 16 arguments (1-8 on in x0-x7, 9-16 are on the stack)\r
-// x9 contains the Entry point that will be the first argument when\r
+// x16 contains the Entry point that will be the first argument when\r
 // EBCInterpret is called.\r
 //\r
 //****************************************************************************\r
@@ -97,7 +101,7 @@ ASM_PFX(EbcLLEbcInterpret):
     mov x3, x2\r
     mov x2, x1\r
     mov x1, x0\r
-    mov x0, x9\r
+    mov x0, x16\r
 \r
     // call C-code\r
     bl ASM_PFX(EbcInterpret)\r
@@ -111,7 +115,7 @@ ASM_PFX(EbcLLEbcInterpret):
 // EbcLLExecuteEbcImageEntryPoint\r
 //\r
 // This function is called by the thunk code to handle the image entry point\r
-// x9 contains the Entry point that will be the first argument when\r
+// x16 contains the Entry point that will be the third argument when\r
 // ExecuteEbcImageEntryPoint is called.\r
 //\r
 //****************************************************************************\r
@@ -120,9 +124,27 @@ ASM_PFX(EbcLLExecuteEbcImageEntryPoint):
     // build new parameter calling convention\r
     mov  x2, x1\r
     mov  x1, x0\r
-    mov  x0, x9\r
+    mov  x0, x16\r
 \r
     // call C-code\r
     bl ASM_PFX(ExecuteEbcImageEntryPoint)\r
     ldp  x29, x30, [sp], #16\r
     ret\r
+\r
+//****************************************************************************\r
+// mEbcInstructionBufferTemplate\r
+//****************************************************************************\r
+    .section    ".rodata", "a"\r
+    .align      3\r
+ASM_PFX(mEbcInstructionBufferTemplate):\r
+    adr     x17, 0f\r
+    ldp     x16, x17, [x17]\r
+    br      x17\r
+\r
+    //\r
+    // Add a magic code here to help the VM recognize the thunk.\r
+    //\r
+    hlt     #0xEBC\r
+\r
+0:  .quad   0   // EBC_ENTRYPOINT_SIGNATURE\r
+    .quad   0   // EBC_LL_EBC_ENTRYPOINT_SIGNATURE\r
index 23261a0701431f58ce0c22cc46d5948e6cd1972f..a5f21f400274200ed884b02e27c0ac5d4ef958c5 100644 (file)
@@ -2,8 +2,10 @@
   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
+\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
@@ -22,47 +24,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 //\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
@@ -414,10 +385,7 @@ EbcCreateThunks (
   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
@@ -426,51 +394,38 @@ EbcCreateThunks (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  ThunkSize = sizeof(mInstructionBufferTemplate);\r
-\r
-  Ptr = AllocatePool (sizeof(mInstructionBufferTemplate));\r
-\r
-  if (Ptr == NULL) {\r
+  InstructionBuffer = AllocatePool (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 (InstructionBuffer, &mEbcInstructionBufferTemplate,\r
+    sizeof (EBC_INSTRUCTION_BUFFER));\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 (ImageHandle, InstructionBuffer,\r
+    sizeof (EBC_INSTRUCTION_BUFFER));\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -500,40 +455,15 @@ EbcLLCALLEX (
   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 (InstructionBuffer, &mEbcInstructionBufferTemplate,\r
+        sizeof(EBC_INSTRUCTION_BUFFER) - 2 * sizeof (UINT64)) == 0) {\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
@@ -545,8 +475,7 @@ EbcLLCALLEX (
     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