]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.c
Adjust directory structures.
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / Ipf / EbcSupport.c
diff --git a/MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.c b/MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.c
new file mode 100644 (file)
index 0000000..3647a12
--- /dev/null
@@ -0,0 +1,869 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. 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
+\r
+Module Name:\r
+\r
+  EbcSupport.c\r
+\r
+Abstract:\r
+\r
+  This module contains EBC support routines that are customized based on\r
+  the target processor.\r
+\r
+--*/\r
+\r
+#include "EbcInt.h"\r
+#include "EbcExecute.h"\r
+#include "EbcSupport.h"\r
+\r
+STATIC\r
+EFI_STATUS\r
+WriteBundle (\r
+  IN    VOID    *MemPtr,\r
+  IN    UINT8   Template,\r
+  IN    UINT64  Slot0,\r
+  IN    UINT64  Slot1,\r
+  IN    UINT64  Slot2\r
+  );\r
+\r
+STATIC\r
+VOID\r
+PushU64 (\r
+  VM_CONTEXT *VmPtr,\r
+  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->R[0] -= sizeof (UINT64);\r
+  *(UINT64 *) VmPtr->R[0] = Arg;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+EbcInterpret (\r
+  UINT64      Arg1,\r
+  ...\r
+  )\r
+{\r
+  //\r
+  // Create a new VM context on the stack\r
+  //\r
+  VM_CONTEXT  VmContext;\r
+  UINTN       Addr;\r
+  EFI_STATUS  Status;\r
+  UINTN       StackIndex;\r
+  VA_LIST     List;\r
+  UINT64      Arg2;\r
+  UINT64      Arg3;\r
+  UINT64      Arg4;\r
+  UINT64      Arg5;\r
+  UINT64      Arg6;\r
+  UINT64      Arg7;\r
+  UINT64      Arg8;\r
+  UINT64      Arg9;\r
+  UINT64      Arg10;\r
+  UINT64      Arg11;\r
+  UINT64      Arg12;\r
+  UINT64      Arg13;\r
+  UINT64      Arg14;\r
+  UINT64      Arg15;\r
+  UINT64      Arg16;\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
+  //\r
+  Addr = EbcLLGetEbcEntryPoint ();\r
+  //\r
+  // Need the args off the stack.\r
+  //\r
+  VA_START (List, Arg1);\r
+  Arg2      = VA_ARG (List, UINT64);\r
+  Arg3      = VA_ARG (List, UINT64);\r
+  Arg4      = VA_ARG (List, UINT64);\r
+  Arg5      = VA_ARG (List, UINT64);\r
+  Arg6      = VA_ARG (List, UINT64);\r
+  Arg7      = VA_ARG (List, UINT64);\r
+  Arg8      = VA_ARG (List, UINT64);\r
+  Arg9      = VA_ARG (List, UINT64);\r
+  Arg10     = VA_ARG (List, UINT64);\r
+  Arg11     = VA_ARG (List, UINT64);\r
+  Arg12     = VA_ARG (List, UINT64);\r
+  Arg13     = VA_ARG (List, UINT64);\r
+  Arg14     = VA_ARG (List, UINT64);\r
+  Arg15     = VA_ARG (List, UINT64);\r
+  Arg16     = VA_ARG (List, UINT64);\r
+  //\r
+  // Now clear out our context\r
+  //\r
+  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+  //\r
+  // Set the VM instruction pointer to the correct location in memory.\r
+  //\r
+  VmContext.Ip = (VMIP) Addr;\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
+  // NOTE: Eventually we should have the interpreter allocate memory\r
+  //       for stack space which it will use during its execution. This\r
+  //       would likely improve performance because the interpreter would\r
+  //       no longer be required to test each memory access and adjust\r
+  //       those reading from the stack gap.\r
+  //\r
+  // For IPF, the stack looks like (assuming 10 args passed)\r
+  //   arg10\r
+  //   arg9       (Bottom of high stack)\r
+  //   [ stack gap for interpreter execution ]\r
+  //   [ magic value for detection of stack corruption ]\r
+  //   arg8       (Top of low stack)\r
+  //   arg7....\r
+  //   arg1\r
+  //   [ 64-bit return address ]\r
+  //   [ ebc stack ]\r
+  // If the EBC accesses memory in the stack gap, then we assume that it's\r
+  // actually trying to access args9 and greater. Therefore we need to\r
+  // adjust memory accesses in this region to point above the stack gap.\r
+  //\r
+  //\r
+  // Now adjust the EBC stack pointer down to leave a gap for interpreter\r
+  // execution. Then stuff a magic value there.\r
+  //\r
+  \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.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+\r
+  \r
+  PushU64 (&VmContext, (UINT64) VM_STACK_KEY_VALUE);\r
+  VmContext.StackMagicPtr = (UINTN *) VmContext.R[0];\r
+  VmContext.LowStackTop   = (UINTN) VmContext.R[0];\r
+  //\r
+  // Push the EBC arguments on the stack. Does not matter that they may not\r
+  // all be valid.\r
+  //\r
+  PushU64 (&VmContext, Arg16);\r
+  PushU64 (&VmContext, Arg15);\r
+  PushU64 (&VmContext, Arg14);\r
+  PushU64 (&VmContext, Arg13);\r
+  PushU64 (&VmContext, Arg12);\r
+  PushU64 (&VmContext, Arg11);\r
+  PushU64 (&VmContext, Arg10);\r
+  PushU64 (&VmContext, Arg9);\r
+  PushU64 (&VmContext, Arg8);\r
+  PushU64 (&VmContext, Arg7);\r
+  PushU64 (&VmContext, Arg6);\r
+  PushU64 (&VmContext, Arg5);\r
+  PushU64 (&VmContext, Arg4);\r
+  PushU64 (&VmContext, Arg3);\r
+  PushU64 (&VmContext, Arg2);\r
+  PushU64 (&VmContext, Arg1);\r
+  //\r
+  // Push a bogus return address on the EBC stack because the\r
+  // interpreter expects one there. For stack alignment purposes on IPF,\r
+  // EBC return addresses are always 16 bytes. Push a bogus value as well.\r
+  //\r
+  PushU64 (&VmContext, 0);\r
+  PushU64 (&VmContext, 0xDEADBEEFDEADBEEF);\r
+  VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
+  //\r
+  // Begin executing the EBC code\r
+  //\r
+  EbcExecute (&VmContext);\r
+  //\r
+  // Return the value in R[7] unless there was an error\r
+  //\r
+  ReturnEBCStack(StackIndex);\r
+  return (UINT64) VmContext.R[7];\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEbcImageEntryPoint (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  IPF implementation.\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
+  \r
+Arguments:\r
+\r
+  ImageHandle   - image handle for the EBC application we're executing\r
+  SystemTable   - standard system table passed into an driver's entry point\r
+\r
+Returns:\r
+\r
+  The value returned by the EBC application we're going to run.\r
+\r
+--*/\r
+{\r
+  //\r
+  // Create a new VM context on the stack\r
+  //\r
+  VM_CONTEXT  VmContext;\r
+  UINTN       Addr;\r
+  EFI_STATUS  Status;\r
+  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
+  //\r
+  Addr = EbcLLGetEbcEntryPoint ();\r
+\r
+  //\r
+  // Now clear out our context\r
+  //\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
+  //\r
+  VmContext.ImageHandle = ImageHandle;\r
+  VmContext.SystemTable = SystemTable;\r
+\r
+  //\r
+  // Set the VM instruction pointer to the correct location in memory.\r
+  //\r
+  VmContext.Ip = (VMIP) Addr;\r
+\r
+  //\r
+  // Get the stack pointer. This is the bottom of the upper stack.\r
+  //\r
+  Addr                      = EbcLLGetStackPointer ();\r
+  \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.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+\r
+  \r
+  //\r
+  // Allocate stack space for the interpreter. Then put a magic value\r
+  // at the bottom so we can detect stack corruption.\r
+  //\r
+  PushU64 (&VmContext, (UINT64) VM_STACK_KEY_VALUE);\r
+  VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
+\r
+  //\r
+  // When we thunk to external native code, we copy the last 8 qwords from\r
+  // the EBC stack into the processor registers, and adjust the stack pointer\r
+  // up. If the caller is not passing 8 parameters, then we've moved the\r
+  // stack pointer up into the stack gap. If this happens, then the caller\r
+  // can mess up the stack gap contents (in particular our magic value).\r
+  // Therefore, leave another gap below the magic value. Pick 10 qwords down,\r
+  // just as a starting point.\r
+  //\r
+  VmContext.R[0] -= 10 * sizeof (UINT64);\r
+\r
+  //\r
+  // Align the stack pointer such that after pushing the system table,\r
+  // image handle, and return address on the stack, it's aligned on a 16-byte\r
+  // boundary as required for IPF.\r
+  //\r
+  VmContext.R[0] &= (INT64)~0x0f;\r
+  VmContext.LowStackTop = (UINTN) VmContext.R[0];\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
+\r
+  //\r
+  // Interpreter assumes 64-bit return address is pushed on the stack.\r
+  // IPF does not do this so pad the stack accordingly. Also, a\r
+  // "return address" is 16 bytes as required for IPF stack alignments.\r
+  //\r
+  PushU64 (&VmContext, (UINT64) 0);\r
+  PushU64 (&VmContext, (UINT64) 0x1234567887654321);\r
+  VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
+\r
+  //\r
+  // Begin executing the EBC code\r
+  //\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.R[7];\r
+}\r
+\r
+EFI_STATUS\r
+EbcCreateThunks (\r
+  IN EFI_HANDLE   ImageHandle,\r
+  IN VOID         *EbcEntryPoint,\r
+  OUT VOID        **Thunk,\r
+  IN  UINT32      Flags\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Create thunks for an EBC image entry point, or an EBC protocol service.\r
+  \r
+Arguments:\r
+\r
+  ImageHandle     - Image handle for the EBC image. If not null, then we're\r
+                    creating a thunk for an image entry point.\r
+  EbcEntryPoint   - Address of the EBC code that the thunk is to call\r
+  Thunk           - Returned thunk we create here\r
+  Flags           - Flags indicating options for creating the thunk\r
+  \r
+Returns:\r
+\r
+  Standard EFI status.\r
+  \r
+--*/\r
+{\r
+  UINT8       *Ptr;\r
+  UINT8       *ThunkBase;\r
+  UINT64      Addr;\r
+  UINT64      Code[3];    // Code in a bundle\r
+  UINT64      RegNum;     // register number for MOVL\r
+  UINT64      I;          // bits of MOVL immediate data\r
+  UINT64      Ic;         // bits of MOVL immediate data\r
+  UINT64      Imm5c;      // bits of MOVL immediate data\r
+  UINT64      Imm9d;      // bits of MOVL immediate data\r
+  UINT64      Imm7b;      // bits of MOVL immediate data\r
+  UINT64      Br;         // branch register for loading and jumping\r
+  UINT64      *Data64Ptr;\r
+  UINT32      ThunkSize;\r
+  UINT32      Size;\r
+\r
+  //\r
+  // Check alignment of pointer to EBC code, which must always be aligned\r
+  // on a 2-byte boundary.\r
+  //\r
+  if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Allocate memory for the thunk. Make the (most likely incorrect) assumption\r
+  // that the returned buffer is not aligned, so round up to the next\r
+  // alignment size.\r
+  //\r
+  Size      = EBC_THUNK_SIZE + EBC_THUNK_ALIGNMENT - 1;\r
+  ThunkSize = Size;\r
+  Ptr = AllocatePool (Size);\r
+\r
+  if (Ptr == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Save the start address of the buffer.\r
+  //\r
+  ThunkBase = Ptr;\r
+\r
+  //\r
+  // Make sure it's aligned for code execution. If not, then\r
+  // round up.\r
+  //\r
+  if ((UINT32) (UINTN) Ptr & (EBC_THUNK_ALIGNMENT - 1)) {\r
+    Ptr = (UINT8 *) (((UINTN) Ptr + (EBC_THUNK_ALIGNMENT - 1)) &~ (UINT64) (EBC_THUNK_ALIGNMENT - 1));\r
+  }\r
+  //\r
+  // Return the pointer to the thunk to the caller to user as the\r
+  // image entry point.\r
+  //\r
+  *Thunk = (VOID *) Ptr;\r
+\r
+  //\r
+  // Clear out the thunk entry\r
+  // ZeroMem(Ptr, Size);\r
+  //\r
+  // For IPF, when you do a call via a function pointer, the function pointer\r
+  // actually points to a function descriptor which consists of a 64-bit\r
+  // address of the function, followed by a 64-bit gp for the function being\r
+  // called. See the the Software Conventions and Runtime Architecture Guide\r
+  // for details.\r
+  // So first off in our thunk, create a descriptor for our actual thunk code.\r
+  // This means we need to create a pointer to the thunk code (which follows\r
+  // the descriptor we're going to create), followed by the gp of the Vm\r
+  // interpret function we're going to eventually execute.\r
+  //\r
+  Data64Ptr = (UINT64 *) Ptr;\r
+\r
+  //\r
+  // Write the function's entry point (which is our thunk code that follows\r
+  // this descriptor we're creating).\r
+  //\r
+  *Data64Ptr = (UINT64) (Data64Ptr + 2);\r
+  //\r
+  // Get the gp from the descriptor for EbcInterpret and stuff it in our thunk\r
+  // descriptor.\r
+  //\r
+  *(Data64Ptr + 1) = *(UINT64 *) ((UINT64 *) (UINTN) EbcInterpret + 1);\r
+  //\r
+  // Advance our thunk data pointer past the descriptor. Since the\r
+  // descriptor consists of 16 bytes, the pointer is still aligned for\r
+  // IPF code execution (on 16-byte boundary).\r
+  //\r
+  Ptr += sizeof (UINT64) * 2;\r
+\r
+  //\r
+  // *************************** MAGIC BUNDLE ********************************\r
+  //\r
+  // Write magic code bundle for: movl r8 = 0xca112ebcca112ebc to help the VM\r
+  // to recognize it is a thunk.\r
+  //\r
+  Addr = (UINT64) 0xCA112EBCCA112EBC;\r
+\r
+  //\r
+  // Now generate the code bytes. First is nop.m 0x0\r
+  //\r
+  Code[0] = OPCODE_NOP;\r
+\r
+  //\r
+  // Next is simply Addr[62:22] (41 bits) of the address\r
+  //\r
+  Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
+\r
+  //\r
+  // Extract bits from the address for insertion into the instruction\r
+  // i = Addr[63:63]\r
+  //\r
+  I = RShiftU64 (Addr, 63) & 0x01;\r
+  //\r
+  // ic = Addr[21:21]\r
+  //\r
+  Ic = RShiftU64 (Addr, 21) & 0x01;\r
+  //\r
+  // imm5c = Addr[20:16] for 5 bits\r
+  //\r
+  Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
+  //\r
+  // imm9d = Addr[15:7] for 9 bits\r
+  //\r
+  Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
+  //\r
+  // imm7b = Addr[6:0] for 7 bits\r
+  //\r
+  Imm7b = Addr & 0x7F;\r
+\r
+  //\r
+  // The EBC entry point will be put into r8, so r8 can be used here\r
+  // temporary. R8 is general register and is auto-serialized.\r
+  //\r
+  RegNum = 8;\r
+\r
+  //\r
+  // Next is jumbled data, including opcode and rest of address\r
+  //\r
+  Code[2] = LShiftU64 (Imm7b, 13);\r
+  Code[2] = Code[2] | LShiftU64 (0x00, 20);   // vc\r
+  Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
+  Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
+  Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
+  Code[2] = Code[2] | LShiftU64 (I, 36);\r
+  Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
+  Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
+\r
+  WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // *************************** FIRST BUNDLE ********************************\r
+  //\r
+  // Write code bundle for: movl r8 = EBC_ENTRY_POINT so we pass\r
+  // the ebc entry point in to the interpreter function via a processor\r
+  // register.\r
+  // Note -- we could easily change this to pass in a pointer to a structure\r
+  // that contained, among other things, the EBC image's entry point. But\r
+  // for now pass it directly.\r
+  //\r
+  Ptr += 16;\r
+  Addr = (UINT64) EbcEntryPoint;\r
+\r
+  //\r
+  // Now generate the code bytes. First is nop.m 0x0\r
+  //\r
+  Code[0] = OPCODE_NOP;\r
+\r
+  //\r
+  // Next is simply Addr[62:22] (41 bits) of the address\r
+  //\r
+  Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
+\r
+  //\r
+  // Extract bits from the address for insertion into the instruction\r
+  // i = Addr[63:63]\r
+  //\r
+  I = RShiftU64 (Addr, 63) & 0x01;\r
+  //\r
+  // ic = Addr[21:21]\r
+  //\r
+  Ic = RShiftU64 (Addr, 21) & 0x01;\r
+  //\r
+  // imm5c = Addr[20:16] for 5 bits\r
+  //\r
+  Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
+  //\r
+  // imm9d = Addr[15:7] for 9 bits\r
+  //\r
+  Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
+  //\r
+  // imm7b = Addr[6:0] for 7 bits\r
+  //\r
+  Imm7b = Addr & 0x7F;\r
+\r
+  //\r
+  // Put the EBC entry point in r8, which is the location of the return value\r
+  // for functions.\r
+  //\r
+  RegNum = 8;\r
+\r
+  //\r
+  // Next is jumbled data, including opcode and rest of address\r
+  //\r
+  Code[2] = LShiftU64 (Imm7b, 13);\r
+  Code[2] = Code[2] | LShiftU64 (0x00, 20);   // vc\r
+  Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
+  Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
+  Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
+  Code[2] = Code[2] | LShiftU64 (I, 36);\r
+  Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
+  Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
+\r
+  WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // *************************** NEXT BUNDLE *********************************\r
+  //\r
+  // Write code bundle for:\r
+  //   movl rx = offset_of(EbcInterpret|ExecuteEbcImageEntryPoint)\r
+  //\r
+  // Advance pointer to next bundle, then compute the offset from this bundle\r
+  // to the address of the entry point of the interpreter.\r
+  //\r
+  Ptr += 16;\r
+  if (Flags & FLAG_THUNK_ENTRY_POINT) {\r
+    Addr = (UINT64) ExecuteEbcImageEntryPoint;\r
+  } else {\r
+    Addr = (UINT64) EbcInterpret;\r
+  }\r
+  //\r
+  // Indirection on Itanium-based systems\r
+  //\r
+  Addr = *(UINT64 *) Addr;\r
+\r
+  //\r
+  // Now write the code to load the offset into a register\r
+  //\r
+  Code[0] = OPCODE_NOP;\r
+\r
+  //\r
+  // Next is simply Addr[62:22] (41 bits) of the address\r
+  //\r
+  Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
+\r
+  //\r
+  // Extract bits from the address for insertion into the instruction\r
+  // i = Addr[63:63]\r
+  //\r
+  I = RShiftU64 (Addr, 63) & 0x01;\r
+  //\r
+  // ic = Addr[21:21]\r
+  //\r
+  Ic = RShiftU64 (Addr, 21) & 0x01;\r
+  //\r
+  // imm5c = Addr[20:16] for 5 bits\r
+  //\r
+  Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
+  //\r
+  // imm9d = Addr[15:7] for 9 bits\r
+  //\r
+  Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
+  //\r
+  // imm7b = Addr[6:0] for 7 bits\r
+  //\r
+  Imm7b = Addr & 0x7F;\r
+\r
+  //\r
+  // Put it in r31, a scratch register\r
+  //\r
+  RegNum = 31;\r
+\r
+  //\r
+  // Next is jumbled data, including opcode and rest of address\r
+  //\r
+  Code[2] =   LShiftU64(Imm7b, 13);\r
+  Code[2] = Code[2] | LShiftU64 (0x00, 20);   // vc\r
+  Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
+  Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
+  Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
+  Code[2] = Code[2] | LShiftU64 (I, 36);\r
+  Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
+  Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
+\r
+  WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // *************************** NEXT BUNDLE *********************************\r
+  //\r
+  // Load branch register with EbcInterpret() function offset from the bundle\r
+  // address: mov b6 = RegNum\r
+  //\r
+  // See volume 3 page 4-29 of the Arch. Software Developer's Manual.\r
+  //\r
+  // Advance pointer to next bundle\r
+  //\r
+  Ptr += 16;\r
+  Code[0] = OPCODE_NOP;\r
+  Code[1] = OPCODE_NOP;\r
+  Code[2] = OPCODE_MOV_BX_RX;\r
+\r
+  //\r
+  // Pick a branch register to use. Then fill in the bits for the branch\r
+  // register and user register (same user register as previous bundle).\r
+  //\r
+  Br = 6;\r
+  Code[2] |= LShiftU64 (Br, 6);\r
+  Code[2] |= LShiftU64 (RegNum, 13);\r
+  WriteBundle ((VOID *) Ptr, 0x0d, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // *************************** NEXT BUNDLE *********************************\r
+  //\r
+  // Now do the branch:  (p0) br.cond.sptk.few b6\r
+  //\r
+  // Advance pointer to next bundle.\r
+  // Fill in the bits for the branch register (same reg as previous bundle)\r
+  //\r
+  Ptr += 16;\r
+  Code[0] = OPCODE_NOP;\r
+  Code[1] = OPCODE_NOP;\r
+  Code[2] = OPCODE_BR_COND_SPTK_FEW;\r
+  Code[2] |= LShiftU64 (Br, 13);\r
+  WriteBundle ((VOID *) Ptr, 0x1d, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // Add the thunk to our list of allocated thunks so we can do some cleanup\r
+  // when the image is unloaded. Do this last since the Add function flushes\r
+  // the instruction cache for us.\r
+  //\r
+  EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
+\r
+  //\r
+  // Done\r
+  //\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+WriteBundle (\r
+  IN    VOID    *MemPtr,\r
+  IN    UINT8   Template,\r
+  IN    UINT64  Slot0,\r
+  IN    UINT64  Slot1,\r
+  IN    UINT64  Slot2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Given raw bytes of Itanium based code, format them into a bundle and\r
+  write them out.\r
+  \r
+Arguments:\r
+\r
+  MemPtr    - pointer to memory location to write the bundles to\r
+  Template  - 5-bit template\r
+  Slot0-2   - instruction slot data for the bundle\r
+\r
+Returns:\r
+\r
+  EFI_INVALID_PARAMETER - Pointer is not aligned\r
+                        - No more than 5 bits in template\r
+                        - More than 41 bits used in code\r
+  EFI_SUCCESS           - All data is written.\r
+\r
+--*/\r
+{\r
+  UINT8   *BPtr;\r
+  UINT32  Index;\r
+  UINT64  Low64;\r
+  UINT64  High64;\r
+\r
+  //\r
+  // Verify pointer is aligned\r
+  //\r
+  if ((UINT64) MemPtr & 0xF) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Verify no more than 5 bits in template\r
+  //\r
+  if (Template &~0x1F) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Verify max of 41 bits used in code\r
+  //\r
+  if ((Slot0 | Slot1 | Slot2) &~0x1ffffffffff) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Low64   = LShiftU64 (Slot1, 46);\r
+  Low64   = Low64 | LShiftU64 (Slot0, 5) | Template;\r
+\r
+  High64  = RShiftU64 (Slot1, 18);\r
+  High64  = High64 | LShiftU64 (Slot2, 23);\r
+\r
+  //\r
+  // Now write it all out\r
+  //\r
+  BPtr = (UINT8 *) MemPtr;\r
+  for (Index = 0; Index < 8; Index++) {\r
+    *BPtr = (UINT8) Low64;\r
+    Low64 = RShiftU64 (Low64, 8);\r
+    BPtr++;\r
+  }\r
+\r
+  for (Index = 0; Index < 8; Index++) {\r
+    *BPtr   = (UINT8) High64;\r
+    High64  = RShiftU64 (High64, 8);\r
+    BPtr++;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\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
+  )\r
+/*++\r
+\r
+Routine Description:\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
+  code or a thunk to another piece of EBC code.\r
+  If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
+  otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
+  be startup which cost time and stack space.\r
+  \r
+Arguments:\r
+\r
+  VmPtr             - Pointer to a VM context.\r
+  FuncAddr          - Callee's address\r
+  NewStackPointer   - New stack pointer after the call\r
+  FramePtr          - New frame pointer after the call\r
+  Size              - The size of call instruction\r
+\r
+Returns:\r
+\r
+  None.\r
+  \r
+--*/\r
+{\r
+  UINTN    IsThunk;\r
+  UINTN    TargetEbcAddr;\r
+  UINTN    CodeOne18;\r
+  UINTN    CodeOne23;\r
+  UINTN    CodeTwoI;\r
+  UINTN    CodeTwoIc;\r
+  UINTN    CodeTwo7b;\r
+  UINTN    CodeTwo5c;\r
+  UINTN    CodeTwo9d;\r
+  UINTN    CalleeAddr;\r
+\r
+  IsThunk       = 1;\r
+  TargetEbcAddr = 0;\r
+\r
+  //\r
+  // FuncAddr points to the descriptor of the target instructions.\r
+  //\r
+  CalleeAddr = *((UINT64 *)FuncAddr);\r
+\r
+  //\r
+  // Processor specific code to check whether the callee is a thunk to EBC.\r
+  //\r
+  if (*((UINT64 *)CalleeAddr) != 0xBCCA000100000005) {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT64 *)CalleeAddr + 1) != 0x697623C1004A112E)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+\r
+  CodeOne18 = RShiftU64 (*((UINT64 *)CalleeAddr + 2), 46) & 0x3FFFF;\r
+  CodeOne23 = (*((UINT64 *)CalleeAddr + 3)) & 0x7FFFFF;\r
+  CodeTwoI  = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 59) & 0x1;\r
+  CodeTwoIc = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 44) & 0x1;\r
+  CodeTwo7b = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 36) & 0x7F;\r
+  CodeTwo5c = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 45) & 0x1F;\r
+  CodeTwo9d = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 50) & 0x1FF;\r
+\r
+  TargetEbcAddr = CodeTwo7b;\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwo9d, 7);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwo5c, 16);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwoIc, 21);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeOne18, 22);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeOne23, 40);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwoI, 63);\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
+    // 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->R[0] -= 8;\r
+    VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
+    VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (VmPtr->Ip + Size));\r
+\r
+    VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
+  } else {\r
+    //\r
+    // The callee is not a thunk to EBC, call native code.\r
+    //\r
+    EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
+\r
+    //\r
+    // Get return value and advance the IP.\r
+    //\r
+    VmPtr->R[7] = EbcLLGetReturnValue ();\r
+    VmPtr->Ip += Size;\r
+  }\r
+}\r