| 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 "plugins/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_udata_cb_t udata; |
| 59 | qemu_plugin_vcpu_udata_cb_t vcpu_udata; |
| 60 | qemu_plugin_vcpu_discon_cb_t vcpu_discon; |
| 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 | qemu_plugin_vcpu_syscall_filter_cb_t vcpu_syscall_filter; |
| 66 | void *generic; |
| 67 | }; |
| 68 | |
| 69 | enum plugin_dyn_cb_type { |
| 70 | PLUGIN_CB_REGULAR, |
| 71 | PLUGIN_CB_COND, |
| 72 | PLUGIN_CB_MEM_REGULAR, |
| 73 | PLUGIN_CB_INLINE_ADD_U64, |
| 74 | PLUGIN_CB_INLINE_STORE_U64, |
| 75 | }; |
| 76 | |
| 77 | struct qemu_plugin_regular_cb { |
| 78 | union qemu_plugin_cb_sig f; |
| 79 | TCGHelperInfo *info; |
| 80 | void *userp; |
| 81 | enum qemu_plugin_mem_rw rw; |
| 82 | }; |
| 83 | |
| 84 | struct qemu_plugin_inline_cb { |
| 85 | qemu_plugin_u64 entry; |
| 86 | uint64_t imm; |
| 87 | enum qemu_plugin_mem_rw rw; |
| 88 | }; |
| 89 | |
| 90 | struct qemu_plugin_conditional_cb { |
| 91 | union qemu_plugin_cb_sig f; |
| 92 | TCGHelperInfo *info; |
| 93 | void *userp; |
| 94 | qemu_plugin_u64 entry; |
| 95 | enum qemu_plugin_cond cond; |
| 96 | uint64_t imm; |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * A dynamic callback has an insertion point that is determined at run-time. |
| 101 | * Usually the insertion point is somewhere in the code cache; think for |
| 102 | * instance of a callback to be called upon the execution of a particular TB. |
| 103 | */ |
| 104 | struct qemu_plugin_dyn_cb { |
| 105 | enum plugin_dyn_cb_type type; |
| 106 | union { |
| 107 | struct qemu_plugin_regular_cb regular; |
| 108 | struct qemu_plugin_conditional_cb cond; |
| 109 | struct qemu_plugin_inline_cb inline_insn; |
| 110 | }; |
| 111 | }; |
| 112 | |
| 113 | /* Internal context for instrumenting an instruction */ |
| 114 | struct qemu_plugin_insn { |
| 115 | uint64_t vaddr; |
| 116 | GArray *insn_cbs; |
| 117 | GArray *mem_cbs; |
| 118 | uint8_t len; |
| 119 | bool calls_helpers; |
| 120 | |
| 121 | /* if set, the instruction calls helpers that might access guest memory */ |
| 122 | bool mem_helper; |
| 123 | }; |
| 124 | |
| 125 | /* A scoreboard is an array of values, indexed by vcpu_index */ |
| 126 | struct qemu_plugin_scoreboard { |
| 127 | GArray *data; |
| 128 | QLIST_ENTRY(qemu_plugin_scoreboard) entry; |
| 129 | }; |
| 130 | |
| 131 | /* Internal context for this TranslationBlock */ |
| 132 | struct qemu_plugin_tb { |
| 133 | GPtrArray *insns; |
| 134 | size_t n; |
| 135 | |
| 136 | /* if set, the TB calls helpers that might access guest memory */ |
| 137 | bool mem_helper; |
| 138 | |
| 139 | GArray *cbs; |
| 140 | }; |
| 141 | |
| 142 | /** |
| 143 | * struct CPUPluginState - per-CPU state for plugins |
| 144 | * @event_mask: plugin event bitmap. Modified only via async work. |
| 145 | */ |
| 146 | struct CPUPluginState { |
| 147 | DECLARE_BITMAP(event_mask, QEMU_PLUGIN_EV_MAX); |
| 148 | }; |
| 149 | |
| 150 | /** |
| 151 | * qemu_plugin_create_vcpu_state: allocate plugin state |
| 152 | * |
| 153 | * The returned data must be released with g_free() |
| 154 | * when no longer required. |
| 155 | */ |
| 156 | CPUPluginState *qemu_plugin_create_vcpu_state(void); |
| 157 | |
| 158 | void qemu_plugin_vcpu_init_hook(CPUState *cpu); |
| 159 | void qemu_plugin_vcpu_exit_hook(CPUState *cpu); |
| 160 | void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb); |
| 161 | void qemu_plugin_vcpu_idle_cb(CPUState *cpu); |
| 162 | void qemu_plugin_vcpu_resume_cb(CPUState *cpu); |
| 163 | void qemu_plugin_vcpu_interrupt_cb(CPUState *cpu, uint64_t from); |
| 164 | void qemu_plugin_vcpu_exception_cb(CPUState *cpu, uint64_t from); |
| 165 | void qemu_plugin_vcpu_hostcall_cb(CPUState *cpu, uint64_t from); |
| 166 | void |
| 167 | qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, |
| 168 | uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, |
| 169 | uint64_t a6, uint64_t a7, uint64_t a8); |
| 170 | void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret); |
| 171 | bool |
| 172 | qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1, |
| 173 | uint64_t a2, uint64_t a3, uint64_t a4, |
| 174 | uint64_t a5, uint64_t a6, uint64_t a7, |
| 175 | uint64_t a8, int64_t *sysret); |
| 176 | |
| 177 | void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, |
| 178 | uint64_t value_low, |
| 179 | uint64_t value_high, |
| 180 | MemOpIdx oi, enum qemu_plugin_mem_rw rw); |
| 181 | |
| 182 | void qemu_plugin_flush_cb(void); |
| 183 | |
| 184 | void qemu_plugin_atexit_cb(void); |
| 185 | |
| 186 | void qemu_plugin_add_dyn_cb_arr(GArray *arr); |
| 187 | |
| 188 | static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu) |
| 189 | { |
| 190 | cpu->neg.plugin_mem_cbs = NULL; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks |
| 195 | * |
| 196 | * This is a user-mode only helper that ensure we have fully cleared |
| 197 | * callbacks from all threads before calling the exit callbacks. This |
| 198 | * is so the plugins themselves don't have to jump through hoops to |
| 199 | * guard against race conditions. |
| 200 | */ |
| 201 | void qemu_plugin_user_exit(void); |
| 202 | |
| 203 | /** |
| 204 | * qemu_plugin_user_prefork_lock(): take plugin lock before forking |
| 205 | * |
| 206 | * This is a user-mode only helper to take the internal plugin lock |
| 207 | * before a fork event. This is ensure a consistent lock state |
| 208 | */ |
| 209 | void qemu_plugin_user_prefork_lock(void); |
| 210 | |
| 211 | /** |
| 212 | * qemu_plugin_user_postfork(): reset the plugin lock |
| 213 | * @is_child: is this thread the child |
| 214 | * |
| 215 | * This user-mode only helper resets the lock state after a fork so we |
| 216 | * can continue using the plugin interface. |
| 217 | */ |
| 218 | void qemu_plugin_user_postfork(bool is_child); |
| 219 | |
| 220 | enum qemu_plugin_cb_flags tcg_call_to_qemu_plugin_cb_flags(int flags); |
| 221 | |
| 222 | static inline void qemu_plugin_set_cb_flags(CPUState *cpu, |
| 223 | enum qemu_plugin_cb_flags flags) |
| 224 | { |
| 225 | assert(cpu); |
| 226 | cpu->neg.plugin_cb_flags = flags; |
| 227 | } |
| 228 | |
| 229 | static inline enum qemu_plugin_cb_flags qemu_plugin_get_cb_flags(void) |
| 230 | { |
| 231 | assert(current_cpu); |
| 232 | return current_cpu->neg.plugin_cb_flags; |
| 233 | } |
| 234 | |
| 235 | #else /* !CONFIG_PLUGIN */ |
| 236 | |
| 237 | static inline void qemu_plugin_add_opts(void) |
| 238 | { } |
| 239 | |
| 240 | static inline void qemu_plugin_opt_parse(const char *optstr, |
| 241 | QemuPluginList *head) |
| 242 | { |
| 243 | error_report("plugin interface not enabled in this build"); |
| 244 | exit(1); |
| 245 | } |
| 246 | |
| 247 | static inline int qemu_plugin_load_list(QemuPluginList *head, Error **errp) |
| 248 | { |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static inline void qemu_plugin_vcpu_init_hook(CPUState *cpu) |
| 253 | { } |
| 254 | |
| 255 | static inline void qemu_plugin_vcpu_exit_hook(CPUState *cpu) |
| 256 | { } |
| 257 | |
| 258 | static inline void qemu_plugin_tb_trans_cb(CPUState *cpu, |
| 259 | struct qemu_plugin_tb *tb) |
| 260 | { } |
| 261 | |
| 262 | static inline void qemu_plugin_vcpu_idle_cb(CPUState *cpu) |
| 263 | { } |
| 264 | |
| 265 | static inline void qemu_plugin_vcpu_resume_cb(CPUState *cpu) |
| 266 | { } |
| 267 | |
| 268 | static inline void qemu_plugin_vcpu_interrupt_cb(CPUState *cpu, uint64_t from) |
| 269 | { } |
| 270 | |
| 271 | static inline void qemu_plugin_vcpu_exception_cb(CPUState *cpu, uint64_t from) |
| 272 | { } |
| 273 | |
| 274 | static inline void qemu_plugin_vcpu_hostcall_cb(CPUState *cpu, uint64_t from) |
| 275 | { } |
| 276 | |
| 277 | static inline void |
| 278 | qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, |
| 279 | uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, |
| 280 | uint64_t a7, uint64_t a8) |
| 281 | { } |
| 282 | |
| 283 | static inline |
| 284 | void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret) |
| 285 | { } |
| 286 | |
| 287 | static inline bool |
| 288 | qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1, |
| 289 | uint64_t a2, uint64_t a3, uint64_t a4, |
| 290 | uint64_t a5, uint64_t a6, uint64_t a7, |
| 291 | uint64_t a8, int64_t *sysret) |
| 292 | { |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | static inline void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, |
| 297 | uint64_t value_low, |
| 298 | uint64_t value_high, |
| 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 */ |