public inbox for qemu-devel@nongnu.org
 help / color / mirror / Atom feed
* [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value()
@ 2026-03-25 21:52 Pierrick Bouvier
  2026-03-25 21:52 ` [PULL 1/3] tcg: Pass host-endian values to plugin_gen_mem_callbacks_* Pierrick Bouvier
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Pierrick Bouvier @ 2026-03-25 21:52 UTC (permalink / raw)
  To: qemu-devel, peter.maydell, richard.henderson, pbonzini, stefanha
  Cc: pierrick.bouvier

The following changes since commit 007b29752ed06e467d3c830bc2c17a8851f8bcd3:

  Merge tag 'for-upstream' of https://gitlab.com/kmwolf/qemu into staging (2026-03-25 09:16:13 +0000)

are available in the Git repository at:

  https://gitlab.com/pbo-linaro/qemu tags/pr-plugins-20260325

for you to fetch changes up to e074c90947a162f795fe85a46e98922c887d6985:

  tests/tcg/plugins/mem.c: fix endian swap in update_region_info (2026-03-25 14:51:15 -0700)

----------------------------------------------------------------
Changes:
- [PATCH 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() (Pierrick Bouvier <pierrick.bouvier@linaro.org>)
   Link: https://lore.kernel.org/qemu-devel/20260325024252.3369186-1-pierrick.bouvier@linaro.org

----------------------------------------------------------------
Pierrick Bouvier (2):
      include/plugins/qemu-plugin.h: add note about endianness of value returned by qemu_plugin_mem_get_value
      tests/tcg/plugins/mem.c: fix endian swap in update_region_info

Richard Henderson (1):
      tcg: Pass host-endian values to plugin_gen_mem_callbacks_*

 include/plugins/qemu-plugin.h |  5 +++--
 tcg/tcg-op-ldst.c             | 52 ++++++++++++++++++++++---------------------
 tests/tcg/plugins/mem.c       | 12 +++++-----
 3 files changed, 36 insertions(+), 33 deletions(-)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PULL 1/3] tcg: Pass host-endian values to plugin_gen_mem_callbacks_*
  2026-03-25 21:52 [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() Pierrick Bouvier
@ 2026-03-25 21:52 ` Pierrick Bouvier
  2026-03-25 21:52 ` [PULL 2/3] include/plugins/qemu-plugin.h: add note about endianness of value returned by qemu_plugin_mem_get_value Pierrick Bouvier
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Pierrick Bouvier @ 2026-03-25 21:52 UTC (permalink / raw)
  To: qemu-devel, peter.maydell, richard.henderson, pbonzini, stefanha
  Cc: pierrick.bouvier

From: Richard Henderson <richard.henderson@linaro.org>

If the host does not support swapped-endian loads and stores,
then we emulate those within the tcg expanders with explicit
bswap operations.

However, we were passing values to the plugin interface in
the middle of those bswap operations, which meant that we
would pass values of the wrong endianness to plugins when
running on hosts without swapped-endian loads and stores.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3351
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Tested-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Link: https://lore.kernel.org/qemu-devel/20260325024252.3369186-2-pierrick.bouvier@linaro.org
Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 tcg/tcg-op-ldst.c | 52 ++++++++++++++++++++++++-----------------------
 1 file changed, 27 insertions(+), 25 deletions(-)

diff --git a/tcg/tcg-op-ldst.c b/tcg/tcg-op-ldst.c
index 354d9968f92..22211ccb452 100644
--- a/tcg/tcg-op-ldst.c
+++ b/tcg/tcg-op-ldst.c
@@ -262,9 +262,6 @@ static void tcg_gen_qemu_ld_i32_int(TCGv_i32 val, TCGTemp *addr,
     addr_new = tci_extend_addr(addr);
     copy_addr = plugin_maybe_preserve_addr(addr);
     gen_ldst1(INDEX_op_qemu_ld, TCG_TYPE_I32, tcgv_i32_temp(val), addr_new, oi);
-    plugin_gen_mem_callbacks_i32(val, copy_addr, addr, orig_oi,
-                                 QEMU_PLUGIN_MEM_R);
-    maybe_free_addr(addr, addr_new);
 
     if ((orig_memop ^ memop) & MO_BSWAP) {
         switch (orig_memop & MO_SIZE) {
@@ -280,6 +277,10 @@ static void tcg_gen_qemu_ld_i32_int(TCGv_i32 val, TCGTemp *addr,
             g_assert_not_reached();
         }
     }
+
+    plugin_gen_mem_callbacks_i32(val, copy_addr, addr, orig_oi,
+                                 QEMU_PLUGIN_MEM_R);
+    maybe_free_addr(addr, addr_new);
 }
 
 void tcg_gen_qemu_ld_i32_chk(TCGv_i32 val, TCGTemp *addr, TCGArg idx,
@@ -290,10 +291,10 @@ void tcg_gen_qemu_ld_i32_chk(TCGv_i32 val, TCGTemp *addr, TCGArg idx,
     tcg_gen_qemu_ld_i32_int(val, addr, idx, memop);
 }
 
-static void tcg_gen_qemu_st_i32_int(TCGv_i32 val, TCGTemp *addr,
+static void tcg_gen_qemu_st_i32_int(TCGv_i32 orig_val, TCGTemp *addr,
                                     TCGArg idx, MemOp memop)
 {
-    TCGv_i32 swap = NULL;
+    TCGv_i32 val = orig_val;
     MemOpIdx orig_oi, oi;
     TCGTemp *addr_new;
 
@@ -302,29 +303,29 @@ static void tcg_gen_qemu_st_i32_int(TCGv_i32 val, TCGTemp *addr,
     orig_oi = oi = make_memop_idx(memop, idx);
 
     if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
-        swap = tcg_temp_ebb_new_i32();
+        val = tcg_temp_ebb_new_i32();
         switch (memop & MO_SIZE) {
         case MO_16:
-            tcg_gen_bswap16_i32(swap, val, 0);
+            tcg_gen_bswap16_i32(val, orig_val, 0);
             break;
         case MO_32:
-            tcg_gen_bswap32_i32(swap, val);
+            tcg_gen_bswap32_i32(val, orig_val);
             break;
         default:
             g_assert_not_reached();
         }
-        val = swap;
         memop &= ~MO_BSWAP;
         oi = make_memop_idx(memop, idx);
     }
 
     addr_new = tci_extend_addr(addr);
     gen_ldst1(INDEX_op_qemu_st, TCG_TYPE_I32, tcgv_i32_temp(val), addr_new, oi);
-    plugin_gen_mem_callbacks_i32(val, NULL, addr, orig_oi, QEMU_PLUGIN_MEM_W);
+    plugin_gen_mem_callbacks_i32(orig_val, NULL, addr, orig_oi,
+                                 QEMU_PLUGIN_MEM_W);
     maybe_free_addr(addr, addr_new);
 
-    if (swap) {
-        tcg_temp_free_i32(swap);
+    if (val != orig_val) {
+        tcg_temp_free_i32(val);
     }
 }
 
@@ -360,9 +361,6 @@ static void tcg_gen_qemu_ld_i64_int(TCGv_i64 val, TCGTemp *addr,
     addr_new = tci_extend_addr(addr);
     copy_addr = plugin_maybe_preserve_addr(addr);
     gen_ld_i64(val, addr_new, oi);
-    plugin_gen_mem_callbacks_i64(val, copy_addr, addr, orig_oi,
-                                 QEMU_PLUGIN_MEM_R);
-    maybe_free_addr(addr, addr_new);
 
     if ((orig_memop ^ memop) & MO_BSWAP) {
         int flags = (orig_memop & MO_SIGN
@@ -382,6 +380,10 @@ static void tcg_gen_qemu_ld_i64_int(TCGv_i64 val, TCGTemp *addr,
             g_assert_not_reached();
         }
     }
+
+    plugin_gen_mem_callbacks_i64(val, copy_addr, addr, orig_oi,
+                                 QEMU_PLUGIN_MEM_R);
+    maybe_free_addr(addr, addr_new);
 }
 
 void tcg_gen_qemu_ld_i64_chk(TCGv_i64 val, TCGTemp *addr, TCGArg idx,
@@ -392,10 +394,10 @@ void tcg_gen_qemu_ld_i64_chk(TCGv_i64 val, TCGTemp *addr, TCGArg idx,
     tcg_gen_qemu_ld_i64_int(val, addr, idx, memop);
 }
 
-static void tcg_gen_qemu_st_i64_int(TCGv_i64 val, TCGTemp *addr,
+static void tcg_gen_qemu_st_i64_int(TCGv_i64 orig_val, TCGTemp *addr,
                                     TCGArg idx, MemOp memop)
 {
-    TCGv_i64 swap = NULL;
+    TCGv_i64 val = orig_val;
     MemOpIdx orig_oi, oi;
     TCGTemp *addr_new;
 
@@ -404,32 +406,32 @@ static void tcg_gen_qemu_st_i64_int(TCGv_i64 val, TCGTemp *addr,
     orig_oi = oi = make_memop_idx(memop, idx);
 
     if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
-        swap = tcg_temp_ebb_new_i64();
+        val = tcg_temp_ebb_new_i64();
         switch (memop & MO_SIZE) {
         case MO_16:
-            tcg_gen_bswap16_i64(swap, val, 0);
+            tcg_gen_bswap16_i64(val, orig_val, 0);
             break;
         case MO_32:
-            tcg_gen_bswap32_i64(swap, val, 0);
+            tcg_gen_bswap32_i64(val, orig_val, 0);
             break;
         case MO_64:
-            tcg_gen_bswap64_i64(swap, val);
+            tcg_gen_bswap64_i64(val, orig_val);
             break;
         default:
             g_assert_not_reached();
         }
-        val = swap;
         memop &= ~MO_BSWAP;
         oi = make_memop_idx(memop, idx);
     }
 
     addr_new = tci_extend_addr(addr);
     gen_st_i64(val, addr_new, oi);
-    plugin_gen_mem_callbacks_i64(val, NULL, addr, orig_oi, QEMU_PLUGIN_MEM_W);
+    plugin_gen_mem_callbacks_i64(orig_val, NULL, addr, orig_oi,
+                                 QEMU_PLUGIN_MEM_W);
     maybe_free_addr(addr, addr_new);
 
-    if (swap) {
-        tcg_temp_free_i64(swap);
+    if (val != orig_val) {
+        tcg_temp_free_i64(val);
     }
 }
 
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PULL 2/3] include/plugins/qemu-plugin.h: add note about endianness of value returned by qemu_plugin_mem_get_value
  2026-03-25 21:52 [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() Pierrick Bouvier
  2026-03-25 21:52 ` [PULL 1/3] tcg: Pass host-endian values to plugin_gen_mem_callbacks_* Pierrick Bouvier
@ 2026-03-25 21:52 ` Pierrick Bouvier
  2026-03-25 21:52 ` [PULL 3/3] tests/tcg/plugins/mem.c: fix endian swap in update_region_info Pierrick Bouvier
  2026-03-26 14:58 ` [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() Peter Maydell
  3 siblings, 0 replies; 5+ messages in thread
From: Pierrick Bouvier @ 2026-03-25 21:52 UTC (permalink / raw)
  To: qemu-devel, peter.maydell, richard.henderson, pbonzini, stefanha
  Cc: pierrick.bouvier

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Link: https://lore.kernel.org/qemu-devel/20260325024252.3369186-3-pierrick.bouvier@linaro.org
Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 include/plugins/qemu-plugin.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/include/plugins/qemu-plugin.h b/include/plugins/qemu-plugin.h
index 2ce7a7fb6c3..4eb1d2cd85c 100644
--- a/include/plugins/qemu-plugin.h
+++ b/include/plugins/qemu-plugin.h
@@ -374,7 +374,8 @@ enum qemu_plugin_mem_value_type {
  *
  * @type: the memory access size
  * @data: the value accessed during the memory operation (value after
- *        read/write)
+ *        read/write). It's directly stored following host endianness, so no
+ *        further swap is needed.
  */
 typedef struct {
     enum qemu_plugin_mem_value_type type;
@@ -682,7 +683,7 @@ bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info);
  * qemu_plugin_mem_get_value() - return last value loaded/stored
  * @info: opaque memory transaction handle
  *
- * Returns: memory value
+ * Returns: memory value in host-endian order (no further swap is necessary).
  */
 QEMU_PLUGIN_API
 qemu_plugin_mem_value qemu_plugin_mem_get_value(qemu_plugin_meminfo_t info);
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PULL 3/3] tests/tcg/plugins/mem.c: fix endian swap in update_region_info
  2026-03-25 21:52 [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() Pierrick Bouvier
  2026-03-25 21:52 ` [PULL 1/3] tcg: Pass host-endian values to plugin_gen_mem_callbacks_* Pierrick Bouvier
  2026-03-25 21:52 ` [PULL 2/3] include/plugins/qemu-plugin.h: add note about endianness of value returned by qemu_plugin_mem_get_value Pierrick Bouvier
@ 2026-03-25 21:52 ` Pierrick Bouvier
  2026-03-26 14:58 ` [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() Peter Maydell
  3 siblings, 0 replies; 5+ messages in thread
From: Pierrick Bouvier @ 2026-03-25 21:52 UTC (permalink / raw)
  To: qemu-devel, peter.maydell, richard.henderson, pbonzini, stefanha
  Cc: pierrick.bouvier

value returned by qemu_plugin_mem_get_value() is always in host-endian
order, so we need to convert TO target endianness and not FROM it.

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Link: https://lore.kernel.org/qemu-devel/20260325024252.3369186-4-pierrick.bouvier@linaro.org
Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 tests/tcg/plugins/mem.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/tests/tcg/plugins/mem.c b/tests/tcg/plugins/mem.c
index 1ee257f855b..6b0732dc0fd 100644
--- a/tests/tcg/plugins/mem.c
+++ b/tests/tcg/plugins/mem.c
@@ -154,20 +154,20 @@ static void update_region_info(uint64_t region, uint64_t offset,
         val_size = 1;
         break;
     case QEMU_PLUGIN_MEM_VALUE_U16:
-        swapped_value.data.u16 = be ? GUINT16_FROM_BE(value.data.u16) :
-            GUINT16_FROM_LE(value.data.u16);
+        swapped_value.data.u16 = be ? GUINT16_TO_BE(value.data.u16) :
+            GUINT16_TO_LE(value.data.u16);
         val_ptr = &swapped_value.data.u16;
         val_size = 2;
         break;
     case QEMU_PLUGIN_MEM_VALUE_U32:
-        swapped_value.data.u32 = be ? GUINT32_FROM_BE(value.data.u32) :
-            GUINT32_FROM_LE(value.data.u32);
+        swapped_value.data.u32 = be ? GUINT32_TO_BE(value.data.u32) :
+            GUINT32_TO_LE(value.data.u32);
         val_ptr = &swapped_value.data.u32;
         val_size = 4;
         break;
     case QEMU_PLUGIN_MEM_VALUE_U64:
-        swapped_value.data.u64 = be ? GUINT64_FROM_BE(value.data.u64) :
-            GUINT64_FROM_LE(value.data.u64);
+        swapped_value.data.u64 = be ? GUINT64_TO_BE(value.data.u64) :
+            GUINT64_TO_LE(value.data.u64);
         val_ptr = &swapped_value.data.u64;
         val_size = 8;
         break;
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value()
  2026-03-25 21:52 [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() Pierrick Bouvier
                   ` (2 preceding siblings ...)
  2026-03-25 21:52 ` [PULL 3/3] tests/tcg/plugins/mem.c: fix endian swap in update_region_info Pierrick Bouvier
@ 2026-03-26 14:58 ` Peter Maydell
  3 siblings, 0 replies; 5+ messages in thread
From: Peter Maydell @ 2026-03-26 14:58 UTC (permalink / raw)
  To: Pierrick Bouvier; +Cc: qemu-devel, richard.henderson, pbonzini, stefanha

On Wed, 25 Mar 2026 at 21:52, Pierrick Bouvier
<pierrick.bouvier@linaro.org> wrote:
>
> The following changes since commit 007b29752ed06e467d3c830bc2c17a8851f8bcd3:
>
>   Merge tag 'for-upstream' of https://gitlab.com/kmwolf/qemu into staging (2026-03-25 09:16:13 +0000)
>
> are available in the Git repository at:
>
>   https://gitlab.com/pbo-linaro/qemu tags/pr-plugins-20260325
>
> for you to fetch changes up to e074c90947a162f795fe85a46e98922c887d6985:
>
>   tests/tcg/plugins/mem.c: fix endian swap in update_region_info (2026-03-25 14:51:15 -0700)
>
> ----------------------------------------------------------------
> Changes:
> - [PATCH 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() (Pierrick Bouvier <pierrick.bouvier@linaro.org>)
>    Link: https://lore.kernel.org/qemu-devel/20260325024252.3369186-1-pierrick.bouvier@linaro.org
>



Applied, thanks.

Please update the changelog at https://wiki.qemu.org/ChangeLog/11.0
for any user-visible changes.

-- PMM


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-26 14:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-25 21:52 [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() Pierrick Bouvier
2026-03-25 21:52 ` [PULL 1/3] tcg: Pass host-endian values to plugin_gen_mem_callbacks_* Pierrick Bouvier
2026-03-25 21:52 ` [PULL 2/3] include/plugins/qemu-plugin.h: add note about endianness of value returned by qemu_plugin_mem_get_value Pierrick Bouvier
2026-03-25 21:52 ` [PULL 3/3] tests/tcg/plugins/mem.c: fix endian swap in update_region_info Pierrick Bouvier
2026-03-26 14:58 ` [PULL 0/3] plugins: fix endianness for qemu_plugin_mem_get_value() Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox