]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Ebc/Dxe/EbcExecute.c
Add EBC, FTW, Crc32SectionExtract, NullMemoryTest modules.
[mirror_edk2.git] / MdeModulePkg / Universal / Ebc / Dxe / EbcExecute.c
diff --git a/MdeModulePkg/Universal/Ebc/Dxe/EbcExecute.c b/MdeModulePkg/Universal/Ebc/Dxe/EbcExecute.c
new file mode 100644 (file)
index 0000000..174e774
--- /dev/null
@@ -0,0 +1,4564 @@
+/*++\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
+  EbcExecute.c\r
+\r
+Abstract:\r
+\r
+  Contains code that implements the virtual machine.\r
+\r
+--*/\r
+\r
+#include "EbcInt.h"\r
+#include "EbcExecute.h"\r
+\r
+\r
+//\r
+// Define some useful data size constants to allow switch statements based on\r
+// size of operands or data.\r
+//\r
+#define DATA_SIZE_INVALID 0\r
+#define DATA_SIZE_8       1\r
+#define DATA_SIZE_16      2\r
+#define DATA_SIZE_32      4\r
+#define DATA_SIZE_64      8\r
+#define DATA_SIZE_N       48  // 4 or 8\r
+//\r
+// Structure we'll use to dispatch opcodes to execute functions.\r
+//\r
+typedef struct {\r
+  EFI_STATUS (*ExecuteFunction) (IN VM_CONTEXT * VmPtr);\r
+}\r
+VM_TABLE_ENTRY;\r
+\r
+typedef\r
+UINT64\r
+(*DATA_MANIP_EXEC_FUNCTION) (\r
+  IN VM_CONTEXT * VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+INT16\r
+VmReadIndex16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     CodeOffset\r
+  );\r
+\r
+STATIC\r
+INT32\r
+VmReadIndex32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     CodeOffset\r
+  );\r
+\r
+STATIC\r
+INT64\r
+VmReadIndex64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     CodeOffset\r
+  );\r
+\r
+STATIC\r
+UINT8\r
+VmReadMem8 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+UINT16\r
+VmReadMem16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+UINT32\r
+VmReadMem32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+VmReadMem64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+UINTN\r
+VmReadMemN (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem8 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  UINTN         Addr,\r
+  IN UINT8      Data\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  UINTN         Addr,\r
+  IN UINT16     Data\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  UINTN         Addr,\r
+  IN UINT32     Data\r
+  );\r
+\r
+STATIC\r
+UINT16\r
+VmReadCode16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+UINT32\r
+VmReadCode32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+VmReadCode64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+INT8\r
+VmReadImmed8 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+INT16\r
+VmReadImmed16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+INT32\r
+VmReadImmed32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+INT64\r
+VmReadImmed64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+UINTN\r
+ConvertStackAddr (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteDataManip (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN BOOLEAN      IsSignedOperation\r
+  );\r
+\r
+//\r
+// Functions that execute VM opcodes\r
+//\r
+STATIC\r
+EFI_STATUS\r
+ExecuteBREAK (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteJMP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteJMP8 (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCALL (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteRET (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCMP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCMPI (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVxx (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVI (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVIn (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVREL (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePUSHn (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePUSH (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePOPn (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePOP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteSignedDataManip (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteUnsignedDataManip (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteLOADSP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteSTORESP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVsnd (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVsnw (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+//\r
+// Data manipulation subfunctions\r
+//\r
+STATIC\r
+UINT64\r
+ExecuteNOT (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteNEG (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteADD (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSUB (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMUL (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMULU (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteDIV (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteDIVU (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMOD (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMODU (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteAND (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteOR (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteXOR (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSHL (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSHR (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteASHR (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDB (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDW (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDD (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+//\r
+// Once we retrieve the operands for the data manipulation instructions,\r
+// call these functions to perform the operation.\r
+//\r
+static CONST DATA_MANIP_EXEC_FUNCTION mDataManipDispatchTable[] = {\r
+  ExecuteNOT,\r
+  ExecuteNEG,\r
+  ExecuteADD,\r
+  ExecuteSUB,\r
+  ExecuteMUL,\r
+  ExecuteMULU,\r
+  ExecuteDIV,\r
+  ExecuteDIVU,\r
+  ExecuteMOD,\r
+  ExecuteMODU,\r
+  ExecuteAND,\r
+  ExecuteOR,\r
+  ExecuteXOR,\r
+  ExecuteSHL,\r
+  ExecuteSHR,\r
+  ExecuteASHR,\r
+  ExecuteEXTNDB,\r
+  ExecuteEXTNDW,\r
+  ExecuteEXTNDD,\r
+};\r
+\r
+static CONST VM_TABLE_ENTRY           mVmOpcodeTable[] = {\r
+  { ExecuteBREAK },             // opcode 0x00\r
+  { ExecuteJMP },               // opcode 0x01\r
+  { ExecuteJMP8 },              // opcode 0x02\r
+  { ExecuteCALL },              // opcode 0x03\r
+  { ExecuteRET },               // opcode 0x04\r
+  { ExecuteCMP },               // opcode 0x05 CMPeq\r
+  { ExecuteCMP },               // opcode 0x06 CMPlte\r
+  { ExecuteCMP },               // opcode 0x07 CMPgte\r
+  { ExecuteCMP },               // opcode 0x08 CMPulte\r
+  { ExecuteCMP },               // opcode 0x09 CMPugte\r
+  { ExecuteUnsignedDataManip }, // opcode 0x0A NOT\r
+  { ExecuteSignedDataManip },   // opcode 0x0B NEG\r
+  { ExecuteSignedDataManip },   // opcode 0x0C ADD\r
+  { ExecuteSignedDataManip },   // opcode 0x0D SUB\r
+  { ExecuteSignedDataManip },   // opcode 0x0E MUL\r
+  { ExecuteUnsignedDataManip }, // opcode 0x0F MULU\r
+  { ExecuteSignedDataManip },   // opcode 0x10 DIV\r
+  { ExecuteUnsignedDataManip }, // opcode 0x11 DIVU\r
+  { ExecuteSignedDataManip },   // opcode 0x12 MOD\r
+  { ExecuteUnsignedDataManip }, // opcode 0x13 MODU\r
+  { ExecuteUnsignedDataManip }, // opcode 0x14 AND\r
+  { ExecuteUnsignedDataManip }, // opcode 0x15 OR\r
+  { ExecuteUnsignedDataManip }, // opcode 0x16 XOR\r
+  { ExecuteUnsignedDataManip }, // opcode 0x17 SHL\r
+  { ExecuteUnsignedDataManip }, // opcode 0x18 SHR\r
+  { ExecuteSignedDataManip },   // opcode 0x19 ASHR\r
+  { ExecuteUnsignedDataManip }, // opcode 0x1A EXTNDB\r
+  { ExecuteUnsignedDataManip }, // opcode 0x1B EXTNDW\r
+  { ExecuteUnsignedDataManip }, // opcode 0x1C EXTNDD\r
+  { ExecuteMOVxx },             // opcode 0x1D MOVBW\r
+  { ExecuteMOVxx },             // opcode 0x1E MOVWW\r
+  { ExecuteMOVxx },             // opcode 0x1F MOVDW\r
+  { ExecuteMOVxx },             // opcode 0x20 MOVQW\r
+  { ExecuteMOVxx },             // opcode 0x21 MOVBD\r
+  { ExecuteMOVxx },             // opcode 0x22 MOVWD\r
+  { ExecuteMOVxx },             // opcode 0x23 MOVDD\r
+  { ExecuteMOVxx },             // opcode 0x24 MOVQD\r
+  { ExecuteMOVsnw },            // opcode 0x25 MOVsnw\r
+  { ExecuteMOVsnd },            // opcode 0x26 MOVsnd\r
+  { NULL },                     // opcode 0x27\r
+  { ExecuteMOVxx },             // opcode 0x28 MOVqq\r
+  { ExecuteLOADSP },            // opcode 0x29 LOADSP SP1, R2\r
+  { ExecuteSTORESP },           // opcode 0x2A STORESP R1, SP2\r
+  { ExecutePUSH },              // opcode 0x2B PUSH {@}R1 [imm16]\r
+  { ExecutePOP },               // opcode 0x2C POP {@}R1 [imm16]\r
+  { ExecuteCMPI },              // opcode 0x2D CMPIEQ\r
+  { ExecuteCMPI },              // opcode 0x2E CMPILTE\r
+  { ExecuteCMPI },              // opcode 0x2F CMPIGTE\r
+  { ExecuteCMPI },              // opcode 0x30 CMPIULTE\r
+  { ExecuteCMPI },              // opcode 0x31 CMPIUGTE\r
+  { ExecuteMOVxx },             // opcode 0x32 MOVN\r
+  { ExecuteMOVxx },             // opcode 0x33 MOVND\r
+  { NULL },                     // opcode 0x34\r
+  { ExecutePUSHn },             // opcode 0x35\r
+  { ExecutePOPn },              // opcode 0x36\r
+  { ExecuteMOVI },              // opcode 0x37 - mov immediate data\r
+  { ExecuteMOVIn },             // opcode 0x38 - mov immediate natural\r
+  { ExecuteMOVREL }             // opcode 0x39 - move data relative to PC\r
+};\r
+\r
+//\r
+// Length of JMP instructions, depending on upper two bits of opcode.\r
+//\r
+static CONST UINT8                    mJMPLen[] = { 2, 2, 6, 10 };\r
+\r
+//\r
+// Simple Debugger Protocol GUID\r
+//\r
+EFI_GUID mEbcSimpleDebuggerProtocolGuid = EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID;\r
+\r
+EFI_STATUS\r
+EbcExecuteInstructions (\r
+  IN EFI_EBC_VM_TEST_PROTOCOL *This,\r
+  IN VM_CONTEXT               *VmPtr,\r
+  IN OUT UINTN                *InstructionCount\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Given a pointer to a new VM context, execute one or more instructions. This\r
+  function is only used for test purposes via the EBC VM test protocol.\r
+\r
+Arguments:\r
+\r
+  This              - pointer to protocol interface\r
+  VmPtr             - pointer to a VM context\r
+  InstructionCount  - how many instructions to execute. 0 if don't count.\r
+\r
+Returns:\r
+\r
+  EFI_UNSUPPORTED\r
+  EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  UINTN       ExecFunc;\r
+  EFI_STATUS  Status;\r
+  UINTN       InstructionsLeft;\r
+  UINTN       SavedInstructionCount;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  if (*InstructionCount == 0) {\r
+    InstructionsLeft = 1;\r
+  } else {\r
+    InstructionsLeft = *InstructionCount;\r
+  }\r
+\r
+  SavedInstructionCount = *InstructionCount;\r
+  *InstructionCount     = 0;\r
+\r
+  //\r
+  // Index into the opcode table using the opcode byte for this instruction.\r
+  // This gives you the execute function, which we first test for null, then\r
+  // call it if it's not null.\r
+  //\r
+  while (InstructionsLeft != 0) {\r
+    ExecFunc = (UINTN) mVmOpcodeTable[(*VmPtr->Ip & 0x3F)].ExecuteFunction;\r
+    if (ExecFunc == (UINTN) NULL) {\r
+      EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      return EFI_UNSUPPORTED;\r
+    } else {\r
+      mVmOpcodeTable[(*VmPtr->Ip & 0x3F)].ExecuteFunction (VmPtr);\r
+      *InstructionCount = *InstructionCount + 1;\r
+    }\r
+\r
+    //\r
+    // Decrement counter if applicable\r
+    //\r
+    if (SavedInstructionCount != 0) {\r
+      InstructionsLeft--;\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+EbcExecute (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute an EBC image from an entry point or from a published protocol.\r
+\r
+Arguments:\r
+\r
+  VmPtr - pointer to prepared VM context.\r
+\r
+Returns:\r
+\r
+  Standard EBC status.\r
+\r
+--*/\r
+{\r
+  UINTN                             ExecFunc;\r
+  UINT8                             StackCorrupted;\r
+  EFI_STATUS                        Status;\r
+  EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL  *EbcSimpleDebugger;\r
+\r
+  mVmPtr            = VmPtr;\r
+  EbcSimpleDebugger = NULL;\r
+  Status            = EFI_SUCCESS;\r
+  StackCorrupted    = 0;\r
+\r
+  //\r
+  // Make sure the magic value has been put on the stack before we got here.\r
+  //\r
+  if (*VmPtr->StackMagicPtr != (UINTN) VM_STACK_KEY_VALUE) {\r
+    StackCorrupted = 1;\r
+  }\r
+\r
+  VmPtr->FramePtr = (VOID *) ((UINT8 *) (UINTN) VmPtr->R[0] + 8);\r
+\r
+  //\r
+  // Try to get the debug support for EBC\r
+  //\r
+  DEBUG_CODE_BEGIN ();\r
+    Status = gBS->LocateProtocol (\r
+                    &mEbcSimpleDebuggerProtocolGuid,\r
+                    NULL,\r
+                    (VOID **) &EbcSimpleDebugger\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      EbcSimpleDebugger = NULL;\r
+    }\r
+  DEBUG_CODE_END ();\r
+\r
+  //\r
+  // Save the start IP for debug. For example, if we take an exception we\r
+  // can print out the location of the exception relative to the entry point,\r
+  // which could then be used in a disassembly listing to find the problem.\r
+  //\r
+  VmPtr->EntryPoint = (VOID *) VmPtr->Ip;\r
+\r
+  //\r
+  // We'll wait for this flag to know when we're done. The RET\r
+  // instruction sets it if it runs out of stack.\r
+  //\r
+  VmPtr->StopFlags = 0;\r
+  while (!(VmPtr->StopFlags & STOPFLAG_APP_DONE)) {\r
+    //\r
+    // If we've found a simple debugger protocol, call it\r
+    //\r
+    DEBUG_CODE_BEGIN ();\r
+      if (EbcSimpleDebugger != NULL) {\r
+        EbcSimpleDebugger->Debugger (EbcSimpleDebugger, VmPtr);\r
+      }\r
+    DEBUG_CODE_END ();\r
+\r
+    //\r
+    // Verify the opcode is in range. Otherwise generate an exception.\r
+    //\r
+    if ((*VmPtr->Ip & OPCODE_M_OPCODE) >= (sizeof (mVmOpcodeTable) / sizeof (mVmOpcodeTable[0]))) {\r
+      EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      Status = EFI_UNSUPPORTED;\r
+      goto Done;\r
+    }\r
+    //\r
+    // Use the opcode bits to index into the opcode dispatch table. If the\r
+    // function pointer is null then generate an exception.\r
+    //\r
+    ExecFunc = (UINTN) mVmOpcodeTable[(*VmPtr->Ip & OPCODE_M_OPCODE)].ExecuteFunction;\r
+    if (ExecFunc == (UINTN) NULL) {\r
+      EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      Status = EFI_UNSUPPORTED;\r
+      goto Done;\r
+    }\r
+    //\r
+    // The EBC VM is a strongly ordered processor, so perform a fence operation before\r
+    // and after each instruction is executed.\r
+    //\r
+    MemoryFence ();\r
+\r
+    mVmOpcodeTable[(*VmPtr->Ip & OPCODE_M_OPCODE)].ExecuteFunction (VmPtr);\r
+\r
+    MemoryFence ();\r
+\r
+    //\r
+    // If the step flag is set, signal an exception and continue. We don't\r
+    // clear it here. Assuming the debugger is responsible for clearing it.\r
+    //\r
+    if (VMFLAG_ISSET (VmPtr, VMFLAGS_STEP)) {\r
+      EbcDebugSignalException (EXCEPT_EBC_STEP, EXCEPTION_FLAG_NONE, VmPtr);\r
+    }\r
+    //\r
+    // Make sure stack has not been corrupted. Only report it once though.\r
+    //\r
+    if (!StackCorrupted && (*VmPtr->StackMagicPtr != (UINTN) VM_STACK_KEY_VALUE)) {\r
+      EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      StackCorrupted = 1;\r
+    }\r
+    if (!StackCorrupted && ((UINT64)VmPtr->R[0] <= (UINT64)(UINTN) VmPtr->StackTop)) {\r
+      EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      StackCorrupted = 1;\r
+    }\r
+  }\r
+\r
+Done:\r
+  mVmPtr          = NULL;\r
+\r
+  return Status;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVxx (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the MOVxx instructions.\r
+\r
+Arguments:\r
+\r
+  VmPtr - pointer to a VM context.\r
+\r
+Returns:\r
+\r
+  EFI_UNSUPPORTED\r
+  EFI_SUCCESS\r
+\r
+Instruction format:\r
+  \r
+  MOV[b|w|d|q|n]{w|d} {@}R1 {Index16|32}, {@}R2 {Index16|32}\r
+  MOVqq {@}R1 {Index64}, {@}R2 {Index64}\r
+\r
+  Copies contents of [R2] -> [R1], zero extending where required.\r
+\r
+  First character indicates the size of the move.\r
+  Second character indicates the size of the index(s).\r
+\r
+  Invalid to have R1 direct with index.\r
+  \r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   OpcMasked;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  UINT8   MoveSize;\r
+  INT16   Index16;\r
+  INT32   Index32;\r
+  INT64   Index64Op1;\r
+  INT64   Index64Op2;\r
+  UINT64  Data64;\r
+  UINT64  DataMask;\r
+  UINTN   Source;\r
+\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  OpcMasked = (UINT8) (Opcode & OPCODE_M_OPCODE);\r
+\r
+  //\r
+  // Get the operands byte so we can get R1 and R2\r
+  //\r
+  Operands = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Assume no indexes\r
+  //\r
+  Index64Op1  = 0;\r
+  Index64Op2  = 0;\r
+  Data64      = 0;\r
+\r
+  //\r
+  // Determine if we have an index/immediate data. Base instruction size\r
+  // is 2 (opcode + operands). Add to this size each index specified.\r
+  //\r
+  Size = 2;\r
+  if (Opcode & (OPCODE_M_IMMED_OP1 | OPCODE_M_IMMED_OP2)) {\r
+    //\r
+    // Determine size of the index from the opcode. Then get it.\r
+    //\r
+    if ((OpcMasked <= OPCODE_MOVQW) || (OpcMasked == OPCODE_MOVNW)) {\r
+      //\r
+      // MOVBW, MOVWW, MOVDW, MOVQW, and MOVNW have 16-bit immediate index.\r
+      // Get one or both index values.\r
+      //\r
+      if (Opcode & OPCODE_M_IMMED_OP1) {\r
+        Index16     = VmReadIndex16 (VmPtr, 2);\r
+        Index64Op1  = (INT64) Index16;\r
+        Size += sizeof (UINT16);\r
+      }\r
+\r
+      if (Opcode & OPCODE_M_IMMED_OP2) {\r
+        Index16     = VmReadIndex16 (VmPtr, Size);\r
+        Index64Op2  = (INT64) Index16;\r
+        Size += sizeof (UINT16);\r
+      }\r
+    } else if ((OpcMasked <= OPCODE_MOVQD) || (OpcMasked == OPCODE_MOVND)) {\r
+      //\r
+      // MOVBD, MOVWD, MOVDD, MOVQD, and MOVND have 32-bit immediate index\r
+      //\r
+      if (Opcode & OPCODE_M_IMMED_OP1) {\r
+        Index32     = VmReadIndex32 (VmPtr, 2);\r
+        Index64Op1  = (INT64) Index32;\r
+        Size += sizeof (UINT32);\r
+      }\r
+\r
+      if (Opcode & OPCODE_M_IMMED_OP2) {\r
+        Index32     = VmReadIndex32 (VmPtr, Size);\r
+        Index64Op2  = (INT64) Index32;\r
+        Size += sizeof (UINT32);\r
+      }\r
+    } else if (OpcMasked == OPCODE_MOVQQ) {\r
+      //\r
+      // MOVqq -- only form with a 64-bit index\r
+      //\r
+      if (Opcode & OPCODE_M_IMMED_OP1) {\r
+        Index64Op1 = VmReadIndex64 (VmPtr, 2);\r
+        Size += sizeof (UINT64);\r
+      }\r
+\r
+      if (Opcode & OPCODE_M_IMMED_OP2) {\r
+        Index64Op2 = VmReadIndex64 (VmPtr, Size);\r
+        Size += sizeof (UINT64);\r
+      }\r
+    } else {\r
+      //\r
+      // Obsolete MOVBQ, MOVWQ, MOVDQ, and MOVNQ have 64-bit immediate index\r
+      //\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+  }\r
+  //\r
+  // Determine the size of the move, and create a mask for it so we can\r
+  // clear unused bits.\r
+  //\r
+  if ((OpcMasked == OPCODE_MOVBW) || (OpcMasked == OPCODE_MOVBD)) {\r
+    MoveSize  = DATA_SIZE_8;\r
+    DataMask  = 0xFF;\r
+  } else if ((OpcMasked == OPCODE_MOVWW) || (OpcMasked == OPCODE_MOVWD)) {\r
+    MoveSize  = DATA_SIZE_16;\r
+    DataMask  = 0xFFFF;\r
+  } else if ((OpcMasked == OPCODE_MOVDW) || (OpcMasked == OPCODE_MOVDD)) {\r
+    MoveSize  = DATA_SIZE_32;\r
+    DataMask  = 0xFFFFFFFF;\r
+  } else if ((OpcMasked == OPCODE_MOVQW) || (OpcMasked == OPCODE_MOVQD) || (OpcMasked == OPCODE_MOVQQ)) {\r
+    MoveSize  = DATA_SIZE_64;\r
+    DataMask  = (UINT64)~0;\r
+  } else if ((OpcMasked == OPCODE_MOVNW) || (OpcMasked == OPCODE_MOVND)) {\r
+    MoveSize  = DATA_SIZE_N;\r
+    DataMask  = (UINT64)~0 >> (64 - 8 * sizeof (UINTN));\r
+  } else {\r
+    //\r
+    // We were dispatched to this function and we don't recognize the opcode\r
+    //\r
+    EbcDebugSignalException (EXCEPT_EBC_UNDEFINED, EXCEPTION_FLAG_FATAL, VmPtr);\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Now get the source address\r
+  //\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    //\r
+    // Indirect form @R2. Compute address of operand2\r
+    //\r
+    Source = (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index64Op2);\r
+    //\r
+    // Now get the data from the source. Always 0-extend and let the compiler\r
+    // sign-extend where required.\r
+    //\r
+    switch (MoveSize) {\r
+    case DATA_SIZE_8:\r
+      Data64 = (UINT64) (UINT8) VmReadMem8 (VmPtr, Source);\r
+      break;\r
+\r
+    case DATA_SIZE_16:\r
+      Data64 = (UINT64) (UINT16) VmReadMem16 (VmPtr, Source);\r
+      break;\r
+\r
+    case DATA_SIZE_32:\r
+      Data64 = (UINT64) (UINT32) VmReadMem32 (VmPtr, Source);\r
+      break;\r
+\r
+    case DATA_SIZE_64:\r
+      Data64 = (UINT64) VmReadMem64 (VmPtr, Source);\r
+      break;\r
+\r
+    case DATA_SIZE_N:\r
+      Data64 = (UINT64) (UINTN) VmReadMemN (VmPtr, Source);\r
+      break;\r
+\r
+    default:\r
+      //\r
+      // not reached\r
+      //\r
+      break;\r
+    }\r
+  } else {\r
+    //\r
+    // Not indirect source: MOVxx {@}Rx, Ry [Index]\r
+    //\r
+    Data64 = VmPtr->R[OPERAND2_REGNUM (Operands)] + Index64Op2;\r
+    //\r
+    // Did Operand2 have an index? If so, treat as two signed values since\r
+    // indexes are signed values.\r
+    //\r
+    if (Opcode & OPCODE_M_IMMED_OP2) {\r
+      //\r
+      // NOTE: need to find a way to fix this, most likely by changing the VM\r
+      // implementation to remove the stack gap. To do that, we'd need to\r
+      // allocate stack space for the VM and actually set the system\r
+      // stack pointer to the allocated buffer when the VM starts.\r
+      //\r
+      // Special case -- if someone took the address of a function parameter\r
+      // then we need to make sure it's not in the stack gap. We can identify\r
+      // this situation if (Operand2 register == 0) && (Operand2 is direct)\r
+      // && (Index applies to Operand2) && (Index > 0) && (Operand1 register != 0)\r
+      // Situations that to be aware of:\r
+      //   * stack adjustments at beginning and end of functions R0 = R0 += stacksize\r
+      //\r
+      if ((OPERAND2_REGNUM (Operands) == 0) &&\r
+          (!OPERAND2_INDIRECT (Operands)) &&\r
+          (Index64Op2 > 0) &&\r
+          (OPERAND1_REGNUM (Operands) == 0) &&\r
+          (OPERAND1_INDIRECT (Operands))\r
+          ) {\r
+        Data64 = (UINT64) ConvertStackAddr (VmPtr, (UINTN) (INT64) Data64);\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Now write it back\r
+  //\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Reuse the Source variable to now be dest.\r
+    //\r
+    Source = (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index64Op1);\r
+    //\r
+    // Do the write based on the size\r
+    //\r
+    switch (MoveSize) {\r
+    case DATA_SIZE_8:\r
+      VmWriteMem8 (VmPtr, Source, (UINT8) Data64);\r
+      break;\r
+\r
+    case DATA_SIZE_16:\r
+      VmWriteMem16 (VmPtr, Source, (UINT16) Data64);\r
+      break;\r
+\r
+    case DATA_SIZE_32:\r
+      VmWriteMem32 (VmPtr, Source, (UINT32) Data64);\r
+      break;\r
+\r
+    case DATA_SIZE_64:\r
+      VmWriteMem64 (VmPtr, Source, Data64);\r
+      break;\r
+\r
+    case DATA_SIZE_N:\r
+      VmWriteMemN (VmPtr, Source, (UINTN) Data64);\r
+      break;\r
+\r
+    default:\r
+      //\r
+      // not reached\r
+      //\r
+      break;\r
+    }\r
+  } else {\r
+    //\r
+    // Operand1 direct.\r
+    // Make sure we didn't have an index on operand1.\r
+    //\r
+    if (Opcode & OPCODE_M_IMMED_OP1) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+    //\r
+    // Direct storage in register. Clear unused bits and store back to\r
+    // register.\r
+    //\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = Data64 & DataMask;\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteBREAK (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC BREAK instruction\r
+\r
+Arguments:\r
+\r
+  VmPtr - pointer to current VM context\r
+\r
+Returns:\r
+\r
+  EFI_UNSUPPORTED\r
+  EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  UINT8       Operands;\r
+  VOID        *EbcEntryPoint;\r
+  VOID        *Thunk;\r
+  UINT64      U64EbcEntryPoint;\r
+  INT32       Offset;\r
+\r
+  Operands = GETOPERANDS (VmPtr);\r
+  switch (Operands) {\r
+  //\r
+  // Runaway program break. Generate an exception and terminate\r
+  //\r
+  case 0:\r
+    EbcDebugSignalException (EXCEPT_EBC_BAD_BREAK, EXCEPTION_FLAG_FATAL, VmPtr);\r
+    break;\r
+\r
+  //\r
+  // Get VM version -- return VM revision number in R7\r
+  //\r
+  case 1:\r
+    //\r
+    // Bits:\r
+    //  63-17 = 0\r
+    //  16-8  = Major version\r
+    //  7-0   = Minor version\r
+    //\r
+    VmPtr->R[7] = GetVmVersion ();\r
+    break;\r
+\r
+  //\r
+  // Debugger breakpoint\r
+  //\r
+  case 3:\r
+    VmPtr->StopFlags |= STOPFLAG_BREAKPOINT;\r
+    //\r
+    // See if someone has registered a handler\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_BREAKPOINT,\r
+      EXCEPTION_FLAG_NONE,\r
+      VmPtr\r
+      );\r
+    break;\r
+\r
+  //\r
+  // System call, which there are none, so NOP it.\r
+  //\r
+  case 4:\r
+    break;\r
+\r
+  //\r
+  // Create a thunk for EBC code. R7 points to a 32-bit (in a 64-bit slot)\r
+  // "offset from self" pointer to the EBC entry point.\r
+  // After we're done, *(UINT64 *)R7 will be the address of the new thunk.\r
+  //\r
+  case 5:\r
+    Offset            = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->R[7]);\r
+    U64EbcEntryPoint  = (UINT64) (VmPtr->R[7] + Offset + 4);\r
+    EbcEntryPoint     = (VOID *) (UINTN) U64EbcEntryPoint;\r
+\r
+    //\r
+    // Now create a new thunk\r
+    //\r
+    EbcCreateThunks (VmPtr->ImageHandle, EbcEntryPoint, &Thunk, 0);\r
+\r
+    //\r
+    // Finally replace the EBC entry point memory with the thunk address\r
+    //\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[7], (UINT64) (UINTN) Thunk);\r
+    break;\r
+\r
+  //\r
+  // Compiler setting version per value in R7\r
+  //\r
+  case 6:\r
+    VmPtr->CompilerVersion = (UINT32) VmPtr->R[7];\r
+    //\r
+    // Check compiler version against VM version?\r
+    //\r
+    break;\r
+\r
+  //\r
+  // Unhandled break code. Signal exception.\r
+  //\r
+  default:\r
+    EbcDebugSignalException (EXCEPT_EBC_BAD_BREAK, EXCEPTION_FLAG_FATAL, VmPtr);\r
+    break;\r
+  }\r
+  //\r
+  // Advance IP\r
+  //\r
+  VmPtr->Ip += 2;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteJMP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the JMP instruction\r
+\r
+Arguments:\r
+  VmPtr      - pointer to VM context\r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  JMP64{cs|cc} Immed64\r
+  JMP32{cs|cc} {@}R1 {Immed32|Index32}\r
+\r
+Encoding:\r
+  b0.7 -  immediate data present\r
+  b0.6 -  1 = 64 bit immediate data\r
+          0 = 32 bit immediate data\r
+  b1.7 -  1 = conditional\r
+  b1.6    1 = CS (condition set)\r
+          0 = CC (condition clear)\r
+  b1.4    1 = relative address\r
+          0 = absolute address\r
+  b1.3    1 = operand1 indirect\r
+  b1.2-0  operand 1\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   CompareSet;\r
+  UINT8   ConditionFlag;\r
+  UINT8   Size;\r
+  UINT8   Operand;\r
+  UINT64  Data64;\r
+  INT32   Index32;\r
+  UINTN   Addr;\r
+\r
+  Operand = GETOPERANDS (VmPtr);\r
+  Opcode  = GETOPCODE (VmPtr);\r
+\r
+  //\r
+  // Get instruction length from the opcode. The upper two bits are used here\r
+  // to index into the length array.\r
+  //\r
+  Size = mJMPLen[(Opcode >> 6) & 0x03];\r
+\r
+  //\r
+  // Decode instruction conditions\r
+  // If we haven't met the condition, then simply advance the IP and return.\r
+  //\r
+  CompareSet    = (UINT8) ((Operand & JMP_M_CS) ? 1 : 0);\r
+  ConditionFlag = (UINT8) VMFLAG_ISSET (VmPtr, VMFLAGS_CC);\r
+  if (Operand & CONDITION_M_CONDITIONAL) {\r
+    if (CompareSet != ConditionFlag) {\r
+      VmPtr->Ip += Size;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+  //\r
+  // Check for 64-bit form and do it right away since it's the most\r
+  // straight-forward form.\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA64) {\r
+    //\r
+    // Double check for immediate-data, which is required. If not there,\r
+    // then signal an exception\r
+    //\r
+    if (!(Opcode & OPCODE_M_IMMDATA)) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_ERROR,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+    //\r
+    // 64-bit immediate data is full address. Read the immediate data,\r
+    // check for alignment, and jump absolute.\r
+    //\r
+    Data64 = VmReadImmed64 (VmPtr, 2);\r
+    if (!IS_ALIGNED ((UINTN) Data64, sizeof (UINT16))) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_ALIGNMENT_CHECK,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    //\r
+    // Take jump -- relative or absolute\r
+    //\r
+    if (Operand & JMP_M_RELATIVE) {\r
+      VmPtr->Ip += (UINTN) Data64 + Size;\r
+    } else {\r
+      VmPtr->Ip = (VMIP) (UINTN) Data64;\r
+    }\r
+\r
+    return EFI_SUCCESS;\r
+  }\r
+  //\r
+  // 32-bit forms:\r
+  // Get the index if there is one. May be either an index, or an immediate\r
+  // offset depending on indirect operand.\r
+  //   JMP32 @R1 Index32 -- immediate data is an index\r
+  //   JMP32 R1 Immed32  -- immedate data is an offset\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operand)) {\r
+      Index32 = VmReadIndex32 (VmPtr, 2);\r
+    } else {\r
+      Index32 = VmReadImmed32 (VmPtr, 2);\r
+    }\r
+  } else {\r
+    Index32 = 0;\r
+  }\r
+  //\r
+  // Get the register data. If R == 0, then special case where it's ignored.\r
+  //\r
+  if (OPERAND1_REGNUM (Operand) == 0) {\r
+    Data64 = 0;\r
+  } else {\r
+    Data64 = OPERAND1_REGDATA (VmPtr, Operand);\r
+  }\r
+  //\r
+  // Decode the forms\r
+  //\r
+  if (OPERAND1_INDIRECT (Operand)) {\r
+    //\r
+    // Form: JMP32 @Rx {Index32}\r
+    //\r
+    Addr = VmReadMemN (VmPtr, (UINTN) Data64 + Index32);\r
+    if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_ALIGNMENT_CHECK,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    if (Operand & JMP_M_RELATIVE) {\r
+      VmPtr->Ip += (UINTN) Addr + Size;\r
+    } else {\r
+      VmPtr->Ip = (VMIP) Addr;\r
+    }\r
+  } else {\r
+    //\r
+    // Form: JMP32 Rx {Immed32}\r
+    //\r
+    Addr = (UINTN) (Data64 + Index32);\r
+    if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_ALIGNMENT_CHECK,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    if (Operand & JMP_M_RELATIVE) {\r
+      VmPtr->Ip += (UINTN) Addr + Size;\r
+    } else {\r
+      VmPtr->Ip = (VMIP) Addr;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteJMP8 (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC JMP8 instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  JMP8{cs|cc}  Offset/2\r
+\r
+--*/\r
+{\r
+  UINT8 Opcode;\r
+  UINT8 ConditionFlag;\r
+  UINT8 CompareSet;\r
+  INT8  Offset;\r
+\r
+  //\r
+  // Decode instruction.\r
+  //\r
+  Opcode        = GETOPCODE (VmPtr);\r
+  CompareSet    = (UINT8) ((Opcode & JMP_M_CS) ? 1 : 0);\r
+  ConditionFlag = (UINT8) VMFLAG_ISSET (VmPtr, VMFLAGS_CC);\r
+\r
+  //\r
+  // If we haven't met the condition, then simply advance the IP and return\r
+  //\r
+  if (Opcode & CONDITION_M_CONDITIONAL) {\r
+    if (CompareSet != ConditionFlag) {\r
+      VmPtr->Ip += 2;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+  //\r
+  // Get the offset from the instruction stream. It's relative to the\r
+  // following instruction, and divided by 2.\r
+  //\r
+  Offset = VmReadImmed8 (VmPtr, 1);\r
+  //\r
+  // Want to check for offset == -2 and then raise an exception?\r
+  //\r
+  VmPtr->Ip += (Offset * 2) + 2;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVI (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOVI \r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVI[b|w|d|q][w|d|q] {@}R1 {Index16}, ImmData16|32|64\r
+\r
+  First variable character specifies the move size\r
+  Second variable character specifies size of the immediate data\r
+\r
+  Sign-extend the immediate data to the size of the operation, and zero-extend\r
+  if storing to a register.\r
+\r
+  Operand1 direct with index/immed is invalid.\r
+    \r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Index16;\r
+  INT64   ImmData64;\r
+  UINT64  Op1;\r
+  UINT64  Mask64;\r
+\r
+  //\r
+  // Get the opcode and operands byte so we can get R1 and R2\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get the index (16-bit) if present\r
+  //\r
+  if (Operands & MOVI_M_IMMDATA) {\r
+    Index16 = VmReadIndex16 (VmPtr, 2);\r
+    Size    = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Extract the immediate data. Sign-extend always.\r
+  //\r
+  if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {\r
+    ImmData64 = (INT64) (INT16) VmReadImmed16 (VmPtr, Size);\r
+    Size += 2;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {\r
+    ImmData64 = (INT64) (INT32) VmReadImmed32 (VmPtr, Size);\r
+    Size += 4;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {\r
+    ImmData64 = (INT64) VmReadImmed64 (VmPtr, Size);\r
+    Size += 8;\r
+  } else {\r
+    //\r
+    // Invalid encoding\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Now write back the result\r
+  //\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Operand1 direct. Make sure it didn't have an index.\r
+    //\r
+    if (Operands & MOVI_M_IMMDATA) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+    //\r
+    // Writing directly to a register. Clear unused bits.\r
+    //\r
+    if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH8) {\r
+      Mask64 = 0x000000FF;\r
+    } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH16) {\r
+      Mask64 = 0x0000FFFF;\r
+    } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH32) {\r
+      Mask64 = 0x00000000FFFFFFFF;\r
+    } else {\r
+      Mask64 = (UINT64)~0;\r
+    }\r
+\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = ImmData64 & Mask64;\r
+  } else {\r
+    //\r
+    // Get the address then write back based on size of the move\r
+    //\r
+    Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH8) {\r
+      VmWriteMem8 (VmPtr, (UINTN) Op1, (UINT8) ImmData64);\r
+    } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH16) {\r
+      VmWriteMem16 (VmPtr, (UINTN) Op1, (UINT16) ImmData64);\r
+    } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH32) {\r
+      VmWriteMem32 (VmPtr, (UINTN) Op1, (UINT32) ImmData64);\r
+    } else {\r
+      VmWriteMem64 (VmPtr, (UINTN) Op1, ImmData64);\r
+    }\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVIn (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOV immediate natural. This instruction moves an immediate\r
+  index value into a register or memory location.\r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVIn[w|d|q] {@}R1 {Index16}, Index16|32|64\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Index16;\r
+  INT16   ImmedIndex16;\r
+  INT32   ImmedIndex32;\r
+  INT64   ImmedIndex64;\r
+  UINT64  Op1;\r
+\r
+  //\r
+  // Get the opcode and operands byte so we can get R1 and R2\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get the operand1 index (16-bit) if present\r
+  //\r
+  if (Operands & MOVI_M_IMMDATA) {\r
+    Index16 = VmReadIndex16 (VmPtr, 2);\r
+    Size    = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Extract the immediate data and convert to a 64-bit index.\r
+  //\r
+  if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {\r
+    ImmedIndex16  = VmReadIndex16 (VmPtr, Size);\r
+    ImmedIndex64  = (INT64) ImmedIndex16;\r
+    Size += 2;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {\r
+    ImmedIndex32  = VmReadIndex32 (VmPtr, Size);\r
+    ImmedIndex64  = (INT64) ImmedIndex32;\r
+    Size += 4;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {\r
+    ImmedIndex64 = VmReadIndex64 (VmPtr, Size);\r
+    Size += 8;\r
+  } else {\r
+    //\r
+    // Invalid encoding\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Now write back the result\r
+  //\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Check for MOVIn R1 Index16, Immed (not indirect, with index), which\r
+    // is illegal\r
+    //\r
+    if (Operands & MOVI_M_IMMDATA) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = ImmedIndex64;\r
+  } else {\r
+    //\r
+    // Get the address\r
+    //\r
+    Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    VmWriteMemN (VmPtr, (UINTN) Op1, (INTN) ImmedIndex64);\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVREL (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOVREL instruction.\r
+  Dest <- Ip + ImmData\r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVREL[w|d|q] {@}R1 {Index16}, ImmData16|32|64\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Index16;\r
+  INT64   ImmData64;\r
+  UINT64  Op1;\r
+  UINT64  Op2;\r
+\r
+  //\r
+  // Get the opcode and operands byte so we can get R1 and R2\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get the Operand 1 index (16-bit) if present\r
+  //\r
+  if (Operands & MOVI_M_IMMDATA) {\r
+    Index16 = VmReadIndex16 (VmPtr, 2);\r
+    Size    = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Get the immediate data.\r
+  //\r
+  if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {\r
+    ImmData64 = (INT64) VmReadImmed16 (VmPtr, Size);\r
+    Size += 2;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {\r
+    ImmData64 = (INT64) VmReadImmed32 (VmPtr, Size);\r
+    Size += 4;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {\r
+    ImmData64 = VmReadImmed64 (VmPtr, Size);\r
+    Size += 8;\r
+  } else {\r
+    //\r
+    // Invalid encoding\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Compute the value and write back the result\r
+  //\r
+  Op2 = (UINT64) ((INT64) ((UINT64) (UINTN) VmPtr->Ip) + (INT64) ImmData64 + Size);\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Check for illegal combination of operand1 direct with immediate data\r
+    //\r
+    if (Operands & MOVI_M_IMMDATA) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = (VM_REGISTER) Op2;\r
+  } else {\r
+    //\r
+    // Get the address = [Rx] + Index16\r
+    // Write back the result. Always a natural size write, since\r
+    // we're talking addresses here.\r
+    //\r
+    Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    VmWriteMemN (VmPtr, (UINTN) Op1, (UINTN) Op2);\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVsnw (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOVsnw instruction. This instruction loads a signed \r
+  natural value from memory or register to another memory or register. On\r
+  32-bit machines, the value gets sign-extended to 64 bits if the destination\r
+  is a register.\r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVsnw {@}R1 {Index16}, {@}R2 {Index16|Immed16}\r
+\r
+  0:7 1=>operand1 index present\r
+  0:6 1=>operand2 index present\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Op1Index;\r
+  INT16   Op2Index;\r
+  UINT64  Op2;\r
+\r
+  //\r
+  // Get the opcode and operand bytes\r
+  //\r
+  Opcode              = GETOPCODE (VmPtr);\r
+  Operands            = GETOPERANDS (VmPtr);\r
+\r
+  Op1Index            = Op2Index = 0;\r
+\r
+  //\r
+  // Get the indexes if present.\r
+  //\r
+  Size = 2;\r
+  if (Opcode & OPCODE_M_IMMED_OP1) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Op1Index = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      //\r
+      // Illegal form operand1 direct with index:  MOVsnw R1 Index16, {@}R2\r
+      //\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    Size += sizeof (UINT16);\r
+  }\r
+\r
+  if (Opcode & OPCODE_M_IMMED_OP2) {\r
+    if (OPERAND2_INDIRECT (Operands)) {\r
+      Op2Index = VmReadIndex16 (VmPtr, Size);\r
+    } else {\r
+      Op2Index = VmReadImmed16 (VmPtr, Size);\r
+    }\r
+\r
+    Size += sizeof (UINT16);\r
+  }\r
+  //\r
+  // Get the data from the source.\r
+  //\r
+  Op2 = (INT64) ((INTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Op2Index));\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    Op2 = (INT64) (INTN) VmReadMemN (VmPtr, (UINTN) Op2);\r
+  }\r
+  //\r
+  // Now write back the result.\r
+  //\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;\r
+  } else {\r
+    VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2);\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVsnd (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOVsnw instruction. This instruction loads a signed \r
+  natural value from memory or register to another memory or register. On\r
+  32-bit machines, the value gets sign-extended to 64 bits if the destination\r
+  is a register.\r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVsnd {@}R1 {Indx32}, {@}R2 {Index32|Immed32}\r
+\r
+  0:7 1=>operand1 index present\r
+  0:6 1=>operand2 index present\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT32   Op1Index;\r
+  INT32   Op2Index;\r
+  UINT64  Op2;\r
+\r
+  //\r
+  // Get the opcode and operand bytes\r
+  //\r
+  Opcode              = GETOPCODE (VmPtr);\r
+  Operands            = GETOPERANDS (VmPtr);\r
+\r
+  Op1Index            = Op2Index = 0;\r
+\r
+  //\r
+  // Get the indexes if present.\r
+  //\r
+  Size = 2;\r
+  if (Opcode & OPCODE_M_IMMED_OP1) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Op1Index = VmReadIndex32 (VmPtr, 2);\r
+    } else {\r
+      //\r
+      // Illegal form operand1 direct with index:  MOVsnd R1 Index16,..\r
+      //\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    Size += sizeof (UINT32);\r
+  }\r
+\r
+  if (Opcode & OPCODE_M_IMMED_OP2) {\r
+    if (OPERAND2_INDIRECT (Operands)) {\r
+      Op2Index = VmReadIndex32 (VmPtr, Size);\r
+    } else {\r
+      Op2Index = VmReadImmed32 (VmPtr, Size);\r
+    }\r
+\r
+    Size += sizeof (UINT32);\r
+  }\r
+  //\r
+  // Get the data from the source.\r
+  //\r
+  Op2 = (INT64) ((INTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Op2Index));\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    Op2 = (INT64) (INTN) VmReadMemN (VmPtr, (UINTN) Op2);\r
+  }\r
+  //\r
+  // Now write back the result.\r
+  //\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;\r
+  } else {\r
+    VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2);\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePUSHn (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC PUSHn instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   PUSHn {@}R1 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8 Opcode;\r
+  UINT8 Operands;\r
+  INT16 Index16;\r
+  UINTN DataN;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get index if present\r
+  //\r
+  if (Opcode & PUSHPOP_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    VmPtr->Ip += 4;\r
+  } else {\r
+    Index16 = 0;\r
+    VmPtr->Ip += 2;\r
+  }\r
+  //\r
+  // Get the data to push\r
+  //\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    DataN = VmReadMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));\r
+  } else {\r
+    DataN = (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16);\r
+  }\r
+  //\r
+  // Adjust the stack down.\r
+  //\r
+  VmPtr->R[0] -= sizeof (UINTN);\r
+  VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], DataN);\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePUSH (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC PUSH instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   PUSH[32|64] {@}R1 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT32  Data32;\r
+  UINT64  Data64;\r
+  INT16   Index16;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Get immediate index if present, then advance the IP.\r
+  //\r
+  if (Opcode & PUSHPOP_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    VmPtr->Ip += 4;\r
+  } else {\r
+    Index16 = 0;\r
+    VmPtr->Ip += 2;\r
+  }\r
+  //\r
+  // Get the data to push\r
+  //\r
+  if (Opcode & PUSHPOP_M_64) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Data64 = VmReadMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));\r
+    } else {\r
+      Data64 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    }\r
+    //\r
+    // Adjust the stack down, then write back the data\r
+    //\r
+    VmPtr->R[0] -= sizeof (UINT64);\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], Data64);\r
+  } else {\r
+    //\r
+    // 32-bit data\r
+    //\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Data32 = VmReadMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));\r
+    } else {\r
+      Data32 = (UINT32) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    }\r
+    //\r
+    // Adjust the stack down and write the data\r
+    //\r
+    VmPtr->R[0] -= sizeof (UINT32);\r
+    VmWriteMem32 (VmPtr, (UINTN) VmPtr->R[0], Data32);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePOPn (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC POPn instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   POPn {@}R1 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8 Opcode;\r
+  UINT8 Operands;\r
+  INT16 Index16;\r
+  UINTN DataN;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Get immediate data if present, and advance the IP\r
+  //\r
+  if (Opcode & PUSHPOP_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    VmPtr->Ip += 4;\r
+  } else {\r
+    Index16 = 0;\r
+    VmPtr->Ip += 2;\r
+  }\r
+  //\r
+  // Read the data off the stack, then adjust the stack pointer\r
+  //\r
+  DataN = VmReadMemN (VmPtr, (UINTN) VmPtr->R[0]);\r
+  VmPtr->R[0] += sizeof (UINTN);\r
+  //\r
+  // Do the write-back\r
+  //\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), DataN);\r
+  } else {\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = (INT64) (UINT64) ((UINTN) DataN + Index16);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePOP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC POP instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  POP {@}R1 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  INT16   Index16;\r
+  INT32   Data32;\r
+  UINT64  Data64;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Get immediate data if present, and advance the IP.\r
+  //\r
+  if (Opcode & PUSHPOP_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    VmPtr->Ip += 4;\r
+  } else {\r
+    Index16 = 0;\r
+    VmPtr->Ip += 2;\r
+  }\r
+  //\r
+  // Get the data off the stack, then write it to the appropriate location\r
+  //\r
+  if (Opcode & PUSHPOP_M_64) {\r
+    //\r
+    // Read the data off the stack, then adjust the stack pointer\r
+    //\r
+    Data64 = VmReadMem64 (VmPtr, (UINTN) VmPtr->R[0]);\r
+    VmPtr->R[0] += sizeof (UINT64);\r
+    //\r
+    // Do the write-back\r
+    //\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      VmWriteMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), Data64);\r
+    } else {\r
+      VmPtr->R[OPERAND1_REGNUM (Operands)] = Data64 + Index16;\r
+    }\r
+  } else {\r
+    //\r
+    // 32-bit pop. Read it off the stack and adjust the stack pointer\r
+    //\r
+    Data32 = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->R[0]);\r
+    VmPtr->R[0] += sizeof (UINT32);\r
+    //\r
+    // Do the write-back\r
+    //\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      VmWriteMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), Data32);\r
+    } else {\r
+      VmPtr->R[OPERAND1_REGNUM (Operands)] = (INT64) Data32 + Index16;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCALL (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Implements the EBC CALL instruction.\r
+\r
+  Instruction format:  \r
+\r
+    CALL64 Immed64\r
+    CALL32 {@}R1 {Immed32|Index32}\r
+    CALLEX64 Immed64\r
+    CALLEX16 {@}R1 {Immed32}\r
+\r
+  If Rx == R0, then it's a PC relative call to PC = PC + imm32.\r
+  \r
+Arguments:\r
+  VmPtr - pointer to a VM context.\r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+--*/\r
+{\r
+  UINT8 Opcode;\r
+  UINT8 Operands;\r
+  INT32 Immed32;\r
+  UINT8 Size;\r
+  INT64 Immed64;\r
+  VOID  *FramePtr;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Assign these as well to avoid compiler warnings\r
+  //\r
+  Immed64   = 0;\r
+  Immed32   = 0;\r
+\r
+  FramePtr  = VmPtr->FramePtr;\r
+  //\r
+  // Determine the instruction size, and get immediate data if present\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA) {\r
+    if (Opcode & OPCODE_M_IMMDATA64) {\r
+      Immed64 = VmReadImmed64 (VmPtr, 2);\r
+      Size    = 10;\r
+    } else {\r
+      //\r
+      // If register operand is indirect, then the immediate data is an index\r
+      //\r
+      if (OPERAND1_INDIRECT (Operands)) {\r
+        Immed32 = VmReadIndex32 (VmPtr, 2);\r
+      } else {\r
+        Immed32 = VmReadImmed32 (VmPtr, 2);\r
+      }\r
+\r
+      Size = 6;\r
+    }\r
+  } else {\r
+    Size = 2;\r
+  }\r
+  //\r
+  // If it's a call to EBC, adjust the stack pointer down 16 bytes and\r
+  // put our return address and frame pointer on the VM stack.\r
+  //\r
+  if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {\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) (UINTN) (VmPtr->Ip + Size));\r
+  }\r
+  //\r
+  // If 64-bit data, then absolute jump only\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA64) {\r
+    //\r
+    // Native or EBC call?\r
+    //\r
+    if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {\r
+      VmPtr->Ip = (VMIP) (UINTN) Immed64;\r
+    } else {\r
+      //\r
+      // Call external function, get the return value, and advance the IP\r
+      //\r
+      EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->R[0], FramePtr, Size);\r
+    }\r
+  } else {\r
+    //\r
+    // Get the register data. If operand1 == 0, then ignore register and\r
+    // take immediate data as relative or absolute address.\r
+    // Compiler should take care of upper bits if 32-bit machine.\r
+    //\r
+    if (OPERAND1_REGNUM (Operands) != 0) {\r
+      Immed64 = (UINT64) (UINTN) VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+    }\r
+    //\r
+    // Get final address\r
+    //\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Immed64 = (INT64) (UINT64) (UINTN) VmReadMemN (VmPtr, (UINTN) (Immed64 + Immed32));\r
+    } else {\r
+      Immed64 += Immed32;\r
+    }\r
+    //\r
+    // Now determine if external call, and then if relative or absolute\r
+    //\r
+    if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {\r
+      //\r
+      // EBC call. Relative or absolute? If relative, then it's relative to the\r
+      // start of the next instruction.\r
+      //\r
+      if (Operands & OPERAND_M_RELATIVE_ADDR) {\r
+        VmPtr->Ip += Immed64 + Size;\r
+      } else {\r
+        VmPtr->Ip = (VMIP) (UINTN) Immed64;\r
+      }\r
+    } else {\r
+      //\r
+      // Native call. Relative or absolute?\r
+      //\r
+      if (Operands & OPERAND_M_RELATIVE_ADDR) {\r
+        EbcLLCALLEX (VmPtr, (UINTN) (Immed64 + VmPtr->Ip + Size), (UINTN) VmPtr->R[0], FramePtr, Size);\r
+      } else {\r
+        if (VmPtr->StopFlags & STOPFLAG_BREAK_ON_CALLEX) {\r
+          CpuBreakpoint ();\r
+        }\r
+\r
+        EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->R[0], FramePtr, Size);\r
+      }\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteRET (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC RET instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   RET\r
+\r
+--*/\r
+{\r
+  //\r
+  // If we're at the top of the stack, then simply set the done\r
+  // flag and return\r
+  //\r
+  if (VmPtr->StackRetAddr == (UINT64) VmPtr->R[0]) {\r
+    VmPtr->StopFlags |= STOPFLAG_APP_DONE;\r
+  } else {\r
+    //\r
+    // Pull the return address off the VM app's stack and set the IP\r
+    // to it\r
+    //\r
+    if (!IS_ALIGNED ((UINTN) VmPtr->R[0], sizeof (UINT16))) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_ALIGNMENT_CHECK,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+    }\r
+    //\r
+    // Restore the IP and frame pointer from the stack\r
+    //\r
+    VmPtr->Ip = (VMIP) (UINTN) VmReadMem64 (VmPtr, (UINTN) VmPtr->R[0]);\r
+    VmPtr->R[0] += 8;\r
+    VmPtr->FramePtr = (VOID *) VmReadMemN (VmPtr, (UINTN) VmPtr->R[0]);\r
+    VmPtr->R[0] += 8;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCMP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC CMP instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   CMP[32|64][eq|lte|gte|ulte|ugte] R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Index16;\r
+  UINT32  Flag;\r
+  INT64   Op2;\r
+  INT64   Op1;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Get the register data we're going to compare to\r
+  //\r
+  Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+  //\r
+  // Get immediate data\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA) {\r
+    if (OPERAND2_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    Size = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Now get Op2\r
+  //\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    if (Opcode & OPCODE_M_64BIT) {\r
+      Op2 = (INT64) VmReadMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16));\r
+    } else {\r
+      //\r
+      // 32-bit operations. 0-extend the values for all cases.\r
+      //\r
+      Op2 = (INT64) (UINT64) ((UINT32) VmReadMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16)));\r
+    }\r
+  } else {\r
+    Op2 = VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16;\r
+  }\r
+  //\r
+  // Now do the compare\r
+  //\r
+  Flag = 0;\r
+  if (Opcode & OPCODE_M_64BIT) {\r
+    //\r
+    // 64-bit compares\r
+    //\r
+    switch (Opcode & OPCODE_M_OPCODE) {\r
+    case OPCODE_CMPEQ:\r
+      if (Op1 == Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPLTE:\r
+      if (Op1 <= Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPGTE:\r
+      if (Op1 >= Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPULTE:\r
+      if ((UINT64) Op1 <= (UINT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPUGTE:\r
+      if ((UINT64) Op1 >= (UINT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    default:\r
+      ASSERT (0);\r
+    }\r
+  } else {\r
+    //\r
+    // 32-bit compares\r
+    //\r
+    switch (Opcode & OPCODE_M_OPCODE) {\r
+    case OPCODE_CMPEQ:\r
+      if ((INT32) Op1 == (INT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPLTE:\r
+      if ((INT32) Op1 <= (INT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPGTE:\r
+      if ((INT32) Op1 >= (INT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPULTE:\r
+      if ((UINT32) Op1 <= (UINT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPUGTE:\r
+      if ((UINT32) Op1 >= (UINT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    default:\r
+      ASSERT (0);\r
+    }\r
+  }\r
+  //\r
+  // Now set the flag accordingly for the comparison\r
+  //\r
+  if (Flag) {\r
+    VMFLAG_SET (VmPtr, VMFLAGS_CC);\r
+  } else {\r
+    VMFLAG_CLEAR (VmPtr, VMFLAGS_CC);\r
+  }\r
+  //\r
+  // Advance the IP\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCMPI (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC CMPI instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   CMPI[32|64]{w|d}[eq|lte|gte|ulte|ugte] {@}Rx {Index16}, Immed16|Immed32\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT64   Op1;\r
+  INT64   Op2;\r
+  INT16   Index16;\r
+  UINT32  Flag;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get operand1 index if present\r
+  //\r
+  Size = 2;\r
+  if (Operands & OPERAND_M_CMPI_INDEX) {\r
+    Index16 = VmReadIndex16 (VmPtr, 2);\r
+    Size += 2;\r
+  } else {\r
+    Index16 = 0;\r
+  }\r
+  //\r
+  // Get operand1 data we're going to compare to\r
+  //\r
+  Op1 = (INT64) VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Indirect operand1. Fetch 32 or 64-bit value based on compare size.\r
+    //\r
+    if (Opcode & OPCODE_M_CMPI64) {\r
+      Op1 = (INT64) VmReadMem64 (VmPtr, (UINTN) Op1 + Index16);\r
+    } else {\r
+      Op1 = (INT64) VmReadMem32 (VmPtr, (UINTN) Op1 + Index16);\r
+    }\r
+  } else {\r
+    //\r
+    // Better not have been an index with direct. That is, CMPI R1 Index,...\r
+    // is illegal.\r
+    //\r
+    if (Operands & OPERAND_M_CMPI_INDEX) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_ERROR,\r
+        VmPtr\r
+        );\r
+      VmPtr->Ip += Size;\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+  }\r
+  //\r
+  // Get immediate data -- 16- or 32-bit sign extended\r
+  //\r
+  if (Opcode & OPCODE_M_CMPI32_DATA) {\r
+    Op2 = (INT64) VmReadImmed32 (VmPtr, Size);\r
+    Size += 4;\r
+  } else {\r
+    //\r
+    // 16-bit immediate data. Sign extend always.\r
+    //\r
+    Op2 = (INT64) ((INT16) VmReadImmed16 (VmPtr, Size));\r
+    Size += 2;\r
+  }\r
+  //\r
+  // Now do the compare\r
+  //\r
+  Flag = 0;\r
+  if (Opcode & OPCODE_M_CMPI64) {\r
+    //\r
+    // 64 bit comparison\r
+    //\r
+    switch (Opcode & OPCODE_M_OPCODE) {\r
+    case OPCODE_CMPIEQ:\r
+      if (Op1 == (INT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPILTE:\r
+      if (Op1 <= (INT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIGTE:\r
+      if (Op1 >= (INT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIULTE:\r
+      if ((UINT64) Op1 <= (UINT64) ((UINT32) Op2)) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIUGTE:\r
+      if ((UINT64) Op1 >= (UINT64) ((UINT32) Op2)) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    default:\r
+      ASSERT (0);\r
+    }\r
+  } else {\r
+    //\r
+    // 32-bit comparisons\r
+    //\r
+    switch (Opcode & OPCODE_M_OPCODE) {\r
+    case OPCODE_CMPIEQ:\r
+      if ((INT32) Op1 == Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPILTE:\r
+      if ((INT32) Op1 <= Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIGTE:\r
+      if ((INT32) Op1 >= Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIULTE:\r
+      if ((UINT32) Op1 <= (UINT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIUGTE:\r
+      if ((UINT32) Op1 >= (UINT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    default:\r
+      ASSERT (0);\r
+    }\r
+  }\r
+  //\r
+  // Now set the flag accordingly for the comparison\r
+  //\r
+  if (Flag) {\r
+    VMFLAG_SET (VmPtr, VMFLAGS_CC);\r
+  } else {\r
+    VMFLAG_CLEAR (VmPtr, VMFLAGS_CC);\r
+  }\r
+  //\r
+  // Advance the IP\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteNOT (\r
+  IN VM_CONTEXT     *VmPtr,\r
+  IN UINT64         Op1,\r
+  IN UINT64         Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC NOT instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  ~Op2\r
+\r
+Instruction syntax:\r
+  NOT[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+  \r
+--*/\r
+{\r
+  return ~Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteNEG (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC NEG instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op2 * -1\r
+\r
+Instruction syntax:\r
+  NEG[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  return ~Op2 + 1;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteADD (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC ADD instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 + Op2\r
+\r
+Instruction syntax:\r
+   ADD[32|64] {@}R1, {@}R2 {Index16}\r
+\r
+--*/\r
+{\r
+  return Op1 + Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSUB (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC SUB instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 - Op2\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  SUB[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return (UINT64) ((INT64) ((INT64) Op1 - (INT64) Op2));\r
+  } else {\r
+    return (UINT64) ((INT64) ((INT32) Op1 - (INT32) Op2));\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMUL (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MUL instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 * Op2\r
+\r
+Instruction syntax:\r
+  MUL[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return MultS64x64 ((INT64)Op1, (INT64)Op2);\r
+  } else {\r
+    return (UINT64) ((INT64) ((INT32) Op1 * (INT32) Op2));\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMULU (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC MULU instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (unsigned)Op1 * (unsigned)Op2 \r
+\r
+Instruction syntax:\r
+  MULU[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return MultU64x64 (Op1, Op2);\r
+  } else {\r
+    return (UINT64) ((UINT32) Op1 * (UINT32) Op2);\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteDIV (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC DIV instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1/Op2\r
+\r
+Instruction syntax:\r
+  DIV[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  INT64   Remainder;\r
+\r
+  //\r
+  // Check for divide-by-0\r
+  //\r
+  if (Op2 == 0) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_DIVIDE_ERROR,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+\r
+    return 0;\r
+  } else {\r
+    if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+      return (UINT64) (DivS64x64Remainder (Op1, Op2, &Remainder));\r
+    } else {\r
+      return (UINT64) ((INT64) ((INT32) Op1 / (INT32) Op2));\r
+    }\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteDIVU (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC DIVU instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (unsigned)Op1 / (unsigned)Op2\r
+\r
+Instruction syntax:\r
+  DIVU[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT64  Remainder;\r
+\r
+  //\r
+  // Check for divide-by-0\r
+  //\r
+  if (Op2 == 0) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_DIVIDE_ERROR,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return 0;\r
+  } else {\r
+    //\r
+    // Get the destination register\r
+    //\r
+    if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+      return (UINT64) (DivU64x64Remainder ((INT64)Op1, (INT64)Op2, &Remainder));\r
+    } else {\r
+      return (UINT64) ((UINT32) Op1 / (UINT32) Op2);\r
+    }\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMOD (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC MOD instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 MODULUS Op2\r
+\r
+Instruction syntax:\r
+  MOD[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  INT64   Remainder;\r
+\r
+  //\r
+  // Check for divide-by-0\r
+  //\r
+  if (Op2 == 0) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_DIVIDE_ERROR,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return 0;\r
+  } else {\r
+    DivS64x64Remainder ((INT64)Op1, (INT64)Op2, &Remainder);\r
+    return Remainder;\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMODU (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC MODU instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 UNSIGNED_MODULUS Op2\r
+\r
+Instruction syntax:\r
+  MODU[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+  \r
+--*/\r
+{\r
+  UINT64  Remainder;\r
+\r
+  //\r
+  // Check for divide-by-0\r
+  //\r
+  if (Op2 == 0) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_DIVIDE_ERROR,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return 0;\r
+  } else {\r
+    DivU64x64Remainder (Op1, Op2, &Remainder);\r
+    return Remainder;\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteAND (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC AND instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 AND Op2\r
+\r
+Instruction syntax:\r
+  AND[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  return Op1 & Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteOR (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC OR instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 OR Op2\r
+\r
+Instruction syntax:\r
+  OR[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  return Op1 | Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteXOR (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC XOR instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 XOR Op2\r
+\r
+Instruction syntax:\r
+  XOR[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  return Op1 ^ Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSHL (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC SHL shift left instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 << Op2\r
+\r
+Instruction syntax:\r
+  SHL[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return LShiftU64 (Op1, (UINTN)Op2);\r
+  } else {\r
+    return (UINT64) ((UINT32) ((UINT32) Op1 << (UINT32) Op2));\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSHR (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC SHR instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 >> Op2  (unsigned operands)\r
+\r
+Instruction syntax:\r
+  SHR[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return RShiftU64 (Op1, (UINTN)Op2);\r
+  } else {\r
+    return (UINT64) ((UINT32) Op1 >> (UINT32) Op2);\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteASHR (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC ASHR instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 >> Op2 (signed)\r
+\r
+Instruction syntax:\r
+  ASHR[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return ARShiftU64 (Op1, (UINTN)Op2);\r
+  } else {\r
+    return (UINT64) ((INT64) ((INT32) Op1 >> (UINT32) Op2));\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDB (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC EXTNDB instruction to sign-extend a byte value.\r
+  \r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (INT64)(INT8)Op2\r
+\r
+Instruction syntax:\r
+  EXTNDB[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+  \r
+--*/\r
+{\r
+  INT8  Data8;\r
+  INT64 Data64;\r
+  //\r
+  // Convert to byte, then return as 64-bit signed value to let compiler\r
+  // sign-extend the value\r
+  //\r
+  Data8   = (INT8) Op2;\r
+  Data64  = (INT64) Data8;\r
+\r
+  return (UINT64) Data64;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDW (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC EXTNDW instruction to sign-extend a 16-bit value.\r
+  \r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (INT64)(INT16)Op2\r
+\r
+Instruction syntax:\r
+  EXTNDW[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+  \r
+--*/\r
+{\r
+  INT16 Data16;\r
+  INT64 Data64;\r
+  //\r
+  // Convert to word, then return as 64-bit signed value to let compiler\r
+  // sign-extend the value\r
+  //\r
+  Data16  = (INT16) Op2;\r
+  Data64  = (INT64) Data16;\r
+\r
+  return (UINT64) Data64;\r
+}\r
+//\r
+// Execute the EBC EXTNDD instruction.\r
+//\r
+// Format: EXTNDD {@}Rx, {@}Ry [Index16|Immed16]\r
+//         EXTNDD Dest, Source\r
+//\r
+// Operation:  Dest <- SignExtended((DWORD)Source))\r
+//\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDD (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC EXTNDD instruction to sign-extend a 32-bit value.\r
+  \r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (INT64)(INT32)Op2\r
+\r
+Instruction syntax:\r
+  EXTNDD[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+  \r
+--*/\r
+{\r
+  INT32 Data32;\r
+  INT64 Data64;\r
+  //\r
+  // Convert to 32-bit value, then return as 64-bit signed value to let compiler\r
+  // sign-extend the value\r
+  //\r
+  Data32  = (INT32) Op2;\r
+  Data64  = (INT64) Data32;\r
+\r
+  return (UINT64) Data64;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteSignedDataManip (\r
+  IN VM_CONTEXT   *VmPtr\r
+  )\r
+{\r
+  //\r
+  // Just call the data manipulation function with a flag indicating this\r
+  // is a signed operation.\r
+  //\r
+  return ExecuteDataManip (VmPtr, TRUE);\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteUnsignedDataManip (\r
+  IN VM_CONTEXT   *VmPtr\r
+  )\r
+{\r
+  //\r
+  // Just call the data manipulation function with a flag indicating this\r
+  // is not a signed operation.\r
+  //\r
+  return ExecuteDataManip (VmPtr, FALSE);\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteDataManip (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN BOOLEAN      IsSignedOp\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute all the EBC data manipulation instructions. \r
+  Since the EBC data manipulation instructions all have the same basic form, \r
+  they can share the code that does the fetch of operands and the write-back\r
+  of the result. This function performs the fetch of the operands (even if\r
+  both are not needed to be fetched, like NOT instruction), dispatches to the\r
+  appropriate subfunction, then writes back the returned result.\r
+\r
+Arguments:\r
+  VmPtr - pointer to VM context\r
+\r
+Returns:\r
+  Standard EBC status\r
+\r
+Format:  \r
+  INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  INT16   Index16;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  UINT64  Op1;\r
+  UINT64  Op2;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Determine if we have immediate data by the opcode\r
+  //\r
+  if (Opcode & DATAMANIP_M_IMMDATA) {\r
+    //\r
+    // Index16 if Ry is indirect, or Immed16 if Ry direct.\r
+    //\r
+    if (OPERAND2_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    Size = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Now get operand2 (source). It's of format {@}R2 {Index16|Immed16}\r
+  //\r
+  Op2 = (UINT64) VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16;\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    //\r
+    // Indirect form: @R2 Index16. Fetch as 32- or 64-bit data\r
+    //\r
+    if (Opcode & DATAMANIP_M_64) {\r
+      Op2 = VmReadMem64 (VmPtr, (UINTN) Op2);\r
+    } else {\r
+      //\r
+      // Read as signed value where appropriate.\r
+      //\r
+      if (IsSignedOp) {\r
+        Op2 = (UINT64) (INT64) ((INT32) VmReadMem32 (VmPtr, (UINTN) Op2));\r
+      } else {\r
+        Op2 = (UINT64) VmReadMem32 (VmPtr, (UINTN) Op2);\r
+      }\r
+    }\r
+  } else {\r
+    if ((Opcode & DATAMANIP_M_64) == 0) {\r
+      if (IsSignedOp) {\r
+        Op2 = (UINT64) (INT64) ((INT32) Op2);\r
+      } else {\r
+        Op2 = (UINT64) ((UINT32) Op2);\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Get operand1 (destination and sometimes also an actual operand)\r
+  // of form {@}R1\r
+  //\r
+  Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    if (Opcode & DATAMANIP_M_64) {\r
+      Op1 = VmReadMem64 (VmPtr, (UINTN) Op1);\r
+    } else {\r
+      if (IsSignedOp) {\r
+        Op1 = (UINT64) (INT64) ((INT32) VmReadMem32 (VmPtr, (UINTN) Op1));\r
+      } else {\r
+        Op1 = (UINT64) VmReadMem32 (VmPtr, (UINTN) Op1);\r
+      }\r
+    }\r
+  } else {\r
+    if ((Opcode & DATAMANIP_M_64) == 0) {\r
+      if (IsSignedOp) {\r
+        Op1 = (UINT64) (INT64) ((INT32) Op1);\r
+      } else {\r
+        Op1 = (UINT64) ((UINT32) Op1);\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Dispatch to the computation function\r
+  //\r
+  if (((Opcode & OPCODE_M_OPCODE) - OPCODE_NOT) >=\r
+        (sizeof (mDataManipDispatchTable) / sizeof (mDataManipDispatchTable[0]))\r
+        ) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INVALID_OPCODE,\r
+      EXCEPTION_FLAG_ERROR,\r
+      VmPtr\r
+      );\r
+    //\r
+    // Advance and return\r
+    //\r
+    VmPtr->Ip += Size;\r
+    return EFI_UNSUPPORTED;\r
+  } else {\r
+    Op2 = mDataManipDispatchTable[(Opcode & OPCODE_M_OPCODE) - OPCODE_NOT](VmPtr, Op1, Op2);\r
+  }\r
+  //\r
+  // Write back the result.\r
+  //\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+    if (Opcode & DATAMANIP_M_64) {\r
+      VmWriteMem64 (VmPtr, (UINTN) Op1, Op2);\r
+    } else {\r
+      VmWriteMem32 (VmPtr, (UINTN) Op1, (UINT32) Op2);\r
+    }\r
+  } else {\r
+    //\r
+    // Storage back to a register. Write back, clearing upper bits (as per\r
+    // the specification) if 32-bit operation.\r
+    //\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;\r
+    if ((Opcode & DATAMANIP_M_64) == 0) {\r
+      VmPtr->R[OPERAND1_REGNUM (Operands)] &= 0xFFFFFFFF;\r
+    }\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteLOADSP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC LOADSP instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  LOADSP  SP1, R2\r
+\r
+--*/\r
+{\r
+  UINT8 Operands;\r
+\r
+  //\r
+  // Get the operands\r
+  //\r
+  Operands = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Do the operation\r
+  //\r
+  switch (OPERAND1_REGNUM (Operands)) {\r
+  //\r
+  // Set flags\r
+  //\r
+  case 0:\r
+    //\r
+    // Spec states that this instruction will not modify reserved bits in\r
+    // the flags register.\r
+    //\r
+    VmPtr->Flags = (VmPtr->Flags &~VMFLAGS_ALL_VALID) | (VmPtr->R[OPERAND2_REGNUM (Operands)] & VMFLAGS_ALL_VALID);\r
+    break;\r
+\r
+  default:\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_WARNING,\r
+      VmPtr\r
+      );\r
+    VmPtr->Ip += 2;\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  VmPtr->Ip += 2;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteSTORESP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC STORESP instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   STORESP  Rx, FLAGS|IP\r
+\r
+--*/\r
+{\r
+  UINT8 Operands;\r
+\r
+  //\r
+  // Get the operands\r
+  //\r
+  Operands = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Do the operation\r
+  //\r
+  switch (OPERAND2_REGNUM (Operands)) {\r
+  //\r
+  // Get flags\r
+  //\r
+  case 0:\r
+    //\r
+    // Retrieve the value in the flags register, then clear reserved bits\r
+    //\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = (UINT64) (VmPtr->Flags & VMFLAGS_ALL_VALID);\r
+    break;\r
+\r
+  //\r
+  // Get IP -- address of following instruction\r
+  //\r
+  case 1:\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = (UINT64) (UINTN) VmPtr->Ip + 2;\r
+    break;\r
+\r
+  default:\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_WARNING,\r
+      VmPtr\r
+      );\r
+    VmPtr->Ip += 2;\r
+    return EFI_UNSUPPORTED;\r
+    break;\r
+  }\r
+\r
+  VmPtr->Ip += 2;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+INT16\r
+VmReadIndex16 (\r
+  IN VM_CONTEXT     *VmPtr,\r
+  IN UINT32         CodeOffset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Decode a 16-bit index to determine the offset. Given an index value:\r
+\r
+    b15     - sign bit\r
+    b14:12  - number of bits in this index assigned to natural units (=a)\r
+    ba:11   - constant units = C\r
+    b0:a    - natural units = N\r
+  \r
+  Given this info, the offset can be computed by:\r
+    offset = sign_bit * (C + N * sizeof(UINTN))\r
+\r
+  Max offset is achieved with index = 0x7FFF giving an offset of\r
+  0x27B (32-bit machine) or 0x477 (64-bit machine).\r
+  Min offset is achieved with index = \r
+  \r
+Arguments:\r
+  VmPtr       - pointer to VM context\r
+  CodeOffset  - offset from IP of the location of the 16-bit index to decode\r
+\r
+Returns:\r
+  The decoded offset.\r
+  \r
+--*/\r
+{\r
+  UINT16  Index;\r
+  INT16   Offset;\r
+  INT16   C;\r
+  INT16   N;\r
+  INT16   NBits;\r
+  INT16   Mask;\r
+\r
+  //\r
+  // First read the index from the code stream\r
+  //\r
+  Index = VmReadCode16 (VmPtr, CodeOffset);\r
+\r
+  //\r
+  // Get the mask for N. First get the number of bits from the index.\r
+  //\r
+  NBits = (INT16) ((Index & 0x7000) >> 12);\r
+\r
+  //\r
+  // Scale it for 16-bit indexes\r
+  //\r
+  NBits *= 2;\r
+\r
+  //\r
+  // Now using the number of bits, create a mask.\r
+  //\r
+  Mask = (INT16) ((INT16)~0 << NBits);\r
+\r
+  //\r
+  // Now using the mask, extract N from the lower bits of the index.\r
+  //\r
+  N = (INT16) (Index &~Mask);\r
+\r
+  //\r
+  // Now compute C\r
+  //\r
+  C       = (INT16) (((Index &~0xF000) & Mask) >> NBits);\r
+\r
+  Offset  = (INT16) (N * sizeof (UINTN) + C);\r
+\r
+  //\r
+  // Now set the sign\r
+  //\r
+  if (Index & 0x8000) {\r
+    //\r
+    // Do it the hard way to work around a bogus compiler warning\r
+    //\r
+    // Offset = -1 * Offset;\r
+    //\r
+    Offset = (INT16) ((INT32) Offset * -1);\r
+  }\r
+\r
+  return Offset;\r
+}\r
+\r
+STATIC\r
+INT32\r
+VmReadIndex32 (\r
+  IN VM_CONTEXT     *VmPtr,\r
+  IN UINT32         CodeOffset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Decode a 32-bit index to determine the offset.\r
+\r
+Arguments:\r
+  VmPtr       - pointer to VM context\r
+  CodeOffset  - offset from IP of the location of the 32-bit index to decode\r
+\r
+Returns:\r
+  Converted index per EBC VM specification\r
+\r
+--*/\r
+{\r
+  UINT32  Index;\r
+  INT32   Offset;\r
+  INT32   C;\r
+  INT32   N;\r
+  INT32   NBits;\r
+  INT32   Mask;\r
+\r
+  Index = VmReadImmed32 (VmPtr, CodeOffset);\r
+\r
+  //\r
+  // Get the mask for N. First get the number of bits from the index.\r
+  //\r
+  NBits = (Index & 0x70000000) >> 28;\r
+\r
+  //\r
+  // Scale it for 32-bit indexes\r
+  //\r
+  NBits *= 4;\r
+\r
+  //\r
+  // Now using the number of bits, create a mask.\r
+  //\r
+  Mask = (INT32)~0 << NBits;\r
+\r
+  //\r
+  // Now using the mask, extract N from the lower bits of the index.\r
+  //\r
+  N = Index &~Mask;\r
+\r
+  //\r
+  // Now compute C\r
+  //\r
+  C       = ((Index &~0xF0000000) & Mask) >> NBits;\r
+\r
+  Offset  = N * sizeof (UINTN) + C;\r
+\r
+  //\r
+  // Now set the sign\r
+  //\r
+  if (Index & 0x80000000) {\r
+    Offset = Offset * -1;\r
+  }\r
+\r
+  return Offset;\r
+}\r
+\r
+STATIC\r
+INT64\r
+VmReadIndex64 (\r
+  IN VM_CONTEXT     *VmPtr,\r
+  IN UINT32         CodeOffset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Decode a 64-bit index to determine the offset.\r
+\r
+Arguments:\r
+  VmPtr       - pointer to VM context\r
+  CodeOffset  - offset from IP of the location of the 64-bit index to decode\r
+\r
+Returns:\r
+  Converted index per EBC VM specification\r
+\r
+--*/\r
+{\r
+  UINT64  Index;\r
+  INT64   Offset;\r
+  INT64   C;\r
+  INT64   N;\r
+  INT64   NBits;\r
+  INT64   Mask;\r
+\r
+  Index = VmReadCode64 (VmPtr, CodeOffset);\r
+\r
+  //\r
+  // Get the mask for N. First get the number of bits from the index.\r
+  //\r
+  NBits = RShiftU64 ((Index & 0x7000000000000000ULL), 60);\r
+\r
+  //\r
+  // Scale it for 64-bit indexes (multiply by 8 by shifting left 3)\r
+  //\r
+  NBits = LShiftU64 ((UINT64)NBits, 3);\r
+\r
+  //\r
+  // Now using the number of bits, create a mask.\r
+  //\r
+  Mask = (LShiftU64 ((UINT64)~0, (UINTN)NBits));\r
+\r
+  //\r
+  // Now using the mask, extract N from the lower bits of the index.\r
+  //\r
+  N = Index &~Mask;\r
+\r
+  //\r
+  // Now compute C\r
+  //\r
+  C       = ARShiftU64 (((Index &~0xF000000000000000ULL) & Mask), (UINTN)NBits);\r
+\r
+  Offset  = MultU64x64 (N, sizeof (UINTN)) + C;\r
+\r
+  //\r
+  // Now set the sign\r
+  //\r
+  if (Index & 0x8000000000000000ULL) {\r
+    Offset = MultS64x64 (Offset, -1);\r
+  }\r
+\r
+  return Offset;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem8 (\r
+  IN VM_CONTEXT    *VmPtr,\r
+  IN UINTN         Addr,\r
+  IN UINT8         Data\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  The following VmWriteMem? routines are called by the EBC data\r
+  movement instructions that write to memory. Since these writes\r
+  may be to the stack, which looks like (high address on top) this,\r
+\r
+  [EBC entry point arguments]\r
+  [VM stack]\r
+  [EBC stack]\r
+\r
+  we need to detect all attempts to write to the EBC entry point argument\r
+  stack area and adjust the address (which will initially point into the \r
+  VM stack) to point into the EBC entry point arguments.\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+  Addr    - adddress to write to\r
+  Data    - value to write to Addr\r
+  \r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+--*/\r
+{\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr            = ConvertStackAddr (VmPtr, Addr);\r
+  *(UINT8 *) Addr = Data;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem16 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINT16       Data\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Do a simple write if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT16))) {\r
+    *(UINT16 *) Addr = Data;\r
+  } else {\r
+    //\r
+    // Write as two bytes\r
+    //\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem8 (VmPtr, Addr, (UINT8) Data)) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem8 (VmPtr, Addr + 1, (UINT8) (Data >> 8))) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem32 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINT32       Data\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Do a simple write if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT32))) {\r
+    *(UINT32 *) Addr = Data;\r
+  } else {\r
+    //\r
+    // Write as two words\r
+    //\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem16 (VmPtr, Addr, (UINT16) Data)) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem16 (VmPtr, Addr + sizeof (UINT16), (UINT16) (Data >> 16))) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+VmWriteMem64 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINT64       Data\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Data32;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Do a simple write if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT64))) {\r
+    *(UINT64 *) Addr = Data;\r
+  } else {\r
+    //\r
+    // Write as two 32-bit words\r
+    //\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem32 (VmPtr, Addr, (UINT32) Data)) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+    Data32 = (UINT32) (((UINT32 *) &Data)[1]);\r
+    if ((Status = VmWriteMem32 (VmPtr, Addr + sizeof (UINT32), Data32)) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+VmWriteMemN (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINTN        Data\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Index;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Do a simple write if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINTN))) {\r
+    *(UINTN *) Addr = Data;\r
+  } else {\r
+    for (Index = 0; Index < sizeof (UINTN) / sizeof (UINT32); Index++) {\r
+      MemoryFence ();\r
+      Status = VmWriteMem32 (VmPtr, Addr + Index * sizeof (UINT32), (UINT32) Data);\r
+      MemoryFence ();\r
+      Data = (UINTN)RShiftU64 ((UINT64)Data, 32);\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+STATIC\r
+INT8\r
+VmReadImmed8 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  The following VmReadImmed routines are called by the EBC execute\r
+  functions to read EBC immediate values from the code stream.\r
+  Since we can't assume alignment, each tries to read in the biggest \r
+  chunks size available, but will revert to smaller reads if necessary.\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+  Offset  - offset from IP of the code bytes to read.\r
+\r
+Returns:\r
+  Signed data of the requested size from the specified address.\r
+\r
+--*/\r
+{\r
+  //\r
+  // Simply return the data in flat memory space\r
+  //\r
+  return * (INT8 *) (VmPtr->Ip + Offset);\r
+}\r
+\r
+STATIC\r
+INT16\r
+VmReadImmed16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (INT16))) {\r
+    return * (INT16 *) (VmPtr->Ip + Offset);\r
+  } else {\r
+    //\r
+    // All code word reads should be aligned\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_ALIGNMENT_CHECK,\r
+      EXCEPTION_FLAG_WARNING,\r
+      VmPtr\r
+      );\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  return (INT16) (*(UINT8 *) (VmPtr->Ip + Offset) + (*(UINT8 *) (VmPtr->Ip + Offset + 1) << 8));\r
+}\r
+\r
+STATIC\r
+INT32\r
+VmReadImmed32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  UINT32  Data;\r
+\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {\r
+    return * (INT32 *) (VmPtr->Ip + Offset);\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  Data = (UINT32) VmReadCode16 (VmPtr, Offset);\r
+  Data |= (UINT32) (VmReadCode16 (VmPtr, Offset + 2) << 16);\r
+  return Data;\r
+}\r
+\r
+STATIC\r
+INT64\r
+VmReadImmed64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  UINT64  Data64;\r
+  UINT32  Data32;\r
+  UINT8   *Ptr;\r
+\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {\r
+    return * (UINT64 *) (VmPtr->Ip + Offset);\r
+  }\r
+  //\r
+  // Return unaligned data.\r
+  //\r
+  Ptr             = (UINT8 *) &Data64;\r
+  Data32          = VmReadCode32 (VmPtr, Offset);\r
+  *(UINT32 *) Ptr = Data32;\r
+  Ptr += sizeof (Data32);\r
+  Data32          = VmReadCode32 (VmPtr, Offset + sizeof (UINT32));\r
+  *(UINT32 *) Ptr = Data32;\r
+  return Data64;\r
+}\r
+\r
+STATIC\r
+UINT16\r
+VmReadCode16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  The following VmReadCode() routines provide the ability to read raw \r
+  unsigned data from the code stream. \r
+  \r
+Arguments:\r
+  VmPtr   - pointer to VM context\r
+  Offset  - offset from current IP to the raw data to read.\r
+\r
+Returns:\r
+  The raw unsigned 16-bit value from the code stream.\r
+  \r
+--*/\r
+{\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT16))) {\r
+    return * (UINT16 *) (VmPtr->Ip + Offset);\r
+  } else {\r
+    //\r
+    // All code word reads should be aligned\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_ALIGNMENT_CHECK,\r
+      EXCEPTION_FLAG_WARNING,\r
+      VmPtr\r
+      );\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  return (UINT16) (*(UINT8 *) (VmPtr->Ip + Offset) + (*(UINT8 *) (VmPtr->Ip + Offset + 1) << 8));\r
+}\r
+\r
+STATIC\r
+UINT32\r
+VmReadCode32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  UINT32  Data;\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {\r
+    return * (UINT32 *) (VmPtr->Ip + Offset);\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  Data = (UINT32) VmReadCode16 (VmPtr, Offset);\r
+  Data |= (VmReadCode16 (VmPtr, Offset + 2) << 16);\r
+  return Data;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+VmReadCode64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  UINT64  Data64;\r
+  UINT32  Data32;\r
+  UINT8   *Ptr;\r
+\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {\r
+    return * (UINT64 *) (VmPtr->Ip + Offset);\r
+  }\r
+  //\r
+  // Return unaligned data.\r
+  //\r
+  Ptr             = (UINT8 *) &Data64;\r
+  Data32          = VmReadCode32 (VmPtr, Offset);\r
+  *(UINT32 *) Ptr = Data32;\r
+  Ptr += sizeof (Data32);\r
+  Data32          = VmReadCode32 (VmPtr, Offset + sizeof (UINT32));\r
+  *(UINT32 *) Ptr = Data32;\r
+  return Data64;\r
+}\r
+\r
+STATIC\r
+UINT8\r
+VmReadMem8 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr\r
+  )\r
+{\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+  //\r
+  // Simply return the data in flat memory space\r
+  //\r
+  return * (UINT8 *) Addr;\r
+}\r
+\r
+STATIC\r
+UINT16\r
+VmReadMem16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  )\r
+{\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT16))) {\r
+    return * (UINT16 *) Addr;\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  return (UINT16) (*(UINT8 *) Addr + (*(UINT8 *) (Addr + 1) << 8));\r
+}\r
+\r
+STATIC\r
+UINT32\r
+VmReadMem32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  )\r
+{\r
+  UINT32  Data;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT32))) {\r
+    return * (UINT32 *) Addr;\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  Data = (UINT32) VmReadMem16 (VmPtr, Addr);\r
+  Data |= (VmReadMem16 (VmPtr, Addr + 2) << 16);\r
+  return Data;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+VmReadMem64 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr\r
+  )\r
+{\r
+  UINT64  Data;\r
+  UINT32  Data32;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT64))) {\r
+    return * (UINT64 *) Addr;\r
+  }\r
+  //\r
+  // Return unaligned data. Assume little endian.\r
+  //\r
+  Data    = (UINT64) VmReadMem32 (VmPtr, Addr);\r
+  Data32  = VmReadMem32 (VmPtr, Addr + sizeof (UINT32));\r
+  *(UINT32 *) ((UINT32 *) &Data + 1) = Data32;\r
+  return Data;\r
+}\r
+\r
+STATIC\r
+UINTN\r
+ConvertStackAddr (\r
+  IN VM_CONTEXT    *VmPtr,\r
+  IN UINTN         Addr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Given an address that EBC is going to read from or write to, return\r
+  an appropriate address that accounts for a gap in the stack.\r
+  \r
+  The stack for this application looks like this (high addr on top)\r
+  [EBC entry point arguments]\r
+  [VM stack]\r
+  [EBC stack]\r
+\r
+  The EBC assumes that its arguments are at the top of its stack, which\r
+  is where the VM stack is really. Therefore if the EBC does memory\r
+  accesses into the VM stack area, then we need to convert the address\r
+  to point to the EBC entry point arguments area. Do this here.\r
+\r
+Arguments:\r
+\r
+  VmPtr    - pointer to VM context\r
+  Addr  - address of interest\r
+\r
+Returns:\r
+\r
+  The unchanged address if it's not in the VM stack region. Otherwise, \r
+  adjust for the stack gap and return the modified address.\r
+  \r
+--*/\r
+{ \r
+  ASSERT(((Addr < VmPtr->LowStackTop) || (Addr > VmPtr->HighStackBottom)));\r
+  return Addr;\r
+}\r
+\r
+STATIC\r
+UINTN\r
+VmReadMemN (\r
+  IN VM_CONTEXT    *VmPtr,\r
+  IN UINTN         Addr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Read a natural value from memory. May or may not be aligned.\r
+  \r
+Arguments:\r
+  VmPtr   - current VM context\r
+  Addr    - the address to read from\r
+\r
+Returns:\r
+  The natural value at address Addr.\r
+  \r
+--*/\r
+{\r
+  UINTN   Data;\r
+  volatile UINT32  Size;\r
+  UINT8   *FromPtr;\r
+  UINT8   *ToPtr;\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINTN))) {\r
+    return * (UINTN *) Addr;\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  Data    = 0;\r
+  FromPtr = (UINT8 *) Addr;\r
+  ToPtr   = (UINT8 *) &Data;\r
+\r
+  for (Size = 0; Size < sizeof (Data); Size++) {\r
+    *ToPtr = *FromPtr;\r
+    ToPtr++;\r
+    FromPtr++;\r
+  }\r
+\r
+  return Data;\r
+}\r
+\r
+UINT64\r
+GetVmVersion (\r
+  VOID\r
+  )\r
+{\r
+  return (UINT64) (((VM_MAJOR_VERSION & 0xFFFF) << 16) | ((VM_MINOR_VERSION & 0xFFFF)));\r
+}\r