qemu/plugins/api.c
<<
>>
Prefs
   1/*
   2 * QEMU Plugin API
   3 *
   4 * This provides the API that is available to the plugins to interact
   5 * with QEMU. We have to be careful not to expose internal details of
   6 * how QEMU works so we abstract out things like translation and
   7 * instructions to anonymous data types:
   8 *
   9 *  qemu_plugin_tb
  10 *  qemu_plugin_insn
  11 *  qemu_plugin_register
  12 *
  13 * Which can then be passed back into the API to do additional things.
  14 * As such all the public functions in here are exported in
  15 * qemu-plugin.h.
  16 *
  17 * The general life-cycle of a plugin is:
  18 *
  19 *  - plugin is loaded, public qemu_plugin_install called
  20 *    - the install func registers callbacks for events
  21 *    - usually an atexit_cb is registered to dump info at the end
  22 *  - when a registered event occurs the plugin is called
  23 *     - some events pass additional info
  24 *     - during translation the plugin can decide to instrument any
  25 *       instruction
  26 *  - when QEMU exits all the registered atexit callbacks are called
  27 *
  28 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
  29 * Copyright (C) 2019, Linaro
  30 *
  31 * License: GNU GPL, version 2 or later.
  32 *   See the COPYING file in the top-level directory.
  33 *
  34 * SPDX-License-Identifier: GPL-2.0-or-later
  35 *
  36 */
  37
  38#include "qemu/osdep.h"
  39#include "qemu/main-loop.h"
  40#include "qemu/plugin.h"
  41#include "qemu/log.h"
  42#include "tcg/tcg.h"
  43#include "exec/gdbstub.h"
  44#include "exec/target_page.h"
  45#include "exec/translation-block.h"
  46#include "exec/translator.h"
  47#include "disas/disas.h"
  48#include "plugin.h"
  49
  50/* Uninstall and Reset handlers */
  51
  52void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  53{
  54    plugin_reset_uninstall(id, cb, false);
  55}
  56
  57void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
  58{
  59    plugin_reset_uninstall(id, cb, true);
  60}
  61
  62/*
  63 * Plugin Register Functions
  64 *
  65 * This allows the plugin to register callbacks for various events
  66 * during the translation.
  67 */
  68
  69void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
  70                                       qemu_plugin_vcpu_simple_cb_t cb)
  71{
  72    plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
  73}
  74
  75void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
  76                                       qemu_plugin_vcpu_simple_cb_t cb)
  77{
  78    plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
  79}
  80
  81static bool tb_is_mem_only(void)
  82{
  83    return tb_cflags(tcg_ctx->gen_tb) & CF_MEMI_ONLY;
  84}
  85
  86void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
  87                                          qemu_plugin_vcpu_udata_cb_t cb,
  88                                          enum qemu_plugin_cb_flags flags,
  89                                          void *udata)
  90{
  91    if (!tb_is_mem_only()) {
  92        plugin_register_dyn_cb__udata(&tb->cbs, cb, flags, udata);
  93    }
  94}
  95
  96void qemu_plugin_register_vcpu_tb_exec_cond_cb(struct qemu_plugin_tb *tb,
  97                                               qemu_plugin_vcpu_udata_cb_t cb,
  98                                               enum qemu_plugin_cb_flags flags,
  99                                               enum qemu_plugin_cond cond,
 100                                               qemu_plugin_u64 entry,
 101                                               uint64_t imm,
 102                                               void *udata)
 103{
 104    if (cond == QEMU_PLUGIN_COND_NEVER || tb_is_mem_only()) {
 105        return;
 106    }
 107    if (cond == QEMU_PLUGIN_COND_ALWAYS) {
 108        qemu_plugin_register_vcpu_tb_exec_cb(tb, cb, flags, udata);
 109        return;
 110    }
 111    plugin_register_dyn_cond_cb__udata(&tb->cbs, cb, flags,
 112                                       cond, entry, imm, udata);
 113}
 114
 115void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
 116    struct qemu_plugin_tb *tb,
 117    enum qemu_plugin_op op,
 118    qemu_plugin_u64 entry,
 119    uint64_t imm)
 120{
 121    if (!tb_is_mem_only()) {
 122        plugin_register_inline_op_on_entry(&tb->cbs, 0, op, entry, imm);
 123    }
 124}
 125
 126void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
 127                                            qemu_plugin_vcpu_udata_cb_t cb,
 128                                            enum qemu_plugin_cb_flags flags,
 129                                            void *udata)
 130{
 131    if (!tb_is_mem_only()) {
 132        plugin_register_dyn_cb__udata(&insn->insn_cbs, cb, flags, udata);
 133    }
 134}
 135
 136void qemu_plugin_register_vcpu_insn_exec_cond_cb(
 137    struct qemu_plugin_insn *insn,
 138    qemu_plugin_vcpu_udata_cb_t cb,
 139    enum qemu_plugin_cb_flags flags,
 140    enum qemu_plugin_cond cond,
 141    qemu_plugin_u64 entry,
 142    uint64_t imm,
 143    void *udata)
 144{
 145    if (cond == QEMU_PLUGIN_COND_NEVER || tb_is_mem_only()) {
 146        return;
 147    }
 148    if (cond == QEMU_PLUGIN_COND_ALWAYS) {
 149        qemu_plugin_register_vcpu_insn_exec_cb(insn, cb, flags, udata);
 150        return;
 151    }
 152    plugin_register_dyn_cond_cb__udata(&insn->insn_cbs, cb, flags,
 153                                       cond, entry, imm, udata);
 154}
 155
 156void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
 157    struct qemu_plugin_insn *insn,
 158    enum qemu_plugin_op op,
 159    qemu_plugin_u64 entry,
 160    uint64_t imm)
 161{
 162    if (!tb_is_mem_only()) {
 163        plugin_register_inline_op_on_entry(&insn->insn_cbs, 0, op, entry, imm);
 164    }
 165}
 166
 167
 168/*
 169 * We always plant memory instrumentation because they don't finalise until
 170 * after the operation has complete.
 171 */
 172void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
 173                                      qemu_plugin_vcpu_mem_cb_t cb,
 174                                      enum qemu_plugin_cb_flags flags,
 175                                      enum qemu_plugin_mem_rw rw,
 176                                      void *udata)
 177{
 178    plugin_register_vcpu_mem_cb(&insn->mem_cbs, cb, flags, rw, udata);
 179}
 180
 181void qemu_plugin_register_vcpu_mem_inline_per_vcpu(
 182    struct qemu_plugin_insn *insn,
 183    enum qemu_plugin_mem_rw rw,
 184    enum qemu_plugin_op op,
 185    qemu_plugin_u64 entry,
 186    uint64_t imm)
 187{
 188    plugin_register_inline_op_on_entry(&insn->mem_cbs, rw, op, entry, imm);
 189}
 190
 191void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
 192                                           qemu_plugin_vcpu_tb_trans_cb_t cb)
 193{
 194    plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
 195}
 196
 197void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
 198                                          qemu_plugin_vcpu_syscall_cb_t cb)
 199{
 200    plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
 201}
 202
 203void
 204qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
 205                                         qemu_plugin_vcpu_syscall_ret_cb_t cb)
 206{
 207    plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
 208}
 209
 210/*
 211 * Plugin Queries
 212 *
 213 * These are queries that the plugin can make to gauge information
 214 * from our opaque data types. We do not want to leak internal details
 215 * here just information useful to the plugin.
 216 */
 217
 218/*
 219 * Translation block information:
 220 *
 221 * A plugin can query the virtual address of the start of the block
 222 * and the number of instructions in it. It can also get access to
 223 * each translated instruction.
 224 */
 225
 226size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
 227{
 228    return tb->n;
 229}
 230
 231uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
 232{
 233    const DisasContextBase *db = tcg_ctx->plugin_db;
 234    return db->pc_first;
 235}
 236
 237struct qemu_plugin_insn *
 238qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
 239{
 240    struct qemu_plugin_insn *insn;
 241    if (unlikely(idx >= tb->n)) {
 242        return NULL;
 243    }
 244    insn = g_ptr_array_index(tb->insns, idx);
 245    return insn;
 246}
 247
 248/*
 249 * Instruction information
 250 *
 251 * These queries allow the plugin to retrieve information about each
 252 * instruction being translated.
 253 */
 254
 255size_t qemu_plugin_insn_data(const struct qemu_plugin_insn *insn,
 256                             void *dest, size_t len)
 257{
 258    const DisasContextBase *db = tcg_ctx->plugin_db;
 259
 260    len = MIN(len, insn->len);
 261    return translator_st(db, dest, insn->vaddr, len) ? len : 0;
 262}
 263
 264size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
 265{
 266    return insn->len;
 267}
 268
 269uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
 270{
 271    return insn->vaddr;
 272}
 273
 274void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
 275{
 276    const DisasContextBase *db = tcg_ctx->plugin_db;
 277    vaddr page0_last = db->pc_first | ~qemu_target_page_mask();
 278
 279    if (db->fake_insn) {
 280        return NULL;
 281    }
 282
 283    /*
 284     * ??? The return value is not intended for use of host memory,
 285     * but as a proxy for address space and physical address.
 286     * Thus we are only interested in the first byte and do not
 287     * care about spanning pages.
 288     */
 289    if (insn->vaddr <= page0_last) {
 290        if (db->host_addr[0] == NULL) {
 291            return NULL;
 292        }
 293        return db->host_addr[0] + insn->vaddr - db->pc_first;
 294    } else {
 295        if (db->host_addr[1] == NULL) {
 296            return NULL;
 297        }
 298        return db->host_addr[1] + insn->vaddr - (page0_last + 1);
 299    }
 300}
 301
 302char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
 303{
 304    return plugin_disas(tcg_ctx->cpu, tcg_ctx->plugin_db,
 305                        insn->vaddr, insn->len);
 306}
 307
 308const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
 309{
 310    const char *sym = lookup_symbol(insn->vaddr);
 311    return sym[0] != 0 ? sym : NULL;
 312}
 313
 314/*
 315 * The memory queries allow the plugin to query information about a
 316 * memory access.
 317 */
 318
 319unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
 320{
 321    MemOp op = get_memop(info);
 322    return op & MO_SIZE;
 323}
 324
 325bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
 326{
 327    MemOp op = get_memop(info);
 328    return op & MO_SIGN;
 329}
 330
 331bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
 332{
 333    MemOp op = get_memop(info);
 334    return (op & MO_BSWAP) == MO_BE;
 335}
 336
 337bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
 338{
 339    return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
 340}
 341
 342qemu_plugin_mem_value qemu_plugin_mem_get_value(qemu_plugin_meminfo_t info)
 343{
 344    uint64_t low = current_cpu->neg.plugin_mem_value_low;
 345    qemu_plugin_mem_value value;
 346
 347    switch (qemu_plugin_mem_size_shift(info)) {
 348    case 0:
 349        value.type = QEMU_PLUGIN_MEM_VALUE_U8;
 350        value.data.u8 = (uint8_t)low;
 351        break;
 352    case 1:
 353        value.type = QEMU_PLUGIN_MEM_VALUE_U16;
 354        value.data.u16 = (uint16_t)low;
 355        break;
 356    case 2:
 357        value.type = QEMU_PLUGIN_MEM_VALUE_U32;
 358        value.data.u32 = (uint32_t)low;
 359        break;
 360    case 3:
 361        value.type = QEMU_PLUGIN_MEM_VALUE_U64;
 362        value.data.u64 = low;
 363        break;
 364    case 4:
 365        value.type = QEMU_PLUGIN_MEM_VALUE_U128;
 366        value.data.u128.low = low;
 367        value.data.u128.high = current_cpu->neg.plugin_mem_value_high;
 368        break;
 369    default:
 370        g_assert_not_reached();
 371    }
 372    return value;
 373}
 374
 375int qemu_plugin_num_vcpus(void)
 376{
 377    return plugin_num_vcpus();
 378}
 379
 380/*
 381 * Plugin output
 382 */
 383void qemu_plugin_outs(const char *string)
 384{
 385    qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
 386}
 387
 388bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
 389{
 390    return name && value && qapi_bool_parse(name, value, ret, NULL);
 391}
 392
 393/*
 394 * Create register handles.
 395 *
 396 * We need to create a handle for each register so the plugin
 397 * infrastructure can call gdbstub to read a register. They are
 398 * currently just a pointer encapsulation of the gdb_reg but in
 399 * future may hold internal plugin state so its important plugin
 400 * authors are not tempted to treat them as numbers.
 401 *
 402 * We also construct a result array with those handles and some
 403 * ancillary data the plugin might find useful.
 404 */
 405
 406static GArray *create_register_handles(GArray *gdbstub_regs)
 407{
 408    GArray *find_data = g_array_new(true, true,
 409                                    sizeof(qemu_plugin_reg_descriptor));
 410
 411    for (int i = 0; i < gdbstub_regs->len; i++) {
 412        GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);
 413        qemu_plugin_reg_descriptor desc;
 414
 415        /* skip "un-named" regs */
 416        if (!grd->name) {
 417            continue;
 418        }
 419
 420        /* Create a record for the plugin */
 421        desc.handle = GINT_TO_POINTER(grd->gdb_reg + 1);
 422        desc.name = g_intern_string(grd->name);
 423        desc.feature = g_intern_string(grd->feature_name);
 424        g_array_append_val(find_data, desc);
 425    }
 426
 427    return find_data;
 428}
 429
 430GArray *qemu_plugin_get_registers(void)
 431{
 432    g_assert(current_cpu);
 433
 434    g_autoptr(GArray) regs = gdb_get_register_list(current_cpu);
 435    return create_register_handles(regs);
 436}
 437
 438bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)
 439{
 440    g_assert(current_cpu);
 441
 442    if (len == 0) {
 443        return false;
 444    }
 445
 446    g_byte_array_set_size(data, len);
 447
 448    int result = cpu_memory_rw_debug(current_cpu, addr, data->data,
 449                                     data->len, false);
 450
 451    if (result < 0) {
 452        return false;
 453    }
 454
 455    return true;
 456}
 457
 458int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
 459{
 460    g_assert(current_cpu);
 461
 462    return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1);
 463}
 464
 465struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size)
 466{
 467    return plugin_scoreboard_new(element_size);
 468}
 469
 470void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)
 471{
 472    plugin_scoreboard_free(score);
 473}
 474
 475void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score,
 476                                  unsigned int vcpu_index)
 477{
 478    g_assert(vcpu_index < qemu_plugin_num_vcpus());
 479    /* we can't use g_array_index since entry size is not statically known */
 480    char *base_ptr = score->data->data;
 481    return base_ptr + vcpu_index * g_array_get_element_size(score->data);
 482}
 483
 484static uint64_t *plugin_u64_address(qemu_plugin_u64 entry,
 485                                    unsigned int vcpu_index)
 486{
 487    char *ptr = qemu_plugin_scoreboard_find(entry.score, vcpu_index);
 488    return (uint64_t *)(ptr + entry.offset);
 489}
 490
 491void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index,
 492                         uint64_t added)
 493{
 494    *plugin_u64_address(entry, vcpu_index) += added;
 495}
 496
 497uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry,
 498                             unsigned int vcpu_index)
 499{
 500    return *plugin_u64_address(entry, vcpu_index);
 501}
 502
 503void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
 504                         uint64_t val)
 505{
 506    *plugin_u64_address(entry, vcpu_index) = val;
 507}
 508
 509uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry)
 510{
 511    uint64_t total = 0;
 512    for (int i = 0, n = qemu_plugin_num_vcpus(); i < n; ++i) {
 513        total += qemu_plugin_u64_get(entry, i);
 514    }
 515    return total;
 516}
 517
 518