xref: /openbmc/qemu/plugins/api.c (revision 957f583b)
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 "exec/ram_addr.h"
46 #include "disas/disas.h"
47 #include "plugin.h"
48 #ifndef CONFIG_USER_ONLY
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 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn)
220 {
221     return insn->data->data;
222 }
223 
224 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
225 {
226     return insn->data->len;
227 }
228 
229 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
230 {
231     return insn->vaddr;
232 }
233 
234 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
235 {
236     return insn->haddr;
237 }
238 
239 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
240 {
241     CPUState *cpu = current_cpu;
242     return plugin_disas(cpu, insn->vaddr, insn->data->len);
243 }
244 
245 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
246 {
247     const char *sym = lookup_symbol(insn->vaddr);
248     return sym[0] != 0 ? sym : NULL;
249 }
250 
251 /*
252  * The memory queries allow the plugin to query information about a
253  * memory access.
254  */
255 
256 unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
257 {
258     MemOp op = get_memop(info);
259     return op & MO_SIZE;
260 }
261 
262 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
263 {
264     MemOp op = get_memop(info);
265     return op & MO_SIGN;
266 }
267 
268 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
269 {
270     MemOp op = get_memop(info);
271     return (op & MO_BSWAP) == MO_BE;
272 }
273 
274 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
275 {
276     return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
277 }
278 
279 /*
280  * Virtual Memory queries
281  */
282 
283 #ifdef CONFIG_SOFTMMU
284 static __thread struct qemu_plugin_hwaddr hwaddr_info;
285 #endif
286 
287 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
288                                                   uint64_t vaddr)
289 {
290 #ifdef CONFIG_SOFTMMU
291     CPUState *cpu = current_cpu;
292     unsigned int mmu_idx = get_mmuidx(info);
293     enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
294     hwaddr_info.is_store = (rw & QEMU_PLUGIN_MEM_W) != 0;
295 
296     assert(mmu_idx < NB_MMU_MODES);
297 
298     if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
299                            hwaddr_info.is_store, &hwaddr_info)) {
300         error_report("invalid use of qemu_plugin_get_hwaddr");
301         return NULL;
302     }
303 
304     return &hwaddr_info;
305 #else
306     return NULL;
307 #endif
308 }
309 
310 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr)
311 {
312 #ifdef CONFIG_SOFTMMU
313     return haddr->is_io;
314 #else
315     return false;
316 #endif
317 }
318 
319 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr)
320 {
321 #ifdef CONFIG_SOFTMMU
322     if (haddr) {
323         return haddr->phys_addr;
324     }
325 #endif
326     return 0;
327 }
328 
329 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h)
330 {
331 #ifdef CONFIG_SOFTMMU
332     if (h && h->is_io) {
333         MemoryRegion *mr = h->mr;
334         if (!mr->name) {
335             unsigned maddr = (uintptr_t)mr;
336             g_autofree char *temp = g_strdup_printf("anon%08x", maddr);
337             return g_intern_string(temp);
338         } else {
339             return g_intern_string(mr->name);
340         }
341     } else {
342         return g_intern_static_string("RAM");
343     }
344 #else
345     return g_intern_static_string("Invalid");
346 #endif
347 }
348 
349 int qemu_plugin_num_vcpus(void)
350 {
351     return plugin_num_vcpus();
352 }
353 
354 /*
355  * Plugin output
356  */
357 void qemu_plugin_outs(const char *string)
358 {
359     qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
360 }
361 
362 bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
363 {
364     return name && value && qapi_bool_parse(name, value, ret, NULL);
365 }
366 
367 /*
368  * Binary path, start and end locations
369  */
370 const char *qemu_plugin_path_to_binary(void)
371 {
372     char *path = NULL;
373 #ifdef CONFIG_USER_ONLY
374     TaskState *ts = get_task_state(current_cpu);
375     path = g_strdup(ts->bprm->filename);
376 #endif
377     return path;
378 }
379 
380 uint64_t qemu_plugin_start_code(void)
381 {
382     uint64_t start = 0;
383 #ifdef CONFIG_USER_ONLY
384     TaskState *ts = get_task_state(current_cpu);
385     start = ts->info->start_code;
386 #endif
387     return start;
388 }
389 
390 uint64_t qemu_plugin_end_code(void)
391 {
392     uint64_t end = 0;
393 #ifdef CONFIG_USER_ONLY
394     TaskState *ts = get_task_state(current_cpu);
395     end = ts->info->end_code;
396 #endif
397     return end;
398 }
399 
400 uint64_t qemu_plugin_entry_code(void)
401 {
402     uint64_t entry = 0;
403 #ifdef CONFIG_USER_ONLY
404     TaskState *ts = get_task_state(current_cpu);
405     entry = ts->info->entry;
406 #endif
407     return entry;
408 }
409 
410 /*
411  * Create register handles.
412  *
413  * We need to create a handle for each register so the plugin
414  * infrastructure can call gdbstub to read a register. They are
415  * currently just a pointer encapsulation of the gdb_reg but in
416  * future may hold internal plugin state so its important plugin
417  * authors are not tempted to treat them as numbers.
418  *
419  * We also construct a result array with those handles and some
420  * ancillary data the plugin might find useful.
421  */
422 
423 static GArray *create_register_handles(GArray *gdbstub_regs)
424 {
425     GArray *find_data = g_array_new(true, true,
426                                     sizeof(qemu_plugin_reg_descriptor));
427 
428     for (int i = 0; i < gdbstub_regs->len; i++) {
429         GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);
430         qemu_plugin_reg_descriptor desc;
431 
432         /* skip "un-named" regs */
433         if (!grd->name) {
434             continue;
435         }
436 
437         /* Create a record for the plugin */
438         desc.handle = GINT_TO_POINTER(grd->gdb_reg);
439         desc.name = g_intern_string(grd->name);
440         desc.feature = g_intern_string(grd->feature_name);
441         g_array_append_val(find_data, desc);
442     }
443 
444     return find_data;
445 }
446 
447 GArray *qemu_plugin_get_registers(void)
448 {
449     g_assert(current_cpu);
450 
451     g_autoptr(GArray) regs = gdb_get_register_list(current_cpu);
452     return create_register_handles(regs);
453 }
454 
455 int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
456 {
457     g_assert(current_cpu);
458 
459     return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg));
460 }
461 
462 struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size)
463 {
464     return plugin_scoreboard_new(element_size);
465 }
466 
467 void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
468 {
469     plugin_scoreboard_free(score);
470 }
471 
472 void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score,
473                                   unsigned int vcpu_index)
474 {
475     g_assert(vcpu_index < qemu_plugin_num_vcpus());
476     /* we can't use g_array_index since entry size is not statically known */
477     char *base_ptr = score->data->data;
478     return base_ptr + vcpu_index * g_array_get_element_size(score->data);
479 }
480 
481 static uint64_t *plugin_u64_address(qemu_plugin_u64 entry,
482                                     unsigned int vcpu_index)
483 {
484     char *ptr = qemu_plugin_scoreboard_find(entry.score, vcpu_index);
485     return (uint64_t *)(ptr + entry.offset);
486 }
487 
488 void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index,
489                          uint64_t added)
490 {
491     *plugin_u64_address(entry, vcpu_index) += added;
492 }
493 
494 uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry,
495                              unsigned int vcpu_index)
496 {
497     return *plugin_u64_address(entry, vcpu_index);
498 }
499 
500 void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
501                          uint64_t val)
502 {
503     *plugin_u64_address(entry, vcpu_index) = val;
504 }
505 
506 uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry)
507 {
508     uint64_t total = 0;
509     for (int i = 0, n = qemu_plugin_num_vcpus(); i < n; ++i) {
510         total += qemu_plugin_u64_get(entry, i);
511     }
512     return total;
513 }
514