qemu/tcg
Jordan Niethe 736a1588c1 tcg/ppc: Fix race in goto_tb implementation
Commit 20b6643324 ("tcg/ppc: Reorg goto_tb implementation") modified
goto_tb to ensure only a single instruction was patched to prevent
incorrect behavior if a thread was in the middle of multiple
instructions when they were replaced. However this introduced a race
between loading the jmp target into TCG_REG_TB and patching and
executing the direct branch.

The relevant part of the goto_tb implementation:

    ld TCG_REG_TB, TARGET_ADDR_LOCATION(TCG_REG_TB)
  patch_location:
    mtctr TCG_REG_TB
    bctr

tb_target_set_jmp_target() will replace 'patch_location' with a direct
branch if the target is in range. The direct branch now relies on
TCG_REG_TB being set up correctly by the ld. Prior to this commit
multiple instructions were patched in for the direct branch case; these
instructions would initialize TCG_REG_TB to the same value as the branch
target.

Imagine the following sequence:

1) Thread A is executing the goto_tb sequence and loads the jmp
   target into TCG_REG_TB.

2) Thread B updates the jmp target address and calls
   tb_target_set_jmp_target(). This patches a new direct branch into the
   goto_tb sequence.

3) Thread A executes the newly patched direct branch. The value in
   TCG_REG_TB still contains the old jmp target.

TCG_REG_TB MUST contain the translation block's tc.ptr. Execution will
eventually crash after performing memory accesses generated from a
faulty value in TCG_REG_TB.

This presents as segfaults or illegal instruction exceptions.

Do not revert commit 20b6643324 as it did fix a different race
condition. Instead remove the direct branch optimization and always use
indirect branches.

The direct branch optimization can be re-added later with a race free
sequence.

Fixes: 20b6643324 ("tcg/ppc: Reorg goto_tb implementation")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1726
Reported-by: Anushree Mathur <anushree.mathur@linux.vnet.ibm.com>
Tested-by: Anushree Mathur <anushree.mathur@linux.vnet.ibm.com>
Tested-by: Michael Tokarev <mjt@tls.msk.ru>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Co-developed-by: Benjamin Gray <bgray@linux.ibm.com>
Signed-off-by: Jordan Niethe <jniethe5@gmail.com>
Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
Message-Id: <20230717093001.13167-1-jniethe5@gmail.com>
2023-07-23 17:52:42 +01:00
..
aarch64 tcg: Split out tcg-target-reg-bits.h 2023-06-05 12:04:28 -07:00
arm tcg: Split out tcg-target-reg-bits.h 2023-06-05 12:04:28 -07:00
i386 tcg: Split out tcg-target-reg-bits.h 2023-06-05 12:04:28 -07:00
loongarch64 tcg: Split out tcg-target-reg-bits.h 2023-06-05 12:04:28 -07:00
mips tcg: Split out tcg-target-reg-bits.h 2023-06-05 12:04:28 -07:00
ppc tcg/ppc: Fix race in goto_tb implementation 2023-07-23 17:52:42 +01:00
riscv tcg: Split out tcg-target-reg-bits.h 2023-06-05 12:04:28 -07:00
s390x tcg: Split out tcg-target-reg-bits.h 2023-06-05 12:04:28 -07:00
sparc64 tcg: Split out tcg-target-reg-bits.h 2023-06-05 12:04:28 -07:00
tci tcg/tci: Fix MemOpIdx operand index for 3-operand memops 2023-06-09 08:30:56 -07:00
meson.build meson: Replace softmmu_ss -> system_ss 2023-06-20 10:01:30 +02:00
optimize.c tcg: Split tcg/tcg-op-common.h from tcg/tcg-op.h 2023-06-05 12:04:28 -07:00
region.c tcg: Remove target-specific headers from tcg.[ch] 2023-06-05 12:04:29 -07:00
tcg-common.c tcg: Silent -Wmissing-field-initializers warning 2023-02-27 22:29:01 +01:00
tcg-internal.h tcg: Move TCGHelperInfo and dependencies to tcg/helper-info.h 2023-06-05 12:04:28 -07:00
tcg-ldst.c.inc tcg: Move TCGLabelQemuLdst to tcg.c 2023-05-05 17:21:03 +01:00
tcg-op-gvec.c tcg: Fix temporary variable in tcg_gen_gvec_andcs 2023-06-26 17:33:00 +02:00
tcg-op-ldst.c tcg: Use HAVE_CMPXCHG128 instead of CONFIG_CMPXCHG128 2023-07-15 08:02:49 +01:00
tcg-op-vec.c tcg: Reduce tcg_assert_listed_vecop() scope 2023-07-01 08:26:54 +02:00
tcg-op.c tcg: Do not elide memory barriers for !CF_PARALLEL in system mode 2023-06-26 17:33:00 +02:00
tcg-pool.c.inc tcg: Introduce tcg_splitwx_to_{rx,rw} 2021-01-07 05:09:41 -10:00
tcg.c tcg: Fix info_in_idx increment in layout_arg_by_ref 2023-07-15 08:02:32 +01:00
tci.c tcg/tci: Adjust passing of MemOpIdx 2023-06-07 08:33:45 -07:00