From patchwork Wed Jun 12 15:35:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 1947015 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=BNRIWsfA; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VzqR30jwrz20Pb for ; Thu, 13 Jun 2024 01:37:03 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sHQ0V-0001B1-SN; Wed, 12 Jun 2024 11:35:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sHQ0S-00012I-Pe for qemu-devel@nongnu.org; Wed, 12 Jun 2024 11:35:24 -0400 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sHQ0K-0004M5-F9 for qemu-devel@nongnu.org; Wed, 12 Jun 2024 11:35:24 -0400 Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-a6f176c5c10so365066b.2 for ; Wed, 12 Jun 2024 08:35:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718206514; x=1718811314; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PGoQj4qpl5RhrrC1ARvmO5/75amh4ElfKsyRGTMZr6o=; b=BNRIWsfAS6JSRuQ1h7z776vVCSnyqwW3e1EPSkDUU1eN4+vEy0BKWBIdzXKT01JxJT 87p6IzEPIUC7ucYRz9YpZPrEajGjXbFoTACBY/P+bm7PHHlxk5vPYzo3CM7jISqmUZj5 KmfCqEeEftHwY2ftuIlp6wAL1emKi3B+j4hPAZopuQZJEfaLnjv1RhhpalgNOcUuy+x0 dgW52lD0qqUireLh9PRdvuK1p8IXjQ+J2gfDlmoGlFz926zbYLfwjPIObf8bsvuuR2KK RKOYX4uu4Uljse+UlpdJP+OmKnfPOr/kKBgI/B5zeHWYk7sEkcyq3JVAvC9UnHX3uhn7 q6xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718206514; x=1718811314; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PGoQj4qpl5RhrrC1ARvmO5/75amh4ElfKsyRGTMZr6o=; b=rP1u5Dhh0KoKLHgGgB0P9uxz26Ubpg3NESvFE7N9hIwHiikVwyADEKlGlUIbxcVaDS /ge5WU/hQxAjPNN4WnohXkbn+NIQRkqTIArN7QkDvrnHM791i+/791euPVDIgOSuxVNM LtmmTKCgPSwBHzuiCoAxQiwE3qHLEzJTpEvCQ4Ri/LyQ0uZMliQtH3TikKpvR7oe/856 Acf+EWqm14/UbQTiOkeRJkvty3IhzWuLTrt8VqGPRDzjm4TV0i6tQeY8aJepX6S7E6nF 8bx4IDGQn/yqbrmvNN9DIJOaT9jMD11YDo5tGKeKACKQU4YTUwcVvSoXQ8MgEvXhcsqU l7dw== X-Gm-Message-State: AOJu0Ywe7Q5f9rh5ARvDB1N24sdgLMf4C4rzJuA7lWGpd8UwUy4YI+iG oRcB6Ro9X0D+b65WadvZSzb2bpEwPpI2W55q9Nw9cCseF7pZJK25VKFYgiLj1io= X-Google-Smtp-Source: AGHT+IG76EZWKhE5JO485CIAo85rC8bVxkvHDRmTHqUcDo+WGQuLSBgGHbJmn17mBFIkxNNff3ckGA== X-Received: by 2002:a17:906:7311:b0:a6f:4b46:dbb4 with SMTP id a640c23a62f3a-a6f4b46dd46mr128087966b.62.1718206514616; Wed, 12 Jun 2024 08:35:14 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6ef669bea6sm676540366b.153.2024.06.12.08.35.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 08:35:11 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 059CA5FA1C; Wed, 12 Jun 2024 16:35:09 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: David Hildenbrand , Ilya Leoshkevich , Daniel Henrique Barboza , Marcelo Tosatti , Paolo Bonzini , Pierrick Bouvier , =?utf-8?q?Philippe_Mathieu-?= =?utf-8?q?Daud=C3=A9?= , Mark Burton , qemu-s390x@nongnu.org, Peter Maydell , kvm@vger.kernel.org, Laurent Vivier , Halil Pasic , Christian Borntraeger , Alexandre Iooss , qemu-arm@nongnu.org, Alexander Graf , Nicholas Piggin , =?utf-8?q?Alex_Benn=C3=A9e?= , Marco Liebel , Thomas Huth , Roman Bolshakov , qemu-ppc@nongnu.org, Mahmoud Mandour , Cameron Esfahani , Jamie Iles , "Dr. David Alan Gilbert" , Richard Henderson Subject: [PATCH 8/9] plugins: add time control API Date: Wed, 12 Jun 2024 16:35:07 +0100 Message-Id: <20240612153508.1532940-9-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240612153508.1532940-1-alex.bennee@linaro.org> References: <20240612153508.1532940-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::634; envelope-from=alex.bennee@linaro.org; helo=mail-ej1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Expose the ability to control time through the plugin API. Only one plugin can control time so it has to request control when loaded. There are probably more corner cases to catch here. From: Alex Bennée Signed-off-by: Pierrick Bouvier [AJB: tweaked user-mode handling] Signed-off-by: Alex Bennée Message-Id: <20240530220610.1245424-6-pierrick.bouvier@linaro.org> --- plugins/next - make qemu_plugin_update_ns a NOP in user-mode --- include/qemu/qemu-plugin.h | 25 +++++++++++++++++++++++++ plugins/api.c | 35 +++++++++++++++++++++++++++++++++++ plugins/qemu-plugins.symbols | 2 ++ 3 files changed, 62 insertions(+) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 95703d8fec..db4d67529e 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -661,6 +661,31 @@ void qemu_plugin_register_vcpu_mem_inline_per_vcpu( qemu_plugin_u64 entry, uint64_t imm); +/** + * qemu_plugin_request_time_control() - request the ability to control time + * + * This grants the plugin the ability to control system time. Only one + * plugin can control time so if multiple plugins request the ability + * all but the first will fail. + * + * Returns an opaque handle or NULL if fails + */ +const void *qemu_plugin_request_time_control(void); + +/** + * qemu_plugin_update_ns() - update system emulation time + * @handle: opaque handle returned by qemu_plugin_request_time_control() + * @time: time in nanoseconds + * + * This allows an appropriately authorised plugin (i.e. holding the + * time control handle) to move system time forward to @time. For + * user-mode emulation the time is not changed by this as all reported + * time comes from the host kernel. + * + * Start time is 0. + */ +void qemu_plugin_update_ns(const void *handle, int64_t time); + typedef void (*qemu_plugin_vcpu_syscall_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_index, int64_t num, uint64_t a1, uint64_t a2, diff --git a/plugins/api.c b/plugins/api.c index 6bdb26bbe3..4431a0ea7e 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -39,6 +39,7 @@ #include "qemu/main-loop.h" #include "qemu/plugin.h" #include "qemu/log.h" +#include "qemu/timer.h" #include "tcg/tcg.h" #include "exec/exec-all.h" #include "exec/gdbstub.h" @@ -583,3 +584,37 @@ uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry) } return total; } + +/* + * Time control + */ +static bool has_control; + +const void *qemu_plugin_request_time_control(void) +{ + if (!has_control) { + has_control = true; + return &has_control; + } + return NULL; +} + +#ifdef CONFIG_SOFTMMU +static void advance_virtual_time__async(CPUState *cpu, run_on_cpu_data data) +{ + int64_t new_time = data.host_ulong; + qemu_clock_advance_virtual_time(new_time); +} +#endif + +void qemu_plugin_update_ns(const void *handle, int64_t new_time) +{ +#ifdef CONFIG_SOFTMMU + if (handle == &has_control) { + /* Need to execute out of cpu_exec, so bql can be locked. */ + async_run_on_cpu(current_cpu, + advance_virtual_time__async, + RUN_ON_CPU_HOST_ULONG(new_time)); + } +#endif +} diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols index aa0a77a319..ca773d8d9f 100644 --- a/plugins/qemu-plugins.symbols +++ b/plugins/qemu-plugins.symbols @@ -38,6 +38,7 @@ qemu_plugin_register_vcpu_tb_exec_cond_cb; qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu; qemu_plugin_register_vcpu_tb_trans_cb; + qemu_plugin_request_time_control; qemu_plugin_reset; qemu_plugin_scoreboard_free; qemu_plugin_scoreboard_find; @@ -51,5 +52,6 @@ qemu_plugin_u64_set; qemu_plugin_u64_sum; qemu_plugin_uninstall; + qemu_plugin_update_ns; qemu_plugin_vcpu_for_each; };