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" 46*cac9b0fdSRichard Henderson #include "cpu.h" 4738b47b19SEmilio G. Cota #include "tcg/tcg.h" 4847f7313dSRichard Henderson #include "tcg/tcg-temp-internal.h" 4938b47b19SEmilio G. Cota #include "tcg/tcg-op.h" 5038b47b19SEmilio G. Cota #include "exec/exec-all.h" 5138b47b19SEmilio G. Cota #include "exec/plugin-gen.h" 5238b47b19SEmilio G. Cota #include "exec/translator.h" 53c213ee2dSRichard Henderson #include "exec/helper-proto-common.h" 54d53106c9SRichard Henderson 55d53106c9SRichard Henderson #define HELPER_H "accel/tcg/plugin-helpers.h" 56d53106c9SRichard Henderson #include "exec/helper-info.c.inc" 57d53106c9SRichard Henderson #undef HELPER_H 5838b47b19SEmilio G. Cota 5938b47b19SEmilio G. Cota #ifdef CONFIG_SOFTMMU 6038b47b19SEmilio G. Cota # define CONFIG_SOFTMMU_GATE 1 6138b47b19SEmilio G. Cota #else 6238b47b19SEmilio G. Cota # define CONFIG_SOFTMMU_GATE 0 6338b47b19SEmilio G. Cota #endif 6438b47b19SEmilio G. Cota 6538b47b19SEmilio G. Cota /* 6638b47b19SEmilio G. Cota * plugin_cb_start TCG op args[]: 6738b47b19SEmilio G. Cota * 0: enum plugin_gen_from 6838b47b19SEmilio G. Cota * 1: enum plugin_gen_cb 6938b47b19SEmilio G. Cota * 2: set to 1 for mem callback that is a write, 0 otherwise. 7038b47b19SEmilio G. Cota */ 7138b47b19SEmilio G. Cota 7238b47b19SEmilio G. Cota enum plugin_gen_from { 7338b47b19SEmilio G. Cota PLUGIN_GEN_FROM_TB, 7438b47b19SEmilio G. Cota PLUGIN_GEN_FROM_INSN, 7538b47b19SEmilio G. Cota PLUGIN_GEN_FROM_MEM, 7638b47b19SEmilio G. Cota PLUGIN_GEN_AFTER_INSN, 7738b47b19SEmilio G. Cota PLUGIN_GEN_N_FROMS, 7838b47b19SEmilio G. Cota }; 7938b47b19SEmilio G. Cota 8038b47b19SEmilio G. Cota enum plugin_gen_cb { 8138b47b19SEmilio G. Cota PLUGIN_GEN_CB_UDATA, 8238b47b19SEmilio G. Cota PLUGIN_GEN_CB_INLINE, 8338b47b19SEmilio G. Cota PLUGIN_GEN_CB_MEM, 8438b47b19SEmilio G. Cota PLUGIN_GEN_ENABLE_MEM_HELPER, 8538b47b19SEmilio G. Cota PLUGIN_GEN_DISABLE_MEM_HELPER, 8638b47b19SEmilio G. Cota PLUGIN_GEN_N_CBS, 8738b47b19SEmilio G. Cota }; 8838b47b19SEmilio G. Cota 8938b47b19SEmilio G. Cota /* 9038b47b19SEmilio G. Cota * These helpers are stubs that get dynamically switched out for calls 9138b47b19SEmilio G. Cota * direct to the plugin if they are subscribed to. 9238b47b19SEmilio G. Cota */ 9338b47b19SEmilio G. Cota void HELPER(plugin_vcpu_udata_cb)(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 static void gen_empty_udata_cb(void) 10238b47b19SEmilio G. Cota { 103713f263aSRichard Henderson TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 104713f263aSRichard Henderson TCGv_ptr udata = tcg_temp_ebb_new_ptr(); 10538b47b19SEmilio G. Cota 106713f263aSRichard Henderson tcg_gen_movi_ptr(udata, 0); 10738b47b19SEmilio G. Cota tcg_gen_ld_i32(cpu_index, cpu_env, 10838b47b19SEmilio G. Cota -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 10938b47b19SEmilio G. Cota gen_helper_plugin_vcpu_udata_cb(cpu_index, udata); 11038b47b19SEmilio G. Cota 11138b47b19SEmilio G. Cota tcg_temp_free_ptr(udata); 11238b47b19SEmilio G. Cota tcg_temp_free_i32(cpu_index); 11338b47b19SEmilio G. Cota } 11438b47b19SEmilio G. Cota 11538b47b19SEmilio G. Cota /* 11638b47b19SEmilio G. Cota * For now we only support addi_i64. 11738b47b19SEmilio G. Cota * When we support more ops, we can generate one empty inline cb for each. 11838b47b19SEmilio G. Cota */ 11938b47b19SEmilio G. Cota static void gen_empty_inline_cb(void) 12038b47b19SEmilio G. Cota { 121713f263aSRichard Henderson TCGv_i64 val = tcg_temp_ebb_new_i64(); 122713f263aSRichard Henderson TCGv_ptr ptr = tcg_temp_ebb_new_ptr(); 12338b47b19SEmilio G. Cota 124713f263aSRichard Henderson tcg_gen_movi_ptr(ptr, 0); 12538b47b19SEmilio G. Cota tcg_gen_ld_i64(val, ptr, 0); 12638b47b19SEmilio G. Cota /* pass an immediate != 0 so that it doesn't get optimized away */ 12738b47b19SEmilio G. Cota tcg_gen_addi_i64(val, val, 0xdeadface); 12838b47b19SEmilio G. Cota tcg_gen_st_i64(val, ptr, 0); 12938b47b19SEmilio G. Cota tcg_temp_free_ptr(ptr); 13038b47b19SEmilio G. Cota tcg_temp_free_i64(val); 13138b47b19SEmilio G. Cota } 13238b47b19SEmilio G. Cota 133fcdab382SRichard Henderson static void gen_empty_mem_cb(TCGv_i64 addr, uint32_t info) 13438b47b19SEmilio G. Cota { 135b6d91645SRichard Henderson TCGv_i32 cpu_index = tcg_temp_ebb_new_i32(); 136b6d91645SRichard Henderson TCGv_i32 meminfo = tcg_temp_ebb_new_i32(); 137b6d91645SRichard Henderson TCGv_ptr udata = tcg_temp_ebb_new_ptr(); 138b6d91645SRichard Henderson 139b6d91645SRichard Henderson tcg_gen_movi_i32(meminfo, info); 140b6d91645SRichard Henderson tcg_gen_movi_ptr(udata, 0); 141b6d91645SRichard Henderson tcg_gen_ld_i32(cpu_index, cpu_env, 142b6d91645SRichard Henderson -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 143b6d91645SRichard Henderson 144fcdab382SRichard Henderson gen_helper_plugin_vcpu_mem_cb(cpu_index, meminfo, addr, udata); 145b6d91645SRichard Henderson 146b6d91645SRichard Henderson tcg_temp_free_ptr(udata); 147b6d91645SRichard Henderson tcg_temp_free_i32(meminfo); 148b6d91645SRichard Henderson tcg_temp_free_i32(cpu_index); 14938b47b19SEmilio G. Cota } 15038b47b19SEmilio G. Cota 15138b47b19SEmilio G. Cota /* 15238b47b19SEmilio G. Cota * Share the same function for enable/disable. When enabling, the NULL 15338b47b19SEmilio G. Cota * pointer will be overwritten later. 15438b47b19SEmilio G. Cota */ 15538b47b19SEmilio G. Cota static void gen_empty_mem_helper(void) 15638b47b19SEmilio G. Cota { 157713f263aSRichard Henderson TCGv_ptr ptr = tcg_temp_ebb_new_ptr(); 15838b47b19SEmilio G. Cota 159713f263aSRichard Henderson tcg_gen_movi_ptr(ptr, 0); 16038b47b19SEmilio G. Cota tcg_gen_st_ptr(ptr, cpu_env, offsetof(CPUState, plugin_mem_cbs) - 16138b47b19SEmilio G. Cota offsetof(ArchCPU, env)); 16238b47b19SEmilio G. Cota tcg_temp_free_ptr(ptr); 16338b47b19SEmilio G. Cota } 16438b47b19SEmilio G. Cota 1659a3ee366SRichard Henderson static void gen_plugin_cb_start(enum plugin_gen_from from, 16638b47b19SEmilio G. Cota enum plugin_gen_cb type, unsigned wr) 16738b47b19SEmilio G. Cota { 16838b47b19SEmilio G. Cota tcg_gen_plugin_cb_start(from, type, wr); 16938b47b19SEmilio G. Cota } 17038b47b19SEmilio G. Cota 17138b47b19SEmilio G. Cota static void gen_wrapped(enum plugin_gen_from from, 17238b47b19SEmilio G. Cota enum plugin_gen_cb type, void (*func)(void)) 17338b47b19SEmilio G. Cota { 17438b47b19SEmilio G. Cota gen_plugin_cb_start(from, type, 0); 17538b47b19SEmilio G. Cota func(); 17638b47b19SEmilio G. Cota tcg_gen_plugin_cb_end(); 17738b47b19SEmilio G. Cota } 17838b47b19SEmilio G. Cota 1799a3ee366SRichard Henderson static void plugin_gen_empty_callback(enum plugin_gen_from from) 18038b47b19SEmilio G. Cota { 18138b47b19SEmilio G. Cota switch (from) { 18238b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 18338b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_DISABLE_MEM_HELPER, 18438b47b19SEmilio G. Cota gen_empty_mem_helper); 18538b47b19SEmilio G. Cota break; 18638b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 18738b47b19SEmilio G. Cota /* 18838b47b19SEmilio G. Cota * Note: plugin_gen_inject() relies on ENABLE_MEM_HELPER being 18938b47b19SEmilio G. Cota * the first callback of an instruction 19038b47b19SEmilio G. Cota */ 19138b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_ENABLE_MEM_HELPER, 19238b47b19SEmilio G. Cota gen_empty_mem_helper); 19338b47b19SEmilio G. Cota /* fall through */ 19438b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_TB: 19538b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_CB_UDATA, gen_empty_udata_cb); 19638b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_CB_INLINE, gen_empty_inline_cb); 19738b47b19SEmilio G. Cota break; 19838b47b19SEmilio G. Cota default: 19938b47b19SEmilio G. Cota g_assert_not_reached(); 20038b47b19SEmilio G. Cota } 20138b47b19SEmilio G. Cota } 20238b47b19SEmilio G. Cota 203fcdab382SRichard Henderson void plugin_gen_empty_mem_callback(TCGv_i64 addr, uint32_t info) 20438b47b19SEmilio G. Cota { 20537aff087SRichard Henderson enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info); 20638b47b19SEmilio G. Cota 207f5c346acSRichard Henderson gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_MEM, rw); 208f5c346acSRichard Henderson gen_empty_mem_cb(addr, info); 20938b47b19SEmilio G. Cota tcg_gen_plugin_cb_end(); 21038b47b19SEmilio G. Cota 211f5c346acSRichard Henderson gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, PLUGIN_GEN_CB_INLINE, rw); 212f5c346acSRichard Henderson gen_empty_inline_cb(); 213f5c346acSRichard Henderson tcg_gen_plugin_cb_end(); 21438b47b19SEmilio G. Cota } 21538b47b19SEmilio G. Cota 21638b47b19SEmilio G. Cota static TCGOp *find_op(TCGOp *op, TCGOpcode opc) 21738b47b19SEmilio G. Cota { 21838b47b19SEmilio G. Cota while (op) { 21938b47b19SEmilio G. Cota if (op->opc == opc) { 22038b47b19SEmilio G. Cota return op; 22138b47b19SEmilio G. Cota } 22238b47b19SEmilio G. Cota op = QTAILQ_NEXT(op, link); 22338b47b19SEmilio G. Cota } 22438b47b19SEmilio G. Cota return NULL; 22538b47b19SEmilio G. Cota } 22638b47b19SEmilio G. Cota 22738b47b19SEmilio G. Cota static TCGOp *rm_ops_range(TCGOp *begin, TCGOp *end) 22838b47b19SEmilio G. Cota { 22938b47b19SEmilio G. Cota TCGOp *ret = QTAILQ_NEXT(end, link); 23038b47b19SEmilio G. Cota 23138b47b19SEmilio G. Cota QTAILQ_REMOVE_SEVERAL(&tcg_ctx->ops, begin, end, link); 23238b47b19SEmilio G. Cota return ret; 23338b47b19SEmilio G. Cota } 23438b47b19SEmilio G. Cota 23538b47b19SEmilio G. Cota /* remove all ops until (and including) plugin_cb_end */ 23638b47b19SEmilio G. Cota static TCGOp *rm_ops(TCGOp *op) 23738b47b19SEmilio G. Cota { 23838b47b19SEmilio G. Cota TCGOp *end_op = find_op(op, INDEX_op_plugin_cb_end); 23938b47b19SEmilio G. Cota 24038b47b19SEmilio G. Cota tcg_debug_assert(end_op); 24138b47b19SEmilio G. Cota return rm_ops_range(op, end_op); 24238b47b19SEmilio G. Cota } 24338b47b19SEmilio G. Cota 24438b47b19SEmilio G. Cota static TCGOp *copy_op_nocheck(TCGOp **begin_op, TCGOp *op) 24538b47b19SEmilio G. Cota { 246cb10bc63SRichard Henderson TCGOp *old_op = QTAILQ_NEXT(*begin_op, link); 247cb10bc63SRichard Henderson unsigned nargs = old_op->nargs; 248d4478943SPhilippe Mathieu-Daudé 249cb10bc63SRichard Henderson *begin_op = old_op; 250cb10bc63SRichard Henderson op = tcg_op_insert_after(tcg_ctx, op, old_op->opc, nargs); 251cb10bc63SRichard Henderson memcpy(op->args, old_op->args, sizeof(op->args[0]) * nargs); 252d4478943SPhilippe Mathieu-Daudé 25338b47b19SEmilio G. Cota return op; 25438b47b19SEmilio G. Cota } 25538b47b19SEmilio G. Cota 25638b47b19SEmilio G. Cota static TCGOp *copy_op(TCGOp **begin_op, TCGOp *op, TCGOpcode opc) 25738b47b19SEmilio G. Cota { 25838b47b19SEmilio G. Cota op = copy_op_nocheck(begin_op, op); 25938b47b19SEmilio G. Cota tcg_debug_assert((*begin_op)->opc == opc); 26038b47b19SEmilio G. Cota return op; 26138b47b19SEmilio G. Cota } 26238b47b19SEmilio G. Cota 26338b47b19SEmilio G. Cota static TCGOp *copy_const_ptr(TCGOp **begin_op, TCGOp *op, void *ptr) 26438b47b19SEmilio G. Cota { 26538b47b19SEmilio G. Cota if (UINTPTR_MAX == UINT32_MAX) { 26680c44bbaSRichard Henderson /* mov_i32 */ 26780c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i32); 26880c44bbaSRichard Henderson op->args[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr)); 26938b47b19SEmilio G. Cota } else { 27080c44bbaSRichard Henderson /* mov_i64 */ 27180c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i64); 27280c44bbaSRichard Henderson op->args[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr)); 27338b47b19SEmilio G. Cota } 27438b47b19SEmilio G. Cota return op; 27538b47b19SEmilio G. Cota } 27638b47b19SEmilio G. Cota 27738b47b19SEmilio G. Cota static TCGOp *copy_ld_i64(TCGOp **begin_op, TCGOp *op) 27838b47b19SEmilio G. Cota { 27938b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 28038b47b19SEmilio G. Cota /* 2x ld_i32 */ 28138b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_ld_i32); 28238b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_ld_i32); 28338b47b19SEmilio G. Cota } else { 28438b47b19SEmilio G. Cota /* ld_i64 */ 28538b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_ld_i64); 28638b47b19SEmilio G. Cota } 28738b47b19SEmilio G. Cota return op; 28838b47b19SEmilio G. Cota } 28938b47b19SEmilio G. Cota 29038b47b19SEmilio G. Cota static TCGOp *copy_st_i64(TCGOp **begin_op, TCGOp *op) 29138b47b19SEmilio G. Cota { 29238b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 29338b47b19SEmilio G. Cota /* 2x st_i32 */ 29438b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 29538b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 29638b47b19SEmilio G. Cota } else { 29738b47b19SEmilio G. Cota /* st_i64 */ 29838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i64); 29938b47b19SEmilio G. Cota } 30038b47b19SEmilio G. Cota return op; 30138b47b19SEmilio G. Cota } 30238b47b19SEmilio G. Cota 3030d6e6cb7SAlex Bennée static TCGOp *copy_add_i64(TCGOp **begin_op, TCGOp *op, uint64_t v) 30438b47b19SEmilio G. Cota { 30538b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 30638b47b19SEmilio G. Cota /* all 32-bit backends must implement add2_i32 */ 30738b47b19SEmilio G. Cota g_assert(TCG_TARGET_HAS_add2_i32); 30838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_add2_i32); 3090d6e6cb7SAlex Bennée op->args[4] = tcgv_i32_arg(tcg_constant_i32(v)); 3100d6e6cb7SAlex Bennée op->args[5] = tcgv_i32_arg(tcg_constant_i32(v >> 32)); 31138b47b19SEmilio G. Cota } else { 31238b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_add_i64); 3130d6e6cb7SAlex Bennée op->args[2] = tcgv_i64_arg(tcg_constant_i64(v)); 31438b47b19SEmilio G. Cota } 31538b47b19SEmilio G. Cota return op; 31638b47b19SEmilio G. Cota } 31738b47b19SEmilio G. Cota 31838b47b19SEmilio G. Cota static TCGOp *copy_st_ptr(TCGOp **begin_op, TCGOp *op) 31938b47b19SEmilio G. Cota { 32038b47b19SEmilio G. Cota if (UINTPTR_MAX == UINT32_MAX) { 32138b47b19SEmilio G. Cota /* st_i32 */ 32238b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 32338b47b19SEmilio G. Cota } else { 32438b47b19SEmilio G. Cota /* st_i64 */ 32538b47b19SEmilio G. Cota op = copy_st_i64(begin_op, op); 32638b47b19SEmilio G. Cota } 32738b47b19SEmilio G. Cota return op; 32838b47b19SEmilio G. Cota } 32938b47b19SEmilio G. Cota 33038b47b19SEmilio G. Cota static TCGOp *copy_call(TCGOp **begin_op, TCGOp *op, void *empty_func, 331c7bb41b4SRichard Henderson void *func, int *cb_idx) 33238b47b19SEmilio G. Cota { 33305d019abSRichard Henderson TCGOp *old_op; 33405d019abSRichard Henderson int func_idx; 33505d019abSRichard Henderson 33638b47b19SEmilio G. Cota /* copy all ops until the call */ 33738b47b19SEmilio G. Cota do { 33838b47b19SEmilio G. Cota op = copy_op_nocheck(begin_op, op); 33938b47b19SEmilio G. Cota } while (op->opc != INDEX_op_call); 34038b47b19SEmilio G. Cota 34138b47b19SEmilio G. Cota /* fill in the op call */ 34205d019abSRichard Henderson old_op = *begin_op; 34305d019abSRichard Henderson TCGOP_CALLI(op) = TCGOP_CALLI(old_op); 34405d019abSRichard Henderson TCGOP_CALLO(op) = TCGOP_CALLO(old_op); 34538b47b19SEmilio G. Cota tcg_debug_assert(op->life == 0); 34638b47b19SEmilio G. Cota 34705d019abSRichard Henderson func_idx = TCGOP_CALLO(op) + TCGOP_CALLI(op); 34805d019abSRichard Henderson *cb_idx = func_idx; 34905d019abSRichard Henderson op->args[func_idx] = (uintptr_t)func; 35038b47b19SEmilio G. Cota 35138b47b19SEmilio G. Cota return op; 35238b47b19SEmilio G. Cota } 35338b47b19SEmilio G. Cota 3540d6e6cb7SAlex Bennée /* 3550d6e6cb7SAlex Bennée * When we append/replace ops here we are sensitive to changing patterns of 3560d6e6cb7SAlex Bennée * TCGOps generated by the tcg_gen_FOO calls when we generated the 3570d6e6cb7SAlex Bennée * empty callbacks. This will assert very quickly in a debug build as 3580d6e6cb7SAlex Bennée * we assert the ops we are replacing are the correct ones. 3590d6e6cb7SAlex Bennée */ 36038b47b19SEmilio G. Cota static TCGOp *append_udata_cb(const struct qemu_plugin_dyn_cb *cb, 36138b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *cb_idx) 36238b47b19SEmilio G. Cota { 36338b47b19SEmilio G. Cota /* const_ptr */ 36438b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 36538b47b19SEmilio G. Cota 36638b47b19SEmilio G. Cota /* copy the ld_i32, but note that we only have to copy it once */ 367f266bec8SRichard Henderson if (*cb_idx == -1) { 368f266bec8SRichard Henderson op = copy_op(&begin_op, op, INDEX_op_ld_i32); 369f266bec8SRichard Henderson } else { 37038b47b19SEmilio G. Cota begin_op = QTAILQ_NEXT(begin_op, link); 37138b47b19SEmilio G. Cota tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32); 37238b47b19SEmilio G. Cota } 37338b47b19SEmilio G. Cota 37438b47b19SEmilio G. Cota /* call */ 37538b47b19SEmilio G. Cota op = copy_call(&begin_op, op, HELPER(plugin_vcpu_udata_cb), 376c7bb41b4SRichard Henderson cb->f.vcpu_udata, cb_idx); 37738b47b19SEmilio G. Cota 37838b47b19SEmilio G. Cota return op; 37938b47b19SEmilio G. Cota } 38038b47b19SEmilio G. Cota 38138b47b19SEmilio G. Cota static TCGOp *append_inline_cb(const struct qemu_plugin_dyn_cb *cb, 38238b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, 38338b47b19SEmilio G. Cota int *unused) 38438b47b19SEmilio G. Cota { 38538b47b19SEmilio G. Cota /* const_ptr */ 38638b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 38738b47b19SEmilio G. Cota 38838b47b19SEmilio G. Cota /* ld_i64 */ 38938b47b19SEmilio G. Cota op = copy_ld_i64(&begin_op, op); 39038b47b19SEmilio G. Cota 39138b47b19SEmilio G. Cota /* add_i64 */ 3920d6e6cb7SAlex Bennée op = copy_add_i64(&begin_op, op, cb->inline_insn.imm); 39338b47b19SEmilio G. Cota 39438b47b19SEmilio G. Cota /* st_i64 */ 39538b47b19SEmilio G. Cota op = copy_st_i64(&begin_op, op); 39638b47b19SEmilio G. Cota 39738b47b19SEmilio G. Cota return op; 39838b47b19SEmilio G. Cota } 39938b47b19SEmilio G. Cota 40038b47b19SEmilio G. Cota static TCGOp *append_mem_cb(const struct qemu_plugin_dyn_cb *cb, 40138b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *cb_idx) 40238b47b19SEmilio G. Cota { 40338b47b19SEmilio G. Cota enum plugin_gen_cb type = begin_op->args[1]; 40438b47b19SEmilio G. Cota 40538b47b19SEmilio G. Cota tcg_debug_assert(type == PLUGIN_GEN_CB_MEM); 40638b47b19SEmilio G. Cota 40780c44bbaSRichard Henderson /* const_i32 == mov_i32 ("info", so it remains as is) */ 40880c44bbaSRichard Henderson op = copy_op(&begin_op, op, INDEX_op_mov_i32); 40938b47b19SEmilio G. Cota 41038b47b19SEmilio G. Cota /* const_ptr */ 41138b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 41238b47b19SEmilio G. Cota 41338b47b19SEmilio G. Cota /* copy the ld_i32, but note that we only have to copy it once */ 414f266bec8SRichard Henderson if (*cb_idx == -1) { 415f266bec8SRichard Henderson op = copy_op(&begin_op, op, INDEX_op_ld_i32); 416f266bec8SRichard Henderson } else { 41738b47b19SEmilio G. Cota begin_op = QTAILQ_NEXT(begin_op, link); 41838b47b19SEmilio G. Cota tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32); 41938b47b19SEmilio G. Cota } 42038b47b19SEmilio G. Cota 42138b47b19SEmilio G. Cota if (type == PLUGIN_GEN_CB_MEM) { 42238b47b19SEmilio G. Cota /* call */ 42338b47b19SEmilio G. Cota op = copy_call(&begin_op, op, HELPER(plugin_vcpu_mem_cb), 424c7bb41b4SRichard Henderson cb->f.vcpu_udata, cb_idx); 42538b47b19SEmilio G. Cota } 42638b47b19SEmilio G. Cota 42738b47b19SEmilio G. Cota return op; 42838b47b19SEmilio G. Cota } 42938b47b19SEmilio G. Cota 43038b47b19SEmilio G. Cota typedef TCGOp *(*inject_fn)(const struct qemu_plugin_dyn_cb *cb, 43138b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *intp); 43238b47b19SEmilio G. Cota typedef bool (*op_ok_fn)(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb); 43338b47b19SEmilio G. Cota 43438b47b19SEmilio G. Cota static bool op_ok(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb) 43538b47b19SEmilio G. Cota { 43638b47b19SEmilio G. Cota return true; 43738b47b19SEmilio G. Cota } 43838b47b19SEmilio G. Cota 43938b47b19SEmilio G. Cota static bool op_rw(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb) 44038b47b19SEmilio G. Cota { 44138b47b19SEmilio G. Cota int w; 44238b47b19SEmilio G. Cota 44338b47b19SEmilio G. Cota w = op->args[2]; 44438b47b19SEmilio G. Cota return !!(cb->rw & (w + 1)); 44538b47b19SEmilio G. Cota } 44638b47b19SEmilio G. Cota 4479a3ee366SRichard Henderson static void inject_cb_type(const GArray *cbs, TCGOp *begin_op, 4489a3ee366SRichard Henderson inject_fn inject, op_ok_fn ok) 44938b47b19SEmilio G. Cota { 45038b47b19SEmilio G. Cota TCGOp *end_op; 45138b47b19SEmilio G. Cota TCGOp *op; 45238b47b19SEmilio G. Cota int cb_idx = -1; 45338b47b19SEmilio G. Cota int i; 45438b47b19SEmilio G. Cota 45538b47b19SEmilio G. Cota if (!cbs || cbs->len == 0) { 45638b47b19SEmilio G. Cota rm_ops(begin_op); 45738b47b19SEmilio G. Cota return; 45838b47b19SEmilio G. Cota } 45938b47b19SEmilio G. Cota 46038b47b19SEmilio G. Cota end_op = find_op(begin_op, INDEX_op_plugin_cb_end); 46138b47b19SEmilio G. Cota tcg_debug_assert(end_op); 46238b47b19SEmilio G. Cota 46338b47b19SEmilio G. Cota op = end_op; 46438b47b19SEmilio G. Cota for (i = 0; i < cbs->len; i++) { 46538b47b19SEmilio G. Cota struct qemu_plugin_dyn_cb *cb = 46638b47b19SEmilio G. Cota &g_array_index(cbs, struct qemu_plugin_dyn_cb, i); 46738b47b19SEmilio G. Cota 46838b47b19SEmilio G. Cota if (!ok(begin_op, cb)) { 46938b47b19SEmilio G. Cota continue; 47038b47b19SEmilio G. Cota } 47138b47b19SEmilio G. Cota op = inject(cb, begin_op, op, &cb_idx); 47238b47b19SEmilio G. Cota } 47338b47b19SEmilio G. Cota rm_ops_range(begin_op, end_op); 47438b47b19SEmilio G. Cota } 47538b47b19SEmilio G. Cota 47638b47b19SEmilio G. Cota static void 47738b47b19SEmilio G. Cota inject_udata_cb(const GArray *cbs, TCGOp *begin_op) 47838b47b19SEmilio G. Cota { 47938b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_udata_cb, op_ok); 48038b47b19SEmilio G. Cota } 48138b47b19SEmilio G. Cota 48238b47b19SEmilio G. Cota static void 48338b47b19SEmilio G. Cota inject_inline_cb(const GArray *cbs, TCGOp *begin_op, op_ok_fn ok) 48438b47b19SEmilio G. Cota { 48538b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_inline_cb, ok); 48638b47b19SEmilio G. Cota } 48738b47b19SEmilio G. Cota 48838b47b19SEmilio G. Cota static void 48938b47b19SEmilio G. Cota inject_mem_cb(const GArray *cbs, TCGOp *begin_op) 49038b47b19SEmilio G. Cota { 49138b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_mem_cb, op_rw); 49238b47b19SEmilio G. Cota } 49338b47b19SEmilio G. Cota 49438b47b19SEmilio G. Cota /* we could change the ops in place, but we can reuse more code by copying */ 49538b47b19SEmilio G. Cota static void inject_mem_helper(TCGOp *begin_op, GArray *arr) 49638b47b19SEmilio G. Cota { 49738b47b19SEmilio G. Cota TCGOp *orig_op = begin_op; 49838b47b19SEmilio G. Cota TCGOp *end_op; 49938b47b19SEmilio G. Cota TCGOp *op; 50038b47b19SEmilio G. Cota 50138b47b19SEmilio G. Cota end_op = find_op(begin_op, INDEX_op_plugin_cb_end); 50238b47b19SEmilio G. Cota tcg_debug_assert(end_op); 50338b47b19SEmilio G. Cota 50438b47b19SEmilio G. Cota /* const ptr */ 50538b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, end_op, arr); 50638b47b19SEmilio G. Cota 50738b47b19SEmilio G. Cota /* st_ptr */ 50838b47b19SEmilio G. Cota op = copy_st_ptr(&begin_op, op); 50938b47b19SEmilio G. Cota 51038b47b19SEmilio G. Cota rm_ops_range(orig_op, end_op); 51138b47b19SEmilio G. Cota } 51238b47b19SEmilio G. Cota 51338b47b19SEmilio G. Cota /* 51438b47b19SEmilio G. Cota * Tracking memory accesses performed from helpers requires extra work. 51538b47b19SEmilio G. Cota * If an instruction is emulated with helpers, we do two things: 51638b47b19SEmilio G. Cota * (1) copy the CB descriptors, and keep track of it so that they can be 51738b47b19SEmilio G. Cota * freed later on, and (2) point CPUState.plugin_mem_cbs to the descriptors, so 51838b47b19SEmilio G. Cota * that we can read them at run-time (i.e. when the helper executes). 51938b47b19SEmilio G. Cota * This run-time access is performed from qemu_plugin_vcpu_mem_cb. 52038b47b19SEmilio G. Cota * 52138b47b19SEmilio G. Cota * Note that plugin_gen_disable_mem_helpers undoes (2). Since it 52238b47b19SEmilio G. Cota * is possible that the code we generate after the instruction is 52338b47b19SEmilio G. Cota * dead, we also add checks before generating tb_exit etc. 52438b47b19SEmilio G. Cota */ 5253fd62e73SEmilio Cota static void inject_mem_enable_helper(struct qemu_plugin_tb *ptb, 5263fd62e73SEmilio Cota struct qemu_plugin_insn *plugin_insn, 52738b47b19SEmilio G. Cota TCGOp *begin_op) 52838b47b19SEmilio G. Cota { 52938b47b19SEmilio G. Cota GArray *cbs[2]; 53038b47b19SEmilio G. Cota GArray *arr; 53138b47b19SEmilio G. Cota size_t n_cbs, i; 53238b47b19SEmilio G. Cota 53338b47b19SEmilio G. Cota cbs[0] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR]; 53438b47b19SEmilio G. Cota cbs[1] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE]; 53538b47b19SEmilio G. Cota 53638b47b19SEmilio G. Cota n_cbs = 0; 53738b47b19SEmilio G. Cota for (i = 0; i < ARRAY_SIZE(cbs); i++) { 53838b47b19SEmilio G. Cota n_cbs += cbs[i]->len; 53938b47b19SEmilio G. Cota } 54038b47b19SEmilio G. Cota 54138b47b19SEmilio G. Cota plugin_insn->mem_helper = plugin_insn->calls_helpers && n_cbs; 54238b47b19SEmilio G. Cota if (likely(!plugin_insn->mem_helper)) { 54338b47b19SEmilio G. Cota rm_ops(begin_op); 54438b47b19SEmilio G. Cota return; 54538b47b19SEmilio G. Cota } 5463fd62e73SEmilio Cota ptb->mem_helper = true; 54738b47b19SEmilio G. Cota 54838b47b19SEmilio G. Cota arr = g_array_sized_new(false, false, 54938b47b19SEmilio G. Cota sizeof(struct qemu_plugin_dyn_cb), n_cbs); 55038b47b19SEmilio G. Cota 55138b47b19SEmilio G. Cota for (i = 0; i < ARRAY_SIZE(cbs); i++) { 55238b47b19SEmilio G. Cota g_array_append_vals(arr, cbs[i]->data, cbs[i]->len); 55338b47b19SEmilio G. Cota } 55438b47b19SEmilio G. Cota 55538b47b19SEmilio G. Cota qemu_plugin_add_dyn_cb_arr(arr); 55638b47b19SEmilio G. Cota inject_mem_helper(begin_op, arr); 55738b47b19SEmilio G. Cota } 55838b47b19SEmilio G. Cota 55938b47b19SEmilio G. Cota static void inject_mem_disable_helper(struct qemu_plugin_insn *plugin_insn, 56038b47b19SEmilio G. Cota TCGOp *begin_op) 56138b47b19SEmilio G. Cota { 56238b47b19SEmilio G. Cota if (likely(!plugin_insn->mem_helper)) { 56338b47b19SEmilio G. Cota rm_ops(begin_op); 56438b47b19SEmilio G. Cota return; 56538b47b19SEmilio G. Cota } 56638b47b19SEmilio G. Cota inject_mem_helper(begin_op, NULL); 56738b47b19SEmilio G. Cota } 56838b47b19SEmilio G. Cota 56938b47b19SEmilio G. Cota /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */ 57038b47b19SEmilio G. Cota void plugin_gen_disable_mem_helpers(void) 57138b47b19SEmilio G. Cota { 5723fd62e73SEmilio Cota /* 5733fd62e73SEmilio Cota * We could emit the clearing unconditionally and be done. However, this can 5743fd62e73SEmilio Cota * be wasteful if for instance plugins don't track memory accesses, or if 5753fd62e73SEmilio Cota * most TBs don't use helpers. Instead, emit the clearing iff the TB calls 5763fd62e73SEmilio Cota * helpers that might access guest memory. 5773fd62e73SEmilio Cota * 5783fd62e73SEmilio Cota * Note: we do not reset plugin_tb->mem_helper here; a TB might have several 5793fd62e73SEmilio Cota * exit points, and we want to emit the clearing from all of them. 5803fd62e73SEmilio Cota */ 5813fd62e73SEmilio Cota if (!tcg_ctx->plugin_tb->mem_helper) { 58238b47b19SEmilio G. Cota return; 58338b47b19SEmilio G. Cota } 58440138843SRichard Henderson tcg_gen_st_ptr(tcg_constant_ptr(NULL), cpu_env, 58540138843SRichard Henderson offsetof(CPUState, plugin_mem_cbs) - offsetof(ArchCPU, env)); 58638b47b19SEmilio G. Cota } 58738b47b19SEmilio G. Cota 58838b47b19SEmilio G. Cota static void plugin_gen_tb_udata(const struct qemu_plugin_tb *ptb, 58938b47b19SEmilio G. Cota TCGOp *begin_op) 59038b47b19SEmilio G. Cota { 59138b47b19SEmilio G. Cota inject_udata_cb(ptb->cbs[PLUGIN_CB_REGULAR], begin_op); 59238b47b19SEmilio G. Cota } 59338b47b19SEmilio G. Cota 59438b47b19SEmilio G. Cota static void plugin_gen_tb_inline(const struct qemu_plugin_tb *ptb, 59538b47b19SEmilio G. Cota TCGOp *begin_op) 59638b47b19SEmilio G. Cota { 59738b47b19SEmilio G. Cota inject_inline_cb(ptb->cbs[PLUGIN_CB_INLINE], begin_op, op_ok); 59838b47b19SEmilio G. Cota } 59938b47b19SEmilio G. Cota 60038b47b19SEmilio G. Cota static void plugin_gen_insn_udata(const struct qemu_plugin_tb *ptb, 60138b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 60238b47b19SEmilio G. Cota { 60338b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 60438b47b19SEmilio G. Cota 60538b47b19SEmilio G. Cota inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR], begin_op); 60638b47b19SEmilio G. Cota } 60738b47b19SEmilio G. Cota 60838b47b19SEmilio G. Cota static void plugin_gen_insn_inline(const struct qemu_plugin_tb *ptb, 60938b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 61038b47b19SEmilio G. Cota { 61138b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 61238b47b19SEmilio G. Cota inject_inline_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE], 61338b47b19SEmilio G. Cota begin_op, op_ok); 61438b47b19SEmilio G. Cota } 61538b47b19SEmilio G. Cota 61638b47b19SEmilio G. Cota static void plugin_gen_mem_regular(const struct qemu_plugin_tb *ptb, 61738b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 61838b47b19SEmilio G. Cota { 61938b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 62038b47b19SEmilio G. Cota inject_mem_cb(insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR], begin_op); 62138b47b19SEmilio G. Cota } 62238b47b19SEmilio G. Cota 62338b47b19SEmilio G. Cota static void plugin_gen_mem_inline(const struct qemu_plugin_tb *ptb, 62438b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 62538b47b19SEmilio G. Cota { 62638b47b19SEmilio G. Cota const GArray *cbs; 62738b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 62838b47b19SEmilio G. Cota 62938b47b19SEmilio G. Cota cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE]; 63038b47b19SEmilio G. Cota inject_inline_cb(cbs, begin_op, op_rw); 63138b47b19SEmilio G. Cota } 63238b47b19SEmilio G. Cota 6333fd62e73SEmilio Cota static void plugin_gen_enable_mem_helper(struct qemu_plugin_tb *ptb, 63438b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 63538b47b19SEmilio G. Cota { 63638b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 6373fd62e73SEmilio Cota inject_mem_enable_helper(ptb, insn, begin_op); 63838b47b19SEmilio G. Cota } 63938b47b19SEmilio G. Cota 6403fd62e73SEmilio Cota static void plugin_gen_disable_mem_helper(struct qemu_plugin_tb *ptb, 64138b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 64238b47b19SEmilio G. Cota { 64338b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 64438b47b19SEmilio G. Cota inject_mem_disable_helper(insn, begin_op); 64538b47b19SEmilio G. Cota } 64638b47b19SEmilio G. Cota 64738b47b19SEmilio G. Cota /* #define DEBUG_PLUGIN_GEN_OPS */ 64838b47b19SEmilio G. Cota static void pr_ops(void) 64938b47b19SEmilio G. Cota { 65038b47b19SEmilio G. Cota #ifdef DEBUG_PLUGIN_GEN_OPS 65138b47b19SEmilio G. Cota TCGOp *op; 65238b47b19SEmilio G. Cota int i = 0; 65338b47b19SEmilio G. Cota 65438b47b19SEmilio G. Cota QTAILQ_FOREACH(op, &tcg_ctx->ops, link) { 65538b47b19SEmilio G. Cota const char *name = ""; 65638b47b19SEmilio G. Cota const char *type = ""; 65738b47b19SEmilio G. Cota 65838b47b19SEmilio G. Cota if (op->opc == INDEX_op_plugin_cb_start) { 65938b47b19SEmilio G. Cota switch (op->args[0]) { 66038b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_TB: 66138b47b19SEmilio G. Cota name = "tb"; 66238b47b19SEmilio G. Cota break; 66338b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 66438b47b19SEmilio G. Cota name = "insn"; 66538b47b19SEmilio G. Cota break; 66638b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_MEM: 66738b47b19SEmilio G. Cota name = "mem"; 66838b47b19SEmilio G. Cota break; 66938b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 67038b47b19SEmilio G. Cota name = "after insn"; 67138b47b19SEmilio G. Cota break; 67238b47b19SEmilio G. Cota default: 67338b47b19SEmilio G. Cota break; 67438b47b19SEmilio G. Cota } 67538b47b19SEmilio G. Cota switch (op->args[1]) { 67638b47b19SEmilio G. Cota case PLUGIN_GEN_CB_UDATA: 67738b47b19SEmilio G. Cota type = "udata"; 67838b47b19SEmilio G. Cota break; 67938b47b19SEmilio G. Cota case PLUGIN_GEN_CB_INLINE: 68038b47b19SEmilio G. Cota type = "inline"; 68138b47b19SEmilio G. Cota break; 68238b47b19SEmilio G. Cota case PLUGIN_GEN_CB_MEM: 68338b47b19SEmilio G. Cota type = "mem"; 68438b47b19SEmilio G. Cota break; 68538b47b19SEmilio G. Cota case PLUGIN_GEN_ENABLE_MEM_HELPER: 68638b47b19SEmilio G. Cota type = "enable mem helper"; 68738b47b19SEmilio G. Cota break; 68838b47b19SEmilio G. Cota case PLUGIN_GEN_DISABLE_MEM_HELPER: 68938b47b19SEmilio G. Cota type = "disable mem helper"; 69038b47b19SEmilio G. Cota break; 69138b47b19SEmilio G. Cota default: 69238b47b19SEmilio G. Cota break; 69338b47b19SEmilio G. Cota } 69438b47b19SEmilio G. Cota } 69538b47b19SEmilio G. Cota printf("op[%2i]: %s %s %s\n", i, tcg_op_defs[op->opc].name, name, type); 69638b47b19SEmilio G. Cota i++; 69738b47b19SEmilio G. Cota } 69838b47b19SEmilio G. Cota #endif 69938b47b19SEmilio G. Cota } 70038b47b19SEmilio G. Cota 7013fd62e73SEmilio Cota static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb) 70238b47b19SEmilio G. Cota { 70338b47b19SEmilio G. Cota TCGOp *op; 704453d50ceSAlex Bennée int insn_idx = -1; 70538b47b19SEmilio G. Cota 70638b47b19SEmilio G. Cota pr_ops(); 707453d50ceSAlex Bennée 708453d50ceSAlex Bennée QTAILQ_FOREACH(op, &tcg_ctx->ops, link) { 709453d50ceSAlex Bennée switch (op->opc) { 710453d50ceSAlex Bennée case INDEX_op_insn_start: 711453d50ceSAlex Bennée insn_idx++; 712453d50ceSAlex Bennée break; 713453d50ceSAlex Bennée case INDEX_op_plugin_cb_start: 714453d50ceSAlex Bennée { 71538b47b19SEmilio G. Cota enum plugin_gen_from from = op->args[0]; 71638b47b19SEmilio G. Cota enum plugin_gen_cb type = op->args[1]; 71738b47b19SEmilio G. Cota 718453d50ceSAlex Bennée switch (from) { 719453d50ceSAlex Bennée case PLUGIN_GEN_FROM_TB: 720453d50ceSAlex Bennée { 721453d50ceSAlex Bennée g_assert(insn_idx == -1); 722453d50ceSAlex Bennée 723453d50ceSAlex Bennée switch (type) { 724453d50ceSAlex Bennée case PLUGIN_GEN_CB_UDATA: 725453d50ceSAlex Bennée plugin_gen_tb_udata(plugin_tb, op); 726453d50ceSAlex Bennée break; 727453d50ceSAlex Bennée case PLUGIN_GEN_CB_INLINE: 728453d50ceSAlex Bennée plugin_gen_tb_inline(plugin_tb, op); 729453d50ceSAlex Bennée break; 730453d50ceSAlex Bennée default: 731453d50ceSAlex Bennée g_assert_not_reached(); 73238b47b19SEmilio G. Cota } 733453d50ceSAlex Bennée break; 734453d50ceSAlex Bennée } 735453d50ceSAlex Bennée case PLUGIN_GEN_FROM_INSN: 736453d50ceSAlex Bennée { 737453d50ceSAlex Bennée g_assert(insn_idx >= 0); 738453d50ceSAlex Bennée 739453d50ceSAlex Bennée switch (type) { 740453d50ceSAlex Bennée case PLUGIN_GEN_CB_UDATA: 741453d50ceSAlex Bennée plugin_gen_insn_udata(plugin_tb, op, insn_idx); 742453d50ceSAlex Bennée break; 743453d50ceSAlex Bennée case PLUGIN_GEN_CB_INLINE: 744453d50ceSAlex Bennée plugin_gen_insn_inline(plugin_tb, op, insn_idx); 745453d50ceSAlex Bennée break; 746453d50ceSAlex Bennée case PLUGIN_GEN_ENABLE_MEM_HELPER: 747453d50ceSAlex Bennée plugin_gen_enable_mem_helper(plugin_tb, op, insn_idx); 748453d50ceSAlex Bennée break; 749453d50ceSAlex Bennée default: 750453d50ceSAlex Bennée g_assert_not_reached(); 751453d50ceSAlex Bennée } 752453d50ceSAlex Bennée break; 753453d50ceSAlex Bennée } 754453d50ceSAlex Bennée case PLUGIN_GEN_FROM_MEM: 755453d50ceSAlex Bennée { 756453d50ceSAlex Bennée g_assert(insn_idx >= 0); 757453d50ceSAlex Bennée 758453d50ceSAlex Bennée switch (type) { 759453d50ceSAlex Bennée case PLUGIN_GEN_CB_MEM: 760453d50ceSAlex Bennée plugin_gen_mem_regular(plugin_tb, op, insn_idx); 761453d50ceSAlex Bennée break; 762453d50ceSAlex Bennée case PLUGIN_GEN_CB_INLINE: 763453d50ceSAlex Bennée plugin_gen_mem_inline(plugin_tb, op, insn_idx); 764453d50ceSAlex Bennée break; 765453d50ceSAlex Bennée default: 766453d50ceSAlex Bennée g_assert_not_reached(); 767453d50ceSAlex Bennée } 768453d50ceSAlex Bennée 769453d50ceSAlex Bennée break; 770453d50ceSAlex Bennée } 771453d50ceSAlex Bennée case PLUGIN_GEN_AFTER_INSN: 772453d50ceSAlex Bennée { 773453d50ceSAlex Bennée g_assert(insn_idx >= 0); 774453d50ceSAlex Bennée 775453d50ceSAlex Bennée switch (type) { 776453d50ceSAlex Bennée case PLUGIN_GEN_DISABLE_MEM_HELPER: 777453d50ceSAlex Bennée plugin_gen_disable_mem_helper(plugin_tb, op, insn_idx); 778453d50ceSAlex Bennée break; 779453d50ceSAlex Bennée default: 780453d50ceSAlex Bennée g_assert_not_reached(); 781453d50ceSAlex Bennée } 782453d50ceSAlex Bennée break; 783453d50ceSAlex Bennée } 784453d50ceSAlex Bennée default: 785453d50ceSAlex Bennée g_assert_not_reached(); 786453d50ceSAlex Bennée } 787453d50ceSAlex Bennée break; 788453d50ceSAlex Bennée } 789453d50ceSAlex Bennée default: 790453d50ceSAlex Bennée /* plugins don't care about any other ops */ 791453d50ceSAlex Bennée break; 792453d50ceSAlex Bennée } 79338b47b19SEmilio G. Cota } 79438b47b19SEmilio G. Cota pr_ops(); 79538b47b19SEmilio G. Cota } 79638b47b19SEmilio G. Cota 797b21af662SRichard Henderson bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db, 798b21af662SRichard Henderson bool mem_only) 79938b47b19SEmilio G. Cota { 80038b47b19SEmilio G. Cota bool ret = false; 80138b47b19SEmilio G. Cota 80238b47b19SEmilio G. Cota if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_mask)) { 8036f15c076SAlex Bennée struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 8046f15c076SAlex Bennée int i; 8056f15c076SAlex Bennée 8066f15c076SAlex Bennée /* reset callbacks */ 8076f15c076SAlex Bennée for (i = 0; i < PLUGIN_N_CB_SUBTYPES; i++) { 8086f15c076SAlex Bennée if (ptb->cbs[i]) { 8096f15c076SAlex Bennée g_array_set_size(ptb->cbs[i], 0); 8106f15c076SAlex Bennée } 8116f15c076SAlex Bennée } 8126f15c076SAlex Bennée ptb->n = 0; 8136f15c076SAlex Bennée 81438b47b19SEmilio G. Cota ret = true; 81538b47b19SEmilio G. Cota 816b21af662SRichard Henderson ptb->vaddr = db->pc_first; 81738b47b19SEmilio G. Cota ptb->vaddr2 = -1; 818b21af662SRichard Henderson ptb->haddr1 = db->host_addr[0]; 81938b47b19SEmilio G. Cota ptb->haddr2 = NULL; 820cfd405eaSAlex Bennée ptb->mem_only = mem_only; 8213fd62e73SEmilio Cota ptb->mem_helper = false; 82238b47b19SEmilio G. Cota 82338b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB); 82438b47b19SEmilio G. Cota } 8256f15c076SAlex Bennée 8266f15c076SAlex Bennée tcg_ctx->plugin_insn = NULL; 8276f15c076SAlex Bennée 82838b47b19SEmilio G. Cota return ret; 82938b47b19SEmilio G. Cota } 83038b47b19SEmilio G. Cota 83138b47b19SEmilio G. Cota void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db) 83238b47b19SEmilio G. Cota { 83338b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 83438b47b19SEmilio G. Cota struct qemu_plugin_insn *pinsn; 83538b47b19SEmilio G. Cota 836357af9beSAlex Bennée pinsn = qemu_plugin_tb_insn_get(ptb, db->pc_next); 83738b47b19SEmilio G. Cota tcg_ctx->plugin_insn = pinsn; 83838b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN); 83938b47b19SEmilio G. Cota 84038b47b19SEmilio G. Cota /* 84138b47b19SEmilio G. Cota * Detect page crossing to get the new host address. 84238b47b19SEmilio G. Cota * Note that we skip this when haddr1 == NULL, e.g. when we're 84338b47b19SEmilio G. Cota * fetching instructions from a region not backed by RAM. 84438b47b19SEmilio G. Cota */ 845b21af662SRichard Henderson if (ptb->haddr1 == NULL) { 846b21af662SRichard Henderson pinsn->haddr = NULL; 847b21af662SRichard Henderson } else if (is_same_page(db, db->pc_next)) { 84838b47b19SEmilio G. Cota pinsn->haddr = ptb->haddr1 + pinsn->vaddr - ptb->vaddr; 84938b47b19SEmilio G. Cota } else { 850b21af662SRichard Henderson if (ptb->vaddr2 == -1) { 851b21af662SRichard Henderson ptb->vaddr2 = TARGET_PAGE_ALIGN(db->pc_first); 852b21af662SRichard Henderson get_page_addr_code_hostp(cpu->env_ptr, ptb->vaddr2, &ptb->haddr2); 853b21af662SRichard Henderson } 85438b47b19SEmilio G. Cota pinsn->haddr = ptb->haddr2 + pinsn->vaddr - ptb->vaddr2; 85538b47b19SEmilio G. Cota } 85638b47b19SEmilio G. Cota } 85738b47b19SEmilio G. Cota 85838b47b19SEmilio G. Cota void plugin_gen_insn_end(void) 85938b47b19SEmilio G. Cota { 86038b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN); 86138b47b19SEmilio G. Cota } 86238b47b19SEmilio G. Cota 8636f15c076SAlex Bennée /* 8646f15c076SAlex Bennée * There are cases where we never get to finalise a translation - for 8656f15c076SAlex Bennée * example a page fault during translation. As a result we shouldn't 8666f15c076SAlex Bennée * do any clean-up here and make sure things are reset in 8676f15c076SAlex Bennée * plugin_gen_tb_start. 8686f15c076SAlex Bennée */ 86938b47b19SEmilio G. Cota void plugin_gen_tb_end(CPUState *cpu) 87038b47b19SEmilio G. Cota { 87138b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 87238b47b19SEmilio G. Cota 87338b47b19SEmilio G. Cota /* collect instrumentation requests */ 87438b47b19SEmilio G. Cota qemu_plugin_tb_trans_cb(cpu, ptb); 87538b47b19SEmilio G. Cota 87638b47b19SEmilio G. Cota /* inject the instrumentation at the appropriate places */ 87738b47b19SEmilio G. Cota plugin_gen_inject(ptb); 87838b47b19SEmilio G. Cota } 879