1 /* 2 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org> 3 * Copyright (C) 2019, Linaro 4 * 5 * License: GNU GPL, version 2 or later. 6 * See the COPYING file in the top-level directory. 7 * 8 * SPDX-License-Identifier: GPL-2.0-or-later 9 */ 10 #ifndef QEMU_PLUGIN_API_H 11 #define QEMU_PLUGIN_API_H 12 13 #include <inttypes.h> 14 #include <stdbool.h> 15 #include <stddef.h> 16 17 /* 18 * For best performance, build the plugin with -fvisibility=hidden so that 19 * QEMU_PLUGIN_LOCAL is implicit. Then, just mark qemu_plugin_install with 20 * QEMU_PLUGIN_EXPORT. For more info, see 21 * https://gcc.gnu.org/wiki/Visibility 22 */ 23 #if defined _WIN32 || defined __CYGWIN__ 24 #ifdef BUILDING_DLL 25 #define QEMU_PLUGIN_EXPORT __declspec(dllexport) 26 #else 27 #define QEMU_PLUGIN_EXPORT __declspec(dllimport) 28 #endif 29 #define QEMU_PLUGIN_LOCAL 30 #else 31 #if __GNUC__ >= 4 32 #define QEMU_PLUGIN_EXPORT __attribute__((visibility("default"))) 33 #define QEMU_PLUGIN_LOCAL __attribute__((visibility("hidden"))) 34 #else 35 #define QEMU_PLUGIN_EXPORT 36 #define QEMU_PLUGIN_LOCAL 37 #endif 38 #endif 39 40 typedef uint64_t qemu_plugin_id_t; 41 42 /* 43 * Versioning plugins: 44 * 45 * The plugin API will pass a minimum and current API version that 46 * QEMU currently supports. The minimum API will be incremented if an 47 * API needs to be deprecated. 48 * 49 * The plugins export the API they were built against by exposing the 50 * symbol qemu_plugin_version which can be checked. 51 */ 52 53 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version; 54 55 #define QEMU_PLUGIN_VERSION 0 56 57 typedef struct { 58 /* string describing architecture */ 59 const char *target_name; 60 struct { 61 int min; 62 int cur; 63 } version; 64 /* is this a full system emulation? */ 65 bool system_emulation; 66 union { 67 /* 68 * smp_vcpus may change if vCPUs can be hot-plugged, max_vcpus 69 * is the system-wide limit. 70 */ 71 struct { 72 int smp_vcpus; 73 int max_vcpus; 74 } system; 75 }; 76 } qemu_info_t; 77 78 /** 79 * qemu_plugin_install() - Install a plugin 80 * @id: this plugin's opaque ID 81 * @info: a block describing some details about the guest 82 * @argc: number of arguments 83 * @argv: array of arguments (@argc elements) 84 * 85 * All plugins must export this symbol. 86 * 87 * Note: Calling qemu_plugin_uninstall() from this function is a bug. To raise 88 * an error during install, return !0. 89 * 90 * Note: @info is only live during the call. Copy any information we 91 * want to keep. 92 * 93 * Note: @argv remains valid throughout the lifetime of the loaded plugin. 94 */ 95 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, 96 const qemu_info_t *info, 97 int argc, char **argv); 98 99 /* 100 * Prototypes for the various callback styles we will be registering 101 * in the following functions. 102 */ 103 typedef void (*qemu_plugin_simple_cb_t)(qemu_plugin_id_t id); 104 105 typedef void (*qemu_plugin_udata_cb_t)(qemu_plugin_id_t id, void *userdata); 106 107 typedef void (*qemu_plugin_vcpu_simple_cb_t)(qemu_plugin_id_t id, 108 unsigned int vcpu_index); 109 110 typedef void (*qemu_plugin_vcpu_udata_cb_t)(unsigned int vcpu_index, 111 void *userdata); 112 113 /** 114 * qemu_plugin_uninstall() - Uninstall a plugin 115 * @id: this plugin's opaque ID 116 * @cb: callback to be called once the plugin has been removed 117 * 118 * Do NOT assume that the plugin has been uninstalled once this function 119 * returns. Plugins are uninstalled asynchronously, and therefore the given 120 * plugin receives callbacks until @cb is called. 121 * 122 * Note: Calling this function from qemu_plugin_install() is a bug. 123 */ 124 void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb); 125 126 /** 127 * qemu_plugin_reset() - Reset a plugin 128 * @id: this plugin's opaque ID 129 * @cb: callback to be called once the plugin has been reset 130 * 131 * Unregisters all callbacks for the plugin given by @id. 132 * 133 * Do NOT assume that the plugin has been reset once this function returns. 134 * Plugins are reset asynchronously, and therefore the given plugin receives 135 * callbacks until @cb is called. 136 */ 137 void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb); 138 139 /** 140 * qemu_plugin_register_vcpu_init_cb() - register a vCPU initialization callback 141 * @id: plugin ID 142 * @cb: callback function 143 * 144 * The @cb function is called every time a vCPU is initialized. 145 * 146 * See also: qemu_plugin_register_vcpu_exit_cb() 147 */ 148 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id, 149 qemu_plugin_vcpu_simple_cb_t cb); 150 151 /** 152 * qemu_plugin_register_vcpu_exit_cb() - register a vCPU exit callback 153 * @id: plugin ID 154 * @cb: callback function 155 * 156 * The @cb function is called every time a vCPU exits. 157 * 158 * See also: qemu_plugin_register_vcpu_init_cb() 159 */ 160 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id, 161 qemu_plugin_vcpu_simple_cb_t cb); 162 163 /** 164 * qemu_plugin_register_vcpu_idle_cb() - register a vCPU idle callback 165 * @id: plugin ID 166 * @cb: callback function 167 * 168 * The @cb function is called every time a vCPU idles. 169 */ 170 void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id, 171 qemu_plugin_vcpu_simple_cb_t cb); 172 173 /** 174 * qemu_plugin_register_vcpu_resume_cb() - register a vCPU resume callback 175 * @id: plugin ID 176 * @cb: callback function 177 * 178 * The @cb function is called every time a vCPU resumes execution. 179 */ 180 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id, 181 qemu_plugin_vcpu_simple_cb_t cb); 182 183 /* 184 * Opaque types that the plugin is given during the translation and 185 * instrumentation phase. 186 */ 187 struct qemu_plugin_tb; 188 struct qemu_plugin_insn; 189 190 enum qemu_plugin_cb_flags { 191 QEMU_PLUGIN_CB_NO_REGS, /* callback does not access the CPU's regs */ 192 QEMU_PLUGIN_CB_R_REGS, /* callback reads the CPU's regs */ 193 QEMU_PLUGIN_CB_RW_REGS, /* callback reads and writes the CPU's regs */ 194 }; 195 196 enum qemu_plugin_mem_rw { 197 QEMU_PLUGIN_MEM_R = 1, 198 QEMU_PLUGIN_MEM_W, 199 QEMU_PLUGIN_MEM_RW, 200 }; 201 202 /** 203 * qemu_plugin_register_vcpu_tb_trans_cb() - register a translate cb 204 * @id: plugin ID 205 * @cb: callback function 206 * 207 * The @cb function is called every time a translation occurs. The @cb 208 * function is passed an opaque qemu_plugin_type which it can query 209 * for additional information including the list of translated 210 * instructions. At this point the plugin can register further 211 * callbacks to be triggered when the block or individual instruction 212 * executes. 213 */ 214 typedef void (*qemu_plugin_vcpu_tb_trans_cb_t)(qemu_plugin_id_t id, 215 struct qemu_plugin_tb *tb); 216 217 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id, 218 qemu_plugin_vcpu_tb_trans_cb_t cb); 219 220 /** 221 * qemu_plugin_register_vcpu_tb_trans_exec_cb() - register execution callback 222 * @tb: the opaque qemu_plugin_tb handle for the translation 223 * @cb: callback function 224 * @flags: does the plugin read or write the CPU's registers? 225 * @userdata: any plugin data to pass to the @cb? 226 * 227 * The @cb function is called every time a translated unit executes. 228 */ 229 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb, 230 qemu_plugin_vcpu_udata_cb_t cb, 231 enum qemu_plugin_cb_flags flags, 232 void *userdata); 233 234 enum qemu_plugin_op { 235 QEMU_PLUGIN_INLINE_ADD_U64, 236 }; 237 238 /** 239 * qemu_plugin_register_vcpu_tb_trans_exec_inline() - execution inline op 240 * @tb: the opaque qemu_plugin_tb handle for the translation 241 * @op: the type of qemu_plugin_op (e.g. ADD_U64) 242 * @ptr: the target memory location for the op 243 * @imm: the op data (e.g. 1) 244 * 245 * Insert an inline op to every time a translated unit executes. 246 * Useful if you just want to increment a single counter somewhere in 247 * memory. 248 */ 249 void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb, 250 enum qemu_plugin_op op, 251 void *ptr, uint64_t imm); 252 253 /** 254 * qemu_plugin_register_vcpu_insn_exec_cb() - register insn execution cb 255 * @insn: the opaque qemu_plugin_insn handle for an instruction 256 * @cb: callback function 257 * @flags: does the plugin read or write the CPU's registers? 258 * @userdata: any plugin data to pass to the @cb? 259 * 260 * The @cb function is called every time an instruction is executed 261 */ 262 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn, 263 qemu_plugin_vcpu_udata_cb_t cb, 264 enum qemu_plugin_cb_flags flags, 265 void *userdata); 266 267 /** 268 * qemu_plugin_register_vcpu_insn_exec_inline() - insn execution inline op 269 * @insn: the opaque qemu_plugin_insn handle for an instruction 270 * @cb: callback function 271 * @op: the type of qemu_plugin_op (e.g. ADD_U64) 272 * @ptr: the target memory location for the op 273 * @imm: the op data (e.g. 1) 274 * 275 * Insert an inline op to every time an instruction executes. Useful 276 * if you just want to increment a single counter somewhere in memory. 277 */ 278 void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn, 279 enum qemu_plugin_op op, 280 void *ptr, uint64_t imm); 281 282 /* 283 * Helpers to query information about the instructions in a block 284 */ 285 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb); 286 287 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb); 288 289 struct qemu_plugin_insn * 290 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx); 291 292 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn); 293 294 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn); 295 296 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn); 297 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn); 298 299 /* 300 * Memory Instrumentation 301 * 302 * The anonymous qemu_plugin_meminfo_t and qemu_plugin_hwaddr types 303 * can be used in queries to QEMU to get more information about a 304 * given memory access. 305 */ 306 typedef uint32_t qemu_plugin_meminfo_t; 307 struct qemu_plugin_hwaddr; 308 309 /* meminfo queries */ 310 unsigned int qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info); 311 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info); 312 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info); 313 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info); 314 315 /* 316 * qemu_plugin_get_hwaddr(): 317 * @vaddr: the virtual address of the memory operation 318 * 319 * For system emulation returns a qemu_plugin_hwaddr handle to query 320 * details about the actual physical address backing the virtual 321 * address. For linux-user guests it just returns NULL. 322 * 323 * This handle is *only* valid for the duration of the callback. Any 324 * information about the handle should be recovered before the 325 * callback returns. 326 */ 327 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info, 328 uint64_t vaddr); 329 330 /* 331 * The following additional queries can be run on the hwaddr structure 332 * to return information about it. For non-IO accesses the device 333 * offset will be into the appropriate block of RAM. 334 */ 335 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr); 336 uint64_t qemu_plugin_hwaddr_device_offset(const struct qemu_plugin_hwaddr *haddr); 337 338 typedef void 339 (*qemu_plugin_vcpu_mem_cb_t)(unsigned int vcpu_index, 340 qemu_plugin_meminfo_t info, uint64_t vaddr, 341 void *userdata); 342 343 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn, 344 qemu_plugin_vcpu_mem_cb_t cb, 345 enum qemu_plugin_cb_flags flags, 346 enum qemu_plugin_mem_rw rw, 347 void *userdata); 348 349 void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn *insn, 350 enum qemu_plugin_mem_rw rw, 351 enum qemu_plugin_op op, void *ptr, 352 uint64_t imm); 353 354 355 356 typedef void 357 (*qemu_plugin_vcpu_syscall_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_index, 358 int64_t num, uint64_t a1, uint64_t a2, 359 uint64_t a3, uint64_t a4, uint64_t a5, 360 uint64_t a6, uint64_t a7, uint64_t a8); 361 362 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id, 363 qemu_plugin_vcpu_syscall_cb_t cb); 364 365 typedef void 366 (*qemu_plugin_vcpu_syscall_ret_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_idx, 367 int64_t num, int64_t ret); 368 369 void 370 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id, 371 qemu_plugin_vcpu_syscall_ret_cb_t cb); 372 373 374 /** 375 * qemu_plugin_insn_disas() - return disassembly string for instruction 376 * @insn: instruction reference 377 * 378 * Returns an allocated string containing the disassembly 379 */ 380 381 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn); 382 383 /** 384 * qemu_plugin_vcpu_for_each() - iterate over the existing vCPU 385 * @id: plugin ID 386 * @cb: callback function 387 * 388 * The @cb function is called once for each existing vCPU. 389 * 390 * See also: qemu_plugin_register_vcpu_init_cb() 391 */ 392 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id, 393 qemu_plugin_vcpu_simple_cb_t cb); 394 395 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id, 396 qemu_plugin_simple_cb_t cb); 397 398 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id, 399 qemu_plugin_udata_cb_t cb, void *userdata); 400 401 /* returns -1 in user-mode */ 402 int qemu_plugin_n_vcpus(void); 403 404 /* returns -1 in user-mode */ 405 int qemu_plugin_n_max_vcpus(void); 406 407 /** 408 * qemu_plugin_outs() - output string via QEMU's logging system 409 * @string: a string 410 */ 411 void qemu_plugin_outs(const char *string); 412 413 #endif /* QEMU_PLUGIN_API_H */ 414