xref: /qemu/plugins/core.c (revision c490e681)
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/rcu_queue.h"
21 #include "qemu/xxhash.h"
22 #include "qemu/rcu.h"
23 #include "hw/core/cpu.h"
24 
25 #include "exec/exec-all.h"
26 #include "exec/tb-flush.h"
27 #include "tcg/tcg.h"
28 #include "tcg/tcg-op.h"
29 #include "plugin.h"
30 
31 struct qemu_plugin_cb {
32     struct qemu_plugin_ctx *ctx;
33     union qemu_plugin_cb_sig f;
34     void *udata;
35     QLIST_ENTRY(qemu_plugin_cb) entry;
36 };
37 
38 struct qemu_plugin_state plugin;
39 
40 struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id)
41 {
42     struct qemu_plugin_ctx *ctx;
43     qemu_plugin_id_t *id_p;
44 
45     id_p = g_hash_table_lookup(plugin.id_ht, &id);
46     ctx = container_of(id_p, struct qemu_plugin_ctx, id);
47     if (ctx == NULL) {
48         error_report("plugin: invalid plugin id %" PRIu64, id);
49         abort();
50     }
51     return ctx;
52 }
53 
54 static void plugin_cpu_update__async(CPUState *cpu, run_on_cpu_data data)
55 {
56     bitmap_copy(cpu->plugin_mask, &data.host_ulong, QEMU_PLUGIN_EV_MAX);
57     tcg_flush_jmp_cache(cpu);
58 }
59 
60 static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
61 {
62     CPUState *cpu = container_of(k, CPUState, cpu_index);
63     run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
64 
65     if (DEVICE(cpu)->realized) {
66         async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
67     } else {
68         plugin_cpu_update__async(cpu, mask);
69     }
70 }
71 
72 void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
73                                   enum qemu_plugin_event ev)
74 {
75     struct qemu_plugin_cb *cb = ctx->callbacks[ev];
76 
77     if (cb == NULL) {
78         return;
79     }
80     QLIST_REMOVE_RCU(cb, entry);
81     g_free(cb);
82     ctx->callbacks[ev] = NULL;
83     if (QLIST_EMPTY_RCU(&plugin.cb_lists[ev])) {
84         clear_bit(ev, plugin.mask);
85         g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked, NULL);
86     }
87 }
88 
89 /*
90  * Disable CFI checks.
91  * The callback function has been loaded from an external library so we do not
92  * have type information
93  */
94 QEMU_DISABLE_CFI
95 static void plugin_vcpu_cb__simple(CPUState *cpu, enum qemu_plugin_event ev)
96 {
97     struct qemu_plugin_cb *cb, *next;
98 
99     switch (ev) {
100     case QEMU_PLUGIN_EV_VCPU_INIT:
101     case QEMU_PLUGIN_EV_VCPU_EXIT:
102     case QEMU_PLUGIN_EV_VCPU_IDLE:
103     case QEMU_PLUGIN_EV_VCPU_RESUME:
104         /* iterate safely; plugins might uninstall themselves at any time */
105         QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
106             qemu_plugin_vcpu_simple_cb_t func = cb->f.vcpu_simple;
107 
108             func(cb->ctx->id, cpu->cpu_index);
109         }
110         break;
111     default:
112         g_assert_not_reached();
113     }
114 }
115 
116 /*
117  * Disable CFI checks.
118  * The callback function has been loaded from an external library so we do not
119  * have type information
120  */
121 QEMU_DISABLE_CFI
122 static void plugin_cb__simple(enum qemu_plugin_event ev)
123 {
124     struct qemu_plugin_cb *cb, *next;
125 
126     switch (ev) {
127     case QEMU_PLUGIN_EV_FLUSH:
128         QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
129             qemu_plugin_simple_cb_t func = cb->f.simple;
130 
131             func(cb->ctx->id);
132         }
133         break;
134     default:
135         g_assert_not_reached();
136     }
137 }
138 
139 /*
140  * Disable CFI checks.
141  * The callback function has been loaded from an external library so we do not
142  * have type information
143  */
144 QEMU_DISABLE_CFI
145 static void plugin_cb__udata(enum qemu_plugin_event ev)
146 {
147     struct qemu_plugin_cb *cb, *next;
148 
149     switch (ev) {
150     case QEMU_PLUGIN_EV_ATEXIT:
151         QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
152             qemu_plugin_udata_cb_t func = cb->f.udata;
153 
154             func(cb->ctx->id, cb->udata);
155         }
156         break;
157     default:
158         g_assert_not_reached();
159     }
160 }
161 
162 static void
163 do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
164                       void *func, void *udata)
165 {
166     struct qemu_plugin_ctx *ctx;
167 
168     QEMU_LOCK_GUARD(&plugin.lock);
169     ctx = plugin_id_to_ctx_locked(id);
170     /* if the plugin is on its way out, ignore this request */
171     if (unlikely(ctx->uninstalling)) {
172         return;
173     }
174     if (func) {
175         struct qemu_plugin_cb *cb = ctx->callbacks[ev];
176 
177         if (cb) {
178             cb->f.generic = func;
179             cb->udata = udata;
180         } else {
181             cb = g_new(struct qemu_plugin_cb, 1);
182             cb->ctx = ctx;
183             cb->f.generic = func;
184             cb->udata = udata;
185             ctx->callbacks[ev] = cb;
186             QLIST_INSERT_HEAD_RCU(&plugin.cb_lists[ev], cb, entry);
187             if (!test_bit(ev, plugin.mask)) {
188                 set_bit(ev, plugin.mask);
189                 g_hash_table_foreach(plugin.cpu_ht, plugin_cpu_update__locked,
190                                      NULL);
191             }
192         }
193     } else {
194         plugin_unregister_cb__locked(ctx, ev);
195     }
196 }
197 
198 void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
199                         void *func)
200 {
201     do_plugin_register_cb(id, ev, func, NULL);
202 }
203 
204 void
205 plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
206                          void *func, void *udata)
207 {
208     do_plugin_register_cb(id, ev, func, udata);
209 }
210 
211 void qemu_plugin_vcpu_init_hook(CPUState *cpu)
212 {
213     bool success;
214 
215     qemu_rec_mutex_lock(&plugin.lock);
216     plugin.num_vcpus = MAX(plugin.num_vcpus, cpu->cpu_index + 1);
217     plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
218     success = g_hash_table_insert(plugin.cpu_ht, &cpu->cpu_index,
219                                   &cpu->cpu_index);
220     g_assert(success);
221     qemu_rec_mutex_unlock(&plugin.lock);
222 
223     plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT);
224 }
225 
226 void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
227 {
228     bool success;
229 
230     plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
231 
232     qemu_rec_mutex_lock(&plugin.lock);
233     success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
234     g_assert(success);
235     qemu_rec_mutex_unlock(&plugin.lock);
236 }
237 
238 struct plugin_for_each_args {
239     struct qemu_plugin_ctx *ctx;
240     qemu_plugin_vcpu_simple_cb_t cb;
241 };
242 
243 static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
244 {
245     struct plugin_for_each_args *args = udata;
246     int cpu_index = *(int *)k;
247 
248     args->cb(args->ctx->id, cpu_index);
249 }
250 
251 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
252                                qemu_plugin_vcpu_simple_cb_t cb)
253 {
254     struct plugin_for_each_args args;
255 
256     if (cb == NULL) {
257         return;
258     }
259     qemu_rec_mutex_lock(&plugin.lock);
260     args.ctx = plugin_id_to_ctx_locked(id);
261     args.cb = cb;
262     g_hash_table_foreach(plugin.cpu_ht, plugin_vcpu_for_each, &args);
263     qemu_rec_mutex_unlock(&plugin.lock);
264 }
265 
266 /* Allocate and return a callback record */
267 static struct qemu_plugin_dyn_cb *plugin_get_dyn_cb(GArray **arr)
268 {
269     GArray *cbs = *arr;
270 
271     if (!cbs) {
272         cbs = g_array_sized_new(false, false,
273                                 sizeof(struct qemu_plugin_dyn_cb), 1);
274         *arr = cbs;
275     }
276 
277     g_array_set_size(cbs, cbs->len + 1);
278     return &g_array_index(cbs, struct qemu_plugin_dyn_cb, cbs->len - 1);
279 }
280 
281 void plugin_register_inline_op(GArray **arr,
282                                enum qemu_plugin_mem_rw rw,
283                                enum qemu_plugin_op op, void *ptr,
284                                uint64_t imm)
285 {
286     struct qemu_plugin_dyn_cb *dyn_cb;
287 
288     dyn_cb = plugin_get_dyn_cb(arr);
289     dyn_cb->userp = ptr;
290     dyn_cb->type = PLUGIN_CB_INLINE;
291     dyn_cb->rw = rw;
292     dyn_cb->inline_insn.op = op;
293     dyn_cb->inline_insn.imm = imm;
294 }
295 
296 void plugin_register_dyn_cb__udata(GArray **arr,
297                                    qemu_plugin_vcpu_udata_cb_t cb,
298                                    enum qemu_plugin_cb_flags flags,
299                                    void *udata)
300 {
301     struct qemu_plugin_dyn_cb *dyn_cb = plugin_get_dyn_cb(arr);
302 
303     dyn_cb->userp = udata;
304     /* Note flags are discarded as unused. */
305     dyn_cb->f.vcpu_udata = cb;
306     dyn_cb->type = PLUGIN_CB_REGULAR;
307 }
308 
309 void plugin_register_vcpu_mem_cb(GArray **arr,
310                                  void *cb,
311                                  enum qemu_plugin_cb_flags flags,
312                                  enum qemu_plugin_mem_rw rw,
313                                  void *udata)
314 {
315     struct qemu_plugin_dyn_cb *dyn_cb;
316 
317     dyn_cb = plugin_get_dyn_cb(arr);
318     dyn_cb->userp = udata;
319     /* Note flags are discarded as unused. */
320     dyn_cb->type = PLUGIN_CB_REGULAR;
321     dyn_cb->rw = rw;
322     dyn_cb->f.generic = cb;
323 }
324 
325 /*
326  * Disable CFI checks.
327  * The callback function has been loaded from an external library so we do not
328  * have type information
329  */
330 QEMU_DISABLE_CFI
331 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb)
332 {
333     struct qemu_plugin_cb *cb, *next;
334     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_TB_TRANS;
335 
336     /* no plugin_mask check here; caller should have checked */
337 
338     QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
339         qemu_plugin_vcpu_tb_trans_cb_t func = cb->f.vcpu_tb_trans;
340 
341         func(cb->ctx->id, tb);
342     }
343 }
344 
345 /*
346  * Disable CFI checks.
347  * The callback function has been loaded from an external library so we do not
348  * have type information
349  */
350 QEMU_DISABLE_CFI
351 void
352 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
353                          uint64_t a3, uint64_t a4, uint64_t a5,
354                          uint64_t a6, uint64_t a7, uint64_t a8)
355 {
356     struct qemu_plugin_cb *cb, *next;
357     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL;
358 
359     if (!test_bit(ev, cpu->plugin_mask)) {
360         return;
361     }
362 
363     QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
364         qemu_plugin_vcpu_syscall_cb_t func = cb->f.vcpu_syscall;
365 
366         func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8);
367     }
368 }
369 
370 /*
371  * Disable CFI checks.
372  * The callback function has been loaded from an external library so we do not
373  * have type information
374  */
375 QEMU_DISABLE_CFI
376 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
377 {
378     struct qemu_plugin_cb *cb, *next;
379     enum qemu_plugin_event ev = QEMU_PLUGIN_EV_VCPU_SYSCALL_RET;
380 
381     if (!test_bit(ev, cpu->plugin_mask)) {
382         return;
383     }
384 
385     QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
386         qemu_plugin_vcpu_syscall_ret_cb_t func = cb->f.vcpu_syscall_ret;
387 
388         func(cb->ctx->id, cpu->cpu_index, num, ret);
389     }
390 }
391 
392 void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
393 {
394     /* idle and resume cb may be called before init, ignore in this case */
395     if (cpu->cpu_index < plugin.num_vcpus) {
396         plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE);
397     }
398 }
399 
400 void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
401 {
402     if (cpu->cpu_index < plugin.num_vcpus) {
403         plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
404     }
405 }
406 
407 void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
408                                        qemu_plugin_vcpu_simple_cb_t cb)
409 {
410     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
411 }
412 
413 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
414                                          qemu_plugin_vcpu_simple_cb_t cb)
415 {
416     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
417 }
418 
419 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
420                                    qemu_plugin_simple_cb_t cb)
421 {
422     plugin_register_cb(id, QEMU_PLUGIN_EV_FLUSH, cb);
423 }
424 
425 static bool free_dyn_cb_arr(void *p, uint32_t h, void *userp)
426 {
427     g_array_free((GArray *) p, true);
428     return true;
429 }
430 
431 void qemu_plugin_flush_cb(void)
432 {
433     qht_iter_remove(&plugin.dyn_cb_arr_ht, free_dyn_cb_arr, NULL);
434     qht_reset(&plugin.dyn_cb_arr_ht);
435 
436     plugin_cb__simple(QEMU_PLUGIN_EV_FLUSH);
437 }
438 
439 void exec_inline_op(struct qemu_plugin_dyn_cb *cb)
440 {
441     uint64_t *val = cb->userp;
442 
443     switch (cb->inline_insn.op) {
444     case QEMU_PLUGIN_INLINE_ADD_U64:
445         *val += cb->inline_insn.imm;
446         break;
447     default:
448         g_assert_not_reached();
449     }
450 }
451 
452 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
453                              MemOpIdx oi, enum qemu_plugin_mem_rw rw)
454 {
455     GArray *arr = cpu->plugin_mem_cbs;
456     size_t i;
457 
458     if (arr == NULL) {
459         return;
460     }
461     for (i = 0; i < arr->len; i++) {
462         struct qemu_plugin_dyn_cb *cb =
463             &g_array_index(arr, struct qemu_plugin_dyn_cb, i);
464 
465         if (!(rw & cb->rw)) {
466                 break;
467         }
468         switch (cb->type) {
469         case PLUGIN_CB_REGULAR:
470             cb->f.vcpu_mem(cpu->cpu_index, make_plugin_meminfo(oi, rw),
471                            vaddr, cb->userp);
472             break;
473         case PLUGIN_CB_INLINE:
474             exec_inline_op(cb);
475             break;
476         default:
477             g_assert_not_reached();
478         }
479     }
480 }
481 
482 void qemu_plugin_atexit_cb(void)
483 {
484     plugin_cb__udata(QEMU_PLUGIN_EV_ATEXIT);
485 }
486 
487 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
488                                     qemu_plugin_udata_cb_t cb,
489                                     void *udata)
490 {
491     plugin_register_cb_udata(id, QEMU_PLUGIN_EV_ATEXIT, cb, udata);
492 }
493 
494 /*
495  * Handle exit from linux-user. Unlike the normal atexit() mechanism
496  * we need to handle the clean-up manually as it's possible threads
497  * are still running. We need to remove all callbacks from code
498  * generation, flush the current translations and then we can safely
499  * trigger the exit callbacks.
500  */
501 
502 void qemu_plugin_user_exit(void)
503 {
504     enum qemu_plugin_event ev;
505     CPUState *cpu;
506 
507     /*
508      * Locking order: we must acquire locks in an order that is consistent
509      * with the one in fork_start(). That is:
510      * - start_exclusive(), which acquires qemu_cpu_list_lock,
511      *   must be called before acquiring plugin.lock.
512      * - tb_flush(), which acquires mmap_lock(), must be called
513      *   while plugin.lock is not held.
514      */
515     start_exclusive();
516 
517     qemu_rec_mutex_lock(&plugin.lock);
518     /* un-register all callbacks except the final AT_EXIT one */
519     for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
520         if (ev != QEMU_PLUGIN_EV_ATEXIT) {
521             struct qemu_plugin_cb *cb, *next;
522 
523             QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) {
524                 plugin_unregister_cb__locked(cb->ctx, ev);
525             }
526         }
527     }
528     CPU_FOREACH(cpu) {
529         qemu_plugin_disable_mem_helpers(cpu);
530     }
531     qemu_rec_mutex_unlock(&plugin.lock);
532 
533     tb_flush(current_cpu);
534     end_exclusive();
535 
536     /* now it's safe to handle the exit case */
537     qemu_plugin_atexit_cb();
538 }
539 
540 /*
541  * Helpers for *-user to ensure locks are sane across fork() events.
542  */
543 
544 void qemu_plugin_user_prefork_lock(void)
545 {
546     qemu_rec_mutex_lock(&plugin.lock);
547 }
548 
549 void qemu_plugin_user_postfork(bool is_child)
550 {
551     if (is_child) {
552         /* should we just reset via plugin_init? */
553         qemu_rec_mutex_init(&plugin.lock);
554     } else {
555         qemu_rec_mutex_unlock(&plugin.lock);
556     }
557 }
558 
559 static bool plugin_dyn_cb_arr_cmp(const void *ap, const void *bp)
560 {
561     return ap == bp;
562 }
563 
564 static void __attribute__((__constructor__)) plugin_init(void)
565 {
566     int i;
567 
568     for (i = 0; i < QEMU_PLUGIN_EV_MAX; i++) {
569         QLIST_INIT(&plugin.cb_lists[i]);
570     }
571     qemu_rec_mutex_init(&plugin.lock);
572     plugin.id_ht = g_hash_table_new(g_int64_hash, g_int64_equal);
573     plugin.cpu_ht = g_hash_table_new(g_int_hash, g_int_equal);
574     QTAILQ_INIT(&plugin.ctxs);
575     qht_init(&plugin.dyn_cb_arr_ht, plugin_dyn_cb_arr_cmp, 16,
576              QHT_MODE_AUTO_RESIZE);
577     atexit(qemu_plugin_atexit_cb);
578 }
579 
580 int plugin_num_vcpus(void)
581 {
582     return plugin.num_vcpus;
583 }
584