From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D6E59C61CE8 for ; Sun, 8 Jun 2025 23:09:49 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOP7s-0005sX-3v; Sun, 08 Jun 2025 19:08:28 -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 1uOP7q-0005rV-CI for qemu-devel@nongnu.org; Sun, 08 Jun 2025 19:08:26 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0: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 1uOP7o-0004b9-Ey for qemu-devel@nongnu.org; Sun, 08 Jun 2025 19:08:26 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2360ff7ac1bso11266765ad.3 for ; Sun, 08 Jun 2025 16:08:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749424103; x=1750028903; 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=Ghfzk3LGle8E568ju6BBjewy9XqILdgm0pkjx09hlJI=; b=BQFXtdZo4UabU7DagxWLqv3V6UGxSMR+8c/IhqlebRfQJBE8e8n5tjcj3uimzddE+i AYgKhJH+b4gTQrx1+4noOE9QJPMMvP45a5RtKtvw4o8loQHR8h4jFS2clm7Lx8G+WIxl qUu/31j+gKmx4MOhnQK2NnY5zRoKUfMJdb7/xD6Q65p2tR8iExA2MujPEc1slaHFtm2l HE4DTK4OILCNZUutHxKQgT+ZKyooeqwG+Fn63mGl1T9saWuGkl+uszSpaSHz9lZDYW4V dhrOBo/9eB36LU+qkhOsj0jPES4nI5ApOVZvT9fm7NV+bB3uAAl56uTnj+/aMZCRl9pU 8fvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749424103; x=1750028903; 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=Ghfzk3LGle8E568ju6BBjewy9XqILdgm0pkjx09hlJI=; b=CJ8JsC+1W/Ha6+7ZSMBDN/DbIRFGWIAlxrl1wT6tJ0cG/wfNXEWCCdtfOn6EyuhvsC ibFghRBxYsmARmDQUQNPSWJFGH8tQwDwNwCjWftzSjWWHYXXgf0yfHRcTeYU5IdP2muz 9Xpm298MTVpX6bxNSmuw7frq7cxKBc5z8ScPfGFxuJE6vU3a0Pl6lkGMM0BNFJ35qbtH xT29FGQKr65kkve9wZzF7oGoruHfdtvSEYKh38qQCdTSaR1kocGq1rOfB0IGrXQVkqFY vaHglso37oG2+4fCR88ndnGE8ZeL/fImQPjGymJVGcjSuui7yh8VGytaRiOQLNoJ/t8U mcrA== X-Gm-Message-State: AOJu0YzBjrvu9mmOsF3U6bCTtQGXC9hVsLN3wKuT+ZkSLHx5af8foa64 MrtT61Go0k0kXFkosvkJ0rqxT8z+KhPNyhlBfP6MPd1X2cFfNrM7Ifdljf3AEde7Aj0= X-Gm-Gg: ASbGncsrBZI4hMqDu0OabY5sGuI/dmCu/6HXCVwfOv5BOStQlmoTbFtfJC5f9uMy0E8 Jng9sa8T/EBDIDZXvd1KLLmm38AWV5EJIB/jFfvKGAtpgQFdAxd0+MclDh3hz1LQEYpbfFZEIIy /BWSkoj3wNgGRUuYV+pS8kF1Dn/fNp05UxabvDLZyFZOMOqj2/T7kOt430j42FbEAt4eIs1ZzdZ cTdYdGeLIQuT4ew4MO4i5iBDATKqr0AepUrT7+Jis2Arch1NxGA9AAyLfZJfyMB4fZBHLFSEjH4 ipmLmrrVIx+2HIZZMF3QuAMEqA2twNJcIoIQB1EzTyNd8VFd25w= X-Google-Smtp-Source: AGHT+IGqwA3wMfyZhV+Pgt8/Ne+iPqYJxvR/Ce0aQBCucYHvwV0gX9fjNIA4fCCtheAm5CjiKREkOw== X-Received: by 2002:a17:903:3bd0:b0:234:8ef1:aa7b with SMTP id d9443c01a7336-23601cfe4a4mr142399985ad.20.1749424102763; Sun, 08 Jun 2025 16:08:22 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23603504f09sm43421325ad.223.2025.06.08.16.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Jun 2025 16:08:22 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: Marcel Apfelbaum , Mahmoud Mandour , Richard Henderson , Eduardo Habkost , Yanan Wang , Pierrick Bouvier , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Zhao Liu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Alexandre Iooss , Rowan Hart Subject: [PATCH v9 3/9] plugins: Add enforcement of QEMU_PLUGIN_CB flags in register R/W callbacks Date: Sun, 8 Jun 2025 16:08:13 -0700 Message-ID: <20250608230819.3382527-4-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250608230819.3382527-1-rowanbhart@gmail.com> References: <20250608230819.3382527-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=rowanbhart@gmail.com; helo=mail-pl1-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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This patch adds functionality to enforce the requested QEMU_PLUGIN_CB_ flags level passed when registering a callback function using the plugins API. Each time a callback is about to be invoked, a thread-local variable will be updated with the level that callback requested. Then, called API functions (in particular, the register read and write API) will call qemu_plugin_get_cb_flags() to check the level is at least the level they require. Signed-off-by: Rowan Hart --- accel/tcg/plugin-gen.c | 30 ++++++++++++++++++++++++++++++ include/hw/core/cpu.h | 1 + include/qemu/plugin.h | 4 ++++ include/qemu/qemu-plugin.h | 3 --- plugins/api.c | 4 ++++ plugins/core.c | 20 ++++++++++++++++++++ 6 files changed, 59 insertions(+), 3 deletions(-) diff --git a/accel/tcg/plugin-gen.c b/accel/tcg/plugin-gen.c index c1da753894..9920381a84 100644 --- a/accel/tcg/plugin-gen.c +++ b/accel/tcg/plugin-gen.c @@ -117,10 +117,20 @@ static TCGv_i32 gen_cpu_index(void) static void gen_udata_cb(struct qemu_plugin_regular_cb *cb) { TCGv_i32 cpu_index = gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags = + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags = tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags = tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_gen_call2(cb->f.vcpu_udata, cb->info, NULL, tcgv_i32_temp(cpu_index), tcgv_ptr_temp(tcg_constant_ptr(cb->userp))); + tcg_gen_st_i32(clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_temp_free_i32(cpu_index); + tcg_temp_free_i32(flags); + tcg_temp_free_i32(clear_flags); } static TCGv_ptr gen_plugin_u64_ptr(qemu_plugin_u64 entry) @@ -173,10 +183,20 @@ static void gen_udata_cond_cb(struct qemu_plugin_conditional_cb *cb) tcg_gen_ld_i64(val, ptr, 0); tcg_gen_brcondi_i64(cond, val, cb->imm, after_cb); TCGv_i32 cpu_index = gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags = + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags = tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags = tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_gen_call2(cb->f.vcpu_udata, cb->info, NULL, tcgv_i32_temp(cpu_index), tcgv_ptr_temp(tcg_constant_ptr(cb->userp))); + tcg_gen_st_i32(clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_temp_free_i32(cpu_index); + tcg_temp_free_i32(flags); + tcg_temp_free_i32(clear_flags); gen_set_label(after_cb); tcg_temp_free_i64(val); @@ -210,12 +230,22 @@ static void gen_mem_cb(struct qemu_plugin_regular_cb *cb, qemu_plugin_meminfo_t meminfo, TCGv_i64 addr) { TCGv_i32 cpu_index = gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags = + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags = tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags = tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_gen_call4(cb->f.vcpu_mem, cb->info, NULL, tcgv_i32_temp(cpu_index), tcgv_i32_temp(tcg_constant_i32(meminfo)), tcgv_i64_temp(addr), tcgv_ptr_temp(tcg_constant_ptr(cb->userp))); + tcg_gen_st_i32(clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_temp_free_i32(cpu_index); + tcg_temp_free_i32(flags); + tcg_temp_free_i32(clear_flags); } static void inject_cb(struct qemu_plugin_dyn_cb *cb) diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 1e87f7d393..d3cc9a5224 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -368,6 +368,7 @@ typedef struct CPUNegativeOffsetState { GArray *plugin_mem_cbs; uint64_t plugin_mem_value_low; uint64_t plugin_mem_value_high; + int32_t plugin_cb_flags; #endif IcountDecr icount_decr; bool can_do_io; diff --git a/include/qemu/plugin.h b/include/qemu/plugin.h index 9726a9ebf3..2fef2e7d71 100644 --- a/include/qemu/plugin.h +++ b/include/qemu/plugin.h @@ -209,6 +209,10 @@ void qemu_plugin_user_prefork_lock(void); */ void qemu_plugin_user_postfork(bool is_child); +enum qemu_plugin_cb_flags tcg_call_to_qemu_plugin_cb_flags(int flags); + +enum qemu_plugin_cb_flags qemu_plugin_get_cb_flags(void); + #else /* !CONFIG_PLUGIN */ static inline void qemu_plugin_add_opts(void) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index cfe1692ecb..120fb626a6 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -254,9 +254,6 @@ typedef struct { * @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs - * - * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change - * system register state. */ enum qemu_plugin_cb_flags { QEMU_PLUGIN_CB_NO_REGS, diff --git a/plugins/api.c b/plugins/api.c index 6514f2c76a..3f04399c26 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -437,6 +437,10 @@ int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf) { g_assert(current_cpu); + if (qemu_plugin_get_cb_flags() == QEMU_PLUGIN_CB_NO_REGS) { + return -1; + } + return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1); } diff --git a/plugins/core.c b/plugins/core.c index eb9281fe54..4fe63472a3 100644 --- a/plugins/core.c +++ b/plugins/core.c @@ -367,6 +367,7 @@ void plugin_register_dyn_cb__udata(GArray **arr, static TCGHelperInfo info[3] = { [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags = 0, /* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) @@ -396,6 +397,7 @@ void plugin_register_dyn_cond_cb__udata(GArray **arr, static TCGHelperInfo info[3] = { [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags = 0, /* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) @@ -434,6 +436,7 @@ void plugin_register_vcpu_mem_cb(GArray **arr, static TCGHelperInfo info[3] = { [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags = 0, /* * Match qemu_plugin_vcpu_mem_cb_t: * void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *) @@ -760,3 +763,20 @@ void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score) g_array_free(score->data, TRUE); g_free(score); } + +enum qemu_plugin_cb_flags tcg_call_to_qemu_plugin_cb_flags(int flags) +{ + if (flags & TCG_CALL_NO_RWG) { + return QEMU_PLUGIN_CB_NO_REGS; + } else if (flags & TCG_CALL_NO_WG) { + return QEMU_PLUGIN_CB_R_REGS; + } else { + return QEMU_PLUGIN_CB_RW_REGS; + } +} + +enum qemu_plugin_cb_flags qemu_plugin_get_cb_flags(void) +{ + assert(current_cpu); + return current_cpu->neg.plugin_cb_flags; +} -- 2.49.0