2 * QEMU Plugin Core Loader Code
4 * This is the code responsible for loading and unloading the plugins.
5 * Aside from the basic housekeeping tasks we also need to ensure any
6 * generated code is flushed when we remove a plugin so we cannot end
7 * up calling and unloaded helper function.
10 * Copyright (C) 2019, Linaro
12 * License: GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
15 * SPDX-License-Identifier: GPL-2.0-or-later
18 #include "qemu/osdep.h"
19 #include "qemu/error-report.h"
20 #include "qemu/config-file.h"
21 #include "qapi/error.h"
22 #include "qemu/option.h"
23 #include "qemu/rcu_queue.h"
25 #include "qemu/bitmap.h"
26 #include "qemu/xxhash.h"
27 #include "qemu/plugin.h"
28 #include "hw/core/cpu.h"
30 #include "exec/exec-all.h"
34 * For convenience we use a bitmap for plugin.mask, but really all we need is a
35 * u32, which is what we store in TranslationBlock.
37 QEMU_BUILD_BUG_ON(QEMU_PLUGIN_EV_MAX > 32);
39 struct qemu_plugin_desc {
42 QTAILQ_ENTRY(qemu_plugin_desc) entry;
46 struct qemu_plugin_parse_arg {
48 struct qemu_plugin_desc *curr;
51 QemuOptsList qemu_plugin_opts = {
53 .implied_opt_name = "file",
54 .head = QTAILQ_HEAD_INITIALIZER(qemu_plugin_opts.head),
56 /* do our own parsing to support multiple plugins */
61 typedef int (*qemu_plugin_install_func_t)(qemu_plugin_id_t, int, char **);
63 extern struct qemu_plugin_state plugin;
65 void qemu_plugin_add_dyn_cb_arr(GArray *arr)
67 uint32_t hash = qemu_xxhash2((uint64_t)(uintptr_t)arr);
70 inserted = qht_insert(&plugin.dyn_cb_arr_ht, arr, hash, NULL);
74 static struct qemu_plugin_desc *plugin_find_desc(QemuPluginList *head,
77 struct qemu_plugin_desc *desc;
79 QTAILQ_FOREACH(desc, head, entry) {
80 if (strcmp(desc->path, path) == 0) {
87 static int plugin_add(void *opaque, const char *name, const char *value,
90 struct qemu_plugin_parse_arg *arg = opaque;
91 struct qemu_plugin_desc *p;
93 if (strcmp(name, "file") == 0) {
94 if (strcmp(value, "") == 0) {
95 error_setg(errp, "requires a non-empty argument");
98 p = plugin_find_desc(arg->head, value);
100 p = g_new0(struct qemu_plugin_desc, 1);
101 p->path = g_strdup(value);
102 QTAILQ_INSERT_TAIL(arg->head, p, entry);
105 } else if (strcmp(name, "arg") == 0) {
106 if (arg->curr == NULL) {
107 error_setg(errp, "missing earlier '-plugin file=' option");
112 p->argv = g_realloc_n(p->argv, p->argc, sizeof(char *));
113 p->argv[p->argc - 1] = g_strdup(value);
115 error_setg(errp, "-plugin: unexpected parameter '%s'; ignored", name);
120 void qemu_plugin_opt_parse(const char *optarg, QemuPluginList *head)
122 struct qemu_plugin_parse_arg arg;
125 opts = qemu_opts_parse_noisily(qemu_find_opts("plugin"), optarg, true);
131 qemu_opt_foreach(opts, plugin_add, &arg, &error_fatal);
136 * From: https://en.wikipedia.org/wiki/Xorshift
137 * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
138 * guaranteed to be >= INT_MAX).
140 static uint64_t xorshift64star(uint64_t x)
142 x ^= x >> 12; /* a */
143 x ^= x << 25; /* b */
144 x ^= x >> 27; /* c */
145 return x * UINT64_C(2685821657736338717);
148 static int plugin_load(struct qemu_plugin_desc *desc)
150 qemu_plugin_install_func_t install;
151 struct qemu_plugin_ctx *ctx;
155 ctx = qemu_memalign(qemu_dcache_linesize, sizeof(*ctx));
156 memset(ctx, 0, sizeof(*ctx));
159 ctx->handle = g_module_open(desc->path, G_MODULE_BIND_LOCAL);
160 if (ctx->handle == NULL) {
161 error_report("%s: %s", __func__, g_module_error());
165 if (!g_module_symbol(ctx->handle, "qemu_plugin_install", &sym)) {
166 error_report("%s: %s", __func__, g_module_error());
169 install = (qemu_plugin_install_func_t) sym;
170 /* symbol was found; it could be NULL though */
171 if (install == NULL) {
172 error_report("%s: %s: qemu_plugin_install is NULL",
173 __func__, desc->path);
177 qemu_rec_mutex_lock(&plugin.lock);
179 /* find an unused random id with &ctx as the seed */
180 ctx->id = (uint64_t)(uintptr_t)ctx;
184 ctx->id = xorshift64star(ctx->id);
185 existing = g_hash_table_lookup(plugin.id_ht, &ctx->id);
186 if (likely(existing == NULL)) {
189 success = g_hash_table_insert(plugin.id_ht, &ctx->id, &ctx->id);
194 QTAILQ_INSERT_TAIL(&plugin.ctxs, ctx, entry);
195 ctx->installing = true;
196 rc = install(ctx->id, desc->argc, desc->argv);
197 ctx->installing = false;
199 error_report("%s: qemu_plugin_install returned error code %d",
202 * we cannot rely on the plugin doing its own cleanup, so
203 * call a full uninstall if the plugin did not yet call it.
205 if (!ctx->uninstalling) {
206 plugin_reset_uninstall(ctx->id, NULL, false);
210 qemu_rec_mutex_unlock(&plugin.lock);
219 /* call after having removed @desc from the list */
220 static void plugin_desc_free(struct qemu_plugin_desc *desc)
224 for (i = 0; i < desc->argc; i++) {
225 g_free(desc->argv[i]);
233 * qemu_plugin_load_list - load a list of plugins
234 * @head: head of the list of descriptors of the plugins to be loaded
236 * Returns 0 if all plugins in the list are installed, !0 otherwise.
238 * Note: the descriptor of each successfully installed plugin is removed
239 * from the list given by @head.
241 int qemu_plugin_load_list(QemuPluginList *head)
243 struct qemu_plugin_desc *desc, *next;
245 QTAILQ_FOREACH_SAFE(desc, head, entry, next) {
248 err = plugin_load(desc);
252 QTAILQ_REMOVE(head, desc, entry);
257 struct qemu_plugin_reset_data {
258 struct qemu_plugin_ctx *ctx;
259 qemu_plugin_simple_cb_t cb;
263 static void plugin_reset_destroy__locked(struct qemu_plugin_reset_data *data)
265 struct qemu_plugin_ctx *ctx = data->ctx;
266 enum qemu_plugin_event ev;
270 * After updating the subscription lists there is no need to wait for an RCU
271 * grace period to elapse, because right now we either are in a "safe async"
272 * work environment (i.e. all vCPUs are asleep), or no vCPUs have yet been
275 for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
276 plugin_unregister_cb__locked(ctx, ev);
280 g_assert(ctx->resetting);
284 ctx->resetting = false;
289 g_assert(ctx->uninstalling);
290 /* we cannot dlclose if we are going to return to plugin code */
291 if (ctx->installing) {
292 error_report("Calling qemu_plugin_uninstall from the install function "
293 "is a bug. Instead, return !0 from the install function.");
297 success = g_hash_table_remove(plugin.id_ht, &ctx->id);
299 QTAILQ_REMOVE(&plugin.ctxs, ctx, entry);
303 if (!g_module_close(ctx->handle)) {
304 warn_report("%s: %s", __func__, g_module_error());
306 plugin_desc_free(ctx->desc);
311 static void plugin_reset_destroy(struct qemu_plugin_reset_data *data)
313 qemu_rec_mutex_lock(&plugin.lock);
314 plugin_reset_destroy__locked(data);
315 qemu_rec_mutex_lock(&plugin.lock);
318 static void plugin_flush_destroy(CPUState *cpu, run_on_cpu_data arg)
320 struct qemu_plugin_reset_data *data = arg.host_ptr;
322 g_assert(cpu_in_exclusive_context(cpu));
324 plugin_reset_destroy(data);
327 void plugin_reset_uninstall(qemu_plugin_id_t id,
328 qemu_plugin_simple_cb_t cb,
331 struct qemu_plugin_reset_data *data;
332 struct qemu_plugin_ctx *ctx;
334 qemu_rec_mutex_lock(&plugin.lock);
335 ctx = plugin_id_to_ctx_locked(id);
336 if (ctx->uninstalling || (reset && ctx->resetting)) {
337 qemu_rec_mutex_unlock(&plugin.lock);
340 ctx->resetting = reset;
341 ctx->uninstalling = !reset;
342 qemu_rec_mutex_unlock(&plugin.lock);
344 data = g_new(struct qemu_plugin_reset_data, 1);
349 * Only flush the code cache if the vCPUs have been created. If so,
350 * current_cpu must be non-NULL.
353 async_safe_run_on_cpu(current_cpu, plugin_flush_destroy,
354 RUN_ON_CPU_HOST_PTR(data));
357 * If current_cpu isn't set, then we don't have yet any vCPU threads
358 * and we therefore can remove the callbacks synchronously.
360 plugin_reset_destroy(data);