154cb65d8SEmilio G. Cota /* 254cb65d8SEmilio G. Cota * Plugin Shared Internal Functions 354cb65d8SEmilio G. Cota * 454cb65d8SEmilio G. Cota * Copyright (C) 2019, Linaro 554cb65d8SEmilio G. Cota * 654cb65d8SEmilio G. Cota * License: GNU GPL, version 2 or later. 754cb65d8SEmilio G. Cota * See the COPYING file in the top-level directory. 854cb65d8SEmilio G. Cota * 954cb65d8SEmilio G. Cota * SPDX-License-Identifier: GPL-2.0-or-later 1054cb65d8SEmilio G. Cota */ 1154cb65d8SEmilio G. Cota 1252581c71SMarkus Armbruster #ifndef PLUGIN_H 1352581c71SMarkus Armbruster #define PLUGIN_H 1454cb65d8SEmilio G. Cota 1554cb65d8SEmilio G. Cota #include <gmodule.h> 16824f4bacSPhilippe Mathieu-Daudé #include "qemu/qht.h" 1754cb65d8SEmilio G. Cota 18926e146eSPierrick Bouvier #define QEMU_PLUGIN_MIN_VERSION 2 193fb356ccSAlex Bennée 2054cb65d8SEmilio G. Cota /* global state */ 2154cb65d8SEmilio G. Cota struct qemu_plugin_state { 2254cb65d8SEmilio G. Cota QTAILQ_HEAD(, qemu_plugin_ctx) ctxs; 2354cb65d8SEmilio G. Cota QLIST_HEAD(, qemu_plugin_cb) cb_lists[QEMU_PLUGIN_EV_MAX]; 2454cb65d8SEmilio G. Cota /* 2554cb65d8SEmilio G. Cota * Use the HT as a hash map by inserting k == v, which saves memory as 2654cb65d8SEmilio G. Cota * documented by GLib. The parent struct is obtained with container_of(). 2754cb65d8SEmilio G. Cota */ 2854cb65d8SEmilio G. Cota GHashTable *id_ht; 2954cb65d8SEmilio G. Cota /* 3054cb65d8SEmilio G. Cota * Use the HT as a hash map. Note that we could use a list here, 3154cb65d8SEmilio G. Cota * but with the HT we avoid adding a field to CPUState. 3254cb65d8SEmilio G. Cota */ 3354cb65d8SEmilio G. Cota GHashTable *cpu_ht; 34a3c2cf0bSPierrick Bouvier QLIST_HEAD(, qemu_plugin_scoreboard) scoreboards; 35a3c2cf0bSPierrick Bouvier size_t scoreboard_alloc_size; 3654cb65d8SEmilio G. Cota DECLARE_BITMAP(mask, QEMU_PLUGIN_EV_MAX); 3754cb65d8SEmilio G. Cota /* 3854cb65d8SEmilio G. Cota * @lock protects the struct as well as ctx->uninstalling. 3954cb65d8SEmilio G. Cota * The lock must be acquired by all API ops. 4054cb65d8SEmilio G. Cota * The lock is recursive, which greatly simplifies things, e.g. 4154cb65d8SEmilio G. Cota * callback registration from qemu_plugin_vcpu_for_each(). 4254cb65d8SEmilio G. Cota */ 4354cb65d8SEmilio G. Cota QemuRecMutex lock; 4454cb65d8SEmilio G. Cota /* 4554cb65d8SEmilio G. Cota * HT of callbacks invoked from helpers. All entries are freed when 4654cb65d8SEmilio G. Cota * the code cache is flushed. 4754cb65d8SEmilio G. Cota */ 4854cb65d8SEmilio G. Cota struct qht dyn_cb_arr_ht; 494a448b14SPierrick Bouvier /* How many vcpus were started */ 504a448b14SPierrick Bouvier int num_vcpus; 5154cb65d8SEmilio G. Cota }; 5254cb65d8SEmilio G. Cota 5354cb65d8SEmilio G. Cota 5454cb65d8SEmilio G. Cota struct qemu_plugin_ctx { 5554cb65d8SEmilio G. Cota GModule *handle; 5654cb65d8SEmilio G. Cota qemu_plugin_id_t id; 5754cb65d8SEmilio G. Cota struct qemu_plugin_cb *callbacks[QEMU_PLUGIN_EV_MAX]; 5854cb65d8SEmilio G. Cota QTAILQ_ENTRY(qemu_plugin_ctx) entry; 5954cb65d8SEmilio G. Cota /* 6054cb65d8SEmilio G. Cota * keep a reference to @desc until uninstall, so that plugins do not have 6154cb65d8SEmilio G. Cota * to strdup plugin args. 6254cb65d8SEmilio G. Cota */ 6354cb65d8SEmilio G. Cota struct qemu_plugin_desc *desc; 6454cb65d8SEmilio G. Cota bool installing; 6554cb65d8SEmilio G. Cota bool uninstalling; 6654cb65d8SEmilio G. Cota bool resetting; 6754cb65d8SEmilio G. Cota }; 6854cb65d8SEmilio G. Cota 6954cb65d8SEmilio G. Cota struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id); 7054cb65d8SEmilio G. Cota 710bcebabaSPierrick Bouvier void plugin_register_inline_op_on_entry(GArray **arr, 720bcebabaSPierrick Bouvier enum qemu_plugin_mem_rw rw, 730bcebabaSPierrick Bouvier enum qemu_plugin_op op, 740bcebabaSPierrick Bouvier qemu_plugin_u64 entry, 750bcebabaSPierrick Bouvier uint64_t imm); 760bcebabaSPierrick Bouvier 7754cb65d8SEmilio G. Cota void plugin_reset_uninstall(qemu_plugin_id_t id, 7854cb65d8SEmilio G. Cota qemu_plugin_simple_cb_t cb, 7954cb65d8SEmilio G. Cota bool reset); 8054cb65d8SEmilio G. Cota 8154cb65d8SEmilio G. Cota void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev, 8254cb65d8SEmilio G. Cota void *func); 8354cb65d8SEmilio G. Cota 8454cb65d8SEmilio G. Cota void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx, 8554cb65d8SEmilio G. Cota enum qemu_plugin_event ev); 8654cb65d8SEmilio G. Cota 8754cb65d8SEmilio G. Cota void 8854cb65d8SEmilio G. Cota plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev, 8954cb65d8SEmilio G. Cota void *func, void *udata); 9054cb65d8SEmilio G. Cota 9154cb65d8SEmilio G. Cota void 9254cb65d8SEmilio G. Cota plugin_register_dyn_cb__udata(GArray **arr, 9354cb65d8SEmilio G. Cota qemu_plugin_vcpu_udata_cb_t cb, 9454cb65d8SEmilio G. Cota enum qemu_plugin_cb_flags flags, void *udata); 9554cb65d8SEmilio G. Cota 967de77d37SPierrick Bouvier void 977de77d37SPierrick Bouvier plugin_register_dyn_cond_cb__udata(GArray **arr, 987de77d37SPierrick Bouvier qemu_plugin_vcpu_udata_cb_t cb, 997de77d37SPierrick Bouvier enum qemu_plugin_cb_flags flags, 1007de77d37SPierrick Bouvier enum qemu_plugin_cond cond, 1017de77d37SPierrick Bouvier qemu_plugin_u64 entry, 1027de77d37SPierrick Bouvier uint64_t imm, 1037de77d37SPierrick Bouvier void *udata); 10454cb65d8SEmilio G. Cota 10554cb65d8SEmilio G. Cota void plugin_register_vcpu_mem_cb(GArray **arr, 10654cb65d8SEmilio G. Cota void *cb, 10754cb65d8SEmilio G. Cota enum qemu_plugin_cb_flags flags, 10854cb65d8SEmilio G. Cota enum qemu_plugin_mem_rw rw, 10954cb65d8SEmilio G. Cota void *udata); 11054cb65d8SEmilio G. Cota 111*09afe967SPierrick Bouvier void exec_inline_op(enum plugin_dyn_cb_type type, 112*09afe967SPierrick Bouvier struct qemu_plugin_inline_cb *cb, 113*09afe967SPierrick Bouvier int cpu_index); 11454cb65d8SEmilio G. Cota 1154a448b14SPierrick Bouvier int plugin_num_vcpus(void); 1164a448b14SPierrick Bouvier 117a3c2cf0bSPierrick Bouvier struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size); 118a3c2cf0bSPierrick Bouvier 119a3c2cf0bSPierrick Bouvier void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score); 120a3c2cf0bSPierrick Bouvier 12152581c71SMarkus Armbruster #endif /* PLUGIN_H */ 122