xref: /openbmc/qemu/plugins/api.c (revision 4abc8923)
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/exec-all.h"
44 #include "exec/gdbstub.h"
45 #include "disas/disas.h"
46 #include "plugin.h"
47 #ifndef CONFIG_USER_ONLY
48 #include "exec/ram_addr.h"
49 #include "qemu/plugin-memory.h"
50 #include "hw/boards.h"
51 #else
52 #include "qemu.h"
53 #ifdef CONFIG_LINUX
54 #include "loader.h"
55 #endif
56 #endif
57 
58 /* Uninstall and Reset handlers */
59 
60 void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
61 {
62     plugin_reset_uninstall(id, cb, false);
63 }
64 
65 void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
66 {
67     plugin_reset_uninstall(id, cb, true);
68 }
69 
70 /*
71  * Plugin Register Functions
72  *
73  * This allows the plugin to register callbacks for various events
74  * during the translation.
75  */
76 
77 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
78                                        qemu_plugin_vcpu_simple_cb_t cb)
79 {
80     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
81 }
82 
83 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
84                                        qemu_plugin_vcpu_simple_cb_t cb)
85 {
86     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
87 }
88 
89 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
90                                           qemu_plugin_vcpu_udata_cb_t cb,
91                                           enum qemu_plugin_cb_flags flags,
92                                           void *udata)
93 {
94     if (!tb->mem_only) {
95         plugin_register_dyn_cb__udata(&tb->cbs, cb, flags, udata);
96     }
97 }
98 
99 void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
100     struct qemu_plugin_tb *tb,
101     enum qemu_plugin_op op,
102     qemu_plugin_u64 entry,
103     uint64_t imm)
104 {
105     if (!tb->mem_only) {
106         plugin_register_inline_op_on_entry(&tb->cbs, 0, op, entry, imm);
107     }
108 }
109 
110 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
111                                             qemu_plugin_vcpu_udata_cb_t cb,
112                                             enum qemu_plugin_cb_flags flags,
113                                             void *udata)
114 {
115     if (!insn->mem_only) {
116         plugin_register_dyn_cb__udata(&insn->insn_cbs, cb, flags, udata);
117     }
118 }
119 
120 void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
121     struct qemu_plugin_insn *insn,
122     enum qemu_plugin_op op,
123     qemu_plugin_u64 entry,
124     uint64_t imm)
125 {
126     if (!insn->mem_only) {
127         plugin_register_inline_op_on_entry(&insn->insn_cbs, 0, op, entry, imm);
128     }
129 }
130 
131 
132 /*
133  * We always plant memory instrumentation because they don't finalise until
134  * after the operation has complete.
135  */
136 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
137                                       qemu_plugin_vcpu_mem_cb_t cb,
138                                       enum qemu_plugin_cb_flags flags,
139                                       enum qemu_plugin_mem_rw rw,
140                                       void *udata)
141 {
142     plugin_register_vcpu_mem_cb(&insn->mem_cbs, cb, flags, rw, udata);
143 }
144 
145 void qemu_plugin_register_vcpu_mem_inline_per_vcpu(
146     struct qemu_plugin_insn *insn,
147     enum qemu_plugin_mem_rw rw,
148     enum qemu_plugin_op op,
149     qemu_plugin_u64 entry,
150     uint64_t imm)
151 {
152     plugin_register_inline_op_on_entry(&insn->mem_cbs, rw, op, entry, imm);
153 }
154 
155 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
156                                            qemu_plugin_vcpu_tb_trans_cb_t cb)
157 {
158     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
159 }
160 
161 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
162                                           qemu_plugin_vcpu_syscall_cb_t cb)
163 {
164     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
165 }
166 
167 void
168 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
169                                          qemu_plugin_vcpu_syscall_ret_cb_t cb)
170 {
171     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
172 }
173 
174 /*
175  * Plugin Queries
176  *
177  * These are queries that the plugin can make to gauge information
178  * from our opaque data types. We do not want to leak internal details
179  * here just information useful to the plugin.
180  */
181 
182 /*
183  * Translation block information:
184  *
185  * A plugin can query the virtual address of the start of the block
186  * and the number of instructions in it. It can also get access to
187  * each translated instruction.
188  */
189 
190 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
191 {
192     return tb->n;
193 }
194 
195 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
196 {
197     return tb->vaddr;
198 }
199 
200 struct qemu_plugin_insn *
201 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
202 {
203     struct qemu_plugin_insn *insn;
204     if (unlikely(idx >= tb->n)) {
205         return NULL;
206     }
207     insn = g_ptr_array_index(tb->insns, idx);
208     insn->mem_only = tb->mem_only;
209     return insn;
210 }
211 
212 /*
213  * Instruction information
214  *
215  * These queries allow the plugin to retrieve information about each
216  * instruction being translated.
217  */
218 
219 size_t qemu_plugin_insn_data(const struct qemu_plugin_insn *insn,
220                              void *dest, size_t len)
221 {
222     len = MIN(len, insn->data->len);
223     memcpy(dest, insn->data->data, len);
224     return len;
225 }
226 
227 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
228 {
229     return insn->data->len;
230 }
231 
232 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
233 {
234     return insn->vaddr;
235 }
236 
237 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
238 {
239     return insn->haddr;
240 }
241 
242 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
243 {
244     CPUState *cpu = current_cpu;
245     return plugin_disas(cpu, insn->vaddr, insn->data->len);
246 }
247 
248 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
249 {
250     const char *sym = lookup_symbol(insn->vaddr);
251     return sym[0] != 0 ? sym : NULL;
252 }
253 
254 /*
255  * The memory queries allow the plugin to query information about a
256  * memory access.
257  */
258 
259 unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
260 {
261     MemOp op = get_memop(info);
262     return op & MO_SIZE;
263 }
264 
265 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
266 {
267     MemOp op = get_memop(info);
268     return op & MO_SIGN;
269 }
270 
271 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
272 {
273     MemOp op = get_memop(info);
274     return (op & MO_BSWAP) == MO_BE;
275 }
276 
277 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
278 {
279     return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
280 }
281 
282 /*
283  * Virtual Memory queries
284  */
285 
286 #ifdef CONFIG_SOFTMMU
287 static __thread struct qemu_plugin_hwaddr hwaddr_info;
288 #endif
289 
290 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
291                                                   uint64_t vaddr)
292 {
293 #ifdef CONFIG_SOFTMMU
294     CPUState *cpu = current_cpu;
295     unsigned int mmu_idx = get_mmuidx(info);
296     enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
297     hwaddr_info.is_store = (rw & QEMU_PLUGIN_MEM_W) != 0;
298 
299     assert(mmu_idx < NB_MMU_MODES);
300 
301     if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
302                            hwaddr_info.is_store, &hwaddr_info)) {
303         error_report("invalid use of qemu_plugin_get_hwaddr");
304         return NULL;
305     }
306 
307     return &hwaddr_info;
308 #else
309     return NULL;
310 #endif
311 }
312 
313 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr)
314 {
315 #ifdef CONFIG_SOFTMMU
316     return haddr->is_io;
317 #else
318     return false;
319 #endif
320 }
321 
322 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr)
323 {
324 #ifdef CONFIG_SOFTMMU
325     if (haddr) {
326         return haddr->phys_addr;
327     }
328 #endif
329     return 0;
330 }
331 
332 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h)
333 {
334 #ifdef CONFIG_SOFTMMU
335     if (h && h->is_io) {
336         MemoryRegion *mr = h->mr;
337         if (!mr->name) {
338             unsigned maddr = (uintptr_t)mr;
339             g_autofree char *temp = g_strdup_printf("anon%08x", maddr);
340             return g_intern_string(temp);
341         } else {
342             return g_intern_string(mr->name);
343         }
344     } else {
345         return g_intern_static_string("RAM");
346     }
347 #else
348     return g_intern_static_string("Invalid");
349 #endif
350 }
351 
352 int qemu_plugin_num_vcpus(void)
353 {
354     return plugin_num_vcpus();
355 }
356 
357 /*
358  * Plugin output
359  */
360 void qemu_plugin_outs(const char *string)
361 {
362     qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
363 }
364 
365 bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
366 {
367     return name && value && qapi_bool_parse(name, value, ret, NULL);
368 }
369 
370 /*
371  * Binary path, start and end locations
372  */
373 const char *qemu_plugin_path_to_binary(void)
374 {
375     char *path = NULL;
376 #ifdef CONFIG_USER_ONLY
377     TaskState *ts = get_task_state(current_cpu);
378     path = g_strdup(ts->bprm->filename);
379 #endif
380     return path;
381 }
382 
383 uint64_t qemu_plugin_start_code(void)
384 {
385     uint64_t start = 0;
386 #ifdef CONFIG_USER_ONLY
387     TaskState *ts = get_task_state(current_cpu);
388     start = ts->info->start_code;
389 #endif
390     return start;
391 }
392 
393 uint64_t qemu_plugin_end_code(void)
394 {
395     uint64_t end = 0;
396 #ifdef CONFIG_USER_ONLY
397     TaskState *ts = get_task_state(current_cpu);
398     end = ts->info->end_code;
399 #endif
400     return end;
401 }
402 
403 uint64_t qemu_plugin_entry_code(void)
404 {
405     uint64_t entry = 0;
406 #ifdef CONFIG_USER_ONLY
407     TaskState *ts = get_task_state(current_cpu);
408     entry = ts->info->entry;
409 #endif
410     return entry;
411 }
412 
413 /*
414  * Create register handles.
415  *
416  * We need to create a handle for each register so the plugin
417  * infrastructure can call gdbstub to read a register. They are
418  * currently just a pointer encapsulation of the gdb_reg but in
419  * future may hold internal plugin state so its important plugin
420  * authors are not tempted to treat them as numbers.
421  *
422  * We also construct a result array with those handles and some
423  * ancillary data the plugin might find useful.
424  */
425 
426 static GArray *create_register_handles(GArray *gdbstub_regs)
427 {
428     GArray *find_data = g_array_new(true, true,
429                                     sizeof(qemu_plugin_reg_descriptor));
430 
431     for (int i = 0; i < gdbstub_regs->len; i++) {
432         GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);
433         qemu_plugin_reg_descriptor desc;
434 
435         /* skip "un-named" regs */
436         if (!grd->name) {
437             continue;
438         }
439 
440         /* Create a record for the plugin */
441         desc.handle = GINT_TO_POINTER(grd->gdb_reg);
442         desc.name = g_intern_string(grd->name);
443         desc.feature = g_intern_string(grd->feature_name);
444         g_array_append_val(find_data, desc);
445     }
446 
447     return find_data;
448 }
449 
450 GArray *qemu_plugin_get_registers(void)
451 {
452     g_assert(current_cpu);
453 
454     g_autoptr(GArray) regs = gdb_get_register_list(current_cpu);
455     return create_register_handles(regs);
456 }
457 
458 int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
459 {
460     g_assert(current_cpu);
461 
462     return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg));
463 }
464 
465 struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size)
466 {
467     return plugin_scoreboard_new(element_size);
468 }
469 
470 void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
471 {
472     plugin_scoreboard_free(score);
473 }
474 
475 void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score,
476                                   unsigned int vcpu_index)
477 {
478     g_assert(vcpu_index < qemu_plugin_num_vcpus());
479     /* we can't use g_array_index since entry size is not statically known */
480     char *base_ptr = score->data->data;
481     return base_ptr + vcpu_index * g_array_get_element_size(score->data);
482 }
483 
484 static uint64_t *plugin_u64_address(qemu_plugin_u64 entry,
485                                     unsigned int vcpu_index)
486 {
487     char *ptr = qemu_plugin_scoreboard_find(entry.score, vcpu_index);
488     return (uint64_t *)(ptr + entry.offset);
489 }
490 
491 void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index,
492                          uint64_t added)
493 {
494     *plugin_u64_address(entry, vcpu_index) += added;
495 }
496 
497 uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry,
498                              unsigned int vcpu_index)
499 {
500     return *plugin_u64_address(entry, vcpu_index);
501 }
502 
503 void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
504                          uint64_t val)
505 {
506     *plugin_u64_address(entry, vcpu_index) = val;
507 }
508 
509 uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry)
510 {
511     uint64_t total = 0;
512     for (int i = 0, n = qemu_plugin_num_vcpus(); i < n; ++i) {
513         total += qemu_plugin_u64_get(entry, i);
514     }
515     return total;
516 }
517