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