xref: /qemu/accel/tcg/plugin-gen.c (revision 21a3f62ff2b40a7a2abbd614b44fe5da461c3fd7)
138b47b19SEmilio G. Cota /*
238b47b19SEmilio G. Cota  * plugin-gen.c - TCG-related bits of plugin infrastructure
338b47b19SEmilio G. Cota  *
438b47b19SEmilio G. Cota  * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
538b47b19SEmilio G. Cota  * License: GNU GPL, version 2 or later.
638b47b19SEmilio G. Cota  *   See the COPYING file in the top-level directory.
738b47b19SEmilio G. Cota  *
838b47b19SEmilio G. Cota  * We support instrumentation at an instruction granularity. That is,
938b47b19SEmilio G. Cota  * if a plugin wants to instrument the memory accesses performed by a
1038b47b19SEmilio G. Cota  * particular instruction, it can just do that instead of instrumenting
1138b47b19SEmilio G. Cota  * all memory accesses. Thus, in order to do this we first have to
1238b47b19SEmilio G. Cota  * translate a TB, so that plugins can decide what/where to instrument.
1338b47b19SEmilio G. Cota  *
1438b47b19SEmilio G. Cota  * Injecting the desired instrumentation could be done with a second
1538b47b19SEmilio G. Cota  * translation pass that combined the instrumentation requests, but that
1638b47b19SEmilio G. Cota  * would be ugly and inefficient since we would decode the guest code twice.
1738b47b19SEmilio G. Cota  * Instead, during TB translation we add "empty" instrumentation calls for all
1838b47b19SEmilio G. Cota  * possible instrumentation events, and then once we collect the instrumentation
1938b47b19SEmilio G. Cota  * requests from plugins, we either "fill in" those empty events or remove them
2038b47b19SEmilio G. Cota  * if they have no requests.
2138b47b19SEmilio G. Cota  *
2238b47b19SEmilio G. Cota  * When "filling in" an event we first copy the empty callback's TCG ops. This
2338b47b19SEmilio G. Cota  * might seem unnecessary, but it is done to support an arbitrary number
2438b47b19SEmilio G. Cota  * of callbacks per event. Take for example a regular instruction callback.
2538b47b19SEmilio G. Cota  * We first generate a callback to an empty helper function. Then, if two
2638b47b19SEmilio G. Cota  * plugins register one callback each for this instruction, we make two copies
2738b47b19SEmilio G. Cota  * of the TCG ops generated for the empty callback, substituting the function
2838b47b19SEmilio G. Cota  * pointer that points to the empty helper function with the plugins' desired
2938b47b19SEmilio G. Cota  * callback functions. After that we remove the empty callback's ops.
3038b47b19SEmilio G. Cota  *
3138b47b19SEmilio G. Cota  * Note that the location in TCGOp.args[] of the pointer to a helper function
3238b47b19SEmilio G. Cota  * varies across different guest and host architectures. Instead of duplicating
3338b47b19SEmilio G. Cota  * the logic that figures this out, we rely on the fact that the empty
3438b47b19SEmilio G. Cota  * callbacks point to empty functions that are unique pointers in the program.
3538b47b19SEmilio G. Cota  * Thus, to find the right location we just have to look for a match in
3638b47b19SEmilio G. Cota  * TCGOp.args[]. This is the main reason why we first copy an empty callback's
3738b47b19SEmilio G. Cota  * TCG ops and then fill them in; regardless of whether we have one or many
3838b47b19SEmilio G. Cota  * callbacks for that event, the logic to add all of them is the same.
3938b47b19SEmilio G. Cota  *
4038b47b19SEmilio G. Cota  * When generating more than one callback per event, we make a small
4138b47b19SEmilio G. Cota  * optimization to avoid generating redundant operations. For instance, for the
4238b47b19SEmilio G. Cota  * second and all subsequent callbacks of an event, we do not need to reload the
4338b47b19SEmilio G. Cota  * CPU's index into a TCG temp, since the first callback did it already.
4438b47b19SEmilio G. Cota  */
4538b47b19SEmilio G. Cota #include "qemu/osdep.h"
46c0061471SAlex Bennée #include "qemu/plugin.h"
47cac9b0fdSRichard Henderson #include "cpu.h"
4838b47b19SEmilio G. Cota #include "tcg/tcg.h"
4947f7313dSRichard Henderson #include "tcg/tcg-temp-internal.h"
5038b47b19SEmilio G. Cota #include "tcg/tcg-op.h"
5138b47b19SEmilio G. Cota #include "exec/exec-all.h"
5238b47b19SEmilio G. Cota #include "exec/plugin-gen.h"
5338b47b19SEmilio G. Cota #include "exec/translator.h"
54c213ee2dSRichard Henderson #include "exec/helper-proto-common.h"
55d53106c9SRichard Henderson 
56d53106c9SRichard Henderson #define HELPER_H  "accel/tcg/plugin-helpers.h"
57d53106c9SRichard Henderson #include "exec/helper-info.c.inc"
58d53106c9SRichard Henderson #undef  HELPER_H
5938b47b19SEmilio G. Cota 
6038b47b19SEmilio G. Cota /*
6138b47b19SEmilio G. Cota  * plugin_cb_start TCG op args[]:
6238b47b19SEmilio G. Cota  * 0: enum plugin_gen_from
6338b47b19SEmilio G. Cota  * 1: enum plugin_gen_cb
6438b47b19SEmilio G. Cota  * 2: set to 1 for mem callback that is a write, 0 otherwise.
6538b47b19SEmilio G. Cota  */
6638b47b19SEmilio G. Cota 
6738b47b19SEmilio G. Cota enum plugin_gen_from {
6838b47b19SEmilio G. Cota     PLUGIN_GEN_FROM_TB,
6938b47b19SEmilio G. Cota     PLUGIN_GEN_FROM_INSN,
7038b47b19SEmilio G. Cota     PLUGIN_GEN_FROM_MEM,
7138b47b19SEmilio G. Cota     PLUGIN_GEN_AFTER_INSN,
7238b47b19SEmilio G. Cota     PLUGIN_GEN_N_FROMS,
7338b47b19SEmilio G. Cota };
7438b47b19SEmilio G. Cota 
7538b47b19SEmilio G. Cota enum plugin_gen_cb {
7638b47b19SEmilio G. Cota     PLUGIN_GEN_CB_UDATA,
7733a277feSAkihiko Odaki     PLUGIN_GEN_CB_UDATA_R,
7838b47b19SEmilio G. Cota     PLUGIN_GEN_CB_INLINE,
7938b47b19SEmilio G. Cota     PLUGIN_GEN_CB_MEM,
8038b47b19SEmilio G. Cota     PLUGIN_GEN_ENABLE_MEM_HELPER,
8138b47b19SEmilio G. Cota     PLUGIN_GEN_DISABLE_MEM_HELPER,
8238b47b19SEmilio G. Cota     PLUGIN_GEN_N_CBS,
8338b47b19SEmilio G. Cota };
8438b47b19SEmilio G. Cota 
8538b47b19SEmilio G. Cota /*
8638b47b19SEmilio G. Cota  * These helpers are stubs that get dynamically switched out for calls
8738b47b19SEmilio G. Cota  * direct to the plugin if they are subscribed to.
8838b47b19SEmilio G. Cota  */
8933a277feSAkihiko Odaki void HELPER(plugin_vcpu_udata_cb_no_wg)(uint32_t cpu_index, void *udata)
9033a277feSAkihiko Odaki { }
9133a277feSAkihiko Odaki 
9233a277feSAkihiko Odaki void HELPER(plugin_vcpu_udata_cb_no_rwg)(uint32_t cpu_index, void *udata)
9338b47b19SEmilio G. Cota { }
9438b47b19SEmilio G. Cota 
9538b47b19SEmilio G. Cota void HELPER(plugin_vcpu_mem_cb)(unsigned int vcpu_index,
9638b47b19SEmilio G. Cota                                 qemu_plugin_meminfo_t info, uint64_t vaddr,
9738b47b19SEmilio G. Cota                                 void *userdata)
9838b47b19SEmilio G. Cota { }
9938b47b19SEmilio G. Cota 
10033a277feSAkihiko Odaki static void gen_empty_udata_cb(void (*gen_helper)(TCGv_i32, TCGv_ptr))
10138b47b19SEmilio G. Cota {
102713f263aSRichard Henderson     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
103713f263aSRichard Henderson     TCGv_ptr udata = tcg_temp_ebb_new_ptr();
10438b47b19SEmilio G. Cota 
105713f263aSRichard Henderson     tcg_gen_movi_ptr(udata, 0);
106ad75a51eSRichard Henderson     tcg_gen_ld_i32(cpu_index, tcg_env,
10738b47b19SEmilio G. Cota                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
10833a277feSAkihiko Odaki     gen_helper(cpu_index, udata);
10938b47b19SEmilio G. Cota 
11038b47b19SEmilio G. Cota     tcg_temp_free_ptr(udata);
11138b47b19SEmilio G. Cota     tcg_temp_free_i32(cpu_index);
11238b47b19SEmilio G. Cota }
11338b47b19SEmilio G. Cota 
11433a277feSAkihiko Odaki static void gen_empty_udata_cb_no_wg(void)
11533a277feSAkihiko Odaki {
11633a277feSAkihiko Odaki     gen_empty_udata_cb(gen_helper_plugin_vcpu_udata_cb_no_wg);
11733a277feSAkihiko Odaki }
11833a277feSAkihiko Odaki 
11933a277feSAkihiko Odaki static void gen_empty_udata_cb_no_rwg(void)
12033a277feSAkihiko Odaki {
12133a277feSAkihiko Odaki     gen_empty_udata_cb(gen_helper_plugin_vcpu_udata_cb_no_rwg);
12233a277feSAkihiko Odaki }
12333a277feSAkihiko Odaki 
12438b47b19SEmilio G. Cota /*
12538b47b19SEmilio G. Cota  * For now we only support addi_i64.
12638b47b19SEmilio G. Cota  * When we support more ops, we can generate one empty inline cb for each.
12738b47b19SEmilio G. Cota  */
12838b47b19SEmilio G. Cota static void gen_empty_inline_cb(void)
12938b47b19SEmilio G. Cota {
13062f92b8dSPierrick Bouvier     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
13162f92b8dSPierrick Bouvier     TCGv_ptr cpu_index_as_ptr = tcg_temp_ebb_new_ptr();
132713f263aSRichard Henderson     TCGv_i64 val = tcg_temp_ebb_new_i64();
133713f263aSRichard Henderson     TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
13438b47b19SEmilio G. Cota 
13562f92b8dSPierrick Bouvier     tcg_gen_ld_i32(cpu_index, tcg_env,
13662f92b8dSPierrick Bouvier                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
13762f92b8dSPierrick Bouvier     /* second operand will be replaced by immediate value */
13862f92b8dSPierrick Bouvier     tcg_gen_mul_i32(cpu_index, cpu_index, cpu_index);
13962f92b8dSPierrick Bouvier     tcg_gen_ext_i32_ptr(cpu_index_as_ptr, cpu_index);
14062f92b8dSPierrick Bouvier 
141713f263aSRichard Henderson     tcg_gen_movi_ptr(ptr, 0);
14262f92b8dSPierrick Bouvier     tcg_gen_add_ptr(ptr, ptr, cpu_index_as_ptr);
14338b47b19SEmilio G. Cota     tcg_gen_ld_i64(val, ptr, 0);
14462f92b8dSPierrick Bouvier     /* second operand will be replaced by immediate value */
14562f92b8dSPierrick Bouvier     tcg_gen_add_i64(val, val, val);
14662f92b8dSPierrick Bouvier 
14738b47b19SEmilio G. Cota     tcg_gen_st_i64(val, ptr, 0);
14838b47b19SEmilio G. Cota     tcg_temp_free_ptr(ptr);
14938b47b19SEmilio G. Cota     tcg_temp_free_i64(val);
15062f92b8dSPierrick Bouvier     tcg_temp_free_ptr(cpu_index_as_ptr);
15162f92b8dSPierrick Bouvier     tcg_temp_free_i32(cpu_index);
15238b47b19SEmilio G. Cota }
15338b47b19SEmilio G. Cota 
154fcdab382SRichard Henderson static void gen_empty_mem_cb(TCGv_i64 addr, uint32_t info)
15538b47b19SEmilio G. Cota {
156b6d91645SRichard Henderson     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
157b6d91645SRichard Henderson     TCGv_i32 meminfo = tcg_temp_ebb_new_i32();
158b6d91645SRichard Henderson     TCGv_ptr udata = tcg_temp_ebb_new_ptr();
159b6d91645SRichard Henderson 
160b6d91645SRichard Henderson     tcg_gen_movi_i32(meminfo, info);
161b6d91645SRichard Henderson     tcg_gen_movi_ptr(udata, 0);
162ad75a51eSRichard Henderson     tcg_gen_ld_i32(cpu_index, tcg_env,
163b6d91645SRichard Henderson                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
164b6d91645SRichard Henderson 
165fcdab382SRichard Henderson     gen_helper_plugin_vcpu_mem_cb(cpu_index, meminfo, addr, udata);
166b6d91645SRichard Henderson 
167b6d91645SRichard Henderson     tcg_temp_free_ptr(udata);
168b6d91645SRichard Henderson     tcg_temp_free_i32(meminfo);
169b6d91645SRichard Henderson     tcg_temp_free_i32(cpu_index);
17038b47b19SEmilio G. Cota }
17138b47b19SEmilio G. Cota 
17238b47b19SEmilio G. Cota /*
17338b47b19SEmilio G. Cota  * Share the same function for enable/disable. When enabling, the NULL
17438b47b19SEmilio G. Cota  * pointer will be overwritten later.
17538b47b19SEmilio G. Cota  */
17638b47b19SEmilio G. Cota static void gen_empty_mem_helper(void)
17738b47b19SEmilio G. Cota {
178713f263aSRichard Henderson     TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
17938b47b19SEmilio G. Cota 
180713f263aSRichard Henderson     tcg_gen_movi_ptr(ptr, 0);
181ad75a51eSRichard Henderson     tcg_gen_st_ptr(ptr, tcg_env, offsetof(CPUState, plugin_mem_cbs) -
18238b47b19SEmilio G. Cota                                  offsetof(ArchCPU, env));
18338b47b19SEmilio G. Cota     tcg_temp_free_ptr(ptr);
18438b47b19SEmilio G. Cota }
18538b47b19SEmilio G. Cota 
1869a3ee366SRichard Henderson static void gen_plugin_cb_start(enum plugin_gen_from from,
18738b47b19SEmilio G. Cota                                 enum plugin_gen_cb type, unsigned wr)
18838b47b19SEmilio G. Cota {
18938b47b19SEmilio G. Cota     tcg_gen_plugin_cb_start(from, type, wr);
19038b47b19SEmilio G. Cota }
19138b47b19SEmilio G. Cota 
19238b47b19SEmilio G. Cota static void gen_wrapped(enum plugin_gen_from from,
19338b47b19SEmilio G. Cota                         enum plugin_gen_cb type, void (*func)(void))
19438b47b19SEmilio G. Cota {
19538b47b19SEmilio G. Cota     gen_plugin_cb_start(from, type, 0);
19638b47b19SEmilio G. Cota     func();
19738b47b19SEmilio G. Cota     tcg_gen_plugin_cb_end();
19838b47b19SEmilio G. Cota }
19938b47b19SEmilio G. Cota 
2009a3ee366SRichard Henderson static void plugin_gen_empty_callback(enum plugin_gen_from from)
20138b47b19SEmilio G. Cota {
20238b47b19SEmilio G. Cota     switch (from) {
20338b47b19SEmilio G. Cota     case PLUGIN_GEN_AFTER_INSN:
204*21a3f62fSRichard Henderson     case PLUGIN_GEN_FROM_TB:
205a0948bb7SRichard Henderson         tcg_gen_plugin_cb(from);
20638b47b19SEmilio G. Cota         break;
20738b47b19SEmilio G. Cota     case PLUGIN_GEN_FROM_INSN:
20838b47b19SEmilio G. Cota         /*
20938b47b19SEmilio G. Cota          * Note: plugin_gen_inject() relies on ENABLE_MEM_HELPER being
21038b47b19SEmilio G. Cota          * the first callback of an instruction
21138b47b19SEmilio G. Cota          */
21238b47b19SEmilio G. Cota         gen_wrapped(from, PLUGIN_GEN_ENABLE_MEM_HELPER,
21338b47b19SEmilio G. Cota                     gen_empty_mem_helper);
21433a277feSAkihiko Odaki         gen_wrapped(from, PLUGIN_GEN_CB_UDATA, gen_empty_udata_cb_no_rwg);
21533a277feSAkihiko Odaki         gen_wrapped(from, PLUGIN_GEN_CB_UDATA_R, gen_empty_udata_cb_no_wg);
21638b47b19SEmilio G. Cota         gen_wrapped(from, PLUGIN_GEN_CB_INLINE, gen_empty_inline_cb);
21738b47b19SEmilio G. Cota         break;
21838b47b19SEmilio G. Cota     default:
21938b47b19SEmilio G. Cota         g_assert_not_reached();
22038b47b19SEmilio G. Cota     }
22138b47b19SEmilio G. Cota }
22238b47b19SEmilio G. Cota 
223fcdab382SRichard Henderson void plugin_gen_empty_mem_callback(TCGv_i64 addr, uint32_t info)
22438b47b19SEmilio G. Cota {
22537aff087SRichard Henderson     enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
22638b47b19SEmilio G. Cota 
227f5c346acSRichard Henderson     gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_MEM, rw);
228f5c346acSRichard Henderson     gen_empty_mem_cb(addr, info);
22938b47b19SEmilio G. Cota     tcg_gen_plugin_cb_end();
23038b47b19SEmilio G. Cota 
231f5c346acSRichard Henderson     gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_INLINE, rw);
232f5c346acSRichard Henderson     gen_empty_inline_cb();
233f5c346acSRichard Henderson     tcg_gen_plugin_cb_end();
23438b47b19SEmilio G. Cota }
23538b47b19SEmilio G. Cota 
23638b47b19SEmilio G. Cota static TCGOp *find_op(TCGOp *op, TCGOpcode opc)
23738b47b19SEmilio G. Cota {
23838b47b19SEmilio G. Cota     while (op) {
23938b47b19SEmilio G. Cota         if (op->opc == opc) {
24038b47b19SEmilio G. Cota             return op;
24138b47b19SEmilio G. Cota         }
24238b47b19SEmilio G. Cota         op = QTAILQ_NEXT(op, link);
24338b47b19SEmilio G. Cota     }
24438b47b19SEmilio G. Cota     return NULL;
24538b47b19SEmilio G. Cota }
24638b47b19SEmilio G. Cota 
24738b47b19SEmilio G. Cota static TCGOp *rm_ops_range(TCGOp *begin, TCGOp *end)
24838b47b19SEmilio G. Cota {
24938b47b19SEmilio G. Cota     TCGOp *ret = QTAILQ_NEXT(end, link);
25038b47b19SEmilio G. Cota 
25138b47b19SEmilio G. Cota     QTAILQ_REMOVE_SEVERAL(&tcg_ctx->ops, begin, end, link);
25238b47b19SEmilio G. Cota     return ret;
25338b47b19SEmilio G. Cota }
25438b47b19SEmilio G. Cota 
25538b47b19SEmilio G. Cota /* remove all ops until (and including) plugin_cb_end */
25638b47b19SEmilio G. Cota static TCGOp *rm_ops(TCGOp *op)
25738b47b19SEmilio G. Cota {
25838b47b19SEmilio G. Cota     TCGOp *end_op = find_op(op, INDEX_op_plugin_cb_end);
25938b47b19SEmilio G. Cota 
26038b47b19SEmilio G. Cota     tcg_debug_assert(end_op);
26138b47b19SEmilio G. Cota     return rm_ops_range(op, end_op);
26238b47b19SEmilio G. Cota }
26338b47b19SEmilio G. Cota 
26438b47b19SEmilio G. Cota static TCGOp *copy_op_nocheck(TCGOp **begin_op, TCGOp *op)
26538b47b19SEmilio G. Cota {
266cb10bc63SRichard Henderson     TCGOp *old_op = QTAILQ_NEXT(*begin_op, link);
267cb10bc63SRichard Henderson     unsigned nargs = old_op->nargs;
268d4478943SPhilippe Mathieu-Daudé 
269cb10bc63SRichard Henderson     *begin_op = old_op;
270cb10bc63SRichard Henderson     op = tcg_op_insert_after(tcg_ctx, op, old_op->opc, nargs);
271cb10bc63SRichard Henderson     memcpy(op->args, old_op->args, sizeof(op->args[0]) * nargs);
272d4478943SPhilippe Mathieu-Daudé 
27338b47b19SEmilio G. Cota     return op;
27438b47b19SEmilio G. Cota }
27538b47b19SEmilio G. Cota 
27638b47b19SEmilio G. Cota static TCGOp *copy_op(TCGOp **begin_op, TCGOp *op, TCGOpcode opc)
27738b47b19SEmilio G. Cota {
27838b47b19SEmilio G. Cota     op = copy_op_nocheck(begin_op, op);
27938b47b19SEmilio G. Cota     tcg_debug_assert((*begin_op)->opc == opc);
28038b47b19SEmilio G. Cota     return op;
28138b47b19SEmilio G. Cota }
28238b47b19SEmilio G. Cota 
28338b47b19SEmilio G. Cota static TCGOp *copy_const_ptr(TCGOp **begin_op, TCGOp *op, void *ptr)
28438b47b19SEmilio G. Cota {
28538b47b19SEmilio G. Cota     if (UINTPTR_MAX == UINT32_MAX) {
28680c44bbaSRichard Henderson         /* mov_i32 */
28780c44bbaSRichard Henderson         op = copy_op(begin_op, op, INDEX_op_mov_i32);
28880c44bbaSRichard Henderson         op->args[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr));
28938b47b19SEmilio G. Cota     } else {
29080c44bbaSRichard Henderson         /* mov_i64 */
29180c44bbaSRichard Henderson         op = copy_op(begin_op, op, INDEX_op_mov_i64);
29280c44bbaSRichard Henderson         op->args[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr));
29338b47b19SEmilio G. Cota     }
29438b47b19SEmilio G. Cota     return op;
29538b47b19SEmilio G. Cota }
29638b47b19SEmilio G. Cota 
29762f92b8dSPierrick Bouvier static TCGOp *copy_ld_i32(TCGOp **begin_op, TCGOp *op)
29862f92b8dSPierrick Bouvier {
29962f92b8dSPierrick Bouvier     return copy_op(begin_op, op, INDEX_op_ld_i32);
30062f92b8dSPierrick Bouvier }
30162f92b8dSPierrick Bouvier 
30262f92b8dSPierrick Bouvier static TCGOp *copy_ext_i32_ptr(TCGOp **begin_op, TCGOp *op)
30362f92b8dSPierrick Bouvier {
30462f92b8dSPierrick Bouvier     if (UINTPTR_MAX == UINT32_MAX) {
30562f92b8dSPierrick Bouvier         op = copy_op(begin_op, op, INDEX_op_mov_i32);
30662f92b8dSPierrick Bouvier     } else {
30762f92b8dSPierrick Bouvier         op = copy_op(begin_op, op, INDEX_op_ext_i32_i64);
30862f92b8dSPierrick Bouvier     }
30962f92b8dSPierrick Bouvier     return op;
31062f92b8dSPierrick Bouvier }
31162f92b8dSPierrick Bouvier 
31262f92b8dSPierrick Bouvier static TCGOp *copy_add_ptr(TCGOp **begin_op, TCGOp *op)
31362f92b8dSPierrick Bouvier {
31462f92b8dSPierrick Bouvier     if (UINTPTR_MAX == UINT32_MAX) {
31562f92b8dSPierrick Bouvier         op = copy_op(begin_op, op, INDEX_op_add_i32);
31662f92b8dSPierrick Bouvier     } else {
31762f92b8dSPierrick Bouvier         op = copy_op(begin_op, op, INDEX_op_add_i64);
31862f92b8dSPierrick Bouvier     }
31962f92b8dSPierrick Bouvier     return op;
32062f92b8dSPierrick Bouvier }
32162f92b8dSPierrick Bouvier 
32238b47b19SEmilio G. Cota static TCGOp *copy_ld_i64(TCGOp **begin_op, TCGOp *op)
32338b47b19SEmilio G. Cota {
32438b47b19SEmilio G. Cota     if (TCG_TARGET_REG_BITS == 32) {
32538b47b19SEmilio G. Cota         /* 2x ld_i32 */
32662f92b8dSPierrick Bouvier         op = copy_ld_i32(begin_op, op);
32762f92b8dSPierrick Bouvier         op = copy_ld_i32(begin_op, op);
32838b47b19SEmilio G. Cota     } else {
32938b47b19SEmilio G. Cota         /* ld_i64 */
33038b47b19SEmilio G. Cota         op = copy_op(begin_op, op, INDEX_op_ld_i64);
33138b47b19SEmilio G. Cota     }
33238b47b19SEmilio G. Cota     return op;
33338b47b19SEmilio G. Cota }
33438b47b19SEmilio G. Cota 
33538b47b19SEmilio G. Cota static TCGOp *copy_st_i64(TCGOp **begin_op, TCGOp *op)
33638b47b19SEmilio G. Cota {
33738b47b19SEmilio G. Cota     if (TCG_TARGET_REG_BITS == 32) {
33838b47b19SEmilio G. Cota         /* 2x st_i32 */
33938b47b19SEmilio G. Cota         op = copy_op(begin_op, op, INDEX_op_st_i32);
34038b47b19SEmilio G. Cota         op = copy_op(begin_op, op, INDEX_op_st_i32);
34138b47b19SEmilio G. Cota     } else {
34238b47b19SEmilio G. Cota         /* st_i64 */
34338b47b19SEmilio G. Cota         op = copy_op(begin_op, op, INDEX_op_st_i64);
34438b47b19SEmilio G. Cota     }
34538b47b19SEmilio G. Cota     return op;
34638b47b19SEmilio G. Cota }
34738b47b19SEmilio G. Cota 
3480d6e6cb7SAlex Bennée static TCGOp *copy_add_i64(TCGOp **begin_op, TCGOp *op, uint64_t v)
34938b47b19SEmilio G. Cota {
35038b47b19SEmilio G. Cota     if (TCG_TARGET_REG_BITS == 32) {
35138b47b19SEmilio G. Cota         /* all 32-bit backends must implement add2_i32 */
35238b47b19SEmilio G. Cota         g_assert(TCG_TARGET_HAS_add2_i32);
35338b47b19SEmilio G. Cota         op = copy_op(begin_op, op, INDEX_op_add2_i32);
3540d6e6cb7SAlex Bennée         op->args[4] = tcgv_i32_arg(tcg_constant_i32(v));
3550d6e6cb7SAlex Bennée         op->args[5] = tcgv_i32_arg(tcg_constant_i32(v >> 32));
35638b47b19SEmilio G. Cota     } else {
35738b47b19SEmilio G. Cota         op = copy_op(begin_op, op, INDEX_op_add_i64);
3580d6e6cb7SAlex Bennée         op->args[2] = tcgv_i64_arg(tcg_constant_i64(v));
35938b47b19SEmilio G. Cota     }
36038b47b19SEmilio G. Cota     return op;
36138b47b19SEmilio G. Cota }
36238b47b19SEmilio G. Cota 
36362f92b8dSPierrick Bouvier static TCGOp *copy_mul_i32(TCGOp **begin_op, TCGOp *op, uint32_t v)
36462f92b8dSPierrick Bouvier {
36562f92b8dSPierrick Bouvier     op = copy_op(begin_op, op, INDEX_op_mul_i32);
36662f92b8dSPierrick Bouvier     op->args[2] = tcgv_i32_arg(tcg_constant_i32(v));
36762f92b8dSPierrick Bouvier     return op;
36862f92b8dSPierrick Bouvier }
36962f92b8dSPierrick Bouvier 
37038b47b19SEmilio G. Cota static TCGOp *copy_st_ptr(TCGOp **begin_op, TCGOp *op)
37138b47b19SEmilio G. Cota {
37238b47b19SEmilio G. Cota     if (UINTPTR_MAX == UINT32_MAX) {
37338b47b19SEmilio G. Cota         /* st_i32 */
37438b47b19SEmilio G. Cota         op = copy_op(begin_op, op, INDEX_op_st_i32);
37538b47b19SEmilio G. Cota     } else {
37638b47b19SEmilio G. Cota         /* st_i64 */
37738b47b19SEmilio G. Cota         op = copy_st_i64(begin_op, op);
37838b47b19SEmilio G. Cota     }
37938b47b19SEmilio G. Cota     return op;
38038b47b19SEmilio G. Cota }
38138b47b19SEmilio G. Cota 
382946bf79eSAkihiko Odaki static TCGOp *copy_call(TCGOp **begin_op, TCGOp *op, void *func, int *cb_idx)
38338b47b19SEmilio G. Cota {
38405d019abSRichard Henderson     TCGOp *old_op;
38505d019abSRichard Henderson     int func_idx;
38605d019abSRichard Henderson 
38738b47b19SEmilio G. Cota     /* copy all ops until the call */
38838b47b19SEmilio G. Cota     do {
38938b47b19SEmilio G. Cota         op = copy_op_nocheck(begin_op, op);
39038b47b19SEmilio G. Cota     } while (op->opc != INDEX_op_call);
39138b47b19SEmilio G. Cota 
39238b47b19SEmilio G. Cota     /* fill in the op call */
39305d019abSRichard Henderson     old_op = *begin_op;
39405d019abSRichard Henderson     TCGOP_CALLI(op) = TCGOP_CALLI(old_op);
39505d019abSRichard Henderson     TCGOP_CALLO(op) = TCGOP_CALLO(old_op);
39638b47b19SEmilio G. Cota     tcg_debug_assert(op->life == 0);
39738b47b19SEmilio G. Cota 
39805d019abSRichard Henderson     func_idx = TCGOP_CALLO(op) + TCGOP_CALLI(op);
39905d019abSRichard Henderson     *cb_idx = func_idx;
40005d019abSRichard Henderson     op->args[func_idx] = (uintptr_t)func;
40138b47b19SEmilio G. Cota 
40238b47b19SEmilio G. Cota     return op;
40338b47b19SEmilio G. Cota }
40438b47b19SEmilio G. Cota 
4050d6e6cb7SAlex Bennée /*
4060d6e6cb7SAlex Bennée  * When we append/replace ops here we are sensitive to changing patterns of
4070d6e6cb7SAlex Bennée  * TCGOps generated by the tcg_gen_FOO calls when we generated the
4080d6e6cb7SAlex Bennée  * empty callbacks. This will assert very quickly in a debug build as
4090d6e6cb7SAlex Bennée  * we assert the ops we are replacing are the correct ones.
4100d6e6cb7SAlex Bennée  */
41138b47b19SEmilio G. Cota static TCGOp *append_udata_cb(const struct qemu_plugin_dyn_cb *cb,
41238b47b19SEmilio G. Cota                               TCGOp *begin_op, TCGOp *op, int *cb_idx)
41338b47b19SEmilio G. Cota {
41438b47b19SEmilio G. Cota     /* const_ptr */
41538b47b19SEmilio G. Cota     op = copy_const_ptr(&begin_op, op, cb->userp);
41638b47b19SEmilio G. Cota 
41738b47b19SEmilio G. Cota     /* copy the ld_i32, but note that we only have to copy it once */
418f266bec8SRichard Henderson     if (*cb_idx == -1) {
419f266bec8SRichard Henderson         op = copy_op(&begin_op, op, INDEX_op_ld_i32);
420f266bec8SRichard Henderson     } else {
42138b47b19SEmilio G. Cota         begin_op = QTAILQ_NEXT(begin_op, link);
42238b47b19SEmilio G. Cota         tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32);
42338b47b19SEmilio G. Cota     }
42438b47b19SEmilio G. Cota 
42538b47b19SEmilio G. Cota     /* call */
426aff56de5SRichard Henderson     op = copy_call(&begin_op, op, cb->regular.f.vcpu_udata, cb_idx);
42738b47b19SEmilio G. Cota 
42838b47b19SEmilio G. Cota     return op;
42938b47b19SEmilio G. Cota }
43038b47b19SEmilio G. Cota 
43138b47b19SEmilio G. Cota static TCGOp *append_inline_cb(const struct qemu_plugin_dyn_cb *cb,
43238b47b19SEmilio G. Cota                                TCGOp *begin_op, TCGOp *op,
43338b47b19SEmilio G. Cota                                int *unused)
43438b47b19SEmilio G. Cota {
4353077be25SPierrick Bouvier     char *ptr = cb->inline_insn.entry.score->data->data;
4363077be25SPierrick Bouvier     size_t elem_size = g_array_get_element_size(
4373077be25SPierrick Bouvier         cb->inline_insn.entry.score->data);
4383077be25SPierrick Bouvier     size_t offset = cb->inline_insn.entry.offset;
4390bcebabaSPierrick Bouvier 
44062f92b8dSPierrick Bouvier     op = copy_ld_i32(&begin_op, op);
44162f92b8dSPierrick Bouvier     op = copy_mul_i32(&begin_op, op, elem_size);
44262f92b8dSPierrick Bouvier     op = copy_ext_i32_ptr(&begin_op, op);
44362f92b8dSPierrick Bouvier     op = copy_const_ptr(&begin_op, op, ptr + offset);
44462f92b8dSPierrick Bouvier     op = copy_add_ptr(&begin_op, op);
44538b47b19SEmilio G. Cota     op = copy_ld_i64(&begin_op, op);
4460d6e6cb7SAlex Bennée     op = copy_add_i64(&begin_op, op, cb->inline_insn.imm);
44738b47b19SEmilio G. Cota     op = copy_st_i64(&begin_op, op);
44838b47b19SEmilio G. Cota     return op;
44938b47b19SEmilio G. Cota }
45038b47b19SEmilio G. Cota 
45138b47b19SEmilio G. Cota static TCGOp *append_mem_cb(const struct qemu_plugin_dyn_cb *cb,
45238b47b19SEmilio G. Cota                             TCGOp *begin_op, TCGOp *op, int *cb_idx)
45338b47b19SEmilio G. Cota {
45438b47b19SEmilio G. Cota     enum plugin_gen_cb type = begin_op->args[1];
45538b47b19SEmilio G. Cota 
45638b47b19SEmilio G. Cota     tcg_debug_assert(type == PLUGIN_GEN_CB_MEM);
45738b47b19SEmilio G. Cota 
45880c44bbaSRichard Henderson     /* const_i32 == mov_i32 ("info", so it remains as is) */
45980c44bbaSRichard Henderson     op = copy_op(&begin_op, op, INDEX_op_mov_i32);
46038b47b19SEmilio G. Cota 
46138b47b19SEmilio G. Cota     /* const_ptr */
46238b47b19SEmilio G. Cota     op = copy_const_ptr(&begin_op, op, cb->userp);
46338b47b19SEmilio G. Cota 
46438b47b19SEmilio G. Cota     /* copy the ld_i32, but note that we only have to copy it once */
465f266bec8SRichard Henderson     if (*cb_idx == -1) {
466f266bec8SRichard Henderson         op = copy_op(&begin_op, op, INDEX_op_ld_i32);
467f266bec8SRichard Henderson     } else {
46838b47b19SEmilio G. Cota         begin_op = QTAILQ_NEXT(begin_op, link);
46938b47b19SEmilio G. Cota         tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32);
47038b47b19SEmilio G. Cota     }
47138b47b19SEmilio G. Cota 
47238b47b19SEmilio G. Cota     if (type == PLUGIN_GEN_CB_MEM) {
47338b47b19SEmilio G. Cota         /* call */
474aff56de5SRichard Henderson         op = copy_call(&begin_op, op, cb->regular.f.vcpu_udata, cb_idx);
47538b47b19SEmilio G. Cota     }
47638b47b19SEmilio G. Cota 
47738b47b19SEmilio G. Cota     return op;
47838b47b19SEmilio G. Cota }
47938b47b19SEmilio G. Cota 
48038b47b19SEmilio G. Cota typedef TCGOp *(*inject_fn)(const struct qemu_plugin_dyn_cb *cb,
48138b47b19SEmilio G. Cota                             TCGOp *begin_op, TCGOp *op, int *intp);
48238b47b19SEmilio G. Cota typedef bool (*op_ok_fn)(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb);
48338b47b19SEmilio G. Cota 
48438b47b19SEmilio G. Cota static bool op_ok(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb)
48538b47b19SEmilio G. Cota {
48638b47b19SEmilio G. Cota     return true;
48738b47b19SEmilio G. Cota }
48838b47b19SEmilio G. Cota 
48938b47b19SEmilio G. Cota static bool op_rw(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb)
49038b47b19SEmilio G. Cota {
49138b47b19SEmilio G. Cota     int w;
49238b47b19SEmilio G. Cota 
49338b47b19SEmilio G. Cota     w = op->args[2];
49438b47b19SEmilio G. Cota     return !!(cb->rw & (w + 1));
49538b47b19SEmilio G. Cota }
49638b47b19SEmilio G. Cota 
4979a3ee366SRichard Henderson static void inject_cb_type(const GArray *cbs, TCGOp *begin_op,
4989a3ee366SRichard Henderson                            inject_fn inject, op_ok_fn ok)
49938b47b19SEmilio G. Cota {
50038b47b19SEmilio G. Cota     TCGOp *end_op;
50138b47b19SEmilio G. Cota     TCGOp *op;
50238b47b19SEmilio G. Cota     int cb_idx = -1;
50338b47b19SEmilio G. Cota     int i;
50438b47b19SEmilio G. Cota 
50538b47b19SEmilio G. Cota     if (!cbs || cbs->len == 0) {
50638b47b19SEmilio G. Cota         rm_ops(begin_op);
50738b47b19SEmilio G. Cota         return;
50838b47b19SEmilio G. Cota     }
50938b47b19SEmilio G. Cota 
51038b47b19SEmilio G. Cota     end_op = find_op(begin_op, INDEX_op_plugin_cb_end);
51138b47b19SEmilio G. Cota     tcg_debug_assert(end_op);
51238b47b19SEmilio G. Cota 
51338b47b19SEmilio G. Cota     op = end_op;
51438b47b19SEmilio G. Cota     for (i = 0; i < cbs->len; i++) {
51538b47b19SEmilio G. Cota         struct qemu_plugin_dyn_cb *cb =
51638b47b19SEmilio G. Cota             &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
51738b47b19SEmilio G. Cota 
51838b47b19SEmilio G. Cota         if (!ok(begin_op, cb)) {
51938b47b19SEmilio G. Cota             continue;
52038b47b19SEmilio G. Cota         }
52138b47b19SEmilio G. Cota         op = inject(cb, begin_op, op, &cb_idx);
52238b47b19SEmilio G. Cota     }
52338b47b19SEmilio G. Cota     rm_ops_range(begin_op, end_op);
52438b47b19SEmilio G. Cota }
52538b47b19SEmilio G. Cota 
52638b47b19SEmilio G. Cota static void
52738b47b19SEmilio G. Cota inject_udata_cb(const GArray *cbs, TCGOp *begin_op)
52838b47b19SEmilio G. Cota {
52938b47b19SEmilio G. Cota     inject_cb_type(cbs, begin_op, append_udata_cb, op_ok);
53038b47b19SEmilio G. Cota }
53138b47b19SEmilio G. Cota 
53238b47b19SEmilio G. Cota static void
53338b47b19SEmilio G. Cota inject_inline_cb(const GArray *cbs, TCGOp *begin_op, op_ok_fn ok)
53438b47b19SEmilio G. Cota {
53538b47b19SEmilio G. Cota     inject_cb_type(cbs, begin_op, append_inline_cb, ok);
53638b47b19SEmilio G. Cota }
53738b47b19SEmilio G. Cota 
53838b47b19SEmilio G. Cota static void
53938b47b19SEmilio G. Cota inject_mem_cb(const GArray *cbs, TCGOp *begin_op)
54038b47b19SEmilio G. Cota {
54138b47b19SEmilio G. Cota     inject_cb_type(cbs, begin_op, append_mem_cb, op_rw);
54238b47b19SEmilio G. Cota }
54338b47b19SEmilio G. Cota 
54438b47b19SEmilio G. Cota /* we could change the ops in place, but we can reuse more code by copying */
54538b47b19SEmilio G. Cota static void inject_mem_helper(TCGOp *begin_op, GArray *arr)
54638b47b19SEmilio G. Cota {
54738b47b19SEmilio G. Cota     TCGOp *orig_op = begin_op;
54838b47b19SEmilio G. Cota     TCGOp *end_op;
54938b47b19SEmilio G. Cota     TCGOp *op;
55038b47b19SEmilio G. Cota 
55138b47b19SEmilio G. Cota     end_op = find_op(begin_op, INDEX_op_plugin_cb_end);
55238b47b19SEmilio G. Cota     tcg_debug_assert(end_op);
55338b47b19SEmilio G. Cota 
55438b47b19SEmilio G. Cota     /* const ptr */
55538b47b19SEmilio G. Cota     op = copy_const_ptr(&begin_op, end_op, arr);
55638b47b19SEmilio G. Cota 
55738b47b19SEmilio G. Cota     /* st_ptr */
55838b47b19SEmilio G. Cota     op = copy_st_ptr(&begin_op, op);
55938b47b19SEmilio G. Cota 
56038b47b19SEmilio G. Cota     rm_ops_range(orig_op, end_op);
56138b47b19SEmilio G. Cota }
56238b47b19SEmilio G. Cota 
56338b47b19SEmilio G. Cota /*
56438b47b19SEmilio G. Cota  * Tracking memory accesses performed from helpers requires extra work.
56538b47b19SEmilio G. Cota  * If an instruction is emulated with helpers, we do two things:
56638b47b19SEmilio G. Cota  * (1) copy the CB descriptors, and keep track of it so that they can be
56738b47b19SEmilio G. Cota  * freed later on, and (2) point CPUState.plugin_mem_cbs to the descriptors, so
56838b47b19SEmilio G. Cota  * that we can read them at run-time (i.e. when the helper executes).
56938b47b19SEmilio G. Cota  * This run-time access is performed from qemu_plugin_vcpu_mem_cb.
57038b47b19SEmilio G. Cota  *
57138b47b19SEmilio G. Cota  * Note that plugin_gen_disable_mem_helpers undoes (2). Since it
57238b47b19SEmilio G. Cota  * is possible that the code we generate after the instruction is
57338b47b19SEmilio G. Cota  * dead, we also add checks before generating tb_exit etc.
57438b47b19SEmilio G. Cota  */
5753fd62e73SEmilio Cota static void inject_mem_enable_helper(struct qemu_plugin_tb *ptb,
5763fd62e73SEmilio Cota                                      struct qemu_plugin_insn *plugin_insn,
57738b47b19SEmilio G. Cota                                      TCGOp *begin_op)
57838b47b19SEmilio G. Cota {
57938b47b19SEmilio G. Cota     GArray *cbs[2];
58038b47b19SEmilio G. Cota     GArray *arr;
58138b47b19SEmilio G. Cota     size_t n_cbs, i;
58238b47b19SEmilio G. Cota 
58338b47b19SEmilio G. Cota     cbs[0] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR];
58438b47b19SEmilio G. Cota     cbs[1] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE];
58538b47b19SEmilio G. Cota 
58638b47b19SEmilio G. Cota     n_cbs = 0;
58738b47b19SEmilio G. Cota     for (i = 0; i < ARRAY_SIZE(cbs); i++) {
58838b47b19SEmilio G. Cota         n_cbs += cbs[i]->len;
58938b47b19SEmilio G. Cota     }
59038b47b19SEmilio G. Cota 
59138b47b19SEmilio G. Cota     plugin_insn->mem_helper = plugin_insn->calls_helpers && n_cbs;
59238b47b19SEmilio G. Cota     if (likely(!plugin_insn->mem_helper)) {
59338b47b19SEmilio G. Cota         rm_ops(begin_op);
59438b47b19SEmilio G. Cota         return;
59538b47b19SEmilio G. Cota     }
5963fd62e73SEmilio Cota     ptb->mem_helper = true;
59738b47b19SEmilio G. Cota 
59838b47b19SEmilio G. Cota     arr = g_array_sized_new(false, false,
59938b47b19SEmilio G. Cota                             sizeof(struct qemu_plugin_dyn_cb), n_cbs);
60038b47b19SEmilio G. Cota 
60138b47b19SEmilio G. Cota     for (i = 0; i < ARRAY_SIZE(cbs); i++) {
60238b47b19SEmilio G. Cota         g_array_append_vals(arr, cbs[i]->data, cbs[i]->len);
60338b47b19SEmilio G. Cota     }
60438b47b19SEmilio G. Cota 
60538b47b19SEmilio G. Cota     qemu_plugin_add_dyn_cb_arr(arr);
60638b47b19SEmilio G. Cota     inject_mem_helper(begin_op, arr);
60738b47b19SEmilio G. Cota }
60838b47b19SEmilio G. Cota 
60938b47b19SEmilio G. Cota /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */
61038b47b19SEmilio G. Cota void plugin_gen_disable_mem_helpers(void)
61138b47b19SEmilio G. Cota {
6123fd62e73SEmilio Cota     /*
6133fd62e73SEmilio Cota      * We could emit the clearing unconditionally and be done. However, this can
6143fd62e73SEmilio Cota      * be wasteful if for instance plugins don't track memory accesses, or if
6153fd62e73SEmilio Cota      * most TBs don't use helpers. Instead, emit the clearing iff the TB calls
6163fd62e73SEmilio Cota      * helpers that might access guest memory.
6173fd62e73SEmilio Cota      *
6183fd62e73SEmilio Cota      * Note: we do not reset plugin_tb->mem_helper here; a TB might have several
6193fd62e73SEmilio Cota      * exit points, and we want to emit the clearing from all of them.
6203fd62e73SEmilio Cota      */
6213fd62e73SEmilio Cota     if (!tcg_ctx->plugin_tb->mem_helper) {
62238b47b19SEmilio G. Cota         return;
62338b47b19SEmilio G. Cota     }
624ad75a51eSRichard Henderson     tcg_gen_st_ptr(tcg_constant_ptr(NULL), tcg_env,
62540138843SRichard Henderson                    offsetof(CPUState, plugin_mem_cbs) - offsetof(ArchCPU, env));
62638b47b19SEmilio G. Cota }
62738b47b19SEmilio G. Cota 
62838b47b19SEmilio G. Cota static void plugin_gen_insn_udata(const struct qemu_plugin_tb *ptb,
62938b47b19SEmilio G. Cota                                   TCGOp *begin_op, int insn_idx)
63038b47b19SEmilio G. Cota {
63138b47b19SEmilio G. Cota     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
63238b47b19SEmilio G. Cota 
63338b47b19SEmilio G. Cota     inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR], begin_op);
63438b47b19SEmilio G. Cota }
63538b47b19SEmilio G. Cota 
63633a277feSAkihiko Odaki static void plugin_gen_insn_udata_r(const struct qemu_plugin_tb *ptb,
63733a277feSAkihiko Odaki                                     TCGOp *begin_op, int insn_idx)
63833a277feSAkihiko Odaki {
63933a277feSAkihiko Odaki     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
64033a277feSAkihiko Odaki 
64133a277feSAkihiko Odaki     inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR_R], begin_op);
64233a277feSAkihiko Odaki }
64333a277feSAkihiko Odaki 
64438b47b19SEmilio G. Cota static void plugin_gen_insn_inline(const struct qemu_plugin_tb *ptb,
64538b47b19SEmilio G. Cota                                    TCGOp *begin_op, int insn_idx)
64638b47b19SEmilio G. Cota {
64738b47b19SEmilio G. Cota     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
64838b47b19SEmilio G. Cota     inject_inline_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE],
64938b47b19SEmilio G. Cota                      begin_op, op_ok);
65038b47b19SEmilio G. Cota }
65138b47b19SEmilio G. Cota 
65238b47b19SEmilio G. Cota static void plugin_gen_mem_regular(const struct qemu_plugin_tb *ptb,
65338b47b19SEmilio G. Cota                                    TCGOp *begin_op, int insn_idx)
65438b47b19SEmilio G. Cota {
65538b47b19SEmilio G. Cota     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
65638b47b19SEmilio G. Cota     inject_mem_cb(insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR], begin_op);
65738b47b19SEmilio G. Cota }
65838b47b19SEmilio G. Cota 
65938b47b19SEmilio G. Cota static void plugin_gen_mem_inline(const struct qemu_plugin_tb *ptb,
66038b47b19SEmilio G. Cota                                   TCGOp *begin_op, int insn_idx)
66138b47b19SEmilio G. Cota {
66238b47b19SEmilio G. Cota     const GArray *cbs;
66338b47b19SEmilio G. Cota     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
66438b47b19SEmilio G. Cota 
66538b47b19SEmilio G. Cota     cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE];
66638b47b19SEmilio G. Cota     inject_inline_cb(cbs, begin_op, op_rw);
66738b47b19SEmilio G. Cota }
66838b47b19SEmilio G. Cota 
6693fd62e73SEmilio Cota static void plugin_gen_enable_mem_helper(struct qemu_plugin_tb *ptb,
67038b47b19SEmilio G. Cota                                          TCGOp *begin_op, int insn_idx)
67138b47b19SEmilio G. Cota {
67238b47b19SEmilio G. Cota     struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx);
6733fd62e73SEmilio Cota     inject_mem_enable_helper(ptb, insn, begin_op);
67438b47b19SEmilio G. Cota }
67538b47b19SEmilio G. Cota 
676a0948bb7SRichard Henderson static void gen_disable_mem_helper(struct qemu_plugin_tb *ptb,
677a0948bb7SRichard Henderson                                    struct qemu_plugin_insn *insn)
67838b47b19SEmilio G. Cota {
679a0948bb7SRichard Henderson     if (insn->mem_helper) {
680a0948bb7SRichard Henderson         tcg_gen_st_ptr(tcg_constant_ptr(0), tcg_env,
681a0948bb7SRichard Henderson                        offsetof(CPUState, plugin_mem_cbs) -
682a0948bb7SRichard Henderson                        offsetof(ArchCPU, env));
683a0948bb7SRichard Henderson     }
68438b47b19SEmilio G. Cota }
68538b47b19SEmilio G. Cota 
686*21a3f62fSRichard Henderson static void gen_udata_cb(struct qemu_plugin_dyn_cb *cb)
687*21a3f62fSRichard Henderson {
688*21a3f62fSRichard Henderson     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
689*21a3f62fSRichard Henderson 
690*21a3f62fSRichard Henderson     tcg_gen_ld_i32(cpu_index, tcg_env,
691*21a3f62fSRichard Henderson                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
692*21a3f62fSRichard Henderson     tcg_gen_call2(cb->regular.f.vcpu_udata, cb->regular.info, NULL,
693*21a3f62fSRichard Henderson                   tcgv_i32_temp(cpu_index),
694*21a3f62fSRichard Henderson                   tcgv_ptr_temp(tcg_constant_ptr(cb->userp)));
695*21a3f62fSRichard Henderson     tcg_temp_free_i32(cpu_index);
696*21a3f62fSRichard Henderson }
697*21a3f62fSRichard Henderson 
698*21a3f62fSRichard Henderson static void gen_inline_cb(struct qemu_plugin_dyn_cb *cb)
699*21a3f62fSRichard Henderson {
700*21a3f62fSRichard Henderson     GArray *arr = cb->inline_insn.entry.score->data;
701*21a3f62fSRichard Henderson     size_t offset = cb->inline_insn.entry.offset;
702*21a3f62fSRichard Henderson     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
703*21a3f62fSRichard Henderson     TCGv_i64 val = tcg_temp_ebb_new_i64();
704*21a3f62fSRichard Henderson     TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
705*21a3f62fSRichard Henderson 
706*21a3f62fSRichard Henderson     tcg_gen_ld_i32(cpu_index, tcg_env,
707*21a3f62fSRichard Henderson                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
708*21a3f62fSRichard Henderson     tcg_gen_muli_i32(cpu_index, cpu_index, g_array_get_element_size(arr));
709*21a3f62fSRichard Henderson     tcg_gen_ext_i32_ptr(ptr, cpu_index);
710*21a3f62fSRichard Henderson     tcg_temp_free_i32(cpu_index);
711*21a3f62fSRichard Henderson 
712*21a3f62fSRichard Henderson     tcg_gen_addi_ptr(ptr, ptr, (intptr_t)arr->data);
713*21a3f62fSRichard Henderson     tcg_gen_ld_i64(val, ptr, offset);
714*21a3f62fSRichard Henderson     tcg_gen_addi_i64(val, val, cb->inline_insn.imm);
715*21a3f62fSRichard Henderson     tcg_gen_st_i64(val, ptr, offset);
716*21a3f62fSRichard Henderson 
717*21a3f62fSRichard Henderson     tcg_temp_free_i64(val);
718*21a3f62fSRichard Henderson     tcg_temp_free_ptr(ptr);
719*21a3f62fSRichard Henderson }
720*21a3f62fSRichard Henderson 
72138b47b19SEmilio G. Cota /* #define DEBUG_PLUGIN_GEN_OPS */
72238b47b19SEmilio G. Cota static void pr_ops(void)
72338b47b19SEmilio G. Cota {
72438b47b19SEmilio G. Cota #ifdef DEBUG_PLUGIN_GEN_OPS
72538b47b19SEmilio G. Cota     TCGOp *op;
72638b47b19SEmilio G. Cota     int i = 0;
72738b47b19SEmilio G. Cota 
72838b47b19SEmilio G. Cota     QTAILQ_FOREACH(op, &tcg_ctx->ops, link) {
72938b47b19SEmilio G. Cota         const char *name = "";
73038b47b19SEmilio G. Cota         const char *type = "";
73138b47b19SEmilio G. Cota 
73238b47b19SEmilio G. Cota         if (op->opc == INDEX_op_plugin_cb_start) {
73338b47b19SEmilio G. Cota             switch (op->args[0]) {
73438b47b19SEmilio G. Cota             case PLUGIN_GEN_FROM_TB:
73538b47b19SEmilio G. Cota                 name = "tb";
73638b47b19SEmilio G. Cota                 break;
73738b47b19SEmilio G. Cota             case PLUGIN_GEN_FROM_INSN:
73838b47b19SEmilio G. Cota                 name = "insn";
73938b47b19SEmilio G. Cota                 break;
74038b47b19SEmilio G. Cota             case PLUGIN_GEN_FROM_MEM:
74138b47b19SEmilio G. Cota                 name = "mem";
74238b47b19SEmilio G. Cota                 break;
74338b47b19SEmilio G. Cota             case PLUGIN_GEN_AFTER_INSN:
74438b47b19SEmilio G. Cota                 name = "after insn";
74538b47b19SEmilio G. Cota                 break;
74638b47b19SEmilio G. Cota             default:
74738b47b19SEmilio G. Cota                 break;
74838b47b19SEmilio G. Cota             }
74938b47b19SEmilio G. Cota             switch (op->args[1]) {
75038b47b19SEmilio G. Cota             case PLUGIN_GEN_CB_UDATA:
75138b47b19SEmilio G. Cota                 type = "udata";
75238b47b19SEmilio G. Cota                 break;
75338b47b19SEmilio G. Cota             case PLUGIN_GEN_CB_INLINE:
75438b47b19SEmilio G. Cota                 type = "inline";
75538b47b19SEmilio G. Cota                 break;
75638b47b19SEmilio G. Cota             case PLUGIN_GEN_CB_MEM:
75738b47b19SEmilio G. Cota                 type = "mem";
75838b47b19SEmilio G. Cota                 break;
75938b47b19SEmilio G. Cota             case PLUGIN_GEN_ENABLE_MEM_HELPER:
76038b47b19SEmilio G. Cota                 type = "enable mem helper";
76138b47b19SEmilio G. Cota                 break;
76238b47b19SEmilio G. Cota             case PLUGIN_GEN_DISABLE_MEM_HELPER:
76338b47b19SEmilio G. Cota                 type = "disable mem helper";
76438b47b19SEmilio G. Cota                 break;
76538b47b19SEmilio G. Cota             default:
76638b47b19SEmilio G. Cota                 break;
76738b47b19SEmilio G. Cota             }
76838b47b19SEmilio G. Cota         }
76938b47b19SEmilio G. Cota         printf("op[%2i]: %s %s %s\n", i, tcg_op_defs[op->opc].name, name, type);
77038b47b19SEmilio G. Cota         i++;
77138b47b19SEmilio G. Cota     }
77238b47b19SEmilio G. Cota #endif
77338b47b19SEmilio G. Cota }
77438b47b19SEmilio G. Cota 
7753fd62e73SEmilio Cota static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb)
77638b47b19SEmilio G. Cota {
777a0948bb7SRichard Henderson     TCGOp *op, *next;
778453d50ceSAlex Bennée     int insn_idx = -1;
77938b47b19SEmilio G. Cota 
78038b47b19SEmilio G. Cota     pr_ops();
781453d50ceSAlex Bennée 
782a0948bb7SRichard Henderson     /*
783a0948bb7SRichard Henderson      * While injecting code, we cannot afford to reuse any ebb temps
784a0948bb7SRichard Henderson      * that might be live within the existing opcode stream.
785a0948bb7SRichard Henderson      * The simplest solution is to release them all and create new.
786a0948bb7SRichard Henderson      */
787a0948bb7SRichard Henderson     memset(tcg_ctx->free_temps, 0, sizeof(tcg_ctx->free_temps));
788a0948bb7SRichard Henderson 
789a0948bb7SRichard Henderson     QTAILQ_FOREACH_SAFE(op, &tcg_ctx->ops, link, next) {
790453d50ceSAlex Bennée         switch (op->opc) {
791453d50ceSAlex Bennée         case INDEX_op_insn_start:
792453d50ceSAlex Bennée             insn_idx++;
793453d50ceSAlex Bennée             break;
794a0948bb7SRichard Henderson 
795a0948bb7SRichard Henderson         case INDEX_op_plugin_cb:
796a0948bb7SRichard Henderson         {
797a0948bb7SRichard Henderson             enum plugin_gen_from from = op->args[0];
798a0948bb7SRichard Henderson             struct qemu_plugin_insn *insn = NULL;
799*21a3f62fSRichard Henderson             const GArray *cbs;
800*21a3f62fSRichard Henderson             int i, n;
801a0948bb7SRichard Henderson 
802a0948bb7SRichard Henderson             if (insn_idx >= 0) {
803a0948bb7SRichard Henderson                 insn = g_ptr_array_index(plugin_tb->insns, insn_idx);
804a0948bb7SRichard Henderson             }
805a0948bb7SRichard Henderson 
806a0948bb7SRichard Henderson             tcg_ctx->emit_before_op = op;
807a0948bb7SRichard Henderson 
808a0948bb7SRichard Henderson             switch (from) {
809a0948bb7SRichard Henderson             case PLUGIN_GEN_AFTER_INSN:
810a0948bb7SRichard Henderson                 assert(insn != NULL);
811a0948bb7SRichard Henderson                 gen_disable_mem_helper(plugin_tb, insn);
812a0948bb7SRichard Henderson                 break;
813*21a3f62fSRichard Henderson 
814*21a3f62fSRichard Henderson             case PLUGIN_GEN_FROM_TB:
815*21a3f62fSRichard Henderson                 assert(insn == NULL);
816*21a3f62fSRichard Henderson 
817*21a3f62fSRichard Henderson                 cbs = plugin_tb->cbs[PLUGIN_CB_REGULAR];
818*21a3f62fSRichard Henderson                 for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
819*21a3f62fSRichard Henderson                     struct qemu_plugin_dyn_cb *cb =
820*21a3f62fSRichard Henderson                         &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
821*21a3f62fSRichard Henderson                     gen_udata_cb(cb);
822*21a3f62fSRichard Henderson                 }
823*21a3f62fSRichard Henderson 
824*21a3f62fSRichard Henderson                 cbs = plugin_tb->cbs[PLUGIN_CB_INLINE];
825*21a3f62fSRichard Henderson                 for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
826*21a3f62fSRichard Henderson                     struct qemu_plugin_dyn_cb *cb =
827*21a3f62fSRichard Henderson                         &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
828*21a3f62fSRichard Henderson                     gen_inline_cb(cb);
829*21a3f62fSRichard Henderson                 }
830*21a3f62fSRichard Henderson                 break;
831*21a3f62fSRichard Henderson 
832a0948bb7SRichard Henderson             default:
833a0948bb7SRichard Henderson                 g_assert_not_reached();
834a0948bb7SRichard Henderson             }
835a0948bb7SRichard Henderson 
836a0948bb7SRichard Henderson             tcg_ctx->emit_before_op = NULL;
837a0948bb7SRichard Henderson             tcg_op_remove(tcg_ctx, op);
838a0948bb7SRichard Henderson             break;
839a0948bb7SRichard Henderson         }
840a0948bb7SRichard Henderson 
841453d50ceSAlex Bennée         case INDEX_op_plugin_cb_start:
842453d50ceSAlex Bennée         {
84338b47b19SEmilio G. Cota             enum plugin_gen_from from = op->args[0];
84438b47b19SEmilio G. Cota             enum plugin_gen_cb type = op->args[1];
84538b47b19SEmilio G. Cota 
846453d50ceSAlex Bennée             switch (from) {
847453d50ceSAlex Bennée             case PLUGIN_GEN_FROM_INSN:
848453d50ceSAlex Bennée             {
849453d50ceSAlex Bennée                 g_assert(insn_idx >= 0);
850453d50ceSAlex Bennée 
851453d50ceSAlex Bennée                 switch (type) {
852453d50ceSAlex Bennée                 case PLUGIN_GEN_CB_UDATA:
853453d50ceSAlex Bennée                     plugin_gen_insn_udata(plugin_tb, op, insn_idx);
854453d50ceSAlex Bennée                     break;
85533a277feSAkihiko Odaki                 case PLUGIN_GEN_CB_UDATA_R:
85633a277feSAkihiko Odaki                     plugin_gen_insn_udata_r(plugin_tb, op, insn_idx);
85733a277feSAkihiko Odaki                     break;
858453d50ceSAlex Bennée                 case PLUGIN_GEN_CB_INLINE:
859453d50ceSAlex Bennée                     plugin_gen_insn_inline(plugin_tb, op, insn_idx);
860453d50ceSAlex Bennée                     break;
861453d50ceSAlex Bennée                 case PLUGIN_GEN_ENABLE_MEM_HELPER:
862453d50ceSAlex Bennée                     plugin_gen_enable_mem_helper(plugin_tb, op, insn_idx);
863453d50ceSAlex Bennée                     break;
864453d50ceSAlex Bennée                 default:
865453d50ceSAlex Bennée                     g_assert_not_reached();
866453d50ceSAlex Bennée                 }
867453d50ceSAlex Bennée                 break;
868453d50ceSAlex Bennée             }
869453d50ceSAlex Bennée             case PLUGIN_GEN_FROM_MEM:
870453d50ceSAlex Bennée             {
871453d50ceSAlex Bennée                 g_assert(insn_idx >= 0);
872453d50ceSAlex Bennée 
873453d50ceSAlex Bennée                 switch (type) {
874453d50ceSAlex Bennée                 case PLUGIN_GEN_CB_MEM:
875453d50ceSAlex Bennée                     plugin_gen_mem_regular(plugin_tb, op, insn_idx);
876453d50ceSAlex Bennée                     break;
877453d50ceSAlex Bennée                 case PLUGIN_GEN_CB_INLINE:
878453d50ceSAlex Bennée                     plugin_gen_mem_inline(plugin_tb, op, insn_idx);
879453d50ceSAlex Bennée                     break;
880453d50ceSAlex Bennée                 default:
881453d50ceSAlex Bennée                     g_assert_not_reached();
882453d50ceSAlex Bennée                 }
883453d50ceSAlex Bennée 
884453d50ceSAlex Bennée                 break;
885453d50ceSAlex Bennée             }
886453d50ceSAlex Bennée             default:
887453d50ceSAlex Bennée                 g_assert_not_reached();
888453d50ceSAlex Bennée             }
889453d50ceSAlex Bennée             break;
890453d50ceSAlex Bennée         }
891453d50ceSAlex Bennée         default:
892453d50ceSAlex Bennée             /* plugins don't care about any other ops */
893453d50ceSAlex Bennée             break;
894453d50ceSAlex Bennée         }
89538b47b19SEmilio G. Cota     }
89638b47b19SEmilio G. Cota     pr_ops();
89738b47b19SEmilio G. Cota }
89838b47b19SEmilio G. Cota 
899b21af662SRichard Henderson bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db,
900b21af662SRichard Henderson                          bool mem_only)
90138b47b19SEmilio G. Cota {
90238b47b19SEmilio G. Cota     bool ret = false;
90338b47b19SEmilio G. Cota 
904c0061471SAlex Bennée     if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_state->event_mask)) {
9056f15c076SAlex Bennée         struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
9066f15c076SAlex Bennée         int i;
9076f15c076SAlex Bennée 
9086f15c076SAlex Bennée         /* reset callbacks */
9096f15c076SAlex Bennée         for (i = 0; i < PLUGIN_N_CB_SUBTYPES; i++) {
9106f15c076SAlex Bennée             if (ptb->cbs[i]) {
9116f15c076SAlex Bennée                 g_array_set_size(ptb->cbs[i], 0);
9126f15c076SAlex Bennée             }
9136f15c076SAlex Bennée         }
9146f15c076SAlex Bennée         ptb->n = 0;
9156f15c076SAlex Bennée 
91638b47b19SEmilio G. Cota         ret = true;
91738b47b19SEmilio G. Cota 
918b21af662SRichard Henderson         ptb->vaddr = db->pc_first;
91938b47b19SEmilio G. Cota         ptb->vaddr2 = -1;
920b21af662SRichard Henderson         ptb->haddr1 = db->host_addr[0];
92138b47b19SEmilio G. Cota         ptb->haddr2 = NULL;
922cfd405eaSAlex Bennée         ptb->mem_only = mem_only;
9233fd62e73SEmilio Cota         ptb->mem_helper = false;
92438b47b19SEmilio G. Cota 
92538b47b19SEmilio G. Cota         plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB);
92638b47b19SEmilio G. Cota     }
9276f15c076SAlex Bennée 
9286f15c076SAlex Bennée     tcg_ctx->plugin_insn = NULL;
9296f15c076SAlex Bennée 
93038b47b19SEmilio G. Cota     return ret;
93138b47b19SEmilio G. Cota }
93238b47b19SEmilio G. Cota 
93338b47b19SEmilio G. Cota void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db)
93438b47b19SEmilio G. Cota {
93538b47b19SEmilio G. Cota     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
93638b47b19SEmilio G. Cota     struct qemu_plugin_insn *pinsn;
93738b47b19SEmilio G. Cota 
938357af9beSAlex Bennée     pinsn = qemu_plugin_tb_insn_get(ptb, db->pc_next);
93938b47b19SEmilio G. Cota     tcg_ctx->plugin_insn = pinsn;
94038b47b19SEmilio G. Cota     plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN);
94138b47b19SEmilio G. Cota 
94238b47b19SEmilio G. Cota     /*
94338b47b19SEmilio G. Cota      * Detect page crossing to get the new host address.
94438b47b19SEmilio G. Cota      * Note that we skip this when haddr1 == NULL, e.g. when we're
94538b47b19SEmilio G. Cota      * fetching instructions from a region not backed by RAM.
94638b47b19SEmilio G. Cota      */
947b21af662SRichard Henderson     if (ptb->haddr1 == NULL) {
948b21af662SRichard Henderson         pinsn->haddr = NULL;
949b21af662SRichard Henderson     } else if (is_same_page(db, db->pc_next)) {
95038b47b19SEmilio G. Cota         pinsn->haddr = ptb->haddr1 + pinsn->vaddr - ptb->vaddr;
95138b47b19SEmilio G. Cota     } else {
952b21af662SRichard Henderson         if (ptb->vaddr2 == -1) {
953b21af662SRichard Henderson             ptb->vaddr2 = TARGET_PAGE_ALIGN(db->pc_first);
954b77af26eSRichard Henderson             get_page_addr_code_hostp(cpu_env(cpu), ptb->vaddr2, &ptb->haddr2);
955b21af662SRichard Henderson         }
95638b47b19SEmilio G. Cota         pinsn->haddr = ptb->haddr2 + pinsn->vaddr - ptb->vaddr2;
95738b47b19SEmilio G. Cota     }
95838b47b19SEmilio G. Cota }
95938b47b19SEmilio G. Cota 
96038b47b19SEmilio G. Cota void plugin_gen_insn_end(void)
96138b47b19SEmilio G. Cota {
96238b47b19SEmilio G. Cota     plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN);
96338b47b19SEmilio G. Cota }
96438b47b19SEmilio G. Cota 
9656f15c076SAlex Bennée /*
9666f15c076SAlex Bennée  * There are cases where we never get to finalise a translation - for
9676f15c076SAlex Bennée  * example a page fault during translation. As a result we shouldn't
9686f15c076SAlex Bennée  * do any clean-up here and make sure things are reset in
9696f15c076SAlex Bennée  * plugin_gen_tb_start.
9706f15c076SAlex Bennée  */
971a392277dSMatt Borgerson void plugin_gen_tb_end(CPUState *cpu, size_t num_insns)
97238b47b19SEmilio G. Cota {
97338b47b19SEmilio G. Cota     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
97438b47b19SEmilio G. Cota 
975a392277dSMatt Borgerson     /* translator may have removed instructions, update final count */
976a392277dSMatt Borgerson     g_assert(num_insns <= ptb->n);
977a392277dSMatt Borgerson     ptb->n = num_insns;
978a392277dSMatt Borgerson 
97938b47b19SEmilio G. Cota     /* collect instrumentation requests */
98038b47b19SEmilio G. Cota     qemu_plugin_tb_trans_cb(cpu, ptb);
98138b47b19SEmilio G. Cota 
98238b47b19SEmilio G. Cota     /* inject the instrumentation at the appropriate places */
98338b47b19SEmilio G. Cota     plugin_gen_inject(ptb);
98438b47b19SEmilio G. Cota }
985