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