From patchwork Thu Sep 14 18:35:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 112613 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp1097823qgf; Thu, 14 Sep 2017 11:40:31 -0700 (PDT) X-Received: by 10.55.148.194 with SMTP id w185mr4082273qkd.354.1505414431143; Thu, 14 Sep 2017 11:40:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1505414431; cv=none; d=google.com; s=arc-20160816; b=DT3xdQNGe1FDLG8w5kGOQS/B76Bs/004hHN4LzD1TcR3tDmOI/XLvcjmrGbMwndEua OToJLISx2l3HyE0LTnI1mzolFJGgaixgI945fxrunDe+DJ04g0MKPh4nBu4GrLy4XjKK 7qNCPoHbE2La3q5a3u3dJJHRu4pfYxuDf8/ne5dPw+ln3wK5M5TVrD9qmr9e1/C4VFq8 R22iVtsVrasqQCVUHh3BAmSUt0jX0MkKrUTfoFj91w/hAVUfF1mAVQfZWiMnQOloiehS km1qaMauJTW6sCOtJGfNABNktaieoPPW0qRsYOD7lSyG5dyTLSM1E3IGQmiXm9rQV9iV mmwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=cHa2/xB6nYGL9i+D4GhHYh/dVeiKGQ/gqH8P2cYg23w=; b=gGSUqytO2/xD7qjx2H1+sJJTzOO92b1LN7DJ7KRXx4CtUN7Wh42MkQrpqZ5AQxQM7e jlK6SK89omCnIlw+X54FxV9qDd6U1PMpKtg2fnXOiQFrYb3bSbqgHQXE2gu/SNeHXExQ QgYU42czaTNp3phE4ocV0cZQO8vGoIVoVHZVf1jl50YLkv9knKq2RS68TTyVHexewayL jHC69QHNkjWL7YyNDUPMioLX2XYunMHQJoYIJraq5WUKJS0v/hyRAur/r5CRD7zj2ttU cc4NSxItKQ2DHmC3lLcFl4WnkOS2gHimh6xkf0/r9N/C+v1SP3vSPfiqlhq/rnbt/A5G vQbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=bIIohRKQ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id y32si642298qtk.80.2017.09.14.11.40.30 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 14 Sep 2017 11:40:31 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=bIIohRKQ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:49467 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsZ3s-0004bL-T6 for patch@linaro.org; Thu, 14 Sep 2017 14:40:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49623) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsYz1-0000Pr-Uh for qemu-devel@nongnu.org; Thu, 14 Sep 2017 14:35:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsYyz-0000T5-On for qemu-devel@nongnu.org; Thu, 14 Sep 2017 14:35:27 -0400 Received: from mail-pf0-x22e.google.com ([2607:f8b0:400e:c00::22e]:47702) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dsYyz-0000RT-GZ for qemu-devel@nongnu.org; Thu, 14 Sep 2017 14:35:25 -0400 Received: by mail-pf0-x22e.google.com with SMTP id u12so118857pfl.4 for ; Thu, 14 Sep 2017 11:35:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=cHa2/xB6nYGL9i+D4GhHYh/dVeiKGQ/gqH8P2cYg23w=; b=bIIohRKQ63BXLIFmmltgDrcd42F5EGBO7I7Qk60iTF/WsSCD3UzESBama8kUZg/q52 ja33e8OJHzsPslkT8fczA1gDcDOSrbHAuRPWLbgikdN7x+WjiHVtvZHRReyoUcHdMPPl 6/tLTvv79I1N0THoIlXOoJqL3iDwGivLSNCNE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=cHa2/xB6nYGL9i+D4GhHYh/dVeiKGQ/gqH8P2cYg23w=; b=TLL0N3j6GeXjpIg1Ox2VAW8V46W4Jxs7MzAKddTJd2R09oGdVFobnr5aS/icX8Ix81 9RzVY597yZ75MNAANW2CVwz/+GGCWt7cbNQMWou49M7kqepz+mKCruHR8IteQFgp7ge1 KDmmruuggF6UZmZrGAGj7ld8BFMDUDynrnBBTMw5IKLNsJxV/bXQgYNkfe/webtN+qrz XPIZA83rWGG/9/0E9auN6nnvzh0mbaSh/U/qtCjOjnUPZCrGL5ge2tnJimafLSLpcUOe +s6lSEy4V8PAarFpJhi3lAoFEZ2cPSvAwFFeyjrceWBaUngc24TBteFAkd7xh5CIXSpt 1r+g== X-Gm-Message-State: AHPjjUjx/LfDhaMQRIOSGsbrktfV0YNR7Y7MOfkA+jnnhHrJPlvoFqG1 6zdZyhlGGA+Rx+RFf0f16A== X-Google-Smtp-Source: ADKCNb5vWdQxMvG1j6PpXfL3S+gNkoUpc86dDTrWpnP6HRdQSz3XDakq6nfDYXeAyh3i5XBcOdZ8nw== X-Received: by 10.98.155.76 with SMTP id r73mr22490118pfd.182.1505414124184; Thu, 14 Sep 2017 11:35:24 -0700 (PDT) Received: from bigtime.twiddle.net (97-126-103-167.tukw.qwest.net. [97.126.103.167]) by smtp.gmail.com with ESMTPSA id l85sm32473848pfb.176.2017.09.14.11.35.22 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 14 Sep 2017 11:35:23 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 14 Sep 2017 11:35:10 -0700 Message-Id: <20170914183516.19537-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20170914183516.19537-1-richard.henderson@linaro.org> References: <20170914183516.19537-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::22e Subject: [Qemu-devel] [PATCH 04/10] disas: Support the Capstone disassembler library X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" If configured, prefer this over our rather dated copy of the GPLv2-only binutils. This will be especially apparent with the proposed vector extensions to TCG, as disas/i386.c does not handle AVX. Signed-off-by: Richard Henderson --- include/disas/bfd.h | 4 ++ include/disas/capstone.h | 38 +++++++++++++++++++ disas.c | 99 ++++++++++++++++++++++++++++++++++++++++++------ configure | 17 +++++++++ 4 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 include/disas/capstone.h -- 2.13.5 diff --git a/include/disas/bfd.h b/include/disas/bfd.h index b01e002b4c..0f4ecdeb88 100644 --- a/include/disas/bfd.h +++ b/include/disas/bfd.h @@ -377,6 +377,10 @@ typedef struct disassemble_info { /* Command line options specific to the target disassembler. */ char * disassembler_options; + /* Options for Capstone disassembly. */ + int cap_arch; + int cap_mode; + } disassemble_info; diff --git a/include/disas/capstone.h b/include/disas/capstone.h new file mode 100644 index 0000000000..84e214956d --- /dev/null +++ b/include/disas/capstone.h @@ -0,0 +1,38 @@ +#ifndef QEMU_CAPSTONE_H +#define QEMU_CAPSTONE_H 1 + +#ifdef CONFIG_CAPSTONE + +#include + +#else + +/* Just enough to allow backends to init without ifdefs. */ + +#define CS_ARCH_ARM -1 +#define CS_ARCH_ARM64 -1 +#define CS_ARCH_MIPS -1 +#define CS_ARCH_X86 -1 +#define CS_ARCH_PPC -1 +#define CS_ARCH_SPARC -1 +#define CS_ARCH_SYSZ -1 + +#define CS_MODE_LITTLE_ENDIAN 0 +#define CS_MODE_BIG_ENDIAN 0 +#define CS_MODE_ARM 0 +#define CS_MODE_16 0 +#define CS_MODE_32 0 +#define CS_MODE_64 0 +#define CS_MODE_THUMB 0 +#define CS_MODE_MCLASS 0 +#define CS_MODE_V8 0 +#define CS_MODE_MICRO 0 +#define CS_MODE_MIPS3 0 +#define CS_MODE_MIPS32R6 0 +#define CS_MODE_MIPSGP64 0 +#define CS_MODE_V9 0 +#define CS_MODE_MIPS32 0 +#define CS_MODE_MIPS64 0 + +#endif /* CONFIG_CAPSTONE */ +#endif /* QEMU_CAPSTONE_H */ diff --git a/disas.c b/disas.c index ad675dc361..76ea76b026 100644 --- a/disas.c +++ b/disas.c @@ -6,6 +6,7 @@ #include "cpu.h" #include "disas/disas.h" +#include "disas/capstone.h" typedef struct CPUDebug { struct disassemble_info info; @@ -171,6 +172,57 @@ static int print_insn_od_target(bfd_vma pc, disassemble_info *info) return print_insn_objdump(pc, info, "OBJD-T"); } +static bool cap_disas(disassemble_info *info, uint64_t pc, size_t size) +{ + bool ret = false; +#ifdef CONFIG_CAPSTONE + csh handle; + cs_insn *insn; + uint8_t *buf; + const uint8_t *cbuf; + uint64_t pc_start; + cs_mode cap_mode = info->cap_mode; + + cap_mode += (info->endian == BFD_ENDIAN_BIG ? CS_MODE_BIG_ENDIAN + : CS_MODE_LITTLE_ENDIAN); + + if (cs_open(info->cap_arch, cap_mode, &handle) != CS_ERR_OK) { + goto err0; + } + + /* ??? There probably ought to be a better place to put this. */ + if (info->cap_arch == CS_ARCH_X86) { + /* We don't care about errors (if for some reason the library + is compiled without AT&T syntax); the user will just have + to deal with the Intel syntax. */ + cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); + } + + insn = cs_malloc(handle); + if (insn == NULL) { + goto err1; + } + + cbuf = buf = g_malloc(size); + info->read_memory_func(pc, buf, size, info); + + pc_start = pc; + while (cs_disasm_iter(handle, &cbuf, &size, &pc, insn)) { + (*info->fprintf_func)(info->stream, + "0x%08" PRIx64 ": %-12s %s\n", + pc_start, insn->mnemonic, insn->op_str); + pc_start = pc; + } + ret = true; + + g_free(buf); + err1: + cs_close(&handle); + err0: +#endif /* CONFIG_CAPSTONE */ + return ret; +} + /* Disassemble this for me please... (debugging). */ void target_disas(FILE *out, CPUState *cpu, target_ulong code, target_ulong size) @@ -188,6 +240,8 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong code, s.info.buffer_vma = code; s.info.buffer_length = size; s.info.print_address_func = generic_print_address; + s.info.cap_arch = -1; + s.info.cap_mode = 0; #ifdef TARGET_WORDS_BIGENDIAN s.info.endian = BFD_ENDIAN_BIG; @@ -199,6 +253,10 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong code, cc->disas_set_info(cpu, &s.info); } + if (s.info.cap_arch >= 0 && cap_disas(&s.info, code, size)) { + return; + } + if (s.info.print_insn == NULL) { s.info.print_insn = print_insn_od_target; } @@ -206,18 +264,6 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong code, for (pc = code; size > 0; pc += count, size -= count) { fprintf(out, "0x" TARGET_FMT_lx ": ", pc); count = s.info.print_insn(pc, &s.info); -#if 0 - { - int i; - uint8_t b; - fprintf(out, " {"); - for(i = 0; i < count; i++) { - target_read_memory(pc + i, &b, 1, &s.info); - fprintf(out, " %02x", b); - } - fprintf(out, " }"); - } -#endif fprintf(out, "\n"); if (count < 0) break; @@ -245,6 +291,8 @@ void disas(FILE *out, void *code, unsigned long size) s.info.buffer = code; s.info.buffer_vma = (uintptr_t)code; s.info.buffer_length = size; + s.info.cap_arch = -1; + s.info.cap_mode = 0; #ifdef HOST_WORDS_BIGENDIAN s.info.endian = BFD_ENDIAN_BIG; @@ -256,21 +304,34 @@ void disas(FILE *out, void *code, unsigned long size) #elif defined(__i386__) s.info.mach = bfd_mach_i386_i386; print_insn = print_insn_i386; + s.info.cap_arch = CS_ARCH_X86; + s.info.cap_mode = CS_MODE_32; #elif defined(__x86_64__) s.info.mach = bfd_mach_x86_64; print_insn = print_insn_i386; + s.info.cap_arch = CS_ARCH_X86; + s.info.cap_mode = CS_MODE_64; #elif defined(_ARCH_PPC) s.info.disassembler_options = (char *)"any"; print_insn = print_insn_ppc; + s.info.cap_arch = CS_ARCH_PPC; +# ifdef _ARCH_PPC64 + s.info.cap_mode = CS_MODE_64; +# endif #elif defined(__aarch64__) && defined(CONFIG_ARM_A64_DIS) print_insn = print_insn_arm_a64; + s.info.cap_arch = CS_ARCH_ARM64; #elif defined(__alpha__) print_insn = print_insn_alpha; #elif defined(__sparc__) print_insn = print_insn_sparc; s.info.mach = bfd_mach_sparc_v9b; + s.info.cap_arch = CS_ARCH_SPARC; + s.info.cap_mode = CS_MODE_V9; #elif defined(__arm__) print_insn = print_insn_arm; + s.info.cap_arch = CS_ARCH_ARM; + /* TCG only generates code for arm mode. */ #elif defined(__MIPSEB__) print_insn = print_insn_big_mips; #elif defined(__MIPSEL__) @@ -279,9 +340,15 @@ void disas(FILE *out, void *code, unsigned long size) print_insn = print_insn_m68k; #elif defined(__s390__) print_insn = print_insn_s390; + s.info.cap_arch = CS_ARCH_SYSZ; #elif defined(__hppa__) print_insn = print_insn_hppa; #endif + + if (s.info.cap_arch >= 0 && cap_disas(&s.info, (uintptr_t)code, size)) { + return; + } + if (print_insn == NULL) { print_insn = print_insn_od_host; } @@ -357,6 +424,14 @@ void monitor_disas(Monitor *mon, CPUState *cpu, cc->disas_set_info(cpu, &s.info); } + /* ??? Capstone requires that we copy the data into a host-addressable + buffer first and has no call-back to read more. Therefore we need + an estimate of buffer size. This will work for most RISC, but we'll + need to figure out something else for variable-length ISAs. */ + if (s.info.cap_arch >= 0 && cap_disas(&s.info, pc, 4 * nb_insn)) { + return; + } + if (!s.info.print_insn) { monitor_printf(mon, "0x" TARGET_FMT_lx ": Asm output not supported on this arch\n", pc); diff --git a/configure b/configure index fd7e3a5e81..a55a8eda8a 100755 --- a/configure +++ b/configure @@ -366,6 +366,7 @@ opengl_dmabuf="no" cpuid_h="no" avx2_opt="no" zlib="yes" +capstone="" lzo="" snappy="" bzip2="" @@ -4381,6 +4382,18 @@ EOF fi ########################################## +# capstone + +if test "$capstone" != no; then + capstone=no + if $pkg_config capstone; then + capstone=yes + QEMU_CFLAGS="$QEMU_CFLAGS $($pkg_config --cflags capstone)" + LDFLAGS="$LDFLAGS $($pkg_config --libs capstone)" + fi +fi + +########################################## # check if we have fdatasync fdatasync=no @@ -5402,6 +5415,7 @@ echo "jemalloc support $jemalloc" echo "avx2 optimization $avx2_opt" echo "replication support $replication" echo "VxHS block device $vxhs" +echo "capstone $capstone" if test "$sdl_too_old" = "yes"; then echo "-> Your SDL version is too old - please upgrade to have SDL support" @@ -6050,6 +6064,9 @@ fi if test "$ivshmem" = "yes" ; then echo "CONFIG_IVSHMEM=y" >> $config_host_mak fi +if test "$capstone" = "yes" ; then + echo "CONFIG_CAPSTONE=y" >> $config_host_mak +fi # Hold two types of flag: # CONFIG_THREAD_SETNAME_BYTHREAD - we've got a way of setting the name on