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