VirtualBox

source: vbox/trunk/src/VBox/Disassembler/Disasm.cpp@ 8361

Last change on this file since 8361 was 8361, checked in by vboxsync, 17 years ago

Even more disassembler updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.6 KB
Line 
1/** @file
2 *
3 * VBox disassembler:
4 * Main
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#ifdef USING_VISUAL_STUDIO
28# include <stdafx.h>
29#endif
30#include <VBox/dis.h>
31#include <VBox/disopcode.h>
32#include <VBox/err.h>
33#include <iprt/assert.h>
34#include <iprt/string.h>
35#include "DisasmInternal.h"
36#include "DisasmTables.h"
37
38
39/**
40 * Disassembles a code block.
41 *
42 * @returns VBox error code
43 * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
44 * set correctly.
45 * @param pvCodeBlock Pointer to the strunction to disassemble.
46 * @param cbMax Maximum number of bytes to disassemble.
47 * @param pcbSize Where to store the size of the instruction.
48 * NULL is allowed.
49 *
50 *
51 * @todo Define output callback.
52 * @todo Using signed integers as sizes is a bit odd. There are still
53 * some GCC warnings about mixing signed and unsigend integers.
54 * @todo Need to extend this interface to include a code address so we
55 * can dissassemble GC code. Perhaps a new function is better...
56 * @remark cbMax isn't respected as a boundry. DISInstr() will read beyond cbMax.
57 * This means *pcbSize >= cbMax sometimes.
58 */
59DISDECL(int) DISBlock(PDISCPUSTATE pCpu, RTUINTPTR pvCodeBlock, unsigned cbMax, unsigned *pSize)
60{
61 unsigned i = 0;
62 char szOutput[256];
63
64 while (i < cbMax)
65 {
66 unsigned cbInstr;
67 int rc = DISInstr(pCpu, pvCodeBlock + i, 0, &cbInstr, szOutput);
68 if (VBOX_FAILURE(rc))
69 return rc;
70
71 i += cbInstr;
72 }
73
74 if (pSize)
75 *pSize = i;
76 return true;
77}
78
79/**
80 * Disassembles one instruction
81 *
82 * @returns VBox error code
83 * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
84 * set correctly.
85 * @param pu8Instruction Pointer to the strunction to disassemble.
86 * @param u32EipOffset Offset to add to instruction address to get the real virtual address
87 * @param pcbSize Where to store the size of the instruction.
88 * NULL is allowed.
89 * @param pszOutput Storage for disassembled instruction
90 *
91 * @todo Define output callback.
92 */
93DISDECL(int) DISInstr(PDISCPUSTATE pCpu, RTUINTPTR pu8Instruction, unsigned u32EipOffset, unsigned *pcbSize,
94 char *pszOutput)
95{
96 return DISInstrEx(pCpu, pu8Instruction, u32EipOffset, pcbSize, pszOutput, OPTYPE_ALL);
97}
98
99/**
100 * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
101 *
102 * @returns VBox error code
103 * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
104 * set correctly.
105 * @param pu8Instruction Pointer to the strunction to disassemble.
106 * @param u32EipOffset Offset to add to instruction address to get the real virtual address
107 * @param pcbSize Where to store the size of the instruction.
108 * NULL is allowed.
109 * @param pszOutput Storage for disassembled instruction
110 * @param uFilter Instruction type filter
111 *
112 * @todo Define output callback.
113 */
114DISDECL(int) DISInstrEx(PDISCPUSTATE pCpu, RTUINTPTR pu8Instruction, unsigned u32EipOffset, unsigned *pcbSize,
115 char *pszOutput, unsigned uFilter)
116{
117 unsigned i = 0, prefixbytes;
118 unsigned idx, inc;
119#ifdef __L4ENV__
120 jmp_buf jumpbuffer;
121#endif
122
123 //reset instruction settings
124 pCpu->prefix = PREFIX_NONE;
125 pCpu->prefix_seg = 0;
126 pCpu->ModRM.u = 0;
127 pCpu->SIB.u = 0;
128 pCpu->lastprefix = 0;
129 pCpu->param1.parval = 0;
130 pCpu->param2.parval = 0;
131 pCpu->param3.parval = 0;
132 pCpu->param1.szParam[0] = 0;
133 pCpu->param2.szParam[0] = 0;
134 pCpu->param3.szParam[0] = 0;
135 pCpu->param1.size = 0;
136 pCpu->param2.size = 0;
137 pCpu->param3.size = 0;
138 pCpu->param1.flags = 0;
139 pCpu->param2.flags = 0;
140 pCpu->param3.flags = 0;
141 pCpu->uFilter = uFilter;
142 pCpu->pfnDisasmFnTable = pfnFullDisasm;
143
144 if (pszOutput)
145 *pszOutput = '\0';
146
147 if (pCpu->mode == CPUMODE_64BIT)
148 {
149 pCpu->addrmode = CPUMODE_64BIT;
150 pCpu->opmode = CPUMODE_32BIT;
151 }
152 else
153 {
154 pCpu->addrmode = pCpu->mode;
155 pCpu->opmode = pCpu->mode;
156 }
157
158 prefixbytes = 0;
159#ifndef __L4ENV__ /* Unfortunately, we have no exception handling in l4env */
160 try
161#else
162 pCpu->pJumpBuffer = &jumpbuffer;
163 if (setjmp(jumpbuffer) == 0)
164#endif
165 {
166 while(1)
167 {
168 uint8_t codebyte = DISReadByte(pCpu, pu8Instruction+i);
169 uint8_t opcode = g_aOneByteMapX86[codebyte].opcode;
170
171 /* Hardcoded assumption about OP_* values!! */
172 if (opcode <= OP_LOCK)
173 {
174 pCpu->lastprefix = opcode;
175
176 /* The REX prefix must precede the opcode byte(s). Any other placement is ignored. */
177 if (opcode != OP_REX)
178 pCpu->prefix &= ~PREFIX_REX;
179
180 switch(opcode)
181 {
182 case OP_INVALID:
183#if 0 //defined (DEBUG_Sander)
184 AssertMsgFailed(("Invalid opcode!!\n"));
185#endif
186 return VERR_DIS_INVALID_OPCODE;
187
188 // segment override prefix byte
189 case OP_SEG:
190 pCpu->prefix_seg = g_aOneByteMapX86[codebyte].param1 - OP_PARM_REG_SEG_START;
191 /* Segment prefixes for CS, DS, ES and SS are ignored in long mode. */
192 if ( pCpu->mode != CPUMODE_64BIT
193 || pCpu->prefix_seg >= OP_PARM_REG_FS)
194 {
195 pCpu->prefix |= PREFIX_SEG;
196 }
197 i += sizeof(uint8_t);
198 prefixbytes++;
199 continue; //fetch the next byte
200
201 // lock prefix byte
202 case OP_LOCK:
203 pCpu->prefix |= PREFIX_LOCK;
204 i += sizeof(uint8_t);
205 prefixbytes++;
206 continue; //fetch the next byte
207
208 // address size override prefix byte
209 case OP_ADDRSIZE:
210 pCpu->prefix |= PREFIX_ADDRSIZE;
211 if (pCpu->mode == CPUMODE_16BIT)
212 pCpu->addrmode = CPUMODE_32BIT;
213 else
214 if (pCpu->mode == CPUMODE_32BIT)
215 pCpu->addrmode = CPUMODE_16BIT;
216 else
217 pCpu->addrmode = CPUMODE_32BIT; /* 64 bits */
218
219 i += sizeof(uint8_t);
220 prefixbytes++;
221 continue; //fetch the next byte
222
223 // operand size override prefix byte
224 case OP_OPSIZE:
225 pCpu->prefix |= PREFIX_OPSIZE;
226 if (pCpu->mode == CPUMODE_16BIT)
227 pCpu->opmode = CPUMODE_32BIT;
228 else
229 pCpu->opmode = CPUMODE_16BIT; /* for 32 and 64 bits mode (there is no 32 bits operand size override prefix) */
230
231 i += sizeof(uint8_t);
232 prefixbytes++;
233 continue; //fetch the next byte
234
235 // rep and repne are not really prefixes, but we'll treat them as such
236 case OP_REPE:
237 pCpu->prefix |= PREFIX_REP;
238 i += sizeof(uint8_t);
239 prefixbytes += sizeof(uint8_t);
240 continue; //fetch the next byte
241
242 case OP_REPNE:
243 pCpu->prefix |= PREFIX_REPNE;
244 i += sizeof(uint8_t);
245 prefixbytes += sizeof(uint8_t);
246 continue; //fetch the next byte
247
248 case OP_REX:
249 Assert(pCpu->mode == CPUMODE_64BIT);
250 /* REX prefix byte */
251 pCpu->prefix |= PREFIX_REX;
252 pCpu->prefix_rex = PREFIX_REX_OP_2_FLAGS(opcode);
253
254 if (pCpu->prefix_rex & PREFIX_REX_FLAGS_W)
255 pCpu->opmode = CPUMODE_64BIT; /* overrides size prefix byte */
256 break;
257 }
258 }
259
260 idx = i;
261 i += sizeof(uint8_t); //first opcode byte
262
263 pCpu->opcode = codebyte;
264 /* Prefix byte(s) is/are part of the instruction. */
265 pCpu->opaddr = pu8Instruction + idx + u32EipOffset - prefixbytes;
266
267 if (pCpu->mode == CPUMODE_64BIT)
268 inc = ParseInstruction(pu8Instruction + i, &g_aOneByteMapX64[pCpu->opcode], pCpu);
269 else
270 inc = ParseInstruction(pu8Instruction + i, &g_aOneByteMapX86[pCpu->opcode], pCpu);
271
272 pCpu->opsize = prefixbytes + inc + sizeof(uint8_t);
273
274 if(pszOutput) {
275 disasmSprintf(pszOutput, pu8Instruction+i-1-prefixbytes, pCpu, &pCpu->param1, &pCpu->param2, &pCpu->param3);
276 }
277
278 i += inc;
279 prefixbytes = 0;
280 break;
281 }
282 }
283#ifndef __L4ENV__
284 catch(...)
285#else
286 else /* setjmp has returned a non-zero value: an exception occured */
287#endif
288 {
289 if (pcbSize)
290 *pcbSize = 0;
291 return VERR_DIS_GEN_FAILURE;
292 }
293
294 if (pcbSize)
295 *pcbSize = i;
296
297 return VINF_SUCCESS;
298}
299//*****************************************************************************
300//*****************************************************************************
301char *DbgBytesToString(PDISCPUSTATE pCpu, RTUINTPTR pBytes, int size, char *pszOutput)
302{
303 char szByte[4];
304 int len = strlen(pszOutput);
305 int i;
306
307 for(i = len; i < 40; i++)
308 {
309 strcat(pszOutput, " ");
310 }
311 strcat(pszOutput, " [");
312 for(i = 0; i < size; i++)
313 {
314 RTStrPrintf(szByte, sizeof(szByte), "%02X ", DISReadByte(pCpu, pBytes+i));
315 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, szByte);
316 }
317 len = strlen(pszOutput);
318 pszOutput[len - 1] = 0; //cut off last space
319
320 strcat(pszOutput, "]");
321 return pszOutput;
322}
323//*****************************************************************************
324//*****************************************************************************
325void disasmSprintf(char *pszOutput, RTUINTPTR pu8Instruction, PDISCPUSTATE pCpu, OP_PARAMETER *pParam1, OP_PARAMETER *pParam2, OP_PARAMETER *pParam3)
326{
327 const char *lpszFormat = pCpu->pszOpcode;
328 int param = 1;
329
330 RTStrPrintf(pszOutput, 64, "%08X: ", (unsigned)pCpu->opaddr);
331 if(pCpu->prefix & PREFIX_LOCK)
332 {
333 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "lock ");
334 }
335 if(pCpu->prefix & PREFIX_REP)
336 {
337 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "rep(e) ");
338 }
339 else
340 if(pCpu->prefix & PREFIX_REPNE)
341 {
342 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "repne ");
343 }
344
345 if(!strcmp("Invalid Opcode", lpszFormat))
346 {
347 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "Invalid Opcode [%02X][%02X]", DISReadByte(pCpu, pu8Instruction), DISReadByte(pCpu, pu8Instruction+1) );
348 }
349 else
350 while(*lpszFormat)
351 {
352 switch(*lpszFormat)
353 {
354 case '%':
355 switch(*(lpszFormat+1))
356 {
357 case 'J': //Relative jump offset
358 {
359 int32_t disp;
360
361 AssertMsg(param == 1, ("Invalid branch parameter nr"));
362 if(pParam1->flags & USE_IMMEDIATE8_REL)
363 {
364 disp = (int32_t)(char)pParam1->parval;
365 }
366 else
367 if(pParam1->flags & USE_IMMEDIATE16_REL)
368 {
369 disp = (int32_t)(uint16_t)pParam1->parval;
370 }
371 else
372 if(pParam1->flags & USE_IMMEDIATE32_REL)
373 {
374 disp = (int32_t)pParam1->parval;
375 }
376 else
377 {
378 AssertMsgFailed(("Oops!\n"));
379 return;
380 }
381 uint32_t addr = (uint32_t)(pCpu->opaddr + pCpu->opsize) + disp;
382 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "[%08X]", addr);
383 }
384
385 //no break;
386
387 case 'A': //direct address
388 case 'C': //control register
389 case 'D': //debug register
390 case 'E': //ModRM specifies parameter
391 case 'F': //Eflags register
392 case 'G': //ModRM selects general register
393 case 'I': //Immediate data
394 case 'M': //ModRM may only refer to memory
395 case 'O': //No ModRM byte
396 case 'P': //ModRM byte selects MMX register
397 case 'Q': //ModRM byte selects MMX register or memory address
398 case 'R': //ModRM byte may only refer to a general register
399 case 'S': //ModRM byte selects a segment register
400 case 'T': //ModRM byte selects a test register
401 case 'V': //ModRM byte selects an XMM/SSE register
402 case 'W': //ModRM byte selects an XMM/SSE register or a memory address
403 case 'X': //DS:SI
404 case 'Y': //ES:DI
405 switch(param)
406 {
407 case 1:
408 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, pParam1->szParam);
409 break;
410 case 2:
411 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, pParam2->szParam);
412 break;
413 case 3:
414 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, pParam3->szParam);
415 break;
416 }
417 break;
418
419 case 'e': //register based on operand size (e.g. %eAX)
420 if(pCpu->opmode == CPUMODE_32BIT)
421 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "E");
422 if(pCpu->opmode == CPUMODE_64BIT)
423 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "R");
424
425 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "%c%c", lpszFormat[2], lpszFormat[3]);
426 break;
427
428 default:
429 AssertMsgFailed(("Oops!\n"));
430 break;
431 }
432
433 //Go to the next parameter in the format string
434 while(*lpszFormat && *lpszFormat != ',') lpszFormat++;
435 if(*lpszFormat == ',') lpszFormat--;
436
437 break;
438
439 case ',':
440 param++;
441 //no break
442
443 default:
444 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "%c", *lpszFormat);
445 break;
446 }
447
448 if(*lpszFormat) lpszFormat++;
449 }
450 DbgBytesToString(pCpu, pu8Instruction, pCpu->opsize, pszOutput);
451 RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "\n");
452}
453//*****************************************************************************
454//*****************************************************************************
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette