VirtualBox

source: vbox/trunk/src/VBox/Disassembler/DisasmReg.cpp@ 30855

Last change on this file since 30855 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
  • Property svn:sync_process set to export
File size: 28.8 KB
Line 
1/** @file
2 *
3 * VBox disassembler:
4 * Core components
5 */
6
7/*
8 * Copyright (C) 2006-2007 Oracle Corporation
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
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_DIS
24#ifdef USING_VISUAL_STUDIO
25# include <stdafx.h>
26#endif
27
28#include <VBox/dis.h>
29#include <VBox/disopcode.h>
30#include <VBox/cpum.h>
31#include <VBox/err.h>
32#include <VBox/log.h>
33#include <iprt/assert.h>
34#include <iprt/string.h>
35#include <iprt/stdarg.h>
36#include "DisasmInternal.h"
37#include "DisasmTables.h"
38
39#if !defined(DIS_CORE_ONLY) && defined(LOG_ENABLED)
40# include <stdlib.h>
41# include <stdio.h>
42#endif
43
44
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48
49/**
50 * Array for accessing 64-bit general registers in VMMREGFRAME structure
51 * by register's index from disasm.
52 */
53static const unsigned g_aReg64Index[] =
54{
55 RT_OFFSETOF(CPUMCTXCORE, rax), /* USE_REG_RAX */
56 RT_OFFSETOF(CPUMCTXCORE, rcx), /* USE_REG_RCX */
57 RT_OFFSETOF(CPUMCTXCORE, rdx), /* USE_REG_RDX */
58 RT_OFFSETOF(CPUMCTXCORE, rbx), /* USE_REG_RBX */
59 RT_OFFSETOF(CPUMCTXCORE, rsp), /* USE_REG_RSP */
60 RT_OFFSETOF(CPUMCTXCORE, rbp), /* USE_REG_RBP */
61 RT_OFFSETOF(CPUMCTXCORE, rsi), /* USE_REG_RSI */
62 RT_OFFSETOF(CPUMCTXCORE, rdi), /* USE_REG_RDI */
63 RT_OFFSETOF(CPUMCTXCORE, r8), /* USE_REG_R8 */
64 RT_OFFSETOF(CPUMCTXCORE, r9), /* USE_REG_R9 */
65 RT_OFFSETOF(CPUMCTXCORE, r10), /* USE_REG_R10 */
66 RT_OFFSETOF(CPUMCTXCORE, r11), /* USE_REG_R11 */
67 RT_OFFSETOF(CPUMCTXCORE, r12), /* USE_REG_R12 */
68 RT_OFFSETOF(CPUMCTXCORE, r13), /* USE_REG_R13 */
69 RT_OFFSETOF(CPUMCTXCORE, r14), /* USE_REG_R14 */
70 RT_OFFSETOF(CPUMCTXCORE, r15) /* USE_REG_R15 */
71};
72
73/**
74 * Macro for accessing 64-bit general purpose registers in CPUMCTXCORE structure.
75 */
76#define DIS_READ_REG64(p, idx) (*(uint64_t *)((char *)(p) + g_aReg64Index[idx]))
77#define DIS_WRITE_REG64(p, idx, val) (*(uint64_t *)((char *)(p) + g_aReg64Index[idx]) = val)
78#define DIS_PTR_REG64(p, idx) ( (uint64_t *)((char *)(p) + g_aReg64Index[idx]))
79
80/**
81 * Array for accessing 32-bit general registers in VMMREGFRAME structure
82 * by register's index from disasm.
83 */
84static const unsigned g_aReg32Index[] =
85{
86 RT_OFFSETOF(CPUMCTXCORE, eax), /* USE_REG_EAX */
87 RT_OFFSETOF(CPUMCTXCORE, ecx), /* USE_REG_ECX */
88 RT_OFFSETOF(CPUMCTXCORE, edx), /* USE_REG_EDX */
89 RT_OFFSETOF(CPUMCTXCORE, ebx), /* USE_REG_EBX */
90 RT_OFFSETOF(CPUMCTXCORE, esp), /* USE_REG_ESP */
91 RT_OFFSETOF(CPUMCTXCORE, ebp), /* USE_REG_EBP */
92 RT_OFFSETOF(CPUMCTXCORE, esi), /* USE_REG_ESI */
93 RT_OFFSETOF(CPUMCTXCORE, edi), /* USE_REG_EDI */
94 RT_OFFSETOF(CPUMCTXCORE, r8), /* USE_REG_R8D */
95 RT_OFFSETOF(CPUMCTXCORE, r9), /* USE_REG_R9D */
96 RT_OFFSETOF(CPUMCTXCORE, r10), /* USE_REG_R10D */
97 RT_OFFSETOF(CPUMCTXCORE, r11), /* USE_REG_R11D */
98 RT_OFFSETOF(CPUMCTXCORE, r12), /* USE_REG_R12D */
99 RT_OFFSETOF(CPUMCTXCORE, r13), /* USE_REG_R13D */
100 RT_OFFSETOF(CPUMCTXCORE, r14), /* USE_REG_R14D */
101 RT_OFFSETOF(CPUMCTXCORE, r15) /* USE_REG_R15D */
102};
103
104/**
105 * Macro for accessing 32-bit general purpose registers in CPUMCTXCORE structure.
106 */
107#define DIS_READ_REG32(p, idx) (*(uint32_t *)((char *)(p) + g_aReg32Index[idx]))
108/* From http://www.cs.cmu.edu/~fp/courses/15213-s06/misc/asm64-handout.pdf:
109 * ``Perhaps unexpectedly, instructions that move or generate 32-bit register
110 * values also set the upper 32 bits of the register to zero. Consequently
111 * there is no need for an instruction movzlq.''
112 */
113#define DIS_WRITE_REG32(p, idx, val) (*(uint64_t *)((char *)(p) + g_aReg32Index[idx]) = (uint32_t)val)
114#define DIS_PTR_REG32(p, idx) ( (uint32_t *)((char *)(p) + g_aReg32Index[idx]))
115
116/**
117 * Array for accessing 16-bit general registers in CPUMCTXCORE structure
118 * by register's index from disasm.
119 */
120static const unsigned g_aReg16Index[] =
121{
122 RT_OFFSETOF(CPUMCTXCORE, eax), /* USE_REG_AX */
123 RT_OFFSETOF(CPUMCTXCORE, ecx), /* USE_REG_CX */
124 RT_OFFSETOF(CPUMCTXCORE, edx), /* USE_REG_DX */
125 RT_OFFSETOF(CPUMCTXCORE, ebx), /* USE_REG_BX */
126 RT_OFFSETOF(CPUMCTXCORE, esp), /* USE_REG_SP */
127 RT_OFFSETOF(CPUMCTXCORE, ebp), /* USE_REG_BP */
128 RT_OFFSETOF(CPUMCTXCORE, esi), /* USE_REG_SI */
129 RT_OFFSETOF(CPUMCTXCORE, edi), /* USE_REG_DI */
130 RT_OFFSETOF(CPUMCTXCORE, r8), /* USE_REG_R8W */
131 RT_OFFSETOF(CPUMCTXCORE, r9), /* USE_REG_R9W */
132 RT_OFFSETOF(CPUMCTXCORE, r10), /* USE_REG_R10W */
133 RT_OFFSETOF(CPUMCTXCORE, r11), /* USE_REG_R11W */
134 RT_OFFSETOF(CPUMCTXCORE, r12), /* USE_REG_R12W */
135 RT_OFFSETOF(CPUMCTXCORE, r13), /* USE_REG_R13W */
136 RT_OFFSETOF(CPUMCTXCORE, r14), /* USE_REG_R14W */
137 RT_OFFSETOF(CPUMCTXCORE, r15) /* USE_REG_R15W */
138};
139
140/**
141 * Macro for accessing 16-bit general purpose registers in CPUMCTXCORE structure.
142 */
143#define DIS_READ_REG16(p, idx) (*(uint16_t *)((char *)(p) + g_aReg16Index[idx]))
144#define DIS_WRITE_REG16(p, idx, val) (*(uint16_t *)((char *)(p) + g_aReg16Index[idx]) = val)
145#define DIS_PTR_REG16(p, idx) ( (uint16_t *)((char *)(p) + g_aReg16Index[idx]))
146
147/**
148 * Array for accessing 8-bit general registers in CPUMCTXCORE structure
149 * by register's index from disasm.
150 */
151static const unsigned g_aReg8Index[] =
152{
153 RT_OFFSETOF(CPUMCTXCORE, eax), /* USE_REG_AL */
154 RT_OFFSETOF(CPUMCTXCORE, ecx), /* USE_REG_CL */
155 RT_OFFSETOF(CPUMCTXCORE, edx), /* USE_REG_DL */
156 RT_OFFSETOF(CPUMCTXCORE, ebx), /* USE_REG_BL */
157 RT_OFFSETOF_ADD(CPUMCTXCORE, eax, 1), /* USE_REG_AH */
158 RT_OFFSETOF_ADD(CPUMCTXCORE, ecx, 1), /* USE_REG_CH */
159 RT_OFFSETOF_ADD(CPUMCTXCORE, edx, 1), /* USE_REG_DH */
160 RT_OFFSETOF_ADD(CPUMCTXCORE, ebx, 1), /* USE_REG_BH */
161 RT_OFFSETOF(CPUMCTXCORE, r8), /* USE_REG_R8B */
162 RT_OFFSETOF(CPUMCTXCORE, r9), /* USE_REG_R9B */
163 RT_OFFSETOF(CPUMCTXCORE, r10), /* USE_REG_R10B*/
164 RT_OFFSETOF(CPUMCTXCORE, r11), /* USE_REG_R11B */
165 RT_OFFSETOF(CPUMCTXCORE, r12), /* USE_REG_R12B */
166 RT_OFFSETOF(CPUMCTXCORE, r13), /* USE_REG_R13B */
167 RT_OFFSETOF(CPUMCTXCORE, r14), /* USE_REG_R14B */
168 RT_OFFSETOF(CPUMCTXCORE, r15), /* USE_REG_R15B */
169 RT_OFFSETOF(CPUMCTXCORE, esp), /* USE_REG_SPL; with REX prefix only */
170 RT_OFFSETOF(CPUMCTXCORE, ebp), /* USE_REG_BPL; with REX prefix only */
171 RT_OFFSETOF(CPUMCTXCORE, esi), /* USE_REG_SIL; with REX prefix only */
172 RT_OFFSETOF(CPUMCTXCORE, edi) /* USE_REG_DIL; with REX prefix only */
173};
174
175/**
176 * Macro for accessing 8-bit general purpose registers in CPUMCTXCORE structure.
177 */
178#define DIS_READ_REG8(p, idx) (*(uint8_t *)((char *)(p) + g_aReg8Index[idx]))
179#define DIS_WRITE_REG8(p, idx, val) (*(uint8_t *)((char *)(p) + g_aReg8Index[idx]) = val)
180#define DIS_PTR_REG8(p, idx) ( (uint8_t *)((char *)(p) + g_aReg8Index[idx]))
181
182/**
183 * Array for accessing segment registers in CPUMCTXCORE structure
184 * by register's index from disasm.
185 */
186static const unsigned g_aRegSegIndex[] =
187{
188 RT_OFFSETOF(CPUMCTXCORE, es), /* DIS_SELREG_ES */
189 RT_OFFSETOF(CPUMCTXCORE, cs), /* DIS_SELREG_CS */
190 RT_OFFSETOF(CPUMCTXCORE, ss), /* DIS_SELREG_SS */
191 RT_OFFSETOF(CPUMCTXCORE, ds), /* DIS_SELREG_DS */
192 RT_OFFSETOF(CPUMCTXCORE, fs), /* DIS_SELREG_FS */
193 RT_OFFSETOF(CPUMCTXCORE, gs) /* DIS_SELREG_GS */
194};
195
196static const unsigned g_aRegHidSegIndex[] =
197{
198 RT_OFFSETOF(CPUMCTXCORE, esHid), /* DIS_SELREG_ES */
199 RT_OFFSETOF(CPUMCTXCORE, csHid), /* DIS_SELREG_CS */
200 RT_OFFSETOF(CPUMCTXCORE, ssHid), /* DIS_SELREG_SS */
201 RT_OFFSETOF(CPUMCTXCORE, dsHid), /* DIS_SELREG_DS */
202 RT_OFFSETOF(CPUMCTXCORE, fsHid), /* DIS_SELREG_FS */
203 RT_OFFSETOF(CPUMCTXCORE, gsHid) /* DIS_SELREG_GS */
204};
205
206/**
207 * Macro for accessing segment registers in CPUMCTXCORE structure.
208 */
209#define DIS_READ_REGSEG(p, idx) (*((uint16_t *)((char *)(p) + g_aRegSegIndex[idx])))
210#define DIS_WRITE_REGSEG(p, idx, val) (*((uint16_t *)((char *)(p) + g_aRegSegIndex[idx])) = val)
211
212//*****************************************************************************
213//*****************************************************************************
214DISDECL(int) DISGetParamSize(PDISCPUSTATE pCpu, POP_PARAMETER pParam)
215{
216 int subtype = OP_PARM_VSUBTYPE(pParam->param);
217
218 if (subtype == OP_PARM_v)
219 {
220 switch(pCpu->opmode)
221 {
222 case CPUMODE_32BIT:
223 subtype = OP_PARM_d;
224 break;
225 case CPUMODE_64BIT:
226 subtype = OP_PARM_q;
227 break;
228 case CPUMODE_16BIT:
229 subtype = OP_PARM_w;
230 break;
231 default:
232 /* make gcc happy */
233 break;
234 }
235 }
236
237 switch(subtype)
238 {
239 case OP_PARM_b:
240 return 1;
241
242 case OP_PARM_w:
243 return 2;
244
245 case OP_PARM_d:
246 return 4;
247
248 case OP_PARM_q:
249 case OP_PARM_dq:
250 return 8;
251
252 case OP_PARM_p: /* far pointer */
253 if (pCpu->addrmode == CPUMODE_32BIT)
254 return 6; /* 16:32 */
255 else
256 if (pCpu->addrmode == CPUMODE_64BIT)
257 return 12; /* 16:64 */
258 else
259 return 4; /* 16:16 */
260
261 default:
262 if (pParam->size)
263 return pParam->size;
264 else //@todo dangerous!!!
265 return 4;
266 }
267}
268//*****************************************************************************
269//*****************************************************************************
270DISDECL(DIS_SELREG) DISDetectSegReg(PDISCPUSTATE pCpu, POP_PARAMETER pParam)
271{
272 if (pCpu->prefix & PREFIX_SEG)
273 {
274 /* Use specified SEG: prefix. */
275 return pCpu->enmPrefixSeg;
276 }
277 else
278 {
279 /* Guess segment register by parameter type. */
280 if (pParam->flags & (USE_REG_GEN32|USE_REG_GEN64|USE_REG_GEN16))
281 {
282 AssertCompile(USE_REG_ESP == USE_REG_RSP);
283 AssertCompile(USE_REG_EBP == USE_REG_RBP);
284 AssertCompile(USE_REG_ESP == USE_REG_SP);
285 AssertCompile(USE_REG_EBP == USE_REG_BP);
286 if (pParam->base.reg_gen == USE_REG_ESP || pParam->base.reg_gen == USE_REG_EBP)
287 return DIS_SELREG_SS;
288 }
289 /* Default is use DS: for data access. */
290 return DIS_SELREG_DS;
291 }
292}
293//*****************************************************************************
294//*****************************************************************************
295DISDECL(uint8_t) DISQuerySegPrefixByte(PDISCPUSTATE pCpu)
296{
297 Assert(pCpu->prefix & PREFIX_SEG);
298 switch(pCpu->enmPrefixSeg)
299 {
300 case DIS_SELREG_ES:
301 return 0x26;
302 case DIS_SELREG_CS:
303 return 0x2E;
304 case DIS_SELREG_SS:
305 return 0x36;
306 case DIS_SELREG_DS:
307 return 0x3E;
308 case DIS_SELREG_FS:
309 return 0x64;
310 case DIS_SELREG_GS:
311 return 0x65;
312 default:
313 AssertFailed();
314 return 0;
315 }
316}
317
318
319/**
320 * Returns the value of the specified 8 bits general purpose register
321 *
322 */
323DISDECL(int) DISFetchReg8(PCCPUMCTXCORE pCtx, unsigned reg8, uint8_t *pVal)
324{
325 AssertReturn(reg8 < RT_ELEMENTS(g_aReg8Index), VERR_INVALID_PARAMETER);
326
327 *pVal = DIS_READ_REG8(pCtx, reg8);
328 return VINF_SUCCESS;
329}
330
331/**
332 * Returns the value of the specified 16 bits general purpose register
333 *
334 */
335DISDECL(int) DISFetchReg16(PCCPUMCTXCORE pCtx, unsigned reg16, uint16_t *pVal)
336{
337 AssertReturn(reg16 < RT_ELEMENTS(g_aReg16Index), VERR_INVALID_PARAMETER);
338
339 *pVal = DIS_READ_REG16(pCtx, reg16);
340 return VINF_SUCCESS;
341}
342
343/**
344 * Returns the value of the specified 32 bits general purpose register
345 *
346 */
347DISDECL(int) DISFetchReg32(PCCPUMCTXCORE pCtx, unsigned reg32, uint32_t *pVal)
348{
349 AssertReturn(reg32 < RT_ELEMENTS(g_aReg32Index), VERR_INVALID_PARAMETER);
350
351 *pVal = DIS_READ_REG32(pCtx, reg32);
352 return VINF_SUCCESS;
353}
354
355/**
356 * Returns the value of the specified 64 bits general purpose register
357 *
358 */
359DISDECL(int) DISFetchReg64(PCCPUMCTXCORE pCtx, unsigned reg64, uint64_t *pVal)
360{
361 AssertReturn(reg64 < RT_ELEMENTS(g_aReg64Index), VERR_INVALID_PARAMETER);
362
363 *pVal = DIS_READ_REG64(pCtx, reg64);
364 return VINF_SUCCESS;
365}
366
367/**
368 * Returns the pointer to the specified 8 bits general purpose register
369 *
370 */
371DISDECL(int) DISPtrReg8(PCPUMCTXCORE pCtx, unsigned reg8, uint8_t **ppReg)
372{
373 AssertReturn(reg8 < RT_ELEMENTS(g_aReg8Index), VERR_INVALID_PARAMETER);
374
375 *ppReg = DIS_PTR_REG8(pCtx, reg8);
376 return VINF_SUCCESS;
377}
378
379/**
380 * Returns the pointer to the specified 16 bits general purpose register
381 *
382 */
383DISDECL(int) DISPtrReg16(PCPUMCTXCORE pCtx, unsigned reg16, uint16_t **ppReg)
384{
385 AssertReturn(reg16 < RT_ELEMENTS(g_aReg16Index), VERR_INVALID_PARAMETER);
386
387 *ppReg = DIS_PTR_REG16(pCtx, reg16);
388 return VINF_SUCCESS;
389}
390
391/**
392 * Returns the pointer to the specified 32 bits general purpose register
393 *
394 */
395DISDECL(int) DISPtrReg32(PCPUMCTXCORE pCtx, unsigned reg32, uint32_t **ppReg)
396{
397 AssertReturn(reg32 < RT_ELEMENTS(g_aReg32Index), VERR_INVALID_PARAMETER);
398
399 *ppReg = DIS_PTR_REG32(pCtx, reg32);
400 return VINF_SUCCESS;
401}
402
403/**
404 * Returns the pointer to the specified 64 bits general purpose register
405 *
406 */
407DISDECL(int) DISPtrReg64(PCPUMCTXCORE pCtx, unsigned reg64, uint64_t **ppReg)
408{
409 AssertReturn(reg64 < RT_ELEMENTS(g_aReg64Index), VERR_INVALID_PARAMETER);
410
411 *ppReg = DIS_PTR_REG64(pCtx, reg64);
412 return VINF_SUCCESS;
413}
414
415/**
416 * Returns the value of the specified segment register
417 *
418 */
419DISDECL(int) DISFetchRegSeg(PCCPUMCTXCORE pCtx, DIS_SELREG sel, RTSEL *pVal)
420{
421 AssertReturn((unsigned)sel < RT_ELEMENTS(g_aRegSegIndex), VERR_INVALID_PARAMETER);
422
423 AssertCompile(sizeof(uint16_t) == sizeof(RTSEL));
424 *pVal = DIS_READ_REGSEG(pCtx, sel);
425 return VINF_SUCCESS;
426}
427
428/**
429 * Returns the value of the specified segment register including a pointer to the hidden register in the supplied cpu context
430 *
431 */
432DISDECL(int) DISFetchRegSegEx(PCCPUMCTXCORE pCtx, DIS_SELREG sel, RTSEL *pVal, CPUMSELREGHID **ppSelHidReg)
433{
434 AssertReturn((unsigned)sel < RT_ELEMENTS(g_aRegSegIndex), VERR_INVALID_PARAMETER);
435
436 AssertCompile(sizeof(uint16_t) == sizeof(RTSEL));
437 *pVal = DIS_READ_REGSEG(pCtx, sel);
438 *ppSelHidReg = (CPUMSELREGHID *)((char *)pCtx + g_aRegHidSegIndex[sel]);
439 return VINF_SUCCESS;
440}
441
442/**
443 * Updates the value of the specified 64 bits general purpose register
444 *
445 */
446DISDECL(int) DISWriteReg64(PCPUMCTXCORE pRegFrame, unsigned reg64, uint64_t val64)
447{
448 AssertReturn(reg64 < RT_ELEMENTS(g_aReg64Index), VERR_INVALID_PARAMETER);
449
450 DIS_WRITE_REG64(pRegFrame, reg64, val64);
451 return VINF_SUCCESS;
452}
453
454/**
455 * Updates the value of the specified 32 bits general purpose register
456 *
457 */
458DISDECL(int) DISWriteReg32(PCPUMCTXCORE pRegFrame, unsigned reg32, uint32_t val32)
459{
460 AssertReturn(reg32 < RT_ELEMENTS(g_aReg32Index), VERR_INVALID_PARAMETER);
461
462 DIS_WRITE_REG32(pRegFrame, reg32, val32);
463 return VINF_SUCCESS;
464}
465
466/**
467 * Updates the value of the specified 16 bits general purpose register
468 *
469 */
470DISDECL(int) DISWriteReg16(PCPUMCTXCORE pRegFrame, unsigned reg16, uint16_t val16)
471{
472 AssertReturn(reg16 < RT_ELEMENTS(g_aReg16Index), VERR_INVALID_PARAMETER);
473
474 DIS_WRITE_REG16(pRegFrame, reg16, val16);
475 return VINF_SUCCESS;
476}
477
478/**
479 * Updates the specified 8 bits general purpose register
480 *
481 */
482DISDECL(int) DISWriteReg8(PCPUMCTXCORE pRegFrame, unsigned reg8, uint8_t val8)
483{
484 AssertReturn(reg8 < RT_ELEMENTS(g_aReg8Index), VERR_INVALID_PARAMETER);
485
486 DIS_WRITE_REG8(pRegFrame, reg8, val8);
487 return VINF_SUCCESS;
488}
489
490/**
491 * Updates the specified segment register
492 *
493 */
494DISDECL(int) DISWriteRegSeg(PCPUMCTXCORE pCtx, DIS_SELREG sel, RTSEL val)
495{
496 AssertReturn((unsigned)sel < RT_ELEMENTS(g_aRegSegIndex), VERR_INVALID_PARAMETER);
497
498 AssertCompile(sizeof(uint16_t) == sizeof(RTSEL));
499 DIS_WRITE_REGSEG(pCtx, sel, val);
500 return VINF_SUCCESS;
501}
502
503/**
504 * Returns the value of the parameter in pParam
505 *
506 * @returns VBox error code
507 * @param pCtx CPU context structure pointer
508 * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
509 * set correctly.
510 * @param pParam Pointer to the parameter to parse
511 * @param pParamVal Pointer to parameter value (OUT)
512 * @param parmtype Parameter type
513 *
514 * @note Currently doesn't handle FPU/XMM/MMX/3DNow! parameters correctly!!
515 *
516 */
517DISDECL(int) DISQueryParamVal(PCPUMCTXCORE pCtx, PDISCPUSTATE pCpu, POP_PARAMETER pParam, POP_PARAMVAL pParamVal, PARAM_TYPE parmtype)
518{
519 memset(pParamVal, 0, sizeof(*pParamVal));
520
521 if (DIS_IS_EFFECTIVE_ADDR(pParam->flags))
522 {
523 // Effective address
524 pParamVal->type = PARMTYPE_ADDRESS;
525 pParamVal->size = pParam->size;
526
527 if (pParam->flags & USE_BASE)
528 {
529 if (pParam->flags & USE_REG_GEN8)
530 {
531 pParamVal->flags |= PARAM_VAL8;
532 if (RT_FAILURE(DISFetchReg8(pCtx, pParam->base.reg_gen, &pParamVal->val.val8))) return VERR_INVALID_PARAMETER;
533 }
534 else
535 if (pParam->flags & USE_REG_GEN16)
536 {
537 pParamVal->flags |= PARAM_VAL16;
538 if (RT_FAILURE(DISFetchReg16(pCtx, pParam->base.reg_gen, &pParamVal->val.val16))) return VERR_INVALID_PARAMETER;
539 }
540 else
541 if (pParam->flags & USE_REG_GEN32)
542 {
543 pParamVal->flags |= PARAM_VAL32;
544 if (RT_FAILURE(DISFetchReg32(pCtx, pParam->base.reg_gen, &pParamVal->val.val32))) return VERR_INVALID_PARAMETER;
545 }
546 else
547 if (pParam->flags & USE_REG_GEN64)
548 {
549 pParamVal->flags |= PARAM_VAL64;
550 if (RT_FAILURE(DISFetchReg64(pCtx, pParam->base.reg_gen, &pParamVal->val.val64))) return VERR_INVALID_PARAMETER;
551 }
552 else
553 {
554 AssertFailed();
555 return VERR_INVALID_PARAMETER;
556 }
557 }
558 // Note that scale implies index (SIB byte)
559 if (pParam->flags & USE_INDEX)
560 {
561 if (pParam->flags & USE_REG_GEN16)
562 {
563 uint16_t val16;
564
565 pParamVal->flags |= PARAM_VAL16;
566 if (RT_FAILURE(DISFetchReg16(pCtx, pParam->index.reg_gen, &val16))) return VERR_INVALID_PARAMETER;
567
568 Assert(!(pParam->flags & USE_SCALE)); /* shouldn't be possible in 16 bits mode */
569
570 pParamVal->val.val16 += val16;
571 }
572 else
573 if (pParam->flags & USE_REG_GEN32)
574 {
575 uint32_t val32;
576
577 pParamVal->flags |= PARAM_VAL32;
578 if (RT_FAILURE(DISFetchReg32(pCtx, pParam->index.reg_gen, &val32))) return VERR_INVALID_PARAMETER;
579
580 if (pParam->flags & USE_SCALE)
581 val32 *= pParam->scale;
582
583 pParamVal->val.val32 += val32;
584 }
585 else
586 if (pParam->flags & USE_REG_GEN64)
587 {
588 uint64_t val64;
589
590 pParamVal->flags |= PARAM_VAL64;
591 if (RT_FAILURE(DISFetchReg64(pCtx, pParam->index.reg_gen, &val64))) return VERR_INVALID_PARAMETER;
592
593 if (pParam->flags & USE_SCALE)
594 val64 *= pParam->scale;
595
596 pParamVal->val.val64 += val64;
597 }
598 else
599 AssertFailed();
600 }
601
602 if (pParam->flags & USE_DISPLACEMENT8)
603 {
604 if (pCpu->mode == CPUMODE_32BIT)
605 pParamVal->val.val32 += (int32_t)pParam->disp8;
606 else
607 if (pCpu->mode == CPUMODE_64BIT)
608 pParamVal->val.val64 += (int64_t)pParam->disp8;
609 else
610 pParamVal->val.val16 += (int16_t)pParam->disp8;
611 }
612 else
613 if (pParam->flags & USE_DISPLACEMENT16)
614 {
615 if (pCpu->mode == CPUMODE_32BIT)
616 pParamVal->val.val32 += (int32_t)pParam->disp16;
617 else
618 if (pCpu->mode == CPUMODE_64BIT)
619 pParamVal->val.val64 += (int64_t)pParam->disp16;
620 else
621 pParamVal->val.val16 += pParam->disp16;
622 }
623 else
624 if (pParam->flags & USE_DISPLACEMENT32)
625 {
626 if (pCpu->mode == CPUMODE_32BIT)
627 pParamVal->val.val32 += pParam->disp32;
628 else
629 pParamVal->val.val64 += pParam->disp32;
630 }
631 else
632 if (pParam->flags & USE_DISPLACEMENT64)
633 {
634 Assert(pCpu->mode == CPUMODE_64BIT);
635 pParamVal->val.val64 += (int64_t)pParam->disp64;
636 }
637 else
638 if (pParam->flags & USE_RIPDISPLACEMENT32)
639 {
640 Assert(pCpu->mode == CPUMODE_64BIT);
641 /* Relative to the RIP of the next instruction. */
642 pParamVal->val.val64 += pParam->disp32 + pCtx->rip + pCpu->opsize;
643 }
644 return VINF_SUCCESS;
645 }
646
647 if (pParam->flags & (USE_REG_GEN8|USE_REG_GEN16|USE_REG_GEN32|USE_REG_GEN64|USE_REG_FP|USE_REG_MMX|USE_REG_XMM|USE_REG_CR|USE_REG_DBG|USE_REG_SEG|USE_REG_TEST))
648 {
649 if (parmtype == PARAM_DEST)
650 {
651 // Caller needs to interpret the register according to the instruction (source/target, special value etc)
652 pParamVal->type = PARMTYPE_REGISTER;
653 pParamVal->size = pParam->size;
654 return VINF_SUCCESS;
655 }
656 //else PARAM_SOURCE
657
658 pParamVal->type = PARMTYPE_IMMEDIATE;
659
660 if (pParam->flags & USE_REG_GEN8)
661 {
662 pParamVal->flags |= PARAM_VAL8;
663 pParamVal->size = sizeof(uint8_t);
664 if (RT_FAILURE(DISFetchReg8(pCtx, pParam->base.reg_gen, &pParamVal->val.val8))) return VERR_INVALID_PARAMETER;
665 }
666 else
667 if (pParam->flags & USE_REG_GEN16)
668 {
669 pParamVal->flags |= PARAM_VAL16;
670 pParamVal->size = sizeof(uint16_t);
671 if (RT_FAILURE(DISFetchReg16(pCtx, pParam->base.reg_gen, &pParamVal->val.val16))) return VERR_INVALID_PARAMETER;
672 }
673 else
674 if (pParam->flags & USE_REG_GEN32)
675 {
676 pParamVal->flags |= PARAM_VAL32;
677 pParamVal->size = sizeof(uint32_t);
678 if (RT_FAILURE(DISFetchReg32(pCtx, pParam->base.reg_gen, &pParamVal->val.val32))) return VERR_INVALID_PARAMETER;
679 }
680 else
681 if (pParam->flags & USE_REG_GEN64)
682 {
683 pParamVal->flags |= PARAM_VAL64;
684 pParamVal->size = sizeof(uint64_t);
685 if (RT_FAILURE(DISFetchReg64(pCtx, pParam->base.reg_gen, &pParamVal->val.val64))) return VERR_INVALID_PARAMETER;
686 }
687 else
688 {
689 // Caller needs to interpret the register according to the instruction (source/target, special value etc)
690 pParamVal->type = PARMTYPE_REGISTER;
691 }
692 Assert(!(pParam->flags & USE_IMMEDIATE));
693 return VINF_SUCCESS;
694 }
695
696 if (pParam->flags & USE_IMMEDIATE)
697 {
698 pParamVal->type = PARMTYPE_IMMEDIATE;
699 if (pParam->flags & (USE_IMMEDIATE8|USE_IMMEDIATE8_REL))
700 {
701 pParamVal->flags |= PARAM_VAL8;
702 if (pParam->size == 2)
703 {
704 pParamVal->size = sizeof(uint16_t);
705 pParamVal->val.val16 = (uint8_t)pParam->parval;
706 }
707 else
708 {
709 pParamVal->size = sizeof(uint8_t);
710 pParamVal->val.val8 = (uint8_t)pParam->parval;
711 }
712 }
713 else
714 if (pParam->flags & (USE_IMMEDIATE16|USE_IMMEDIATE16_REL|USE_IMMEDIATE_ADDR_0_16|USE_IMMEDIATE16_SX8))
715 {
716 pParamVal->flags |= PARAM_VAL16;
717 pParamVal->size = sizeof(uint16_t);
718 pParamVal->val.val16 = (uint16_t)pParam->parval;
719 AssertMsg(pParamVal->size == pParam->size || ((pParam->size == 1) && (pParam->flags & USE_IMMEDIATE16_SX8)), ("pParamVal->size %d vs %d EIP=%RX32\n", pParamVal->size, pParam->size, pCtx->eip) );
720 }
721 else
722 if (pParam->flags & (USE_IMMEDIATE32|USE_IMMEDIATE32_REL|USE_IMMEDIATE_ADDR_0_32|USE_IMMEDIATE32_SX8))
723 {
724 pParamVal->flags |= PARAM_VAL32;
725 pParamVal->size = sizeof(uint32_t);
726 pParamVal->val.val32 = (uint32_t)pParam->parval;
727 Assert(pParamVal->size == pParam->size || ((pParam->size == 1) && (pParam->flags & USE_IMMEDIATE32_SX8)) );
728 }
729 else
730 if (pParam->flags & (USE_IMMEDIATE64 | USE_IMMEDIATE64_REL | USE_IMMEDIATE64_SX8))
731 {
732 pParamVal->flags |= PARAM_VAL64;
733 pParamVal->size = sizeof(uint64_t);
734 pParamVal->val.val64 = pParam->parval;
735 Assert(pParamVal->size == pParam->size || ((pParam->size == 1) && (pParam->flags & USE_IMMEDIATE64_SX8)) );
736 }
737 else
738 if (pParam->flags & (USE_IMMEDIATE_ADDR_16_16))
739 {
740 pParamVal->flags |= PARAM_VALFARPTR16;
741 pParamVal->size = sizeof(uint16_t)*2;
742 pParamVal->val.farptr.sel = (uint16_t)RT_LOWORD(pParam->parval >> 16);
743 pParamVal->val.farptr.offset = (uint32_t)RT_LOWORD(pParam->parval);
744 Assert(pParamVal->size == pParam->size);
745 }
746 else
747 if (pParam->flags & (USE_IMMEDIATE_ADDR_16_32))
748 {
749 pParamVal->flags |= PARAM_VALFARPTR32;
750 pParamVal->size = sizeof(uint16_t) + sizeof(uint32_t);
751 pParamVal->val.farptr.sel = (uint16_t)RT_LOWORD(pParam->parval >> 32);
752 pParamVal->val.farptr.offset = (uint32_t)(pParam->parval & 0xFFFFFFFF);
753 Assert(pParam->size == 8);
754 }
755 }
756 return VINF_SUCCESS;
757}
758
759/**
760 * Returns the pointer to a register of the parameter in pParam. We need this
761 * pointer when an interpreted instruction updates a register as a side effect.
762 * In CMPXCHG we know that only [r/e]ax is updated, but with XADD this could
763 * be every register.
764 *
765 * @returns VBox error code
766 * @param pCtx CPU context structure pointer
767 * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
768 * set correctly.
769 * @param pParam Pointer to the parameter to parse
770 * @param pReg Pointer to parameter value (OUT)
771 * @param cbsize Parameter size (OUT)
772 *
773 * @note Currently doesn't handle FPU/XMM/MMX/3DNow! parameters correctly!!
774 *
775 */
776DISDECL(int) DISQueryParamRegPtr(PCPUMCTXCORE pCtx, PDISCPUSTATE pCpu, POP_PARAMETER pParam, void **ppReg, size_t *pcbSize)
777{
778 if (pParam->flags & (USE_REG_GEN8|USE_REG_GEN16|USE_REG_GEN32|USE_REG_FP|USE_REG_MMX|USE_REG_XMM|USE_REG_CR|USE_REG_DBG|USE_REG_SEG|USE_REG_TEST))
779 {
780 if (pParam->flags & USE_REG_GEN8)
781 {
782 uint8_t *pu8Reg;
783 if (RT_SUCCESS(DISPtrReg8(pCtx, pParam->base.reg_gen, &pu8Reg)))
784 {
785 *pcbSize = sizeof(uint8_t);
786 *ppReg = (void *)pu8Reg;
787 return VINF_SUCCESS;
788 }
789 }
790 else
791 if (pParam->flags & USE_REG_GEN16)
792 {
793 uint16_t *pu16Reg;
794 if (RT_SUCCESS(DISPtrReg16(pCtx, pParam->base.reg_gen, &pu16Reg)))
795 {
796 *pcbSize = sizeof(uint16_t);
797 *ppReg = (void *)pu16Reg;
798 return VINF_SUCCESS;
799 }
800 }
801 else
802 if (pParam->flags & USE_REG_GEN32)
803 {
804 uint32_t *pu32Reg;
805 if (RT_SUCCESS(DISPtrReg32(pCtx, pParam->base.reg_gen, &pu32Reg)))
806 {
807 *pcbSize = sizeof(uint32_t);
808 *ppReg = (void *)pu32Reg;
809 return VINF_SUCCESS;
810 }
811 }
812 else
813 if (pParam->flags & USE_REG_GEN64)
814 {
815 uint64_t *pu64Reg;
816 if (RT_SUCCESS(DISPtrReg64(pCtx, pParam->base.reg_gen, &pu64Reg)))
817 {
818 *pcbSize = sizeof(uint64_t);
819 *ppReg = (void *)pu64Reg;
820 return VINF_SUCCESS;
821 }
822 }
823 }
824 return VERR_INVALID_PARAMETER;
825}
826//*****************************************************************************
827//*****************************************************************************
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