Fix SIGILL psw.addr reporting
cover Fix SIGILL psw.addr reporting
qemu-s390x puts a wrong value into SIGILL's siginfo_t's psw.addr: it should be a pointer to the instruction following the illegal instruction, but at the moment it is a pointer to the illegal instruction itself. This breaks OpenJDK, which relies on this value.
Patch 1 fixes the issue, patch 2 adds a test.
Ilya Leoshkevich (2):
target/s390x: Fix SIGILL psw.addr reporting
tests/tcg/s390x: Test SIGILL handling
linux-user/s390x/cpu_loop.c | 6 ++- target/s390x/excp_helper.c | 69 ++++++++++++++++++--------------- target/s390x/internal.h | 1 + tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/sigill.c | 41 ++++++++++++++++++++ 5 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 tests/tcg/s390x/sigill.c
Signature · 1 line
2.31.1
PATCH 2/2 tests/tcg/s390x: Test SIGILL handling
Verify that s390x-specific uc_mcontext.psw.addr is reported correctly.
--- tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/sigill.c | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/tcg/s390x/sigill.c
tests/tcg/s390x/Makefile.target +1 -0
@@ -8,3 +8,4 @@ TESTS+=exrl-trtr TESTS+=pack TESTS+=mvo TESTS+=mvc +TESTS+=sigill
tests/tcg/s390x/sigill.c new file +41 -0
41 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH 1/2 target/s390x: Fix SIGILL psw.addr reporting
In reply to Ilya LeoshkevichWhen a s390x CPU attempts to execute an illegal instruction, an operation exception is recognized. This is a suppressing exception, which means that the PSW is advanced by the length of the illegal instruction.
On the real hardware or in qemu-system-s390x the kernel then raises SIGILL with si_addr pointing to the suppressed instruction and psw.addr containing the updated PSW.
Unfortunately qemu-s390x sets both to the address of the suppressed instruction at the moment. Fix by sharing the PSW advancement logic with qemu-system-s390x and setting si_addr to the address of the instruction that raised the exception.
--- linux-user/s390x/cpu_loop.c | 6 +++- target/s390x/excp_helper.c | 69 ++++++++++++++++++++----------------- target/s390x/internal.h | 1 + 3 files changed, 43 insertions(+), 33 deletions(-)
linux-user/s390x/cpu_loop.c +5 -1
@@ -21,6 +21,7 @@#include "qemu-common.h" #include "qemu.h" #include "cpu_loop-common.h" +#include "internal.h" /* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */ #define S390X_FAIL_ADDR_MASK -4096LL
@@ -29,6 +30,7 @@ void cpu_loop(CPUS390XState *env) {CPUState *cs =env_cpu (env);int trapnr, n, sig; +target_ulong excp_psw_addr;target_siginfo_t info;target_ulong addr;abi_long ret;
@@ -38,6 +40,7 @@ void cpu_loop(CPUS390XState *env) trapnr =cpu_exec (cs);cpu_exec_end (cs);process_queued_cpu_work (cs); + excp_psw_addr = env->psw.addr;switch (trapnr) {case EXCP_INTERRUPT :
@@ -66,6 +69,7 @@ void cpu_loop(CPUS390XState *env) n =TARGET_TRAP_BRKPT ; goto do_signal_pc;case EXCP_PGM : +s390_cpu_program_interrupt_advance_psw (env); n = env->int_pgm_code;switch (n) {case PGM_OPERATION :
@@ -131,7 +135,7 @@ void cpu_loop(CPUS390XState *env)break ; do_signal_pc: - addr = env->psw.addr; + addr = excp_psw_addr; do_signal: info.si_signo = sig; info.si_errno =0 ;
target/s390x/excp_helper.c +37 -32
81 lines not on this page · raw message
target/s390x/internal.h +1 -0
@@ -272,6 +272,7 @@ bool s390_cpu_tlb_fill(CPUState *cs, vaddr address, int size,void s390x_cpu_do_unaligned_access (CPUState *cs,vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr); +void s390_cpu_program_interrupt_advance_psw (CPUS390XState *cpu);/* fpu_helper.c */
Signature · 1 line
2.31.1
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [PATCH 0/2] Fix SIGILL psw.addr reporting
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
069d02b tests/tcg/s390x: Test SIGILL handling a869643 target/s390x: Fix SIGILL psw.addr reporting
=== OUTPUT BEGIN ===
1/2 Checking commit a86964364d4d (target/s390x: Fix SIGILL psw.addr reporting) 2/2 Checking commit 069d02b0edf2 (tests/tcg/s390x: Test SIGILL handling)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #26:
new file mode 100644
ERROR: externs should be avoided in .c files
#37: FILE: tests/tcg/s390x/sigill.c:7:
+extern char expected_si_addr[];
ERROR: externs should be avoided in .c files
#38: FILE: tests/tcg/s390x/sigill.c:8:
+extern char expected_psw_addr[];
total: 2 errors, 1 warnings, 45 lines checked
Patch 2/2 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Quoted 6 lines · Ilya Leoshkevich
qemu-s390x puts a wrong value into SIGILL's siginfo_t's psw.addr: it should be a pointer to the instruction following the illegal instruction, but at the moment it is a pointer to the illegal instruction itself. This breaks OpenJDK, which relies on this value.
Patch 1 fixes the issue, patch 2 adds a test.
I assume that should fix
https://bugs.launchpad.net/qemu/+bug/1920913
right?
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 1/2] target/s390x: Fix SIGILL psw.addr reporting
In reply to Ilya LeoshkevichQuoted 160 lines · Ilya Leoshkevich
When a s390x CPU attempts to execute an illegal instruction, an operation exception is recognized. This is a suppressing exception, which means that the PSW is advanced by the length of the illegal instruction.
On the real hardware or in qemu-system-s390x the kernel then raises SIGILL with
si_addrpointing to the suppressed instruction and psw.addr containing the updated PSW.Unfortunately qemu-s390x sets both to the address of the suppressed instruction at the moment. Fix by sharing the PSW advancement logic with qemu-system-s390x and setting
si_addrto the address of the instruction that raised the exception.Signed-off-by Ilya Leoshkevich <[email protected]>--- linux-user/s390x/cpu_loop.c | 6 +++- target/s390x/excp_helper.c | 69 ++++++++++++++++++++----------------- target/s390x/internal.h | 1 + 3 files changed, 43 insertions(+), 33 deletions(-)linux-user/s390x/cpu_loop.c +5
-1@@ -21,6 +21,7 @@#include "qemu-common.h" #include "qemu.h" #include "cpu_loop-common.h" +#include "internal.h" /* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */ #define S390X_FAIL_ADDR_MASK -4096LL@@ -29,6 +30,7 @@ void cpu_loop(CPUS390XState *env) {CPUState *cs =env_cpu (env);int trapnr, n, sig; +target_ulong excp_psw_addr;target_siginfo_t info;target_ulong addr;abi_long ret;@@ -38,6 +40,7 @@ void cpu_loop(CPUS390XState *env) trapnr =cpu_exec (cs);cpu_exec_end (cs);process_queued_cpu_work (cs); + excp_psw_addr = env->psw.addr;switch (trapnr) {case EXCP_INTERRUPT :@@ -66,6 +69,7 @@ void cpu_loop(CPUS390XState *env) n =TARGET_TRAP_BRKPT ; goto do_signal_pc;case EXCP_PGM : +s390_cpu_program_interrupt_advance_psw (env); n = env->int_pgm_code;switch (n) {case PGM_OPERATION :@@ -131,7 +135,7 @@ void cpu_loop(CPUS390XState *env)break ; do_signal_pc: - addr = env->psw.addr; + addr = excp_psw_addr; do_signal: info.si_signo = sig; info.si_errno =0 ;target/s390x/excp_helper.c +37
-32@@ -82,6 +82,42 @@ void HELPER(data_exception)(CPUS390XState *env, uint32_t dxc)tcg_s390_data_exception (env, dxc,GETPC ()); } +void s390_cpu_program_interrupt_advance_psw (CPUS390XState *env) +{ +switch (env->int_pgm_code) { +case PGM_PER : +if (env->per_perc_atmid &PER_CODE_EVENT_NULLIFICATION ) { +break ; + } +/* FALL THROUGH */ +case PGM_OPERATION : +case PGM_PRIVILEGED : +case PGM_EXECUTE : +case PGM_PROTECTION : +case PGM_ADDRESSING : +case PGM_SPECIFICATION : +case PGM_DATA : +case PGM_FIXPT_OVERFLOW : +case PGM_FIXPT_DIVIDE : +case PGM_DEC_OVERFLOW : +case PGM_DEC_DIVIDE : +case PGM_HFP_EXP_OVERFLOW : +case PGM_HFP_EXP_UNDERFLOW : +case PGM_HFP_SIGNIFICANCE : +case PGM_HFP_DIVIDE : +case PGM_TRANS_SPEC : +case PGM_SPECIAL_OP : +case PGM_OPERAND : +case PGM_HFP_SQRT : +case PGM_PC_TRANS_SPEC : +case PGM_ALET_SPEC : +case PGM_MONITOR : +/* advance the PSW if our exception is not nullifying */ + env->psw.addr += env->int_pgm_ilen; +break ; + } +} +#if defined(CONFIG_USER_ONLY )void s390_cpu_do_interrupt (CPUState *cs)@@ -202,38 +238,7 @@ static void do_program_interrupt(CPUS390XState *env)assert (ilen ==2 || ilen ==4 || ilen ==6 ); -switch (env->int_pgm_code) { -case PGM_PER : -if (env->per_perc_atmid &PER_CODE_EVENT_NULLIFICATION ) { -break ; - } -/* FALL THROUGH */ -case PGM_OPERATION : -case PGM_PRIVILEGED : -case PGM_EXECUTE : -case PGM_PROTECTION : -case PGM_ADDRESSING : -case PGM_SPECIFICATION : -case PGM_DATA : -case PGM_FIXPT_OVERFLOW : -case PGM_FIXPT_DIVIDE : -case PGM_DEC_OVERFLOW : -case PGM_DEC_DIVIDE : -case PGM_HFP_EXP_OVERFLOW : -case PGM_HFP_EXP_UNDERFLOW : -case PGM_HFP_SIGNIFICANCE : -case PGM_HFP_DIVIDE : -case PGM_TRANS_SPEC : -case PGM_SPECIAL_OP : -case PGM_OPERAND : -case PGM_HFP_SQRT : -case PGM_PC_TRANS_SPEC : -case PGM_ALET_SPEC : -case PGM_MONITOR : -/* advance the PSW if our exception is not nullifying */ - env->psw.addr += ilen; -break ; - } +s390_cpu_program_interrupt_advance_psw (env); qemu_log_mask(CPU_LOG_INT, "%s: code=0x%x ilen=%d psw: %" PRIx64 " %" PRIx64 "\n",target/s390x/internal.h +0
-0
target/s390x/internal.h bool s390_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 278
Quoted 7 lines
target/s390x/internal.h +1
-0@@ -272,6 +272,7 @@ bool s390_cpu_tlb_fill(CPUState *cs, vaddr address, int size,void s390x_cpu_do_unaligned_access (CPUState *cs,vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr); +void s390_cpu_program_interrupt_advance_psw (CPUS390XState *cpu);/* fpu_helper.c */
LGTM, thanks
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 2/2] tests/tcg/s390x: Test SIGILL handling
In reply to Ilya LeoshkevichQuoted 24 lines · Ilya Leoshkevich
Verify that s390x-specific uc_mcontext.psw.addr is reported correctly.
Signed-off-by Ilya Leoshkevich <[email protected]>--- tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/sigill.c | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)create mode 100644
tests/tcg/s390x/sigill.ctests/tcg/s390x/Makefile.target +1
-0@@ -8,3 +8,4 @@ TESTS+=exrl-trtr TESTS+=pack TESTS+=mvo TESTS+=mvc +TESTS+=sigilltests/tcg/s390x/sigill.c new file +0
-0
tests/tcg/s390x/sigill.c line 8
Quoted 8 lines
tests/tcg/s390x/sigill.c new file +8
-0@@ -0,0 +1,41 @@ +#include <assert.h> +#include <signal.h> +#include <string.h> +#include <ucontext.h> +#include <unistd.h> + +extern char expected_si_addr[]; +extern char expected_psw_addr[];
Why "extern" ? For the magic inline asm below to work?
Quoted 16 lines
tests/tcg/s390x/sigill.c new file +16
-0+ +static void handle_signal (int sig,siginfo_t *info,void *ucontext) +{ +if (sig !=SIGILL ) { +_exit (1 ); + } + +if (info->si_addr != expected_si_addr) { +_exit (2 ); + } + +if (((ucontext_t *)ucontext)->uc_mcontext.psw.addr != + (unsigned long )expected_psw_addr) { +_exit (3 ); + } +}
tests/tcg/s390x/sigill.c line 34
Quoted 10 lines
tests/tcg/s390x/sigill.c new file +10
-0+ +int main (void ) +{ +struct sigaction act; + +memset (&act,0 ,sizeof (act)); + act.sa_sigaction = handle_signal; + act.sa_flags =SA_SIGINFO ; + +int ret =sigaction (SIGILL , &act,NULL );
Mixing code and declaration.
tests/tcg/s390x/sigill.c line 38
Quoted 4 lines
tests/tcg/s390x/sigill.c new file +4
-0+assert (ret ==0 ); + + asmvolatile ("expected_si_addr:\t.byte\t0x00,0x00\n" +"expected_psw_addr:" );
At least I am confused how the right values actually end up in expected_si_addr and expected_psw_addr.
Can we maybe add a comment? This looks quite hacky ;)
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 2/2] tests/tcg/s390x: Test SIGILL handling
Quoted 30 lines · David Hildenbrand
Quoted 29 lines · Ilya Leoshkevich
Verify that s390x-specific uc_mcontext.psw.addr is reported correctly.
Signed-off-by Ilya Leoshkevich <[email protected]>---
tests/tcg/s390x/Makefile.target | 1 +
tests/tcg/s390x/sigill.c | 41
+++++++++++++++++++++++++++++++++2 files changed, 42 insertions(+)create mode 100644
tests/tcg/s390x/sigill.ctests/tcg/s390x/Makefile.target +0
-0@@ -8,3 +8,4 @@ TESTS+=exrl-trtrTESTS+=pack
TESTS+=mvo
TESTS+=mvctests/tcg/s390x/Makefile.target +1
-0+TESTS+=sigilltests/tcg/s390x/sigill.c new file +2
-0@@ -0,0 +1,41 @@ +#include <assert.h> +#include <signal.h>
Quoted 8 lines
tests/tcg/s390x/sigill.c line 8
Quoted 6 lines
tests/tcg/s390x/sigill.c new file +6
-0+#include <string.h> +#include <ucontext.h> +#include <unistd.h> + +extern char expected_si_addr[]; +extern char expected_psw_addr[];Why "extern" ? For the magic inline asm below to work?
Yes - it cannot be static, because AFAIK there is no such thing as static variable declaration (one can only define static variables).
Quoted 21 lines
Quoted 21 lines
tests/tcg/s390x/sigill.c new file +2
-0+ +static void handle_signal(int sig, siginfo_t *info, void*ucontext)
tests/tcg/s390x/sigill.c new file +18
-0+{ + if (sig != SIGILL) { + _exit(1); + } + + if (info->si_addr != expected_si_addr) { + _exit(2); + } + + if (((ucontext_t *)ucontext)->uc_mcontext.psw.addr != + (unsigned long)expected_psw_addr) { + _exit(3); + } +} + +int main (void ) +{ + struct sigaction act;
Quoted 8 lines
tests/tcg/s390x/sigill.c line 34
Quoted 6 lines
tests/tcg/s390x/sigill.c new file +6
-0+ + memset(&act, 0, sizeof(act)); + act.sa_sigaction = handle_signal; + act.sa_flags = SA_SIGINFO; + + int ret = sigaction(SIGILL, &act, NULL);Mixing code and declaration.
Ouch, will fix.
Quoted 9 lines
Quoted 4 lines
+ assert(ret == 0);
+
+ asmvolatile("expected_si_addr:\t.byte\t0x00,0x00\n" + "expected_psw_addr:");At least I am confused how the right values actually end up in
expected_si_addrandexpected_psw_addr.Can we maybe add a comment? This looks quite hacky ;)
This whole construction is roughly the same as having sigill.s file with:
.globl expected_si_addrexpected_si_addr: .byte 0,0
.globl expected_psw_addrexpected_psw_addr: br 14
and sigill.h file with:
void expected_si_addr(void);
extern char expected_psw_addr[];
Doing it this way would complicate the build, so I thought it would be better to just put everything into a single file.
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Ilya Leoshkevich
qemu-s390x puts a wrong value into SIGILL's siginfo_t's psw.addr:
it
should be a pointer to the instruction following the illegal instruction, but at the moment it is a pointer to the illegal instruction itself. This breaks OpenJDK, which relies on this value.
Quoted 7 lines
Quoted 1 line
Patch 1 fixes the issue, patch 2 adds a test.
I assume that should fix
https://bugs.launchpad.net/qemu/+bug/1920913
right?
Yes, I have this Buglink in the fix commit message.
I have to admit I did not test Java more extensively - there might be more things going on - but at least with this the SIGILL on startup is gone, and very simple programs work.
Quoted 11 lines · On Fri, 21 May 2021 12:45:30 +0200 Ilya Leoshkevich <[email protected]> wrote:
Quoted 10 lines · David Hildenbrand
Quoted 8 lines · Ilya Leoshkevich
qemu-s390x puts a wrong value into SIGILL's siginfo_t's psw.addr:
it
should be a pointer to the instruction following the illegal instruction, but at the moment it is a pointer to the illegal instruction itself. This breaks OpenJDK, which relies on this value.Patch 1 fixes the issue, patch 2 adds a test.
Quoted 7 lines
Quoted 5 lines
I assume that should fix
https://bugs.launchpad.net/qemu/+bug/1920913
right?
Yes, I have this Buglink in the fix commit message.
Better add https://gitlab.com/qemu-project/qemu/-/issues/319 as the Buglink :)
Quoted 5 lines
I have to admit I did not test Java more extensively - there might be more things going on - but at least with this the SIGILL on startup is gone, and very simple programs work.