1 | /* $Id: DisasmInternal.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox disassembler - Internal header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VBOX_INCLUDED_SRC_DisasmInternal_h
|
---|
29 | #define VBOX_INCLUDED_SRC_DisasmInternal_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <VBox/types.h>
|
---|
35 | #include <VBox/dis.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /** @defgroup grp_dis_int Internals.
|
---|
39 | * @ingroup grp_dis
|
---|
40 | * @{
|
---|
41 | */
|
---|
42 |
|
---|
43 | /** @name Index into g_apfnCalcSize and g_apfnFullDisasm.
|
---|
44 | * @{ */
|
---|
45 | enum IDX_Parse
|
---|
46 | {
|
---|
47 | IDX_ParseNop = 0,
|
---|
48 | IDX_ParseModRM,
|
---|
49 | IDX_UseModRM,
|
---|
50 | IDX_ParseImmByte,
|
---|
51 | IDX_ParseImmBRel,
|
---|
52 | IDX_ParseImmUshort,
|
---|
53 | IDX_ParseImmV,
|
---|
54 | IDX_ParseImmVRel,
|
---|
55 | IDX_ParseImmAddr,
|
---|
56 | IDX_ParseFixedReg,
|
---|
57 | IDX_ParseImmUlong,
|
---|
58 | IDX_ParseImmQword,
|
---|
59 | IDX_ParseTwoByteEsc,
|
---|
60 | IDX_ParseGrp1,
|
---|
61 | IDX_ParseShiftGrp2,
|
---|
62 | IDX_ParseGrp3,
|
---|
63 | IDX_ParseGrp4,
|
---|
64 | IDX_ParseGrp5,
|
---|
65 | IDX_Parse3DNow,
|
---|
66 | IDX_ParseGrp6,
|
---|
67 | IDX_ParseGrp7,
|
---|
68 | IDX_ParseGrp8,
|
---|
69 | IDX_ParseGrp9,
|
---|
70 | IDX_ParseGrp10,
|
---|
71 | IDX_ParseGrp12,
|
---|
72 | IDX_ParseGrp13,
|
---|
73 | IDX_ParseGrp14,
|
---|
74 | IDX_ParseGrp15,
|
---|
75 | IDX_ParseGrp16,
|
---|
76 | IDX_ParseGrp17,
|
---|
77 | IDX_ParseModFence,
|
---|
78 | IDX_ParseYv,
|
---|
79 | IDX_ParseYb,
|
---|
80 | IDX_ParseXv,
|
---|
81 | IDX_ParseXb,
|
---|
82 | IDX_ParseEscFP,
|
---|
83 | IDX_ParseNopPause,
|
---|
84 | IDX_ParseImmByteSX,
|
---|
85 | IDX_ParseImmZ,
|
---|
86 | IDX_ParseThreeByteEsc4,
|
---|
87 | IDX_ParseThreeByteEsc5,
|
---|
88 | IDX_ParseImmAddrF,
|
---|
89 | IDX_ParseInvOpModRM,
|
---|
90 | IDX_ParseVex2b,
|
---|
91 | IDX_ParseVex3b,
|
---|
92 | IDX_ParseVexDest,
|
---|
93 | IDX_ParseMax
|
---|
94 | };
|
---|
95 | AssertCompile(IDX_ParseMax < 64 /* Packed DISOPCODE assumption. */);
|
---|
96 | /** @} */
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Opcode map descriptor.
|
---|
100 | *
|
---|
101 | * This is used a number of places to save storage space where there are lots of
|
---|
102 | * invalid instructions and the beginning or end of the map.
|
---|
103 | */
|
---|
104 | typedef struct DISOPMAPDESC
|
---|
105 | {
|
---|
106 | /** Pointer to the opcodes described by this structure. */
|
---|
107 | PCDISOPCODE papOpcodes;
|
---|
108 | #if ARCH_BITS <= 32
|
---|
109 | uint16_t
|
---|
110 | #else
|
---|
111 | uint32_t
|
---|
112 | #endif
|
---|
113 | /** The map index corresponding to the first papOpcodes entry. */
|
---|
114 | idxFirst,
|
---|
115 | /** Number of opcodes in the map. */
|
---|
116 | cOpcodes;
|
---|
117 | } DISOPMAPDESC;
|
---|
118 | /** Pointer to a const opcode map descriptor. */
|
---|
119 | typedef DISOPMAPDESC const *PCDISOPMAPDESC;
|
---|
120 |
|
---|
121 | /** @name Opcode maps.
|
---|
122 | * @{ */
|
---|
123 | extern const DISOPCODE g_InvalidOpcode[1];
|
---|
124 |
|
---|
125 | extern const DISOPCODE g_aOneByteMapX86[256];
|
---|
126 | extern const DISOPCODE g_aOneByteMapX64[256];
|
---|
127 | extern const DISOPCODE g_aTwoByteMapX86[256];
|
---|
128 |
|
---|
129 | /** Two byte opcode map with prefix 0x66 */
|
---|
130 | extern const DISOPCODE g_aTwoByteMapX86_PF66[256];
|
---|
131 |
|
---|
132 | /** Two byte opcode map with prefix 0xF2 */
|
---|
133 | extern const DISOPCODE g_aTwoByteMapX86_PFF2[256];
|
---|
134 |
|
---|
135 | /** Two byte opcode map with prefix 0xF3 */
|
---|
136 | extern const DISOPCODE g_aTwoByteMapX86_PFF3[256];
|
---|
137 |
|
---|
138 | /** Three byte opcode map (0xF 0x38) */
|
---|
139 | extern PCDISOPCODE const g_apThreeByteMapX86_0F38[16];
|
---|
140 |
|
---|
141 | /** Three byte opcode map with prefix 0x66 (0xF 0x38) */
|
---|
142 | extern PCDISOPCODE const g_apThreeByteMapX86_660F38[16];
|
---|
143 |
|
---|
144 | /** Three byte opcode map with prefix 0xF2 (0xF 0x38) */
|
---|
145 | extern PCDISOPCODE const g_apThreeByteMapX86_F20F38[16];
|
---|
146 |
|
---|
147 | /** Three byte opcode map with prefix 0xF3 (0xF 0x38) */
|
---|
148 | extern PCDISOPCODE const g_apThreeByteMapX86_F30F38[16];
|
---|
149 |
|
---|
150 | extern PCDISOPCODE const g_apThreeByteMapX86_0F3A[16];
|
---|
151 |
|
---|
152 | /** Three byte opcode map with prefix 0x66 (0xF 0x3A) */
|
---|
153 | extern PCDISOPCODE const g_apThreeByteMapX86_660F3A[16];
|
---|
154 |
|
---|
155 | /** Three byte opcode map with prefixes 0x66 0xF2 (0xF 0x38) */
|
---|
156 | extern PCDISOPCODE const g_apThreeByteMapX86_66F20F38[16];
|
---|
157 |
|
---|
158 | /** VEX opcodes table defined by [VEX.m-mmmm - 1].
|
---|
159 | * 0Fh, 0F38h, 0F3Ah correspondingly, VEX.pp = 00b */
|
---|
160 | extern PCDISOPMAPDESC const g_apVexOpcodesMapRanges_None[3];
|
---|
161 |
|
---|
162 | /** VEX opcodes table defined by [VEX.m-mmmm - 1].
|
---|
163 | * 0Fh, 0F38h, 0F3Ah correspondingly, VEX.pp = 01b (66h) */
|
---|
164 | extern PCDISOPMAPDESC const g_apVexOpcodesMapRanges_66H[3];
|
---|
165 |
|
---|
166 | /** 0Fh, 0F38h, 0F3Ah correspondingly, VEX.pp = 10b (F3h) */
|
---|
167 | extern PCDISOPMAPDESC const g_apVexOpcodesMapRanges_F3H[3];
|
---|
168 |
|
---|
169 | /** 0Fh, 0F38h, 0F3Ah correspondingly, VEX.pp = 11b (F2h) */
|
---|
170 | extern PCDISOPMAPDESC const g_apVexOpcodesMapRanges_F2H[3];
|
---|
171 |
|
---|
172 | /** Two dimmentional map descriptor array: first index is by VEX.pp (prefix),
|
---|
173 | * second by the VEX.mmmm (map).
|
---|
174 | * The latter has to be bounced checked as we only have the first 4 maps. */
|
---|
175 | extern PCDISOPMAPDESC const g_aapVexOpcodesMapRanges[4][4];
|
---|
176 | /** @} */
|
---|
177 |
|
---|
178 | /** @name Opcode extensions (Group tables)
|
---|
179 | * @{ */
|
---|
180 | extern const DISOPCODE g_aMapX86_Group1[8*4];
|
---|
181 | extern const DISOPCODE g_aMapX86_Group2[8*6];
|
---|
182 | extern const DISOPCODE g_aMapX86_Group3[8*2];
|
---|
183 | extern const DISOPCODE g_aMapX86_Group4[8];
|
---|
184 | extern const DISOPCODE g_aMapX86_Group5[8];
|
---|
185 | extern const DISOPCODE g_aMapX86_Group6[8];
|
---|
186 | extern const DISOPCODE g_aMapX86_Group7_mem[8];
|
---|
187 | extern const DISOPCODE g_aMapX86_Group7_mod11_rm000[8];
|
---|
188 | extern const DISOPCODE g_aMapX86_Group7_mod11_rm001[8];
|
---|
189 | extern const DISOPCODE g_aMapX86_Group8[8];
|
---|
190 | extern const DISOPCODE g_aMapX86_Group9[8];
|
---|
191 | extern const DISOPCODE g_aMapX86_Group10[8];
|
---|
192 | extern const DISOPCODE g_aMapX86_Group11[8*2];
|
---|
193 | extern const DISOPCODE g_aMapX86_Group12[8*2];
|
---|
194 | extern const DISOPCODE g_aMapX86_Group13[8*2];
|
---|
195 | extern const DISOPCODE g_aMapX86_Group14[8*2];
|
---|
196 | extern const DISOPCODE g_aMapX86_Group15_mem[8];
|
---|
197 | extern const DISOPCODE g_aMapX86_Group15_mod11_rm000[8];
|
---|
198 | extern const DISOPCODE g_aMapX86_Group16[8];
|
---|
199 | extern const DISOPCODE g_aMapX86_Group17[8*2];
|
---|
200 | extern const DISOPCODE g_aMapX86_NopPause[2];
|
---|
201 | /** @} */
|
---|
202 |
|
---|
203 | /** 3DNow! map (0x0F 0x0F prefix) */
|
---|
204 | extern const DISOPCODE g_aTwoByteMapX86_3DNow[256];
|
---|
205 |
|
---|
206 | /** Floating point opcodes starting with escape byte 0xDF
|
---|
207 | * @{ */
|
---|
208 | extern const DISOPCODE g_aMapX86_EscF0_Low[8];
|
---|
209 | extern const DISOPCODE g_aMapX86_EscF0_High[16*4];
|
---|
210 | extern const DISOPCODE g_aMapX86_EscF1_Low[8];
|
---|
211 | extern const DISOPCODE g_aMapX86_EscF1_High[16*4];
|
---|
212 | extern const DISOPCODE g_aMapX86_EscF2_Low[8];
|
---|
213 | extern const DISOPCODE g_aMapX86_EscF2_High[16*4];
|
---|
214 | extern const DISOPCODE g_aMapX86_EscF3_Low[8];
|
---|
215 | extern const DISOPCODE g_aMapX86_EscF3_High[16*4];
|
---|
216 | extern const DISOPCODE g_aMapX86_EscF4_Low[8];
|
---|
217 | extern const DISOPCODE g_aMapX86_EscF4_High[16*4];
|
---|
218 | extern const DISOPCODE g_aMapX86_EscF5_Low[8];
|
---|
219 | extern const DISOPCODE g_aMapX86_EscF5_High[16*4];
|
---|
220 | extern const DISOPCODE g_aMapX86_EscF6_Low[8];
|
---|
221 | extern const DISOPCODE g_aMapX86_EscF6_High[16*4];
|
---|
222 | extern const DISOPCODE g_aMapX86_EscF7_Low[8];
|
---|
223 | extern const DISOPCODE g_aMapX86_EscF7_High[16*4];
|
---|
224 |
|
---|
225 | extern const PCDISOPCODE g_apMapX86_FP_Low[8];
|
---|
226 | extern const PCDISOPCODE g_apMapX86_FP_High[8];
|
---|
227 | /** @} */
|
---|
228 |
|
---|
229 | /** @def OP
|
---|
230 | * Wrapper which initializes an DISOPCODE.
|
---|
231 | * We must use this so that we can exclude unused fields in order
|
---|
232 | * to save precious bytes in the GC version.
|
---|
233 | *
|
---|
234 | * @internal
|
---|
235 | */
|
---|
236 | #if DISOPCODE_FORMAT == 0
|
---|
237 | # define OP(pszOpcode, idxParse1, idxParse2, idxParse3, opcode, param1, param2, param3, optype) \
|
---|
238 | { pszOpcode, idxParse1, idxParse2, idxParse3, 0, opcode, param1, param2, param3, 0, 0, optype }
|
---|
239 | # define OPVEX(pszOpcode, idxParse1, idxParse2, idxParse3, idxParse4, opcode, param1, param2, param3, param4, optype) \
|
---|
240 | { pszOpcode, idxParse1, idxParse2, idxParse3, idxParse4, opcode, param1, param2, param3, param4, 0, optype | DISOPTYPE_SSE }
|
---|
241 |
|
---|
242 | #elif DISOPCODE_FORMAT == 16
|
---|
243 | # define OP(pszOpcode, idxParse1, idxParse2, idxParse3, opcode, param1, param2, param3, optype) \
|
---|
244 | { optype, opcode, idxParse1, idxParse2, param1, param2, idxParse3, param3, 0, 0 }
|
---|
245 | # define OPVEX(pszOpcode, idxParse1, idxParse2, idxParse3, idxParse4, opcode, param1, param2, param3, param4, optype) \
|
---|
246 | { optype | DISOPTYPE_SSE, opcode, idxParse1, idxParse2, param1, param2, idxParse3, param3, param4, idxParse4 }
|
---|
247 |
|
---|
248 | #elif DISOPCODE_FORMAT == 15
|
---|
249 | # define OP(pszOpcode, idxParse1, idxParse2, idxParse3, opcode, param1, param2, param3, optype) \
|
---|
250 | { opcode, idxParse1, idxParse2, idxParse3, param1, param2, param3, optype, 0, 0 }
|
---|
251 | # define OPVEX(pszOpcode, idxParse1, idxParse2, idxParse3, idxParse4, opcode, param1, param2, param3, param4, optype) \
|
---|
252 | { opcode, idxParse1, idxParse2, idxParse3, param1, param2, param3, optype | DISOPTYPE_SSE, param4, idxParse4 }
|
---|
253 | #else
|
---|
254 | # error Unsupported DISOPCODE_FORMAT value
|
---|
255 | #endif
|
---|
256 |
|
---|
257 |
|
---|
258 | size_t disFormatBytes(PCDISSTATE pDis, char *pszDst, size_t cchDst, uint32_t fFlags);
|
---|
259 |
|
---|
260 | /** @} */
|
---|
261 | #endif /* !VBOX_INCLUDED_SRC_DisasmInternal_h */
|
---|
262 |
|
---|