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