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