1 /* 2 * QEMU Plugin Core Loader Code 3 * 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. 8 * 9 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org> 10 * Copyright (C) 2019, Linaro 11 * 12 * License: GNU GPL, version 2 or later. 13 * See the COPYING file in the top-level directory. 14 * 15 * SPDX-License-Identifier: GPL-2.0-or-later 16 */ 17 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" 24 #include "qemu/qht.h" 25 #include "qemu/bitmap.h" 26 #include "qemu/xxhash.h" 27 #include "qemu/plugin.h" 28 #include "hw/core/cpu.h" 29 #include "cpu.h" 30 #include "exec/exec-all.h" 31 #include "plugin.h" 32 33 /* 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. 36 */ 37 QEMU_BUILD_BUG_ON(QEMU_PLUGIN_EV_MAX > 32); 38 39 struct qemu_plugin_desc { 40 char *path; 41 char **argv; 42 QTAILQ_ENTRY(qemu_plugin_desc) entry; 43 int argc; 44 }; 45 46 struct qemu_plugin_parse_arg { 47 QemuPluginList *head; 48 struct qemu_plugin_desc *curr; 49 }; 50 51 QemuOptsList qemu_plugin_opts = { 52 .name = "plugin", 53 .implied_opt_name = "file", 54 .head = QTAILQ_HEAD_INITIALIZER(qemu_plugin_opts.head), 55 .desc = { 56 /* do our own parsing to support multiple plugins */ 57 { /* end of list */ } 58 }, 59 }; 60 61 typedef int (*qemu_plugin_install_func_t)(qemu_plugin_id_t, int, char **); 62 63 extern struct qemu_plugin_state plugin; 64 65 void qemu_plugin_add_dyn_cb_arr(GArray *arr) 66 { 67 uint32_t hash = qemu_xxhash2((uint64_t)(uintptr_t)arr); 68 bool inserted; 69 70 inserted = qht_insert(&plugin.dyn_cb_arr_ht, arr, hash, NULL); 71 g_assert(inserted); 72 } 73 74 static struct qemu_plugin_desc *plugin_find_desc(QemuPluginList *head, 75 const char *path) 76 { 77 struct qemu_plugin_desc *desc; 78 79 QTAILQ_FOREACH(desc, head, entry) { 80 if (strcmp(desc->path, path) == 0) { 81 return desc; 82 } 83 } 84 return NULL; 85 } 86 87 static int plugin_add(void *opaque, const char *name, const char *value, 88 Error **errp) 89 { 90 struct qemu_plugin_parse_arg *arg = opaque; 91 struct qemu_plugin_desc *p; 92 93 if (strcmp(name, "file") == 0) { 94 if (strcmp(value, "") == 0) { 95 error_setg(errp, "requires a non-empty argument"); 96 return 1; 97 } 98 p = plugin_find_desc(arg->head, value); 99 if (p == NULL) { 100 p = g_new0(struct qemu_plugin_desc, 1); 101 p->path = g_strdup(value); 102 QTAILQ_INSERT_TAIL(arg->head, p, entry); 103 } 104 arg->curr = p; 105 } else if (strcmp(name, "arg") == 0) { 106 if (arg->curr == NULL) { 107 error_setg(errp, "missing earlier '-plugin file=' option"); 108 return 1; 109 } 110 p = arg->curr; 111 p->argc++; 112 p->argv = g_realloc_n(p->argv, p->argc, sizeof(char *)); 113 p->argv[p->argc - 1] = g_strdup(value); 114 } else { 115 error_setg(errp, "-plugin: unexpected parameter '%s'; ignored", name); 116 } 117 return 0; 118 } 119 120 void qemu_plugin_opt_parse(const char *optarg, QemuPluginList *head) 121 { 122 struct qemu_plugin_parse_arg arg; 123 QemuOpts *opts; 124 125 opts = qemu_opts_parse_noisily(qemu_find_opts("plugin"), optarg, true); 126 if (opts == NULL) { 127 exit(1); 128 } 129 arg.head = head; 130 arg.curr = NULL; 131 qemu_opt_foreach(opts, plugin_add, &arg, &error_fatal); 132 qemu_opts_del(opts); 133 } 134 135 /* 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). 139 */ 140 static uint64_t xorshift64star(uint64_t x) 141 { 142 x ^= x >> 12; /* a */ 143 x ^= x << 25; /* b */ 144 x ^= x >> 27; /* c */ 145 return x * UINT64_C(2685821657736338717); 146 } 147 148 static int plugin_load(struct qemu_plugin_desc *desc) 149 { 150 qemu_plugin_install_func_t install; 151 struct qemu_plugin_ctx *ctx; 152 gpointer sym; 153 int rc; 154 155 ctx = qemu_memalign(qemu_dcache_linesize, sizeof(*ctx)); 156 memset(ctx, 0, sizeof(*ctx)); 157 ctx->desc = desc; 158 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()); 162 goto err_dlopen; 163 } 164 165 if (!g_module_symbol(ctx->handle, "qemu_plugin_install", &sym)) { 166 error_report("%s: %s", __func__, g_module_error()); 167 goto err_symbol; 168 } 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); 174 goto err_symbol; 175 } 176 177 qemu_rec_mutex_lock(&plugin.lock); 178 179 /* find an unused random id with &ctx as the seed */ 180 ctx->id = (uint64_t)(uintptr_t)ctx; 181 for (;;) { 182 void *existing; 183 184 ctx->id = xorshift64star(ctx->id); 185 existing = g_hash_table_lookup(plugin.id_ht, &ctx->id); 186 if (likely(existing == NULL)) { 187 bool success; 188 189 success = g_hash_table_insert(plugin.id_ht, &ctx->id, &ctx->id); 190 g_assert(success); 191 break; 192 } 193 } 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; 198 if (rc) { 199 error_report("%s: qemu_plugin_install returned error code %d", 200 __func__, rc); 201 /* 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. 204 */ 205 if (!ctx->uninstalling) { 206 plugin_reset_uninstall(ctx->id, NULL, false); 207 } 208 } 209 210 qemu_rec_mutex_unlock(&plugin.lock); 211 return rc; 212 213 err_symbol: 214 err_dlopen: 215 qemu_vfree(ctx); 216 return 1; 217 } 218 219 /* call after having removed @desc from the list */ 220 static void plugin_desc_free(struct qemu_plugin_desc *desc) 221 { 222 int i; 223 224 for (i = 0; i < desc->argc; i++) { 225 g_free(desc->argv[i]); 226 } 227 g_free(desc->argv); 228 g_free(desc->path); 229 g_free(desc); 230 } 231 232 /** 233 * qemu_plugin_load_list - load a list of plugins 234 * @head: head of the list of descriptors of the plugins to be loaded 235 * 236 * Returns 0 if all plugins in the list are installed, !0 otherwise. 237 * 238 * Note: the descriptor of each successfully installed plugin is removed 239 * from the list given by @head. 240 */ 241 int qemu_plugin_load_list(QemuPluginList *head) 242 { 243 struct qemu_plugin_desc *desc, *next; 244 245 QTAILQ_FOREACH_SAFE(desc, head, entry, next) { 246 int err; 247 248 err = plugin_load(desc); 249 if (err) { 250 return err; 251 } 252 QTAILQ_REMOVE(head, desc, entry); 253 } 254 return 0; 255 } 256 257 struct qemu_plugin_reset_data { 258 struct qemu_plugin_ctx *ctx; 259 qemu_plugin_simple_cb_t cb; 260 bool reset; 261 }; 262 263 static void plugin_reset_destroy__locked(struct qemu_plugin_reset_data *data) 264 { 265 struct qemu_plugin_ctx *ctx = data->ctx; 266 enum qemu_plugin_event ev; 267 bool success; 268 269 /* 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 273 * created. 274 */ 275 for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) { 276 plugin_unregister_cb__locked(ctx, ev); 277 } 278 279 if (data->reset) { 280 g_assert(ctx->resetting); 281 if (data->cb) { 282 data->cb(ctx->id); 283 } 284 ctx->resetting = false; 285 g_free(data); 286 return; 287 } 288 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."); 294 abort(); 295 } 296 297 success = g_hash_table_remove(plugin.id_ht, &ctx->id); 298 g_assert(success); 299 QTAILQ_REMOVE(&plugin.ctxs, ctx, entry); 300 if (data->cb) { 301 data->cb(ctx->id); 302 } 303 if (!g_module_close(ctx->handle)) { 304 warn_report("%s: %s", __func__, g_module_error()); 305 } 306 plugin_desc_free(ctx->desc); 307 qemu_vfree(ctx); 308 g_free(data); 309 } 310 311 static void plugin_reset_destroy(struct qemu_plugin_reset_data *data) 312 { 313 qemu_rec_mutex_lock(&plugin.lock); 314 plugin_reset_destroy__locked(data); 315 qemu_rec_mutex_lock(&plugin.lock); 316 } 317 318 static void plugin_flush_destroy(CPUState *cpu, run_on_cpu_data arg) 319 { 320 struct qemu_plugin_reset_data *data = arg.host_ptr; 321 322 g_assert(cpu_in_exclusive_context(cpu)); 323 tb_flush(cpu); 324 plugin_reset_destroy(data); 325 } 326 327 void plugin_reset_uninstall(qemu_plugin_id_t id, 328 qemu_plugin_simple_cb_t cb, 329 bool reset) 330 { 331 struct qemu_plugin_reset_data *data; 332 struct qemu_plugin_ctx *ctx; 333 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); 338 return; 339 } 340 ctx->resetting = reset; 341 ctx->uninstalling = !reset; 342 qemu_rec_mutex_unlock(&plugin.lock); 343 344 data = g_new(struct qemu_plugin_reset_data, 1); 345 data->ctx = ctx; 346 data->cb = cb; 347 data->reset = reset; 348 /* 349 * Only flush the code cache if the vCPUs have been created. If so, 350 * current_cpu must be non-NULL. 351 */ 352 if (current_cpu) { 353 async_safe_run_on_cpu(current_cpu, plugin_flush_destroy, 354 RUN_ON_CPU_HOST_PTR(data)); 355 } else { 356 /* 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. 359 */ 360 plugin_reset_destroy(data); 361 } 362 } 363