From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
"Akihiko Odaki" <akihiko.odaki@daynix.com>,
"Alexandre Iooss" <erdnaxe@crans.org>,
"Mahmoud Mandour" <ma.mandourr@gmail.com>
Subject: [PULL 23/29] plugins: add an API to read registers
Date: Wed, 28 Feb 2024 11:56:55 +0000 [thread overview]
Message-ID: <20240228115701.1416107-24-alex.bennee@linaro.org> (raw)
In-Reply-To: <20240228115701.1416107-1-alex.bennee@linaro.org>
We can only request a list of registers once the vCPU has been
initialised so the user needs to use either call the get function on
vCPU initialisation or during the translation phase.
We don't expose the reg number to the plugin instead hiding it behind
an opaque handle. For now this is just the gdb_regnum encapsulated in
an anonymous GPOINTER but in future as we add more state for plugins
to track we can expand it.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1706
Based-on: <20231025093128.33116-18-akihiko.odaki@daynix.com>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <20240227144335.1196131-24-alex.bennee@linaro.org>
diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
index 93981f8f89f..45e2ebc8f8f 100644
--- a/include/qemu/qemu-plugin.h
+++ b/include/qemu/qemu-plugin.h
@@ -11,6 +11,7 @@
#ifndef QEMU_QEMU_PLUGIN_H
#define QEMU_QEMU_PLUGIN_H
+#include <glib.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
@@ -229,8 +230,8 @@ struct qemu_plugin_insn;
* @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 unused, plugins cannot read or change system
- * register state.
+ * 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,
@@ -707,4 +708,49 @@ uint64_t qemu_plugin_end_code(void);
QEMU_PLUGIN_API
uint64_t qemu_plugin_entry_code(void);
+/** struct qemu_plugin_register - Opaque handle for register access */
+struct qemu_plugin_register;
+
+/**
+ * typedef qemu_plugin_reg_descriptor - register descriptions
+ *
+ * @handle: opaque handle for retrieving value with qemu_plugin_read_register
+ * @name: register name
+ * @feature: optional feature descriptor, can be NULL
+ */
+typedef struct {
+ struct qemu_plugin_register *handle;
+ const char *name;
+ const char *feature;
+} qemu_plugin_reg_descriptor;
+
+/**
+ * qemu_plugin_get_registers() - return register list for current vCPU
+ *
+ * Returns a potentially empty GArray of qemu_plugin_reg_descriptor.
+ * Caller frees the array (but not the const strings).
+ *
+ * Should be used from a qemu_plugin_register_vcpu_init_cb() callback
+ * after the vCPU is initialised, i.e. in the vCPU context.
+ */
+QEMU_PLUGIN_API
+GArray *qemu_plugin_get_registers(void);
+
+/**
+ * qemu_plugin_read_register() - read register for current vCPU
+ *
+ * @handle: a @qemu_plugin_reg_handle handle
+ * @buf: A GByteArray for the data owned by the plugin
+ *
+ * This function is only available in a context that register read access is
+ * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag.
+ *
+ * Returns the size of the read register. The content of @buf is in target byte
+ * order. On failure returns -1.
+ */
+QEMU_PLUGIN_API
+int qemu_plugin_read_register(struct qemu_plugin_register *handle,
+ GByteArray *buf);
+
+
#endif /* QEMU_QEMU_PLUGIN_H */
diff --git a/plugins/api.c b/plugins/api.c
index 54df72c1c00..81f43c9ce8a 100644
--- a/plugins/api.c
+++ b/plugins/api.c
@@ -8,6 +8,7 @@
*
* qemu_plugin_tb
* qemu_plugin_insn
+ * qemu_plugin_register
*
* Which can then be passed back into the API to do additional things.
* As such all the public functions in here are exported in
@@ -35,10 +36,12 @@
*/
#include "qemu/osdep.h"
+#include "qemu/main-loop.h"
#include "qemu/plugin.h"
#include "qemu/log.h"
#include "tcg/tcg.h"
#include "exec/exec-all.h"
+#include "exec/gdbstub.h"
#include "exec/ram_addr.h"
#include "disas/disas.h"
#include "plugin.h"
@@ -410,3 +413,55 @@ uint64_t qemu_plugin_entry_code(void)
#endif
return entry;
}
+
+/*
+ * Create register handles.
+ *
+ * We need to create a handle for each register so the plugin
+ * infrastructure can call gdbstub to read a register. They are
+ * currently just a pointer encapsulation of the gdb_reg but in
+ * future may hold internal plugin state so its important plugin
+ * authors are not tempted to treat them as numbers.
+ *
+ * We also construct a result array with those handles and some
+ * ancillary data the plugin might find useful.
+ */
+
+static GArray *create_register_handles(GArray *gdbstub_regs)
+{
+ GArray *find_data = g_array_new(true, true,
+ sizeof(qemu_plugin_reg_descriptor));
+
+ for (int i = 0; i < gdbstub_regs->len; i++) {
+ GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);
+ qemu_plugin_reg_descriptor desc;
+
+ /* skip "un-named" regs */
+ if (!grd->name) {
+ continue;
+ }
+
+ /* Create a record for the plugin */
+ desc.handle = GINT_TO_POINTER(grd->gdb_reg);
+ desc.name = g_intern_string(grd->name);
+ desc.feature = g_intern_string(grd->feature_name);
+ g_array_append_val(find_data, desc);
+ }
+
+ return find_data;
+}
+
+GArray *qemu_plugin_get_registers(void)
+{
+ g_assert(current_cpu);
+
+ g_autoptr(GArray) regs = gdb_get_register_list(current_cpu);
+ return create_register_handles(regs);
+}
+
+int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
+{
+ g_assert(current_cpu);
+
+ return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg));
+}
diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols
index adb67608598..27fe97239be 100644
--- a/plugins/qemu-plugins.symbols
+++ b/plugins/qemu-plugins.symbols
@@ -3,6 +3,7 @@
qemu_plugin_end_code;
qemu_plugin_entry_code;
qemu_plugin_get_hwaddr;
+ qemu_plugin_get_registers;
qemu_plugin_hwaddr_device_name;
qemu_plugin_hwaddr_is_io;
qemu_plugin_hwaddr_phys_addr;
@@ -19,6 +20,7 @@
qemu_plugin_num_vcpus;
qemu_plugin_outs;
qemu_plugin_path_to_binary;
+ qemu_plugin_read_register;
qemu_plugin_register_atexit_cb;
qemu_plugin_register_flush_cb;
qemu_plugin_register_vcpu_exit_cb;
--
2.39.2
next prev parent reply other threads:[~2024-02-28 12:14 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-28 11:56 [PULL 00/29] testing, gdbstub and plugin updates Alex Bennée
2024-02-28 11:56 ` [PULL 01/29] tests/tcg: update licenses to GPLv2 as intended Alex Bennée
2024-02-28 11:56 ` [PULL 02/29] tests/tcg: bump TCG test timeout to 120s Alex Bennée
2024-02-28 11:56 ` [PULL 03/29] tests/vm: avoid re-building the VM images all the time Alex Bennée
2024-02-28 11:56 ` [PULL 04/29] tests/vm: update openbsd image to 7.4 Alex Bennée
2024-02-28 11:56 ` [PULL 05/29] target/arm: Use GDBFeature for dynamic XML Alex Bennée
2024-02-28 11:56 ` [PULL 06/29] target/ppc: " Alex Bennée
2024-02-28 11:56 ` [PULL 07/29] target/riscv: " Alex Bennée
2024-02-28 11:56 ` [PULL 08/29] gdbstub: Use GDBFeature for gdb_register_coprocessor Alex Bennée
2024-02-28 11:56 ` [PULL 09/29] gdbstub: Use GDBFeature for GDBRegisterState Alex Bennée
2024-02-28 11:56 ` [PULL 10/29] gdbstub: Change gdb_get_reg_cb and gdb_set_reg_cb Alex Bennée
2024-02-28 11:56 ` [PULL 11/29] gdbstub: Simplify XML lookup Alex Bennée
2024-02-28 11:56 ` [PULL 12/29] gdbstub: Infer number of core registers from XML Alex Bennée
2024-02-28 11:56 ` [PULL 13/29] hw/core/cpu: Remove gdb_get_dynamic_xml member Alex Bennée
2024-02-28 11:56 ` [PULL 14/29] gdbstub: Add members to identify registers to GDBFeature Alex Bennée
2024-02-28 11:56 ` [PULL 15/29] plugins: remove previous n_vcpus functions from API Alex Bennée
2024-02-28 11:56 ` [PULL 16/29] plugins: add qemu_plugin_num_vcpus function Alex Bennée
2024-02-28 11:56 ` [PULL 17/29] plugins: fix order of init/idle/resume callback Alex Bennée
2024-02-28 11:56 ` [PULL 18/29] linux-user: ensure nios2 processes queued work Alex Bennée
2024-02-28 11:56 ` [PULL 19/29] cpu: call plugin init hook asynchronously Alex Bennée
2024-02-28 11:56 ` [PULL 20/29] plugins: Use different helpers when reading registers Alex Bennée
2024-02-28 11:56 ` [PULL 21/29] gdbstub: expose api to find registers Alex Bennée
2024-02-28 11:56 ` [PULL 22/29] plugins: create CPUPluginState and migrate plugin_mask Alex Bennée
2024-02-28 11:56 ` Alex Bennée [this message]
2024-02-28 11:56 ` [PULL 24/29] tests/tcg: expand insn test case to exercise register API Alex Bennée
2024-02-28 11:56 ` [PULL 25/29] contrib/plugins: fix imatch Alex Bennée
2024-02-28 11:56 ` [PULL 26/29] contrib/plugins: extend execlog to track register changes Alex Bennée
2024-03-01 7:19 ` Zhao Liu
2024-03-01 10:22 ` Alex Bennée
2024-03-01 15:54 ` Zhao Liu
2024-03-01 16:30 ` Alex Bennée
2024-03-02 7:02 ` Zhao Liu
2024-03-08 13:21 ` Peter Maydell
2024-02-28 11:56 ` [PULL 27/29] docs/devel: lift example and plugin API sections up Alex Bennée
2024-02-28 11:57 ` [PULL 28/29] docs/devel: document some plugin assumptions Alex Bennée
2024-02-28 11:57 ` [PULL 29/29] docs/devel: plugins can trigger a tb flush Alex Bennée
2024-02-28 17:26 ` [PULL 00/29] testing, gdbstub and plugin updates Peter Maydell
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=20240228115701.1416107-24-alex.bennee@linaro.org \
--to=alex.bennee@linaro.org \
--cc=akihiko.odaki@daynix.com \
--cc=erdnaxe@crans.org \
--cc=ma.mandourr@gmail.com \
--cc=pierrick.bouvier@linaro.org \
--cc=qemu-devel@nongnu.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).