diff mbox series

[v15,16/19] plugins: Introduce CPU flags

Message ID 20231025052744.20697-17-akihiko.odaki@daynix.com
State New
Headers show
Series plugins: Allow to read registers | expand

Commit Message

Akihiko Odaki Oct. 25, 2023, 5:27 a.m. UTC
The capability to read registers is being added to plugins. Whether the
capability is enabled affects TCG translation.

Introduce "CPU flags" to represent a condition affecting TCG
translation.

The CPU flags replaces the plugin event bitmap held by CPUState; the
plugin event bitmap was needed because the presence of a
QEMU_PLUGIN_EV_VCPU_TB_TRANS callback affected TCG translation. We now
have a dedicated CPU flag for this, QEMU_PLUGIN_CPU_FLAG_CB_TRANS.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 include/hw/core/cpu.h  |  4 ++--
 include/qemu/plugin.h  |  2 ++
 plugins/plugin.h       |  1 +
 accel/tcg/plugin-gen.c |  2 +-
 plugins/core.c         | 26 ++++++++++++++++----------
 plugins/loader.c       |  6 ------
 6 files changed, 22 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index d2e70643f2..dbdca8b105 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -437,7 +437,7 @@  struct qemu_work_item;
  * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
  *                        to @trace_dstate).
  * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
- * @plugin_mask: Plugin event bitmap. Modified only via async work.
+ * @plugin_flags: Plugin flags. Modified only via async work.
  * @ignore_memory_transaction_failures: Cached copy of the MachineState
  *    flag of the same name: allows the board to suppress calling of the
  *    CPU do_transaction_failed hook function.
@@ -529,7 +529,7 @@  struct CPUState {
     /* Use by accel-block: CPU is executing an ioctl() */
     QemuLockCnt in_ioctl_lock;
 
-    DECLARE_BITMAP(plugin_mask, QEMU_PLUGIN_EV_MAX);
+    unsigned long plugin_flags;
 
 #ifdef CONFIG_PLUGIN
     GArray *plugin_mem_cbs;
diff --git a/include/qemu/plugin.h b/include/qemu/plugin.h
index 7fdc3a4849..b15fee39fd 100644
--- a/include/qemu/plugin.h
+++ b/include/qemu/plugin.h
@@ -16,6 +16,8 @@ 
 #include "exec/memopidx.h"
 #include "hw/core/cpu.h"
 
+#define QEMU_PLUGIN_CPU_FLAG_CB_TB_TRANS BIT(0)
+
 /*
  * Option parsing/processing.
  * Note that we can load an arbitrary number of plugins.
diff --git a/plugins/plugin.h b/plugins/plugin.h
index 5eb2fdbc85..db25e2650d 100644
--- a/plugins/plugin.h
+++ b/plugins/plugin.h
@@ -31,6 +31,7 @@  struct qemu_plugin_state {
      * but with the HT we avoid adding a field to CPUState.
      */
     GHashTable *cpu_ht;
+    unsigned long cpu_flags;
     DECLARE_BITMAP(mask, QEMU_PLUGIN_EV_MAX);
     /*
      * @lock protects the struct as well as ctx->uninstalling.
diff --git a/accel/tcg/plugin-gen.c b/accel/tcg/plugin-gen.c
index 78b331b251..4870433f3a 100644
--- a/accel/tcg/plugin-gen.c
+++ b/accel/tcg/plugin-gen.c
@@ -796,7 +796,7 @@  bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db,
 {
     bool ret = false;
 
-    if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_mask)) {
+    if (cpu->plugin_flags & QEMU_PLUGIN_CPU_FLAG_CB_TB_TRANS) {
         struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
         int i;
 
diff --git a/plugins/core.c b/plugins/core.c
index fcd33a2bff..368e2d1f54 100644
--- a/plugins/core.c
+++ b/plugins/core.c
@@ -55,19 +55,19 @@  struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)
 
 static void plugin_cpu_update__async(CPUState *cpu, run_on_cpu_data data)
 {
-    bitmap_copy(cpu->plugin_mask, &data.host_ulong, QEMU_PLUGIN_EV_MAX);
+    cpu->plugin_flags = data.host_ulong;
     tcg_flush_jmp_cache(cpu);
 }
 
 static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
 {
     CPUState *cpu = container_of(k, CPUState, cpu_index);
-    run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
+    run_on_cpu_data flags = RUN_ON_CPU_HOST_ULONG(plugin.cpu_flags);
 
     if (DEVICE(cpu)->realized) {
-        async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
+        async_run_on_cpu(cpu, plugin_cpu_update__async, flags);
     } else {
-        plugin_cpu_update__async(cpu, mask);
+        plugin_cpu_update__async(cpu, flags);
     }
 }
 
@@ -84,7 +84,11 @@  void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
     ctx->callbacks[ev] = NULL;
     if (QLIST_EMPTY_RCU(&plugin.cb_lists[ev])) {
         clear_bit(ev, plugin.mask);
-        g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked, NULL);
+        if (ev == QEMU_PLUGIN_EV_VCPU_TB_TRANS) {
+            plugin.cpu_flags &= ~QEMU_PLUGIN_CPU_FLAG_CB_TB_TRANS;
+            g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
+                                 NULL);
+        }
     }
 }
 
@@ -186,8 +190,10 @@  do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
             cb->udata = udata;
             ctx->callbacks[ev] = cb;
             QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
-            if (!test_bit(ev, plugin.mask)) {
-                set_bit(ev, plugin.mask);
+            set_bit(ev, plugin.mask);
+            if (ev == QEMU_PLUGIN_EV_VCPU_TB_TRANS &&
+                !(plugin.cpu_flags & QEMU_PLUGIN_CPU_FLAG_CB_TB_TRANS)) {
+                plugin.cpu_flags |= QEMU_PLUGIN_CPU_FLAG_CB_TB_TRANS;
                 g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
                                      NULL);
             }
@@ -334,7 +340,7 @@  void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
     struct qemu_plugin_cb *cb, *next;
     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
 
-    /* no plugin_mask check here; caller should have checked */
+    /* no plugin.mask check here; caller should have checked */
 
     QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
         qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
@@ -357,7 +363,7 @@  qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
     struct qemu_plugin_cb *cb, *next;
     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
 
-    if (!test_bit(ev, cpu->plugin_mask)) {
+    if (!test_bit(ev, plugin.mask)) {
         return;
     }
 
@@ -379,7 +385,7 @@  void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
     struct qemu_plugin_cb *cb, *next;
     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
 
-    if (!test_bit(ev, cpu->plugin_mask)) {
+    if (!test_bit(ev, plugin.mask)) {
         return;
     }
 
diff --git a/plugins/loader.c b/plugins/loader.c
index 734c11cae0..2c6e60c5d0 100644
--- a/plugins/loader.c
+++ b/plugins/loader.c
@@ -37,12 +37,6 @@ 
 
 #include "plugin.h"
 
-/*
- * For convenience we use a bitmap for plugin.mask, but really all we need is a
- * u32, which is what we store in TranslationBlock.
- */
-QEMU_BUILD_BUG_ON(QEMU_PLUGIN_EV_MAX > 32);
-
 struct qemu_plugin_desc {
     char *path;
     char **argv;