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, 7274bb8accSRichard Henderson PLUGIN_GEN_AFTER_TB, 7338b47b19SEmilio G. Cota PLUGIN_GEN_N_FROMS, 7438b47b19SEmilio G. Cota }; 7538b47b19SEmilio G. Cota 7638b47b19SEmilio G. Cota enum plugin_gen_cb { 7738b47b19SEmilio G. Cota PLUGIN_GEN_CB_UDATA, 7833a277feSAkihiko Odaki PLUGIN_GEN_CB_UDATA_R, 7938b47b19SEmilio G. Cota PLUGIN_GEN_CB_INLINE, 8038b47b19SEmilio G. Cota PLUGIN_GEN_CB_MEM, 8138b47b19SEmilio G. Cota PLUGIN_GEN_ENABLE_MEM_HELPER, 8238b47b19SEmilio G. Cota PLUGIN_GEN_DISABLE_MEM_HELPER, 8338b47b19SEmilio G. Cota PLUGIN_GEN_N_CBS, 8438b47b19SEmilio G. Cota }; 8538b47b19SEmilio G. Cota 8638b47b19SEmilio G. Cota /* 8738b47b19SEmilio G. Cota * These helpers are stubs that get dynamically switched out for calls 8838b47b19SEmilio G. Cota * direct to the plugin if they are subscribed to. 8938b47b19SEmilio G. Cota */ 9033a277feSAkihiko Odaki void HELPER(plugin_vcpu_udata_cb_no_wg)(uint32_t cpu_index, void *udata) 9133a277feSAkihiko Odaki { } 9233a277feSAkihiko Odaki 9333a277feSAkihiko Odaki void HELPER(plugin_vcpu_udata_cb_no_rwg)(uint32_t cpu_index, void *udata) 9438b47b19SEmilio G. Cota { } 9538b47b19SEmilio G. Cota 9638b47b19SEmilio G. Cota void HELPER(plugin_vcpu_mem_cb)(unsigned int vcpu_index, 9738b47b19SEmilio G. Cota qemu_plugin_meminfo_t info, uint64_t vaddr, 9838b47b19SEmilio G. Cota void *userdata) 9938b47b19SEmilio G. Cota { } 10038b47b19SEmilio G. Cota 10138b47b19SEmilio G. Cota /* 10238b47b19SEmilio G. Cota * For now we only support addi_i64. 10338b47b19SEmilio G. Cota * When we support more ops, we can generate one empty inline cb for each. 10438b47b19SEmilio G. Cota */ 10538b47b19SEmilio G. Cota static void gen_empty_inline_cb(void) 10638b47b19SEmilio G. Cota { 10762f92b8dSPierrick Bouvier TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 10862f92b8dSPierrick Bouvier TCGv_ptr cpu_index_as_ptr = tcg_temp_ebb_new_ptr(); 109713f263aSRichard Henderson TCGv_i64 val = tcg_temp_ebb_new_i64(); 110713f263aSRichard Henderson TCGv_ptr ptr = tcg_temp_ebb_new_ptr(); 11138b47b19SEmilio G. Cota 11262f92b8dSPierrick Bouvier tcg_gen_ld_i32(cpu_index, tcg_env, 11362f92b8dSPierrick Bouvier -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 11462f92b8dSPierrick Bouvier /* second operand will be replaced by immediate value */ 11562f92b8dSPierrick Bouvier tcg_gen_mul_i32(cpu_index, cpu_index, cpu_index); 11662f92b8dSPierrick Bouvier tcg_gen_ext_i32_ptr(cpu_index_as_ptr, cpu_index); 11762f92b8dSPierrick Bouvier 118713f263aSRichard Henderson tcg_gen_movi_ptr(ptr, 0); 11962f92b8dSPierrick Bouvier tcg_gen_add_ptr(ptr, ptr, cpu_index_as_ptr); 12038b47b19SEmilio G. Cota tcg_gen_ld_i64(val, ptr, 0); 12162f92b8dSPierrick Bouvier /* second operand will be replaced by immediate value */ 12262f92b8dSPierrick Bouvier tcg_gen_add_i64(val, val, val); 12362f92b8dSPierrick Bouvier 12438b47b19SEmilio G. Cota tcg_gen_st_i64(val, ptr, 0); 12538b47b19SEmilio G. Cota tcg_temp_free_ptr(ptr); 12638b47b19SEmilio G. Cota tcg_temp_free_i64(val); 12762f92b8dSPierrick Bouvier tcg_temp_free_ptr(cpu_index_as_ptr); 12862f92b8dSPierrick Bouvier tcg_temp_free_i32(cpu_index); 12938b47b19SEmilio G. Cota } 13038b47b19SEmilio G. Cota 131fcdab382SRichard Henderson static void gen_empty_mem_cb(TCGv_i64 addr, uint32_t info) 13238b47b19SEmilio G. Cota { 133b6d91645SRichard Henderson TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 134b6d91645SRichard Henderson TCGv_i32 meminfo = tcg_temp_ebb_new_i32(); 135b6d91645SRichard Henderson TCGv_ptr udata = tcg_temp_ebb_new_ptr(); 136b6d91645SRichard Henderson 137b6d91645SRichard Henderson tcg_gen_movi_i32(meminfo, info); 138b6d91645SRichard Henderson tcg_gen_movi_ptr(udata, 0); 139ad75a51eSRichard Henderson tcg_gen_ld_i32(cpu_index, tcg_env, 140b6d91645SRichard Henderson -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 141b6d91645SRichard Henderson 142fcdab382SRichard Henderson gen_helper_plugin_vcpu_mem_cb(cpu_index, meminfo, addr, udata); 143b6d91645SRichard Henderson 144b6d91645SRichard Henderson tcg_temp_free_ptr(udata); 145b6d91645SRichard Henderson tcg_temp_free_i32(meminfo); 146b6d91645SRichard Henderson tcg_temp_free_i32(cpu_index); 14738b47b19SEmilio G. Cota } 14838b47b19SEmilio G. Cota 1499a3ee366SRichard Henderson static void gen_plugin_cb_start(enum plugin_gen_from from, 15038b47b19SEmilio G. Cota enum plugin_gen_cb type, unsigned wr) 15138b47b19SEmilio G. Cota { 15238b47b19SEmilio G. Cota tcg_gen_plugin_cb_start(from, type, wr); 15338b47b19SEmilio G. Cota } 15438b47b19SEmilio G. Cota 1559a3ee366SRichard Henderson static void plugin_gen_empty_callback(enum plugin_gen_from from) 15638b47b19SEmilio G. Cota { 15738b47b19SEmilio G. Cota switch (from) { 15838b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 15921a3f62fSRichard Henderson case PLUGIN_GEN_FROM_TB: 16038b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 161*ac977170SRichard Henderson tcg_gen_plugin_cb(from); 16238b47b19SEmilio G. Cota break; 16338b47b19SEmilio G. Cota default: 16438b47b19SEmilio G. Cota g_assert_not_reached(); 16538b47b19SEmilio G. Cota } 16638b47b19SEmilio G. Cota } 16738b47b19SEmilio G. Cota 168fcdab382SRichard Henderson void plugin_gen_empty_mem_callback(TCGv_i64 addr, uint32_t info) 16938b47b19SEmilio G. Cota { 17037aff087SRichard Henderson enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info); 17138b47b19SEmilio G. Cota 172f5c346acSRichard Henderson gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_MEM, rw); 173f5c346acSRichard Henderson gen_empty_mem_cb(addr, info); 17438b47b19SEmilio G. Cota tcg_gen_plugin_cb_end(); 17538b47b19SEmilio G. Cota 176f5c346acSRichard Henderson gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_INLINE, rw); 177f5c346acSRichard Henderson gen_empty_inline_cb(); 178f5c346acSRichard Henderson tcg_gen_plugin_cb_end(); 17938b47b19SEmilio G. Cota } 18038b47b19SEmilio G. Cota 18138b47b19SEmilio G. Cota static TCGOp *find_op(TCGOp *op, TCGOpcode opc) 18238b47b19SEmilio G. Cota { 18338b47b19SEmilio G. Cota while (op) { 18438b47b19SEmilio G. Cota if (op->opc == opc) { 18538b47b19SEmilio G. Cota return op; 18638b47b19SEmilio G. Cota } 18738b47b19SEmilio G. Cota op = QTAILQ_NEXT(op, link); 18838b47b19SEmilio G. Cota } 18938b47b19SEmilio G. Cota return NULL; 19038b47b19SEmilio G. Cota } 19138b47b19SEmilio G. Cota 19238b47b19SEmilio G. Cota static TCGOp *rm_ops_range(TCGOp *begin, TCGOp *end) 19338b47b19SEmilio G. Cota { 19438b47b19SEmilio G. Cota TCGOp *ret = QTAILQ_NEXT(end, link); 19538b47b19SEmilio G. Cota 19638b47b19SEmilio G. Cota QTAILQ_REMOVE_SEVERAL(&tcg_ctx->ops, begin, end, link); 19738b47b19SEmilio G. Cota return ret; 19838b47b19SEmilio G. Cota } 19938b47b19SEmilio G. Cota 20038b47b19SEmilio G. Cota /* remove all ops until (and including) plugin_cb_end */ 20138b47b19SEmilio G. Cota static TCGOp *rm_ops(TCGOp *op) 20238b47b19SEmilio G. Cota { 20338b47b19SEmilio G. Cota TCGOp *end_op = find_op(op, INDEX_op_plugin_cb_end); 20438b47b19SEmilio G. Cota 20538b47b19SEmilio G. Cota tcg_debug_assert(end_op); 20638b47b19SEmilio G. Cota return rm_ops_range(op, end_op); 20738b47b19SEmilio G. Cota } 20838b47b19SEmilio G. Cota 20938b47b19SEmilio G. Cota static TCGOp *copy_op_nocheck(TCGOp **begin_op, TCGOp *op) 21038b47b19SEmilio G. Cota { 211cb10bc63SRichard Henderson TCGOp *old_op = QTAILQ_NEXT(*begin_op, link); 212cb10bc63SRichard Henderson unsigned nargs = old_op->nargs; 213d4478943SPhilippe Mathieu-Daudé 214cb10bc63SRichard Henderson *begin_op = old_op; 215cb10bc63SRichard Henderson op = tcg_op_insert_after(tcg_ctx, op, old_op->opc, nargs); 216cb10bc63SRichard Henderson memcpy(op->args, old_op->args, sizeof(op->args[0]) * nargs); 217d4478943SPhilippe Mathieu-Daudé 21838b47b19SEmilio G. Cota return op; 21938b47b19SEmilio G. Cota } 22038b47b19SEmilio G. Cota 22138b47b19SEmilio G. Cota static TCGOp *copy_op(TCGOp **begin_op, TCGOp *op, TCGOpcode opc) 22238b47b19SEmilio G. Cota { 22338b47b19SEmilio G. Cota op = copy_op_nocheck(begin_op, op); 22438b47b19SEmilio G. Cota tcg_debug_assert((*begin_op)->opc == opc); 22538b47b19SEmilio G. Cota return op; 22638b47b19SEmilio G. Cota } 22738b47b19SEmilio G. Cota 22838b47b19SEmilio G. Cota static TCGOp *copy_const_ptr(TCGOp **begin_op, TCGOp *op, void *ptr) 22938b47b19SEmilio G. Cota { 23038b47b19SEmilio G. Cota if (UINTPTR_MAX == UINT32_MAX) { 23180c44bbaSRichard Henderson /* mov_i32 */ 23280c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i32); 23380c44bbaSRichard Henderson op->args[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr)); 23438b47b19SEmilio G. Cota } else { 23580c44bbaSRichard Henderson /* mov_i64 */ 23680c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i64); 23780c44bbaSRichard Henderson op->args[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr)); 23838b47b19SEmilio G. Cota } 23938b47b19SEmilio G. Cota return op; 24038b47b19SEmilio G. Cota } 24138b47b19SEmilio G. Cota 24262f92b8dSPierrick Bouvier static TCGOp *copy_ld_i32(TCGOp **begin_op, TCGOp *op) 24362f92b8dSPierrick Bouvier { 24462f92b8dSPierrick Bouvier return copy_op(begin_op, op, INDEX_op_ld_i32); 24562f92b8dSPierrick Bouvier } 24662f92b8dSPierrick Bouvier 24762f92b8dSPierrick Bouvier static TCGOp *copy_ext_i32_ptr(TCGOp **begin_op, TCGOp *op) 24862f92b8dSPierrick Bouvier { 24962f92b8dSPierrick Bouvier if (UINTPTR_MAX == UINT32_MAX) { 25062f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_mov_i32); 25162f92b8dSPierrick Bouvier } else { 25262f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_ext_i32_i64); 25362f92b8dSPierrick Bouvier } 25462f92b8dSPierrick Bouvier return op; 25562f92b8dSPierrick Bouvier } 25662f92b8dSPierrick Bouvier 25762f92b8dSPierrick Bouvier static TCGOp *copy_add_ptr(TCGOp **begin_op, TCGOp *op) 25862f92b8dSPierrick Bouvier { 25962f92b8dSPierrick Bouvier if (UINTPTR_MAX == UINT32_MAX) { 26062f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_add_i32); 26162f92b8dSPierrick Bouvier } else { 26262f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_add_i64); 26362f92b8dSPierrick Bouvier } 26462f92b8dSPierrick Bouvier return op; 26562f92b8dSPierrick Bouvier } 26662f92b8dSPierrick Bouvier 26738b47b19SEmilio G. Cota static TCGOp *copy_ld_i64(TCGOp **begin_op, TCGOp *op) 26838b47b19SEmilio G. Cota { 26938b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 27038b47b19SEmilio G. Cota /* 2x ld_i32 */ 27162f92b8dSPierrick Bouvier op = copy_ld_i32(begin_op, op); 27262f92b8dSPierrick Bouvier op = copy_ld_i32(begin_op, op); 27338b47b19SEmilio G. Cota } else { 27438b47b19SEmilio G. Cota /* ld_i64 */ 27538b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_ld_i64); 27638b47b19SEmilio G. Cota } 27738b47b19SEmilio G. Cota return op; 27838b47b19SEmilio G. Cota } 27938b47b19SEmilio G. Cota 28038b47b19SEmilio G. Cota static TCGOp *copy_st_i64(TCGOp **begin_op, TCGOp *op) 28138b47b19SEmilio G. Cota { 28238b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 28338b47b19SEmilio G. Cota /* 2x st_i32 */ 28438b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 28538b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 28638b47b19SEmilio G. Cota } else { 28738b47b19SEmilio G. Cota /* st_i64 */ 28838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i64); 28938b47b19SEmilio G. Cota } 29038b47b19SEmilio G. Cota return op; 29138b47b19SEmilio G. Cota } 29238b47b19SEmilio G. Cota 2930d6e6cb7SAlex Bennée static TCGOp *copy_add_i64(TCGOp **begin_op, TCGOp *op, uint64_t v) 29438b47b19SEmilio G. Cota { 29538b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 29638b47b19SEmilio G. Cota /* all 32-bit backends must implement add2_i32 */ 29738b47b19SEmilio G. Cota g_assert(TCG_TARGET_HAS_add2_i32); 29838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_add2_i32); 2990d6e6cb7SAlex Bennée op->args[4] = tcgv_i32_arg(tcg_constant_i32(v)); 3000d6e6cb7SAlex Bennée op->args[5] = tcgv_i32_arg(tcg_constant_i32(v >> 32)); 30138b47b19SEmilio G. Cota } else { 30238b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_add_i64); 3030d6e6cb7SAlex Bennée op->args[2] = tcgv_i64_arg(tcg_constant_i64(v)); 30438b47b19SEmilio G. Cota } 30538b47b19SEmilio G. Cota return op; 30638b47b19SEmilio G. Cota } 30738b47b19SEmilio G. Cota 30862f92b8dSPierrick Bouvier static TCGOp *copy_mul_i32(TCGOp **begin_op, TCGOp *op, uint32_t v) 30962f92b8dSPierrick Bouvier { 31062f92b8dSPierrick Bouvier op = copy_op(begin_op, op, INDEX_op_mul_i32); 31162f92b8dSPierrick Bouvier op->args[2] = tcgv_i32_arg(tcg_constant_i32(v)); 31262f92b8dSPierrick Bouvier return op; 31362f92b8dSPierrick Bouvier } 31462f92b8dSPierrick Bouvier 315946bf79eSAkihiko Odaki static TCGOp *copy_call(TCGOp **begin_op, TCGOp *op, void *func, int *cb_idx) 31638b47b19SEmilio G. Cota { 31705d019abSRichard Henderson TCGOp *old_op; 31805d019abSRichard Henderson int func_idx; 31905d019abSRichard Henderson 32038b47b19SEmilio G. Cota /* copy all ops until the call */ 32138b47b19SEmilio G. Cota do { 32238b47b19SEmilio G. Cota op = copy_op_nocheck(begin_op, op); 32338b47b19SEmilio G. Cota } while (op->opc != INDEX_op_call); 32438b47b19SEmilio G. Cota 32538b47b19SEmilio G. Cota /* fill in the op call */ 32605d019abSRichard Henderson old_op = *begin_op; 32705d019abSRichard Henderson TCGOP_CALLI(op) = TCGOP_CALLI(old_op); 32805d019abSRichard Henderson TCGOP_CALLO(op) = TCGOP_CALLO(old_op); 32938b47b19SEmilio G. Cota tcg_debug_assert(op->life == 0); 33038b47b19SEmilio G. Cota 33105d019abSRichard Henderson func_idx = TCGOP_CALLO(op) + TCGOP_CALLI(op); 33205d019abSRichard Henderson *cb_idx = func_idx; 33305d019abSRichard Henderson op->args[func_idx] = (uintptr_t)func; 33438b47b19SEmilio G. Cota 33538b47b19SEmilio G. Cota return op; 33638b47b19SEmilio G. Cota } 33738b47b19SEmilio G. Cota 33838b47b19SEmilio G. Cota static TCGOp *append_inline_cb(const struct qemu_plugin_dyn_cb *cb, 33938b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, 34038b47b19SEmilio G. Cota int *unused) 34138b47b19SEmilio G. Cota { 3423077be25SPierrick Bouvier char *ptr = cb->inline_insn.entry.score->data->data; 3433077be25SPierrick Bouvier size_t elem_size = g_array_get_element_size( 3443077be25SPierrick Bouvier cb->inline_insn.entry.score->data); 3453077be25SPierrick Bouvier size_t offset = cb->inline_insn.entry.offset; 3460bcebabaSPierrick Bouvier 34762f92b8dSPierrick Bouvier op = copy_ld_i32(&begin_op, op); 34862f92b8dSPierrick Bouvier op = copy_mul_i32(&begin_op, op, elem_size); 34962f92b8dSPierrick Bouvier op = copy_ext_i32_ptr(&begin_op, op); 35062f92b8dSPierrick Bouvier op = copy_const_ptr(&begin_op, op, ptr + offset); 35162f92b8dSPierrick Bouvier op = copy_add_ptr(&begin_op, op); 35238b47b19SEmilio G. Cota op = copy_ld_i64(&begin_op, op); 3530d6e6cb7SAlex Bennée op = copy_add_i64(&begin_op, op, cb->inline_insn.imm); 35438b47b19SEmilio G. Cota op = copy_st_i64(&begin_op, op); 35538b47b19SEmilio G. Cota return op; 35638b47b19SEmilio G. Cota } 35738b47b19SEmilio G. Cota 35838b47b19SEmilio G. Cota static TCGOp *append_mem_cb(const struct qemu_plugin_dyn_cb *cb, 35938b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *cb_idx) 36038b47b19SEmilio G. Cota { 36138b47b19SEmilio G. Cota enum plugin_gen_cb type = begin_op->args[1]; 36238b47b19SEmilio G. Cota 36338b47b19SEmilio G. Cota tcg_debug_assert(type == PLUGIN_GEN_CB_MEM); 36438b47b19SEmilio G. Cota 36580c44bbaSRichard Henderson /* const_i32 == mov_i32 ("info", so it remains as is) */ 36680c44bbaSRichard Henderson op = copy_op(&begin_op, op, INDEX_op_mov_i32); 36738b47b19SEmilio G. Cota 36838b47b19SEmilio G. Cota /* const_ptr */ 36938b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 37038b47b19SEmilio G. Cota 37138b47b19SEmilio G. Cota /* copy the ld_i32, but note that we only have to copy it once */ 372f266bec8SRichard Henderson if (*cb_idx == -1) { 373f266bec8SRichard Henderson op = copy_op(&begin_op, op, INDEX_op_ld_i32); 374f266bec8SRichard Henderson } else { 37538b47b19SEmilio G. Cota begin_op = QTAILQ_NEXT(begin_op, link); 37638b47b19SEmilio G. Cota tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32); 37738b47b19SEmilio G. Cota } 37838b47b19SEmilio G. Cota 37938b47b19SEmilio G. Cota if (type == PLUGIN_GEN_CB_MEM) { 38038b47b19SEmilio G. Cota /* call */ 381aff56de5SRichard Henderson op = copy_call(&begin_op, op, cb->regular.f.vcpu_udata, cb_idx); 38238b47b19SEmilio G. Cota } 38338b47b19SEmilio G. Cota 38438b47b19SEmilio G. Cota return op; 38538b47b19SEmilio G. Cota } 38638b47b19SEmilio G. Cota 38738b47b19SEmilio G. Cota typedef TCGOp *(*inject_fn)(const struct qemu_plugin_dyn_cb *cb, 38838b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *intp); 38938b47b19SEmilio G. Cota typedef bool (*op_ok_fn)(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb); 39038b47b19SEmilio G. Cota 39138b47b19SEmilio G. Cota static bool op_rw(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb) 39238b47b19SEmilio G. Cota { 39338b47b19SEmilio G. Cota int w; 39438b47b19SEmilio G. Cota 39538b47b19SEmilio G. Cota w = op->args[2]; 39638b47b19SEmilio G. Cota return !!(cb->rw & (w + 1)); 39738b47b19SEmilio G. Cota } 39838b47b19SEmilio G. Cota 3999a3ee366SRichard Henderson static void inject_cb_type(const GArray *cbs, TCGOp *begin_op, 4009a3ee366SRichard Henderson inject_fn inject, op_ok_fn ok) 40138b47b19SEmilio G. Cota { 40238b47b19SEmilio G. Cota TCGOp *end_op; 40338b47b19SEmilio G. Cota TCGOp *op; 40438b47b19SEmilio G. Cota int cb_idx = -1; 40538b47b19SEmilio G. Cota int i; 40638b47b19SEmilio G. Cota 40738b47b19SEmilio G. Cota if (!cbs || cbs->len == 0) { 40838b47b19SEmilio G. Cota rm_ops(begin_op); 40938b47b19SEmilio G. Cota return; 41038b47b19SEmilio G. Cota } 41138b47b19SEmilio G. Cota 41238b47b19SEmilio G. Cota end_op = find_op(begin_op, INDEX_op_plugin_cb_end); 41338b47b19SEmilio G. Cota tcg_debug_assert(end_op); 41438b47b19SEmilio G. Cota 41538b47b19SEmilio G. Cota op = end_op; 41638b47b19SEmilio G. Cota for (i = 0; i < cbs->len; i++) { 41738b47b19SEmilio G. Cota struct qemu_plugin_dyn_cb *cb = 41838b47b19SEmilio G. Cota &g_array_index(cbs, struct qemu_plugin_dyn_cb, i); 41938b47b19SEmilio G. Cota 42038b47b19SEmilio G. Cota if (!ok(begin_op, cb)) { 42138b47b19SEmilio G. Cota continue; 42238b47b19SEmilio G. Cota } 42338b47b19SEmilio G. Cota op = inject(cb, begin_op, op, &cb_idx); 42438b47b19SEmilio G. Cota } 42538b47b19SEmilio G. Cota rm_ops_range(begin_op, end_op); 42638b47b19SEmilio G. Cota } 42738b47b19SEmilio G. Cota 42838b47b19SEmilio G. Cota static void 42938b47b19SEmilio G. Cota inject_inline_cb(const GArray *cbs, TCGOp *begin_op, op_ok_fn ok) 43038b47b19SEmilio G. Cota { 43138b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_inline_cb, ok); 43238b47b19SEmilio G. Cota } 43338b47b19SEmilio G. Cota 43438b47b19SEmilio G. Cota static void 43538b47b19SEmilio G. Cota inject_mem_cb(const GArray *cbs, TCGOp *begin_op) 43638b47b19SEmilio G. Cota { 43738b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_mem_cb, op_rw); 43838b47b19SEmilio G. Cota } 43938b47b19SEmilio G. Cota 44038b47b19SEmilio G. Cota /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */ 44138b47b19SEmilio G. Cota void plugin_gen_disable_mem_helpers(void) 44238b47b19SEmilio G. Cota { 44374bb8accSRichard Henderson if (tcg_ctx->plugin_insn) { 44474bb8accSRichard Henderson tcg_gen_plugin_cb(PLUGIN_GEN_AFTER_TB); 44538b47b19SEmilio G. Cota } 44638b47b19SEmilio G. Cota } 44738b47b19SEmilio G. Cota 44838b47b19SEmilio G. Cota static void plugin_gen_mem_regular(const struct qemu_plugin_tb *ptb, 44938b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 45038b47b19SEmilio G. Cota { 45138b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 45238b47b19SEmilio G. Cota inject_mem_cb(insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR], begin_op); 45338b47b19SEmilio G. Cota } 45438b47b19SEmilio G. Cota 45538b47b19SEmilio G. Cota static void plugin_gen_mem_inline(const struct qemu_plugin_tb *ptb, 45638b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 45738b47b19SEmilio G. Cota { 45838b47b19SEmilio G. Cota const GArray *cbs; 45938b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 46038b47b19SEmilio G. Cota 46138b47b19SEmilio G. Cota cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE]; 46238b47b19SEmilio G. Cota inject_inline_cb(cbs, begin_op, op_rw); 46338b47b19SEmilio G. Cota } 46438b47b19SEmilio G. Cota 465*ac977170SRichard Henderson static void gen_enable_mem_helper(struct qemu_plugin_tb *ptb, 466*ac977170SRichard Henderson struct qemu_plugin_insn *insn) 46738b47b19SEmilio G. Cota { 468*ac977170SRichard Henderson GArray *cbs[2]; 469*ac977170SRichard Henderson GArray *arr; 470*ac977170SRichard Henderson size_t n_cbs; 471*ac977170SRichard Henderson 472*ac977170SRichard Henderson /* 473*ac977170SRichard Henderson * Tracking memory accesses performed from helpers requires extra work. 474*ac977170SRichard Henderson * If an instruction is emulated with helpers, we do two things: 475*ac977170SRichard Henderson * (1) copy the CB descriptors, and keep track of it so that they can be 476*ac977170SRichard Henderson * freed later on, and (2) point CPUState.plugin_mem_cbs to the 477*ac977170SRichard Henderson * descriptors, so that we can read them at run-time 478*ac977170SRichard Henderson * (i.e. when the helper executes). 479*ac977170SRichard Henderson * This run-time access is performed from qemu_plugin_vcpu_mem_cb. 480*ac977170SRichard Henderson * 481*ac977170SRichard Henderson * Note that plugin_gen_disable_mem_helpers undoes (2). Since it 482*ac977170SRichard Henderson * is possible that the code we generate after the instruction is 483*ac977170SRichard Henderson * dead, we also add checks before generating tb_exit etc. 484*ac977170SRichard Henderson */ 485*ac977170SRichard Henderson if (!insn->calls_helpers) { 486*ac977170SRichard Henderson return; 487*ac977170SRichard Henderson } 488*ac977170SRichard Henderson 489*ac977170SRichard Henderson cbs[0] = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR]; 490*ac977170SRichard Henderson cbs[1] = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE]; 491*ac977170SRichard Henderson n_cbs = cbs[0]->len + cbs[1]->len; 492*ac977170SRichard Henderson 493*ac977170SRichard Henderson if (n_cbs == 0) { 494*ac977170SRichard Henderson insn->mem_helper = false; 495*ac977170SRichard Henderson return; 496*ac977170SRichard Henderson } 497*ac977170SRichard Henderson insn->mem_helper = true; 498*ac977170SRichard Henderson ptb->mem_helper = true; 499*ac977170SRichard Henderson 500*ac977170SRichard Henderson arr = g_array_sized_new(false, false, 501*ac977170SRichard Henderson sizeof(struct qemu_plugin_dyn_cb), n_cbs); 502*ac977170SRichard Henderson g_array_append_vals(arr, cbs[0]->data, cbs[0]->len); 503*ac977170SRichard Henderson g_array_append_vals(arr, cbs[1]->data, cbs[1]->len); 504*ac977170SRichard Henderson 505*ac977170SRichard Henderson qemu_plugin_add_dyn_cb_arr(arr); 506*ac977170SRichard Henderson 507*ac977170SRichard Henderson tcg_gen_st_ptr(tcg_constant_ptr((intptr_t)arr), tcg_env, 508*ac977170SRichard Henderson offsetof(CPUState, plugin_mem_cbs) - 509*ac977170SRichard Henderson offsetof(ArchCPU, env)); 51038b47b19SEmilio G. Cota } 51138b47b19SEmilio G. Cota 51274bb8accSRichard Henderson static void gen_disable_mem_helper(void) 51338b47b19SEmilio G. Cota { 514a0948bb7SRichard Henderson tcg_gen_st_ptr(tcg_constant_ptr(0), tcg_env, 515a0948bb7SRichard Henderson offsetof(CPUState, plugin_mem_cbs) - 516a0948bb7SRichard Henderson offsetof(ArchCPU, env)); 517a0948bb7SRichard Henderson } 51838b47b19SEmilio G. Cota 51921a3f62fSRichard Henderson static void gen_udata_cb(struct qemu_plugin_dyn_cb *cb) 52021a3f62fSRichard Henderson { 52121a3f62fSRichard Henderson TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 52221a3f62fSRichard Henderson 52321a3f62fSRichard Henderson tcg_gen_ld_i32(cpu_index, tcg_env, 52421a3f62fSRichard Henderson -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 52521a3f62fSRichard Henderson tcg_gen_call2(cb->regular.f.vcpu_udata, cb->regular.info, NULL, 52621a3f62fSRichard Henderson tcgv_i32_temp(cpu_index), 52721a3f62fSRichard Henderson tcgv_ptr_temp(tcg_constant_ptr(cb->userp))); 52821a3f62fSRichard Henderson tcg_temp_free_i32(cpu_index); 52921a3f62fSRichard Henderson } 53021a3f62fSRichard Henderson 53121a3f62fSRichard Henderson static void gen_inline_cb(struct qemu_plugin_dyn_cb *cb) 53221a3f62fSRichard Henderson { 53321a3f62fSRichard Henderson GArray *arr = cb->inline_insn.entry.score->data; 53421a3f62fSRichard Henderson size_t offset = cb->inline_insn.entry.offset; 53521a3f62fSRichard Henderson TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 53621a3f62fSRichard Henderson TCGv_i64 val = tcg_temp_ebb_new_i64(); 53721a3f62fSRichard Henderson TCGv_ptr ptr = tcg_temp_ebb_new_ptr(); 53821a3f62fSRichard Henderson 53921a3f62fSRichard Henderson tcg_gen_ld_i32(cpu_index, tcg_env, 54021a3f62fSRichard Henderson -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 54121a3f62fSRichard Henderson tcg_gen_muli_i32(cpu_index, cpu_index, g_array_get_element_size(arr)); 54221a3f62fSRichard Henderson tcg_gen_ext_i32_ptr(ptr, cpu_index); 54321a3f62fSRichard Henderson tcg_temp_free_i32(cpu_index); 54421a3f62fSRichard Henderson 54521a3f62fSRichard Henderson tcg_gen_addi_ptr(ptr, ptr, (intptr_t)arr->data); 54621a3f62fSRichard Henderson tcg_gen_ld_i64(val, ptr, offset); 54721a3f62fSRichard Henderson tcg_gen_addi_i64(val, val, cb->inline_insn.imm); 54821a3f62fSRichard Henderson tcg_gen_st_i64(val, ptr, offset); 54921a3f62fSRichard Henderson 55021a3f62fSRichard Henderson tcg_temp_free_i64(val); 55121a3f62fSRichard Henderson tcg_temp_free_ptr(ptr); 55221a3f62fSRichard Henderson } 55321a3f62fSRichard Henderson 55438b47b19SEmilio G. Cota /* #define DEBUG_PLUGIN_GEN_OPS */ 55538b47b19SEmilio G. Cota static void pr_ops(void) 55638b47b19SEmilio G. Cota { 55738b47b19SEmilio G. Cota #ifdef DEBUG_PLUGIN_GEN_OPS 55838b47b19SEmilio G. Cota TCGOp *op; 55938b47b19SEmilio G. Cota int i = 0; 56038b47b19SEmilio G. Cota 56138b47b19SEmilio G. Cota QTAILQ_FOREACH(op, &tcg_ctx->ops, link) { 56238b47b19SEmilio G. Cota const char *name = ""; 56338b47b19SEmilio G. Cota const char *type = ""; 56438b47b19SEmilio G. Cota 56538b47b19SEmilio G. Cota if (op->opc == INDEX_op_plugin_cb_start) { 56638b47b19SEmilio G. Cota switch (op->args[0]) { 56738b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_TB: 56838b47b19SEmilio G. Cota name = "tb"; 56938b47b19SEmilio G. Cota break; 57038b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 57138b47b19SEmilio G. Cota name = "insn"; 57238b47b19SEmilio G. Cota break; 57338b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_MEM: 57438b47b19SEmilio G. Cota name = "mem"; 57538b47b19SEmilio G. Cota break; 57638b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 57738b47b19SEmilio G. Cota name = "after insn"; 57838b47b19SEmilio G. Cota break; 57938b47b19SEmilio G. Cota default: 58038b47b19SEmilio G. Cota break; 58138b47b19SEmilio G. Cota } 58238b47b19SEmilio G. Cota switch (op->args[1]) { 58338b47b19SEmilio G. Cota case PLUGIN_GEN_CB_UDATA: 58438b47b19SEmilio G. Cota type = "udata"; 58538b47b19SEmilio G. Cota break; 58638b47b19SEmilio G. Cota case PLUGIN_GEN_CB_INLINE: 58738b47b19SEmilio G. Cota type = "inline"; 58838b47b19SEmilio G. Cota break; 58938b47b19SEmilio G. Cota case PLUGIN_GEN_CB_MEM: 59038b47b19SEmilio G. Cota type = "mem"; 59138b47b19SEmilio G. Cota break; 59238b47b19SEmilio G. Cota case PLUGIN_GEN_ENABLE_MEM_HELPER: 59338b47b19SEmilio G. Cota type = "enable mem helper"; 59438b47b19SEmilio G. Cota break; 59538b47b19SEmilio G. Cota case PLUGIN_GEN_DISABLE_MEM_HELPER: 59638b47b19SEmilio G. Cota type = "disable mem helper"; 59738b47b19SEmilio G. Cota break; 59838b47b19SEmilio G. Cota default: 59938b47b19SEmilio G. Cota break; 60038b47b19SEmilio G. Cota } 60138b47b19SEmilio G. Cota } 60238b47b19SEmilio G. Cota printf("op[%2i]: %s %s %s\n", i, tcg_op_defs[op->opc].name, name, type); 60338b47b19SEmilio G. Cota i++; 60438b47b19SEmilio G. Cota } 60538b47b19SEmilio G. Cota #endif 60638b47b19SEmilio G. Cota } 60738b47b19SEmilio G. Cota 6083fd62e73SEmilio Cota static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb) 60938b47b19SEmilio G. Cota { 610a0948bb7SRichard Henderson TCGOp *op, *next; 611453d50ceSAlex Bennée int insn_idx = -1; 61238b47b19SEmilio G. Cota 61338b47b19SEmilio G. Cota pr_ops(); 614453d50ceSAlex Bennée 615a0948bb7SRichard Henderson /* 616a0948bb7SRichard Henderson * While injecting code, we cannot afford to reuse any ebb temps 617a0948bb7SRichard Henderson * that might be live within the existing opcode stream. 618a0948bb7SRichard Henderson * The simplest solution is to release them all and create new. 619a0948bb7SRichard Henderson */ 620a0948bb7SRichard Henderson memset(tcg_ctx->free_temps, 0, sizeof(tcg_ctx->free_temps)); 621a0948bb7SRichard Henderson 622a0948bb7SRichard Henderson QTAILQ_FOREACH_SAFE(op, &tcg_ctx->ops, link, next) { 623453d50ceSAlex Bennée switch (op->opc) { 624453d50ceSAlex Bennée case INDEX_op_insn_start: 625453d50ceSAlex Bennée insn_idx++; 626453d50ceSAlex Bennée break; 627a0948bb7SRichard Henderson 628a0948bb7SRichard Henderson case INDEX_op_plugin_cb: 629a0948bb7SRichard Henderson { 630a0948bb7SRichard Henderson enum plugin_gen_from from = op->args[0]; 631a0948bb7SRichard Henderson struct qemu_plugin_insn *insn = NULL; 63221a3f62fSRichard Henderson const GArray *cbs; 63321a3f62fSRichard Henderson int i, n; 634a0948bb7SRichard Henderson 635a0948bb7SRichard Henderson if (insn_idx >= 0) { 636a0948bb7SRichard Henderson insn = g_ptr_array_index(plugin_tb->insns, insn_idx); 637a0948bb7SRichard Henderson } 638a0948bb7SRichard Henderson 639a0948bb7SRichard Henderson tcg_ctx->emit_before_op = op; 640a0948bb7SRichard Henderson 641a0948bb7SRichard Henderson switch (from) { 64274bb8accSRichard Henderson case PLUGIN_GEN_AFTER_TB: 64374bb8accSRichard Henderson if (plugin_tb->mem_helper) { 64474bb8accSRichard Henderson gen_disable_mem_helper(); 64574bb8accSRichard Henderson } 64674bb8accSRichard Henderson break; 64774bb8accSRichard Henderson 648a0948bb7SRichard Henderson case PLUGIN_GEN_AFTER_INSN: 649a0948bb7SRichard Henderson assert(insn != NULL); 65074bb8accSRichard Henderson if (insn->mem_helper) { 65174bb8accSRichard Henderson gen_disable_mem_helper(); 65274bb8accSRichard Henderson } 653a0948bb7SRichard Henderson break; 65421a3f62fSRichard Henderson 65521a3f62fSRichard Henderson case PLUGIN_GEN_FROM_TB: 65621a3f62fSRichard Henderson assert(insn == NULL); 65721a3f62fSRichard Henderson 65821a3f62fSRichard Henderson cbs = plugin_tb->cbs[PLUGIN_CB_REGULAR]; 65921a3f62fSRichard Henderson for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) { 66021a3f62fSRichard Henderson struct qemu_plugin_dyn_cb *cb = 66121a3f62fSRichard Henderson &g_array_index(cbs, struct qemu_plugin_dyn_cb, i); 66221a3f62fSRichard Henderson gen_udata_cb(cb); 66321a3f62fSRichard Henderson } 66421a3f62fSRichard Henderson 66521a3f62fSRichard Henderson cbs = plugin_tb->cbs[PLUGIN_CB_INLINE]; 66621a3f62fSRichard Henderson for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) { 66721a3f62fSRichard Henderson struct qemu_plugin_dyn_cb *cb = 66821a3f62fSRichard Henderson &g_array_index(cbs, struct qemu_plugin_dyn_cb, i); 66921a3f62fSRichard Henderson gen_inline_cb(cb); 67021a3f62fSRichard Henderson } 67121a3f62fSRichard Henderson break; 67221a3f62fSRichard Henderson 673*ac977170SRichard Henderson case PLUGIN_GEN_FROM_INSN: 674*ac977170SRichard Henderson assert(insn != NULL); 675*ac977170SRichard Henderson 676*ac977170SRichard Henderson gen_enable_mem_helper(plugin_tb, insn); 677*ac977170SRichard Henderson 678*ac977170SRichard Henderson cbs = insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR]; 679*ac977170SRichard Henderson for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) { 680*ac977170SRichard Henderson struct qemu_plugin_dyn_cb *cb = 681*ac977170SRichard Henderson &g_array_index(cbs, struct qemu_plugin_dyn_cb, i); 682*ac977170SRichard Henderson gen_udata_cb(cb); 683*ac977170SRichard Henderson } 684*ac977170SRichard Henderson 685*ac977170SRichard Henderson cbs = insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE]; 686*ac977170SRichard Henderson for (i = 0, n = (cbs ? cbs->len : 0); i < n; i++) { 687*ac977170SRichard Henderson struct qemu_plugin_dyn_cb *cb = 688*ac977170SRichard Henderson &g_array_index(cbs, struct qemu_plugin_dyn_cb, i); 689*ac977170SRichard Henderson gen_inline_cb(cb); 690*ac977170SRichard Henderson } 691*ac977170SRichard Henderson break; 692*ac977170SRichard Henderson 693a0948bb7SRichard Henderson default: 694a0948bb7SRichard Henderson g_assert_not_reached(); 695a0948bb7SRichard Henderson } 696a0948bb7SRichard Henderson 697a0948bb7SRichard Henderson tcg_ctx->emit_before_op = NULL; 698a0948bb7SRichard Henderson tcg_op_remove(tcg_ctx, op); 699a0948bb7SRichard Henderson break; 700a0948bb7SRichard Henderson } 701a0948bb7SRichard Henderson 702453d50ceSAlex Bennée case INDEX_op_plugin_cb_start: 703453d50ceSAlex Bennée { 70438b47b19SEmilio G. Cota enum plugin_gen_from from = op->args[0]; 70538b47b19SEmilio G. Cota enum plugin_gen_cb type = op->args[1]; 70638b47b19SEmilio G. Cota 707453d50ceSAlex Bennée switch (from) { 708453d50ceSAlex Bennée case PLUGIN_GEN_FROM_MEM: 709453d50ceSAlex Bennée { 710453d50ceSAlex Bennée g_assert(insn_idx >= 0); 711453d50ceSAlex Bennée 712453d50ceSAlex Bennée switch (type) { 713453d50ceSAlex Bennée case PLUGIN_GEN_CB_MEM: 714453d50ceSAlex Bennée plugin_gen_mem_regular(plugin_tb, op, insn_idx); 715453d50ceSAlex Bennée break; 716453d50ceSAlex Bennée case PLUGIN_GEN_CB_INLINE: 717453d50ceSAlex Bennée plugin_gen_mem_inline(plugin_tb, op, insn_idx); 718453d50ceSAlex Bennée break; 719453d50ceSAlex Bennée default: 720453d50ceSAlex Bennée g_assert_not_reached(); 721453d50ceSAlex Bennée } 722453d50ceSAlex Bennée 723453d50ceSAlex Bennée break; 724453d50ceSAlex Bennée } 725453d50ceSAlex Bennée default: 726453d50ceSAlex Bennée g_assert_not_reached(); 727453d50ceSAlex Bennée } 728453d50ceSAlex Bennée break; 729453d50ceSAlex Bennée } 730453d50ceSAlex Bennée default: 731453d50ceSAlex Bennée /* plugins don't care about any other ops */ 732453d50ceSAlex Bennée break; 733453d50ceSAlex Bennée } 73438b47b19SEmilio G. Cota } 73538b47b19SEmilio G. Cota pr_ops(); 73638b47b19SEmilio G. Cota } 73738b47b19SEmilio G. Cota 738b21af662SRichard Henderson bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db, 739b21af662SRichard Henderson bool mem_only) 74038b47b19SEmilio G. Cota { 74138b47b19SEmilio G. Cota bool ret = false; 74238b47b19SEmilio G. Cota 743c0061471SAlex Bennée if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_state->event_mask)) { 7446f15c076SAlex Bennée struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 7456f15c076SAlex Bennée int i; 7466f15c076SAlex Bennée 7476f15c076SAlex Bennée /* reset callbacks */ 7486f15c076SAlex Bennée for (i = 0; i < PLUGIN_N_CB_SUBTYPES; i++) { 7496f15c076SAlex Bennée if (ptb->cbs[i]) { 7506f15c076SAlex Bennée g_array_set_size(ptb->cbs[i], 0); 7516f15c076SAlex Bennée } 7526f15c076SAlex Bennée } 7536f15c076SAlex Bennée ptb->n = 0; 7546f15c076SAlex Bennée 75538b47b19SEmilio G. Cota ret = true; 75638b47b19SEmilio G. Cota 757b21af662SRichard Henderson ptb->vaddr = db->pc_first; 75838b47b19SEmilio G. Cota ptb->vaddr2 = -1; 759b21af662SRichard Henderson ptb->haddr1 = db->host_addr[0]; 76038b47b19SEmilio G. Cota ptb->haddr2 = NULL; 761cfd405eaSAlex Bennée ptb->mem_only = mem_only; 7623fd62e73SEmilio Cota ptb->mem_helper = false; 76338b47b19SEmilio G. Cota 76438b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB); 76538b47b19SEmilio G. Cota } 7666f15c076SAlex Bennée 7676f15c076SAlex Bennée tcg_ctx->plugin_insn = NULL; 7686f15c076SAlex Bennée 76938b47b19SEmilio G. Cota return ret; 77038b47b19SEmilio G. Cota } 77138b47b19SEmilio G. Cota 77238b47b19SEmilio G. Cota void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db) 77338b47b19SEmilio G. Cota { 77438b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 77538b47b19SEmilio G. Cota struct qemu_plugin_insn *pinsn; 77638b47b19SEmilio G. Cota 777357af9beSAlex Bennée pinsn = qemu_plugin_tb_insn_get(ptb, db->pc_next); 77838b47b19SEmilio G. Cota tcg_ctx->plugin_insn = pinsn; 77938b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN); 78038b47b19SEmilio G. Cota 78138b47b19SEmilio G. Cota /* 78238b47b19SEmilio G. Cota * Detect page crossing to get the new host address. 78338b47b19SEmilio G. Cota * Note that we skip this when haddr1 == NULL, e.g. when we're 78438b47b19SEmilio G. Cota * fetching instructions from a region not backed by RAM. 78538b47b19SEmilio G. Cota */ 786b21af662SRichard Henderson if (ptb->haddr1 == NULL) { 787b21af662SRichard Henderson pinsn->haddr = NULL; 788b21af662SRichard Henderson } else if (is_same_page(db, db->pc_next)) { 78938b47b19SEmilio G. Cota pinsn->haddr = ptb->haddr1 + pinsn->vaddr - ptb->vaddr; 79038b47b19SEmilio G. Cota } else { 791b21af662SRichard Henderson if (ptb->vaddr2 == -1) { 792b21af662SRichard Henderson ptb->vaddr2 = TARGET_PAGE_ALIGN(db->pc_first); 793b77af26eSRichard Henderson get_page_addr_code_hostp(cpu_env(cpu), ptb->vaddr2, &ptb->haddr2); 794b21af662SRichard Henderson } 79538b47b19SEmilio G. Cota pinsn->haddr = ptb->haddr2 + pinsn->vaddr - ptb->vaddr2; 79638b47b19SEmilio G. Cota } 79738b47b19SEmilio G. Cota } 79838b47b19SEmilio G. Cota 79938b47b19SEmilio G. Cota void plugin_gen_insn_end(void) 80038b47b19SEmilio G. Cota { 80138b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN); 80238b47b19SEmilio G. Cota } 80338b47b19SEmilio G. Cota 8046f15c076SAlex Bennée /* 8056f15c076SAlex Bennée * There are cases where we never get to finalise a translation - for 8066f15c076SAlex Bennée * example a page fault during translation. As a result we shouldn't 8076f15c076SAlex Bennée * do any clean-up here and make sure things are reset in 8086f15c076SAlex Bennée * plugin_gen_tb_start. 8096f15c076SAlex Bennée */ 810a392277dSMatt Borgerson void plugin_gen_tb_end(CPUState *cpu, size_t num_insns) 81138b47b19SEmilio G. Cota { 81238b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 81338b47b19SEmilio G. Cota 814a392277dSMatt Borgerson /* translator may have removed instructions, update final count */ 815a392277dSMatt Borgerson g_assert(num_insns <= ptb->n); 816a392277dSMatt Borgerson ptb->n = num_insns; 817a392277dSMatt Borgerson 81838b47b19SEmilio G. Cota /* collect instrumentation requests */ 81938b47b19SEmilio G. Cota qemu_plugin_tb_trans_cb(cpu, ptb); 82038b47b19SEmilio G. Cota 82138b47b19SEmilio G. Cota /* inject the instrumentation at the appropriate places */ 82238b47b19SEmilio G. Cota plugin_gen_inject(ptb); 82338b47b19SEmilio G. Cota } 824