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