xref: /qemu/include/qemu/plugin.h (revision 62f92b8d)
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 "qemu/plugin-event.h"
16 #include "exec/memopidx.h"
17 #include "hw/core/cpu.h"
18 
19 /*
20  * Option parsing/processing.
21  * Note that we can load an arbitrary number of plugins.
22  */
23 struct qemu_plugin_desc;
24 typedef QTAILQ_HEAD(, qemu_plugin_desc) QemuPluginList;
25 
26 /*
27  * Construct a qemu_plugin_meminfo_t.
28  */
29 static inline qemu_plugin_meminfo_t
30 make_plugin_meminfo(MemOpIdx oi, enum qemu_plugin_mem_rw rw)
31 {
32     return oi | (rw << 16);
33 }
34 
35 /*
36  * Extract the memory operation direction from a qemu_plugin_meminfo_t.
37  * Other portions may be extracted via get_memop and get_mmuidx.
38  */
39 static inline enum qemu_plugin_mem_rw
40 get_plugin_meminfo_rw(qemu_plugin_meminfo_t i)
41 {
42     return i >> 16;
43 }
44 
45 #ifdef CONFIG_PLUGIN
46 extern QemuOptsList qemu_plugin_opts;
47 
48 static inline void qemu_plugin_add_opts(void)
49 {
50     qemu_add_opts(&qemu_plugin_opts);
51 }
52 
53 void qemu_plugin_opt_parse(const char *optstr, QemuPluginList *head);
54 int qemu_plugin_load_list(QemuPluginList *head, Error **errp);
55 
56 union qemu_plugin_cb_sig {
57     qemu_plugin_simple_cb_t          simple;
58     qemu_plugin_udata_cb_t           udata;
59     qemu_plugin_vcpu_simple_cb_t     vcpu_simple;
60     qemu_plugin_vcpu_udata_cb_t      vcpu_udata;
61     qemu_plugin_vcpu_tb_trans_cb_t   vcpu_tb_trans;
62     qemu_plugin_vcpu_mem_cb_t        vcpu_mem;
63     qemu_plugin_vcpu_syscall_cb_t    vcpu_syscall;
64     qemu_plugin_vcpu_syscall_ret_cb_t vcpu_syscall_ret;
65     void *generic;
66 };
67 
68 enum plugin_dyn_cb_type {
69     PLUGIN_CB_INSN,
70     PLUGIN_CB_MEM,
71     PLUGIN_N_CB_TYPES,
72 };
73 
74 enum plugin_dyn_cb_subtype {
75     PLUGIN_CB_REGULAR,
76     PLUGIN_CB_REGULAR_R,
77     PLUGIN_CB_INLINE,
78     PLUGIN_N_CB_SUBTYPES,
79 };
80 
81 /*
82  * A dynamic callback has an insertion point that is determined at run-time.
83  * Usually the insertion point is somewhere in the code cache; think for
84  * instance of a callback to be called upon the execution of a particular TB.
85  */
86 struct qemu_plugin_dyn_cb {
87     union qemu_plugin_cb_sig f;
88     void *userp;
89     enum plugin_dyn_cb_subtype type;
90     /* @rw applies to mem callbacks only (both regular and inline) */
91     enum qemu_plugin_mem_rw rw;
92     /* fields specific to each dyn_cb type go here */
93     union {
94         struct {
95             enum qemu_plugin_op op;
96             uint64_t imm;
97         } inline_insn;
98     };
99 };
100 
101 /* Internal context for instrumenting an instruction */
102 struct qemu_plugin_insn {
103     GByteArray *data;
104     uint64_t vaddr;
105     void *haddr;
106     GArray *cbs[PLUGIN_N_CB_TYPES][PLUGIN_N_CB_SUBTYPES];
107     bool calls_helpers;
108 
109     /* if set, the instruction calls helpers that might access guest memory */
110     bool mem_helper;
111 
112     bool mem_only;
113 };
114 
115 /* A scoreboard is an array of values, indexed by vcpu_index */
116 struct qemu_plugin_scoreboard {
117     GArray *data;
118     QLIST_ENTRY(qemu_plugin_scoreboard) entry;
119 };
120 
121 /*
122  * qemu_plugin_insn allocate and cleanup functions. We don't expect to
123  * cleanup many of these structures. They are reused for each fresh
124  * translation.
125  */
126 
127 static inline void qemu_plugin_insn_cleanup_fn(gpointer data)
128 {
129     struct qemu_plugin_insn *insn = (struct qemu_plugin_insn *) data;
130     g_byte_array_free(insn->data, true);
131 }
132 
133 static inline struct qemu_plugin_insn *qemu_plugin_insn_alloc(void)
134 {
135     int i, j;
136     struct qemu_plugin_insn *insn = g_new0(struct qemu_plugin_insn, 1);
137     insn->data = g_byte_array_sized_new(4);
138 
139     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
140         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
141             insn->cbs[i][j] = g_array_new(false, false,
142                                           sizeof(struct qemu_plugin_dyn_cb));
143         }
144     }
145     return insn;
146 }
147 
148 /* Internal context for this TranslationBlock */
149 struct qemu_plugin_tb {
150     GPtrArray *insns;
151     size_t n;
152     uint64_t vaddr;
153     uint64_t vaddr2;
154     void *haddr1;
155     void *haddr2;
156     bool mem_only;
157 
158     /* if set, the TB calls helpers that might access guest memory */
159     bool mem_helper;
160 
161     GArray *cbs[PLUGIN_N_CB_SUBTYPES];
162 };
163 
164 /**
165  * qemu_plugin_tb_insn_get(): get next plugin record for translation.
166  * @tb: the internal tb context
167  * @pc: address of instruction
168  */
169 static inline
170 struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb,
171                                                  uint64_t pc)
172 {
173     struct qemu_plugin_insn *insn;
174     int i, j;
175 
176     if (unlikely(tb->n == tb->insns->len)) {
177         struct qemu_plugin_insn *new_insn = qemu_plugin_insn_alloc();
178         g_ptr_array_add(tb->insns, new_insn);
179     }
180     insn = g_ptr_array_index(tb->insns, tb->n++);
181     g_byte_array_set_size(insn->data, 0);
182     insn->calls_helpers = false;
183     insn->mem_helper = false;
184     insn->vaddr = pc;
185 
186     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
187         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
188             g_array_set_size(insn->cbs[i][j], 0);
189         }
190     }
191 
192     return insn;
193 }
194 
195 /**
196  * struct CPUPluginState - per-CPU state for plugins
197  * @event_mask: plugin event bitmap. Modified only via async work.
198  */
199 struct CPUPluginState {
200     DECLARE_BITMAP(event_mask, QEMU_PLUGIN_EV_MAX);
201 };
202 
203 /**
204  * qemu_plugin_create_vcpu_state: allocate plugin state
205  */
206 CPUPluginState *qemu_plugin_create_vcpu_state(void);
207 
208 void qemu_plugin_vcpu_init_hook(CPUState *cpu);
209 void qemu_plugin_vcpu_exit_hook(CPUState *cpu);
210 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb);
211 void qemu_plugin_vcpu_idle_cb(CPUState *cpu);
212 void qemu_plugin_vcpu_resume_cb(CPUState *cpu);
213 void
214 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1,
215                          uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5,
216                          uint64_t a6, uint64_t a7, uint64_t a8);
217 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret);
218 
219 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
220                              MemOpIdx oi, enum qemu_plugin_mem_rw rw);
221 
222 void qemu_plugin_flush_cb(void);
223 
224 void qemu_plugin_atexit_cb(void);
225 
226 void qemu_plugin_add_dyn_cb_arr(GArray *arr);
227 
228 static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
229 {
230     cpu->plugin_mem_cbs = NULL;
231 }
232 
233 /**
234  * qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks
235  *
236  * This is a user-mode only helper that ensure we have fully cleared
237  * callbacks from all threads before calling the exit callbacks. This
238  * is so the plugins themselves don't have to jump through hoops to
239  * guard against race conditions.
240  */
241 void qemu_plugin_user_exit(void);
242 
243 /**
244  * qemu_plugin_user_prefork_lock(): take plugin lock before forking
245  *
246  * This is a user-mode only helper to take the internal plugin lock
247  * before a fork event. This is ensure a consistent lock state
248  */
249 void qemu_plugin_user_prefork_lock(void);
250 
251 /**
252  * qemu_plugin_user_postfork(): reset the plugin lock
253  * @is_child: is this thread the child
254  *
255  * This user-mode only helper resets the lock state after a fork so we
256  * can continue using the plugin interface.
257  */
258 void qemu_plugin_user_postfork(bool is_child);
259 
260 #else /* !CONFIG_PLUGIN */
261 
262 static inline void qemu_plugin_add_opts(void)
263 { }
264 
265 static inline void qemu_plugin_opt_parse(const char *optstr,
266                                          QemuPluginList *head)
267 {
268     error_report("plugin interface not enabled in this build");
269     exit(1);
270 }
271 
272 static inline int qemu_plugin_load_list(QemuPluginList *head, Error **errp)
273 {
274     return 0;
275 }
276 
277 static inline void qemu_plugin_vcpu_init_hook(CPUState *cpu)
278 { }
279 
280 static inline void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
281 { }
282 
283 static inline void qemu_plugin_tb_trans_cb(CPUState *cpu,
284                                            struct qemu_plugin_tb *tb)
285 { }
286 
287 static inline void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
288 { }
289 
290 static inline void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
291 { }
292 
293 static inline void
294 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
295                          uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6,
296                          uint64_t a7, uint64_t a8)
297 { }
298 
299 static inline
300 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
301 { }
302 
303 static inline void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
304                                            MemOpIdx oi,
305                                            enum qemu_plugin_mem_rw rw)
306 { }
307 
308 static inline void qemu_plugin_flush_cb(void)
309 { }
310 
311 static inline void qemu_plugin_atexit_cb(void)
312 { }
313 
314 static inline
315 void qemu_plugin_add_dyn_cb_arr(GArray *arr)
316 { }
317 
318 static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
319 { }
320 
321 static inline void qemu_plugin_user_exit(void)
322 { }
323 
324 static inline void qemu_plugin_user_prefork_lock(void)
325 { }
326 
327 static inline void qemu_plugin_user_postfork(bool is_child)
328 { }
329 
330 #endif /* !CONFIG_PLUGIN */
331 
332 #endif /* QEMU_PLUGIN_H */
333