154cb65d8SEmilio G. Cota /*
254cb65d8SEmilio G. Cota * QEMU Plugin Core code
354cb65d8SEmilio G. Cota *
454cb65d8SEmilio G. Cota * This is the core code that deals with injecting instrumentation into the code
554cb65d8SEmilio G. Cota *
654cb65d8SEmilio G. Cota * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
754cb65d8SEmilio G. Cota * Copyright (C) 2019, Linaro
854cb65d8SEmilio G. Cota *
954cb65d8SEmilio G. Cota * License: GNU GPL, version 2 or later.
1054cb65d8SEmilio G. Cota * See the COPYING file in the top-level directory.
1154cb65d8SEmilio G. Cota *
1254cb65d8SEmilio G. Cota * SPDX-License-Identifier: GPL-2.0-or-later
1354cb65d8SEmilio G. Cota */
1454cb65d8SEmilio G. Cota #include "qemu/osdep.h"
1554cb65d8SEmilio G. Cota #include "qemu/error-report.h"
1654cb65d8SEmilio G. Cota #include "qemu/config-file.h"
1754cb65d8SEmilio G. Cota #include "qapi/error.h"
18ac90871cSStefan Hajnoczi #include "qemu/lockable.h"
1954cb65d8SEmilio G. Cota #include "qemu/option.h"
20c0061471SAlex Bennée #include "qemu/plugin.h"
21a3c2cf0bSPierrick Bouvier #include "qemu/queue.h"
2254cb65d8SEmilio G. Cota #include "qemu/rcu_queue.h"
2354cb65d8SEmilio G. Cota #include "qemu/xxhash.h"
2454cb65d8SEmilio G. Cota #include "qemu/rcu.h"
2554cb65d8SEmilio G. Cota #include "hw/core/cpu.h"
2654cb65d8SEmilio G. Cota
2754cb65d8SEmilio G. Cota #include "exec/exec-all.h"
28548c9609SAlex Bennée #include "exec/tb-flush.h"
2954cb65d8SEmilio G. Cota #include "tcg/tcg.h"
3054cb65d8SEmilio G. Cota #include "tcg/tcg-op.h"
3154cb65d8SEmilio G. Cota #include "plugin.h"
3254cb65d8SEmilio G. Cota
3354cb65d8SEmilio G. Cota struct qemu_plugin_cb {
3454cb65d8SEmilio G. Cota struct qemu_plugin_ctx *ctx;
3554cb65d8SEmilio G. Cota union qemu_plugin_cb_sig f;
3654cb65d8SEmilio G. Cota void *udata;
3754cb65d8SEmilio G. Cota QLIST_ENTRY(qemu_plugin_cb) entry;
3854cb65d8SEmilio G. Cota };
3954cb65d8SEmilio G. Cota
4054cb65d8SEmilio G. Cota struct qemu_plugin_state plugin;
4154cb65d8SEmilio G. Cota
plugin_id_to_ctx_locked(qemu_plugin_id_t id)4254cb65d8SEmilio G. Cota struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)
4354cb65d8SEmilio G. Cota {
4454cb65d8SEmilio G. Cota struct qemu_plugin_ctx *ctx;
4554cb65d8SEmilio G. Cota qemu_plugin_id_t *id_p;
4654cb65d8SEmilio G. Cota
4754cb65d8SEmilio G. Cota id_p = g_hash_table_lookup(plugin.id_ht, &id);
4854cb65d8SEmilio G. Cota ctx = container_of(id_p, struct qemu_plugin_ctx, id);
4954cb65d8SEmilio G. Cota if (ctx == NULL) {
5054cb65d8SEmilio G. Cota error_report("plugin: invalid plugin id %" PRIu64, id);
5154cb65d8SEmilio G. Cota abort();
5254cb65d8SEmilio G. Cota }
5354cb65d8SEmilio G. Cota return ctx;
5454cb65d8SEmilio G. Cota }
5554cb65d8SEmilio G. Cota
plugin_cpu_update__async(CPUState * cpu,run_on_cpu_data data)5654cb65d8SEmilio G. Cota static void plugin_cpu_update__async(CPUState *cpu, run_on_cpu_data data)
5754cb65d8SEmilio G. Cota {
58c0061471SAlex Bennée bitmap_copy(cpu->plugin_state->event_mask,
59c0061471SAlex Bennée &data.host_ulong, QEMU_PLUGIN_EV_MAX);
60a976a99aSRichard Henderson tcg_flush_jmp_cache(cpu);
6154cb65d8SEmilio G. Cota }
6254cb65d8SEmilio G. Cota
plugin_cpu_update__locked(gpointer k,gpointer v,gpointer udata)6354cb65d8SEmilio G. Cota static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
6454cb65d8SEmilio G. Cota {
6554cb65d8SEmilio G. Cota CPUState *cpu = container_of(k, CPUState, cpu_index);
6654cb65d8SEmilio G. Cota run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
6754cb65d8SEmilio G. Cota
6854cb65d8SEmilio G. Cota async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
6954cb65d8SEmilio G. Cota }
7054cb65d8SEmilio G. Cota
plugin_unregister_cb__locked(struct qemu_plugin_ctx * ctx,enum qemu_plugin_event ev)7154cb65d8SEmilio G. Cota void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
7254cb65d8SEmilio G. Cota enum qemu_plugin_event ev)
7354cb65d8SEmilio G. Cota {
7454cb65d8SEmilio G. Cota struct qemu_plugin_cb *cb = ctx->callbacks[ev];
7554cb65d8SEmilio G. Cota
7654cb65d8SEmilio G. Cota if (cb == NULL) {
7754cb65d8SEmilio G. Cota return;
7854cb65d8SEmilio G. Cota }
7954cb65d8SEmilio G. Cota QLIST_REMOVE_RCU(cb, entry);
8054cb65d8SEmilio G. Cota g_free(cb);
8154cb65d8SEmilio G. Cota ctx->callbacks[ev] = NULL;
8254cb65d8SEmilio G. Cota if (QLIST_EMPTY_RCU(&plugin.cb_lists[ev])) {
8354cb65d8SEmilio G. Cota clear_bit(ev, plugin.mask);
8454cb65d8SEmilio G. Cota g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked, NULL);
8554cb65d8SEmilio G. Cota }
8654cb65d8SEmilio G. Cota }
8754cb65d8SEmilio G. Cota
88c905a368SDaniele Buono /*
89c905a368SDaniele Buono * Disable CFI checks.
90c905a368SDaniele Buono * The callback function has been loaded from an external library so we do not
91c905a368SDaniele Buono * have type information
92c905a368SDaniele Buono */
93c905a368SDaniele Buono QEMU_DISABLE_CFI
plugin_vcpu_cb__simple(CPUState * cpu,enum qemu_plugin_event ev)9454cb65d8SEmilio G. Cota static void plugin_vcpu_cb__simple(CPUState *cpu, enum qemu_plugin_event ev)
9554cb65d8SEmilio G. Cota {
9654cb65d8SEmilio G. Cota struct qemu_plugin_cb *cb, *next;
9754cb65d8SEmilio G. Cota
9854cb65d8SEmilio G. Cota switch (ev) {
9954cb65d8SEmilio G. Cota case QEMU_PLUGIN_EV_VCPU_INIT:
10054cb65d8SEmilio G. Cota case QEMU_PLUGIN_EV_VCPU_EXIT:
10154cb65d8SEmilio G. Cota case QEMU_PLUGIN_EV_VCPU_IDLE:
10254cb65d8SEmilio G. Cota case QEMU_PLUGIN_EV_VCPU_RESUME:
10354cb65d8SEmilio G. Cota /* iterate safely; plugins might uninstall themselves at any time */
10454cb65d8SEmilio G. Cota QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
10554cb65d8SEmilio G. Cota qemu_plugin_vcpu_simple_cb_t func = cb->f.vcpu_simple;
10654cb65d8SEmilio G. Cota
10754cb65d8SEmilio G. Cota func(cb->ctx->id, cpu->cpu_index);
10854cb65d8SEmilio G. Cota }
10954cb65d8SEmilio G. Cota break;
11054cb65d8SEmilio G. Cota default:
11154cb65d8SEmilio G. Cota g_assert_not_reached();
11254cb65d8SEmilio G. Cota }
11354cb65d8SEmilio G. Cota }
11454cb65d8SEmilio G. Cota
115c905a368SDaniele Buono /*
116c905a368SDaniele Buono * Disable CFI checks.
117c905a368SDaniele Buono * The callback function has been loaded from an external library so we do not
118c905a368SDaniele Buono * have type information
119c905a368SDaniele Buono */
120c905a368SDaniele Buono QEMU_DISABLE_CFI
plugin_cb__simple(enum qemu_plugin_event ev)12154cb65d8SEmilio G. Cota static void plugin_cb__simple(enum qemu_plugin_event ev)
12254cb65d8SEmilio G. Cota {
12354cb65d8SEmilio G. Cota struct qemu_plugin_cb *cb, *next;
12454cb65d8SEmilio G. Cota
12554cb65d8SEmilio G. Cota switch (ev) {
12654cb65d8SEmilio G. Cota case QEMU_PLUGIN_EV_FLUSH:
12754cb65d8SEmilio G. Cota QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
12854cb65d8SEmilio G. Cota qemu_plugin_simple_cb_t func = cb->f.simple;
12954cb65d8SEmilio G. Cota
13054cb65d8SEmilio G. Cota func(cb->ctx->id);
13154cb65d8SEmilio G. Cota }
13254cb65d8SEmilio G. Cota break;
13354cb65d8SEmilio G. Cota default:
13454cb65d8SEmilio G. Cota g_assert_not_reached();
13554cb65d8SEmilio G. Cota }
13654cb65d8SEmilio G. Cota }
13754cb65d8SEmilio G. Cota
138c905a368SDaniele Buono /*
139c905a368SDaniele Buono * Disable CFI checks.
140c905a368SDaniele Buono * The callback function has been loaded from an external library so we do not
141c905a368SDaniele Buono * have type information
142c905a368SDaniele Buono */
143c905a368SDaniele Buono QEMU_DISABLE_CFI
plugin_cb__udata(enum qemu_plugin_event ev)14454cb65d8SEmilio G. Cota static void plugin_cb__udata(enum qemu_plugin_event ev)
14554cb65d8SEmilio G. Cota {
14654cb65d8SEmilio G. Cota struct qemu_plugin_cb *cb, *next;
14754cb65d8SEmilio G. Cota
14854cb65d8SEmilio G. Cota switch (ev) {
14954cb65d8SEmilio G. Cota case QEMU_PLUGIN_EV_ATEXIT:
15054cb65d8SEmilio G. Cota QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
15154cb65d8SEmilio G. Cota qemu_plugin_udata_cb_t func = cb->f.udata;
15254cb65d8SEmilio G. Cota
15354cb65d8SEmilio G. Cota func(cb->ctx->id, cb->udata);
15454cb65d8SEmilio G. Cota }
15554cb65d8SEmilio G. Cota break;
15654cb65d8SEmilio G. Cota default:
15754cb65d8SEmilio G. Cota g_assert_not_reached();
15854cb65d8SEmilio G. Cota }
15954cb65d8SEmilio G. Cota }
16054cb65d8SEmilio G. Cota
16154cb65d8SEmilio G. Cota static void
do_plugin_register_cb(qemu_plugin_id_t id,enum qemu_plugin_event ev,void * func,void * udata)16254cb65d8SEmilio G. Cota do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
16354cb65d8SEmilio G. Cota void *func, void *udata)
16454cb65d8SEmilio G. Cota {
16554cb65d8SEmilio G. Cota struct qemu_plugin_ctx *ctx;
16654cb65d8SEmilio G. Cota
167ac90871cSStefan Hajnoczi QEMU_LOCK_GUARD(&plugin.lock);
16854cb65d8SEmilio G. Cota ctx = plugin_id_to_ctx_locked(id);
16954cb65d8SEmilio G. Cota /* if the plugin is on its way out, ignore this request */
17054cb65d8SEmilio G. Cota if (unlikely(ctx->uninstalling)) {
171ac90871cSStefan Hajnoczi return;
17254cb65d8SEmilio G. Cota }
17354cb65d8SEmilio G. Cota if (func) {
17454cb65d8SEmilio G. Cota struct qemu_plugin_cb *cb = ctx->callbacks[ev];
17554cb65d8SEmilio G. Cota
17654cb65d8SEmilio G. Cota if (cb) {
17754cb65d8SEmilio G. Cota cb->f.generic = func;
17854cb65d8SEmilio G. Cota cb->udata = udata;
17954cb65d8SEmilio G. Cota } else {
18054cb65d8SEmilio G. Cota cb = g_new(struct qemu_plugin_cb, 1);
18154cb65d8SEmilio G. Cota cb->ctx = ctx;
18254cb65d8SEmilio G. Cota cb->f.generic = func;
18354cb65d8SEmilio G. Cota cb->udata = udata;
18454cb65d8SEmilio G. Cota ctx->callbacks[ev] = cb;
18554cb65d8SEmilio G. Cota QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
18654cb65d8SEmilio G. Cota if (!test_bit(ev, plugin.mask)) {
18754cb65d8SEmilio G. Cota set_bit(ev, plugin.mask);
18854cb65d8SEmilio G. Cota g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
18954cb65d8SEmilio G. Cota NULL);
19054cb65d8SEmilio G. Cota }
19154cb65d8SEmilio G. Cota }
19254cb65d8SEmilio G. Cota } else {
19354cb65d8SEmilio G. Cota plugin_unregister_cb__locked(ctx, ev);
19454cb65d8SEmilio G. Cota }
19554cb65d8SEmilio G. Cota }
19654cb65d8SEmilio G. Cota
plugin_register_cb(qemu_plugin_id_t id,enum qemu_plugin_event ev,void * func)19754cb65d8SEmilio G. Cota void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
19854cb65d8SEmilio G. Cota void *func)
19954cb65d8SEmilio G. Cota {
20054cb65d8SEmilio G. Cota do_plugin_register_cb(id, ev, func, NULL);
20154cb65d8SEmilio G. Cota }
20254cb65d8SEmilio G. Cota
20354cb65d8SEmilio G. Cota void
plugin_register_cb_udata(qemu_plugin_id_t id,enum qemu_plugin_event ev,void * func,void * udata)20454cb65d8SEmilio G. Cota plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
20554cb65d8SEmilio G. Cota void *func, void *udata)
20654cb65d8SEmilio G. Cota {
20754cb65d8SEmilio G. Cota do_plugin_register_cb(id, ev, func, udata);
20854cb65d8SEmilio G. Cota }
20954cb65d8SEmilio G. Cota
qemu_plugin_create_vcpu_state(void)210c0061471SAlex Bennée CPUPluginState *qemu_plugin_create_vcpu_state(void)
211c0061471SAlex Bennée {
212c0061471SAlex Bennée return g_new0(CPUPluginState, 1);
213c0061471SAlex Bennée }
214c0061471SAlex Bennée
plugin_grow_scoreboards__locked(CPUState * cpu)215a3c2cf0bSPierrick Bouvier static void plugin_grow_scoreboards__locked(CPUState *cpu)
216a3c2cf0bSPierrick Bouvier {
217278035fcSPierrick Bouvier size_t scoreboard_size = plugin.scoreboard_alloc_size;
218278035fcSPierrick Bouvier bool need_realloc = false;
219278035fcSPierrick Bouvier
220278035fcSPierrick Bouvier if (cpu->cpu_index < scoreboard_size) {
221a3c2cf0bSPierrick Bouvier return;
222a3c2cf0bSPierrick Bouvier }
223a3c2cf0bSPierrick Bouvier
224278035fcSPierrick Bouvier while (cpu->cpu_index >= scoreboard_size) {
225278035fcSPierrick Bouvier scoreboard_size *= 2;
226278035fcSPierrick Bouvier need_realloc = true;
227a3c2cf0bSPierrick Bouvier }
228a3c2cf0bSPierrick Bouvier
229278035fcSPierrick Bouvier if (!need_realloc) {
230a3c2cf0bSPierrick Bouvier return;
231a3c2cf0bSPierrick Bouvier }
232a3c2cf0bSPierrick Bouvier
233278035fcSPierrick Bouvier if (QLIST_EMPTY(&plugin.scoreboards)) {
234278035fcSPierrick Bouvier /* just update size for future scoreboards */
235278035fcSPierrick Bouvier plugin.scoreboard_alloc_size = scoreboard_size;
236278035fcSPierrick Bouvier return;
237278035fcSPierrick Bouvier }
238278035fcSPierrick Bouvier
239278035fcSPierrick Bouvier /*
240278035fcSPierrick Bouvier * A scoreboard creation/deletion might be in progress. If a new vcpu is
241278035fcSPierrick Bouvier * initialized at the same time, we are safe, as the new
242278035fcSPierrick Bouvier * plugin.scoreboard_alloc_size was not yet written.
243278035fcSPierrick Bouvier */
244278035fcSPierrick Bouvier qemu_rec_mutex_unlock(&plugin.lock);
245278035fcSPierrick Bouvier
246a3c2cf0bSPierrick Bouvier /* cpus must be stopped, as tb might still use an existing scoreboard. */
247a3c2cf0bSPierrick Bouvier start_exclusive();
248278035fcSPierrick Bouvier /* re-acquire lock */
249278035fcSPierrick Bouvier qemu_rec_mutex_lock(&plugin.lock);
250278035fcSPierrick Bouvier /* in case another vcpu is created between unlock and exclusive section. */
251278035fcSPierrick Bouvier if (scoreboard_size > plugin.scoreboard_alloc_size) {
252a3c2cf0bSPierrick Bouvier struct qemu_plugin_scoreboard *score;
253a3c2cf0bSPierrick Bouvier QLIST_FOREACH(score, &plugin.scoreboards, entry) {
254278035fcSPierrick Bouvier g_array_set_size(score->data, scoreboard_size);
255a3c2cf0bSPierrick Bouvier }
256278035fcSPierrick Bouvier plugin.scoreboard_alloc_size = scoreboard_size;
257a3c2cf0bSPierrick Bouvier /* force all tb to be flushed, as scoreboard pointers were changed. */
258a3c2cf0bSPierrick Bouvier tb_flush(cpu);
259278035fcSPierrick Bouvier }
260a3c2cf0bSPierrick Bouvier end_exclusive();
261a3c2cf0bSPierrick Bouvier }
262a3c2cf0bSPierrick Bouvier
qemu_plugin_vcpu_init__async(CPUState * cpu,run_on_cpu_data unused)2630f3974b6SPhilippe Mathieu-Daudé static void qemu_plugin_vcpu_init__async(CPUState *cpu, run_on_cpu_data unused)
26454cb65d8SEmilio G. Cota {
26554cb65d8SEmilio G. Cota bool success;
26654cb65d8SEmilio G. Cota
2672089a2e5SPhilippe Mathieu-Daudé assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
26854cb65d8SEmilio G. Cota qemu_rec_mutex_lock(&plugin.lock);
2694a448b14SPierrick Bouvier plugin.num_vcpus = MAX(plugin.num_vcpus, cpu->cpu_index + 1);
27054cb65d8SEmilio G. Cota plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
27154cb65d8SEmilio G. Cota success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
27254cb65d8SEmilio G. Cota &cpu->cpu_index);
27354cb65d8SEmilio G. Cota g_assert(success);
274a3c2cf0bSPierrick Bouvier plugin_grow_scoreboards__locked(cpu);
27554cb65d8SEmilio G. Cota qemu_rec_mutex_unlock(&plugin.lock);
27654cb65d8SEmilio G. Cota
27754cb65d8SEmilio G. Cota plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
27854cb65d8SEmilio G. Cota }
27954cb65d8SEmilio G. Cota
qemu_plugin_vcpu_init_hook(CPUState * cpu)2800f3974b6SPhilippe Mathieu-Daudé void qemu_plugin_vcpu_init_hook(CPUState *cpu)
2810f3974b6SPhilippe Mathieu-Daudé {
2820f3974b6SPhilippe Mathieu-Daudé /* Plugin initialization must wait until the cpu start executing code */
2830f3974b6SPhilippe Mathieu-Daudé async_run_on_cpu(cpu, qemu_plugin_vcpu_init__async, RUN_ON_CPU_NULL);
2840f3974b6SPhilippe Mathieu-Daudé }
2850f3974b6SPhilippe Mathieu-Daudé
qemu_plugin_vcpu_exit_hook(CPUState * cpu)28654cb65d8SEmilio G. Cota void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
28754cb65d8SEmilio G. Cota {
28854cb65d8SEmilio G. Cota bool success;
28954cb65d8SEmilio G. Cota
29054cb65d8SEmilio G. Cota plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
29154cb65d8SEmilio G. Cota
2922089a2e5SPhilippe Mathieu-Daudé assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
29354cb65d8SEmilio G. Cota qemu_rec_mutex_lock(&plugin.lock);
29454cb65d8SEmilio G. Cota success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
29554cb65d8SEmilio G. Cota g_assert(success);
29654cb65d8SEmilio G. Cota qemu_rec_mutex_unlock(&plugin.lock);
29754cb65d8SEmilio G. Cota }
29854cb65d8SEmilio G. Cota
29954cb65d8SEmilio G. Cota struct plugin_for_each_args {
30054cb65d8SEmilio G. Cota struct qemu_plugin_ctx *ctx;
30154cb65d8SEmilio G. Cota qemu_plugin_vcpu_simple_cb_t cb;
30254cb65d8SEmilio G. Cota };
30354cb65d8SEmilio G. Cota
plugin_vcpu_for_each(gpointer k,gpointer v,gpointer udata)30454cb65d8SEmilio G. Cota static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
30554cb65d8SEmilio G. Cota {
30654cb65d8SEmilio G. Cota struct plugin_for_each_args *args = udata;
30754cb65d8SEmilio G. Cota int cpu_index = *(int *)k;
30854cb65d8SEmilio G. Cota
30954cb65d8SEmilio G. Cota args->cb(args->ctx->id, cpu_index);
31054cb65d8SEmilio G. Cota }
31154cb65d8SEmilio G. Cota
qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,qemu_plugin_vcpu_simple_cb_t cb)31254cb65d8SEmilio G. Cota void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
31354cb65d8SEmilio G. Cota qemu_plugin_vcpu_simple_cb_t cb)
31454cb65d8SEmilio G. Cota {
31554cb65d8SEmilio G. Cota struct plugin_for_each_args args;
31654cb65d8SEmilio G. Cota
31754cb65d8SEmilio G. Cota if (cb == NULL) {
31854cb65d8SEmilio G. Cota return;
31954cb65d8SEmilio G. Cota }
32054cb65d8SEmilio G. Cota qemu_rec_mutex_lock(&plugin.lock);
32154cb65d8SEmilio G. Cota args.ctx = plugin_id_to_ctx_locked(id);
32254cb65d8SEmilio G. Cota args.cb = cb;
32354cb65d8SEmilio G. Cota g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
32454cb65d8SEmilio G. Cota qemu_rec_mutex_unlock(&plugin.lock);
32554cb65d8SEmilio G. Cota }
32654cb65d8SEmilio G. Cota
32754cb65d8SEmilio G. Cota /* Allocate and return a callback record */
plugin_get_dyn_cb(GArray ** arr)32854cb65d8SEmilio G. Cota static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
32954cb65d8SEmilio G. Cota {
33054cb65d8SEmilio G. Cota GArray *cbs = *arr;
33154cb65d8SEmilio G. Cota
33254cb65d8SEmilio G. Cota if (!cbs) {
33325875fe9SRichard Henderson cbs = g_array_sized_new(false, true,
33454cb65d8SEmilio G. Cota sizeof(struct qemu_plugin_dyn_cb), 1);
33554cb65d8SEmilio G. Cota *arr = cbs;
33654cb65d8SEmilio G. Cota }
33754cb65d8SEmilio G. Cota
33854cb65d8SEmilio G. Cota g_array_set_size(cbs, cbs->len + 1);
33954cb65d8SEmilio G. Cota return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
34054cb65d8SEmilio G. Cota }
34154cb65d8SEmilio G. Cota
op_to_cb_type(enum qemu_plugin_op op)34221032784SPierrick Bouvier static enum plugin_dyn_cb_type op_to_cb_type(enum qemu_plugin_op op)
34321032784SPierrick Bouvier {
34421032784SPierrick Bouvier switch (op) {
34521032784SPierrick Bouvier case QEMU_PLUGIN_INLINE_ADD_U64:
34621032784SPierrick Bouvier return PLUGIN_CB_INLINE_ADD_U64;
34736a1d8e7SPierrick Bouvier case QEMU_PLUGIN_INLINE_STORE_U64:
34836a1d8e7SPierrick Bouvier return PLUGIN_CB_INLINE_STORE_U64;
34921032784SPierrick Bouvier default:
35021032784SPierrick Bouvier g_assert_not_reached();
35121032784SPierrick Bouvier }
35221032784SPierrick Bouvier }
35321032784SPierrick Bouvier
plugin_register_inline_op_on_entry(GArray ** arr,enum qemu_plugin_mem_rw rw,enum qemu_plugin_op op,qemu_plugin_u64 entry,uint64_t imm)3540bcebabaSPierrick Bouvier void plugin_register_inline_op_on_entry(GArray **arr,
3550bcebabaSPierrick Bouvier enum qemu_plugin_mem_rw rw,
3560bcebabaSPierrick Bouvier enum qemu_plugin_op op,
3570bcebabaSPierrick Bouvier qemu_plugin_u64 entry,
3580bcebabaSPierrick Bouvier uint64_t imm)
3590bcebabaSPierrick Bouvier {
3600bcebabaSPierrick Bouvier struct qemu_plugin_dyn_cb *dyn_cb;
3610bcebabaSPierrick Bouvier
362f86fd4d8SPierrick Bouvier struct qemu_plugin_inline_cb inline_cb = { .rw = rw,
363f86fd4d8SPierrick Bouvier .entry = entry,
364f86fd4d8SPierrick Bouvier .imm = imm };
3650bcebabaSPierrick Bouvier dyn_cb = plugin_get_dyn_cb(arr);
36621032784SPierrick Bouvier dyn_cb->type = op_to_cb_type(op);
367f86fd4d8SPierrick Bouvier dyn_cb->inline_insn = inline_cb;
3680bcebabaSPierrick Bouvier }
3690bcebabaSPierrick Bouvier
plugin_register_dyn_cb__udata(GArray ** arr,qemu_plugin_vcpu_udata_cb_t cb,enum qemu_plugin_cb_flags flags,void * udata)370c7bb41b4SRichard Henderson void plugin_register_dyn_cb__udata(GArray **arr,
37154cb65d8SEmilio G. Cota qemu_plugin_vcpu_udata_cb_t cb,
372c7bb41b4SRichard Henderson enum qemu_plugin_cb_flags flags,
373c7bb41b4SRichard Henderson void *udata)
37454cb65d8SEmilio G. Cota {
375c7ba9483SRichard Henderson static TCGHelperInfo info[3] = {
376b0748975SRichard Henderson [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
377b0748975SRichard Henderson [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
378c7ba9483SRichard Henderson /*
379c7ba9483SRichard Henderson * Match qemu_plugin_vcpu_udata_cb_t:
380c7ba9483SRichard Henderson * void (*)(uint32_t, void *)
381c7ba9483SRichard Henderson */
382c7ba9483SRichard Henderson [0 ... 2].typemask = (dh_typemask(void, 0) |
383c7ba9483SRichard Henderson dh_typemask(i32, 1) |
384c7ba9483SRichard Henderson dh_typemask(ptr, 2))
385c7ba9483SRichard Henderson };
386f86fd4d8SPierrick Bouvier assert((unsigned)flags < ARRAY_SIZE(info));
38754cb65d8SEmilio G. Cota
388c7ba9483SRichard Henderson struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
389f86fd4d8SPierrick Bouvier struct qemu_plugin_regular_cb regular_cb = { .f.vcpu_udata = cb,
390f86fd4d8SPierrick Bouvier .userp = udata,
391f86fd4d8SPierrick Bouvier .info = &info[flags] };
39254cb65d8SEmilio G. Cota dyn_cb->type = PLUGIN_CB_REGULAR;
393f86fd4d8SPierrick Bouvier dyn_cb->regular = regular_cb;
39454cb65d8SEmilio G. Cota }
39554cb65d8SEmilio G. Cota
plugin_register_dyn_cond_cb__udata(GArray ** arr,qemu_plugin_vcpu_udata_cb_t cb,enum qemu_plugin_cb_flags flags,enum qemu_plugin_cond cond,qemu_plugin_u64 entry,uint64_t imm,void * udata)3967de77d37SPierrick Bouvier void plugin_register_dyn_cond_cb__udata(GArray **arr,
3977de77d37SPierrick Bouvier qemu_plugin_vcpu_udata_cb_t cb,
3987de77d37SPierrick Bouvier enum qemu_plugin_cb_flags flags,
3997de77d37SPierrick Bouvier enum qemu_plugin_cond cond,
4007de77d37SPierrick Bouvier qemu_plugin_u64 entry,
4017de77d37SPierrick Bouvier uint64_t imm,
4027de77d37SPierrick Bouvier void *udata)
4037de77d37SPierrick Bouvier {
4047de77d37SPierrick Bouvier static TCGHelperInfo info[3] = {
4057de77d37SPierrick Bouvier [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
4067de77d37SPierrick Bouvier [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
4077de77d37SPierrick Bouvier /*
4087de77d37SPierrick Bouvier * Match qemu_plugin_vcpu_udata_cb_t:
4097de77d37SPierrick Bouvier * void (*)(uint32_t, void *)
4107de77d37SPierrick Bouvier */
4117de77d37SPierrick Bouvier [0 ... 2].typemask = (dh_typemask(void, 0) |
4127de77d37SPierrick Bouvier dh_typemask(i32, 1) |
4137de77d37SPierrick Bouvier dh_typemask(ptr, 2))
4147de77d37SPierrick Bouvier };
415f86fd4d8SPierrick Bouvier assert((unsigned)flags < ARRAY_SIZE(info));
4167de77d37SPierrick Bouvier
4177de77d37SPierrick Bouvier struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
418f86fd4d8SPierrick Bouvier struct qemu_plugin_conditional_cb cond_cb = { .userp = udata,
419f86fd4d8SPierrick Bouvier .f.vcpu_udata = cb,
420f86fd4d8SPierrick Bouvier .cond = cond,
421f86fd4d8SPierrick Bouvier .entry = entry,
422f86fd4d8SPierrick Bouvier .imm = imm,
423f86fd4d8SPierrick Bouvier .info = &info[flags] };
4247de77d37SPierrick Bouvier dyn_cb->type = PLUGIN_CB_COND;
425f86fd4d8SPierrick Bouvier dyn_cb->cond = cond_cb;
4267de77d37SPierrick Bouvier }
4277de77d37SPierrick Bouvier
plugin_register_vcpu_mem_cb(GArray ** arr,void * cb,enum qemu_plugin_cb_flags flags,enum qemu_plugin_mem_rw rw,void * udata)42854cb65d8SEmilio G. Cota void plugin_register_vcpu_mem_cb(GArray **arr,
42954cb65d8SEmilio G. Cota void *cb,
43054cb65d8SEmilio G. Cota enum qemu_plugin_cb_flags flags,
43154cb65d8SEmilio G. Cota enum qemu_plugin_mem_rw rw,
43254cb65d8SEmilio G. Cota void *udata)
43354cb65d8SEmilio G. Cota {
434c7ba9483SRichard Henderson /*
435c7ba9483SRichard Henderson * Expect that the underlying type for enum qemu_plugin_meminfo_t
436c7ba9483SRichard Henderson * is either int32_t or uint32_t, aka int or unsigned int.
437c7ba9483SRichard Henderson */
438c7ba9483SRichard Henderson QEMU_BUILD_BUG_ON(
439c7ba9483SRichard Henderson !__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t) &&
440c7ba9483SRichard Henderson !__builtin_types_compatible_p(qemu_plugin_meminfo_t, int32_t));
44154cb65d8SEmilio G. Cota
442c7ba9483SRichard Henderson static TCGHelperInfo info[3] = {
443b0748975SRichard Henderson [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
444b0748975SRichard Henderson [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
445c7ba9483SRichard Henderson /*
446c7ba9483SRichard Henderson * Match qemu_plugin_vcpu_mem_cb_t:
447c7ba9483SRichard Henderson * void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *)
448c7ba9483SRichard Henderson */
449c7ba9483SRichard Henderson [0 ... 2].typemask =
450c7ba9483SRichard Henderson (dh_typemask(void, 0) |
451c7ba9483SRichard Henderson dh_typemask(i32, 1) |
452c7ba9483SRichard Henderson (__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t)
453c7ba9483SRichard Henderson ? dh_typemask(i32, 2) : dh_typemask(s32, 2)) |
454c7ba9483SRichard Henderson dh_typemask(i64, 3) |
455c7ba9483SRichard Henderson dh_typemask(ptr, 4))
456c7ba9483SRichard Henderson };
457f86fd4d8SPierrick Bouvier assert((unsigned)flags < ARRAY_SIZE(info));
458c7ba9483SRichard Henderson
459c7ba9483SRichard Henderson struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
460f86fd4d8SPierrick Bouvier struct qemu_plugin_regular_cb regular_cb = { .userp = udata,
461f86fd4d8SPierrick Bouvier .rw = rw,
462f86fd4d8SPierrick Bouvier .f.vcpu_mem = cb,
463f86fd4d8SPierrick Bouvier .info = &info[flags] };
464ccd8f17eSRichard Henderson dyn_cb->type = PLUGIN_CB_MEM_REGULAR;
465f86fd4d8SPierrick Bouvier dyn_cb->regular = regular_cb;
46654cb65d8SEmilio G. Cota }
46754cb65d8SEmilio G. Cota
468c905a368SDaniele Buono /*
469c905a368SDaniele Buono * Disable CFI checks.
470c905a368SDaniele Buono * The callback function has been loaded from an external library so we do not
471c905a368SDaniele Buono * have type information
472c905a368SDaniele Buono */
473c905a368SDaniele Buono QEMU_DISABLE_CFI
qemu_plugin_tb_trans_cb(CPUState * cpu,struct qemu_plugin_tb * tb)47454cb65d8SEmilio G. Cota void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
47554cb65d8SEmilio G. Cota {
47654cb65d8SEmilio G. Cota struct qemu_plugin_cb *cb, *next;
47754cb65d8SEmilio G. Cota enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
47854cb65d8SEmilio G. Cota
479e096d370SPhilippe Mathieu-Daudé /* no plugin_state->event_mask check here; caller should have checked */
48054cb65d8SEmilio G. Cota
48154cb65d8SEmilio G. Cota QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
48254cb65d8SEmilio G. Cota qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
48354cb65d8SEmilio G. Cota
48454cb65d8SEmilio G. Cota func(cb->ctx->id, tb);
48554cb65d8SEmilio G. Cota }
48654cb65d8SEmilio G. Cota }
48754cb65d8SEmilio G. Cota
488c905a368SDaniele Buono /*
489c905a368SDaniele Buono * Disable CFI checks.
490c905a368SDaniele Buono * The callback function has been loaded from an external library so we do not
491c905a368SDaniele Buono * have type information
492c905a368SDaniele Buono */
493c905a368SDaniele Buono QEMU_DISABLE_CFI
49454cb65d8SEmilio G. Cota void
qemu_plugin_vcpu_syscall(CPUState * cpu,int64_t num,uint64_t a1,uint64_t a2,uint64_t a3,uint64_t a4,uint64_t a5,uint64_t a6,uint64_t a7,uint64_t a8)49554cb65d8SEmilio G. Cota qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
49654cb65d8SEmilio G. Cota uint64_t a3, uint64_t a4, uint64_t a5,
49754cb65d8SEmilio G. Cota uint64_t a6, uint64_t a7, uint64_t a8)
49854cb65d8SEmilio G. Cota {
49954cb65d8SEmilio G. Cota struct qemu_plugin_cb *cb, *next;
50054cb65d8SEmilio G. Cota enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
50154cb65d8SEmilio G. Cota
502c0061471SAlex Bennée if (!test_bit(ev, cpu->plugin_state->event_mask)) {
50354cb65d8SEmilio G. Cota return;
50454cb65d8SEmilio G. Cota }
50554cb65d8SEmilio G. Cota
50654cb65d8SEmilio G. Cota QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
50754cb65d8SEmilio G. Cota qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
50854cb65d8SEmilio G. Cota
50954cb65d8SEmilio G. Cota func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);
51054cb65d8SEmilio G. Cota }
51154cb65d8SEmilio G. Cota }
51254cb65d8SEmilio G. Cota
513c905a368SDaniele Buono /*
514c905a368SDaniele Buono * Disable CFI checks.
515c905a368SDaniele Buono * The callback function has been loaded from an external library so we do not
516c905a368SDaniele Buono * have type information
517c905a368SDaniele Buono */
518c905a368SDaniele Buono QEMU_DISABLE_CFI
qemu_plugin_vcpu_syscall_ret(CPUState * cpu,int64_t num,int64_t ret)51954cb65d8SEmilio G. Cota void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
52054cb65d8SEmilio G. Cota {
52154cb65d8SEmilio G. Cota struct qemu_plugin_cb *cb, *next;
52254cb65d8SEmilio G. Cota enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
52354cb65d8SEmilio G. Cota
524c0061471SAlex Bennée if (!test_bit(ev, cpu->plugin_state->event_mask)) {
52554cb65d8SEmilio G. Cota return;
52654cb65d8SEmilio G. Cota }
52754cb65d8SEmilio G. Cota
52854cb65d8SEmilio G. Cota QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
52954cb65d8SEmilio G. Cota qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret;
53054cb65d8SEmilio G. Cota
53154cb65d8SEmilio G. Cota func(cb->ctx->id, cpu->cpu_index, num, ret);
53254cb65d8SEmilio G. Cota }
53354cb65d8SEmilio G. Cota }
53454cb65d8SEmilio G. Cota
qemu_plugin_vcpu_idle_cb(CPUState * cpu)53554cb65d8SEmilio G. Cota void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
53654cb65d8SEmilio G. Cota {
537c490e681SPierrick Bouvier /* idle and resume cb may be called before init, ignore in this case */
538c490e681SPierrick Bouvier if (cpu->cpu_index < plugin.num_vcpus) {
53954cb65d8SEmilio G. Cota plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE);
54054cb65d8SEmilio G. Cota }
541c490e681SPierrick Bouvier }
54254cb65d8SEmilio G. Cota
qemu_plugin_vcpu_resume_cb(CPUState * cpu)54354cb65d8SEmilio G. Cota void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
54454cb65d8SEmilio G. Cota {
545c490e681SPierrick Bouvier if (cpu->cpu_index < plugin.num_vcpus) {
54654cb65d8SEmilio G. Cota plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
54754cb65d8SEmilio G. Cota }
548c490e681SPierrick Bouvier }
54954cb65d8SEmilio G. Cota
qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,qemu_plugin_vcpu_simple_cb_t cb)55054cb65d8SEmilio G. Cota void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
55154cb65d8SEmilio G. Cota qemu_plugin_vcpu_simple_cb_t cb)
55254cb65d8SEmilio G. Cota {
55354cb65d8SEmilio G. Cota plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
55454cb65d8SEmilio G. Cota }
55554cb65d8SEmilio G. Cota
qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,qemu_plugin_vcpu_simple_cb_t cb)55654cb65d8SEmilio G. Cota void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
55754cb65d8SEmilio G. Cota qemu_plugin_vcpu_simple_cb_t cb)
55854cb65d8SEmilio G. Cota {
55954cb65d8SEmilio G. Cota plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
56054cb65d8SEmilio G. Cota }
56154cb65d8SEmilio G. Cota
qemu_plugin_register_flush_cb(qemu_plugin_id_t id,qemu_plugin_simple_cb_t cb)56254cb65d8SEmilio G. Cota void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
56354cb65d8SEmilio G. Cota qemu_plugin_simple_cb_t cb)
56454cb65d8SEmilio G. Cota {
56554cb65d8SEmilio G. Cota plugin_register_cb(id, QEMU_PLUGIN_EV_FLUSH, cb);
56654cb65d8SEmilio G. Cota }
56754cb65d8SEmilio G. Cota
free_dyn_cb_arr(void * p,uint32_t h,void * userp)56854cb65d8SEmilio G. Cota static bool free_dyn_cb_arr(void *p, uint32_t h, void *userp)
56954cb65d8SEmilio G. Cota {
57054cb65d8SEmilio G. Cota g_array_free((GArray *) p, true);
57154cb65d8SEmilio G. Cota return true;
57254cb65d8SEmilio G. Cota }
57354cb65d8SEmilio G. Cota
qemu_plugin_flush_cb(void)57454cb65d8SEmilio G. Cota void qemu_plugin_flush_cb(void)
57554cb65d8SEmilio G. Cota {
57654cb65d8SEmilio G. Cota qht_iter_remove(&plugin.dyn_cb_arr_ht, free_dyn_cb_arr, NULL);
57754cb65d8SEmilio G. Cota qht_reset(&plugin.dyn_cb_arr_ht);
57854cb65d8SEmilio G. Cota
57954cb65d8SEmilio G. Cota plugin_cb__simple(QEMU_PLUGIN_EV_FLUSH);
58054cb65d8SEmilio G. Cota }
58154cb65d8SEmilio G. Cota
exec_inline_op(enum plugin_dyn_cb_type type,struct qemu_plugin_inline_cb * cb,int cpu_index)58209afe967SPierrick Bouvier void exec_inline_op(enum plugin_dyn_cb_type type,
58309afe967SPierrick Bouvier struct qemu_plugin_inline_cb *cb,
58409afe967SPierrick Bouvier int cpu_index)
58554cb65d8SEmilio G. Cota {
586f86fd4d8SPierrick Bouvier char *ptr = cb->entry.score->data->data;
5873077be25SPierrick Bouvier size_t elem_size = g_array_get_element_size(
588f86fd4d8SPierrick Bouvier cb->entry.score->data);
589f86fd4d8SPierrick Bouvier size_t offset = cb->entry.offset;
59062f92b8dSPierrick Bouvier uint64_t *val = (uint64_t *)(ptr + offset + cpu_index * elem_size);
59154cb65d8SEmilio G. Cota
59209afe967SPierrick Bouvier switch (type) {
59309afe967SPierrick Bouvier case PLUGIN_CB_INLINE_ADD_U64:
594f86fd4d8SPierrick Bouvier *val += cb->imm;
59554cb65d8SEmilio G. Cota break;
59609afe967SPierrick Bouvier case PLUGIN_CB_INLINE_STORE_U64:
597f86fd4d8SPierrick Bouvier *val = cb->imm;
59836a1d8e7SPierrick Bouvier break;
59954cb65d8SEmilio G. Cota default:
60054cb65d8SEmilio G. Cota g_assert_not_reached();
60154cb65d8SEmilio G. Cota }
60254cb65d8SEmilio G. Cota }
60354cb65d8SEmilio G. Cota
qemu_plugin_vcpu_mem_cb(CPUState * cpu,uint64_t vaddr,uint64_t value_low,uint64_t value_high,MemOpIdx oi,enum qemu_plugin_mem_rw rw)60437aff087SRichard Henderson void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
605*b709da5dSPierrick Bouvier uint64_t value_low,
606*b709da5dSPierrick Bouvier uint64_t value_high,
60737aff087SRichard Henderson MemOpIdx oi, enum qemu_plugin_mem_rw rw)
60854cb65d8SEmilio G. Cota {
60980f034c5SPhilippe Mathieu-Daudé GArray *arr = cpu->neg.plugin_mem_cbs;
61054cb65d8SEmilio G. Cota size_t i;
61154cb65d8SEmilio G. Cota
61254cb65d8SEmilio G. Cota if (arr == NULL) {
61354cb65d8SEmilio G. Cota return;
61454cb65d8SEmilio G. Cota }
615*b709da5dSPierrick Bouvier
616*b709da5dSPierrick Bouvier cpu->neg.plugin_mem_value_low = value_low;
617*b709da5dSPierrick Bouvier cpu->neg.plugin_mem_value_high = value_high;
618*b709da5dSPierrick Bouvier
61954cb65d8SEmilio G. Cota for (i = 0; i < arr->len; i++) {
62054cb65d8SEmilio G. Cota struct qemu_plugin_dyn_cb *cb =
62154cb65d8SEmilio G. Cota &g_array_index(arr, struct qemu_plugin_dyn_cb, i);
62254cb65d8SEmilio G. Cota
62354cb65d8SEmilio G. Cota switch (cb->type) {
624ccd8f17eSRichard Henderson case PLUGIN_CB_MEM_REGULAR:
625ca7d7f42SPierrick Bouvier if (rw & cb->regular.rw) {
626f86fd4d8SPierrick Bouvier cb->regular.f.vcpu_mem(cpu->cpu_index,
627f86fd4d8SPierrick Bouvier make_plugin_meminfo(oi, rw),
628f86fd4d8SPierrick Bouvier vaddr, cb->regular.userp);
629f86fd4d8SPierrick Bouvier }
63054cb65d8SEmilio G. Cota break;
63121032784SPierrick Bouvier case PLUGIN_CB_INLINE_ADD_U64:
63236a1d8e7SPierrick Bouvier case PLUGIN_CB_INLINE_STORE_U64:
633ca7d7f42SPierrick Bouvier if (rw & cb->inline_insn.rw) {
63409afe967SPierrick Bouvier exec_inline_op(cb->type, &cb->inline_insn, cpu->cpu_index);
635f86fd4d8SPierrick Bouvier }
63654cb65d8SEmilio G. Cota break;
63754cb65d8SEmilio G. Cota default:
63854cb65d8SEmilio G. Cota g_assert_not_reached();
63954cb65d8SEmilio G. Cota }
64054cb65d8SEmilio G. Cota }
64154cb65d8SEmilio G. Cota }
64254cb65d8SEmilio G. Cota
qemu_plugin_atexit_cb(void)64354cb65d8SEmilio G. Cota void qemu_plugin_atexit_cb(void)
64454cb65d8SEmilio G. Cota {
64554cb65d8SEmilio G. Cota plugin_cb__udata(QEMU_PLUGIN_EV_ATEXIT);
64654cb65d8SEmilio G. Cota }
64754cb65d8SEmilio G. Cota
qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,qemu_plugin_udata_cb_t cb,void * udata)64854cb65d8SEmilio G. Cota void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
64954cb65d8SEmilio G. Cota qemu_plugin_udata_cb_t cb,
65054cb65d8SEmilio G. Cota void *udata)
65154cb65d8SEmilio G. Cota {
65254cb65d8SEmilio G. Cota plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
65354cb65d8SEmilio G. Cota }
65454cb65d8SEmilio G. Cota
65554cb65d8SEmilio G. Cota /*
656f7e68c9cSAlex Bennée * Handle exit from linux-user. Unlike the normal atexit() mechanism
657f7e68c9cSAlex Bennée * we need to handle the clean-up manually as it's possible threads
658f7e68c9cSAlex Bennée * are still running. We need to remove all callbacks from code
659f7e68c9cSAlex Bennée * generation, flush the current translations and then we can safely
660f7e68c9cSAlex Bennée * trigger the exit callbacks.
661f7e68c9cSAlex Bennée */
662f7e68c9cSAlex Bennée
qemu_plugin_user_exit(void)663f7e68c9cSAlex Bennée void qemu_plugin_user_exit(void)
664f7e68c9cSAlex Bennée {
665f7e68c9cSAlex Bennée enum qemu_plugin_event ev;
666f7e68c9cSAlex Bennée CPUState *cpu;
667f7e68c9cSAlex Bennée
6682bbbc1beSEmilio Cota /*
6692bbbc1beSEmilio Cota * Locking order: we must acquire locks in an order that is consistent
6702bbbc1beSEmilio Cota * with the one in fork_start(). That is:
6712bbbc1beSEmilio Cota * - start_exclusive(), which acquires qemu_cpu_list_lock,
6722bbbc1beSEmilio Cota * must be called before acquiring plugin.lock.
6732bbbc1beSEmilio Cota * - tb_flush(), which acquires mmap_lock(), must be called
6742bbbc1beSEmilio Cota * while plugin.lock is not held.
6752bbbc1beSEmilio Cota */
676f7e68c9cSAlex Bennée start_exclusive();
677f7e68c9cSAlex Bennée
6782bbbc1beSEmilio Cota qemu_rec_mutex_lock(&plugin.lock);
679f7e68c9cSAlex Bennée /* un-register all callbacks except the final AT_EXIT one */
680f7e68c9cSAlex Bennée for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
681f7e68c9cSAlex Bennée if (ev != QEMU_PLUGIN_EV_ATEXIT) {
682f4554923SRichard Henderson struct qemu_plugin_cb *cb, *next;
683f4554923SRichard Henderson
684f4554923SRichard Henderson QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
685f4554923SRichard Henderson plugin_unregister_cb__locked(cb->ctx, ev);
686f7e68c9cSAlex Bennée }
687f7e68c9cSAlex Bennée }
688f7e68c9cSAlex Bennée }
689f7e68c9cSAlex Bennée CPU_FOREACH(cpu) {
690f7e68c9cSAlex Bennée qemu_plugin_disable_mem_helpers(cpu);
691f7e68c9cSAlex Bennée }
6922bbbc1beSEmilio Cota qemu_rec_mutex_unlock(&plugin.lock);
693f7e68c9cSAlex Bennée
6942bbbc1beSEmilio Cota tb_flush(current_cpu);
695f7e68c9cSAlex Bennée end_exclusive();
696f7e68c9cSAlex Bennée
697f7e68c9cSAlex Bennée /* now it's safe to handle the exit case */
698f7e68c9cSAlex Bennée qemu_plugin_atexit_cb();
699f7e68c9cSAlex Bennée }
700f7e68c9cSAlex Bennée
701f7e68c9cSAlex Bennée /*
702f7e15affSAlex Bennée * Helpers for *-user to ensure locks are sane across fork() events.
703f7e15affSAlex Bennée */
704f7e15affSAlex Bennée
qemu_plugin_user_prefork_lock(void)705f7e15affSAlex Bennée void qemu_plugin_user_prefork_lock(void)
706f7e15affSAlex Bennée {
707f7e15affSAlex Bennée qemu_rec_mutex_lock(&plugin.lock);
708f7e15affSAlex Bennée }
709f7e15affSAlex Bennée
qemu_plugin_user_postfork(bool is_child)710f7e15affSAlex Bennée void qemu_plugin_user_postfork(bool is_child)
711f7e15affSAlex Bennée {
712f7e15affSAlex Bennée if (is_child) {
713f7e15affSAlex Bennée /* should we just reset via plugin_init? */
714f7e15affSAlex Bennée qemu_rec_mutex_init(&plugin.lock);
715f7e15affSAlex Bennée } else {
716f7e15affSAlex Bennée qemu_rec_mutex_unlock(&plugin.lock);
717f7e15affSAlex Bennée }
718f7e15affSAlex Bennée }
719f7e15affSAlex Bennée
plugin_dyn_cb_arr_cmp(const void * ap,const void * bp)72054cb65d8SEmilio G. Cota static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp)
72154cb65d8SEmilio G. Cota {
72254cb65d8SEmilio G. Cota return ap == bp;
72354cb65d8SEmilio G. Cota }
72454cb65d8SEmilio G. Cota
plugin_init(void)72554cb65d8SEmilio G. Cota static void __attribute__((__constructor__)) plugin_init(void)
72654cb65d8SEmilio G. Cota {
72754cb65d8SEmilio G. Cota int i;
72854cb65d8SEmilio G. Cota
72954cb65d8SEmilio G. Cota for (i = 0; i < QEMU_PLUGIN_EV_MAX; i++) {
73054cb65d8SEmilio G. Cota QLIST_INIT(&plugin.cb_lists[i]);
73154cb65d8SEmilio G. Cota }
73254cb65d8SEmilio G. Cota qemu_rec_mutex_init(&plugin.lock);
73354cb65d8SEmilio G. Cota plugin.id_ht = g_hash_table_new(g_int64_hash, g_int64_equal);
73454cb65d8SEmilio G. Cota plugin.cpu_ht = g_hash_table_new(g_int_hash, g_int_equal);
735a3c2cf0bSPierrick Bouvier QLIST_INIT(&plugin.scoreboards);
736a3c2cf0bSPierrick Bouvier plugin.scoreboard_alloc_size = 16; /* avoid frequent reallocation */
73754cb65d8SEmilio G. Cota QTAILQ_INIT(&plugin.ctxs);
73854cb65d8SEmilio G. Cota qht_init(&plugin.dyn_cb_arr_ht, plugin_dyn_cb_arr_cmp, 16,
73954cb65d8SEmilio G. Cota QHT_MODE_AUTO_RESIZE);
74054cb65d8SEmilio G. Cota atexit(qemu_plugin_atexit_cb);
74154cb65d8SEmilio G. Cota }
7424a448b14SPierrick Bouvier
plugin_num_vcpus(void)7434a448b14SPierrick Bouvier int plugin_num_vcpus(void)
7444a448b14SPierrick Bouvier {
7454a448b14SPierrick Bouvier return plugin.num_vcpus;
7464a448b14SPierrick Bouvier }
747a3c2cf0bSPierrick Bouvier
plugin_scoreboard_new(size_t element_size)748a3c2cf0bSPierrick Bouvier struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size)
749a3c2cf0bSPierrick Bouvier {
750a3c2cf0bSPierrick Bouvier struct qemu_plugin_scoreboard *score =
751a3c2cf0bSPierrick Bouvier g_malloc0(sizeof(struct qemu_plugin_scoreboard));
752a3c2cf0bSPierrick Bouvier score->data = g_array_new(FALSE, TRUE, element_size);
753a3c2cf0bSPierrick Bouvier g_array_set_size(score->data, plugin.scoreboard_alloc_size);
754a3c2cf0bSPierrick Bouvier
755a3c2cf0bSPierrick Bouvier qemu_rec_mutex_lock(&plugin.lock);
756a3c2cf0bSPierrick Bouvier QLIST_INSERT_HEAD(&plugin.scoreboards, score, entry);
757a3c2cf0bSPierrick Bouvier qemu_rec_mutex_unlock(&plugin.lock);
758a3c2cf0bSPierrick Bouvier
759a3c2cf0bSPierrick Bouvier return score;
760a3c2cf0bSPierrick Bouvier }
761a3c2cf0bSPierrick Bouvier
plugin_scoreboard_free(struct qemu_plugin_scoreboard * score)762a3c2cf0bSPierrick Bouvier void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
763a3c2cf0bSPierrick Bouvier {
764a3c2cf0bSPierrick Bouvier qemu_rec_mutex_lock(&plugin.lock);
765a3c2cf0bSPierrick Bouvier QLIST_REMOVE(score, entry);
766a3c2cf0bSPierrick Bouvier qemu_rec_mutex_unlock(&plugin.lock);
767a3c2cf0bSPierrick Bouvier
768a3c2cf0bSPierrick Bouvier g_array_free(score->data, TRUE);
769a3c2cf0bSPierrick Bouvier g_free(score);
770a3c2cf0bSPierrick Bouvier }
771