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"
42 #include "system/memory.h"
44 #include "exec/gdbstub.h"
45 #include "exec/target_page.h"
46 #include "exec/translation-block.h"
47 #include "exec/translator.h"
48 #include "disas/disas.h"
51 /* Uninstall and Reset handlers */
53 void qemu_plugin_uninstall(qemu_plugin_id_t id
, qemu_plugin_simple_cb_t cb
)
55 plugin_reset_uninstall(id
, cb
, false);
58 void qemu_plugin_reset(qemu_plugin_id_t id
, qemu_plugin_simple_cb_t cb
)
60 plugin_reset_uninstall(id
, cb
, true);
64 * Plugin Register Functions
66 * This allows the plugin to register callbacks for various events
67 * during the translation.
70 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id
,
71 qemu_plugin_vcpu_simple_cb_t cb
)
73 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_INIT
, cb
);
76 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id
,
77 qemu_plugin_vcpu_simple_cb_t cb
)
79 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_EXIT
, cb
);
82 static bool tb_is_mem_only(void)
84 return tb_cflags(tcg_ctx
->gen_tb
) & CF_MEMI_ONLY
;
87 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb
*tb
,
88 qemu_plugin_vcpu_udata_cb_t cb
,
89 enum qemu_plugin_cb_flags flags
,
92 if (!tb_is_mem_only()) {
93 plugin_register_dyn_cb__udata(&tb
->cbs
, cb
, flags
, udata
);
97 void qemu_plugin_register_vcpu_tb_exec_cond_cb(struct qemu_plugin_tb
*tb
,
98 qemu_plugin_vcpu_udata_cb_t cb
,
99 enum qemu_plugin_cb_flags flags
,
100 enum qemu_plugin_cond cond
,
101 qemu_plugin_u64 entry
,
105 if (cond
== QEMU_PLUGIN_COND_NEVER
|| tb_is_mem_only()) {
108 if (cond
== QEMU_PLUGIN_COND_ALWAYS
) {
109 qemu_plugin_register_vcpu_tb_exec_cb(tb
, cb
, flags
, udata
);
112 plugin_register_dyn_cond_cb__udata(&tb
->cbs
, cb
, flags
,
113 cond
, entry
, imm
, udata
);
116 void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
117 struct qemu_plugin_tb
*tb
,
118 enum qemu_plugin_op op
,
119 qemu_plugin_u64 entry
,
122 if (!tb_is_mem_only()) {
123 plugin_register_inline_op_on_entry(&tb
->cbs
, 0, op
, entry
, imm
);
127 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn
*insn
,
128 qemu_plugin_vcpu_udata_cb_t cb
,
129 enum qemu_plugin_cb_flags flags
,
132 if (!tb_is_mem_only()) {
133 plugin_register_dyn_cb__udata(&insn
->insn_cbs
, cb
, flags
, udata
);
137 void qemu_plugin_register_vcpu_insn_exec_cond_cb(
138 struct qemu_plugin_insn
*insn
,
139 qemu_plugin_vcpu_udata_cb_t cb
,
140 enum qemu_plugin_cb_flags flags
,
141 enum qemu_plugin_cond cond
,
142 qemu_plugin_u64 entry
,
146 if (cond
== QEMU_PLUGIN_COND_NEVER
|| tb_is_mem_only()) {
149 if (cond
== QEMU_PLUGIN_COND_ALWAYS
) {
150 qemu_plugin_register_vcpu_insn_exec_cb(insn
, cb
, flags
, udata
);
153 plugin_register_dyn_cond_cb__udata(&insn
->insn_cbs
, cb
, flags
,
154 cond
, entry
, imm
, udata
);
157 void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
158 struct qemu_plugin_insn
*insn
,
159 enum qemu_plugin_op op
,
160 qemu_plugin_u64 entry
,
163 if (!tb_is_mem_only()) {
164 plugin_register_inline_op_on_entry(&insn
->insn_cbs
, 0, op
, entry
, imm
);
170 * We always plant memory instrumentation because they don't finalise until
171 * after the operation has complete.
173 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn
*insn
,
174 qemu_plugin_vcpu_mem_cb_t cb
,
175 enum qemu_plugin_cb_flags flags
,
176 enum qemu_plugin_mem_rw rw
,
179 plugin_register_vcpu_mem_cb(&insn
->mem_cbs
, cb
, flags
, rw
, udata
);
182 void qemu_plugin_register_vcpu_mem_inline_per_vcpu(
183 struct qemu_plugin_insn
*insn
,
184 enum qemu_plugin_mem_rw rw
,
185 enum qemu_plugin_op op
,
186 qemu_plugin_u64 entry
,
189 plugin_register_inline_op_on_entry(&insn
->mem_cbs
, rw
, op
, entry
, imm
);
192 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id
,
193 qemu_plugin_vcpu_tb_trans_cb_t cb
)
195 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_TB_TRANS
, cb
);
198 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id
,
199 qemu_plugin_vcpu_syscall_cb_t cb
)
201 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_SYSCALL
, cb
);
205 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id
,
206 qemu_plugin_vcpu_syscall_ret_cb_t cb
)
208 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET
, cb
);
214 * These are queries that the plugin can make to gauge information
215 * from our opaque data types. We do not want to leak internal details
216 * here just information useful to the plugin.
220 * Translation block information:
222 * A plugin can query the virtual address of the start of the block
223 * and the number of instructions in it. It can also get access to
224 * each translated instruction.
227 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb
*tb
)
232 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb
*tb
)
234 const DisasContextBase
*db
= tcg_ctx
->plugin_db
;
238 struct qemu_plugin_insn
*
239 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb
*tb
, size_t idx
)
241 if (unlikely(idx
>= tb
->n
)) {
244 return g_ptr_array_index(tb
->insns
, idx
);
248 * Instruction information
250 * These queries allow the plugin to retrieve information about each
251 * instruction being translated.
254 size_t qemu_plugin_insn_data(const struct qemu_plugin_insn
*insn
,
255 void *dest
, size_t len
)
257 const DisasContextBase
*db
= tcg_ctx
->plugin_db
;
259 len
= MIN(len
, insn
->len
);
260 return translator_st(db
, dest
, insn
->vaddr
, len
) ? len
: 0;
263 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn
*insn
)
268 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn
*insn
)
273 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn
*insn
)
275 const DisasContextBase
*db
= tcg_ctx
->plugin_db
;
276 vaddr page0_last
= db
->pc_first
| ~qemu_target_page_mask();
283 * ??? The return value is not intended for use of host memory,
284 * but as a proxy for address space and physical address.
285 * Thus we are only interested in the first byte and do not
286 * care about spanning pages.
288 if (insn
->vaddr
<= page0_last
) {
289 if (db
->host_addr
[0] == NULL
) {
292 return db
->host_addr
[0] + insn
->vaddr
- db
->pc_first
;
294 if (db
->host_addr
[1] == NULL
) {
297 return db
->host_addr
[1] + insn
->vaddr
- (page0_last
+ 1);
301 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn
*insn
)
303 return plugin_disas(tcg_ctx
->cpu
, tcg_ctx
->plugin_db
,
304 insn
->vaddr
, insn
->len
);
307 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn
*insn
)
309 const char *sym
= lookup_symbol(insn
->vaddr
);
310 return sym
[0] != 0 ? sym
: NULL
;
314 * The memory queries allow the plugin to query information about a
318 unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info
)
320 MemOp op
= get_memop(info
);
324 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info
)
326 MemOp op
= get_memop(info
);
330 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info
)
332 MemOp op
= get_memop(info
);
333 return (op
& MO_BSWAP
) == MO_BE
;
336 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info
)
338 return get_plugin_meminfo_rw(info
) & QEMU_PLUGIN_MEM_W
;
341 qemu_plugin_mem_value
qemu_plugin_mem_get_value(qemu_plugin_meminfo_t info
)
343 uint64_t low
= current_cpu
->neg
.plugin_mem_value_low
;
344 qemu_plugin_mem_value value
;
346 switch (qemu_plugin_mem_size_shift(info
)) {
348 value
.type
= QEMU_PLUGIN_MEM_VALUE_U8
;
349 value
.data
.u8
= (uint8_t)low
;
352 value
.type
= QEMU_PLUGIN_MEM_VALUE_U16
;
353 value
.data
.u16
= (uint16_t)low
;
356 value
.type
= QEMU_PLUGIN_MEM_VALUE_U32
;
357 value
.data
.u32
= (uint32_t)low
;
360 value
.type
= QEMU_PLUGIN_MEM_VALUE_U64
;
361 value
.data
.u64
= low
;
364 value
.type
= QEMU_PLUGIN_MEM_VALUE_U128
;
365 value
.data
.u128
.low
= low
;
366 value
.data
.u128
.high
= current_cpu
->neg
.plugin_mem_value_high
;
369 g_assert_not_reached();
374 int qemu_plugin_num_vcpus(void)
376 return plugin_num_vcpus();
382 void qemu_plugin_outs(const char *string
)
384 qemu_log_mask(CPU_LOG_PLUGIN
, "%s", string
);
387 bool qemu_plugin_bool_parse(const char *name
, const char *value
, bool *ret
)
389 return name
&& value
&& qapi_bool_parse(name
, value
, ret
, NULL
);
393 * Create register handles.
395 * We need to create a handle for each register so the plugin
396 * infrastructure can call gdbstub to read a register. They are
397 * currently just a pointer encapsulation of the gdb_reg but in
398 * future may hold internal plugin state so its important plugin
399 * authors are not tempted to treat them as numbers.
401 * We also construct a result array with those handles and some
402 * ancillary data the plugin might find useful.
405 static GArray
*create_register_handles(GArray
*gdbstub_regs
)
407 GArray
*find_data
= g_array_new(true, true,
408 sizeof(qemu_plugin_reg_descriptor
));
410 for (int i
= 0; i
< gdbstub_regs
->len
; i
++) {
411 GDBRegDesc
*grd
= &g_array_index(gdbstub_regs
, GDBRegDesc
, i
);
412 qemu_plugin_reg_descriptor desc
;
414 /* skip "un-named" regs */
419 /* Create a record for the plugin */
420 desc
.handle
= GINT_TO_POINTER(grd
->gdb_reg
+ 1);
421 desc
.name
= g_intern_string(grd
->name
);
422 desc
.feature
= g_intern_string(grd
->feature_name
);
423 g_array_append_val(find_data
, desc
);
429 GArray
*qemu_plugin_get_registers(void)
431 g_assert(current_cpu
);
433 g_autoptr(GArray
) regs
= gdb_get_register_list(current_cpu
);
434 return create_register_handles(regs
);
437 int qemu_plugin_read_register(struct qemu_plugin_register
*reg
, GByteArray
*buf
)
439 g_assert(current_cpu
);
441 if (qemu_plugin_get_cb_flags() == QEMU_PLUGIN_CB_NO_REGS
) {
445 return gdb_read_register(current_cpu
, buf
, GPOINTER_TO_INT(reg
) - 1);
448 int qemu_plugin_write_register(struct qemu_plugin_register
*reg
,
451 g_assert(current_cpu
);
453 if (buf
->len
== 0 || qemu_plugin_get_cb_flags() != QEMU_PLUGIN_CB_RW_REGS
) {
457 return gdb_write_register(current_cpu
, buf
->data
, GPOINTER_TO_INT(reg
) - 1);
460 bool qemu_plugin_read_memory_vaddr(uint64_t addr
, GByteArray
*data
, size_t len
)
462 g_assert(current_cpu
);
468 g_byte_array_set_size(data
, len
);
470 int result
= cpu_memory_rw_debug(current_cpu
, addr
, data
->data
,
480 bool qemu_plugin_write_memory_vaddr(uint64_t addr
, GByteArray
*data
)
482 g_assert(current_cpu
);
484 if (data
->len
== 0) {
488 int result
= cpu_memory_rw_debug(current_cpu
, addr
, data
->data
,
498 enum qemu_plugin_hwaddr_operation_result
499 qemu_plugin_read_memory_hwaddr(hwaddr addr
, GByteArray
*data
, size_t len
)
501 #ifdef CONFIG_SOFTMMU
503 return QEMU_PLUGIN_HWADDR_OPERATION_ERROR
;
506 g_assert(current_cpu
);
509 int as_idx
= cpu_asidx_from_attrs(current_cpu
, MEMTXATTRS_UNSPECIFIED
);
510 AddressSpace
*as
= cpu_get_address_space(current_cpu
, as_idx
);
513 return QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS_SPACE
;
516 g_byte_array_set_size(data
, len
);
517 MemTxResult res
= address_space_rw(as
, addr
,
518 MEMTXATTRS_UNSPECIFIED
, data
->data
,
523 return QEMU_PLUGIN_HWADDR_OPERATION_OK
;
525 return QEMU_PLUGIN_HWADDR_OPERATION_DEVICE_ERROR
;
526 case MEMTX_DECODE_ERROR
:
527 return QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS
;
528 case MEMTX_ACCESS_ERROR
:
529 return QEMU_PLUGIN_HWADDR_OPERATION_ACCESS_DENIED
;
531 return QEMU_PLUGIN_HWADDR_OPERATION_ERROR
;
534 return QEMU_PLUGIN_HWADDR_OPERATION_ERROR
;
538 enum qemu_plugin_hwaddr_operation_result
539 qemu_plugin_write_memory_hwaddr(hwaddr addr
, GByteArray
*data
)
541 #ifdef CONFIG_SOFTMMU
542 if (data
->len
== 0) {
543 return QEMU_PLUGIN_HWADDR_OPERATION_ERROR
;
546 g_assert(current_cpu
);
548 int as_idx
= cpu_asidx_from_attrs(current_cpu
, MEMTXATTRS_UNSPECIFIED
);
549 AddressSpace
*as
= cpu_get_address_space(current_cpu
, as_idx
);
552 return QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS_SPACE
;
555 MemTxResult res
= address_space_rw(as
, addr
,
556 MEMTXATTRS_UNSPECIFIED
, data
->data
,
560 return QEMU_PLUGIN_HWADDR_OPERATION_OK
;
562 return QEMU_PLUGIN_HWADDR_OPERATION_DEVICE_ERROR
;
563 case MEMTX_DECODE_ERROR
:
564 return QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS
;
565 case MEMTX_ACCESS_ERROR
:
566 return QEMU_PLUGIN_HWADDR_OPERATION_ACCESS_DENIED
;
568 return QEMU_PLUGIN_HWADDR_OPERATION_ERROR
;
571 return QEMU_PLUGIN_HWADDR_OPERATION_ERROR
;
575 bool qemu_plugin_translate_vaddr(uint64_t vaddr
, uint64_t *hwaddr
)
577 #ifdef CONFIG_SOFTMMU
578 g_assert(current_cpu
);
580 uint64_t res
= cpu_get_phys_page_debug(current_cpu
, vaddr
);
582 if (res
== (uint64_t)-1) {
586 *hwaddr
= res
| (vaddr
& ~TARGET_PAGE_MASK
);
594 struct qemu_plugin_scoreboard
*qemu_plugin_scoreboard_new(size_t element_size
)
596 return plugin_scoreboard_new(element_size
);
599 void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard
*score
)
601 plugin_scoreboard_free(score
);
604 void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard
*score
,
605 unsigned int vcpu_index
)
607 g_assert(vcpu_index
< qemu_plugin_num_vcpus());
608 /* we can't use g_array_index since entry size is not statically known */
609 char *base_ptr
= score
->data
->data
;
610 return base_ptr
+ vcpu_index
* g_array_get_element_size(score
->data
);
613 static uint64_t *plugin_u64_address(qemu_plugin_u64 entry
,
614 unsigned int vcpu_index
)
616 char *ptr
= qemu_plugin_scoreboard_find(entry
.score
, vcpu_index
);
617 return (uint64_t *)(ptr
+ entry
.offset
);
620 void qemu_plugin_u64_add(qemu_plugin_u64 entry
, unsigned int vcpu_index
,
623 *plugin_u64_address(entry
, vcpu_index
) += added
;
626 uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry
,
627 unsigned int vcpu_index
)
629 return *plugin_u64_address(entry
, vcpu_index
);
632 void qemu_plugin_u64_set(qemu_plugin_u64 entry
, unsigned int vcpu_index
,
635 *plugin_u64_address(entry
, vcpu_index
) = val
;
638 uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry
)
641 for (int i
= 0, n
= qemu_plugin_num_vcpus(); i
< n
; ++i
) {
642 total
+= qemu_plugin_u64_get(entry
, i
);