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:
12 * Which can then be passed back into the API to do additional things.
13 * As such all the public functions in here are exported in
16 * The general life-cycle of a plugin is:
18 * - plugin is loaded, public qemu_plugin_install called
19 * - the install func registers callbacks for events
20 * - usually an atexit_cb is registered to dump info at the end
21 * - when a registered event occurs the plugin is called
22 * - some events pass additional info
23 * - during translation the plugin can decide to instrument any
25 * - when QEMU exits all the registered atexit callbacks are called
27 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
28 * Copyright (C) 2019, Linaro
30 * License: GNU GPL, version 2 or later.
31 * See the COPYING file in the top-level directory.
33 * SPDX-License-Identifier: GPL-2.0-or-later
37 #include "qemu/osdep.h"
38 #include "qemu/plugin.h"
40 #include "exec/exec-all.h"
41 #include "exec/ram_addr.h"
42 #include "disas/disas.h"
44 #ifndef CONFIG_USER_ONLY
45 #include "qemu/plugin-memory.h"
46 #include "hw/boards.h"
48 #include "trace/mem.h"
50 /* Uninstall and Reset handlers */
52 void qemu_plugin_uninstall(qemu_plugin_id_t id
, qemu_plugin_simple_cb_t cb
)
54 plugin_reset_uninstall(id
, cb
, false);
57 void qemu_plugin_reset(qemu_plugin_id_t id
, qemu_plugin_simple_cb_t cb
)
59 plugin_reset_uninstall(id
, cb
, true);
63 * Plugin Register Functions
65 * This allows the plugin to register callbacks for various events
66 * during the translation.
69 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id
,
70 qemu_plugin_vcpu_simple_cb_t cb
)
72 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_INIT
, cb
);
75 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id
,
76 qemu_plugin_vcpu_simple_cb_t cb
)
78 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_EXIT
, cb
);
81 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb
*tb
,
82 qemu_plugin_vcpu_udata_cb_t cb
,
83 enum qemu_plugin_cb_flags flags
,
87 plugin_register_dyn_cb__udata(&tb
->cbs
[PLUGIN_CB_REGULAR
],
92 void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb
*tb
,
93 enum qemu_plugin_op op
,
94 void *ptr
, uint64_t imm
)
97 plugin_register_inline_op(&tb
->cbs
[PLUGIN_CB_INLINE
], 0, op
, ptr
, imm
);
101 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn
*insn
,
102 qemu_plugin_vcpu_udata_cb_t cb
,
103 enum qemu_plugin_cb_flags flags
,
106 if (!insn
->mem_only
) {
107 plugin_register_dyn_cb__udata(&insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_REGULAR
],
112 void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn
*insn
,
113 enum qemu_plugin_op op
,
114 void *ptr
, uint64_t imm
)
116 if (!insn
->mem_only
) {
117 plugin_register_inline_op(&insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_INLINE
],
124 * We always plant memory instrumentation because they don't finalise until
125 * after the operation has complete.
127 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn
*insn
,
128 qemu_plugin_vcpu_mem_cb_t cb
,
129 enum qemu_plugin_cb_flags flags
,
130 enum qemu_plugin_mem_rw rw
,
133 plugin_register_vcpu_mem_cb(&insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_REGULAR
],
134 cb
, flags
, rw
, udata
);
137 void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn
*insn
,
138 enum qemu_plugin_mem_rw rw
,
139 enum qemu_plugin_op op
, void *ptr
,
142 plugin_register_inline_op(&insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_INLINE
],
146 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id
,
147 qemu_plugin_vcpu_tb_trans_cb_t cb
)
149 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_TB_TRANS
, cb
);
152 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id
,
153 qemu_plugin_vcpu_syscall_cb_t cb
)
155 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_SYSCALL
, cb
);
159 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id
,
160 qemu_plugin_vcpu_syscall_ret_cb_t cb
)
162 plugin_register_cb(id
, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET
, cb
);
168 * These are queries that the plugin can make to gauge information
169 * from our opaque data types. We do not want to leak internal details
170 * here just information useful to the plugin.
174 * Translation block information:
176 * A plugin can query the virtual address of the start of the block
177 * and the number of instructions in it. It can also get access to
178 * each translated instruction.
181 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb
*tb
)
186 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb
*tb
)
191 struct qemu_plugin_insn
*
192 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb
*tb
, size_t idx
)
194 struct qemu_plugin_insn
*insn
;
195 if (unlikely(idx
>= tb
->n
)) {
198 insn
= g_ptr_array_index(tb
->insns
, idx
);
199 insn
->mem_only
= tb
->mem_only
;
204 * Instruction information
206 * These queries allow the plugin to retrieve information about each
207 * instruction being translated.
210 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn
*insn
)
212 return insn
->data
->data
;
215 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn
*insn
)
217 return insn
->data
->len
;
220 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn
*insn
)
225 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn
*insn
)
230 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn
*insn
)
232 CPUState
*cpu
= current_cpu
;
233 return plugin_disas(cpu
, insn
->vaddr
, insn
->data
->len
);
236 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn
*insn
)
238 const char *sym
= lookup_symbol(insn
->vaddr
);
239 return sym
[0] != 0 ? sym
: NULL
;
243 * The memory queries allow the plugin to query information about a
247 unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info
)
249 return info
& TRACE_MEM_SZ_SHIFT_MASK
;
252 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info
)
254 return !!(info
& TRACE_MEM_SE
);
257 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info
)
259 return !!(info
& TRACE_MEM_BE
);
262 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info
)
264 return !!(info
& TRACE_MEM_ST
);
268 * Virtual Memory queries
271 #ifdef CONFIG_SOFTMMU
272 static __thread
struct qemu_plugin_hwaddr hwaddr_info
;
275 struct qemu_plugin_hwaddr
*qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info
,
278 #ifdef CONFIG_SOFTMMU
279 CPUState
*cpu
= current_cpu
;
280 unsigned int mmu_idx
= info
>> TRACE_MEM_MMU_SHIFT
;
281 hwaddr_info
.is_store
= info
& TRACE_MEM_ST
;
283 if (!tlb_plugin_lookup(cpu
, vaddr
, mmu_idx
,
284 info
& TRACE_MEM_ST
, &hwaddr_info
)) {
285 error_report("invalid use of qemu_plugin_get_hwaddr");
295 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr
*haddr
)
297 #ifdef CONFIG_SOFTMMU
304 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr
*haddr
)
306 #ifdef CONFIG_SOFTMMU
311 void *hostaddr
= haddr
->v
.ram
.hostaddr
;
313 block
= qemu_ram_block_from_host(hostaddr
, false, &offset
);
315 error_report("Bad host ram pointer %p", haddr
->v
.ram
.hostaddr
);
319 return block
->offset
+ offset
+ block
->mr
->addr
;
321 MemoryRegionSection
*mrs
= haddr
->v
.io
.section
;
322 return mrs
->offset_within_address_space
+ haddr
->v
.io
.offset
;
329 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr
*h
)
331 #ifdef CONFIG_SOFTMMU
333 MemoryRegionSection
*mrs
= h
->v
.io
.section
;
334 if (!mrs
->mr
->name
) {
335 unsigned long maddr
= 0xffffffff & (uintptr_t) mrs
->mr
;
336 g_autofree
char *temp
= g_strdup_printf("anon%08lx", maddr
);
337 return g_intern_string(temp
);
339 return g_intern_string(mrs
->mr
->name
);
342 return g_intern_static_string("RAM");
345 return g_intern_static_string("Invalid");
350 * Queries to the number and potential maximum number of vCPUs there
351 * will be. This helps the plugin dimension per-vcpu arrays.
354 #ifndef CONFIG_USER_ONLY
355 static MachineState
* get_ms(void)
357 return MACHINE(qdev_get_machine());
361 int qemu_plugin_n_vcpus(void)
363 #ifdef CONFIG_USER_ONLY
366 return get_ms()->smp
.cpus
;
370 int qemu_plugin_n_max_vcpus(void)
372 #ifdef CONFIG_USER_ONLY
375 return get_ms()->smp
.max_cpus
;
382 void qemu_plugin_outs(const char *string
)
384 qemu_log_mask(CPU_LOG_PLUGIN
, "%s", string
);