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 #include "exec/memopidx.h" 16 17 /* 18 * Events that plugins can subscribe to. 19 */ 20 enum qemu_plugin_event { 21 QEMU_PLUGIN_EV_VCPU_INIT, 22 QEMU_PLUGIN_EV_VCPU_EXIT, 23 QEMU_PLUGIN_EV_VCPU_TB_TRANS, 24 QEMU_PLUGIN_EV_VCPU_IDLE, 25 QEMU_PLUGIN_EV_VCPU_RESUME, 26 QEMU_PLUGIN_EV_VCPU_SYSCALL, 27 QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, 28 QEMU_PLUGIN_EV_FLUSH, 29 QEMU_PLUGIN_EV_ATEXIT, 30 QEMU_PLUGIN_EV_MAX, /* total number of plugin events we support */ 31 }; 32 33 /* 34 * Option parsing/processing. 35 * Note that we can load an arbitrary number of plugins. 36 */ 37 struct qemu_plugin_desc; 38 typedef QTAILQ_HEAD(, qemu_plugin_desc) QemuPluginList; 39 40 /* 41 * Construct a qemu_plugin_meminfo_t. 42 */ 43 static inline qemu_plugin_meminfo_t 44 make_plugin_meminfo(MemOpIdx oi, enum qemu_plugin_mem_rw rw) 45 { 46 return oi | (rw << 16); 47 } 48 49 /* 50 * Extract the memory operation direction from a qemu_plugin_meminfo_t. 51 * Other portions may be extracted via get_memop and get_mmuidx. 52 */ 53 static inline enum qemu_plugin_mem_rw 54 get_plugin_meminfo_rw(qemu_plugin_meminfo_t i) 55 { 56 return i >> 16; 57 } 58 59 #ifdef CONFIG_PLUGIN 60 extern QemuOptsList qemu_plugin_opts; 61 62 #define QEMU_PLUGIN_ASSERT(cond) g_assert(cond) 63 64 static inline void qemu_plugin_add_opts(void) 65 { 66 qemu_add_opts(&qemu_plugin_opts); 67 } 68 69 void qemu_plugin_opt_parse(const char *optarg, QemuPluginList *head); 70 int qemu_plugin_load_list(QemuPluginList *head, Error **errp); 71 72 union qemu_plugin_cb_sig { 73 qemu_plugin_simple_cb_t simple; 74 qemu_plugin_udata_cb_t udata; 75 qemu_plugin_vcpu_simple_cb_t vcpu_simple; 76 qemu_plugin_vcpu_udata_cb_t vcpu_udata; 77 qemu_plugin_vcpu_tb_trans_cb_t vcpu_tb_trans; 78 qemu_plugin_vcpu_mem_cb_t vcpu_mem; 79 qemu_plugin_vcpu_syscall_cb_t vcpu_syscall; 80 qemu_plugin_vcpu_syscall_ret_cb_t vcpu_syscall_ret; 81 void *generic; 82 }; 83 84 enum plugin_dyn_cb_type { 85 PLUGIN_CB_INSN, 86 PLUGIN_CB_MEM, 87 PLUGIN_N_CB_TYPES, 88 }; 89 90 enum plugin_dyn_cb_subtype { 91 PLUGIN_CB_REGULAR, 92 PLUGIN_CB_INLINE, 93 PLUGIN_N_CB_SUBTYPES, 94 }; 95 96 /* 97 * A dynamic callback has an insertion point that is determined at run-time. 98 * Usually the insertion point is somewhere in the code cache; think for 99 * instance of a callback to be called upon the execution of a particular TB. 100 */ 101 struct qemu_plugin_dyn_cb { 102 union qemu_plugin_cb_sig f; 103 void *userp; 104 enum plugin_dyn_cb_subtype type; 105 /* @rw applies to mem callbacks only (both regular and inline) */ 106 enum qemu_plugin_mem_rw rw; 107 /* fields specific to each dyn_cb type go here */ 108 union { 109 struct { 110 enum qemu_plugin_op op; 111 uint64_t imm; 112 } inline_insn; 113 }; 114 }; 115 116 /* Internal context for instrumenting an instruction */ 117 struct qemu_plugin_insn { 118 GByteArray *data; 119 uint64_t vaddr; 120 void *haddr; 121 GArray *cbs[PLUGIN_N_CB_TYPES][PLUGIN_N_CB_SUBTYPES]; 122 bool calls_helpers; 123 124 /* if set, the instruction calls helpers that might access guest memory */ 125 bool mem_helper; 126 127 bool mem_only; 128 }; 129 130 /* 131 * qemu_plugin_insn allocate and cleanup functions. We don't expect to 132 * cleanup many of these structures. They are reused for each fresh 133 * translation. 134 */ 135 136 static inline void qemu_plugin_insn_cleanup_fn(gpointer data) 137 { 138 struct qemu_plugin_insn *insn = (struct qemu_plugin_insn *) data; 139 g_byte_array_free(insn->data, true); 140 } 141 142 static inline struct qemu_plugin_insn *qemu_plugin_insn_alloc(void) 143 { 144 int i, j; 145 struct qemu_plugin_insn *insn = g_new0(struct qemu_plugin_insn, 1); 146 insn->data = g_byte_array_sized_new(4); 147 148 for (i = 0; i < PLUGIN_N_CB_TYPES; i++) { 149 for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) { 150 insn->cbs[i][j] = g_array_new(false, false, 151 sizeof(struct qemu_plugin_dyn_cb)); 152 } 153 } 154 return insn; 155 } 156 157 /* Internal context for this TranslationBlock */ 158 struct qemu_plugin_tb { 159 GPtrArray *insns; 160 size_t n; 161 uint64_t vaddr; 162 uint64_t vaddr2; 163 void *haddr1; 164 void *haddr2; 165 bool mem_only; 166 167 /* if set, the TB calls helpers that might access guest memory */ 168 bool mem_helper; 169 170 GArray *cbs[PLUGIN_N_CB_SUBTYPES]; 171 }; 172 173 /** 174 * qemu_plugin_tb_insn_get(): get next plugin record for translation. 175 * @tb: the internal tb context 176 * @pc: address of instruction 177 */ 178 static inline 179 struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb, 180 uint64_t pc) 181 { 182 struct qemu_plugin_insn *insn; 183 int i, j; 184 185 if (unlikely(tb->n == tb->insns->len)) { 186 struct qemu_plugin_insn *new_insn = qemu_plugin_insn_alloc(); 187 g_ptr_array_add(tb->insns, new_insn); 188 } 189 insn = g_ptr_array_index(tb->insns, tb->n++); 190 g_byte_array_set_size(insn->data, 0); 191 insn->calls_helpers = false; 192 insn->mem_helper = false; 193 insn->vaddr = pc; 194 195 for (i = 0; i < PLUGIN_N_CB_TYPES; i++) { 196 for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) { 197 g_array_set_size(insn->cbs[i][j], 0); 198 } 199 } 200 201 return insn; 202 } 203 204 void qemu_plugin_vcpu_init_hook(CPUState *cpu); 205 void qemu_plugin_vcpu_exit_hook(CPUState *cpu); 206 void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb); 207 void qemu_plugin_vcpu_idle_cb(CPUState *cpu); 208 void qemu_plugin_vcpu_resume_cb(CPUState *cpu); 209 void 210 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, 211 uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, 212 uint64_t a6, uint64_t a7, uint64_t a8); 213 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret); 214 215 void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, 216 MemOpIdx oi, enum qemu_plugin_mem_rw rw); 217 218 void qemu_plugin_flush_cb(void); 219 220 void qemu_plugin_atexit_cb(void); 221 222 void qemu_plugin_add_dyn_cb_arr(GArray *arr); 223 224 void qemu_plugin_disable_mem_helpers(CPUState *cpu); 225 226 /** 227 * qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks 228 * 229 * This is a user-mode only helper that ensure we have fully cleared 230 * callbacks from all threads before calling the exit callbacks. This 231 * is so the plugins themselves don't have to jump through hoops to 232 * guard against race conditions. 233 */ 234 void qemu_plugin_user_exit(void); 235 236 /** 237 * qemu_plugin_user_prefork_lock(): take plugin lock before forking 238 * 239 * This is a user-mode only helper to take the internal plugin lock 240 * before a fork event. This is ensure a consistent lock state 241 */ 242 void qemu_plugin_user_prefork_lock(void); 243 244 /** 245 * qemu_plugin_user_postfork(): reset the plugin lock 246 * @is_child: is this thread the child 247 * 248 * This user-mode only helper resets the lock state after a fork so we 249 * can continue using the plugin interface. 250 */ 251 void qemu_plugin_user_postfork(bool is_child); 252 253 #else /* !CONFIG_PLUGIN */ 254 255 #define QEMU_PLUGIN_ASSERT(cond) 256 257 static inline void qemu_plugin_add_opts(void) 258 { } 259 260 static inline void qemu_plugin_opt_parse(const char *optarg, 261 QemuPluginList *head) 262 { 263 error_report("plugin interface not enabled in this build"); 264 exit(1); 265 } 266 267 static inline int qemu_plugin_load_list(QemuPluginList *head, Error **errp) 268 { 269 return 0; 270 } 271 272 static inline void qemu_plugin_vcpu_init_hook(CPUState *cpu) 273 { } 274 275 static inline void qemu_plugin_vcpu_exit_hook(CPUState *cpu) 276 { } 277 278 static inline void qemu_plugin_tb_trans_cb(CPUState *cpu, 279 struct qemu_plugin_tb *tb) 280 { } 281 282 static inline void qemu_plugin_vcpu_idle_cb(CPUState *cpu) 283 { } 284 285 static inline void qemu_plugin_vcpu_resume_cb(CPUState *cpu) 286 { } 287 288 static inline void 289 qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, 290 uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, 291 uint64_t a7, uint64_t a8) 292 { } 293 294 static inline 295 void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret) 296 { } 297 298 static inline void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, 299 MemOpIdx oi, 300 enum qemu_plugin_mem_rw rw) 301 { } 302 303 static inline void qemu_plugin_flush_cb(void) 304 { } 305 306 static inline void qemu_plugin_atexit_cb(void) 307 { } 308 309 static inline 310 void qemu_plugin_add_dyn_cb_arr(GArray *arr) 311 { } 312 313 static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu) 314 { } 315 316 static inline void qemu_plugin_user_exit(void) 317 { } 318 319 static inline void qemu_plugin_user_prefork_lock(void) 320 { } 321 322 static inline void qemu_plugin_user_postfork(bool is_child) 323 { } 324 325 #endif /* !CONFIG_PLUGIN */ 326 327 #endif /* QEMU_PLUGIN_H */ 328