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-op.h"
48 #include "exec/exec-all.h"
49 #include "exec/plugin-gen.h"
50 #include "exec/translator.h"
53 # define CONFIG_SOFTMMU_GATE 1
55 # define CONFIG_SOFTMMU_GATE 0
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.
65 enum plugin_gen_from
{
69 PLUGIN_GEN_AFTER_INSN
,
77 PLUGIN_GEN_ENABLE_MEM_HELPER
,
78 PLUGIN_GEN_DISABLE_MEM_HELPER
,
83 * These helpers are stubs that get dynamically switched out for calls
84 * direct to the plugin if they are subscribed to.
86 void HELPER(plugin_vcpu_udata_cb
)(uint32_t cpu_index
, void *udata
)
89 void HELPER(plugin_vcpu_mem_cb
)(unsigned int vcpu_index
,
90 qemu_plugin_meminfo_t info
, uint64_t vaddr
,
94 static void do_gen_mem_cb(TCGv vaddr
, uint32_t info
)
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
);
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
);
105 gen_helper_plugin_vcpu_mem_cb(cpu_index
, meminfo
, vaddr64
, udata
);
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
);
113 static void gen_empty_udata_cb(void)
115 TCGv_i32 cpu_index
= tcg_temp_new_i32();
116 TCGv_ptr udata
= tcg_const_ptr(NULL
); /* will be overwritten later */
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
);
122 tcg_temp_free_ptr(udata
);
123 tcg_temp_free_i32(cpu_index
);
127 * For now we only support addi_i64.
128 * When we support more ops, we can generate one empty inline cb for each.
130 static void gen_empty_inline_cb(void)
132 TCGv_i64 val
= tcg_temp_new_i64();
133 TCGv_ptr ptr
= tcg_const_ptr(NULL
); /* overwritten later */
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
);
143 static void gen_empty_mem_cb(TCGv addr
, uint32_t info
)
145 do_gen_mem_cb(addr
, info
);
149 * Share the same function for enable/disable. When enabling, the NULL
150 * pointer will be overwritten later.
152 static void gen_empty_mem_helper(void)
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
);
162 static void gen_plugin_cb_start(enum plugin_gen_from from
,
163 enum plugin_gen_cb type
, unsigned wr
)
165 tcg_gen_plugin_cb_start(from
, type
, wr
);
168 static void gen_wrapped(enum plugin_gen_from from
,
169 enum plugin_gen_cb type
, void (*func
)(void))
171 gen_plugin_cb_start(from
, type
, 0);
173 tcg_gen_plugin_cb_end();
176 static void plugin_gen_empty_callback(enum plugin_gen_from from
)
179 case PLUGIN_GEN_AFTER_INSN
:
180 gen_wrapped(from
, PLUGIN_GEN_DISABLE_MEM_HELPER
,
181 gen_empty_mem_helper
);
183 case PLUGIN_GEN_FROM_INSN
:
185 * Note: plugin_gen_inject() relies on ENABLE_MEM_HELPER being
186 * the first callback of an instruction
188 gen_wrapped(from
, PLUGIN_GEN_ENABLE_MEM_HELPER
,
189 gen_empty_mem_helper
);
191 case PLUGIN_GEN_FROM_TB
:
192 gen_wrapped(from
, PLUGIN_GEN_CB_UDATA
, gen_empty_udata_cb
);
193 gen_wrapped(from
, PLUGIN_GEN_CB_INLINE
, gen_empty_inline_cb
);
196 g_assert_not_reached();
201 void (*mem_fn
)(TCGv
, uint32_t);
202 void (*inline_fn
)(void);
205 static void gen_mem_wrapped(enum plugin_gen_cb type
,
206 const union mem_gen_fn
*f
, TCGv addr
,
207 uint32_t info
, bool is_mem
)
209 enum qemu_plugin_mem_rw rw
= get_plugin_meminfo_rw(info
);
211 gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM
, type
, rw
);
213 f
->mem_fn(addr
, info
);
217 tcg_gen_plugin_cb_end();
220 void plugin_gen_empty_mem_callback(TCGv addr
, uint32_t info
)
224 fn
.mem_fn
= gen_empty_mem_cb
;
225 gen_mem_wrapped(PLUGIN_GEN_CB_MEM
, &fn
, addr
, info
, true);
227 fn
.inline_fn
= gen_empty_inline_cb
;
228 gen_mem_wrapped(PLUGIN_GEN_CB_INLINE
, &fn
, 0, info
, false);
231 static TCGOp
*find_op(TCGOp
*op
, TCGOpcode opc
)
234 if (op
->opc
== opc
) {
237 op
= QTAILQ_NEXT(op
, link
);
242 static TCGOp
*rm_ops_range(TCGOp
*begin
, TCGOp
*end
)
244 TCGOp
*ret
= QTAILQ_NEXT(end
, link
);
246 QTAILQ_REMOVE_SEVERAL(&tcg_ctx
->ops
, begin
, end
, link
);
250 /* remove all ops until (and including) plugin_cb_end */
251 static TCGOp
*rm_ops(TCGOp
*op
)
253 TCGOp
*end_op
= find_op(op
, INDEX_op_plugin_cb_end
);
255 tcg_debug_assert(end_op
);
256 return rm_ops_range(op
, end_op
);
259 static TCGOp
*copy_op_nocheck(TCGOp
**begin_op
, TCGOp
*op
)
261 TCGOp
*old_op
= QTAILQ_NEXT(*begin_op
, link
);
262 unsigned nargs
= old_op
->nargs
;
265 op
= tcg_op_insert_after(tcg_ctx
, op
, old_op
->opc
, nargs
);
266 memcpy(op
->args
, old_op
->args
, sizeof(op
->args
[0]) * nargs
);
271 static TCGOp
*copy_op(TCGOp
**begin_op
, TCGOp
*op
, TCGOpcode opc
)
273 op
= copy_op_nocheck(begin_op
, op
);
274 tcg_debug_assert((*begin_op
)->opc
== opc
);
278 static TCGOp
*copy_extu_i32_i64(TCGOp
**begin_op
, TCGOp
*op
)
280 if (TCG_TARGET_REG_BITS
== 32) {
282 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
284 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
287 op
= copy_op(begin_op
, op
, INDEX_op_extu_i32_i64
);
292 static TCGOp
*copy_mov_i64(TCGOp
**begin_op
, TCGOp
*op
)
294 if (TCG_TARGET_REG_BITS
== 32) {
296 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
297 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
300 op
= copy_op(begin_op
, op
, INDEX_op_mov_i64
);
305 static TCGOp
*copy_const_ptr(TCGOp
**begin_op
, TCGOp
*op
, void *ptr
)
307 if (UINTPTR_MAX
== UINT32_MAX
) {
309 op
= copy_op(begin_op
, op
, INDEX_op_mov_i32
);
310 op
->args
[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr
));
313 op
= copy_op(begin_op
, op
, INDEX_op_mov_i64
);
314 op
->args
[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr
));
319 static TCGOp
*copy_extu_tl_i64(TCGOp
**begin_op
, TCGOp
*op
)
321 if (TARGET_LONG_BITS
== 32) {
323 op
= copy_extu_i32_i64(begin_op
, op
);
326 op
= copy_mov_i64(begin_op
, op
);
331 static TCGOp
*copy_ld_i64(TCGOp
**begin_op
, TCGOp
*op
)
333 if (TCG_TARGET_REG_BITS
== 32) {
335 op
= copy_op(begin_op
, op
, INDEX_op_ld_i32
);
336 op
= copy_op(begin_op
, op
, INDEX_op_ld_i32
);
339 op
= copy_op(begin_op
, op
, INDEX_op_ld_i64
);
344 static TCGOp
*copy_st_i64(TCGOp
**begin_op
, TCGOp
*op
)
346 if (TCG_TARGET_REG_BITS
== 32) {
348 op
= copy_op(begin_op
, op
, INDEX_op_st_i32
);
349 op
= copy_op(begin_op
, op
, INDEX_op_st_i32
);
352 op
= copy_op(begin_op
, op
, INDEX_op_st_i64
);
357 static TCGOp
*copy_add_i64(TCGOp
**begin_op
, TCGOp
*op
, uint64_t v
)
359 if (TCG_TARGET_REG_BITS
== 32) {
360 /* all 32-bit backends must implement add2_i32 */
361 g_assert(TCG_TARGET_HAS_add2_i32
);
362 op
= copy_op(begin_op
, op
, INDEX_op_add2_i32
);
363 op
->args
[4] = tcgv_i32_arg(tcg_constant_i32(v
));
364 op
->args
[5] = tcgv_i32_arg(tcg_constant_i32(v
>> 32));
366 op
= copy_op(begin_op
, op
, INDEX_op_add_i64
);
367 op
->args
[2] = tcgv_i64_arg(tcg_constant_i64(v
));
372 static TCGOp
*copy_st_ptr(TCGOp
**begin_op
, TCGOp
*op
)
374 if (UINTPTR_MAX
== UINT32_MAX
) {
376 op
= copy_op(begin_op
, op
, INDEX_op_st_i32
);
379 op
= copy_st_i64(begin_op
, op
);
384 static TCGOp
*copy_call(TCGOp
**begin_op
, TCGOp
*op
, void *empty_func
,
385 void *func
, int *cb_idx
)
390 /* copy all ops until the call */
392 op
= copy_op_nocheck(begin_op
, op
);
393 } while (op
->opc
!= INDEX_op_call
);
395 /* fill in the op call */
397 TCGOP_CALLI(op
) = TCGOP_CALLI(old_op
);
398 TCGOP_CALLO(op
) = TCGOP_CALLO(old_op
);
399 tcg_debug_assert(op
->life
== 0);
401 func_idx
= TCGOP_CALLO(op
) + TCGOP_CALLI(op
);
403 op
->args
[func_idx
] = (uintptr_t)func
;
409 * When we append/replace ops here we are sensitive to changing patterns of
410 * TCGOps generated by the tcg_gen_FOO calls when we generated the
411 * empty callbacks. This will assert very quickly in a debug build as
412 * we assert the ops we are replacing are the correct ones.
414 static TCGOp
*append_udata_cb(const struct qemu_plugin_dyn_cb
*cb
,
415 TCGOp
*begin_op
, TCGOp
*op
, int *cb_idx
)
418 op
= copy_const_ptr(&begin_op
, op
, cb
->userp
);
420 /* copy the ld_i32, but note that we only have to copy it once */
422 op
= copy_op(&begin_op
, op
, INDEX_op_ld_i32
);
424 begin_op
= QTAILQ_NEXT(begin_op
, link
);
425 tcg_debug_assert(begin_op
&& begin_op
->opc
== INDEX_op_ld_i32
);
429 op
= copy_call(&begin_op
, op
, HELPER(plugin_vcpu_udata_cb
),
430 cb
->f
.vcpu_udata
, cb_idx
);
435 static TCGOp
*append_inline_cb(const struct qemu_plugin_dyn_cb
*cb
,
436 TCGOp
*begin_op
, TCGOp
*op
,
440 op
= copy_const_ptr(&begin_op
, op
, cb
->userp
);
443 op
= copy_ld_i64(&begin_op
, op
);
446 op
= copy_add_i64(&begin_op
, op
, cb
->inline_insn
.imm
);
449 op
= copy_st_i64(&begin_op
, op
);
454 static TCGOp
*append_mem_cb(const struct qemu_plugin_dyn_cb
*cb
,
455 TCGOp
*begin_op
, TCGOp
*op
, int *cb_idx
)
457 enum plugin_gen_cb type
= begin_op
->args
[1];
459 tcg_debug_assert(type
== PLUGIN_GEN_CB_MEM
);
461 /* const_i32 == mov_i32 ("info", so it remains as is) */
462 op
= copy_op(&begin_op
, op
, INDEX_op_mov_i32
);
465 op
= copy_const_ptr(&begin_op
, op
, cb
->userp
);
467 /* copy the ld_i32, but note that we only have to copy it once */
469 op
= copy_op(&begin_op
, op
, INDEX_op_ld_i32
);
471 begin_op
= QTAILQ_NEXT(begin_op
, link
);
472 tcg_debug_assert(begin_op
&& begin_op
->opc
== INDEX_op_ld_i32
);
476 op
= copy_extu_tl_i64(&begin_op
, op
);
478 if (type
== PLUGIN_GEN_CB_MEM
) {
480 op
= copy_call(&begin_op
, op
, HELPER(plugin_vcpu_mem_cb
),
481 cb
->f
.vcpu_udata
, cb_idx
);
487 typedef TCGOp
*(*inject_fn
)(const struct qemu_plugin_dyn_cb
*cb
,
488 TCGOp
*begin_op
, TCGOp
*op
, int *intp
);
489 typedef bool (*op_ok_fn
)(const TCGOp
*op
, const struct qemu_plugin_dyn_cb
*cb
);
491 static bool op_ok(const TCGOp
*op
, const struct qemu_plugin_dyn_cb
*cb
)
496 static bool op_rw(const TCGOp
*op
, const struct qemu_plugin_dyn_cb
*cb
)
501 return !!(cb
->rw
& (w
+ 1));
504 static void inject_cb_type(const GArray
*cbs
, TCGOp
*begin_op
,
505 inject_fn inject
, op_ok_fn ok
)
512 if (!cbs
|| cbs
->len
== 0) {
517 end_op
= find_op(begin_op
, INDEX_op_plugin_cb_end
);
518 tcg_debug_assert(end_op
);
521 for (i
= 0; i
< cbs
->len
; i
++) {
522 struct qemu_plugin_dyn_cb
*cb
=
523 &g_array_index(cbs
, struct qemu_plugin_dyn_cb
, i
);
525 if (!ok(begin_op
, cb
)) {
528 op
= inject(cb
, begin_op
, op
, &cb_idx
);
530 rm_ops_range(begin_op
, end_op
);
534 inject_udata_cb(const GArray
*cbs
, TCGOp
*begin_op
)
536 inject_cb_type(cbs
, begin_op
, append_udata_cb
, op_ok
);
540 inject_inline_cb(const GArray
*cbs
, TCGOp
*begin_op
, op_ok_fn ok
)
542 inject_cb_type(cbs
, begin_op
, append_inline_cb
, ok
);
546 inject_mem_cb(const GArray
*cbs
, TCGOp
*begin_op
)
548 inject_cb_type(cbs
, begin_op
, append_mem_cb
, op_rw
);
551 /* we could change the ops in place, but we can reuse more code by copying */
552 static void inject_mem_helper(TCGOp
*begin_op
, GArray
*arr
)
554 TCGOp
*orig_op
= begin_op
;
558 end_op
= find_op(begin_op
, INDEX_op_plugin_cb_end
);
559 tcg_debug_assert(end_op
);
562 op
= copy_const_ptr(&begin_op
, end_op
, arr
);
565 op
= copy_st_ptr(&begin_op
, op
);
567 rm_ops_range(orig_op
, end_op
);
571 * Tracking memory accesses performed from helpers requires extra work.
572 * If an instruction is emulated with helpers, we do two things:
573 * (1) copy the CB descriptors, and keep track of it so that they can be
574 * freed later on, and (2) point CPUState.plugin_mem_cbs to the descriptors, so
575 * that we can read them at run-time (i.e. when the helper executes).
576 * This run-time access is performed from qemu_plugin_vcpu_mem_cb.
578 * Note that plugin_gen_disable_mem_helpers undoes (2). Since it
579 * is possible that the code we generate after the instruction is
580 * dead, we also add checks before generating tb_exit etc.
582 static void inject_mem_enable_helper(struct qemu_plugin_insn
*plugin_insn
,
589 cbs
[0] = plugin_insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_REGULAR
];
590 cbs
[1] = plugin_insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_INLINE
];
593 for (i
= 0; i
< ARRAY_SIZE(cbs
); i
++) {
594 n_cbs
+= cbs
[i
]->len
;
597 plugin_insn
->mem_helper
= plugin_insn
->calls_helpers
&& n_cbs
;
598 if (likely(!plugin_insn
->mem_helper
)) {
603 arr
= g_array_sized_new(false, false,
604 sizeof(struct qemu_plugin_dyn_cb
), n_cbs
);
606 for (i
= 0; i
< ARRAY_SIZE(cbs
); i
++) {
607 g_array_append_vals(arr
, cbs
[i
]->data
, cbs
[i
]->len
);
610 qemu_plugin_add_dyn_cb_arr(arr
);
611 inject_mem_helper(begin_op
, arr
);
614 static void inject_mem_disable_helper(struct qemu_plugin_insn
*plugin_insn
,
617 if (likely(!plugin_insn
->mem_helper
)) {
621 inject_mem_helper(begin_op
, NULL
);
624 /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */
625 void plugin_gen_disable_mem_helpers(void)
629 if (likely(tcg_ctx
->plugin_insn
== NULL
||
630 !tcg_ctx
->plugin_insn
->mem_helper
)) {
633 ptr
= tcg_const_ptr(NULL
);
634 tcg_gen_st_ptr(ptr
, cpu_env
, offsetof(CPUState
, plugin_mem_cbs
) -
635 offsetof(ArchCPU
, env
));
636 tcg_temp_free_ptr(ptr
);
637 tcg_ctx
->plugin_insn
->mem_helper
= false;
640 static void plugin_gen_tb_udata(const struct qemu_plugin_tb
*ptb
,
643 inject_udata_cb(ptb
->cbs
[PLUGIN_CB_REGULAR
], begin_op
);
646 static void plugin_gen_tb_inline(const struct qemu_plugin_tb
*ptb
,
649 inject_inline_cb(ptb
->cbs
[PLUGIN_CB_INLINE
], begin_op
, op_ok
);
652 static void plugin_gen_insn_udata(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
);
657 inject_udata_cb(insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_REGULAR
], begin_op
);
660 static void plugin_gen_insn_inline(const struct qemu_plugin_tb
*ptb
,
661 TCGOp
*begin_op
, int insn_idx
)
663 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
664 inject_inline_cb(insn
->cbs
[PLUGIN_CB_INSN
][PLUGIN_CB_INLINE
],
668 static void plugin_gen_mem_regular(const struct qemu_plugin_tb
*ptb
,
669 TCGOp
*begin_op
, int insn_idx
)
671 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
672 inject_mem_cb(insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_REGULAR
], begin_op
);
675 static void plugin_gen_mem_inline(const struct qemu_plugin_tb
*ptb
,
676 TCGOp
*begin_op
, int insn_idx
)
679 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
681 cbs
= insn
->cbs
[PLUGIN_CB_MEM
][PLUGIN_CB_INLINE
];
682 inject_inline_cb(cbs
, begin_op
, op_rw
);
685 static void plugin_gen_enable_mem_helper(const struct qemu_plugin_tb
*ptb
,
686 TCGOp
*begin_op
, int insn_idx
)
688 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
689 inject_mem_enable_helper(insn
, begin_op
);
692 static void plugin_gen_disable_mem_helper(const struct qemu_plugin_tb
*ptb
,
693 TCGOp
*begin_op
, int insn_idx
)
695 struct qemu_plugin_insn
*insn
= g_ptr_array_index(ptb
->insns
, insn_idx
);
696 inject_mem_disable_helper(insn
, begin_op
);
699 /* #define DEBUG_PLUGIN_GEN_OPS */
700 static void pr_ops(void)
702 #ifdef DEBUG_PLUGIN_GEN_OPS
706 QTAILQ_FOREACH(op
, &tcg_ctx
->ops
, link
) {
707 const char *name
= "";
708 const char *type
= "";
710 if (op
->opc
== INDEX_op_plugin_cb_start
) {
711 switch (op
->args
[0]) {
712 case PLUGIN_GEN_FROM_TB
:
715 case PLUGIN_GEN_FROM_INSN
:
718 case PLUGIN_GEN_FROM_MEM
:
721 case PLUGIN_GEN_AFTER_INSN
:
727 switch (op
->args
[1]) {
728 case PLUGIN_GEN_CB_UDATA
:
731 case PLUGIN_GEN_CB_INLINE
:
734 case PLUGIN_GEN_CB_MEM
:
737 case PLUGIN_GEN_ENABLE_MEM_HELPER
:
738 type
= "enable mem helper";
740 case PLUGIN_GEN_DISABLE_MEM_HELPER
:
741 type
= "disable mem helper";
747 printf("op[%2i]: %s %s %s\n", i
, tcg_op_defs
[op
->opc
].name
, name
, type
);
753 static void plugin_gen_inject(const struct qemu_plugin_tb
*plugin_tb
)
760 QTAILQ_FOREACH(op
, &tcg_ctx
->ops
, link
) {
762 case INDEX_op_insn_start
:
765 case INDEX_op_plugin_cb_start
:
767 enum plugin_gen_from from
= op
->args
[0];
768 enum plugin_gen_cb type
= op
->args
[1];
771 case PLUGIN_GEN_FROM_TB
:
773 g_assert(insn_idx
== -1);
776 case PLUGIN_GEN_CB_UDATA
:
777 plugin_gen_tb_udata(plugin_tb
, op
);
779 case PLUGIN_GEN_CB_INLINE
:
780 plugin_gen_tb_inline(plugin_tb
, op
);
783 g_assert_not_reached();
787 case PLUGIN_GEN_FROM_INSN
:
789 g_assert(insn_idx
>= 0);
792 case PLUGIN_GEN_CB_UDATA
:
793 plugin_gen_insn_udata(plugin_tb
, op
, insn_idx
);
795 case PLUGIN_GEN_CB_INLINE
:
796 plugin_gen_insn_inline(plugin_tb
, op
, insn_idx
);
798 case PLUGIN_GEN_ENABLE_MEM_HELPER
:
799 plugin_gen_enable_mem_helper(plugin_tb
, op
, insn_idx
);
802 g_assert_not_reached();
806 case PLUGIN_GEN_FROM_MEM
:
808 g_assert(insn_idx
>= 0);
811 case PLUGIN_GEN_CB_MEM
:
812 plugin_gen_mem_regular(plugin_tb
, op
, insn_idx
);
814 case PLUGIN_GEN_CB_INLINE
:
815 plugin_gen_mem_inline(plugin_tb
, op
, insn_idx
);
818 g_assert_not_reached();
823 case PLUGIN_GEN_AFTER_INSN
:
825 g_assert(insn_idx
>= 0);
828 case PLUGIN_GEN_DISABLE_MEM_HELPER
:
829 plugin_gen_disable_mem_helper(plugin_tb
, op
, insn_idx
);
832 g_assert_not_reached();
837 g_assert_not_reached();
842 /* plugins don't care about any other ops */
849 bool plugin_gen_tb_start(CPUState
*cpu
, const DisasContextBase
*db
,
854 if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS
, cpu
->plugin_mask
)) {
855 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
858 /* reset callbacks */
859 for (i
= 0; i
< PLUGIN_N_CB_SUBTYPES
; i
++) {
861 g_array_set_size(ptb
->cbs
[i
], 0);
868 ptb
->vaddr
= db
->pc_first
;
870 ptb
->haddr1
= db
->host_addr
[0];
872 ptb
->mem_only
= mem_only
;
874 plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB
);
877 tcg_ctx
->plugin_insn
= NULL
;
882 void plugin_gen_insn_start(CPUState
*cpu
, const DisasContextBase
*db
)
884 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
885 struct qemu_plugin_insn
*pinsn
;
887 pinsn
= qemu_plugin_tb_insn_get(ptb
, db
->pc_next
);
888 tcg_ctx
->plugin_insn
= pinsn
;
889 plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN
);
892 * Detect page crossing to get the new host address.
893 * Note that we skip this when haddr1 == NULL, e.g. when we're
894 * fetching instructions from a region not backed by RAM.
896 if (ptb
->haddr1
== NULL
) {
898 } else if (is_same_page(db
, db
->pc_next
)) {
899 pinsn
->haddr
= ptb
->haddr1
+ pinsn
->vaddr
- ptb
->vaddr
;
901 if (ptb
->vaddr2
== -1) {
902 ptb
->vaddr2
= TARGET_PAGE_ALIGN(db
->pc_first
);
903 get_page_addr_code_hostp(cpu
->env_ptr
, ptb
->vaddr2
, &ptb
->haddr2
);
905 pinsn
->haddr
= ptb
->haddr2
+ pinsn
->vaddr
- ptb
->vaddr2
;
909 void plugin_gen_insn_end(void)
911 plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN
);
915 * There are cases where we never get to finalise a translation - for
916 * example a page fault during translation. As a result we shouldn't
917 * do any clean-up here and make sure things are reset in
918 * plugin_gen_tb_start.
920 void plugin_gen_tb_end(CPUState
*cpu
)
922 struct qemu_plugin_tb
*ptb
= tcg_ctx
->plugin_tb
;
924 /* collect instrumentation requests */
925 qemu_plugin_tb_trans_cb(cpu
, ptb
);
927 /* inject the instrumentation at the appropriate places */
928 plugin_gen_inject(ptb
);