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 #ifdef CONFIG_SOFTMMU 6138b47b19SEmilio G. Cota # define CONFIG_SOFTMMU_GATE 1 6238b47b19SEmilio G. Cota #else 6338b47b19SEmilio G. Cota # define CONFIG_SOFTMMU_GATE 0 6438b47b19SEmilio G. Cota #endif 6538b47b19SEmilio G. Cota 6638b47b19SEmilio G. Cota /* 6738b47b19SEmilio G. Cota * plugin_cb_start TCG op args[]: 6838b47b19SEmilio G. Cota * 0: enum plugin_gen_from 6938b47b19SEmilio G. Cota * 1: enum plugin_gen_cb 7038b47b19SEmilio G. Cota * 2: set to 1 for mem callback that is a write, 0 otherwise. 7138b47b19SEmilio G. Cota */ 7238b47b19SEmilio G. Cota 7338b47b19SEmilio G. Cota enum plugin_gen_from { 7438b47b19SEmilio G. Cota PLUGIN_GEN_FROM_TB, 7538b47b19SEmilio G. Cota PLUGIN_GEN_FROM_INSN, 7638b47b19SEmilio G. Cota PLUGIN_GEN_FROM_MEM, 7738b47b19SEmilio G. Cota PLUGIN_GEN_AFTER_INSN, 7838b47b19SEmilio G. Cota PLUGIN_GEN_N_FROMS, 7938b47b19SEmilio G. Cota }; 8038b47b19SEmilio G. Cota 8138b47b19SEmilio G. Cota enum plugin_gen_cb { 8238b47b19SEmilio G. Cota PLUGIN_GEN_CB_UDATA, 8333a277feSAkihiko Odaki PLUGIN_GEN_CB_UDATA_R, 8438b47b19SEmilio G. Cota PLUGIN_GEN_CB_INLINE, 8538b47b19SEmilio G. Cota PLUGIN_GEN_CB_MEM, 8638b47b19SEmilio G. Cota PLUGIN_GEN_ENABLE_MEM_HELPER, 8738b47b19SEmilio G. Cota PLUGIN_GEN_DISABLE_MEM_HELPER, 8838b47b19SEmilio G. Cota PLUGIN_GEN_N_CBS, 8938b47b19SEmilio G. Cota }; 9038b47b19SEmilio G. Cota 9138b47b19SEmilio G. Cota /* 9238b47b19SEmilio G. Cota * These helpers are stubs that get dynamically switched out for calls 9338b47b19SEmilio G. Cota * direct to the plugin if they are subscribed to. 9438b47b19SEmilio G. Cota */ 9533a277feSAkihiko Odaki void HELPER(plugin_vcpu_udata_cb_no_wg)(uint32_t cpu_index, void *udata) 9633a277feSAkihiko Odaki { } 9733a277feSAkihiko Odaki 9833a277feSAkihiko Odaki void HELPER(plugin_vcpu_udata_cb_no_rwg)(uint32_t cpu_index, void *udata) 9938b47b19SEmilio G. Cota { } 10038b47b19SEmilio G. Cota 10138b47b19SEmilio G. Cota void HELPER(plugin_vcpu_mem_cb)(unsigned int vcpu_index, 10238b47b19SEmilio G. Cota qemu_plugin_meminfo_t info, uint64_t vaddr, 10338b47b19SEmilio G. Cota void *userdata) 10438b47b19SEmilio G. Cota { } 10538b47b19SEmilio G. Cota 10633a277feSAkihiko Odaki static void gen_empty_udata_cb(void (*gen_helper)(TCGv_i32, TCGv_ptr)) 10738b47b19SEmilio G. Cota { 108713f263aSRichard Henderson TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 109713f263aSRichard Henderson TCGv_ptr udata = tcg_temp_ebb_new_ptr(); 11038b47b19SEmilio G. Cota 111713f263aSRichard Henderson tcg_gen_movi_ptr(udata, 0); 112ad75a51eSRichard Henderson tcg_gen_ld_i32(cpu_index, tcg_env, 11338b47b19SEmilio G. Cota -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 11433a277feSAkihiko Odaki gen_helper(cpu_index, udata); 11538b47b19SEmilio G. Cota 11638b47b19SEmilio G. Cota tcg_temp_free_ptr(udata); 11738b47b19SEmilio G. Cota tcg_temp_free_i32(cpu_index); 11838b47b19SEmilio G. Cota } 11938b47b19SEmilio G. Cota 12033a277feSAkihiko Odaki static void gen_empty_udata_cb_no_wg(void) 12133a277feSAkihiko Odaki { 12233a277feSAkihiko Odaki gen_empty_udata_cb(gen_helper_plugin_vcpu_udata_cb_no_wg); 12333a277feSAkihiko Odaki } 12433a277feSAkihiko Odaki 12533a277feSAkihiko Odaki static void gen_empty_udata_cb_no_rwg(void) 12633a277feSAkihiko Odaki { 12733a277feSAkihiko Odaki gen_empty_udata_cb(gen_helper_plugin_vcpu_udata_cb_no_rwg); 12833a277feSAkihiko Odaki } 12933a277feSAkihiko Odaki 13038b47b19SEmilio G. Cota /* 13138b47b19SEmilio G. Cota * For now we only support addi_i64. 13238b47b19SEmilio G. Cota * When we support more ops, we can generate one empty inline cb for each. 13338b47b19SEmilio G. Cota */ 13438b47b19SEmilio G. Cota static void gen_empty_inline_cb(void) 13538b47b19SEmilio G. Cota { 136*62f92b8dSPierrick Bouvier TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 137*62f92b8dSPierrick Bouvier TCGv_ptr cpu_index_as_ptr = tcg_temp_ebb_new_ptr(); 138713f263aSRichard Henderson TCGv_i64 val = tcg_temp_ebb_new_i64(); 139713f263aSRichard Henderson TCGv_ptr ptr = tcg_temp_ebb_new_ptr(); 14038b47b19SEmilio G. Cota 141*62f92b8dSPierrick Bouvier tcg_gen_ld_i32(cpu_index, tcg_env, 142*62f92b8dSPierrick Bouvier -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 143*62f92b8dSPierrick Bouvier /* second operand will be replaced by immediate value */ 144*62f92b8dSPierrick Bouvier tcg_gen_mul_i32(cpu_index, cpu_index, cpu_index); 145*62f92b8dSPierrick Bouvier tcg_gen_ext_i32_ptr(cpu_index_as_ptr, cpu_index); 146*62f92b8dSPierrick Bouvier 147713f263aSRichard Henderson tcg_gen_movi_ptr(ptr, 0); 148*62f92b8dSPierrick Bouvier tcg_gen_add_ptr(ptr, ptr, cpu_index_as_ptr); 14938b47b19SEmilio G. Cota tcg_gen_ld_i64(val, ptr, 0); 150*62f92b8dSPierrick Bouvier /* second operand will be replaced by immediate value */ 151*62f92b8dSPierrick Bouvier tcg_gen_add_i64(val, val, val); 152*62f92b8dSPierrick Bouvier 15338b47b19SEmilio G. Cota tcg_gen_st_i64(val, ptr, 0); 15438b47b19SEmilio G. Cota tcg_temp_free_ptr(ptr); 15538b47b19SEmilio G. Cota tcg_temp_free_i64(val); 156*62f92b8dSPierrick Bouvier tcg_temp_free_ptr(cpu_index_as_ptr); 157*62f92b8dSPierrick Bouvier tcg_temp_free_i32(cpu_index); 15838b47b19SEmilio G. Cota } 15938b47b19SEmilio G. Cota 160fcdab382SRichard Henderson static void gen_empty_mem_cb(TCGv_i64 addr, uint32_t info) 16138b47b19SEmilio G. Cota { 162b6d91645SRichard Henderson TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 163b6d91645SRichard Henderson TCGv_i32 meminfo = tcg_temp_ebb_new_i32(); 164b6d91645SRichard Henderson TCGv_ptr udata = tcg_temp_ebb_new_ptr(); 165b6d91645SRichard Henderson 166b6d91645SRichard Henderson tcg_gen_movi_i32(meminfo, info); 167b6d91645SRichard Henderson tcg_gen_movi_ptr(udata, 0); 168ad75a51eSRichard Henderson tcg_gen_ld_i32(cpu_index, tcg_env, 169b6d91645SRichard Henderson -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 170b6d91645SRichard Henderson 171fcdab382SRichard Henderson gen_helper_plugin_vcpu_mem_cb(cpu_index, meminfo, addr, udata); 172b6d91645SRichard Henderson 173b6d91645SRichard Henderson tcg_temp_free_ptr(udata); 174b6d91645SRichard Henderson tcg_temp_free_i32(meminfo); 175b6d91645SRichard Henderson tcg_temp_free_i32(cpu_index); 17638b47b19SEmilio G. Cota } 17738b47b19SEmilio G. Cota 17838b47b19SEmilio G. Cota /* 17938b47b19SEmilio G. Cota * Share the same function for enable/disable. When enabling, the NULL 18038b47b19SEmilio G. Cota * pointer will be overwritten later. 18138b47b19SEmilio G. Cota */ 18238b47b19SEmilio G. Cota static void gen_empty_mem_helper(void) 18338b47b19SEmilio G. Cota { 184713f263aSRichard Henderson TCGv_ptr ptr = tcg_temp_ebb_new_ptr(); 18538b47b19SEmilio G. Cota 186713f263aSRichard Henderson tcg_gen_movi_ptr(ptr, 0); 187ad75a51eSRichard Henderson tcg_gen_st_ptr(ptr, tcg_env, offsetof(CPUState, plugin_mem_cbs) - 18838b47b19SEmilio G. Cota offsetof(ArchCPU, env)); 18938b47b19SEmilio G. Cota tcg_temp_free_ptr(ptr); 19038b47b19SEmilio G. Cota } 19138b47b19SEmilio G. Cota 1929a3ee366SRichard Henderson static void gen_plugin_cb_start(enum plugin_gen_from from, 19338b47b19SEmilio G. Cota enum plugin_gen_cb type, unsigned wr) 19438b47b19SEmilio G. Cota { 19538b47b19SEmilio G. Cota tcg_gen_plugin_cb_start(from, type, wr); 19638b47b19SEmilio G. Cota } 19738b47b19SEmilio G. Cota 19838b47b19SEmilio G. Cota static void gen_wrapped(enum plugin_gen_from from, 19938b47b19SEmilio G. Cota enum plugin_gen_cb type, void (*func)(void)) 20038b47b19SEmilio G. Cota { 20138b47b19SEmilio G. Cota gen_plugin_cb_start(from, type, 0); 20238b47b19SEmilio G. Cota func(); 20338b47b19SEmilio G. Cota tcg_gen_plugin_cb_end(); 20438b47b19SEmilio G. Cota } 20538b47b19SEmilio G. Cota 2069a3ee366SRichard Henderson static void plugin_gen_empty_callback(enum plugin_gen_from from) 20738b47b19SEmilio G. Cota { 20838b47b19SEmilio G. Cota switch (from) { 20938b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 21038b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_DISABLE_MEM_HELPER, 21138b47b19SEmilio G. Cota gen_empty_mem_helper); 21238b47b19SEmilio G. Cota break; 21338b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 21438b47b19SEmilio G. Cota /* 21538b47b19SEmilio G. Cota * Note: plugin_gen_inject() relies on ENABLE_MEM_HELPER being 21638b47b19SEmilio G. Cota * the first callback of an instruction 21738b47b19SEmilio G. Cota */ 21838b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_ENABLE_MEM_HELPER, 21938b47b19SEmilio G. Cota gen_empty_mem_helper); 22038b47b19SEmilio G. Cota /* fall through */ 22138b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_TB: 22233a277feSAkihiko Odaki gen_wrapped(from, PLUGIN_GEN_CB_UDATA, gen_empty_udata_cb_no_rwg); 22333a277feSAkihiko Odaki gen_wrapped(from, PLUGIN_GEN_CB_UDATA_R, gen_empty_udata_cb_no_wg); 22438b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_CB_INLINE, gen_empty_inline_cb); 22538b47b19SEmilio G. Cota break; 22638b47b19SEmilio G. Cota default: 22738b47b19SEmilio G. Cota g_assert_not_reached(); 22838b47b19SEmilio G. Cota } 22938b47b19SEmilio G. Cota } 23038b47b19SEmilio G. Cota 231fcdab382SRichard Henderson void plugin_gen_empty_mem_callback(TCGv_i64 addr, uint32_t info) 23238b47b19SEmilio G. Cota { 23337aff087SRichard Henderson enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info); 23438b47b19SEmilio G. Cota 235f5c346acSRichard Henderson gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_MEM, rw); 236f5c346acSRichard Henderson gen_empty_mem_cb(addr, info); 23738b47b19SEmilio G. Cota tcg_gen_plugin_cb_end(); 23838b47b19SEmilio G. Cota 239f5c346acSRichard Henderson gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_INLINE, rw); 240f5c346acSRichard Henderson gen_empty_inline_cb(); 241f5c346acSRichard Henderson tcg_gen_plugin_cb_end(); 24238b47b19SEmilio G. Cota } 24338b47b19SEmilio G. Cota 24438b47b19SEmilio G. Cota static TCGOp *find_op(TCGOp *op, TCGOpcode opc) 24538b47b19SEmilio G. Cota { 24638b47b19SEmilio G. Cota while (op) { 24738b47b19SEmilio G. Cota if (op->opc == opc) { 24838b47b19SEmilio G. Cota return op; 24938b47b19SEmilio G. Cota } 25038b47b19SEmilio G. Cota op = QTAILQ_NEXT(op, link); 25138b47b19SEmilio G. Cota } 25238b47b19SEmilio G. Cota return NULL; 25338b47b19SEmilio G. Cota } 25438b47b19SEmilio G. Cota 25538b47b19SEmilio G. Cota static TCGOp *rm_ops_range(TCGOp *begin, TCGOp *end) 25638b47b19SEmilio G. Cota { 25738b47b19SEmilio G. Cota TCGOp *ret = QTAILQ_NEXT(end, link); 25838b47b19SEmilio G. Cota 25938b47b19SEmilio G. Cota QTAILQ_REMOVE_SEVERAL(&tcg_ctx->ops, begin, end, link); 26038b47b19SEmilio G. Cota return ret; 26138b47b19SEmilio G. Cota } 26238b47b19SEmilio G. Cota 26338b47b19SEmilio G. Cota /* remove all ops until (and including) plugin_cb_end */ 26438b47b19SEmilio G. Cota static TCGOp *rm_ops(TCGOp *op) 26538b47b19SEmilio G. Cota { 26638b47b19SEmilio G. Cota TCGOp *end_op = find_op(op, INDEX_op_plugin_cb_end); 26738b47b19SEmilio G. Cota 26838b47b19SEmilio G. Cota tcg_debug_assert(end_op); 26938b47b19SEmilio G. Cota return rm_ops_range(op, end_op); 27038b47b19SEmilio G. Cota } 27138b47b19SEmilio G. Cota 27238b47b19SEmilio G. Cota static TCGOp *copy_op_nocheck(TCGOp **begin_op, TCGOp *op) 27338b47b19SEmilio G. Cota { 274cb10bc63SRichard Henderson TCGOp *old_op = QTAILQ_NEXT(*begin_op, link); 275cb10bc63SRichard Henderson unsigned nargs = old_op->nargs; 276d4478943SPhilippe Mathieu-Daudé 277cb10bc63SRichard Henderson *begin_op = old_op; 278cb10bc63SRichard Henderson op = tcg_op_insert_after(tcg_ctx, op, old_op->opc, nargs); 279cb10bc63SRichard Henderson memcpy(op->args, old_op->args, sizeof(op->args[0]) * nargs); 280d4478943SPhilippe Mathieu-Daudé 28138b47b19SEmilio G. Cota return op; 28238b47b19SEmilio G. Cota } 28338b47b19SEmilio G. Cota 28438b47b19SEmilio G. Cota static TCGOp *copy_op(TCGOp **begin_op, TCGOp *op, TCGOpcode opc) 28538b47b19SEmilio G. Cota { 28638b47b19SEmilio G. Cota op = copy_op_nocheck(begin_op, op); 28738b47b19SEmilio G. Cota tcg_debug_assert((*begin_op)->opc == opc); 28838b47b19SEmilio G. Cota return op; 28938b47b19SEmilio G. Cota } 29038b47b19SEmilio G. Cota 29138b47b19SEmilio G. Cota static TCGOp *copy_const_ptr(TCGOp **begin_op, TCGOp *op, void *ptr) 29238b47b19SEmilio G. Cota { 29338b47b19SEmilio G. Cota if (UINTPTR_MAX == UINT32_MAX) { 29480c44bbaSRichard Henderson /* mov_i32 */ 29580c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i32); 29680c44bbaSRichard Henderson op->args[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr)); 29738b47b19SEmilio G. Cota } else { 29880c44bbaSRichard Henderson /* mov_i64 */ 29980c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i64); 30080c44bbaSRichard Henderson op->args[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr)); 30138b47b19SEmilio G. Cota } 30238b47b19SEmilio G. Cota return op; 30338b47b19SEmilio G. Cota } 30438b47b19SEmilio G. Cota 305*62f92b8dSPierrick Bouvier static TCGOp *copy_ld_i32(TCGOp **begin_op, TCGOp *op) 306*62f92b8dSPierrick Bouvier { 307*62f92b8dSPierrick Bouvier return copy_op(begin_op, op, INDEX_op_ld_i32); 308*62f92b8dSPierrick Bouvier } 309*62f92b8dSPierrick Bouvier 310*62f92b8dSPierrick Bouvier static TCGOp *copy_ext_i32_ptr(TCGOp **begin_op, TCGOp *op) 311*62f92b8dSPierrick Bouvier { 312*62f92b8dSPierrick Bouvier if (UINTPTR_MAX == UINT32_MAX) { 313*62f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_mov_i32); 314*62f92b8dSPierrick Bouvier } else { 315*62f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_ext_i32_i64); 316*62f92b8dSPierrick Bouvier } 317*62f92b8dSPierrick Bouvier return op; 318*62f92b8dSPierrick Bouvier } 319*62f92b8dSPierrick Bouvier 320*62f92b8dSPierrick Bouvier static TCGOp *copy_add_ptr(TCGOp **begin_op, TCGOp *op) 321*62f92b8dSPierrick Bouvier { 322*62f92b8dSPierrick Bouvier if (UINTPTR_MAX == UINT32_MAX) { 323*62f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_add_i32); 324*62f92b8dSPierrick Bouvier } else { 325*62f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_add_i64); 326*62f92b8dSPierrick Bouvier } 327*62f92b8dSPierrick Bouvier return op; 328*62f92b8dSPierrick Bouvier } 329*62f92b8dSPierrick Bouvier 33038b47b19SEmilio G. Cota static TCGOp *copy_ld_i64(TCGOp **begin_op, TCGOp *op) 33138b47b19SEmilio G. Cota { 33238b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 33338b47b19SEmilio G. Cota /* 2x ld_i32 */ 334*62f92b8dSPierrick Bouvier op = copy_ld_i32(begin_op, op); 335*62f92b8dSPierrick Bouvier op = copy_ld_i32(begin_op, op); 33638b47b19SEmilio G. Cota } else { 33738b47b19SEmilio G. Cota /* ld_i64 */ 33838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_ld_i64); 33938b47b19SEmilio G. Cota } 34038b47b19SEmilio G. Cota return op; 34138b47b19SEmilio G. Cota } 34238b47b19SEmilio G. Cota 34338b47b19SEmilio G. Cota static TCGOp *copy_st_i64(TCGOp **begin_op, TCGOp *op) 34438b47b19SEmilio G. Cota { 34538b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 34638b47b19SEmilio G. Cota /* 2x st_i32 */ 34738b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 34838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 34938b47b19SEmilio G. Cota } else { 35038b47b19SEmilio G. Cota /* st_i64 */ 35138b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i64); 35238b47b19SEmilio G. Cota } 35338b47b19SEmilio G. Cota return op; 35438b47b19SEmilio G. Cota } 35538b47b19SEmilio G. Cota 3560d6e6cb7SAlex Bennée static TCGOp *copy_add_i64(TCGOp **begin_op, TCGOp *op, uint64_t v) 35738b47b19SEmilio G. Cota { 35838b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 35938b47b19SEmilio G. Cota /* all 32-bit backends must implement add2_i32 */ 36038b47b19SEmilio G. Cota g_assert(TCG_TARGET_HAS_add2_i32); 36138b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_add2_i32); 3620d6e6cb7SAlex Bennée op->args[4] = tcgv_i32_arg(tcg_constant_i32(v)); 3630d6e6cb7SAlex Bennée op->args[5] = tcgv_i32_arg(tcg_constant_i32(v >> 32)); 36438b47b19SEmilio G. Cota } else { 36538b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_add_i64); 3660d6e6cb7SAlex Bennée op->args[2] = tcgv_i64_arg(tcg_constant_i64(v)); 36738b47b19SEmilio G. Cota } 36838b47b19SEmilio G. Cota return op; 36938b47b19SEmilio G. Cota } 37038b47b19SEmilio G. Cota 371*62f92b8dSPierrick Bouvier static TCGOp *copy_mul_i32(TCGOp **begin_op, TCGOp *op, uint32_t v) 372*62f92b8dSPierrick Bouvier { 373*62f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_mul_i32); 374*62f92b8dSPierrick Bouvier op->args[2] = tcgv_i32_arg(tcg_constant_i32(v)); 375*62f92b8dSPierrick Bouvier return op; 376*62f92b8dSPierrick Bouvier } 377*62f92b8dSPierrick Bouvier 37838b47b19SEmilio G. Cota static TCGOp *copy_st_ptr(TCGOp **begin_op, TCGOp *op) 37938b47b19SEmilio G. Cota { 38038b47b19SEmilio G. Cota if (UINTPTR_MAX == UINT32_MAX) { 38138b47b19SEmilio G. Cota /* st_i32 */ 38238b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 38338b47b19SEmilio G. Cota } else { 38438b47b19SEmilio G. Cota /* st_i64 */ 38538b47b19SEmilio G. Cota op = copy_st_i64(begin_op, op); 38638b47b19SEmilio G. Cota } 38738b47b19SEmilio G. Cota return op; 38838b47b19SEmilio G. Cota } 38938b47b19SEmilio G. Cota 390946bf79eSAkihiko Odaki static TCGOp *copy_call(TCGOp **begin_op, TCGOp *op, void *func, int *cb_idx) 39138b47b19SEmilio G. Cota { 39205d019abSRichard Henderson TCGOp *old_op; 39305d019abSRichard Henderson int func_idx; 39405d019abSRichard Henderson 39538b47b19SEmilio G. Cota /* copy all ops until the call */ 39638b47b19SEmilio G. Cota do { 39738b47b19SEmilio G. Cota op = copy_op_nocheck(begin_op, op); 39838b47b19SEmilio G. Cota } while (op->opc != INDEX_op_call); 39938b47b19SEmilio G. Cota 40038b47b19SEmilio G. Cota /* fill in the op call */ 40105d019abSRichard Henderson old_op = *begin_op; 40205d019abSRichard Henderson TCGOP_CALLI(op) = TCGOP_CALLI(old_op); 40305d019abSRichard Henderson TCGOP_CALLO(op) = TCGOP_CALLO(old_op); 40438b47b19SEmilio G. Cota tcg_debug_assert(op->life == 0); 40538b47b19SEmilio G. Cota 40605d019abSRichard Henderson func_idx = TCGOP_CALLO(op) + TCGOP_CALLI(op); 40705d019abSRichard Henderson *cb_idx = func_idx; 40805d019abSRichard Henderson op->args[func_idx] = (uintptr_t)func; 40938b47b19SEmilio G. Cota 41038b47b19SEmilio G. Cota return op; 41138b47b19SEmilio G. Cota } 41238b47b19SEmilio G. Cota 4130d6e6cb7SAlex Bennée /* 4140d6e6cb7SAlex Bennée * When we append/replace ops here we are sensitive to changing patterns of 4150d6e6cb7SAlex Bennée * TCGOps generated by the tcg_gen_FOO calls when we generated the 4160d6e6cb7SAlex Bennée * empty callbacks. This will assert very quickly in a debug build as 4170d6e6cb7SAlex Bennée * we assert the ops we are replacing are the correct ones. 4180d6e6cb7SAlex Bennée */ 41938b47b19SEmilio G. Cota static TCGOp *append_udata_cb(const struct qemu_plugin_dyn_cb *cb, 42038b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *cb_idx) 42138b47b19SEmilio G. Cota { 42238b47b19SEmilio G. Cota /* const_ptr */ 42338b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 42438b47b19SEmilio G. Cota 42538b47b19SEmilio G. Cota /* copy the ld_i32, but note that we only have to copy it once */ 426f266bec8SRichard Henderson if (*cb_idx == -1) { 427f266bec8SRichard Henderson op = copy_op(&begin_op, op, INDEX_op_ld_i32); 428f266bec8SRichard Henderson } else { 42938b47b19SEmilio G. Cota begin_op = QTAILQ_NEXT(begin_op, link); 43038b47b19SEmilio G. Cota tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32); 43138b47b19SEmilio G. Cota } 43238b47b19SEmilio G. Cota 43338b47b19SEmilio G. Cota /* call */ 434946bf79eSAkihiko Odaki op = copy_call(&begin_op, op, cb->f.vcpu_udata, cb_idx); 43538b47b19SEmilio G. Cota 43638b47b19SEmilio G. Cota return op; 43738b47b19SEmilio G. Cota } 43838b47b19SEmilio G. Cota 43938b47b19SEmilio G. Cota static TCGOp *append_inline_cb(const struct qemu_plugin_dyn_cb *cb, 44038b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, 44138b47b19SEmilio G. Cota int *unused) 44238b47b19SEmilio G. Cota { 443*62f92b8dSPierrick Bouvier char *ptr = cb->userp; 444*62f92b8dSPierrick Bouvier size_t elem_size = 0; 445*62f92b8dSPierrick Bouvier size_t offset = 0; 446*62f92b8dSPierrick Bouvier op = copy_ld_i32(&begin_op, op); 447*62f92b8dSPierrick Bouvier op = copy_mul_i32(&begin_op, op, elem_size); 448*62f92b8dSPierrick Bouvier op = copy_ext_i32_ptr(&begin_op, op); 449*62f92b8dSPierrick Bouvier op = copy_const_ptr(&begin_op, op, ptr + offset); 450*62f92b8dSPierrick Bouvier op = copy_add_ptr(&begin_op, op); 45138b47b19SEmilio G. Cota op = copy_ld_i64(&begin_op, op); 4520d6e6cb7SAlex Bennée op = copy_add_i64(&begin_op, op, cb->inline_insn.imm); 45338b47b19SEmilio G. Cota op = copy_st_i64(&begin_op, op); 45438b47b19SEmilio G. Cota return op; 45538b47b19SEmilio G. Cota } 45638b47b19SEmilio G. Cota 45738b47b19SEmilio G. Cota static TCGOp *append_mem_cb(const struct qemu_plugin_dyn_cb *cb, 45838b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *cb_idx) 45938b47b19SEmilio G. Cota { 46038b47b19SEmilio G. Cota enum plugin_gen_cb type = begin_op->args[1]; 46138b47b19SEmilio G. Cota 46238b47b19SEmilio G. Cota tcg_debug_assert(type == PLUGIN_GEN_CB_MEM); 46338b47b19SEmilio G. Cota 46480c44bbaSRichard Henderson /* const_i32 == mov_i32 ("info", so it remains as is) */ 46580c44bbaSRichard Henderson op = copy_op(&begin_op, op, INDEX_op_mov_i32); 46638b47b19SEmilio G. Cota 46738b47b19SEmilio G. Cota /* const_ptr */ 46838b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 46938b47b19SEmilio G. Cota 47038b47b19SEmilio G. Cota /* copy the ld_i32, but note that we only have to copy it once */ 471f266bec8SRichard Henderson if (*cb_idx == -1) { 472f266bec8SRichard Henderson op = copy_op(&begin_op, op, INDEX_op_ld_i32); 473f266bec8SRichard Henderson } else { 47438b47b19SEmilio G. Cota begin_op = QTAILQ_NEXT(begin_op, link); 47538b47b19SEmilio G. Cota tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32); 47638b47b19SEmilio G. Cota } 47738b47b19SEmilio G. Cota 47838b47b19SEmilio G. Cota if (type == PLUGIN_GEN_CB_MEM) { 47938b47b19SEmilio G. Cota /* call */ 480946bf79eSAkihiko Odaki op = copy_call(&begin_op, op, cb->f.vcpu_udata, cb_idx); 48138b47b19SEmilio G. Cota } 48238b47b19SEmilio G. Cota 48338b47b19SEmilio G. Cota return op; 48438b47b19SEmilio G. Cota } 48538b47b19SEmilio G. Cota 48638b47b19SEmilio G. Cota typedef TCGOp *(*inject_fn)(const struct qemu_plugin_dyn_cb *cb, 48738b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *intp); 48838b47b19SEmilio G. Cota typedef bool (*op_ok_fn)(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb); 48938b47b19SEmilio G. Cota 49038b47b19SEmilio G. Cota static bool op_ok(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb) 49138b47b19SEmilio G. Cota { 49238b47b19SEmilio G. Cota return true; 49338b47b19SEmilio G. Cota } 49438b47b19SEmilio G. Cota 49538b47b19SEmilio G. Cota static bool op_rw(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb) 49638b47b19SEmilio G. Cota { 49738b47b19SEmilio G. Cota int w; 49838b47b19SEmilio G. Cota 49938b47b19SEmilio G. Cota w = op->args[2]; 50038b47b19SEmilio G. Cota return !!(cb->rw & (w + 1)); 50138b47b19SEmilio G. Cota } 50238b47b19SEmilio G. Cota 5039a3ee366SRichard Henderson static void inject_cb_type(const GArray *cbs, TCGOp *begin_op, 5049a3ee366SRichard Henderson inject_fn inject, op_ok_fn ok) 50538b47b19SEmilio G. Cota { 50638b47b19SEmilio G. Cota TCGOp *end_op; 50738b47b19SEmilio G. Cota TCGOp *op; 50838b47b19SEmilio G. Cota int cb_idx = -1; 50938b47b19SEmilio G. Cota int i; 51038b47b19SEmilio G. Cota 51138b47b19SEmilio G. Cota if (!cbs || cbs->len == 0) { 51238b47b19SEmilio G. Cota rm_ops(begin_op); 51338b47b19SEmilio G. Cota return; 51438b47b19SEmilio G. Cota } 51538b47b19SEmilio G. Cota 51638b47b19SEmilio G. Cota end_op = find_op(begin_op, INDEX_op_plugin_cb_end); 51738b47b19SEmilio G. Cota tcg_debug_assert(end_op); 51838b47b19SEmilio G. Cota 51938b47b19SEmilio G. Cota op = end_op; 52038b47b19SEmilio G. Cota for (i = 0; i < cbs->len; i++) { 52138b47b19SEmilio G. Cota struct qemu_plugin_dyn_cb *cb = 52238b47b19SEmilio G. Cota &g_array_index(cbs, struct qemu_plugin_dyn_cb, i); 52338b47b19SEmilio G. Cota 52438b47b19SEmilio G. Cota if (!ok(begin_op, cb)) { 52538b47b19SEmilio G. Cota continue; 52638b47b19SEmilio G. Cota } 52738b47b19SEmilio G. Cota op = inject(cb, begin_op, op, &cb_idx); 52838b47b19SEmilio G. Cota } 52938b47b19SEmilio G. Cota rm_ops_range(begin_op, end_op); 53038b47b19SEmilio G. Cota } 53138b47b19SEmilio G. Cota 53238b47b19SEmilio G. Cota static void 53338b47b19SEmilio G. Cota inject_udata_cb(const GArray *cbs, TCGOp *begin_op) 53438b47b19SEmilio G. Cota { 53538b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_udata_cb, op_ok); 53638b47b19SEmilio G. Cota } 53738b47b19SEmilio G. Cota 53838b47b19SEmilio G. Cota static void 53938b47b19SEmilio G. Cota inject_inline_cb(const GArray *cbs, TCGOp *begin_op, op_ok_fn ok) 54038b47b19SEmilio G. Cota { 54138b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_inline_cb, ok); 54238b47b19SEmilio G. Cota } 54338b47b19SEmilio G. Cota 54438b47b19SEmilio G. Cota static void 54538b47b19SEmilio G. Cota inject_mem_cb(const GArray *cbs, TCGOp *begin_op) 54638b47b19SEmilio G. Cota { 54738b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_mem_cb, op_rw); 54838b47b19SEmilio G. Cota } 54938b47b19SEmilio G. Cota 55038b47b19SEmilio G. Cota /* we could change the ops in place, but we can reuse more code by copying */ 55138b47b19SEmilio G. Cota static void inject_mem_helper(TCGOp *begin_op, GArray *arr) 55238b47b19SEmilio G. Cota { 55338b47b19SEmilio G. Cota TCGOp *orig_op = begin_op; 55438b47b19SEmilio G. Cota TCGOp *end_op; 55538b47b19SEmilio G. Cota TCGOp *op; 55638b47b19SEmilio G. Cota 55738b47b19SEmilio G. Cota end_op = find_op(begin_op, INDEX_op_plugin_cb_end); 55838b47b19SEmilio G. Cota tcg_debug_assert(end_op); 55938b47b19SEmilio G. Cota 56038b47b19SEmilio G. Cota /* const ptr */ 56138b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, end_op, arr); 56238b47b19SEmilio G. Cota 56338b47b19SEmilio G. Cota /* st_ptr */ 56438b47b19SEmilio G. Cota op = copy_st_ptr(&begin_op, op); 56538b47b19SEmilio G. Cota 56638b47b19SEmilio G. Cota rm_ops_range(orig_op, end_op); 56738b47b19SEmilio G. Cota } 56838b47b19SEmilio G. Cota 56938b47b19SEmilio G. Cota /* 57038b47b19SEmilio G. Cota * Tracking memory accesses performed from helpers requires extra work. 57138b47b19SEmilio G. Cota * If an instruction is emulated with helpers, we do two things: 57238b47b19SEmilio G. Cota * (1) copy the CB descriptors, and keep track of it so that they can be 57338b47b19SEmilio G. Cota * freed later on, and (2) point CPUState.plugin_mem_cbs to the descriptors, so 57438b47b19SEmilio G. Cota * that we can read them at run-time (i.e. when the helper executes). 57538b47b19SEmilio G. Cota * This run-time access is performed from qemu_plugin_vcpu_mem_cb. 57638b47b19SEmilio G. Cota * 57738b47b19SEmilio G. Cota * Note that plugin_gen_disable_mem_helpers undoes (2). Since it 57838b47b19SEmilio G. Cota * is possible that the code we generate after the instruction is 57938b47b19SEmilio G. Cota * dead, we also add checks before generating tb_exit etc. 58038b47b19SEmilio G. Cota */ 5813fd62e73SEmilio Cota static void inject_mem_enable_helper(struct qemu_plugin_tb *ptb, 5823fd62e73SEmilio Cota struct qemu_plugin_insn *plugin_insn, 58338b47b19SEmilio G. Cota TCGOp *begin_op) 58438b47b19SEmilio G. Cota { 58538b47b19SEmilio G. Cota GArray *cbs[2]; 58638b47b19SEmilio G. Cota GArray *arr; 58738b47b19SEmilio G. Cota size_t n_cbs, i; 58838b47b19SEmilio G. Cota 58938b47b19SEmilio G. Cota cbs[0] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR]; 59038b47b19SEmilio G. Cota cbs[1] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE]; 59138b47b19SEmilio G. Cota 59238b47b19SEmilio G. Cota n_cbs = 0; 59338b47b19SEmilio G. Cota for (i = 0; i < ARRAY_SIZE(cbs); i++) { 59438b47b19SEmilio G. Cota n_cbs += cbs[i]->len; 59538b47b19SEmilio G. Cota } 59638b47b19SEmilio G. Cota 59738b47b19SEmilio G. Cota plugin_insn->mem_helper = plugin_insn->calls_helpers && n_cbs; 59838b47b19SEmilio G. Cota if (likely(!plugin_insn->mem_helper)) { 59938b47b19SEmilio G. Cota rm_ops(begin_op); 60038b47b19SEmilio G. Cota return; 60138b47b19SEmilio G. Cota } 6023fd62e73SEmilio Cota ptb->mem_helper = true; 60338b47b19SEmilio G. Cota 60438b47b19SEmilio G. Cota arr = g_array_sized_new(false, false, 60538b47b19SEmilio G. Cota sizeof(struct qemu_plugin_dyn_cb), n_cbs); 60638b47b19SEmilio G. Cota 60738b47b19SEmilio G. Cota for (i = 0; i < ARRAY_SIZE(cbs); i++) { 60838b47b19SEmilio G. Cota g_array_append_vals(arr, cbs[i]->data, cbs[i]->len); 60938b47b19SEmilio G. Cota } 61038b47b19SEmilio G. Cota 61138b47b19SEmilio G. Cota qemu_plugin_add_dyn_cb_arr(arr); 61238b47b19SEmilio G. Cota inject_mem_helper(begin_op, arr); 61338b47b19SEmilio G. Cota } 61438b47b19SEmilio G. Cota 61538b47b19SEmilio G. Cota static void inject_mem_disable_helper(struct qemu_plugin_insn *plugin_insn, 61638b47b19SEmilio G. Cota TCGOp *begin_op) 61738b47b19SEmilio G. Cota { 61838b47b19SEmilio G. Cota if (likely(!plugin_insn->mem_helper)) { 61938b47b19SEmilio G. Cota rm_ops(begin_op); 62038b47b19SEmilio G. Cota return; 62138b47b19SEmilio G. Cota } 62238b47b19SEmilio G. Cota inject_mem_helper(begin_op, NULL); 62338b47b19SEmilio G. Cota } 62438b47b19SEmilio G. Cota 62538b47b19SEmilio G. Cota /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */ 62638b47b19SEmilio G. Cota void plugin_gen_disable_mem_helpers(void) 62738b47b19SEmilio G. Cota { 6283fd62e73SEmilio Cota /* 6293fd62e73SEmilio Cota * We could emit the clearing unconditionally and be done. However, this can 6303fd62e73SEmilio Cota * be wasteful if for instance plugins don't track memory accesses, or if 6313fd62e73SEmilio Cota * most TBs don't use helpers. Instead, emit the clearing iff the TB calls 6323fd62e73SEmilio Cota * helpers that might access guest memory. 6333fd62e73SEmilio Cota * 6343fd62e73SEmilio Cota * Note: we do not reset plugin_tb->mem_helper here; a TB might have several 6353fd62e73SEmilio Cota * exit points, and we want to emit the clearing from all of them. 6363fd62e73SEmilio Cota */ 6373fd62e73SEmilio Cota if (!tcg_ctx->plugin_tb->mem_helper) { 63838b47b19SEmilio G. Cota return; 63938b47b19SEmilio G. Cota } 640ad75a51eSRichard Henderson tcg_gen_st_ptr(tcg_constant_ptr(NULL), tcg_env, 64140138843SRichard Henderson offsetof(CPUState, plugin_mem_cbs) - offsetof(ArchCPU, env)); 64238b47b19SEmilio G. Cota } 64338b47b19SEmilio G. Cota 64438b47b19SEmilio G. Cota static void plugin_gen_tb_udata(const struct qemu_plugin_tb *ptb, 64538b47b19SEmilio G. Cota TCGOp *begin_op) 64638b47b19SEmilio G. Cota { 64738b47b19SEmilio G. Cota inject_udata_cb(ptb->cbs[PLUGIN_CB_REGULAR], begin_op); 64838b47b19SEmilio G. Cota } 64938b47b19SEmilio G. Cota 65033a277feSAkihiko Odaki static void plugin_gen_tb_udata_r(const struct qemu_plugin_tb *ptb, 65133a277feSAkihiko Odaki TCGOp *begin_op) 65233a277feSAkihiko Odaki { 65333a277feSAkihiko Odaki inject_udata_cb(ptb->cbs[PLUGIN_CB_REGULAR_R], begin_op); 65433a277feSAkihiko Odaki } 65533a277feSAkihiko Odaki 65638b47b19SEmilio G. Cota static void plugin_gen_tb_inline(const struct qemu_plugin_tb *ptb, 65738b47b19SEmilio G. Cota TCGOp *begin_op) 65838b47b19SEmilio G. Cota { 65938b47b19SEmilio G. Cota inject_inline_cb(ptb->cbs[PLUGIN_CB_INLINE], begin_op, op_ok); 66038b47b19SEmilio G. Cota } 66138b47b19SEmilio G. Cota 66238b47b19SEmilio G. Cota static void plugin_gen_insn_udata(const struct qemu_plugin_tb *ptb, 66338b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 66438b47b19SEmilio G. Cota { 66538b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 66638b47b19SEmilio G. Cota 66738b47b19SEmilio G. Cota inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR], begin_op); 66838b47b19SEmilio G. Cota } 66938b47b19SEmilio G. Cota 67033a277feSAkihiko Odaki static void plugin_gen_insn_udata_r(const struct qemu_plugin_tb *ptb, 67133a277feSAkihiko Odaki TCGOp *begin_op, int insn_idx) 67233a277feSAkihiko Odaki { 67333a277feSAkihiko Odaki struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 67433a277feSAkihiko Odaki 67533a277feSAkihiko Odaki inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR_R], begin_op); 67633a277feSAkihiko Odaki } 67733a277feSAkihiko Odaki 67838b47b19SEmilio G. Cota static void plugin_gen_insn_inline(const struct qemu_plugin_tb *ptb, 67938b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 68038b47b19SEmilio G. Cota { 68138b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 68238b47b19SEmilio G. Cota inject_inline_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE], 68338b47b19SEmilio G. Cota begin_op, op_ok); 68438b47b19SEmilio G. Cota } 68538b47b19SEmilio G. Cota 68638b47b19SEmilio G. Cota static void plugin_gen_mem_regular(const struct qemu_plugin_tb *ptb, 68738b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 68838b47b19SEmilio G. Cota { 68938b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 69038b47b19SEmilio G. Cota inject_mem_cb(insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR], begin_op); 69138b47b19SEmilio G. Cota } 69238b47b19SEmilio G. Cota 69338b47b19SEmilio G. Cota static void plugin_gen_mem_inline(const struct qemu_plugin_tb *ptb, 69438b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 69538b47b19SEmilio G. Cota { 69638b47b19SEmilio G. Cota const GArray *cbs; 69738b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 69838b47b19SEmilio G. Cota 69938b47b19SEmilio G. Cota cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE]; 70038b47b19SEmilio G. Cota inject_inline_cb(cbs, begin_op, op_rw); 70138b47b19SEmilio G. Cota } 70238b47b19SEmilio G. Cota 7033fd62e73SEmilio Cota static void plugin_gen_enable_mem_helper(struct qemu_plugin_tb *ptb, 70438b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 70538b47b19SEmilio G. Cota { 70638b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 7073fd62e73SEmilio Cota inject_mem_enable_helper(ptb, insn, begin_op); 70838b47b19SEmilio G. Cota } 70938b47b19SEmilio G. Cota 7103fd62e73SEmilio Cota static void plugin_gen_disable_mem_helper(struct qemu_plugin_tb *ptb, 71138b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 71238b47b19SEmilio G. Cota { 71338b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 71438b47b19SEmilio G. Cota inject_mem_disable_helper(insn, begin_op); 71538b47b19SEmilio G. Cota } 71638b47b19SEmilio G. Cota 71738b47b19SEmilio G. Cota /* #define DEBUG_PLUGIN_GEN_OPS */ 71838b47b19SEmilio G. Cota static void pr_ops(void) 71938b47b19SEmilio G. Cota { 72038b47b19SEmilio G. Cota #ifdef DEBUG_PLUGIN_GEN_OPS 72138b47b19SEmilio G. Cota TCGOp *op; 72238b47b19SEmilio G. Cota int i = 0; 72338b47b19SEmilio G. Cota 72438b47b19SEmilio G. Cota QTAILQ_FOREACH(op, &tcg_ctx->ops, link) { 72538b47b19SEmilio G. Cota const char *name = ""; 72638b47b19SEmilio G. Cota const char *type = ""; 72738b47b19SEmilio G. Cota 72838b47b19SEmilio G. Cota if (op->opc == INDEX_op_plugin_cb_start) { 72938b47b19SEmilio G. Cota switch (op->args[0]) { 73038b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_TB: 73138b47b19SEmilio G. Cota name = "tb"; 73238b47b19SEmilio G. Cota break; 73338b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 73438b47b19SEmilio G. Cota name = "insn"; 73538b47b19SEmilio G. Cota break; 73638b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_MEM: 73738b47b19SEmilio G. Cota name = "mem"; 73838b47b19SEmilio G. Cota break; 73938b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 74038b47b19SEmilio G. Cota name = "after insn"; 74138b47b19SEmilio G. Cota break; 74238b47b19SEmilio G. Cota default: 74338b47b19SEmilio G. Cota break; 74438b47b19SEmilio G. Cota } 74538b47b19SEmilio G. Cota switch (op->args[1]) { 74638b47b19SEmilio G. Cota case PLUGIN_GEN_CB_UDATA: 74738b47b19SEmilio G. Cota type = "udata"; 74838b47b19SEmilio G. Cota break; 74938b47b19SEmilio G. Cota case PLUGIN_GEN_CB_INLINE: 75038b47b19SEmilio G. Cota type = "inline"; 75138b47b19SEmilio G. Cota break; 75238b47b19SEmilio G. Cota case PLUGIN_GEN_CB_MEM: 75338b47b19SEmilio G. Cota type = "mem"; 75438b47b19SEmilio G. Cota break; 75538b47b19SEmilio G. Cota case PLUGIN_GEN_ENABLE_MEM_HELPER: 75638b47b19SEmilio G. Cota type = "enable mem helper"; 75738b47b19SEmilio G. Cota break; 75838b47b19SEmilio G. Cota case PLUGIN_GEN_DISABLE_MEM_HELPER: 75938b47b19SEmilio G. Cota type = "disable mem helper"; 76038b47b19SEmilio G. Cota break; 76138b47b19SEmilio G. Cota default: 76238b47b19SEmilio G. Cota break; 76338b47b19SEmilio G. Cota } 76438b47b19SEmilio G. Cota } 76538b47b19SEmilio G. Cota printf("op[%2i]: %s %s %s\n", i, tcg_op_defs[op->opc].name, name, type); 76638b47b19SEmilio G. Cota i++; 76738b47b19SEmilio G. Cota } 76838b47b19SEmilio G. Cota #endif 76938b47b19SEmilio G. Cota } 77038b47b19SEmilio G. Cota 7713fd62e73SEmilio Cota static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb) 77238b47b19SEmilio G. Cota { 77338b47b19SEmilio G. Cota TCGOp *op; 774453d50ceSAlex Bennée int insn_idx = -1; 77538b47b19SEmilio G. Cota 77638b47b19SEmilio G. Cota pr_ops(); 777453d50ceSAlex Bennée 778453d50ceSAlex Bennée QTAILQ_FOREACH(op, &tcg_ctx->ops, link) { 779453d50ceSAlex Bennée switch (op->opc) { 780453d50ceSAlex Bennée case INDEX_op_insn_start: 781453d50ceSAlex Bennée insn_idx++; 782453d50ceSAlex Bennée break; 783453d50ceSAlex Bennée case INDEX_op_plugin_cb_start: 784453d50ceSAlex Bennée { 78538b47b19SEmilio G. Cota enum plugin_gen_from from = op->args[0]; 78638b47b19SEmilio G. Cota enum plugin_gen_cb type = op->args[1]; 78738b47b19SEmilio G. Cota 788453d50ceSAlex Bennée switch (from) { 789453d50ceSAlex Bennée case PLUGIN_GEN_FROM_TB: 790453d50ceSAlex Bennée { 791453d50ceSAlex Bennée g_assert(insn_idx == -1); 792453d50ceSAlex Bennée 793453d50ceSAlex Bennée switch (type) { 794453d50ceSAlex Bennée case PLUGIN_GEN_CB_UDATA: 795453d50ceSAlex Bennée plugin_gen_tb_udata(plugin_tb, op); 796453d50ceSAlex Bennée break; 79733a277feSAkihiko Odaki case PLUGIN_GEN_CB_UDATA_R: 79833a277feSAkihiko Odaki plugin_gen_tb_udata_r(plugin_tb, op); 79933a277feSAkihiko Odaki break; 800453d50ceSAlex Bennée case PLUGIN_GEN_CB_INLINE: 801453d50ceSAlex Bennée plugin_gen_tb_inline(plugin_tb, op); 802453d50ceSAlex Bennée break; 803453d50ceSAlex Bennée default: 804453d50ceSAlex Bennée g_assert_not_reached(); 80538b47b19SEmilio G. Cota } 806453d50ceSAlex Bennée break; 807453d50ceSAlex Bennée } 808453d50ceSAlex Bennée case PLUGIN_GEN_FROM_INSN: 809453d50ceSAlex Bennée { 810453d50ceSAlex Bennée g_assert(insn_idx >= 0); 811453d50ceSAlex Bennée 812453d50ceSAlex Bennée switch (type) { 813453d50ceSAlex Bennée case PLUGIN_GEN_CB_UDATA: 814453d50ceSAlex Bennée plugin_gen_insn_udata(plugin_tb, op, insn_idx); 815453d50ceSAlex Bennée break; 81633a277feSAkihiko Odaki case PLUGIN_GEN_CB_UDATA_R: 81733a277feSAkihiko Odaki plugin_gen_insn_udata_r(plugin_tb, op, insn_idx); 81833a277feSAkihiko Odaki break; 819453d50ceSAlex Bennée case PLUGIN_GEN_CB_INLINE: 820453d50ceSAlex Bennée plugin_gen_insn_inline(plugin_tb, op, insn_idx); 821453d50ceSAlex Bennée break; 822453d50ceSAlex Bennée case PLUGIN_GEN_ENABLE_MEM_HELPER: 823453d50ceSAlex Bennée plugin_gen_enable_mem_helper(plugin_tb, op, insn_idx); 824453d50ceSAlex Bennée break; 825453d50ceSAlex Bennée default: 826453d50ceSAlex Bennée g_assert_not_reached(); 827453d50ceSAlex Bennée } 828453d50ceSAlex Bennée break; 829453d50ceSAlex Bennée } 830453d50ceSAlex Bennée case PLUGIN_GEN_FROM_MEM: 831453d50ceSAlex Bennée { 832453d50ceSAlex Bennée g_assert(insn_idx >= 0); 833453d50ceSAlex Bennée 834453d50ceSAlex Bennée switch (type) { 835453d50ceSAlex Bennée case PLUGIN_GEN_CB_MEM: 836453d50ceSAlex Bennée plugin_gen_mem_regular(plugin_tb, op, insn_idx); 837453d50ceSAlex Bennée break; 838453d50ceSAlex Bennée case PLUGIN_GEN_CB_INLINE: 839453d50ceSAlex Bennée plugin_gen_mem_inline(plugin_tb, op, insn_idx); 840453d50ceSAlex Bennée break; 841453d50ceSAlex Bennée default: 842453d50ceSAlex Bennée g_assert_not_reached(); 843453d50ceSAlex Bennée } 844453d50ceSAlex Bennée 845453d50ceSAlex Bennée break; 846453d50ceSAlex Bennée } 847453d50ceSAlex Bennée case PLUGIN_GEN_AFTER_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_DISABLE_MEM_HELPER: 853453d50ceSAlex Bennée plugin_gen_disable_mem_helper(plugin_tb, op, insn_idx); 854453d50ceSAlex Bennée break; 855453d50ceSAlex Bennée default: 856453d50ceSAlex Bennée g_assert_not_reached(); 857453d50ceSAlex Bennée } 858453d50ceSAlex Bennée break; 859453d50ceSAlex Bennée } 860453d50ceSAlex Bennée default: 861453d50ceSAlex Bennée g_assert_not_reached(); 862453d50ceSAlex Bennée } 863453d50ceSAlex Bennée break; 864453d50ceSAlex Bennée } 865453d50ceSAlex Bennée default: 866453d50ceSAlex Bennée /* plugins don't care about any other ops */ 867453d50ceSAlex Bennée break; 868453d50ceSAlex Bennée } 86938b47b19SEmilio G. Cota } 87038b47b19SEmilio G. Cota pr_ops(); 87138b47b19SEmilio G. Cota } 87238b47b19SEmilio G. Cota 873b21af662SRichard Henderson bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db, 874b21af662SRichard Henderson bool mem_only) 87538b47b19SEmilio G. Cota { 87638b47b19SEmilio G. Cota bool ret = false; 87738b47b19SEmilio G. Cota 878c0061471SAlex Bennée if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_state->event_mask)) { 8796f15c076SAlex Bennée struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 8806f15c076SAlex Bennée int i; 8816f15c076SAlex Bennée 8826f15c076SAlex Bennée /* reset callbacks */ 8836f15c076SAlex Bennée for (i = 0; i < PLUGIN_N_CB_SUBTYPES; i++) { 8846f15c076SAlex Bennée if (ptb->cbs[i]) { 8856f15c076SAlex Bennée g_array_set_size(ptb->cbs[i], 0); 8866f15c076SAlex Bennée } 8876f15c076SAlex Bennée } 8886f15c076SAlex Bennée ptb->n = 0; 8896f15c076SAlex Bennée 89038b47b19SEmilio G. Cota ret = true; 89138b47b19SEmilio G. Cota 892b21af662SRichard Henderson ptb->vaddr = db->pc_first; 89338b47b19SEmilio G. Cota ptb->vaddr2 = -1; 894b21af662SRichard Henderson ptb->haddr1 = db->host_addr[0]; 89538b47b19SEmilio G. Cota ptb->haddr2 = NULL; 896cfd405eaSAlex Bennée ptb->mem_only = mem_only; 8973fd62e73SEmilio Cota ptb->mem_helper = false; 89838b47b19SEmilio G. Cota 89938b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB); 90038b47b19SEmilio G. Cota } 9016f15c076SAlex Bennée 9026f15c076SAlex Bennée tcg_ctx->plugin_insn = NULL; 9036f15c076SAlex Bennée 90438b47b19SEmilio G. Cota return ret; 90538b47b19SEmilio G. Cota } 90638b47b19SEmilio G. Cota 90738b47b19SEmilio G. Cota void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db) 90838b47b19SEmilio G. Cota { 90938b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 91038b47b19SEmilio G. Cota struct qemu_plugin_insn *pinsn; 91138b47b19SEmilio G. Cota 912357af9beSAlex Bennée pinsn = qemu_plugin_tb_insn_get(ptb, db->pc_next); 91338b47b19SEmilio G. Cota tcg_ctx->plugin_insn = pinsn; 91438b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN); 91538b47b19SEmilio G. Cota 91638b47b19SEmilio G. Cota /* 91738b47b19SEmilio G. Cota * Detect page crossing to get the new host address. 91838b47b19SEmilio G. Cota * Note that we skip this when haddr1 == NULL, e.g. when we're 91938b47b19SEmilio G. Cota * fetching instructions from a region not backed by RAM. 92038b47b19SEmilio G. Cota */ 921b21af662SRichard Henderson if (ptb->haddr1 == NULL) { 922b21af662SRichard Henderson pinsn->haddr = NULL; 923b21af662SRichard Henderson } else if (is_same_page(db, db->pc_next)) { 92438b47b19SEmilio G. Cota pinsn->haddr = ptb->haddr1 + pinsn->vaddr - ptb->vaddr; 92538b47b19SEmilio G. Cota } else { 926b21af662SRichard Henderson if (ptb->vaddr2 == -1) { 927b21af662SRichard Henderson ptb->vaddr2 = TARGET_PAGE_ALIGN(db->pc_first); 928b77af26eSRichard Henderson get_page_addr_code_hostp(cpu_env(cpu), ptb->vaddr2, &ptb->haddr2); 929b21af662SRichard Henderson } 93038b47b19SEmilio G. Cota pinsn->haddr = ptb->haddr2 + pinsn->vaddr - ptb->vaddr2; 93138b47b19SEmilio G. Cota } 93238b47b19SEmilio G. Cota } 93338b47b19SEmilio G. Cota 93438b47b19SEmilio G. Cota void plugin_gen_insn_end(void) 93538b47b19SEmilio G. Cota { 93638b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN); 93738b47b19SEmilio G. Cota } 93838b47b19SEmilio G. Cota 9396f15c076SAlex Bennée /* 9406f15c076SAlex Bennée * There are cases where we never get to finalise a translation - for 9416f15c076SAlex Bennée * example a page fault during translation. As a result we shouldn't 9426f15c076SAlex Bennée * do any clean-up here and make sure things are reset in 9436f15c076SAlex Bennée * plugin_gen_tb_start. 9446f15c076SAlex Bennée */ 945a392277dSMatt Borgerson void plugin_gen_tb_end(CPUState *cpu, size_t num_insns) 94638b47b19SEmilio G. Cota { 94738b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 94838b47b19SEmilio G. Cota 949a392277dSMatt Borgerson /* translator may have removed instructions, update final count */ 950a392277dSMatt Borgerson g_assert(num_insns <= ptb->n); 951a392277dSMatt Borgerson ptb->n = num_insns; 952a392277dSMatt Borgerson 95338b47b19SEmilio G. Cota /* collect instrumentation requests */ 95438b47b19SEmilio G. Cota qemu_plugin_tb_trans_cb(cpu, ptb); 95538b47b19SEmilio G. Cota 95638b47b19SEmilio G. Cota /* inject the instrumentation at the appropriate places */ 95738b47b19SEmilio G. Cota plugin_gen_inject(ptb); 95838b47b19SEmilio G. Cota } 959