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:
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
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
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"
43 #include "exec/exec-all.h"
44 #include "exec/gdbstub.h"
45 #include "exec/ram_addr.h"
46 #include "disas/disas.h"
48 #ifndef CONFIG_USER_ONLY
49 #include "qemu/plugin-memory.h"
50 #include "hw/boards.h"
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
,
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
],
104 void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb
*tb
,
105 enum qemu_plugin_op op
,
106 void *ptr
, uint64_t imm
)
109 plugin_register_inline_op(&tb
->cbs
[PLUGIN_CB_INLINE
], 0, op
, ptr
, imm
);
113 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn
*insn
,
114 qemu_plugin_vcpu_udata_cb_t cb
,
115 enum qemu_plugin_cb_flags flags
,
118 if (!insn
->mem_only
) {
119 int index
= flags
== QEMU_PLUGIN_CB_R_REGS
||
120 flags
== QEMU_PLUGIN_CB_RW_REGS
?
121 PLUGIN_CB_REGULAR_R
: PLUGIN_CB_REGULAR
;
123 plugin_register_dyn_cb__udata(&insn
->cbs
[PLUGIN_CB_INSN
][index
],
128 void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn
*insn
,
129 enum qemu_plugin_op op
,
130 void *ptr
, uint64_t imm
)
132 if (!insn
->mem_only
) {
133 plugin_register_inline_op(&insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_INLINE
],
140 * We always plant memory instrumentation because they don't finalise until
141 * after the operation has complete.
143 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn
*insn
,
144 qemu_plugin_vcpu_mem_cb_t cb
,
145 enum qemu_plugin_cb_flags flags
,
146 enum qemu_plugin_mem_rw rw
,
149 plugin_register_vcpu_mem_cb(&insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_REGULAR
],
150 cb
, flags
, rw
, udata
);
153 void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn
*insn
,
154 enum qemu_plugin_mem_rw rw
,
155 enum qemu_plugin_op op
, void *ptr
,
158 plugin_register_inline_op(&insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_INLINE
],
162 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id
,
163 qemu_plugin_vcpu_tb_trans_cb_t cb
)
165 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_TB_TRANS
, cb
);
168 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id
,
169 qemu_plugin_vcpu_syscall_cb_t cb
)
171 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_SYSCALL
, cb
);
175 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id
,
176 qemu_plugin_vcpu_syscall_ret_cb_t cb
)
178 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET
, cb
);
184 * These are queries that the plugin can make to gauge information
185 * from our opaque data types. We do not want to leak internal details
186 * here just information useful to the plugin.
190 * Translation block information:
192 * A plugin can query the virtual address of the start of the block
193 * and the number of instructions in it. It can also get access to
194 * each translated instruction.
197 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb
*tb
)
202 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb
*tb
)
207 struct qemu_plugin_insn
*
208 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb
*tb
, size_t idx
)
210 struct qemu_plugin_insn
*insn
;
211 if (unlikely(idx
>= tb
->n
)) {
214 insn
= g_ptr_array_index(tb
->insns
, idx
);
215 insn
->mem_only
= tb
->mem_only
;
220 * Instruction information
222 * These queries allow the plugin to retrieve information about each
223 * instruction being translated.
226 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn
*insn
)
228 return insn
->data
->data
;
231 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn
*insn
)
233 return insn
->data
->len
;
236 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn
*insn
)
241 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn
*insn
)
246 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn
*insn
)
248 CPUState
*cpu
= current_cpu
;
249 return plugin_disas(cpu
, insn
->vaddr
, insn
->data
->len
);
252 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn
*insn
)
254 const char *sym
= lookup_symbol(insn
->vaddr
);
255 return sym
[0] != 0 ? sym
: NULL
;
259 * The memory queries allow the plugin to query information about a
263 unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info
)
265 MemOp op
= get_memop(info
);
269 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info
)
271 MemOp op
= get_memop(info
);
275 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info
)
277 MemOp op
= get_memop(info
);
278 return (op
& MO_BSWAP
) == MO_BE
;
281 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info
)
283 return get_plugin_meminfo_rw(info
) & QEMU_PLUGIN_MEM_W
;
287 * Virtual Memory queries
290 #ifdef CONFIG_SOFTMMU
291 static __thread
struct qemu_plugin_hwaddr hwaddr_info
;
294 struct qemu_plugin_hwaddr
*qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info
,
297 #ifdef CONFIG_SOFTMMU
298 CPUState
*cpu
= current_cpu
;
299 unsigned int mmu_idx
= get_mmuidx(info
);
300 enum qemu_plugin_mem_rw rw
= get_plugin_meminfo_rw(info
);
301 hwaddr_info
.is_store
= (rw
& QEMU_PLUGIN_MEM_W
) != 0;
303 assert(mmu_idx
< NB_MMU_MODES
);
305 if (!tlb_plugin_lookup(cpu
, vaddr
, mmu_idx
,
306 hwaddr_info
.is_store
, &hwaddr_info
)) {
307 error_report("invalid use of qemu_plugin_get_hwaddr");
317 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr
*haddr
)
319 #ifdef CONFIG_SOFTMMU
326 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr
*haddr
)
328 #ifdef CONFIG_SOFTMMU
330 return haddr
->phys_addr
;
336 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr
*h
)
338 #ifdef CONFIG_SOFTMMU
340 MemoryRegion
*mr
= h
->mr
;
342 unsigned maddr
= (uintptr_t)mr
;
343 g_autofree
char *temp
= g_strdup_printf("anon%08x", maddr
);
344 return g_intern_string(temp
);
346 return g_intern_string(mr
->name
);
349 return g_intern_static_string("RAM");
352 return g_intern_static_string("Invalid");
356 int qemu_plugin_num_vcpus(void)
358 return plugin_num_vcpus();
364 void qemu_plugin_outs(const char *string
)
366 qemu_log_mask(CPU_LOG_PLUGIN
, "%s", string
);
369 bool qemu_plugin_bool_parse(const char *name
, const char *value
, bool *ret
)
371 return name
&& value
&& qapi_bool_parse(name
, value
, ret
, NULL
);
375 * Binary path, start and end locations
377 const char *qemu_plugin_path_to_binary(void)
380 #ifdef CONFIG_USER_ONLY
381 TaskState
*ts
= (TaskState
*) current_cpu
->opaque
;
382 path
= g_strdup(ts
->bprm
->filename
);
387 uint64_t qemu_plugin_start_code(void)
390 #ifdef CONFIG_USER_ONLY
391 TaskState
*ts
= (TaskState
*) current_cpu
->opaque
;
392 start
= ts
->info
->start_code
;
397 uint64_t qemu_plugin_end_code(void)
400 #ifdef CONFIG_USER_ONLY
401 TaskState
*ts
= (TaskState
*) current_cpu
->opaque
;
402 end
= ts
->info
->end_code
;
407 uint64_t qemu_plugin_entry_code(void)
410 #ifdef CONFIG_USER_ONLY
411 TaskState
*ts
= (TaskState
*) current_cpu
->opaque
;
412 entry
= ts
->info
->entry
;
418 * Create register handles.
420 * We need to create a handle for each register so the plugin
421 * infrastructure can call gdbstub to read a register. They are
422 * currently just a pointer encapsulation of the gdb_reg but in
423 * future may hold internal plugin state so its important plugin
424 * authors are not tempted to treat them as numbers.
426 * We also construct a result array with those handles and some
427 * ancillary data the plugin might find useful.
430 static GArray
*create_register_handles(GArray
*gdbstub_regs
)
432 GArray
*find_data
= g_array_new(true, true,
433 sizeof(qemu_plugin_reg_descriptor
));
435 for (int i
= 0; i
< gdbstub_regs
->len
; i
++) {
436 GDBRegDesc
*grd
= &g_array_index(gdbstub_regs
, GDBRegDesc
, i
);
437 qemu_plugin_reg_descriptor desc
;
439 /* skip "un-named" regs */
444 /* Create a record for the plugin */
445 desc
.handle
= GINT_TO_POINTER(grd
->gdb_reg
);
446 desc
.name
= g_intern_string(grd
->name
);
447 desc
.feature
= g_intern_string(grd
->feature_name
);
448 g_array_append_val(find_data
, desc
);
454 GArray
*qemu_plugin_get_registers(void)
456 g_assert(current_cpu
);
458 g_autoptr(GArray
) regs
= gdb_get_register_list(current_cpu
);
459 return create_register_handles(regs
);
462 int qemu_plugin_read_register(struct qemu_plugin_register
*reg
, GByteArray
*buf
)
464 g_assert(current_cpu
);
466 return gdb_read_register(current_cpu
, buf
, GPOINTER_TO_INT(reg
));