VirtualBox

source: vbox/trunk/include/VBox/dis.h@ 41729

Last change on this file since 41729 was 41729, checked in by vboxsync, 13 years ago

DISQueryParamVal: renamed related types and constants and reduced the param structure by 8-12 bytes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.6 KB
Line 
1/** @file
2 * DIS - The VirtualBox Disassembler.
3 */
4
5/*
6 * Copyright (C) 2006-2012 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.215389.xyz. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_dis_h
27#define ___VBox_dis_h
28
29#include <VBox/types.h>
30#include <VBox/disopcode.h>
31#include <iprt/assert.h>
32
33
34RT_C_DECLS_BEGIN
35
36
37/**
38 * CPU mode flags (DISCPUSTATE::mode).
39 */
40typedef enum DISCPUMODE
41{
42 DISCPUMODE_INVALID = 0,
43 DISCPUMODE_16BIT,
44 DISCPUMODE_32BIT,
45 DISCPUMODE_64BIT,
46 /** hack forcing the size of the enum to 32-bits. */
47 DISCPUMODE_MAKE_32BIT_HACK = 0x7fffffff
48} DISCPUMODE;
49
50/** @name Prefix byte flags (DISCPUSTATE::prefix_rex).
51 * @{
52 */
53#define DISPREFIX_NONE UINT8_C(0x00)
54/** non-default address size. */
55#define DISPREFIX_ADDRSIZE UINT8_C(0x01)
56/** non-default operand size. */
57#define DISPREFIX_OPSIZE UINT8_C(0x02)
58/** lock prefix. */
59#define DISPREFIX_LOCK UINT8_C(0x04)
60/** segment prefix. */
61#define DISPREFIX_SEG UINT8_C(0x08)
62/** rep(e) prefix (not a prefix, but we'll treat is as one). */
63#define DISPREFIX_REP UINT8_C(0x10)
64/** rep(e) prefix (not a prefix, but we'll treat is as one). */
65#define DISPREFIX_REPNE UINT8_C(0x20)
66/** REX prefix (64 bits) */
67#define DISPREFIX_REX UINT8_C(0x40)
68/** @} */
69
70/** @name 64 bits prefix byte flags (DISCPUSTATE::prefix_rex).
71 * Requires VBox/disopcode.h.
72 * @{
73 */
74#define DISPREFIX_REX_OP_2_FLAGS(a) (a - OP_PARM_REX_START)
75#define DISPREFIX_REX_FLAGS DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX)
76#define DISPREFIX_REX_FLAGS_B DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_B)
77#define DISPREFIX_REX_FLAGS_X DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_X)
78#define DISPREFIX_REX_FLAGS_XB DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_XB)
79#define DISPREFIX_REX_FLAGS_R DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_R)
80#define DISPREFIX_REX_FLAGS_RB DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_RB)
81#define DISPREFIX_REX_FLAGS_RX DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_RX)
82#define DISPREFIX_REX_FLAGS_RXB DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_RXB)
83#define DISPREFIX_REX_FLAGS_W DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_W)
84#define DISPREFIX_REX_FLAGS_WB DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_WB)
85#define DISPREFIX_REX_FLAGS_WX DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_WX)
86#define DISPREFIX_REX_FLAGS_WXB DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_WXB)
87#define DISPREFIX_REX_FLAGS_WR DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_WR)
88#define DISPREFIX_REX_FLAGS_WRB DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_WRB)
89#define DISPREFIX_REX_FLAGS_WRX DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_WRX)
90#define DISPREFIX_REX_FLAGS_WRXB DISPREFIX_REX_OP_2_FLAGS(OP_PARM_REX_WRXB)
91/** @} */
92
93/** @name Operand type.
94 * @{
95 */
96#define DISOPTYPE_INVALID RT_BIT_32(0)
97#define DISOPTYPE_HARMLESS RT_BIT_32(1)
98#define DISOPTYPE_CONTROLFLOW RT_BIT_32(2)
99#define DISOPTYPE_POTENTIALLY_DANGEROUS RT_BIT_32(3)
100#define DISOPTYPE_DANGEROUS RT_BIT_32(4)
101#define DISOPTYPE_PORTIO RT_BIT_32(5)
102#define DISOPTYPE_PRIVILEGED RT_BIT_32(6)
103#define DISOPTYPE_PRIVILEGED_NOTRAP RT_BIT_32(7)
104#define DISOPTYPE_UNCOND_CONTROLFLOW RT_BIT_32(8)
105#define DISOPTYPE_RELATIVE_CONTROLFLOW RT_BIT_32(9)
106#define DISOPTYPE_COND_CONTROLFLOW RT_BIT_32(10)
107#define DISOPTYPE_INTERRUPT RT_BIT_32(11)
108#define DISOPTYPE_ILLEGAL RT_BIT_32(12)
109#define DISOPTYPE_RRM_DANGEROUS RT_BIT_32(14) /**< Some additional dangerous ones when recompiling raw r0. */
110#define DISOPTYPE_RRM_DANGEROUS_16 RT_BIT_32(15) /**< Some additional dangerous ones when recompiling 16-bit raw r0. */
111#define DISOPTYPE_RRM_MASK (DISOPTYPE_RRM_DANGEROUS | DISOPTYPE_RRM_DANGEROUS_16)
112#define DISOPTYPE_INHIBIT_IRQS RT_BIT_32(16) /**< Will or can inhibit irqs (sti, pop ss, mov ss) */
113#define DISOPTYPE_PORTIO_READ RT_BIT_32(17)
114#define DISOPTYPE_PORTIO_WRITE RT_BIT_32(18)
115#define DISOPTYPE_INVALID_64 RT_BIT_32(19) /**< Invalid in 64 bits mode */
116#define DISOPTYPE_ONLY_64 RT_BIT_32(20) /**< Only valid in 64 bits mode */
117#define DISOPTYPE_DEFAULT_64_OP_SIZE RT_BIT_32(21) /**< Default 64 bits operand size */
118#define DISOPTYPE_FORCED_64_OP_SIZE RT_BIT_32(22) /**< Forced 64 bits operand size; regardless of prefix bytes */
119#define DISOPTYPE_REXB_EXTENDS_OPREG RT_BIT_32(23) /**< REX.B extends the register field in the opcode byte */
120#define DISOPTYPE_MOD_FIXED_11 RT_BIT_32(24) /**< modrm.mod is always 11b */
121#define DISOPTYPE_FORCED_32_OP_SIZE_X86 RT_BIT_32(25) /**< Forced 32 bits operand size; regardless of prefix bytes (only in 16 & 32 bits mode!) */
122#define DISOPTYPE_ALL UINT32_C(0xffffffff)
123/** @} */
124
125/** @name Parameter usage flags.
126 * @{
127 */
128#define DISUSE_BASE RT_BIT_64(0)
129#define DISUSE_INDEX RT_BIT_64(1)
130#define DISUSE_SCALE RT_BIT_64(2)
131#define DISUSE_REG_GEN8 RT_BIT_64(3)
132#define DISUSE_REG_GEN16 RT_BIT_64(4)
133#define DISUSE_REG_GEN32 RT_BIT_64(5)
134#define DISUSE_REG_GEN64 RT_BIT_64(6)
135#define DISUSE_REG_FP RT_BIT_64(7)
136#define DISUSE_REG_MMX RT_BIT_64(8)
137#define DISUSE_REG_XMM RT_BIT_64(9)
138#define DISUSE_REG_CR RT_BIT_64(10)
139#define DISUSE_REG_DBG RT_BIT_64(11)
140#define DISUSE_REG_SEG RT_BIT_64(12)
141#define DISUSE_REG_TEST RT_BIT_64(13)
142#define DISUSE_DISPLACEMENT8 RT_BIT_64(14)
143#define DISUSE_DISPLACEMENT16 RT_BIT_64(15)
144#define DISUSE_DISPLACEMENT32 RT_BIT_64(16)
145#define DISUSE_DISPLACEMENT64 RT_BIT_64(17)
146#define DISUSE_RIPDISPLACEMENT32 RT_BIT_64(18)
147#define DISUSE_IMMEDIATE8 RT_BIT_64(19)
148#define DISUSE_IMMEDIATE8_REL RT_BIT_64(20)
149#define DISUSE_IMMEDIATE16 RT_BIT_64(21)
150#define DISUSE_IMMEDIATE16_REL RT_BIT_64(22)
151#define DISUSE_IMMEDIATE32 RT_BIT_64(23)
152#define DISUSE_IMMEDIATE32_REL RT_BIT_64(24)
153#define DISUSE_IMMEDIATE64 RT_BIT_64(25)
154#define DISUSE_IMMEDIATE64_REL RT_BIT_64(26)
155#define DISUSE_IMMEDIATE_ADDR_0_32 RT_BIT_64(27)
156#define DISUSE_IMMEDIATE_ADDR_16_32 RT_BIT_64(28)
157#define DISUSE_IMMEDIATE_ADDR_0_16 RT_BIT_64(29)
158#define DISUSE_IMMEDIATE_ADDR_16_16 RT_BIT_64(30)
159/** DS:ESI */
160#define DISUSE_POINTER_DS_BASED RT_BIT_64(31)
161/** ES:EDI */
162#define DISUSE_POINTER_ES_BASED RT_BIT_64(32)
163#define DISUSE_IMMEDIATE16_SX8 RT_BIT_64(33)
164#define DISUSE_IMMEDIATE32_SX8 RT_BIT_64(34)
165#define DISUSE_IMMEDIATE64_SX8 RT_BIT_64(36)
166
167/** Mask of immediate use flags. */
168#define DISUSE_IMMEDIATE ( DISUSE_IMMEDIATE8 \
169 | DISUSE_IMMEDIATE16 \
170 | DISUSE_IMMEDIATE32 \
171 | DISUSE_IMMEDIATE64 \
172 | DISUSE_IMMEDIATE8_REL \
173 | DISUSE_IMMEDIATE16_REL \
174 | DISUSE_IMMEDIATE32_REL \
175 | DISUSE_IMMEDIATE64_REL \
176 | DISUSE_IMMEDIATE_ADDR_0_32 \
177 | DISUSE_IMMEDIATE_ADDR_16_32 \
178 | DISUSE_IMMEDIATE_ADDR_0_16 \
179 | DISUSE_IMMEDIATE_ADDR_16_16 \
180 | DISUSE_IMMEDIATE16_SX8 \
181 | DISUSE_IMMEDIATE32_SX8 \
182 | DISUSE_IMMEDIATE64_SX8)
183/** Check if the use flags indicates an effective address. */
184#define DISUSE_IS_EFFECTIVE_ADDR(a_fUseFlags) (!!( (a_fUseFlags) \
185 & ( DISUSE_BASE \
186 | DISUSE_INDEX \
187 | DISUSE_DISPLACEMENT32 \
188 | DISUSE_DISPLACEMENT64 \
189 | DISUSE_DISPLACEMENT16 \
190 | DISUSE_DISPLACEMENT8 \
191 | DISUSE_RIPDISPLACEMENT32) ))
192/** @} */
193
194/** @name 64-bit general register indexes.
195 * This matches the AMD64 register encoding. It is found used in
196 * DISOPPARAM::base.reg_gen and DISOPPARAM::index.reg_gen.
197 * @note Safe to assume same values as the 16-bit and 32-bit general registers.
198 * @{
199 */
200#define DISGREG_RAX UINT8_C(0)
201#define DISGREG_RCX UINT8_C(1)
202#define DISGREG_RDX UINT8_C(2)
203#define DISGREG_RBX UINT8_C(3)
204#define DISGREG_RSP UINT8_C(4)
205#define DISGREG_RBP UINT8_C(5)
206#define DISGREG_RSI UINT8_C(6)
207#define DISGREG_RDI UINT8_C(7)
208#define DISGREG_R8 UINT8_C(8)
209#define DISGREG_R9 UINT8_C(9)
210#define DISGREG_R10 UINT8_C(10)
211#define DISGREG_R11 UINT8_C(11)
212#define DISGREG_R12 UINT8_C(12)
213#define DISGREG_R13 UINT8_C(13)
214#define DISGREG_R14 UINT8_C(14)
215#define DISGREG_R15 UINT8_C(15)
216/** @} */
217
218/** @name 32-bit general register indexes.
219 * This matches the AMD64 register encoding. It is found used in
220 * DISOPPARAM::base.reg_gen and DISOPPARAM::index.reg_gen.
221 * @note Safe to assume same values as the 16-bit and 64-bit general registers.
222 * @{
223 */
224#define DISGREG_EAX UINT8_C(0)
225#define DISGREG_ECX UINT8_C(1)
226#define DISGREG_EDX UINT8_C(2)
227#define DISGREG_EBX UINT8_C(3)
228#define DISGREG_ESP UINT8_C(4)
229#define DISGREG_EBP UINT8_C(5)
230#define DISGREG_ESI UINT8_C(6)
231#define DISGREG_EDI UINT8_C(7)
232#define DISGREG_R8D UINT8_C(8)
233#define DISGREG_R9D UINT8_C(9)
234#define DISGREG_R10D UINT8_C(10)
235#define DISGREG_R11D UINT8_C(11)
236#define DISGREG_R12D UINT8_C(12)
237#define DISGREG_R13D UINT8_C(13)
238#define DISGREG_R14D UINT8_C(14)
239#define DISGREG_R15D UINT8_C(15)
240/** @} */
241
242/** @name 16-bit general register indexes.
243 * This matches the AMD64 register encoding. It is found used in
244 * DISOPPARAM::base.reg_gen and DISOPPARAM::index.reg_gen.
245 * @note Safe to assume same values as the 32-bit and 64-bit general registers.
246 * @{
247 */
248#define DISGREG_AX UINT8_C(0)
249#define DISGREG_CX UINT8_C(1)
250#define DISGREG_DX UINT8_C(2)
251#define DISGREG_BX UINT8_C(3)
252#define DISGREG_SP UINT8_C(4)
253#define DISGREG_BP UINT8_C(5)
254#define DISGREG_SI UINT8_C(6)
255#define DISGREG_DI UINT8_C(7)
256#define DISGREG_R8W UINT8_C(8)
257#define DISGREG_R9W UINT8_C(9)
258#define DISGREG_R10W UINT8_C(10)
259#define DISGREG_R11W UINT8_C(11)
260#define DISGREG_R12W UINT8_C(12)
261#define DISGREG_R13W UINT8_C(13)
262#define DISGREG_R14W UINT8_C(14)
263#define DISGREG_R15W UINT8_C(15)
264/** @} */
265
266/** @name 8-bit general register indexes.
267 * This mostly (?) matches the AMD64 register encoding. It is found used in
268 * DISOPPARAM::base.reg_gen and DISOPPARAM::index.reg_gen.
269 * @{
270 */
271#define DISGREG_AL UINT8_C(0)
272#define DISGREG_CL UINT8_C(1)
273#define DISGREG_DL UINT8_C(2)
274#define DISGREG_BL UINT8_C(3)
275#define DISGREG_AH UINT8_C(4)
276#define DISGREG_CH UINT8_C(5)
277#define DISGREG_DH UINT8_C(6)
278#define DISGREG_BH UINT8_C(7)
279#define DISGREG_R8B UINT8_C(8)
280#define DISGREG_R9B UINT8_C(9)
281#define DISGREG_R10B UINT8_C(10)
282#define DISGREG_R11B UINT8_C(11)
283#define DISGREG_R12B UINT8_C(12)
284#define DISGREG_R13B UINT8_C(13)
285#define DISGREG_R14B UINT8_C(14)
286#define DISGREG_R15B UINT8_C(15)
287#define DISGREG_SPL UINT8_C(16)
288#define DISGREG_BPL UINT8_C(17)
289#define DISGREG_SIL UINT8_C(18)
290#define DISGREG_DIL UINT8_C(19)
291/** @} */
292
293/** @name Segment registerindexes.
294 * This matches the AMD64 register encoding. It is found used in
295 * DISOPPARAM::base.reg_seg.
296 * @{
297 */
298typedef enum
299{
300 DISSELREG_ES = 0,
301 DISSELREG_CS = 1,
302 DISSELREG_SS = 2,
303 DISSELREG_DS = 3,
304 DISSELREG_FS = 4,
305 DISSELREG_GS = 5,
306 /** The usual 32-bit paranoia. */
307 DIS_SEGREG_32BIT_HACK = 0x7fffffff
308} DISSELREG;
309/** @} */
310
311/** @name FPU register indexes.
312 * This matches the AMD64 register encoding. It is found used in
313 * DISOPPARAM::base.reg_fp.
314 * @{
315 */
316#define DISFPREG_ST0 UINT8_C(0)
317#define DISFPREG_ST1 UINT8_C(1)
318#define DISFPREG_ST2 UINT8_C(2)
319#define DISFPREG_ST3 UINT8_C(3)
320#define DISFPREG_ST4 UINT8_C(4)
321#define DISFPREG_ST5 UINT8_C(5)
322#define DISFPREG_ST6 UINT8_C(6)
323#define DISFPREG_ST7 UINT8_C(7)
324/** @} */
325
326/** @name Control register indexes.
327 * This matches the AMD64 register encoding. It is found used in
328 * DISOPPARAM::base.reg_ctrl.
329 * @{
330 */
331#define DISCREG_CR0 UINT8_C(0)
332#define DISCREG_CR1 UINT8_C(1)
333#define DISCREG_CR2 UINT8_C(2)
334#define DISCREG_CR3 UINT8_C(3)
335#define DISCREG_CR4 UINT8_C(4)
336#define DISCREG_CR8 UINT8_C(8)
337/** @} */
338
339/** @name Debug register indexes.
340 * This matches the AMD64 register encoding. It is found used in
341 * DISOPPARAM::base.reg_dbg.
342 * @{
343 */
344#define DISDREG_DR0 UINT8_C(0)
345#define DISDREG_DR1 UINT8_C(1)
346#define DISDREG_DR2 UINT8_C(2)
347#define DISDREG_DR3 UINT8_C(3)
348#define DISDREG_DR4 UINT8_C(4)
349#define DISDREG_DR5 UINT8_C(5)
350#define DISDREG_DR6 UINT8_C(6)
351#define DISDREG_DR7 UINT8_C(7)
352/** @} */
353
354/** @name MMX register indexes.
355 * This matches the AMD64 register encoding. It is found used in
356 * DISOPPARAM::base.reg_mmx.
357 * @{
358 */
359#define DISMREG_MMX0 UINT8_C(0)
360#define DISMREG_MMX1 UINT8_C(1)
361#define DISMREG_MMX2 UINT8_C(2)
362#define DISMREG_MMX3 UINT8_C(3)
363#define DISMREG_MMX4 UINT8_C(4)
364#define DISMREG_MMX5 UINT8_C(5)
365#define DISMREG_MMX6 UINT8_C(6)
366#define DISMREG_MMX7 UINT8_C(7)
367/** @} */
368
369/** @name SSE register indexes.
370 * This matches the AMD64 register encoding. It is found used in
371 * DISOPPARAM::base.reg_xmm.
372 * @{
373 */
374#define DISXREG_XMM0 UINT8_C(0)
375#define DISXREG_XMM1 UINT8_C(1)
376#define DISXREG_XMM2 UINT8_C(2)
377#define DISXREG_XMM3 UINT8_C(3)
378#define DISXREG_XMM4 UINT8_C(4)
379#define DISXREG_XMM5 UINT8_C(5)
380#define DISXREG_XMM6 UINT8_C(6)
381#define DISXREG_XMM7 UINT8_C(7)
382/** @} */
383
384/** @name Flags returned by DISQueryParamVal (DISQPVPARAMVAL::flags).
385 * @{
386 */
387#define DISQPV_FLAG_8 UINT8_C(0x01)
388#define DISQPV_FLAG_16 UINT8_C(0x02)
389#define DISQPV_FLAG_32 UINT8_C(0x04)
390#define DISQPV_FLAG_64 UINT8_C(0x08)
391#define DISQPV_FLAG_FARPTR16 UINT8_C(0x10)
392#define DISQPV_FLAG_FARPTR32 UINT8_C(0x20)
393/** @} */
394
395/** @name Types returned by DISQueryParamVal (DISQPVPARAMVAL::flags).
396 * @{ */
397#define DISQPV_TYPE_REGISTER UINT8_C(1)
398#define DISQPV_TYPE_ADDRESS UINT8_C(2)
399#define DISQPV_TYPE_IMMEDIATE UINT8_C(3)
400/** @} */
401
402typedef struct
403{
404 union
405 {
406 uint8_t val8;
407 uint16_t val16;
408 uint32_t val32;
409 uint64_t val64;
410
411 struct
412 {
413 uint16_t sel;
414 uint32_t offset;
415 } farptr;
416 } val;
417
418 uint8_t type;
419 uint8_t size;
420 uint8_t flags;
421} DISQPVPARAMVAL;
422/** Pointer to opcode parameter value. */
423typedef DISQPVPARAMVAL *PDISQPVPARAMVAL;
424
425/** Indicates which parameter DISQueryParamVal should operate on. */
426typedef enum DISQPVWHICH
427{
428 DISQPVWHICH_DST = 1,
429 DISQPVWHICH_SRC,
430 DISQPVWHAT_32_BIT_HACK = 0x7fffffff
431} DISQPVWHICH;
432
433
434/**
435 * Operand Parameter.
436 */
437typedef struct DISOPPARAM
438{
439 uint64_t parval;
440 /** A combination of DISUSE_XXX. */
441 uint64_t fUse;
442 union
443 {
444 int64_t i64;
445 int32_t i32;
446 int32_t i16;
447 int32_t i8;
448 uint64_t u64;
449 uint32_t u32;
450 uint32_t u16;
451 uint32_t u8;
452 } uDisp;
453 int32_t param;
454
455 union
456 {
457 /** DISGREG_XXX. */
458 uint8_t reg_gen;
459 /** DISFPREG_XXX */
460 uint8_t reg_fp;
461 /** DISMREG_XXX. */
462 uint8_t reg_mmx;
463 /** DISXREG_XXX. */
464 uint8_t reg_xmm;
465 /** DISSELREG_XXX. */
466 uint8_t reg_seg;
467 /** TR0-TR7 (no defines for these). */
468 uint8_t reg_test;
469 /** DISCREG_XXX */
470 uint8_t reg_ctrl;
471 /** DISDREG_XXX */
472 uint8_t reg_dbg;
473 } base;
474 union
475 {
476 /** DISGREG_XXX. */
477 uint8_t reg_gen;
478 } index;
479
480 /** 2, 4 or 8. */
481 uint8_t scale;
482 /** Parameter size. */
483 uint8_t cb;
484} DISOPPARAM;
485AssertCompileSize(DISOPPARAM, 32);
486/** Pointer to opcode parameter. */
487typedef DISOPPARAM *PDISOPPARAM;
488/** Pointer to opcode parameter. */
489typedef const DISOPPARAM *PCOP_PARAMETER;
490
491
492/** Pointer to const opcode. */
493typedef const struct DISOPCODE *PCDISOPCODE;
494
495/**
496 * Callback for reading opcode bytes.
497 *
498 * @param pDisState Pointer to the CPU state. The primary user argument
499 * can be retrived from DISCPUSTATE::apvUserData[0]. If
500 * more is required these can be passed in the
501 * subsequent slots.
502 * @param pbDst Pointer to output buffer.
503 * @param uSrcAddr The address to start reading at.
504 * @param cbToRead The number of bytes to read.
505 */
506typedef DECLCALLBACK(int) FNDISREADBYTES(PDISCPUSTATE pDisState, uint8_t *pbDst, RTUINTPTR uSrcAddr, uint32_t cbToRead);
507/** Pointer to a opcode byte reader. */
508typedef FNDISREADBYTES *PFNDISREADBYTES;
509
510/** Parser callback.
511 * @remark no DECLCALLBACK() here because it's considered to be internal (really, I'm too lazy to update all the functions). */
512typedef unsigned FNDISPARSE(RTUINTPTR pu8CodeBlock, PCDISOPCODE pOp, PDISOPPARAM pParam, PDISCPUSTATE pCpu);
513typedef FNDISPARSE *PFNDISPARSE;
514typedef PFNDISPARSE const *PCPFNDISPARSE;
515
516typedef struct DISCPUSTATE
517{
518 /* Because of apvUserData[1] and apvUserData[2], put the less frequently
519 used bits at the top for now. (Might be better off in the middle?) */
520 DISOPPARAM param3;
521 DISOPPARAM param2;
522 DISOPPARAM param1;
523
524 /* off: 0x060 (96) */
525 /** ModRM fields. */
526 union
527 {
528 /** Bitfield view */
529 struct
530 {
531 unsigned Rm : 4;
532 unsigned Reg : 4;
533 unsigned Mod : 2;
534 } Bits;
535 /** unsigned view */
536 unsigned u;
537 } ModRM;
538 /** SIB fields. */
539 union
540 {
541 /** Bitfield view */
542 struct
543 {
544 unsigned Base : 4;
545 unsigned Index : 4;
546 unsigned Scale : 2;
547 } Bits;
548 /** unsigned view */
549 unsigned u;
550 } SIB;
551 int32_t i32SibDisp;
552
553 /* off: 0x06c (108) */
554 /** The CPU mode (DISCPUMODE). */
555 uint8_t mode;
556 /** The addressing mode (DISCPUMODE). */
557 uint8_t addrmode;
558 /** The operand mode (DISCPUMODE). */
559 uint8_t opmode;
560 /** Per instruction prefix settings. */
561 uint8_t prefix;
562 /* off: 0x070 (112) */
563 /** REX prefix value (64 bits only). */
564 uint8_t prefix_rex;
565 /** Segment prefix value (DISSELREG). */
566 uint8_t idxSegPrefix;
567 /** Last prefix byte (for SSE2 extension tables). */
568 uint8_t lastprefix;
569 /** First opcode byte of instruction. */
570 uint8_t opcode;
571 /* off: 0x074 (116) */
572 /** The size of the prefix bytes. */
573 uint8_t cbPrefix;
574 /** The instruction size. */
575 uint8_t opsize;
576 uint8_t abUnused[2];
577 /* off: 0x078 (120) */
578 /** Return code set by a worker function like the opcode bytes readers. */
579 int32_t rc;
580 /** Internal: instruction filter */
581 uint32_t fFilter;
582 /* off: 0x080 (128) */
583 /** Internal: pointer to disassembly function table */
584 PCPFNDISPARSE pfnDisasmFnTable;
585#if ARCH_BITS == 32
586 uint32_t uPtrPadding1;
587#endif
588 /** Pointer to the current instruction. */
589 PCDISOPCODE pCurInstr;
590#if ARCH_BITS == 32
591 uint32_t uPtrPadding2;
592#endif
593 /* off: 0x090 (144) */
594 /** The address of the instruction. */
595 RTUINTPTR uInstrAddr;
596 /* off: 0x098 (152) */
597 /** Optional read function */
598 PFNDISREADBYTES pfnReadBytes;
599#if ARCH_BITS == 32
600 uint32_t uPadding3;
601#endif
602 /* off: 0x0a0 (160) */
603 /** The instruction bytes. */
604 uint8_t abInstr[16];
605 /* off: 0x0b0 (176) */
606 /** User data slots for the read callback. The first entry is used for the
607 * pvUser argument, the rest are up for grabs.
608 * @remarks This must come last so that we can memset everything before this. */
609 void *apvUserData[3];
610#if ARCH_BITS == 32
611 uint32_t auPadding4[3];
612#endif
613} DISCPUSTATE;
614
615
616/**
617 * Opcode descriptor.
618 */
619typedef struct DISOPCODE
620{
621#ifndef DIS_CORE_ONLY
622 const char *pszOpcode;
623#endif
624 uint8_t idxParse1;
625 uint8_t idxParse2;
626 uint8_t idxParse3;
627 uint8_t uUnused;
628 uint16_t opcode;
629 uint16_t param1;
630 uint16_t param2;
631 uint16_t param3;
632 uint32_t optype;
633} DISOPCODE;
634
635
636DISDECL(int) DISInstrToStr(void const *pvInstr, DISCPUMODE enmCpuMode,
637 PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput);
638DISDECL(int) DISInstrToStrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
639 PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput);
640DISDECL(int) DISInstrToStrEx(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode,
641 PFNDISREADBYTES pfnReadBytes, void *pvUser, uint32_t uFilter,
642 PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput);
643
644DISDECL(int) DISInstr(void const *pvInstr, DISCPUMODE enmCpuMode, PDISCPUSTATE pCpu, uint32_t *pcbInstr);
645DISDECL(int) DISInstrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
646 PDISCPUSTATE pCpu, uint32_t *pcbInstr);
647DISDECL(int) DISInstEx(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, uint32_t uFilter,
648 PFNDISREADBYTES pfnReadBytes, void *pvUser,
649 PDISCPUSTATE pCpu, uint32_t *pcbInstr);
650
651DISDECL(int) DISGetParamSize(PDISCPUSTATE pCpu, PDISOPPARAM pParam);
652DISDECL(DISSELREG) DISDetectSegReg(PDISCPUSTATE pCpu, PDISOPPARAM pParam);
653DISDECL(uint8_t) DISQuerySegPrefixByte(PDISCPUSTATE pCpu);
654
655DISDECL(int) DISQueryParamVal(PCPUMCTXCORE pCtx, PDISCPUSTATE pCpu, PDISOPPARAM pParam, PDISQPVPARAMVAL pParamVal, DISQPVWHICH parmtype);
656DISDECL(int) DISQueryParamRegPtr(PCPUMCTXCORE pCtx, PDISCPUSTATE pCpu, PDISOPPARAM pParam, void **ppReg, size_t *pcbSize);
657
658DISDECL(int) DISFetchReg8(PCCPUMCTXCORE pCtx, unsigned reg8, uint8_t *pVal);
659DISDECL(int) DISFetchReg16(PCCPUMCTXCORE pCtx, unsigned reg16, uint16_t *pVal);
660DISDECL(int) DISFetchReg32(PCCPUMCTXCORE pCtx, unsigned reg32, uint32_t *pVal);
661DISDECL(int) DISFetchReg64(PCCPUMCTXCORE pCtx, unsigned reg64, uint64_t *pVal);
662DISDECL(int) DISFetchRegSeg(PCCPUMCTXCORE pCtx, DISSELREG sel, RTSEL *pVal);
663DISDECL(int) DISFetchRegSegEx(PCCPUMCTXCORE pCtx, DISSELREG sel, RTSEL *pVal, PCPUMSELREGHID *ppSelHidReg);
664DISDECL(int) DISWriteReg8(PCPUMCTXCORE pRegFrame, unsigned reg8, uint8_t val8);
665DISDECL(int) DISWriteReg16(PCPUMCTXCORE pRegFrame, unsigned reg32, uint16_t val16);
666DISDECL(int) DISWriteReg32(PCPUMCTXCORE pRegFrame, unsigned reg32, uint32_t val32);
667DISDECL(int) DISWriteReg64(PCPUMCTXCORE pRegFrame, unsigned reg64, uint64_t val64);
668DISDECL(int) DISWriteRegSeg(PCPUMCTXCORE pCtx, DISSELREG sel, RTSEL val);
669DISDECL(int) DISPtrReg8(PCPUMCTXCORE pCtx, unsigned reg8, uint8_t **ppReg);
670DISDECL(int) DISPtrReg16(PCPUMCTXCORE pCtx, unsigned reg16, uint16_t **ppReg);
671DISDECL(int) DISPtrReg32(PCPUMCTXCORE pCtx, unsigned reg32, uint32_t **ppReg);
672DISDECL(int) DISPtrReg64(PCPUMCTXCORE pCtx, unsigned reg64, uint64_t **ppReg);
673
674
675/**
676 * Try resolve an address into a symbol name.
677 *
678 * For use with DISFormatYasmEx(), DISFormatMasmEx() and DISFormatGasEx().
679 *
680 * @returns VBox status code.
681 * @retval VINF_SUCCESS on success, pszBuf contains the full symbol name.
682 * @retval VINF_BUFFER_OVERFLOW if pszBuf is too small the symbol name. The
683 * content of pszBuf is truncated and zero terminated.
684 * @retval VERR_SYMBOL_NOT_FOUND if no matching symbol was found for the address.
685 *
686 * @param pCpu Pointer to the disassembler CPU state.
687 * @param u32Sel The selector value. Use DIS_FMT_SEL_IS_REG, DIS_FMT_SEL_GET_VALUE,
688 * DIS_FMT_SEL_GET_REG to access this.
689 * @param uAddress The segment address.
690 * @param pszBuf Where to store the symbol name
691 * @param cchBuf The size of the buffer.
692 * @param poff If not a perfect match, then this is where the offset from the return
693 * symbol to the specified address is returned.
694 * @param pvUser The user argument.
695 */
696typedef DECLCALLBACK(int) FNDISGETSYMBOL(PCDISCPUSTATE pCpu, uint32_t u32Sel, RTUINTPTR uAddress, char *pszBuf, size_t cchBuf, RTINTPTR *poff, void *pvUser);
697/** Pointer to a FNDISGETSYMBOL(). */
698typedef FNDISGETSYMBOL *PFNDISGETSYMBOL;
699
700/**
701 * Checks if the FNDISGETSYMBOL argument u32Sel is a register or not.
702 */
703#define DIS_FMT_SEL_IS_REG(u32Sel) ( !!((u32Sel) & RT_BIT(31)) )
704
705/**
706 * Extracts the selector value from the FNDISGETSYMBOL argument u32Sel.
707 * @returns Selector value.
708 */
709#define DIS_FMT_SEL_GET_VALUE(u32Sel) ( (RTSEL)(u32Sel) )
710
711/**
712 * Extracts the register number from the FNDISGETSYMBOL argument u32Sel.
713 * @returns USE_REG_CS, USE_REG_SS, USE_REG_DS, USE_REG_ES, USE_REG_FS or USE_REG_FS.
714 */
715#define DIS_FMT_SEL_GET_REG(u32Sel) ( ((u32Sel) >> 16) & 0xf )
716
717/** @internal */
718#define DIS_FMT_SEL_FROM_REG(uReg) ( ((uReg) << 16) | RT_BIT(31) | 0xffff )
719/** @internal */
720#define DIS_FMT_SEL_FROM_VALUE(Sel) ( (Sel) & 0xffff )
721
722
723/** @name Flags for use with DISFormatYasmEx(), DISFormatMasmEx() and DISFormatGasEx().
724 * @{
725 */
726/** Put the address to the right. */
727#define DIS_FMT_FLAGS_ADDR_RIGHT RT_BIT_32(0)
728/** Put the address to the left. */
729#define DIS_FMT_FLAGS_ADDR_LEFT RT_BIT_32(1)
730/** Put the address in comments.
731 * For some assemblers this implies placing it to the right. */
732#define DIS_FMT_FLAGS_ADDR_COMMENT RT_BIT_32(2)
733/** Put the instruction bytes to the right of the disassembly. */
734#define DIS_FMT_FLAGS_BYTES_RIGHT RT_BIT_32(3)
735/** Put the instruction bytes to the left of the disassembly. */
736#define DIS_FMT_FLAGS_BYTES_LEFT RT_BIT_32(4)
737/** Put the instruction bytes in comments.
738 * For some assemblers this implies placing the bytes to the right. */
739#define DIS_FMT_FLAGS_BYTES_COMMENT RT_BIT_32(5)
740/** Put the bytes in square brackets. */
741#define DIS_FMT_FLAGS_BYTES_BRACKETS RT_BIT_32(6)
742/** Put spaces between the bytes. */
743#define DIS_FMT_FLAGS_BYTES_SPACED RT_BIT_32(7)
744/** Display the relative +/- offset of branch instructions that uses relative addresses,
745 * and put the target address in parenthesis. */
746#define DIS_FMT_FLAGS_RELATIVE_BRANCH RT_BIT_32(8)
747/** Strict assembly. The assembly should, when ever possible, make the
748 * assembler reproduce the exact same binary. (Refers to the yasm
749 * strict keyword.) */
750#define DIS_FMT_FLAGS_STRICT RT_BIT_32(9)
751/** Checks if the given flags are a valid combination. */
752#define DIS_FMT_FLAGS_IS_VALID(fFlags) \
753 ( !((fFlags) & ~UINT32_C(0x000003ff)) \
754 && ((fFlags) & (DIS_FMT_FLAGS_ADDR_RIGHT | DIS_FMT_FLAGS_ADDR_LEFT)) != (DIS_FMT_FLAGS_ADDR_RIGHT | DIS_FMT_FLAGS_ADDR_LEFT) \
755 && ( !((fFlags) & DIS_FMT_FLAGS_ADDR_COMMENT) \
756 || (fFlags & (DIS_FMT_FLAGS_ADDR_RIGHT | DIS_FMT_FLAGS_ADDR_LEFT)) ) \
757 && ((fFlags) & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_BYTES_LEFT)) != (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_BYTES_LEFT) \
758 && ( !((fFlags) & (DIS_FMT_FLAGS_BYTES_COMMENT | DIS_FMT_FLAGS_BYTES_BRACKETS)) \
759 || (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_BYTES_LEFT)) ) \
760 )
761/** @} */
762
763DISDECL(size_t) DISFormatYasm( PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf);
764DISDECL(size_t) DISFormatYasmEx(PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf, uint32_t fFlags, PFNDISGETSYMBOL pfnGetSymbol, void *pvUser);
765DISDECL(size_t) DISFormatMasm( PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf);
766DISDECL(size_t) DISFormatMasmEx(PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf, uint32_t fFlags, PFNDISGETSYMBOL pfnGetSymbol, void *pvUser);
767DISDECL(size_t) DISFormatGas( PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf);
768DISDECL(size_t) DISFormatGasEx( PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf, uint32_t fFlags, PFNDISGETSYMBOL pfnGetSymbol, void *pvUser);
769
770/** @todo DISAnnotate(PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf, register reader, memory reader); */
771
772DISDECL(bool) DISFormatYasmIsOddEncoding(PDISCPUSTATE pCpu);
773
774
775RT_C_DECLS_END
776
777#endif
778
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