From: novafacing <rowanbh...@gmail.com> Signed-off-by: novafacing <rowanbh...@gmail.com> Signed-off-by: Rowan Hart <rowanbh...@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