[41668] | 1 | /* $Id: Disasm.cpp 99319 2023-04-06 19:28:23Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[41668] | 3 | * VBox disassembler - Disassemble and optionally format.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 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
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
| 28 |
|
---|
[57358] | 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
[41668] | 32 | #define LOG_GROUP LOG_GROUP_DIS
|
---|
[1] | 33 | #include <VBox/dis.h>
|
---|
[76455] | 34 | #include <iprt/errcore.h>
|
---|
[1] | 35 | #include <iprt/assert.h>
|
---|
| 36 | #include <iprt/string.h>
|
---|
| 37 | #include "DisasmInternal.h"
|
---|
| 38 |
|
---|
| 39 |
|
---|
[99236] | 40 | /*********************************************************************************************************************************
|
---|
| 41 | * Defined Constants And Macros *
|
---|
| 42 | *********************************************************************************************************************************/
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | /*********************************************************************************************************************************
|
---|
| 46 | * Internal Functions *
|
---|
| 47 | *********************************************************************************************************************************/
|
---|
| 48 |
|
---|
[1] | 49 | /**
|
---|
[99236] | 50 | * @interface_method_impl{FNDISREADBYTES, The default byte reader callber.}
|
---|
| 51 | */
|
---|
| 52 | static DECLCALLBACK(int) disReadBytesDefault(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
|
---|
| 53 | {
|
---|
| 54 | #if 0 /*def IN_RING0 - why? */
|
---|
| 55 | RT_NOREF_PV(cbMinRead);
|
---|
| 56 | AssertMsgFailed(("disReadWord with no read callback in ring 0!!\n"));
|
---|
| 57 | RT_BZERO(&pDis->u.abInstr[offInstr], cbMaxRead);
|
---|
| 58 | pDis->cbCachedInstr = offInstr + cbMaxRead;
|
---|
| 59 | return VERR_DIS_NO_READ_CALLBACK;
|
---|
| 60 | #else
|
---|
| 61 | uint8_t const *pbSrc = (uint8_t const *)(uintptr_t)pDis->uInstrAddr + offInstr;
|
---|
| 62 | size_t cbLeftOnPage = (uintptr_t)pbSrc & PAGE_OFFSET_MASK;
|
---|
| 63 | uint8_t cbToRead = cbLeftOnPage >= cbMaxRead
|
---|
| 64 | ? cbMaxRead
|
---|
| 65 | : cbLeftOnPage <= cbMinRead
|
---|
| 66 | ? cbMinRead
|
---|
| 67 | : (uint8_t)cbLeftOnPage;
|
---|
| 68 | memcpy(&pDis->u.abInstr[offInstr], pbSrc, cbToRead);
|
---|
| 69 | pDis->cbCachedInstr = offInstr + cbToRead;
|
---|
| 70 | return VINF_SUCCESS;
|
---|
| 71 | #endif
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Read more bytes into the DISSTATE::u.abInstr buffer, advance
|
---|
| 77 | * DISSTATE::cbCachedInstr.
|
---|
| 78 | *
|
---|
| 79 | * Will set DISSTATE::rc on failure, but still advance cbCachedInstr.
|
---|
| 80 | *
|
---|
| 81 | * The caller shall fend off reads beyond the DISSTATE::u.abInstr buffer.
|
---|
| 82 | *
|
---|
| 83 | * @param pDis The disassembler state.
|
---|
| 84 | * @param offInstr The offset of the read request.
|
---|
| 85 | * @param cbMin The size of the read request that needs to be
|
---|
| 86 | * satisfied.
|
---|
| 87 | */
|
---|
| 88 | DECLHIDDEN(void) disReadMore(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMin)
|
---|
| 89 | {
|
---|
| 90 | Assert(cbMin + offInstr <= sizeof(pDis->u.abInstr));
|
---|
| 91 |
|
---|
| 92 | /*
|
---|
| 93 | * Adjust the incoming request to not overlap with bytes that has already
|
---|
| 94 | * been read and to make sure we don't leave unread gaps.
|
---|
| 95 | */
|
---|
| 96 | if (offInstr < pDis->cbCachedInstr)
|
---|
| 97 | {
|
---|
| 98 | Assert(offInstr + cbMin > pDis->cbCachedInstr);
|
---|
| 99 | cbMin -= pDis->cbCachedInstr - offInstr;
|
---|
| 100 | offInstr = pDis->cbCachedInstr;
|
---|
| 101 | }
|
---|
| 102 | else if (offInstr > pDis->cbCachedInstr)
|
---|
| 103 | {
|
---|
| 104 | cbMin += offInstr - pDis->cbCachedInstr;
|
---|
| 105 | offInstr = pDis->cbCachedInstr;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /*
|
---|
| 109 | * Do the read.
|
---|
| 110 | * (No need to zero anything on failure as u.abInstr is already zeroed by the
|
---|
| 111 | * DISInstrEx API.)
|
---|
| 112 | */
|
---|
| 113 | int rc = pDis->pfnReadBytes(pDis, offInstr, cbMin, sizeof(pDis->u.abInstr) - offInstr);
|
---|
| 114 | if (RT_SUCCESS(rc))
|
---|
| 115 | {
|
---|
| 116 | Assert(pDis->cbCachedInstr >= offInstr + cbMin);
|
---|
| 117 | Assert(pDis->cbCachedInstr <= sizeof(pDis->u.abInstr));
|
---|
| 118 | }
|
---|
| 119 | else
|
---|
| 120 | {
|
---|
| 121 | Log(("disReadMore failed with rc=%Rrc!!\n", rc));
|
---|
| 122 | pDis->rc = rc;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Function for handling a 8-bit cache miss.
|
---|
| 129 | *
|
---|
| 130 | * @returns The requested byte.
|
---|
| 131 | * @param pDis The disassembler state.
|
---|
| 132 | * @param offInstr The offset of the byte relative to the
|
---|
| 133 | * instruction.
|
---|
| 134 | */
|
---|
| 135 | DECLHIDDEN(uint8_t) disReadByteSlow(PDISSTATE pDis, size_t offInstr)
|
---|
| 136 | {
|
---|
| 137 | if (RT_LIKELY(offInstr < DIS_MAX_INSTR_LENGTH))
|
---|
| 138 | {
|
---|
| 139 | disReadMore(pDis, (uint8_t)offInstr, 1);
|
---|
| 140 | return pDis->u.abInstr[offInstr];
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | Log(("disReadByte: too long instruction...\n"));
|
---|
| 144 | pDis->rc = VERR_DIS_TOO_LONG_INSTR;
|
---|
| 145 | ssize_t cbLeft = (ssize_t)(sizeof(pDis->u.abInstr) - offInstr);
|
---|
| 146 | if (cbLeft > 0)
|
---|
| 147 | return pDis->u.abInstr[offInstr];
|
---|
| 148 | return 0;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * Function for handling a 16-bit cache miss.
|
---|
| 154 | *
|
---|
| 155 | * @returns The requested word.
|
---|
| 156 | * @param pDis The disassembler state.
|
---|
| 157 | * @param offInstr The offset of the word relative to the
|
---|
| 158 | * instruction.
|
---|
| 159 | */
|
---|
| 160 | DECLHIDDEN(uint16_t) disReadWordSlow(PDISSTATE pDis, size_t offInstr)
|
---|
| 161 | {
|
---|
| 162 | if (RT_LIKELY(offInstr + 2 <= DIS_MAX_INSTR_LENGTH))
|
---|
| 163 | {
|
---|
| 164 | disReadMore(pDis, (uint8_t)offInstr, 2);
|
---|
| 165 | #ifdef DIS_HOST_UNALIGNED_ACCESS_OK
|
---|
| 166 | return *(uint16_t const *)&pDis->u.abInstr[offInstr];
|
---|
| 167 | #else
|
---|
| 168 | return RT_MAKE_U16(pDis->u.abInstr[offInstr], pDis->u.abInstr[offInstr + 1]);
|
---|
| 169 | #endif
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | Log(("disReadWord: too long instruction...\n"));
|
---|
| 173 | pDis->rc = VERR_DIS_TOO_LONG_INSTR;
|
---|
| 174 | ssize_t cbLeft = (ssize_t)(sizeof(pDis->u.abInstr) - offInstr);
|
---|
| 175 | switch (cbLeft)
|
---|
| 176 | {
|
---|
| 177 | case 1:
|
---|
| 178 | return pDis->u.abInstr[offInstr];
|
---|
| 179 | default:
|
---|
| 180 | if (cbLeft >= 2)
|
---|
| 181 | return RT_MAKE_U16(pDis->u.abInstr[offInstr], pDis->u.abInstr[offInstr + 1]);
|
---|
| 182 | return 0;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | * Function for handling a 32-bit cache miss.
|
---|
| 189 | *
|
---|
| 190 | * @returns The requested dword.
|
---|
| 191 | * @param pDis The disassembler state.
|
---|
| 192 | * @param offInstr The offset of the dword relative to the
|
---|
| 193 | * instruction.
|
---|
| 194 | */
|
---|
| 195 | DECLHIDDEN(uint32_t) disReadDWordSlow(PDISSTATE pDis, size_t offInstr)
|
---|
| 196 | {
|
---|
| 197 | if (RT_LIKELY(offInstr + 4 <= DIS_MAX_INSTR_LENGTH))
|
---|
| 198 | {
|
---|
| 199 | disReadMore(pDis, (uint8_t)offInstr, 4);
|
---|
| 200 | #ifdef DIS_HOST_UNALIGNED_ACCESS_OK
|
---|
| 201 | return *(uint32_t const *)&pDis->u.abInstr[offInstr];
|
---|
| 202 | #else
|
---|
| 203 | return RT_MAKE_U32_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 204 | pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3]);
|
---|
| 205 | #endif
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | Log(("disReadDWord: too long instruction...\n"));
|
---|
| 209 | pDis->rc = VERR_DIS_TOO_LONG_INSTR;
|
---|
| 210 | ssize_t cbLeft = (ssize_t)(sizeof(pDis->u.abInstr) - offInstr);
|
---|
| 211 | switch (cbLeft)
|
---|
| 212 | {
|
---|
| 213 | case 1:
|
---|
| 214 | return RT_MAKE_U32_FROM_U8(pDis->u.abInstr[offInstr], 0, 0, 0);
|
---|
| 215 | case 2:
|
---|
| 216 | return RT_MAKE_U32_FROM_U8(pDis->u.abInstr[offInstr], pDis->u.abInstr[offInstr + 1], 0, 0);
|
---|
| 217 | case 3:
|
---|
| 218 | return RT_MAKE_U32_FROM_U8(pDis->u.abInstr[offInstr], pDis->u.abInstr[offInstr + 1], pDis->u.abInstr[offInstr + 2], 0);
|
---|
| 219 | default:
|
---|
| 220 | if (cbLeft >= 4)
|
---|
| 221 | return RT_MAKE_U32_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 222 | pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3]);
|
---|
| 223 | return 0;
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * Function for handling a 64-bit cache miss.
|
---|
| 230 | *
|
---|
| 231 | * @returns The requested qword.
|
---|
| 232 | * @param pDis The disassembler state.
|
---|
| 233 | * @param offInstr The offset of the qword relative to the
|
---|
| 234 | * instruction.
|
---|
| 235 | */
|
---|
| 236 | DECLHIDDEN(uint64_t) disReadQWordSlow(PDISSTATE pDis, size_t offInstr)
|
---|
| 237 | {
|
---|
| 238 | if (RT_LIKELY(offInstr + 8 <= DIS_MAX_INSTR_LENGTH))
|
---|
| 239 | {
|
---|
| 240 | disReadMore(pDis, (uint8_t)offInstr, 8);
|
---|
| 241 | #ifdef DIS_HOST_UNALIGNED_ACCESS_OK
|
---|
| 242 | return *(uint64_t const *)&pDis->u.abInstr[offInstr];
|
---|
| 243 | #else
|
---|
| 244 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 245 | pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3],
|
---|
| 246 | pDis->u.abInstr[offInstr + 4], pDis->u.abInstr[offInstr + 5],
|
---|
| 247 | pDis->u.abInstr[offInstr + 6], pDis->u.abInstr[offInstr + 7]);
|
---|
| 248 | #endif
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | Log(("disReadQWord: too long instruction...\n"));
|
---|
| 252 | pDis->rc = VERR_DIS_TOO_LONG_INSTR;
|
---|
| 253 | ssize_t cbLeft = (ssize_t)(sizeof(pDis->u.abInstr) - offInstr);
|
---|
| 254 | switch (cbLeft)
|
---|
| 255 | {
|
---|
| 256 | case 1:
|
---|
| 257 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr], 0, 0, 0, 0, 0, 0, 0);
|
---|
| 258 | case 2:
|
---|
| 259 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr], pDis->u.abInstr[offInstr + 1], 0, 0, 0, 0, 0, 0);
|
---|
| 260 | case 3:
|
---|
| 261 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 262 | pDis->u.abInstr[offInstr + 2], 0, 0, 0, 0, 0);
|
---|
| 263 | case 4:
|
---|
| 264 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 265 | pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3],
|
---|
| 266 | 0, 0, 0, 0);
|
---|
| 267 | case 5:
|
---|
| 268 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 269 | pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3],
|
---|
| 270 | pDis->u.abInstr[offInstr + 4], 0, 0, 0);
|
---|
| 271 | case 6:
|
---|
| 272 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 273 | pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3],
|
---|
| 274 | pDis->u.abInstr[offInstr + 4], pDis->u.abInstr[offInstr + 5],
|
---|
| 275 | 0, 0);
|
---|
| 276 | case 7:
|
---|
| 277 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 278 | pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3],
|
---|
| 279 | pDis->u.abInstr[offInstr + 4], pDis->u.abInstr[offInstr + 5],
|
---|
| 280 | pDis->u.abInstr[offInstr + 6], 0);
|
---|
| 281 | default:
|
---|
| 282 | if (cbLeft >= 8)
|
---|
| 283 | return RT_MAKE_U64_FROM_U8(pDis->u.abInstr[offInstr ], pDis->u.abInstr[offInstr + 1],
|
---|
| 284 | pDis->u.abInstr[offInstr + 2], pDis->u.abInstr[offInstr + 3],
|
---|
| 285 | pDis->u.abInstr[offInstr + 4], pDis->u.abInstr[offInstr + 5],
|
---|
| 286 | pDis->u.abInstr[offInstr + 6], pDis->u.abInstr[offInstr + 7]);
|
---|
| 287 | return 0;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 |
|
---|
| 292 | /**
|
---|
| 293 | * Inlined worker that initializes the disassembler state.
|
---|
| 294 | *
|
---|
| 295 | * @returns The primary opcode map to use.
|
---|
| 296 | * @param pDis The disassembler state.
|
---|
| 297 | * @param uInstrAddr The instruction address.
|
---|
| 298 | * @param enmCpuMode The CPU mode.
|
---|
| 299 | * @param fFilter The instruction filter settings.
|
---|
| 300 | * @param pfnReadBytes The byte reader, can be NULL.
|
---|
| 301 | * @param pvUser The user data for the reader.
|
---|
| 302 | */
|
---|
| 303 | DECL_FORCE_INLINE(PCDISOPCODE)
|
---|
| 304 | disInitializeState(PDISSTATE pDis, RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, uint32_t fFilter,
|
---|
| 305 | PFNDISREADBYTES pfnReadBytes, void *pvUser)
|
---|
| 306 | {
|
---|
| 307 | RT_ZERO(*pDis);
|
---|
| 308 |
|
---|
[99241] | 309 | #ifdef VBOX_STRICT
|
---|
| 310 | pDis->Param1.uValue = UINT64_C(0xb1b1b1b1b1b1b1b1);
|
---|
| 311 | pDis->Param2.uValue = UINT64_C(0xb2b2b2b2b2b2b2b2);
|
---|
| 312 | pDis->Param3.uValue = UINT64_C(0xb3b3b3b3b3b3b3b3);
|
---|
| 313 | #endif
|
---|
| 314 |
|
---|
[99236] | 315 | pDis->rc = VINF_SUCCESS;
|
---|
| 316 | pDis->uInstrAddr = uInstrAddr;
|
---|
| 317 | pDis->pfnReadBytes = pfnReadBytes ? pfnReadBytes : disReadBytesDefault;
|
---|
| 318 | pDis->pvUser = pvUser;
|
---|
| 319 | pDis->uCpuMode = (uint8_t)enmCpuMode;
|
---|
[99241] | 320 |
|
---|
| 321 | switch (enmCpuMode)
|
---|
| 322 | {
|
---|
| 323 | case DISCPUMODE_16BIT:
|
---|
| 324 | case DISCPUMODE_32BIT:
|
---|
| 325 | case DISCPUMODE_64BIT:
|
---|
[99239] | 326 | #if defined(VBOX_DIS_WITH_X86_AMD64)
|
---|
[99241] | 327 | return disInitializeStateX86(pDis, enmCpuMode, fFilter);
|
---|
[99239] | 328 | #else
|
---|
[99241] | 329 | return NULL;
|
---|
[99239] | 330 | #endif
|
---|
[99241] | 331 | case DISCPUMODE_ARMV8_A64:
|
---|
| 332 | case DISCPUMODE_ARMV8_A32:
|
---|
| 333 | case DISCPUMODE_ARMV8_T32:
|
---|
| 334 | #if defined(VBOX_DIS_WITH_ARMV8)
|
---|
| 335 | return disInitializeStateArmV8(pDis, enmCpuMode, fFilter);
|
---|
| 336 | #else
|
---|
| 337 | return NULL;
|
---|
| 338 | #endif
|
---|
| 339 | default:
|
---|
| 340 | break;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | AssertReleaseFailed(); /* Should never get here. */
|
---|
| 344 | return NULL;
|
---|
[99236] | 345 | }
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 | /**
|
---|
| 349 | * Disassembles on instruction, details in @a pDis and length in @a pcbInstr.
|
---|
| 350 | *
|
---|
| 351 | * @returns VBox status code.
|
---|
| 352 | * @param uInstrAddr Address of the instruction to decode. What this means
|
---|
| 353 | * is left to the pfnReadBytes function.
|
---|
| 354 | * @param enmCpuMode The CPU mode. DISCPUMODE_32BIT, DISCPUMODE_16BIT, or DISCPUMODE_64BIT.
|
---|
| 355 | * @param pfnReadBytes Callback for reading instruction bytes.
|
---|
| 356 | * @param fFilter Instruction type filter.
|
---|
| 357 | * @param pvUser User argument for the instruction reader. (Ends up in pvUser.)
|
---|
| 358 | * @param pDis Pointer to disassembler state (output).
|
---|
| 359 | * @param pcbInstr Where to store the size of the instruction. (This
|
---|
| 360 | * is also stored in PDISSTATE::cbInstr.) Optional.
|
---|
| 361 | */
|
---|
| 362 | DISDECL(int) DISInstrEx(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, uint32_t fFilter,
|
---|
| 363 | PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
| 364 | PDISSTATE pDis, uint32_t *pcbInstr)
|
---|
| 365 | {
|
---|
| 366 |
|
---|
| 367 | PCDISOPCODE paOneByteMap = disInitializeState(pDis, uInstrAddr, enmCpuMode, fFilter, pfnReadBytes, pvUser);
|
---|
| 368 | disPrefetchBytes(pDis);
|
---|
[99241] | 369 |
|
---|
| 370 | switch (enmCpuMode)
|
---|
| 371 | {
|
---|
| 372 | case DISCPUMODE_16BIT:
|
---|
| 373 | case DISCPUMODE_32BIT:
|
---|
| 374 | case DISCPUMODE_64BIT:
|
---|
[99239] | 375 | #if defined(VBOX_DIS_WITH_X86_AMD64)
|
---|
[99241] | 376 | return disInstrWorkerX86(pDis, paOneByteMap, pcbInstr);
|
---|
[99239] | 377 | #else
|
---|
[99241] | 378 | return VERR_NOT_SUPPORTED;
|
---|
[99239] | 379 | #endif
|
---|
[99241] | 380 | case DISCPUMODE_ARMV8_A64:
|
---|
| 381 | case DISCPUMODE_ARMV8_A32:
|
---|
| 382 | case DISCPUMODE_ARMV8_T32:
|
---|
| 383 | #if defined(VBOX_DIS_WITH_ARMV8)
|
---|
| 384 | return disInstrWorkerArmV8(pDis, paOneByteMap, pcbInstr);
|
---|
| 385 | #else
|
---|
| 386 | return VERR_NOT_SUPPORTED;
|
---|
| 387 | #endif
|
---|
| 388 | default:
|
---|
| 389 | break;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | AssertReleaseFailed(); /* Should never get here. */
|
---|
| 393 | return VERR_INTERNAL_ERROR;
|
---|
[99236] | 394 | }
|
---|
| 395 |
|
---|
| 396 |
|
---|
| 397 | /**
|
---|
| 398 | * Disassembles on instruction partially or fully from prefetched bytes, details
|
---|
| 399 | * in @a pDis and length in @a pcbInstr.
|
---|
| 400 | *
|
---|
| 401 | * @returns VBox status code.
|
---|
| 402 | * @param uInstrAddr Address of the instruction to decode. What this means
|
---|
| 403 | * is left to the pfnReadBytes function.
|
---|
| 404 | * @param enmCpuMode The CPU mode. DISCPUMODE_32BIT, DISCPUMODE_16BIT, or DISCPUMODE_64BIT.
|
---|
| 405 | * @param pvPrefetched Pointer to the prefetched bytes.
|
---|
| 406 | * @param cbPrefetched The number of valid bytes pointed to by @a
|
---|
| 407 | * pbPrefetched.
|
---|
| 408 | * @param pfnReadBytes Callback for reading instruction bytes.
|
---|
| 409 | * @param fFilter Instruction type filter.
|
---|
| 410 | * @param pvUser User argument for the instruction reader. (Ends up in pvUser.)
|
---|
| 411 | * @param pDis Pointer to disassembler state (output).
|
---|
| 412 | * @param pcbInstr Where to store the size of the instruction. (This
|
---|
| 413 | * is also stored in PDISSTATE::cbInstr.) Optional.
|
---|
| 414 | */
|
---|
| 415 | DISDECL(int) DISInstrWithPrefetchedBytes(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, uint32_t fFilter,
|
---|
| 416 | void const *pvPrefetched, size_t cbPretched,
|
---|
| 417 | PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
| 418 | PDISSTATE pDis, uint32_t *pcbInstr)
|
---|
| 419 | {
|
---|
| 420 | PCDISOPCODE paOneByteMap = disInitializeState(pDis, uInstrAddr, enmCpuMode, fFilter, pfnReadBytes, pvUser);
|
---|
| 421 |
|
---|
| 422 | if (!cbPretched)
|
---|
| 423 | disPrefetchBytes(pDis);
|
---|
| 424 | else
|
---|
| 425 | {
|
---|
| 426 | if (cbPretched >= sizeof(pDis->u.abInstr))
|
---|
| 427 | {
|
---|
| 428 | memcpy(pDis->u.abInstr, pvPrefetched, sizeof(pDis->u.abInstr));
|
---|
| 429 | pDis->cbCachedInstr = (uint8_t)sizeof(pDis->u.abInstr);
|
---|
| 430 | }
|
---|
| 431 | else
|
---|
| 432 | {
|
---|
| 433 | memcpy(pDis->u.abInstr, pvPrefetched, cbPretched);
|
---|
| 434 | pDis->cbCachedInstr = (uint8_t)cbPretched;
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[99241] | 438 | switch (enmCpuMode)
|
---|
| 439 | {
|
---|
| 440 | case DISCPUMODE_16BIT:
|
---|
| 441 | case DISCPUMODE_32BIT:
|
---|
| 442 | case DISCPUMODE_64BIT:
|
---|
[99239] | 443 | #if defined(VBOX_DIS_WITH_X86_AMD64)
|
---|
[99241] | 444 | return disInstrWorkerX86(pDis, paOneByteMap, pcbInstr);
|
---|
[99239] | 445 | #else
|
---|
[99241] | 446 | return VERR_NOT_SUPPORTED;
|
---|
[99239] | 447 | #endif
|
---|
[99241] | 448 | case DISCPUMODE_ARMV8_A64:
|
---|
| 449 | case DISCPUMODE_ARMV8_A32:
|
---|
| 450 | case DISCPUMODE_ARMV8_T32:
|
---|
| 451 | #if defined(VBOX_DIS_WITH_ARMV8)
|
---|
| 452 | return disInstrWorkerArmV8(pDis, paOneByteMap, pcbInstr);
|
---|
| 453 | #else
|
---|
| 454 | return VERR_NOT_SUPPORTED;
|
---|
| 455 | #endif
|
---|
| 456 | default:
|
---|
| 457 | break;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | AssertReleaseFailed(); /* Should never get here. */
|
---|
| 461 | return VERR_INTERNAL_ERROR;
|
---|
[99236] | 462 | }
|
---|
| 463 |
|
---|
| 464 |
|
---|
| 465 | /**
|
---|
| 466 | * Parses one guest instruction.
|
---|
| 467 | *
|
---|
| 468 | * The result is found in pDis and pcbInstr.
|
---|
| 469 | *
|
---|
| 470 | * @returns VBox status code.
|
---|
| 471 | * @param uInstrAddr Address of the instruction to decode. What this means
|
---|
| 472 | * is left to the pfnReadBytes function.
|
---|
| 473 | * @param enmCpuMode The CPU mode. DISCPUMODE_32BIT, DISCPUMODE_16BIT, or DISCPUMODE_64BIT.
|
---|
| 474 | * @param pfnReadBytes Callback for reading instruction bytes.
|
---|
| 475 | * @param pvUser User argument for the instruction reader. (Ends up in pvUser.)
|
---|
| 476 | * @param pDis Pointer to disassembler state (output).
|
---|
| 477 | * @param pcbInstr Where to store the size of the instruction.
|
---|
| 478 | * NULL is allowed. This is also stored in
|
---|
| 479 | * PDISSTATE::cbInstr.
|
---|
| 480 | */
|
---|
| 481 | DISDECL(int) DISInstrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
| 482 | PDISSTATE pDis, uint32_t *pcbInstr)
|
---|
| 483 | {
|
---|
| 484 | return DISInstrEx(uInstrAddr, enmCpuMode, DISOPTYPE_ALL, pfnReadBytes, pvUser, pDis, pcbInstr);
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 |
|
---|
| 488 | /**
|
---|
| 489 | * Parses one guest instruction.
|
---|
| 490 | *
|
---|
| 491 | * The result is found in pDis and pcbInstr.
|
---|
| 492 | *
|
---|
| 493 | * @returns VBox status code.
|
---|
| 494 | * @param pvInstr Address of the instruction to decode. This is a
|
---|
| 495 | * real address in the current context that can be
|
---|
| 496 | * accessed without faulting. (Consider
|
---|
| 497 | * DISInstrWithReader if this isn't the case.)
|
---|
| 498 | * @param enmCpuMode The CPU mode. DISCPUMODE_32BIT, DISCPUMODE_16BIT, or DISCPUMODE_64BIT.
|
---|
| 499 | * @param pfnReadBytes Callback for reading instruction bytes.
|
---|
| 500 | * @param pvUser User argument for the instruction reader. (Ends up in pvUser.)
|
---|
| 501 | * @param pDis Pointer to disassembler state (output).
|
---|
| 502 | * @param pcbInstr Where to store the size of the instruction.
|
---|
| 503 | * NULL is allowed. This is also stored in
|
---|
| 504 | * PDISSTATE::cbInstr.
|
---|
| 505 | */
|
---|
| 506 | DISDECL(int) DISInstr(const void *pvInstr, DISCPUMODE enmCpuMode, PDISSTATE pDis, uint32_t *pcbInstr)
|
---|
| 507 | {
|
---|
| 508 | return DISInstrEx((uintptr_t)pvInstr, enmCpuMode, DISOPTYPE_ALL, NULL /*pfnReadBytes*/, NULL /*pvUser*/, pDis, pcbInstr);
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 |
|
---|
| 512 | #ifndef DIS_CORE_ONLY
|
---|
| 513 | /**
|
---|
[41658] | 514 | * Disassembles one instruction
|
---|
[1] | 515 | *
|
---|
[4953] | 516 | * @returns VBox error code
|
---|
[41669] | 517 | * @param pvInstr Pointer to the instruction to disassemble.
|
---|
[41789] | 518 | * @param enmCpuMode The CPU state.
|
---|
| 519 | * @param pDis The disassembler state (output).
|
---|
[41668] | 520 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
| 521 | * allowed.
|
---|
[41658] | 522 | * @param pszOutput Storage for disassembled instruction
|
---|
[41669] | 523 | * @param cbOutput Size of the output buffer.
|
---|
[1] | 524 | *
|
---|
| 525 | * @todo Define output callback.
|
---|
| 526 | */
|
---|
[41790] | 527 | DISDECL(int) DISInstrToStr(void const *pvInstr, DISCPUMODE enmCpuMode, PDISSTATE pDis, uint32_t *pcbInstr,
|
---|
[41669] | 528 | char *pszOutput, size_t cbOutput)
|
---|
[1] | 529 | {
|
---|
[41676] | 530 | return DISInstrToStrEx((uintptr_t)pvInstr, enmCpuMode, NULL, NULL, DISOPTYPE_ALL,
|
---|
[41789] | 531 | pDis, pcbInstr, pszOutput, cbOutput);
|
---|
[1] | 532 | }
|
---|
| 533 |
|
---|
[99236] | 534 |
|
---|
[1] | 535 | /**
|
---|
[41658] | 536 | * Disassembles one instruction with a byte fetcher caller.
|
---|
| 537 | *
|
---|
| 538 | * @returns VBox error code
|
---|
| 539 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
| 540 | * @param enmCpuMode The CPU mode.
|
---|
| 541 | * @param pfnCallback The byte fetcher callback.
|
---|
| 542 | * @param pvUser The user argument (found in
|
---|
[41790] | 543 | * DISSTATE::pvUser).
|
---|
[41789] | 544 | * @param pDis The disassembler state (output).
|
---|
[41668] | 545 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
| 546 | * allowed.
|
---|
[41671] | 547 | * @param pszOutput Storage for disassembled instruction.
|
---|
| 548 | * @param cbOutput Size of the output buffer.
|
---|
[41658] | 549 | *
|
---|
| 550 | * @todo Define output callback.
|
---|
| 551 | */
|
---|
[41671] | 552 | DISDECL(int) DISInstrToStrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
[41790] | 553 | PDISSTATE pDis, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
[41658] | 554 |
|
---|
| 555 | {
|
---|
[41676] | 556 | return DISInstrToStrEx(uInstrAddr, enmCpuMode, pfnReadBytes, pvUser, DISOPTYPE_ALL,
|
---|
[41789] | 557 | pDis, pcbInstr, pszOutput, cbOutput);
|
---|
[41658] | 558 | }
|
---|
| 559 |
|
---|
[99236] | 560 |
|
---|
[41658] | 561 | /**
|
---|
[1] | 562 | * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
|
---|
| 563 | *
|
---|
[4953] | 564 | * @returns VBox error code
|
---|
[41658] | 565 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
[41671] | 566 | * @param enmCpuMode The CPU mode.
|
---|
| 567 | * @param pfnCallback The byte fetcher callback.
|
---|
| 568 | * @param uFilter Instruction filter.
|
---|
[41789] | 569 | * @param pDis Where to return the disassembled instruction info.
|
---|
[41668] | 570 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
| 571 | * allowed.
|
---|
[41671] | 572 | * @param pszOutput Storage for disassembled instruction.
|
---|
| 573 | * @param cbOutput Size of the output buffer.
|
---|
[1] | 574 | *
|
---|
| 575 | * @todo Define output callback.
|
---|
| 576 | */
|
---|
[41671] | 577 | DISDECL(int) DISInstrToStrEx(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode,
|
---|
| 578 | PFNDISREADBYTES pfnReadBytes, void *pvUser, uint32_t uFilter,
|
---|
[41790] | 579 | PDISSTATE pDis, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
[1] | 580 | {
|
---|
[41886] | 581 | /* Don't filter if formatting is desired. */
|
---|
| 582 | if (uFilter != DISOPTYPE_ALL && pszOutput && cbOutput)
|
---|
[42284] | 583 | uFilter = DISOPTYPE_ALL;
|
---|
[41886] | 584 |
|
---|
| 585 | int rc = DISInstrEx(uInstrAddr, enmCpuMode, uFilter, pfnReadBytes, pvUser, pDis, pcbInstr);
|
---|
[41671] | 586 | if (RT_SUCCESS(rc) && pszOutput && cbOutput)
|
---|
[8361] | 587 | {
|
---|
[99319] | 588 | size_t cch = 0;
|
---|
| 589 |
|
---|
| 590 | switch (enmCpuMode)
|
---|
| 591 | {
|
---|
| 592 | case DISCPUMODE_16BIT:
|
---|
| 593 | case DISCPUMODE_32BIT:
|
---|
| 594 | case DISCPUMODE_64BIT:
|
---|
| 595 | #if defined(VBOX_DIS_WITH_X86_AMD64)
|
---|
| 596 | cch = DISFormatYasmEx(pDis, pszOutput, cbOutput,
|
---|
| 597 | DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
|
---|
| 598 | | DIS_FMT_FLAGS_RELATIVE_BRANCH | DIS_FMT_FLAGS_ADDR_LEFT,
|
---|
| 599 | NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
|
---|
| 600 | #else
|
---|
| 601 | AssertReleaseFailed(); /* Shouldn't ever get here (DISInstrEx() returning VERR_NOT_SUPPORTED). */
|
---|
| 602 | #endif
|
---|
| 603 | break;
|
---|
| 604 | case DISCPUMODE_ARMV8_A64:
|
---|
| 605 | case DISCPUMODE_ARMV8_A32:
|
---|
| 606 | case DISCPUMODE_ARMV8_T32:
|
---|
| 607 | #if defined(VBOX_DIS_WITH_ARMV8)
|
---|
| 608 | cch = DISFormatArmV8Ex(pDis, pszOutput, cbOutput,
|
---|
| 609 | DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
|
---|
| 610 | | DIS_FMT_FLAGS_RELATIVE_BRANCH | DIS_FMT_FLAGS_ADDR_LEFT,
|
---|
| 611 | NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
|
---|
| 612 | #else
|
---|
| 613 | AssertReleaseFailed(); /* Shouldn't ever get here (DISInstrEx() returning VERR_NOT_SUPPORTED). */
|
---|
| 614 | #endif
|
---|
| 615 | break;
|
---|
| 616 | default:
|
---|
| 617 | break;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
[41871] | 620 | if (cch + 2 <= cbOutput)
|
---|
[1] | 621 | {
|
---|
[41668] | 622 | pszOutput[cch++] = '\n';
|
---|
| 623 | pszOutput[cch] = '\0';
|
---|
[41658] | 624 | }
|
---|
[1] | 625 | }
|
---|
[41668] | 626 | return rc;
|
---|
[1] | 627 | }
|
---|
[99236] | 628 | #endif /* DIS_CORE_ONLY */
|
---|
[1] | 629 |
|
---|