qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Rowan Hart <rowanbhart@gmail.com>
To: qemu-devel@nongnu.org
Cc: "Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
	"Alexandre Iooss" <erdnaxe@crans.org>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Mahmoud Mandour" <ma.mandourr@gmail.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	novafacing <rowanbhart@gmail.com>
Subject: [PATCH v3 3/8] Add address space API
Date: Wed, 21 May 2025 02:43:27 -0700	[thread overview]
Message-ID: <20250521094333.4075796-4-rowanbhart@gmail.com> (raw)
In-Reply-To: <20250521094333.4075796-1-rowanbhart@gmail.com>

From: novafacing <rowanbhart@gmail.com>

Signed-off-by: novafacing <rowanbhart@gmail.com>
Signed-off-by: Rowan Hart <rowanbhart@gmail.com>
---
 include/qemu/plugin.h      |  6 +++
 include/qemu/qemu-plugin.h | 45 ++++++++++++++++++++++
 plugins/api.c              | 79 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+)

diff --git a/include/qemu/plugin.h b/include/qemu/plugin.h
index 9726a9ebf3..38439a37fa 100644
--- a/include/qemu/plugin.h
+++ b/include/qemu/plugin.h
@@ -139,6 +139,12 @@ struct qemu_plugin_tb {
     GArray *cbs;
 };
 
+/* Internal context for address space information */
+struct qemu_plugin_address_space_info {
+    CPUState *cpu;
+    GPtrArray *names;
+};
+
 /**
  * struct CPUPluginState - per-CPU state for plugins
  * @event_mask: plugin event bitmap. Modified only via async work.
diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
index 68c8632fd7..1380f7d441 100644
--- a/include/qemu/qemu-plugin.h
+++ b/include/qemu/qemu-plugin.h
@@ -926,6 +926,51 @@ QEMU_PLUGIN_API
 int qemu_plugin_write_register(struct qemu_plugin_register *handle,
                               GByteArray *buf);
 
+/** struct qemu_plugin_address_space_info - Opaque handle for space info */
+struct qemu_plugin_address_space_info;
+
+/**
+ * qemu_plugin_get_current_vcpu_address_spaces() - get a list of address spaces
+ * for the current vCPU
+ *
+ * This function should be called in vCPU context, i.e. from a vCPU, translation
+ * block, or operation callback.
+ *
+ * This function is only valid for softmmu targets.
+ *
+ * Returns an opaque qemu_plugin_address_space* handle that is only valid for
+ * the duration of the callback. The caller is not responsible for freeing the
+ * result.
+ */
+QEMU_PLUGIN_API
+struct qemu_plugin_address_space_info*
+qemu_plugin_get_current_vcpu_address_spaces(void);
+
+/**
+ * qemu_plugin_n_address_spaces() - get the number of address spaces
+ *
+ * @info: opaque handle to address space information
+ *
+ * Returns the number of address spaces, or -1 if the handle is invalid.
+ */
+QEMU_PLUGIN_API
+int qemu_plugin_n_address_spaces(struct qemu_plugin_address_space_info *info);
+
+/**
+ * qemu_plugin_address_space_name() - get the name of an address space
+ *
+ * @info: opaque handle to address space information
+ * @idx: index of the address space
+ *
+ * Returns the name of the address space, or NULL if the handle is invalid. The
+ * caller is responsible for freeing the result.
+ *
+ */
+QEMU_PLUGIN_API
+const char*
+qemu_plugin_address_space_name(struct qemu_plugin_address_space_info *info,
+                               unsigned int idx);
+
 /**
  * qemu_plugin_read_memory_vaddr() - read from memory using a virtual address
  *
diff --git a/plugins/api.c b/plugins/api.c
index 79b2dc20b8..d1cc6ff86e 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 "system/memory.h"
 #include "tcg/tcg.h"
 #include "exec/gdbstub.h"
 #include "exec/target_page.h"
@@ -452,6 +453,84 @@ int qemu_plugin_write_register(struct qemu_plugin_register *reg,
     return gdb_write_register(current_cpu, buf->data, GPOINTER_TO_INT(reg) - 1);
 }
 
+#ifdef CONFIG_SOFTMMU
+static __thread struct qemu_plugin_address_space_info address_space_info = {
+    NULL, NULL
+};
+static void free_g_string_and_data(gpointer data)
+{
+    g_string_free(data, true);
+}
+#endif
+
+struct qemu_plugin_address_space_info*
+qemu_plugin_get_current_vcpu_address_spaces(void)
+{
+#ifdef CONFIG_SOFTMMU
+    CPUState *cpu = current_cpu;
+
+    if (address_space_info.names == NULL) {
+        address_space_info.cpu = NULL;
+        address_space_info.names = g_ptr_array_new();
+        g_ptr_array_set_free_func(address_space_info.names,
+                                  free_g_string_and_data);
+    }
+
+    g_ptr_array_set_size(address_space_info.names, 0);
+
+    for (size_t i = 0; i < cpu->cpu_ases_count; i++) {
+        AddressSpace *as = cpu_get_address_space(cpu, i);
+
+        if (as == NULL || as->name == NULL) {
+            return NULL;
+        }
+
+        g_ptr_array_add(address_space_info.names,
+                        g_string_new(as->name));
+    }
+
+    address_space_info.cpu = cpu;
+
+    return &address_space_info;
+#else
+    return NULL;
+#endif
+}
+
+int qemu_plugin_n_address_spaces(struct qemu_plugin_address_space_info *info)
+{
+#ifdef CONFIG_SOFTMMU
+    if (info->cpu != current_cpu) {
+        address_space_info.cpu = NULL;
+        g_ptr_array_set_size(address_space_info.names, 0);
+        return -1;
+    }
+
+    return info->names->len;
+#else
+    return -1;
+#endif
+}
+
+const char *
+qemu_plugin_address_space_name(struct qemu_plugin_address_space_info *info,
+                               unsigned int idx)
+{
+#ifdef CONFIG_SOFTMMU
+    if (info->cpu != current_cpu) {
+        address_space_info.cpu = NULL;
+        g_ptr_array_set_size(address_space_info.names, 0);
+        return NULL;
+    }
+
+    if (idx < info->names->len) {
+        GString *name = g_ptr_array_index(info->names, idx);
+        return g_strdup(name->str);
+    }
+#endif
+    return NULL;
+}
+
 bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)
 {
     g_assert(current_cpu);
-- 
2.49.0



  parent reply	other threads:[~2025-05-21  9:45 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-21  9:43 [PATCH v3 0/8] Add additional plugin API functions to read and write memory and registers Rowan Hart
2025-05-21  9:43 ` [PATCH v3 1/8] Expose gdb_write_register function to consumers of gdbstub Rowan Hart
2025-05-21 22:52   ` Pierrick Bouvier
2025-05-22  8:53   ` Manos Pitsidianakis
2025-05-22 11:59   ` Julian Ganz
2025-05-21  9:43 ` [PATCH v3 2/8] Add register write API Rowan Hart
2025-05-21 22:52   ` Pierrick Bouvier
2025-05-22 11:59   ` Julian Ganz
2025-05-22 15:02     ` Rowan Hart
2025-05-22 15:16       ` Julian Ganz
2025-05-22 15:39   ` Alex Bennée
2025-05-22 20:11     ` Rowan Hart
2025-05-21  9:43 ` Rowan Hart [this message]
2025-05-21  9:43 ` [PATCH v3 4/8] Add memory virtual address " Rowan Hart
2025-05-21 22:53   ` Pierrick Bouvier
2025-05-21  9:43 ` [PATCH v3 5/8] Add memory hardware address read/write API Rowan Hart
2025-05-21 23:18   ` Pierrick Bouvier
2025-05-22  3:34     ` Rowan Hart
2025-05-22 19:46       ` Pierrick Bouvier
2025-05-22 11:59   ` Julian Ganz
2025-05-22 19:16     ` Pierrick Bouvier
2025-05-22 21:01       ` Rowan Hart
2025-05-22 22:37         ` Pierrick Bouvier
2025-05-21  9:43 ` [PATCH v3 6/8] Add patcher plugin and test Rowan Hart
2025-05-21  9:43 ` [PATCH v3 7/8] Add hypercalls " Rowan Hart
2025-05-21  9:43 ` [PATCH v3 8/8] Update plugin version and add notes Rowan Hart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250521094333.4075796-4-rowanbhart@gmail.com \
    --to=rowanbhart@gmail.com \
    --cc=alex.bennee@linaro.org \
    --cc=eduardo@habkost.net \
    --cc=erdnaxe@crans.org \
    --cc=ma.mandourr@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=pierrick.bouvier@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).