xref: /qemu/plugins/api.c (revision 465a4b80e2afd93f2eb4c9f55233788ebfb47ac0)
1 /*
2  * QEMU Plugin API
3  *
4  * This provides the API that is available to the plugins to interact
5  * with QEMU. We have to be careful not to expose internal details of
6  * how QEMU works so we abstract out things like translation and
7  * instructions to anonymous data types:
8  *
9  *  qemu_plugin_tb
10  *  qemu_plugin_insn
11  *  qemu_plugin_register
12  *
13  * Which can then be passed back into the API to do additional things.
14  * As such all the public functions in here are exported in
15  * qemu-plugin.h.
16  *
17  * The general life-cycle of a plugin is:
18  *
19  *  - plugin is loaded, public qemu_plugin_install called
20  *    - the install func registers callbacks for events
21  *    - usually an atexit_cb is registered to dump info at the end
22  *  - when a registered event occurs the plugin is called
23  *     - some events pass additional info
24  *     - during translation the plugin can decide to instrument any
25  *       instruction
26  *  - when QEMU exits all the registered atexit callbacks are called
27  *
28  * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
29  * Copyright (C) 2019, Linaro
30  *
31  * License: GNU GPL, version 2 or later.
32  *   See the COPYING file in the top-level directory.
33  *
34  * SPDX-License-Identifier: GPL-2.0-or-later
35  *
36  */
37 
38 #include "qemu/osdep.h"
39 #include "qemu/main-loop.h"
40 #include "qemu/plugin.h"
41 #include "qemu/log.h"
42 #include "tcg/tcg.h"
43 #include "exec/gdbstub.h"
44 #include "exec/target_page.h"
45 #include "exec/translation-block.h"
46 #include "exec/translator.h"
47 #include "disas/disas.h"
48 #include "plugin.h"
49 
50 /* Uninstall and Reset handlers */
51 
52 void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
53 {
54     plugin_reset_uninstall(id, cb, false);
55 }
56 
57 void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
58 {
59     plugin_reset_uninstall(id, cb, true);
60 }
61 
62 /*
63  * Plugin Register Functions
64  *
65  * This allows the plugin to register callbacks for various events
66  * during the translation.
67  */
68 
69 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
70                                        qemu_plugin_vcpu_simple_cb_t cb)
71 {
72     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
73 }
74 
75 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
76                                        qemu_plugin_vcpu_simple_cb_t cb)
77 {
78     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
79 }
80 
81 static bool tb_is_mem_only(void)
82 {
83     return tb_cflags(tcg_ctx->gen_tb) & CF_MEMI_ONLY;
84 }
85 
86 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
87                                           qemu_plugin_vcpu_udata_cb_t cb,
88                                           enum qemu_plugin_cb_flags flags,
89                                           void *udata)
90 {
91     if (!tb_is_mem_only()) {
92         plugin_register_dyn_cb__udata(&tb->cbs, cb, flags, udata);
93     }
94 }
95 
96 void qemu_plugin_register_vcpu_tb_exec_cond_cb(struct qemu_plugin_tb *tb,
97                                                qemu_plugin_vcpu_udata_cb_t cb,
98                                                enum qemu_plugin_cb_flags flags,
99                                                enum qemu_plugin_cond cond,
100                                                qemu_plugin_u64 entry,
101                                                uint64_t imm,
102                                                void *udata)
103 {
104     if (cond == QEMU_PLUGIN_COND_NEVER || tb_is_mem_only()) {
105         return;
106     }
107     if (cond == QEMU_PLUGIN_COND_ALWAYS) {
108         qemu_plugin_register_vcpu_tb_exec_cb(tb, cb, flags, udata);
109         return;
110     }
111     plugin_register_dyn_cond_cb__udata(&tb->cbs, cb, flags,
112                                        cond, entry, imm, udata);
113 }
114 
115 void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
116     struct qemu_plugin_tb *tb,
117     enum qemu_plugin_op op,
118     qemu_plugin_u64 entry,
119     uint64_t imm)
120 {
121     if (!tb_is_mem_only()) {
122         plugin_register_inline_op_on_entry(&tb->cbs, 0, op, entry, imm);
123     }
124 }
125 
126 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
127                                             qemu_plugin_vcpu_udata_cb_t cb,
128                                             enum qemu_plugin_cb_flags flags,
129                                             void *udata)
130 {
131     if (!tb_is_mem_only()) {
132         plugin_register_dyn_cb__udata(&insn->insn_cbs, cb, flags, udata);
133     }
134 }
135 
136 void qemu_plugin_register_vcpu_insn_exec_cond_cb(
137     struct qemu_plugin_insn *insn,
138     qemu_plugin_vcpu_udata_cb_t cb,
139     enum qemu_plugin_cb_flags flags,
140     enum qemu_plugin_cond cond,
141     qemu_plugin_u64 entry,
142     uint64_t imm,
143     void *udata)
144 {
145     if (cond == QEMU_PLUGIN_COND_NEVER || tb_is_mem_only()) {
146         return;
147     }
148     if (cond == QEMU_PLUGIN_COND_ALWAYS) {
149         qemu_plugin_register_vcpu_insn_exec_cb(insn, cb, flags, udata);
150         return;
151     }
152     plugin_register_dyn_cond_cb__udata(&insn->insn_cbs, cb, flags,
153                                        cond, entry, imm, udata);
154 }
155 
156 void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
157     struct qemu_plugin_insn *insn,
158     enum qemu_plugin_op op,
159     qemu_plugin_u64 entry,
160     uint64_t imm)
161 {
162     if (!tb_is_mem_only()) {
163         plugin_register_inline_op_on_entry(&insn->insn_cbs, 0, op, entry, imm);
164     }
165 }
166 
167 
168 /*
169  * We always plant memory instrumentation because they don't finalise until
170  * after the operation has complete.
171  */
172 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
173                                       qemu_plugin_vcpu_mem_cb_t cb,
174                                       enum qemu_plugin_cb_flags flags,
175                                       enum qemu_plugin_mem_rw rw,
176                                       void *udata)
177 {
178     plugin_register_vcpu_mem_cb(&insn->mem_cbs, cb, flags, rw, udata);
179 }
180 
181 void qemu_plugin_register_vcpu_mem_inline_per_vcpu(
182     struct qemu_plugin_insn *insn,
183     enum qemu_plugin_mem_rw rw,
184     enum qemu_plugin_op op,
185     qemu_plugin_u64 entry,
186     uint64_t imm)
187 {
188     plugin_register_inline_op_on_entry(&insn->mem_cbs, rw, op, entry, imm);
189 }
190 
191 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
192                                            qemu_plugin_vcpu_tb_trans_cb_t cb)
193 {
194     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
195 }
196 
197 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
198                                           qemu_plugin_vcpu_syscall_cb_t cb)
199 {
200     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
201 }
202 
203 void
204 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
205                                          qemu_plugin_vcpu_syscall_ret_cb_t cb)
206 {
207     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
208 }
209 
210 /*
211  * Plugin Queries
212  *
213  * These are queries that the plugin can make to gauge information
214  * from our opaque data types. We do not want to leak internal details
215  * here just information useful to the plugin.
216  */
217 
218 /*
219  * Translation block information:
220  *
221  * A plugin can query the virtual address of the start of the block
222  * and the number of instructions in it. It can also get access to
223  * each translated instruction.
224  */
225 
226 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
227 {
228     return tb->n;
229 }
230 
231 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
232 {
233     const DisasContextBase *db = tcg_ctx->plugin_db;
234     return db->pc_first;
235 }
236 
237 struct qemu_plugin_insn *
238 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
239 {
240     if (unlikely(idx >= tb->n)) {
241         return NULL;
242     }
243     return g_ptr_array_index(tb->insns, idx);
244 }
245 
246 /*
247  * Instruction information
248  *
249  * These queries allow the plugin to retrieve information about each
250  * instruction being translated.
251  */
252 
253 size_t qemu_plugin_insn_data(const struct qemu_plugin_insn *insn,
254                              void *dest, size_t len)
255 {
256     const DisasContextBase *db = tcg_ctx->plugin_db;
257 
258     len = MIN(len, insn->len);
259     return translator_st(db, dest, insn->vaddr, len) ? len : 0;
260 }
261 
262 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
263 {
264     return insn->len;
265 }
266 
267 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
268 {
269     return insn->vaddr;
270 }
271 
272 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
273 {
274     const DisasContextBase *db = tcg_ctx->plugin_db;
275     vaddr page0_last = db->pc_first | ~qemu_target_page_mask();
276 
277     if (db->fake_insn) {
278         return NULL;
279     }
280 
281     /*
282      * ??? The return value is not intended for use of host memory,
283      * but as a proxy for address space and physical address.
284      * Thus we are only interested in the first byte and do not
285      * care about spanning pages.
286      */
287     if (insn->vaddr <= page0_last) {
288         if (db->host_addr[0] == NULL) {
289             return NULL;
290         }
291         return db->host_addr[0] + insn->vaddr - db->pc_first;
292     } else {
293         if (db->host_addr[1] == NULL) {
294             return NULL;
295         }
296         return db->host_addr[1] + insn->vaddr - (page0_last + 1);
297     }
298 }
299 
300 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
301 {
302     return plugin_disas(tcg_ctx->cpu, tcg_ctx->plugin_db,
303                         insn->vaddr, insn->len);
304 }
305 
306 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
307 {
308     const char *sym = lookup_symbol(insn->vaddr);
309     return sym[0] != 0 ? sym : NULL;
310 }
311 
312 /*
313  * The memory queries allow the plugin to query information about a
314  * memory access.
315  */
316 
317 unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
318 {
319     MemOp op = get_memop(info);
320     return op & MO_SIZE;
321 }
322 
323 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
324 {
325     MemOp op = get_memop(info);
326     return op & MO_SIGN;
327 }
328 
329 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
330 {
331     MemOp op = get_memop(info);
332     return (op & MO_BSWAP) == MO_BE;
333 }
334 
335 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
336 {
337     return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
338 }
339 
340 qemu_plugin_mem_value qemu_plugin_mem_get_value(qemu_plugin_meminfo_t info)
341 {
342     uint64_t low = current_cpu->neg.plugin_mem_value_low;
343     qemu_plugin_mem_value value;
344 
345     switch (qemu_plugin_mem_size_shift(info)) {
346     case 0:
347         value.type = QEMU_PLUGIN_MEM_VALUE_U8;
348         value.data.u8 = (uint8_t)low;
349         break;
350     case 1:
351         value.type = QEMU_PLUGIN_MEM_VALUE_U16;
352         value.data.u16 = (uint16_t)low;
353         break;
354     case 2:
355         value.type = QEMU_PLUGIN_MEM_VALUE_U32;
356         value.data.u32 = (uint32_t)low;
357         break;
358     case 3:
359         value.type = QEMU_PLUGIN_MEM_VALUE_U64;
360         value.data.u64 = low;
361         break;
362     case 4:
363         value.type = QEMU_PLUGIN_MEM_VALUE_U128;
364         value.data.u128.low = low;
365         value.data.u128.high = current_cpu->neg.plugin_mem_value_high;
366         break;
367     default:
368         g_assert_not_reached();
369     }
370     return value;
371 }
372 
373 int qemu_plugin_num_vcpus(void)
374 {
375     return plugin_num_vcpus();
376 }
377 
378 /*
379  * Plugin output
380  */
381 void qemu_plugin_outs(const char *string)
382 {
383     qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
384 }
385 
386 bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
387 {
388     return name && value && qapi_bool_parse(name, value, ret, NULL);
389 }
390 
391 /*
392  * Create register handles.
393  *
394  * We need to create a handle for each register so the plugin
395  * infrastructure can call gdbstub to read a register. They are
396  * currently just a pointer encapsulation of the gdb_reg but in
397  * future may hold internal plugin state so its important plugin
398  * authors are not tempted to treat them as numbers.
399  *
400  * We also construct a result array with those handles and some
401  * ancillary data the plugin might find useful.
402  */
403 
404 static GArray *create_register_handles(GArray *gdbstub_regs)
405 {
406     GArray *find_data = g_array_new(true, true,
407                                     sizeof(qemu_plugin_reg_descriptor));
408 
409     for (int i = 0; i < gdbstub_regs->len; i++) {
410         GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);
411         qemu_plugin_reg_descriptor desc;
412 
413         /* skip "un-named" regs */
414         if (!grd->name) {
415             continue;
416         }
417 
418         /* Create a record for the plugin */
419         desc.handle = GINT_TO_POINTER(grd->gdb_reg + 1);
420         desc.name = g_intern_string(grd->name);
421         desc.feature = g_intern_string(grd->feature_name);
422         g_array_append_val(find_data, desc);
423     }
424 
425     return find_data;
426 }
427 
428 GArray *qemu_plugin_get_registers(void)
429 {
430     g_assert(current_cpu);
431 
432     g_autoptr(GArray) regs = gdb_get_register_list(current_cpu);
433     return create_register_handles(regs);
434 }
435 
436 bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)
437 {
438     g_assert(current_cpu);
439 
440     if (len == 0) {
441         return false;
442     }
443 
444     g_byte_array_set_size(data, len);
445 
446     int result = cpu_memory_rw_debug(current_cpu, addr, data->data,
447                                      data->len, false);
448 
449     if (result < 0) {
450         return false;
451     }
452 
453     return true;
454 }
455 
456 int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
457 {
458     g_assert(current_cpu);
459 
460     return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1);
461 }
462 
463 struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size)
464 {
465     return plugin_scoreboard_new(element_size);
466 }
467 
468 void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
469 {
470     plugin_scoreboard_free(score);
471 }
472 
473 void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score,
474                                   unsigned int vcpu_index)
475 {
476     g_assert(vcpu_index < qemu_plugin_num_vcpus());
477     /* we can't use g_array_index since entry size is not statically known */
478     char *base_ptr = score->data->data;
479     return base_ptr + vcpu_index * g_array_get_element_size(score->data);
480 }
481 
482 static uint64_t *plugin_u64_address(qemu_plugin_u64 entry,
483                                     unsigned int vcpu_index)
484 {
485     char *ptr = qemu_plugin_scoreboard_find(entry.score, vcpu_index);
486     return (uint64_t *)(ptr + entry.offset);
487 }
488 
489 void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index,
490                          uint64_t added)
491 {
492     *plugin_u64_address(entry, vcpu_index) += added;
493 }
494 
495 uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry,
496                              unsigned int vcpu_index)
497 {
498     return *plugin_u64_address(entry, vcpu_index);
499 }
500 
501 void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
502                          uint64_t val)
503 {
504     *plugin_u64_address(entry, vcpu_index) = val;
505 }
506 
507 uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry)
508 {
509     uint64_t total = 0;
510     for (int i = 0, n = qemu_plugin_num_vcpus(); i < n; ++i) {
511         total += qemu_plugin_u64_get(entry, i);
512     }
513     return total;
514 }
515 
516