Disasm.cpp revision 7de10baf03a4b6aaaf7bb0bc21db9d40b49a2ce1
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * VBox disassembler:
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * available from http://www.virtualbox.org. This file is free software;
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * additional information or have any questions.
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync/*******************************************************************************
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync* Header Files *
0c437bb10c61b229407a7517efde04dfe3b1e4a1vboxsync*******************************************************************************/
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * Disassembles a code block.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @returns VBox error code
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * set correctly.
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * @param pvCodeBlock Pointer to the strunction to disassemble.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param cbMax Maximum number of bytes to disassemble.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pcbSize Where to store the size of the instruction.
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * NULL is allowed.
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync * @todo Define output callback.
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync * @todo Using signed integers as sizes is a bit odd. There are still
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync * some GCC warnings about mixing signed and unsigend integers.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @todo Need to extend this interface to include a code address so we
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync * can dissassemble GC code. Perhaps a new function is better...
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @remark cbMax isn't respected as a boundry. DISInstr() will read beyond cbMax.
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync * This means *pcbSize >= cbMax sometimes.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsyncDISDECL(int) DISBlock(PDISCPUSTATE pCpu, RTUINTPTR pvCodeBlock, unsigned cbMax, unsigned *pSize)
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync unsigned i = 0;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync int rc = DISInstr(pCpu, pvCodeBlock + i, 0, &cbInstr, szOutput);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync return true;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Disassembles one instruction
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @returns VBox error code
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * set correctly.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * @param pu8Instruction Pointer to the strunction to disassemble.
f94f82d66536c7332c347dd9a3a9f0f8c79247f4vboxsync * @param u32EipOffset Offset to add to instruction address to get the real virtual address
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * @param pcbSize Where to store the size of the instruction.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * NULL is allowed.
43dff6077acb4176145b18bdb862eb73620182d2vboxsync * @param pszOutput Storage for disassembled instruction
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * @todo Define output callback.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsyncDISDECL(int) DISInstr(PDISCPUSTATE pCpu, RTUINTPTR pu8Instruction, unsigned u32EipOffset, unsigned *pcbSize,
b40179b44fea65b72b2f226f62af1ed7bd3c48fcvboxsync return DISInstrEx(pCpu, pu8Instruction, u32EipOffset, pcbSize, pszOutput, OPTYPE_ALL);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
0d4bc23ca3867d6dbedd76d5b1e3725c766adb75vboxsync * @returns VBox error code
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * set correctly.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param pu8Instruction Pointer to the strunction to disassemble.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param u32EipOffset Offset to add to instruction address to get the real virtual address
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param pcbSize Where to store the size of the instruction.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * NULL is allowed.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param pszOutput Storage for disassembled instruction
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param uFilter Instruction type filter
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @todo Define output callback.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsyncDISDECL(int) DISInstrEx(PDISCPUSTATE pCpu, RTUINTPTR pu8Instruction, unsigned u32EipOffset, unsigned *pcbSize,
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync unsigned i = 0, prefixbytes;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync //reset instruction settings
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#ifndef __L4ENV__ /* Unfortunately, we have no exception handling in l4env */
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync uint8_t codebyte = DISReadByte(pCpu, pu8Instruction+i);
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync /* Hardcoded assumption about OP_* values!! */
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync /* The REX prefix must precede the opcode byte(s). Any other placement is ignored. */
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync#if 0 //defined (DEBUG_Sander)
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync // segment override prefix byte
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync pCpu->enmPrefixSeg = (DIS_SELREG)(paOneByteMap[codebyte].param1 - OP_PARM_REG_SEG_START);
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync /* Segment prefixes for CS, DS, ES and SS are ignored in long mode. */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync i += sizeof(uint8_t);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync continue; //fetch the next byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync // lock prefix byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync i += sizeof(uint8_t);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync continue; //fetch the next byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync // address size override prefix byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync i += sizeof(uint8_t);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync continue; //fetch the next byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync // operand size override prefix byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pCpu->opmode = CPUMODE_16BIT; /* for 32 and 64 bits mode (there is no 32 bits operand size override prefix) */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync i += sizeof(uint8_t);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync continue; //fetch the next byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync // rep and repne are not really prefixes, but we'll treat them as such
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync i += sizeof(uint8_t);
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync continue; //fetch the next byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync i += sizeof(uint8_t);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync continue; //fetch the next byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* REX prefix byte */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pCpu->prefix_rex = PREFIX_REX_OP_2_FLAGS(paOneByteMap[codebyte].param1);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync i += sizeof(uint8_t);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pCpu->opmode = CPUMODE_64BIT; /* overrides size prefix byte */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync continue; //fetch the next byte
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* Prefix byte(s) is/are part of the instruction. */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pCpu->opaddr = pu8Instruction + idx + u32EipOffset - prefixbytes;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync inc = ParseInstruction(pu8Instruction + i, &paOneByteMap[pCpu->opcode], pCpu);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pCpu->opsize = prefixbytes + inc + sizeof(uint8_t);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync disasmSprintf(pszOutput, pu8Instruction+i-1-prefixbytes, pCpu, &pCpu->param1, &pCpu->param2, &pCpu->param3);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync else /* setjmp has returned a non-zero value: an exception occured */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync//*****************************************************************************
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync//*****************************************************************************
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsyncchar *DbgBytesToString(PDISCPUSTATE pCpu, RTUINTPTR pBytes, int size, char *pszOutput)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync for(i = 0; i < size; i++)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync RTStrPrintf(szByte, sizeof(szByte), "%02X ", DISReadByte(pCpu, pBytes+i));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, szByte);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync//*****************************************************************************
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync//*****************************************************************************
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsyncvoid disasmSprintf(char *pszOutput, RTUINTPTR pu8Instruction, PDISCPUSTATE pCpu, OP_PARAMETER *pParam1, OP_PARAMETER *pParam2, OP_PARAMETER *pParam3)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync RTStrPrintf(pszOutput, 64, "%08X: ", (unsigned)pCpu->opaddr);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "lock ");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "rep(e) ");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "repne ");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "Invalid Opcode [%02X][%02X]", DISReadByte(pCpu, pu8Instruction), DISReadByte(pCpu, pu8Instruction+1) );
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync AssertMsg(param == 1, ("Invalid branch parameter nr"));
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync uint32_t addr = (uint32_t)(pCpu->opaddr + pCpu->opsize) + disp;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "[%08X]", addr);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync //no break;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync case 'Q': //ModRM byte selects MMX register or memory address
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync case 'R': //ModRM byte may only refer to a general register
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync case 'W': //ModRM byte selects an XMM/SSE register or a memory address
1986f56777969a25707ab214f8dd070804be666cvboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, pParam1->szParam);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, pParam2->szParam);
1986f56777969a25707ab214f8dd070804be666cvboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, pParam3->szParam);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync case 'e': //register based on operand size (e.g. %eAX)
1986f56777969a25707ab214f8dd070804be666cvboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "E");
1986f56777969a25707ab214f8dd070804be666cvboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "R");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "%c%c", lpszFormat[2], lpszFormat[3]);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync //Go to the next parameter in the format string
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync while(*lpszFormat && *lpszFormat != ',') lpszFormat++;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "%c", *lpszFormat);
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync DbgBytesToString(pCpu, pu8Instruction, pCpu->opsize, pszOutput);
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync RTStrPrintf(&pszOutput[strlen(pszOutput)], 64, "\n");
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync//*****************************************************************************
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync//*****************************************************************************