qemu/linux-user
Mikulas Patocka 1e4c468ec7 linux-user/sh4: Fix crashes on signal delivery
sh4 uses gUSA (general UserSpace Atomicity) to provide atomicity on CPUs
that don't have atomic instructions. A gUSA region that adds 1 to an
atomic variable stored in @R2 looks like this:

  4004b6:       03 c7           mova    4004c4 <gusa+0x10>,r0
  4004b8:       f3 61           mov     r15,r1
  4004ba:       09 00           nop
  4004bc:       fa ef           mov     #-6,r15
  4004be:       22 63           mov.l   @r2,r3
  4004c0:       01 73           add     #1,r3
  4004c2:       32 22           mov.l   r3,@r2
  4004c4:       13 6f           mov     r1,r15

R0 contains a pointer to the end of the gUSA region
R1 contains the saved stack pointer
R15 contains negative length of the gUSA region

When this region is interrupted by a signal, the kernel detects if
R15 >= -128U. If yes, the kernel rolls back PC to the beginning of the
region and restores SP by copying R1 to R15.

The problem happens if we are interrupted by a signal at address 4004c4.
R15 still holds the value -6, but the atomic value was already written by
an instruction at address 4004c2. In this situation we can't undo the
gUSA. The function unwind_gusa does nothing, the signal handler attempts
to push a signal frame to the address -6 and crashes.

This patch fixes it, so that if we are interrupted at the last instruction
in a gUSA region, we copy R1 to R15 to restore the correct stack pointer
and avoid crashing.

There's another bug: if we are interrupted in a delay slot, we save the
address of the instruction in the delay slot. We must save the address of
the previous instruction.

Cc: qemu-stable@nongnu.org
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Reviewed-by: Yoshinori Sato <ysato@users.sourcefoege.jp>
Message-Id: <b16389f7-6c62-70b7-59b3-87533c0bcc@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
(cherry picked from commit 3b894b699c9a9c064466e128c18be80a3f2113bc)
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2023-10-19 21:59:58 +03:00
..
aarch64 linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
alpha linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
arm linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
cris linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
generic
hexagon linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
hppa linux-user/hppa: Fix struct target_sigcontext layout 2023-10-06 15:29:15 +03:00
i386 linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
include linux-user: cleanup unused linux-user/include/host directories 2023-08-08 20:44:08 +02:00
loongarch64 linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
m68k linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
microblaze linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
mips linux-user/mips: fix abort on integer overflow 2023-10-19 21:59:58 +03:00
mips64
nios2 linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
openrisc linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
ppc linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
riscv linux-user/riscv: Use abi type for target_ucontext 2023-09-21 19:35:19 +03:00
s390x linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
sh4 linux-user/sh4: Fix crashes on signal delivery 2023-10-19 21:59:58 +03:00
sparc linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
x86_64 linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
xtensa linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
cpu_loop-common.h
elfload.c linux-user: Fixes for zero_bss 2023-10-19 21:58:31 +03:00
errnos.c.inc
exit.c
fd-trans.c
fd-trans.h
flat.h
flatload.c
ioctls.h
linux_loop.h
linuxload.c
loader.h
main.c linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
meson.build
mmap.c linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
qemu.h linux-user: Adjust initial brk when interpreter is close to executable 2023-08-08 13:27:17 -07:00
semihost.c
signal-common.h
signal.c
socket.h
strace.c
strace.h
strace.list
syscall.c linux-user: Fix openat() emulation to correctly detect accesses to /proc 2023-08-09 09:31:30 -07:00
syscall_defs.h
syscall_types.h
thunk.c linux-user: Use ARRAY_SIZE with bitmask_transtbl 2023-08-09 07:17:42 -07:00
trace-events
trace.h
uaccess.c
uname.c
uname.h
user-internals.h
user-mmap.h linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h 2023-08-08 13:27:15 -07:00
vm86.c