linux-user/sh4: preserve T/M/Q bits across signal delivery
PATCH 1/3 linux-user/sh4: preserve T/M/Q bits across signal delivery
QEMU keeps the SH4 T, M and Q status-register bits outside env->sr, in the dedicated env->sr_t, env->sr_m and env->sr_q fields; cpu_read_sr() folds them back into the architectural SR value and cpu_write_sr() splits them back out.
setup_sigcontext() saved the bare env->sr (so the T/M/Q bits were always zero in the signal frame) and restore_sigcontext() wrote the value straight back into env->sr without updating sr_t/sr_m/sr_q. As a result the T bit was never preserved across signal delivery: on sigreturn the interrupted code resumed with whatever T value the signal handler last left behind. Any conditional branch (or addc/subc/rotcl/div1, etc.) immediately following the interrupted instruction could then take the wrong path.
This is the cause of the long-standing intermittent failures of the tests/tcg/multiarch/signals.c test on sh4, which was marked BROKEN. With a SIGRTMIN timer firing every millisecond across many threads, the race was hit a few percent of the time and corrupted the guest heap, surfacing as a SIGSEGV in memset, a malloc assertion, or an rseq registration abort.
Traced on a deterministic rr recording: a cmp/hi set T=0, the timer signal interrupted the very next instruction (a bf), the handler left T=1, and the resumed bf took glibc calloc's MORECORE_CLEARS branch, using the old top-chunk size as the clear length for a freshly split small chunk and running memset off the end of the heap.
Fix setup_sigcontext()/restore_sigcontext() to use cpu_read_sr() and cpu_write_sr() so the T, M and Q bits round-trip correctly, and drop the BROKEN annotation on the sh4 signals test.
--- linux-user/sh4/signal.c | 12 ++++++++++-- tests/tcg/sh4/Makefile.target | 7 ------- 2 files changed, 10 insertions(+), 9 deletions(-)
linux-user/sh4/signal.c +10 -2
@@ -131,8 +131,10 @@ static void setup_sigcontext(struct target_sigcontext *sc,COPY (gregs[14 ]);COPY (gregs[15 ]);COPY (gbr);COPY (mach);COPY (macl);COPY (pr); -COPY (sr);COPY (pc); +COPY (pc);#undef COPY +/* The T, M and Q bits live outside env->sr; fold them back in. */ +__put_user (cpu_read_sr (regs), &sc->sc_sr);for (i=0 ; i<16 ; i++) {__put_user (regs->fregs[i], &sc->sc_fpregs[i]);
@@ -159,8 +161,14 @@ static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc)COPY (gregs[14 ]);COPY (gregs[15 ]);COPY (gbr);COPY (mach);COPY (macl);COPY (pr); -COPY (sr);COPY (pc); +COPY (pc);#undef COPY +/* The T, M and Q bits live outside env->sr; unfold them. */ + { +uint32_t sr; +__get_user (sr, &sc->sc_sr); +cpu_write_sr (regs, sr); + }for (i=0 ; i<16 ; i++) {__get_user (regs->fregs[i], &sc->sc_fpregs[i]);
tests/tcg/sh4/Makefile.target +0 -7
@@ -3,13 +3,6 @@ # SuperH specific tweaks # -# This triggers failures for sh4-linux about 10% of the time. -# Random SIGSEGV at unpredictable guest address, cause unknown. -run-signals: signals - $(call skip-test, $<, "BROKEN") -run-plugin-signals-with-%: - $(call skip-test, $<, "BROKEN") - VPATH += $(SRC_PATH)/tests/tcg/sh4 test-macl: CFLAGS += -O -g
Signature · 1 line
2.53.0
PATCH 2/3 linux-user/sh4: restore FP rounding mode on sigreturn
The SH4 FPSCR rounding-mode (RM) and denormal (DN) bits are not held only in env->fpscr: they are also reflected into the derived env->fp_status via set_float_rounding_mode()/set_flush_to_zero(). The guest keeps the two in sync by routing every write to FPSCR through helper_ld_fpscr().
restore_sigcontext() wrote the saved value straight into env->fpscr and never touched env->fp_status, so on sigreturn the interrupted code resumed with whatever FP rounding mode and flush-to-zero setting the signal handler last installed. (regs->flags = 0 forces the FR/SZ/PR TB flags to be recomputed, but fp_status is runtime float state, not a TB flag, so it was left stale.) This is the FP analogue of the T/M/Q bit problem just fixed for the integer status register.
Factor the FPSCR -> fp_status synchronisation out of helper_ld_fpscr() into cpu_load_fpscr() and use it from restore_sigcontext() so the rounding mode round-trips correctly across signal delivery.
--- linux-user/sh4/signal.c | 7 ++++++- target/sh4/cpu.h | 3 +++ target/sh4/op_helper.c | 7 ++++++- 3 files changed, 15 insertions(+), 2 deletions(-)
linux-user/sh4/signal.c +6 -1
@@ -173,7 +173,12 @@ static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc)for (i=0 ; i<16 ; i++) {__get_user (regs->fregs[i], &sc->sc_fpregs[i]); } -__get_user (regs->fpscr, &sc->sc_fpscr); +/* Resync the derived float_status state, not just env->fpscr. */ + { +uint32_t fpscr; +__get_user (fpscr, &sc->sc_fpscr); +cpu_load_fpscr (regs, fpscr); + }__get_user (regs->fpul, &sc->sc_fpul); regs->tra =-1 ;/* disable syscall checks */
target/sh4/cpu.h +3 -0
@@ -379,4 +379,7 @@ static inline void cpu_write_sr(CPUSH4State *env, uint32_t sr) env->sr = sr & ~((1u << SR_M) | (1u << SR_Q) | (1u << SR_T)); } +/* Set FPSCR and the derived float_status rounding/flush-to-zero state. */ +void cpu_load_fpscr (CPUSH4State *env,uint32_t val); +#endif /* SH4_CPU_H */
target/sh4/op_helper.c +6 -1
@@ -204,7 +204,7 @@ void helper_macw(CPUSH4State *env, int32_t arg0, int32_t arg1) } } -void helper_ld_fpscr (CPUSH4State *env,uint32_t val) +void cpu_load_fpscr (CPUSH4State *env,uint32_t val) { env->fpscr = val &FPSCR_MASK ;if ((val &FPSCR_RM_MASK ) ==FPSCR_RM_ZERO ) {
@@ -215,6 +215,11 @@ void helper_ld_fpscr(CPUSH4State *env, uint32_t val)set_flush_to_zero ((val &FPSCR_DN ) !=0 , &env->fp_status); } +void helper_ld_fpscr (CPUSH4State *env,uint32_t val) +{ +cpu_load_fpscr (env, val); +} +static void update_fpscr (CPUSH4State *env,uintptr_t retaddr) {int xcpt, cause, enable;
Signature · 1 line
2.53.0
Re: [PATCH 2/3] linux-user/sh4: restore FP rounding mode on sigreturn
Quoted 15 lines · Matt Turner
The SH4 FPSCR rounding-mode (RM) and denormal (DN) bits are not held only in
env->fpscr: they are also reflected into the derivedenv->fp_statusvia set_float_rounding_mode()/set_flush_to_zero(). The guest keeps the two in sync by routing every write to FPSCR throughhelper_ld_fpscr().
restore_sigcontext()wrote the saved value straight intoenv->fpscrand never touchedenv->fp_status, so on sigreturn the interrupted code resumed with whatever FP rounding mode and flush-to-zero setting the signal handler last installed. (regs->flags= 0 forces the FR/SZ/PR TB flags to be recomputed, butfp_statusis runtime float state, not a TB flag, so it was left stale.) This is the FP analogue of the T/M/Q bit problem just fixed for the integer status register.
Quoted 7 lines
Factor the FPSCR ->
fp_statussynchronisation out ofhelper_ld_fpscr()intocpu_load_fpscr()and use it fromrestore_sigcontext()so the rounding mode round-trips correctly across signal delivery.Fixes c3b5bc8ab3 ("SH4: Signal handling for the user space emulator, by Magnus Damm.")---
Re: [PATCH 2/3] linux-user/sh4: restore FP rounding mode on sigreturn
In reply to Matt TurnerQuoted 18 lines · Matt Turner
The SH4 FPSCR rounding-mode (RM) and denormal (DN) bits are not held only in
env->fpscr: they are also reflected into the derivedenv->fp_statusvia set_float_rounding_mode()/set_flush_to_zero(). The guest keeps the two in sync by routing every write to FPSCR throughhelper_ld_fpscr().
restore_sigcontext()wrote the saved value straight intoenv->fpscrand never touchedenv->fp_status, so on sigreturn the interrupted code resumed with whatever FP rounding mode and flush-to-zero setting the signal handler last installed. (regs->flags= 0 forces the FR/SZ/PR TB flags to be recomputed, butfp_statusis runtime float state, not a TB flag, so it was left stale.) This is the FP analogue of the T/M/Q bit problem just fixed for the integer status register.Factor the FPSCR ->
fp_statussynchronisation out ofhelper_ld_fpscr()intocpu_load_fpscr()and use it fromrestore_sigcontext()so the rounding mode round-trips correctly across signal delivery.
Quoted 7 lines
Fixes c3b5bc8ab3 ("SH4: Signal handling for the user space emulator, by Magnus Damm.")--- linux-user/sh4/signal.c | 7 ++++++- target/sh4/cpu.h | 3 +++ target/sh4/op_helper.c | 7 ++++++- 3 files changed, 15 insertions(+), 2 deletions(-)
r~
Re: [PATCH 2/3] linux-user/sh4: restore FP rounding mode on sigreturn
In reply to Matt TurnerQuoted 85 lines · Matt Turner
The SH4 FPSCR rounding-mode (RM) and denormal (DN) bits are not held only in
env->fpscr: they are also reflected into the derivedenv->fp_statusvia set_float_rounding_mode()/set_flush_to_zero(). The guest keeps the two in sync by routing every write to FPSCR throughhelper_ld_fpscr().
restore_sigcontext()wrote the saved value straight intoenv->fpscrand never touchedenv->fp_status, so on sigreturn the interrupted code resumed with whatever FP rounding mode and flush-to-zero setting the signal handler last installed. (regs->flags= 0 forces the FR/SZ/PR TB flags to be recomputed, butfp_statusis runtime float state, not a TB flag, so it was left stale.) This is the FP analogue of the T/M/Q bit problem just fixed for the integer status register.Factor the FPSCR ->
fp_statussynchronisation out ofhelper_ld_fpscr()intocpu_load_fpscr()and use it fromrestore_sigcontext()so the rounding mode round-trips correctly across signal delivery.Fixes c3b5bc8ab3 ("SH4: Signal handling for the user space emulator, by Magnus Damm.")--- linux-user/sh4/signal.c | 7 ++++++- target/sh4/cpu.h | 3 +++ target/sh4/op_helper.c | 7 ++++++- 3 files changed, 15 insertions(+), 2 deletions(-)linux-user/sh4/signal.c +6
-1@@ -173,7 +173,12 @@ static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc)for (i=0 ; i<16 ; i++) {__get_user (regs->fregs[i], &sc->sc_fpregs[i]); } -__get_user (regs->fpscr, &sc->sc_fpscr); +/* Resync the derived float_status state, not just env->fpscr. */ + { +uint32_t fpscr; +__get_user (fpscr, &sc->sc_fpscr); +cpu_load_fpscr (regs, fpscr); + }__get_user (regs->fpul, &sc->sc_fpul); regs->tra =-1 ;/* disable syscall checks */ target/sh4/cpu.h +3
-0@@ -379,4 +379,7 @@ static inline void cpu_write_sr(CPUSH4State *env, uint32_t sr) env->sr = sr & ~((1u << SR_M) | (1u << SR_Q) | (1u << SR_T)); } +/* Set FPSCR and the derived float_status rounding/flush-to-zero state. */ +void cpu_load_fpscr (CPUSH4State *env,uint32_t val); +#endif /* SH4_CPU_H */ target/sh4/op_helper.c +6
-1@@ -204,7 +204,7 @@ void helper_macw(CPUSH4State *env, int32_t arg0, int32_t arg1) } } -void helper_ld_fpscr (CPUSH4State *env,uint32_t val) +void cpu_load_fpscr (CPUSH4State *env,uint32_t val) { env->fpscr = val &FPSCR_MASK ;if ((val &FPSCR_RM_MASK ) ==FPSCR_RM_ZERO ) {@@ -215,6 +215,11 @@ void helper_ld_fpscr(CPUSH4State *env, uint32_t val)set_flush_to_zero ((val &FPSCR_DN ) !=0 , &env->fp_status); } +void helper_ld_fpscr (CPUSH4State *env,uint32_t val) +{ +cpu_load_fpscr (env, val); +} +static void update_fpscr (CPUSH4State *env,uintptr_t retaddr) {int xcpt, cause, enable;Signature · 1 line
2.53.0
Signature · 1 line
Yosinori Sato
PATCH 3/3 target/sh4: sync fp_status when gdb writes FPSCR
In reply to Matt Turnersh4_cpu_gdb_write_register() wrote the incoming FPSCR value straight into env->fpscr, leaving the derived env->fp_status (rounding mode and flush-to-zero) stale, so a gdb-initiated FPSCR change did not take effect for subsequent FP operations. Use cpu_load_fpscr() instead, the same way the adjacent case already uses cpu_write_sr() for SR.
--- target/sh4/gdbstub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/sh4/gdbstub.c +1 -1
@@ -113,7 +113,7 @@ int superh_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) env->fpul =ldl_p (mem_buf);break ;case 24 : - env->fpscr =ldl_p (mem_buf); +cpu_load_fpscr (env,ldl_p (mem_buf));break ; case 25 ... 40:if (env->fpscr &FPSCR_FR ) {
Signature · 1 line
2.53.0
Re: [PATCH 3/3] target/sh4: sync fp_status when gdb writes FPSCR
Quoted 9 lines · Matt Turner
sh4_cpu_gdb_write_register()wrote the incoming FPSCR value straight intoenv->fpscr, leaving the derivedenv->fp_status(rounding mode and flush-to-zero) stale, so a gdb-initiated FPSCR change did not take effect for subsequent FP operations. Usecpu_load_fpscr()instead, the same way the adjacent case already usescpu_write_sr()for SR.---
Re: [PATCH 3/3] target/sh4: sync fp_status when gdb writes FPSCR
In reply to Matt TurnerQuoted 16 lines · Matt Turner
sh4_cpu_gdb_write_register()wrote the incoming FPSCR value straight intoenv->fpscr, leaving the derivedenv->fp_status(rounding mode and flush-to-zero) stale, so a gdb-initiated FPSCR change did not take effect for subsequent FP operations. Usecpu_load_fpscr()instead, the same way the adjacent case already usescpu_write_sr()for SR.--- target/sh4/gdbstub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/gdbstub.c +0
-0
target/sh4/gdbstub.c int superh_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 119
Quoted 8 lines
target/sh4/gdbstub.c +1
-1@@ -113,7 +113,7 @@ int superh_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) env->fpul =ldl_p (mem_buf);break ;case 24 : - env->fpscr =ldl_p (mem_buf); +cpu_load_fpscr (env,ldl_p (mem_buf));break ; case 25 ... 40:if (env->fpscr &FPSCR_FR ) {
r~
Re: [PATCH 3/3] target/sh4: sync fp_status when gdb writes FPSCR
In reply to Matt TurnerQuoted 28 lines · Matt Turner
sh4_cpu_gdb_write_register()wrote the incoming FPSCR value straight intoenv->fpscr, leaving the derivedenv->fp_status(rounding mode and flush-to-zero) stale, so a gdb-initiated FPSCR change did not take effect for subsequent FP operations. Usecpu_load_fpscr()instead, the same way the adjacent case already usescpu_write_sr()for SR.--- target/sh4/gdbstub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/gdbstub.c +1
-1@@ -113,7 +113,7 @@ int superh_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) env->fpul =ldl_p (mem_buf);break ;case 24 : - env->fpscr =ldl_p (mem_buf); +cpu_load_fpscr (env,ldl_p (mem_buf));break ; case 25 ... 40:if (env->fpscr &FPSCR_FR ) {Signature · 1 line
2.53.0
Signature · 1 line
Yosinori Sato
Quoted 27 lines · Matt Turner
QEMU keeps the SH4 T, M and Q status-register bits outside
env->sr, in the dedicatedenv->sr_t,env->sr_mandenv->sr_qfields;cpu_read_sr()folds them back into the architectural SR value andcpu_write_sr()splits them back out.
setup_sigcontext()saved the bareenv->sr(so the T/M/Q bits were always zero in the signal frame) andrestore_sigcontext()wrote the value straight back intoenv->srwithout updating sr_t/sr_m/sr_q. As a result the T bit was never preserved across signal delivery: on sigreturn the interrupted code resumed with whatever T value the signal handler last left behind. Any conditional branch (or addc/subc/rotcl/div1, etc.) immediately following the interrupted instruction could then take the wrong path.This is the cause of the long-standing intermittent failures of the
tests/tcg/multiarch/signals.ctest on sh4, which was marked BROKEN. With a SIGRTMIN timer firing every millisecond across many threads, the race was hit a few percent of the time and corrupted the guest heap, surfacing as a SIGSEGV in memset, a malloc assertion, or an rseq registration abort.Traced on a deterministic rr recording: a cmp/hi set T=0, the timer signal interrupted the very next instruction (a bf), the handler left T=1, and the resumed bf took glibc calloc's
MORECORE_CLEARSbranch, using the old top-chunk size as the clear length for a freshly split small chunk and running memset off the end of the heap.
Quoted 7 lines
Fix setup_sigcontext()/restore_sigcontext() to use
cpu_read_sr()andcpu_write_sr()so the T, M and Q bits round-trip correctly, and drop the BROKEN annotation on the sh4 signals test.Fixes c3b5bc8ab3 ("SH4: Signal handling for the user space emulator, by Magnus Damm.")---
Quoted 30 lines · Matt Turner
QEMU keeps the SH4 T, M and Q status-register bits outside
env->sr, in the dedicatedenv->sr_t,env->sr_mandenv->sr_qfields;cpu_read_sr()folds them back into the architectural SR value andcpu_write_sr()splits them back out.
setup_sigcontext()saved the bareenv->sr(so the T/M/Q bits were always zero in the signal frame) andrestore_sigcontext()wrote the value straight back intoenv->srwithout updating sr_t/sr_m/sr_q. As a result the T bit was never preserved across signal delivery: on sigreturn the interrupted code resumed with whatever T value the signal handler last left behind. Any conditional branch (or addc/subc/rotcl/div1, etc.) immediately following the interrupted instruction could then take the wrong path.This is the cause of the long-standing intermittent failures of the
tests/tcg/multiarch/signals.ctest on sh4, which was marked BROKEN. With a SIGRTMIN timer firing every millisecond across many threads, the race was hit a few percent of the time and corrupted the guest heap, surfacing as a SIGSEGV in memset, a malloc assertion, or an rseq registration abort.Traced on a deterministic rr recording: a cmp/hi set T=0, the timer signal interrupted the very next instruction (a bf), the handler left T=1, and the resumed bf took glibc calloc's
MORECORE_CLEARSbranch, using the old top-chunk size as the clear length for a freshly split small chunk and running memset off the end of the heap.Fix setup_sigcontext()/restore_sigcontext() to use
cpu_read_sr()andcpu_write_sr()so the T, M and Q bits round-trip correctly, and drop the BROKEN annotation on the sh4 signals test.
Quoted 6 lines
Fixes c3b5bc8ab3 ("SH4: Signal handling for the user space emulator, by Magnus Damm.")--- linux-user/sh4/signal.c | 12 ++++++++++-- tests/tcg/sh4/Makefile.target | 7 ------- 2 files changed, 10 insertions(+), 9 deletions(-)
r~
Quoted 91 lines · Matt Turner
QEMU keeps the SH4 T, M and Q status-register bits outside
env->sr, in the dedicatedenv->sr_t,env->sr_mandenv->sr_qfields;cpu_read_sr()folds them back into the architectural SR value andcpu_write_sr()splits them back out.
setup_sigcontext()saved the bareenv->sr(so the T/M/Q bits were always zero in the signal frame) andrestore_sigcontext()wrote the value straight back intoenv->srwithout updating sr_t/sr_m/sr_q. As a result the T bit was never preserved across signal delivery: on sigreturn the interrupted code resumed with whatever T value the signal handler last left behind. Any conditional branch (or addc/subc/rotcl/div1, etc.) immediately following the interrupted instruction could then take the wrong path.This is the cause of the long-standing intermittent failures of the
tests/tcg/multiarch/signals.ctest on sh4, which was marked BROKEN. With a SIGRTMIN timer firing every millisecond across many threads, the race was hit a few percent of the time and corrupted the guest heap, surfacing as a SIGSEGV in memset, a malloc assertion, or an rseq registration abort.Traced on a deterministic rr recording: a cmp/hi set T=0, the timer signal interrupted the very next instruction (a bf), the handler left T=1, and the resumed bf took glibc calloc's
MORECORE_CLEARSbranch, using the old top-chunk size as the clear length for a freshly split small chunk and running memset off the end of the heap.Fix setup_sigcontext()/restore_sigcontext() to use
cpu_read_sr()andcpu_write_sr()so the T, M and Q bits round-trip correctly, and drop the BROKEN annotation on the sh4 signals test.Fixes c3b5bc8ab3 ("SH4: Signal handling for the user space emulator, by Magnus Damm.")--- linux-user/sh4/signal.c | 12 ++++++++++-- tests/tcg/sh4/Makefile.target | 7 ------- 2 files changed, 10 insertions(+), 9 deletions(-)linux-user/sh4/signal.c +10
-2@@ -131,8 +131,10 @@ static void setup_sigcontext(struct target_sigcontext *sc,COPY (gregs[14 ]);COPY (gregs[15 ]);COPY (gbr);COPY (mach);COPY (macl);COPY (pr); -COPY (sr);COPY (pc); +COPY (pc);#undef COPY +/* The T, M and Q bits live outside env->sr; fold them back in. */ +__put_user (cpu_read_sr (regs), &sc->sc_sr);for (i=0 ; i<16 ; i++) {__put_user (regs->fregs[i], &sc->sc_fpregs[i]);@@ -159,8 +161,14 @@ static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc)COPY (gregs[14 ]);COPY (gregs[15 ]);COPY (gbr);COPY (mach);COPY (macl);COPY (pr); -COPY (sr);COPY (pc); +COPY (pc);#undef COPY +/* The T, M and Q bits live outside env->sr; unfold them. */ + { +uint32_t sr; +__get_user (sr, &sc->sc_sr); +cpu_write_sr (regs, sr); + }for (i=0 ; i<16 ; i++) {__get_user (regs->fregs[i], &sc->sc_fpregs[i]);tests/tcg/sh4/Makefile.target +0
-7@@ -3,13 +3,6 @@ # SuperH specific tweaks # -# This triggers failures for sh4-linux about 10% of the time. -# Random SIGSEGV at unpredictable guest address, cause unknown. -run-signals: signals - $(call skip-test, $<, "BROKEN") -run-plugin-signals-with-%: - $(call skip-test, $<, "BROKEN") - VPATH += $(SRC_PATH)/tests/tcg/sh4 test-macl: CFLAGS += -O -gSignature · 1 line
2.53.0
Signature · 1 line
Yosinori Sato