VirtualBox

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

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

Deal with sign extending bytes to uint64_t.

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

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