xref: /qemu/include/qemu/plugin.h (revision d051d0e1)
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 
16 /*
17  * Events that plugins can subscribe to.
18  */
19 enum qemu_plugin_event {
20     QEMU_PLUGIN_EV_VCPU_INIT,
21     QEMU_PLUGIN_EV_VCPU_EXIT,
22     QEMU_PLUGIN_EV_VCPU_TB_TRANS,
23     QEMU_PLUGIN_EV_VCPU_IDLE,
24     QEMU_PLUGIN_EV_VCPU_RESUME,
25     QEMU_PLUGIN_EV_VCPU_SYSCALL,
26     QEMU_PLUGIN_EV_VCPU_SYSCALL_RET,
27     QEMU_PLUGIN_EV_FLUSH,
28     QEMU_PLUGIN_EV_ATEXIT,
29     QEMU_PLUGIN_EV_MAX, /* total number of plugin events we support */
30 };
31 
32 /*
33  * Option parsing/processing.
34  * Note that we can load an arbitrary number of plugins.
35  */
36 struct qemu_plugin_desc;
37 typedef QTAILQ_HEAD(, qemu_plugin_desc) QemuPluginList;
38 
39 #ifdef CONFIG_PLUGIN
40 extern QemuOptsList qemu_plugin_opts;
41 
42 static inline void qemu_plugin_add_opts(void)
43 {
44     qemu_add_opts(&qemu_plugin_opts);
45 }
46 
47 void qemu_plugin_opt_parse(const char *optarg, QemuPluginList *head);
48 int qemu_plugin_load_list(QemuPluginList *head, Error **errp);
49 
50 union qemu_plugin_cb_sig {
51     qemu_plugin_simple_cb_t          simple;
52     qemu_plugin_udata_cb_t           udata;
53     qemu_plugin_vcpu_simple_cb_t     vcpu_simple;
54     qemu_plugin_vcpu_udata_cb_t      vcpu_udata;
55     qemu_plugin_vcpu_tb_trans_cb_t   vcpu_tb_trans;
56     qemu_plugin_vcpu_mem_cb_t        vcpu_mem;
57     qemu_plugin_vcpu_syscall_cb_t    vcpu_syscall;
58     qemu_plugin_vcpu_syscall_ret_cb_t vcpu_syscall_ret;
59     void *generic;
60 };
61 
62 enum plugin_dyn_cb_type {
63     PLUGIN_CB_INSN,
64     PLUGIN_CB_MEM,
65     PLUGIN_N_CB_TYPES,
66 };
67 
68 enum plugin_dyn_cb_subtype {
69     PLUGIN_CB_REGULAR,
70     PLUGIN_CB_INLINE,
71     PLUGIN_N_CB_SUBTYPES,
72 };
73 
74 /*
75  * A dynamic callback has an insertion point that is determined at run-time.
76  * Usually the insertion point is somewhere in the code cache; think for
77  * instance of a callback to be called upon the execution of a particular TB.
78  */
79 struct qemu_plugin_dyn_cb {
80     union qemu_plugin_cb_sig f;
81     void *userp;
82     enum plugin_dyn_cb_subtype type;
83     /* @rw applies to mem callbacks only (both regular and inline) */
84     enum qemu_plugin_mem_rw rw;
85     /* fields specific to each dyn_cb type go here */
86     union {
87         struct {
88             enum qemu_plugin_op op;
89             uint64_t imm;
90         } inline_insn;
91     };
92 };
93 
94 /* Internal context for instrumenting an instruction */
95 struct qemu_plugin_insn {
96     GByteArray *data;
97     uint64_t vaddr;
98     void *haddr;
99     GArray *cbs[PLUGIN_N_CB_TYPES][PLUGIN_N_CB_SUBTYPES];
100     bool calls_helpers;
101     bool mem_helper;
102     bool mem_only;
103 };
104 
105 /*
106  * qemu_plugin_insn allocate and cleanup functions. We don't expect to
107  * cleanup many of these structures. They are reused for each fresh
108  * translation.
109  */
110 
111 static inline void qemu_plugin_insn_cleanup_fn(gpointer data)
112 {
113     struct qemu_plugin_insn *insn = (struct qemu_plugin_insn *) data;
114     g_byte_array_free(insn->data, true);
115 }
116 
117 static inline struct qemu_plugin_insn *qemu_plugin_insn_alloc(void)
118 {
119     int i, j;
120     struct qemu_plugin_insn *insn = g_new0(struct qemu_plugin_insn, 1);
121     insn->data = g_byte_array_sized_new(4);
122 
123     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
124         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
125             insn->cbs[i][j] = g_array_new(false, false,
126                                           sizeof(struct qemu_plugin_dyn_cb));
127         }
128     }
129     return insn;
130 }
131 
132 /* Internal context for this TranslationBlock */
133 struct qemu_plugin_tb {
134     GPtrArray *insns;
135     size_t n;
136     uint64_t vaddr;
137     uint64_t vaddr2;
138     void *haddr1;
139     void *haddr2;
140     bool mem_only;
141     GArray *cbs[PLUGIN_N_CB_SUBTYPES];
142 };
143 
144 /**
145  * qemu_plugin_tb_insn_get(): get next plugin record for translation.
146  *
147  */
148 static inline
149 struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb)
150 {
151     struct qemu_plugin_insn *insn;
152     int i, j;
153 
154     if (unlikely(tb->n == tb->insns->len)) {
155         struct qemu_plugin_insn *new_insn = qemu_plugin_insn_alloc();
156         g_ptr_array_add(tb->insns, new_insn);
157     }
158     insn = g_ptr_array_index(tb->insns, tb->n++);
159     g_byte_array_set_size(insn->data, 0);
160     insn->calls_helpers = false;
161     insn->mem_helper = false;
162 
163     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
164         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
165             g_array_set_size(insn->cbs[i][j], 0);
166         }
167     }
168 
169     return insn;
170 }
171 
172 void qemu_plugin_vcpu_init_hook(CPUState *cpu);
173 void qemu_plugin_vcpu_exit_hook(CPUState *cpu);
174 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb);
175 void qemu_plugin_vcpu_idle_cb(CPUState *cpu);
176 void qemu_plugin_vcpu_resume_cb(CPUState *cpu);
177 void
178 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1,
179                          uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5,
180                          uint64_t a6, uint64_t a7, uint64_t a8);
181 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret);
182 
183 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, uint32_t meminfo);
184 
185 void qemu_plugin_flush_cb(void);
186 
187 void qemu_plugin_atexit_cb(void);
188 
189 void qemu_plugin_add_dyn_cb_arr(GArray *arr);
190 
191 void qemu_plugin_disable_mem_helpers(CPUState *cpu);
192 
193 /**
194  * qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks
195  *
196  * This is a user-mode only helper that ensure we have fully cleared
197  * callbacks from all threads before calling the exit callbacks. This
198  * is so the plugins themselves don't have to jump through hoops to
199  * guard against race conditions.
200  */
201 void qemu_plugin_user_exit(void);
202 
203 #else /* !CONFIG_PLUGIN */
204 
205 static inline void qemu_plugin_add_opts(void)
206 { }
207 
208 static inline void qemu_plugin_opt_parse(const char *optarg,
209                                          QemuPluginList *head)
210 {
211     error_report("plugin interface not enabled in this build");
212     exit(1);
213 }
214 
215 static inline int qemu_plugin_load_list(QemuPluginList *head, Error **errp)
216 {
217     return 0;
218 }
219 
220 static inline void qemu_plugin_vcpu_init_hook(CPUState *cpu)
221 { }
222 
223 static inline void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
224 { }
225 
226 static inline void qemu_plugin_tb_trans_cb(CPUState *cpu,
227                                            struct qemu_plugin_tb *tb)
228 { }
229 
230 static inline void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
231 { }
232 
233 static inline void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
234 { }
235 
236 static inline void
237 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
238                          uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6,
239                          uint64_t a7, uint64_t a8)
240 { }
241 
242 static inline
243 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
244 { }
245 
246 static inline void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
247                                            uint32_t meminfo)
248 { }
249 
250 static inline void qemu_plugin_flush_cb(void)
251 { }
252 
253 static inline void qemu_plugin_atexit_cb(void)
254 { }
255 
256 static inline
257 void qemu_plugin_add_dyn_cb_arr(GArray *arr)
258 { }
259 
260 static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
261 { }
262 
263 static inline void qemu_plugin_user_exit(void)
264 { }
265 #endif /* !CONFIG_PLUGIN */
266 
267 #endif /* QEMU_PLUGIN_H */
268