xref: /qemu/accel/tcg/plugin-gen.c (revision 8a2927f290fb6c3ba51dfd6465e4ea51a3c9e1a0)
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_AFTER_INSN,
7174bb8accSRichard Henderson     PLUGIN_GEN_AFTER_TB,
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 
1009a3ee366SRichard Henderson static void plugin_gen_empty_callback(enum plugin_gen_from from)
10138b47b19SEmilio G. Cota {
10238b47b19SEmilio G. Cota     switch (from) {
10338b47b19SEmilio G. Cota     case PLUGIN_GEN_AFTER_INSN:
10421a3f62fSRichard Henderson     case PLUGIN_GEN_FROM_TB:
10538b47b19SEmilio G. Cota     case PLUGIN_GEN_FROM_INSN:
106ac977170SRichard Henderson         tcg_gen_plugin_cb(from);
10738b47b19SEmilio G. Cota         break;
10838b47b19SEmilio G. Cota     default:
10938b47b19SEmilio G. Cota         g_assert_not_reached();
11038b47b19SEmilio G. Cota     }
11138b47b19SEmilio G. Cota }
11238b47b19SEmilio G. Cota 
11338b47b19SEmilio G. Cota /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */
11438b47b19SEmilio G. Cota void plugin_gen_disable_mem_helpers(void)
11538b47b19SEmilio G. Cota {
11674bb8accSRichard Henderson     if (tcg_ctx->plugin_insn) {
11774bb8accSRichard Henderson         tcg_gen_plugin_cb(PLUGIN_GEN_AFTER_TB);
11838b47b19SEmilio G. Cota     }
11938b47b19SEmilio G. Cota }
12038b47b19SEmilio G. Cota 
121ac977170SRichard Henderson static void gen_enable_mem_helper(struct qemu_plugin_tb *ptb,
122ac977170SRichard Henderson                                   struct qemu_plugin_insn *insn)
12338b47b19SEmilio G. Cota {
124ac977170SRichard Henderson     GArray *cbs[2];
125ac977170SRichard Henderson     GArray *arr;
126ac977170SRichard Henderson     size_t n_cbs;
127ac977170SRichard Henderson 
128ac977170SRichard Henderson     /*
129ac977170SRichard Henderson      * Tracking memory accesses performed from helpers requires extra work.
130ac977170SRichard Henderson      * If an instruction is emulated with helpers, we do two things:
131ac977170SRichard Henderson      * (1) copy the CB descriptors, and keep track of it so that they can be
132ac977170SRichard Henderson      * freed later on, and (2) point CPUState.plugin_mem_cbs to the
133ac977170SRichard Henderson      * descriptors, so that we can read them at run-time
134ac977170SRichard Henderson      * (i.e. when the helper executes).
135ac977170SRichard Henderson      * This run-time access is performed from qemu_plugin_vcpu_mem_cb.
136ac977170SRichard Henderson      *
137ac977170SRichard Henderson      * Note that plugin_gen_disable_mem_helpers undoes (2). Since it
138ac977170SRichard Henderson      * is possible that the code we generate after the instruction is
139ac977170SRichard Henderson      * dead, we also add checks before generating tb_exit etc.
140ac977170SRichard Henderson      */
141ac977170SRichard Henderson     if (!insn->calls_helpers) {
142ac977170SRichard Henderson         return;
143ac977170SRichard Henderson     }
144ac977170SRichard Henderson 
145ac977170SRichard Henderson     cbs[0] = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR];
146ac977170SRichard Henderson     cbs[1] = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE];
147ac977170SRichard Henderson     n_cbs = cbs[0]->len + cbs[1]->len;
148ac977170SRichard Henderson 
149ac977170SRichard Henderson     if (n_cbs == 0) {
150ac977170SRichard Henderson         insn->mem_helper = false;
151ac977170SRichard Henderson         return;
152ac977170SRichard Henderson     }
153ac977170SRichard Henderson     insn->mem_helper = true;
154ac977170SRichard Henderson     ptb->mem_helper = true;
155ac977170SRichard Henderson 
156ac977170SRichard Henderson     arr = g_array_sized_new(false, false,
157ac977170SRichard Henderson                             sizeof(struct qemu_plugin_dyn_cb), n_cbs);
158ac977170SRichard Henderson     g_array_append_vals(arr, cbs[0]->data, cbs[0]->len);
159ac977170SRichard Henderson     g_array_append_vals(arr, cbs[1]->data, cbs[1]->len);
160ac977170SRichard Henderson 
161ac977170SRichard Henderson     qemu_plugin_add_dyn_cb_arr(arr);
162ac977170SRichard Henderson 
163ac977170SRichard Henderson     tcg_gen_st_ptr(tcg_constant_ptr((intptr_t)arr), tcg_env,
164ac977170SRichard Henderson                    offsetof(CPUState, plugin_mem_cbs) -
165ac977170SRichard Henderson                    offsetof(ArchCPU, env));
16638b47b19SEmilio G. Cota }
16738b47b19SEmilio G. Cota 
16874bb8accSRichard Henderson static void gen_disable_mem_helper(void)
16938b47b19SEmilio G. Cota {
170a0948bb7SRichard Henderson     tcg_gen_st_ptr(tcg_constant_ptr(0), tcg_env,
171a0948bb7SRichard Henderson                    offsetof(CPUState, plugin_mem_cbs) -
172a0948bb7SRichard Henderson                    offsetof(ArchCPU, env));
173a0948bb7SRichard Henderson }
17438b47b19SEmilio G. Cota 
17521a3f62fSRichard Henderson static void gen_udata_cb(struct qemu_plugin_dyn_cb *cb)
17621a3f62fSRichard Henderson {
17721a3f62fSRichard Henderson     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
17821a3f62fSRichard Henderson 
17921a3f62fSRichard Henderson     tcg_gen_ld_i32(cpu_index, tcg_env,
18021a3f62fSRichard Henderson                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
18121a3f62fSRichard Henderson     tcg_gen_call2(cb->regular.f.vcpu_udata, cb->regular.info, NULL,
18221a3f62fSRichard Henderson                   tcgv_i32_temp(cpu_index),
18321a3f62fSRichard Henderson                   tcgv_ptr_temp(tcg_constant_ptr(cb->userp)));
18421a3f62fSRichard Henderson     tcg_temp_free_i32(cpu_index);
18521a3f62fSRichard Henderson }
18621a3f62fSRichard Henderson 
18721a3f62fSRichard Henderson static void gen_inline_cb(struct qemu_plugin_dyn_cb *cb)
18821a3f62fSRichard Henderson {
18921a3f62fSRichard Henderson     GArray *arr = cb->inline_insn.entry.score->data;
19021a3f62fSRichard Henderson     size_t offset = cb->inline_insn.entry.offset;
19121a3f62fSRichard Henderson     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
19221a3f62fSRichard Henderson     TCGv_i64 val = tcg_temp_ebb_new_i64();
19321a3f62fSRichard Henderson     TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
19421a3f62fSRichard Henderson 
19521a3f62fSRichard Henderson     tcg_gen_ld_i32(cpu_index, tcg_env,
19621a3f62fSRichard Henderson                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
19721a3f62fSRichard Henderson     tcg_gen_muli_i32(cpu_index, cpu_index, g_array_get_element_size(arr));
19821a3f62fSRichard Henderson     tcg_gen_ext_i32_ptr(ptr, cpu_index);
19921a3f62fSRichard Henderson     tcg_temp_free_i32(cpu_index);
20021a3f62fSRichard Henderson 
20121a3f62fSRichard Henderson     tcg_gen_addi_ptr(ptr, ptr, (intptr_t)arr->data);
20221a3f62fSRichard Henderson     tcg_gen_ld_i64(val, ptr, offset);
20321a3f62fSRichard Henderson     tcg_gen_addi_i64(val, val, cb->inline_insn.imm);
20421a3f62fSRichard Henderson     tcg_gen_st_i64(val, ptr, offset);
20521a3f62fSRichard Henderson 
20621a3f62fSRichard Henderson     tcg_temp_free_i64(val);
20721a3f62fSRichard Henderson     tcg_temp_free_ptr(ptr);
20821a3f62fSRichard Henderson }
20921a3f62fSRichard Henderson 
210*8a2927f2SRichard Henderson static void gen_mem_cb(struct qemu_plugin_dyn_cb *cb,
211*8a2927f2SRichard Henderson                        qemu_plugin_meminfo_t meminfo, TCGv_i64 addr)
212*8a2927f2SRichard Henderson {
213*8a2927f2SRichard Henderson     TCGv_i32 cpu_index = tcg_temp_ebb_new_i32();
214*8a2927f2SRichard Henderson 
215*8a2927f2SRichard Henderson     tcg_gen_ld_i32(cpu_index, tcg_env,
216*8a2927f2SRichard Henderson                    -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index));
217*8a2927f2SRichard Henderson     tcg_gen_call4(cb->regular.f.vcpu_mem, cb->regular.info, NULL,
218*8a2927f2SRichard Henderson                   tcgv_i32_temp(cpu_index),
219*8a2927f2SRichard Henderson                   tcgv_i32_temp(tcg_constant_i32(meminfo)),
220*8a2927f2SRichard Henderson                   tcgv_i64_temp(addr),
221*8a2927f2SRichard Henderson                   tcgv_ptr_temp(tcg_constant_ptr(cb->userp)));
222*8a2927f2SRichard Henderson     tcg_temp_free_i32(cpu_index);
223*8a2927f2SRichard Henderson }
224*8a2927f2SRichard Henderson 
22538b47b19SEmilio G. Cota /* #define DEBUG_PLUGIN_GEN_OPS */
22638b47b19SEmilio G. Cota static void pr_ops(void)
22738b47b19SEmilio G. Cota {
22838b47b19SEmilio G. Cota #ifdef DEBUG_PLUGIN_GEN_OPS
22938b47b19SEmilio G. Cota     TCGOp *op;
23038b47b19SEmilio G. Cota     int i = 0;
23138b47b19SEmilio G. Cota 
23238b47b19SEmilio G. Cota     QTAILQ_FOREACH(op, &tcg_ctx->ops, link) {
23338b47b19SEmilio G. Cota         const char *name = "";
23438b47b19SEmilio G. Cota         const char *type = "";
23538b47b19SEmilio G. Cota 
23638b47b19SEmilio G. Cota         if (op->opc == INDEX_op_plugin_cb_start) {
23738b47b19SEmilio G. Cota             switch (op->args[0]) {
23838b47b19SEmilio G. Cota             case PLUGIN_GEN_FROM_TB:
23938b47b19SEmilio G. Cota                 name = "tb";
24038b47b19SEmilio G. Cota                 break;
24138b47b19SEmilio G. Cota             case PLUGIN_GEN_FROM_INSN:
24238b47b19SEmilio G. Cota                 name = "insn";
24338b47b19SEmilio G. Cota                 break;
24438b47b19SEmilio G. Cota             case PLUGIN_GEN_FROM_MEM:
24538b47b19SEmilio G. Cota                 name = "mem";
24638b47b19SEmilio G. Cota                 break;
24738b47b19SEmilio G. Cota             case PLUGIN_GEN_AFTER_INSN:
24838b47b19SEmilio G. Cota                 name = "after insn";
24938b47b19SEmilio G. Cota                 break;
25038b47b19SEmilio G. Cota             default:
25138b47b19SEmilio G. Cota                 break;
25238b47b19SEmilio G. Cota             }
25338b47b19SEmilio G. Cota             switch (op->args[1]) {
25438b47b19SEmilio G. Cota             case PLUGIN_GEN_CB_UDATA:
25538b47b19SEmilio G. Cota                 type = "udata";
25638b47b19SEmilio G. Cota                 break;
25738b47b19SEmilio G. Cota             case PLUGIN_GEN_CB_INLINE:
25838b47b19SEmilio G. Cota                 type = "inline";
25938b47b19SEmilio G. Cota                 break;
26038b47b19SEmilio G. Cota             case PLUGIN_GEN_CB_MEM:
26138b47b19SEmilio G. Cota                 type = "mem";
26238b47b19SEmilio G. Cota                 break;
26338b47b19SEmilio G. Cota             case PLUGIN_GEN_ENABLE_MEM_HELPER:
26438b47b19SEmilio G. Cota                 type = "enable mem helper";
26538b47b19SEmilio G. Cota                 break;
26638b47b19SEmilio G. Cota             case PLUGIN_GEN_DISABLE_MEM_HELPER:
26738b47b19SEmilio G. Cota                 type = "disable mem helper";
26838b47b19SEmilio G. Cota                 break;
26938b47b19SEmilio G. Cota             default:
27038b47b19SEmilio G. Cota                 break;
27138b47b19SEmilio G. Cota             }
27238b47b19SEmilio G. Cota         }
27338b47b19SEmilio G. Cota         printf("op[%2i]: %s %s %s\n", i, tcg_op_defs[op->opc].name, name, type);
27438b47b19SEmilio G. Cota         i++;
27538b47b19SEmilio G. Cota     }
27638b47b19SEmilio G. Cota #endif
27738b47b19SEmilio G. Cota }
27838b47b19SEmilio G. Cota 
2793fd62e73SEmilio Cota static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb)
28038b47b19SEmilio G. Cota {
281a0948bb7SRichard Henderson     TCGOp *op, *next;
282453d50ceSAlex Bennée     int insn_idx = -1;
28338b47b19SEmilio G. Cota 
28438b47b19SEmilio G. Cota     pr_ops();
285453d50ceSAlex Bennée 
286a0948bb7SRichard Henderson     /*
287a0948bb7SRichard Henderson      * While injecting code, we cannot afford to reuse any ebb temps
288a0948bb7SRichard Henderson      * that might be live within the existing opcode stream.
289a0948bb7SRichard Henderson      * The simplest solution is to release them all and create new.
290a0948bb7SRichard Henderson      */
291a0948bb7SRichard Henderson     memset(tcg_ctx->free_temps, 0, sizeof(tcg_ctx->free_temps));
292a0948bb7SRichard Henderson 
293a0948bb7SRichard Henderson     QTAILQ_FOREACH_SAFE(op, &tcg_ctx->ops, link, next) {
294453d50ceSAlex Bennée         switch (op->opc) {
295453d50ceSAlex Bennée         case INDEX_op_insn_start:
296453d50ceSAlex Bennée             insn_idx++;
297453d50ceSAlex Bennée             break;
298a0948bb7SRichard Henderson 
299a0948bb7SRichard Henderson         case INDEX_op_plugin_cb:
300a0948bb7SRichard Henderson         {
301a0948bb7SRichard Henderson             enum plugin_gen_from from = op->args[0];
302a0948bb7SRichard Henderson             struct qemu_plugin_insn *insn = NULL;
30321a3f62fSRichard Henderson             const GArray *cbs;
30421a3f62fSRichard Henderson             int i, n;
305a0948bb7SRichard Henderson 
306a0948bb7SRichard Henderson             if (insn_idx >= 0) {
307a0948bb7SRichard Henderson                 insn = g_ptr_array_index(plugin_tb->insns, insn_idx);
308a0948bb7SRichard Henderson             }
309a0948bb7SRichard Henderson 
310a0948bb7SRichard Henderson             tcg_ctx->emit_before_op = op;
311a0948bb7SRichard Henderson 
312a0948bb7SRichard Henderson             switch (from) {
31374bb8accSRichard Henderson             case PLUGIN_GEN_AFTER_TB:
31474bb8accSRichard Henderson                 if (plugin_tb->mem_helper) {
31574bb8accSRichard Henderson                     gen_disable_mem_helper();
31674bb8accSRichard Henderson                 }
31774bb8accSRichard Henderson                 break;
31874bb8accSRichard Henderson 
319a0948bb7SRichard Henderson             case PLUGIN_GEN_AFTER_INSN:
320a0948bb7SRichard Henderson                 assert(insn != NULL);
32174bb8accSRichard Henderson                 if (insn->mem_helper) {
32274bb8accSRichard Henderson                     gen_disable_mem_helper();
32374bb8accSRichard Henderson                 }
324a0948bb7SRichard Henderson                 break;
32521a3f62fSRichard Henderson 
32621a3f62fSRichard Henderson             case PLUGIN_GEN_FROM_TB:
32721a3f62fSRichard Henderson                 assert(insn == NULL);
32821a3f62fSRichard Henderson 
32921a3f62fSRichard Henderson                 cbs = plugin_tb->cbs[PLUGIN_CB_REGULAR];
33021a3f62fSRichard Henderson                 for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
33121a3f62fSRichard Henderson                     struct qemu_plugin_dyn_cb *cb =
33221a3f62fSRichard Henderson                         &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
33321a3f62fSRichard Henderson                     gen_udata_cb(cb);
33421a3f62fSRichard Henderson                 }
33521a3f62fSRichard Henderson 
33621a3f62fSRichard Henderson                 cbs = plugin_tb->cbs[PLUGIN_CB_INLINE];
33721a3f62fSRichard Henderson                 for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
33821a3f62fSRichard Henderson                     struct qemu_plugin_dyn_cb *cb =
33921a3f62fSRichard Henderson                         &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
34021a3f62fSRichard Henderson                     gen_inline_cb(cb);
34121a3f62fSRichard Henderson                 }
34221a3f62fSRichard Henderson                 break;
34321a3f62fSRichard Henderson 
344ac977170SRichard Henderson             case PLUGIN_GEN_FROM_INSN:
345ac977170SRichard Henderson                 assert(insn != NULL);
346ac977170SRichard Henderson 
347ac977170SRichard Henderson                 gen_enable_mem_helper(plugin_tb, insn);
348ac977170SRichard Henderson 
349ac977170SRichard Henderson                 cbs = insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR];
350ac977170SRichard Henderson                 for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
351ac977170SRichard Henderson                     struct qemu_plugin_dyn_cb *cb =
352ac977170SRichard Henderson                         &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
353ac977170SRichard Henderson                     gen_udata_cb(cb);
354ac977170SRichard Henderson                 }
355ac977170SRichard Henderson 
356ac977170SRichard Henderson                 cbs = insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE];
357ac977170SRichard Henderson                 for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
358ac977170SRichard Henderson                     struct qemu_plugin_dyn_cb *cb =
359ac977170SRichard Henderson                         &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
360ac977170SRichard Henderson                     gen_inline_cb(cb);
361ac977170SRichard Henderson                 }
362ac977170SRichard Henderson                 break;
363ac977170SRichard Henderson 
364a0948bb7SRichard Henderson             default:
365a0948bb7SRichard Henderson                 g_assert_not_reached();
366a0948bb7SRichard Henderson             }
367a0948bb7SRichard Henderson 
368a0948bb7SRichard Henderson             tcg_ctx->emit_before_op = NULL;
369a0948bb7SRichard Henderson             tcg_op_remove(tcg_ctx, op);
370a0948bb7SRichard Henderson             break;
371a0948bb7SRichard Henderson         }
372a0948bb7SRichard Henderson 
373*8a2927f2SRichard Henderson         case INDEX_op_plugin_mem_cb:
374453d50ceSAlex Bennée         {
375*8a2927f2SRichard Henderson             TCGv_i64 addr = temp_tcgv_i64(arg_temp(op->args[0]));
376*8a2927f2SRichard Henderson             qemu_plugin_meminfo_t meminfo = op->args[1];
377*8a2927f2SRichard Henderson             struct qemu_plugin_insn *insn;
378*8a2927f2SRichard Henderson             const GArray *cbs;
379*8a2927f2SRichard Henderson             int i, n, rw;
38038b47b19SEmilio G. Cota 
381*8a2927f2SRichard Henderson             assert(insn_idx >= 0);
382*8a2927f2SRichard Henderson             insn = g_ptr_array_index(plugin_tb->insns, insn_idx);
383*8a2927f2SRichard Henderson             rw = qemu_plugin_mem_is_store(meminfo) ? 2 : 1;
384453d50ceSAlex Bennée 
385*8a2927f2SRichard Henderson             tcg_ctx->emit_before_op = op;
386*8a2927f2SRichard Henderson 
387*8a2927f2SRichard Henderson             cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR];
388*8a2927f2SRichard Henderson             for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
389*8a2927f2SRichard Henderson                 struct qemu_plugin_dyn_cb *cb =
390*8a2927f2SRichard Henderson                     &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
391*8a2927f2SRichard Henderson                 if (cb->rw & rw) {
392*8a2927f2SRichard Henderson                     gen_mem_cb(cb, meminfo, addr);
393*8a2927f2SRichard Henderson                 }
394453d50ceSAlex Bennée             }
395453d50ceSAlex Bennée 
396*8a2927f2SRichard Henderson             cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE];
397*8a2927f2SRichard Henderson             for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) {
398*8a2927f2SRichard Henderson                 struct qemu_plugin_dyn_cb *cb =
399*8a2927f2SRichard Henderson                     &g_array_index(cbs, struct qemu_plugin_dyn_cb, i);
400*8a2927f2SRichard Henderson                 if (cb->rw & rw) {
401*8a2927f2SRichard Henderson                     gen_inline_cb(cb);
402*8a2927f2SRichard Henderson                 }
403*8a2927f2SRichard Henderson             }
404*8a2927f2SRichard Henderson 
405*8a2927f2SRichard Henderson             tcg_ctx->emit_before_op = NULL;
406*8a2927f2SRichard Henderson             tcg_op_remove(tcg_ctx, op);
407453d50ceSAlex Bennée             break;
408453d50ceSAlex Bennée         }
409*8a2927f2SRichard Henderson 
410453d50ceSAlex Bennée         default:
411453d50ceSAlex Bennée             /* plugins don't care about any other ops */
412453d50ceSAlex Bennée             break;
413453d50ceSAlex Bennée         }
41438b47b19SEmilio G. Cota     }
41538b47b19SEmilio G. Cota     pr_ops();
41638b47b19SEmilio G. Cota }
41738b47b19SEmilio G. Cota 
418b21af662SRichard Henderson bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db,
419b21af662SRichard Henderson                          bool mem_only)
42038b47b19SEmilio G. Cota {
42138b47b19SEmilio G. Cota     bool ret = false;
42238b47b19SEmilio G. Cota 
423c0061471SAlex Bennée     if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_state->event_mask)) {
4246f15c076SAlex Bennée         struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
4256f15c076SAlex Bennée         int i;
4266f15c076SAlex Bennée 
4276f15c076SAlex Bennée         /* reset callbacks */
4286f15c076SAlex Bennée         for (i = 0; i < PLUGIN_N_CB_SUBTYPES; i++) {
4296f15c076SAlex Bennée             if (ptb->cbs[i]) {
4306f15c076SAlex Bennée                 g_array_set_size(ptb->cbs[i], 0);
4316f15c076SAlex Bennée             }
4326f15c076SAlex Bennée         }
4336f15c076SAlex Bennée         ptb->n = 0;
4346f15c076SAlex Bennée 
43538b47b19SEmilio G. Cota         ret = true;
43638b47b19SEmilio G. Cota 
437b21af662SRichard Henderson         ptb->vaddr = db->pc_first;
43838b47b19SEmilio G. Cota         ptb->vaddr2 = -1;
439b21af662SRichard Henderson         ptb->haddr1 = db->host_addr[0];
44038b47b19SEmilio G. Cota         ptb->haddr2 = NULL;
441cfd405eaSAlex Bennée         ptb->mem_only = mem_only;
4423fd62e73SEmilio Cota         ptb->mem_helper = false;
44338b47b19SEmilio G. Cota 
44438b47b19SEmilio G. Cota         plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB);
44538b47b19SEmilio G. Cota     }
4466f15c076SAlex Bennée 
4476f15c076SAlex Bennée     tcg_ctx->plugin_insn = NULL;
4486f15c076SAlex Bennée 
44938b47b19SEmilio G. Cota     return ret;
45038b47b19SEmilio G. Cota }
45138b47b19SEmilio G. Cota 
45238b47b19SEmilio G. Cota void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db)
45338b47b19SEmilio G. Cota {
45438b47b19SEmilio G. Cota     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
45538b47b19SEmilio G. Cota     struct qemu_plugin_insn *pinsn;
45638b47b19SEmilio G. Cota 
457357af9beSAlex Bennée     pinsn = qemu_plugin_tb_insn_get(ptb, db->pc_next);
45838b47b19SEmilio G. Cota     tcg_ctx->plugin_insn = pinsn;
45938b47b19SEmilio G. Cota     plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN);
46038b47b19SEmilio G. Cota 
46138b47b19SEmilio G. Cota     /*
46238b47b19SEmilio G. Cota      * Detect page crossing to get the new host address.
46338b47b19SEmilio G. Cota      * Note that we skip this when haddr1 == NULL, e.g. when we're
46438b47b19SEmilio G. Cota      * fetching instructions from a region not backed by RAM.
46538b47b19SEmilio G. Cota      */
466b21af662SRichard Henderson     if (ptb->haddr1 == NULL) {
467b21af662SRichard Henderson         pinsn->haddr = NULL;
468b21af662SRichard Henderson     } else if (is_same_page(db, db->pc_next)) {
46938b47b19SEmilio G. Cota         pinsn->haddr = ptb->haddr1 + pinsn->vaddr - ptb->vaddr;
47038b47b19SEmilio G. Cota     } else {
471b21af662SRichard Henderson         if (ptb->vaddr2 == -1) {
472b21af662SRichard Henderson             ptb->vaddr2 = TARGET_PAGE_ALIGN(db->pc_first);
473b77af26eSRichard Henderson             get_page_addr_code_hostp(cpu_env(cpu), ptb->vaddr2, &ptb->haddr2);
474b21af662SRichard Henderson         }
47538b47b19SEmilio G. Cota         pinsn->haddr = ptb->haddr2 + pinsn->vaddr - ptb->vaddr2;
47638b47b19SEmilio G. Cota     }
47738b47b19SEmilio G. Cota }
47838b47b19SEmilio G. Cota 
47938b47b19SEmilio G. Cota void plugin_gen_insn_end(void)
48038b47b19SEmilio G. Cota {
48138b47b19SEmilio G. Cota     plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN);
48238b47b19SEmilio G. Cota }
48338b47b19SEmilio G. Cota 
4846f15c076SAlex Bennée /*
4856f15c076SAlex Bennée  * There are cases where we never get to finalise a translation - for
4866f15c076SAlex Bennée  * example a page fault during translation. As a result we shouldn't
4876f15c076SAlex Bennée  * do any clean-up here and make sure things are reset in
4886f15c076SAlex Bennée  * plugin_gen_tb_start.
4896f15c076SAlex Bennée  */
490a392277dSMatt Borgerson void plugin_gen_tb_end(CPUState *cpu, size_t num_insns)
49138b47b19SEmilio G. Cota {
49238b47b19SEmilio G. Cota     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
49338b47b19SEmilio G. Cota 
494a392277dSMatt Borgerson     /* translator may have removed instructions, update final count */
495a392277dSMatt Borgerson     g_assert(num_insns <= ptb->n);
496a392277dSMatt Borgerson     ptb->n = num_insns;
497a392277dSMatt Borgerson 
49838b47b19SEmilio G. Cota     /* collect instrumentation requests */
49938b47b19SEmilio G. Cota     qemu_plugin_tb_trans_cb(cpu, ptb);
50038b47b19SEmilio G. Cota 
50138b47b19SEmilio G. Cota     /* inject the instrumentation at the appropriate places */
50238b47b19SEmilio G. Cota     plugin_gen_inject(ptb);
50338b47b19SEmilio G. Cota }
504