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" 4638b47b19SEmilio G. Cota #include "tcg/tcg.h" 4738b47b19SEmilio G. Cota #include "tcg/tcg-op.h" 4838b47b19SEmilio G. Cota #include "trace/mem.h" 4938b47b19SEmilio G. Cota #include "exec/exec-all.h" 5038b47b19SEmilio G. Cota #include "exec/plugin-gen.h" 5138b47b19SEmilio G. Cota #include "exec/translator.h" 5238b47b19SEmilio G. Cota 5338b47b19SEmilio G. Cota #ifdef CONFIG_SOFTMMU 5438b47b19SEmilio G. Cota # define CONFIG_SOFTMMU_GATE 1 5538b47b19SEmilio G. Cota #else 5638b47b19SEmilio G. Cota # define CONFIG_SOFTMMU_GATE 0 5738b47b19SEmilio G. Cota #endif 5838b47b19SEmilio G. Cota 5938b47b19SEmilio G. Cota /* 6038b47b19SEmilio G. Cota * plugin_cb_start TCG op args[]: 6138b47b19SEmilio G. Cota * 0: enum plugin_gen_from 6238b47b19SEmilio G. Cota * 1: enum plugin_gen_cb 6338b47b19SEmilio G. Cota * 2: set to 1 for mem callback that is a write, 0 otherwise. 6438b47b19SEmilio G. Cota */ 6538b47b19SEmilio G. Cota 6638b47b19SEmilio G. Cota enum plugin_gen_from { 6738b47b19SEmilio G. Cota PLUGIN_GEN_FROM_TB, 6838b47b19SEmilio G. Cota PLUGIN_GEN_FROM_INSN, 6938b47b19SEmilio G. Cota PLUGIN_GEN_FROM_MEM, 7038b47b19SEmilio G. Cota PLUGIN_GEN_AFTER_INSN, 7138b47b19SEmilio G. Cota PLUGIN_GEN_N_FROMS, 7238b47b19SEmilio G. Cota }; 7338b47b19SEmilio G. Cota 7438b47b19SEmilio G. Cota enum plugin_gen_cb { 7538b47b19SEmilio G. Cota PLUGIN_GEN_CB_UDATA, 7638b47b19SEmilio G. Cota PLUGIN_GEN_CB_INLINE, 7738b47b19SEmilio G. Cota PLUGIN_GEN_CB_MEM, 7838b47b19SEmilio G. Cota PLUGIN_GEN_ENABLE_MEM_HELPER, 7938b47b19SEmilio G. Cota PLUGIN_GEN_DISABLE_MEM_HELPER, 8038b47b19SEmilio G. Cota PLUGIN_GEN_N_CBS, 8138b47b19SEmilio G. Cota }; 8238b47b19SEmilio G. Cota 8338b47b19SEmilio G. Cota /* 8438b47b19SEmilio G. Cota * These helpers are stubs that get dynamically switched out for calls 8538b47b19SEmilio G. Cota * direct to the plugin if they are subscribed to. 8638b47b19SEmilio G. Cota */ 8738b47b19SEmilio G. Cota void HELPER(plugin_vcpu_udata_cb)(uint32_t cpu_index, void *udata) 8838b47b19SEmilio G. Cota { } 8938b47b19SEmilio G. Cota 9038b47b19SEmilio G. Cota void HELPER(plugin_vcpu_mem_cb)(unsigned int vcpu_index, 9138b47b19SEmilio G. Cota qemu_plugin_meminfo_t info, uint64_t vaddr, 9238b47b19SEmilio G. Cota void *userdata) 9338b47b19SEmilio G. Cota { } 9438b47b19SEmilio G. Cota 9538b47b19SEmilio G. Cota static void do_gen_mem_cb(TCGv vaddr, uint32_t info) 9638b47b19SEmilio G. Cota { 9738b47b19SEmilio G. Cota TCGv_i32 cpu_index = tcg_temp_new_i32(); 9838b47b19SEmilio G. Cota TCGv_i32 meminfo = tcg_const_i32(info); 9938b47b19SEmilio G. Cota TCGv_i64 vaddr64 = tcg_temp_new_i64(); 10038b47b19SEmilio G. Cota TCGv_ptr udata = tcg_const_ptr(NULL); 10138b47b19SEmilio G. Cota 10238b47b19SEmilio G. Cota tcg_gen_ld_i32(cpu_index, cpu_env, 10338b47b19SEmilio G. Cota -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 10438b47b19SEmilio G. Cota tcg_gen_extu_tl_i64(vaddr64, vaddr); 10538b47b19SEmilio G. Cota 10638b47b19SEmilio G. Cota gen_helper_plugin_vcpu_mem_cb(cpu_index, meminfo, vaddr64, udata); 10738b47b19SEmilio G. Cota 10838b47b19SEmilio G. Cota tcg_temp_free_ptr(udata); 10938b47b19SEmilio G. Cota tcg_temp_free_i64(vaddr64); 11038b47b19SEmilio G. Cota tcg_temp_free_i32(meminfo); 11138b47b19SEmilio G. Cota tcg_temp_free_i32(cpu_index); 11238b47b19SEmilio G. Cota } 11338b47b19SEmilio G. Cota 11438b47b19SEmilio G. Cota static void gen_empty_udata_cb(void) 11538b47b19SEmilio G. Cota { 11638b47b19SEmilio G. Cota TCGv_i32 cpu_index = tcg_temp_new_i32(); 11738b47b19SEmilio G. Cota TCGv_ptr udata = tcg_const_ptr(NULL); /* will be overwritten later */ 11838b47b19SEmilio G. Cota 11938b47b19SEmilio G. Cota tcg_gen_ld_i32(cpu_index, cpu_env, 12038b47b19SEmilio G. Cota -offsetof(ArchCPU, env) + offsetof(CPUState, cpu_index)); 12138b47b19SEmilio G. Cota gen_helper_plugin_vcpu_udata_cb(cpu_index, udata); 12238b47b19SEmilio G. Cota 12338b47b19SEmilio G. Cota tcg_temp_free_ptr(udata); 12438b47b19SEmilio G. Cota tcg_temp_free_i32(cpu_index); 12538b47b19SEmilio G. Cota } 12638b47b19SEmilio G. Cota 12738b47b19SEmilio G. Cota /* 12838b47b19SEmilio G. Cota * For now we only support addi_i64. 12938b47b19SEmilio G. Cota * When we support more ops, we can generate one empty inline cb for each. 13038b47b19SEmilio G. Cota */ 13138b47b19SEmilio G. Cota static void gen_empty_inline_cb(void) 13238b47b19SEmilio G. Cota { 13338b47b19SEmilio G. Cota TCGv_i64 val = tcg_temp_new_i64(); 13438b47b19SEmilio G. Cota TCGv_ptr ptr = tcg_const_ptr(NULL); /* overwritten later */ 13538b47b19SEmilio G. Cota 13638b47b19SEmilio G. Cota tcg_gen_ld_i64(val, ptr, 0); 13738b47b19SEmilio G. Cota /* pass an immediate != 0 so that it doesn't get optimized away */ 13838b47b19SEmilio G. Cota tcg_gen_addi_i64(val, val, 0xdeadface); 13938b47b19SEmilio G. Cota tcg_gen_st_i64(val, ptr, 0); 14038b47b19SEmilio G. Cota tcg_temp_free_ptr(ptr); 14138b47b19SEmilio G. Cota tcg_temp_free_i64(val); 14238b47b19SEmilio G. Cota } 14338b47b19SEmilio G. Cota 14438b47b19SEmilio G. Cota static void gen_empty_mem_cb(TCGv addr, uint32_t info) 14538b47b19SEmilio G. Cota { 14638b47b19SEmilio G. Cota do_gen_mem_cb(addr, info); 14738b47b19SEmilio G. Cota } 14838b47b19SEmilio G. Cota 14938b47b19SEmilio G. Cota /* 15038b47b19SEmilio G. Cota * Share the same function for enable/disable. When enabling, the NULL 15138b47b19SEmilio G. Cota * pointer will be overwritten later. 15238b47b19SEmilio G. Cota */ 15338b47b19SEmilio G. Cota static void gen_empty_mem_helper(void) 15438b47b19SEmilio G. Cota { 15538b47b19SEmilio G. Cota TCGv_ptr ptr; 15638b47b19SEmilio G. Cota 15738b47b19SEmilio G. Cota ptr = tcg_const_ptr(NULL); 15838b47b19SEmilio G. Cota tcg_gen_st_ptr(ptr, cpu_env, offsetof(CPUState, plugin_mem_cbs) - 15938b47b19SEmilio G. Cota offsetof(ArchCPU, env)); 16038b47b19SEmilio G. Cota tcg_temp_free_ptr(ptr); 16138b47b19SEmilio G. Cota } 16238b47b19SEmilio G. Cota 1639a3ee366SRichard Henderson static void gen_plugin_cb_start(enum plugin_gen_from from, 16438b47b19SEmilio G. Cota enum plugin_gen_cb type, unsigned wr) 16538b47b19SEmilio G. Cota { 16638b47b19SEmilio G. Cota TCGOp *op; 16738b47b19SEmilio G. Cota 16838b47b19SEmilio G. Cota tcg_gen_plugin_cb_start(from, type, wr); 16938b47b19SEmilio G. Cota op = tcg_last_op(); 17038b47b19SEmilio G. Cota QSIMPLEQ_INSERT_TAIL(&tcg_ctx->plugin_ops, op, plugin_link); 17138b47b19SEmilio G. Cota } 17238b47b19SEmilio G. Cota 17338b47b19SEmilio G. Cota static void gen_wrapped(enum plugin_gen_from from, 17438b47b19SEmilio G. Cota enum plugin_gen_cb type, void (*func)(void)) 17538b47b19SEmilio G. Cota { 17638b47b19SEmilio G. Cota gen_plugin_cb_start(from, type, 0); 17738b47b19SEmilio G. Cota func(); 17838b47b19SEmilio G. Cota tcg_gen_plugin_cb_end(); 17938b47b19SEmilio G. Cota } 18038b47b19SEmilio G. Cota 1819a3ee366SRichard Henderson static void plugin_gen_empty_callback(enum plugin_gen_from from) 18238b47b19SEmilio G. Cota { 18338b47b19SEmilio G. Cota switch (from) { 18438b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 18538b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_DISABLE_MEM_HELPER, 18638b47b19SEmilio G. Cota gen_empty_mem_helper); 18738b47b19SEmilio G. Cota break; 18838b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 18938b47b19SEmilio G. Cota /* 19038b47b19SEmilio G. Cota * Note: plugin_gen_inject() relies on ENABLE_MEM_HELPER being 19138b47b19SEmilio G. Cota * the first callback of an instruction 19238b47b19SEmilio G. Cota */ 19338b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_ENABLE_MEM_HELPER, 19438b47b19SEmilio G. Cota gen_empty_mem_helper); 19538b47b19SEmilio G. Cota /* fall through */ 19638b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_TB: 19738b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_CB_UDATA, gen_empty_udata_cb); 19838b47b19SEmilio G. Cota gen_wrapped(from, PLUGIN_GEN_CB_INLINE, gen_empty_inline_cb); 19938b47b19SEmilio G. Cota break; 20038b47b19SEmilio G. Cota default: 20138b47b19SEmilio G. Cota g_assert_not_reached(); 20238b47b19SEmilio G. Cota } 20338b47b19SEmilio G. Cota } 20438b47b19SEmilio G. Cota 20538b47b19SEmilio G. Cota union mem_gen_fn { 20638b47b19SEmilio G. Cota void (*mem_fn)(TCGv, uint32_t); 20738b47b19SEmilio G. Cota void (*inline_fn)(void); 20838b47b19SEmilio G. Cota }; 20938b47b19SEmilio G. Cota 21038b47b19SEmilio G. Cota static void gen_mem_wrapped(enum plugin_gen_cb type, 21138b47b19SEmilio G. Cota const union mem_gen_fn *f, TCGv addr, 21238b47b19SEmilio G. Cota uint32_t info, bool is_mem) 21338b47b19SEmilio G. Cota { 21438b47b19SEmilio G. Cota int wr = !!(info & TRACE_MEM_ST); 21538b47b19SEmilio G. Cota 21638b47b19SEmilio G. Cota gen_plugin_cb_start(PLUGIN_GEN_FROM_MEM, type, wr); 21738b47b19SEmilio G. Cota if (is_mem) { 21838b47b19SEmilio G. Cota f->mem_fn(addr, info); 21938b47b19SEmilio G. Cota } else { 22038b47b19SEmilio G. Cota f->inline_fn(); 22138b47b19SEmilio G. Cota } 22238b47b19SEmilio G. Cota tcg_gen_plugin_cb_end(); 22338b47b19SEmilio G. Cota } 22438b47b19SEmilio G. Cota 22538b47b19SEmilio G. Cota void plugin_gen_empty_mem_callback(TCGv addr, uint32_t info) 22638b47b19SEmilio G. Cota { 22738b47b19SEmilio G. Cota union mem_gen_fn fn; 22838b47b19SEmilio G. Cota 22938b47b19SEmilio G. Cota fn.mem_fn = gen_empty_mem_cb; 23038b47b19SEmilio G. Cota gen_mem_wrapped(PLUGIN_GEN_CB_MEM, &fn, addr, info, true); 23138b47b19SEmilio G. Cota 23238b47b19SEmilio G. Cota fn.inline_fn = gen_empty_inline_cb; 23338b47b19SEmilio G. Cota gen_mem_wrapped(PLUGIN_GEN_CB_INLINE, &fn, 0, info, false); 23438b47b19SEmilio G. Cota } 23538b47b19SEmilio G. Cota 23638b47b19SEmilio G. Cota static TCGOp *find_op(TCGOp *op, TCGOpcode opc) 23738b47b19SEmilio G. Cota { 23838b47b19SEmilio G. Cota while (op) { 23938b47b19SEmilio G. Cota if (op->opc == opc) { 24038b47b19SEmilio G. Cota return op; 24138b47b19SEmilio G. Cota } 24238b47b19SEmilio G. Cota op = QTAILQ_NEXT(op, link); 24338b47b19SEmilio G. Cota } 24438b47b19SEmilio G. Cota return NULL; 24538b47b19SEmilio G. Cota } 24638b47b19SEmilio G. Cota 24738b47b19SEmilio G. Cota static TCGOp *rm_ops_range(TCGOp *begin, TCGOp *end) 24838b47b19SEmilio G. Cota { 24938b47b19SEmilio G. Cota TCGOp *ret = QTAILQ_NEXT(end, link); 25038b47b19SEmilio G. Cota 25138b47b19SEmilio G. Cota QTAILQ_REMOVE_SEVERAL(&tcg_ctx->ops, begin, end, link); 25238b47b19SEmilio G. Cota return ret; 25338b47b19SEmilio G. Cota } 25438b47b19SEmilio G. Cota 25538b47b19SEmilio G. Cota /* remove all ops until (and including) plugin_cb_end */ 25638b47b19SEmilio G. Cota static TCGOp *rm_ops(TCGOp *op) 25738b47b19SEmilio G. Cota { 25838b47b19SEmilio G. Cota TCGOp *end_op = find_op(op, INDEX_op_plugin_cb_end); 25938b47b19SEmilio G. Cota 26038b47b19SEmilio G. Cota tcg_debug_assert(end_op); 26138b47b19SEmilio G. Cota return rm_ops_range(op, end_op); 26238b47b19SEmilio G. Cota } 26338b47b19SEmilio G. Cota 26438b47b19SEmilio G. Cota static TCGOp *copy_op_nocheck(TCGOp **begin_op, TCGOp *op) 26538b47b19SEmilio G. Cota { 26638b47b19SEmilio G. Cota *begin_op = QTAILQ_NEXT(*begin_op, link); 26738b47b19SEmilio G. Cota tcg_debug_assert(*begin_op); 26838b47b19SEmilio G. Cota op = tcg_op_insert_after(tcg_ctx, op, (*begin_op)->opc); 26938b47b19SEmilio G. Cota memcpy(op->args, (*begin_op)->args, sizeof(op->args)); 27038b47b19SEmilio G. Cota return op; 27138b47b19SEmilio G. Cota } 27238b47b19SEmilio G. Cota 27338b47b19SEmilio G. Cota static TCGOp *copy_op(TCGOp **begin_op, TCGOp *op, TCGOpcode opc) 27438b47b19SEmilio G. Cota { 27538b47b19SEmilio G. Cota op = copy_op_nocheck(begin_op, op); 27638b47b19SEmilio G. Cota tcg_debug_assert((*begin_op)->opc == opc); 27738b47b19SEmilio G. Cota return op; 27838b47b19SEmilio G. Cota } 27938b47b19SEmilio G. Cota 28038b47b19SEmilio G. Cota static TCGOp *copy_extu_i32_i64(TCGOp **begin_op, TCGOp *op) 28138b47b19SEmilio G. Cota { 28238b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 28338b47b19SEmilio G. Cota /* mov_i32 */ 28438b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_mov_i32); 28580c44bbaSRichard Henderson /* mov_i32 w/ $0 */ 28680c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i32); 28738b47b19SEmilio G. Cota } else { 28838b47b19SEmilio G. Cota /* extu_i32_i64 */ 28938b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_extu_i32_i64); 29038b47b19SEmilio G. Cota } 29138b47b19SEmilio G. Cota return op; 29238b47b19SEmilio G. Cota } 29338b47b19SEmilio G. Cota 29438b47b19SEmilio G. Cota static TCGOp *copy_mov_i64(TCGOp **begin_op, TCGOp *op) 29538b47b19SEmilio G. Cota { 29638b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 29738b47b19SEmilio G. Cota /* 2x mov_i32 */ 29838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_mov_i32); 29938b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_mov_i32); 30038b47b19SEmilio G. Cota } else { 30138b47b19SEmilio G. Cota /* mov_i64 */ 30238b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_mov_i64); 30338b47b19SEmilio G. Cota } 30438b47b19SEmilio G. Cota return op; 30538b47b19SEmilio G. Cota } 30638b47b19SEmilio G. Cota 30738b47b19SEmilio G. Cota static TCGOp *copy_const_ptr(TCGOp **begin_op, TCGOp *op, void *ptr) 30838b47b19SEmilio G. Cota { 30938b47b19SEmilio G. Cota if (UINTPTR_MAX == UINT32_MAX) { 31080c44bbaSRichard Henderson /* mov_i32 */ 31180c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i32); 31280c44bbaSRichard Henderson op->args[1] = tcgv_i32_arg(tcg_constant_i32((uintptr_t)ptr)); 31338b47b19SEmilio G. Cota } else { 31480c44bbaSRichard Henderson /* mov_i64 */ 31580c44bbaSRichard Henderson op = copy_op(begin_op, op, INDEX_op_mov_i64); 31680c44bbaSRichard Henderson op->args[1] = tcgv_i64_arg(tcg_constant_i64((uintptr_t)ptr)); 31738b47b19SEmilio G. Cota } 31838b47b19SEmilio G. Cota return op; 31938b47b19SEmilio G. Cota } 32038b47b19SEmilio G. Cota 32138b47b19SEmilio G. Cota static TCGOp *copy_extu_tl_i64(TCGOp **begin_op, TCGOp *op) 32238b47b19SEmilio G. Cota { 32338b47b19SEmilio G. Cota if (TARGET_LONG_BITS == 32) { 32438b47b19SEmilio G. Cota /* extu_i32_i64 */ 32538b47b19SEmilio G. Cota op = copy_extu_i32_i64(begin_op, op); 32638b47b19SEmilio G. Cota } else { 32738b47b19SEmilio G. Cota /* mov_i64 */ 32838b47b19SEmilio G. Cota op = copy_mov_i64(begin_op, op); 32938b47b19SEmilio G. Cota } 33038b47b19SEmilio G. Cota return op; 33138b47b19SEmilio G. Cota } 33238b47b19SEmilio G. Cota 33338b47b19SEmilio G. Cota static TCGOp *copy_ld_i64(TCGOp **begin_op, TCGOp *op) 33438b47b19SEmilio G. Cota { 33538b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 33638b47b19SEmilio G. Cota /* 2x ld_i32 */ 33738b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_ld_i32); 33838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_ld_i32); 33938b47b19SEmilio G. Cota } else { 34038b47b19SEmilio G. Cota /* ld_i64 */ 34138b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_ld_i64); 34238b47b19SEmilio G. Cota } 34338b47b19SEmilio G. Cota return op; 34438b47b19SEmilio G. Cota } 34538b47b19SEmilio G. Cota 34638b47b19SEmilio G. Cota static TCGOp *copy_st_i64(TCGOp **begin_op, TCGOp *op) 34738b47b19SEmilio G. Cota { 34838b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 34938b47b19SEmilio G. Cota /* 2x st_i32 */ 35038b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 35138b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 35238b47b19SEmilio G. Cota } else { 35338b47b19SEmilio G. Cota /* st_i64 */ 35438b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i64); 35538b47b19SEmilio G. Cota } 35638b47b19SEmilio G. Cota return op; 35738b47b19SEmilio G. Cota } 35838b47b19SEmilio G. Cota 3590d6e6cb7SAlex Bennée static TCGOp *copy_add_i64(TCGOp **begin_op, TCGOp *op, uint64_t v) 36038b47b19SEmilio G. Cota { 36138b47b19SEmilio G. Cota if (TCG_TARGET_REG_BITS == 32) { 36238b47b19SEmilio G. Cota /* all 32-bit backends must implement add2_i32 */ 36338b47b19SEmilio G. Cota g_assert(TCG_TARGET_HAS_add2_i32); 36438b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_add2_i32); 3650d6e6cb7SAlex Bennée op->args[4] = tcgv_i32_arg(tcg_constant_i32(v)); 3660d6e6cb7SAlex Bennée op->args[5] = tcgv_i32_arg(tcg_constant_i32(v >> 32)); 36738b47b19SEmilio G. Cota } else { 36838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_add_i64); 3690d6e6cb7SAlex Bennée op->args[2] = tcgv_i64_arg(tcg_constant_i64(v)); 37038b47b19SEmilio G. Cota } 37138b47b19SEmilio G. Cota return op; 37238b47b19SEmilio G. Cota } 37338b47b19SEmilio G. Cota 37438b47b19SEmilio G. Cota static TCGOp *copy_st_ptr(TCGOp **begin_op, TCGOp *op) 37538b47b19SEmilio G. Cota { 37638b47b19SEmilio G. Cota if (UINTPTR_MAX == UINT32_MAX) { 37738b47b19SEmilio G. Cota /* st_i32 */ 37838b47b19SEmilio G. Cota op = copy_op(begin_op, op, INDEX_op_st_i32); 37938b47b19SEmilio G. Cota } else { 38038b47b19SEmilio G. Cota /* st_i64 */ 38138b47b19SEmilio G. Cota op = copy_st_i64(begin_op, op); 38238b47b19SEmilio G. Cota } 38338b47b19SEmilio G. Cota return op; 38438b47b19SEmilio G. Cota } 38538b47b19SEmilio G. Cota 38638b47b19SEmilio G. Cota static TCGOp *copy_call(TCGOp **begin_op, TCGOp *op, void *empty_func, 387*c7bb41b4SRichard Henderson void *func, int *cb_idx) 38838b47b19SEmilio G. Cota { 38938b47b19SEmilio G. Cota /* copy all ops until the call */ 39038b47b19SEmilio G. Cota do { 39138b47b19SEmilio G. Cota op = copy_op_nocheck(begin_op, op); 39238b47b19SEmilio G. Cota } while (op->opc != INDEX_op_call); 39338b47b19SEmilio G. Cota 39438b47b19SEmilio G. Cota /* fill in the op call */ 39538b47b19SEmilio G. Cota op->param1 = (*begin_op)->param1; 39638b47b19SEmilio G. Cota op->param2 = (*begin_op)->param2; 39738b47b19SEmilio G. Cota tcg_debug_assert(op->life == 0); 39838b47b19SEmilio G. Cota if (*cb_idx == -1) { 39938b47b19SEmilio G. Cota int i; 40038b47b19SEmilio G. Cota 40138b47b19SEmilio G. Cota /* 40238b47b19SEmilio G. Cota * Instead of working out the position of the callback in args[], just 40338b47b19SEmilio G. Cota * look for @empty_func, since it should be a unique pointer. 40438b47b19SEmilio G. Cota */ 40538b47b19SEmilio G. Cota for (i = 0; i < MAX_OPC_PARAM_ARGS; i++) { 40638b47b19SEmilio G. Cota if ((uintptr_t)(*begin_op)->args[i] == (uintptr_t)empty_func) { 40738b47b19SEmilio G. Cota *cb_idx = i; 40838b47b19SEmilio G. Cota break; 40938b47b19SEmilio G. Cota } 41038b47b19SEmilio G. Cota } 41138b47b19SEmilio G. Cota tcg_debug_assert(i < MAX_OPC_PARAM_ARGS); 41238b47b19SEmilio G. Cota } 41338b47b19SEmilio G. Cota op->args[*cb_idx] = (uintptr_t)func; 414*c7bb41b4SRichard Henderson op->args[*cb_idx + 1] = (*begin_op)->args[*cb_idx + 1]; 41538b47b19SEmilio G. Cota 41638b47b19SEmilio G. Cota return op; 41738b47b19SEmilio G. Cota } 41838b47b19SEmilio G. Cota 4190d6e6cb7SAlex Bennée /* 4200d6e6cb7SAlex Bennée * When we append/replace ops here we are sensitive to changing patterns of 4210d6e6cb7SAlex Bennée * TCGOps generated by the tcg_gen_FOO calls when we generated the 4220d6e6cb7SAlex Bennée * empty callbacks. This will assert very quickly in a debug build as 4230d6e6cb7SAlex Bennée * we assert the ops we are replacing are the correct ones. 4240d6e6cb7SAlex Bennée */ 42538b47b19SEmilio G. Cota static TCGOp *append_udata_cb(const struct qemu_plugin_dyn_cb *cb, 42638b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *cb_idx) 42738b47b19SEmilio G. Cota { 42838b47b19SEmilio G. Cota /* const_ptr */ 42938b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 43038b47b19SEmilio G. Cota 43138b47b19SEmilio G. Cota /* copy the ld_i32, but note that we only have to copy it once */ 43238b47b19SEmilio G. Cota begin_op = QTAILQ_NEXT(begin_op, link); 43338b47b19SEmilio G. Cota tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32); 43438b47b19SEmilio G. Cota if (*cb_idx == -1) { 43538b47b19SEmilio G. Cota op = tcg_op_insert_after(tcg_ctx, op, INDEX_op_ld_i32); 43638b47b19SEmilio G. Cota memcpy(op->args, begin_op->args, sizeof(op->args)); 43738b47b19SEmilio G. Cota } 43838b47b19SEmilio G. Cota 43938b47b19SEmilio G. Cota /* call */ 44038b47b19SEmilio G. Cota op = copy_call(&begin_op, op, HELPER(plugin_vcpu_udata_cb), 441*c7bb41b4SRichard Henderson cb->f.vcpu_udata, cb_idx); 44238b47b19SEmilio G. Cota 44338b47b19SEmilio G. Cota return op; 44438b47b19SEmilio G. Cota } 44538b47b19SEmilio G. Cota 44638b47b19SEmilio G. Cota static TCGOp *append_inline_cb(const struct qemu_plugin_dyn_cb *cb, 44738b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, 44838b47b19SEmilio G. Cota int *unused) 44938b47b19SEmilio G. Cota { 45038b47b19SEmilio G. Cota /* const_ptr */ 45138b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 45238b47b19SEmilio G. Cota 45338b47b19SEmilio G. Cota /* ld_i64 */ 45438b47b19SEmilio G. Cota op = copy_ld_i64(&begin_op, op); 45538b47b19SEmilio G. Cota 45638b47b19SEmilio G. Cota /* add_i64 */ 4570d6e6cb7SAlex Bennée op = copy_add_i64(&begin_op, op, cb->inline_insn.imm); 45838b47b19SEmilio G. Cota 45938b47b19SEmilio G. Cota /* st_i64 */ 46038b47b19SEmilio G. Cota op = copy_st_i64(&begin_op, op); 46138b47b19SEmilio G. Cota 46238b47b19SEmilio G. Cota return op; 46338b47b19SEmilio G. Cota } 46438b47b19SEmilio G. Cota 46538b47b19SEmilio G. Cota static TCGOp *append_mem_cb(const struct qemu_plugin_dyn_cb *cb, 46638b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *cb_idx) 46738b47b19SEmilio G. Cota { 46838b47b19SEmilio G. Cota enum plugin_gen_cb type = begin_op->args[1]; 46938b47b19SEmilio G. Cota 47038b47b19SEmilio G. Cota tcg_debug_assert(type == PLUGIN_GEN_CB_MEM); 47138b47b19SEmilio G. Cota 47280c44bbaSRichard Henderson /* const_i32 == mov_i32 ("info", so it remains as is) */ 47380c44bbaSRichard Henderson op = copy_op(&begin_op, op, INDEX_op_mov_i32); 47438b47b19SEmilio G. Cota 47538b47b19SEmilio G. Cota /* const_ptr */ 47638b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, op, cb->userp); 47738b47b19SEmilio G. Cota 47838b47b19SEmilio G. Cota /* copy the ld_i32, but note that we only have to copy it once */ 47938b47b19SEmilio G. Cota begin_op = QTAILQ_NEXT(begin_op, link); 48038b47b19SEmilio G. Cota tcg_debug_assert(begin_op && begin_op->opc == INDEX_op_ld_i32); 48138b47b19SEmilio G. Cota if (*cb_idx == -1) { 48238b47b19SEmilio G. Cota op = tcg_op_insert_after(tcg_ctx, op, INDEX_op_ld_i32); 48338b47b19SEmilio G. Cota memcpy(op->args, begin_op->args, sizeof(op->args)); 48438b47b19SEmilio G. Cota } 48538b47b19SEmilio G. Cota 48638b47b19SEmilio G. Cota /* extu_tl_i64 */ 48738b47b19SEmilio G. Cota op = copy_extu_tl_i64(&begin_op, op); 48838b47b19SEmilio G. Cota 48938b47b19SEmilio G. Cota if (type == PLUGIN_GEN_CB_MEM) { 49038b47b19SEmilio G. Cota /* call */ 49138b47b19SEmilio G. Cota op = copy_call(&begin_op, op, HELPER(plugin_vcpu_mem_cb), 492*c7bb41b4SRichard Henderson cb->f.vcpu_udata, cb_idx); 49338b47b19SEmilio G. Cota } 49438b47b19SEmilio G. Cota 49538b47b19SEmilio G. Cota return op; 49638b47b19SEmilio G. Cota } 49738b47b19SEmilio G. Cota 49838b47b19SEmilio G. Cota typedef TCGOp *(*inject_fn)(const struct qemu_plugin_dyn_cb *cb, 49938b47b19SEmilio G. Cota TCGOp *begin_op, TCGOp *op, int *intp); 50038b47b19SEmilio G. Cota typedef bool (*op_ok_fn)(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb); 50138b47b19SEmilio G. Cota 50238b47b19SEmilio G. Cota static bool op_ok(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb) 50338b47b19SEmilio G. Cota { 50438b47b19SEmilio G. Cota return true; 50538b47b19SEmilio G. Cota } 50638b47b19SEmilio G. Cota 50738b47b19SEmilio G. Cota static bool op_rw(const TCGOp *op, const struct qemu_plugin_dyn_cb *cb) 50838b47b19SEmilio G. Cota { 50938b47b19SEmilio G. Cota int w; 51038b47b19SEmilio G. Cota 51138b47b19SEmilio G. Cota w = op->args[2]; 51238b47b19SEmilio G. Cota return !!(cb->rw & (w + 1)); 51338b47b19SEmilio G. Cota } 51438b47b19SEmilio G. Cota 5159a3ee366SRichard Henderson static void inject_cb_type(const GArray *cbs, TCGOp *begin_op, 5169a3ee366SRichard Henderson inject_fn inject, op_ok_fn ok) 51738b47b19SEmilio G. Cota { 51838b47b19SEmilio G. Cota TCGOp *end_op; 51938b47b19SEmilio G. Cota TCGOp *op; 52038b47b19SEmilio G. Cota int cb_idx = -1; 52138b47b19SEmilio G. Cota int i; 52238b47b19SEmilio G. Cota 52338b47b19SEmilio G. Cota if (!cbs || cbs->len == 0) { 52438b47b19SEmilio G. Cota rm_ops(begin_op); 52538b47b19SEmilio G. Cota return; 52638b47b19SEmilio G. Cota } 52738b47b19SEmilio G. Cota 52838b47b19SEmilio G. Cota end_op = find_op(begin_op, INDEX_op_plugin_cb_end); 52938b47b19SEmilio G. Cota tcg_debug_assert(end_op); 53038b47b19SEmilio G. Cota 53138b47b19SEmilio G. Cota op = end_op; 53238b47b19SEmilio G. Cota for (i = 0; i < cbs->len; i++) { 53338b47b19SEmilio G. Cota struct qemu_plugin_dyn_cb *cb = 53438b47b19SEmilio G. Cota &g_array_index(cbs, struct qemu_plugin_dyn_cb, i); 53538b47b19SEmilio G. Cota 53638b47b19SEmilio G. Cota if (!ok(begin_op, cb)) { 53738b47b19SEmilio G. Cota continue; 53838b47b19SEmilio G. Cota } 53938b47b19SEmilio G. Cota op = inject(cb, begin_op, op, &cb_idx); 54038b47b19SEmilio G. Cota } 54138b47b19SEmilio G. Cota rm_ops_range(begin_op, end_op); 54238b47b19SEmilio G. Cota } 54338b47b19SEmilio G. Cota 54438b47b19SEmilio G. Cota static void 54538b47b19SEmilio G. Cota inject_udata_cb(const GArray *cbs, TCGOp *begin_op) 54638b47b19SEmilio G. Cota { 54738b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_udata_cb, op_ok); 54838b47b19SEmilio G. Cota } 54938b47b19SEmilio G. Cota 55038b47b19SEmilio G. Cota static void 55138b47b19SEmilio G. Cota inject_inline_cb(const GArray *cbs, TCGOp *begin_op, op_ok_fn ok) 55238b47b19SEmilio G. Cota { 55338b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_inline_cb, ok); 55438b47b19SEmilio G. Cota } 55538b47b19SEmilio G. Cota 55638b47b19SEmilio G. Cota static void 55738b47b19SEmilio G. Cota inject_mem_cb(const GArray *cbs, TCGOp *begin_op) 55838b47b19SEmilio G. Cota { 55938b47b19SEmilio G. Cota inject_cb_type(cbs, begin_op, append_mem_cb, op_rw); 56038b47b19SEmilio G. Cota } 56138b47b19SEmilio G. Cota 56238b47b19SEmilio G. Cota /* we could change the ops in place, but we can reuse more code by copying */ 56338b47b19SEmilio G. Cota static void inject_mem_helper(TCGOp *begin_op, GArray *arr) 56438b47b19SEmilio G. Cota { 56538b47b19SEmilio G. Cota TCGOp *orig_op = begin_op; 56638b47b19SEmilio G. Cota TCGOp *end_op; 56738b47b19SEmilio G. Cota TCGOp *op; 56838b47b19SEmilio G. Cota 56938b47b19SEmilio G. Cota end_op = find_op(begin_op, INDEX_op_plugin_cb_end); 57038b47b19SEmilio G. Cota tcg_debug_assert(end_op); 57138b47b19SEmilio G. Cota 57238b47b19SEmilio G. Cota /* const ptr */ 57338b47b19SEmilio G. Cota op = copy_const_ptr(&begin_op, end_op, arr); 57438b47b19SEmilio G. Cota 57538b47b19SEmilio G. Cota /* st_ptr */ 57638b47b19SEmilio G. Cota op = copy_st_ptr(&begin_op, op); 57738b47b19SEmilio G. Cota 57838b47b19SEmilio G. Cota rm_ops_range(orig_op, end_op); 57938b47b19SEmilio G. Cota } 58038b47b19SEmilio G. Cota 58138b47b19SEmilio G. Cota /* 58238b47b19SEmilio G. Cota * Tracking memory accesses performed from helpers requires extra work. 58338b47b19SEmilio G. Cota * If an instruction is emulated with helpers, we do two things: 58438b47b19SEmilio G. Cota * (1) copy the CB descriptors, and keep track of it so that they can be 58538b47b19SEmilio G. Cota * freed later on, and (2) point CPUState.plugin_mem_cbs to the descriptors, so 58638b47b19SEmilio G. Cota * that we can read them at run-time (i.e. when the helper executes). 58738b47b19SEmilio G. Cota * This run-time access is performed from qemu_plugin_vcpu_mem_cb. 58838b47b19SEmilio G. Cota * 58938b47b19SEmilio G. Cota * Note that plugin_gen_disable_mem_helpers undoes (2). Since it 59038b47b19SEmilio G. Cota * is possible that the code we generate after the instruction is 59138b47b19SEmilio G. Cota * dead, we also add checks before generating tb_exit etc. 59238b47b19SEmilio G. Cota */ 59338b47b19SEmilio G. Cota static void inject_mem_enable_helper(struct qemu_plugin_insn *plugin_insn, 59438b47b19SEmilio G. Cota TCGOp *begin_op) 59538b47b19SEmilio G. Cota { 59638b47b19SEmilio G. Cota GArray *cbs[2]; 59738b47b19SEmilio G. Cota GArray *arr; 59838b47b19SEmilio G. Cota size_t n_cbs, i; 59938b47b19SEmilio G. Cota 60038b47b19SEmilio G. Cota cbs[0] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR]; 60138b47b19SEmilio G. Cota cbs[1] = plugin_insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE]; 60238b47b19SEmilio G. Cota 60338b47b19SEmilio G. Cota n_cbs = 0; 60438b47b19SEmilio G. Cota for (i = 0; i < ARRAY_SIZE(cbs); i++) { 60538b47b19SEmilio G. Cota n_cbs += cbs[i]->len; 60638b47b19SEmilio G. Cota } 60738b47b19SEmilio G. Cota 60838b47b19SEmilio G. Cota plugin_insn->mem_helper = plugin_insn->calls_helpers && n_cbs; 60938b47b19SEmilio G. Cota if (likely(!plugin_insn->mem_helper)) { 61038b47b19SEmilio G. Cota rm_ops(begin_op); 61138b47b19SEmilio G. Cota return; 61238b47b19SEmilio G. Cota } 61338b47b19SEmilio G. Cota 61438b47b19SEmilio G. Cota arr = g_array_sized_new(false, false, 61538b47b19SEmilio G. Cota sizeof(struct qemu_plugin_dyn_cb), n_cbs); 61638b47b19SEmilio G. Cota 61738b47b19SEmilio G. Cota for (i = 0; i < ARRAY_SIZE(cbs); i++) { 61838b47b19SEmilio G. Cota g_array_append_vals(arr, cbs[i]->data, cbs[i]->len); 61938b47b19SEmilio G. Cota } 62038b47b19SEmilio G. Cota 62138b47b19SEmilio G. Cota qemu_plugin_add_dyn_cb_arr(arr); 62238b47b19SEmilio G. Cota inject_mem_helper(begin_op, arr); 62338b47b19SEmilio G. Cota } 62438b47b19SEmilio G. Cota 62538b47b19SEmilio G. Cota static void inject_mem_disable_helper(struct qemu_plugin_insn *plugin_insn, 62638b47b19SEmilio G. Cota TCGOp *begin_op) 62738b47b19SEmilio G. Cota { 62838b47b19SEmilio G. Cota if (likely(!plugin_insn->mem_helper)) { 62938b47b19SEmilio G. Cota rm_ops(begin_op); 63038b47b19SEmilio G. Cota return; 63138b47b19SEmilio G. Cota } 63238b47b19SEmilio G. Cota inject_mem_helper(begin_op, NULL); 63338b47b19SEmilio G. Cota } 63438b47b19SEmilio G. Cota 63538b47b19SEmilio G. Cota /* called before finishing a TB with exit_tb, goto_tb or goto_ptr */ 63638b47b19SEmilio G. Cota void plugin_gen_disable_mem_helpers(void) 63738b47b19SEmilio G. Cota { 63838b47b19SEmilio G. Cota TCGv_ptr ptr; 63938b47b19SEmilio G. Cota 64038b47b19SEmilio G. Cota if (likely(tcg_ctx->plugin_insn == NULL || 64138b47b19SEmilio G. Cota !tcg_ctx->plugin_insn->mem_helper)) { 64238b47b19SEmilio G. Cota return; 64338b47b19SEmilio G. Cota } 64438b47b19SEmilio G. Cota ptr = tcg_const_ptr(NULL); 64538b47b19SEmilio G. Cota tcg_gen_st_ptr(ptr, cpu_env, offsetof(CPUState, plugin_mem_cbs) - 64638b47b19SEmilio G. Cota offsetof(ArchCPU, env)); 64738b47b19SEmilio G. Cota tcg_temp_free_ptr(ptr); 64838b47b19SEmilio G. Cota tcg_ctx->plugin_insn->mem_helper = false; 64938b47b19SEmilio G. Cota } 65038b47b19SEmilio G. Cota 65138b47b19SEmilio G. Cota static void plugin_gen_tb_udata(const struct qemu_plugin_tb *ptb, 65238b47b19SEmilio G. Cota TCGOp *begin_op) 65338b47b19SEmilio G. Cota { 65438b47b19SEmilio G. Cota inject_udata_cb(ptb->cbs[PLUGIN_CB_REGULAR], begin_op); 65538b47b19SEmilio G. Cota } 65638b47b19SEmilio G. Cota 65738b47b19SEmilio G. Cota static void plugin_gen_tb_inline(const struct qemu_plugin_tb *ptb, 65838b47b19SEmilio G. Cota TCGOp *begin_op) 65938b47b19SEmilio G. Cota { 66038b47b19SEmilio G. Cota inject_inline_cb(ptb->cbs[PLUGIN_CB_INLINE], begin_op, op_ok); 66138b47b19SEmilio G. Cota } 66238b47b19SEmilio G. Cota 66338b47b19SEmilio G. Cota static void plugin_gen_insn_udata(const struct qemu_plugin_tb *ptb, 66438b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 66538b47b19SEmilio G. Cota { 66638b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 66738b47b19SEmilio G. Cota 66838b47b19SEmilio G. Cota inject_udata_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR], begin_op); 66938b47b19SEmilio G. Cota } 67038b47b19SEmilio G. Cota 67138b47b19SEmilio G. Cota static void plugin_gen_insn_inline(const struct qemu_plugin_tb *ptb, 67238b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 67338b47b19SEmilio G. Cota { 67438b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 67538b47b19SEmilio G. Cota inject_inline_cb(insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE], 67638b47b19SEmilio G. Cota begin_op, op_ok); 67738b47b19SEmilio G. Cota } 67838b47b19SEmilio G. Cota 67938b47b19SEmilio G. Cota static void plugin_gen_mem_regular(const struct qemu_plugin_tb *ptb, 68038b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 68138b47b19SEmilio G. Cota { 68238b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 68338b47b19SEmilio G. Cota inject_mem_cb(insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR], begin_op); 68438b47b19SEmilio G. Cota } 68538b47b19SEmilio G. Cota 68638b47b19SEmilio G. Cota static void plugin_gen_mem_inline(const struct qemu_plugin_tb *ptb, 68738b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 68838b47b19SEmilio G. Cota { 68938b47b19SEmilio G. Cota const GArray *cbs; 69038b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 69138b47b19SEmilio G. Cota 69238b47b19SEmilio G. Cota cbs = insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE]; 69338b47b19SEmilio G. Cota inject_inline_cb(cbs, begin_op, op_rw); 69438b47b19SEmilio G. Cota } 69538b47b19SEmilio G. Cota 69638b47b19SEmilio G. Cota static void plugin_gen_enable_mem_helper(const struct qemu_plugin_tb *ptb, 69738b47b19SEmilio G. Cota TCGOp *begin_op, int insn_idx) 69838b47b19SEmilio G. Cota { 69938b47b19SEmilio G. Cota struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); 70038b47b19SEmilio G. Cota inject_mem_enable_helper(insn, begin_op); 70138b47b19SEmilio G. Cota } 70238b47b19SEmilio G. Cota 70338b47b19SEmilio G. Cota static void plugin_gen_disable_mem_helper(const 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); 70738b47b19SEmilio G. Cota inject_mem_disable_helper(insn, begin_op); 70838b47b19SEmilio G. Cota } 70938b47b19SEmilio G. Cota 71038b47b19SEmilio G. Cota static void plugin_inject_cb(const struct qemu_plugin_tb *ptb, TCGOp *begin_op, 71138b47b19SEmilio G. Cota int insn_idx) 71238b47b19SEmilio G. Cota { 71338b47b19SEmilio G. Cota enum plugin_gen_from from = begin_op->args[0]; 71438b47b19SEmilio G. Cota enum plugin_gen_cb type = begin_op->args[1]; 71538b47b19SEmilio G. Cota 71638b47b19SEmilio G. Cota switch (from) { 71738b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_TB: 71838b47b19SEmilio G. Cota switch (type) { 71938b47b19SEmilio G. Cota case PLUGIN_GEN_CB_UDATA: 72038b47b19SEmilio G. Cota plugin_gen_tb_udata(ptb, begin_op); 72138b47b19SEmilio G. Cota return; 72238b47b19SEmilio G. Cota case PLUGIN_GEN_CB_INLINE: 72338b47b19SEmilio G. Cota plugin_gen_tb_inline(ptb, begin_op); 72438b47b19SEmilio G. Cota return; 72538b47b19SEmilio G. Cota default: 72638b47b19SEmilio G. Cota g_assert_not_reached(); 72738b47b19SEmilio G. Cota } 72838b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 72938b47b19SEmilio G. Cota switch (type) { 73038b47b19SEmilio G. Cota case PLUGIN_GEN_CB_UDATA: 73138b47b19SEmilio G. Cota plugin_gen_insn_udata(ptb, begin_op, insn_idx); 73238b47b19SEmilio G. Cota return; 73338b47b19SEmilio G. Cota case PLUGIN_GEN_CB_INLINE: 73438b47b19SEmilio G. Cota plugin_gen_insn_inline(ptb, begin_op, insn_idx); 73538b47b19SEmilio G. Cota return; 73638b47b19SEmilio G. Cota case PLUGIN_GEN_ENABLE_MEM_HELPER: 73738b47b19SEmilio G. Cota plugin_gen_enable_mem_helper(ptb, begin_op, insn_idx); 73838b47b19SEmilio G. Cota return; 73938b47b19SEmilio G. Cota default: 74038b47b19SEmilio G. Cota g_assert_not_reached(); 74138b47b19SEmilio G. Cota } 74238b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_MEM: 74338b47b19SEmilio G. Cota switch (type) { 74438b47b19SEmilio G. Cota case PLUGIN_GEN_CB_MEM: 74538b47b19SEmilio G. Cota plugin_gen_mem_regular(ptb, begin_op, insn_idx); 74638b47b19SEmilio G. Cota return; 74738b47b19SEmilio G. Cota case PLUGIN_GEN_CB_INLINE: 74838b47b19SEmilio G. Cota plugin_gen_mem_inline(ptb, begin_op, insn_idx); 74938b47b19SEmilio G. Cota return; 75038b47b19SEmilio G. Cota default: 75138b47b19SEmilio G. Cota g_assert_not_reached(); 75238b47b19SEmilio G. Cota } 75338b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 75438b47b19SEmilio G. Cota switch (type) { 75538b47b19SEmilio G. Cota case PLUGIN_GEN_DISABLE_MEM_HELPER: 75638b47b19SEmilio G. Cota plugin_gen_disable_mem_helper(ptb, begin_op, insn_idx); 75738b47b19SEmilio G. Cota return; 75838b47b19SEmilio G. Cota default: 75938b47b19SEmilio G. Cota g_assert_not_reached(); 76038b47b19SEmilio G. Cota } 76138b47b19SEmilio G. Cota default: 76238b47b19SEmilio G. Cota g_assert_not_reached(); 76338b47b19SEmilio G. Cota } 76438b47b19SEmilio G. Cota } 76538b47b19SEmilio G. Cota 76638b47b19SEmilio G. Cota /* #define DEBUG_PLUGIN_GEN_OPS */ 76738b47b19SEmilio G. Cota static void pr_ops(void) 76838b47b19SEmilio G. Cota { 76938b47b19SEmilio G. Cota #ifdef DEBUG_PLUGIN_GEN_OPS 77038b47b19SEmilio G. Cota TCGOp *op; 77138b47b19SEmilio G. Cota int i = 0; 77238b47b19SEmilio G. Cota 77338b47b19SEmilio G. Cota QTAILQ_FOREACH(op, &tcg_ctx->ops, link) { 77438b47b19SEmilio G. Cota const char *name = ""; 77538b47b19SEmilio G. Cota const char *type = ""; 77638b47b19SEmilio G. Cota 77738b47b19SEmilio G. Cota if (op->opc == INDEX_op_plugin_cb_start) { 77838b47b19SEmilio G. Cota switch (op->args[0]) { 77938b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_TB: 78038b47b19SEmilio G. Cota name = "tb"; 78138b47b19SEmilio G. Cota break; 78238b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_INSN: 78338b47b19SEmilio G. Cota name = "insn"; 78438b47b19SEmilio G. Cota break; 78538b47b19SEmilio G. Cota case PLUGIN_GEN_FROM_MEM: 78638b47b19SEmilio G. Cota name = "mem"; 78738b47b19SEmilio G. Cota break; 78838b47b19SEmilio G. Cota case PLUGIN_GEN_AFTER_INSN: 78938b47b19SEmilio G. Cota name = "after insn"; 79038b47b19SEmilio G. Cota break; 79138b47b19SEmilio G. Cota default: 79238b47b19SEmilio G. Cota break; 79338b47b19SEmilio G. Cota } 79438b47b19SEmilio G. Cota switch (op->args[1]) { 79538b47b19SEmilio G. Cota case PLUGIN_GEN_CB_UDATA: 79638b47b19SEmilio G. Cota type = "udata"; 79738b47b19SEmilio G. Cota break; 79838b47b19SEmilio G. Cota case PLUGIN_GEN_CB_INLINE: 79938b47b19SEmilio G. Cota type = "inline"; 80038b47b19SEmilio G. Cota break; 80138b47b19SEmilio G. Cota case PLUGIN_GEN_CB_MEM: 80238b47b19SEmilio G. Cota type = "mem"; 80338b47b19SEmilio G. Cota break; 80438b47b19SEmilio G. Cota case PLUGIN_GEN_ENABLE_MEM_HELPER: 80538b47b19SEmilio G. Cota type = "enable mem helper"; 80638b47b19SEmilio G. Cota break; 80738b47b19SEmilio G. Cota case PLUGIN_GEN_DISABLE_MEM_HELPER: 80838b47b19SEmilio G. Cota type = "disable mem helper"; 80938b47b19SEmilio G. Cota break; 81038b47b19SEmilio G. Cota default: 81138b47b19SEmilio G. Cota break; 81238b47b19SEmilio G. Cota } 81338b47b19SEmilio G. Cota } 81438b47b19SEmilio G. Cota printf("op[%2i]: %s %s %s\n", i, tcg_op_defs[op->opc].name, name, type); 81538b47b19SEmilio G. Cota i++; 81638b47b19SEmilio G. Cota } 81738b47b19SEmilio G. Cota #endif 81838b47b19SEmilio G. Cota } 81938b47b19SEmilio G. Cota 82038b47b19SEmilio G. Cota static void plugin_gen_inject(const struct qemu_plugin_tb *plugin_tb) 82138b47b19SEmilio G. Cota { 82238b47b19SEmilio G. Cota TCGOp *op; 82338b47b19SEmilio G. Cota int insn_idx; 82438b47b19SEmilio G. Cota 82538b47b19SEmilio G. Cota pr_ops(); 82638b47b19SEmilio G. Cota insn_idx = -1; 82738b47b19SEmilio G. Cota QSIMPLEQ_FOREACH(op, &tcg_ctx->plugin_ops, plugin_link) { 82838b47b19SEmilio G. Cota enum plugin_gen_from from = op->args[0]; 82938b47b19SEmilio G. Cota enum plugin_gen_cb type = op->args[1]; 83038b47b19SEmilio G. Cota 83138b47b19SEmilio G. Cota tcg_debug_assert(op->opc == INDEX_op_plugin_cb_start); 83238b47b19SEmilio G. Cota /* ENABLE_MEM_HELPER is the first callback of an instruction */ 83338b47b19SEmilio G. Cota if (from == PLUGIN_GEN_FROM_INSN && 83438b47b19SEmilio G. Cota type == PLUGIN_GEN_ENABLE_MEM_HELPER) { 83538b47b19SEmilio G. Cota insn_idx++; 83638b47b19SEmilio G. Cota } 83738b47b19SEmilio G. Cota plugin_inject_cb(plugin_tb, op, insn_idx); 83838b47b19SEmilio G. Cota } 83938b47b19SEmilio G. Cota pr_ops(); 84038b47b19SEmilio G. Cota } 84138b47b19SEmilio G. Cota 842cfd405eaSAlex Bennée bool plugin_gen_tb_start(CPUState *cpu, const TranslationBlock *tb, bool mem_only) 84338b47b19SEmilio G. Cota { 84438b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 84538b47b19SEmilio G. Cota bool ret = false; 84638b47b19SEmilio G. Cota 84738b47b19SEmilio G. Cota if (test_bit(QEMU_PLUGIN_EV_VCPU_TB_TRANS, cpu->plugin_mask)) { 84838b47b19SEmilio G. Cota ret = true; 84938b47b19SEmilio G. Cota 85038b47b19SEmilio G. Cota QSIMPLEQ_INIT(&tcg_ctx->plugin_ops); 85138b47b19SEmilio G. Cota ptb->vaddr = tb->pc; 85238b47b19SEmilio G. Cota ptb->vaddr2 = -1; 85338b47b19SEmilio G. Cota get_page_addr_code_hostp(cpu->env_ptr, tb->pc, &ptb->haddr1); 85438b47b19SEmilio G. Cota ptb->haddr2 = NULL; 855cfd405eaSAlex Bennée ptb->mem_only = mem_only; 85638b47b19SEmilio G. Cota 85738b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB); 85838b47b19SEmilio G. Cota } 85938b47b19SEmilio G. Cota return ret; 86038b47b19SEmilio G. Cota } 86138b47b19SEmilio G. Cota 86238b47b19SEmilio G. Cota void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db) 86338b47b19SEmilio G. Cota { 86438b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 86538b47b19SEmilio G. Cota struct qemu_plugin_insn *pinsn; 86638b47b19SEmilio G. Cota 86738b47b19SEmilio G. Cota pinsn = qemu_plugin_tb_insn_get(ptb); 86838b47b19SEmilio G. Cota tcg_ctx->plugin_insn = pinsn; 86938b47b19SEmilio G. Cota pinsn->vaddr = db->pc_next; 87038b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN); 87138b47b19SEmilio G. Cota 87238b47b19SEmilio G. Cota /* 87338b47b19SEmilio G. Cota * Detect page crossing to get the new host address. 87438b47b19SEmilio G. Cota * Note that we skip this when haddr1 == NULL, e.g. when we're 87538b47b19SEmilio G. Cota * fetching instructions from a region not backed by RAM. 87638b47b19SEmilio G. Cota */ 87738b47b19SEmilio G. Cota if (likely(ptb->haddr1 != NULL && ptb->vaddr2 == -1) && 87838b47b19SEmilio G. Cota unlikely((db->pc_next & TARGET_PAGE_MASK) != 87938b47b19SEmilio G. Cota (db->pc_first & TARGET_PAGE_MASK))) { 88038b47b19SEmilio G. Cota get_page_addr_code_hostp(cpu->env_ptr, db->pc_next, 88138b47b19SEmilio G. Cota &ptb->haddr2); 88238b47b19SEmilio G. Cota ptb->vaddr2 = db->pc_next; 88338b47b19SEmilio G. Cota } 88438b47b19SEmilio G. Cota if (likely(ptb->vaddr2 == -1)) { 88538b47b19SEmilio G. Cota pinsn->haddr = ptb->haddr1 + pinsn->vaddr - ptb->vaddr; 88638b47b19SEmilio G. Cota } else { 88738b47b19SEmilio G. Cota pinsn->haddr = ptb->haddr2 + pinsn->vaddr - ptb->vaddr2; 88838b47b19SEmilio G. Cota } 88938b47b19SEmilio G. Cota } 89038b47b19SEmilio G. Cota 89138b47b19SEmilio G. Cota void plugin_gen_insn_end(void) 89238b47b19SEmilio G. Cota { 89338b47b19SEmilio G. Cota plugin_gen_empty_callback(PLUGIN_GEN_AFTER_INSN); 89438b47b19SEmilio G. Cota } 89538b47b19SEmilio G. Cota 89638b47b19SEmilio G. Cota void plugin_gen_tb_end(CPUState *cpu) 89738b47b19SEmilio G. Cota { 89838b47b19SEmilio G. Cota struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb; 89938b47b19SEmilio G. Cota int i; 90038b47b19SEmilio G. Cota 90138b47b19SEmilio G. Cota /* collect instrumentation requests */ 90238b47b19SEmilio G. Cota qemu_plugin_tb_trans_cb(cpu, ptb); 90338b47b19SEmilio G. Cota 90438b47b19SEmilio G. Cota /* inject the instrumentation at the appropriate places */ 90538b47b19SEmilio G. Cota plugin_gen_inject(ptb); 90638b47b19SEmilio G. Cota 90738b47b19SEmilio G. Cota /* clean up */ 90838b47b19SEmilio G. Cota for (i = 0; i < PLUGIN_N_CB_SUBTYPES; i++) { 90938b47b19SEmilio G. Cota if (ptb->cbs[i]) { 91038b47b19SEmilio G. Cota g_array_set_size(ptb->cbs[i], 0); 91138b47b19SEmilio G. Cota } 91238b47b19SEmilio G. Cota } 91338b47b19SEmilio G. Cota ptb->n = 0; 91438b47b19SEmilio G. Cota tcg_ctx->plugin_insn = NULL; 91538b47b19SEmilio G. Cota } 916