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