3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Header file for Virtual Machine support. Contains EBC defines that can
19 be of use to a disassembler for the most part. Also provides function
20 prototypes for VM functions.
24 #ifndef _EBC_EXECUTE_H_
25 #define _EBC_EXECUTE_H_
28 // VM major/minor version
30 #define VM_MAJOR_VERSION 1
31 #define VM_MINOR_VERSION 0
34 // Macros to check and set alignment
36 #define ASSERT_ALIGNED(addr, size) ASSERT (!((UINT32) (addr) & (size - 1)))
37 #define IS_ALIGNED(addr, size) !((UINT32) (addr) & (size - 1))
40 // Define a macro to get the operand. Then we can change it to be either a
41 // direct read or have it call a function to read memory.
43 #define GETOPERANDS(pVM) (UINT8) (*(UINT8 *) (pVM->Ip + 1))
44 #define GETOPCODE(pVM) (UINT8) (*(UINT8 *) pVM->Ip)
47 // Bit masks for opcode encodings
49 #define OPCODE_M_OPCODE 0x3F // bits of interest for first level decode
50 #define OPCODE_M_IMMDATA 0x80
51 #define OPCODE_M_IMMDATA64 0x40
52 #define OPCODE_M_64BIT 0x40 // for CMP
53 #define OPCODE_M_RELADDR 0x10 // for CALL instruction
54 #define OPCODE_M_CMPI32_DATA 0x80 // for CMPI
55 #define OPCODE_M_CMPI64 0x40 // for CMPI 32 or 64 bit comparison
56 #define OPERAND_M_MOVIN_N 0x80
57 #define OPERAND_M_CMPI_INDEX 0x10
60 // Masks for instructions that encode presence of indexes for operand1 and/or
63 #define OPCODE_M_IMMED_OP1 0x80
64 #define OPCODE_M_IMMED_OP2 0x40
67 // Bit masks for operand encodings
69 #define OPERAND_M_INDIRECT1 0x08
70 #define OPERAND_M_INDIRECT2 0x80
71 #define OPERAND_M_OP1 0x07
72 #define OPERAND_M_OP2 0x70
75 // Masks for data manipulation instructions
77 #define DATAMANIP_M_64 0x40 // 64-bit width operation
78 #define DATAMANIP_M_IMMDATA 0x80
81 // For MOV instructions, need a mask for the opcode when immediate
82 // data applies to R2.
84 #define OPCODE_M_IMMED_OP2 0x40
87 // The MOVI/MOVIn instructions use bit 6 of operands byte to indicate
88 // if an index is present. Then bits 4 and 5 are used to indicate the width
91 #define MOVI_M_IMMDATA 0x40
92 #define MOVI_M_DATAWIDTH 0xC0
93 #define MOVI_DATAWIDTH16 0x40
94 #define MOVI_DATAWIDTH32 0x80
95 #define MOVI_DATAWIDTH64 0xC0
96 #define MOVI_M_MOVEWIDTH 0x30
97 #define MOVI_MOVEWIDTH8 0x00
98 #define MOVI_MOVEWIDTH16 0x10
99 #define MOVI_MOVEWIDTH32 0x20
100 #define MOVI_MOVEWIDTH64 0x30
103 // Masks for CALL instruction encodings
105 #define OPERAND_M_RELATIVE_ADDR 0x10
106 #define OPERAND_M_NATIVE_CALL 0x20
109 // Masks for decoding push/pop instructions
111 #define PUSHPOP_M_IMMDATA 0x80 // opcode bit indicating immediate data
112 #define PUSHPOP_M_64 0x40 // opcode bit indicating 64-bit operation
114 // Mask for operand of JMP instruction
116 #define JMP_M_RELATIVE 0x10
117 #define JMP_M_CONDITIONAL 0x80
118 #define JMP_M_CS 0x40
121 // Macros to determine if a given operand is indirect
123 #define OPERAND1_INDIRECT(op) ((op) & OPERAND_M_INDIRECT1)
124 #define OPERAND2_INDIRECT(op) ((op) & OPERAND_M_INDIRECT2)
127 // Macros to extract the operands from second byte of instructions
129 #define OPERAND1_REGNUM(op) ((op) & OPERAND_M_OP1)
130 #define OPERAND2_REGNUM(op) (((op) & OPERAND_M_OP2) >> 4)
132 #define OPERAND1_CHAR(op) ('0' + OPERAND1_REGNUM (op))
133 #define OPERAND2_CHAR(op) ('0' + OPERAND2_REGNUM (op))
135 #define OPERAND1_REGDATA(pvm, op) pvm->R[OPERAND1_REGNUM (op)]
136 #define OPERAND2_REGDATA(pvm, op) pvm->R[OPERAND2_REGNUM (op)]
139 // Condition masks usually for byte 1 encodings of code
141 #define CONDITION_M_CONDITIONAL 0x80
142 #define CONDITION_M_CS 0x40
145 // Bits in the VM->StopFlags field
147 #define STOPFLAG_APP_DONE 0x0001
148 #define STOPFLAG_BREAKPOINT 0x0002
149 #define STOPFLAG_INVALID_BREAK 0x0004
150 #define STOPFLAG_BREAK_ON_CALLEX 0x0008
153 // Masks for working with the VM flags register
155 #define VMFLAGS_CC 0x0001 // condition flag
156 #define VMFLAGS_STEP 0x0002 // step instruction mode
157 #define VMFLAGS_ALL_VALID (VMFLAGS_CC | VMFLAGS_STEP)
160 // Macros for operating on the VM flags register
162 #define VMFLAG_SET(pVM, Flag) (pVM->Flags |= (Flag))
163 #define VMFLAG_ISSET(pVM, Flag) ((pVM->Flags & (Flag)) ? 1 : 0)
164 #define VMFLAG_CLEAR(pVM, Flag) (pVM->Flags &= ~(Flag))
169 #define EBCMSG(s) gST->ConOut->OutputString (gST->ConOut, s)
174 #define OPCODE_BREAK 0x00
175 #define OPCODE_JMP 0x01
176 #define OPCODE_JMP8 0x02
177 #define OPCODE_CALL 0x03
178 #define OPCODE_RET 0x04
179 #define OPCODE_CMPEQ 0x05
180 #define OPCODE_CMPLTE 0x06
181 #define OPCODE_CMPGTE 0x07
182 #define OPCODE_CMPULTE 0x08
183 #define OPCODE_CMPUGTE 0x09
184 #define OPCODE_NOT 0x0A
185 #define OPCODE_NEG 0x0B
186 #define OPCODE_ADD 0x0C
187 #define OPCODE_SUB 0x0D
188 #define OPCODE_MUL 0x0E
189 #define OPCODE_MULU 0x0F
190 #define OPCODE_DIV 0x10
191 #define OPCODE_DIVU 0x11
192 #define OPCODE_MOD 0x12
193 #define OPCODE_MODU 0x13
194 #define OPCODE_AND 0x14
195 #define OPCODE_OR 0x15
196 #define OPCODE_XOR 0x16
197 #define OPCODE_SHL 0x17
198 #define OPCODE_SHR 0x18
199 #define OPCODE_ASHR 0x19
200 #define OPCODE_EXTNDB 0x1A
201 #define OPCODE_EXTNDW 0x1B
202 #define OPCODE_EXTNDD 0x1C
203 #define OPCODE_MOVBW 0x1D
204 #define OPCODE_MOVWW 0x1E
205 #define OPCODE_MOVDW 0x1F
206 #define OPCODE_MOVQW 0x20
207 #define OPCODE_MOVBD 0x21
208 #define OPCODE_MOVWD 0x22
209 #define OPCODE_MOVDD 0x23
210 #define OPCODE_MOVQD 0x24
211 #define OPCODE_MOVSNW 0x25 // Move signed natural with word index
212 #define OPCODE_MOVSND 0x26 // Move signed natural with dword index
214 // #define OPCODE_27 0x27
216 #define OPCODE_MOVQQ 0x28 // Does this go away?
217 #define OPCODE_LOADSP 0x29
218 #define OPCODE_STORESP 0x2A
219 #define OPCODE_PUSH 0x2B
220 #define OPCODE_POP 0x2C
221 #define OPCODE_CMPIEQ 0x2D
222 #define OPCODE_CMPILTE 0x2E
223 #define OPCODE_CMPIGTE 0x2F
224 #define OPCODE_CMPIULTE 0x30
225 #define OPCODE_CMPIUGTE 0x31
226 #define OPCODE_MOVNW 0x32
227 #define OPCODE_MOVND 0x33
229 // #define OPCODE_34 0x34
231 #define OPCODE_PUSHN 0x35
232 #define OPCODE_POPN 0x36
233 #define OPCODE_MOVI 0x37
234 #define OPCODE_MOVIN 0x38
235 #define OPCODE_MOVREL 0x39
253 IN VM_CONTEXT
*VmPtr
,
261 IN VM_CONTEXT
*VmPtr
,
268 // Define a protocol for an EBC VM test interface.
270 #define EFI_EBC_VM_TEST_PROTOCOL_GUID \
272 0xAAEACCFDL, 0xF27B, 0x4C17, { 0xB6, 0x10, 0x75, 0xCA, 0x1F, 0x2D, 0xFB, 0x52 } \
276 // Define for forward reference.
278 typedef struct _EFI_EBC_VM_TEST_PROTOCOL EFI_EBC_VM_TEST_PROTOCOL
;
282 (*EBC_VM_TEST_EXECUTE
) (
283 IN EFI_EBC_VM_TEST_PROTOCOL
* This
,
284 IN VM_CONTEXT
* VmPtr
,
285 IN OUT UINTN
*InstructionCount
291 IN EFI_EBC_VM_TEST_PROTOCOL
* This
,
294 IN OUT UINTN
*BufferLen
299 (*EBC_VM_TEST_DASM
) (
300 IN EFI_EBC_VM_TEST_PROTOCOL
* This
,
301 IN OUT CHAR16
*AsmText
,
307 // Prototype for the actual EBC test protocol interface
309 struct _EFI_EBC_VM_TEST_PROTOCOL
{
310 EBC_VM_TEST_EXECUTE Execute
;
311 EBC_VM_TEST_ASM Assemble
;
312 EBC_VM_TEST_DASM Disassemble
;
316 EbcExecuteInstructions (
317 IN EFI_EBC_VM_TEST_PROTOCOL
*This
,
318 IN VM_CONTEXT
*VmPtr
,
319 IN OUT UINTN
*InstructionCount
323 #endif // ifndef _EBC_EXECUTE_H_