]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Header file for Virtual Machine support. Contains EBC defines that can\r | |
3 | be of use to a disassembler for the most part. Also provides function\r | |
4 | prototypes for VM functions.\r | |
5 | \r | |
6 | Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r | |
7 | This program and the accompanying materials\r | |
8 | are licensed and made available under the terms and conditions of the BSD License\r | |
9 | which accompanies this distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #ifndef _EBC_EXECUTE_H_\r | |
18 | #define _EBC_EXECUTE_H_\r | |
19 | \r | |
20 | //\r | |
21 | // VM major/minor version\r | |
22 | //\r | |
23 | #define VM_MAJOR_VERSION 1\r | |
24 | #define VM_MINOR_VERSION 0\r | |
25 | \r | |
26 | //\r | |
27 | // Macros to check and set alignment\r | |
28 | //\r | |
29 | #define ASSERT_ALIGNED(addr, size) ASSERT (!((UINT32) (addr) & (size - 1)))\r | |
30 | #define IS_ALIGNED(addr, size) !((UINT32) (addr) & (size - 1))\r | |
31 | \r | |
32 | //\r | |
33 | // Define a macro to get the operand. Then we can change it to be either a\r | |
34 | // direct read or have it call a function to read memory.\r | |
35 | //\r | |
36 | #define GETOPERANDS(pVM) (UINT8) (*(UINT8 *) (pVM->Ip + 1))\r | |
37 | #define GETOPCODE(pVM) (UINT8) (*(UINT8 *) pVM->Ip)\r | |
38 | \r | |
39 | //\r | |
40 | // Bit masks for opcode encodings\r | |
41 | //\r | |
42 | #define OPCODE_M_OPCODE 0x3F // bits of interest for first level decode\r | |
43 | #define OPCODE_M_IMMDATA 0x80\r | |
44 | #define OPCODE_M_IMMDATA64 0x40\r | |
45 | #define OPCODE_M_64BIT 0x40 // for CMP\r | |
46 | #define OPCODE_M_RELADDR 0x10 // for CALL instruction\r | |
47 | #define OPCODE_M_CMPI32_DATA 0x80 // for CMPI\r | |
48 | #define OPCODE_M_CMPI64 0x40 // for CMPI 32 or 64 bit comparison\r | |
49 | #define OPERAND_M_MOVIN_N 0x80\r | |
50 | #define OPERAND_M_CMPI_INDEX 0x10\r | |
51 | \r | |
52 | //\r | |
53 | // Masks for instructions that encode presence of indexes for operand1 and/or\r | |
54 | // operand2.\r | |
55 | //\r | |
56 | #define OPCODE_M_IMMED_OP1 0x80\r | |
57 | #define OPCODE_M_IMMED_OP2 0x40\r | |
58 | \r | |
59 | //\r | |
60 | // Bit masks for operand encodings\r | |
61 | //\r | |
62 | #define OPERAND_M_INDIRECT1 0x08\r | |
63 | #define OPERAND_M_INDIRECT2 0x80\r | |
64 | #define OPERAND_M_OP1 0x07\r | |
65 | #define OPERAND_M_OP2 0x70\r | |
66 | \r | |
67 | //\r | |
68 | // Masks for data manipulation instructions\r | |
69 | //\r | |
70 | #define DATAMANIP_M_64 0x40 // 64-bit width operation\r | |
71 | #define DATAMANIP_M_IMMDATA 0x80\r | |
72 | \r | |
73 | //\r | |
74 | // For MOV instructions, need a mask for the opcode when immediate\r | |
75 | // data applies to R2.\r | |
76 | //\r | |
77 | #define OPCODE_M_IMMED_OP2 0x40\r | |
78 | \r | |
79 | //\r | |
80 | // The MOVI/MOVIn instructions use bit 6 of operands byte to indicate\r | |
81 | // if an index is present. Then bits 4 and 5 are used to indicate the width\r | |
82 | // of the move.\r | |
83 | //\r | |
84 | #define MOVI_M_IMMDATA 0x40\r | |
85 | #define MOVI_M_DATAWIDTH 0xC0\r | |
86 | #define MOVI_DATAWIDTH16 0x40\r | |
87 | #define MOVI_DATAWIDTH32 0x80\r | |
88 | #define MOVI_DATAWIDTH64 0xC0\r | |
89 | #define MOVI_M_MOVEWIDTH 0x30\r | |
90 | #define MOVI_MOVEWIDTH8 0x00\r | |
91 | #define MOVI_MOVEWIDTH16 0x10\r | |
92 | #define MOVI_MOVEWIDTH32 0x20\r | |
93 | #define MOVI_MOVEWIDTH64 0x30\r | |
94 | \r | |
95 | //\r | |
96 | // Masks for CALL instruction encodings\r | |
97 | //\r | |
98 | #define OPERAND_M_RELATIVE_ADDR 0x10\r | |
99 | #define OPERAND_M_NATIVE_CALL 0x20\r | |
100 | \r | |
101 | //\r | |
102 | // Masks for decoding push/pop instructions\r | |
103 | //\r | |
104 | #define PUSHPOP_M_IMMDATA 0x80 // opcode bit indicating immediate data\r | |
105 | #define PUSHPOP_M_64 0x40 // opcode bit indicating 64-bit operation\r | |
106 | //\r | |
107 | // Mask for operand of JMP instruction\r | |
108 | //\r | |
109 | #define JMP_M_RELATIVE 0x10\r | |
110 | #define JMP_M_CONDITIONAL 0x80\r | |
111 | #define JMP_M_CS 0x40\r | |
112 | \r | |
113 | //\r | |
114 | // Macros to determine if a given operand is indirect\r | |
115 | //\r | |
116 | #define OPERAND1_INDIRECT(op) ((op) & OPERAND_M_INDIRECT1)\r | |
117 | #define OPERAND2_INDIRECT(op) ((op) & OPERAND_M_INDIRECT2)\r | |
118 | \r | |
119 | //\r | |
120 | // Macros to extract the operands from second byte of instructions\r | |
121 | //\r | |
122 | #define OPERAND1_REGNUM(op) ((op) & OPERAND_M_OP1)\r | |
123 | #define OPERAND2_REGNUM(op) (((op) & OPERAND_M_OP2) >> 4)\r | |
124 | \r | |
125 | #define OPERAND1_CHAR(op) ('0' + OPERAND1_REGNUM (op))\r | |
126 | #define OPERAND2_CHAR(op) ('0' + OPERAND2_REGNUM (op))\r | |
127 | \r | |
128 | #define OPERAND1_REGDATA(pvm, op) pvm->Gpr[OPERAND1_REGNUM (op)]\r | |
129 | #define OPERAND2_REGDATA(pvm, op) pvm->Gpr[OPERAND2_REGNUM (op)]\r | |
130 | \r | |
131 | //\r | |
132 | // Condition masks usually for byte 1 encodings of code\r | |
133 | //\r | |
134 | #define CONDITION_M_CONDITIONAL 0x80\r | |
135 | #define CONDITION_M_CS 0x40\r | |
136 | \r | |
137 | //\r | |
138 | // Bits in the VM->StopFlags field\r | |
139 | //\r | |
140 | #define STOPFLAG_APP_DONE 0x0001\r | |
141 | #define STOPFLAG_BREAKPOINT 0x0002\r | |
142 | #define STOPFLAG_INVALID_BREAK 0x0004\r | |
143 | #define STOPFLAG_BREAK_ON_CALLEX 0x0008\r | |
144 | \r | |
145 | //\r | |
146 | // Masks for working with the VM flags register\r | |
147 | //\r | |
148 | #define VMFLAGS_CC 0x0001 // condition flag\r | |
149 | #define VMFLAGS_STEP 0x0002 // step instruction mode\r | |
150 | #define VMFLAGS_ALL_VALID (VMFLAGS_CC | VMFLAGS_STEP)\r | |
151 | \r | |
152 | //\r | |
153 | // Macros for operating on the VM flags register\r | |
154 | //\r | |
155 | #define VMFLAG_SET(pVM, Flag) (pVM->Flags |= (Flag))\r | |
156 | #define VMFLAG_ISSET(pVM, Flag) ((pVM->Flags & (Flag)) ? 1 : 0)\r | |
157 | #define VMFLAG_CLEAR(pVM, Flag) (pVM->Flags &= ~(Flag))\r | |
158 | \r | |
159 | //\r | |
160 | // Debug macro\r | |
161 | //\r | |
162 | #define EBCMSG(s) gST->ConOut->OutputString (gST->ConOut, s)\r | |
163 | \r | |
164 | //\r | |
165 | // Define OPCODES\r | |
166 | //\r | |
167 | #define OPCODE_BREAK 0x00\r | |
168 | #define OPCODE_JMP 0x01\r | |
169 | #define OPCODE_JMP8 0x02\r | |
170 | #define OPCODE_CALL 0x03\r | |
171 | #define OPCODE_RET 0x04\r | |
172 | #define OPCODE_CMPEQ 0x05\r | |
173 | #define OPCODE_CMPLTE 0x06\r | |
174 | #define OPCODE_CMPGTE 0x07\r | |
175 | #define OPCODE_CMPULTE 0x08\r | |
176 | #define OPCODE_CMPUGTE 0x09\r | |
177 | #define OPCODE_NOT 0x0A\r | |
178 | #define OPCODE_NEG 0x0B\r | |
179 | #define OPCODE_ADD 0x0C\r | |
180 | #define OPCODE_SUB 0x0D\r | |
181 | #define OPCODE_MUL 0x0E\r | |
182 | #define OPCODE_MULU 0x0F\r | |
183 | #define OPCODE_DIV 0x10\r | |
184 | #define OPCODE_DIVU 0x11\r | |
185 | #define OPCODE_MOD 0x12\r | |
186 | #define OPCODE_MODU 0x13\r | |
187 | #define OPCODE_AND 0x14\r | |
188 | #define OPCODE_OR 0x15\r | |
189 | #define OPCODE_XOR 0x16\r | |
190 | #define OPCODE_SHL 0x17\r | |
191 | #define OPCODE_SHR 0x18\r | |
192 | #define OPCODE_ASHR 0x19\r | |
193 | #define OPCODE_EXTNDB 0x1A\r | |
194 | #define OPCODE_EXTNDW 0x1B\r | |
195 | #define OPCODE_EXTNDD 0x1C\r | |
196 | #define OPCODE_MOVBW 0x1D\r | |
197 | #define OPCODE_MOVWW 0x1E\r | |
198 | #define OPCODE_MOVDW 0x1F\r | |
199 | #define OPCODE_MOVQW 0x20\r | |
200 | #define OPCODE_MOVBD 0x21\r | |
201 | #define OPCODE_MOVWD 0x22\r | |
202 | #define OPCODE_MOVDD 0x23\r | |
203 | #define OPCODE_MOVQD 0x24\r | |
204 | #define OPCODE_MOVSNW 0x25 // Move signed natural with word index\r | |
205 | #define OPCODE_MOVSND 0x26 // Move signed natural with dword index\r | |
206 | //\r | |
207 | // #define OPCODE_27 0x27\r | |
208 | //\r | |
209 | #define OPCODE_MOVQQ 0x28 // Does this go away?\r | |
210 | #define OPCODE_LOADSP 0x29\r | |
211 | #define OPCODE_STORESP 0x2A\r | |
212 | #define OPCODE_PUSH 0x2B\r | |
213 | #define OPCODE_POP 0x2C\r | |
214 | #define OPCODE_CMPIEQ 0x2D\r | |
215 | #define OPCODE_CMPILTE 0x2E\r | |
216 | #define OPCODE_CMPIGTE 0x2F\r | |
217 | #define OPCODE_CMPIULTE 0x30\r | |
218 | #define OPCODE_CMPIUGTE 0x31\r | |
219 | #define OPCODE_MOVNW 0x32\r | |
220 | #define OPCODE_MOVND 0x33\r | |
221 | //\r | |
222 | // #define OPCODE_34 0x34\r | |
223 | //\r | |
224 | #define OPCODE_PUSHN 0x35\r | |
225 | #define OPCODE_POPN 0x36\r | |
226 | #define OPCODE_MOVI 0x37\r | |
227 | #define OPCODE_MOVIN 0x38\r | |
228 | #define OPCODE_MOVREL 0x39\r | |
229 | \r | |
230 | /**\r | |
231 | Execute an EBC image from an entry point or from a published protocol.\r | |
232 | \r | |
233 | @param VmPtr A pointer to a VM context.\r | |
234 | \r | |
235 | @retval EFI_UNSUPPORTED At least one of the opcodes is not supported.\r | |
236 | @retval EFI_SUCCESS All of the instructions are executed successfully.\r | |
237 | \r | |
238 | **/\r | |
239 | EFI_STATUS\r | |
240 | EbcExecute (\r | |
241 | IN VM_CONTEXT *VmPtr\r | |
242 | );\r | |
243 | \r | |
244 | \r | |
245 | \r | |
246 | /**\r | |
247 | Returns the version of the EBC virtual machine.\r | |
248 | \r | |
249 | @return The 64-bit version of EBC virtual machine.\r | |
250 | \r | |
251 | **/\r | |
252 | UINT64\r | |
253 | GetVmVersion (\r | |
254 | VOID\r | |
255 | );\r | |
256 | \r | |
257 | /**\r | |
258 | Writes UINTN data to memory address.\r | |
259 | \r | |
260 | This routine is called by the EBC data\r | |
261 | movement instructions that write to memory. Since these writes\r | |
262 | may be to the stack, which looks like (high address on top) this,\r | |
263 | \r | |
264 | [EBC entry point arguments]\r | |
265 | [VM stack]\r | |
266 | [EBC stack]\r | |
267 | \r | |
268 | we need to detect all attempts to write to the EBC entry point argument\r | |
269 | stack area and adjust the address (which will initially point into the\r | |
270 | VM stack) to point into the EBC entry point arguments.\r | |
271 | \r | |
272 | @param VmPtr A pointer to a VM context.\r | |
273 | @param Addr Address to write to.\r | |
274 | @param Data Value to write to Addr.\r | |
275 | \r | |
276 | @retval EFI_SUCCESS The instruction is executed successfully.\r | |
277 | @retval Other Some error occurs when writing data to the address.\r | |
278 | \r | |
279 | **/\r | |
280 | EFI_STATUS\r | |
281 | VmWriteMemN (\r | |
282 | IN VM_CONTEXT *VmPtr,\r | |
283 | IN UINTN Addr,\r | |
284 | IN UINTN Data\r | |
285 | );\r | |
286 | \r | |
287 | /**\r | |
288 | Writes 64-bit data to memory address.\r | |
289 | \r | |
290 | This routine is called by the EBC data\r | |
291 | movement instructions that write to memory. Since these writes\r | |
292 | may be to the stack, which looks like (high address on top) this,\r | |
293 | \r | |
294 | [EBC entry point arguments]\r | |
295 | [VM stack]\r | |
296 | [EBC stack]\r | |
297 | \r | |
298 | we need to detect all attempts to write to the EBC entry point argument\r | |
299 | stack area and adjust the address (which will initially point into the\r | |
300 | VM stack) to point into the EBC entry point arguments.\r | |
301 | \r | |
302 | @param VmPtr A pointer to a VM context.\r | |
303 | @param Addr Address to write to.\r | |
304 | @param Data Value to write to Addr.\r | |
305 | \r | |
306 | @retval EFI_SUCCESS The instruction is executed successfully.\r | |
307 | @retval Other Some error occurs when writing data to the address.\r | |
308 | \r | |
309 | **/\r | |
310 | EFI_STATUS\r | |
311 | VmWriteMem64 (\r | |
312 | IN VM_CONTEXT *VmPtr,\r | |
313 | IN UINTN Addr,\r | |
314 | IN UINT64 Data\r | |
315 | );\r | |
316 | \r | |
317 | /**\r | |
318 | Given a pointer to a new VM context, execute one or more instructions. This\r | |
319 | function is only used for test purposes via the EBC VM test protocol.\r | |
320 | \r | |
321 | @param This A pointer to the EFI_EBC_VM_TEST_PROTOCOL structure.\r | |
322 | @param VmPtr A pointer to a VM context.\r | |
323 | @param InstructionCount A pointer to a UINTN value holding the number of\r | |
324 | instructions to execute. If it holds value of 0,\r | |
325 | then the instruction to be executed is 1.\r | |
326 | \r | |
327 | @retval EFI_UNSUPPORTED At least one of the opcodes is not supported.\r | |
328 | @retval EFI_SUCCESS All of the instructions are executed successfully.\r | |
329 | \r | |
330 | **/\r | |
331 | EFI_STATUS\r | |
332 | EFIAPI\r | |
333 | EbcExecuteInstructions (\r | |
334 | IN EFI_EBC_VM_TEST_PROTOCOL *This,\r | |
335 | IN VM_CONTEXT *VmPtr,\r | |
336 | IN OUT UINTN *InstructionCount\r | |
337 | );\r | |
338 | \r | |
339 | #endif // ifndef _EBC_EXECUTE_H_\r |