blob: 817c9b6b69c9aa80ee38855ea5a5143ba81f1f31 [file] [log] [blame]
Alex Bennée5c5d69b2019-06-13 14:58:58 +01001/*
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 *
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
14 * qemu-plugin.h.
15 *
16 * The general life-cycle of a plugin is:
17 *
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
24 * instruction
25 * - when QEMU exits all the registered atexit callbacks are called
26 *
27 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
28 * Copyright (C) 2019, Linaro
29 *
30 * License: GNU GPL, version 2 or later.
31 * See the COPYING file in the top-level directory.
32 *
33 * SPDX-License-Identifier: GPL-2.0-or-later
34 *
35 */
36
37#include "qemu/osdep.h"
38#include "qemu/plugin.h"
Alex Bennée5c5d69b2019-06-13 14:58:58 +010039#include "tcg/tcg.h"
Alex Bennéecbafa232019-05-22 10:27:14 +010040#include "exec/exec-all.h"
Aaron Lindsay787148b2021-03-12 17:28:09 +000041#include "exec/ram_addr.h"
Alex Bennéecbafa232019-05-22 10:27:14 +010042#include "disas/disas.h"
Alex Bennée5c5d69b2019-06-13 14:58:58 +010043#include "plugin.h"
44#ifndef CONFIG_USER_ONLY
Alex Bennée235537f2019-06-19 20:20:08 +010045#include "qemu/plugin-memory.h"
Alex Bennée5c5d69b2019-06-13 14:58:58 +010046#include "hw/boards.h"
47#endif
Richard Henderson208b2d22019-12-16 15:28:24 -100048#include "trace/mem.h"
Alex Bennée5c5d69b2019-06-13 14:58:58 +010049
50/* Uninstall and Reset handlers */
51
52void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
53{
54 plugin_reset_uninstall(id, cb, false);
55}
56
57void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
58{
59 plugin_reset_uninstall(id, cb, true);
60}
61
62/*
63 * Plugin Register Functions
64 *
65 * This allows the plugin to register callbacks for various events
66 * during the translation.
67 */
68
69void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
70 qemu_plugin_vcpu_simple_cb_t cb)
71{
72 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
73}
74
75void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
76 qemu_plugin_vcpu_simple_cb_t cb)
77{
78 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
79}
80
81void 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,
84 void *udata)
85{
Alex Bennéecfd405e2021-02-13 13:03:22 +000086 if (!tb->mem_only) {
87 plugin_register_dyn_cb__udata(&tb->cbs[PLUGIN_CB_REGULAR],
88 cb, flags, udata);
89 }
Alex Bennée5c5d69b2019-06-13 14:58:58 +010090}
91
92void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb,
93 enum qemu_plugin_op op,
94 void *ptr, uint64_t imm)
95{
Alex Bennéecfd405e2021-02-13 13:03:22 +000096 if (!tb->mem_only) {
97 plugin_register_inline_op(&tb->cbs[PLUGIN_CB_INLINE], 0, op, ptr, imm);
98 }
Alex Bennée5c5d69b2019-06-13 14:58:58 +010099}
100
101void 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,
104 void *udata)
105{
Alex Bennéecfd405e2021-02-13 13:03:22 +0000106 if (!insn->mem_only) {
107 plugin_register_dyn_cb__udata(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR],
108 cb, flags, udata);
109 }
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100110}
111
112void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn,
113 enum qemu_plugin_op op,
114 void *ptr, uint64_t imm)
115{
Alex Bennéecfd405e2021-02-13 13:03:22 +0000116 if (!insn->mem_only) {
117 plugin_register_inline_op(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE],
118 0, op, ptr, imm);
119 }
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100120}
121
122
Alex Bennéecfd405e2021-02-13 13:03:22 +0000123/*
124 * We always plant memory instrumentation because they don't finalise until
125 * after the operation has complete.
126 */
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100127void 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,
131 void *udata)
132{
133 plugin_register_vcpu_mem_cb(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR],
Alex Bennéecfd405e2021-02-13 13:03:22 +0000134 cb, flags, rw, udata);
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100135}
136
137void 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,
140 uint64_t imm)
141{
142 plugin_register_inline_op(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE],
Alex Bennéecfd405e2021-02-13 13:03:22 +0000143 rw, op, ptr, imm);
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100144}
145
146void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
147 qemu_plugin_vcpu_tb_trans_cb_t cb)
148{
149 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
150}
151
152void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
153 qemu_plugin_vcpu_syscall_cb_t cb)
154{
155 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
156}
157
158void
159qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
160 qemu_plugin_vcpu_syscall_ret_cb_t cb)
161{
162 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
163}
164
165/*
166 * Plugin Queries
167 *
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.
171 */
172
173/*
174 * Translation block information:
175 *
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.
179 */
180
181size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
182{
183 return tb->n;
184}
185
186uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
187{
188 return tb->vaddr;
189}
190
191struct qemu_plugin_insn *
192qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
193{
Alex Bennéecfd405e2021-02-13 13:03:22 +0000194 struct qemu_plugin_insn *insn;
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100195 if (unlikely(idx >= tb->n)) {
196 return NULL;
197 }
Alex Bennéecfd405e2021-02-13 13:03:22 +0000198 insn = g_ptr_array_index(tb->insns, idx);
199 insn->mem_only = tb->mem_only;
200 return insn;
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100201}
202
203/*
204 * Instruction information
205 *
206 * These queries allow the plugin to retrieve information about each
207 * instruction being translated.
208 */
209
210const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn)
211{
212 return insn->data->data;
213}
214
215size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
216{
217 return insn->data->len;
218}
219
220uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
221{
222 return insn->vaddr;
223}
224
225void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
226{
227 return insn->haddr;
228}
229
Alex Bennéecbafa232019-05-22 10:27:14 +0100230char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
231{
232 CPUState *cpu = current_cpu;
233 return plugin_disas(cpu, insn->vaddr, insn->data->len);
234}
235
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100236/*
237 * The memory queries allow the plugin to query information about a
238 * memory access.
239 */
240
241unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
242{
243 return info & TRACE_MEM_SZ_SHIFT_MASK;
244}
245
246bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
247{
248 return !!(info & TRACE_MEM_SE);
249}
250
251bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
252{
253 return !!(info & TRACE_MEM_BE);
254}
255
256bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
257{
258 return !!(info & TRACE_MEM_ST);
259}
260
261/*
262 * Virtual Memory queries
263 */
264
Alex Bennée235537f2019-06-19 20:20:08 +0100265#ifdef CONFIG_SOFTMMU
266static __thread struct qemu_plugin_hwaddr hwaddr_info;
Yonggang Luoa2b88162021-03-12 17:28:20 +0000267#endif
Alex Bennée235537f2019-06-19 20:20:08 +0100268
269struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
270 uint64_t vaddr)
271{
Yonggang Luoa2b88162021-03-12 17:28:20 +0000272#ifdef CONFIG_SOFTMMU
Alex Bennée235537f2019-06-19 20:20:08 +0100273 CPUState *cpu = current_cpu;
274 unsigned int mmu_idx = info >> TRACE_MEM_MMU_SHIFT;
275 hwaddr_info.is_store = info & TRACE_MEM_ST;
276
277 if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
278 info & TRACE_MEM_ST, &hwaddr_info)) {
279 error_report("invalid use of qemu_plugin_get_hwaddr");
280 return NULL;
281 }
282
283 return &hwaddr_info;
Alex Bennée235537f2019-06-19 20:20:08 +0100284#else
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100285 return NULL;
Alex Bennée235537f2019-06-19 20:20:08 +0100286#endif
Yonggang Luoa2b88162021-03-12 17:28:20 +0000287}
Alex Bennée235537f2019-06-19 20:20:08 +0100288
Philippe Mathieu-Daudé308e7542020-05-13 18:31:55 +0100289bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr)
Alex Bennée235537f2019-06-19 20:20:08 +0100290{
291#ifdef CONFIG_SOFTMMU
Philippe Mathieu-Daudé308e7542020-05-13 18:31:55 +0100292 return haddr->is_io;
Alex Bennée235537f2019-06-19 20:20:08 +0100293#else
294 return false;
295#endif
296}
297
Aaron Lindsay787148b2021-03-12 17:28:09 +0000298uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr)
Alex Bennée235537f2019-06-19 20:20:08 +0100299{
300#ifdef CONFIG_SOFTMMU
301 if (haddr) {
302 if (!haddr->is_io) {
Aaron Lindsay787148b2021-03-12 17:28:09 +0000303 RAMBlock *block;
304 ram_addr_t offset;
305 void *hostaddr = (void *) haddr->v.ram.hostaddr;
306
307 block = qemu_ram_block_from_host(hostaddr, false, &offset);
308 if (!block) {
Alex Bennée235537f2019-06-19 20:20:08 +0100309 error_report("Bad ram pointer %"PRIx64"", haddr->v.ram.hostaddr);
310 abort();
311 }
Aaron Lindsay787148b2021-03-12 17:28:09 +0000312
313 return block->offset + offset + block->mr->addr;
Alex Bennée235537f2019-06-19 20:20:08 +0100314 } else {
Aaron Lindsay787148b2021-03-12 17:28:09 +0000315 MemoryRegionSection *mrs = haddr->v.io.section;
316 return haddr->v.io.offset + mrs->mr->addr;
Alex Bennée235537f2019-06-19 20:20:08 +0100317 }
318 }
319#endif
320 return 0;
321}
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100322
Alex Bennéeb853a792021-02-13 13:03:04 +0000323const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h)
324{
325#ifdef CONFIG_SOFTMMU
326 if (h && h->is_io) {
327 MemoryRegionSection *mrs = h->v.io.section;
328 if (!mrs->mr->name) {
329 unsigned long maddr = 0xffffffff & (uintptr_t) mrs->mr;
330 g_autofree char *temp = g_strdup_printf("anon%08lx", maddr);
331 return g_intern_string(temp);
332 } else {
333 return g_intern_string(mrs->mr->name);
334 }
335 } else {
336 return g_intern_static_string("RAM");
337 }
338#else
339 return g_intern_static_string("Invalid");
340#endif
341}
342
Alex Bennée5c5d69b2019-06-13 14:58:58 +0100343/*
344 * Queries to the number and potential maximum number of vCPUs there
345 * will be. This helps the plugin dimension per-vcpu arrays.
346 */
347
348#ifndef CONFIG_USER_ONLY
349static MachineState * get_ms(void)
350{
351 return MACHINE(qdev_get_machine());
352}
353#endif
354
355int qemu_plugin_n_vcpus(void)
356{
357#ifdef CONFIG_USER_ONLY
358 return -1;
359#else
360 return get_ms()->smp.cpus;
361#endif
362}
363
364int qemu_plugin_n_max_vcpus(void)
365{
366#ifdef CONFIG_USER_ONLY
367 return -1;
368#else
369 return get_ms()->smp.max_cpus;
370#endif
371}
Alex Bennéeca76a662019-10-11 16:34:05 +0100372
373/*
374 * Plugin output
375 */
376void qemu_plugin_outs(const char *string)
377{
378 qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
379}