xref: /openbmc/qemu/plugins/core.c (revision 36a1d8e7)
1 /*
2  * QEMU Plugin Core code
3  *
4  * This is the core code that deals with injecting instrumentation into the code
5  *
6  * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
7  * Copyright (C) 2019, Linaro
8  *
9  * License: GNU GPL, version 2 or later.
10  *   See the COPYING file in the top-level directory.
11  *
12  * SPDX-License-Identifier: GPL-2.0-or-later
13  */
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/plugin.h"
21 #include "qemu/queue.h"
22 #include "qemu/rcu_queue.h"
23 #include "qemu/xxhash.h"
24 #include "qemu/rcu.h"
25 #include "hw/core/cpu.h"
26 
27 #include "exec/exec-all.h"
28 #include "exec/tb-flush.h"
29 #include "tcg/tcg.h"
30 #include "tcg/tcg-op.h"
31 #include "plugin.h"
32 
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;
38 };
39 
40 struct qemu_plugin_state plugin;
41 
42 struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)
43 {
44     struct qemu_plugin_ctx *ctx;
45     qemu_plugin_id_t *id_p;
46 
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();
52     }
53     return ctx;
54 }
55 
56 static void plugin_cpu_update__async(CPUState *cpu, run_on_cpu_data data)
57 {
58     bitmap_copy(cpu->plugin_state->event_mask,
59                 &data.host_ulong, QEMU_PLUGIN_EV_MAX);
60     tcg_flush_jmp_cache(cpu);
61 }
62 
63 static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
64 {
65     CPUState *cpu = container_of(k, CPUState, cpu_index);
66     run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
67 
68     if (DEVICE(cpu)->realized) {
69         async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
70     } else {
71         plugin_cpu_update__async(cpu, mask);
72     }
73 }
74 
75 void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
76                                   enum qemu_plugin_event ev)
77 {
78     struct qemu_plugin_cb *cb = ctx->callbacks[ev];
79 
80     if (cb == NULL) {
81         return;
82     }
83     QLIST_REMOVE_RCU(cb, entry);
84     g_free(cb);
85     ctx->callbacks[ev] = NULL;
86     if (QLIST_EMPTY_RCU(&plugin.cb_lists[ev])) {
87         clear_bit(ev, plugin.mask);
88         g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked, NULL);
89     }
90 }
91 
92 /*
93  * Disable CFI checks.
94  * The callback function has been loaded from an external library so we do not
95  * have type information
96  */
97 QEMU_DISABLE_CFI
98 static void plugin_vcpu_cb__simple(CPUState *cpu, enum qemu_plugin_event ev)
99 {
100     struct qemu_plugin_cb *cb, *next;
101 
102     switch (ev) {
103     case QEMU_PLUGIN_EV_VCPU_INIT:
104     case QEMU_PLUGIN_EV_VCPU_EXIT:
105     case QEMU_PLUGIN_EV_VCPU_IDLE:
106     case QEMU_PLUGIN_EV_VCPU_RESUME:
107         /* iterate safely; plugins might uninstall themselves at any time */
108         QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
109             qemu_plugin_vcpu_simple_cb_t func = cb->f.vcpu_simple;
110 
111             func(cb->ctx->id, cpu->cpu_index);
112         }
113         break;
114     default:
115         g_assert_not_reached();
116     }
117 }
118 
119 /*
120  * Disable CFI checks.
121  * The callback function has been loaded from an external library so we do not
122  * have type information
123  */
124 QEMU_DISABLE_CFI
125 static void plugin_cb__simple(enum qemu_plugin_event ev)
126 {
127     struct qemu_plugin_cb *cb, *next;
128 
129     switch (ev) {
130     case QEMU_PLUGIN_EV_FLUSH:
131         QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
132             qemu_plugin_simple_cb_t func = cb->f.simple;
133 
134             func(cb->ctx->id);
135         }
136         break;
137     default:
138         g_assert_not_reached();
139     }
140 }
141 
142 /*
143  * Disable CFI checks.
144  * The callback function has been loaded from an external library so we do not
145  * have type information
146  */
147 QEMU_DISABLE_CFI
148 static void plugin_cb__udata(enum qemu_plugin_event ev)
149 {
150     struct qemu_plugin_cb *cb, *next;
151 
152     switch (ev) {
153     case QEMU_PLUGIN_EV_ATEXIT:
154         QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
155             qemu_plugin_udata_cb_t func = cb->f.udata;
156 
157             func(cb->ctx->id, cb->udata);
158         }
159         break;
160     default:
161         g_assert_not_reached();
162     }
163 }
164 
165 static void
166 do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
167                       void *func, void *udata)
168 {
169     struct qemu_plugin_ctx *ctx;
170 
171     QEMU_LOCK_GUARD(&plugin.lock);
172     ctx = plugin_id_to_ctx_locked(id);
173     /* if the plugin is on its way out, ignore this request */
174     if (unlikely(ctx->uninstalling)) {
175         return;
176     }
177     if (func) {
178         struct qemu_plugin_cb *cb = ctx->callbacks[ev];
179 
180         if (cb) {
181             cb->f.generic = func;
182             cb->udata = udata;
183         } else {
184             cb = g_new(struct qemu_plugin_cb, 1);
185             cb->ctx = ctx;
186             cb->f.generic = func;
187             cb->udata = udata;
188             ctx->callbacks[ev] = cb;
189             QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
190             if (!test_bit(ev, plugin.mask)) {
191                 set_bit(ev, plugin.mask);
192                 g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
193                                      NULL);
194             }
195         }
196     } else {
197         plugin_unregister_cb__locked(ctx, ev);
198     }
199 }
200 
201 void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
202                         void *func)
203 {
204     do_plugin_register_cb(id, ev, func, NULL);
205 }
206 
207 void
208 plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
209                          void *func, void *udata)
210 {
211     do_plugin_register_cb(id, ev, func, udata);
212 }
213 
214 CPUPluginState *qemu_plugin_create_vcpu_state(void)
215 {
216     return g_new0(CPUPluginState, 1);
217 }
218 
219 static void plugin_grow_scoreboards__locked(CPUState *cpu)
220 {
221     if (cpu->cpu_index < plugin.scoreboard_alloc_size) {
222         return;
223     }
224 
225     bool need_realloc = FALSE;
226     while (cpu->cpu_index >= plugin.scoreboard_alloc_size) {
227         plugin.scoreboard_alloc_size *= 2;
228         need_realloc = TRUE;
229     }
230 
231 
232     if (!need_realloc || QLIST_EMPTY(&plugin.scoreboards)) {
233         /* nothing to do, we just updated sizes for future scoreboards */
234         return;
235     }
236 
237     /* cpus must be stopped, as tb might still use an existing scoreboard. */
238     start_exclusive();
239     struct qemu_plugin_scoreboard *score;
240     QLIST_FOREACH(score, &plugin.scoreboards, entry) {
241         g_array_set_size(score->data, plugin.scoreboard_alloc_size);
242     }
243     /* force all tb to be flushed, as scoreboard pointers were changed. */
244     tb_flush(cpu);
245     end_exclusive();
246 }
247 
248 void qemu_plugin_vcpu_init_hook(CPUState *cpu)
249 {
250     bool success;
251 
252     qemu_rec_mutex_lock(&plugin.lock);
253     plugin.num_vcpus = MAX(plugin.num_vcpus, cpu->cpu_index + 1);
254     plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
255     success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
256                                   &cpu->cpu_index);
257     g_assert(success);
258     plugin_grow_scoreboards__locked(cpu);
259     qemu_rec_mutex_unlock(&plugin.lock);
260 
261     plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
262 }
263 
264 void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
265 {
266     bool success;
267 
268     plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
269 
270     qemu_rec_mutex_lock(&plugin.lock);
271     success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
272     g_assert(success);
273     qemu_rec_mutex_unlock(&plugin.lock);
274 }
275 
276 struct plugin_for_each_args {
277     struct qemu_plugin_ctx *ctx;
278     qemu_plugin_vcpu_simple_cb_t cb;
279 };
280 
281 static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
282 {
283     struct plugin_for_each_args *args = udata;
284     int cpu_index = *(int *)k;
285 
286     args->cb(args->ctx->id, cpu_index);
287 }
288 
289 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
290                                qemu_plugin_vcpu_simple_cb_t cb)
291 {
292     struct plugin_for_each_args args;
293 
294     if (cb == NULL) {
295         return;
296     }
297     qemu_rec_mutex_lock(&plugin.lock);
298     args.ctx = plugin_id_to_ctx_locked(id);
299     args.cb = cb;
300     g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
301     qemu_rec_mutex_unlock(&plugin.lock);
302 }
303 
304 /* Allocate and return a callback record */
305 static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
306 {
307     GArray *cbs = *arr;
308 
309     if (!cbs) {
310         cbs = g_array_sized_new(false, true,
311                                 sizeof(struct qemu_plugin_dyn_cb), 1);
312         *arr = cbs;
313     }
314 
315     g_array_set_size(cbs, cbs->len + 1);
316     return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
317 }
318 
319 static enum plugin_dyn_cb_type op_to_cb_type(enum qemu_plugin_op op)
320 {
321     switch (op) {
322     case QEMU_PLUGIN_INLINE_ADD_U64:
323         return PLUGIN_CB_INLINE_ADD_U64;
324     case QEMU_PLUGIN_INLINE_STORE_U64:
325         return PLUGIN_CB_INLINE_STORE_U64;
326     default:
327         g_assert_not_reached();
328     }
329 }
330 
331 void plugin_register_inline_op_on_entry(GArray **arr,
332                                         enum qemu_plugin_mem_rw rw,
333                                         enum qemu_plugin_op op,
334                                         qemu_plugin_u64 entry,
335                                         uint64_t imm)
336 {
337     struct qemu_plugin_dyn_cb *dyn_cb;
338 
339     dyn_cb = plugin_get_dyn_cb(arr);
340     dyn_cb->userp = NULL;
341     dyn_cb->type = op_to_cb_type(op);
342     dyn_cb->rw = rw;
343     dyn_cb->inline_insn.entry = entry;
344     dyn_cb->inline_insn.op = op;
345     dyn_cb->inline_insn.imm = imm;
346 }
347 
348 void plugin_register_dyn_cb__udata(GArray **arr,
349                                    qemu_plugin_vcpu_udata_cb_t cb,
350                                    enum qemu_plugin_cb_flags flags,
351                                    void *udata)
352 {
353     static TCGHelperInfo info[3] = {
354         [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
355         [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
356         /*
357          * Match qemu_plugin_vcpu_udata_cb_t:
358          *   void (*)(uint32_t, void *)
359          */
360         [0 ... 2].typemask = (dh_typemask(void, 0) |
361                               dh_typemask(i32, 1) |
362                               dh_typemask(ptr, 2))
363     };
364 
365     struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
366     dyn_cb->userp = udata;
367     dyn_cb->type = PLUGIN_CB_REGULAR;
368     dyn_cb->regular.f.vcpu_udata = cb;
369 
370     assert((unsigned)flags < ARRAY_SIZE(info));
371     dyn_cb->regular.info = &info[flags];
372 }
373 
374 void plugin_register_vcpu_mem_cb(GArray **arr,
375                                  void *cb,
376                                  enum qemu_plugin_cb_flags flags,
377                                  enum qemu_plugin_mem_rw rw,
378                                  void *udata)
379 {
380     /*
381      * Expect that the underlying type for enum qemu_plugin_meminfo_t
382      * is either int32_t or uint32_t, aka int or unsigned int.
383      */
384     QEMU_BUILD_BUG_ON(
385         !__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t) &&
386         !__builtin_types_compatible_p(qemu_plugin_meminfo_t, int32_t));
387 
388     static TCGHelperInfo info[3] = {
389         [QEMU_PLUGIN_CB_NO_REGS].flags = TCG_CALL_NO_RWG,
390         [QEMU_PLUGIN_CB_R_REGS].flags = TCG_CALL_NO_WG,
391         /*
392          * Match qemu_plugin_vcpu_mem_cb_t:
393          *   void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *)
394          */
395         [0 ... 2].typemask =
396             (dh_typemask(void, 0) |
397              dh_typemask(i32, 1) |
398              (__builtin_types_compatible_p(qemu_plugin_meminfo_t, uint32_t)
399               ? dh_typemask(i32, 2) : dh_typemask(s32, 2)) |
400              dh_typemask(i64, 3) |
401              dh_typemask(ptr, 4))
402     };
403 
404     struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
405     dyn_cb->userp = udata;
406     dyn_cb->type = PLUGIN_CB_MEM_REGULAR;
407     dyn_cb->rw = rw;
408     dyn_cb->regular.f.vcpu_mem = cb;
409 
410     assert((unsigned)flags < ARRAY_SIZE(info));
411     dyn_cb->regular.info = &info[flags];
412 }
413 
414 /*
415  * Disable CFI checks.
416  * The callback function has been loaded from an external library so we do not
417  * have type information
418  */
419 QEMU_DISABLE_CFI
420 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
421 {
422     struct qemu_plugin_cb *cb, *next;
423     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
424 
425     /* no plugin_state->event_mask check here; caller should have checked */
426 
427     QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
428         qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
429 
430         func(cb->ctx->id, tb);
431     }
432 }
433 
434 /*
435  * Disable CFI checks.
436  * The callback function has been loaded from an external library so we do not
437  * have type information
438  */
439 QEMU_DISABLE_CFI
440 void
441 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
442                          uint64_t a3, uint64_t a4, uint64_t a5,
443                          uint64_t a6, uint64_t a7, uint64_t a8)
444 {
445     struct qemu_plugin_cb *cb, *next;
446     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
447 
448     if (!test_bit(ev, cpu->plugin_state->event_mask)) {
449         return;
450     }
451 
452     QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
453         qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
454 
455         func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);
456     }
457 }
458 
459 /*
460  * Disable CFI checks.
461  * The callback function has been loaded from an external library so we do not
462  * have type information
463  */
464 QEMU_DISABLE_CFI
465 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
466 {
467     struct qemu_plugin_cb *cb, *next;
468     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
469 
470     if (!test_bit(ev, cpu->plugin_state->event_mask)) {
471         return;
472     }
473 
474     QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
475         qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret;
476 
477         func(cb->ctx->id, cpu->cpu_index, num, ret);
478     }
479 }
480 
481 void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
482 {
483     /* idle and resume cb may be called before init, ignore in this case */
484     if (cpu->cpu_index < plugin.num_vcpus) {
485         plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE);
486     }
487 }
488 
489 void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
490 {
491     if (cpu->cpu_index < plugin.num_vcpus) {
492         plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
493     }
494 }
495 
496 void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
497                                        qemu_plugin_vcpu_simple_cb_t cb)
498 {
499     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
500 }
501 
502 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
503                                          qemu_plugin_vcpu_simple_cb_t cb)
504 {
505     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
506 }
507 
508 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
509                                    qemu_plugin_simple_cb_t cb)
510 {
511     plugin_register_cb(id, QEMU_PLUGIN_EV_FLUSH, cb);
512 }
513 
514 static bool free_dyn_cb_arr(void *p, uint32_t h, void *userp)
515 {
516     g_array_free((GArray *) p, true);
517     return true;
518 }
519 
520 void qemu_plugin_flush_cb(void)
521 {
522     qht_iter_remove(&plugin.dyn_cb_arr_ht, free_dyn_cb_arr, NULL);
523     qht_reset(&plugin.dyn_cb_arr_ht);
524 
525     plugin_cb__simple(QEMU_PLUGIN_EV_FLUSH);
526 }
527 
528 void exec_inline_op(struct qemu_plugin_dyn_cb *cb, int cpu_index)
529 {
530     char *ptr = cb->inline_insn.entry.score->data->data;
531     size_t elem_size = g_array_get_element_size(
532         cb->inline_insn.entry.score->data);
533     size_t offset = cb->inline_insn.entry.offset;
534     uint64_t *val = (uint64_t *)(ptr + offset + cpu_index * elem_size);
535 
536     switch (cb->inline_insn.op) {
537     case QEMU_PLUGIN_INLINE_ADD_U64:
538         *val += cb->inline_insn.imm;
539         break;
540     case QEMU_PLUGIN_INLINE_STORE_U64:
541         *val = cb->inline_insn.imm;
542         break;
543     default:
544         g_assert_not_reached();
545     }
546 }
547 
548 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
549                              MemOpIdx oi, enum qemu_plugin_mem_rw rw)
550 {
551     GArray *arr = cpu->neg.plugin_mem_cbs;
552     size_t i;
553 
554     if (arr == NULL) {
555         return;
556     }
557     for (i = 0; i < arr->len; i++) {
558         struct qemu_plugin_dyn_cb *cb =
559             &g_array_index(arr, struct qemu_plugin_dyn_cb, i);
560 
561         if (!(rw & cb->rw)) {
562                 break;
563         }
564         switch (cb->type) {
565         case PLUGIN_CB_MEM_REGULAR:
566             cb->regular.f.vcpu_mem(cpu->cpu_index, make_plugin_meminfo(oi, rw),
567                                    vaddr, cb->userp);
568             break;
569         case PLUGIN_CB_INLINE_ADD_U64:
570         case PLUGIN_CB_INLINE_STORE_U64:
571             exec_inline_op(cb, cpu->cpu_index);
572             break;
573         default:
574             g_assert_not_reached();
575         }
576     }
577 }
578 
579 void qemu_plugin_atexit_cb(void)
580 {
581     plugin_cb__udata(QEMU_PLUGIN_EV_ATEXIT);
582 }
583 
584 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
585                                     qemu_plugin_udata_cb_t cb,
586                                     void *udata)
587 {
588     plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
589 }
590 
591 /*
592  * Handle exit from linux-user. Unlike the normal atexit() mechanism
593  * we need to handle the clean-up manually as it's possible threads
594  * are still running. We need to remove all callbacks from code
595  * generation, flush the current translations and then we can safely
596  * trigger the exit callbacks.
597  */
598 
599 void qemu_plugin_user_exit(void)
600 {
601     enum qemu_plugin_event ev;
602     CPUState *cpu;
603 
604     /*
605      * Locking order: we must acquire locks in an order that is consistent
606      * with the one in fork_start(). That is:
607      * - start_exclusive(), which acquires qemu_cpu_list_lock,
608      *   must be called before acquiring plugin.lock.
609      * - tb_flush(), which acquires mmap_lock(), must be called
610      *   while plugin.lock is not held.
611      */
612     start_exclusive();
613 
614     qemu_rec_mutex_lock(&plugin.lock);
615     /* un-register all callbacks except the final AT_EXIT one */
616     for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
617         if (ev != QEMU_PLUGIN_EV_ATEXIT) {
618             struct qemu_plugin_cb *cb, *next;
619 
620             QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
621                 plugin_unregister_cb__locked(cb->ctx, ev);
622             }
623         }
624     }
625     CPU_FOREACH(cpu) {
626         qemu_plugin_disable_mem_helpers(cpu);
627     }
628     qemu_rec_mutex_unlock(&plugin.lock);
629 
630     tb_flush(current_cpu);
631     end_exclusive();
632 
633     /* now it's safe to handle the exit case */
634     qemu_plugin_atexit_cb();
635 }
636 
637 /*
638  * Helpers for *-user to ensure locks are sane across fork() events.
639  */
640 
641 void qemu_plugin_user_prefork_lock(void)
642 {
643     qemu_rec_mutex_lock(&plugin.lock);
644 }
645 
646 void qemu_plugin_user_postfork(bool is_child)
647 {
648     if (is_child) {
649         /* should we just reset via plugin_init? */
650         qemu_rec_mutex_init(&plugin.lock);
651     } else {
652         qemu_rec_mutex_unlock(&plugin.lock);
653     }
654 }
655 
656 static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp)
657 {
658     return ap == bp;
659 }
660 
661 static void __attribute__((__constructor__)) plugin_init(void)
662 {
663     int i;
664 
665     for (i = 0; i < QEMU_PLUGIN_EV_MAX; i++) {
666         QLIST_INIT(&plugin.cb_lists[i]);
667     }
668     qemu_rec_mutex_init(&plugin.lock);
669     plugin.id_ht = g_hash_table_new(g_int64_hash, g_int64_equal);
670     plugin.cpu_ht = g_hash_table_new(g_int_hash, g_int_equal);
671     QLIST_INIT(&plugin.scoreboards);
672     plugin.scoreboard_alloc_size = 16; /* avoid frequent reallocation */
673     QTAILQ_INIT(&plugin.ctxs);
674     qht_init(&plugin.dyn_cb_arr_ht, plugin_dyn_cb_arr_cmp, 16,
675              QHT_MODE_AUTO_RESIZE);
676     atexit(qemu_plugin_atexit_cb);
677 }
678 
679 int plugin_num_vcpus(void)
680 {
681     return plugin.num_vcpus;
682 }
683 
684 struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size)
685 {
686     struct qemu_plugin_scoreboard *score =
687         g_malloc0(sizeof(struct qemu_plugin_scoreboard));
688     score->data = g_array_new(FALSE, TRUE, element_size);
689     g_array_set_size(score->data, plugin.scoreboard_alloc_size);
690 
691     qemu_rec_mutex_lock(&plugin.lock);
692     QLIST_INSERT_HEAD(&plugin.scoreboards, score, entry);
693     qemu_rec_mutex_unlock(&plugin.lock);
694 
695     return score;
696 }
697 
698 void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
699 {
700     qemu_rec_mutex_lock(&plugin.lock);
701     QLIST_REMOVE(score, entry);
702     qemu_rec_mutex_unlock(&plugin.lock);
703 
704     g_array_free(score->data, TRUE);
705     g_free(score);
706 }
707