X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FEbcDxe%2FEbcExecute.c;h=a4c51e9b0f614c73a16f2fdbaa75a876ad5ac822;hp=d44892e84af3b6be9f0b5d3f74db81fb19830f72;hb=6f0a3cd23e4a2322c58c7dbf0ef8e66a4a01c42c;hpb=fb0b259e4e440577dcd6ba6722c252d90605b3e9 diff --git a/MdeModulePkg/Universal/EbcDxe/EbcExecute.c b/MdeModulePkg/Universal/EbcDxe/EbcExecute.c index d44892e84a..a4c51e9b0f 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcExecute.c +++ b/MdeModulePkg/Universal/EbcDxe/EbcExecute.c @@ -1,8 +1,8 @@ /** @file Contains code that implements the virtual machine. -Copyright (c) 2006, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "EbcInt.h" #include "EbcExecute.h" +#include "EbcDebuggerHook.h" // @@ -42,273 +43,822 @@ UINT64 IN UINT64 Op2 ); -STATIC +/** + Decode a 16-bit index to determine the offset. Given an index value: + + b15 - sign bit + b14:12 - number of bits in this index assigned to natural units (=a) + ba:11 - constant units = ConstUnits + b0:a - natural units = NaturalUnits + + Given this info, the offset can be computed by: + offset = sign_bit * (ConstUnits + NaturalUnits * sizeof(UINTN)) + + Max offset is achieved with index = 0x7FFF giving an offset of + 0x27B (32-bit machine) or 0x477 (64-bit machine). + Min offset is achieved with index = + + @param VmPtr A pointer to VM context. + @param CodeOffset Offset from IP of the location of the 16-bit index + to decode. + + @return The decoded offset. + +**/ INT16 VmReadIndex16 ( - IN VM_CONTEXT *VmPtr, - IN UINT32 CodeOffset + IN VM_CONTEXT *VmPtr, + IN UINT32 CodeOffset ); -STATIC +/** + Decode a 32-bit index to determine the offset. + + @param VmPtr A pointer to VM context. + @param CodeOffset Offset from IP of the location of the 32-bit index + to decode. + + @return Converted index per EBC VM specification. + +**/ INT32 VmReadIndex32 ( - IN VM_CONTEXT *VmPtr, - IN UINT32 CodeOffset + IN VM_CONTEXT *VmPtr, + IN UINT32 CodeOffset ); -STATIC +/** + Decode a 64-bit index to determine the offset. + + @param VmPtr A pointer to VM context.s + @param CodeOffset Offset from IP of the location of the 64-bit index + to decode. + + @return Converted index per EBC VM specification + +**/ INT64 VmReadIndex64 ( - IN VM_CONTEXT *VmPtr, - IN UINT32 CodeOffset + IN VM_CONTEXT *VmPtr, + IN UINT32 CodeOffset ); -STATIC +/** + Reads 8-bit data form the memory address. + + @param VmPtr A pointer to VM context. + @param Addr The memory address. + + @return The 8-bit value from the memory address. + +**/ UINT8 VmReadMem8 ( - IN VM_CONTEXT *VmPtr, - IN UINTN Addr + IN VM_CONTEXT *VmPtr, + IN UINTN Addr ); -STATIC +/** + Reads 16-bit data form the memory address. + + @param VmPtr A pointer to VM context. + @param Addr The memory address. + + @return The 16-bit value from the memory address. + +**/ UINT16 VmReadMem16 ( IN VM_CONTEXT *VmPtr, IN UINTN Addr ); -STATIC +/** + Reads 32-bit data form the memory address. + + @param VmPtr A pointer to VM context. + @param Addr The memory address. + + @return The 32-bit value from the memory address. + +**/ UINT32 VmReadMem32 ( IN VM_CONTEXT *VmPtr, IN UINTN Addr ); -STATIC +/** + Reads 64-bit data form the memory address. + + @param VmPtr A pointer to VM context. + @param Addr The memory address. + + @return The 64-bit value from the memory address. + +**/ UINT64 VmReadMem64 ( - IN VM_CONTEXT *VmPtr, - IN UINTN Addr + IN VM_CONTEXT *VmPtr, + IN UINTN Addr ); -STATIC +/** + Read a natural value from memory. May or may not be aligned. + + @param VmPtr current VM context + @param Addr the address to read from + + @return The natural value at address Addr. + +**/ UINTN VmReadMemN ( - IN VM_CONTEXT *VmPtr, - IN UINTN Addr + IN VM_CONTEXT *VmPtr, + IN UINTN Addr ); -STATIC +/** + Writes 8-bit data to memory address. + + This routine is called by the EBC data + movement instructions that write to memory. Since these writes + may be to the stack, which looks like (high address on top) this, + + [EBC entry point arguments] + [VM stack] + [EBC stack] + + we need to detect all attempts to write to the EBC entry point argument + stack area and adjust the address (which will initially point into the + VM stack) to point into the EBC entry point arguments. + + @param VmPtr A pointer to a VM context. + @param Addr Address to write to. + @param Data Value to write to Addr. + + @retval EFI_SUCCESS The instruction is executed successfully. + @retval Other Some error occurs when writing data to the address. + +**/ EFI_STATUS VmWriteMem8 ( - IN VM_CONTEXT *VmPtr, - UINTN Addr, - IN UINT8 Data + IN VM_CONTEXT *VmPtr, + IN UINTN Addr, + IN UINT8 Data ); -STATIC +/** + Writes 16-bit data to memory address. + + This routine is called by the EBC data + movement instructions that write to memory. Since these writes + may be to the stack, which looks like (high address on top) this, + + [EBC entry point arguments] + [VM stack] + [EBC stack] + + we need to detect all attempts to write to the EBC entry point argument + stack area and adjust the address (which will initially point into the + VM stack) to point into the EBC entry point arguments. + + @param VmPtr A pointer to a VM context. + @param Addr Address to write to. + @param Data Value to write to Addr. + + @retval EFI_SUCCESS The instruction is executed successfully. + @retval Other Some error occurs when writing data to the address. + +**/ EFI_STATUS VmWriteMem16 ( - IN VM_CONTEXT *VmPtr, - UINTN Addr, - IN UINT16 Data + IN VM_CONTEXT *VmPtr, + IN UINTN Addr, + IN UINT16 Data ); -STATIC +/** + Writes 32-bit data to memory address. + + This routine is called by the EBC data + movement instructions that write to memory. Since these writes + may be to the stack, which looks like (high address on top) this, + + [EBC entry point arguments] + [VM stack] + [EBC stack] + + we need to detect all attempts to write to the EBC entry point argument + stack area and adjust the address (which will initially point into the + VM stack) to point into the EBC entry point arguments. + + @param VmPtr A pointer to a VM context. + @param Addr Address to write to. + @param Data Value to write to Addr. + + @retval EFI_SUCCESS The instruction is executed successfully. + @retval Other Some error occurs when writing data to the address. + +**/ EFI_STATUS VmWriteMem32 ( - IN VM_CONTEXT *VmPtr, - UINTN Addr, - IN UINT32 Data + IN VM_CONTEXT *VmPtr, + IN UINTN Addr, + IN UINT32 Data ); -STATIC +/** + Reads 16-bit unsigned data from the code stream. + + This routine provides the ability to read raw unsigned data from the code + stream. + + @param VmPtr A pointer to VM context + @param Offset Offset from current IP to the raw data to read. + + @return The raw unsigned 16-bit value from the code stream. + +**/ UINT16 VmReadCode16 ( IN VM_CONTEXT *VmPtr, IN UINT32 Offset ); -STATIC +/** + Reads 32-bit unsigned data from the code stream. + + This routine provides the ability to read raw unsigned data from the code + stream. + + @param VmPtr A pointer to VM context + @param Offset Offset from current IP to the raw data to read. + + @return The raw unsigned 32-bit value from the code stream. + +**/ UINT32 VmReadCode32 ( IN VM_CONTEXT *VmPtr, IN UINT32 Offset ); -STATIC +/** + Reads 64-bit unsigned data from the code stream. + + This routine provides the ability to read raw unsigned data from the code + stream. + + @param VmPtr A pointer to VM context + @param Offset Offset from current IP to the raw data to read. + + @return The raw unsigned 64-bit value from the code stream. + +**/ UINT64 VmReadCode64 ( IN VM_CONTEXT *VmPtr, IN UINT32 Offset ); -STATIC +/** + Reads 8-bit immediate value at the offset. + + This routine is called by the EBC execute + functions to read EBC immediate values from the code stream. + Since we can't assume alignment, each tries to read in the biggest + chunks size available, but will revert to smaller reads if necessary. + + @param VmPtr A pointer to a VM context. + @param Offset offset from IP of the code bytes to read. + + @return Signed data of the requested size from the specified address. + +**/ INT8 VmReadImmed8 ( IN VM_CONTEXT *VmPtr, IN UINT32 Offset ); -STATIC +/** + Reads 16-bit immediate value at the offset. + + This routine is called by the EBC execute + functions to read EBC immediate values from the code stream. + Since we can't assume alignment, each tries to read in the biggest + chunks size available, but will revert to smaller reads if necessary. + + @param VmPtr A pointer to a VM context. + @param Offset offset from IP of the code bytes to read. + + @return Signed data of the requested size from the specified address. + +**/ INT16 VmReadImmed16 ( IN VM_CONTEXT *VmPtr, IN UINT32 Offset ); -STATIC +/** + Reads 32-bit immediate value at the offset. + + This routine is called by the EBC execute + functions to read EBC immediate values from the code stream. + Since we can't assume alignment, each tries to read in the biggest + chunks size available, but will revert to smaller reads if necessary. + + @param VmPtr A pointer to a VM context. + @param Offset offset from IP of the code bytes to read. + + @return Signed data of the requested size from the specified address. + +**/ INT32 VmReadImmed32 ( IN VM_CONTEXT *VmPtr, IN UINT32 Offset ); -STATIC +/** + Reads 64-bit immediate value at the offset. + + This routine is called by the EBC execute + functions to read EBC immediate values from the code stream. + Since we can't assume alignment, each tries to read in the biggest + chunks size available, but will revert to smaller reads if necessary. + + @param VmPtr A pointer to a VM context. + @param Offset offset from IP of the code bytes to read. + + @return Signed data of the requested size from the specified address. + +**/ INT64 VmReadImmed64 ( IN VM_CONTEXT *VmPtr, IN UINT32 Offset ); -STATIC +/** + Given an address that EBC is going to read from or write to, return + an appropriate address that accounts for a gap in the stack. + The stack for this application looks like this (high addr on top) + [EBC entry point arguments] + [VM stack] + [EBC stack] + The EBC assumes that its arguments are at the top of its stack, which + is where the VM stack is really. Therefore if the EBC does memory + accesses into the VM stack area, then we need to convert the address + to point to the EBC entry point arguments area. Do this here. + + @param VmPtr A Pointer to VM context. + @param Addr Address of interest + + @return The unchanged address if it's not in the VM stack region. Otherwise, + adjust for the stack gap and return the modified address. + +**/ UINTN ConvertStackAddr ( - IN VM_CONTEXT *VmPtr, - IN UINTN Addr + IN VM_CONTEXT *VmPtr, + IN UINTN Addr ); -STATIC +/** + Execute all the EBC data manipulation instructions. + Since the EBC data manipulation instructions all have the same basic form, + they can share the code that does the fetch of operands and the write-back + of the result. This function performs the fetch of the operands (even if + both are not needed to be fetched, like NOT instruction), dispatches to the + appropriate subfunction, then writes back the returned result. + + Format: + INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16} + + @param VmPtr A pointer to VM context. + @param IsSignedOp Indicates whether the operand is signed or not. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteDataManip ( IN VM_CONTEXT *VmPtr, - IN BOOLEAN IsSignedOperation + IN BOOLEAN IsSignedOp ); // // Functions that execute VM opcodes // -STATIC +/** + Execute the EBC BREAK instruction. + + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteBREAK ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the JMP instruction. + + Instruction syntax: + JMP64{cs|cc} Immed64 + JMP32{cs|cc} {@}R1 {Immed32|Index32} + + Encoding: + b0.7 - immediate data present + b0.6 - 1 = 64 bit immediate data + 0 = 32 bit immediate data + b1.7 - 1 = conditional + b1.6 1 = CS (condition set) + 0 = CC (condition clear) + b1.4 1 = relative address + 0 = absolute address + b1.3 1 = operand1 indirect + b1.2-0 operand 1 + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteJMP ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC JMP8 instruction. + + Instruction syntax: + JMP8{cs|cc} Offset/2 + + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteJMP8 ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Implements the EBC CALL instruction. + + Instruction format: + CALL64 Immed64 + CALL32 {@}R1 {Immed32|Index32} + CALLEX64 Immed64 + CALLEX16 {@}R1 {Immed32} + + If Rx == R0, then it's a PC relative call to PC = PC + imm32. + + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteCALL ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC RET instruction. + + Instruction syntax: + RET + + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteRET ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC CMP instruction. + + Instruction syntax: + CMP[32|64][eq|lte|gte|ulte|ugte] R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteCMP ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC CMPI instruction + + Instruction syntax: + CMPI[32|64]{w|d}[eq|lte|gte|ulte|ugte] {@}Rx {Index16}, Immed16|Immed32 + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteCMPI ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the MOVxx instructions. + + Instruction format: + + MOV[b|w|d|q|n]{w|d} {@}R1 {Index16|32}, {@}R2 {Index16|32} + MOVqq {@}R1 {Index64}, {@}R2 {Index64} + + Copies contents of [R2] -> [R1], zero extending where required. + + First character indicates the size of the move. + Second character indicates the size of the index(s). + + Invalid to have R1 direct with index. + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteMOVxx ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC MOVI. + + Instruction syntax: + + MOVI[b|w|d|q][w|d|q] {@}R1 {Index16}, ImmData16|32|64 + + First variable character specifies the move size + Second variable character specifies size of the immediate data + + Sign-extend the immediate data to the size of the operation, and zero-extend + if storing to a register. + + Operand1 direct with index/immed is invalid. + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteMOVI ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC MOV immediate natural. This instruction moves an immediate + index value into a register or memory location. + + Instruction syntax: + + MOVIn[w|d|q] {@}R1 {Index16}, Index16|32|64 + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteMOVIn ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC MOVREL instruction. + Dest <- Ip + ImmData + + Instruction syntax: + + MOVREL[w|d|q] {@}R1 {Index16}, ImmData16|32|64 + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteMOVREL ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC PUSHn instruction + + Instruction syntax: + PUSHn {@}R1 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecutePUSHn ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC PUSH instruction. + + Instruction syntax: + PUSH[32|64] {@}R1 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecutePUSH ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC POPn instruction. + + Instruction syntax: + POPn {@}R1 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecutePOPn ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC POP instruction. + + Instruction syntax: + POPn {@}R1 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecutePOP ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute all the EBC signed data manipulation instructions. + Since the EBC data manipulation instructions all have the same basic form, + they can share the code that does the fetch of operands and the write-back + of the result. This function performs the fetch of the operands (even if + both are not needed to be fetched, like NOT instruction), dispatches to the + appropriate subfunction, then writes back the returned result. + + Format: + INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16} + + @param VmPtr A pointer to VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteSignedDataManip ( - IN VM_CONTEXT *VmPtr + IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute all the EBC unsigned data manipulation instructions. + Since the EBC data manipulation instructions all have the same basic form, + they can share the code that does the fetch of operands and the write-back + of the result. This function performs the fetch of the operands (even if + both are not needed to be fetched, like NOT instruction), dispatches to the + appropriate subfunction, then writes back the returned result. + + Format: + INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16} + + @param VmPtr A pointer to VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteUnsignedDataManip ( - IN VM_CONTEXT *VmPtr + IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC LOADSP instruction. + + Instruction syntax: + LOADSP SP1, R2 + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteLOADSP ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC STORESP instruction. + + Instruction syntax: + STORESP Rx, FLAGS|IP + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteSTORESP ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC MOVsnw instruction. This instruction loads a signed + natural value from memory or register to another memory or register. On + 32-bit machines, the value gets sign-extended to 64 bits if the destination + is a register. + + Instruction syntax: + + MOVsnd {@}R1 {Indx32}, {@}R2 {Index32|Immed32} + + 0:7 1=>operand1 index present + 0:6 1=>operand2 index present + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteMOVsnd ( IN VM_CONTEXT *VmPtr ); -STATIC +/** + Execute the EBC MOVsnw instruction. This instruction loads a signed + natural value from memory or register to another memory or register. On + 32-bit machines, the value gets sign-extended to 64 bits if the destination + is a register. + + Instruction syntax: + + MOVsnw {@}R1 {Index16}, {@}R2 {Index16|Immed16} + + 0:7 1=>operand1 index present + 0:6 1=>operand2 index present + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteMOVsnw ( IN VM_CONTEXT *VmPtr @@ -317,163 +867,391 @@ ExecuteMOVsnw ( // // Data manipulation subfunctions // -STATIC +/** + Execute the EBC NOT instruction.s + + Instruction syntax: + NOT[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return ~Op2 + +**/ UINT64 ExecuteNOT ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC NEG instruction. + + Instruction syntax: + NEG[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op2 * -1 + +**/ UINT64 ExecuteNEG ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC ADD instruction. + + Instruction syntax: + ADD[32|64] {@}R1, {@}R2 {Index16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 + Op2 + +**/ UINT64 ExecuteADD ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC SUB instruction. + + Instruction syntax: + SUB[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 - Op2 + +**/ UINT64 ExecuteSUB ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC MUL instruction. + + Instruction syntax: + SUB[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 * Op2 + +**/ UINT64 ExecuteMUL ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC MULU instruction + + Instruction syntax: + MULU[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return (unsigned)Op1 * (unsigned)Op2 + +**/ UINT64 ExecuteMULU ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC DIV instruction. + + Instruction syntax: + DIV[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 / Op2 + +**/ UINT64 ExecuteDIV ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC DIVU instruction + + Instruction syntax: + DIVU[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return (unsigned)Op1 / (unsigned)Op2 + +**/ UINT64 ExecuteDIVU ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC MOD instruction. + + Instruction syntax: + MOD[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 MODULUS Op2 + +**/ UINT64 ExecuteMOD ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC MODU instruction. + + Instruction syntax: + MODU[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 UNSIGNED_MODULUS Op2 + +**/ UINT64 ExecuteMODU ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC AND instruction. + + Instruction syntax: + AND[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 AND Op2 + +**/ UINT64 ExecuteAND ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC OR instruction. + + Instruction syntax: + OR[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 OR Op2 + +**/ UINT64 ExecuteOR ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC XOR instruction. + + Instruction syntax: + XOR[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 XOR Op2 + +**/ UINT64 ExecuteXOR ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC SHL shift left instruction. + + Instruction syntax: + SHL[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 << Op2 + +**/ UINT64 ExecuteSHL ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC SHR instruction. + + Instruction syntax: + SHR[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 >> Op2 (unsigned operands) + +**/ UINT64 ExecuteSHR ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC ASHR instruction. + + Instruction syntax: + ASHR[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return Op1 >> Op2 (signed) + +**/ UINT64 ExecuteASHR ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC EXTNDB instruction to sign-extend a byte value. + + Instruction syntax: + EXTNDB[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return (INT64)(INT8)Op2 + +**/ UINT64 ExecuteEXTNDB ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC EXTNDW instruction to sign-extend a 16-bit value. + + Instruction syntax: + EXTNDW[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return (INT64)(INT16)Op2 + +**/ UINT64 ExecuteEXTNDW ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); -STATIC +/** + Execute the EBC EXTNDD instruction to sign-extend a 32-bit value. + + Instruction syntax: + EXTNDD[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. + @param Op1 Operand 1 from the instruction + @param Op2 Operand 2 from the instruction + + @return (INT64)(INT32)Op2 + +**/ UINT64 ExecuteEXTNDD ( - IN VM_CONTEXT *VmPtr, - IN UINT64 Op1, - IN UINT64 Op2 + IN VM_CONTEXT *VmPtr, + IN UINT64 Op1, + IN UINT64 Op2 ); // // Once we retrieve the operands for the data manipulation instructions, // call these functions to perform the operation. // -static CONST DATA_MANIP_EXEC_FUNCTION mDataManipDispatchTable[] = { +CONST DATA_MANIP_EXEC_FUNCTION mDataManipDispatchTable[] = { ExecuteNOT, ExecuteNEG, ExecuteADD, @@ -495,7 +1273,7 @@ static CONST DATA_MANIP_EXEC_FUNCTION mDataManipDispatchTable[] = { ExecuteEXTNDD, }; -static CONST VM_TABLE_ENTRY mVmOpcodeTable[] = { +CONST VM_TABLE_ENTRY mVmOpcodeTable[] = { { ExecuteBREAK }, // opcode 0x00 { ExecuteJMP }, // opcode 0x01 { ExecuteJMP8 }, // opcode 0x02 @@ -553,33 +1331,36 @@ static CONST VM_TABLE_ENTRY mVmOpcodeTable[] = { { ExecutePOPn }, // opcode 0x36 { ExecuteMOVI }, // opcode 0x37 - mov immediate data { ExecuteMOVIn }, // opcode 0x38 - mov immediate natural - { ExecuteMOVREL } // opcode 0x39 - move data relative to PC + { ExecuteMOVREL }, // opcode 0x39 - move data relative to PC + { NULL }, // opcode 0x3a + { NULL }, // opcode 0x3b + { NULL }, // opcode 0x3c + { NULL }, // opcode 0x3d + { NULL }, // opcode 0x3e + { NULL } // opcode 0x3f }; // // Length of JMP instructions, depending on upper two bits of opcode. // -static CONST UINT8 mJMPLen[] = { 2, 2, 6, 10 }; - -// -// Simple Debugger Protocol GUID -// -EFI_GUID mEbcSimpleDebuggerProtocolGuid = EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID; - +CONST UINT8 mJMPLen[] = { 2, 2, 6, 10 }; /** Given a pointer to a new VM context, execute one or more instructions. This function is only used for test purposes via the EBC VM test protocol. - @param This pointer to protocol interface - @param VmPtr pointer to a VM context - @param InstructionCount how many instructions to execute. 0 if don't count. + @param This A pointer to the EFI_EBC_VM_TEST_PROTOCOL structure. + @param VmPtr A pointer to a VM context. + @param InstructionCount A pointer to a UINTN value holding the number of + instructions to execute. If it holds value of 0, + then the instruction to be executed is 1. - @return EFI_UNSUPPORTED - @return EFI_SUCCESS + @retval EFI_UNSUPPORTED At least one of the opcodes is not supported. + @retval EFI_SUCCESS All of the instructions are executed successfully. **/ EFI_STATUS +EFIAPI EbcExecuteInstructions ( IN EFI_EBC_VM_TEST_PROTOCOL *This, IN VM_CONTEXT *VmPtr, @@ -608,12 +1389,12 @@ EbcExecuteInstructions ( // call it if it's not null. // while (InstructionsLeft != 0) { - ExecFunc = (UINTN) mVmOpcodeTable[(*VmPtr->Ip & 0x3F)].ExecuteFunction; + ExecFunc = (UINTN) mVmOpcodeTable[(*VmPtr->Ip & OPCODE_M_OPCODE)].ExecuteFunction; if (ExecFunc == (UINTN) NULL) { EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr); return EFI_UNSUPPORTED; } else { - mVmOpcodeTable[(*VmPtr->Ip & 0x3F)].ExecuteFunction (VmPtr); + mVmOpcodeTable[(*VmPtr->Ip & OPCODE_M_OPCODE)].ExecuteFunction (VmPtr); *InstructionCount = *InstructionCount + 1; } @@ -632,9 +1413,10 @@ EbcExecuteInstructions ( /** Execute an EBC image from an entry point or from a published protocol. - @param VmPtr pointer to prepared VM context. + @param VmPtr A pointer to a VM context. - @return Standard EBC status. + @retval EFI_UNSUPPORTED At least one of the opcodes is not supported. + @retval EFI_SUCCESS All of the instructions are executed successfully. **/ EFI_STATUS @@ -659,14 +1441,14 @@ EbcExecute ( StackCorrupted = 1; } - VmPtr->FramePtr = (VOID *) ((UINT8 *) (UINTN) VmPtr->R[0] + 8); + VmPtr->FramePtr = (VOID *) ((UINT8 *) (UINTN) VmPtr->Gpr[0] + 8); // // Try to get the debug support for EBC // DEBUG_CODE_BEGIN (); Status = gBS->LocateProtocol ( - &mEbcSimpleDebuggerProtocolGuid, + &gEfiEbcSimpleDebuggerProtocolGuid, NULL, (VOID **) &EbcSimpleDebugger ); @@ -687,24 +1469,16 @@ EbcExecute ( // instruction sets it if it runs out of stack. // VmPtr->StopFlags = 0; - while (!(VmPtr->StopFlags & STOPFLAG_APP_DONE)) { + while ((VmPtr->StopFlags & STOPFLAG_APP_DONE) == 0) { // // If we've found a simple debugger protocol, call it // DEBUG_CODE_BEGIN (); if (EbcSimpleDebugger != NULL) { - EbcSimpleDebugger->Debugger (EbcSimpleDebugger, VmPtr); - } - DEBUG_CODE_END (); - - // - // Verify the opcode is in range. Otherwise generate an exception. - // - if ((*VmPtr->Ip & OPCODE_M_OPCODE) >= (sizeof (mVmOpcodeTable) / sizeof (mVmOpcodeTable[0]))) { - EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr); - Status = EFI_UNSUPPORTED; - goto Done; - } + EbcSimpleDebugger->Debugger (EbcSimpleDebugger, VmPtr); + } + DEBUG_CODE_END (); + // // Use the opcode bits to index into the opcode dispatch table. If the // function pointer is null then generate an exception. @@ -715,6 +1489,9 @@ EbcExecute ( Status = EFI_UNSUPPORTED; goto Done; } + + EbcDebuggerHookExecuteStart (VmPtr); + // // The EBC VM is a strongly ordered processor, so perform a fence operation before // and after each instruction is executed. @@ -725,6 +1502,8 @@ EbcExecute ( MemoryFence (); + EbcDebuggerHookExecuteEnd (VmPtr); + // // If the step flag is set, signal an exception and continue. We don't // clear it here. Assuming the debugger is responsible for clearing it. @@ -735,11 +1514,11 @@ EbcExecute ( // // Make sure stack has not been corrupted. Only report it once though. // - if (!StackCorrupted && (*VmPtr->StackMagicPtr != (UINTN) VM_STACK_KEY_VALUE)) { + if ((StackCorrupted == 0) && (*VmPtr->StackMagicPtr != (UINTN) VM_STACK_KEY_VALUE)) { EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT, EXCEPTION_FLAG_FATAL, VmPtr); StackCorrupted = 1; } - if (!StackCorrupted && ((UINT64)VmPtr->R[0] <= (UINT64)(UINTN) VmPtr->StackTop)) { + if ((StackCorrupted == 0) && ((UINT64)VmPtr->Gpr[0] <= (UINT64)(UINTN) VmPtr->StackTop)) { EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT, EXCEPTION_FLAG_FATAL, VmPtr); StackCorrupted = 1; } @@ -755,20 +1534,24 @@ Done: /** Execute the MOVxx instructions. - @param VmPtr pointer to a VM context. + Instruction format: + + MOV[b|w|d|q|n]{w|d} {@}R1 {Index16|32}, {@}R2 {Index16|32} + MOVqq {@}R1 {Index64}, {@}R2 {Index64} + + Copies contents of [R2] -> [R1], zero extending where required. + + First character indicates the size of the move. + Second character indicates the size of the index(s). + + Invalid to have R1 direct with index. - @return EFI_UNSUPPORTED - @return EFI_SUCCESS - @return Instruction format: - @return MOV[b|w|d|q|n]{w|d} {@}R1 {Index16|32}, {@}R2 {Index16|32} - @return MOVqq {@}R1 {Index64}, {@}R2 {Index64} - @return Copies contents of [R2] -> [R1], zero extending where required. - @return First character indicates the size of the move. - @return Second character indicates the size of the index(s). - @return Invalid to have R1 direct with index. + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteMOVxx ( IN VM_CONTEXT *VmPtr @@ -807,7 +1590,7 @@ ExecuteMOVxx ( // is 2 (opcode + operands). Add to this size each index specified. // Size = 2; - if (Opcode & (OPCODE_M_IMMED_OP1 | OPCODE_M_IMMED_OP2)) { + if ((Opcode & (OPCODE_M_IMMED_OP1 | OPCODE_M_IMMED_OP2)) != 0) { // // Determine size of the index from the opcode. Then get it. // @@ -816,13 +1599,13 @@ ExecuteMOVxx ( // MOVBW, MOVWW, MOVDW, MOVQW, and MOVNW have 16-bit immediate index. // Get one or both index values. // - if (Opcode & OPCODE_M_IMMED_OP1) { + if ((Opcode & OPCODE_M_IMMED_OP1) != 0) { Index16 = VmReadIndex16 (VmPtr, 2); Index64Op1 = (INT64) Index16; Size += sizeof (UINT16); } - if (Opcode & OPCODE_M_IMMED_OP2) { + if ((Opcode & OPCODE_M_IMMED_OP2) != 0) { Index16 = VmReadIndex16 (VmPtr, Size); Index64Op2 = (INT64) Index16; Size += sizeof (UINT16); @@ -831,13 +1614,13 @@ ExecuteMOVxx ( // // MOVBD, MOVWD, MOVDD, MOVQD, and MOVND have 32-bit immediate index // - if (Opcode & OPCODE_M_IMMED_OP1) { + if ((Opcode & OPCODE_M_IMMED_OP1) != 0) { Index32 = VmReadIndex32 (VmPtr, 2); Index64Op1 = (INT64) Index32; Size += sizeof (UINT32); } - if (Opcode & OPCODE_M_IMMED_OP2) { + if ((Opcode & OPCODE_M_IMMED_OP2) != 0) { Index32 = VmReadIndex32 (VmPtr, Size); Index64Op2 = (INT64) Index32; Size += sizeof (UINT32); @@ -846,12 +1629,12 @@ ExecuteMOVxx ( // // MOVqq -- only form with a 64-bit index // - if (Opcode & OPCODE_M_IMMED_OP1) { + if ((Opcode & OPCODE_M_IMMED_OP1) != 0) { Index64Op1 = VmReadIndex64 (VmPtr, 2); Size += sizeof (UINT64); } - if (Opcode & OPCODE_M_IMMED_OP2) { + if ((Opcode & OPCODE_M_IMMED_OP2) != 0) { Index64Op2 = VmReadIndex64 (VmPtr, Size); Size += sizeof (UINT64); } @@ -900,7 +1683,7 @@ ExecuteMOVxx ( // // Indirect form @R2. Compute address of operand2 // - Source = (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index64Op2); + Source = (UINTN) (VmPtr->Gpr[OPERAND2_REGNUM (Operands)] + Index64Op2); // // Now get the data from the source. Always 0-extend and let the compiler // sign-extend where required. @@ -936,12 +1719,12 @@ ExecuteMOVxx ( // // Not indirect source: MOVxx {@}Rx, Ry [Index] // - Data64 = VmPtr->R[OPERAND2_REGNUM (Operands)] + Index64Op2; + Data64 = (UINT64) (VmPtr->Gpr[OPERAND2_REGNUM (Operands)] + Index64Op2); // // Did Operand2 have an index? If so, treat as two signed values since // indexes are signed values. // - if (Opcode & OPCODE_M_IMMED_OP2) { + if ((Opcode & OPCODE_M_IMMED_OP2) != 0) { // // NOTE: need to find a way to fix this, most likely by changing the VM // implementation to remove the stack gap. To do that, we'd need to @@ -972,7 +1755,7 @@ ExecuteMOVxx ( // // Reuse the Source variable to now be dest. // - Source = (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index64Op1); + Source = (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index64Op1); // // Do the write based on the size // @@ -1008,7 +1791,7 @@ ExecuteMOVxx ( // Operand1 direct. // Make sure we didn't have an index on operand1. // - if (Opcode & OPCODE_M_IMMED_OP1) { + if ((Opcode & OPCODE_M_IMMED_OP1) != 0) { EbcDebugSignalException ( EXCEPT_EBC_INSTRUCTION_ENCODING, EXCEPTION_FLAG_FATAL, @@ -1020,7 +1803,7 @@ ExecuteMOVxx ( // Direct storage in register. Clear unused bits and store back to // register. // - VmPtr->R[OPERAND1_REGNUM (Operands)] = Data64 & DataMask; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = Data64 & DataMask; } // // Advance the instruction pointer @@ -1031,26 +1814,26 @@ ExecuteMOVxx ( /** - Execute the EBC BREAK instruction + Execute the EBC BREAK instruction. - @param VmPtr pointer to current VM context + @param VmPtr A pointer to a VM context. - @return EFI_UNSUPPORTED - @return EFI_SUCCESS + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteBREAK ( IN VM_CONTEXT *VmPtr ) { + EFI_STATUS Status; UINT8 Operands; VOID *EbcEntryPoint; VOID *Thunk; UINT64 U64EbcEntryPoint; INT32 Offset; + Thunk = NULL; Operands = GETOPERANDS (VmPtr); switch (Operands) { // @@ -1070,7 +1853,7 @@ ExecuteBREAK ( // 16-8 = Major version // 7-0 = Minor version // - VmPtr->R[7] = GetVmVersion (); + VmPtr->Gpr[7] = GetVmVersion (); break; // @@ -1100,26 +1883,29 @@ ExecuteBREAK ( // After we're done, *(UINT64 *)R7 will be the address of the new thunk. // case 5: - Offset = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->R[7]); - U64EbcEntryPoint = (UINT64) (VmPtr->R[7] + Offset + 4); + Offset = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->Gpr[7]); + U64EbcEntryPoint = (UINT64) (VmPtr->Gpr[7] + Offset + 4); EbcEntryPoint = (VOID *) (UINTN) U64EbcEntryPoint; // // Now create a new thunk // - EbcCreateThunks (VmPtr->ImageHandle, EbcEntryPoint, &Thunk, 0); + Status = EbcCreateThunks (VmPtr->ImageHandle, EbcEntryPoint, &Thunk, 0); + if (EFI_ERROR (Status)) { + return Status; + } // // Finally replace the EBC entry point memory with the thunk address // - VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[7], (UINT64) (UINTN) Thunk); + VmWriteMem64 (VmPtr, (UINTN) VmPtr->Gpr[7], (UINT64) (UINTN) Thunk); break; // // Compiler setting version per value in R7 // case 6: - VmPtr->CompilerVersion = (UINT32) VmPtr->R[7]; + VmPtr->CompilerVersion = (UINT32) VmPtr->Gpr[7]; // // Check compiler version against VM version? // @@ -1141,24 +1927,30 @@ ExecuteBREAK ( /** - Execute the JMP instruction - - @param VmPtr pointer to VM context - - @return Standard EFI_STATUS - @return Instruction syntax: - @return JMP64{cs|cc} Immed64 - @return JMP32{cs|cc} {@}R1 {Immed32|Index32} - @return Encoding: - @retval b0.7 immediate data present - @retval b0.6 1 = 64 bit immediate data 0 = 32 bit immediate data - @retval b1.7 1 = conditional b1.6 1 = CS (condition set) 0 = CC - (condition clear) b1.4 1 = relative address 0 = - absolute address b1.3 1 = operand1 indirect b1.2-0 - operand 1 + Execute the JMP instruction. + + Instruction syntax: + JMP64{cs|cc} Immed64 + JMP32{cs|cc} {@}R1 {Immed32|Index32} + + Encoding: + b0.7 - immediate data present + b0.6 - 1 = 64 bit immediate data + 0 = 32 bit immediate data + b1.7 - 1 = conditional + b1.6 1 = CS (condition set) + 0 = CC (condition clear) + b1.4 1 = relative address + 0 = absolute address + b1.3 1 = operand1 indirect + b1.2-0 operand 1 + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteJMP ( IN VM_CONTEXT *VmPtr @@ -1186,11 +1978,13 @@ ExecuteJMP ( // Decode instruction conditions // If we haven't met the condition, then simply advance the IP and return. // - CompareSet = (UINT8) ((Operand & JMP_M_CS) ? 1 : 0); + CompareSet = (UINT8) (((Operand & JMP_M_CS) != 0) ? 1 : 0); ConditionFlag = (UINT8) VMFLAG_ISSET (VmPtr, VMFLAGS_CC); - if (Operand & CONDITION_M_CONDITIONAL) { + if ((Operand & CONDITION_M_CONDITIONAL) != 0) { if (CompareSet != ConditionFlag) { + EbcDebuggerHookJMPStart (VmPtr); VmPtr->Ip += Size; + EbcDebuggerHookJMPEnd (VmPtr); return EFI_SUCCESS; } } @@ -1198,12 +1992,12 @@ ExecuteJMP ( // Check for 64-bit form and do it right away since it's the most // straight-forward form. // - if (Opcode & OPCODE_M_IMMDATA64) { + if ((Opcode & OPCODE_M_IMMDATA64) != 0) { // // Double check for immediate-data, which is required. If not there, // then signal an exception // - if (!(Opcode & OPCODE_M_IMMDATA)) { + if ((Opcode & OPCODE_M_IMMDATA) == 0) { EbcDebugSignalException ( EXCEPT_EBC_INSTRUCTION_ENCODING, EXCEPTION_FLAG_ERROR, @@ -1215,7 +2009,7 @@ ExecuteJMP ( // 64-bit immediate data is full address. Read the immediate data, // check for alignment, and jump absolute. // - Data64 = VmReadImmed64 (VmPtr, 2); + Data64 = (UINT64) VmReadImmed64 (VmPtr, 2); if (!IS_ALIGNED ((UINTN) Data64, sizeof (UINT16))) { EbcDebugSignalException ( EXCEPT_EBC_ALIGNMENT_CHECK, @@ -1229,11 +2023,13 @@ ExecuteJMP ( // // Take jump -- relative or absolute // - if (Operand & JMP_M_RELATIVE) { + EbcDebuggerHookJMPStart (VmPtr); + if ((Operand & JMP_M_RELATIVE) != 0) { VmPtr->Ip += (UINTN) Data64 + Size; } else { VmPtr->Ip = (VMIP) (UINTN) Data64; } + EbcDebuggerHookJMPEnd (VmPtr); return EFI_SUCCESS; } @@ -1244,7 +2040,7 @@ ExecuteJMP ( // JMP32 @R1 Index32 -- immediate data is an index // JMP32 R1 Immed32 -- immedate data is an offset // - if (Opcode & OPCODE_M_IMMDATA) { + if ((Opcode & OPCODE_M_IMMDATA) != 0) { if (OPERAND1_INDIRECT (Operand)) { Index32 = VmReadIndex32 (VmPtr, 2); } else { @@ -1259,7 +2055,7 @@ ExecuteJMP ( if (OPERAND1_REGNUM (Operand) == 0) { Data64 = 0; } else { - Data64 = OPERAND1_REGDATA (VmPtr, Operand); + Data64 = (UINT64) OPERAND1_REGDATA (VmPtr, Operand); } // // Decode the forms @@ -1279,11 +2075,14 @@ ExecuteJMP ( return EFI_UNSUPPORTED; } - if (Operand & JMP_M_RELATIVE) { + EbcDebuggerHookJMPStart (VmPtr); + if ((Operand & JMP_M_RELATIVE) != 0) { VmPtr->Ip += (UINTN) Addr + Size; } else { VmPtr->Ip = (VMIP) Addr; } + EbcDebuggerHookJMPEnd (VmPtr); + } else { // // Form: JMP32 Rx {Immed32} @@ -1299,11 +2098,14 @@ ExecuteJMP ( return EFI_UNSUPPORTED; } - if (Operand & JMP_M_RELATIVE) { + EbcDebuggerHookJMPStart (VmPtr); + if ((Operand & JMP_M_RELATIVE) != 0) { VmPtr->Ip += (UINTN) Addr + Size; } else { VmPtr->Ip = (VMIP) Addr; } + EbcDebuggerHookJMPEnd (VmPtr); + } return EFI_SUCCESS; @@ -1311,16 +2113,16 @@ ExecuteJMP ( /** - Execute the EBC JMP8 instruction + Execute the EBC JMP8 instruction. + + Instruction syntax: + JMP8{cs|cc} Offset/2 - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. - @return Standard EFI_STATUS - @return Instruction syntax: - @return JMP8{cs|cc} Offset/2 + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteJMP8 ( IN VM_CONTEXT *VmPtr @@ -1335,15 +2137,17 @@ ExecuteJMP8 ( // Decode instruction. // Opcode = GETOPCODE (VmPtr); - CompareSet = (UINT8) ((Opcode & JMP_M_CS) ? 1 : 0); + CompareSet = (UINT8) (((Opcode & JMP_M_CS) != 0) ? 1 : 0); ConditionFlag = (UINT8) VMFLAG_ISSET (VmPtr, VMFLAGS_CC); // // If we haven't met the condition, then simply advance the IP and return // - if (Opcode & CONDITION_M_CONDITIONAL) { + if ((Opcode & CONDITION_M_CONDITIONAL) != 0) { if (CompareSet != ConditionFlag) { + EbcDebuggerHookJMP8Start (VmPtr); VmPtr->Ip += 2; + EbcDebuggerHookJMP8End (VmPtr); return EFI_SUCCESS; } } @@ -1355,27 +2159,34 @@ ExecuteJMP8 ( // // Want to check for offset == -2 and then raise an exception? // + EbcDebuggerHookJMP8Start (VmPtr); VmPtr->Ip += (Offset * 2) + 2; + EbcDebuggerHookJMP8End (VmPtr); return EFI_SUCCESS; } /** - Execute the EBC MOVI + Execute the EBC MOVI. - @param VmPtr pointer to a VM context + Instruction syntax: - @return Standard EFI_STATUS - @return Instruction syntax: - @return MOVI[b|w|d|q][w|d|q] {@}R1 {Index16}, ImmData16|32|64 - @return First variable character specifies the move size - @return Second variable character specifies size of the immediate data - @return Sign-extend the immediate data to the size of the operation, and zero-extend - @return if storing to a register. - @return Operand1 direct with index/immed is invalid. + MOVI[b|w|d|q][w|d|q] {@}R1 {Index16}, ImmData16|32|64 + + First variable character specifies the move size + Second variable character specifies size of the immediate data + + Sign-extend the immediate data to the size of the operation, and zero-extend + if storing to a register. + + Operand1 direct with index/immed is invalid. + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteMOVI ( IN VM_CONTEXT *VmPtr @@ -1398,7 +2209,7 @@ ExecuteMOVI ( // // Get the index (16-bit) if present // - if (Operands & MOVI_M_IMMDATA) { + if ((Operands & MOVI_M_IMMDATA) != 0) { Index16 = VmReadIndex16 (VmPtr, 2); Size = 4; } else { @@ -1435,7 +2246,7 @@ ExecuteMOVI ( // // Operand1 direct. Make sure it didn't have an index. // - if (Operands & MOVI_M_IMMDATA) { + if ((Operands & MOVI_M_IMMDATA) != 0) { EbcDebugSignalException ( EXCEPT_EBC_INSTRUCTION_ENCODING, EXCEPTION_FLAG_FATAL, @@ -1456,12 +2267,12 @@ ExecuteMOVI ( Mask64 = (UINT64)~0; } - VmPtr->R[OPERAND1_REGNUM (Operands)] = ImmData64 & Mask64; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = ImmData64 & Mask64; } else { // // Get the address then write back based on size of the move // - Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16; + Op1 = (UINT64) VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16; if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH8) { VmWriteMem8 (VmPtr, (UINTN) Op1, (UINT8) ImmData64); } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH16) { @@ -1469,7 +2280,7 @@ ExecuteMOVI ( } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH32) { VmWriteMem32 (VmPtr, (UINTN) Op1, (UINT32) ImmData64); } else { - VmWriteMem64 (VmPtr, (UINTN) Op1, ImmData64); + VmWriteMem64 (VmPtr, (UINTN) Op1, (UINT64) ImmData64); } } // @@ -1484,14 +2295,16 @@ ExecuteMOVI ( Execute the EBC MOV immediate natural. This instruction moves an immediate index value into a register or memory location. - @param VmPtr pointer to a VM context + Instruction syntax: + + MOVIn[w|d|q] {@}R1 {Index16}, Index16|32|64 - @return Standard EFI_STATUS - @return Instruction syntax: - @return MOVIn[w|d|q] {@}R1 {Index16}, Index16|32|64 + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteMOVIn ( IN VM_CONTEXT *VmPtr @@ -1515,7 +2328,7 @@ ExecuteMOVIn ( // // Get the operand1 index (16-bit) if present // - if (Operands & MOVI_M_IMMDATA) { + if ((Operands & MOVI_M_IMMDATA) != 0) { Index16 = VmReadIndex16 (VmPtr, 2); Size = 4; } else { @@ -1555,7 +2368,7 @@ ExecuteMOVIn ( // Check for MOVIn R1 Index16, Immed (not indirect, with index), which // is illegal // - if (Operands & MOVI_M_IMMDATA) { + if ((Operands & MOVI_M_IMMDATA) != 0) { EbcDebugSignalException ( EXCEPT_EBC_INSTRUCTION_ENCODING, EXCEPTION_FLAG_FATAL, @@ -1564,13 +2377,13 @@ ExecuteMOVIn ( return EFI_UNSUPPORTED; } - VmPtr->R[OPERAND1_REGNUM (Operands)] = ImmedIndex64; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = ImmedIndex64; } else { // // Get the address // - Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16; - VmWriteMemN (VmPtr, (UINTN) Op1, (INTN) ImmedIndex64); + Op1 = (UINT64) VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16; + VmWriteMemN (VmPtr, (UINTN) Op1, (UINTN)(INTN) ImmedIndex64); } // // Advance the instruction pointer @@ -1584,14 +2397,16 @@ ExecuteMOVIn ( Execute the EBC MOVREL instruction. Dest <- Ip + ImmData - @param VmPtr pointer to a VM context + Instruction syntax: + + MOVREL[w|d|q] {@}R1 {Index16}, ImmData16|32|64 - @return Standard EFI_STATUS - @return Instruction syntax: - @return MOVREL[w|d|q] {@}R1 {Index16}, ImmData16|32|64 + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteMOVREL ( IN VM_CONTEXT *VmPtr @@ -1614,7 +2429,7 @@ ExecuteMOVREL ( // // Get the Operand 1 index (16-bit) if present // - if (Operands & MOVI_M_IMMDATA) { + if ((Operands & MOVI_M_IMMDATA) != 0) { Index16 = VmReadIndex16 (VmPtr, 2); Size = 4; } else { @@ -1652,7 +2467,7 @@ ExecuteMOVREL ( // // Check for illegal combination of operand1 direct with immediate data // - if (Operands & MOVI_M_IMMDATA) { + if ((Operands & MOVI_M_IMMDATA) != 0) { EbcDebugSignalException ( EXCEPT_EBC_INSTRUCTION_ENCODING, EXCEPTION_FLAG_FATAL, @@ -1661,14 +2476,14 @@ ExecuteMOVREL ( return EFI_UNSUPPORTED; } - VmPtr->R[OPERAND1_REGNUM (Operands)] = (VM_REGISTER) Op2; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = (VM_REGISTER) Op2; } else { // // Get the address = [Rx] + Index16 // Write back the result. Always a natural size write, since // we're talking addresses here. // - Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16; + Op1 = (UINT64) VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16; VmWriteMemN (VmPtr, (UINTN) Op1, (UINTN) Op2); } // @@ -1685,16 +2500,19 @@ ExecuteMOVREL ( 32-bit machines, the value gets sign-extended to 64 bits if the destination is a register. - @param VmPtr pointer to a VM context + Instruction syntax: + + MOVsnw {@}R1 {Index16}, {@}R2 {Index16|Immed16} + + 0:7 1=>operand1 index present + 0:6 1=>operand2 index present + + @param VmPtr A pointer to a VM context. - @return Standard EFI_STATUS - @return Instruction syntax: - @return MOVsnw {@}R1 {Index16}, {@}R2 {Index16|Immed16} - @return 0:7 1=>operand1 index present - @return 0:6 1=>operand2 index present + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteMOVsnw ( IN VM_CONTEXT *VmPtr @@ -1719,7 +2537,7 @@ ExecuteMOVsnw ( // Get the indexes if present. // Size = 2; - if (Opcode & OPCODE_M_IMMED_OP1) { + if ((Opcode & OPCODE_M_IMMED_OP1) !=0) { if (OPERAND1_INDIRECT (Operands)) { Op1Index = VmReadIndex16 (VmPtr, 2); } else { @@ -1737,7 +2555,7 @@ ExecuteMOVsnw ( Size += sizeof (UINT16); } - if (Opcode & OPCODE_M_IMMED_OP2) { + if ((Opcode & OPCODE_M_IMMED_OP2) != 0) { if (OPERAND2_INDIRECT (Operands)) { Op2Index = VmReadIndex16 (VmPtr, Size); } else { @@ -1749,17 +2567,17 @@ ExecuteMOVsnw ( // // Get the data from the source. // - Op2 = (INT64) ((INTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Op2Index)); + Op2 = (UINT64)(INT64)(INTN)(VmPtr->Gpr[OPERAND2_REGNUM (Operands)] + Op2Index); if (OPERAND2_INDIRECT (Operands)) { - Op2 = (INT64) (INTN) VmReadMemN (VmPtr, (UINTN) Op2); + Op2 = (UINT64)(INT64)(INTN)VmReadMemN (VmPtr, (UINTN) Op2); } // // Now write back the result. // if (!OPERAND1_INDIRECT (Operands)) { - VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = Op2; } else { - VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2); + VmWriteMemN (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2); } // // Advance the instruction pointer @@ -1775,16 +2593,19 @@ ExecuteMOVsnw ( 32-bit machines, the value gets sign-extended to 64 bits if the destination is a register. - @param VmPtr pointer to a VM context + Instruction syntax: - @return Standard EFI_STATUS - @return Instruction syntax: - @return MOVsnd {@}R1 {Indx32}, {@}R2 {Index32|Immed32} - @return 0:7 1=>operand1 index present - @return 0:6 1=>operand2 index present + MOVsnd {@}R1 {Indx32}, {@}R2 {Index32|Immed32} + + 0:7 1=>operand1 index present + 0:6 1=>operand2 index present + + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteMOVsnd ( IN VM_CONTEXT *VmPtr @@ -1809,7 +2630,7 @@ ExecuteMOVsnd ( // Get the indexes if present. // Size = 2; - if (Opcode & OPCODE_M_IMMED_OP1) { + if ((Opcode & OPCODE_M_IMMED_OP1) != 0) { if (OPERAND1_INDIRECT (Operands)) { Op1Index = VmReadIndex32 (VmPtr, 2); } else { @@ -1827,7 +2648,7 @@ ExecuteMOVsnd ( Size += sizeof (UINT32); } - if (Opcode & OPCODE_M_IMMED_OP2) { + if ((Opcode & OPCODE_M_IMMED_OP2) != 0) { if (OPERAND2_INDIRECT (Operands)) { Op2Index = VmReadIndex32 (VmPtr, Size); } else { @@ -1839,17 +2660,17 @@ ExecuteMOVsnd ( // // Get the data from the source. // - Op2 = (INT64) ((INTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Op2Index)); + Op2 = (UINT64)(INT64)(INTN)(INT64)(VmPtr->Gpr[OPERAND2_REGNUM (Operands)] + Op2Index); if (OPERAND2_INDIRECT (Operands)) { - Op2 = (INT64) (INTN) VmReadMemN (VmPtr, (UINTN) Op2); + Op2 = (UINT64)(INT64)(INTN)(INT64)VmReadMemN (VmPtr, (UINTN) Op2); } // // Now write back the result. // if (!OPERAND1_INDIRECT (Operands)) { - VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = Op2; } else { - VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2); + VmWriteMemN (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2); } // // Advance the instruction pointer @@ -1862,14 +2683,14 @@ ExecuteMOVsnd ( /** Execute the EBC PUSHn instruction - @param VmPtr pointer to a VM context + Instruction syntax: + PUSHn {@}R1 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. - @return Standard EFI_STATUS - @return Instruction syntax: - @return PUSHn {@}R1 {Index16|Immed16} + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecutePUSHn ( IN VM_CONTEXT *VmPtr @@ -1889,7 +2710,7 @@ ExecutePUSHn ( // // Get index if present // - if (Opcode & PUSHPOP_M_IMMDATA) { + if ((Opcode & PUSHPOP_M_IMMDATA) != 0) { if (OPERAND1_INDIRECT (Operands)) { Index16 = VmReadIndex16 (VmPtr, 2); } else { @@ -1905,30 +2726,30 @@ ExecutePUSHn ( // Get the data to push // if (OPERAND1_INDIRECT (Operands)) { - DataN = VmReadMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16)); + DataN = VmReadMemN (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16)); } else { - DataN = (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16); + DataN = (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16); } // // Adjust the stack down. // - VmPtr->R[0] -= sizeof (UINTN); - VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], DataN); + VmPtr->Gpr[0] -= sizeof (UINTN); + VmWriteMemN (VmPtr, (UINTN) VmPtr->Gpr[0], DataN); return EFI_SUCCESS; } /** - Execute the EBC PUSH instruction + Execute the EBC PUSH instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + PUSH[32|64] {@}R1 {Index16|Immed16} - @return Standard EFI_STATUS - @return Instruction syntax: - @return PUSH[32|64] {@}R1 {Index16|Immed16} + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecutePUSH ( IN VM_CONTEXT *VmPtr @@ -1948,7 +2769,7 @@ ExecutePUSH ( // // Get immediate index if present, then advance the IP. // - if (Opcode & PUSHPOP_M_IMMDATA) { + if ((Opcode & PUSHPOP_M_IMMDATA) != 0) { if (OPERAND1_INDIRECT (Operands)) { Index16 = VmReadIndex16 (VmPtr, 2); } else { @@ -1963,31 +2784,31 @@ ExecutePUSH ( // // Get the data to push // - if (Opcode & PUSHPOP_M_64) { + if ((Opcode & PUSHPOP_M_64) != 0) { if (OPERAND1_INDIRECT (Operands)) { - Data64 = VmReadMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16)); + Data64 = VmReadMem64 (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16)); } else { - Data64 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16; + Data64 = (UINT64) VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16; } // // Adjust the stack down, then write back the data // - VmPtr->R[0] -= sizeof (UINT64); - VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], Data64); + VmPtr->Gpr[0] -= sizeof (UINT64); + VmWriteMem64 (VmPtr, (UINTN) VmPtr->Gpr[0], Data64); } else { // // 32-bit data // if (OPERAND1_INDIRECT (Operands)) { - Data32 = VmReadMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16)); + Data32 = VmReadMem32 (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16)); } else { - Data32 = (UINT32) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16; + Data32 = (UINT32) VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16; } // // Adjust the stack down and write the data // - VmPtr->R[0] -= sizeof (UINT32); - VmWriteMem32 (VmPtr, (UINTN) VmPtr->R[0], Data32); + VmPtr->Gpr[0] -= sizeof (UINT32); + VmWriteMem32 (VmPtr, (UINTN) VmPtr->Gpr[0], Data32); } return EFI_SUCCESS; @@ -1995,16 +2816,16 @@ ExecutePUSH ( /** - Execute the EBC POPn instruction + Execute the EBC POPn instruction. + + Instruction syntax: + POPn {@}R1 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. - @return Standard EFI_STATUS - @return Instruction syntax: - @return POPn {@}R1 {Index16|Immed16} + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecutePOPn ( IN VM_CONTEXT *VmPtr @@ -2023,7 +2844,7 @@ ExecutePOPn ( // // Get immediate data if present, and advance the IP // - if (Opcode & PUSHPOP_M_IMMDATA) { + if ((Opcode & PUSHPOP_M_IMMDATA) != 0) { if (OPERAND1_INDIRECT (Operands)) { Index16 = VmReadIndex16 (VmPtr, 2); } else { @@ -2038,15 +2859,15 @@ ExecutePOPn ( // // Read the data off the stack, then adjust the stack pointer // - DataN = VmReadMemN (VmPtr, (UINTN) VmPtr->R[0]); - VmPtr->R[0] += sizeof (UINTN); + DataN = VmReadMemN (VmPtr, (UINTN) VmPtr->Gpr[0]); + VmPtr->Gpr[0] += sizeof (UINTN); // // Do the write-back // if (OPERAND1_INDIRECT (Operands)) { - VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), DataN); + VmWriteMemN (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16), DataN); } else { - VmPtr->R[OPERAND1_REGNUM (Operands)] = (INT64) (UINT64) ((UINTN) DataN + Index16); + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = (INT64) (UINT64) ((UINTN) DataN + Index16); } return EFI_SUCCESS; @@ -2054,16 +2875,16 @@ ExecutePOPn ( /** - Execute the EBC POP instruction + Execute the EBC POP instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + POPn {@}R1 {Index16|Immed16} - @return Standard EFI_STATUS - @return Instruction syntax: - @return POP {@}R1 {Index16|Immed16} + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecutePOP ( IN VM_CONTEXT *VmPtr @@ -2083,7 +2904,7 @@ ExecutePOP ( // // Get immediate data if present, and advance the IP. // - if (Opcode & PUSHPOP_M_IMMDATA) { + if ((Opcode & PUSHPOP_M_IMMDATA) != 0) { if (OPERAND1_INDIRECT (Operands)) { Index16 = VmReadIndex16 (VmPtr, 2); } else { @@ -2098,33 +2919,33 @@ ExecutePOP ( // // Get the data off the stack, then write it to the appropriate location // - if (Opcode & PUSHPOP_M_64) { + if ((Opcode & PUSHPOP_M_64) != 0) { // // Read the data off the stack, then adjust the stack pointer // - Data64 = VmReadMem64 (VmPtr, (UINTN) VmPtr->R[0]); - VmPtr->R[0] += sizeof (UINT64); + Data64 = VmReadMem64 (VmPtr, (UINTN) VmPtr->Gpr[0]); + VmPtr->Gpr[0] += sizeof (UINT64); // // Do the write-back // if (OPERAND1_INDIRECT (Operands)) { - VmWriteMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), Data64); + VmWriteMem64 (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16), Data64); } else { - VmPtr->R[OPERAND1_REGNUM (Operands)] = Data64 + Index16; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = Data64 + Index16; } } else { // // 32-bit pop. Read it off the stack and adjust the stack pointer // - Data32 = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->R[0]); - VmPtr->R[0] += sizeof (UINT32); + Data32 = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->Gpr[0]); + VmPtr->Gpr[0] += sizeof (UINT32); // // Do the write-back // if (OPERAND1_INDIRECT (Operands)) { - VmWriteMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), Data32); + VmWriteMem32 (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND1_REGNUM (Operands)] + Index16), Data32); } else { - VmPtr->R[OPERAND1_REGNUM (Operands)] = (INT64) Data32 + Index16; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = (INT64) Data32 + Index16; } } @@ -2134,19 +2955,20 @@ ExecutePOP ( /** Implements the EBC CALL instruction. + Instruction format: - CALL64 Immed64 - CALL32 {@}R1 {Immed32|Index32} - CALLEX64 Immed64 - CALLEX16 {@}R1 {Immed32} - If Rx == R0, then it's a PC relative call to PC = PC + imm32. + CALL64 Immed64 + CALL32 {@}R1 {Immed32|Index32} + CALLEX64 Immed64 + CALLEX16 {@}R1 {Immed32} - @param VmPtr pointer to a VM context. + If Rx == R0, then it's a PC relative call to PC = PC + imm32. - @return Standard EFI_STATUS + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteCALL ( IN VM_CONTEXT *VmPtr @@ -2164,6 +2986,13 @@ ExecuteCALL ( // Opcode = GETOPCODE (VmPtr); Operands = GETOPERANDS (VmPtr); + + if (Operands & OPERAND_M_NATIVE_CALL) { + EbcDebuggerHookCALLEXStart (VmPtr); + } else { + EbcDebuggerHookCALLStart (VmPtr); + } + // // Assign these as well to avoid compiler warnings // @@ -2174,8 +3003,8 @@ ExecuteCALL ( // // Determine the instruction size, and get immediate data if present // - if (Opcode & OPCODE_M_IMMDATA) { - if (Opcode & OPCODE_M_IMMDATA64) { + if ((Opcode & OPCODE_M_IMMDATA) != 0) { + if ((Opcode & OPCODE_M_IMMDATA64) != 0) { Immed64 = VmReadImmed64 (VmPtr, 2); Size = 10; } else { @@ -2198,16 +3027,16 @@ ExecuteCALL ( // put our return address and frame pointer on the VM stack. // if ((Operands & OPERAND_M_NATIVE_CALL) == 0) { - VmPtr->R[0] -= 8; - VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr); - VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0]; - VmPtr->R[0] -= 8; - VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (UINTN) (VmPtr->Ip + Size)); + VmPtr->Gpr[0] -= 8; + VmWriteMemN (VmPtr, (UINTN) VmPtr->Gpr[0], (UINTN) FramePtr); + VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->Gpr[0]; + VmPtr->Gpr[0] -= 8; + VmWriteMem64 (VmPtr, (UINTN) VmPtr->Gpr[0], (UINT64) (UINTN) (VmPtr->Ip + Size)); } // // If 64-bit data, then absolute jump only // - if (Opcode & OPCODE_M_IMMDATA64) { + if ((Opcode & OPCODE_M_IMMDATA64) != 0) { // // Native or EBC call? // @@ -2217,7 +3046,7 @@ ExecuteCALL ( // // Call external function, get the return value, and advance the IP // - EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->R[0], FramePtr, Size); + EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->Gpr[0], FramePtr, Size); } } else { // @@ -2226,7 +3055,7 @@ ExecuteCALL ( // Compiler should take care of upper bits if 32-bit machine. // if (OPERAND1_REGNUM (Operands) != 0) { - Immed64 = (UINT64) (UINTN) VmPtr->R[OPERAND1_REGNUM (Operands)]; + Immed64 = (UINT64) (UINTN) VmPtr->Gpr[OPERAND1_REGNUM (Operands)]; } // // Get final address @@ -2244,7 +3073,7 @@ ExecuteCALL ( // EBC call. Relative or absolute? If relative, then it's relative to the // start of the next instruction. // - if (Operands & OPERAND_M_RELATIVE_ADDR) { + if ((Operands & OPERAND_M_RELATIVE_ADDR) != 0) { VmPtr->Ip += Immed64 + Size; } else { VmPtr->Ip = (VMIP) (UINTN) Immed64; @@ -2253,50 +3082,59 @@ ExecuteCALL ( // // Native call. Relative or absolute? // - if (Operands & OPERAND_M_RELATIVE_ADDR) { - EbcLLCALLEX (VmPtr, (UINTN) (Immed64 + VmPtr->Ip + Size), (UINTN) VmPtr->R[0], FramePtr, Size); + if ((Operands & OPERAND_M_RELATIVE_ADDR) != 0) { + EbcLLCALLEX (VmPtr, (UINTN) (Immed64 + VmPtr->Ip + Size), (UINTN) VmPtr->Gpr[0], FramePtr, Size); } else { - if (VmPtr->StopFlags & STOPFLAG_BREAK_ON_CALLEX) { + if ((VmPtr->StopFlags & STOPFLAG_BREAK_ON_CALLEX) != 0) { CpuBreakpoint (); } - EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->R[0], FramePtr, Size); + EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->Gpr[0], FramePtr, Size); } } } + if (Operands & OPERAND_M_NATIVE_CALL) { + EbcDebuggerHookCALLEXEnd (VmPtr); + } else { + EbcDebuggerHookCALLEnd (VmPtr); + } + return EFI_SUCCESS; } /** - Execute the EBC RET instruction + Execute the EBC RET instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + RET - @return Standard EFI_STATUS - @return Instruction syntax: - @return RET + @param VmPtr A pointer to a VM context. + + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteRET ( IN VM_CONTEXT *VmPtr ) { + + EbcDebuggerHookRETStart (VmPtr); + // // If we're at the top of the stack, then simply set the done // flag and return // - if (VmPtr->StackRetAddr == (UINT64) VmPtr->R[0]) { + if (VmPtr->StackRetAddr == (UINT64) VmPtr->Gpr[0]) { VmPtr->StopFlags |= STOPFLAG_APP_DONE; } else { // // Pull the return address off the VM app's stack and set the IP // to it // - if (!IS_ALIGNED ((UINTN) VmPtr->R[0], sizeof (UINT16))) { + if (!IS_ALIGNED ((UINTN) VmPtr->Gpr[0], sizeof (UINT16))) { EbcDebugSignalException ( EXCEPT_EBC_ALIGNMENT_CHECK, EXCEPTION_FLAG_FATAL, @@ -2306,27 +3144,31 @@ ExecuteRET ( // // Restore the IP and frame pointer from the stack // - VmPtr->Ip = (VMIP) (UINTN) VmReadMem64 (VmPtr, (UINTN) VmPtr->R[0]); - VmPtr->R[0] += 8; - VmPtr->FramePtr = (VOID *) VmReadMemN (VmPtr, (UINTN) VmPtr->R[0]); - VmPtr->R[0] += 8; + VmPtr->Ip = (VMIP) (UINTN) VmReadMem64 (VmPtr, (UINTN) VmPtr->Gpr[0]); + VmPtr->Gpr[0] += 8; + VmPtr->FramePtr = (VOID *) VmReadMemN (VmPtr, (UINTN) VmPtr->Gpr[0]); + VmPtr->Gpr[0] += 8; } + + EbcDebuggerHookRETEnd (VmPtr); + return EFI_SUCCESS; } /** - Execute the EBC CMP instruction + Execute the EBC CMP instruction. + + Instruction syntax: + CMP[32|64][eq|lte|gte|ulte|ugte] R1, {@}R2 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. - @return Standard EFI_STATUS - @return Instruction syntax: - @return CMP[32|64][eq|lte|gte|ulte|ugte] R1, {@}R2 {Index16|Immed16} + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteCMP ( IN VM_CONTEXT *VmPtr @@ -2348,11 +3190,11 @@ ExecuteCMP ( // // Get the register data we're going to compare to // - Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)]; + Op1 = VmPtr->Gpr[OPERAND1_REGNUM (Operands)]; // // Get immediate data // - if (Opcode & OPCODE_M_IMMDATA) { + if ((Opcode & OPCODE_M_IMMDATA) != 0) { if (OPERAND2_INDIRECT (Operands)) { Index16 = VmReadIndex16 (VmPtr, 2); } else { @@ -2368,22 +3210,22 @@ ExecuteCMP ( // Now get Op2 // if (OPERAND2_INDIRECT (Operands)) { - if (Opcode & OPCODE_M_64BIT) { - Op2 = (INT64) VmReadMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16)); + if ((Opcode & OPCODE_M_64BIT) != 0) { + Op2 = (INT64) VmReadMem64 (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND2_REGNUM (Operands)] + Index16)); } else { // // 32-bit operations. 0-extend the values for all cases. // - Op2 = (INT64) (UINT64) ((UINT32) VmReadMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16))); + Op2 = (INT64) (UINT64) ((UINT32) VmReadMem32 (VmPtr, (UINTN) (VmPtr->Gpr[OPERAND2_REGNUM (Operands)] + Index16))); } } else { - Op2 = VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16; + Op2 = VmPtr->Gpr[OPERAND2_REGNUM (Operands)] + Index16; } // // Now do the compare // Flag = 0; - if (Opcode & OPCODE_M_64BIT) { + if ((Opcode & OPCODE_M_64BIT) != 0) { // // 64-bit compares // @@ -2463,10 +3305,10 @@ ExecuteCMP ( // // Now set the flag accordingly for the comparison // - if (Flag) { + if (Flag != 0) { VMFLAG_SET (VmPtr, VMFLAGS_CC); } else { - VMFLAG_CLEAR (VmPtr, VMFLAGS_CC); + VMFLAG_CLEAR (VmPtr, (UINT64)VMFLAGS_CC); } // // Advance the IP @@ -2479,14 +3321,15 @@ ExecuteCMP ( /** Execute the EBC CMPI instruction - @param VmPtr pointer to a VM context + Instruction syntax: + CMPI[32|64]{w|d}[eq|lte|gte|ulte|ugte] {@}Rx {Index16}, Immed16|Immed32 - @return Standard EFI_STATUS - @return Instruction syntax: - @return CMPI[32|64]{w|d}[eq|lte|gte|ulte|ugte] {@}Rx {Index16}, Immed16|Immed32 + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteCMPI ( IN VM_CONTEXT *VmPtr @@ -2510,7 +3353,7 @@ ExecuteCMPI ( // Get operand1 index if present // Size = 2; - if (Operands & OPERAND_M_CMPI_INDEX) { + if ((Operands & OPERAND_M_CMPI_INDEX) != 0) { Index16 = VmReadIndex16 (VmPtr, 2); Size += 2; } else { @@ -2519,12 +3362,12 @@ ExecuteCMPI ( // // Get operand1 data we're going to compare to // - Op1 = (INT64) VmPtr->R[OPERAND1_REGNUM (Operands)]; + Op1 = (INT64) VmPtr->Gpr[OPERAND1_REGNUM (Operands)]; if (OPERAND1_INDIRECT (Operands)) { // // Indirect operand1. Fetch 32 or 64-bit value based on compare size. // - if (Opcode & OPCODE_M_CMPI64) { + if ((Opcode & OPCODE_M_CMPI64) != 0) { Op1 = (INT64) VmReadMem64 (VmPtr, (UINTN) Op1 + Index16); } else { Op1 = (INT64) VmReadMem32 (VmPtr, (UINTN) Op1 + Index16); @@ -2534,7 +3377,7 @@ ExecuteCMPI ( // Better not have been an index with direct. That is, CMPI R1 Index,... // is illegal. // - if (Operands & OPERAND_M_CMPI_INDEX) { + if ((Operands & OPERAND_M_CMPI_INDEX) != 0) { EbcDebugSignalException ( EXCEPT_EBC_INSTRUCTION_ENCODING, EXCEPTION_FLAG_ERROR, @@ -2547,7 +3390,7 @@ ExecuteCMPI ( // // Get immediate data -- 16- or 32-bit sign extended // - if (Opcode & OPCODE_M_CMPI32_DATA) { + if ((Opcode & OPCODE_M_CMPI32_DATA) != 0) { Op2 = (INT64) VmReadImmed32 (VmPtr, Size); Size += 4; } else { @@ -2561,7 +3404,7 @@ ExecuteCMPI ( // Now do the compare // Flag = 0; - if (Opcode & OPCODE_M_CMPI64) { + if ((Opcode & OPCODE_M_CMPI64) != 0) { // // 64 bit comparison // @@ -2641,10 +3484,10 @@ ExecuteCMPI ( // // Now set the flag accordingly for the comparison // - if (Flag) { + if (Flag != 0) { VMFLAG_SET (VmPtr, VMFLAGS_CC); } else { - VMFLAG_CLEAR (VmPtr, VMFLAGS_CC); + VMFLAG_CLEAR (VmPtr, (UINT64)VMFLAGS_CC); } // // Advance the IP @@ -2655,18 +3498,18 @@ ExecuteCMPI ( /** - Execute the EBC NOT instruction + Execute the EBC NOT instruction.s + + Instruction syntax: + NOT[32|64] {@}R1, {@}R2 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return ~Op2 - @return Instruction syntax: - @return NOT[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteNOT ( IN VM_CONTEXT *VmPtr, @@ -2679,18 +3522,18 @@ ExecuteNOT ( /** - Execute the EBC NEG instruction + Execute the EBC NEG instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + NEG[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op2 * -1 - @return Instruction syntax: - @return NEG[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteNEG ( IN VM_CONTEXT *VmPtr, @@ -2703,18 +3546,18 @@ ExecuteNEG ( /** - Execute the EBC ADD instruction + Execute the EBC ADD instruction. + + Instruction syntax: + ADD[32|64] {@}R1, {@}R2 {Index16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 + Op2 - @return Instruction syntax: - @return ADD[32|64] {@}R1, {@}R2 {Index16} **/ -STATIC UINT64 ExecuteADD ( IN VM_CONTEXT *VmPtr, @@ -2727,18 +3570,18 @@ ExecuteADD ( /** - Execute the EBC SUB instruction + Execute the EBC SUB instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + SUB[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction - @retval Op1 Op2 Standard EFI_STATUS - @return Instruction syntax: - @return SUB[32|64] {@}R1, {@}R2 {Index16|Immed16} + @return Op1 - Op2 **/ -STATIC UINT64 ExecuteSUB ( IN VM_CONTEXT *VmPtr, @@ -2746,7 +3589,7 @@ ExecuteSUB ( IN UINT64 Op2 ) { - if (*VmPtr->Ip & DATAMANIP_M_64) { + if ((*VmPtr->Ip & DATAMANIP_M_64) != 0) { return (UINT64) ((INT64) ((INT64) Op1 - (INT64) Op2)); } else { return (UINT64) ((INT64) ((INT32) Op1 - (INT32) Op2)); @@ -2755,18 +3598,18 @@ ExecuteSUB ( /** - Execute the EBC MUL instruction + Execute the EBC MUL instruction. + + Instruction syntax: + SUB[32|64] {@}R1, {@}R2 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 * Op2 - @return Instruction syntax: - @return MUL[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteMUL ( IN VM_CONTEXT *VmPtr, @@ -2774,7 +3617,7 @@ ExecuteMUL ( IN UINT64 Op2 ) { - if (*VmPtr->Ip & DATAMANIP_M_64) { + if ((*VmPtr->Ip & DATAMANIP_M_64) != 0) { return MultS64x64 ((INT64)Op1, (INT64)Op2); } else { return (UINT64) ((INT64) ((INT32) Op1 * (INT32) Op2)); @@ -2785,16 +3628,16 @@ ExecuteMUL ( /** Execute the EBC MULU instruction - @param VmPtr pointer to a VM context + Instruction syntax: + MULU[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return (unsigned)Op1 * (unsigned)Op2 - @return Instruction syntax: - @return MULU[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteMULU ( IN VM_CONTEXT *VmPtr, @@ -2802,7 +3645,7 @@ ExecuteMULU ( IN UINT64 Op2 ) { - if (*VmPtr->Ip & DATAMANIP_M_64) { + if ((*VmPtr->Ip & DATAMANIP_M_64) != 0) { return MultU64x64 (Op1, Op2); } else { return (UINT64) ((UINT32) Op1 * (UINT32) Op2); @@ -2811,18 +3654,18 @@ ExecuteMULU ( /** - Execute the EBC DIV instruction + Execute the EBC DIV instruction. + + Instruction syntax: + DIV[32|64] {@}R1, {@}R2 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction - @return Op1/Op2 - @return Instruction syntax: - @return DIV[32|64] {@}R1, {@}R2 {Index16|Immed16} + @return Op1 / Op2 **/ -STATIC UINT64 ExecuteDIV ( IN VM_CONTEXT *VmPtr, @@ -2844,7 +3687,7 @@ ExecuteDIV ( return 0; } else { - if (*VmPtr->Ip & DATAMANIP_M_64) { + if ((*VmPtr->Ip & DATAMANIP_M_64) != 0) { return (UINT64) (DivS64x64Remainder (Op1, Op2, &Remainder)); } else { return (UINT64) ((INT64) ((INT32) Op1 / (INT32) Op2)); @@ -2856,16 +3699,16 @@ ExecuteDIV ( /** Execute the EBC DIVU instruction - @param VmPtr pointer to a VM context + Instruction syntax: + DIVU[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return (unsigned)Op1 / (unsigned)Op2 - @return Instruction syntax: - @return DIVU[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteDIVU ( IN VM_CONTEXT *VmPtr, @@ -2889,8 +3732,8 @@ ExecuteDIVU ( // // Get the destination register // - if (*VmPtr->Ip & DATAMANIP_M_64) { - return (UINT64) (DivU64x64Remainder ((INT64)Op1, (INT64)Op2, &Remainder)); + if ((*VmPtr->Ip & DATAMANIP_M_64) != 0) { + return (UINT64) (DivU64x64Remainder (Op1, Op2, &Remainder)); } else { return (UINT64) ((UINT32) Op1 / (UINT32) Op2); } @@ -2899,18 +3742,18 @@ ExecuteDIVU ( /** - Execute the EBC MOD instruction + Execute the EBC MOD instruction. + + Instruction syntax: + MOD[32|64] {@}R1, {@}R2 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 MODULUS Op2 - @return Instruction syntax: - @return MOD[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteMOD ( IN VM_CONTEXT *VmPtr, @@ -2938,18 +3781,18 @@ ExecuteMOD ( /** - Execute the EBC MODU instruction + Execute the EBC MODU instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + MODU[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 UNSIGNED_MODULUS Op2 - @return Instruction syntax: - @return MODU[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteMODU ( IN VM_CONTEXT *VmPtr, @@ -2977,18 +3820,18 @@ ExecuteMODU ( /** - Execute the EBC AND instruction + Execute the EBC AND instruction. + + Instruction syntax: + AND[32|64] {@}R1, {@}R2 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 AND Op2 - @return Instruction syntax: - @return AND[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteAND ( IN VM_CONTEXT *VmPtr, @@ -3001,18 +3844,18 @@ ExecuteAND ( /** - Execute the EBC OR instruction + Execute the EBC OR instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + OR[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 OR Op2 - @return Instruction syntax: - @return OR[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteOR ( IN VM_CONTEXT *VmPtr, @@ -3025,18 +3868,18 @@ ExecuteOR ( /** - Execute the EBC XOR instruction + Execute the EBC XOR instruction. + + Instruction syntax: + XOR[32|64] {@}R1, {@}R2 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 XOR Op2 - @return Instruction syntax: - @return XOR[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteXOR ( IN VM_CONTEXT *VmPtr, @@ -3049,18 +3892,18 @@ ExecuteXOR ( /** - Execute the EBC SHL shift left instruction + Execute the EBC SHL shift left instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + SHL[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 << Op2 - @return Instruction syntax: - @return SHL[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteSHL ( IN VM_CONTEXT *VmPtr, @@ -3068,7 +3911,7 @@ ExecuteSHL ( IN UINT64 Op2 ) { - if (*VmPtr->Ip & DATAMANIP_M_64) { + if ((*VmPtr->Ip & DATAMANIP_M_64) != 0) { return LShiftU64 (Op1, (UINTN)Op2); } else { return (UINT64) ((UINT32) ((UINT32) Op1 << (UINT32) Op2)); @@ -3077,18 +3920,18 @@ ExecuteSHL ( /** - Execute the EBC SHR instruction + Execute the EBC SHR instruction. + + Instruction syntax: + SHR[32|64] {@}R1, {@}R2 {Index16|Immed16} - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 >> Op2 (unsigned operands) - @return Instruction syntax: - @return SHR[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteSHR ( IN VM_CONTEXT *VmPtr, @@ -3096,7 +3939,7 @@ ExecuteSHR ( IN UINT64 Op2 ) { - if (*VmPtr->Ip & DATAMANIP_M_64) { + if ((*VmPtr->Ip & DATAMANIP_M_64) != 0) { return RShiftU64 (Op1, (UINTN)Op2); } else { return (UINT64) ((UINT32) Op1 >> (UINT32) Op2); @@ -3105,18 +3948,18 @@ ExecuteSHR ( /** - Execute the EBC ASHR instruction + Execute the EBC ASHR instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + ASHR[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return Op1 >> Op2 (signed) - @return Instruction syntax: - @return ASHR[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteASHR ( IN VM_CONTEXT *VmPtr, @@ -3124,7 +3967,7 @@ ExecuteASHR ( IN UINT64 Op2 ) { - if (*VmPtr->Ip & DATAMANIP_M_64) { + if ((*VmPtr->Ip & DATAMANIP_M_64) != 0) { return ARShiftU64 (Op1, (UINTN)Op2); } else { return (UINT64) ((INT64) ((INT32) Op1 >> (UINT32) Op2)); @@ -3135,16 +3978,16 @@ ExecuteASHR ( /** Execute the EBC EXTNDB instruction to sign-extend a byte value. - @param VmPtr pointer to a VM context + Instruction syntax: + EXTNDB[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return (INT64)(INT8)Op2 - @return Instruction syntax: - @return EXTNDB[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteEXTNDB ( IN VM_CONTEXT *VmPtr, @@ -3168,16 +4011,16 @@ ExecuteEXTNDB ( /** Execute the EBC EXTNDW instruction to sign-extend a 16-bit value. - @param VmPtr pointer to a VM context + Instruction syntax: + EXTNDW[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return (INT64)(INT16)Op2 - @return Instruction syntax: - @return EXTNDW[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteEXTNDW ( IN VM_CONTEXT *VmPtr, @@ -3208,16 +4051,16 @@ ExecuteEXTNDW ( /** Execute the EBC EXTNDD instruction to sign-extend a 32-bit value. - @param VmPtr pointer to a VM context + Instruction syntax: + EXTNDD[32|64] {@}R1, {@}R2 {Index16|Immed16} + + @param VmPtr A pointer to a VM context. @param Op1 Operand 1 from the instruction @param Op2 Operand 2 from the instruction @return (INT64)(INT32)Op2 - @return Instruction syntax: - @return EXTNDD[32|64] {@}R1, {@}R2 {Index16|Immed16} **/ -STATIC UINT64 ExecuteEXTNDD ( IN VM_CONTEXT *VmPtr, @@ -3237,7 +4080,24 @@ ExecuteEXTNDD ( return (UINT64) Data64; } -STATIC + +/** + Execute all the EBC signed data manipulation instructions. + Since the EBC data manipulation instructions all have the same basic form, + they can share the code that does the fetch of operands and the write-back + of the result. This function performs the fetch of the operands (even if + both are not needed to be fetched, like NOT instruction), dispatches to the + appropriate subfunction, then writes back the returned result. + + Format: + INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16} + + @param VmPtr A pointer to VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteSignedDataManip ( IN VM_CONTEXT *VmPtr @@ -3250,7 +4110,24 @@ ExecuteSignedDataManip ( return ExecuteDataManip (VmPtr, TRUE); } -STATIC + +/** + Execute all the EBC unsigned data manipulation instructions. + Since the EBC data manipulation instructions all have the same basic form, + they can share the code that does the fetch of operands and the write-back + of the result. This function performs the fetch of the operands (even if + both are not needed to be fetched, like NOT instruction), dispatches to the + appropriate subfunction, then writes back the returned result. + + Format: + INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16} + + @param VmPtr A pointer to VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. + +**/ EFI_STATUS ExecuteUnsignedDataManip ( IN VM_CONTEXT *VmPtr @@ -3272,14 +4149,16 @@ ExecuteUnsignedDataManip ( both are not needed to be fetched, like NOT instruction), dispatches to the appropriate subfunction, then writes back the returned result. - @param VmPtr pointer to VM context + Format: + INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16} - @return Standard EBC status - @return Format: - @return INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16} + @param VmPtr A pointer to VM context. + @param IsSignedOp Indicates whether the operand is signed or not. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteDataManip ( IN VM_CONTEXT *VmPtr, @@ -3292,6 +4171,7 @@ ExecuteDataManip ( UINT8 Size; UINT64 Op1; UINT64 Op2; + INTN DataManipDispatchTableIndex; // // Get opcode and operands @@ -3302,7 +4182,7 @@ ExecuteDataManip ( // // Determine if we have immediate data by the opcode // - if (Opcode & DATAMANIP_M_IMMDATA) { + if ((Opcode & DATAMANIP_M_IMMDATA) != 0) { // // Index16 if Ry is indirect, or Immed16 if Ry direct. // @@ -3320,12 +4200,12 @@ ExecuteDataManip ( // // Now get operand2 (source). It's of format {@}R2 {Index16|Immed16} // - Op2 = (UINT64) VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16; + Op2 = (UINT64) VmPtr->Gpr[OPERAND2_REGNUM (Operands)] + Index16; if (OPERAND2_INDIRECT (Operands)) { // // Indirect form: @R2 Index16. Fetch as 32- or 64-bit data // - if (Opcode & DATAMANIP_M_64) { + if ((Opcode & DATAMANIP_M_64) != 0) { Op2 = VmReadMem64 (VmPtr, (UINTN) Op2); } else { // @@ -3350,9 +4230,9 @@ ExecuteDataManip ( // Get operand1 (destination and sometimes also an actual operand) // of form {@}R1 // - Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)]; + Op1 = (UINT64) VmPtr->Gpr[OPERAND1_REGNUM (Operands)]; if (OPERAND1_INDIRECT (Operands)) { - if (Opcode & DATAMANIP_M_64) { + if ((Opcode & DATAMANIP_M_64) != 0) { Op1 = VmReadMem64 (VmPtr, (UINTN) Op1); } else { if (IsSignedOp) { @@ -3373,9 +4253,9 @@ ExecuteDataManip ( // // Dispatch to the computation function // - if (((Opcode & OPCODE_M_OPCODE) - OPCODE_NOT) >= - (sizeof (mDataManipDispatchTable) / sizeof (mDataManipDispatchTable[0])) - ) { + DataManipDispatchTableIndex = (Opcode & OPCODE_M_OPCODE) - OPCODE_NOT; + if ((DataManipDispatchTableIndex < 0) || + (DataManipDispatchTableIndex >= ARRAY_SIZE (mDataManipDispatchTable))) { EbcDebugSignalException ( EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_ERROR, @@ -3387,14 +4267,14 @@ ExecuteDataManip ( VmPtr->Ip += Size; return EFI_UNSUPPORTED; } else { - Op2 = mDataManipDispatchTable[(Opcode & OPCODE_M_OPCODE) - OPCODE_NOT](VmPtr, Op1, Op2); + Op2 = mDataManipDispatchTable[DataManipDispatchTableIndex](VmPtr, Op1, Op2); } // // Write back the result. // if (OPERAND1_INDIRECT (Operands)) { - Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)]; - if (Opcode & DATAMANIP_M_64) { + Op1 = (UINT64) VmPtr->Gpr[OPERAND1_REGNUM (Operands)]; + if ((Opcode & DATAMANIP_M_64) != 0) { VmWriteMem64 (VmPtr, (UINTN) Op1, Op2); } else { VmWriteMem32 (VmPtr, (UINTN) Op1, (UINT32) Op2); @@ -3404,9 +4284,9 @@ ExecuteDataManip ( // Storage back to a register. Write back, clearing upper bits (as per // the specification) if 32-bit operation. // - VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = Op2; if ((Opcode & DATAMANIP_M_64) == 0) { - VmPtr->R[OPERAND1_REGNUM (Operands)] &= 0xFFFFFFFF; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] &= 0xFFFFFFFF; } } // @@ -3418,16 +4298,17 @@ ExecuteDataManip ( /** - Execute the EBC LOADSP instruction + Execute the EBC LOADSP instruction. + + Instruction syntax: + LOADSP SP1, R2 - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. - @return Standard EFI_STATUS - @return Instruction syntax: - @return LOADSP SP1, R2 + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteLOADSP ( IN VM_CONTEXT *VmPtr @@ -3452,7 +4333,7 @@ ExecuteLOADSP ( // Spec states that this instruction will not modify reserved bits in // the flags register. // - VmPtr->Flags = (VmPtr->Flags &~VMFLAGS_ALL_VALID) | (VmPtr->R[OPERAND2_REGNUM (Operands)] & VMFLAGS_ALL_VALID); + VmPtr->Flags = (VmPtr->Flags &~VMFLAGS_ALL_VALID) | (VmPtr->Gpr[OPERAND2_REGNUM (Operands)] & VMFLAGS_ALL_VALID); break; default: @@ -3471,16 +4352,17 @@ ExecuteLOADSP ( /** - Execute the EBC STORESP instruction + Execute the EBC STORESP instruction. - @param VmPtr pointer to a VM context + Instruction syntax: + STORESP Rx, FLAGS|IP - @return Standard EFI_STATUS - @return Instruction syntax: - @return STORESP Rx, FLAGS|IP + @param VmPtr A pointer to a VM context. + + @retval EFI_UNSUPPORTED The opcodes/operands is not supported. + @retval EFI_SUCCESS The instruction is executed successfully. **/ -STATIC EFI_STATUS ExecuteSTORESP ( IN VM_CONTEXT *VmPtr @@ -3504,14 +4386,14 @@ ExecuteSTORESP ( // // Retrieve the value in the flags register, then clear reserved bits // - VmPtr->R[OPERAND1_REGNUM (Operands)] = (UINT64) (VmPtr->Flags & VMFLAGS_ALL_VALID); + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = (UINT64) (VmPtr->Flags & VMFLAGS_ALL_VALID); break; // // Get IP -- address of following instruction // case 1: - VmPtr->R[OPERAND1_REGNUM (Operands)] = (UINT64) (UINTN) VmPtr->Ip + 2; + VmPtr->Gpr[OPERAND1_REGNUM (Operands)] = (UINT64) (UINTN) VmPtr->Ip + 2; break; default: @@ -3532,24 +4414,26 @@ ExecuteSTORESP ( /** Decode a 16-bit index to determine the offset. Given an index value: - b15 - sign bit - b14:12 - number of bits in this index assigned to natural units (=a) - ba:11 - constant units = C - b0:a - natural units = N + + b15 - sign bit + b14:12 - number of bits in this index assigned to natural units (=a) + ba:11 - constant units = ConstUnits + b0:a - natural units = NaturalUnits + Given this info, the offset can be computed by: - offset = sign_bit * (C + N * sizeof(UINTN)) + offset = sign_bit * (ConstUnits + NaturalUnits * sizeof(UINTN)) + Max offset is achieved with index = 0x7FFF giving an offset of 0x27B (32-bit machine) or 0x477 (64-bit machine). Min offset is achieved with index = - @param VmPtr pointer to VM context - @param CodeOffset offset from IP of the location of the 16-bit index to - decode + @param VmPtr A pointer to VM context. + @param CodeOffset Offset from IP of the location of the 16-bit index + to decode. @return The decoded offset. **/ -STATIC INT16 VmReadIndex16 ( IN VM_CONTEXT *VmPtr, @@ -3558,8 +4442,8 @@ VmReadIndex16 ( { UINT16 Index; INT16 Offset; - INT16 C; - INT16 N; + INT16 ConstUnits; + INT16 NaturalUnits; INT16 NBits; INT16 Mask; @@ -3569,7 +4453,7 @@ VmReadIndex16 ( Index = VmReadCode16 (VmPtr, CodeOffset); // - // Get the mask for N. First get the number of bits from the index. + // Get the mask for NaturalUnits. First get the number of bits from the index. // NBits = (INT16) ((Index & 0x7000) >> 12); @@ -3584,21 +4468,21 @@ VmReadIndex16 ( Mask = (INT16) ((INT16)~0 << NBits); // - // Now using the mask, extract N from the lower bits of the index. + // Now using the mask, extract NaturalUnits from the lower bits of the index. // - N = (INT16) (Index &~Mask); + NaturalUnits = (INT16) (Index &~Mask); // - // Now compute C + // Now compute ConstUnits // - C = (INT16) (((Index &~0xF000) & Mask) >> NBits); + ConstUnits = (INT16) (((Index &~0xF000) & Mask) >> NBits); - Offset = (INT16) (N * sizeof (UINTN) + C); + Offset = (INT16) (NaturalUnits * sizeof (UINTN) + ConstUnits); // // Now set the sign // - if (Index & 0x8000) { + if ((Index & 0x8000) != 0) { // // Do it the hard way to work around a bogus compiler warning // @@ -3614,14 +4498,13 @@ VmReadIndex16 ( /** Decode a 32-bit index to determine the offset. - @param VmPtr pointer to VM context - @param CodeOffset offset from IP of the location of the 32-bit index to - decode + @param VmPtr A pointer to VM context. + @param CodeOffset Offset from IP of the location of the 32-bit index + to decode. - @return Converted index per EBC VM specification + @return Converted index per EBC VM specification. **/ -STATIC INT32 VmReadIndex32 ( IN VM_CONTEXT *VmPtr, @@ -3630,15 +4513,15 @@ VmReadIndex32 ( { UINT32 Index; INT32 Offset; - INT32 C; - INT32 N; + INT32 ConstUnits; + INT32 NaturalUnits; INT32 NBits; INT32 Mask; Index = VmReadImmed32 (VmPtr, CodeOffset); // - // Get the mask for N. First get the number of bits from the index. + // Get the mask for NaturalUnits. First get the number of bits from the index. // NBits = (Index & 0x70000000) >> 28; @@ -3653,21 +4536,21 @@ VmReadIndex32 ( Mask = (INT32)~0 << NBits; // - // Now using the mask, extract N from the lower bits of the index. + // Now using the mask, extract NaturalUnits from the lower bits of the index. // - N = Index &~Mask; + NaturalUnits = Index &~Mask; // - // Now compute C + // Now compute ConstUnits // - C = ((Index &~0xF0000000) & Mask) >> NBits; + ConstUnits = ((Index &~0xF0000000) & Mask) >> NBits; - Offset = N * sizeof (UINTN) + C; + Offset = NaturalUnits * sizeof (UINTN) + ConstUnits; // // Now set the sign // - if (Index & 0x80000000) { + if ((Index & 0x80000000) != 0) { Offset = Offset * -1; } @@ -3678,14 +4561,13 @@ VmReadIndex32 ( /** Decode a 64-bit index to determine the offset. - @param VmPtr pointer to VM context - @param CodeOffset offset from IP of the location of the 64-bit index to - decode + @param VmPtr A pointer to VM context.s + @param CodeOffset Offset from IP of the location of the 64-bit index + to decode. @return Converted index per EBC VM specification **/ -STATIC INT64 VmReadIndex64 ( IN VM_CONTEXT *VmPtr, @@ -3694,15 +4576,15 @@ VmReadIndex64 ( { UINT64 Index; INT64 Offset; - INT64 C; - INT64 N; + INT64 ConstUnits; + INT64 NaturalUnits; INT64 NBits; INT64 Mask; Index = VmReadCode64 (VmPtr, CodeOffset); // - // Get the mask for N. First get the number of bits from the index. + // Get the mask for NaturalUnits. First get the number of bits from the index. // NBits = RShiftU64 ((Index & 0x7000000000000000ULL), 60); @@ -3717,21 +4599,21 @@ VmReadIndex64 ( Mask = (LShiftU64 ((UINT64)~0, (UINTN)NBits)); // - // Now using the mask, extract N from the lower bits of the index. + // Now using the mask, extract NaturalUnits from the lower bits of the index. // - N = Index &~Mask; + NaturalUnits = Index &~Mask; // - // Now compute C + // Now compute ConstUnits // - C = ARShiftU64 (((Index &~0xF000000000000000ULL) & Mask), (UINTN)NBits); + ConstUnits = ARShiftU64 (((Index &~0xF000000000000000ULL) & Mask), (UINTN)NBits); - Offset = MultU64x64 (N, sizeof (UINTN)) + C; + Offset = MultU64x64 ((UINT64) NaturalUnits, sizeof (UINTN)) + ConstUnits; // // Now set the sign // - if (Index & 0x8000000000000000ULL) { + if ((Index & 0x8000000000000000ULL) != 0) { Offset = MultS64x64 (Offset, -1); } @@ -3740,24 +4622,28 @@ VmReadIndex64 ( /** - The following VmWriteMem? routines are called by the EBC data + Writes 8-bit data to memory address. + + This routine is called by the EBC data movement instructions that write to memory. Since these writes may be to the stack, which looks like (high address on top) this, + [EBC entry point arguments] [VM stack] [EBC stack] + we need to detect all attempts to write to the EBC entry point argument stack area and adjust the address (which will initially point into the VM stack) to point into the EBC entry point arguments. - @param VmPtr pointer to a VM context - @param Addr adddress to write to - @param Data value to write to Addr + @param VmPtr A pointer to a VM context. + @param Addr Address to write to. + @param Data Value to write to Addr. - @return Standard EFI_STATUS + @retval EFI_SUCCESS The instruction is executed successfully. + @retval Other Some error occurs when writing data to the address. **/ -STATIC EFI_STATUS VmWriteMem8 ( IN VM_CONTEXT *VmPtr, @@ -3773,7 +4659,29 @@ VmWriteMem8 ( return EFI_SUCCESS; } -STATIC +/** + Writes 16-bit data to memory address. + + This routine is called by the EBC data + movement instructions that write to memory. Since these writes + may be to the stack, which looks like (high address on top) this, + + [EBC entry point arguments] + [VM stack] + [EBC stack] + + we need to detect all attempts to write to the EBC entry point argument + stack area and adjust the address (which will initially point into the + VM stack) to point into the EBC entry point arguments. + + @param VmPtr A pointer to a VM context. + @param Addr Address to write to. + @param Data Value to write to Addr. + + @retval EFI_SUCCESS The instruction is executed successfully. + @retval Other Some error occurs when writing data to the address. + +**/ EFI_STATUS VmWriteMem16 ( IN VM_CONTEXT *VmPtr, @@ -3813,7 +4721,30 @@ VmWriteMem16 ( return EFI_SUCCESS; } -STATIC + +/** + Writes 32-bit data to memory address. + + This routine is called by the EBC data + movement instructions that write to memory. Since these writes + may be to the stack, which looks like (high address on top) this, + + [EBC entry point arguments] + [VM stack] + [EBC stack] + + we need to detect all attempts to write to the EBC entry point argument + stack area and adjust the address (which will initially point into the + VM stack) to point into the EBC entry point arguments. + + @param VmPtr A pointer to a VM context. + @param Addr Address to write to. + @param Data Value to write to Addr. + + @retval EFI_SUCCESS The instruction is executed successfully. + @retval Other Some error occurs when writing data to the address. + +**/ EFI_STATUS VmWriteMem32 ( IN VM_CONTEXT *VmPtr, @@ -3853,6 +4784,30 @@ VmWriteMem32 ( return EFI_SUCCESS; } + +/** + Writes 64-bit data to memory address. + + This routine is called by the EBC data + movement instructions that write to memory. Since these writes + may be to the stack, which looks like (high address on top) this, + + [EBC entry point arguments] + [VM stack] + [EBC stack] + + we need to detect all attempts to write to the EBC entry point argument + stack area and adjust the address (which will initially point into the + VM stack) to point into the EBC entry point arguments. + + @param VmPtr A pointer to a VM context. + @param Addr Address to write to. + @param Data Value to write to Addr. + + @retval EFI_SUCCESS The instruction is executed successfully. + @retval Other Some error occurs when writing data to the address. + +**/ EFI_STATUS VmWriteMem64 ( IN VM_CONTEXT *VmPtr, @@ -3861,7 +4816,6 @@ VmWriteMem64 ( ) { EFI_STATUS Status; - UINT32 Data32; // // Convert the address if it's in the stack gap @@ -3883,8 +4837,7 @@ VmWriteMem64 ( } MemoryFence (); - Data32 = (UINT32) (((UINT32 *) &Data)[1]); - if ((Status = VmWriteMem32 (VmPtr, Addr + sizeof (UINT32), Data32)) != EFI_SUCCESS) { + if ((Status = VmWriteMem32 (VmPtr, Addr + sizeof (UINT32), (UINT32) RShiftU64(Data, 32))) != EFI_SUCCESS) { return Status; } @@ -3894,6 +4847,30 @@ VmWriteMem64 ( return EFI_SUCCESS; } + +/** + Writes UINTN data to memory address. + + This routine is called by the EBC data + movement instructions that write to memory. Since these writes + may be to the stack, which looks like (high address on top) this, + + [EBC entry point arguments] + [VM stack] + [EBC stack] + + we need to detect all attempts to write to the EBC entry point argument + stack area and adjust the address (which will initially point into the + VM stack) to point into the EBC entry point arguments. + + @param VmPtr A pointer to a VM context. + @param Addr Address to write to. + @param Data Value to write to Addr. + + @retval EFI_SUCCESS The instruction is executed successfully. + @retval Other Some error occurs when writing data to the address. + +**/ EFI_STATUS VmWriteMemN ( IN VM_CONTEXT *VmPtr, @@ -3921,7 +4898,7 @@ VmWriteMemN ( MemoryFence (); Status = VmWriteMem32 (VmPtr, Addr + Index * sizeof (UINT32), (UINT32) Data); MemoryFence (); - Data = (UINTN)RShiftU64 ((UINT64)Data, 32); + Data = (UINTN) RShiftU64 ((UINT64)Data, 32); } } @@ -3930,18 +4907,19 @@ VmWriteMemN ( /** - The following VmReadImmed routines are called by the EBC execute + Reads 8-bit immediate value at the offset. + + This routine is called by the EBC execute functions to read EBC immediate values from the code stream. Since we can't assume alignment, each tries to read in the biggest chunks size available, but will revert to smaller reads if necessary. - @param VmPtr pointer to a VM context + @param VmPtr A pointer to a VM context. @param Offset offset from IP of the code bytes to read. @return Signed data of the requested size from the specified address. **/ -STATIC INT8 VmReadImmed8 ( IN VM_CONTEXT *VmPtr, @@ -3954,7 +4932,20 @@ VmReadImmed8 ( return * (INT8 *) (VmPtr->Ip + Offset); } -STATIC +/** + Reads 16-bit immediate value at the offset. + + This routine is called by the EBC execute + functions to read EBC immediate values from the code stream. + Since we can't assume alignment, each tries to read in the biggest + chunks size available, but will revert to smaller reads if necessary. + + @param VmPtr A pointer to a VM context. + @param Offset offset from IP of the code bytes to read. + + @return Signed data of the requested size from the specified address. + +**/ INT16 VmReadImmed16 ( IN VM_CONTEXT *VmPtr, @@ -3982,7 +4973,21 @@ VmReadImmed16 ( return (INT16) (*(UINT8 *) (VmPtr->Ip + Offset) + (*(UINT8 *) (VmPtr->Ip + Offset + 1) << 8)); } -STATIC + +/** + Reads 32-bit immediate value at the offset. + + This routine is called by the EBC execute + functions to read EBC immediate values from the code stream. + Since we can't assume alignment, each tries to read in the biggest + chunks size available, but will revert to smaller reads if necessary. + + @param VmPtr A pointer to a VM context. + @param Offset offset from IP of the code bytes to read. + + @return Signed data of the requested size from the specified address. + +**/ INT32 VmReadImmed32 ( IN VM_CONTEXT *VmPtr, @@ -4000,12 +5005,26 @@ VmReadImmed32 ( // // Return unaligned data // - Data = (UINT32) VmReadCode16 (VmPtr, Offset); - Data |= (UINT32) (VmReadCode16 (VmPtr, Offset + 2) << 16); + Data = (UINT32) VmReadCode16 (VmPtr, Offset); + Data |= (UINT32)(VmReadCode16 (VmPtr, Offset + 2) << 16); return Data; } -STATIC + +/** + Reads 64-bit immediate value at the offset. + + This routine is called by the EBC execute + functions to read EBC immediate values from the code stream. + Since we can't assume alignment, each tries to read in the biggest + chunks size available, but will revert to smaller reads if necessary. + + @param VmPtr A pointer to a VM context. + @param Offset offset from IP of the code bytes to read. + + @return Signed data of the requested size from the specified address. + +**/ INT64 VmReadImmed64 ( IN VM_CONTEXT *VmPtr, @@ -4028,7 +5047,7 @@ VmReadImmed64 ( Ptr = (UINT8 *) &Data64; Data32 = VmReadCode32 (VmPtr, Offset); *(UINT32 *) Ptr = Data32; - Ptr += sizeof (Data32); + Ptr += sizeof (Data32); Data32 = VmReadCode32 (VmPtr, Offset + sizeof (UINT32)); *(UINT32 *) Ptr = Data32; return Data64; @@ -4036,16 +5055,17 @@ VmReadImmed64 ( /** - The following VmReadCode() routines provide the ability to read raw - unsigned data from the code stream. + Reads 16-bit unsigned data from the code stream. - @param VmPtr pointer to VM context - @param Offset offset from current IP to the raw data to read. + This routine provides the ability to read raw unsigned data from the code + stream. + + @param VmPtr A pointer to VM context + @param Offset Offset from current IP to the raw data to read. @return The raw unsigned 16-bit value from the code stream. **/ -STATIC UINT16 VmReadCode16 ( IN VM_CONTEXT *VmPtr, @@ -4073,7 +5093,19 @@ VmReadCode16 ( return (UINT16) (*(UINT8 *) (VmPtr->Ip + Offset) + (*(UINT8 *) (VmPtr->Ip + Offset + 1) << 8)); } -STATIC + +/** + Reads 32-bit unsigned data from the code stream. + + This routine provides the ability to read raw unsigned data from the code + stream. + + @param VmPtr A pointer to VM context + @param Offset Offset from current IP to the raw data to read. + + @return The raw unsigned 32-bit value from the code stream. + +**/ UINT32 VmReadCode32 ( IN VM_CONTEXT *VmPtr, @@ -4095,7 +5127,19 @@ VmReadCode32 ( return Data; } -STATIC + +/** + Reads 64-bit unsigned data from the code stream. + + This routine provides the ability to read raw unsigned data from the code + stream. + + @param VmPtr A pointer to VM context + @param Offset Offset from current IP to the raw data to read. + + @return The raw unsigned 64-bit value from the code stream. + +**/ UINT64 VmReadCode64 ( IN VM_CONTEXT *VmPtr, @@ -4118,13 +5162,22 @@ VmReadCode64 ( Ptr = (UINT8 *) &Data64; Data32 = VmReadCode32 (VmPtr, Offset); *(UINT32 *) Ptr = Data32; - Ptr += sizeof (Data32); + Ptr += sizeof (Data32); Data32 = VmReadCode32 (VmPtr, Offset + sizeof (UINT32)); *(UINT32 *) Ptr = Data32; return Data64; } -STATIC + +/** + Reads 8-bit data form the memory address. + + @param VmPtr A pointer to VM context. + @param Addr The memory address. + + @return The 8-bit value from the memory address. + +**/ UINT8 VmReadMem8 ( IN VM_CONTEXT *VmPtr, @@ -4141,7 +5194,15 @@ VmReadMem8 ( return * (UINT8 *) Addr; } -STATIC +/** + Reads 16-bit data form the memory address. + + @param VmPtr A pointer to VM context. + @param Addr The memory address. + + @return The 16-bit value from the memory address. + +**/ UINT16 VmReadMem16 ( IN VM_CONTEXT *VmPtr, @@ -4164,7 +5225,15 @@ VmReadMem16 ( return (UINT16) (*(UINT8 *) Addr + (*(UINT8 *) (Addr + 1) << 8)); } -STATIC +/** + Reads 32-bit data form the memory address. + + @param VmPtr A pointer to VM context. + @param Addr The memory address. + + @return The 32-bit value from the memory address. + +**/ UINT32 VmReadMem32 ( IN VM_CONTEXT *VmPtr, @@ -4191,7 +5260,15 @@ VmReadMem32 ( return Data; } -STATIC +/** + Reads 64-bit data form the memory address. + + @param VmPtr A pointer to VM context. + @param Addr The memory address. + + @return The 64-bit value from the memory address. + +**/ UINT64 VmReadMem64 ( IN VM_CONTEXT *VmPtr, @@ -4215,9 +5292,9 @@ VmReadMem64 ( // // Return unaligned data. Assume little endian. // - Data = (UINT64) VmReadMem32 (VmPtr, Addr); - Data32 = VmReadMem32 (VmPtr, Addr + sizeof (UINT32)); - *(UINT32 *) ((UINT32 *) &Data + 1) = Data32; + Data32 = VmReadMem32 (VmPtr, Addr); + Data = (UINT64) VmReadMem32 (VmPtr, Addr + sizeof (UINT32)); + Data = LShiftU64 (Data, 32) | Data32; return Data; } @@ -4234,14 +5311,13 @@ VmReadMem64 ( accesses into the VM stack area, then we need to convert the address to point to the EBC entry point arguments area. Do this here. - @param VmPtr pointer to VM context - @param Addr address of interest + @param VmPtr A Pointer to VM context. + @param Addr Address of interest @return The unchanged address if it's not in the VM stack region. Otherwise, - @return adjust for the stack gap and return the modified address. + adjust for the stack gap and return the modified address. **/ -STATIC UINTN ConvertStackAddr ( IN VM_CONTEXT *VmPtr, @@ -4262,7 +5338,6 @@ ConvertStackAddr ( @return The natural value at address Addr. **/ -STATIC UINTN VmReadMemN ( IN VM_CONTEXT *VmPtr, @@ -4299,6 +5374,12 @@ VmReadMemN ( return Data; } +/** + Returns the version of the EBC virtual machine. + + @return The 64-bit version of EBC virtual machine. + +**/ UINT64 GetVmVersion ( VOID