+++ /dev/null
-/*++\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
- Ebc.dxs\r
-\r
-Abstract:\r
-\r
- Dependency expression file for EBC VM.\r
- \r
---*/\r
-#include <DxeDepex.h> \r
-\r
-DEPENDENCY_START\r
- TRUE\r
-DEPENDENCY_END\r
+++ /dev/null
-#/** @file\r
-# Component description file for Ebc module.\r
-#\r
-# This module for the EBC virtual machine implementation produces \r
-# EBC and EBC debug support protocols.\r
-# Copyright (c) 2006 - 2007, Intel Corporation\r
-#\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
-# 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
-#\r
-#**/\r
-\r
-################################################################################\r
-#\r
-# Defines Section - statements that will be processed to create a Makefile.\r
-#\r
-################################################################################\r
-[Defines]\r
- INF_VERSION = 0x00010005\r
- BASE_NAME = Ebc\r
- FILE_GUID = 13AC6DD0-73D0-11D4-B06B-00AA00BD6DE7\r
- MODULE_TYPE = DXE_DRIVER\r
- VERSION_STRING = 1.0\r
- EDK_RELEASE_VERSION = 0x00020000\r
- EFI_SPECIFICATION_VERSION = 0x00020000\r
-\r
- ENTRY_POINT = InitializeEbcDriver\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-# VALID_ARCHITECTURES = IA32 X64 IPF\r
-#\r
-\r
-################################################################################\r
-#\r
-# Sources Section - list of files that are required for the build to succeed.\r
-#\r
-################################################################################\r
-\r
-[Sources.common]\r
- EbcExecute.h\r
- EbcExecute.c\r
- EbcInt.h\r
- EbcInt.c\r
-\r
-[Sources.Ia32]\r
- Ia32/EbcSupport.c\r
- Ia32/EbcLowLevel.S\r
- Ia32/EbcLowLevel.asm\r
-\r
-[Sources.X64]\r
- x64/EbcSupport.c\r
- x64/EbcLowLevel.S\r
- x64/EbcLowLevel.asm\r
-\r
-[Sources.IPF]\r
- Ipf/EbcSupport.h\r
- Ipf/EbcSupport.c\r
- Ipf/EbcLowLevel.s\r
-\r
-\r
-################################################################################\r
-#\r
-# Package Dependency Section - list of Package files that are required for\r
-# this module.\r
-#\r
-################################################################################\r
-\r
-[Packages]\r
- MdePkg/MdePkg.dec\r
- \r
-\r
-\r
-################################################################################\r
-#\r
-# Library Class Section - list of Library Classes that are required for\r
-# this module.\r
-#\r
-################################################################################\r
-\r
-[LibraryClasses]\r
- MemoryAllocationLib\r
- UefiBootServicesTableLib\r
- BaseMemoryLib\r
- UefiDriverEntryPoint\r
- DebugLib\r
- BaseLib\r
-\r
-\r
-################################################################################\r
-#\r
-# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
-# that this module uses or produces.\r
-#\r
-################################################################################\r
-\r
-[Protocols]\r
- gEfiDebugSupportProtocolGuid # PROTOCOL ALWAYS_PRODUCED\r
- gEfiEbcProtocolGuid # PROTOCOL ALWAYS_PRODUCED\r
-\r
-################################################################################\r
-#\r
-# Dependency Expression Section - list of Dependency expressions that are required for\r
-# this module.\r
-#\r
-################################################################################\r
-\r
-[Depex]\r
- TRUE\r
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>\r
-<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
- <MsaHeader>\r
- <ModuleName>Ebc</ModuleName>\r
- <ModuleType>DXE_DRIVER</ModuleType>\r
- <GuidValue>13AC6DD0-73D0-11D4-B06B-00AA00BD6DE7</GuidValue>\r
- <Version>1.0</Version>\r
- <Abstract>Component description file for Ebc module.</Abstract>\r
- <Description>This module for the EBC virtual machine implementation produces \r
- EBC and EBC debug support protocols.</Description>\r
- <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
- <License>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
- 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.</License>\r
- <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052</Specification>\r
- </MsaHeader>\r
- <ModuleDefinitions>\r
- <SupportedArchitectures>IA32 X64 IPF</SupportedArchitectures>\r
- <BinaryModule>false</BinaryModule>\r
- <OutputFileBasename>Ebc</OutputFileBasename>\r
- </ModuleDefinitions>\r
- <LibraryClassDefinitions>\r
- <LibraryClass Usage="ALWAYS_CONSUMED">\r
- <Keyword>BaseLib</Keyword>\r
- </LibraryClass>\r
- <LibraryClass Usage="ALWAYS_CONSUMED">\r
- <Keyword>DebugLib</Keyword>\r
- </LibraryClass>\r
- <LibraryClass Usage="ALWAYS_CONSUMED">\r
- <Keyword>UefiDriverEntryPoint</Keyword>\r
- </LibraryClass>\r
- <LibraryClass Usage="ALWAYS_CONSUMED">\r
- <Keyword>BaseMemoryLib</Keyword>\r
- </LibraryClass>\r
- <LibraryClass Usage="ALWAYS_CONSUMED">\r
- <Keyword>UefiBootServicesTableLib</Keyword>\r
- </LibraryClass>\r
- <LibraryClass Usage="ALWAYS_CONSUMED">\r
- <Keyword>MemoryAllocationLib</Keyword>\r
- </LibraryClass>\r
- </LibraryClassDefinitions>\r
- <SourceFiles>\r
- <Filename>EbcInt.c</Filename>\r
- <Filename>EbcInt.h</Filename>\r
- <Filename>EbcExecute.c</Filename>\r
- <Filename>EbcExecute.h</Filename>\r
- <Filename>Ebc.dxs</Filename>\r
- <Filename SupArchList="IA32" ToolChainFamily="MSFT">Ia32/EbcLowLevel.asm</Filename>\r
- <Filename SupArchList="IA32" ToolChainFamily="GCC">Ia32/EbcLowLevel.S</Filename>\r
- <Filename SupArchList="IA32">Ia32/EbcSupport.c</Filename>\r
- <Filename SupArchList="X64" ToolChainFamily="MSFT">x64/EbcLowLevel.asm</Filename>\r
- <Filename SupArchList="X64" ToolChainFamily="GCC">x64/EbcLowLevel.S</Filename>\r
- <Filename SupArchList="X64">x64/EbcSupport.c</Filename>\r
- <Filename SupArchList="IPF">Ipf/EbcLowLevel.s</Filename>\r
- <Filename SupArchList="IPF">Ipf/EbcSupport.c</Filename>\r
- <Filename SupArchList="IPF">Ipf/EbcSupport.h</Filename>\r
- </SourceFiles>\r
- <PackageDependencies>\r
- <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
- <Package PackageGuid="2759ded5-bb57-4b06-af4f-c398fa552719"/>\r
- </PackageDependencies>\r
- <Protocols>\r
- <Protocol Usage="ALWAYS_PRODUCED">\r
- <ProtocolCName>gEfiEbcProtocolGuid</ProtocolCName>\r
- </Protocol>\r
- <Protocol Usage="ALWAYS_PRODUCED">\r
- <ProtocolCName>gEfiDebugSupportProtocolGuid</ProtocolCName>\r
- </Protocol>\r
- </Protocols>\r
- <Externs>\r
- <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
- <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
- <Extern>\r
- <ModuleEntryPoint>InitializeEbcDriver</ModuleEntryPoint>\r
- </Extern>\r
- </Externs>\r
-</ModuleSurfaceArea>\r
+++ /dev/null
-/*++\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
+++ /dev/null
-/*++\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.h\r
-\r
-Abstract:\r
-\r
- Header file for Virtual Machine support. Contains EBC defines that can\r
- be of use to a disassembler for the most part. Also provides function \r
- prototypes for VM functions.\r
-\r
---*/\r
-\r
-#ifndef _EBC_EXECUTE_H_\r
-#define _EBC_EXECUTE_H_\r
-\r
-//\r
-// VM major/minor version\r
-//\r
-#define VM_MAJOR_VERSION 1\r
-#define VM_MINOR_VERSION 0\r
-\r
-//\r
-// Macros to check and set alignment\r
-//\r
-#define ASSERT_ALIGNED(addr, size) ASSERT (!((UINT32) (addr) & (size - 1)))\r
-#define IS_ALIGNED(addr, size) !((UINT32) (addr) & (size - 1))\r
-\r
-//\r
-// Define a macro to get the operand. Then we can change it to be either a\r
-// direct read or have it call a function to read memory.\r
-//\r
-#define GETOPERANDS(pVM) (UINT8) (*(UINT8 *) (pVM->Ip + 1))\r
-#define GETOPCODE(pVM) (UINT8) (*(UINT8 *) pVM->Ip)\r
-\r
-//\r
-// Bit masks for opcode encodings\r
-//\r
-#define OPCODE_M_OPCODE 0x3F // bits of interest for first level decode\r
-#define OPCODE_M_IMMDATA 0x80\r
-#define OPCODE_M_IMMDATA64 0x40\r
-#define OPCODE_M_64BIT 0x40 // for CMP\r
-#define OPCODE_M_RELADDR 0x10 // for CALL instruction\r
-#define OPCODE_M_CMPI32_DATA 0x80 // for CMPI\r
-#define OPCODE_M_CMPI64 0x40 // for CMPI 32 or 64 bit comparison\r
-#define OPERAND_M_MOVIN_N 0x80\r
-#define OPERAND_M_CMPI_INDEX 0x10\r
-\r
-//\r
-// Masks for instructions that encode presence of indexes for operand1 and/or\r
-// operand2.\r
-//\r
-#define OPCODE_M_IMMED_OP1 0x80\r
-#define OPCODE_M_IMMED_OP2 0x40\r
-\r
-//\r
-// Bit masks for operand encodings\r
-//\r
-#define OPERAND_M_INDIRECT1 0x08\r
-#define OPERAND_M_INDIRECT2 0x80\r
-#define OPERAND_M_OP1 0x07\r
-#define OPERAND_M_OP2 0x70\r
-\r
-//\r
-// Masks for data manipulation instructions\r
-//\r
-#define DATAMANIP_M_64 0x40 // 64-bit width operation\r
-#define DATAMANIP_M_IMMDATA 0x80\r
-\r
-//\r
-// For MOV instructions, need a mask for the opcode when immediate\r
-// data applies to R2.\r
-//\r
-#define OPCODE_M_IMMED_OP2 0x40\r
-\r
-//\r
-// The MOVI/MOVIn instructions use bit 6 of operands byte to indicate\r
-// if an index is present. Then bits 4 and 5 are used to indicate the width\r
-// of the move.\r
-//\r
-#define MOVI_M_IMMDATA 0x40\r
-#define MOVI_M_DATAWIDTH 0xC0\r
-#define MOVI_DATAWIDTH16 0x40\r
-#define MOVI_DATAWIDTH32 0x80\r
-#define MOVI_DATAWIDTH64 0xC0\r
-#define MOVI_M_MOVEWIDTH 0x30\r
-#define MOVI_MOVEWIDTH8 0x00\r
-#define MOVI_MOVEWIDTH16 0x10\r
-#define MOVI_MOVEWIDTH32 0x20\r
-#define MOVI_MOVEWIDTH64 0x30\r
-\r
-//\r
-// Masks for CALL instruction encodings\r
-//\r
-#define OPERAND_M_RELATIVE_ADDR 0x10\r
-#define OPERAND_M_NATIVE_CALL 0x20\r
-\r
-//\r
-// Masks for decoding push/pop instructions\r
-//\r
-#define PUSHPOP_M_IMMDATA 0x80 // opcode bit indicating immediate data\r
-#define PUSHPOP_M_64 0x40 // opcode bit indicating 64-bit operation\r
-//\r
-// Mask for operand of JMP instruction\r
-//\r
-#define JMP_M_RELATIVE 0x10\r
-#define JMP_M_CONDITIONAL 0x80\r
-#define JMP_M_CS 0x40\r
-\r
-//\r
-// Macros to determine if a given operand is indirect\r
-//\r
-#define OPERAND1_INDIRECT(op) ((op) & OPERAND_M_INDIRECT1)\r
-#define OPERAND2_INDIRECT(op) ((op) & OPERAND_M_INDIRECT2)\r
-\r
-//\r
-// Macros to extract the operands from second byte of instructions\r
-//\r
-#define OPERAND1_REGNUM(op) ((op) & OPERAND_M_OP1)\r
-#define OPERAND2_REGNUM(op) (((op) & OPERAND_M_OP2) >> 4)\r
-\r
-#define OPERAND1_CHAR(op) ('0' + OPERAND1_REGNUM (op))\r
-#define OPERAND2_CHAR(op) ('0' + OPERAND2_REGNUM (op))\r
-\r
-#define OPERAND1_REGDATA(pvm, op) pvm->R[OPERAND1_REGNUM (op)]\r
-#define OPERAND2_REGDATA(pvm, op) pvm->R[OPERAND2_REGNUM (op)]\r
-\r
-//\r
-// Condition masks usually for byte 1 encodings of code\r
-//\r
-#define CONDITION_M_CONDITIONAL 0x80\r
-#define CONDITION_M_CS 0x40\r
-\r
-//\r
-// Bits in the VM->StopFlags field\r
-//\r
-#define STOPFLAG_APP_DONE 0x0001\r
-#define STOPFLAG_BREAKPOINT 0x0002\r
-#define STOPFLAG_INVALID_BREAK 0x0004\r
-#define STOPFLAG_BREAK_ON_CALLEX 0x0008\r
-\r
-//\r
-// Masks for working with the VM flags register\r
-//\r
-#define VMFLAGS_CC 0x0001 // condition flag\r
-#define VMFLAGS_STEP 0x0002 // step instruction mode\r
-#define VMFLAGS_ALL_VALID (VMFLAGS_CC | VMFLAGS_STEP)\r
-\r
-//\r
-// Macros for operating on the VM flags register\r
-//\r
-#define VMFLAG_SET(pVM, Flag) (pVM->Flags |= (Flag))\r
-#define VMFLAG_ISSET(pVM, Flag) ((pVM->Flags & (Flag)) ? 1 : 0)\r
-#define VMFLAG_CLEAR(pVM, Flag) (pVM->Flags &= ~(Flag))\r
-\r
-//\r
-// Debug macro\r
-//\r
-#define EBCMSG(s) gST->ConOut->OutputString (gST->ConOut, s)\r
-\r
-//\r
-// Define OPCODES\r
-//\r
-#define OPCODE_BREAK 0x00\r
-#define OPCODE_JMP 0x01\r
-#define OPCODE_JMP8 0x02\r
-#define OPCODE_CALL 0x03\r
-#define OPCODE_RET 0x04\r
-#define OPCODE_CMPEQ 0x05\r
-#define OPCODE_CMPLTE 0x06\r
-#define OPCODE_CMPGTE 0x07\r
-#define OPCODE_CMPULTE 0x08\r
-#define OPCODE_CMPUGTE 0x09\r
-#define OPCODE_NOT 0x0A\r
-#define OPCODE_NEG 0x0B\r
-#define OPCODE_ADD 0x0C\r
-#define OPCODE_SUB 0x0D\r
-#define OPCODE_MUL 0x0E\r
-#define OPCODE_MULU 0x0F\r
-#define OPCODE_DIV 0x10\r
-#define OPCODE_DIVU 0x11\r
-#define OPCODE_MOD 0x12\r
-#define OPCODE_MODU 0x13\r
-#define OPCODE_AND 0x14\r
-#define OPCODE_OR 0x15\r
-#define OPCODE_XOR 0x16\r
-#define OPCODE_SHL 0x17\r
-#define OPCODE_SHR 0x18\r
-#define OPCODE_ASHR 0x19\r
-#define OPCODE_EXTNDB 0x1A\r
-#define OPCODE_EXTNDW 0x1B\r
-#define OPCODE_EXTNDD 0x1C\r
-#define OPCODE_MOVBW 0x1D\r
-#define OPCODE_MOVWW 0x1E\r
-#define OPCODE_MOVDW 0x1F\r
-#define OPCODE_MOVQW 0x20\r
-#define OPCODE_MOVBD 0x21\r
-#define OPCODE_MOVWD 0x22\r
-#define OPCODE_MOVDD 0x23\r
-#define OPCODE_MOVQD 0x24\r
-#define OPCODE_MOVSNW 0x25 // Move signed natural with word index\r
-#define OPCODE_MOVSND 0x26 // Move signed natural with dword index\r
-//\r
-// #define OPCODE_27 0x27\r
-//\r
-#define OPCODE_MOVQQ 0x28 // Does this go away?\r
-#define OPCODE_LOADSP 0x29\r
-#define OPCODE_STORESP 0x2A\r
-#define OPCODE_PUSH 0x2B\r
-#define OPCODE_POP 0x2C\r
-#define OPCODE_CMPIEQ 0x2D\r
-#define OPCODE_CMPILTE 0x2E\r
-#define OPCODE_CMPIGTE 0x2F\r
-#define OPCODE_CMPIULTE 0x30\r
-#define OPCODE_CMPIUGTE 0x31\r
-#define OPCODE_MOVNW 0x32\r
-#define OPCODE_MOVND 0x33\r
-//\r
-// #define OPCODE_34 0x34\r
-//\r
-#define OPCODE_PUSHN 0x35\r
-#define OPCODE_POPN 0x36\r
-#define OPCODE_MOVI 0x37\r
-#define OPCODE_MOVIN 0x38\r
-#define OPCODE_MOVREL 0x39\r
-\r
-EFI_STATUS\r
-EbcExecute (\r
- IN VM_CONTEXT *VmPtr\r
- )\r
-;\r
-\r
-\r
-\r
-UINT64\r
-GetVmVersion (\r
- VOID\r
- )\r
-;\r
-\r
-EFI_STATUS\r
-VmWriteMemN (\r
- IN VM_CONTEXT *VmPtr,\r
- IN UINTN Addr,\r
- IN UINTN Data\r
- )\r
-;\r
-\r
-EFI_STATUS\r
-VmWriteMem64 (\r
- IN VM_CONTEXT *VmPtr,\r
- UINTN Addr,\r
- IN UINT64 Data\r
- )\r
-;\r
-\r
-//\r
-// Define a protocol for an EBC VM test interface.\r
-//\r
-#define EFI_EBC_VM_TEST_PROTOCOL_GUID \\r
- { \\r
- 0xAAEACCFDL, 0xF27B, 0x4C17, { 0xB6, 0x10, 0x75, 0xCA, 0x1F, 0x2D, 0xFB, 0x52 } \\r
- }\r
-\r
-//\r
-// Define for forward reference.\r
-//\r
-typedef struct _EFI_EBC_VM_TEST_PROTOCOL EFI_EBC_VM_TEST_PROTOCOL;\r
-\r
-typedef\r
-EFI_STATUS\r
-(*EBC_VM_TEST_EXECUTE) (\r
- IN EFI_EBC_VM_TEST_PROTOCOL * This,\r
- IN VM_CONTEXT * VmPtr,\r
- IN OUT UINTN *InstructionCount\r
- );\r
-\r
-typedef\r
-EFI_STATUS\r
-(*EBC_VM_TEST_ASM) (\r
- IN EFI_EBC_VM_TEST_PROTOCOL * This,\r
- IN CHAR16 *AsmText,\r
- IN OUT INT8 *Buffer,\r
- IN OUT UINTN *BufferLen\r
- );\r
-\r
-typedef\r
-EFI_STATUS\r
-(*EBC_VM_TEST_DASM) (\r
- IN EFI_EBC_VM_TEST_PROTOCOL * This,\r
- IN OUT CHAR16 *AsmText,\r
- IN OUT INT8 *Buffer,\r
- IN OUT UINTN *Len\r
- );\r
-\r
-//\r
-// Prototype for the actual EBC test protocol interface\r
-//\r
-struct _EFI_EBC_VM_TEST_PROTOCOL {\r
- EBC_VM_TEST_EXECUTE Execute;\r
- EBC_VM_TEST_ASM Assemble;\r
- EBC_VM_TEST_DASM Disassemble;\r
-};\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
-#endif // ifndef _EBC_EXECUTE_H_\r
+++ /dev/null
-/*++\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
- EbcInt.c\r
-\r
-Abstract:\r
-\r
- Top level module for the EBC virtual machine implementation.\r
- Provides auxilliary support routines for the VM. That is, routines\r
- that are not particularly related to VM execution of EBC instructions.\r
-\r
---*/\r
-\r
-#include "EbcInt.h"\r
-#include "EbcExecute.h"\r
-\r
-//\r
-// We'll keep track of all thunks we create in a linked list. Each\r
-// thunk is tied to an image handle, so we have a linked list of\r
-// image handles, with each having a linked list of thunks allocated\r
-// to that image handle.\r
-//\r
-typedef struct _EBC_THUNK_LIST {\r
- VOID *ThunkBuffer;\r
- struct _EBC_THUNK_LIST *Next;\r
-} EBC_THUNK_LIST;\r
-\r
-typedef struct _EBC_IMAGE_LIST {\r
- struct _EBC_IMAGE_LIST *Next;\r
- EFI_HANDLE ImageHandle;\r
- EBC_THUNK_LIST *ThunkList;\r
-} EBC_IMAGE_LIST;\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcUnloadImage (\r
- IN EFI_EBC_PROTOCOL *This,\r
- IN EFI_HANDLE ImageHandle\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcCreateThunk (\r
- IN EFI_EBC_PROTOCOL *This,\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *EbcEntryPoint,\r
- OUT VOID **Thunk\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcGetVersion (\r
- IN EFI_EBC_PROTOCOL *This,\r
- IN OUT UINT64 *Version\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeEbcCallback (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This\r
- );\r
-\r
-STATIC\r
-VOID\r
-EFIAPI\r
-CommonEbcExceptionHandler (\r
- IN EFI_EXCEPTION_TYPE InterruptType,\r
- IN EFI_SYSTEM_CONTEXT SystemContext\r
- );\r
-\r
-STATIC\r
-VOID\r
-EFIAPI\r
-EbcPeriodicNotifyFunction (\r
- IN EFI_EVENT Event,\r
- IN VOID *Context\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugPeriodic (\r
- IN VM_CONTEXT *VmPtr\r
- );\r
-\r
-//\r
-// These two functions and the GUID are used to produce an EBC test protocol.\r
-// This functionality is definitely not required for execution.\r
-//\r
-STATIC\r
-EFI_STATUS\r
-InitEbcVmTestProtocol (\r
- IN EFI_HANDLE *Handle\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EbcVmTestUnsupported (\r
- VOID\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcRegisterICacheFlush (\r
- IN EFI_EBC_PROTOCOL *This,\r
- IN EBC_ICACHE_FLUSH Flush\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugGetMaximumProcessorIndex (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
- OUT UINTN *MaxProcessorIndex\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugRegisterPeriodicCallback (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
- IN UINTN ProcessorIndex,\r
- IN EFI_PERIODIC_CALLBACK PeriodicCallback\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugRegisterExceptionCallback (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
- IN UINTN ProcessorIndex,\r
- IN EFI_EXCEPTION_CALLBACK ExceptionCallback,\r
- IN EFI_EXCEPTION_TYPE ExceptionType\r
- );\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugInvalidateInstructionCache (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
- IN UINTN ProcessorIndex,\r
- IN VOID *Start,\r
- IN UINT64 Length\r
- );\r
-\r
-//\r
-// We have one linked list of image handles for the whole world. Since\r
-// there should only be one interpreter, make them global. They must\r
-// also be global since the execution of an EBC image does not provide\r
-// a This pointer.\r
-//\r
-static EBC_IMAGE_LIST *mEbcImageList = NULL;\r
-\r
-//\r
-// Callback function to flush the icache after thunk creation\r
-//\r
-static EBC_ICACHE_FLUSH mEbcICacheFlush;\r
-\r
-//\r
-// These get set via calls by the debug agent\r
-//\r
-static EFI_PERIODIC_CALLBACK mDebugPeriodicCallback = NULL;\r
-static EFI_EXCEPTION_CALLBACK mDebugExceptionCallback[MAX_EBC_EXCEPTION + 1] = {NULL};\r
-static EFI_GUID mEfiEbcVmTestProtocolGuid = EFI_EBC_VM_TEST_PROTOCOL_GUID;\r
-\r
-static VOID* mStackBuffer[MAX_STACK_NUM];\r
-static EFI_HANDLE mStackBufferIndex[MAX_STACK_NUM];\r
-static UINTN mStackNum = 0;\r
-\r
-//\r
-// Event for Periodic callback\r
-//\r
-static EFI_EVENT mEbcPeriodicEvent;\r
-VM_CONTEXT *mVmPtr = NULL;\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeEbcDriver (\r
- IN EFI_HANDLE ImageHandle,\r
- IN EFI_SYSTEM_TABLE *SystemTable\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Initializes the VM EFI interface. Allocates memory for the VM interface\r
- and registers the VM protocol.\r
-\r
-Arguments:\r
-\r
- ImageHandle - EFI image handle.\r
- SystemTable - Pointer to the EFI system table.\r
-\r
-Returns:\r
- Standard EFI status code.\r
-\r
---*/\r
-{\r
- EFI_EBC_PROTOCOL *EbcProtocol;\r
- EFI_EBC_PROTOCOL *OldEbcProtocol;\r
- EFI_STATUS Status;\r
- EFI_DEBUG_SUPPORT_PROTOCOL *EbcDebugProtocol;\r
- EFI_HANDLE *HandleBuffer;\r
- UINTN NumHandles;\r
- UINTN Index;\r
- BOOLEAN Installed;\r
-\r
- EbcProtocol = NULL;\r
- EbcDebugProtocol = NULL;\r
-\r
- //\r
- // Allocate memory for our protocol. Then fill in the blanks.\r
- //\r
- EbcProtocol = AllocatePool (sizeof (EFI_EBC_PROTOCOL));\r
-\r
- if (EbcProtocol == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
-\r
- EbcProtocol->CreateThunk = EbcCreateThunk;\r
- EbcProtocol->UnloadImage = EbcUnloadImage;\r
- EbcProtocol->RegisterICacheFlush = EbcRegisterICacheFlush;\r
- EbcProtocol->GetVersion = EbcGetVersion;\r
- mEbcICacheFlush = NULL;\r
-\r
- //\r
- // Find any already-installed EBC protocols and uninstall them\r
- //\r
- Installed = FALSE;\r
- HandleBuffer = NULL;\r
- Status = gBS->LocateHandleBuffer (\r
- ByProtocol,\r
- &gEfiEbcProtocolGuid,\r
- NULL,\r
- &NumHandles,\r
- &HandleBuffer\r
- );\r
- if (Status == EFI_SUCCESS) {\r
- //\r
- // Loop through the handles\r
- //\r
- for (Index = 0; Index < NumHandles; Index++) {\r
- Status = gBS->HandleProtocol (\r
- HandleBuffer[Index],\r
- &gEfiEbcProtocolGuid,\r
- (VOID **) &OldEbcProtocol\r
- );\r
- if (Status == EFI_SUCCESS) {\r
- if (gBS->ReinstallProtocolInterface (\r
- HandleBuffer[Index],\r
- &gEfiEbcProtocolGuid,\r
- OldEbcProtocol,\r
- EbcProtocol\r
- ) == EFI_SUCCESS) {\r
- Installed = TRUE;\r
- }\r
- }\r
- }\r
- }\r
-\r
- if (HandleBuffer != NULL) {\r
- FreePool (HandleBuffer);\r
- HandleBuffer = NULL;\r
- }\r
- //\r
- // Add the protocol so someone can locate us if we haven't already.\r
- //\r
- if (!Installed) {\r
- Status = gBS->InstallProtocolInterface (\r
- &ImageHandle,\r
- &gEfiEbcProtocolGuid,\r
- EFI_NATIVE_INTERFACE,\r
- EbcProtocol\r
- );\r
- if (EFI_ERROR (Status)) {\r
- FreePool (EbcProtocol);\r
- return Status;\r
- }\r
- }\r
-\r
- Status = InitEBCStack();\r
- if (EFI_ERROR(Status)) {\r
- goto ErrorExit;\r
- }\r
-\r
- //\r
- // Allocate memory for our debug protocol. Then fill in the blanks.\r
- //\r
- EbcDebugProtocol = AllocatePool (sizeof (EFI_DEBUG_SUPPORT_PROTOCOL));\r
-\r
- if (EbcDebugProtocol == NULL) {\r
- goto ErrorExit;\r
- }\r
-\r
- EbcDebugProtocol->Isa = IsaEbc;\r
- EbcDebugProtocol->GetMaximumProcessorIndex = EbcDebugGetMaximumProcessorIndex;\r
- EbcDebugProtocol->RegisterPeriodicCallback = EbcDebugRegisterPeriodicCallback;\r
- EbcDebugProtocol->RegisterExceptionCallback = EbcDebugRegisterExceptionCallback;\r
- EbcDebugProtocol->InvalidateInstructionCache = EbcDebugInvalidateInstructionCache;\r
-\r
- //\r
- // Add the protocol so the debug agent can find us\r
- //\r
- Status = gBS->InstallProtocolInterface (\r
- &ImageHandle,\r
- &gEfiDebugSupportProtocolGuid,\r
- EFI_NATIVE_INTERFACE,\r
- EbcDebugProtocol\r
- );\r
- //\r
- // This is recoverable, so free the memory and continue.\r
- //\r
- if (EFI_ERROR (Status)) {\r
- FreePool (EbcDebugProtocol);\r
- goto ErrorExit;\r
- }\r
- //\r
- // Install EbcDebugSupport Protocol Successfully\r
- // Now we need to initialize the Ebc default Callback\r
- //\r
- Status = InitializeEbcCallback (EbcDebugProtocol);\r
-\r
- //\r
- // Produce a VM test interface protocol. Not required for execution.\r
- //\r
- DEBUG_CODE_BEGIN ();\r
- InitEbcVmTestProtocol (&ImageHandle);\r
- DEBUG_CODE_END ();\r
-\r
- return EFI_SUCCESS;\r
-\r
-ErrorExit:\r
- FreeEBCStack();\r
- HandleBuffer = NULL;\r
- Status = gBS->LocateHandleBuffer (\r
- ByProtocol,\r
- &gEfiEbcProtocolGuid,\r
- NULL,\r
- &NumHandles,\r
- &HandleBuffer\r
- );\r
- if (Status == EFI_SUCCESS) {\r
- //\r
- // Loop through the handles\r
- //\r
- for (Index = 0; Index < NumHandles; Index++) {\r
- Status = gBS->HandleProtocol (\r
- HandleBuffer[Index],\r
- &gEfiEbcProtocolGuid,\r
- (VOID **) &OldEbcProtocol\r
- );\r
- if (Status == EFI_SUCCESS) {\r
- gBS->UninstallProtocolInterface (\r
- HandleBuffer[Index],\r
- &gEfiEbcProtocolGuid,\r
- OldEbcProtocol\r
- );\r
- }\r
- }\r
- }\r
-\r
- if (HandleBuffer != NULL) {\r
- FreePool (HandleBuffer);\r
- HandleBuffer = NULL;\r
- }\r
-\r
- FreePool (EbcProtocol);\r
-\r
- return Status;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcCreateThunk (\r
- IN EFI_EBC_PROTOCOL *This,\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *EbcEntryPoint,\r
- OUT VOID **Thunk\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This is the top-level routine plugged into the EBC protocol. Since thunks\r
- are very processor-specific, from here we dispatch directly to the very\r
- processor-specific routine EbcCreateThunks().\r
-\r
-Arguments:\r
-\r
- This - protocol instance pointer\r
- ImageHandle - handle to the image. The EBC interpreter may use this to keep\r
- track of any resource allocations performed in loading and\r
- executing the image.\r
- EbcEntryPoint - the entry point for the image (as defined in the file header)\r
- Thunk - pointer to thunk pointer where the address of the created\r
- thunk is returned.\r
-\r
-Returns:\r
-\r
- EFI_STATUS\r
-\r
---*/\r
-{\r
- EFI_STATUS Status;\r
-\r
- Status = EbcCreateThunks (\r
- ImageHandle,\r
- EbcEntryPoint,\r
- Thunk,\r
- FLAG_THUNK_ENTRY_POINT\r
- );\r
- return Status;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugGetMaximumProcessorIndex (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
- OUT UINTN *MaxProcessorIndex\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This EBC debugger protocol service is called by the debug agent\r
-\r
-Arguments:\r
-\r
- This - pointer to the caller's debug support protocol interface\r
- MaxProcessorIndex - pointer to a caller allocated UINTN in which the maximum\r
- processor index is returned.\r
-\r
-Returns:\r
-\r
- Standard EFI_STATUS\r
-\r
---*/\r
-{\r
- *MaxProcessorIndex = 0;\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugRegisterPeriodicCallback (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
- IN UINTN ProcessorIndex,\r
- IN EFI_PERIODIC_CALLBACK PeriodicCallback\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This protocol service is called by the debug agent to register a function\r
- for us to call on a periodic basis.\r
-\r
-\r
-Arguments:\r
-\r
- This - pointer to the caller's debug support protocol interface\r
- PeriodicCallback - pointer to the function to call periodically\r
-\r
-Returns:\r
-\r
- Always EFI_SUCCESS\r
-\r
---*/\r
-{\r
- if ((mDebugPeriodicCallback == NULL) && (PeriodicCallback == NULL)) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- if ((mDebugPeriodicCallback != NULL) && (PeriodicCallback != NULL)) {\r
- return EFI_ALREADY_STARTED;\r
- }\r
-\r
- mDebugPeriodicCallback = PeriodicCallback;\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugRegisterExceptionCallback (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
- IN UINTN ProcessorIndex,\r
- IN EFI_EXCEPTION_CALLBACK ExceptionCallback,\r
- IN EFI_EXCEPTION_TYPE ExceptionType\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This protocol service is called by the debug agent to register a function\r
- for us to call when we detect an exception.\r
-\r
-\r
-Arguments:\r
-\r
- This - pointer to the caller's debug support protocol interface\r
- ExceptionCallback - pointer to the function to the exception\r
-\r
-Returns:\r
-\r
- Always EFI_SUCCESS\r
-\r
---*/\r
-{\r
- if ((ExceptionType < 0) || (ExceptionType > MAX_EBC_EXCEPTION)) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- if ((mDebugExceptionCallback[ExceptionType] == NULL) && (ExceptionCallback == NULL)) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- if ((mDebugExceptionCallback[ExceptionType] != NULL) && (ExceptionCallback != NULL)) {\r
- return EFI_ALREADY_STARTED;\r
- }\r
- mDebugExceptionCallback[ExceptionType] = ExceptionCallback;\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcDebugInvalidateInstructionCache (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
- IN UINTN ProcessorIndex,\r
- IN VOID *Start,\r
- IN UINT64 Length\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This EBC debugger protocol service is called by the debug agent. Required\r
- for DebugSupport compliance but is only stubbed out for EBC.\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
- EFI_SUCCESS\r
-\r
---*/\r
-{\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EbcDebugSignalException (\r
- IN EFI_EXCEPTION_TYPE ExceptionType,\r
- IN EXCEPTION_FLAGS ExceptionFlags,\r
- IN VM_CONTEXT *VmPtr\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- The VM interpreter calls this function when an exception is detected.\r
-\r
-Arguments:\r
-\r
- VmPtr - pointer to a VM context for passing info to the EFI debugger.\r
-\r
-Returns:\r
-\r
- EFI_SUCCESS if it returns at all\r
-\r
---*/\r
-{\r
- EFI_SYSTEM_CONTEXT_EBC EbcContext;\r
- EFI_SYSTEM_CONTEXT SystemContext;\r
-\r
- ASSERT ((ExceptionType >= 0) && (ExceptionType <= MAX_EBC_EXCEPTION));\r
- //\r
- // Save the exception in the context passed in\r
- //\r
- VmPtr->ExceptionFlags |= ExceptionFlags;\r
- VmPtr->LastException = ExceptionType;\r
- //\r
- // If it's a fatal exception, then flag it in the VM context in case an\r
- // attached debugger tries to return from it.\r
- //\r
- if (ExceptionFlags & EXCEPTION_FLAG_FATAL) {\r
- VmPtr->StopFlags |= STOPFLAG_APP_DONE;\r
- }\r
-\r
- //\r
- // If someone's registered for exception callbacks, then call them.\r
- //\r
- // EBC driver will register default exception callback to report the\r
- // status code via the status code API\r
- //\r
- if (mDebugExceptionCallback[ExceptionType] != NULL) {\r
-\r
- //\r
- // Initialize the context structure\r
- //\r
- EbcContext.R0 = VmPtr->R[0];\r
- EbcContext.R1 = VmPtr->R[1];\r
- EbcContext.R2 = VmPtr->R[2];\r
- EbcContext.R3 = VmPtr->R[3];\r
- EbcContext.R4 = VmPtr->R[4];\r
- EbcContext.R5 = VmPtr->R[5];\r
- EbcContext.R6 = VmPtr->R[6];\r
- EbcContext.R7 = VmPtr->R[7];\r
- EbcContext.Ip = (UINT64)(UINTN)VmPtr->Ip;\r
- EbcContext.Flags = VmPtr->Flags;\r
- EbcContext.ControlFlags = 0;\r
- SystemContext.SystemContextEbc = &EbcContext;\r
-\r
- mDebugExceptionCallback[ExceptionType] (ExceptionType, SystemContext);\r
- //\r
- // Restore the context structure and continue to execute\r
- //\r
- VmPtr->R[0] = EbcContext.R0;\r
- VmPtr->R[1] = EbcContext.R1;\r
- VmPtr->R[2] = EbcContext.R2;\r
- VmPtr->R[3] = EbcContext.R3;\r
- VmPtr->R[4] = EbcContext.R4;\r
- VmPtr->R[5] = EbcContext.R5;\r
- VmPtr->R[6] = EbcContext.R6;\r
- VmPtr->R[7] = EbcContext.R7;\r
- VmPtr->Ip = (VMIP)(UINTN)EbcContext.Ip;\r
- VmPtr->Flags = EbcContext.Flags;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-InitializeEbcCallback (\r
- IN EFI_DEBUG_SUPPORT_PROTOCOL *This\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- To install default Callback function for the VM interpreter.\r
-\r
-Arguments:\r
-\r
- This - pointer to the instance of DebugSupport protocol\r
-\r
-Returns:\r
-\r
- None\r
-\r
---*/\r
-{\r
- INTN Index;\r
- EFI_STATUS Status;\r
-\r
- //\r
- // For ExceptionCallback\r
- //\r
- for (Index = 0; Index <= MAX_EBC_EXCEPTION; Index++) {\r
- EbcDebugRegisterExceptionCallback (\r
- This,\r
- 0,\r
- CommonEbcExceptionHandler,\r
- Index\r
- );\r
- }\r
-\r
- //\r
- // For PeriodicCallback\r
- //\r
- Status = gBS->CreateEvent (\r
- EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
- TPL_NOTIFY,\r
- EbcPeriodicNotifyFunction,\r
- &mVmPtr,\r
- &mEbcPeriodicEvent\r
- );\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
-\r
- Status = gBS->SetTimer (\r
- mEbcPeriodicEvent,\r
- TimerPeriodic,\r
- EBC_VM_PERIODIC_CALLBACK_RATE\r
- );\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-VOID\r
-CommonEbcExceptionHandler (\r
- IN EFI_EXCEPTION_TYPE InterruptType,\r
- IN EFI_SYSTEM_CONTEXT SystemContext\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- The default Exception Callback for the VM interpreter.\r
- In this function, we report status code, and print debug information\r
- about EBC_CONTEXT, then dead loop.\r
-\r
-Arguments:\r
-\r
- InterruptType - Interrupt type.\r
- SystemContext - EBC system context.\r
-\r
-Returns:\r
-\r
- None\r
-\r
---*/\r
-{\r
- //\r
- // We deadloop here to make it easy to debug this issue.\r
- //\r
- ASSERT (FALSE);\r
-\r
- return ;\r
-}\r
-\r
-STATIC\r
-VOID\r
-EFIAPI\r
-EbcPeriodicNotifyFunction (\r
- IN EFI_EVENT Event,\r
- IN VOID *Context\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- The periodic callback function for EBC VM interpreter, which is used\r
- to support the EFI debug support protocol.\r
-\r
-Arguments:\r
-\r
- Event - The Periodic Callback Event.\r
- Context - It should be the address of VM_CONTEXT pointer.\r
-\r
-Returns:\r
-\r
- None.\r
-\r
---*/\r
-{\r
- VM_CONTEXT *VmPtr;\r
-\r
- VmPtr = *(VM_CONTEXT **)Context;\r
-\r
- if (VmPtr != NULL) {\r
- EbcDebugPeriodic (VmPtr);\r
- }\r
-\r
- return ;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EbcDebugPeriodic (\r
- IN VM_CONTEXT *VmPtr\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- The VM interpreter calls this function on a periodic basis to support\r
- the EFI debug support protocol.\r
-\r
-Arguments:\r
-\r
- VmPtr - pointer to a VM context for passing info to the debugger.\r
-\r
-Returns:\r
-\r
- Standard EFI status.\r
-\r
---*/\r
-{\r
- EFI_SYSTEM_CONTEXT_EBC EbcContext;\r
- EFI_SYSTEM_CONTEXT SystemContext;\r
-\r
- //\r
- // If someone's registered for periodic callbacks, then call them.\r
- //\r
- if (mDebugPeriodicCallback != NULL) {\r
-\r
- //\r
- // Initialize the context structure\r
- //\r
- EbcContext.R0 = VmPtr->R[0];\r
- EbcContext.R1 = VmPtr->R[1];\r
- EbcContext.R2 = VmPtr->R[2];\r
- EbcContext.R3 = VmPtr->R[3];\r
- EbcContext.R4 = VmPtr->R[4];\r
- EbcContext.R5 = VmPtr->R[5];\r
- EbcContext.R6 = VmPtr->R[6];\r
- EbcContext.R7 = VmPtr->R[7];\r
- EbcContext.Ip = (UINT64)(UINTN)VmPtr->Ip;\r
- EbcContext.Flags = VmPtr->Flags;\r
- EbcContext.ControlFlags = 0;\r
- SystemContext.SystemContextEbc = &EbcContext;\r
-\r
- mDebugPeriodicCallback (SystemContext);\r
-\r
- //\r
- // Restore the context structure and continue to execute\r
- //\r
- VmPtr->R[0] = EbcContext.R0;\r
- VmPtr->R[1] = EbcContext.R1;\r
- VmPtr->R[2] = EbcContext.R2;\r
- VmPtr->R[3] = EbcContext.R3;\r
- VmPtr->R[4] = EbcContext.R4;\r
- VmPtr->R[5] = EbcContext.R5;\r
- VmPtr->R[6] = EbcContext.R6;\r
- VmPtr->R[7] = EbcContext.R7;\r
- VmPtr->Ip = (VMIP)(UINTN)EbcContext.Ip;\r
- VmPtr->Flags = EbcContext.Flags;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcUnloadImage (\r
- IN EFI_EBC_PROTOCOL *This,\r
- IN EFI_HANDLE ImageHandle\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This routine is called by the core when an image is being unloaded from\r
- memory. Basically we now have the opportunity to do any necessary cleanup.\r
- Typically this will include freeing any memory allocated for thunk-creation.\r
-\r
-Arguments:\r
-\r
- This - protocol instance pointer\r
- ImageHandle - handle to the image being unloaded.\r
-\r
-Returns:\r
-\r
- EFI_INVALID_PARAMETER - the ImageHandle passed in was not found in\r
- the internal list of EBC image handles.\r
- EFI_STATUS - completed successfully\r
-\r
---*/\r
-{\r
- EBC_THUNK_LIST *ThunkList;\r
- EBC_THUNK_LIST *NextThunkList;\r
- EBC_IMAGE_LIST *ImageList;\r
- EBC_IMAGE_LIST *PrevImageList;\r
- //\r
- // First go through our list of known image handles and see if we've already\r
- // created an image list element for this image handle.\r
- //\r
- ReturnEBCStackByHandle(ImageHandle);\r
- PrevImageList = NULL;\r
- for (ImageList = mEbcImageList; ImageList != NULL; ImageList = ImageList->Next) {\r
- if (ImageList->ImageHandle == ImageHandle) {\r
- break;\r
- }\r
- //\r
- // Save the previous so we can connect the lists when we remove this one\r
- //\r
- PrevImageList = ImageList;\r
- }\r
-\r
- if (ImageList == NULL) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- //\r
- // Free up all the thunk buffers and thunks list elements for this image\r
- // handle.\r
- //\r
- ThunkList = ImageList->ThunkList;\r
- while (ThunkList != NULL) {\r
- NextThunkList = ThunkList->Next;\r
- FreePool (ThunkList->ThunkBuffer);\r
- FreePool (ThunkList);\r
- ThunkList = NextThunkList;\r
- }\r
- //\r
- // Now remove this image list element from the chain\r
- //\r
- if (PrevImageList == NULL) {\r
- //\r
- // Remove from head\r
- //\r
- mEbcImageList = ImageList->Next;\r
- } else {\r
- PrevImageList->Next = ImageList->Next;\r
- }\r
- //\r
- // Now free up the image list element\r
- //\r
- FreePool (ImageList);\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EbcAddImageThunk (\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *ThunkBuffer,\r
- IN UINT32 ThunkSize\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Add a thunk to our list of thunks for a given image handle.\r
- Also flush the instruction cache since we've written thunk code\r
- to memory that will be executed eventually.\r
-\r
-Arguments:\r
-\r
- ImageHandle - the image handle to which the thunk is tied\r
- ThunkBuffer - the buffer we've created/allocated\r
- ThunkSize - the size of the thunk memory allocated\r
-\r
-Returns:\r
-\r
- EFI_OUT_OF_RESOURCES - memory allocation failed\r
- EFI_SUCCESS - successful completion\r
-\r
---*/\r
-{\r
- EBC_THUNK_LIST *ThunkList;\r
- EBC_IMAGE_LIST *ImageList;\r
- EFI_STATUS Status;\r
-\r
- //\r
- // It so far so good, then flush the instruction cache\r
- //\r
- if (mEbcICacheFlush != NULL) {\r
- Status = mEbcICacheFlush ((EFI_PHYSICAL_ADDRESS) (UINTN) ThunkBuffer, ThunkSize);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
- }\r
- //\r
- // Go through our list of known image handles and see if we've already\r
- // created a image list element for this image handle.\r
- //\r
- for (ImageList = mEbcImageList; ImageList != NULL; ImageList = ImageList->Next) {\r
- if (ImageList->ImageHandle == ImageHandle) {\r
- break;\r
- }\r
- }\r
-\r
- if (ImageList == NULL) {\r
- //\r
- // Allocate a new one\r
- //\r
- ImageList = AllocatePool (sizeof (EBC_IMAGE_LIST));\r
-\r
- if (ImageList == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
-\r
- ImageList->ThunkList = NULL;\r
- ImageList->ImageHandle = ImageHandle;\r
- ImageList->Next = mEbcImageList;\r
- mEbcImageList = ImageList;\r
- }\r
- //\r
- // Ok, now create a new thunk element to add to the list\r
- //\r
- ThunkList = AllocatePool (sizeof (EBC_THUNK_LIST));\r
-\r
- if (ThunkList == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- //\r
- // Add it to the head of the list\r
- //\r
- ThunkList->Next = ImageList->ThunkList;\r
- ThunkList->ThunkBuffer = ThunkBuffer;\r
- ImageList->ThunkList = ThunkList;\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcRegisterICacheFlush (\r
- IN EFI_EBC_PROTOCOL *This,\r
- IN EBC_ICACHE_FLUSH Flush\r
- )\r
-{\r
- mEbcICacheFlush = Flush;\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-EbcGetVersion (\r
- IN EFI_EBC_PROTOCOL *This,\r
- IN OUT UINT64 *Version\r
- )\r
-{\r
- if (Version == NULL) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- *Version = GetVmVersion ();\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-GetEBCStack(\r
- EFI_HANDLE Handle,\r
- VOID **StackBuffer,\r
- UINTN *BufferIndex\r
- )\r
-{\r
- UINTN Index;\r
- EFI_TPL OldTpl;\r
- OldTpl = gBS->RaiseTPL(TPL_HIGH_LEVEL);\r
- for (Index = 0; Index < mStackNum; Index ++) {\r
- if (mStackBufferIndex[Index] == NULL) {\r
- mStackBufferIndex[Index] = Handle;\r
- break;\r
- }\r
- }\r
- gBS->RestoreTPL(OldTpl);\r
- if (Index == mStackNum) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- *BufferIndex = Index;\r
- *StackBuffer = mStackBuffer[Index];\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-ReturnEBCStack(\r
- UINTN Index\r
- )\r
-{\r
- mStackBufferIndex[Index] =NULL;\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-ReturnEBCStackByHandle(\r
- EFI_HANDLE Handle\r
- )\r
-{\r
- UINTN Index;\r
- for (Index = 0; Index < mStackNum; Index ++) {\r
- if (mStackBufferIndex[Index] == Handle) {\r
- break;\r
- }\r
- }\r
- if (Index == mStackNum) {\r
- return EFI_NOT_FOUND;\r
- }\r
- mStackBufferIndex[Index] = NULL;\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-InitEBCStack (\r
- VOID\r
- )\r
-{\r
- for (mStackNum = 0; mStackNum < MAX_STACK_NUM; mStackNum ++) {\r
- mStackBuffer[mStackNum] = AllocatePool(STACK_POOL_SIZE);\r
- mStackBufferIndex[mStackNum] = NULL;\r
- if (mStackBuffer[mStackNum] == NULL) {\r
- break;\r
- }\r
- }\r
- if (mStackNum == 0) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-FreeEBCStack(\r
- VOID\r
- )\r
-{\r
- UINTN Index;\r
- for (Index = 0; Index < mStackNum; Index ++) {\r
- FreePool(mStackBuffer[Index]);\r
- }\r
- return EFI_SUCCESS;\r
-}\r
-STATIC\r
-EFI_STATUS\r
-InitEbcVmTestProtocol (\r
- IN EFI_HANDLE *IHandle\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Produce an EBC VM test protocol that can be used for regression tests.\r
-\r
-Arguments:\r
-\r
- IHandle - handle on which to install the protocol.\r
-\r
-Returns:\r
-\r
- EFI_OUT_OF_RESOURCES - memory allocation failed\r
- EFI_SUCCESS - successful completion\r
-\r
---*/\r
-{\r
- EFI_HANDLE Handle;\r
- EFI_STATUS Status;\r
- EFI_EBC_VM_TEST_PROTOCOL *EbcVmTestProtocol;\r
-\r
- //\r
- // Allocate memory for the protocol, then fill in the fields\r
- //\r
- EbcVmTestProtocol = AllocatePool (sizeof (EFI_EBC_VM_TEST_PROTOCOL));\r
- if (EbcVmTestProtocol == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- EbcVmTestProtocol->Execute = (EBC_VM_TEST_EXECUTE) EbcExecuteInstructions;\r
-\r
- DEBUG_CODE_BEGIN ();\r
- EbcVmTestProtocol->Assemble = (EBC_VM_TEST_ASM) EbcVmTestUnsupported;\r
- EbcVmTestProtocol->Disassemble = (EBC_VM_TEST_DASM) EbcVmTestUnsupported;\r
- DEBUG_CODE_END ();\r
-\r
- //\r
- // Publish the protocol\r
- //\r
- Handle = NULL;\r
- Status = gBS->InstallProtocolInterface (&Handle, &mEfiEbcVmTestProtocolGuid, EFI_NATIVE_INTERFACE, EbcVmTestProtocol);\r
- if (EFI_ERROR (Status)) {\r
- FreePool (EbcVmTestProtocol);\r
- }\r
- return Status;\r
-}\r
-STATIC\r
-EFI_STATUS\r
-EbcVmTestUnsupported ()\r
-{\r
- return EFI_UNSUPPORTED;\r
-}\r
-\r
+++ /dev/null
-/*++ \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
- EbcInt.h\r
- \r
-Abstract:\r
-\r
- Main routines for the EBC interpreter. Includes the initialization and\r
- main interpreter routines. \r
- \r
---*/\r
-\r
-#ifndef _EBC_INT_H_\r
-#define _EBC_INT_H_\r
-\r
-//\r
-// The package level header files this module uses\r
-//\r
-#include <PiDxe.h>\r
-//\r
-// The protocols, PPI and GUID defintions for this module\r
-//\r
-#include <Protocol/DebugSupport.h>\r
-#include <Protocol/Ebc.h>\r
-//\r
-// The Library classes this module consumes\r
-//\r
-#include <Library/BaseLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-\r
-typedef INT64 VM_REGISTER;\r
-typedef UINT8 *VMIP; // instruction pointer for the VM\r
-typedef UINT32 EXCEPTION_FLAGS;\r
-\r
-typedef struct {\r
- VM_REGISTER R[8]; // General purpose registers.\r
- UINT64 Flags; // Flags register:\r
- // 0 Set to 1 if the result of the last compare was true\r
- // 1 Set to 1 if stepping\r
- // 2..63 Reserved.\r
- VMIP Ip; // Instruction pointer.\r
- UINTN LastException; //\r
- EXCEPTION_FLAGS ExceptionFlags; // to keep track of exceptions\r
- UINT32 StopFlags;\r
- UINT32 CompilerVersion; // via break(6)\r
- UINTN HighStackBottom; // bottom of the upper stack\r
- UINTN LowStackTop; // top of the lower stack\r
- UINT64 StackRetAddr; // location of final return address on stack\r
- UINTN *StackMagicPtr; // pointer to magic value on stack to detect corruption\r
- EFI_HANDLE ImageHandle; // for this EBC driver\r
- EFI_SYSTEM_TABLE *SystemTable; // for debugging only\r
- UINTN LastAddrConverted; // for debug\r
- UINTN LastAddrConvertedValue; // for debug\r
- VOID *FramePtr;\r
- VOID *EntryPoint; // entry point of EBC image\r
- UINTN ImageBase;\r
- VOID *StackPool;\r
- VOID *StackTop;\r
-} VM_CONTEXT;\r
-\r
-extern VM_CONTEXT *mVmPtr;\r
-\r
-//\r
-// Bits of exception flags field of VM context\r
-//\r
-#define EXCEPTION_FLAG_FATAL 0x80000000 // can't continue\r
-#define EXCEPTION_FLAG_ERROR 0x40000000 // bad, but try to continue\r
-#define EXCEPTION_FLAG_WARNING 0x20000000 // harmless problem\r
-#define EXCEPTION_FLAG_NONE 0x00000000 // for normal return\r
-//\r
-// Flags passed to the internal create-thunks function.\r
-//\r
-#define FLAG_THUNK_ENTRY_POINT 0x01 // thunk for an image entry point\r
-#define FLAG_THUNK_PROTOCOL 0x00 // thunk for an EBC protocol service\r
-//\r
-// Put this value at the bottom of the VM's stack gap so we can check it on\r
-// occasion to make sure the stack has not been corrupted.\r
-//\r
-#define VM_STACK_KEY_VALUE 0xDEADBEEF\r
-\r
-EFI_STATUS\r
-EbcCreateThunks (\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *EbcEntryPoint,\r
- OUT VOID **Thunk,\r
- IN UINT32 Flags\r
- )\r
-;\r
-\r
-EFI_STATUS\r
-EbcAddImageThunk (\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *ThunkBuffer,\r
- IN UINT32 ThunkSize\r
- )\r
-;\r
-\r
-//\r
-// The interpreter calls these when an exception is detected,\r
-// or as a periodic callback.\r
-//\r
-EFI_STATUS\r
-EbcDebugSignalException (\r
- IN EFI_EXCEPTION_TYPE ExceptionType,\r
- IN EXCEPTION_FLAGS ExceptionFlags,\r
- IN VM_CONTEXT *VmPtr\r
- )\r
-;\r
-\r
-//\r
-// Define a constant of how often to call the debugger periodic callback\r
-// function.\r
-//\r
-#define EFI_TIMER_UNIT_1MS (1000 * 10)\r
-#define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)\r
-#define STACK_POOL_SIZE (1024 * 1020)\r
-#define MAX_STACK_NUM 4\r
-\r
-EFI_STATUS\r
-EbcDebugSignalPeriodic (\r
- IN VM_CONTEXT *VmPtr\r
- )\r
-;\r
-\r
-//\r
-// External low level functions that are native-processor dependent\r
-//\r
-UINTN\r
-EbcLLGetEbcEntryPoint (\r
- VOID\r
- )\r
-;\r
-\r
-UINTN\r
-EbcLLGetStackPointer (\r
- VOID\r
- )\r
-;\r
-\r
-VOID\r
-EbcLLCALLEXNative (\r
- IN UINTN CallAddr,\r
- IN UINTN EbcSp,\r
- IN VOID *FramePtr\r
- )\r
-;\r
-\r
-VOID\r
-EbcLLCALLEX (\r
- IN VM_CONTEXT *VmPtr,\r
- IN UINTN CallAddr,\r
- IN UINTN EbcSp,\r
- IN VOID *FramePtr,\r
- IN UINT8 Size\r
- )\r
-;\r
-\r
-INT64\r
-EbcLLGetReturnValue (\r
- VOID\r
- )\r
-;\r
-\r
-EFI_STATUS\r
-GetEBCStack(\r
- EFI_HANDLE Handle,\r
- VOID **StackBuffer,\r
- UINTN *BufferIndex\r
- );\r
-\r
-EFI_STATUS\r
-ReturnEBCStack(\r
- UINTN Index\r
- );\r
-\r
-EFI_STATUS\r
-InitEBCStack (\r
- VOID\r
- );\r
-\r
-EFI_STATUS\r
-FreeEBCStack(\r
- VOID\r
- );\r
-\r
-EFI_STATUS\r
-ReturnEBCStackByHandle(\r
- EFI_HANDLE Handle\r
- );\r
-//\r
-// Defines for a simple EBC debugger interface\r
-//\r
-typedef struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL;\r
-\r
-#define EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID \\r
- { \\r
- 0x2a72d11e, 0x7376, 0x40f6, { 0x9c, 0x68, 0x23, 0xfa, 0x2f, 0xe3, 0x63, 0xf1 } \\r
- }\r
-\r
-typedef\r
-EFI_STATUS\r
-(*EBC_DEBUGGER_SIGNAL_EXCEPTION) (\r
- IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL * This,\r
- IN VM_CONTEXT * VmPtr,\r
- IN EFI_EXCEPTION_TYPE ExceptionType\r
- );\r
-\r
-typedef\r
-VOID\r
-(*EBC_DEBUGGER_DEBUG) (\r
- IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL * This,\r
- IN VM_CONTEXT * VmPtr\r
- );\r
-\r
-typedef\r
-UINT32\r
-(*EBC_DEBUGGER_DASM) (\r
- IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL * This,\r
- IN VM_CONTEXT * VmPtr,\r
- IN UINT16 *DasmString OPTIONAL,\r
- IN UINT32 DasmStringSize\r
- );\r
-\r
-//\r
-// This interface allows you to configure the EBC debug support\r
-// driver. For example, turn on or off saving and printing of\r
-// delta VM even if called. Or to even disable the entire interface,\r
-// in which case all functions become no-ops.\r
-//\r
-typedef\r
-EFI_STATUS\r
-(*EBC_DEBUGGER_CONFIGURE) (\r
- IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL * This,\r
- IN UINT32 ConfigId,\r
- IN UINTN ConfigValue\r
- );\r
-\r
-//\r
-// Prototype for the actual EBC debug support protocol interface\r
-//\r
-struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL {\r
- EBC_DEBUGGER_DEBUG Debugger;\r
- EBC_DEBUGGER_SIGNAL_EXCEPTION SignalException;\r
- EBC_DEBUGGER_DASM Dasm;\r
- EBC_DEBUGGER_CONFIGURE Configure;\r
-};\r
-\r
-typedef struct {\r
- EFI_EBC_PROTOCOL *This;\r
- VOID *EntryPoint;\r
- EFI_HANDLE ImageHandle;\r
- VM_CONTEXT VmContext;\r
-} EFI_EBC_THUNK_DATA;\r
-\r
-#define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE EFI_SIGNATURE_32 ('e', 'b', 'c', 'p')\r
-\r
-struct _EBC_PROTOCOL_PRIVATE_DATA {\r
- UINT32 Signature;\r
- EFI_EBC_PROTOCOL EbcProtocol;\r
- UINTN StackBase;\r
- UINTN StackTop;\r
- UINTN StackSize;\r
-} ;\r
-\r
-#define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \\r
- CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)\r
-\r
-\r
-#endif // #ifndef _EBC_INT_H_\r
+++ /dev/null
-#****************************************************************************\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
-#****************************************************************************\r
-.globl ASM_PFX(CopyMem)\r
-\r
-.globl ASM_PFX(EbcLLCALLEXNative)\r
-ASM_PFX(EbcLLCALLEXNative):\r
- push %ebp\r
- push %ebx\r
- mov %esp,%ebp\r
- mov 0xc(%esp),%ecx\r
- mov 0x14(%esp),%eax\r
- mov 0x10(%esp),%edx\r
- sub %edx,%eax\r
- sub %eax,%esp\r
- mov %esp,%ebx\r
- push %ecx\r
- push %eax\r
- push %edx\r
- push %ebx\r
- call ASM_PFX(CopyMem)\r
- pop %eax\r
- pop %eax\r
- pop %eax\r
- pop %ecx\r
- call *%ecx\r
- mov %ebp,%esp\r
- mov %ebp,%esp\r
- pop %ebx\r
- pop %ebp\r
- ret \r
-\r
-.globl ASM_PFX(EbcLLGetEbcEntryPoint)\r
-ASM_PFX(EbcLLGetEbcEntryPoint):\r
- ret \r
-\r
-.globl ASM_PFX(EbcLLGetStackPointer)\r
-ASM_PFX(EbcLLGetStackPointer):\r
- mov %esp,%eax\r
- add $0x4,%eax\r
- ret \r
-\r
-.globl ASM_PFX(EbcLLGetReturnValue)\r
-ASM_PFX(EbcLLGetReturnValue):\r
- ret \r
+++ /dev/null
- page ,132\r
- title VM ASSEMBLY LANGUAGE ROUTINES\r
-;****************************************************************************\r
-;* \r
-;* Copyright (c) 2006 - 2007, 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
-;****************************************************************************\r
-;****************************************************************************\r
-; REV 1.0\r
-;****************************************************************************\r
-;\r
-; Rev Date Description\r
-; --- -------- ------------------------------------------------------------\r
-; 1.0 03/14/01 Initial creation of file.\r
-;\r
-;****************************************************************************\r
- \r
-;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
-; This code provides low level routines that support the Virtual Machine\r
-; for option ROMs. \r
-;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
-\r
-;---------------------------------------------------------------------------\r
-; Equate files needed.\r
-;---------------------------------------------------------------------------\r
-\r
-.XLIST\r
-\r
-.LIST\r
-\r
-;---------------------------------------------------------------------------\r
-; Assembler options\r
-;---------------------------------------------------------------------------\r
-\r
-.686p\r
-.model flat \r
-.code \r
-;---------------------------------------------------------------------------\r
-;;GenericPostSegment SEGMENT USE16\r
-;---------------------------------------------------------------------------\r
-CopyMem PROTO C Destination:PTR DWORD, Source:PTR DWORD, Count:DWORD\r
-\r
-;****************************************************************************\r
-; EbcLLCALLEXNative\r
-;\r
-; This function is called to execute an EBC CALLEX instruction\r
-; to native code. \r
-; This instruction requires that we thunk out to external native\r
-; code. For IA32, we simply switch stacks and jump to the \r
-; specified function. On return, we restore the stack pointer\r
-; to its original location.\r
-;\r
-; Destroys no working registers.\r
-;****************************************************************************\r
-; VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
-_EbcLLCALLEXNative PROC NEAR PUBLIC\r
- push ebp\r
- push ebx\r
- mov ebp, esp ; standard function prolog\r
- \r
- ; Get function address in a register\r
- ; mov ecx, FuncAddr => mov ecx, dword ptr [FuncAddr]\r
- mov ecx, dword ptr [esp]+0Ch\r
- \r
- ; Set stack pointer to new value\r
- ; mov eax, NewStackPointer => mov eax, dword ptr [NewSp]\r
- mov eax, dword ptr [esp] + 14h\r
- mov edx, dword ptr [esp] + 10h\r
- sub eax, edx\r
- sub esp, eax \r
- mov ebx, esp\r
- push ecx\r
- push eax\r
- push edx\r
- push ebx\r
- call CopyMem\r
- pop eax\r
- pop eax\r
- pop eax\r
- pop ecx\r
-\r
- ; Now call the external routine\r
- call ecx\r
- \r
- ; ebp is preserved by the callee. In this function it\r
- ; equals the original esp, so set them equal\r
- mov esp, ebp\r
-\r
- ; Standard function epilog\r
- mov esp, ebp\r
- pop ebx\r
- pop ebp\r
- ret\r
-_EbcLLCALLEXNative ENDP\r
-\r
-\r
-; UINTN EbcLLGetEbcEntryPoint(VOID);\r
-; Routine Description:\r
-; The VM thunk code stuffs an EBC entry point into a processor\r
-; register. Since we can't use inline assembly to get it from\r
-; the interpreter C code, stuff it into the return value \r
-; register and return.\r
-;\r
-; Arguments:\r
-; None.\r
-;\r
-; Returns:\r
-; The contents of the register in which the entry point is passed.\r
-;\r
-_EbcLLGetEbcEntryPoint PROC NEAR PUBLIC\r
- ret\r
-_EbcLLGetEbcEntryPoint ENDP\r
-\r
-;/*++\r
-;\r
-;Routine Description:\r
-; \r
-; Return the caller's value of the stack pointer.\r
-;\r
-;Arguments:\r
-;\r
-; None.\r
-;\r
-;Returns:\r
-;\r
-; The current value of the stack pointer for the caller. We\r
-; adjust it by 4 here because when they called us, the return address\r
-; is put on the stack, thereby lowering it by 4 bytes.\r
-;\r
-;--*/\r
-\r
-; UINTN EbcLLGetStackPointer() \r
-_EbcLLGetStackPointer PROC NEAR PUBLIC\r
- mov eax, esp ; get current stack pointer\r
- add eax, 4 ; stack adjusted by this much when we were called\r
- ret\r
-_EbcLLGetStackPointer ENDP\r
-\r
-; UINT64 EbcLLGetReturnValue(VOID);\r
-; Routine Description:\r
-; When EBC calls native, on return the VM has to stuff the return\r
-; value into a VM register. It's assumed here that the value is still\r
-; in the register, so simply return and the caller should get the\r
-; return result properly.\r
-;\r
-; Arguments:\r
-; None.\r
-;\r
-; Returns:\r
-; The unmodified value returned by the native code.\r
-;\r
-_EbcLLGetReturnValue PROC NEAR PUBLIC\r
- ret\r
-_EbcLLGetReturnValue ENDP\r
-\r
-END\r
+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation \r
-All rights reserved. This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution. The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php \r
- \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
-\r
- EbcSupport.c\r
-\r
-Abstract:\r
-\r
- This module contains EBC support routines that are customized based on\r
- the target processor.\r
-\r
---*/\r
-\r
-#include "EbcInt.h"\r
-#include "EbcExecute.h"\r
-\r
-//\r
-// NOTE: This is the stack size allocated for the interpreter\r
-// when it executes an EBC image. The requirements can change\r
-// based on whether or not a debugger is present, and other\r
-// platform-specific configurations.\r
-//\r
-#define VM_STACK_SIZE (1024 * 4)\r
-#define EBC_THUNK_SIZE 32\r
-\r
-#define STACK_REMAIN_SIZE (1024 * 4)\r
-VOID\r
-EbcLLCALLEX (\r
- IN VM_CONTEXT *VmPtr,\r
- IN UINTN FuncAddr,\r
- IN UINTN NewStackPointer,\r
- IN VOID *FramePtr,\r
- IN UINT8 Size\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This function is called to execute an EBC CALLEX instruction. \r
- The function check the callee's content to see whether it is common native\r
- code or a thunk to another piece of EBC code.\r
- If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
- otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
- be startup which cost time and stack space.\r
- \r
-Arguments:\r
-\r
- VmPtr - Pointer to a VM context.\r
- FuncAddr - Callee's address\r
- NewStackPointer - New stack pointer after the call\r
- FramePtr - New frame pointer after the call\r
- Size - The size of call instruction\r
-\r
-Returns:\r
-\r
- None.\r
- \r
---*/\r
-{\r
- UINTN IsThunk;\r
- UINTN TargetEbcAddr;\r
-\r
- IsThunk = 1;\r
- TargetEbcAddr = 0;\r
-\r
- //\r
- // Processor specific code to check whether the callee is a thunk to EBC.\r
- //\r
- if (*((UINT8 *)FuncAddr) != 0xB8) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 1) != 0xBC) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 2) != 0x2E) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 3) != 0x11) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 4) != 0xCA) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 5) != 0xB8) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 10) != 0xB9) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 15) != 0xFF) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 16) != 0xE1) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
-\r
- TargetEbcAddr = ((UINTN)(*((UINT8 *)FuncAddr + 9)) << 24) + ((UINTN)(*((UINT8 *)FuncAddr + 8)) << 16) +\r
- ((UINTN)(*((UINT8 *)FuncAddr + 7)) << 8) + ((UINTN)(*((UINT8 *)FuncAddr + 6)));\r
-\r
-Action:\r
- if (IsThunk == 1){\r
- //\r
- // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
- // put our return address and frame pointer on the VM stack.\r
- // Then set the VM's IP to new EBC code.\r
- //\r
- VmPtr->R[0] -= 8;\r
- VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
- VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
- VmPtr->R[0] -= 8;\r
- VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (UINTN) (VmPtr->Ip + Size));\r
-\r
- VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
- } else {\r
- //\r
- // The callee is not a thunk to EBC, call native code.\r
- //\r
- EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
- \r
- //\r
- // Get return value and advance the IP.\r
- //\r
- VmPtr->R[7] = EbcLLGetReturnValue ();\r
- VmPtr->Ip += Size;\r
- }\r
-}\r
-\r
-STATIC\r
-UINT64\r
-EbcInterpret (\r
- IN OUT UINTN Arg1,\r
- IN OUT UINTN Arg2,\r
- IN OUT UINTN Arg3,\r
- IN OUT UINTN Arg4,\r
- IN OUT UINTN Arg5,\r
- IN OUT UINTN Arg6,\r
- IN OUT UINTN Arg7,\r
- IN OUT UINTN Arg8,\r
- IN OUT UINTN Arg9,\r
- IN OUT UINTN Arg10,\r
- IN OUT UINTN Arg11,\r
- IN OUT UINTN Arg12,\r
- IN OUT UINTN Arg13,\r
- IN OUT UINTN Arg14,\r
- IN OUT UINTN Arg15,\r
- IN OUT UINTN Arg16\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Begin executing an EBC image. The address of the entry point is passed\r
- in via a processor register, so we'll need to make a call to get the\r
- value.\r
- \r
-Arguments:\r
-\r
- None. Since we're called from a fixed up thunk (which we want to keep\r
- small), our only so-called argument is the EBC entry point passed in\r
- to us in a processor register.\r
-\r
-Returns:\r
-\r
- The value returned by the EBC application we're going to run.\r
- \r
---*/\r
-{\r
- //\r
- // Create a new VM context on the stack\r
- //\r
- VM_CONTEXT VmContext;\r
- UINTN Addr;\r
- EFI_STATUS Status;\r
- UINTN StackIndex;\r
-\r
- //\r
- // Get the EBC entry point from the processor register.\r
- //\r
- Addr = EbcLLGetEbcEntryPoint ();\r
-\r
- //\r
- // Now clear out our context\r
- //\r
- ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
-\r
- //\r
- // Set the VM instruction pointer to the correct location in memory.\r
- //\r
- VmContext.Ip = (VMIP) Addr;\r
- //\r
- // Initialize the stack pointer for the EBC. Get the current system stack\r
- // pointer and adjust it down by the max needed for the interpreter.\r
- //\r
-\r
- //\r
- // Align the stack on a natural boundary\r
- //\r
-\r
- //\r
- // Allocate stack pool\r
- //\r
- Status = GetEBCStack((EFI_HANDLE)-1, &VmContext.StackPool, &StackIndex);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
- VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
- VmContext.R[0] = (UINT64)(UINTN) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
- VmContext.HighStackBottom = (UINTN)VmContext.R[0];\r
- VmContext.R[0] &= ~(sizeof (UINTN) - 1);\r
- VmContext.R[0] -= sizeof (UINTN);\r
-\r
- //\r
- // Put a magic value in the stack gap, then adjust down again\r
- //\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
- VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
- VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
-\r
- //\r
- // For IA32, this is where we say our return address is\r
- //\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg16;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg15;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg14;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg13;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg12;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg11;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg10;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg9;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg8;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg7;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg6;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg5;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg4;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg3;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg2;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg1;\r
- VmContext.R[0] -= 16;\r
- VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
-\r
- //\r
- // We need to keep track of where the EBC stack starts. This way, if the EBC\r
- // accesses any stack variables above its initial stack setting, then we know\r
- // it's accessing variables passed into it, which means the data is on the\r
- // VM's stack.\r
- // When we're called, on the stack (high to low) we have the parameters, the\r
- // return address, then the saved ebp. Save the pointer to the return address.\r
- // EBC code knows that's there, so should look above it for function parameters.\r
- // The offset is the size of locals (VMContext + Addr + saved ebp).\r
- // Note that the interpreter assumes there is a 16 bytes of return address on\r
- // the stack too, so adjust accordingly.\r
- // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
- //\r
-\r
- //\r
- // Begin executing the EBC code\r
- //\r
- EbcExecute (&VmContext);\r
-\r
- //\r
- // Return the value in R[7] unless there was an error\r
- //\r
- ReturnEBCStack(StackIndex);\r
- return (UINT64) VmContext.R[7];\r
-}\r
-\r
-STATIC\r
-UINT64\r
-ExecuteEbcImageEntryPoint (\r
- IN EFI_HANDLE ImageHandle,\r
- IN EFI_SYSTEM_TABLE *SystemTable\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Begin executing an EBC image. The address of the entry point is passed\r
- in via a processor register, so we'll need to make a call to get the\r
- value.\r
- \r
-Arguments:\r
-\r
- ImageHandle - image handle for the EBC application we're executing\r
- SystemTable - standard system table passed into an driver's entry point\r
-\r
-Returns:\r
-\r
- The value returned by the EBC application we're going to run.\r
-\r
---*/\r
-{\r
- //\r
- // Create a new VM context on the stack\r
- //\r
- VM_CONTEXT VmContext;\r
- UINTN Addr;\r
- EFI_STATUS Status;\r
- UINTN StackIndex;\r
-\r
- //\r
- // Get the EBC entry point from the processor register. Make sure you don't\r
- // call any functions before this or you could mess up the register the\r
- // entry point is passed in.\r
- //\r
- Addr = EbcLLGetEbcEntryPoint ();\r
-\r
- //\r
- // Print(L"*** Thunked into EBC entry point - ImageHandle = 0x%X\n", (UINTN)ImageHandle);\r
- // Print(L"EBC entry point is 0x%X\n", (UINT32)(UINTN)Addr);\r
- //\r
- // Now clear out our context\r
- //\r
- ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
-\r
- //\r
- // Save the image handle so we can track the thunks created for this image\r
- //\r
- VmContext.ImageHandle = ImageHandle;\r
- VmContext.SystemTable = SystemTable;\r
-\r
- //\r
- // Set the VM instruction pointer to the correct location in memory.\r
- //\r
- VmContext.Ip = (VMIP) Addr;\r
-\r
- //\r
- // Initialize the stack pointer for the EBC. Get the current system stack\r
- // pointer and adjust it down by the max needed for the interpreter.\r
- //\r
-\r
- //\r
- // Allocate stack pool\r
- //\r
- Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
- VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
- VmContext.R[0] = (UINT64)(UINTN) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
- VmContext.HighStackBottom = (UINTN)VmContext.R[0];\r
- VmContext.R[0] -= sizeof (UINTN);\r
- \r
- //\r
- // Put a magic value in the stack gap, then adjust down again\r
- //\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
- VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
-\r
- //\r
- // Align the stack on a natural boundary\r
- // VmContext.R[0] &= ~(sizeof(UINTN) - 1);\r
- //\r
- VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) SystemTable;\r
- VmContext.R[0] -= sizeof (UINTN);\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) ImageHandle;\r
-\r
- VmContext.R[0] -= 16; \r
- VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
- //\r
- // VM pushes 16-bytes for return address. Simulate that here.\r
- //\r
-\r
- //\r
- // Begin executing the EBC code\r
- //\r
- EbcExecute (&VmContext);\r
-\r
- //\r
- // Return the value in R[7] unless there was an error\r
- //\r
- return (UINT64) VmContext.R[7];\r
-}\r
-\r
-EFI_STATUS\r
-EbcCreateThunks (\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *EbcEntryPoint,\r
- OUT VOID **Thunk,\r
- IN UINT32 Flags\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Create an IA32 thunk for the given EBC entry point.\r
- \r
-Arguments:\r
-\r
- ImageHandle - Handle of image for which this thunk is being created\r
- EbcEntryPoint - Address of the EBC code that the thunk is to call\r
- Thunk - Returned thunk we create here\r
-\r
-Returns:\r
-\r
- Standard EFI status.\r
- \r
---*/\r
-{\r
- UINT8 *Ptr;\r
- UINT8 *ThunkBase;\r
- UINT32 I;\r
- UINT32 Addr;\r
- INT32 Size;\r
- INT32 ThunkSize;\r
-\r
- //\r
- // Check alignment of pointer to EBC code\r
- //\r
- if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- Size = EBC_THUNK_SIZE;\r
- ThunkSize = Size;\r
-\r
- Ptr = AllocatePool (Size);\r
-\r
- if (Ptr == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- //\r
- // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
- //\r
- // Save the start address so we can add a pointer to it to a list later.\r
- //\r
- ThunkBase = Ptr;\r
-\r
- //\r
- // Give them the address of our buffer we're going to fix up\r
- //\r
- *Thunk = (VOID *) Ptr;\r
-\r
- //\r
- // Add a magic code here to help the VM recognize the thunk..\r
- // mov eax, 0xca112ebc => B8 BC 2E 11 CA\r
- //\r
- *Ptr = 0xB8;\r
- Ptr++;\r
- Size--;\r
- Addr = (UINT32) 0xCA112EBC;\r
- for (I = 0; I < sizeof (Addr); I++) {\r
- *Ptr = (UINT8) (UINTN) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
-\r
- //\r
- // Add code bytes to load up a processor register with the EBC entry point.\r
- // mov eax, 0xaa55aa55 => B8 55 AA 55 AA\r
- // The first 8 bytes of the thunk entry is the address of the EBC\r
- // entry point.\r
- //\r
- *Ptr = 0xB8;\r
- Ptr++;\r
- Size--;\r
- Addr = (UINT32) EbcEntryPoint;\r
- for (I = 0; I < sizeof (Addr); I++) {\r
- *Ptr = (UINT8) (UINTN) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
- //\r
- // Stick in a load of ecx with the address of appropriate VM function.\r
- // mov ecx 12345678h => 0xB9 0x78 0x56 0x34 0x12\r
- //\r
- if (Flags & FLAG_THUNK_ENTRY_POINT) {\r
- Addr = (UINT32) (UINTN) ExecuteEbcImageEntryPoint;\r
- } else {\r
- Addr = (UINT32) (UINTN) EbcInterpret;\r
- }\r
-\r
- //\r
- // MOV ecx\r
- //\r
- *Ptr = 0xB9;\r
- Ptr++;\r
- Size--;\r
- for (I = 0; I < sizeof (Addr); I++) {\r
- *Ptr = (UINT8) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
- //\r
- // Stick in jump opcode bytes for jmp ecx => 0xFF 0xE1\r
- //\r
- *Ptr = 0xFF;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xE1;\r
- Size--;\r
-\r
- //\r
- // Double check that our defined size is ok (application error)\r
- //\r
- if (Size < 0) {\r
- ASSERT (FALSE);\r
- return EFI_BUFFER_TOO_SMALL;\r
- }\r
- //\r
- // Add the thunk to the list for this image. Do this last since the add\r
- // function flushes the cache for us.\r
- //\r
- EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
-\r
- return EFI_SUCCESS;\r
-}\r
+++ /dev/null
-//++\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
-// EbcLowLevel.s\r
-//\r
-// Abstract:\r
-//\r
-// Contains low level routines for the Virtual Machine implementation\r
-// on an Itanium-based platform.\r
-//\r
-//\r
-//--\r
-\r
-.file "EbcLowLevel.s"\r
-\r
-#define PROCEDURE_ENTRY(name) .##text; \\r
- .##type name, @function; \\r
- .##proc name; \\r
-name::\r
-\r
-#define PROCEDURE_EXIT(name) .##endp name\r
-\r
-// Note: use of NESTED_SETUP requires number of locals (l) >= 3\r
-\r
-#define NESTED_SETUP(i,l,o,r) \\r
- alloc loc1=ar##.##pfs,i,l,o,r ;\\r
- mov loc0=b0\r
-\r
-#define NESTED_RETURN \\r
- mov b0=loc0 ;\\r
- mov ar##.##pfs=loc1 ;;\\r
- br##.##ret##.##dpnt b0;;\r
-\r
-.type CopyMem, @function; \r
-\r
-//-----------------------------------------------------------------------------\r
-//++\r
-// EbcAsmLLCALLEX\r
-//\r
-// Implements the low level EBC CALLEX instruction. Sets up the\r
-// stack pointer, does the spill of function arguments, and\r
-// calls the native function. On return it restores the original\r
-// stack pointer and returns to the caller.\r
-//\r
-// Arguments : \r
-//\r
-// On Entry :\r
-// in0 = Address of native code to call\r
-// in1 = New stack pointer\r
-//\r
-// Return Value: \r
-// \r
-// As per static calling conventions. \r
-// \r
-//--\r
-//---------------------------------------------------------------------------\r
-;// void EbcAsmLLCALLEX (UINTN FunctionAddr, UINTN EbcStackPointer)\r
-PROCEDURE_ENTRY(EbcAsmLLCALLEX)\r
- NESTED_SETUP (2,6,8,0)\r
-\r
- // NESTED_SETUP uses loc0 and loc1 for context save\r
-\r
- //\r
- // Save a copy of the EBC VM stack pointer\r
- //\r
- mov r8 = in1;;\r
-\r
- //\r
- // Copy stack arguments from EBC stack into registers. \r
- // Assume worst case and copy 8.\r
- //\r
- ld8 out0 = [r8], 8;;\r
- ld8 out1 = [r8], 8;;\r
- ld8 out2 = [r8], 8;;\r
- ld8 out3 = [r8], 8;;\r
- ld8 out4 = [r8], 8;;\r
- ld8 out5 = [r8], 8;;\r
- ld8 out6 = [r8], 8;;\r
- ld8 out7 = [r8], 8;;\r
-\r
- //\r
- // Save the original stack pointer\r
- //\r
- mov loc2 = r12;\r
-\r
- //\r
- // Save the gp\r
- //\r
- or loc3 = r1, r0\r
-\r
- //\r
- // Set the new aligned stack pointer. Reserve space for the required \r
- // 16-bytes of scratch area as well.\r
- //\r
- add r12 = 48, in1\r
-\r
- //\r
- // Now call the function. Load up the function address from the descriptor\r
- // pointed to by in0. Then get the gp from the descriptor at the following\r
- // address in the descriptor.\r
- //\r
- ld8 r31 = [in0], 8;;\r
- ld8 r30 = [in0];;\r
- mov b1 = r31\r
- mov r1 = r30\r
- (p0) br.call.dptk.many b0 = b1;;\r
-\r
- //\r
- // Restore the original stack pointer and gp\r
- //\r
- mov r12 = loc2\r
- or r1 = loc3, r0\r
-\r
- //\r
- // Now return\r
- //\r
- NESTED_RETURN\r
-\r
-PROCEDURE_EXIT(EbcAsmLLCALLEX)\r
-\r
-PROCEDURE_ENTRY(EbcLLCALLEXNative)\r
- NESTED_SETUP (3,6,3,0)\r
- \r
- mov loc2 = in2;;\r
- mov loc3 = in1;;\r
- sub loc2 = loc2, loc3\r
- mov loc4 = r12;;\r
- or loc5 = r1, r0\r
- \r
- sub r12 = r12, loc2\r
- mov out2 = loc2;;\r
-\r
- and r12 = -0x10, r12\r
- mov out1 = in1;;\r
- mov out0 = r12;;\r
- adds r12 = -0x8, r12\r
- (p0) br.call.dptk.many b0 = CopyMem;;\r
- adds r12 = 0x8, r12\r
- \r
- mov out0 = in0;;\r
- mov out1 = r12;;\r
- (p0) br.call.dptk.many b0 = EbcAsmLLCALLEX;;\r
- mov r12 = loc4;;\r
- or r1 = loc5, r0\r
- \r
- NESTED_RETURN\r
-PROCEDURE_EXIT(EbcLLCALLEXNative)\r
-\r
-\r
-//\r
-// UINTN EbcLLGetEbcEntryPoint(VOID)\r
-//\r
-// Description:\r
-// Simply return, so that the caller retrieves the return register\r
-// contents (R8). That's where the thunk-to-ebc code stuffed the\r
-// EBC entry point.\r
-//\r
-PROCEDURE_ENTRY(EbcLLGetEbcEntryPoint)\r
- br.ret.sptk b0 ;;\r
-PROCEDURE_EXIT(EbcLLGetEbcEntryPoint)\r
-\r
-//\r
-// INT64 EbcLLGetReturnValue(VOID)\r
-//\r
-// Description:\r
-// This function is called to get the value returned by native code\r
-// to EBC. It simply returns because the return value should still\r
-// be in the register, so the caller just gets the unmodified value.\r
-//\r
-PROCEDURE_ENTRY(EbcLLGetReturnValue)\r
- br.ret.sptk b0 ;;\r
-PROCEDURE_EXIT(EbcLLGetReturnValue)\r
-\r
-//\r
-// UINTN EbcLLGetStackPointer(VOID)\r
-//\r
-PROCEDURE_ENTRY(EbcLLGetStackPointer)\r
- mov r8 = r12 ;;\r
- br.ret.sptk b0 ;;\r
- br.sptk.few b6 \r
-PROCEDURE_EXIT(EbcLLGetStackPointer)\r
-\r
-\r
-\r
-\r
-\r
-\r
-\r
+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation \r
-All rights reserved. This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution. The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php \r
- \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
-\r
- EbcSupport.c\r
-\r
-Abstract:\r
-\r
- This module contains EBC support routines that are customized based on\r
- the target processor.\r
-\r
---*/\r
-\r
-#include "EbcInt.h"\r
-#include "EbcExecute.h"\r
-#include "EbcSupport.h"\r
-\r
-STATIC\r
-EFI_STATUS\r
-WriteBundle (\r
- IN VOID *MemPtr,\r
- IN UINT8 Template,\r
- IN UINT64 Slot0,\r
- IN UINT64 Slot1,\r
- IN UINT64 Slot2\r
- );\r
-\r
-STATIC\r
-VOID\r
-PushU64 (\r
- VM_CONTEXT *VmPtr,\r
- UINT64 Arg\r
- )\r
-{\r
- //\r
- // Advance the VM stack down, and then copy the argument to the stack.\r
- // Hope it's aligned.\r
- //\r
- VmPtr->R[0] -= sizeof (UINT64);\r
- *(UINT64 *) VmPtr->R[0] = Arg;\r
-}\r
-\r
-STATIC\r
-UINT64\r
-EbcInterpret (\r
- UINT64 Arg1,\r
- ...\r
- )\r
-{\r
- //\r
- // Create a new VM context on the stack\r
- //\r
- VM_CONTEXT VmContext;\r
- UINTN Addr;\r
- EFI_STATUS Status;\r
- UINTN StackIndex;\r
- VA_LIST List;\r
- UINT64 Arg2;\r
- UINT64 Arg3;\r
- UINT64 Arg4;\r
- UINT64 Arg5;\r
- UINT64 Arg6;\r
- UINT64 Arg7;\r
- UINT64 Arg8;\r
- UINT64 Arg9;\r
- UINT64 Arg10;\r
- UINT64 Arg11;\r
- UINT64 Arg12;\r
- UINT64 Arg13;\r
- UINT64 Arg14;\r
- UINT64 Arg15;\r
- UINT64 Arg16;\r
- //\r
- // Get the EBC entry point from the processor register. Make sure you don't\r
- // call any functions before this or you could mess up the register the\r
- // entry point is passed in.\r
- //\r
- Addr = EbcLLGetEbcEntryPoint ();\r
- //\r
- // Need the args off the stack.\r
- //\r
- VA_START (List, Arg1);\r
- Arg2 = VA_ARG (List, UINT64);\r
- Arg3 = VA_ARG (List, UINT64);\r
- Arg4 = VA_ARG (List, UINT64);\r
- Arg5 = VA_ARG (List, UINT64);\r
- Arg6 = VA_ARG (List, UINT64);\r
- Arg7 = VA_ARG (List, UINT64);\r
- Arg8 = VA_ARG (List, UINT64);\r
- Arg9 = VA_ARG (List, UINT64);\r
- Arg10 = VA_ARG (List, UINT64);\r
- Arg11 = VA_ARG (List, UINT64);\r
- Arg12 = VA_ARG (List, UINT64);\r
- Arg13 = VA_ARG (List, UINT64);\r
- Arg14 = VA_ARG (List, UINT64);\r
- Arg15 = VA_ARG (List, UINT64);\r
- Arg16 = VA_ARG (List, UINT64);\r
- //\r
- // Now clear out our context\r
- //\r
- ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
- //\r
- // Set the VM instruction pointer to the correct location in memory.\r
- //\r
- VmContext.Ip = (VMIP) Addr;\r
- //\r
- // Initialize the stack pointer for the EBC. Get the current system stack\r
- // pointer and adjust it down by the max needed for the interpreter.\r
- //\r
- //\r
- // NOTE: Eventually we should have the interpreter allocate memory\r
- // for stack space which it will use during its execution. This\r
- // would likely improve performance because the interpreter would\r
- // no longer be required to test each memory access and adjust\r
- // those reading from the stack gap.\r
- //\r
- // For IPF, the stack looks like (assuming 10 args passed)\r
- // arg10\r
- // arg9 (Bottom of high stack)\r
- // [ stack gap for interpreter execution ]\r
- // [ magic value for detection of stack corruption ]\r
- // arg8 (Top of low stack)\r
- // arg7....\r
- // arg1\r
- // [ 64-bit return address ]\r
- // [ ebc stack ]\r
- // If the EBC accesses memory in the stack gap, then we assume that it's\r
- // actually trying to access args9 and greater. Therefore we need to\r
- // adjust memory accesses in this region to point above the stack gap.\r
- //\r
- //\r
- // Now adjust the EBC stack pointer down to leave a gap for interpreter\r
- // execution. Then stuff a magic value there.\r
- //\r
- \r
- Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
- VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
- VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
- VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
- VmContext.R[0] -= sizeof (UINTN);\r
-\r
- \r
- PushU64 (&VmContext, (UINT64) VM_STACK_KEY_VALUE);\r
- VmContext.StackMagicPtr = (UINTN *) VmContext.R[0];\r
- VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
- //\r
- // Push the EBC arguments on the stack. Does not matter that they may not\r
- // all be valid.\r
- //\r
- PushU64 (&VmContext, Arg16);\r
- PushU64 (&VmContext, Arg15);\r
- PushU64 (&VmContext, Arg14);\r
- PushU64 (&VmContext, Arg13);\r
- PushU64 (&VmContext, Arg12);\r
- PushU64 (&VmContext, Arg11);\r
- PushU64 (&VmContext, Arg10);\r
- PushU64 (&VmContext, Arg9);\r
- PushU64 (&VmContext, Arg8);\r
- PushU64 (&VmContext, Arg7);\r
- PushU64 (&VmContext, Arg6);\r
- PushU64 (&VmContext, Arg5);\r
- PushU64 (&VmContext, Arg4);\r
- PushU64 (&VmContext, Arg3);\r
- PushU64 (&VmContext, Arg2);\r
- PushU64 (&VmContext, Arg1);\r
- //\r
- // Push a bogus return address on the EBC stack because the\r
- // interpreter expects one there. For stack alignment purposes on IPF,\r
- // EBC return addresses are always 16 bytes. Push a bogus value as well.\r
- //\r
- PushU64 (&VmContext, 0);\r
- PushU64 (&VmContext, 0xDEADBEEFDEADBEEF);\r
- VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
- //\r
- // Begin executing the EBC code\r
- //\r
- EbcExecute (&VmContext);\r
- //\r
- // Return the value in R[7] unless there was an error\r
- //\r
- ReturnEBCStack(StackIndex);\r
- return (UINT64) VmContext.R[7];\r
-}\r
-\r
-STATIC\r
-UINT64\r
-ExecuteEbcImageEntryPoint (\r
- IN EFI_HANDLE ImageHandle,\r
- IN EFI_SYSTEM_TABLE *SystemTable\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- IPF implementation.\r
-\r
- Begin executing an EBC image. The address of the entry point is passed\r
- in via a processor register, so we'll need to make a call to get the\r
- value.\r
- \r
-Arguments:\r
-\r
- ImageHandle - image handle for the EBC application we're executing\r
- SystemTable - standard system table passed into an driver's entry point\r
-\r
-Returns:\r
-\r
- The value returned by the EBC application we're going to run.\r
-\r
---*/\r
-{\r
- //\r
- // Create a new VM context on the stack\r
- //\r
- VM_CONTEXT VmContext;\r
- UINTN Addr;\r
- EFI_STATUS Status;\r
- UINTN StackIndex;\r
-\r
- //\r
- // Get the EBC entry point from the processor register. Make sure you don't\r
- // call any functions before this or you could mess up the register the\r
- // entry point is passed in.\r
- //\r
- Addr = EbcLLGetEbcEntryPoint ();\r
-\r
- //\r
- // Now clear out our context\r
- //\r
- ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
-\r
- //\r
- // Save the image handle so we can track the thunks created for this image\r
- //\r
- VmContext.ImageHandle = ImageHandle;\r
- VmContext.SystemTable = SystemTable;\r
-\r
- //\r
- // Set the VM instruction pointer to the correct location in memory.\r
- //\r
- VmContext.Ip = (VMIP) Addr;\r
-\r
- //\r
- // Get the stack pointer. This is the bottom of the upper stack.\r
- //\r
- Addr = EbcLLGetStackPointer ();\r
- \r
- Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
- VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
- VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
- VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
- VmContext.R[0] -= sizeof (UINTN);\r
-\r
- \r
- //\r
- // Allocate stack space for the interpreter. Then put a magic value\r
- // at the bottom so we can detect stack corruption.\r
- //\r
- PushU64 (&VmContext, (UINT64) VM_STACK_KEY_VALUE);\r
- VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
-\r
- //\r
- // When we thunk to external native code, we copy the last 8 qwords from\r
- // the EBC stack into the processor registers, and adjust the stack pointer\r
- // up. If the caller is not passing 8 parameters, then we've moved the\r
- // stack pointer up into the stack gap. If this happens, then the caller\r
- // can mess up the stack gap contents (in particular our magic value).\r
- // Therefore, leave another gap below the magic value. Pick 10 qwords down,\r
- // just as a starting point.\r
- //\r
- VmContext.R[0] -= 10 * sizeof (UINT64);\r
-\r
- //\r
- // Align the stack pointer such that after pushing the system table,\r
- // image handle, and return address on the stack, it's aligned on a 16-byte\r
- // boundary as required for IPF.\r
- //\r
- VmContext.R[0] &= (INT64)~0x0f;\r
- VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
- //\r
- // Simply copy the image handle and system table onto the EBC stack.\r
- // Greatly simplifies things by not having to spill the args\r
- //\r
- PushU64 (&VmContext, (UINT64) SystemTable);\r
- PushU64 (&VmContext, (UINT64) ImageHandle);\r
-\r
- //\r
- // Interpreter assumes 64-bit return address is pushed on the stack.\r
- // IPF does not do this so pad the stack accordingly. Also, a\r
- // "return address" is 16 bytes as required for IPF stack alignments.\r
- //\r
- PushU64 (&VmContext, (UINT64) 0);\r
- PushU64 (&VmContext, (UINT64) 0x1234567887654321);\r
- VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
-\r
- //\r
- // Begin executing the EBC code\r
- //\r
- EbcExecute (&VmContext);\r
-\r
- //\r
- // Return the value in R[7] unless there was an error\r
- //\r
- ReturnEBCStack(StackIndex);\r
- return (UINT64) VmContext.R[7];\r
-}\r
-\r
-EFI_STATUS\r
-EbcCreateThunks (\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *EbcEntryPoint,\r
- OUT VOID **Thunk,\r
- IN UINT32 Flags\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Create thunks for an EBC image entry point, or an EBC protocol service.\r
- \r
-Arguments:\r
-\r
- ImageHandle - Image handle for the EBC image. If not null, then we're\r
- creating a thunk for an image entry point.\r
- EbcEntryPoint - Address of the EBC code that the thunk is to call\r
- Thunk - Returned thunk we create here\r
- Flags - Flags indicating options for creating the thunk\r
- \r
-Returns:\r
-\r
- Standard EFI status.\r
- \r
---*/\r
-{\r
- UINT8 *Ptr;\r
- UINT8 *ThunkBase;\r
- UINT64 Addr;\r
- UINT64 Code[3]; // Code in a bundle\r
- UINT64 RegNum; // register number for MOVL\r
- UINT64 I; // bits of MOVL immediate data\r
- UINT64 Ic; // bits of MOVL immediate data\r
- UINT64 Imm5c; // bits of MOVL immediate data\r
- UINT64 Imm9d; // bits of MOVL immediate data\r
- UINT64 Imm7b; // bits of MOVL immediate data\r
- UINT64 Br; // branch register for loading and jumping\r
- UINT64 *Data64Ptr;\r
- UINT32 ThunkSize;\r
- UINT32 Size;\r
-\r
- //\r
- // Check alignment of pointer to EBC code, which must always be aligned\r
- // on a 2-byte boundary.\r
- //\r
- if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- //\r
- // Allocate memory for the thunk. Make the (most likely incorrect) assumption\r
- // that the returned buffer is not aligned, so round up to the next\r
- // alignment size.\r
- //\r
- Size = EBC_THUNK_SIZE + EBC_THUNK_ALIGNMENT - 1;\r
- ThunkSize = Size;\r
- Ptr = AllocatePool (Size);\r
-\r
- if (Ptr == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- //\r
- // Save the start address of the buffer.\r
- //\r
- ThunkBase = Ptr;\r
-\r
- //\r
- // Make sure it's aligned for code execution. If not, then\r
- // round up.\r
- //\r
- if ((UINT32) (UINTN) Ptr & (EBC_THUNK_ALIGNMENT - 1)) {\r
- Ptr = (UINT8 *) (((UINTN) Ptr + (EBC_THUNK_ALIGNMENT - 1)) &~ (UINT64) (EBC_THUNK_ALIGNMENT - 1));\r
- }\r
- //\r
- // Return the pointer to the thunk to the caller to user as the\r
- // image entry point.\r
- //\r
- *Thunk = (VOID *) Ptr;\r
-\r
- //\r
- // Clear out the thunk entry\r
- // ZeroMem(Ptr, Size);\r
- //\r
- // For IPF, when you do a call via a function pointer, the function pointer\r
- // actually points to a function descriptor which consists of a 64-bit\r
- // address of the function, followed by a 64-bit gp for the function being\r
- // called. See the the Software Conventions and Runtime Architecture Guide\r
- // for details.\r
- // So first off in our thunk, create a descriptor for our actual thunk code.\r
- // This means we need to create a pointer to the thunk code (which follows\r
- // the descriptor we're going to create), followed by the gp of the Vm\r
- // interpret function we're going to eventually execute.\r
- //\r
- Data64Ptr = (UINT64 *) Ptr;\r
-\r
- //\r
- // Write the function's entry point (which is our thunk code that follows\r
- // this descriptor we're creating).\r
- //\r
- *Data64Ptr = (UINT64) (Data64Ptr + 2);\r
- //\r
- // Get the gp from the descriptor for EbcInterpret and stuff it in our thunk\r
- // descriptor.\r
- //\r
- *(Data64Ptr + 1) = *(UINT64 *) ((UINT64 *) (UINTN) EbcInterpret + 1);\r
- //\r
- // Advance our thunk data pointer past the descriptor. Since the\r
- // descriptor consists of 16 bytes, the pointer is still aligned for\r
- // IPF code execution (on 16-byte boundary).\r
- //\r
- Ptr += sizeof (UINT64) * 2;\r
-\r
- //\r
- // *************************** MAGIC BUNDLE ********************************\r
- //\r
- // Write magic code bundle for: movl r8 = 0xca112ebcca112ebc to help the VM\r
- // to recognize it is a thunk.\r
- //\r
- Addr = (UINT64) 0xCA112EBCCA112EBC;\r
-\r
- //\r
- // Now generate the code bytes. First is nop.m 0x0\r
- //\r
- Code[0] = OPCODE_NOP;\r
-\r
- //\r
- // Next is simply Addr[62:22] (41 bits) of the address\r
- //\r
- Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
-\r
- //\r
- // Extract bits from the address for insertion into the instruction\r
- // i = Addr[63:63]\r
- //\r
- I = RShiftU64 (Addr, 63) & 0x01;\r
- //\r
- // ic = Addr[21:21]\r
- //\r
- Ic = RShiftU64 (Addr, 21) & 0x01;\r
- //\r
- // imm5c = Addr[20:16] for 5 bits\r
- //\r
- Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
- //\r
- // imm9d = Addr[15:7] for 9 bits\r
- //\r
- Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
- //\r
- // imm7b = Addr[6:0] for 7 bits\r
- //\r
- Imm7b = Addr & 0x7F;\r
-\r
- //\r
- // The EBC entry point will be put into r8, so r8 can be used here\r
- // temporary. R8 is general register and is auto-serialized.\r
- //\r
- RegNum = 8;\r
-\r
- //\r
- // Next is jumbled data, including opcode and rest of address\r
- //\r
- Code[2] = LShiftU64 (Imm7b, 13);\r
- Code[2] = Code[2] | LShiftU64 (0x00, 20); // vc\r
- Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
- Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
- Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
- Code[2] = Code[2] | LShiftU64 (I, 36);\r
- Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
- Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
-\r
- WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
-\r
- //\r
- // *************************** FIRST BUNDLE ********************************\r
- //\r
- // Write code bundle for: movl r8 = EBC_ENTRY_POINT so we pass\r
- // the ebc entry point in to the interpreter function via a processor\r
- // register.\r
- // Note -- we could easily change this to pass in a pointer to a structure\r
- // that contained, among other things, the EBC image's entry point. But\r
- // for now pass it directly.\r
- //\r
- Ptr += 16;\r
- Addr = (UINT64) EbcEntryPoint;\r
-\r
- //\r
- // Now generate the code bytes. First is nop.m 0x0\r
- //\r
- Code[0] = OPCODE_NOP;\r
-\r
- //\r
- // Next is simply Addr[62:22] (41 bits) of the address\r
- //\r
- Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
-\r
- //\r
- // Extract bits from the address for insertion into the instruction\r
- // i = Addr[63:63]\r
- //\r
- I = RShiftU64 (Addr, 63) & 0x01;\r
- //\r
- // ic = Addr[21:21]\r
- //\r
- Ic = RShiftU64 (Addr, 21) & 0x01;\r
- //\r
- // imm5c = Addr[20:16] for 5 bits\r
- //\r
- Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
- //\r
- // imm9d = Addr[15:7] for 9 bits\r
- //\r
- Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
- //\r
- // imm7b = Addr[6:0] for 7 bits\r
- //\r
- Imm7b = Addr & 0x7F;\r
-\r
- //\r
- // Put the EBC entry point in r8, which is the location of the return value\r
- // for functions.\r
- //\r
- RegNum = 8;\r
-\r
- //\r
- // Next is jumbled data, including opcode and rest of address\r
- //\r
- Code[2] = LShiftU64 (Imm7b, 13);\r
- Code[2] = Code[2] | LShiftU64 (0x00, 20); // vc\r
- Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
- Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
- Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
- Code[2] = Code[2] | LShiftU64 (I, 36);\r
- Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
- Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
-\r
- WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
-\r
- //\r
- // *************************** NEXT BUNDLE *********************************\r
- //\r
- // Write code bundle for:\r
- // movl rx = offset_of(EbcInterpret|ExecuteEbcImageEntryPoint)\r
- //\r
- // Advance pointer to next bundle, then compute the offset from this bundle\r
- // to the address of the entry point of the interpreter.\r
- //\r
- Ptr += 16;\r
- if (Flags & FLAG_THUNK_ENTRY_POINT) {\r
- Addr = (UINT64) ExecuteEbcImageEntryPoint;\r
- } else {\r
- Addr = (UINT64) EbcInterpret;\r
- }\r
- //\r
- // Indirection on Itanium-based systems\r
- //\r
- Addr = *(UINT64 *) Addr;\r
-\r
- //\r
- // Now write the code to load the offset into a register\r
- //\r
- Code[0] = OPCODE_NOP;\r
-\r
- //\r
- // Next is simply Addr[62:22] (41 bits) of the address\r
- //\r
- Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
-\r
- //\r
- // Extract bits from the address for insertion into the instruction\r
- // i = Addr[63:63]\r
- //\r
- I = RShiftU64 (Addr, 63) & 0x01;\r
- //\r
- // ic = Addr[21:21]\r
- //\r
- Ic = RShiftU64 (Addr, 21) & 0x01;\r
- //\r
- // imm5c = Addr[20:16] for 5 bits\r
- //\r
- Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
- //\r
- // imm9d = Addr[15:7] for 9 bits\r
- //\r
- Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
- //\r
- // imm7b = Addr[6:0] for 7 bits\r
- //\r
- Imm7b = Addr & 0x7F;\r
-\r
- //\r
- // Put it in r31, a scratch register\r
- //\r
- RegNum = 31;\r
-\r
- //\r
- // Next is jumbled data, including opcode and rest of address\r
- //\r
- Code[2] = LShiftU64(Imm7b, 13);\r
- Code[2] = Code[2] | LShiftU64 (0x00, 20); // vc\r
- Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
- Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
- Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
- Code[2] = Code[2] | LShiftU64 (I, 36);\r
- Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
- Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
-\r
- WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
-\r
- //\r
- // *************************** NEXT BUNDLE *********************************\r
- //\r
- // Load branch register with EbcInterpret() function offset from the bundle\r
- // address: mov b6 = RegNum\r
- //\r
- // See volume 3 page 4-29 of the Arch. Software Developer's Manual.\r
- //\r
- // Advance pointer to next bundle\r
- //\r
- Ptr += 16;\r
- Code[0] = OPCODE_NOP;\r
- Code[1] = OPCODE_NOP;\r
- Code[2] = OPCODE_MOV_BX_RX;\r
-\r
- //\r
- // Pick a branch register to use. Then fill in the bits for the branch\r
- // register and user register (same user register as previous bundle).\r
- //\r
- Br = 6;\r
- Code[2] |= LShiftU64 (Br, 6);\r
- Code[2] |= LShiftU64 (RegNum, 13);\r
- WriteBundle ((VOID *) Ptr, 0x0d, Code[0], Code[1], Code[2]);\r
-\r
- //\r
- // *************************** NEXT BUNDLE *********************************\r
- //\r
- // Now do the branch: (p0) br.cond.sptk.few b6\r
- //\r
- // Advance pointer to next bundle.\r
- // Fill in the bits for the branch register (same reg as previous bundle)\r
- //\r
- Ptr += 16;\r
- Code[0] = OPCODE_NOP;\r
- Code[1] = OPCODE_NOP;\r
- Code[2] = OPCODE_BR_COND_SPTK_FEW;\r
- Code[2] |= LShiftU64 (Br, 13);\r
- WriteBundle ((VOID *) Ptr, 0x1d, Code[0], Code[1], Code[2]);\r
-\r
- //\r
- // Add the thunk to our list of allocated thunks so we can do some cleanup\r
- // when the image is unloaded. Do this last since the Add function flushes\r
- // the instruction cache for us.\r
- //\r
- EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
-\r
- //\r
- // Done\r
- //\r
- return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-WriteBundle (\r
- IN VOID *MemPtr,\r
- IN UINT8 Template,\r
- IN UINT64 Slot0,\r
- IN UINT64 Slot1,\r
- IN UINT64 Slot2\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Given raw bytes of Itanium based code, format them into a bundle and\r
- write them out.\r
- \r
-Arguments:\r
-\r
- MemPtr - pointer to memory location to write the bundles to\r
- Template - 5-bit template\r
- Slot0-2 - instruction slot data for the bundle\r
-\r
-Returns:\r
-\r
- EFI_INVALID_PARAMETER - Pointer is not aligned\r
- - No more than 5 bits in template\r
- - More than 41 bits used in code\r
- EFI_SUCCESS - All data is written.\r
-\r
---*/\r
-{\r
- UINT8 *BPtr;\r
- UINT32 Index;\r
- UINT64 Low64;\r
- UINT64 High64;\r
-\r
- //\r
- // Verify pointer is aligned\r
- //\r
- if ((UINT64) MemPtr & 0xF) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- //\r
- // Verify no more than 5 bits in template\r
- //\r
- if (Template &~0x1F) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- //\r
- // Verify max of 41 bits used in code\r
- //\r
- if ((Slot0 | Slot1 | Slot2) &~0x1ffffffffff) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- Low64 = LShiftU64 (Slot1, 46);\r
- Low64 = Low64 | LShiftU64 (Slot0, 5) | Template;\r
-\r
- High64 = RShiftU64 (Slot1, 18);\r
- High64 = High64 | LShiftU64 (Slot2, 23);\r
-\r
- //\r
- // Now write it all out\r
- //\r
- BPtr = (UINT8 *) MemPtr;\r
- for (Index = 0; Index < 8; Index++) {\r
- *BPtr = (UINT8) Low64;\r
- Low64 = RShiftU64 (Low64, 8);\r
- BPtr++;\r
- }\r
-\r
- for (Index = 0; Index < 8; Index++) {\r
- *BPtr = (UINT8) High64;\r
- High64 = RShiftU64 (High64, 8);\r
- BPtr++;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-VOID\r
-EbcLLCALLEX (\r
- IN VM_CONTEXT *VmPtr,\r
- IN UINTN FuncAddr,\r
- IN UINTN NewStackPointer,\r
- IN VOID *FramePtr,\r
- IN UINT8 Size\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This function is called to execute an EBC CALLEX instruction. \r
- The function check the callee's content to see whether it is common native\r
- code or a thunk to another piece of EBC code.\r
- If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
- otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
- be startup which cost time and stack space.\r
- \r
-Arguments:\r
-\r
- VmPtr - Pointer to a VM context.\r
- FuncAddr - Callee's address\r
- NewStackPointer - New stack pointer after the call\r
- FramePtr - New frame pointer after the call\r
- Size - The size of call instruction\r
-\r
-Returns:\r
-\r
- None.\r
- \r
---*/\r
-{\r
- UINTN IsThunk;\r
- UINTN TargetEbcAddr;\r
- UINTN CodeOne18;\r
- UINTN CodeOne23;\r
- UINTN CodeTwoI;\r
- UINTN CodeTwoIc;\r
- UINTN CodeTwo7b;\r
- UINTN CodeTwo5c;\r
- UINTN CodeTwo9d;\r
- UINTN CalleeAddr;\r
-\r
- IsThunk = 1;\r
- TargetEbcAddr = 0;\r
-\r
- //\r
- // FuncAddr points to the descriptor of the target instructions.\r
- //\r
- CalleeAddr = *((UINT64 *)FuncAddr);\r
-\r
- //\r
- // Processor specific code to check whether the callee is a thunk to EBC.\r
- //\r
- if (*((UINT64 *)CalleeAddr) != 0xBCCA000100000005) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT64 *)CalleeAddr + 1) != 0x697623C1004A112E) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
-\r
- CodeOne18 = RShiftU64 (*((UINT64 *)CalleeAddr + 2), 46) & 0x3FFFF;\r
- CodeOne23 = (*((UINT64 *)CalleeAddr + 3)) & 0x7FFFFF;\r
- CodeTwoI = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 59) & 0x1;\r
- CodeTwoIc = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 44) & 0x1;\r
- CodeTwo7b = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 36) & 0x7F;\r
- CodeTwo5c = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 45) & 0x1F;\r
- CodeTwo9d = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 50) & 0x1FF;\r
-\r
- TargetEbcAddr = CodeTwo7b;\r
- TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwo9d, 7);\r
- TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwo5c, 16);\r
- TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwoIc, 21);\r
- TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeOne18, 22);\r
- TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeOne23, 40);\r
- TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwoI, 63);\r
-\r
-Action:\r
- if (IsThunk == 1){\r
- //\r
- // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
- // put our return address and frame pointer on the VM stack.\r
- // Then set the VM's IP to new EBC code.\r
- //\r
- VmPtr->R[0] -= 8;\r
- VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
- VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
- VmPtr->R[0] -= 8;\r
- VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (VmPtr->Ip + Size));\r
-\r
- VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
- } else {\r
- //\r
- // The callee is not a thunk to EBC, call native code.\r
- //\r
- EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
-\r
- //\r
- // Get return value and advance the IP.\r
- //\r
- VmPtr->R[7] = EbcLLGetReturnValue ();\r
- VmPtr->Ip += Size;\r
- }\r
-}\r
+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-Module Name:\r
-\r
- EbcSupport.h\r
-\r
-Abstract:\r
-\r
- Definition of EBC Support function\r
-\r
-Revision History\r
-\r
---*/\r
-\r
-#ifndef _IPF_EBC_SUPPORT_H_\r
-#define _IPF_EBC_SUPPORT_H_\r
-\r
-#define VM_STACK_SIZE (1024 * 32)\r
-\r
-#define EBC_THUNK_SIZE 128\r
-#define STACK_REMAIN_SIZE (1024 * 4)\r
-\r
-//\r
-// For code execution, thunks must be aligned on 16-byte boundary\r
-//\r
-#define EBC_THUNK_ALIGNMENT 16\r
-\r
-//\r
-// Opcodes for IPF instructions. We'll need to hand-create thunk code (stuffing\r
-// bits) to insert a jump to the interpreter.\r
-//\r
-#define OPCODE_NOP (UINT64) 0x00008000000\r
-#define OPCODE_BR_COND_SPTK_FEW (UINT64) 0x00100000000\r
-#define OPCODE_MOV_BX_RX (UINT64) 0x00E00100000\r
-\r
-//\r
-// Opcode for MOVL instruction\r
-//\r
-#define MOVL_OPCODE 0x06\r
-\r
-VOID\r
-EbcAsmLLCALLEX (\r
- IN UINTN CallAddr,\r
- IN UINTN EbcSp\r
- );\r
-\r
-#endif\r
+++ /dev/null
-#****************************************************************************\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
-#****************************************************************************\r
-#****************************************************************************\r
-# REV 1.0\r
-#****************************************************************************\r
-#\r
-# Rev Date Description\r
-# --- -------- ------------------------------------------------------------\r
-# 1.0 05/09/12 Initial creation of file.\r
-#\r
-#****************************************************************************\r
-\r
-#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
-# This code provides low level routines that support the Virtual Machine\r
-# for option ROMs. \r
-#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
-\r
-#---------------------------------------------------------------------------\r
-# Equate files needed.\r
-#---------------------------------------------------------------------------\r
-\r
-#---------------------------------------------------------------------------\r
-##GenericPostSegment SEGMENT USE16\r
-#---------------------------------------------------------------------------\r
-\r
-#****************************************************************************\r
-# EbcLLCALLEX\r
-#\r
-# This function is called to execute an EBC CALLEX instruction. \r
-# This instruction requires that we thunk out to external native\r
-# code. For x64, we switch stacks, copy the arguments to the stack\r
-# and jump to the specified function. \r
-# On return, we restore the stack pointer to its original location.\r
-#\r
-# Destroys no working registers.\r
-#****************************************************************************\r
-.global _CopyMem;\r
-\r
-# VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
-.global _EbcLLCALLEXNative;\r
-_EbcLLCALLEXNative:\r
- push %rbp\r
- push %rbx\r
- mov %rsp, %rbp\r
- # Function prolog\r
-\r
- # Copy FuncAddr to a preserved register.\r
- mov %rcx, %rbx\r
-\r
- # Set stack pointer to new value\r
- sub %r8, %rdx\r
- sub %rsp, %r8\r
- mov %rsp, %rcx\r
- sub %rsp, 0x20\r
- call _CopyMem \r
- add %rsp, 0x20\r
-\r
- # Considering the worst case, load 4 potiential arguments\r
- # into registers.\r
- mov (%rsp), %rcx\r
- mov 8(%rsp), %rdx\r
- mov 10(%rsp), %r8\r
- mov 18(%rsp), %r9\r
-\r
- # Now call the external routine\r
- call *%rbx\r
-\r
- # Function epilog\r
- mov %rbp, %rsp\r
- pop %rbx\r
- pop %rbp\r
- ret\r
-\r
-\r
-# UINTN EbcLLGetEbcEntryPoint(VOID);\r
-# Routine Description:\r
-# The VM thunk code stuffs an EBC entry point into a processor\r
-# register. Since we can't use inline assembly to get it from\r
-# the interpreter C code, stuff it into the return value \r
-# register and return.\r
-#\r
-# Arguments:\r
-# None.\r
-#\r
-# Returns:\r
-# The contents of the register in which the entry point is passed.\r
-#\r
-.global _EbcLLGetEbcEntryPoint;\r
-_EbcLLGetEbcEntryPoint:\r
- ret\r
-\r
-#/*++\r
-#\r
-#Routine Description:\r
-# \r
-# Return the caller's value of the stack pointer.\r
-#\r
-#Arguments:\r
-#\r
-# None.\r
-#\r
-#Returns:\r
-#\r
-# The current value of the stack pointer for the caller. We\r
-# adjust it by 4 here because when they called us, the return address\r
-# is put on the stack, thereby lowering it by 4 bytes.\r
-#\r
-#--*/\r
-\r
-# UINTN EbcLLGetStackPointer() \r
-.global _EbcLLGetStackPointer;\r
-_EbcLLGetStackPointer:\r
- mov %rsp, %rax\r
- # Stack adjusted by this much when we were called,\r
- # For this function, it's 4.\r
- add $4, %rax\r
- ret\r
-\r
-.global _EbcLLGetReturnValue;\r
-_EbcLLGetReturnValue:\r
-# UINT64 EbcLLGetReturnValue(VOID);\r
-# Routine Description:\r
-# When EBC calls native, on return the VM has to stuff the return\r
-# value into a VM register. It's assumed here that the value is still\r
-# in the register, so simply return and the caller should get the\r
-# return result properly.\r
-#\r
-# Arguments:\r
-# None.\r
-#\r
-# Returns:\r
-# The unmodified value returned by the native code.\r
-#\r
- ret\r
+++ /dev/null
- page ,132\r
- title VM ASSEMBLY LANGUAGE ROUTINES\r
-;****************************************************************************\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
-;****************************************************************************\r
-;****************************************************************************\r
-; REV 1.0\r
-;****************************************************************************\r
-;\r
-; Rev Date Description\r
-; --- -------- ------------------------------------------------------------\r
-; 1.0 05/09/12 Initial creation of file.\r
-;\r
-;****************************************************************************\r
- \r
-;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
-; This code provides low level routines that support the Virtual Machine\r
-; for option ROMs. \r
-;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
-\r
-;---------------------------------------------------------------------------\r
-; Equate files needed.\r
-;---------------------------------------------------------------------------\r
-\r
-text SEGMENT\r
-\r
-;---------------------------------------------------------------------------\r
-;;GenericPostSegment SEGMENT USE16\r
-;---------------------------------------------------------------------------\r
-\r
-;****************************************************************************\r
-; EbcLLCALLEX\r
-;\r
-; This function is called to execute an EBC CALLEX instruction. \r
-; This instruction requires that we thunk out to external native\r
-; code. For x64, we switch stacks, copy the arguments to the stack\r
-; and jump to the specified function. \r
-; On return, we restore the stack pointer to its original location.\r
-;\r
-; Destroys no working registers.\r
-;****************************************************************************\r
-; VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
-\r
-CopyMem PROTO Destination:PTR DWORD, Source:PTR DWORD, Count:DWORD\r
-\r
-\r
-EbcLLCALLEXNative PROC NEAR PUBLIC\r
- push rbp\r
- push rbx\r
- mov rbp, rsp\r
- ; Function prolog\r
- \r
- ; Copy FuncAddr to a preserved register.\r
- mov rbx, rcx\r
-\r
- ; Set stack pointer to new value\r
- sub r8, rdx\r
- sub rsp, r8\r
- mov rcx, rsp\r
- sub rsp, 20h\r
- call CopyMem \r
- add rsp, 20h\r
- \r
- ; Considering the worst case, load 4 potiential arguments\r
- ; into registers.\r
- mov rcx, qword ptr [rsp]\r
- mov rdx, qword ptr [rsp+8h]\r
- mov r8, qword ptr [rsp+10h]\r
- mov r9, qword ptr [rsp+18h]\r
-\r
- ; Now call the external routine\r
- call rbx\r
- \r
- ; Function epilog\r
- mov rsp, rbp\r
- pop rbx\r
- pop rbp\r
- ret\r
-EbcLLCALLEXNative ENDP\r
-\r
-\r
-; UINTN EbcLLGetEbcEntryPoint(VOID);\r
-; Routine Description:\r
-; The VM thunk code stuffs an EBC entry point into a processor\r
-; register. Since we can't use inline assembly to get it from\r
-; the interpreter C code, stuff it into the return value \r
-; register and return.\r
-;\r
-; Arguments:\r
-; None.\r
-;\r
-; Returns:\r
-; The contents of the register in which the entry point is passed.\r
-;\r
-EbcLLGetEbcEntryPoint PROC NEAR PUBLIC\r
- ret\r
-EbcLLGetEbcEntryPoint ENDP\r
-\r
-;/*++\r
-;\r
-;Routine Description:\r
-; \r
-; Return the caller's value of the stack pointer.\r
-;\r
-;Arguments:\r
-;\r
-; None.\r
-;\r
-;Returns:\r
-;\r
-; The current value of the stack pointer for the caller. We\r
-; adjust it by 4 here because when they called us, the return address\r
-; is put on the stack, thereby lowering it by 4 bytes.\r
-;\r
-;--*/\r
-\r
-; UINTN EbcLLGetStackPointer() \r
-EbcLLGetStackPointer PROC NEAR PUBLIC\r
- mov rax, rsp ; get current stack pointer\r
- ; Stack adjusted by this much when we were called,\r
- ; For this function, it's 4.\r
- add rax, 4\r
- ret\r
-EbcLLGetStackPointer ENDP\r
-\r
-; UINT64 EbcLLGetReturnValue(VOID);\r
-; Routine Description:\r
-; When EBC calls native, on return the VM has to stuff the return\r
-; value into a VM register. It's assumed here that the value is still\r
-; in the register, so simply return and the caller should get the\r
-; return result properly.\r
-;\r
-; Arguments:\r
-; None.\r
-;\r
-; Returns:\r
-; The unmodified value returned by the native code.\r
-;\r
-EbcLLGetReturnValue PROC NEAR PUBLIC\r
- ret\r
-EbcLLGetReturnValue ENDP\r
-\r
-text ENDS\r
-END\r
-\r
+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation \r
-All rights reserved. This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution. The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php \r
- \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
-\r
- EbcSupport.c\r
-\r
-Abstract:\r
-\r
- This module contains EBC support routines that are customized based on\r
- the target x64 processor.\r
-\r
---*/\r
-\r
-#include "EbcInt.h"\r
-#include "EbcExecute.h"\r
-\r
-//\r
-// NOTE: This is the stack size allocated for the interpreter\r
-// when it executes an EBC image. The requirements can change\r
-// based on whether or not a debugger is present, and other\r
-// platform-specific configurations.\r
-//\r
-#define VM_STACK_SIZE (1024 * 8)\r
-#define EBC_THUNK_SIZE 64\r
-\r
-#define STACK_REMAIN_SIZE (1024 * 4)\r
-\r
-STATIC\r
-VOID\r
-PushU64 (\r
- VM_CONTEXT *VmPtr,\r
- UINT64 Arg\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Push a 64 bit unsigned value to the VM stack.\r
- \r
-Arguments:\r
-\r
- VmPtr - The pointer to current VM context.\r
- Arg - The value to be pushed\r
-\r
-Returns:\r
-\r
- VOID\r
- \r
---*/\r
-{\r
- //\r
- // Advance the VM stack down, and then copy the argument to the stack.\r
- // Hope it's aligned.\r
- //\r
- VmPtr->R[0] -= sizeof (UINT64);\r
- *(UINT64 *) VmPtr->R[0] = Arg;\r
- return;\r
-}\r
-\r
-STATIC\r
-UINT64\r
-EbcInterpret (\r
- UINTN Arg1,\r
- UINTN Arg2,\r
- UINTN Arg3,\r
- UINTN Arg4,\r
- UINTN Arg5,\r
- UINTN Arg6,\r
- UINTN Arg7,\r
- UINTN Arg8,\r
- UINTN Arg9,\r
- UINTN Arg10,\r
- UINTN Arg11,\r
- UINTN Arg12,\r
- UINTN Arg13,\r
- UINTN Arg14,\r
- UINTN Arg15,\r
- UINTN Arg16\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Begin executing an EBC image. The address of the entry point is passed\r
- in via a processor register, so we'll need to make a call to get the\r
- value.\r
- \r
-Arguments:\r
-\r
- This is a thunk function. Microsoft x64 compiler only provide fast_call\r
- calling convention, so the first four arguments are passed by rcx, rdx, \r
- r8, and r9, while other arguments are passed in stack.\r
-\r
-Returns:\r
-\r
- The value returned by the EBC application we're going to run.\r
- \r
---*/\r
-{\r
- //\r
- // Create a new VM context on the stack\r
- //\r
- VM_CONTEXT VmContext;\r
- UINTN Addr;\r
- EFI_STATUS Status;\r
- UINTN StackIndex;\r
-\r
- //\r
- // Get the EBC entry point from the processor register.\r
- // Don't call any function before getting the EBC entry\r
- // point because this will collab the return register.\r
- //\r
- Addr = EbcLLGetEbcEntryPoint ();\r
-\r
- //\r
- // Now clear out our context\r
- //\r
- ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
-\r
- //\r
- // Set the VM instruction pointer to the correct location in memory.\r
- //\r
- VmContext.Ip = (VMIP) Addr;\r
-\r
- //\r
- // Initialize the stack pointer for the EBC. Get the current system stack\r
- // pointer and adjust it down by the max needed for the interpreter.\r
- //\r
- Addr = EbcLLGetStackPointer ();\r
-\r
- //\r
- // Adjust the VM's stack pointer down.\r
- //\r
- \r
- Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
- VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
- VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
- VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
- VmContext.R[0] -= sizeof (UINTN);\r
-\r
- //\r
- // Align the stack on a natural boundary.\r
- //\r
- VmContext.R[0] &= ~(sizeof (UINTN) - 1);\r
-\r
- //\r
- // Put a magic value in the stack gap, then adjust down again.\r
- //\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
- VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
-\r
- //\r
- // The stack upper to LowStackTop is belong to the VM.\r
- //\r
- VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
-\r
- //\r
- // For the worst case, assume there are 4 arguments passed in registers, store\r
- // them to VM's stack.\r
- //\r
- PushU64 (&VmContext, (UINT64) Arg16);\r
- PushU64 (&VmContext, (UINT64) Arg15);\r
- PushU64 (&VmContext, (UINT64) Arg14);\r
- PushU64 (&VmContext, (UINT64) Arg13);\r
- PushU64 (&VmContext, (UINT64) Arg12);\r
- PushU64 (&VmContext, (UINT64) Arg11);\r
- PushU64 (&VmContext, (UINT64) Arg10);\r
- PushU64 (&VmContext, (UINT64) Arg9);\r
- PushU64 (&VmContext, (UINT64) Arg8);\r
- PushU64 (&VmContext, (UINT64) Arg7);\r
- PushU64 (&VmContext, (UINT64) Arg6);\r
- PushU64 (&VmContext, (UINT64) Arg5);\r
- PushU64 (&VmContext, (UINT64) Arg4);\r
- PushU64 (&VmContext, (UINT64) Arg3);\r
- PushU64 (&VmContext, (UINT64) Arg2);\r
- PushU64 (&VmContext, (UINT64) Arg1);\r
-\r
- //\r
- // Interpreter assumes 64-bit return address is pushed on the stack.\r
- // The x64 does not do this so pad the stack accordingly.\r
- //\r
- PushU64 (&VmContext, (UINT64) 0);\r
- PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
-\r
- //\r
- // For x64, this is where we say our return address is\r
- //\r
- VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
-\r
- //\r
- // We need to keep track of where the EBC stack starts. This way, if the EBC\r
- // accesses any stack variables above its initial stack setting, then we know\r
- // it's accessing variables passed into it, which means the data is on the\r
- // VM's stack.\r
- // When we're called, on the stack (high to low) we have the parameters, the\r
- // return address, then the saved ebp. Save the pointer to the return address.\r
- // EBC code knows that's there, so should look above it for function parameters.\r
- // The offset is the size of locals (VMContext + Addr + saved ebp).\r
- // Note that the interpreter assumes there is a 16 bytes of return address on\r
- // the stack too, so adjust accordingly.\r
- // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
- //\r
-\r
- //\r
- // Begin executing the EBC code\r
- //\r
- EbcExecute (&VmContext);\r
-\r
- //\r
- // Return the value in R[7] unless there was an error\r
- //\r
- ReturnEBCStack(StackIndex);\r
- return (UINT64) VmContext.R[7];\r
-}\r
-\r
-STATIC\r
-UINT64\r
-ExecuteEbcImageEntryPoint (\r
- IN EFI_HANDLE ImageHandle,\r
- IN EFI_SYSTEM_TABLE *SystemTable\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Begin executing an EBC image. The address of the entry point is passed\r
- in via a processor register, so we'll need to make a call to get the\r
- value.\r
- \r
-Arguments:\r
-\r
- ImageHandle - image handle for the EBC application we're executing\r
- SystemTable - standard system table passed into an driver's entry point\r
-\r
-Returns:\r
-\r
- The value returned by the EBC application we're going to run.\r
-\r
---*/\r
-{\r
- //\r
- // Create a new VM context on the stack\r
- //\r
- VM_CONTEXT VmContext;\r
- UINTN Addr;\r
- EFI_STATUS Status;\r
- UINTN StackIndex;\r
-\r
- //\r
- // Get the EBC entry point from the processor register. Make sure you don't\r
- // call any functions before this or you could mess up the register the\r
- // entry point is passed in.\r
- //\r
- Addr = EbcLLGetEbcEntryPoint ();\r
-\r
- //\r
- // Now clear out our context\r
- //\r
- ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
-\r
- //\r
- // Save the image handle so we can track the thunks created for this image\r
- //\r
- VmContext.ImageHandle = ImageHandle;\r
- VmContext.SystemTable = SystemTable;\r
-\r
- //\r
- // Set the VM instruction pointer to the correct location in memory.\r
- //\r
- VmContext.Ip = (VMIP) Addr;\r
-\r
- //\r
- // Initialize the stack pointer for the EBC. Get the current system stack\r
- // pointer and adjust it down by the max needed for the interpreter.\r
- //\r
- Addr = EbcLLGetStackPointer ();\r
-\r
- Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
- VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
- VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
- VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
- VmContext.R[0] -= sizeof (UINTN);\r
-\r
-\r
- //\r
- // Put a magic value in the stack gap, then adjust down again\r
- //\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
- VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
-\r
- //\r
- // Align the stack on a natural boundary\r
- VmContext.R[0] &= ~(sizeof(UINTN) - 1);\r
- //\r
- VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
-\r
- //\r
- // Simply copy the image handle and system table onto the EBC stack.\r
- // Greatly simplifies things by not having to spill the args.\r
- //\r
- PushU64 (&VmContext, (UINT64) SystemTable);\r
- PushU64 (&VmContext, (UINT64) ImageHandle);\r
-\r
- //\r
- // VM pushes 16-bytes for return address. Simulate that here.\r
- //\r
- PushU64 (&VmContext, (UINT64) 0);\r
- PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
-\r
- //\r
- // For x64, this is where we say our return address is\r
- //\r
- VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
-\r
- //\r
- // Entry function needn't access high stack context, simply\r
- // put the stack pointer here.\r
- //\r
-\r
- //\r
- // Begin executing the EBC code\r
- //\r
- EbcExecute (&VmContext);\r
-\r
- //\r
- // Return the value in R[7] unless there was an error\r
- //\r
- ReturnEBCStack(StackIndex);\r
- return (UINT64) VmContext.R[7];\r
-}\r
-\r
-EFI_STATUS\r
-EbcCreateThunks (\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *EbcEntryPoint,\r
- OUT VOID **Thunk,\r
- IN UINT32 Flags\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Create an IA32 thunk for the given EBC entry point.\r
- \r
-Arguments:\r
-\r
- ImageHandle - Handle of image for which this thunk is being created\r
- EbcEntryPoint - Address of the EBC code that the thunk is to call\r
- Thunk - Returned thunk we create here\r
-\r
-Returns:\r
-\r
- Standard EFI status.\r
- \r
---*/\r
-{\r
- UINT8 *Ptr;\r
- UINT8 *ThunkBase;\r
- UINT32 I;\r
- UINT64 Addr;\r
- INT32 Size;\r
- INT32 ThunkSize;\r
-\r
- //\r
- // Check alignment of pointer to EBC code\r
- //\r
- if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- Size = EBC_THUNK_SIZE;\r
- ThunkSize = Size;\r
-\r
- Ptr = AllocatePool (Size);\r
-\r
- if (Ptr == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- //\r
- // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
- //\r
- // Save the start address so we can add a pointer to it to a list later.\r
- //\r
- ThunkBase = Ptr;\r
-\r
- //\r
- // Give them the address of our buffer we're going to fix up\r
- //\r
- *Thunk = (VOID *) Ptr;\r
-\r
- //\r
- // Add a magic code here to help the VM recognize the thunk..\r
- // mov rax, ca112ebccall2ebch => 48 B8 BC 2E 11 CA BC 2E 11 CA\r
- //\r
- *Ptr = 0x48;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xB8;\r
- Ptr++;\r
- Size--;\r
- Addr = (UINT64) 0xCA112EBCCA112EBCULL;\r
- for (I = 0; I < sizeof (Addr); I++) {\r
- *Ptr = (UINT8) (UINTN) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
-\r
- //\r
- // Add code bytes to load up a processor register with the EBC entry point.\r
- // mov rax, 123456789abcdef0h => 48 B8 F0 DE BC 9A 78 56 34 12\r
- // The first 8 bytes of the thunk entry is the address of the EBC\r
- // entry point.\r
- //\r
- *Ptr = 0x48;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xB8;\r
- Ptr++;\r
- Size--;\r
- Addr = (UINT64) EbcEntryPoint;\r
- for (I = 0; I < sizeof (Addr); I++) {\r
- *Ptr = (UINT8) (UINTN) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
-\r
- //\r
- // Stick in a load of ecx with the address of appropriate VM function.\r
- // Using r11 because it's a volatile register and won't be used in this\r
- // point.\r
- // mov r11 123456789abcdef0h => 49 BB F0 DE BC 9A 78 56 34 12\r
- //\r
- if (Flags & FLAG_THUNK_ENTRY_POINT) {\r
- Addr = (UINTN) ExecuteEbcImageEntryPoint;\r
- } else {\r
- Addr = (UINTN) EbcInterpret;\r
- }\r
-\r
- //\r
- // mov r11 Addr => 0x49 0xBB\r
- //\r
- *Ptr = 0x49;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xBB;\r
- Ptr++;\r
- Size--;\r
- for (I = 0; I < sizeof (Addr); I++) {\r
- *Ptr = (UINT8) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
- //\r
- // Stick in jump opcode bytes for jmp r11 => 0x41 0xFF 0xE3\r
- //\r
- *Ptr = 0x41;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xFF;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xE3;\r
- Size--;\r
-\r
- //\r
- // Double check that our defined size is ok (application error)\r
- //\r
- if (Size < 0) {\r
- ASSERT (FALSE);\r
- return EFI_BUFFER_TOO_SMALL;\r
- }\r
- //\r
- // Add the thunk to the list for this image. Do this last since the add\r
- // function flushes the cache for us.\r
- //\r
- EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-VOID\r
-EbcLLCALLEX (\r
- IN VM_CONTEXT *VmPtr,\r
- IN UINTN FuncAddr,\r
- IN UINTN NewStackPointer,\r
- IN VOID *FramePtr,\r
- IN UINT8 Size\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This function is called to execute an EBC CALLEX instruction. \r
- The function check the callee's content to see whether it is common native\r
- code or a thunk to another piece of EBC code.\r
- If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
- otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
- be startup which cost time and stack space.\r
- \r
-Arguments:\r
-\r
- VmPtr - Pointer to a VM context.\r
- FuncAddr - Callee's address\r
- NewStackPointer - New stack pointer after the call\r
- FramePtr - New frame pointer after the call\r
- Size - The size of call instruction\r
-\r
-Returns:\r
-\r
- None.\r
- \r
---*/\r
-{\r
- UINTN IsThunk;\r
- UINTN TargetEbcAddr;\r
-\r
- IsThunk = 1;\r
- TargetEbcAddr = 0;\r
-\r
- //\r
- // Processor specific code to check whether the callee is a thunk to EBC.\r
- //\r
- if (*((UINT8 *)FuncAddr) != 0x48) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 1) != 0xB8) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 2) != 0xBC) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 3) != 0x2E) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 4) != 0x11) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 5) != 0xCA) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 6) != 0xBC) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 7) != 0x2E) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 8) != 0x11) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 9) != 0xCA) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 10) != 0x48) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 11) != 0xB8) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
-\r
- CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + 12, 8);\r
-\r
-Action:\r
- if (IsThunk == 1){\r
- //\r
- // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
- // put our return address and frame pointer on the VM stack.\r
- // Then set the VM's IP to new EBC code.\r
- //\r
- VmPtr->R[0] -= 8;\r
- VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
- VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
- VmPtr->R[0] -= 8;\r
- VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (VmPtr->Ip + Size));\r
-\r
- VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
- } else {\r
- //\r
- // The callee is not a thunk to EBC, call native code.\r
- //\r
- EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
-\r
- //\r
- // Get return value and advance the IP.\r
- //\r
- VmPtr->R[7] = EbcLLGetReturnValue ();\r
- VmPtr->Ip += Size;\r
- }\r
-}\r
-\r