xref: /openbmc/qemu/plugins/plugin.h (revision 4b2190da)
1 /*
2  * Plugin Shared Internal Functions
3  *
4  * Copyright (C) 2019, Linaro
5  *
6  * License: GNU GPL, version 2 or later.
7  *   See the COPYING file in the top-level directory.
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 #ifndef _PLUGIN_INTERNAL_H_
13 #define _PLUGIN_INTERNAL_H_
14 
15 #include <gmodule.h>
16 
17 /* global state */
18 struct qemu_plugin_state {
19     QTAILQ_HEAD(, qemu_plugin_ctx) ctxs;
20     QLIST_HEAD(, qemu_plugin_cb) cb_lists[QEMU_PLUGIN_EV_MAX];
21     /*
22      * Use the HT as a hash map by inserting k == v, which saves memory as
23      * documented by GLib. The parent struct is obtained with container_of().
24      */
25     GHashTable *id_ht;
26     /*
27      * Use the HT as a hash map. Note that we could use a list here,
28      * but with the HT we avoid adding a field to CPUState.
29      */
30     GHashTable *cpu_ht;
31     DECLARE_BITMAP(mask, QEMU_PLUGIN_EV_MAX);
32     /*
33      * @lock protects the struct as well as ctx->uninstalling.
34      * The lock must be acquired by all API ops.
35      * The lock is recursive, which greatly simplifies things, e.g.
36      * callback registration from qemu_plugin_vcpu_for_each().
37      */
38     QemuRecMutex lock;
39     /*
40      * HT of callbacks invoked from helpers. All entries are freed when
41      * the code cache is flushed.
42      */
43     struct qht dyn_cb_arr_ht;
44 };
45 
46 
47 struct qemu_plugin_ctx {
48     GModule *handle;
49     qemu_plugin_id_t id;
50     struct qemu_plugin_cb *callbacks[QEMU_PLUGIN_EV_MAX];
51     QTAILQ_ENTRY(qemu_plugin_ctx) entry;
52     /*
53      * keep a reference to @desc until uninstall, so that plugins do not have
54      * to strdup plugin args.
55      */
56     struct qemu_plugin_desc *desc;
57     bool installing;
58     bool uninstalling;
59     bool resetting;
60 };
61 
62 struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id);
63 
64 void plugin_register_inline_op(GArray **arr,
65                                enum qemu_plugin_mem_rw rw,
66                                enum qemu_plugin_op op, void *ptr,
67                                uint64_t imm);
68 
69 void plugin_reset_uninstall(qemu_plugin_id_t id,
70                             qemu_plugin_simple_cb_t cb,
71                             bool reset);
72 
73 void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
74                         void *func);
75 
76 void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
77                                   enum qemu_plugin_event ev);
78 
79 void
80 plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
81                          void *func, void *udata);
82 
83 void
84 plugin_register_dyn_cb__udata(GArray **arr,
85                               qemu_plugin_vcpu_udata_cb_t cb,
86                               enum qemu_plugin_cb_flags flags, void *udata);
87 
88 
89 void plugin_register_vcpu_mem_cb(GArray **arr,
90                                  void *cb,
91                                  enum qemu_plugin_cb_flags flags,
92                                  enum qemu_plugin_mem_rw rw,
93                                  void *udata);
94 
95 void exec_inline_op(struct qemu_plugin_dyn_cb *cb);
96 
97 #endif /* _PLUGIN_INTERNAL_H_ */
98