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/lockable.h" 23 #include "qemu/option.h" 24 #include "qemu/rcu_queue.h" 25 #include "qemu/qht.h" 26 #include "qemu/bitmap.h" 27 #include "qemu/xxhash.h" 28 #include "qemu/plugin.h" 29 #include "hw/core/cpu.h" 30 #include "cpu.h" 31 #include "exec/exec-all.h" 32 #ifndef CONFIG_USER_ONLY 33 #include "hw/boards.h" 34 #endif 35 #include "qemu/compiler.h" 36 37 #include "plugin.h" 38 39 /* 40 * For convenience we use a bitmap for plugin.mask, but really all we need is a 41 * u32, which is what we store in TranslationBlock. 42 */ 43 QEMU_BUILD_BUG_ON(QEMU_PLUGIN_EV_MAX > 32); 44 45 struct qemu_plugin_desc { 46 char *path; 47 char **argv; 48 QTAILQ_ENTRY(qemu_plugin_desc) entry; 49 int argc; 50 }; 51 52 struct qemu_plugin_parse_arg { 53 QemuPluginList *head; 54 struct qemu_plugin_desc *curr; 55 }; 56 57 QemuOptsList qemu_plugin_opts = { 58 .name = "plugin", 59 .implied_opt_name = "file", 60 .head = QTAILQ_HEAD_INITIALIZER(qemu_plugin_opts.head), 61 .desc = { 62 /* do our own parsing to support multiple plugins */ 63 { /* end of list */ } 64 }, 65 }; 66 67 typedef int (*qemu_plugin_install_func_t)(qemu_plugin_id_t, const qemu_info_t *, int, char **); 68 69 extern struct qemu_plugin_state plugin; 70 71 void qemu_plugin_add_dyn_cb_arr(GArray *arr) 72 { 73 uint32_t hash = qemu_xxhash2((uint64_t)(uintptr_t)arr); 74 bool inserted; 75 76 inserted = qht_insert(&plugin.dyn_cb_arr_ht, arr, hash, NULL); 77 g_assert(inserted); 78 } 79 80 static struct qemu_plugin_desc *plugin_find_desc(QemuPluginList *head, 81 const char *path) 82 { 83 struct qemu_plugin_desc *desc; 84 85 QTAILQ_FOREACH(desc, head, entry) { 86 if (strcmp(desc->path, path) == 0) { 87 return desc; 88 } 89 } 90 return NULL; 91 } 92 93 static int plugin_add(void *opaque, const char *name, const char *value, 94 Error **errp) 95 { 96 struct qemu_plugin_parse_arg *arg = opaque; 97 struct qemu_plugin_desc *p; 98 99 if (strcmp(name, "file") == 0) { 100 if (strcmp(value, "") == 0) { 101 error_setg(errp, "requires a non-empty argument"); 102 return 1; 103 } 104 p = plugin_find_desc(arg->head, value); 105 if (p == NULL) { 106 p = g_new0(struct qemu_plugin_desc, 1); 107 p->path = g_strdup(value); 108 QTAILQ_INSERT_TAIL(arg->head, p, entry); 109 } 110 arg->curr = p; 111 } else if (strcmp(name, "arg") == 0) { 112 if (arg->curr == NULL) { 113 error_setg(errp, "missing earlier '-plugin file=' option"); 114 return 1; 115 } 116 p = arg->curr; 117 p->argc++; 118 p->argv = g_realloc_n(p->argv, p->argc, sizeof(char *)); 119 p->argv[p->argc - 1] = g_strdup(value); 120 } else { 121 error_setg(errp, "-plugin: unexpected parameter '%s'; ignored", name); 122 } 123 return 0; 124 } 125 126 void qemu_plugin_opt_parse(const char *optarg, QemuPluginList *head) 127 { 128 struct qemu_plugin_parse_arg arg; 129 QemuOpts *opts; 130 131 opts = qemu_opts_parse_noisily(qemu_find_opts("plugin"), optarg, true); 132 if (opts == NULL) { 133 exit(1); 134 } 135 arg.head = head; 136 arg.curr = NULL; 137 qemu_opt_foreach(opts, plugin_add, &arg, &error_fatal); 138 qemu_opts_del(opts); 139 } 140 141 /* 142 * From: https://en.wikipedia.org/wiki/Xorshift 143 * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only 144 * guaranteed to be >= INT_MAX). 145 */ 146 static uint64_t xorshift64star(uint64_t x) 147 { 148 x ^= x >> 12; /* a */ 149 x ^= x << 25; /* b */ 150 x ^= x >> 27; /* c */ 151 return x * UINT64_C(2685821657736338717); 152 } 153 154 /* 155 * Disable CFI checks. 156 * The install and version functions have been loaded from an external library 157 * so we do not have type information 158 */ 159 QEMU_DISABLE_CFI 160 static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info, Error **errp) 161 { 162 qemu_plugin_install_func_t install; 163 struct qemu_plugin_ctx *ctx; 164 gpointer sym; 165 int rc; 166 167 ctx = qemu_memalign(qemu_dcache_linesize, sizeof(*ctx)); 168 memset(ctx, 0, sizeof(*ctx)); 169 ctx->desc = desc; 170 171 ctx->handle = g_module_open(desc->path, G_MODULE_BIND_LOCAL); 172 if (ctx->handle == NULL) { 173 error_setg(errp, "Could not load plugin %s: %s", desc->path, g_module_error()); 174 goto err_dlopen; 175 } 176 177 if (!g_module_symbol(ctx->handle, "qemu_plugin_install", &sym)) { 178 error_setg(errp, "Could not load plugin %s: %s", desc->path, g_module_error()); 179 goto err_symbol; 180 } 181 install = (qemu_plugin_install_func_t) sym; 182 /* symbol was found; it could be NULL though */ 183 if (install == NULL) { 184 error_setg(errp, "Could not load plugin %s: qemu_plugin_install is NULL", 185 desc->path); 186 goto err_symbol; 187 } 188 189 if (!g_module_symbol(ctx->handle, "qemu_plugin_version", &sym)) { 190 error_setg(errp, "Could not load plugin %s: plugin does not declare API version %s", 191 desc->path, g_module_error()); 192 goto err_symbol; 193 } else { 194 int version = *(int *)sym; 195 if (version < QEMU_PLUGIN_MIN_VERSION) { 196 error_setg(errp, "Could not load plugin %s: plugin requires API version %d, but " 197 "this QEMU supports only a minimum version of %d", 198 desc->path, version, QEMU_PLUGIN_MIN_VERSION); 199 goto err_symbol; 200 } else if (version > QEMU_PLUGIN_VERSION) { 201 error_setg(errp, "Could not load plugin %s: plugin requires API version %d, but " 202 "this QEMU supports only up to version %d", 203 desc->path, version, QEMU_PLUGIN_VERSION); 204 goto err_symbol; 205 } 206 } 207 208 qemu_rec_mutex_lock(&plugin.lock); 209 210 /* find an unused random id with &ctx as the seed */ 211 ctx->id = (uint64_t)(uintptr_t)ctx; 212 for (;;) { 213 void *existing; 214 215 ctx->id = xorshift64star(ctx->id); 216 existing = g_hash_table_lookup(plugin.id_ht, &ctx->id); 217 if (likely(existing == NULL)) { 218 bool success; 219 220 success = g_hash_table_insert(plugin.id_ht, &ctx->id, &ctx->id); 221 g_assert(success); 222 break; 223 } 224 } 225 QTAILQ_INSERT_TAIL(&plugin.ctxs, ctx, entry); 226 ctx->installing = true; 227 rc = install(ctx->id, info, desc->argc, desc->argv); 228 ctx->installing = false; 229 if (rc) { 230 error_setg(errp, "Could not load plugin %s: qemu_plugin_install returned error code %d", 231 desc->path, rc); 232 /* 233 * we cannot rely on the plugin doing its own cleanup, so 234 * call a full uninstall if the plugin did not yet call it. 235 */ 236 if (!ctx->uninstalling) { 237 plugin_reset_uninstall(ctx->id, NULL, false); 238 } 239 } 240 241 qemu_rec_mutex_unlock(&plugin.lock); 242 return rc; 243 244 err_symbol: 245 g_module_close(ctx->handle); 246 err_dlopen: 247 qemu_vfree(ctx); 248 return 1; 249 } 250 251 /* call after having removed @desc from the list */ 252 static void plugin_desc_free(struct qemu_plugin_desc *desc) 253 { 254 int i; 255 256 for (i = 0; i < desc->argc; i++) { 257 g_free(desc->argv[i]); 258 } 259 g_free(desc->argv); 260 g_free(desc->path); 261 g_free(desc); 262 } 263 264 /** 265 * qemu_plugin_load_list - load a list of plugins 266 * @head: head of the list of descriptors of the plugins to be loaded 267 * 268 * Returns 0 if all plugins in the list are installed, !0 otherwise. 269 * 270 * Note: the descriptor of each successfully installed plugin is removed 271 * from the list given by @head. 272 */ 273 int qemu_plugin_load_list(QemuPluginList *head, Error **errp) 274 { 275 struct qemu_plugin_desc *desc, *next; 276 g_autofree qemu_info_t *info = g_new0(qemu_info_t, 1); 277 278 info->target_name = TARGET_NAME; 279 info->version.min = QEMU_PLUGIN_MIN_VERSION; 280 info->version.cur = QEMU_PLUGIN_VERSION; 281 #ifndef CONFIG_USER_ONLY 282 MachineState *ms = MACHINE(qdev_get_machine()); 283 info->system_emulation = true; 284 info->system.smp_vcpus = ms->smp.cpus; 285 info->system.max_vcpus = ms->smp.max_cpus; 286 #else 287 info->system_emulation = false; 288 #endif 289 290 QTAILQ_FOREACH_SAFE(desc, head, entry, next) { 291 int err; 292 293 err = plugin_load(desc, info, errp); 294 if (err) { 295 return err; 296 } 297 QTAILQ_REMOVE(head, desc, entry); 298 } 299 return 0; 300 } 301 302 struct qemu_plugin_reset_data { 303 struct qemu_plugin_ctx *ctx; 304 qemu_plugin_simple_cb_t cb; 305 bool reset; 306 }; 307 308 static void plugin_reset_destroy__locked(struct qemu_plugin_reset_data *data) 309 { 310 struct qemu_plugin_ctx *ctx = data->ctx; 311 enum qemu_plugin_event ev; 312 bool success; 313 314 /* 315 * After updating the subscription lists there is no need to wait for an RCU 316 * grace period to elapse, because right now we either are in a "safe async" 317 * work environment (i.e. all vCPUs are asleep), or no vCPUs have yet been 318 * created. 319 */ 320 for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) { 321 plugin_unregister_cb__locked(ctx, ev); 322 } 323 324 if (data->reset) { 325 g_assert(ctx->resetting); 326 if (data->cb) { 327 data->cb(ctx->id); 328 } 329 ctx->resetting = false; 330 g_free(data); 331 return; 332 } 333 334 g_assert(ctx->uninstalling); 335 /* we cannot dlclose if we are going to return to plugin code */ 336 if (ctx->installing) { 337 error_report("Calling qemu_plugin_uninstall from the install function " 338 "is a bug. Instead, return !0 from the install function."); 339 abort(); 340 } 341 342 success = g_hash_table_remove(plugin.id_ht, &ctx->id); 343 g_assert(success); 344 QTAILQ_REMOVE(&plugin.ctxs, ctx, entry); 345 if (data->cb) { 346 data->cb(ctx->id); 347 } 348 if (!g_module_close(ctx->handle)) { 349 warn_report("%s: %s", __func__, g_module_error()); 350 } 351 plugin_desc_free(ctx->desc); 352 qemu_vfree(ctx); 353 g_free(data); 354 } 355 356 static void plugin_reset_destroy(struct qemu_plugin_reset_data *data) 357 { 358 qemu_rec_mutex_lock(&plugin.lock); 359 plugin_reset_destroy__locked(data); 360 qemu_rec_mutex_lock(&plugin.lock); 361 } 362 363 static void plugin_flush_destroy(CPUState *cpu, run_on_cpu_data arg) 364 { 365 struct qemu_plugin_reset_data *data = arg.host_ptr; 366 367 g_assert(cpu_in_exclusive_context(cpu)); 368 tb_flush(cpu); 369 plugin_reset_destroy(data); 370 } 371 372 void plugin_reset_uninstall(qemu_plugin_id_t id, 373 qemu_plugin_simple_cb_t cb, 374 bool reset) 375 { 376 struct qemu_plugin_reset_data *data; 377 struct qemu_plugin_ctx *ctx; 378 379 WITH_QEMU_LOCK_GUARD(&plugin.lock) { 380 ctx = plugin_id_to_ctx_locked(id); 381 if (ctx->uninstalling || (reset && ctx->resetting)) { 382 return; 383 } 384 ctx->resetting = reset; 385 ctx->uninstalling = !reset; 386 } 387 388 data = g_new(struct qemu_plugin_reset_data, 1); 389 data->ctx = ctx; 390 data->cb = cb; 391 data->reset = reset; 392 /* 393 * Only flush the code cache if the vCPUs have been created. If so, 394 * current_cpu must be non-NULL. 395 */ 396 if (current_cpu) { 397 async_safe_run_on_cpu(current_cpu, plugin_flush_destroy, 398 RUN_ON_CPU_HOST_PTR(data)); 399 } else { 400 /* 401 * If current_cpu isn't set, then we don't have yet any vCPU threads 402 * and we therefore can remove the callbacks synchronously. 403 */ 404 plugin_reset_destroy(data); 405 } 406 } 407