]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/EbcDxe/X64/EbcSupport.c
1) Add type cast for better coding style.
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / X64 / EbcSupport.c
index b737db1c3c6d5ccadf321d70b15ab5610e6a84ba..bdde5e41fad80d7f7730d85638013f0872d6f2b3 100644 (file)
@@ -2,8 +2,8 @@
   This module contains EBC support routines that are customized based on\r
   the target x64 processor.\r
 \r
-Copyright (c) 2006 - 2010, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\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
@@ -23,10 +23,80 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 //       platform-specific configurations.\r
 //\r
 #define VM_STACK_SIZE   (1024 * 8)\r
-#define EBC_THUNK_SIZE  64\r
 \r
 #define STACK_REMAIN_SIZE (1024 * 4)\r
 \r
+//\r
+// This is instruction buffer used to create EBC thunk\r
+//\r
+#define EBC_ENTRYPOINT_SIGNATURE           0xAFAFAFAFAFAFAFAFull\r
+#define EBC_LL_EBC_ENTRYPOINT_SIGNATURE    0xFAFAFAFAFAFAFAFAull\r
+UINT8  mInstructionBufferTemplate[] = {\r
+  //\r
+  // Add a magic code here to help the VM recognize the thunk..\r
+  // mov rax, 0xca112ebcca112ebc  => 48 B8 BC 2E 11 CA BC 2E 11 CA\r
+  //\r
+  0x48, 0xB8, 0xBC, 0x2E, 0x11, 0xCA, 0xBC, 0x2E, 0x11, 0xCA,\r
+  //\r
+  // Add code bytes to load up a processor register with the EBC entry point.\r
+  //  mov r10, EbcEntryPoint  => 49 BA XX XX XX XX XX XX XX XX (To be fixed at runtime)\r
+  // These 8 bytes of the thunk entry is the address of the EBC\r
+  // entry point.\r
+  //\r
+  0x49, 0xBA, \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
+  //\r
+  // Stick in a load of r11 with the address of appropriate VM function.\r
+  //  mov r11, EbcLLEbcInterpret  => 49 BB XX XX XX XX XX XX XX XX (To be fixed at runtime)\r
+  //\r
+  0x49, 0xBB,\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
+  //\r
+  // Stick in jump opcode bytes\r
+  //  jmp r11 => 41 FF E3\r
+  //\r
+  0x41, 0xFF, 0xE3,\r
+};\r
+\r
+/**\r
+  Begin executing an EBC image.\r
+  This is used for Ebc Thunk call.\r
+\r
+  @return The value returned by the EBC application we're going to run.\r
+\r
+**/\r
+UINT64\r
+EFIAPI\r
+EbcLLEbcInterpret (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Begin executing an EBC image.\r
+  This is used for Ebc image entrypoint.\r
+\r
+  @return The value returned by the EBC application we're going to run.\r
+\r
+**/\r
+UINT64\r
+EFIAPI\r
+EbcLLExecuteEbcImageEntryPoint (\r
+  VOID\r
+  );\r
 \r
 /**\r
   Pushes a 64 bit unsigned value to the VM stack.\r
@@ -52,14 +122,13 @@ PushU64 (
 \r
 \r
 /**\r
-  Begin executing an EBC image. The address of the entry point is passed\r
-  in via a processor register, so we'll need to make a call to get the\r
-  value.\r
+  Begin executing an EBC image.\r
 \r
   This is a thunk function. Microsoft x64 compiler only provide fast_call\r
   calling convention, so the first four arguments are passed by rcx, rdx,\r
   r8, and r9, while other arguments are passed in stack.\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
@@ -81,23 +150,25 @@ PushU64 (
 \r
 **/\r
 UINT64\r
+EFIAPI\r
 EbcInterpret (\r
-  IN OUT UINTN      Arg1,\r
-  IN OUT UINTN      Arg2,\r
-  IN OUT UINTN      Arg3,\r
-  IN OUT UINTN      Arg4,\r
-  IN OUT UINTN      Arg5,\r
-  IN OUT UINTN      Arg6,\r
-  IN OUT UINTN      Arg7,\r
-  IN OUT UINTN      Arg8,\r
-  IN OUT UINTN      Arg9,\r
-  IN OUT UINTN      Arg10,\r
-  IN OUT UINTN      Arg11,\r
-  IN OUT UINTN      Arg12,\r
-  IN OUT UINTN      Arg13,\r
-  IN OUT UINTN      Arg14,\r
-  IN OUT UINTN      Arg15,\r
-  IN OUT UINTN      Arg16\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
   )\r
 {\r
   //\r
@@ -109,11 +180,9 @@ EbcInterpret (
   UINTN       StackIndex;\r
 \r
   //\r
-  // Get the EBC entry point from the processor register.\r
-  // Don't call any function before getting the EBC entry\r
-  // point because this will collab the return register.\r
+  // Get the EBC entry point\r
   //\r
-  Addr = EbcLLGetEbcEntryPoint ();\r
+  Addr = EntryPoint;\r
 \r
   //\r
   // Now clear out our context\r
@@ -129,7 +198,6 @@ EbcInterpret (
   // 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
-  Addr            = EbcLLGetStackPointer ();\r
 \r
   //\r
   // Adjust the VM's stack pointer down.\r
@@ -147,7 +215,7 @@ EbcInterpret (
   //\r
   // Align the stack on a natural boundary.\r
   //\r
-  VmContext.Gpr[0] &= ~(sizeof (UINTN) - 1);\r
+  VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof (UINTN) - 1);\r
 \r
   //\r
   // Put a magic value in the stack gap, then adjust down again.\r
@@ -221,10 +289,9 @@ EbcInterpret (
 \r
 \r
 /**\r
-  Begin executing an EBC image. The address of the entry point is passed\r
-  in via a processor register, so we'll need to make a call to get the\r
-  value.\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
@@ -233,7 +300,9 @@ EbcInterpret (
 \r
 **/\r
 UINT64\r
+EFIAPI\r
 ExecuteEbcImageEntryPoint (\r
+  IN UINTN                EntryPoint,\r
   IN EFI_HANDLE           ImageHandle,\r
   IN EFI_SYSTEM_TABLE     *SystemTable\r
   )\r
@@ -247,11 +316,9 @@ ExecuteEbcImageEntryPoint (
   UINTN       StackIndex;\r
 \r
   //\r
-  // Get the EBC entry point from the processor register. Make sure you don't\r
-  // call any functions before this or you could mess up the register the\r
-  // entry point is passed in.\r
+  // Get the EBC entry point\r
   //\r
-  Addr = EbcLLGetEbcEntryPoint ();\r
+  Addr = EntryPoint;\r
 \r
   //\r
   // Now clear out our context\r
@@ -273,7 +340,6 @@ ExecuteEbcImageEntryPoint (
   // 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
-  Addr = EbcLLGetStackPointer ();\r
 \r
   Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
   if (EFI_ERROR(Status)) {\r
@@ -293,7 +359,7 @@ ExecuteEbcImageEntryPoint (
 \r
   //\r
   // Align the stack on a natural boundary\r
-  VmContext.Gpr[0] &= ~(sizeof(UINTN) - 1);\r
+  VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof(UINTN) - 1);\r
   //\r
   VmContext.LowStackTop   = (UINTN) VmContext.Gpr[0];\r
 \r
@@ -361,8 +427,6 @@ EbcCreateThunks (
   UINT8       *Ptr;\r
   UINT8       *ThunkBase;\r
   UINT32      Index;\r
-  UINT64      Addr;\r
-  INT32       Size;\r
   INT32       ThunkSize;\r
 \r
   //\r
@@ -372,10 +436,9 @@ EbcCreateThunks (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Size      = EBC_THUNK_SIZE;\r
-  ThunkSize = Size;\r
+  ThunkSize = sizeof(mInstructionBufferTemplate);\r
 \r
-  Ptr = AllocatePool (Size);\r
+  Ptr = AllocatePool (sizeof(mInstructionBufferTemplate));\r
 \r
   if (Ptr == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -393,89 +456,26 @@ EbcCreateThunks (
   *Thunk = (VOID *) Ptr;\r
 \r
   //\r
-  // Add a magic code here to help the VM recognize the thunk..\r
-  // mov rax, ca112ebccall2ebch  => 48 B8 BC 2E 11 CA BC 2E 11 CA\r
-  //\r
-  *Ptr = 0x48;\r
-  Ptr++;\r
-  Size--;\r
-  *Ptr = 0xB8;\r
-  Ptr++;\r
-  Size--;\r
-  Addr = (UINT64) 0xCA112EBCCA112EBCULL;\r
-  for (Index = 0; Index < sizeof (Addr); Index++) {\r
-    *Ptr = (UINT8) (UINTN) Addr;\r
-    Addr >>= 8;\r
-    Ptr++;\r
-    Size--;\r
-  }\r
-\r
-  //\r
-  // Add code bytes to load up a processor register with the EBC entry point.\r
-  // mov r10, 123456789abcdef0h  => 49 BA F0 DE BC 9A 78 56 34 12\r
-  // The first 8 bytes of the thunk entry is the address of the EBC\r
-  // entry point.\r
-  //\r
-  *Ptr = 0x49;\r
-  Ptr++;\r
-  Size--;\r
-  *Ptr = 0xBA;\r
-  Ptr++;\r
-  Size--;\r
-  Addr = (UINT64) EbcEntryPoint;\r
-  for (Index = 0; Index < sizeof (Addr); Index++) {\r
-    *Ptr = (UINT8) (UINTN) Addr;\r
-    Addr >>= 8;\r
-    Ptr++;\r
-    Size--;\r
-  }\r
-\r
-  //\r
-  // Stick in a load of ecx with the address of appropriate VM function.\r
-  // Using r11 because it's a volatile register and won't be used in this\r
-  // point.\r
-  // mov r11 123456789abcdef0h  => 49 BB F0 DE BC 9A 78 56 34 12\r
+  // Copy whole thunk instruction buffer template\r
   //\r
-  if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r
-    Addr = (UINTN) ExecuteEbcImageEntryPoint;\r
-  } else {\r
-    Addr = (UINTN) EbcInterpret;\r
-  }\r
+  CopyMem (Ptr, mInstructionBufferTemplate, sizeof(mInstructionBufferTemplate));\r
 \r
   //\r
-  // mov r11 Addr => 0x49 0xBB\r
+  // Patch EbcEntryPoint and EbcLLEbcInterpret\r
   //\r
-  *Ptr = 0x49;\r
-  Ptr++;\r
-  Size--;\r
-  *Ptr = 0xBB;\r
-  Ptr++;\r
-  Size--;\r
-  for (Index = 0; Index < sizeof (Addr); Index++) {\r
-    *Ptr = (UINT8) Addr;\r
-    Addr >>= 8;\r
-    Ptr++;\r
-    Size--;\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
   }\r
-  //\r
-  // Stick in jump opcode bytes for jmp r11 => 0x41 0xFF 0xE3\r
-  //\r
-  *Ptr = 0x41;\r
-  Ptr++;\r
-  Size--;\r
-  *Ptr = 0xFF;\r
-  Ptr++;\r
-  Size--;\r
-  *Ptr = 0xE3;\r
-  Size--;\r
 \r
-  //\r
-  // Double check that our defined size is ok (application error)\r
-  //\r
-  if (Size < 0) {\r
-    ASSERT (FALSE);\r
-    return EFI_BUFFER_TOO_SMALL;\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
@@ -512,65 +512,37 @@ EbcLLCALLEX (
 {\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
 \r
   //\r
   // Processor specific code to check whether the callee is a thunk to EBC.\r
   //\r
-  if (*((UINT8 *)FuncAddr) != 0x48) {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 1) != 0xB8) {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 2) != 0xBC)  {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 3) != 0x2E)  {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 4) != 0x11)  {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 5) != 0xCA)  {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 6) != 0xBC)  {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 7) != 0x2E)  {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 8) != 0x11)  {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 9) != 0xCA)  {\r
-    IsThunk = 0;\r
-    goto Action;\r
-  }\r
-  if (*((UINT8 *)FuncAddr + 10) != 0x48)  {\r
-    IsThunk = 0;\r
-    goto Action;\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
-  if (*((UINT8 *)FuncAddr + 11) != 0xB8)  {\r
+  //\r
+  // Check if we need thunk to native\r
+  //\r
+  if (CompareMem (InstructionBuffer, mInstructionBufferTemplate, sizeof(mInstructionBufferTemplate)) != 0) {\r
     IsThunk = 0;\r
-    goto Action;\r
   }\r
 \r
-  CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + 12, 8);\r
-\r
-Action:\r
   if (IsThunk == 1){\r
     //\r
     // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
@@ -581,19 +553,20 @@ Action:
     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) (VmPtr->Ip + Size));\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
   } else {\r
     //\r
-    // The callee is not a thunk to EBC, call native code.\r
+    // The callee is not a thunk to EBC, call native code,\r
+    // and get return value.\r
     //\r
-    EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
+    VmPtr->Gpr[7] = EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
 \r
     //\r
-    // Get return value and advance the IP.\r
+    // Advance the IP.\r
     //\r
-    VmPtr->Gpr[7] = EbcLLGetReturnValue ();\r
     VmPtr->Ip += Size;\r
   }\r
 }\r