audio: run downstream playback queue unconditionally
[qemu/ar7.git] / plugins / core.c
blobccb770a48501a1561dc44c5d93ce0d03a130a0d8
1 /*
2 * QEMU Plugin Core code
4 * This is the core code that deals with injecting instrumentation into the code
6 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
7 * Copyright (C) 2019, Linaro
9 * License: GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "qemu/osdep.h"
15 #include "qemu/error-report.h"
16 #include "qemu/config-file.h"
17 #include "qapi/error.h"
18 #include "qemu/lockable.h"
19 #include "qemu/option.h"
20 #include "qemu/rcu_queue.h"
21 #include "qemu/xxhash.h"
22 #include "qemu/rcu.h"
23 #include "hw/core/cpu.h"
24 #include "exec/cpu-common.h"
26 #include "exec/exec-all.h"
27 #include "exec/helper-proto.h"
28 #include "tcg/tcg.h"
29 #include "tcg/tcg-op.h"
30 #include "plugin.h"
31 #include "qemu/compiler.h"
33 struct qemu_plugin_cb {
34 struct qemu_plugin_ctx *ctx;
35 union qemu_plugin_cb_sig f;
36 void *udata;
37 QLIST_ENTRY(qemu_plugin_cb) entry;
40 struct qemu_plugin_state plugin;
42 struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)
44 struct qemu_plugin_ctx *ctx;
45 qemu_plugin_id_t *id_p;
47 id_p = g_hash_table_lookup(plugin.id_ht, &id);
48 ctx = container_of(id_p, struct qemu_plugin_ctx, id);
49 if (ctx == NULL) {
50 error_report("plugin: invalid plugin id %" PRIu64, id);
51 abort();
53 return ctx;
56 static void plugin_cpu_update__async(CPUState *cpu, run_on_cpu_data data)
58 bitmap_copy(cpu->plugin_mask, &data.host_ulong, QEMU_PLUGIN_EV_MAX);
59 tcg_flush_jmp_cache(cpu);
62 static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
64 CPUState *cpu = container_of(k, CPUState, cpu_index);
65 run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
67 if (cpu->created) {
68 async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
69 } else {
70 plugin_cpu_update__async(cpu, mask);
74 void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
75 enum qemu_plugin_event ev)
77 struct qemu_plugin_cb *cb = ctx->callbacks[ev];
79 if (cb == NULL) {
80 return;
82 QLIST_REMOVE_RCU(cb, entry);
83 g_free(cb);
84 ctx->callbacks[ev] = NULL;
85 if (QLIST_EMPTY_RCU(&plugin.cb_lists[ev])) {
86 clear_bit(ev, plugin.mask);
87 g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked, NULL);
92 * Disable CFI checks.
93 * The callback function has been loaded from an external library so we do not
94 * have type information
96 QEMU_DISABLE_CFI
97 static void plugin_vcpu_cb__simple(CPUState *cpu, enum qemu_plugin_event ev)
99 struct qemu_plugin_cb *cb, *next;
101 switch (ev) {
102 case QEMU_PLUGIN_EV_VCPU_INIT:
103 case QEMU_PLUGIN_EV_VCPU_EXIT:
104 case QEMU_PLUGIN_EV_VCPU_IDLE:
105 case QEMU_PLUGIN_EV_VCPU_RESUME:
106 /* iterate safely; plugins might uninstall themselves at any time */
107 QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
108 qemu_plugin_vcpu_simple_cb_t func = cb->f.vcpu_simple;
110 func(cb->ctx->id, cpu->cpu_index);
112 break;
113 default:
114 g_assert_not_reached();
119 * Disable CFI checks.
120 * The callback function has been loaded from an external library so we do not
121 * have type information
123 QEMU_DISABLE_CFI
124 static void plugin_cb__simple(enum qemu_plugin_event ev)
126 struct qemu_plugin_cb *cb, *next;
128 switch (ev) {
129 case QEMU_PLUGIN_EV_FLUSH:
130 QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
131 qemu_plugin_simple_cb_t func = cb->f.simple;
133 func(cb->ctx->id);
135 break;
136 default:
137 g_assert_not_reached();
142 * Disable CFI checks.
143 * The callback function has been loaded from an external library so we do not
144 * have type information
146 QEMU_DISABLE_CFI
147 static void plugin_cb__udata(enum qemu_plugin_event ev)
149 struct qemu_plugin_cb *cb, *next;
151 switch (ev) {
152 case QEMU_PLUGIN_EV_ATEXIT:
153 QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
154 qemu_plugin_udata_cb_t func = cb->f.udata;
156 func(cb->ctx->id, cb->udata);
158 break;
159 default:
160 g_assert_not_reached();
164 static void
165 do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
166 void *func, void *udata)
168 struct qemu_plugin_ctx *ctx;
170 QEMU_LOCK_GUARD(&plugin.lock);
171 ctx = plugin_id_to_ctx_locked(id);
172 /* if the plugin is on its way out, ignore this request */
173 if (unlikely(ctx->uninstalling)) {
174 return;
176 if (func) {
177 struct qemu_plugin_cb *cb = ctx->callbacks[ev];
179 if (cb) {
180 cb->f.generic = func;
181 cb->udata = udata;
182 } else {
183 cb = g_new(struct qemu_plugin_cb, 1);
184 cb->ctx = ctx;
185 cb->f.generic = func;
186 cb->udata = udata;
187 ctx->callbacks[ev] = cb;
188 QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
189 if (!test_bit(ev, plugin.mask)) {
190 set_bit(ev, plugin.mask);
191 g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
192 NULL);
195 } else {
196 plugin_unregister_cb__locked(ctx, ev);
200 void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
201 void *func)
203 do_plugin_register_cb(id, ev, func, NULL);
206 void
207 plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
208 void *func, void *udata)
210 do_plugin_register_cb(id, ev, func, udata);
213 void qemu_plugin_vcpu_init_hook(CPUState *cpu)
215 bool success;
217 qemu_rec_mutex_lock(&plugin.lock);
218 plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
219 success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
220 &cpu->cpu_index);
221 g_assert(success);
222 qemu_rec_mutex_unlock(&plugin.lock);
224 plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
227 void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
229 bool success;
231 plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
233 qemu_rec_mutex_lock(&plugin.lock);
234 success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
235 g_assert(success);
236 qemu_rec_mutex_unlock(&plugin.lock);
239 struct plugin_for_each_args {
240 struct qemu_plugin_ctx *ctx;
241 qemu_plugin_vcpu_simple_cb_t cb;
244 static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
246 struct plugin_for_each_args *args = udata;
247 int cpu_index = *(int *)k;
249 args->cb(args->ctx->id, cpu_index);
252 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
253 qemu_plugin_vcpu_simple_cb_t cb)
255 struct plugin_for_each_args args;
257 if (cb == NULL) {
258 return;
260 qemu_rec_mutex_lock(&plugin.lock);
261 args.ctx = plugin_id_to_ctx_locked(id);
262 args.cb = cb;
263 g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
264 qemu_rec_mutex_unlock(&plugin.lock);
267 /* Allocate and return a callback record */
268 static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
270 GArray *cbs = *arr;
272 if (!cbs) {
273 cbs = g_array_sized_new(false, false,
274 sizeof(struct qemu_plugin_dyn_cb), 1);
275 *arr = cbs;
278 g_array_set_size(cbs, cbs->len + 1);
279 return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
282 void plugin_register_inline_op(GArray **arr,
283 enum qemu_plugin_mem_rw rw,
284 enum qemu_plugin_op op, void *ptr,
285 uint64_t imm)
287 struct qemu_plugin_dyn_cb *dyn_cb;
289 dyn_cb = plugin_get_dyn_cb(arr);
290 dyn_cb->userp = ptr;
291 dyn_cb->type = PLUGIN_CB_INLINE;
292 dyn_cb->rw = rw;
293 dyn_cb->inline_insn.op = op;
294 dyn_cb->inline_insn.imm = imm;
297 void plugin_register_dyn_cb__udata(GArray **arr,
298 qemu_plugin_vcpu_udata_cb_t cb,
299 enum qemu_plugin_cb_flags flags,
300 void *udata)
302 struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
304 dyn_cb->userp = udata;
305 /* Note flags are discarded as unused. */
306 dyn_cb->f.vcpu_udata = cb;
307 dyn_cb->type = PLUGIN_CB_REGULAR;
310 void plugin_register_vcpu_mem_cb(GArray **arr,
311 void *cb,
312 enum qemu_plugin_cb_flags flags,
313 enum qemu_plugin_mem_rw rw,
314 void *udata)
316 struct qemu_plugin_dyn_cb *dyn_cb;
318 dyn_cb = plugin_get_dyn_cb(arr);
319 dyn_cb->userp = udata;
320 /* Note flags are discarded as unused. */
321 dyn_cb->type = PLUGIN_CB_REGULAR;
322 dyn_cb->rw = rw;
323 dyn_cb->f.generic = cb;
327 * Disable CFI checks.
328 * The callback function has been loaded from an external library so we do not
329 * have type information
331 QEMU_DISABLE_CFI
332 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
334 struct qemu_plugin_cb *cb, *next;
335 enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
337 /* no plugin_mask check here; caller should have checked */
339 QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
340 qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
342 func(cb->ctx->id, tb);
347 * Disable CFI checks.
348 * The callback function has been loaded from an external library so we do not
349 * have type information
351 QEMU_DISABLE_CFI
352 void
353 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
354 uint64_t a3, uint64_t a4, uint64_t a5,
355 uint64_t a6, uint64_t a7, uint64_t a8)
357 struct qemu_plugin_cb *cb, *next;
358 enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
360 if (!test_bit(ev, cpu->plugin_mask)) {
361 return;
364 QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
365 qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
367 func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);
372 * Disable CFI checks.
373 * The callback function has been loaded from an external library so we do not
374 * have type information
376 QEMU_DISABLE_CFI
377 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
379 struct qemu_plugin_cb *cb, *next;
380 enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
382 if (!test_bit(ev, cpu->plugin_mask)) {
383 return;
386 QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
387 qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret;
389 func(cb->ctx->id, cpu->cpu_index, num, ret);
393 void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
395 plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE);
398 void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
400 plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
403 void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
404 qemu_plugin_vcpu_simple_cb_t cb)
406 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
409 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
410 qemu_plugin_vcpu_simple_cb_t cb)
412 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
415 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
416 qemu_plugin_simple_cb_t cb)
418 plugin_register_cb(id, QEMU_PLUGIN_EV_FLUSH, cb);
421 static bool free_dyn_cb_arr(void *p, uint32_t h, void *userp)
423 g_array_free((GArray *) p, true);
424 return true;
427 void qemu_plugin_flush_cb(void)
429 qht_iter_remove(&plugin.dyn_cb_arr_ht, free_dyn_cb_arr, NULL);
430 qht_reset(&plugin.dyn_cb_arr_ht);
432 plugin_cb__simple(QEMU_PLUGIN_EV_FLUSH);
435 void exec_inline_op(struct qemu_plugin_dyn_cb *cb)
437 uint64_t *val = cb->userp;
439 switch (cb->inline_insn.op) {
440 case QEMU_PLUGIN_INLINE_ADD_U64:
441 *val += cb->inline_insn.imm;
442 break;
443 default:
444 g_assert_not_reached();
448 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
449 MemOpIdx oi, enum qemu_plugin_mem_rw rw)
451 GArray *arr = cpu->plugin_mem_cbs;
452 size_t i;
454 if (arr == NULL) {
455 return;
457 for (i = 0; i < arr->len; i++) {
458 struct qemu_plugin_dyn_cb *cb =
459 &g_array_index(arr, struct qemu_plugin_dyn_cb, i);
461 if (!(rw & cb->rw)) {
462 break;
464 switch (cb->type) {
465 case PLUGIN_CB_REGULAR:
466 cb->f.vcpu_mem(cpu->cpu_index, make_plugin_meminfo(oi, rw),
467 vaddr, cb->userp);
468 break;
469 case PLUGIN_CB_INLINE:
470 exec_inline_op(cb);
471 break;
472 default:
473 g_assert_not_reached();
478 void qemu_plugin_atexit_cb(void)
480 plugin_cb__udata(QEMU_PLUGIN_EV_ATEXIT);
483 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
484 qemu_plugin_udata_cb_t cb,
485 void *udata)
487 plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
491 * Handle exit from linux-user. Unlike the normal atexit() mechanism
492 * we need to handle the clean-up manually as it's possible threads
493 * are still running. We need to remove all callbacks from code
494 * generation, flush the current translations and then we can safely
495 * trigger the exit callbacks.
498 void qemu_plugin_user_exit(void)
500 enum qemu_plugin_event ev;
501 CPUState *cpu;
503 QEMU_LOCK_GUARD(&plugin.lock);
505 start_exclusive();
507 /* un-register all callbacks except the final AT_EXIT one */
508 for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
509 if (ev != QEMU_PLUGIN_EV_ATEXIT) {
510 struct qemu_plugin_ctx *ctx;
511 QTAILQ_FOREACH(ctx, &plugin.ctxs, entry) {
512 plugin_unregister_cb__locked(ctx, ev);
517 tb_flush(current_cpu);
519 CPU_FOREACH(cpu) {
520 qemu_plugin_disable_mem_helpers(cpu);
523 end_exclusive();
525 /* now it's safe to handle the exit case */
526 qemu_plugin_atexit_cb();
530 * Helpers for *-user to ensure locks are sane across fork() events.
533 void qemu_plugin_user_prefork_lock(void)
535 qemu_rec_mutex_lock(&plugin.lock);
538 void qemu_plugin_user_postfork(bool is_child)
540 if (is_child) {
541 /* should we just reset via plugin_init? */
542 qemu_rec_mutex_init(&plugin.lock);
543 } else {
544 qemu_rec_mutex_unlock(&plugin.lock);
550 * Call this function after longjmp'ing to the main loop. It's possible that the
551 * last instruction of a TB might have used helpers, and therefore the
552 * "disable" instruction will never execute because it ended up as dead code.
554 void qemu_plugin_disable_mem_helpers(CPUState *cpu)
556 cpu->plugin_mem_cbs = NULL;
559 static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp)
561 return ap == bp;
564 static void __attribute__((__constructor__)) plugin_init(void)
566 int i;
568 for (i = 0; i < QEMU_PLUGIN_EV_MAX; i++) {
569 QLIST_INIT(&plugin.cb_lists[i]);
571 qemu_rec_mutex_init(&plugin.lock);
572 plugin.id_ht = g_hash_table_new(g_int64_hash, g_int64_equal);
573 plugin.cpu_ht = g_hash_table_new(g_int_hash, g_int_equal);
574 QTAILQ_INIT(&plugin.ctxs);
575 qht_init(&plugin.dyn_cb_arr_ht, plugin_dyn_cb_arr_cmp, 16,
576 QHT_MODE_AUTO_RESIZE);
577 atexit(qemu_plugin_atexit_cb);