From patchwork Wed Sep 13 10:46:09 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: 9951001 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 BB8D26024A for ; Wed, 13 Sep 2017 10:47:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AA8BF20144 for ; Wed, 13 Sep 2017 10:47:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9B6C6205F7; Wed, 13 Sep 2017 10:47:25 +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 BEA3920144 for ; Wed, 13 Sep 2017 10:47:24 +0000 (UTC) Received: from localhost ([::1]:41498 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds5CV-0004il-I9 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 13 Sep 2017 06:47:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33813) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds5Bc-0004hX-Sm for qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:46:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ds5BY-0000f9-Tj for qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:46:28 -0400 Received: from roura.ac.upc.es ([147.83.33.10]:59429) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds5BY-0000eT-I2 for qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:46:24 -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 v8DAkKZ9009218; Wed, 13 Sep 2017 12:46:20 +0200 Received: from localhost (unknown [132.68.137.204]) by correu-2.ac.upc.es (Postfix) with ESMTPSA id 29F3A39C; Wed, 13 Sep 2017 12:46:14 +0200 (CEST) From: =?utf-8?b?TGx1w61z?= Vilanova To: qemu-devel@nongnu.org Date: Wed, 13 Sep 2017 13:46:09 +0300 Message-Id: <150529956916.10902.377591821566438337.stgit@frigg.lan> X-Mailer: git-send-email 2.14.1 In-Reply-To: <150529642278.10902.18234057937634437857.stgit@frigg.lan> References: <150529642278.10902.18234057937634437857.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 v8DAkKZ9009218 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 v6 13/22] instrument: Support synchronous modification of vCPU state 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: "Emilio G. Cota" , Markus Armbruster , Stefan Hajnoczi , =?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 Stops all vCPUs to allow performing management operations like TB invalidations. These are later necessary to ensure translated code does not reference unloaded instrumentation libraries. Signed-off-by: LluĂ­s Vilanova --- instrument/control.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++ instrument/control.h | 26 ++++++++++++++++++ instrument/control.inc.h | 11 ++++++++ 3 files changed, 104 insertions(+) diff --git a/instrument/control.c b/instrument/control.c index c4b3ca0440..4f9c138ccf 100644 --- a/instrument/control.c +++ b/instrument/control.c @@ -13,6 +13,7 @@ #include "instrument/load.h" #include "instrument/qemu-instr/control.h" #include "qemu/compiler.h" +#include "qemu/main-loop.h" #include "qom/cpu.h" @@ -40,6 +41,72 @@ void instr_cpu_remove(CPUState *vcpu) } +static void instr_cpu_stop_all__cb(CPUState *cpu, run_on_cpu_data data) +{ + InstrCPUStop *info = data.host_ptr; + /* run posted function */ + if (info->fun) { + info->fun(cpu, info->data); + } +#if !defined(CONFIG_USER_ONLY) + /* signal we're out of the main vCPU loop */ + unsigned int count = atomic_load_acquire(&info->count); + atomic_store_release(&info->count, count + 1); + atomic_store_release(&info->stopped, true); + /* wait until we're good to go again */ + qemu_cond_wait(&info->cond, &info->mutex); + count = atomic_load_acquire(&info->count); + atomic_store_release(&info->count, count - 1); + qemu_mutex_unlock(&info->mutex); +#endif +} + +void instr_cpu_stop_all_begin(InstrCPUStop *info, + instr_cpu_stop_fun fun, void *data) +{ + CPUState *cpu; + + info->fun = fun; + info->data = data; + +#if !defined(CONFIG_USER_ONLY) + info->count = 0; + qemu_cond_init(&info->cond); + qemu_mutex_init(&info->mutex); + + /* main dispatch loop and run_on_cpu() lock the BQL */ + qemu_mutex_unlock_iothread(); +#endif + + CPU_FOREACH(cpu) { +#if !defined(CONFIG_USER_ONLY) + atomic_store_release(&info->stopped, false); + qemu_mutex_lock(&info->mutex); + async_run_on_cpu(cpu, instr_cpu_stop_all__cb, + RUN_ON_CPU_HOST_PTR(info)); + while (!atomic_load_acquire(&info->stopped)) { + /* wait for vCPU to signal it's stopped */ + } +#else + instr_cpu_stop_all__cb(cpu, RUN_ON_CPU_HOST_PTR(info)); +#endif + } +} + +void instr_cpu_stop_all_end(InstrCPUStop *info) +{ +#if !defined(CONFIG_USER_ONLY) + qemu_cond_broadcast(&info->cond); + while (atomic_load_acquire(&info->count)) { + /* wait for all vCPUs to continue before we can destroy info */ + } + qemu_cond_destroy(&info->cond); + qemu_mutex_destroy(&info->mutex); + qemu_mutex_lock_iothread(); +#endif +} + + qi_fini_fn instr_event__fini_fn; void *instr_event__fini_data; diff --git a/instrument/control.h b/instrument/control.h index 57cea07fa7..03e87b2b8f 100644 --- a/instrument/control.h +++ b/instrument/control.h @@ -46,6 +46,32 @@ static inline QICPU instr_cpu_to_qicpu(CPUState *vcpu); */ static inline CPUState *instr_cpu_from_qicpu(QICPU vcpu); +typedef struct InstrCPUStop InstrCPUStop; +typedef void (*instr_cpu_stop_fun)(CPUState *cpu, void *data); + +/** + * instr_cpu_stop_all_begin: + * @info: Opaque structure describing the operation. + * @fun: Function to run on the context of each vCPU once stopped. + * @data: Pointer to pass to @fun. + * + * Ensure all vCPUs stop executing guest code, and execute @fun on their context + * in turn. Returns with all vCPUs still stopped. + * + * Assumes cpu_list_lock() and that the QBL is locked before calling. + */ +void instr_cpu_stop_all_begin(InstrCPUStop *info, + instr_cpu_stop_fun fun, void *data); + +/** + * instr_cpu_stop_all_end: + * @info: Opaque structure passed to a previous instr_cpu_stop_all_begin() + * call. + * + * Resume execution on all vCPUs stopped by instr_cpu_stop_all_begin(). + */ +void instr_cpu_stop_all_end(InstrCPUStop *info); + /** * InstrState: diff --git a/instrument/control.inc.h b/instrument/control.inc.h index 45daae7d1d..6d65b23ead 100644 --- a/instrument/control.inc.h +++ b/instrument/control.inc.h @@ -15,6 +15,17 @@ #include +struct InstrCPUStop { + instr_cpu_stop_fun fun; + void *data; +#if !defined(CONFIG_USER_ONLY) + bool stopped; + unsigned int count; + QemuCond cond; + QemuMutex mutex; +#endif +}; + extern unsigned int instr_cpus_count; extern CPUState **instr_cpus;