From patchwork Tue Sep 12 21:50:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Llu=C3=ADs_Vilanova?= X-Patchwork-Id: 9950187 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7C5786038F for ; Tue, 12 Sep 2017 21:51:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6DF25201F3 for ; Tue, 12 Sep 2017 21:51:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6264D28F7E; Tue, 12 Sep 2017 21:51:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9B3BE201F3 for ; Tue, 12 Sep 2017 21:51:16 +0000 (UTC) Received: from localhost ([::1]:38818 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drt5P-0006ZC-O8 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 12 Sep 2017 17:51:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51821) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drt4a-0006Z5-Fn for qemu-devel@nongnu.org; Tue, 12 Sep 2017 17:50:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drt4X-0007dB-2E for qemu-devel@nongnu.org; Tue, 12 Sep 2017 17:50:24 -0400 Received: from roura.ac.upc.es ([147.83.33.10]:38353) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drt4W-0007cJ-Iu for qemu-devel@nongnu.org; Tue, 12 Sep 2017 17:50:20 -0400 Received: from correu-2.ac.upc.es (correu-2.ac.upc.es [147.83.30.92]) by roura.ac.upc.es (8.13.8/8.13.8) with ESMTP id v8CLoH8V022816; Tue, 12 Sep 2017 23:50:17 +0200 Received: from localhost (unknown [31.210.187.58]) by correu-2.ac.upc.es (Postfix) with ESMTPSA id 6FB88259; Tue, 12 Sep 2017 23:50:11 +0200 (CEST) From: =?utf-8?b?TGx1w61z?= Vilanova To: qemu-devel@nongnu.org Date: Wed, 13 Sep 2017 00:50:10 +0300 Message-Id: <150525300993.15988.4392688345407178657.stgit@frigg.lan> X-Mailer: git-send-email 2.14.1 In-Reply-To: <150525010239.15988.8172586618197849619.stgit@frigg.lan> References: <150525010239.15988.8172586618197849619.stgit@frigg.lan> User-Agent: StGit/0.18 MIME-Version: 1.0 X-MIME-Autoconverted: from 8bit to quoted-printable by roura.ac.upc.es id v8CLoH8V022816 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x [fuzzy] X-Received-From: 147.83.33.10 Subject: [Qemu-devel] [PATCH v5 12/22] instrument: Add event 'guest_cpu_enter' 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: , Cc: Markus Armbruster , "Emilio G. Cota" , Stefan Hajnoczi , Paolo Bonzini , =?UTF-8?q?Llu=C3=ADs=20Vilanova?= Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: LluĂ­s Vilanova --- instrument/control.c | 9 ++++++++ instrument/events.h | 5 ++++ instrument/events.inc.h | 11 +++++++++ instrument/load.c | 9 ++++++++ instrument/qemu-instr/control.h | 46 +++++++++++++++++++++++++++++++++++++++ stubs/instrument.c | 1 + trace/control-target.c | 2 ++ 7 files changed, 83 insertions(+) diff --git a/instrument/control.c b/instrument/control.c index 8cf2b4f967..c4b3ca0440 100644 --- a/instrument/control.c +++ b/instrument/control.c @@ -49,3 +49,12 @@ SYM_PUBLIC void qi_set_fini(qi_fini_fn fn, void *data) instr_set_event(fini_fn, fn); instr_set_event(fini_data, data); } + + +void (*instr_event__guest_cpu_enter)(QICPU vcpu); + +SYM_PUBLIC void qi_event_set_guest_cpu_enter(void (*fn)(QICPU vcpu)) +{ + ERROR_IF(!instr_get_state(), "called outside instrumentation"); + instr_set_event(guest_cpu_enter, fn); +} diff --git a/instrument/events.h b/instrument/events.h index 82ad0bd827..947f120aa9 100644 --- a/instrument/events.h +++ b/instrument/events.h @@ -11,6 +11,7 @@ #define INSTRUMENT__EVENTS_H #include "instrument/qemu-instr/control.h" +#include "instrument/qemu-instr/types.h" /** * instr_get_event: @@ -32,6 +33,10 @@ extern qi_fini_fn instr_event__fini_fn; extern void *instr_event__fini_data; +extern void (*instr_event__guest_cpu_enter)(QICPU vcpu); +static inline void instr_guest_cpu_enter(CPUState *vcpu); + + #include "instrument/events.inc.h" #endif /* INSTRUMENT__EVENTS_H */ diff --git a/instrument/events.inc.h b/instrument/events.inc.h index 8b1ce7fcb2..e3f8024716 100644 --- a/instrument/events.inc.h +++ b/instrument/events.inc.h @@ -7,5 +7,16 @@ * See the COPYING file in the top-level directory. */ +#include "instrument/control.h" +static inline void instr_guest_cpu_enter(CPUState *vcpu) +{ + void (*cb)(QICPU vcpu) = instr_get_event(guest_cpu_enter); + if (cb) { + QICPU vcpu_ = instr_cpu_to_qicpu(vcpu); + instr_set_state(INSTR_STATE_ENABLE); + (*cb)(vcpu_); + instr_set_state(INSTR_STATE_DISABLE); + } +} diff --git a/instrument/load.c b/instrument/load.c index a01d66a4d4..218bca74b2 100644 --- a/instrument/load.c +++ b/instrument/load.c @@ -11,6 +11,7 @@ #include "qemu-common.h" #include +#include "exec/cpu-common.h" #include "instrument/control.h" #include "instrument/events.h" #include "instrument/load.h" @@ -109,6 +110,13 @@ InstrLoadError instr_load(const char *path, int argc, const char **argv, goto err; } + cpu_list_lock(); + CPUState *cpu; + CPU_FOREACH(cpu) { + instr_guest_cpu_enter(cpu); + } + cpu_list_unlock(); + res = INSTR_LOAD_OK; goto out; @@ -138,6 +146,7 @@ InstrUnloadError instr_unload(const char *id) } instr_set_event(fini_fn, NULL); + instr_set_event(guest_cpu_enter, NULL); /* this should never fail */ if (dlclose(handle->dlhandle) < 0) { diff --git a/instrument/qemu-instr/control.h b/instrument/qemu-instr/control.h index b841afaa31..f61e7a2b6e 100644 --- a/instrument/qemu-instr/control.h +++ b/instrument/qemu-instr/control.h @@ -16,6 +16,7 @@ extern "C" { #include #include +#include /** @@ -39,6 +40,51 @@ typedef void (*qi_fini_fn)(void *arg); */ void qi_set_fini(qi_fini_fn fn, void *data); + +/* + * Set callbacks for available events. Each event has a short description and + * various indicators of when it can be triggered: + * + * - Mode :: user + * Triggered in QEMU user application emulation (e.g., linux-user). + * + * - Mode :: softmmy + * Triggered in QEMU full-system emulation. + * + * + * - Targets :: all + * Triggered on all targets, both using TCG or native hardware virtualization + * (e.g., KVM). + * + * - Targets :: TCG() + * Triggered on the given guest target architectures when executing with TCG + * (no native hardware virtualization). + * + * + * - Time :: exec + * Triggered when the guest executes the described operation. + * + * - Time :: trans + * Triggered when QEMU translates a guest operation. This is only available + * when executing with TCG. Guest instructions are decompiled and translated + * into the intermediate TCG language (when "Time: trans" events are + * triggered). Then, the TCG compiler translates TCG code into the native host + * code that QEMU will execute to emulate the guest (when "Time: exec" events + * are triggered). As QEMU uses a cache of translated code, the same + * instruction might be translated more than once (when the cache overflows). + */ + +/* + * Hot-plug a new virtual (guest) CPU. + * + * Also triggered on each CPU when an instrumentation library is loaded. + * + * Mode: user, softmmu + * Targets: all + * Time: exec + */ +void qi_event_set_guest_cpu_enter(void (*fn)(QICPU vcpu)); + #ifdef __cplusplus } #endif diff --git a/stubs/instrument.c b/stubs/instrument.c index 9498fcdfe5..6b59ba9a7a 100644 --- a/stubs/instrument.c +++ b/stubs/instrument.c @@ -46,3 +46,4 @@ void qmp_instr_unload(const char *id, Error **errp) __thread InstrState instr_cur_state; +void (*instr_event__guest_cpu_enter)(QICPU *vcpu); diff --git a/trace/control-target.c b/trace/control-target.c index 706b2cee9d..f22688bcd5 100644 --- a/trace/control-target.c +++ b/trace/control-target.c @@ -9,6 +9,7 @@ #include "qemu/osdep.h" #include "cpu.h" +#include "instrument/events.h" #include "trace-root.h" #include "trace/control.h" #include "translate-all.h" @@ -146,5 +147,6 @@ void trace_init_vcpu(CPUState *vcpu) } } } + instr_guest_cpu_enter(vcpu); trace_guest_cpu_enter(vcpu); }