From: Richard Henderson <[email protected]> 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 <[email protected]> Reviewed-by: Pierrick Bouvier <[email protected]> Tested-by: Pierrick Bouvier <[email protected]> Link: https://lore.kernel.org/qemu-devel/[email protected] Signed-off-by: Pierrick Bouvier <[email protected]> (cherry picked from commit 539421a428fd4b8231d9be042143f2d09c719e2a) (Mjt: back-port to 10.0.x) Signed-off-by: Michael Tokarev <[email protected]> diff --git a/tcg/tcg-op-ldst.c b/tcg/tcg-op-ldst.c index a8f35e1970..335b50f088 100644 --- a/tcg/tcg-op-ldst.c +++ b/tcg/tcg-op-ldst.c @@ -260,9 +260,6 @@ static void tcg_gen_qemu_ld_i32_int(TCGv_i32 val, TCGTemp *addr, copy_addr = plugin_maybe_preserve_addr(addr); gen_ldst(INDEX_op_qemu_ld_i32, TCG_TYPE_I32, tcgv_i32_temp(val), NULL, 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) { @@ -278,6 +275,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, @@ -288,10 +289,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; TCGOpcode opc; TCGTemp *addr_new; @@ -301,18 +302,17 @@ 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); } @@ -324,11 +324,12 @@ static void tcg_gen_qemu_st_i32_int(TCGv_i32 val, TCGTemp *addr, } addr_new = tci_extend_addr(addr); gen_ldst(opc, TCG_TYPE_I32, tcgv_i32_temp(val), NULL, 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); } } @@ -374,9 +375,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_ldst_i64(INDEX_op_qemu_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 @@ -396,6 +394,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, @@ -406,10 +408,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; @@ -423,32 +425,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_ldst_i64(INDEX_op_qemu_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
