2 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
10 #include "qemu/config-file.h"
11 #include "qemu/qemu-plugin.h"
12 #include "qemu/error-report.h"
13 #include "qemu/queue.h"
14 #include "qemu/option.h"
15 #include "exec/memopidx.h"
18 * Events that plugins can subscribe to.
20 enum qemu_plugin_event
{
21 QEMU_PLUGIN_EV_VCPU_INIT
,
22 QEMU_PLUGIN_EV_VCPU_EXIT
,
23 QEMU_PLUGIN_EV_VCPU_TB_TRANS
,
24 QEMU_PLUGIN_EV_VCPU_IDLE
,
25 QEMU_PLUGIN_EV_VCPU_RESUME
,
26 QEMU_PLUGIN_EV_VCPU_SYSCALL
,
27 QEMU_PLUGIN_EV_VCPU_SYSCALL_RET
,
29 QEMU_PLUGIN_EV_ATEXIT
,
30 QEMU_PLUGIN_EV_MAX
, /* total number of plugin events we support */
34 * Option parsing/processing.
35 * Note that we can load an arbitrary number of plugins.
37 struct qemu_plugin_desc
;
38 typedef QTAILQ_HEAD(, qemu_plugin_desc
) QemuPluginList
;
41 * Construct a qemu_plugin_meminfo_t.
43 static inline qemu_plugin_meminfo_t
44 make_plugin_meminfo(MemOpIdx oi
, enum qemu_plugin_mem_rw rw
)
46 return oi
| (rw
<< 16);
50 * Extract the memory operation direction from a qemu_plugin_meminfo_t.
51 * Other portions may be extracted via get_memop and get_mmuidx.
53 static inline enum qemu_plugin_mem_rw
54 get_plugin_meminfo_rw(qemu_plugin_meminfo_t i
)
60 extern QemuOptsList qemu_plugin_opts
;
62 static inline void qemu_plugin_add_opts(void)
64 qemu_add_opts(&qemu_plugin_opts
);
67 void qemu_plugin_opt_parse(const char *optarg
, QemuPluginList
*head
);
68 int qemu_plugin_load_list(QemuPluginList
*head
, Error
**errp
);
70 union qemu_plugin_cb_sig
{
71 qemu_plugin_simple_cb_t simple
;
72 qemu_plugin_udata_cb_t udata
;
73 qemu_plugin_vcpu_simple_cb_t vcpu_simple
;
74 qemu_plugin_vcpu_udata_cb_t vcpu_udata
;
75 qemu_plugin_vcpu_tb_trans_cb_t vcpu_tb_trans
;
76 qemu_plugin_vcpu_mem_cb_t vcpu_mem
;
77 qemu_plugin_vcpu_syscall_cb_t vcpu_syscall
;
78 qemu_plugin_vcpu_syscall_ret_cb_t vcpu_syscall_ret
;
82 enum plugin_dyn_cb_type
{
88 enum plugin_dyn_cb_subtype
{
95 * A dynamic callback has an insertion point that is determined at run-time.
96 * Usually the insertion point is somewhere in the code cache; think for
97 * instance of a callback to be called upon the execution of a particular TB.
99 struct qemu_plugin_dyn_cb
{
100 union qemu_plugin_cb_sig f
;
102 enum plugin_dyn_cb_subtype type
;
103 /* @rw applies to mem callbacks only (both regular and inline) */
104 enum qemu_plugin_mem_rw rw
;
105 /* fields specific to each dyn_cb type go here */
108 enum qemu_plugin_op op
;
114 /* Internal context for instrumenting an instruction */
115 struct qemu_plugin_insn
{
119 GArray
*cbs
[PLUGIN_N_CB_TYPES
][PLUGIN_N_CB_SUBTYPES
];
126 * qemu_plugin_insn allocate and cleanup functions. We don't expect to
127 * cleanup many of these structures. They are reused for each fresh
131 static inline void qemu_plugin_insn_cleanup_fn(gpointer data
)
133 struct qemu_plugin_insn
*insn
= (struct qemu_plugin_insn
*) data
;
134 g_byte_array_free(insn
->data
, true);
137 static inline struct qemu_plugin_insn
*qemu_plugin_insn_alloc(void)
140 struct qemu_plugin_insn
*insn
= g_new0(struct qemu_plugin_insn
, 1);
141 insn
->data
= g_byte_array_sized_new(4);
143 for (i
= 0; i
< PLUGIN_N_CB_TYPES
; i
++) {
144 for (j
= 0; j
< PLUGIN_N_CB_SUBTYPES
; j
++) {
145 insn
->cbs
[i
][j
] = g_array_new(false, false,
146 sizeof(struct qemu_plugin_dyn_cb
));
152 /* Internal context for this TranslationBlock */
153 struct qemu_plugin_tb
{
161 GArray
*cbs
[PLUGIN_N_CB_SUBTYPES
];
165 * qemu_plugin_tb_insn_get(): get next plugin record for translation.
169 struct qemu_plugin_insn
*qemu_plugin_tb_insn_get(struct qemu_plugin_tb
*tb
)
171 struct qemu_plugin_insn
*insn
;
174 if (unlikely(tb
->n
== tb
->insns
->len
)) {
175 struct qemu_plugin_insn
*new_insn
= qemu_plugin_insn_alloc();
176 g_ptr_array_add(tb
->insns
, new_insn
);
178 insn
= g_ptr_array_index(tb
->insns
, tb
->n
++);
179 g_byte_array_set_size(insn
->data
, 0);
180 insn
->calls_helpers
= false;
181 insn
->mem_helper
= false;
183 for (i
= 0; i
< PLUGIN_N_CB_TYPES
; i
++) {
184 for (j
= 0; j
< PLUGIN_N_CB_SUBTYPES
; j
++) {
185 g_array_set_size(insn
->cbs
[i
][j
], 0);
192 void qemu_plugin_vcpu_init_hook(CPUState
*cpu
);
193 void qemu_plugin_vcpu_exit_hook(CPUState
*cpu
);
194 void qemu_plugin_tb_trans_cb(CPUState
*cpu
, struct qemu_plugin_tb
*tb
);
195 void qemu_plugin_vcpu_idle_cb(CPUState
*cpu
);
196 void qemu_plugin_vcpu_resume_cb(CPUState
*cpu
);
198 qemu_plugin_vcpu_syscall(CPUState
*cpu
, int64_t num
, uint64_t a1
,
199 uint64_t a2
, uint64_t a3
, uint64_t a4
, uint64_t a5
,
200 uint64_t a6
, uint64_t a7
, uint64_t a8
);
201 void qemu_plugin_vcpu_syscall_ret(CPUState
*cpu
, int64_t num
, int64_t ret
);
203 void qemu_plugin_vcpu_mem_cb(CPUState
*cpu
, uint64_t vaddr
,
204 MemOpIdx oi
, enum qemu_plugin_mem_rw rw
);
206 void qemu_plugin_flush_cb(void);
208 void qemu_plugin_atexit_cb(void);
210 void qemu_plugin_add_dyn_cb_arr(GArray
*arr
);
212 void qemu_plugin_disable_mem_helpers(CPUState
*cpu
);
215 * qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks
217 * This is a user-mode only helper that ensure we have fully cleared
218 * callbacks from all threads before calling the exit callbacks. This
219 * is so the plugins themselves don't have to jump through hoops to
220 * guard against race conditions.
222 void qemu_plugin_user_exit(void);
224 #else /* !CONFIG_PLUGIN */
226 static inline void qemu_plugin_add_opts(void)
229 static inline void qemu_plugin_opt_parse(const char *optarg
,
230 QemuPluginList
*head
)
232 error_report("plugin interface not enabled in this build");
236 static inline int qemu_plugin_load_list(QemuPluginList
*head
, Error
**errp
)
241 static inline void qemu_plugin_vcpu_init_hook(CPUState
*cpu
)
244 static inline void qemu_plugin_vcpu_exit_hook(CPUState
*cpu
)
247 static inline void qemu_plugin_tb_trans_cb(CPUState
*cpu
,
248 struct qemu_plugin_tb
*tb
)
251 static inline void qemu_plugin_vcpu_idle_cb(CPUState
*cpu
)
254 static inline void qemu_plugin_vcpu_resume_cb(CPUState
*cpu
)
258 qemu_plugin_vcpu_syscall(CPUState
*cpu
, int64_t num
, uint64_t a1
, uint64_t a2
,
259 uint64_t a3
, uint64_t a4
, uint64_t a5
, uint64_t a6
,
260 uint64_t a7
, uint64_t a8
)
264 void qemu_plugin_vcpu_syscall_ret(CPUState
*cpu
, int64_t num
, int64_t ret
)
267 static inline void qemu_plugin_vcpu_mem_cb(CPUState
*cpu
, uint64_t vaddr
,
269 enum qemu_plugin_mem_rw rw
)
272 static inline void qemu_plugin_flush_cb(void)
275 static inline void qemu_plugin_atexit_cb(void)
279 void qemu_plugin_add_dyn_cb_arr(GArray
*arr
)
282 static inline void qemu_plugin_disable_mem_helpers(CPUState
*cpu
)
285 static inline void qemu_plugin_user_exit(void)
287 #endif /* !CONFIG_PLUGIN */
289 #endif /* QEMU_PLUGIN_H */