xref: /qemu/accel/tcg/plugin-gen.c (revision 401e311f)
1 /*
2  * plugin-gen.c - TCG-related bits of plugin infrastructure
3  *
4  * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
5  * License: GNU GPL, version 2 or later.
6  *   See the COPYING file in the top-level directory.
7  *
8  * We support instrumentation at an instruction granularity. That is,
9  * if a plugin wants to instrument the memory accesses performed by a
10  * particular instruction, it can just do that instead of instrumenting
11  * all memory accesses. Thus, in order to do this we first have to
12  * translate a TB, so that plugins can decide what/where to instrument.
13  *
14  * Injecting the desired instrumentation could be done with a second
15  * translation pass that combined the instrumentation requests, but that
16  * would be ugly and inefficient since we would decode the guest code twice.
17  * Instead, during TB translation we add "empty" instrumentation calls for all
18  * possible instrumentation events, and then once we collect the instrumentation
19  * requests from plugins, we either "fill in" those empty events or remove them
20  * if they have no requests.
21  *
22  * When "filling in" an event we first copy the empty callback's TCG ops. This
23  * might seem unnecessary, but it is done to support an arbitrary number
24  * of callbacks per event. Take for example a regular instruction callback.
25  * We first generate a callback to an empty helper function. Then, if two
26  * plugins register one callback each for this instruction, we make two copies
27  * of the TCG ops generated for the empty callback, substituting the function
28  * pointer that points to the empty helper function with the plugins' desired
29  * callback functions. After that we remove the empty callback's ops.
30  *
31  * Note that the location in TCGOp.args[] of the pointer to a helper function
32  * varies across different guest and host architectures. Instead of duplicating
33  * the logic that figures this out, we rely on the fact that the empty
34  * callbacks point to empty functions that are unique pointers in the program.
35  * Thus, to find the right location we just have to look for a match in
36  * TCGOp.args[]. This is the main reason why we first copy an empty callback's
37  * TCG ops and then fill them in; regardless of whether we have one or many
38  * callbacks for that event, the logic to add all of them is the same.
39  *
40  * When generating more than one callback per event, we make a small
41  * optimization to avoid generating redundant operations. For instance, for the
42  * second and all subsequent callbacks of an event, we do not need to reload the
43  * CPU's index into a TCG temp, since the first callback did it already.
44  */
45 #include "qemu/osdep.h"
46 #include "qemu/plugin.h"
47 #include "cpu.h"
48 #include "tcg/tcg.h"
49 #include "tcg/tcg-temp-internal.h"
50 #include "tcg/tcg-op.h"
51 #include "exec/exec-all.h"
52 #include "exec/plugin-gen.h"
53 #include "exec/translator.h"
54 #include "exec/helper-proto-common.h"
55 
56 #define HELPER_H  "accel/tcg/plugin-helpers.h"
57 #include "exec/helper-info.c.inc"
58 #undef  HELPER_H
59 
60 #ifdef CONFIG_SOFTMMU
61 # define CONFIG_SOFTMMU_GATE 1
62 #else
63 # define CONFIG_SOFTMMU_GATE 0
64 #endif
65 
66 /*
67  * plugin_cb_start TCG op args[]:
68  * 0: enum plugin_gen_from
69  * 1: enum plugin_gen_cb
70  * 2: set to 1 for mem callback that is a write, 0 otherwise.
71  */
72 
73 enum plugin_gen_from {
74     PLUGIN_GEN_FROM_TB,
75     PLUGIN_GEN_FROM_INSN,
76     PLUGIN_GEN_FROM_MEM,
77     PLUGIN_GEN_AFTER_INSN,
78     PLUGIN_GEN_N_FROMS,
79 };
80 
81 enum plugin_gen_cb {
82     PLUGIN_GEN_CB_UDATA,
83     PLUGIN_GEN_CB_UDATA_R,
84     PLUGIN_GEN_CB_INLINE,
85     PLUGIN_GEN_CB_MEM,
86     PLUGIN_GEN_ENABLE_MEM_HELPER,
87     PLUGIN_GEN_DISABLE_MEM_HELPER,
88     PLUGIN_GEN_N_CBS,
89 };
90 
91 /*
92  * These helpers are stubs that get dynamically switched out for calls
93  * direct to the plugin if they are subscribed to.
94  */
95 void HELPER(plugin_vcpu_udata_cb_no_wg)(uint32_t cpu_index, void *udata)
96 { }
97 
98 void HELPER(plugin_vcpu_udata_cb_no_rwg)(uint32_t cpu_index, void *udata)
99 { }
100 
101 void HELPER(plugin_vcpu_mem_cb)(unsigned int vcpu_index,
102                                 qemu_plugin_meminfo_t info, uint64_t vaddr,
103                                 void *userdata)
104 { }
105 
106 static void gen_empty_udata_cb(void (*gen_helper)(TCGv_i32, TCGv_ptr))
107 {
108     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
109     TCGv_ptr udata = tcg_temp_ebb_new_ptr();
110 
111     tcg_gen_movi_ptr(udata, 0);
112     tcg_gen_ld_i32(cpu_index, tcg_env,
113                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
114     gen_helper(cpu_index, udata);
115 
116     tcg_temp_free_ptr(udata);
117     tcg_temp_free_i32(cpu_index);
118 }
119 
120 static void gen_empty_udata_cb_no_wg(void)
121 {
122     gen_empty_udata_cb(gen_helper_plugin_vcpu_udata_cb_no_wg);
123 }
124 
125 static void gen_empty_udata_cb_no_rwg(void)
126 {
127     gen_empty_udata_cb(gen_helper_plugin_vcpu_udata_cb_no_rwg);
128 }
129 
130 /*
131  * For now we only support addi_i64.
132  * When we support more ops, we can generate one empty inline cb for each.
133  */
134 static void gen_empty_inline_cb(void)
135 {
136     TCGv_i64 val = tcg_temp_ebb_new_i64();
137     TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
138 
139     tcg_gen_movi_ptr(ptr, 0);
140     tcg_gen_ld_i64(val, ptr, 0);
141     /* pass an immediate != 0 so that it doesn't get optimized away */
142     tcg_gen_addi_i64(val, val, 0xdeadface);
143     tcg_gen_st_i64(val, ptr, 0);
144     tcg_temp_free_ptr(ptr);
145     tcg_temp_free_i64(val);
146 }
147 
148 static void gen_empty_mem_cb(TCGv_i64 addr, uint32_t info)
149 {
150     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
151     TCGv_i32 meminfo = tcg_temp_ebb_new_i32();
152     TCGv_ptr udata = tcg_temp_ebb_new_ptr();
153 
154     tcg_gen_movi_i32(meminfo, info);
155     tcg_gen_movi_ptr(udata, 0);
156     tcg_gen_ld_i32(cpu_index, tcg_env,
157                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
158 
159     gen_helper_plugin_vcpu_mem_cb(cpu_index, meminfo, addr, udata);
160 
161     tcg_temp_free_ptr(udata);
162     tcg_temp_free_i32(meminfo);
163     tcg_temp_free_i32(cpu_index);
164 }
165 
166 /*
167  * Share the same function for enable/disable. When enabling, the NULL
168  * pointer will be overwritten later.
169  */
170 static void gen_empty_mem_helper(void)
171 {
172     TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
173 
174     tcg_gen_movi_ptr(ptr, 0);
175     tcg_gen_st_ptr(ptr, tcg_env, offsetof(CPUState, plugin_mem_cbs) -
176                                  offsetof(ArchCPU, env));
177     tcg_temp_free_ptr(ptr);
178 }
179 
180 static void gen_plugin_cb_start(enum plugin_gen_from from,
181                                 enum plugin_gen_cb type, unsigned wr)
182 {
183     tcg_gen_plugin_cb_start(from, type, wr);
184 }
185 
186 static void gen_wrapped(enum plugin_gen_from from,
187                         enum plugin_gen_cb type, void (*func)(void))
188 {
189     gen_plugin_cb_start(from, type, 0);
190     func();
191     tcg_gen_plugin_cb_end();
192 }
193 
194 static void plugin_gen_empty_callback(enum plugin_gen_from from)
195 {
196     switch (from) {
197     case PLUGIN_GEN_AFTER_INSN:
198         gen_wrapped(from, PLUGIN_GEN_DISABLE_MEM_HELPER,
199                     gen_empty_mem_helper);
200         break;
201     case PLUGIN_GEN_FROM_INSN:
202         /*
203          * Note: plugin_gen_inject() relies on ENABLE_MEM_HELPER being
204          * the first callback of an instruction
205          */
206         gen_wrapped(from, PLUGIN_GEN_ENABLE_MEM_HELPER,
207                     gen_empty_mem_helper);
208         /* fall through */
209     case PLUGIN_GEN_FROM_TB:
210         gen_wrapped(from, PLUGIN_GEN_CB_UDATA, gen_empty_udata_cb_no_rwg);
211         gen_wrapped(from, PLUGIN_GEN_CB_UDATA_R, gen_empty_udata_cb_no_wg);
212         gen_wrapped(from, PLUGIN_GEN_CB_INLINE, gen_empty_inline_cb);
213         break;
214     default:
215         g_assert_not_reached();
216     }
217 }
218 
219 void plugin_gen_empty_mem_callback(TCGv_i64 addr, uint32_t info)
220 {
221     enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
222 
223     gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_MEM, rw);
224     gen_empty_mem_cb(addr, info);
225     tcg_gen_plugin_cb_end();
226 
227     gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_INLINE, rw);
228     gen_empty_inline_cb();
229     tcg_gen_plugin_cb_end();
230 }
231 
232 static TCGOp *find_op(TCGOp *op, TCGOpcode opc)
233 {
234     while (op) {
235         if (op->opc == opc) {
236             return op;
237         }
238         op = QTAILQ_NEXT(op, link);
239     }
240     return NULL;
241 }
242 
243 static TCGOp *rm_ops_range(TCGOp *begin, TCGOp *end)
244 {
245     TCGOp *ret = QTAILQ_NEXT(end, link);
246 
247     QTAILQ_REMOVE_SEVERAL(&tcg_ctx->ops, begin, end, link);
248     return ret;
249 }
250 
251 /* remove all ops until (and including) plugin_cb_end */
252 static TCGOp *rm_ops(TCGOp *op)
253 {
254     TCGOp *end_op = find_op(op, INDEX_op_plugin_cb_end);
255 
256     tcg_debug_assert(end_op);
257     return rm_ops_range(op, end_op);
258 }
259 
260 static TCGOp *copy_op_nocheck(TCGOp **begin_op, TCGOp *op)
261 {
262     TCGOp *old_op = QTAILQ_NEXT(*begin_op, link);
263     unsigned nargs = old_op->nargs;
264 
265     *begin_op = old_op;
266     op = tcg_op_insert_after(tcg_ctx, op, old_op->opc, nargs);
267     memcpy(op->args, old_op->args, sizeof(op->args[0]) * nargs);
268 
269     return op;
270 }
271 
272 static TCGOp *copy_op(TCGOp **begin_op, TCGOp *op, TCGOpcode opc)
273 {
274     op = copy_op_nocheck(begin_op, op);
275     tcg_debug_assert((*begin_op)->opc == opc);
276     return op;
277 }
278 
279 static TCGOp *copy_const_ptr(TCGOp **begin_op, TCGOp *op, void *ptr)
280 {
281     if (UINTPTR_MAX == UINT32_MAX) {
282         /* mov_i32 */
283         op = copy_op(begin_op, op, INDEX_op_mov_i32);
284         op->args[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr));
285     } else {
286         /* mov_i64 */
287         op = copy_op(begin_op, op, INDEX_op_mov_i64);
288         op->args[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr));
289     }
290     return op;
291 }
292 
293 static TCGOp *copy_ld_i64(TCGOp **begin_op, TCGOp *op)
294 {
295     if (TCG_TARGET_REG_BITS == 32) {
296         /* 2x ld_i32 */
297         op = copy_op(begin_op, op, INDEX_op_ld_i32);
298         op = copy_op(begin_op, op, INDEX_op_ld_i32);
299     } else {
300         /* ld_i64 */
301         op = copy_op(begin_op, op, INDEX_op_ld_i64);
302     }
303     return op;
304 }
305 
306 static TCGOp *copy_st_i64(TCGOp **begin_op, TCGOp *op)
307 {
308     if (TCG_TARGET_REG_BITS == 32) {
309         /* 2x st_i32 */
310         op = copy_op(begin_op, op, INDEX_op_st_i32);
311         op = copy_op(begin_op, op, INDEX_op_st_i32);
312     } else {
313         /* st_i64 */
314         op = copy_op(begin_op, op, INDEX_op_st_i64);
315     }
316     return op;
317 }
318 
319 static TCGOp *copy_add_i64(TCGOp **begin_op, TCGOp *op, uint64_t v)
320 {
321     if (TCG_TARGET_REG_BITS == 32) {
322         /* all 32-bit backends must implement add2_i32 */
323         g_assert(TCG_TARGET_HAS_add2_i32);
324         op = copy_op(begin_op, op, INDEX_op_add2_i32);
325         op->args[4] = tcgv_i32_arg(tcg_constant_i32(v));
326         op->args[5] = tcgv_i32_arg(tcg_constant_i32(v >> 32));
327     } else {
328         op = copy_op(begin_op, op, INDEX_op_add_i64);
329         op->args[2] = tcgv_i64_arg(tcg_constant_i64(v));
330     }
331     return op;
332 }
333 
334 static TCGOp *copy_st_ptr(TCGOp **begin_op, TCGOp *op)
335 {
336     if (UINTPTR_MAX == UINT32_MAX) {
337         /* st_i32 */
338         op = copy_op(begin_op, op, INDEX_op_st_i32);
339     } else {
340         /* st_i64 */
341         op = copy_st_i64(begin_op, op);
342     }
343     return op;
344 }
345 
346 static TCGOp *copy_call(TCGOp **begin_op, TCGOp *op, void *func, int *cb_idx)
347 {
348     TCGOp *old_op;
349     int func_idx;
350 
351     /* copy all ops until the call */
352     do {
353         op = copy_op_nocheck(begin_op, op);
354     } while (op->opc != INDEX_op_call);
355 
356     /* fill in the op call */
357     old_op = *begin_op;
358     TCGOP_CALLI(op) = TCGOP_CALLI(old_op);
359     TCGOP_CALLO(op) = TCGOP_CALLO(old_op);
360     tcg_debug_assert(op->life == 0);
361 
362     func_idx = TCGOP_CALLO(op) + TCGOP_CALLI(op);
363     *cb_idx = func_idx;
364     op->args[func_idx] = (uintptr_t)func;
365 
366     return op;
367 }
368 
369 /*
370  * When we append/replace ops here we are sensitive to changing patterns of
371  * TCGOps generated by the tcg_gen_FOO calls when we generated the
372  * empty callbacks. This will assert very quickly in a debug build as
373  * we assert the ops we are replacing are the correct ones.
374  */
375 static TCGOp *append_udata_cb(const struct qemu_plugin_dyn_cb *cb,
376                               TCGOp *begin_op, TCGOp *op, int *cb_idx)
377 {
378     /* const_ptr */
379     op = copy_const_ptr(&begin_op, op, cb->userp);
380 
381     /* copy the ld_i32, but note that we only have to copy it once */
382     if (*cb_idx == -1) {
383         op = copy_op(&begin_op, op, INDEX_op_ld_i32);
384     } else {
385         begin_op = QTAILQ_NEXT(begin_op, link);
386         tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32);
387     }
388 
389     /* call */
390     op = copy_call(&begin_op, op, cb->f.vcpu_udata, cb_idx);
391 
392     return op;
393 }
394 
395 static TCGOp *append_inline_cb(const struct qemu_plugin_dyn_cb *cb,
396                                TCGOp *begin_op, TCGOp *op,
397                                int *unused)
398 {
399     /* const_ptr */
400     op = copy_const_ptr(&begin_op, op, cb->userp);
401 
402     /* ld_i64 */
403     op = copy_ld_i64(&begin_op, op);
404 
405     /* add_i64 */
406     op = copy_add_i64(&begin_op, op, cb->inline_insn.imm);
407 
408     /* st_i64 */
409     op = copy_st_i64(&begin_op, op);
410 
411     return op;
412 }
413 
414 static TCGOp *append_mem_cb(const struct qemu_plugin_dyn_cb *cb,
415                             TCGOp *begin_op, TCGOp *op, int *cb_idx)
416 {
417     enum plugin_gen_cb type = begin_op->args[1];
418 
419     tcg_debug_assert(type == PLUGIN_GEN_CB_MEM);
420 
421     /* const_i32 == mov_i32 ("info", so it remains as is) */
422     op = copy_op(&begin_op, op, INDEX_op_mov_i32);
423 
424     /* const_ptr */
425     op = copy_const_ptr(&begin_op, op, cb->userp);
426 
427     /* copy the ld_i32, but note that we only have to copy it once */
428     if (*cb_idx == -1) {
429         op = copy_op(&begin_op, op, INDEX_op_ld_i32);
430     } else {
431         begin_op = QTAILQ_NEXT(begin_op, link);
432         tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32);
433     }
434 
435     if (type == PLUGIN_GEN_CB_MEM) {
436         /* call */
437         op = copy_call(&begin_op, op, cb->f.vcpu_udata, cb_idx);
438     }
439 
440     return op;
441 }
442 
443 typedef TCGOp *(*inject_fn)(const struct qemu_plugin_dyn_cb *cb,
444                             TCGOp *begin_op, TCGOp *op, int *intp);
445 typedef bool (*op_ok_fn)(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb);
446 
447 static bool op_ok(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb)
448 {
449     return true;
450 }
451 
452 static bool op_rw(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb)
453 {
454     int w;
455 
456     w = op->args[2];
457     return !!(cb->rw & (w + 1));
458 }
459 
460 static void inject_cb_type(const GArray *cbs, TCGOp *begin_op,
461                            inject_fn inject, op_ok_fn ok)
462 {
463     TCGOp *end_op;
464     TCGOp *op;
465     int cb_idx = -1;
466     int i;
467 
468     if (!cbs || cbs->len == 0) {
469         rm_ops(begin_op);
470         return;
471     }
472 
473     end_op = find_op(begin_op, INDEX_op_plugin_cb_end);
474     tcg_debug_assert(end_op);
475 
476     op = end_op;
477     for (i = 0; i < cbs->len; i++) {
478         struct qemu_plugin_dyn_cb *cb =
479             &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
480 
481         if (!ok(begin_op, cb)) {
482             continue;
483         }
484         op = inject(cb, begin_op, op, &cb_idx);
485     }
486     rm_ops_range(begin_op, end_op);
487 }
488 
489 static void
490 inject_udata_cb(const GArray *cbs, TCGOp *begin_op)
491 {
492     inject_cb_type(cbs, begin_op, append_udata_cb, op_ok);
493 }
494 
495 static void
496 inject_inline_cb(const GArray *cbs, TCGOp *begin_op, op_ok_fn ok)
497 {
498     inject_cb_type(cbs, begin_op, append_inline_cb, ok);
499 }
500 
501 static void
502 inject_mem_cb(const GArray *cbs, TCGOp *begin_op)
503 {
504     inject_cb_type(cbs, begin_op, append_mem_cb, op_rw);
505 }
506 
507 /* we could change the ops in place, but we can reuse more code by copying */
508 static void inject_mem_helper(TCGOp *begin_op, GArray *arr)
509 {
510     TCGOp *orig_op = begin_op;
511     TCGOp *end_op;
512     TCGOp *op;
513 
514     end_op = find_op(begin_op, INDEX_op_plugin_cb_end);
515     tcg_debug_assert(end_op);
516 
517     /* const ptr */
518     op = copy_const_ptr(&begin_op, end_op, arr);
519 
520     /* st_ptr */
521     op = copy_st_ptr(&begin_op, op);
522 
523     rm_ops_range(orig_op, end_op);
524 }
525 
526 /*
527  * Tracking memory accesses performed from helpers requires extra work.
528  * If an instruction is emulated with helpers, we do two things:
529  * (1) copy the CB descriptors, and keep track of it so that they can be
530  * freed later on, and (2) point CPUState.plugin_mem_cbs to the descriptors, so
531  * that we can read them at run-time (i.e. when the helper executes).
532  * This run-time access is performed from qemu_plugin_vcpu_mem_cb.
533  *
534  * Note that plugin_gen_disable_mem_helpers undoes (2). Since it
535  * is possible that the code we generate after the instruction is
536  * dead, we also add checks before generating tb_exit etc.
537  */
538 static void inject_mem_enable_helper(struct qemu_plugin_tb *ptb,
539                                      struct qemu_plugin_insn *plugin_insn,
540                                      TCGOp *begin_op)
541 {
542     GArray *cbs[2];
543     GArray *arr;
544     size_t n_cbs, i;
545 
546     cbs[0] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR];
547     cbs[1] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE];
548 
549     n_cbs = 0;
550     for (i = 0; i < ARRAY_SIZE(cbs); i++) {
551         n_cbs += cbs[i]->len;
552     }
553 
554     plugin_insn->mem_helper = plugin_insn->calls_helpers && n_cbs;
555     if (likely(!plugin_insn->mem_helper)) {
556         rm_ops(begin_op);
557         return;
558     }
559     ptb->mem_helper = true;
560 
561     arr = g_array_sized_new(false, false,
562                             sizeof(struct qemu_plugin_dyn_cb), n_cbs);
563 
564     for (i = 0; i < ARRAY_SIZE(cbs); i++) {
565         g_array_append_vals(arr, cbs[i]->data, cbs[i]->len);
566     }
567 
568     qemu_plugin_add_dyn_cb_arr(arr);
569     inject_mem_helper(begin_op, arr);
570 }
571 
572 static void inject_mem_disable_helper(struct qemu_plugin_insn *plugin_insn,
573                                       TCGOp *begin_op)
574 {
575     if (likely(!plugin_insn->mem_helper)) {
576         rm_ops(begin_op);
577         return;
578     }
579     inject_mem_helper(begin_op, NULL);
580 }
581 
582 /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */
583 void plugin_gen_disable_mem_helpers(void)
584 {
585     /*
586      * We could emit the clearing unconditionally and be done. However, this can
587      * be wasteful if for instance plugins don't track memory accesses, or if
588      * most TBs don't use helpers. Instead, emit the clearing iff the TB calls
589      * helpers that might access guest memory.
590      *
591      * Note: we do not reset plugin_tb->mem_helper here; a TB might have several
592      * exit points, and we want to emit the clearing from all of them.
593      */
594     if (!tcg_ctx->plugin_tb->mem_helper) {
595         return;
596     }
597     tcg_gen_st_ptr(tcg_constant_ptr(NULL), tcg_env,
598                    offsetof(CPUState, plugin_mem_cbs) - offsetof(ArchCPU, env));
599 }
600 
601 static void plugin_gen_tb_udata(const struct qemu_plugin_tb *ptb,
602                                 TCGOp *begin_op)
603 {
604     inject_udata_cb(ptb->cbs[PLUGIN_CB_REGULAR], begin_op);
605 }
606 
607 static void plugin_gen_tb_udata_r(const struct qemu_plugin_tb *ptb,
608                                   TCGOp *begin_op)
609 {
610     inject_udata_cb(ptb->cbs[PLUGIN_CB_REGULAR_R], begin_op);
611 }
612 
613 static void plugin_gen_tb_inline(const struct qemu_plugin_tb *ptb,
614                                  TCGOp *begin_op)
615 {
616     inject_inline_cb(ptb->cbs[PLUGIN_CB_INLINE], begin_op, op_ok);
617 }
618 
619 static void plugin_gen_insn_udata(const struct qemu_plugin_tb *ptb,
620                                   TCGOp *begin_op, int insn_idx)
621 {
622     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
623 
624     inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR], begin_op);
625 }
626 
627 static void plugin_gen_insn_udata_r(const struct qemu_plugin_tb *ptb,
628                                     TCGOp *begin_op, int insn_idx)
629 {
630     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
631 
632     inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR_R], begin_op);
633 }
634 
635 static void plugin_gen_insn_inline(const struct qemu_plugin_tb *ptb,
636                                    TCGOp *begin_op, int insn_idx)
637 {
638     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
639     inject_inline_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE],
640                      begin_op, op_ok);
641 }
642 
643 static void plugin_gen_mem_regular(const struct qemu_plugin_tb *ptb,
644                                    TCGOp *begin_op, int insn_idx)
645 {
646     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
647     inject_mem_cb(insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR], begin_op);
648 }
649 
650 static void plugin_gen_mem_inline(const struct qemu_plugin_tb *ptb,
651                                   TCGOp *begin_op, int insn_idx)
652 {
653     const GArray *cbs;
654     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
655 
656     cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE];
657     inject_inline_cb(cbs, begin_op, op_rw);
658 }
659 
660 static void plugin_gen_enable_mem_helper(struct qemu_plugin_tb *ptb,
661                                          TCGOp *begin_op, int insn_idx)
662 {
663     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
664     inject_mem_enable_helper(ptb, insn, begin_op);
665 }
666 
667 static void plugin_gen_disable_mem_helper(struct qemu_plugin_tb *ptb,
668                                           TCGOp *begin_op, int insn_idx)
669 {
670     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
671     inject_mem_disable_helper(insn, begin_op);
672 }
673 
674 /* #define DEBUG_PLUGIN_GEN_OPS */
675 static void pr_ops(void)
676 {
677 #ifdef DEBUG_PLUGIN_GEN_OPS
678     TCGOp *op;
679     int i = 0;
680 
681     QTAILQ_FOREACH(op, &tcg_ctx->ops, link) {
682         const char *name = "";
683         const char *type = "";
684 
685         if (op->opc == INDEX_op_plugin_cb_start) {
686             switch (op->args[0]) {
687             case PLUGIN_GEN_FROM_TB:
688                 name = "tb";
689                 break;
690             case PLUGIN_GEN_FROM_INSN:
691                 name = "insn";
692                 break;
693             case PLUGIN_GEN_FROM_MEM:
694                 name = "mem";
695                 break;
696             case PLUGIN_GEN_AFTER_INSN:
697                 name = "after insn";
698                 break;
699             default:
700                 break;
701             }
702             switch (op->args[1]) {
703             case PLUGIN_GEN_CB_UDATA:
704                 type = "udata";
705                 break;
706             case PLUGIN_GEN_CB_INLINE:
707                 type = "inline";
708                 break;
709             case PLUGIN_GEN_CB_MEM:
710                 type = "mem";
711                 break;
712             case PLUGIN_GEN_ENABLE_MEM_HELPER:
713                 type = "enable mem helper";
714                 break;
715             case PLUGIN_GEN_DISABLE_MEM_HELPER:
716                 type = "disable mem helper";
717                 break;
718             default:
719                 break;
720             }
721         }
722         printf("op[%2i]: %s %s %s\n", i, tcg_op_defs[op->opc].name, name, type);
723         i++;
724     }
725 #endif
726 }
727 
728 static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb)
729 {
730     TCGOp *op;
731     int insn_idx = -1;
732 
733     pr_ops();
734 
735     QTAILQ_FOREACH(op, &tcg_ctx->ops, link) {
736         switch (op->opc) {
737         case INDEX_op_insn_start:
738             insn_idx++;
739             break;
740         case INDEX_op_plugin_cb_start:
741         {
742             enum plugin_gen_from from = op->args[0];
743             enum plugin_gen_cb type = op->args[1];
744 
745             switch (from) {
746             case PLUGIN_GEN_FROM_TB:
747             {
748                 g_assert(insn_idx == -1);
749 
750                 switch (type) {
751                 case PLUGIN_GEN_CB_UDATA:
752                     plugin_gen_tb_udata(plugin_tb, op);
753                     break;
754                 case PLUGIN_GEN_CB_UDATA_R:
755                     plugin_gen_tb_udata_r(plugin_tb, op);
756                     break;
757                 case PLUGIN_GEN_CB_INLINE:
758                     plugin_gen_tb_inline(plugin_tb, op);
759                     break;
760                 default:
761                     g_assert_not_reached();
762                 }
763                 break;
764             }
765             case PLUGIN_GEN_FROM_INSN:
766             {
767                 g_assert(insn_idx >= 0);
768 
769                 switch (type) {
770                 case PLUGIN_GEN_CB_UDATA:
771                     plugin_gen_insn_udata(plugin_tb, op, insn_idx);
772                     break;
773                 case PLUGIN_GEN_CB_UDATA_R:
774                     plugin_gen_insn_udata_r(plugin_tb, op, insn_idx);
775                     break;
776                 case PLUGIN_GEN_CB_INLINE:
777                     plugin_gen_insn_inline(plugin_tb, op, insn_idx);
778                     break;
779                 case PLUGIN_GEN_ENABLE_MEM_HELPER:
780                     plugin_gen_enable_mem_helper(plugin_tb, op, insn_idx);
781                     break;
782                 default:
783                     g_assert_not_reached();
784                 }
785                 break;
786             }
787             case PLUGIN_GEN_FROM_MEM:
788             {
789                 g_assert(insn_idx >= 0);
790 
791                 switch (type) {
792                 case PLUGIN_GEN_CB_MEM:
793                     plugin_gen_mem_regular(plugin_tb, op, insn_idx);
794                     break;
795                 case PLUGIN_GEN_CB_INLINE:
796                     plugin_gen_mem_inline(plugin_tb, op, insn_idx);
797                     break;
798                 default:
799                     g_assert_not_reached();
800                 }
801 
802                 break;
803             }
804             case PLUGIN_GEN_AFTER_INSN:
805             {
806                 g_assert(insn_idx >= 0);
807 
808                 switch (type) {
809                 case PLUGIN_GEN_DISABLE_MEM_HELPER:
810                     plugin_gen_disable_mem_helper(plugin_tb, op, insn_idx);
811                     break;
812                 default:
813                     g_assert_not_reached();
814                 }
815                 break;
816             }
817             default:
818                 g_assert_not_reached();
819             }
820             break;
821         }
822         default:
823             /* plugins don't care about any other ops */
824             break;
825         }
826     }
827     pr_ops();
828 }
829 
830 bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db,
831                          bool mem_only)
832 {
833     bool ret = false;
834 
835     if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_state->event_mask)) {
836         struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
837         int i;
838 
839         /* reset callbacks */
840         for (i = 0; i < PLUGIN_N_CB_SUBTYPES; i++) {
841             if (ptb->cbs[i]) {
842                 g_array_set_size(ptb->cbs[i], 0);
843             }
844         }
845         ptb->n = 0;
846 
847         ret = true;
848 
849         ptb->vaddr = db->pc_first;
850         ptb->vaddr2 = -1;
851         ptb->haddr1 = db->host_addr[0];
852         ptb->haddr2 = NULL;
853         ptb->mem_only = mem_only;
854         ptb->mem_helper = false;
855 
856         plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB);
857     }
858 
859     tcg_ctx->plugin_insn = NULL;
860 
861     return ret;
862 }
863 
864 void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db)
865 {
866     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
867     struct qemu_plugin_insn *pinsn;
868 
869     pinsn = qemu_plugin_tb_insn_get(ptb, db->pc_next);
870     tcg_ctx->plugin_insn = pinsn;
871     plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN);
872 
873     /*
874      * Detect page crossing to get the new host address.
875      * Note that we skip this when haddr1 == NULL, e.g. when we're
876      * fetching instructions from a region not backed by RAM.
877      */
878     if (ptb->haddr1 == NULL) {
879         pinsn->haddr = NULL;
880     } else if (is_same_page(db, db->pc_next)) {
881         pinsn->haddr = ptb->haddr1 + pinsn->vaddr - ptb->vaddr;
882     } else {
883         if (ptb->vaddr2 == -1) {
884             ptb->vaddr2 = TARGET_PAGE_ALIGN(db->pc_first);
885             get_page_addr_code_hostp(cpu_env(cpu), ptb->vaddr2, &ptb->haddr2);
886         }
887         pinsn->haddr = ptb->haddr2 + pinsn->vaddr - ptb->vaddr2;
888     }
889 }
890 
891 void plugin_gen_insn_end(void)
892 {
893     plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN);
894 }
895 
896 /*
897  * There are cases where we never get to finalise a translation - for
898  * example a page fault during translation. As a result we shouldn't
899  * do any clean-up here and make sure things are reset in
900  * plugin_gen_tb_start.
901  */
902 void plugin_gen_tb_end(CPUState *cpu, size_t num_insns)
903 {
904     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
905 
906     /* translator may have removed instructions, update final count */
907     g_assert(num_insns <= ptb->n);
908     ptb->n = num_insns;
909 
910     /* collect instrumentation requests */
911     qemu_plugin_tb_trans_cb(cpu, ptb);
912 
913     /* inject the instrumentation at the appropriate places */
914     plugin_gen_inject(ptb);
915 }
916