linux-user: Move tswap_siginfo out of target code
[qemu/armbru.git] / plugins / api.c
blob8fa5a600ac3af78b3eb55374b8f20e37eec6b4f6
1 /*
2 * QEMU Plugin API
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:
9 * qemu_plugin_tb
10 * qemu_plugin_insn
11 * qemu_plugin_register
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.
17 * The general life-cycle of a plugin is:
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
28 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
29 * Copyright (C) 2019, Linaro
31 * License: GNU GPL, version 2 or later.
32 * See the COPYING file in the top-level directory.
34 * SPDX-License-Identifier: GPL-2.0-or-later
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
58 /* Uninstall and Reset handlers */
60 void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
62 plugin_reset_uninstall(id, cb, false);
65 void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
67 plugin_reset_uninstall(id, cb, true);
71 * Plugin Register Functions
73 * This allows the plugin to register callbacks for various events
74 * during the translation.
77 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
78 qemu_plugin_vcpu_simple_cb_t cb)
80 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
83 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
84 qemu_plugin_vcpu_simple_cb_t cb)
86 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
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)
94 if (!tb->mem_only) {
95 int index = flags == QEMU_PLUGIN_CB_R_REGS ||
96 flags == QEMU_PLUGIN_CB_RW_REGS ?
97 PLUGIN_CB_REGULAR_R : PLUGIN_CB_REGULAR;
99 plugin_register_dyn_cb__udata(&tb->cbs[index],
100 cb, flags, udata);
104 void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
105 struct qemu_plugin_tb *tb,
106 enum qemu_plugin_op op,
107 qemu_plugin_u64 entry,
108 uint64_t imm)
110 if (!tb->mem_only) {
111 plugin_register_inline_op_on_entry(
112 &tb->cbs[PLUGIN_CB_INLINE], 0, op, entry, imm);
116 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
117 qemu_plugin_vcpu_udata_cb_t cb,
118 enum qemu_plugin_cb_flags flags,
119 void *udata)
121 if (!insn->mem_only) {
122 int index = flags == QEMU_PLUGIN_CB_R_REGS ||
123 flags == QEMU_PLUGIN_CB_RW_REGS ?
124 PLUGIN_CB_REGULAR_R : PLUGIN_CB_REGULAR;
126 plugin_register_dyn_cb__udata(&insn->cbs[PLUGIN_CB_INSN][index],
127 cb, flags, udata);
131 void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
132 struct qemu_plugin_insn *insn,
133 enum qemu_plugin_op op,
134 qemu_plugin_u64 entry,
135 uint64_t imm)
137 if (!insn->mem_only) {
138 plugin_register_inline_op_on_entry(
139 &insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE], 0, op, entry, imm);
145 * We always plant memory instrumentation because they don't finalise until
146 * after the operation has complete.
148 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
149 qemu_plugin_vcpu_mem_cb_t cb,
150 enum qemu_plugin_cb_flags flags,
151 enum qemu_plugin_mem_rw rw,
152 void *udata)
154 plugin_register_vcpu_mem_cb(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR],
155 cb, flags, rw, udata);
158 void qemu_plugin_register_vcpu_mem_inline_per_vcpu(
159 struct qemu_plugin_insn *insn,
160 enum qemu_plugin_mem_rw rw,
161 enum qemu_plugin_op op,
162 qemu_plugin_u64 entry,
163 uint64_t imm)
165 plugin_register_inline_op_on_entry(
166 &insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE], rw, op, entry, imm);
169 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
170 qemu_plugin_vcpu_tb_trans_cb_t cb)
172 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
175 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
176 qemu_plugin_vcpu_syscall_cb_t cb)
178 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
181 void
182 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
183 qemu_plugin_vcpu_syscall_ret_cb_t cb)
185 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
189 * Plugin Queries
191 * These are queries that the plugin can make to gauge information
192 * from our opaque data types. We do not want to leak internal details
193 * here just information useful to the plugin.
197 * Translation block information:
199 * A plugin can query the virtual address of the start of the block
200 * and the number of instructions in it. It can also get access to
201 * each translated instruction.
204 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
206 return tb->n;
209 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
211 return tb->vaddr;
214 struct qemu_plugin_insn *
215 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
217 struct qemu_plugin_insn *insn;
218 if (unlikely(idx >= tb->n)) {
219 return NULL;
221 insn = g_ptr_array_index(tb->insns, idx);
222 insn->mem_only = tb->mem_only;
223 return insn;
227 * Instruction information
229 * These queries allow the plugin to retrieve information about each
230 * instruction being translated.
233 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn)
235 return insn->data->data;
238 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
240 return insn->data->len;
243 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
245 return insn->vaddr;
248 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
250 return insn->haddr;
253 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
255 CPUState *cpu = current_cpu;
256 return plugin_disas(cpu, insn->vaddr, insn->data->len);
259 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
261 const char *sym = lookup_symbol(insn->vaddr);
262 return sym[0] != 0 ? sym : NULL;
266 * The memory queries allow the plugin to query information about a
267 * memory access.
270 unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
272 MemOp op = get_memop(info);
273 return op & MO_SIZE;
276 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
278 MemOp op = get_memop(info);
279 return op & MO_SIGN;
282 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
284 MemOp op = get_memop(info);
285 return (op & MO_BSWAP) == MO_BE;
288 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
290 return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
294 * Virtual Memory queries
297 #ifdef CONFIG_SOFTMMU
298 static __thread struct qemu_plugin_hwaddr hwaddr_info;
299 #endif
301 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
302 uint64_t vaddr)
304 #ifdef CONFIG_SOFTMMU
305 CPUState *cpu = current_cpu;
306 unsigned int mmu_idx = get_mmuidx(info);
307 enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
308 hwaddr_info.is_store = (rw & QEMU_PLUGIN_MEM_W) != 0;
310 assert(mmu_idx < NB_MMU_MODES);
312 if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
313 hwaddr_info.is_store, &hwaddr_info)) {
314 error_report("invalid use of qemu_plugin_get_hwaddr");
315 return NULL;
318 return &hwaddr_info;
319 #else
320 return NULL;
321 #endif
324 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr)
326 #ifdef CONFIG_SOFTMMU
327 return haddr->is_io;
328 #else
329 return false;
330 #endif
333 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr)
335 #ifdef CONFIG_SOFTMMU
336 if (haddr) {
337 return haddr->phys_addr;
339 #endif
340 return 0;
343 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h)
345 #ifdef CONFIG_SOFTMMU
346 if (h && h->is_io) {
347 MemoryRegion *mr = h->mr;
348 if (!mr->name) {
349 unsigned maddr = (uintptr_t)mr;
350 g_autofree char *temp = g_strdup_printf("anon%08x", maddr);
351 return g_intern_string(temp);
352 } else {
353 return g_intern_string(mr->name);
355 } else {
356 return g_intern_static_string("RAM");
358 #else
359 return g_intern_static_string("Invalid");
360 #endif
363 int qemu_plugin_num_vcpus(void)
365 return plugin_num_vcpus();
369 * Plugin output
371 void qemu_plugin_outs(const char *string)
373 qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
376 bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
378 return name && value && qapi_bool_parse(name, value, ret, NULL);
382 * Binary path, start and end locations
384 const char *qemu_plugin_path_to_binary(void)
386 char *path = NULL;
387 #ifdef CONFIG_USER_ONLY
388 TaskState *ts = get_task_state(current_cpu);
389 path = g_strdup(ts->bprm->filename);
390 #endif
391 return path;
394 uint64_t qemu_plugin_start_code(void)
396 uint64_t start = 0;
397 #ifdef CONFIG_USER_ONLY
398 TaskState *ts = get_task_state(current_cpu);
399 start = ts->info->start_code;
400 #endif
401 return start;
404 uint64_t qemu_plugin_end_code(void)
406 uint64_t end = 0;
407 #ifdef CONFIG_USER_ONLY
408 TaskState *ts = get_task_state(current_cpu);
409 end = ts->info->end_code;
410 #endif
411 return end;
414 uint64_t qemu_plugin_entry_code(void)
416 uint64_t entry = 0;
417 #ifdef CONFIG_USER_ONLY
418 TaskState *ts = get_task_state(current_cpu);
419 entry = ts->info->entry;
420 #endif
421 return entry;
425 * Create register handles.
427 * We need to create a handle for each register so the plugin
428 * infrastructure can call gdbstub to read a register. They are
429 * currently just a pointer encapsulation of the gdb_reg but in
430 * future may hold internal plugin state so its important plugin
431 * authors are not tempted to treat them as numbers.
433 * We also construct a result array with those handles and some
434 * ancillary data the plugin might find useful.
437 static GArray *create_register_handles(GArray *gdbstub_regs)
439 GArray *find_data = g_array_new(true, true,
440 sizeof(qemu_plugin_reg_descriptor));
442 for (int i = 0; i < gdbstub_regs->len; i++) {
443 GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);
444 qemu_plugin_reg_descriptor desc;
446 /* skip "un-named" regs */
447 if (!grd->name) {
448 continue;
451 /* Create a record for the plugin */
452 desc.handle = GINT_TO_POINTER(grd->gdb_reg);
453 desc.name = g_intern_string(grd->name);
454 desc.feature = g_intern_string(grd->feature_name);
455 g_array_append_val(find_data, desc);
458 return find_data;
461 GArray *qemu_plugin_get_registers(void)
463 g_assert(current_cpu);
465 g_autoptr(GArray) regs = gdb_get_register_list(current_cpu);
466 return create_register_handles(regs);
469 int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
471 g_assert(current_cpu);
473 return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg));
476 struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size)
478 return plugin_scoreboard_new(element_size);
481 void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
483 plugin_scoreboard_free(score);
486 void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score,
487 unsigned int vcpu_index)
489 g_assert(vcpu_index < qemu_plugin_num_vcpus());
490 /* we can't use g_array_index since entry size is not statically known */
491 char *base_ptr = score->data->data;
492 return base_ptr + vcpu_index * g_array_get_element_size(score->data);
495 static uint64_t *plugin_u64_address(qemu_plugin_u64 entry,
496 unsigned int vcpu_index)
498 char *ptr = qemu_plugin_scoreboard_find(entry.score, vcpu_index);
499 return (uint64_t *)(ptr + entry.offset);
502 void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index,
503 uint64_t added)
505 *plugin_u64_address(entry, vcpu_index) += added;
508 uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry,
509 unsigned int vcpu_index)
511 return *plugin_u64_address(entry, vcpu_index);
514 void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
515 uint64_t val)
517 *plugin_u64_address(entry, vcpu_index) = val;
520 uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry)
522 uint64_t total = 0;
523 for (int i = 0, n = qemu_plugin_num_vcpus(); i < n; ++i) {
524 total += qemu_plugin_u64_get(entry, i);
526 return total;