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 // Macros to check and set alignment
30 #define ASSERT_ALIGNED(addr, size) ASSERT (!((UINT32) (addr) & (size - 1)))
31 #define IS_ALIGNED(addr, size) !((UINT32) (addr) & (size - 1))
34 // Define a macro to get the operand. Then we can change it to be either a
35 // direct read or have it call a function to read memory.
37 #define GETOPERANDS(pVM) (UINT8) (*(UINT8 *) (pVM->Ip + 1))
38 #define GETOPCODE(pVM) (UINT8) (*(UINT8 *) pVM->Ip)
41 // Bit masks for opcode encodings
43 #define OPCODE_M_OPCODE 0x3F // bits of interest for first level decode
44 #define OPCODE_M_IMMDATA 0x80
45 #define OPCODE_M_IMMDATA64 0x40
46 #define OPCODE_M_64BIT 0x40 // for CMP
47 #define OPCODE_M_RELADDR 0x10 // for CALL instruction
48 #define OPCODE_M_CMPI32_DATA 0x80 // for CMPI
49 #define OPCODE_M_CMPI64 0x40 // for CMPI 32 or 64 bit comparison
50 #define OPERAND_M_MOVIN_N 0x80
51 #define OPERAND_M_CMPI_INDEX 0x10
54 // Masks for instructions that encode presence of indexes for operand1 and/or
57 #define OPCODE_M_IMMED_OP1 0x80
58 #define OPCODE_M_IMMED_OP2 0x40
61 // Bit masks for operand encodings
63 #define OPERAND_M_INDIRECT1 0x08
64 #define OPERAND_M_INDIRECT2 0x80
65 #define OPERAND_M_OP1 0x07
66 #define OPERAND_M_OP2 0x70
69 // Masks for data manipulation instructions
71 #define DATAMANIP_M_64 0x40 // 64-bit width operation
72 #define DATAMANIP_M_IMMDATA 0x80
75 // For MOV instructions, need a mask for the opcode when immediate
76 // data applies to R2.
78 #define OPCODE_M_IMMED_OP2 0x40
81 // The MOVI/MOVIn instructions use bit 6 of operands byte to indicate
82 // if an index is present. Then bits 4 and 5 are used to indicate the width
85 #define MOVI_M_IMMDATA 0x40
86 #define MOVI_M_DATAWIDTH 0xC0
87 #define MOVI_DATAWIDTH16 0x40
88 #define MOVI_DATAWIDTH32 0x80
89 #define MOVI_DATAWIDTH64 0xC0
90 #define MOVI_M_MOVEWIDTH 0x30
91 #define MOVI_MOVEWIDTH8 0x00
92 #define MOVI_MOVEWIDTH16 0x10
93 #define MOVI_MOVEWIDTH32 0x20
94 #define MOVI_MOVEWIDTH64 0x30
97 // Masks for CALL instruction encodings
99 #define OPERAND_M_RELATIVE_ADDR 0x10
100 #define OPERAND_M_NATIVE_CALL 0x20
103 // Masks for decoding push/pop instructions
105 #define PUSHPOP_M_IMMDATA 0x80 // opcode bit indicating immediate data
106 #define PUSHPOP_M_64 0x40 // opcode bit indicating 64-bit operation
108 // Mask for operand of JMP instruction
110 #define JMP_M_RELATIVE 0x10
111 #define JMP_M_CONDITIONAL 0x80
112 #define JMP_M_CS 0x40
115 // Macros to determine if a given operand is indirect
117 #define OPERAND1_INDIRECT(op) ((op) & OPERAND_M_INDIRECT1)
118 #define OPERAND2_INDIRECT(op) ((op) & OPERAND_M_INDIRECT2)
121 // Macros to extract the operands from second byte of instructions
123 #define OPERAND1_REGNUM(op) ((op) & OPERAND_M_OP1)
124 #define OPERAND2_REGNUM(op) (((op) & OPERAND_M_OP2) >> 4)
126 #define OPERAND1_CHAR(op) ('0' + OPERAND1_REGNUM (op))
127 #define OPERAND2_CHAR(op) ('0' + OPERAND2_REGNUM (op))
129 #define OPERAND1_REGDATA(pvm, op) pvm->R[OPERAND1_REGNUM (op)]
130 #define OPERAND2_REGDATA(pvm, op) pvm->R[OPERAND2_REGNUM (op)]
133 // Condition masks usually for byte 1 encodings of code
135 #define CONDITION_M_CONDITIONAL 0x80
136 #define CONDITION_M_CS 0x40
139 // Bits in the VM->StopFlags field
141 #define STOPFLAG_APP_DONE 0x0001
142 #define STOPFLAG_BREAKPOINT 0x0002
143 #define STOPFLAG_INVALID_BREAK 0x0004
144 #define STOPFLAG_BREAK_ON_CALLEX 0x0008
147 // Masks for working with the VM flags register
149 #define VMFLAGS_CC 0x0001 // condition flag
150 #define VMFLAGS_STEP 0x0002 // step instruction mode
151 #define VMFLAGS_ALL_VALID (VMFLAGS_CC | VMFLAGS_STEP)
154 // Macros for operating on the VM flags register
156 #define VMFLAG_SET(pVM, Flag) (pVM->Flags |= (Flag))
157 #define VMFLAG_ISSET(pVM, Flag) ((pVM->Flags & (Flag)) ? 1 : 0)
158 #define VMFLAG_CLEAR(pVM, Flag) (pVM->Flags &= ~(Flag))
163 #define EBCMSG(s) gST->ConOut->OutputString (gST->ConOut, s)
168 #define OPCODE_BREAK 0x00
169 #define OPCODE_JMP 0x01
170 #define OPCODE_JMP8 0x02
171 #define OPCODE_CALL 0x03
172 #define OPCODE_RET 0x04
173 #define OPCODE_CMPEQ 0x05
174 #define OPCODE_CMPLTE 0x06
175 #define OPCODE_CMPGTE 0x07
176 #define OPCODE_CMPULTE 0x08
177 #define OPCODE_CMPUGTE 0x09
178 #define OPCODE_NOT 0x0A
179 #define OPCODE_NEG 0x0B
180 #define OPCODE_ADD 0x0C
181 #define OPCODE_SUB 0x0D
182 #define OPCODE_MUL 0x0E
183 #define OPCODE_MULU 0x0F
184 #define OPCODE_DIV 0x10
185 #define OPCODE_DIVU 0x11
186 #define OPCODE_MOD 0x12
187 #define OPCODE_MODU 0x13
188 #define OPCODE_AND 0x14
189 #define OPCODE_OR 0x15
190 #define OPCODE_XOR 0x16
191 #define OPCODE_SHL 0x17
192 #define OPCODE_SHR 0x18
193 #define OPCODE_ASHR 0x19
194 #define OPCODE_EXTNDB 0x1A
195 #define OPCODE_EXTNDW 0x1B
196 #define OPCODE_EXTNDD 0x1C
197 #define OPCODE_MOVBW 0x1D
198 #define OPCODE_MOVWW 0x1E
199 #define OPCODE_MOVDW 0x1F
200 #define OPCODE_MOVQW 0x20
201 #define OPCODE_MOVBD 0x21
202 #define OPCODE_MOVWD 0x22
203 #define OPCODE_MOVDD 0x23
204 #define OPCODE_MOVQD 0x24
205 #define OPCODE_MOVSNW 0x25 // Move signed natural with word index
206 #define OPCODE_MOVSND 0x26 // Move signed natural with dword index
208 // #define OPCODE_27 0x27
210 #define OPCODE_MOVQQ 0x28 // Does this go away?
211 #define OPCODE_LOADSP 0x29
212 #define OPCODE_STORESP 0x2A
213 #define OPCODE_PUSH 0x2B
214 #define OPCODE_POP 0x2C
215 #define OPCODE_CMPIEQ 0x2D
216 #define OPCODE_CMPILTE 0x2E
217 #define OPCODE_CMPIGTE 0x2F
218 #define OPCODE_CMPIULTE 0x30
219 #define OPCODE_CMPIUGTE 0x31
220 #define OPCODE_MOVNW 0x32
221 #define OPCODE_MOVND 0x33
223 // #define OPCODE_34 0x34
225 #define OPCODE_PUSHN 0x35
226 #define OPCODE_POPN 0x36
227 #define OPCODE_MOVI 0x37
228 #define OPCODE_MOVIN 0x38
229 #define OPCODE_MOVREL 0x39
247 IN VM_CONTEXT
*VmPtr
,
255 IN VM_CONTEXT
*VmPtr
,
262 // Define a protocol for an EBC VM test interface.
264 #define EFI_EBC_VM_TEST_PROTOCOL_GUID \
266 0xAAEACCFDL, 0xF27B, 0x4C17, { 0xB6, 0x10, 0x75, 0xCA, 0x1F, 0x2D, 0xFB, 0x52 } \
270 // Define for forward reference.
272 typedef struct _EFI_EBC_VM_TEST_PROTOCOL EFI_EBC_VM_TEST_PROTOCOL
;
276 (*EBC_VM_TEST_EXECUTE
) (
277 IN EFI_EBC_VM_TEST_PROTOCOL
* This
,
278 IN VM_CONTEXT
* VmPtr
,
279 IN OUT UINTN
*InstructionCount
285 IN EFI_EBC_VM_TEST_PROTOCOL
* This
,
288 IN OUT UINTN
*BufferLen
293 (*EBC_VM_TEST_DASM
) (
294 IN EFI_EBC_VM_TEST_PROTOCOL
* This
,
295 IN OUT CHAR16
*AsmText
,
301 // Prototype for the actual EBC test protocol interface
303 struct _EFI_EBC_VM_TEST_PROTOCOL
{
304 EBC_VM_TEST_EXECUTE Execute
;
305 EBC_VM_TEST_ASM Assemble
;
306 EBC_VM_TEST_DASM Disassemble
;
310 EbcExecuteInstructions (
311 IN EFI_EBC_VM_TEST_PROTOCOL
*This
,
312 IN VM_CONTEXT
*VmPtr
,
313 IN OUT UINTN
*InstructionCount
317 #endif // ifndef _EBC_EXECUTE_H_