2017-08-31 00:39:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
|
|
|
|
*
|
|
|
|
* License: GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
#ifndef QEMU_PLUGIN_H
|
|
|
|
#define QEMU_PLUGIN_H
|
|
|
|
|
|
|
|
#include "qemu/config-file.h"
|
|
|
|
#include "qemu/qemu-plugin.h"
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "qemu/queue.h"
|
|
|
|
#include "qemu/option.h"
|
2023-03-15 18:43:15 +01:00
|
|
|
#include "qemu/plugin-event.h"
|
2021-07-26 23:48:30 +02:00
|
|
|
#include "exec/memopidx.h"
|
2023-03-15 18:43:16 +01:00
|
|
|
#include "hw/core/cpu.h"
|
2017-08-31 00:39:53 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Option parsing/processing.
|
|
|
|
* Note that we can load an arbitrary number of plugins.
|
|
|
|
*/
|
|
|
|
struct qemu_plugin_desc;
|
|
|
|
typedef QTAILQ_HEAD(, qemu_plugin_desc) QemuPluginList;
|
|
|
|
|
2021-07-26 23:48:30 +02:00
|
|
|
/*
|
|
|
|
* Construct a qemu_plugin_meminfo_t.
|
|
|
|
*/
|
|
|
|
static inline qemu_plugin_meminfo_t
|
|
|
|
make_plugin_meminfo(MemOpIdx oi, enum qemu_plugin_mem_rw rw)
|
|
|
|
{
|
|
|
|
return oi | (rw << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the memory operation direction from a qemu_plugin_meminfo_t.
|
|
|
|
* Other portions may be extracted via get_memop and get_mmuidx.
|
|
|
|
*/
|
|
|
|
static inline enum qemu_plugin_mem_rw
|
|
|
|
get_plugin_meminfo_rw(qemu_plugin_meminfo_t i)
|
|
|
|
{
|
|
|
|
return i >> 16;
|
|
|
|
}
|
|
|
|
|
2017-08-31 00:39:53 +02:00
|
|
|
#ifdef CONFIG_PLUGIN
|
|
|
|
extern QemuOptsList qemu_plugin_opts;
|
|
|
|
|
|
|
|
static inline void qemu_plugin_add_opts(void)
|
|
|
|
{
|
|
|
|
qemu_add_opts(&qemu_plugin_opts);
|
|
|
|
}
|
|
|
|
|
2023-10-04 14:00:08 +02:00
|
|
|
void qemu_plugin_opt_parse(const char *optstr, QemuPluginList *head);
|
2020-10-27 09:58:26 +01:00
|
|
|
int qemu_plugin_load_list(QemuPluginList *head, Error **errp);
|
2017-08-31 00:39:53 +02:00
|
|
|
|
|
|
|
union qemu_plugin_cb_sig {
|
|
|
|
qemu_plugin_simple_cb_t simple;
|
|
|
|
qemu_plugin_udata_cb_t udata;
|
|
|
|
qemu_plugin_vcpu_simple_cb_t vcpu_simple;
|
|
|
|
qemu_plugin_vcpu_udata_cb_t vcpu_udata;
|
|
|
|
qemu_plugin_vcpu_tb_trans_cb_t vcpu_tb_trans;
|
|
|
|
qemu_plugin_vcpu_mem_cb_t vcpu_mem;
|
|
|
|
qemu_plugin_vcpu_syscall_cb_t vcpu_syscall;
|
|
|
|
qemu_plugin_vcpu_syscall_ret_cb_t vcpu_syscall_ret;
|
|
|
|
void *generic;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum plugin_dyn_cb_type {
|
|
|
|
PLUGIN_CB_INSN,
|
|
|
|
PLUGIN_CB_MEM,
|
|
|
|
PLUGIN_N_CB_TYPES,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum plugin_dyn_cb_subtype {
|
|
|
|
PLUGIN_CB_REGULAR,
|
2024-02-27 15:43:26 +01:00
|
|
|
PLUGIN_CB_REGULAR_R,
|
2017-08-31 00:39:53 +02:00
|
|
|
PLUGIN_CB_INLINE,
|
|
|
|
PLUGIN_N_CB_SUBTYPES,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A dynamic callback has an insertion point that is determined at run-time.
|
|
|
|
* Usually the insertion point is somewhere in the code cache; think for
|
|
|
|
* instance of a callback to be called upon the execution of a particular TB.
|
|
|
|
*/
|
|
|
|
struct qemu_plugin_dyn_cb {
|
|
|
|
union qemu_plugin_cb_sig f;
|
|
|
|
void *userp;
|
|
|
|
enum plugin_dyn_cb_subtype type;
|
|
|
|
/* @rw applies to mem callbacks only (both regular and inline) */
|
|
|
|
enum qemu_plugin_mem_rw rw;
|
|
|
|
/* fields specific to each dyn_cb type go here */
|
|
|
|
union {
|
|
|
|
struct {
|
2024-03-05 13:09:53 +01:00
|
|
|
qemu_plugin_u64 entry;
|
2017-08-31 00:39:53 +02:00
|
|
|
enum qemu_plugin_op op;
|
|
|
|
uint64_t imm;
|
|
|
|
} inline_insn;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-13 14:03:22 +01:00
|
|
|
/* Internal context for instrumenting an instruction */
|
2017-08-31 00:39:53 +02:00
|
|
|
struct qemu_plugin_insn {
|
|
|
|
GByteArray *data;
|
|
|
|
uint64_t vaddr;
|
|
|
|
void *haddr;
|
|
|
|
GArray *cbs[PLUGIN_N_CB_TYPES][PLUGIN_N_CB_SUBTYPES];
|
|
|
|
bool calls_helpers;
|
2023-01-24 19:01:23 +01:00
|
|
|
|
|
|
|
/* if set, the instruction calls helpers that might access guest memory */
|
2017-08-31 00:39:53 +02:00
|
|
|
bool mem_helper;
|
2023-01-24 19:01:23 +01:00
|
|
|
|
2021-02-13 14:03:22 +01:00
|
|
|
bool mem_only;
|
2017-08-31 00:39:53 +02:00
|
|
|
};
|
|
|
|
|
2024-03-05 13:09:50 +01:00
|
|
|
/* A scoreboard is an array of values, indexed by vcpu_index */
|
|
|
|
struct qemu_plugin_scoreboard {
|
|
|
|
GArray *data;
|
|
|
|
QLIST_ENTRY(qemu_plugin_scoreboard) entry;
|
|
|
|
};
|
|
|
|
|
2017-08-31 00:39:53 +02:00
|
|
|
/*
|
|
|
|
* qemu_plugin_insn allocate and cleanup functions. We don't expect to
|
|
|
|
* cleanup many of these structures. They are reused for each fresh
|
|
|
|
* translation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void qemu_plugin_insn_cleanup_fn(gpointer data)
|
|
|
|
{
|
|
|
|
struct qemu_plugin_insn *insn = (struct qemu_plugin_insn *) data;
|
|
|
|
g_byte_array_free(insn->data, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct qemu_plugin_insn *qemu_plugin_insn_alloc(void)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
struct qemu_plugin_insn *insn = g_new0(struct qemu_plugin_insn, 1);
|
|
|
|
insn->data = g_byte_array_sized_new(4);
|
|
|
|
|
|
|
|
for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
|
|
|
|
for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
|
|
|
|
insn->cbs[i][j] = g_array_new(false, false,
|
|
|
|
sizeof(struct qemu_plugin_dyn_cb));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return insn;
|
|
|
|
}
|
|
|
|
|
2021-02-13 14:03:22 +01:00
|
|
|
/* Internal context for this TranslationBlock */
|
2017-08-31 00:39:53 +02:00
|
|
|
struct qemu_plugin_tb {
|
|
|
|
GPtrArray *insns;
|
|
|
|
size_t n;
|
|
|
|
uint64_t vaddr;
|
|
|
|
uint64_t vaddr2;
|
|
|
|
void *haddr1;
|
|
|
|
void *haddr2;
|
2021-02-13 14:03:22 +01:00
|
|
|
bool mem_only;
|
2023-01-24 19:01:23 +01:00
|
|
|
|
|
|
|
/* if set, the TB calls helpers that might access guest memory */
|
|
|
|
bool mem_helper;
|
|
|
|
|
2017-08-31 00:39:53 +02:00
|
|
|
GArray *cbs[PLUGIN_N_CB_SUBTYPES];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_plugin_tb_insn_get(): get next plugin record for translation.
|
2021-10-26 12:22:26 +02:00
|
|
|
* @tb: the internal tb context
|
|
|
|
* @pc: address of instruction
|
2017-08-31 00:39:53 +02:00
|
|
|
*/
|
|
|
|
static inline
|
2021-10-26 12:22:26 +02:00
|
|
|
struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb,
|
|
|
|
uint64_t pc)
|
2017-08-31 00:39:53 +02:00
|
|
|
{
|
|
|
|
struct qemu_plugin_insn *insn;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if (unlikely(tb->n == tb->insns->len)) {
|
|
|
|
struct qemu_plugin_insn *new_insn = qemu_plugin_insn_alloc();
|
|
|
|
g_ptr_array_add(tb->insns, new_insn);
|
|
|
|
}
|
|
|
|
insn = g_ptr_array_index(tb->insns, tb->n++);
|
|
|
|
g_byte_array_set_size(insn->data, 0);
|
|
|
|
insn->calls_helpers = false;
|
|
|
|
insn->mem_helper = false;
|
2021-10-26 12:22:26 +02:00
|
|
|
insn->vaddr = pc;
|
2017-08-31 00:39:53 +02:00
|
|
|
|
|
|
|
for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
|
|
|
|
for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
|
|
|
|
g_array_set_size(insn->cbs[i][j], 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return insn;
|
|
|
|
}
|
|
|
|
|
2024-02-27 15:43:28 +01:00
|
|
|
/**
|
|
|
|
* struct CPUPluginState - per-CPU state for plugins
|
|
|
|
* @event_mask: plugin event bitmap. Modified only via async work.
|
|
|
|
*/
|
|
|
|
struct CPUPluginState {
|
|
|
|
DECLARE_BITMAP(event_mask, QEMU_PLUGIN_EV_MAX);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_plugin_create_vcpu_state: allocate plugin state
|
|
|
|
*/
|
|
|
|
CPUPluginState *qemu_plugin_create_vcpu_state(void);
|
|
|
|
|
2017-08-31 00:39:53 +02:00
|
|
|
void qemu_plugin_vcpu_init_hook(CPUState *cpu);
|
|
|
|
void qemu_plugin_vcpu_exit_hook(CPUState *cpu);
|
|
|
|
void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu_plugin_tb *tb);
|
|
|
|
void qemu_plugin_vcpu_idle_cb(CPUState *cpu);
|
|
|
|
void qemu_plugin_vcpu_resume_cb(CPUState *cpu);
|
|
|
|
void
|
|
|
|
qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1,
|
|
|
|
uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5,
|
|
|
|
uint64_t a6, uint64_t a7, uint64_t a8);
|
|
|
|
void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret);
|
|
|
|
|
2021-07-26 23:48:30 +02:00
|
|
|
void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
|
|
|
|
MemOpIdx oi, enum qemu_plugin_mem_rw rw);
|
2017-08-31 00:39:53 +02:00
|
|
|
|
|
|
|
void qemu_plugin_flush_cb(void);
|
|
|
|
|
|
|
|
void qemu_plugin_atexit_cb(void);
|
|
|
|
|
|
|
|
void qemu_plugin_add_dyn_cb_arr(GArray *arr);
|
|
|
|
|
2023-03-15 18:43:16 +01:00
|
|
|
static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
|
|
|
|
{
|
|
|
|
cpu->plugin_mem_cbs = NULL;
|
|
|
|
}
|
2017-08-31 00:39:53 +02:00
|
|
|
|
2021-07-21 01:26:54 +02:00
|
|
|
/**
|
|
|
|
* qemu_plugin_user_exit(): clean-up callbacks before calling exit callbacks
|
|
|
|
*
|
|
|
|
* This is a user-mode only helper that ensure we have fully cleared
|
|
|
|
* callbacks from all threads before calling the exit callbacks. This
|
|
|
|
* is so the plugins themselves don't have to jump through hoops to
|
|
|
|
* guard against race conditions.
|
|
|
|
*/
|
|
|
|
void qemu_plugin_user_exit(void);
|
|
|
|
|
2022-10-04 13:52:21 +02:00
|
|
|
/**
|
|
|
|
* qemu_plugin_user_prefork_lock(): take plugin lock before forking
|
|
|
|
*
|
|
|
|
* This is a user-mode only helper to take the internal plugin lock
|
|
|
|
* before a fork event. This is ensure a consistent lock state
|
|
|
|
*/
|
|
|
|
void qemu_plugin_user_prefork_lock(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_plugin_user_postfork(): reset the plugin lock
|
|
|
|
* @is_child: is this thread the child
|
|
|
|
*
|
|
|
|
* This user-mode only helper resets the lock state after a fork so we
|
|
|
|
* can continue using the plugin interface.
|
|
|
|
*/
|
|
|
|
void qemu_plugin_user_postfork(bool is_child);
|
|
|
|
|
2017-08-31 00:39:53 +02:00
|
|
|
#else /* !CONFIG_PLUGIN */
|
|
|
|
|
2020-05-13 19:31:54 +02:00
|
|
|
static inline void qemu_plugin_add_opts(void)
|
|
|
|
{ }
|
|
|
|
|
2023-10-04 14:00:08 +02:00
|
|
|
static inline void qemu_plugin_opt_parse(const char *optstr,
|
2020-05-13 19:31:54 +02:00
|
|
|
QemuPluginList *head)
|
|
|
|
{
|
|
|
|
error_report("plugin interface not enabled in this build");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-10-27 09:58:26 +01:00
|
|
|
static inline int qemu_plugin_load_list(QemuPluginList *head, Error **errp)
|
2020-05-13 19:31:54 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-31 00:39:53 +02:00
|
|
|
static inline void qemu_plugin_vcpu_init_hook(CPUState *cpu)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_tb_trans_cb(CPUState *cpu,
|
|
|
|
struct qemu_plugin_tb *tb)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_vcpu_idle_cb(CPUState *cpu)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2,
|
|
|
|
uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6,
|
|
|
|
uint64_t a7, uint64_t a8)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline
|
|
|
|
void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_t num, int64_t ret)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr,
|
2021-07-26 23:48:30 +02:00
|
|
|
MemOpIdx oi,
|
|
|
|
enum qemu_plugin_mem_rw rw)
|
2017-08-31 00:39:53 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_flush_cb(void)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_atexit_cb(void)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline
|
|
|
|
void qemu_plugin_add_dyn_cb_arr(GArray *arr)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu)
|
|
|
|
{ }
|
|
|
|
|
2021-07-21 01:26:54 +02:00
|
|
|
static inline void qemu_plugin_user_exit(void)
|
|
|
|
{ }
|
2022-10-04 13:52:21 +02:00
|
|
|
|
|
|
|
static inline void qemu_plugin_user_prefork_lock(void)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static inline void qemu_plugin_user_postfork(bool is_child)
|
|
|
|
{ }
|
|
|
|
|
2017-08-31 00:39:53 +02:00
|
|
|
#endif /* !CONFIG_PLUGIN */
|
|
|
|
|
|
|
|
#endif /* QEMU_PLUGIN_H */
|