xref: /qemu/include/qemu/plugin.h (revision cc37d98b)
1 /*
2  * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
3  *
4  * License: GNU GPL, version 2 or later.
5  *   See the COPYING file in the top-level directory.
6  */
7 #ifndef QEMU_PLUGIN_H
8 #define QEMU_PLUGIN_H
9 
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"
16 
17 /*
18  * Events that plugins can subscribe to.
19  */
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,
28     QEMU_PLUGIN_EV_FLUSH,
29     QEMU_PLUGIN_EV_ATEXIT,
30     QEMU_PLUGIN_EV_MAX, /* total number of plugin events we support */
31 };
32 
33 /*
34  * Option parsing/processing.
35  * Note that we can load an arbitrary number of plugins.
36  */
37 struct qemu_plugin_desc;
38 typedef QTAILQ_HEAD(, qemu_plugin_desc) QemuPluginList;
39 
40 /*
41  * Construct a qemu_plugin_meminfo_t.
42  */
43 static inline qemu_plugin_meminfo_t
44 make_plugin_meminfo(MemOpIdx oi, enum qemu_plugin_mem_rw rw)
45 {
46     return oi | (rw << 16);
47 }
48 
49 /*
50  * Extract the memory operation direction from a qemu_plugin_meminfo_t.
51  * Other portions may be extracted via get_memop and get_mmuidx.
52  */
53 static inline enum qemu_plugin_mem_rw
54 get_plugin_meminfo_rw(qemu_plugin_meminfo_t i)
55 {
56     return i >> 16;
57 }
58 
59 #ifdef CONFIG_PLUGIN
60 extern QemuOptsList qemu_plugin_opts;
61 
62 static inline void qemu_plugin_add_opts(void)
63 {
64     qemu_add_opts(&qemu_plugin_opts);
65 }
66 
67 void qemu_plugin_opt_parse(const char *optarg, QemuPluginList *head);
68 int qemu_plugin_load_list(QemuPluginList *head, Error **errp);
69 
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;
79     void *generic;
80 };
81 
82 enum plugin_dyn_cb_type {
83     PLUGIN_CB_INSN,
84     PLUGIN_CB_MEM,
85     PLUGIN_N_CB_TYPES,
86 };
87 
88 enum plugin_dyn_cb_subtype {
89     PLUGIN_CB_REGULAR,
90     PLUGIN_CB_INLINE,
91     PLUGIN_N_CB_SUBTYPES,
92 };
93 
94 /*
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.
98  */
99 struct qemu_plugin_dyn_cb {
100     union qemu_plugin_cb_sig f;
101     void *userp;
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 */
106     union {
107         struct {
108             enum qemu_plugin_op op;
109             uint64_t imm;
110         } inline_insn;
111     };
112 };
113 
114 /* Internal context for instrumenting an instruction */
115 struct qemu_plugin_insn {
116     GByteArray *data;
117     uint64_t vaddr;
118     void *haddr;
119     GArray *cbs[PLUGIN_N_CB_TYPES][PLUGIN_N_CB_SUBTYPES];
120     bool calls_helpers;
121 
122     /* if set, the instruction calls helpers that might access guest memory */
123     bool mem_helper;
124 
125     bool mem_only;
126 };
127 
128 /*
129  * qemu_plugin_insn allocate and cleanup functions. We don't expect to
130  * cleanup many of these structures. They are reused for each fresh
131  * translation.
132  */
133 
134 static inline void qemu_plugin_insn_cleanup_fn(gpointer data)
135 {
136     struct qemu_plugin_insn *insn = (struct qemu_plugin_insn *) data;
137     g_byte_array_free(insn->data, true);
138 }
139 
140 static inline struct qemu_plugin_insn *qemu_plugin_insn_alloc(void)
141 {
142     int i, j;
143     struct qemu_plugin_insn *insn = g_new0(struct qemu_plugin_insn, 1);
144     insn->data = g_byte_array_sized_new(4);
145 
146     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
147         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
148             insn->cbs[i][j] = g_array_new(false, false,
149                                           sizeof(struct qemu_plugin_dyn_cb));
150         }
151     }
152     return insn;
153 }
154 
155 /* Internal context for this TranslationBlock */
156 struct qemu_plugin_tb {
157     GPtrArray *insns;
158     size_t n;
159     uint64_t vaddr;
160     uint64_t vaddr2;
161     void *haddr1;
162     void *haddr2;
163     bool mem_only;
164 
165     /* if set, the TB calls helpers that might access guest memory */
166     bool mem_helper;
167 
168     GArray *cbs[PLUGIN_N_CB_SUBTYPES];
169 };
170 
171 /**
172  * qemu_plugin_tb_insn_get(): get next plugin record for translation.
173  * @tb: the internal tb context
174  * @pc: address of instruction
175  */
176 static inline
177 struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb,
178                                                  uint64_t pc)
179 {
180     struct qemu_plugin_insn *insn;
181     int i, j;
182 
183     if (unlikely(tb->n == tb->insns->len)) {
184         struct qemu_plugin_insn *new_insn = qemu_plugin_insn_alloc();
185         g_ptr_array_add(tb->insns, new_insn);
186     }
187     insn = g_ptr_array_index(tb->insns, tb->n++);
188     g_byte_array_set_size(insn->data, 0);
189     insn->calls_helpers = false;
190     insn->mem_helper = false;
191     insn->vaddr = pc;
192 
193     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
194         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
195             g_array_set_size(insn->cbs[i][j], 0);
196         }
197     }
198 
199     return insn;
200 }
201 
202 void qemu_plugin_vcpu_init_hook(CPUState *cpu);
203 void qemu_plugin_vcpu_exit_hook(CPUState *cpu);
204 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb);
205 void qemu_plugin_vcpu_idle_cb(CPUState *cpu);
206 void qemu_plugin_vcpu_resume_cb(CPUState *cpu);
207 void
208 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1,
209                          uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5,
210                          uint64_t a6, uint64_t a7, uint64_t a8);
211 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret);
212 
213 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
214                              MemOpIdx oi, enum qemu_plugin_mem_rw rw);
215 
216 void qemu_plugin_flush_cb(void);
217 
218 void qemu_plugin_atexit_cb(void);
219 
220 void qemu_plugin_add_dyn_cb_arr(GArray *arr);
221 
222 void qemu_plugin_disable_mem_helpers(CPUState *cpu);
223 
224 /**
225  * qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks
226  *
227  * This is a user-mode only helper that ensure we have fully cleared
228  * callbacks from all threads before calling the exit callbacks. This
229  * is so the plugins themselves don't have to jump through hoops to
230  * guard against race conditions.
231  */
232 void qemu_plugin_user_exit(void);
233 
234 /**
235  * qemu_plugin_user_prefork_lock(): take plugin lock before forking
236  *
237  * This is a user-mode only helper to take the internal plugin lock
238  * before a fork event. This is ensure a consistent lock state
239  */
240 void qemu_plugin_user_prefork_lock(void);
241 
242 /**
243  * qemu_plugin_user_postfork(): reset the plugin lock
244  * @is_child: is this thread the child
245  *
246  * This user-mode only helper resets the lock state after a fork so we
247  * can continue using the plugin interface.
248  */
249 void qemu_plugin_user_postfork(bool is_child);
250 
251 #else /* !CONFIG_PLUGIN */
252 
253 static inline void qemu_plugin_add_opts(void)
254 { }
255 
256 static inline void qemu_plugin_opt_parse(const char *optarg,
257                                          QemuPluginList *head)
258 {
259     error_report("plugin interface not enabled in this build");
260     exit(1);
261 }
262 
263 static inline int qemu_plugin_load_list(QemuPluginList *head, Error **errp)
264 {
265     return 0;
266 }
267 
268 static inline void qemu_plugin_vcpu_init_hook(CPUState *cpu)
269 { }
270 
271 static inline void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
272 { }
273 
274 static inline void qemu_plugin_tb_trans_cb(CPUState *cpu,
275                                            struct qemu_plugin_tb *tb)
276 { }
277 
278 static inline void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
279 { }
280 
281 static inline void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
282 { }
283 
284 static inline void
285 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
286                          uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6,
287                          uint64_t a7, uint64_t a8)
288 { }
289 
290 static inline
291 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
292 { }
293 
294 static inline void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
295                                            MemOpIdx oi,
296                                            enum qemu_plugin_mem_rw rw)
297 { }
298 
299 static inline void qemu_plugin_flush_cb(void)
300 { }
301 
302 static inline void qemu_plugin_atexit_cb(void)
303 { }
304 
305 static inline
306 void qemu_plugin_add_dyn_cb_arr(GArray *arr)
307 { }
308 
309 static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
310 { }
311 
312 static inline void qemu_plugin_user_exit(void)
313 { }
314 
315 static inline void qemu_plugin_user_prefork_lock(void)
316 { }
317 
318 static inline void qemu_plugin_user_postfork(bool is_child)
319 { }
320 
321 #endif /* !CONFIG_PLUGIN */
322 
323 #endif /* QEMU_PLUGIN_H */
324