xref: /qemu/include/qemu/plugin.h (revision 370ed600)
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 *optarg, 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_INLINE,
77     PLUGIN_N_CB_SUBTYPES,
78 };
79 
80 /*
81  * A dynamic callback has an insertion point that is determined at run-time.
82  * Usually the insertion point is somewhere in the code cache; think for
83  * instance of a callback to be called upon the execution of a particular TB.
84  */
85 struct qemu_plugin_dyn_cb {
86     union qemu_plugin_cb_sig f;
87     void *userp;
88     enum plugin_dyn_cb_subtype type;
89     /* @rw applies to mem callbacks only (both regular and inline) */
90     enum qemu_plugin_mem_rw rw;
91     /* fields specific to each dyn_cb type go here */
92     union {
93         struct {
94             enum qemu_plugin_op op;
95             uint64_t imm;
96         } inline_insn;
97     };
98 };
99 
100 /* Internal context for instrumenting an instruction */
101 struct qemu_plugin_insn {
102     GByteArray *data;
103     uint64_t vaddr;
104     void *haddr;
105     GArray *cbs[PLUGIN_N_CB_TYPES][PLUGIN_N_CB_SUBTYPES];
106     bool calls_helpers;
107 
108     /* if set, the instruction calls helpers that might access guest memory */
109     bool mem_helper;
110 
111     bool mem_only;
112 };
113 
114 /*
115  * qemu_plugin_insn allocate and cleanup functions. We don't expect to
116  * cleanup many of these structures. They are reused for each fresh
117  * translation.
118  */
119 
120 static inline void qemu_plugin_insn_cleanup_fn(gpointer data)
121 {
122     struct qemu_plugin_insn *insn = (struct qemu_plugin_insn *) data;
123     g_byte_array_free(insn->data, true);
124 }
125 
126 static inline struct qemu_plugin_insn *qemu_plugin_insn_alloc(void)
127 {
128     int i, j;
129     struct qemu_plugin_insn *insn = g_new0(struct qemu_plugin_insn, 1);
130     insn->data = g_byte_array_sized_new(4);
131 
132     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
133         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
134             insn->cbs[i][j] = g_array_new(false, false,
135                                           sizeof(struct qemu_plugin_dyn_cb));
136         }
137     }
138     return insn;
139 }
140 
141 /* Internal context for this TranslationBlock */
142 struct qemu_plugin_tb {
143     GPtrArray *insns;
144     size_t n;
145     uint64_t vaddr;
146     uint64_t vaddr2;
147     void *haddr1;
148     void *haddr2;
149     bool mem_only;
150 
151     /* if set, the TB calls helpers that might access guest memory */
152     bool mem_helper;
153 
154     GArray *cbs[PLUGIN_N_CB_SUBTYPES];
155 };
156 
157 /**
158  * qemu_plugin_tb_insn_get(): get next plugin record for translation.
159  * @tb: the internal tb context
160  * @pc: address of instruction
161  */
162 static inline
163 struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb,
164                                                  uint64_t pc)
165 {
166     struct qemu_plugin_insn *insn;
167     int i, j;
168 
169     if (unlikely(tb->n == tb->insns->len)) {
170         struct qemu_plugin_insn *new_insn = qemu_plugin_insn_alloc();
171         g_ptr_array_add(tb->insns, new_insn);
172     }
173     insn = g_ptr_array_index(tb->insns, tb->n++);
174     g_byte_array_set_size(insn->data, 0);
175     insn->calls_helpers = false;
176     insn->mem_helper = false;
177     insn->vaddr = pc;
178 
179     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
180         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
181             g_array_set_size(insn->cbs[i][j], 0);
182         }
183     }
184 
185     return insn;
186 }
187 
188 void qemu_plugin_vcpu_init_hook(CPUState *cpu);
189 void qemu_plugin_vcpu_exit_hook(CPUState *cpu);
190 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb);
191 void qemu_plugin_vcpu_idle_cb(CPUState *cpu);
192 void qemu_plugin_vcpu_resume_cb(CPUState *cpu);
193 void
194 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1,
195                          uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5,
196                          uint64_t a6, uint64_t a7, uint64_t a8);
197 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret);
198 
199 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
200                              MemOpIdx oi, enum qemu_plugin_mem_rw rw);
201 
202 void qemu_plugin_flush_cb(void);
203 
204 void qemu_plugin_atexit_cb(void);
205 
206 void qemu_plugin_add_dyn_cb_arr(GArray *arr);
207 
208 static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
209 {
210     cpu->plugin_mem_cbs = NULL;
211 }
212 
213 /**
214  * qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks
215  *
216  * This is a user-mode only helper that ensure we have fully cleared
217  * callbacks from all threads before calling the exit callbacks. This
218  * is so the plugins themselves don't have to jump through hoops to
219  * guard against race conditions.
220  */
221 void qemu_plugin_user_exit(void);
222 
223 /**
224  * qemu_plugin_user_prefork_lock(): take plugin lock before forking
225  *
226  * This is a user-mode only helper to take the internal plugin lock
227  * before a fork event. This is ensure a consistent lock state
228  */
229 void qemu_plugin_user_prefork_lock(void);
230 
231 /**
232  * qemu_plugin_user_postfork(): reset the plugin lock
233  * @is_child: is this thread the child
234  *
235  * This user-mode only helper resets the lock state after a fork so we
236  * can continue using the plugin interface.
237  */
238 void qemu_plugin_user_postfork(bool is_child);
239 
240 #else /* !CONFIG_PLUGIN */
241 
242 static inline void qemu_plugin_add_opts(void)
243 { }
244 
245 static inline void qemu_plugin_opt_parse(const char *optarg,
246                                          QemuPluginList *head)
247 {
248     error_report("plugin interface not enabled in this build");
249     exit(1);
250 }
251 
252 static inline int qemu_plugin_load_list(QemuPluginList *head, Error **errp)
253 {
254     return 0;
255 }
256 
257 static inline void qemu_plugin_vcpu_init_hook(CPUState *cpu)
258 { }
259 
260 static inline void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
261 { }
262 
263 static inline void qemu_plugin_tb_trans_cb(CPUState *cpu,
264                                            struct qemu_plugin_tb *tb)
265 { }
266 
267 static inline void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
268 { }
269 
270 static inline void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
271 { }
272 
273 static inline void
274 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
275                          uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6,
276                          uint64_t a7, uint64_t a8)
277 { }
278 
279 static inline
280 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
281 { }
282 
283 static inline void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
284                                            MemOpIdx oi,
285                                            enum qemu_plugin_mem_rw rw)
286 { }
287 
288 static inline void qemu_plugin_flush_cb(void)
289 { }
290 
291 static inline void qemu_plugin_atexit_cb(void)
292 { }
293 
294 static inline
295 void qemu_plugin_add_dyn_cb_arr(GArray *arr)
296 { }
297 
298 static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
299 { }
300 
301 static inline void qemu_plugin_user_exit(void)
302 { }
303 
304 static inline void qemu_plugin_user_prefork_lock(void)
305 { }
306 
307 static inline void qemu_plugin_user_postfork(bool is_child)
308 { }
309 
310 #endif /* !CONFIG_PLUGIN */
311 
312 #endif /* QEMU_PLUGIN_H */
313