1 /* 2 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org> 3 * 4 * License: GNU GPL, version 2 or later. 5 * See the COPYING file in the top-level directory. 6 */ 7 #ifndef QEMU_PLUGIN_H 8 #define QEMU_PLUGIN_H 9 10 #include "qemu/config-file.h" 11 #include "qemu/qemu-plugin.h" 12 #include "qemu/error-report.h" 13 #include "qemu/queue.h" 14 #include "qemu/option.h" 15 16 /* 17 * Option parsing/processing. 18 * Note that we can load an arbitrary number of plugins. 19 */ 20 struct qemu_plugin_desc; 21 typedef QTAILQ_HEAD(, qemu_plugin_desc) QemuPluginList; 22 23 #ifdef CONFIG_PLUGIN 24 extern QemuOptsList qemu_plugin_opts; 25 26 static inline void qemu_plugin_add_opts(void) 27 { 28 qemu_add_opts(&qemu_plugin_opts); 29 } 30 31 void qemu_plugin_opt_parse(const char *optarg, QemuPluginList *head); 32 int qemu_plugin_load_list(QemuPluginList *head); 33 #else /* !CONFIG_PLUGIN */ 34 static inline void qemu_plugin_add_opts(void) 35 { } 36 37 static inline void qemu_plugin_opt_parse(const char *optarg, 38 QemuPluginList *head) 39 { 40 error_report("plugin interface not enabled in this build"); 41 exit(1); 42 } 43 44 static inline int qemu_plugin_load_list(QemuPluginList *head) 45 { 46 return 0; 47 } 48 #endif /* !CONFIG_PLUGIN */ 49 50 /* 51 * Events that plugins can subscribe to. 52 */ 53 enum qemu_plugin_event { 54 QEMU_PLUGIN_EV_VCPU_INIT, 55 QEMU_PLUGIN_EV_VCPU_EXIT, 56 QEMU_PLUGIN_EV_VCPU_TB_TRANS, 57 QEMU_PLUGIN_EV_VCPU_IDLE, 58 QEMU_PLUGIN_EV_VCPU_RESUME, 59 QEMU_PLUGIN_EV_VCPU_SYSCALL, 60 QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, 61 QEMU_PLUGIN_EV_FLUSH, 62 QEMU_PLUGIN_EV_ATEXIT, 63 QEMU_PLUGIN_EV_MAX, /* total number of plugin events we support */ 64 }; 65 66 union qemu_plugin_cb_sig { 67 qemu_plugin_simple_cb_t simple; 68 qemu_plugin_udata_cb_t udata; 69 qemu_plugin_vcpu_simple_cb_t vcpu_simple; 70 qemu_plugin_vcpu_udata_cb_t vcpu_udata; 71 qemu_plugin_vcpu_tb_trans_cb_t vcpu_tb_trans; 72 qemu_plugin_vcpu_mem_cb_t vcpu_mem; 73 qemu_plugin_vcpu_syscall_cb_t vcpu_syscall; 74 qemu_plugin_vcpu_syscall_ret_cb_t vcpu_syscall_ret; 75 void *generic; 76 }; 77 78 enum plugin_dyn_cb_type { 79 PLUGIN_CB_INSN, 80 PLUGIN_CB_MEM, 81 PLUGIN_N_CB_TYPES, 82 }; 83 84 enum plugin_dyn_cb_subtype { 85 PLUGIN_CB_REGULAR, 86 PLUGIN_CB_INLINE, 87 PLUGIN_N_CB_SUBTYPES, 88 }; 89 90 /* 91 * A dynamic callback has an insertion point that is determined at run-time. 92 * Usually the insertion point is somewhere in the code cache; think for 93 * instance of a callback to be called upon the execution of a particular TB. 94 */ 95 struct qemu_plugin_dyn_cb { 96 union qemu_plugin_cb_sig f; 97 void *userp; 98 unsigned tcg_flags; 99 enum plugin_dyn_cb_subtype type; 100 /* @rw applies to mem callbacks only (both regular and inline) */ 101 enum qemu_plugin_mem_rw rw; 102 /* fields specific to each dyn_cb type go here */ 103 union { 104 struct { 105 enum qemu_plugin_op op; 106 uint64_t imm; 107 } inline_insn; 108 }; 109 }; 110 111 struct qemu_plugin_insn { 112 GByteArray *data; 113 uint64_t vaddr; 114 void *haddr; 115 GArray *cbs[PLUGIN_N_CB_TYPES][PLUGIN_N_CB_SUBTYPES]; 116 bool calls_helpers; 117 bool mem_helper; 118 }; 119 120 /* 121 * qemu_plugin_insn allocate and cleanup functions. We don't expect to 122 * cleanup many of these structures. They are reused for each fresh 123 * translation. 124 */ 125 126 static inline void qemu_plugin_insn_cleanup_fn(gpointer data) 127 { 128 struct qemu_plugin_insn *insn = (struct qemu_plugin_insn *) data; 129 g_byte_array_free(insn->data, true); 130 } 131 132 static inline struct qemu_plugin_insn *qemu_plugin_insn_alloc(void) 133 { 134 int i, j; 135 struct qemu_plugin_insn *insn = g_new0(struct qemu_plugin_insn, 1); 136 insn->data = g_byte_array_sized_new(4); 137 138 for (i = 0; i < PLUGIN_N_CB_TYPES; i++) { 139 for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) { 140 insn->cbs[i][j] = g_array_new(false, false, 141 sizeof(struct qemu_plugin_dyn_cb)); 142 } 143 } 144 return insn; 145 } 146 147 struct qemu_plugin_tb { 148 GPtrArray *insns; 149 size_t n; 150 uint64_t vaddr; 151 uint64_t vaddr2; 152 void *haddr1; 153 void *haddr2; 154 GArray *cbs[PLUGIN_N_CB_SUBTYPES]; 155 }; 156 157 /** 158 * qemu_plugin_tb_insn_get(): get next plugin record for translation. 159 * 160 */ 161 static inline 162 struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb) 163 { 164 struct qemu_plugin_insn *insn; 165 int i, j; 166 167 if (unlikely(tb->n == tb->insns->len)) { 168 struct qemu_plugin_insn *new_insn = qemu_plugin_insn_alloc(); 169 g_ptr_array_add(tb->insns, new_insn); 170 } 171 insn = g_ptr_array_index(tb->insns, tb->n++); 172 g_byte_array_set_size(insn->data, 0); 173 insn->calls_helpers = false; 174 insn->mem_helper = false; 175 176 for (i = 0; i < PLUGIN_N_CB_TYPES; i++) { 177 for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) { 178 g_array_set_size(insn->cbs[i][j], 0); 179 } 180 } 181 182 return insn; 183 } 184 185 #ifdef CONFIG_PLUGIN 186 187 void qemu_plugin_vcpu_init_hook(CPUState *cpu); 188 void qemu_plugin_vcpu_exit_hook(CPUState *cpu); 189 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb); 190 void qemu_plugin_vcpu_idle_cb(CPUState *cpu); 191 void qemu_plugin_vcpu_resume_cb(CPUState *cpu); 192 void 193 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, 194 uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, 195 uint64_t a6, uint64_t a7, uint64_t a8); 196 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret); 197 198 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, uint32_t meminfo); 199 200 void qemu_plugin_flush_cb(void); 201 202 void qemu_plugin_atexit_cb(void); 203 204 void qemu_plugin_add_dyn_cb_arr(GArray *arr); 205 206 void qemu_plugin_disable_mem_helpers(CPUState *cpu); 207 208 #else /* !CONFIG_PLUGIN */ 209 210 static inline void qemu_plugin_vcpu_init_hook(CPUState *cpu) 211 { } 212 213 static inline void qemu_plugin_vcpu_exit_hook(CPUState *cpu) 214 { } 215 216 static inline void qemu_plugin_tb_trans_cb(CPUState *cpu, 217 struct qemu_plugin_tb *tb) 218 { } 219 220 static inline void qemu_plugin_vcpu_idle_cb(CPUState *cpu) 221 { } 222 223 static inline void qemu_plugin_vcpu_resume_cb(CPUState *cpu) 224 { } 225 226 static inline void 227 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, 228 uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, 229 uint64_t a7, uint64_t a8) 230 { } 231 232 static inline 233 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret) 234 { } 235 236 static inline void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, 237 uint32_t meminfo) 238 { } 239 240 static inline void qemu_plugin_flush_cb(void) 241 { } 242 243 static inline void qemu_plugin_atexit_cb(void) 244 { } 245 246 static inline 247 void qemu_plugin_add_dyn_cb_arr(GArray *arr) 248 { } 249 250 static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu) 251 { } 252 253 #endif /* !CONFIG_PLUGIN */ 254 255 #endif /* QEMU_PLUGIN_H */ 256