VirtualBox

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

Last change on this file since 9923 was 9923, checked in by vboxsync, 16 years ago

Remove comma

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