1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/* $Id$ */
/** @file
* VBox disassembler - Internal header.
*/
/*
* Copyright (C) 2006-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#ifndef VBOX_INCLUDED_SRC_DisasmInternal_h
#define VBOX_INCLUDED_SRC_DisasmInternal_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <VBox/types.h>
#include <VBox/err.h>
#include <VBox/dis.h>
#include <VBox/log.h>
#include <iprt/param.h>
/** @defgroup grp_dis_int Internals.
* @ingroup grp_dis
* @{
*/
/** This must be less or equal to DISSTATE::u.abInstr.
* See Vol3A/Table 6-2 and Vol3B/Section22.25 for instance. */
#define DIS_MAX_INSTR_LENGTH 15
/** Whether we can do unaligned access. */
#if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
# define DIS_HOST_UNALIGNED_ACCESS_OK
#endif
/** @def OP
* Wrapper which initializes an DISOPCODE.
* We must use this so that we can exclude unused fields in order
* to save precious bytes in the GC version.
*
* @internal
*/
#if DISOPCODE_FORMAT == 0
# define OP(pszOpcode, idxParse1, idxParse2, idxParse3, opcode, param1, param2, param3, optype) \
{ pszOpcode, idxParse1, idxParse2, idxParse3, 0, opcode, param1, param2, param3, 0, 0, optype }
# define OPVEX(pszOpcode, idxParse1, idxParse2, idxParse3, idxParse4, opcode, param1, param2, param3, param4, optype) \
{ pszOpcode, idxParse1, idxParse2, idxParse3, idxParse4, opcode, param1, param2, param3, param4, 0, optype | DISOPTYPE_X86_SSE }
#elif DISOPCODE_FORMAT == 16
# define OP(pszOpcode, idxParse1, idxParse2, idxParse3, opcode, param1, param2, param3, optype) \
{ optype, opcode, idxParse1, idxParse2, param1, param2, idxParse3, param3, 0, 0 }
# define OPVEX(pszOpcode, idxParse1, idxParse2, idxParse3, idxParse4, opcode, param1, param2, param3, param4, optype) \
{ optype | DISOPTYPE_X86_SSE, opcode, idxParse1, idxParse2, param1, param2, idxParse3, param3, param4, idxParse4 }
#elif DISOPCODE_FORMAT == 15
# define OP(pszOpcode, idxParse1, idxParse2, idxParse3, opcode, param1, param2, param3, optype) \
{ opcode, idxParse1, idxParse2, idxParse3, param1, param2, param3, optype, 0, 0 }
# define OPVEX(pszOpcode, idxParse1, idxParse2, idxParse3, idxParse4, opcode, param1, param2, param3, param4, optype) \
{ opcode, idxParse1, idxParse2, idxParse3, param1, param2, param3, optype | DISOPTYPE_X86_SSE, param4, idxParse4 }
#else
# error Unsupported DISOPCODE_FORMAT value
#endif
/* Common */
DECLHIDDEN(void) disReadMore(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMin);
DECLHIDDEN(uint8_t) disReadByteSlow(PDISSTATE pDis, size_t offInstr);
DECLHIDDEN(uint16_t) disReadWordSlow(PDISSTATE pDis, size_t offInstr);
DECLHIDDEN(uint32_t) disReadDWordSlow(PDISSTATE pDis, size_t offInstr);
DECLHIDDEN(uint64_t) disReadQWordSlow(PDISSTATE pDis, size_t offInstr);
/**
* Read a byte (8-bit) instruction.
*
* @returns The requested byte.
* @param pDis The disassembler state.
* @param uAddress The address.
*/
DECL_FORCE_INLINE(uint8_t) disReadByte(PDISSTATE pDis, size_t offInstr)
{
if (offInstr >= pDis->cbCachedInstr)
return disReadByteSlow(pDis, offInstr);
return pDis->u.abInstr[offInstr];
}
/**
* Read a word (16-bit) instruction.
*
* @returns The requested word.
* @param pDis The disassembler state.
* @param offInstr The offset of the qword relative to the
* instruction.
*/
DECL_FORCE_INLINE(uint16_t) disReadWord(PDISSTATE pDis, size_t offInstr)
{
if (offInstr + 2 > pDis->cbCachedInstr)
return disReadWordSlow(pDis, offInstr);
#ifdef DIS_HOST_UNALIGNED_ACCESS_OK
return *(uint16_t const *)&pDis->u.abInstr[offInstr];
#else
return RT_MAKE_U16(pDis->u.abInstr[offInstr], pDis->u.abInstr[offInstr + 1]);
#endif
}
/**
* Read a dword (32-bit) instruction.
*
* @returns The requested dword.
* @param pDis The disassembler state.
* @param offInstr The offset of the qword relative to the
* instruction.
*/
DECL_FORCE_INLINE(uint32_t) disReadDWord(PDISSTATE pDis, size_t offInstr)
{
if (offInstr + 4 > pDis->cbCachedInstr)
return disReadDWordSlow(pDis, offInstr);
#ifdef DIS_HOST_UNALIGNED_ACCESS_OK
return *(uint32_t const *)&pDis->u.abInstr[offInstr];
#else
return RT_MAKE_U32_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3]);
#endif
}
/**
* Read a qword (64-bit) instruction.
*
* @returns The requested qword.
* @param pDis The disassembler state.
* @param uAddress The address.
*/
DECL_FORCE_INLINE(uint64_t) disReadQWord(PDISSTATE pDis, size_t offInstr)
{
if (offInstr + 8 > pDis->cbCachedInstr)
return disReadQWordSlow(pDis, offInstr);
#ifdef DIS_HOST_UNALIGNED_ACCESS_OK
return *(uint64_t const *)&pDis->u.abInstr[offInstr];
#else
return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3],
pDis->u.abInstr[offInstr + 4], pDis->u.abInstr[offInstr + 5],
pDis->u.abInstr[offInstr + 6], pDis->u.abInstr[offInstr + 7]);
#endif
}
/**
* Reads some bytes into the cache.
*
* While this will set DISSTATE::rc on failure, the caller should disregard
* this since that is what would happen if we didn't prefetch bytes prior to the
* instruction parsing.
*
* @param pDis The disassembler state.
*/
DECL_FORCE_INLINE(void) disPrefetchBytes(PDISSTATE pDis)
{
/*
* Read some bytes into the cache. (If this fail we continue as nothing
* has gone wrong since this is what would happen if we didn't precharge
* the cache here.)
*/
int rc = pDis->pfnReadBytes(pDis, 0, 1, sizeof(pDis->u.abInstr));
if (RT_SUCCESS(rc))
{
Assert(pDis->cbCachedInstr >= 1);
Assert(pDis->cbCachedInstr <= sizeof(pDis->u.abInstr));
}
else
{
Log(("Initial read failed with rc=%Rrc!!\n", rc));
pDis->rc = rc;
}
}
#if defined(VBOX_DIS_WITH_X86_AMD64)
/* x86/amd64 */
DECLHIDDEN(PCDISOPCODE) disInitializeStateX86(PDISSTATE pDis, DISCPUMODE enmCpuMode, uint32_t fFilter);
DECLHIDDEN(int) disInstrWorkerX86(PDISSTATE pDis, PCDISOPCODE paOneByteMap, uint32_t *pcbInstr);
#endif
#if defined(VBOX_DIS_WITH_ARMV8)
/* x86/amd64 */
DECLHIDDEN(PCDISOPCODE) disInitializeStateArmV8(PDISSTATE pDis, DISCPUMODE enmCpuMode, uint32_t fFilter);
DECLHIDDEN(int) disInstrWorkerArmV8(PDISSTATE pDis, PCDISOPCODE paOneByteMap, uint32_t *pcbInstr);
#endif
size_t disFormatBytes(PCDISSTATE pDis, char *pszDst, size_t cchDst, uint32_t fFlags);
/** @} */
#endif /* !VBOX_INCLUDED_SRC_DisasmInternal_h */
|