2 * plugin-gen.c - TCG-related bits of plugin infrastructure
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.
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.
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.
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.
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.
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.
45 #include "qemu/osdep.h"
47 #include "tcg/tcg-temp-internal.h"
48 #include "tcg/tcg-op.h"
49 #include "exec/exec-all.h"
50 #include "exec/plugin-gen.h"
51 #include "exec/translator.h"
54 # define CONFIG_SOFTMMU_GATE 1
56 # define CONFIG_SOFTMMU_GATE 0
60 * plugin_cb_start TCG op args[]:
61 * 0: enum plugin_gen_from
62 * 1: enum plugin_gen_cb
63 * 2: set to 1 for mem callback that is a write, 0 otherwise.
66 enum plugin_gen_from
{
70 PLUGIN_GEN_AFTER_INSN
,
78 PLUGIN_GEN_ENABLE_MEM_HELPER
,
79 PLUGIN_GEN_DISABLE_MEM_HELPER
,
84 * These helpers are stubs that get dynamically switched out for calls
85 * direct to the plugin if they are subscribed to.
87 void HELPER(plugin_vcpu_udata_cb
)(uint32_t cpu_index
, void *udata
)
90 void HELPER(plugin_vcpu_mem_cb
)(unsigned int vcpu_index
,
91 qemu_plugin_meminfo_t info
, uint64_t vaddr
,
95 static void gen_empty_udata_cb(void)
97 TCGv_i32 cpu_index
= tcg_temp_ebb_new_i32();
98 TCGv_ptr udata
= tcg_temp_ebb_new_ptr();
100 tcg_gen_movi_ptr(udata
, 0);
101 tcg_gen_ld_i32(cpu_index
, cpu_env
,
102 -offsetof(ArchCPU
, env
) + offsetof(CPUState
, cpu_index
));
103 gen_helper_plugin_vcpu_udata_cb(cpu_index
, udata
);
105 tcg_temp_free_ptr(udata
);
106 tcg_temp_free_i32(cpu_index
);
110 * For now we only support addi_i64.
111 * When we support more ops, we can generate one empty inline cb for each.
113 static void gen_empty_inline_cb(void)
115 TCGv_i64 val
= tcg_temp_ebb_new_i64();
116 TCGv_ptr ptr
= tcg_temp_ebb_new_ptr();
118 tcg_gen_movi_ptr(ptr
, 0);
119 tcg_gen_ld_i64(val
, ptr
, 0);
120 /* pass an immediate != 0 so that it doesn't get optimized away */
121 tcg_gen_addi_i64(val
, val
, 0xdeadface);
122 tcg_gen_st_i64(val
, ptr
, 0);
123 tcg_temp_free_ptr(ptr
);
124 tcg_temp_free_i64(val
);
127 static void gen_empty_mem_cb(TCGv_i64 addr
, uint32_t info
)
129 TCGv_i32 cpu_index
= tcg_temp_ebb_new_i32();
130 TCGv_i32 meminfo
= tcg_temp_ebb_new_i32();
131 TCGv_ptr udata
= tcg_temp_ebb_new_ptr();
133 tcg_gen_movi_i32(meminfo
, info
);
134 tcg_gen_movi_ptr(udata
, 0);
135 tcg_gen_ld_i32(cpu_index
, cpu_env
,
136 -offsetof(ArchCPU
, env
) + offsetof(CPUState
, cpu_index
));
138 gen_helper_plugin_vcpu_mem_cb(cpu_index
, meminfo
, addr
, udata
);
140 tcg_temp_free_ptr(udata
);
141 tcg_temp_free_i32(meminfo
);
142 tcg_temp_free_i32(cpu_index
);
146 * Share the same function for enable/disable. When enabling, the NULL
147 * pointer will be overwritten later.
149 static void gen_empty_mem_helper(void)
151 TCGv_ptr ptr
= tcg_temp_ebb_new_ptr();
153 tcg_gen_movi_ptr(ptr
, 0);
154 tcg_gen_st_ptr(ptr
, cpu_env
, offsetof(CPUState
, plugin_mem_cbs
) -
155 offsetof(ArchCPU
, env
));
156 tcg_temp_free_ptr(ptr
);
159 static void gen_plugin_cb_start(enum plugin_gen_from from
,
160 enum plugin_gen_cb type
, unsigned wr
)
162 tcg_gen_plugin_cb_start(from
, type
, wr
);
165 static void gen_wrapped(enum plugin_gen_from from
,
166 enum plugin_gen_cb type
, void (*func
)(void))
168 gen_plugin_cb_start(from
, type
, 0);
170 tcg_gen_plugin_cb_end();
173 static void plugin_gen_empty_callback(enum plugin_gen_from from
)
176 case PLUGIN_GEN_AFTER_INSN
:
177 gen_wrapped(from
, PLUGIN_GEN_DISABLE_MEM_HELPER
,
178 gen_empty_mem_helper
);
180 case PLUGIN_GEN_FROM_INSN
:
182 * Note: plugin_gen_inject() relies on ENABLE_MEM_HELPER being
183 * the first callback of an instruction
185 gen_wrapped(from
, PLUGIN_GEN_ENABLE_MEM_HELPER
,
186 gen_empty_mem_helper
);
188 case PLUGIN_GEN_FROM_TB
:
189 gen_wrapped(from
, PLUGIN_GEN_CB_UDATA
, gen_empty_udata_cb
);
190 gen_wrapped(from
, PLUGIN_GEN_CB_INLINE
, gen_empty_inline_cb
);
193 g_assert_not_reached();
197 void plugin_gen_empty_mem_callback(TCGv_i64 addr
, uint32_t info
)
199 enum qemu_plugin_mem_rw rw
= get_plugin_meminfo_rw(info
);
201 gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM
, PLUGIN_GEN_CB_MEM
, rw
);
202 gen_empty_mem_cb(addr
, info
);
203 tcg_gen_plugin_cb_end();
205 gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM
, PLUGIN_GEN_CB_INLINE
, rw
);
206 gen_empty_inline_cb();
207 tcg_gen_plugin_cb_end();
210 static TCGOp
*find_op(TCGOp
*op
, TCGOpcode opc
)
213 if (op
->opc
== opc
) {
216 op
= QTAILQ_NEXT(op
, link
);
221 static TCGOp
*rm_ops_range(TCGOp
*begin
, TCGOp
*end
)
223 TCGOp
*ret
= QTAILQ_NEXT(end
, link
);
225 QTAILQ_REMOVE_SEVERAL(&tcg_ctx
->ops
, begin
, end
, link
);
229 /* remove all ops until (and including) plugin_cb_end */
230 static TCGOp
*rm_ops(TCGOp
*op
)
232 TCGOp
*end_op
= find_op(op
, INDEX_op_plugin_cb_end
);
234 tcg_debug_assert(end_op
);
235 return rm_ops_range(op
, end_op
);
238 static TCGOp
*copy_op_nocheck(TCGOp
**begin_op
, TCGOp
*op
)
240 TCGOp
*old_op
= QTAILQ_NEXT(*begin_op
, link
);
241 unsigned nargs
= old_op
->nargs
;
244 op
= tcg_op_insert_after(tcg_ctx
, op
, old_op
->opc
, nargs
);
245 memcpy(op
->args
, old_op
->args
, sizeof(op
->args
[0]) * nargs
);
250 static TCGOp
*copy_op(TCGOp
**begin_op
, TCGOp
*op
, TCGOpcode opc
)
252 op
= copy_op_nocheck(begin_op
, op
);
253 tcg_debug_assert((*begin_op
)->opc
== opc
);
257 static TCGOp
*copy_extu_i32_i64(TCGOp
**begin_op
, TCGOp
*op
)
259 if (TCG_TARGET_REG_BITS
== 32) {
261 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
263 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
266 op
= copy_op(begin_op
, op
, INDEX_op_extu_i32_i64
);
271 static TCGOp
*copy_mov_i64(TCGOp
**begin_op
, TCGOp
*op
)
273 if (TCG_TARGET_REG_BITS
== 32) {
275 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
276 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
279 op
= copy_op(begin_op
, op
, INDEX_op_mov_i64
);
284 static TCGOp
*copy_const_ptr(TCGOp
**begin_op
, TCGOp
*op
, void *ptr
)
286 if (UINTPTR_MAX
== UINT32_MAX
) {
288 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
289 op
->args
[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr
));
292 op
= copy_op(begin_op
, op
, INDEX_op_mov_i64
);
293 op
->args
[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr
));
298 static TCGOp
*copy_extu_tl_i64(TCGOp
**begin_op
, TCGOp
*op
)
300 if (TARGET_LONG_BITS
== 32) {
302 op
= copy_extu_i32_i64(begin_op
, op
);
305 op
= copy_mov_i64(begin_op
, op
);
310 static TCGOp
*copy_ld_i64(TCGOp
**begin_op
, TCGOp
*op
)
312 if (TCG_TARGET_REG_BITS
== 32) {
314 op
= copy_op(begin_op
, op
, INDEX_op_ld_i32
);
315 op
= copy_op(begin_op
, op
, INDEX_op_ld_i32
);
318 op
= copy_op(begin_op
, op
, INDEX_op_ld_i64
);
323 static TCGOp
*copy_st_i64(TCGOp
**begin_op
, TCGOp
*op
)
325 if (TCG_TARGET_REG_BITS
== 32) {
327 op
= copy_op(begin_op
, op
, INDEX_op_st_i32
);
328 op
= copy_op(begin_op
, op
, INDEX_op_st_i32
);
331 op
= copy_op(begin_op
, op
, INDEX_op_st_i64
);
336 static TCGOp
*copy_add_i64(TCGOp
**begin_op
, TCGOp
*op
, uint64_t v
)
338 if (TCG_TARGET_REG_BITS
== 32) {
339 /* all 32-bit backends must implement add2_i32 */
340 g_assert(TCG_TARGET_HAS_add2_i32
);
341 op
= copy_op(begin_op
, op
, INDEX_op_add2_i32
);
342 op
->args
[4] = tcgv_i32_arg(tcg_constant_i32(v
));
343 op
->args
[5] = tcgv_i32_arg(tcg_constant_i32(v
>> 32));
345 op
= copy_op(begin_op
, op
, INDEX_op_add_i64
);
346 op
->args
[2] = tcgv_i64_arg(tcg_constant_i64(v
));
351 static TCGOp
*copy_st_ptr(TCGOp
**begin_op
, TCGOp
*op
)
353 if (UINTPTR_MAX
== UINT32_MAX
) {
355 op
= copy_op(begin_op
, op
, INDEX_op_st_i32
);
358 op
= copy_st_i64(begin_op
, op
);
363 static TCGOp
*copy_call(TCGOp
**begin_op
, TCGOp
*op
, void *empty_func
,
364 void *func
, int *cb_idx
)
369 /* copy all ops until the call */
371 op
= copy_op_nocheck(begin_op
, op
);
372 } while (op
->opc
!= INDEX_op_call
);
374 /* fill in the op call */
376 TCGOP_CALLI(op
) = TCGOP_CALLI(old_op
);
377 TCGOP_CALLO(op
) = TCGOP_CALLO(old_op
);
378 tcg_debug_assert(op
->life
== 0);
380 func_idx
= TCGOP_CALLO(op
) + TCGOP_CALLI(op
);
382 op
->args
[func_idx
] = (uintptr_t)func
;
388 * When we append/replace ops here we are sensitive to changing patterns of
389 * TCGOps generated by the tcg_gen_FOO calls when we generated the
390 * empty callbacks. This will assert very quickly in a debug build as
391 * we assert the ops we are replacing are the correct ones.
393 static TCGOp
*append_udata_cb(const struct qemu_plugin_dyn_cb
*cb
,
394 TCGOp
*begin_op
, TCGOp
*op
, int *cb_idx
)
397 op
= copy_const_ptr(&begin_op
, op
, cb
->userp
);
399 /* copy the ld_i32, but note that we only have to copy it once */
401 op
= copy_op(&begin_op
, op
, INDEX_op_ld_i32
);
403 begin_op
= QTAILQ_NEXT(begin_op
, link
);
404 tcg_debug_assert(begin_op
&& begin_op
->opc
== INDEX_op_ld_i32
);
408 op
= copy_call(&begin_op
, op
, HELPER(plugin_vcpu_udata_cb
),
409 cb
->f
.vcpu_udata
, cb_idx
);
414 static TCGOp
*append_inline_cb(const struct qemu_plugin_dyn_cb
*cb
,
415 TCGOp
*begin_op
, TCGOp
*op
,
419 op
= copy_const_ptr(&begin_op
, op
, cb
->userp
);
422 op
= copy_ld_i64(&begin_op
, op
);
425 op
= copy_add_i64(&begin_op
, op
, cb
->inline_insn
.imm
);
428 op
= copy_st_i64(&begin_op
, op
);
433 static TCGOp
*append_mem_cb(const struct qemu_plugin_dyn_cb
*cb
,
434 TCGOp
*begin_op
, TCGOp
*op
, int *cb_idx
)
436 enum plugin_gen_cb type
= begin_op
->args
[1];
438 tcg_debug_assert(type
== PLUGIN_GEN_CB_MEM
);
440 /* const_i32 == mov_i32 ("info", so it remains as is) */
441 op
= copy_op(&begin_op
, op
, INDEX_op_mov_i32
);
444 op
= copy_const_ptr(&begin_op
, op
, cb
->userp
);
446 /* copy the ld_i32, but note that we only have to copy it once */
448 op
= copy_op(&begin_op
, op
, INDEX_op_ld_i32
);
450 begin_op
= QTAILQ_NEXT(begin_op
, link
);
451 tcg_debug_assert(begin_op
&& begin_op
->opc
== INDEX_op_ld_i32
);
455 op
= copy_extu_tl_i64(&begin_op
, op
);
457 if (type
== PLUGIN_GEN_CB_MEM
) {
459 op
= copy_call(&begin_op
, op
, HELPER(plugin_vcpu_mem_cb
),
460 cb
->f
.vcpu_udata
, cb_idx
);
466 typedef TCGOp
*(*inject_fn
)(const struct qemu_plugin_dyn_cb
*cb
,
467 TCGOp
*begin_op
, TCGOp
*op
, int *intp
);
468 typedef bool (*op_ok_fn
)(const TCGOp
*op
, const struct qemu_plugin_dyn_cb
*cb
);
470 static bool op_ok(const TCGOp
*op
, const struct qemu_plugin_dyn_cb
*cb
)
475 static bool op_rw(const TCGOp
*op
, const struct qemu_plugin_dyn_cb
*cb
)
480 return !!(cb
->rw
& (w
+ 1));
483 static void inject_cb_type(const GArray
*cbs
, TCGOp
*begin_op
,
484 inject_fn inject
, op_ok_fn ok
)
491 if (!cbs
|| cbs
->len
== 0) {
496 end_op
= find_op(begin_op
, INDEX_op_plugin_cb_end
);
497 tcg_debug_assert(end_op
);
500 for (i
= 0; i
< cbs
->len
; i
++) {
501 struct qemu_plugin_dyn_cb
*cb
=
502 &g_array_index(cbs
, struct qemu_plugin_dyn_cb
, i
);
504 if (!ok(begin_op
, cb
)) {
507 op
= inject(cb
, begin_op
, op
, &cb_idx
);
509 rm_ops_range(begin_op
, end_op
);
513 inject_udata_cb(const GArray
*cbs
, TCGOp
*begin_op
)
515 inject_cb_type(cbs
, begin_op
, append_udata_cb
, op_ok
);
519 inject_inline_cb(const GArray
*cbs
, TCGOp
*begin_op
, op_ok_fn ok
)
521 inject_cb_type(cbs
, begin_op
, append_inline_cb
, ok
);
525 inject_mem_cb(const GArray
*cbs
, TCGOp
*begin_op
)
527 inject_cb_type(cbs
, begin_op
, append_mem_cb
, op_rw
);
530 /* we could change the ops in place, but we can reuse more code by copying */
531 static void inject_mem_helper(TCGOp
*begin_op
, GArray
*arr
)
533 TCGOp
*orig_op
= begin_op
;
537 end_op
= find_op(begin_op
, INDEX_op_plugin_cb_end
);
538 tcg_debug_assert(end_op
);
541 op
= copy_const_ptr(&begin_op
, end_op
, arr
);
544 op
= copy_st_ptr(&begin_op
, op
);
546 rm_ops_range(orig_op
, end_op
);
550 * Tracking memory accesses performed from helpers requires extra work.
551 * If an instruction is emulated with helpers, we do two things:
552 * (1) copy the CB descriptors, and keep track of it so that they can be
553 * freed later on, and (2) point CPUState.plugin_mem_cbs to the descriptors, so
554 * that we can read them at run-time (i.e. when the helper executes).
555 * This run-time access is performed from qemu_plugin_vcpu_mem_cb.
557 * Note that plugin_gen_disable_mem_helpers undoes (2). Since it
558 * is possible that the code we generate after the instruction is
559 * dead, we also add checks before generating tb_exit etc.
561 static void inject_mem_enable_helper(struct qemu_plugin_tb
*ptb
,
562 struct qemu_plugin_insn
*plugin_insn
,
569 cbs
[0] = plugin_insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_REGULAR
];
570 cbs
[1] = plugin_insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_INLINE
];
573 for (i
= 0; i
< ARRAY_SIZE(cbs
); i
++) {
574 n_cbs
+= cbs
[i
]->len
;
577 plugin_insn
->mem_helper
= plugin_insn
->calls_helpers
&& n_cbs
;
578 if (likely(!plugin_insn
->mem_helper
)) {
582 ptb
->mem_helper
= true;
584 arr
= g_array_sized_new(false, false,
585 sizeof(struct qemu_plugin_dyn_cb
), n_cbs
);
587 for (i
= 0; i
< ARRAY_SIZE(cbs
); i
++) {
588 g_array_append_vals(arr
, cbs
[i
]->data
, cbs
[i
]->len
);
591 qemu_plugin_add_dyn_cb_arr(arr
);
592 inject_mem_helper(begin_op
, arr
);
595 static void inject_mem_disable_helper(struct qemu_plugin_insn
*plugin_insn
,
598 if (likely(!plugin_insn
->mem_helper
)) {
602 inject_mem_helper(begin_op
, NULL
);
605 /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */
606 void plugin_gen_disable_mem_helpers(void)
609 * We could emit the clearing unconditionally and be done. However, this can
610 * be wasteful if for instance plugins don't track memory accesses, or if
611 * most TBs don't use helpers. Instead, emit the clearing iff the TB calls
612 * helpers that might access guest memory.
614 * Note: we do not reset plugin_tb->mem_helper here; a TB might have several
615 * exit points, and we want to emit the clearing from all of them.
617 if (!tcg_ctx
->plugin_tb
->mem_helper
) {
620 tcg_gen_st_ptr(tcg_constant_ptr(NULL
), cpu_env
,
621 offsetof(CPUState
, plugin_mem_cbs
) - offsetof(ArchCPU
, env
));
624 static void plugin_gen_tb_udata(const struct qemu_plugin_tb
*ptb
,
627 inject_udata_cb(ptb
->cbs
[PLUGIN_CB_REGULAR
], begin_op
);
630 static void plugin_gen_tb_inline(const struct qemu_plugin_tb
*ptb
,
633 inject_inline_cb(ptb
->cbs
[PLUGIN_CB_INLINE
], begin_op
, op_ok
);
636 static void plugin_gen_insn_udata(const struct qemu_plugin_tb
*ptb
,
637 TCGOp
*begin_op
, int insn_idx
)
639 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
641 inject_udata_cb(insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_REGULAR
], begin_op
);
644 static void plugin_gen_insn_inline(const struct qemu_plugin_tb
*ptb
,
645 TCGOp
*begin_op
, int insn_idx
)
647 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
648 inject_inline_cb(insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_INLINE
],
652 static void plugin_gen_mem_regular(const struct qemu_plugin_tb
*ptb
,
653 TCGOp
*begin_op
, int insn_idx
)
655 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
656 inject_mem_cb(insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_REGULAR
], begin_op
);
659 static void plugin_gen_mem_inline(const struct qemu_plugin_tb
*ptb
,
660 TCGOp
*begin_op
, int insn_idx
)
663 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
665 cbs
= insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_INLINE
];
666 inject_inline_cb(cbs
, begin_op
, op_rw
);
669 static void plugin_gen_enable_mem_helper(struct qemu_plugin_tb
*ptb
,
670 TCGOp
*begin_op
, int insn_idx
)
672 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
673 inject_mem_enable_helper(ptb
, insn
, begin_op
);
676 static void plugin_gen_disable_mem_helper(struct qemu_plugin_tb
*ptb
,
677 TCGOp
*begin_op
, int insn_idx
)
679 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
680 inject_mem_disable_helper(insn
, begin_op
);
683 /* #define DEBUG_PLUGIN_GEN_OPS */
684 static void pr_ops(void)
686 #ifdef DEBUG_PLUGIN_GEN_OPS
690 QTAILQ_FOREACH(op
, &tcg_ctx
->ops
, link
) {
691 const char *name
= "";
692 const char *type
= "";
694 if (op
->opc
== INDEX_op_plugin_cb_start
) {
695 switch (op
->args
[0]) {
696 case PLUGIN_GEN_FROM_TB
:
699 case PLUGIN_GEN_FROM_INSN
:
702 case PLUGIN_GEN_FROM_MEM
:
705 case PLUGIN_GEN_AFTER_INSN
:
711 switch (op
->args
[1]) {
712 case PLUGIN_GEN_CB_UDATA
:
715 case PLUGIN_GEN_CB_INLINE
:
718 case PLUGIN_GEN_CB_MEM
:
721 case PLUGIN_GEN_ENABLE_MEM_HELPER
:
722 type
= "enable mem helper";
724 case PLUGIN_GEN_DISABLE_MEM_HELPER
:
725 type
= "disable mem helper";
731 printf("op[%2i]: %s %s %s\n", i
, tcg_op_defs
[op
->opc
].name
, name
, type
);
737 static void plugin_gen_inject(struct qemu_plugin_tb
*plugin_tb
)
744 QTAILQ_FOREACH(op
, &tcg_ctx
->ops
, link
) {
746 case INDEX_op_insn_start
:
749 case INDEX_op_plugin_cb_start
:
751 enum plugin_gen_from from
= op
->args
[0];
752 enum plugin_gen_cb type
= op
->args
[1];
755 case PLUGIN_GEN_FROM_TB
:
757 g_assert(insn_idx
== -1);
760 case PLUGIN_GEN_CB_UDATA
:
761 plugin_gen_tb_udata(plugin_tb
, op
);
763 case PLUGIN_GEN_CB_INLINE
:
764 plugin_gen_tb_inline(plugin_tb
, op
);
767 g_assert_not_reached();
771 case PLUGIN_GEN_FROM_INSN
:
773 g_assert(insn_idx
>= 0);
776 case PLUGIN_GEN_CB_UDATA
:
777 plugin_gen_insn_udata(plugin_tb
, op
, insn_idx
);
779 case PLUGIN_GEN_CB_INLINE
:
780 plugin_gen_insn_inline(plugin_tb
, op
, insn_idx
);
782 case PLUGIN_GEN_ENABLE_MEM_HELPER
:
783 plugin_gen_enable_mem_helper(plugin_tb
, op
, insn_idx
);
786 g_assert_not_reached();
790 case PLUGIN_GEN_FROM_MEM
:
792 g_assert(insn_idx
>= 0);
795 case PLUGIN_GEN_CB_MEM
:
796 plugin_gen_mem_regular(plugin_tb
, op
, insn_idx
);
798 case PLUGIN_GEN_CB_INLINE
:
799 plugin_gen_mem_inline(plugin_tb
, op
, insn_idx
);
802 g_assert_not_reached();
807 case PLUGIN_GEN_AFTER_INSN
:
809 g_assert(insn_idx
>= 0);
812 case PLUGIN_GEN_DISABLE_MEM_HELPER
:
813 plugin_gen_disable_mem_helper(plugin_tb
, op
, insn_idx
);
816 g_assert_not_reached();
821 g_assert_not_reached();
826 /* plugins don't care about any other ops */
833 bool plugin_gen_tb_start(CPUState
*cpu
, const DisasContextBase
*db
,
838 if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS
, cpu
->plugin_mask
)) {
839 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
842 /* reset callbacks */
843 for (i
= 0; i
< PLUGIN_N_CB_SUBTYPES
; i
++) {
845 g_array_set_size(ptb
->cbs
[i
], 0);
852 ptb
->vaddr
= db
->pc_first
;
854 ptb
->haddr1
= db
->host_addr
[0];
856 ptb
->mem_only
= mem_only
;
857 ptb
->mem_helper
= false;
859 plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB
);
862 tcg_ctx
->plugin_insn
= NULL
;
867 void plugin_gen_insn_start(CPUState
*cpu
, const DisasContextBase
*db
)
869 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
870 struct qemu_plugin_insn
*pinsn
;
872 pinsn
= qemu_plugin_tb_insn_get(ptb
, db
->pc_next
);
873 tcg_ctx
->plugin_insn
= pinsn
;
874 plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN
);
877 * Detect page crossing to get the new host address.
878 * Note that we skip this when haddr1 == NULL, e.g. when we're
879 * fetching instructions from a region not backed by RAM.
881 if (ptb
->haddr1
== NULL
) {
883 } else if (is_same_page(db
, db
->pc_next
)) {
884 pinsn
->haddr
= ptb
->haddr1
+ pinsn
->vaddr
- ptb
->vaddr
;
886 if (ptb
->vaddr2
== -1) {
887 ptb
->vaddr2
= TARGET_PAGE_ALIGN(db
->pc_first
);
888 get_page_addr_code_hostp(cpu
->env_ptr
, ptb
->vaddr2
, &ptb
->haddr2
);
890 pinsn
->haddr
= ptb
->haddr2
+ pinsn
->vaddr
- ptb
->vaddr2
;
894 void plugin_gen_insn_end(void)
896 plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN
);
900 * There are cases where we never get to finalise a translation - for
901 * example a page fault during translation. As a result we shouldn't
902 * do any clean-up here and make sure things are reset in
903 * plugin_gen_tb_start.
905 void plugin_gen_tb_end(CPUState
*cpu
)
907 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
909 /* collect instrumentation requests */
910 qemu_plugin_tb_trans_cb(cpu
, ptb
);
912 /* inject the instrumentation at the appropriate places */
913 plugin_gen_inject(ptb
);