Skip to contentSkip to conversation

initial plugin support

RFC In review Emilio G. Cota 5 participants 17 messages Sep 6, 2017 – Sep 26, 2017 Kbuild

lore
Share & tools
lore
#0 lore raw

cover initial plugin support

Related threads:
[PATCH 00/13] instrument: Add basic event instrumentation
Date: Mon, 24 Jul 2017 20:02:24 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html
and
[PATCH v4 00/20] instrument: Add basic event instrumentation
Date: Wed, 6 Sep 2017 20:22:41 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html

This set does something similar to the instrumentation patches by Lluis, but with a different implementation (and for now less events).

My focus has been on working on the skeleton of a (pseudo) stable API, as Stefan requested. Of course more events would have to be added, but before spending more time on this I'd like to get some feedback on the core of the design. Patch 2 has all the details.

Note: yes, patch 1 is not used in the series, but this is an RFC. It's there because it will be needed to get the tb->plugin_mask when deciding whether to generate a mem_cb helper when generating loads/stores from TCG.

This set applies on top of:
https://github.com/cota/qemu/tree/tcg-generic-15%2Bmulti-tcg-v4-parallel

The tree can be fetched from:
https://github.com/cota/qemu/tree/plugins

Thanks,

Emilio

---
 Makefile                  |   7 +-
 Makefile.objs             |   1 +
 accel/tcg/cpu-exec.c      |   6 +-
 accel/tcg/translate-all.c |   9 +-
 configure                 |  52 ++++
 include/exec/exec-all.h   |   2 +
 include/exec/tb-hash-xx.h |  26 +-
 include/exec/tb-hash.h    |   6 +-
 include/exec/tb-lookup.h  |   1 +
 include/qemu/plugin-api.h | 105 +++++++
 include/qemu/plugin.h     |  74 +++++
 include/qom/cpu.h         |   4 +
 linux-user/main.c         |  18 ++
 plugin.c                  | 519 ++++++++++++++++++++++++++++++++
 qemu-options.hx           |  17 ++
 qemu-plugins.symbols      |   6 +
 qom/cpu.c                 |   3 +
 tcg/tcg-op.c              |  10 +-
 tcg/tcg.h                 |   2 +-
 tests/qht-bench.c         |   2 +-
 vl.c                      |  10 +
 21 files changed, 857 insertions(+), 23 deletions(-)
#1 lore raw

RFC 1/6 tcg: keep a pointer to the current TB in TCGContext

Instead of just a copy of tb->cflags. This gives access in TCG to other fields in TB, which will be needed when we implement callbacks from memory accesses performed by TCG-translated code.

---
 tcg/tcg.h                 |  2 +-
 accel/tcg/translate-all.c |  3 ++-
 tcg/tcg-op.c              | 10 +++++-----
 3 files changed, 8 insertions(+), 7 deletions(-)
tcg/tcg.h +1 -1
index cf4eeaf..86ca604 100644
@@ -676,7 +676,7 @@ struct TCGContext {
     uintptr_t *tb_jmp_target_addr; /* tb->jmp_target_addr if !USE_DIRECT_JUMP */
 
     TCGRegSet reserved_regs;
-    uint32_t tb_cflags; /* cflags of the current TB */
+    const TranslationBlock *tb;
     intptr_t current_frame_offset;
     intptr_t frame_start;
     intptr_t frame_end;
accel/tcg/translate-all.c +2 -1
index 69cc7dc..7332afc 100644
@@ -1600,7 +1600,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
     tb->flags = flags;
     tb->cflags = cflags;
     tb->trace_vcpu_dstate = *cpu->trace_dstate;
-    tcg_ctx->tb_cflags = cflags;
 
 #ifdef CONFIG_PROFILER
     /* includes aborted translations because of exceptions */
@@ -1610,9 +1609,11 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
 
     tcg_func_start(tcg_ctx);
 
+    tcg_ctx->tb = tb;
     tcg_ctx->cpu = ENV_GET_CPU(env);
     gen_intermediate_code(cpu, tb);
     tcg_ctx->cpu = NULL;
+    tcg_ctx->tb = NULL;
 
     trace_translate_block(tb, tb->pc, tb->tc.ptr);
 
tcg/tcg-op.c +5 -5
index 2a8bf90..32449d2 100644
@@ -150,7 +150,7 @@ void tcg_gen_op6(TCGContext *ctx, TCGOpcode opc, TCGArg a1, TCGArg a2,
 
 void tcg_gen_mb(TCGBar mb_type)
 {
-    if (tcg_ctx->tb_cflags & CF_PARALLEL) {
+    if (tb_cflags(tcg_ctx->tb) & CF_PARALLEL) {
         tcg_gen_op1(tcg_ctx, INDEX_op_mb, mb_type);
     }
 }
@@ -2794,7 +2794,7 @@ void tcg_gen_atomic_cmpxchg_i32(TCGv_i32 retv, TCGv addr, TCGv_i32 cmpv,
 {
     memop = tcg_canonicalize_memop(memop, 0, 0);
 
-    if (!(tcg_ctx->tb_cflags & CF_PARALLEL)) {
+    if (!(tb_cflags(tcg_ctx->tb) & CF_PARALLEL)) {
         TCGv_i32 t1 = tcg_temp_new_i32();
         TCGv_i32 t2 = tcg_temp_new_i32();
 
@@ -2838,7 +2838,7 @@ void tcg_gen_atomic_cmpxchg_i64(TCGv_i64 retv, TCGv addr, TCGv_i64 cmpv,
 {
     memop = tcg_canonicalize_memop(memop, 1, 0);
 
-    if (!(tcg_ctx->tb_cflags & CF_PARALLEL)) {
+    if (!(tb_cflags(tcg_ctx->tb) & CF_PARALLEL)) {
         TCGv_i64 t1 = tcg_temp_new_i64();
         TCGv_i64 t2 = tcg_temp_new_i64();
 
@@ -3015,7 +3015,7 @@ static void * const table_##NAME[16] = {                                \
 void tcg_gen_atomic_##NAME##_i32                                        \
     (TCGv_i32 ret, TCGv addr, TCGv_i32 val, TCGArg idx, TCGMemOp memop) \
 {                                                                       \
-    if (tcg_ctx->tb_cflags & CF_PARALLEL) {                             \
+    if (tb_cflags(tcg_ctx->tb) & CF_PARALLEL) {                         \
         do_atomic_op_i32(ret, addr, val, idx, memop, table_##NAME);     \
     } else {                                                            \
         do_nonatomic_op_i32(ret, addr, val, idx, memop, NEW,            \
@@ -3025,7 +3025,7 @@ void tcg_gen_atomic_##NAME##_i32                                        \
 void tcg_gen_atomic_##NAME##_i64                                        \
     (TCGv_i64 ret, TCGv addr, TCGv_i64 val, TCGArg idx, TCGMemOp memop) \
 {                                                                       \
-    if (tcg_ctx->tb_cflags & CF_PARALLEL) {                             \
+    if (tb_cflags(tcg_ctx->tb) & CF_PARALLEL) {                         \
         do_atomic_op_i64(ret, addr, val, idx, memop, table_##NAME);     \
     } else {                                                            \
         do_nonatomic_op_i64(ret, addr, val, idx, memop, NEW,            \
Signature · 1 line
2.7.4
#2 lore raw

RFC 2/6 plugin: add initial plugin support

In reply to Emilio G. Cota

This is still a work in progress, hence the limited public-facing API. I wanted to add more events (e.g. memory and basic block callbacks), but before spending more time on time I'd like to see whether this direction is appreciated by others.

The goals are to:
- Have a simple implementation that shares nothing with tracing code.

- Make sure we cannot deadlock, particularly under MTTCG. For this, we acquire a lock when called from plugin code, and keep
RCU lists of callbacks so that we do not have to hold the lock when calling the callbacks. This is also for performance, since some callbacks (e.g. memory access callbacks) might be called very frequently.
* A consequence of this is that we keep our own list of CPUs, so that we do not have to worry about locking order wrt cpu_list_lock.
* Some functions such as vcpu_for_each will call plugin code with the lock held. Instead of making the lock recursive, just document that the function called by vcpu_for_each cannot call any other plugin API.

- Support as many plugins as the user wants (e.g. -plugin foo -plugin bar), just like other tools (e.g. dynamorio) do.

- Support the installation/uninstallation of a plugin any time (i.e. from any callback from the guest, or from QAPI/QMP).

- Avoid malicious plugins from abusing the API. This is done by:
* Adding a qemu_plugin_id_t that all calls need to use. This is a unique id per plugin.
* Hiding CPUState * under cpu_index. Plugin code can keep per-vcpu data by using said index (say to index an array).
* Only exporting the relevant qemu_plugin symbols to the plugins by passing --dynamic-file to the linker (when supported), instead of exporting all symbols with -rdynamic.

- Performance: registering/unregistering callbacks is slow. But this is very infrequent; we want performance when calling (or not) callbacks. Using RCU is great for this. The only difficulty is when uninstalling a plugin, where some callbacks might still be called after the uninstall returns. An alternative would be to use r/w locks, but that would complicate code quite a bit for very little gain; I suspect most plugins will just run until QEMU exits.

Some design decisions:
- I considered registering callbacks per-vcpu, but really I don't see the use case for it (would complicate the API and 99% of plugins won't care, so I'd rather make that 1% slower by letting them discard unwanted callbacks).

- Using a per-vcpu mask is key to allow for maximum performance/scalability. With RCU callbacks and async work we can ensure no events are missed, and we keep maximum scalability (again, we assume callbacks are a lot more common than callback registration).

- Last, 'plugin' vs. 'instrumentation' naming: I think instrumentation is a subset of the functionality that plugins can provide. IOW, in the future not all plugins might be considered instrumentation, even if currently my goal is to use them for that purpose.

---
 Makefile.objs             |   1 +
 include/exec/exec-all.h   |   2 +
 include/exec/tb-hash-xx.h |  26 ++-
 include/exec/tb-hash.h    |   6 +-
 include/exec/tb-lookup.h  |   1 +
 include/qemu/plugin-api.h | 105 ++++++++++
 include/qemu/plugin.h     |  74 +++++++
 include/qom/cpu.h         |   4 +
 accel/tcg/cpu-exec.c      |   6 +-
 accel/tcg/translate-all.c |   6 +-
 plugin.c                  | 519 ++++++++++++++++++++++++++++++++++++++++++++++
 qom/cpu.c                 |   3 +
 tests/qht-bench.c         |   2 +-
 qemu-plugins.symbols      |   6 +
 14 files changed, 746 insertions(+), 15 deletions(-)
 create mode 100644 include/qemu/plugin-api.h
 create mode 100644 include/qemu/plugin.h
 create mode 100644 plugin.c
 create mode 100644 qemu-plugins.symbols
Makefile.objs +1 -0
index 24a4ea0..52cc89c 100644
@@ -84,6 +84,7 @@ endif
 #######################################################################
 # Target-independent parts used in system and user emulation
 common-obj-y += cpus-common.o
+common-obj-$(CONFIG_PLUGINS) += plugin.o
 common-obj-y += hw/
 common-obj-y += qom/
 common-obj-y += disas/
include/exec/exec-all.h +2 -0
index 1d4e977..add9423 100644
@@ -346,6 +346,8 @@ struct TranslationBlock {
     /* Per-vCPU dynamic tracing state used to generate this TB */
     uint32_t trace_vcpu_dstate;
 
+    uint32_t plugin_mask;
+
     struct tb_tc tc;
 
     /* original tb when cflags has CF_NOCACHE */
include/exec/tb-hash-xx.h +17 -9
index 747a9a6..4015d6a 100644
@@ -49,7 +49,8 @@
  * contiguous in memory.
  */
 static inline uint32_t
-tb_hash_func7(uint64_t a0, uint64_t b0, uint32_t e, uint32_t f, uint32_t g)
+tb_hash_func8(uint64_t a0, uint64_t b0, uint32_t e, uint32_t f, uint32_t g,
+              uint32_t h)
 {
     uint32_t v1 = TB_HASH_XX_SEED + PRIME32_1 + PRIME32_2;
     uint32_t v2 = TB_HASH_XX_SEED + PRIME32_2;
@@ -77,17 +78,24 @@ tb_hash_func7(uint64_t a0, uint64_t b0, uint32_t e, uint32_t f, uint32_t g)
     v4 = rol32(v4, 13);
     v4 *= PRIME32_1;
 
-    h32 = rol32(v1, 1) + rol32(v2, 7) + rol32(v3, 12) + rol32(v4, 18);
-    h32 += 28;
+    v1 += e * PRIME32_2;
+    v1 = rol32(v1, 13);
+    v1 *= PRIME32_1;
 
-    h32 += e * PRIME32_3;
-    h32  = rol32(h32, 17) * PRIME32_4;
+    v2 += f * PRIME32_2;
+    v2 = rol32(v2, 13);
+    v2 *= PRIME32_1;
 
-    h32 += f * PRIME32_3;
-    h32  = rol32(h32, 17) * PRIME32_4;
+    v3 += g * PRIME32_2;
+    v3 = rol32(v3, 13);
+    v3 *= PRIME32_1;
 
-    h32 += g * PRIME32_3;
-    h32  = rol32(h32, 17) * PRIME32_4;
+    v4 += h * PRIME32_2;
+    v4 = rol32(v4, 13);
+    v4 *= PRIME32_1;
+
+    h32 = rol32(v1, 1) + rol32(v2, 7) + rol32(v3, 12) + rol32(v4, 18);
+    h32 += 32;
 
     h32 ^= h32 >> 15;
     h32 *= PRIME32_2;
include/exec/tb-hash.h +4 -2
index 0526c4f..f501dd1 100644
@@ -59,9 +59,11 @@ static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)
 
 static inline
 uint32_t tb_hash_func(tb_page_addr_t phys_pc, target_ulong pc, uint32_t flags,
-                      uint32_t cf_mask, uint32_t trace_vcpu_dstate)
+                      uint32_t cf_mask, uint32_t trace_vcpu_dstate,
+                      uint32_t plugin_mask)
 {
-    return tb_hash_func7(phys_pc, pc, flags, cf_mask, trace_vcpu_dstate);
+    return tb_hash_func8(phys_pc, pc, flags, cf_mask, trace_vcpu_dstate,
+                         plugin_mask);
 }
 
 #endif
include/exec/tb-lookup.h +1 -0
index 2961385..c879903 100644
@@ -35,6 +35,7 @@ tb_lookup__cpu_state(CPUState *cpu, target_ulong *pc, target_ulong *cs_base,
                tb->cs_base == *cs_base &&
                tb->flags == *flags &&
                tb->trace_vcpu_dstate == *cpu->trace_dstate &&
+               tb->plugin_mask == *cpu->plugin_mask &&
                (tb_cflags(tb) & (CF_HASH_MASK | CF_INVALID)) == cf_mask)) {
         return tb;
     }
include/qemu/plugin-api.h new file +105 -0

105 lines not on this page · raw message

include/qemu/plugin.h new file +74 -0

74 lines not on this page · raw message

include/qom/cpu.h +4 -0
index 25eefea..899b2d6 100644
@@ -27,6 +27,7 @@
 #include "qemu/bitmap.h"
 #include "qemu/queue.h"
 #include "qemu/thread.h"
+#include "qemu/plugin.h"
 
 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
                                      void *opaque);
@@ -305,6 +306,7 @@ struct qemu_work_item;
  * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
  *                        to @trace_dstate).
  * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
+ * @plugin_mask: Plugin event bitmap. Modified only via async work.
  *
  * State of one CPU core or thread.
  */
@@ -377,6 +379,8 @@ struct CPUState {
     DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
     DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
 
+    DECLARE_BITMAP(plugin_mask, QEMU_PLUGIN_EV_MAX);
+
     /* TODO Move common fields from CPUArchState here. */
     int cpu_index; /* used by alpha TCG */
     uint32_t halted; /* used by alpha, cris, ppc TCG */
accel/tcg/cpu-exec.c +5 -1
index e38479b..5523c44 100644
@@ -271,6 +271,7 @@ struct tb_desc {
     uint32_t flags;
     uint32_t cf_mask;
     uint32_t trace_vcpu_dstate;
+    uint32_t plugin_mask;
 };
 
 static bool tb_lookup_cmp(const void *p, const void *d)
@@ -283,6 +284,7 @@ static bool tb_lookup_cmp(const void *p, const void *d)
         tb->cs_base == desc->cs_base &&
         tb->flags == desc->flags &&
         tb->trace_vcpu_dstate == desc->trace_vcpu_dstate &&
+        tb->plugin_mask == desc->plugin_mask &&
         (tb_cflags(tb) & (CF_HASH_MASK | CF_INVALID)) == desc->cf_mask) {
         /* check next page if needed */
         if (tb->page_addr[1] == -1) {
@@ -314,10 +316,12 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
     desc.flags = flags;
     desc.cf_mask = cf_mask;
     desc.trace_vcpu_dstate = *cpu->trace_dstate;
+    desc.plugin_mask = *cpu->plugin_mask;
     desc.pc = pc;
     phys_pc = get_page_addr_code(desc.env, pc);
     desc.phys_page1 = phys_pc & TARGET_PAGE_MASK;
-    h = tb_hash_func(phys_pc, pc, flags, cf_mask, *cpu->trace_dstate);
+    h = tb_hash_func(phys_pc, pc, flags, cf_mask, *cpu->trace_dstate,
+                     *cpu->plugin_mask);
     return qht_lookup_custom(&tb_ctx.htable, tb_lookup_cmp, &desc, h);
 }
 
accel/tcg/translate-all.c +4 -2
index 7332afc..17abfcc 100644
@@ -1027,6 +1027,7 @@ static bool tb_cmp(const void *ap, const void *bp)
         a->flags == b->flags &&
         (tb_cflags(a) & CF_HASH_MASK) == (tb_cflags(b) & CF_HASH_MASK) &&
         a->trace_vcpu_dstate == b->trace_vcpu_dstate &&
+        a->plugin_mask == b->plugin_mask &&
         a->page_addr[0] == b->page_addr[0] &&
         a->page_addr[1] == b->page_addr[1];
 }
@@ -1344,7 +1345,7 @@ static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)
     /* remove the TB from the hash list */
     phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
     h = tb_hash_func(phys_pc, tb->pc, tb->flags, tb->cflags & CF_HASH_MASK,
-                     tb->trace_vcpu_dstate);
+                     tb->trace_vcpu_dstate, tb->plugin_mask);
     if (!qht_remove(&tb_ctx.htable, tb, h)) {
         return;
     }
@@ -1533,7 +1534,7 @@ tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
 
     /* add in the hash table */
     h = tb_hash_func(phys_pc, tb->pc, tb->flags, tb->cflags & CF_HASH_MASK,
-                     tb->trace_vcpu_dstate);
+                     tb->trace_vcpu_dstate, tb->plugin_mask);
     existing_tb = qht_insert(&tb_ctx.htable, tb, h);
 
     /* remove TB from the page(s) if we couldn't insert it */
@@ -1600,6 +1601,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
     tb->flags = flags;
     tb->cflags = cflags;
     tb->trace_vcpu_dstate = *cpu->trace_dstate;
+    tb->plugin_mask = *cpu->plugin_mask;
 
 #ifdef CONFIG_PROFILER
     /* includes aborted translations because of exceptions */
plugin.c new file +519 -0

519 lines not on this page · raw message

qom/cpu.c +3 -0
index 4f38db0..60b4a56 100644
@@ -31,6 +31,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/qdev-properties.h"
 #include "trace-root.h"
+#include "qemu/plugin.h"
 
 CPUInterruptHandler cpu_interrupt_handler;
 
@@ -368,6 +369,7 @@ static void cpu_common_realizefn(DeviceState *dev, Error **errp)
 
     /* NOTE: latest generic point where the cpu is fully realized */
     trace_init_vcpu(cpu);
+    qemu_plugin_vcpu_init_hook(cpu);
 }
 
 static void cpu_common_unrealizefn(DeviceState *dev, Error **errp)
@@ -375,6 +377,7 @@ static void cpu_common_unrealizefn(DeviceState *dev, Error **errp)
     CPUState *cpu = CPU(dev);
     /* NOTE: latest generic point before the cpu is fully unrealized */
     trace_fini_vcpu(cpu);
+    qemu_plugin_vcpu_exit_hook(cpu);
     cpu_exec_unrealizefn(cpu);
 }
 
tests/qht-bench.c +1 -1
index 6c2eb8e..86cd85a 100644
@@ -103,7 +103,7 @@ static bool is_equal(const void *ap, const void *bp)
 
 static inline uint32_t h(unsigned long v)
 {
-    return tb_hash_func7(v, 0, 0, 0, 0);
+    return tb_hash_func8(v, 0, 0, 0, 0, 0);
 }
 
 /*
qemu-plugins.symbols new file +6 -0
new file mode 100644
index 0000000..2b8463e
@@ -0,0 +1,6 @@
+{
+  qemu_plugin_uninstall;
+  qemu_plugin_register_vcpu_init_cb;
+  qemu_plugin_register_vcpu_exit_cb;
+  qemu_plugin_vcpu_for_each;
+};
Signature · 1 line
2.7.4
#3 lore raw

Re: [Qemu-devel] [RFC 2/6] plugin: add initial plugin support

Quoted 3 lines · Emilio G. Cota
plugin.c new file +0 -0
@@ -0,0 +1,519 @@

(snip)

plugin.c line 62

Quoted 7 lines
plugin.c new file +7 -0
+    /*
+     * @lock protects the struct as well as ctx->uninstalling.
+     * The lock must be acquired by all API ops. Since some API ops
+     * call plugin code repeatedly (e.g. vcpu_for_each), we keep
+     * a counter to allow for recursive acquisitions.
+     */
+    QemuMutex lock;

This comment is outdated. The lock is not recursive anymore (it used to be).

		E.
#4 lore raw

Re: [Qemu-devel] [RFC 2/6] plugin: add initial plugin support

In reply to Emilio G. Cota

On 06.09.2017 22:28, Emilio G. Cota wrote:
[...]

plugin.c line 7

Quoted 13 lines
plugin.c new file +7 -0
new file mode 100644
index 0000000..3cd19df
@@ -0,0 +1,519 @@
+/* plugin.c - QEMU Plugin interface
+ *
+ * Copyright (C) 2017, Emilio G. Cota <[email protected]>
+ *
+ * License: GNU GPL, version 2 or later.
+ *   See the COPYING file in the top-level directory.
+ */

If you introduce new .c files, please add some sentences with a proper description in the header with some very high level description about what the code in the file is supposed to be doing. Just reading "plugin interface" is not really very helpful when trying to understand new code.

Thanks,
Thomas

#5 lore raw
Quoted 9 lines · Thomas Huth

Sorry for my ignorance, but if you send a patch series like this, could you please elaborate a little bit more on the topic what this all is about? In this cover letter, you basically give only some pointers about other patch series and point the reader to patch 2, but also patch 2 does not really have a proper *description* of what this is really all about. Sure, it's about plugins, but what kind of plugins? Audio? Video? CPU? Everything? If you send RFC, you should properly describe your vision first, and maybe give some examples, before you jump into the details.

Quoted 4 lines · Thomas Huth

If you introduce new .c files, please add some sentences with a proper description in the header with some very high level description about what the code in the file is supposed to be doing. Just reading "plugin interface" is not really very helpful when trying to understand new code.

You make very good points.

It was an RFC aimed mostly at people who had been following the previous threads on this topic. However, I understand people who had not followed those threads lacked the necessary context. Sorry about that.

As Stefan pointed out, the goal is to provide some sort of instrumentation a la Pin/DynamoRIO. The extent of said instrumentation is still to be decided, although we agree on that at least we should be able to "instrument" (i.e. observe via callbacks) instructions executed and memory accesses in the guest.

Currently I'm working bottom-up on this; my goal is to simulate an ARMv8 system, so for that I need to feed the instruction stream and memory accesses to the timing model. This could be done in different ways, so I'll evaluate the alternatives to see how they affect performance.

Note that plugin (or instrumentation) support has been implemented in the past. However, this was for academic projects and the code was never submitted to the list. Some examples:
- QTrace
code: https://github.com/x-y-z/QTRACE
paper: http://ieeexplore.ieee.org/document/7095810/ (paywall)
- Decaf
code: https://github.com/sycurelab/DECAF
paper: http://www.cs.ucr.edu/~heng/pubs/issta14.pdf
- QSim
code: https://github.com/gtcasl/qsim
paper: http://ieeexplore.ieee.org/abstract/document/6429075/ (paywall)
- PEMU
code: https://github.com/utds3lab/pemu
paper: http://www.utdallas.edu/~zxl111930/file/VEE15.pdf
- ESESC
code: https://github.com/masc-ucsc/esesc
paper: https://users.soe.ucsc.edu/~renau/docs/hpca13.pdf

Thanks,

Emilio

#6 lore raw

RFC 3/6 configure: add --enable-plugins

In reply to Emilio G. Cota

For now only add it for ELF platforms, since we rely on the linker's --dynamic-list flag to pass a list of symbols to be exported to the executable. An alternative would be to use -rdynamic, but that would expose all of QEMU's objects to plugins.

I have no experience with non-ELF systems but I suspect adding support for those should be pretty easy.

---
 configure | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
configure +52 -0
index 987f59b..45f7af5 100755
@@ -30,6 +30,7 @@ TMPO="${TMPDIR1}/${TMPB}.o"
 TMPCXX="${TMPDIR1}/${TMPB}.cxx"
 TMPE="${TMPDIR1}/${TMPB}.exe"
 TMPMO="${TMPDIR1}/${TMPB}.mo"
+TMPTXT="${TMPDIR1}/${TMPB}.txt"
 
 rm -f config.log
 
@@ -404,6 +405,8 @@ tcmalloc="no"
 jemalloc="no"
 replication="yes"
 vxhs=""
+plugins="no"
+ld_dynamic_list="no"
 
 supported_cpu="no"
 supported_os="no"
@@ -1282,6 +1285,10 @@ for opt do
   ;;
   --enable-vxhs) vxhs="yes"
   ;;
+  --enable-plugins) plugins="yes"
+  ;;
+  --disable-plugins) plugins="no"
+  ;;
   *)
       echo "ERROR: unknown option $opt"
       echo "Try '$0 --help' for more information"
@@ -1442,6 +1449,8 @@ Advanced options (experts only):
                            xen pv domain builder
   --enable-debug-stack-usage
                            track the maximum stack usage of stacks created by qemu_alloc_stack
+  --enable-plugins
+                           enable plugins via shared library loading
 
 Optional features, enabled with --enable-FEATURE and
 disabled with --disable-FEATURE, default is enabled if available:
@@ -4745,6 +4754,42 @@ if compile_prog "" "" ; then
   atomic64=yes
 fi
 
+#########################################
+# See if --dynamic-list is supported by the linker
+
+cat > $TMPTXT <<EOF
+{
+  foo;
+};
+EOF
+
+cat > $TMPC <<EOF
+#include <stdio.h>
+void foo(void);
+
+void foo(void)
+{
+  printf("foo\n");
+}
+
+int main(void)
+{
+  foo();
+  return 0;
+}
+EOF
+
+if compile_prog "" "-Wl,--dynamic-list=$TMPTXT" ; then
+  ld_dynamic_list="yes"
+else
+  if test "$plugins" = "yes" ; then
+    error_exit \
+        "Plugin support requires specifying a set of symbols that " \
+        "are exported to plugins. Unfortunately your linker doesn't " \
+        "support the flag (--dynamic-list) used for this purpose."
+  fi
+fi
+
 ########################################
 # check if getauxval is available.
 
@@ -5388,6 +5433,7 @@ echo "jemalloc support  $jemalloc"
 echo "avx2 optimization $avx2_opt"
 echo "replication support $replication"
 echo "VxHS block device $vxhs"
+echo "plugin support    $plugins"
 
 if test "$sdl_too_old" = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -6053,6 +6099,12 @@ if test "$vxhs" = "yes" ; then
   echo "VXHS_LIBS=$vxhs_libs" >> $config_host_mak
 fi
 
+if test "$plugins" = "yes" ; then
+    echo "CONFIG_PLUGINS=y" >> $config_host_mak
+    LIBS="-ldl $LIBS"
+    LDFLAGS="-Wl,--dynamic-list=\$(SRC_PATH)/qemu-plugins.symbols $LDFLAGS"
+fi
+
 if test "$tcg_interpreter" = "yes"; then
   QEMU_INCLUDES="-I\$(SRC_PATH)/tcg/tci $QEMU_INCLUDES"
 elif test "$ARCH" = "sparc64" ; then
Signature · 1 line
2.7.4
#7 lore raw

RFC 4/6 Makefile: install qemu-api.h as include/qemu-plugin.h

In reply to Emilio G. Cota
---
 Makefile | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Makefile +6 -1
index ef72148..326867e 100644
@@ -602,8 +602,10 @@ ifneq (,$(findstring qemu-ga,$(TOOLS)))
 endif
 endif
 
+install-includedir:
+	$(INSTALL_DIR) "$(DESTDIR)$(includedir)"
 
-install: all $(if $(BUILD_DOCS),install-doc) install-datadir install-localstatedir
+install: all $(if $(BUILD_DOCS),install-doc) install-datadir install-localstatedir install-includedir
 ifneq ($(TOOLS),)
 	$(call install-prog,$(subst qemu-ga,qemu-ga$(EXESUF),$(TOOLS)),$(DESTDIR)$(bindir))
 endif
@@ -626,6 +628,9 @@ endif
 ifeq ($(CONFIG_GTK),y)
 	$(MAKE) -C po $@
 endif
+ifeq ($(CONFIG_PLUGINS),y)
+	$(INSTALL_DATA) $(SRC_PATH)/include/qemu/plugin-api.h "$(DESTDIR)$(includedir)/qemu-plugin.h"
+endif
 	$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/keymaps"
 	set -e; for x in $(KEYMAPS); do \
 		$(INSTALL_DATA) $(SRC_PATH)/pc-bios/keymaps/$$x "$(DESTDIR)$(qemu_datadir)/keymaps"; \
Signature · 1 line
2.7.4
#8 lore raw

RFC 5/6 vl: support -plugin option

In reply to Emilio G. Cota

From: Lluís Vilanova <[email protected]>

[ cota: s/instrument/plugin ]
---
 vl.c            | 10 ++++++++++
 qemu-options.hx | 17 +++++++++++++++++
 2 files changed, 27 insertions(+)
vl.c +10 -0
index fb6b2ef..d3d8adc 100644
@@ -118,6 +118,7 @@ int main(int argc, char **argv)
 
 #include "trace-root.h"
 #include "trace/control.h"
+#include "qemu/plugin.h"
 #include "qemu/queue.h"
 #include "sysemu/arch_init.h"
 
@@ -3031,6 +3032,7 @@ int main(int argc, char **argv, char **envp)
     } BlockdevOptions_queue;
     QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
         = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
+    struct qemu_plugin_list plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
 
     module_call_init(MODULE_INIT_TRACE);
 
@@ -3058,6 +3060,7 @@ int main(int argc, char **argv, char **envp)
     qemu_add_opts(&qemu_global_opts);
     qemu_add_opts(&qemu_mon_opts);
     qemu_add_opts(&qemu_trace_opts);
+    qemu_plugin_add_opts();
     qemu_add_opts(&qemu_option_rom_opts);
     qemu_add_opts(&qemu_machine_opts);
     qemu_add_opts(&qemu_accel_opts);
@@ -3999,6 +4002,9 @@ int main(int argc, char **argv, char **envp)
                 g_free(trace_file);
                 trace_file = trace_opt_parse(optarg);
                 break;
+            case QEMU_OPTION_plugin:
+                qemu_plugin_opt_parse(optarg, &plugin_list);
+                break;
             case QEMU_OPTION_readconfig:
                 {
                     int ret = qemu_read_config_file(optarg);
@@ -4185,6 +4191,10 @@ int main(int argc, char **argv, char **envp)
     }
     trace_init_file(trace_file);
 
+    if (qemu_plugin_load_list(&plugin_list)) {
+        exit(1);
+    }
+
     /* Open the logfile at this point and set the log mask if necessary.
      */
     if (log_file) {
qemu-options.hx +17 -0
index 746b5fa..34557e6 100644
@@ -4064,6 +4064,23 @@ HXCOMM HX does not support conditional compilation of text.
 @findex -trace
 @include qemu-option-trace.texi
 ETEXI
+DEF("plugin", HAS_ARG, QEMU_OPTION_plugin,
+    "-plugin [file=]<file>[,arg=<string>]\n"
+    "                load a plugin\n",
+    QEMU_ARCH_ALL)
+STEXI
+@item -plugin file=@var{file}[,arg=@var{string}]
+@findex -plugin
+
+Load a plugin.
+
+@table @option
+@item file=@var{file}
+Load the given plugin from a shared library file.
+@item arg=@var{string}
+Argument string passed to the plugin. (Can be given multiple times.)
+@end table
+ETEXI
 
 HXCOMM Internal use
 DEF("qtest", HAS_ARG, QEMU_OPTION_qtest, "", QEMU_ARCH_ALL)
Signature · 1 line
2.7.4
#9 lore raw

RFC 6/6 linux-user: support -plugin option

In reply to Emilio G. Cota

From: Lluís Vilanova <[email protected]>

[ cota: s/instrument/plugin ]
---
 linux-user/main.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
linux-user/main.c +18 -0
index 31c8f1a..211ee02 100644
@@ -27,6 +27,7 @@
 #include "qemu/config-file.h"
 #include "qemu/cutils.h"
 #include "qemu/help_option.h"
+#include "qemu/plugin.h"
 #include "cpu.h"
 #include "exec/exec-all.h"
 #include "tcg.h"
@@ -4014,6 +4015,15 @@ static void handle_arg_trace(const char *arg)
     trace_file = trace_opt_parse(arg);
 }
 
+static struct qemu_plugin_list plugins = QTAILQ_HEAD_INITIALIZER(plugins);
+
+#ifdef CONFIG_PLUGINS
+static void handle_arg_plugin(const char *arg)
+{
+    qemu_plugin_opt_parse(arg, &plugins);
+}
+#endif
+
 struct qemu_argument {
     const char *argv;
     const char *env;
@@ -4063,6 +4073,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Seed for pseudo-random number generator"},
     {"trace",      "QEMU_TRACE",       true,  handle_arg_trace,
      "",           "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
+#ifdef CONFIG_PLUGINS
+    {"plugin",     "QEMU_PLUGIN",      true,  handle_arg_plugin,
+     "",           "[file=]<file>[,arg=<string>]"},
+#endif
     {"version",    "QEMU_VERSION",     false, handle_arg_version,
      "",           "display version information and exit"},
     {NULL, NULL, false, NULL, NULL, NULL}
@@ -4252,6 +4266,7 @@ int main(int argc, char **argv, char **envp)
     srand(time(NULL));
 
     qemu_add_opts(&qemu_trace_opts);
+    qemu_plugin_add_opts();
 
     optind = parse_args(argc, argv);
 
@@ -4259,6 +4274,9 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
     trace_init_file(trace_file);
+    if (qemu_plugin_load_list(&plugins)) {
+        exit(1);
+    }
 
     /* Zero out regs */
     memset(regs, 0, sizeof(struct target_pt_regs));
Signature · 1 line
2.7.4
#10 lore raw
In reply to Emilio G. Cota
Quoted 21 lines · Emilio G. Cota

Related threads:
[PATCH 00/13] instrument: Add basic event instrumentation
Date: Mon, 24 Jul 2017 20:02:24 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html
and
[PATCH v4 00/20] instrument: Add basic event instrumentation
Date: Wed, 6 Sep 2017 20:22:41 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html

This set does something similar to the instrumentation patches by Lluis, but with a different implementation (and for now less events).

My focus has been on working on the skeleton of a (pseudo) stable API, as Stefan requested. Of course more events would have to be added, but before spending more time on this I'd like to get some feedback on the core of the design. Patch 2 has all the details.

Note: yes, patch 1 is not used in the series, but this is an RFC. It's there because it will be needed to get the tb->plugin_mask when deciding whether to generate a mem_cb helper when generating loads/stores from TCG.

Quoted 5 lines

This set applies on top of:
https://github.com/cota/qemu/tree/tcg-generic-15%2Bmulti-tcg-v4-parallel

The tree can be fetched from:
https://github.com/cota/qemu/tree/plugins

Hi -- do you have documentation of what the plugin-facing API here is? What I'd like to do for the initial evaluation of this series and Luis's is just to look at the plugin API and the command line/etc interface for users to provide the API, because I think that's the interesting part.

thanks
-- PMM

#11 lore raw
In reply to Emilio G. Cota
Quoted 21 lines · Emilio G. Cota

Related threads:
[PATCH 00/13] instrument: Add basic event instrumentation
Date: Mon, 24 Jul 2017 20:02:24 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html
and
[PATCH v4 00/20] instrument: Add basic event instrumentation
Date: Wed, 6 Sep 2017 20:22:41 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html

This set does something similar to the instrumentation patches by Lluis, but with a different implementation (and for now less events).

My focus has been on working on the skeleton of a (pseudo) stable API, as Stefan requested. Of course more events would have to be added, but before spending more time on this I'd like to get some feedback on the core of the design. Patch 2 has all the details.

Note: yes, patch 1 is not used in the series, but this is an RFC. It's there because it will be needed to get the tb->plugin_mask when deciding whether to generate a mem_cb helper when generating loads/stores from TCG.

Quoted 5 lines

This set applies on top of:
https://github.com/cota/qemu/tree/tcg-generic-15%2Bmulti-tcg-v4-parallel

The tree can be fetched from:
https://github.com/cota/qemu/tree/plugins

Hi Emilio,
Thanks for sending this! Could you and Lluís collaborate on a single instrumentation plugin API?

Let's have a discussion with Lluís, Peter, myself, and potentially others then we can use the QEMU Developer Conference Call that is scheduled every 2 weeks.

The next call time slot is Tue 26 Sept at 13:00 UTC:
https://www.timeanddate.com/worldclock/fixedtime.html?iso=20170926T140000&p1=136

Stefan

#12 lore raw
Quoted 9 lines · Stefan Hajnoczi

Thanks for sending this! Could you and Lluís collaborate on a single instrumentation plugin API?

Let's have a discussion with Lluís, Peter, myself, and potentially others then we can use the QEMU Developer Conference Call that is scheduled every 2 weeks.

The next call time slot is Tue 26 Sept at 13:00 UTC:
https://www.timeanddate.com/worldclock/fixedtime.html?iso=20170926T140000&p1=136

I can't make that, I'm afraid, it's Linaro Connect that week.

Quoted 1 line

From my point of view, it would be helpful to start with

just designing the API that we're trying to implement. I care a lot that we get that API correct, I don't care much at all about the implementation...

thanks
-- PMM

#13 lore raw
Quoted 11 lines · Peter Maydell
Quoted 9 lines · Stefan Hajnoczi

Thanks for sending this! Could you and Lluís collaborate on a single instrumentation plugin API?

Let's have a discussion with Lluís, Peter, myself, and potentially others then we can use the QEMU Developer Conference Call that is scheduled every 2 weeks.

The next call time slot is Tue 26 Sept at 13:00 UTC:
https://www.timeanddate.com/worldclock/fixedtime.html?iso=20170926T140000&p1=136

Quoted 6 lines

I can't make that, I'm afraid, it's Linaro Connect that week.

From my point of view, it would be helpful to start with just designing the API that we're trying to implement. I care a lot that we get that API correct, I don't care much at all about the implementation...

That sounds good to me.

An RFC patch with just the header files would be enough for mailing list discussion. No implementation is needed yet.

Stefan

#14 lore raw
Quoted 19 lines · Stefan Hajnoczi
Quoted 11 lines · Peter Maydell
Quoted 9 lines · Stefan Hajnoczi

Thanks for sending this! Could you and Lluís collaborate on a single instrumentation plugin API?

Let's have a discussion with Lluís, Peter, myself, and potentially others then we can use the QEMU Developer Conference Call that is scheduled every 2 weeks.

The next call time slot is Tue 26 Sept at 13:00 UTC:
https://www.timeanddate.com/worldclock/fixedtime.html?iso=20170926T140000&p1=136

Quoted 6 lines

I can't make that, I'm afraid, it's Linaro Connect that week.

From my point of view, it would be helpful to start with just designing the API that we're trying to implement. I care a lot that we get that API correct, I don't care much at all about the implementation...

Quoted 4 lines

That sounds good to me.

An RFC patch with just the header files would be enough for mailing list discussion. No implementation is needed yet.

I can send a stripped down version of this series only with the docs and public headers.

But first I would like to know how to handle the translate/execute differentiation first (if it is indeed needed).

Cheers,
Lluis

#15 lore raw
In reply to Emilio G. Cota
Quoted 9 lines · Emilio G. Cota

Related threads:
[PATCH 00/13] instrument: Add basic event instrumentation
Date: Mon, 24 Jul 2017 20:02:24 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html
and
[PATCH v4 00/20] instrument: Add basic event instrumentation
Date: Wed, 6 Sep 2017 20:22:41 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html

Quoted 7 lines

This set does something similar to the instrumentation patches by Lluis, but with a different implementation (and for now less events).

My focus has been on working on the skeleton of a (pseudo) stable API, as Stefan requested. Of course more events would have to be added, but before spending more time on this I'd like to get some feedback on the core of the design. Patch 2 has all the details.

Sorry for my ignorance, but if you send a patch series like this, could you please elaborate a little bit more on the topic what this all is about? In this cover letter, you basically give only some pointers about other patch series and point the reader to patch 2, but also patch 2 does not really have a proper *description* of what this is really all about. Sure, it's about plugins, but what kind of plugins? Audio? Video? CPU? Everything? If you send RFC, you should properly describe your vision first, and maybe give some examples, before you jump into the details.

Thanks,
Thomas

#16 lore raw
Quoted 18 lines · Thomas Huth
Quoted 9 lines · Emilio G. Cota

Related threads:
[PATCH 00/13] instrument: Add basic event instrumentation
Date: Mon, 24 Jul 2017 20:02:24 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html
and
[PATCH v4 00/20] instrument: Add basic event instrumentation
Date: Wed, 6 Sep 2017 20:22:41 +0300
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07419.html

Quoted 7 lines

This set does something similar to the instrumentation patches by Lluis, but with a different implementation (and for now less events).

My focus has been on working on the skeleton of a (pseudo) stable API, as Stefan requested. Of course more events would have to be added, but before spending more time on this I'd like to get some feedback on the core of the design. Patch 2 has all the details.

Quoted 9 lines

Sorry for my ignorance, but if you send a patch series like this, could you please elaborate a little bit more on the topic what this all is about? In this cover letter, you basically give only some pointers about other patch series and point the reader to patch 2, but also patch 2 does not really have a proper *description* of what this is really all about. Sure, it's about plugins, but what kind of plugins? Audio? Video? CPU? Everything? If you send RFC, you should properly describe your vision first, and maybe give some examples, before you jump into the details.

Good point.

This series is about introducing a stable API/ABI and loadable instrumentation plugin interface. The goal is to offer dynamic binary instrumentation frameworks like Intel's Pin and DynamoRIO offer.

In other words, performance analysis tasks (especially for TCG) can be implemented in a shared library that is loaded by QEMU when running a benchmark.

Stefan