From nobody Mon Feb 9 18:19:56 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1748843802; cv=none; d=zohomail.com; s=zohoarc; b=lsAz2s+EchHGaFtONCiOulGe5bTfeVGZ+utIOp0RTzCKXutkd0Gl1SwV2RIklBXG32ReAUuL9QoRKK//qxiqoJxeDMmpgX/0Yz2xM03JghoAR4lTJgYPZZcH0P11ggBbeesjM7Lm/eLELDmq0w2F7huOz4MI7eR1Tiy5jBj3ejM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748843802; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=X9RtpUCTerBxjSxJjAWOPQ228nfpEZKJhq7knuun/F4=; b=bLsnLHsix0jAtmJjOXGzWLeoX/cyLRff5QR/fk/j14ftRvBhApVUEt5n7VSZ89a7CrkAitU+hu0+PP9bZxPD3Zu+92gj+FKGgavqXVtCNmv9M1KPmSjeAFeT52klo3NVQpCVpT9aiiyLK9q5RJQ4amOgqfcD2mI/yM4kE3C7Aq8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17488438021821008.7364946919987; Sun, 1 Jun 2025 22:56:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uLy8J-0002n7-RX; Mon, 02 Jun 2025 01:55:26 -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 1uLy3L-0001hv-K3 for qemu-devel@nongnu.org; Mon, 02 Jun 2025 01:50:14 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uLy3F-0007K8-2D for qemu-devel@nongnu.org; Mon, 02 Jun 2025 01:49:41 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2350fc2591dso25274105ad.1 for ; Sun, 01 Jun 2025 22:49:22 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23506bdce12sm63464085ad.99.2025.06.01.22.49.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Jun 2025 22:49:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748843361; x=1749448161; 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=X9RtpUCTerBxjSxJjAWOPQ228nfpEZKJhq7knuun/F4=; b=Uuq9hnAM2af9SnuWYtWWs/+6BeSyAwEPvmWAFa1f7F7rlB3EjL8R16OWtXqxxM4rFH V+y8wJhKXxM4MqGmZrl+5S6WYe7IXLXqF2OvnhySDc4RqS+8O+8xs0fxgeMqkSB0T/J6 U5W8UFyT07so+fCtBRWjHaythcYmPWYHdkA6GwspyGtf3cXyaWXLla65ykl3ISSb7Y8Q dGEEwYFEPyLDKcespGO+kElRf/N0BilSBa21IV3rqTKWCvq463oKriitPWtlmczOsZoK KtbqJdPNirYFo+ckcfvYe17OeXi5TsLEeOUk0N4WWnZfuBfM/NDDjhPeNDl1vxG/vYPE 1C0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748843361; x=1749448161; 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=X9RtpUCTerBxjSxJjAWOPQ228nfpEZKJhq7knuun/F4=; b=txo8Gik7w+adAsP331j1NSNmY2V1gNEnSqtjbjG70WSNCkUhTdy4itQjeRjv8BHEZa pktWgEdeTYdzao7lYqo5yJBEWNImqsA0G/eWC9O4tmn8SHsGw7okYYZ2CTmljEY47qXM laS8yqoLuEVxJohPmeBJS/OWQj6OAHJ1DKrR5WvPwE+YR1mXeOhN4zZzq9912Np6cIt5 8IjQsg/EPfS0T6HrarR3wAFtBXgHk8Dqf/IUVA1Y4tHvtiBZOJoD4Gh6HPflgyq0740J kFWkCUcNBowJXvO677J1s/C5tja0D4fC1FXV5Aa1G1zR8XUq08OaKDBq77XkcUOVOuhg 0I6Q== X-Gm-Message-State: AOJu0YxWJAKOO0C6Tol0i4LbmRPvuekDHeXi0fCZdX4iwhVzwPpfdH57 FI8HLiOHBj1ascBvHsAWyRnrO2BbeKmlhfeDxu9wG30WzLAdhTSYvWIDjTVDmdXJevQ= X-Gm-Gg: ASbGncuGO2J/cWSTN4mlAJCGpoiSDosQlRKKESpWSjpmwbkMWr7BYnEXj758VpxgXiM NWzdlarASqR/yoY7h/OqvNTJh2K0IiIDCJ9jZepBT70zP2P7l9Yck+9gm2oGF/el28Bv7Bfw02F TygoTb/20FCoYdedbZKSAq5RTnd0WK4+1efdPsJKNaxsNmcXFHeaGYb6HCyHzK391Gmg7vpzEGz /jrwRUYNbYtEblcFEu5fyMWixixuGpNzUNI9RHW48Q9MIdAWR8tb5rbakcH5azczbBFjH4fFZ8/ Km1gHR5PVbJMKtSN+12l3BbsCL9cQm0Li3hlEPZapG2oYnFsWxw= X-Google-Smtp-Source: AGHT+IFU8c7NMJN7G9wx+cGCISWrb4kld9GrRMyX/bkEEJp3oH+F9s002JO/zXZCa/2GWSLufWrvGQ== X-Received: by 2002:a17:902:e546:b0:231:c9bb:6106 with SMTP id d9443c01a7336-2352b025447mr173083945ad.9.1748843360788; Sun, 01 Jun 2025 22:49:20 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: Yanan Wang , Pierrick Bouvier , Zhao Liu , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Alexandre Iooss , Eduardo Habkost , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mahmoud Mandour , Paolo Bonzini , Marcel Apfelbaum , Richard Henderson , Rowan Hart Subject: [PATCH v5 3/9] plugins: Add enforcement of QEMU_PLUGIN_CB flags in register R/W callbacks Date: Sun, 1 Jun 2025 22:49:10 -0700 Message-ID: <20250602054917.302638-4-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250602054917.302638-1-rowanbhart@gmail.com> References: <20250602054917.302638-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::629; envelope-from=rowanbhart@gmail.com; helo=mail-pl1-x629.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1748843804502116600 Content-Type: text/plain; charset="utf-8" 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 | 8 ++++++++ plugins/core.c | 32 ++++++++++++++++++++++++++------ 6 files changed, 69 insertions(+), 9 deletions(-) diff --git a/accel/tcg/plugin-gen.c b/accel/tcg/plugin-gen.c index c1da753894..1d330a1c03 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 =3D gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags =3D + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags =3D tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags =3D tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env,=20 + 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,=20 + 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); } =20 static TCGv_ptr gen_plugin_u64_ptr(qemu_plugin_u64 entry) @@ -173,10 +183,20 @@ static void gen_udata_cond_cb(struct qemu_plugin_cond= itional_cb *cb) tcg_gen_ld_i64(val, ptr, 0); tcg_gen_brcondi_i64(cond, val, cb->imm, after_cb); TCGv_i32 cpu_index =3D gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags =3D + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags =3D tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags =3D tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env,=20 + 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,=20 + 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); =20 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 =3D gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags =3D + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags =3D tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags =3D tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env,=20 + 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,=20 + 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); } =20 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); =20 +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 */ =20 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 3a7add50d2..16141f5c25 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -437,6 +437,10 @@ int qemu_plugin_read_register(struct qemu_plugin_regis= ter *reg, GByteArray *buf) { g_assert(current_cpu); =20 + if (qemu_plugin_get_cb_flags() =3D=3D QEMU_PLUGIN_CB_NO_REGS) { + return -1; + } + return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1); } =20 @@ -445,6 +449,10 @@ int qemu_plugin_write_register(struct qemu_plugin_regi= ster *reg, { g_assert(current_cpu); =20 + if (buf->len =3D=3D 0 || qemu_plugin_get_cb_flags() !=3D QEMU_PLUGIN_C= B_RW_REGS) { + return 0; + } + return gdb_write_register(current_cpu, buf->data, GPOINTER_TO_INT(reg)= - 1); } =20 diff --git a/plugins/core.c b/plugins/core.c index eb9281fe54..28541f7f80 100644 --- a/plugins/core.c +++ b/plugins/core.c @@ -364,14 +364,15 @@ void plugin_register_dyn_cb__udata(GArray **arr, enum qemu_plugin_cb_flags flags, void *udata) { - static TCGHelperInfo info[3] =3D { + static TCGHelperInfo info[4] =3D { [QEMU_PLUGIN_CB_NO_REGS].flags =3D TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags =3D TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags =3D 0, /* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) */ - [0 ... 2].typemask =3D (dh_typemask(void, 0) | + [0 ... 3].typemask =3D (dh_typemask(void, 0) | dh_typemask(i32, 1) | dh_typemask(ptr, 2)) }; @@ -393,14 +394,15 @@ void plugin_register_dyn_cond_cb__udata(GArray **arr, uint64_t imm, void *udata) { - static TCGHelperInfo info[3] =3D { + static TCGHelperInfo info[4] =3D { [QEMU_PLUGIN_CB_NO_REGS].flags =3D TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags =3D TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags =3D 0, /* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) */ - [0 ... 2].typemask =3D (dh_typemask(void, 0) | + [0 ... 3].typemask =3D (dh_typemask(void, 0) | dh_typemask(i32, 1) | dh_typemask(ptr, 2)) }; @@ -431,14 +433,15 @@ void plugin_register_vcpu_mem_cb(GArray **arr, !__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t) && !__builtin_types_compatible_p(qemu_plugin_meminfo_t, int32_t)); =20 - static TCGHelperInfo info[3] =3D { + static TCGHelperInfo info[4] =3D { [QEMU_PLUGIN_CB_NO_REGS].flags =3D TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags =3D TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags =3D 0, /* * Match qemu_plugin_vcpu_mem_cb_t: * void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *) */ - [0 ... 2].typemask =3D + [0 ... 3].typemask =3D (dh_typemask(void, 0) | dh_typemask(i32, 1) | (__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t) @@ -760,3 +763,20 @@ void plugin_scoreboard_free(struct qemu_plugin_scorebo= ard *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; +} \ No newline at end of file --=20 2.49.0