All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: peter.maydell@linaro.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	richard.henderson@linaro.org, qemu-devel@nongnu.org,
	"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PULL 17/21] plugins: try and make plugin_insn_append more ergonomic
Date: Wed,  3 Nov 2021 17:05:54 +0000	[thread overview]
Message-ID: <20211103170558.717981-18-alex.bennee@linaro.org> (raw)
In-Reply-To: <20211103170558.717981-1-alex.bennee@linaro.org>

Currently we make the assumption that the guest frontend loads all
op code bytes sequentially. This mostly holds up for regular fixed
encodings but some architectures like s390x like to re-read the
instruction which causes weirdness to occur. Rather than changing the
frontends make the plugin API a little more ergonomic and able to
handle the re-read case.

Stuff will still get strange if we read ahead of the opcode but so far
no front ends have done that and this patch asserts the case so we can
catch it early if they do.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Suggested-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20211026102234.3961636-21-alex.bennee@linaro.org>

diff --git a/include/exec/plugin-gen.h b/include/exec/plugin-gen.h
index b1b72b5d90..f92f169739 100644
--- a/include/exec/plugin-gen.h
+++ b/include/exec/plugin-gen.h
@@ -27,13 +27,21 @@ void plugin_gen_insn_end(void);
 void plugin_gen_disable_mem_helpers(void);
 void plugin_gen_empty_mem_callback(TCGv addr, uint32_t info);
 
-static inline void plugin_insn_append(const void *from, size_t size)
+static inline void plugin_insn_append(abi_ptr pc, const void *from, size_t size)
 {
     struct qemu_plugin_insn *insn = tcg_ctx->plugin_insn;
+    abi_ptr off;
 
     if (insn == NULL) {
         return;
     }
+    off = pc - insn->vaddr;
+    if (off < insn->data->len) {
+        g_byte_array_set_size(insn->data, off);
+    } else if (off > insn->data->len) {
+        /* we have an unexpected gap */
+        g_assert_not_reached();
+    }
 
     insn->data = g_byte_array_append(insn->data, from, size);
 }
@@ -62,7 +70,7 @@ static inline void plugin_gen_disable_mem_helpers(void)
 static inline void plugin_gen_empty_mem_callback(TCGv addr, uint32_t info)
 { }
 
-static inline void plugin_insn_append(const void *from, size_t size)
+static inline void plugin_insn_append(abi_ptr pc, const void *from, size_t size)
 { }
 
 #endif /* CONFIG_PLUGIN */
diff --git a/include/qemu/plugin.h b/include/qemu/plugin.h
index b3172b147f..145f8a221a 100644
--- a/include/qemu/plugin.h
+++ b/include/qemu/plugin.h
@@ -163,10 +163,12 @@ struct qemu_plugin_tb {
 
 /**
  * qemu_plugin_tb_insn_get(): get next plugin record for translation.
- *
+ * @tb: the internal tb context
+ * @pc: address of instruction
  */
 static inline
-struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb)
+struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb,
+                                                 uint64_t pc)
 {
     struct qemu_plugin_insn *insn;
     int i, j;
@@ -179,6 +181,7 @@ struct qemu_plugin_insn *qemu_plugin_tb_insn_get(struct qemu_plugin_tb *tb)
     g_byte_array_set_size(insn->data, 0);
     insn->calls_helpers = false;
     insn->mem_helper = false;
+    insn->vaddr = pc;
 
     for (i = 0; i < PLUGIN_N_CB_TYPES; i++) {
         for (j = 0; j < PLUGIN_N_CB_SUBTYPES; j++) {
diff --git a/accel/tcg/plugin-gen.c b/accel/tcg/plugin-gen.c
index 61be64b78c..22d95fe1c3 100644
--- a/accel/tcg/plugin-gen.c
+++ b/accel/tcg/plugin-gen.c
@@ -876,9 +876,8 @@ void plugin_gen_insn_start(CPUState *cpu, const DisasContextBase *db)
     struct qemu_plugin_tb *ptb = tcg_ctx->plugin_tb;
     struct qemu_plugin_insn *pinsn;
 
-    pinsn = qemu_plugin_tb_insn_get(ptb);
+    pinsn = qemu_plugin_tb_insn_get(ptb, db->pc_next);
     tcg_ctx->plugin_insn = pinsn;
-    pinsn->vaddr = db->pc_next;
     plugin_gen_empty_callback(PLUGIN_GEN_FROM_INSN);
 
     /*
diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c
index 390bd9db0a..f06c314266 100644
--- a/accel/tcg/translator.c
+++ b/accel/tcg/translator.c
@@ -169,7 +169,7 @@ static inline void translator_maybe_page_protect(DisasContextBase *dcbase,
         if (do_swap) {                                                  \
             ret = swap_fn(ret);                                         \
         }                                                               \
-        plugin_insn_append(&ret, sizeof(ret));                          \
+        plugin_insn_append(pc, &ret, sizeof(ret));                      \
         return ret;                                                     \
     }
 
-- 
2.30.2



  parent reply	other threads:[~2021-11-03 17:20 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-03 17:05 [PULL for 6.2 00/21] testing, plugin and gdbstub updates Alex Bennée
2021-11-03 17:05 ` [PULL 01/21] tests/docker: Update debian-hexagon-cross to a newer toolchain Alex Bennée
2021-11-03 17:05 ` [PULL 02/21] gitlab-ci: Remove special casing for hexagon testing Alex Bennée
2021-11-03 17:05 ` [PULL 03/21] tests/docker: Add debian-nios2-cross image Alex Bennée
2021-11-03 17:05 ` [PULL 04/21] tests/docker: Add debian-microblaze-cross image Alex Bennée
2021-11-03 17:05 ` [PULL 05/21] tests/tcg: Enable container_cross_cc for microblaze Alex Bennée
2021-11-03 17:05 ` [PULL 06/21] tests/tcg: Fix some targets default cross compiler path Alex Bennée
2021-11-03 17:05 ` [PULL 07/21] tests/docker: split PARTIAL into PARTIAL and VIRTUAL images Alex Bennée
2021-11-03 17:05 ` [PULL 08/21] tests/tcg: enable debian-nios2-cross for test building Alex Bennée
2021-11-03 17:05 ` [PULL 09/21] ebpf: really include it only in system emulators Alex Bennée
2021-11-03 17:05 ` [PULL 10/21] plugins/cache: freed heap-allocated mutexes Alex Bennée
2021-11-03 17:05 ` [PULL 11/21] plugins/cache: implement unified L2 cache emulation Alex Bennée
2021-11-03 17:05 ` [PULL 12/21] plugins/cache: split command line arguments into name and value Alex Bennée
2021-11-03 17:05 ` [PULL 13/21] plugins/cache: make L2 emulation optional through args Alex Bennée
2021-11-03 17:05 ` [PULL 14/21] docs/tcg-plugins: add L2 arguments to cache docs Alex Bennée
2021-11-03 17:05 ` [PULL 15/21] chardev: don't exit() straight away on C-a x Alex Bennée
2021-11-03 17:05 ` [PULL 16/21] tests/plugins: extend the insn plugin to track opcode sizes Alex Bennée
2021-11-03 17:05 ` Alex Bennée [this message]
2021-11-03 17:05 ` [PULL 18/21] tests/tcg: remove duplicate EXTRA_RUNS Alex Bennée
2021-11-03 17:05 ` [PULL 19/21] gdbstub: Switch to the thread receiving a signal Alex Bennée
2021-11-03 17:05 ` [PULL 20/21] tests/tcg: remove debug polluting make output Alex Bennée
2021-11-03 17:05 ` [PULL 21/21] tests/vm/openbsd: Update to release 7.0 Alex Bennée
2021-11-03 17:20 ` [PULL for 6.2 00/21] testing, plugin and gdbstub updates Alex Bennée
2021-11-03 19:32   ` Taylor Simpson
2021-11-04  4:45     ` Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211103170558.717981-18-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.