From nobody Thu Nov 6 10:38:34 2025 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1540488659247573.9283333157358; Thu, 25 Oct 2018 10:30:59 -0700 (PDT) Received: from localhost ([::1]:56060 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFjT6-0005wY-4H for importer@patchew.org; Thu, 25 Oct 2018 13:30:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40455) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFjKj-0006j0-Bp for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFjKh-0000Pr-Pn for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:09 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:54545) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gFjKh-000089-Hj for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:07 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 84ABE22231; Thu, 25 Oct 2018 13:21:16 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 25 Oct 2018 13:21:16 -0400 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 01353E4AB8; Thu, 25 Oct 2018 13:21:16 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h= from:to:cc:subject:date:message-id:in-reply-to:references; s= mesmtp; bh=XfR82fSVFws5xnGSbnMvDlP4t/oV4ytjd8+UV2zc6kc=; b=eV5bs 9YVDeD1Uh5UFtVxvHsNOU/7WEvx7tF/PEYM61bDSQxGfcySQTLi2IvyY9WQN/u7m XAffgUdoYdjKSpZz7fh5hY8n0ueis6aCoQb48Uzg7UuuH1r/TX/ZTXnQrKaCxGXe sAZPnjtoBN0+IFncnq3DWMymG558CCEVfWxOl0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=XfR82fSVFws5xnGSbnMvDlP4t/oV4 ytjd8+UV2zc6kc=; b=n/ucud8XOV3W1cUyg95YyL+Hph9yTNhK5Cw6WsS1idMYb QTwM0mSFxjoFKIZwtsnsF2UBNCXSMIRen+iHLK4ITEXhcYznSEcYPPwKQifqG3ca xqHLjMcLjGrSm7hcnDcmGf2+plu8W6honPY7T/bEVLAur3JK2V8LMQjsVnpVU8ue J5iAiLOEcseD+yDVw0kYbqo64Rb6IY3585L8niH51GEujbl5kiTC4tDsdOQKPklJ vImnnQP0Ko+3SJuM8wwxFLUd+zUbnlcjZZykTQ7VRp4blb8gv/92r1WYjRL7YFZ5 8zg9QGQxKCzKL91HSmHkFQOHByT/5tOFEGiFyMxGQ== X-ME-Sender: X-ME-Proxy: From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Thu, 25 Oct 2018 13:20:49 -0400 Message-Id: <20181025172057.20414-41-cota@braap.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181025172057.20414-1-cota@braap.org> References: <20181025172057.20414-1-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.27 Subject: [Qemu-devel] [RFC 40/48] plugin: let plugins control the virtual clock 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: Peter Maydell , =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Llu=C3=ADs=20Vilanova?= , Pavel Dovgalyuk , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Emilio G. Cota --- include/qemu/plugin-api.h | 9 +++++ include/qemu/plugin.h | 5 +++ plugin.c | 79 +++++++++++++++++++++++++++++++++++++++ stubs/plugin.c | 9 +++++ util/qemu-timer.c | 3 ++ qemu-plugins.symbols | 1 + stubs/Makefile.objs | 1 + 7 files changed, 107 insertions(+) create mode 100644 stubs/plugin.c diff --git a/include/qemu/plugin-api.h b/include/qemu/plugin-api.h index 5c6bb45279..076353a2d2 100644 --- a/include/qemu/plugin-api.h +++ b/include/qemu/plugin-api.h @@ -218,6 +218,15 @@ void qemu_plugin_register_flush_cb(qemu_plugin_id_t id, void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id, qemu_plugin_udata_cb_t cb, void *userd= ata); =20 +typedef int64_t (*qemu_plugin_clock_func_t)(void); + +/* + * Can only be called from plugin_init. + * Returns true on success + */ +bool qemu_plugin_register_virtual_clock(qemu_plugin_id_t id, + qemu_plugin_clock_func_t clock); + /* returns -1 in user-mode */ int qemu_plugin_n_vcpus(void); =20 diff --git a/include/qemu/plugin.h b/include/qemu/plugin.h index 0da0f1b892..617161329f 100644 --- a/include/qemu/plugin.h +++ b/include/qemu/plugin.h @@ -127,6 +127,8 @@ struct qemu_plugin_tb { struct qemu_plugin_dyn_cb_arr cbs; }; =20 +extern bool use_plugin_clock; + static inline void qemu_plugin_insn_append(struct qemu_plugin_insn *insn, const void *from, size_t size) { @@ -191,6 +193,7 @@ void qemu_plugin_flush_cb(void); void qemu_plugin_atexit_cb(void); =20 void qemu_plugin_add_dyn_cb_arr(struct qemu_plugin_dyn_cb_arr *arr); +int64_t plugin_get_clock(void); =20 #else /* !CONFIG_PLUGINS */ =20 @@ -234,6 +237,8 @@ static inline void qemu_plugin_add_dyn_cb_arr(struct qemu_plugin_dyn_cb_arr *arr) { } =20 +int64_t plugin_get_clock(void); + #endif /* !CONFIG_PLUGINS */ =20 #endif /* QEMU_PLUGIN_H */ diff --git a/plugin.c b/plugin.c index 76609f1da4..291767f2bb 100644 --- a/plugin.c +++ b/plugin.c @@ -71,6 +71,13 @@ struct qemu_plugin_state { * the code cache is flushed. */ struct qht dyn_cb_arr_ht; + /* + * We support a single clock reference from plugins. We keep a pointer + * to the context of the plugin that provides the reference, + * so that we can remove the reference when the plugin is uninstalled. + */ + qemu_plugin_clock_func_t clock_ref; + struct qemu_plugin_ctx *clock_ctx; }; =20 /* @@ -104,6 +111,8 @@ QemuOptsList qemu_plugin_opts =3D { typedef int (*qemu_plugin_install_func_t)(qemu_plugin_id_t, int, char **); =20 static struct qemu_plugin_state plugin; +bool use_plugin_clock; +static bool plugin_installing; =20 static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp) { @@ -251,7 +260,9 @@ static int plugin_load(struct qemu_plugin_desc *desc) QTAILQ_INSERT_TAIL(&plugin.ctxs, ctx, entry); qemu_rec_mutex_unlock(&plugin.lock); =20 + plugin_installing =3D true; rc =3D install(ctx->id, desc->argc, desc->argv); + plugin_installing =3D false; if (rc) { error_report("%s: qemu_plugin_install returned error code %d", __func__, rc); @@ -418,6 +429,10 @@ void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_p= lugin_uninstall_cb_t cb) for (ev =3D 0; ev < QEMU_PLUGIN_EV_MAX; ev++) { plugin_unregister_cb__locked(ctx, ev); } + if (ctx =3D=3D plugin.clock_ctx) { + atomic_set(&plugin.clock_ref, NULL); + plugin.clock_ctx =3D NULL; + } qemu_rec_mutex_unlock(&plugin.lock); =20 /* XXX how to flush when we're not in a vCPU thread? */ @@ -964,6 +979,70 @@ uint64_t qemu_plugin_ram_addr_from_host(void *haddr) #endif } =20 +#ifndef CONFIG_USER_ONLY +static bool +qemu_plugin_register_virtual_clock__locked(qemu_plugin_id_t id, + qemu_plugin_clock_func_t clock) +{ + struct qemu_plugin_ctx *ctx =3D id_to_ctx__locked(id); + + if (!plugin_installing) { + error_report("plugin: can only call %s during plugin installation", + __func__); + return false; + } + if (use_plugin_clock) { + error_report("plugin: clock reference already registered"); + return false; + } + if (clock =3D=3D NULL) { + error_report("%s: cannot pass NULL clock", __func__); + return false; + } + plugin.clock_ctx =3D ctx; + use_plugin_clock =3D true; + atomic_set(&plugin.clock_ref, clock); + return true; +} +#endif /* !CONFIG_USER_ONLY */ + +bool qemu_plugin_register_virtual_clock(qemu_plugin_id_t id, + qemu_plugin_clock_func_t clock) +{ +#ifdef CONFIG_USER_ONLY + return false; +#else + bool ret; + + qemu_rec_mutex_lock(&plugin.lock); + ret =3D qemu_plugin_register_virtual_clock__locked(id, clock); + qemu_rec_mutex_unlock(&plugin.lock); + return ret; +#endif +} + +#ifdef CONFIG_USER_ONLY +int64_t plugin_get_clock(void) +{ + abort(); + return 0; +} +#else +/* + * Note: use_plugin_clock might be set, but the plugin providing the clock + * might have been uninstalled. + */ +int64_t plugin_get_clock(void) +{ + qemu_plugin_clock_func_t clock_ref =3D atomic_read(&plugin.clock_ref); + + if (clock_ref) { + return clock_ref(); + } + return cpu_get_clock(); +} +#endif + static void __attribute__((__constructor__)) plugin_init(void) { int i; diff --git a/stubs/plugin.c b/stubs/plugin.c new file mode 100644 index 0000000000..ab70365652 --- /dev/null +++ b/stubs/plugin.c @@ -0,0 +1,9 @@ +#include "qemu/osdep.h" +#include "qemu/plugin.h" + +bool use_plugin_clock; + +int64_t plugin_get_clock(void) +{ + abort(); +} diff --git a/util/qemu-timer.c b/util/qemu-timer.c index eb60d8f73a..d20ab0331c 100644 --- a/util/qemu-timer.c +++ b/util/qemu-timer.c @@ -24,6 +24,7 @@ =20 #include "qemu/osdep.h" #include "qemu/main-loop.h" +#include "qemu/plugin.h" #include "qemu/timer.h" #include "sysemu/replay.h" #include "sysemu/sysemu.h" @@ -601,6 +602,8 @@ int64_t qemu_clock_get_ns(QEMUClockType type) case QEMU_CLOCK_VIRTUAL_EXT: if (use_icount) { return cpu_get_icount(); + } else if (use_plugin_clock) { + return plugin_get_clock(); } else { return cpu_get_clock(); } diff --git a/qemu-plugins.symbols b/qemu-plugins.symbols index 76a57e62bb..93587b07e1 100644 --- a/qemu-plugins.symbols +++ b/qemu-plugins.symbols @@ -16,6 +16,7 @@ qemu_plugin_register_vcpu_syscall_cb; qemu_plugin_register_vcpu_syscall_ret_cb; qemu_plugin_register_atexit_cb; + qemu_plugin_register_virtual_clock; qemu_plugin_tb_n_insns; qemu_plugin_tb_get_insn; qemu_plugin_tb_vaddr; diff --git a/stubs/Makefile.objs b/stubs/Makefile.objs index fbcdc0256d..f32bea429f 100644 --- a/stubs/Makefile.objs +++ b/stubs/Makefile.objs @@ -23,6 +23,7 @@ stub-obj-y +=3D migr-blocker.o stub-obj-y +=3D change-state-handler.o stub-obj-y +=3D monitor.o stub-obj-y +=3D notify-event.o +stub-obj-y +=3D plugin.o stub-obj-y +=3D qtest.o stub-obj-y +=3D replay.o stub-obj-y +=3D runstate-check.o --=20 2.17.1