From nobody Sat Nov 1 22:28:51 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527687006050508.0197937731242; Wed, 30 May 2018 06:30:06 -0700 (PDT) Received: from localhost ([::1]:38735 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fO1Az-0002WG-8C for importer@patchew.org; Wed, 30 May 2018 09:30:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44270) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fO0n4-0001Tq-B2 for qemu-devel@nongnu.org; Wed, 30 May 2018 09:05:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fO0n2-0001I7-Ty for qemu-devel@nongnu.org; Wed, 30 May 2018 09:05:22 -0400 Received: from mail.ispras.ru ([83.149.199.45]:51842) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fO0n2-0001BL-8y for qemu-devel@nongnu.org; Wed, 30 May 2018 09:05:20 -0400 Received: from [127.0.1.1] (unknown [85.142.117.226]) by mail.ispras.ru (Postfix) with ESMTPSA id 978B6540184 for ; Wed, 30 May 2018 16:05:19 +0300 (MSK) From: Pavel Dovgalyuk To: qemu-devel@nongnu.org Date: Wed, 30 May 2018 16:05:19 +0300 Message-ID: <152768551938.13955.9382831286362765211.stgit@pasha-ThinkPad-T60> In-Reply-To: <152768551387.13955.7085403440722380428.stgit@pasha-ThinkPad-T60> References: <152768551387.13955.7085403440722380428.stgit@pasha-ThinkPad-T60> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 83.149.199.45 X-Mailman-Approved-At: Wed, 30 May 2018 09:27:20 -0400 Subject: [Qemu-devel] [RFC PATCH v1 1/2] tcg: add headers for non-target helpers 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 This patch adds functions and headers for adding the helpers from the modules other than the target translators. Signed-off-by: Pavel Dovgalyuk --- include/exec/helper-register.h | 53 ++++++++++++++++++++++++++++++++++++= ++++ tcg/tcg.c | 12 +++++++++ tcg/tcg.h | 3 ++ 3 files changed, 68 insertions(+) create mode 100644 include/exec/helper-register.h diff --git a/include/exec/helper-register.h b/include/exec/helper-register.h new file mode 100644 index 0000000..89b7b9b --- /dev/null +++ b/include/exec/helper-register.h @@ -0,0 +1,53 @@ +#ifndef HELPER_REGISTER_H +#define HELPER_REGISTER_H + +#include "exec/helper-head.h" + +/* Need one more level of indirection before stringification + to get all the macros expanded first. */ +#define str(s) #s + +#define DEF_HELPER_FLAGS_0(NAME, FLAGS, ret) \ + tcg_register_helper(HELPER(NAME), str(NAME), FLAGS, dh_sizemask(ret, 0= )); + +#define DEF_HELPER_FLAGS_1(NAME, FLAGS, ret, t1) \ + tcg_register_helper(HELPER(NAME), str(NAME), FLAGS, \ + dh_sizemask(ret, 0) | dh_sizemask(t1, 1)); + +#define DEF_HELPER_FLAGS_2(NAME, FLAGS, ret, t1, t2) \ + tcg_register_helper(HELPER(NAME), str(NAME), FLAGS, \ + dh_sizemask(ret, 0) | dh_sizemask(t1, 1) | dh_sizemask(t2, 2)); + +#define DEF_HELPER_FLAGS_3(NAME, FLAGS, ret, t1, t2, t3) \ + tcg_register_helper(HELPER(NAME), str(NAME), FLAGS, \ + dh_sizemask(ret, 0) | dh_sizemask(t1, 1) | dh_sizemask(t2, 2) \ + | dh_sizemask(t3, 3)); + +#define DEF_HELPER_FLAGS_4(NAME, FLAGS, ret, t1, t2, t3, t4) \ + tcg_register_helper(HELPER(NAME), str(NAME), FLAGS, \ + dh_sizemask(ret, 0) | dh_sizemask(t1, 1) | dh_sizemask(t2, 2) \ + | dh_sizemask(t3, 3) | dh_sizemask(t4, 4)); + +#define DEF_HELPER_FLAGS_5(NAME, FLAGS, ret, t1, t2, t3, t4, t5) \ + tcg_register_helper(HELPER(NAME), str(NAME), FLAGS, \ + dh_sizemask(ret, 0) | dh_sizemask(t1, 1) | dh_sizemask(t2, 2) \ + | dh_sizemask(t3, 3) | dh_sizemask(t4, 4) | dh_sizemask(t5, 5)); + +#define DEF_HELPER_FLAGS_6(NAME, FLAGS, ret, t1, t2, t3, t4, t5, t6) \ + tcg_register_helper(HELPER(NAME), str(NAME), FLAGS, \ + dh_sizemask(ret, 0) | dh_sizemask(t1, 1) | dh_sizemask(t2, 2) \ + | dh_sizemask(t3, 3) | dh_sizemask(t4, 4) | dh_sizemask(t5, 5) \ + | dh_sizemask(t6, 6)); + +#include "helper.h" + +#undef str +#undef DEF_HELPER_FLAGS_0 +#undef DEF_HELPER_FLAGS_1 +#undef DEF_HELPER_FLAGS_2 +#undef DEF_HELPER_FLAGS_3 +#undef DEF_HELPER_FLAGS_4 +#undef DEF_HELPER_FLAGS_5 +#undef DEF_HELPER_FLAGS_6 + +#endif // HELPER_REGISTER_H diff --git a/tcg/tcg.c b/tcg/tcg.c index 6eeebe0..8191381 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -1623,6 +1623,18 @@ static inline const char *tcg_find_helper(TCGContext= *s, uintptr_t val) return ret; } =20 +void tcg_register_helper(void *func, const char *name, + unsigned flags, unsigned sizemask) +{ + TCGHelperInfo *info =3D g_new0(TCGHelperInfo, 1); + info->func =3D func; + info->name =3D name; + info->flags =3D flags; + info->sizemask =3D sizemask; + + g_hash_table_insert(helper_table, func, info); +} + static const char * const cond_name[] =3D { [TCG_COND_NEVER] =3D "never", diff --git a/tcg/tcg.h b/tcg/tcg.h index 08f8bbf..7a4b750 100644 --- a/tcg/tcg.h +++ b/tcg/tcg.h @@ -890,6 +890,9 @@ void tcg_register_thread(void); void tcg_prologue_init(TCGContext *s); void tcg_func_start(TCGContext *s); =20 +void tcg_register_helper(void *func, const char *name, + unsigned flags, unsigned sizemask); + int tcg_gen_code(TCGContext *s, TranslationBlock *tb); =20 void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t siz= e); From nobody Sat Nov 1 22:28:51 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527686961590169.79325180014428; Wed, 30 May 2018 06:29:21 -0700 (PDT) Received: from localhost ([::1]:38733 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fO1A4-0001p6-F2 for importer@patchew.org; Wed, 30 May 2018 09:29:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44317) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fO0nD-0001cv-GI for qemu-devel@nongnu.org; Wed, 30 May 2018 09:05:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fO0n8-00021a-7g for qemu-devel@nongnu.org; Wed, 30 May 2018 09:05:31 -0400 Received: from mail.ispras.ru ([83.149.199.45]:51848) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fO0n7-0001uL-K0 for qemu-devel@nongnu.org; Wed, 30 May 2018 09:05:26 -0400 Received: from [127.0.1.1] (unknown [85.142.117.226]) by mail.ispras.ru (Postfix) with ESMTPSA id E3B75540184 for ; Wed, 30 May 2018 16:05:24 +0300 (MSK) From: Pavel Dovgalyuk To: qemu-devel@nongnu.org Date: Wed, 30 May 2018 16:05:24 +0300 Message-ID: <152768552472.13955.11576907392919187527.stgit@pasha-ThinkPad-T60> In-Reply-To: <152768551387.13955.7085403440722380428.stgit@pasha-ThinkPad-T60> References: <152768551387.13955.7085403440722380428.stgit@pasha-ThinkPad-T60> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 83.149.199.45 X-Mailman-Approved-At: Wed, 30 May 2018 09:27:20 -0400 Subject: [Qemu-devel] [RFC PATCH v1 2/2] tcg: add instrumenting module 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 This is a samples of the instrumenting interface and implementation of some instruction tracing tasks. Signed-off-by: Pavel Dovgalyuk --- accel/tcg/Makefile.objs | 1=20 accel/tcg/instrument/Makefile.objs | 1=20 accel/tcg/instrument/helper.h | 1=20 accel/tcg/instrument/instrument.c | 82 ++++++++++++++++++++++++++++++++= ++++ accel/tcg/instrument/instrument.h | 11 +++++ accel/tcg/translate-all.c | 2 + accel/tcg/translator.c | 5 ++ 7 files changed, 103 insertions(+) create mode 100644 accel/tcg/instrument/Makefile.objs create mode 100644 accel/tcg/instrument/helper.h create mode 100644 accel/tcg/instrument/instrument.c create mode 100644 accel/tcg/instrument/instrument.h diff --git a/accel/tcg/Makefile.objs b/accel/tcg/Makefile.objs index d381a02..fed2b6c 100644 --- a/accel/tcg/Makefile.objs +++ b/accel/tcg/Makefile.objs @@ -3,6 +3,7 @@ obj-$(CONFIG_SOFTMMU) +=3D cputlb.o obj-y +=3D tcg-runtime.o tcg-runtime-gvec.o obj-y +=3D cpu-exec.o cpu-exec-common.o translate-all.o obj-y +=3D translator.o +obj-y +=3D instrument/ =20 obj-$(CONFIG_USER_ONLY) +=3D user-exec.o obj-$(call lnot,$(CONFIG_SOFTMMU)) +=3D user-exec-stub.o diff --git a/accel/tcg/instrument/Makefile.objs b/accel/tcg/instrument/Make= file.objs new file mode 100644 index 0000000..f40c75a --- /dev/null +++ b/accel/tcg/instrument/Makefile.objs @@ -0,0 +1 @@ +obj-$(CONFIG_TCG) +=3D instrument.o diff --git a/accel/tcg/instrument/helper.h b/accel/tcg/instrument/helper.h new file mode 100644 index 0000000..007b395 --- /dev/null +++ b/accel/tcg/instrument/helper.h @@ -0,0 +1 @@ +DEF_HELPER_2(before_insn, void, tl, ptr) diff --git a/accel/tcg/instrument/instrument.c b/accel/tcg/instrument/instr= ument.c new file mode 100644 index 0000000..076810d --- /dev/null +++ b/accel/tcg/instrument/instrument.c @@ -0,0 +1,82 @@ +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qemu/error-report.h" +#include "cpu.h" +#include "tcg/tcg.h" +#include "tcg/tcg-op.h" +#include "exec/exec-all.h" +#include "exec/log.h" +#include "exec/translator.h" +#include "exec/helper-proto.h" +#include "exec/helper-gen.h" +#include "qemu/log.h" + +#include "instrument.h" + +//#define QI_ALL +#define QI_SYSCALL + +bool qi_needs_before_insn(DisasContextBase *db, CPUState *cpu) +{ +#ifdef QI_ALL + /* instrument all the instructions */ + return true; +#endif +#ifdef QI_SYSCALL + /* instrument only system calls */ +#ifdef TARGET_I386 + uint8_t code =3D 0; + // int 80h is processed by exception handlers + if (!cpu_memory_rw_debug(cpu, db->pc_next, &code, 1, false) + && code =3D=3D 0x0f) { + if (cpu_memory_rw_debug(cpu, db->pc_next + 1, &code, 1, false)) { + return false; + } + if (code =3D=3D 0x34) { + /* sysenter */ + return true; + } + if (code =3D=3D 0x35) { + /* sysexit */ + return true; + } + } +#endif =20 + return false; +#endif +} + +void qi_instrument_before_insn(DisasContextBase *db, CPUState *cpu) +{ + TCGv t_pc =3D tcg_const_tl(db->pc_next); + TCGv_ptr t_cpu=3D tcg_const_ptr(cpu); + gen_helper_before_insn(t_pc, t_cpu); + tcg_temp_free(t_pc); + tcg_temp_free_ptr(t_cpu); +} + +void helper_before_insn(target_ulong pc, void *cpu) +{ +#ifdef QI_ALL + /* log all the executed instructions */ + qemu_log("executing %"PRIx64"\n", (uint64_t)pc); +#endif +#ifdef QI_SYSCALL + uint8_t code =3D 0; + cpu_memory_rw_debug(cpu, pc + 1, &code, 1, false); +#ifdef TARGET_I386 + CPUArchState *env =3D ((CPUState*)cpu)->env_ptr; + /* log system calls */ + if (code =3D=3D 0x34) { + qemu_log("syscall %x\n", (uint32_t)env->regs[R_EAX]); + } else if (code =3D=3D 0x35) { + qemu_log("sysexit %x\n", (uint32_t)env->regs[R_EAX]); + } +#endif +#endif +} + +void qi_init(void) +{ +#include "exec/helper-register.h" +} diff --git a/accel/tcg/instrument/instrument.h b/accel/tcg/instrument/instr= ument.h new file mode 100644 index 0000000..758ea49 --- /dev/null +++ b/accel/tcg/instrument/instrument.h @@ -0,0 +1,11 @@ +#ifndef INSTRUMENT_H +#define INSTRUMENT_H + +typedef struct DisasContextBase DisasContextBase; + +void qi_init(void); + +bool qi_needs_before_insn(DisasContextBase *db, CPUState *cpu); +void qi_instrument_before_insn(DisasContextBase *db, CPUState *cpu); + +#endif // INSTRUMENT_H diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 732c919..517db13 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -58,6 +58,7 @@ #include "qemu/main-loop.h" #include "exec/log.h" #include "sysemu/cpus.h" +#include "instrument/instrument.h" =20 /* #define DEBUG_TB_INVALIDATE */ /* #define DEBUG_TB_FLUSH */ @@ -210,6 +211,7 @@ static TranslationBlock *tb_find_pc(uintptr_t tc_ptr); void cpu_gen_init(void) { tcg_context_init(&tcg_init_ctx); + qi_init(); } =20 /* Encode VAL as a signed leb128 sequence at P. diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c index 0f9dca9..ec4c933 100644 --- a/accel/tcg/translator.c +++ b/accel/tcg/translator.c @@ -17,6 +17,7 @@ #include "exec/gen-icount.h" #include "exec/log.h" #include "exec/translator.h" +#include "instrument/instrument.h" =20 /* Pairs with tcg_clear_temp_count. To be called by #TranslatorOps.{translate_insn,tb_stop} if @@ -89,6 +90,10 @@ void translator_loop(const TranslatorOps *ops, DisasCont= extBase *db, } } =20 + if (qi_needs_before_insn(db, cpu)) { + qi_instrument_before_insn(db, cpu); + } + /* Disassemble one instruction. The translate_insn hook should update db->pc_next and db->is_jmp to indicate what should be done next -- either exiting this loop or locate the start of