Skip to contentSkip to conversation

linux-user/s390x: signal with SIGFPE on compare-and-trap

PATCH In review Jonathan Albrecht 3 participants 7 messages Jul 9, 2021 – Jul 13, 2021

lore
Share & tools
lore
#0 lore raw

v3 cover linux-user/s390x: signal with SIGFPE on compare-and-trap

qemu-s390x signals with SIGILL on compare-and-trap instructions. This breaks OpenJDK which expects SIGFPE in its implementation of implicit exceptions.

This patch depends on [PATCH v6 0/2] target/s390x: Fix SIGILL and SIGFPE psw.addr reporting
https://lore.kernel.org/qemu-devel/[email protected]/

Based-on: [email protected]

v1 -> v2:
- Update to latest version of '... psw.addr reporting' patch
- Rebase to master and fix conflicts in tests/tcg/s390x/Makefile.target

v2 -> v3:
- Check for non-simulated IEEE exception DXC codes explicitly when getting si_codes
- Ensure si_code is set in all cases
- Improve comments

Jonathan Albrecht (2):
linux-user/s390x: signal with SIGFPE on compare-and-trap tests/tcg: Test that compare-and-trap raises SIGFPE

 linux-user/s390x/cpu_loop.c     |  54 ++++++++++-------
 tests/tcg/s390x/Makefile.target |   1 +
 tests/tcg/s390x/trap.c          | 102 ++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+), 20 deletions(-)
 create mode 100644 tests/tcg/s390x/trap.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/2 linux-user/s390x: signal with SIGFPE on compare-and-trap

Currently when a compare-and-trap instruction is executed, qemu will always raise a SIGILL signal. On real hardware, a SIGFPE is raised.

Change the PGM_DATA case in cpu_loop to follow the behavior in linux kernel /arch/s390/kernel/traps.c.
* Only raise SIGILL if DXC == 0
* If DXC matches a non-simulated IEEE exception, raise SIGFPE with correct si_code
* Raise SIGFPE with si_code == 0 for everything else

When applied on [email protected], this fixes crashes in the java jdk such as the linked bug.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/319

---
 linux-user/s390x/cpu_loop.c | 54 +++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 20 deletions(-)
linux-user/s390x/cpu_loop.c +34 -20
index 22f2e89c62..6a69a6dd26 100644
@@ -25,6 +25,35 @@
 /* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */
 #define S390X_FAIL_ADDR_MASK -4096LL
 
+static int get_pgm_data_si_code(int dxc_code)
+{
+    switch (dxc_code) {
+    /* Non-simulated IEEE exceptions */
+    case 0x80:
+        return TARGET_FPE_FLTINV;
+    case 0x40:
+        return TARGET_FPE_FLTDIV;
+    case 0x20:
+    case 0x28:
+    case 0x2c:
+        return TARGET_FPE_FLTOVF;
+    case 0x10:
+    case 0x18:
+    case 0x1c:
+        return TARGET_FPE_FLTUND;
+    case 0x08:
+    case 0x0c:
+        return TARGET_FPE_FLTRES;
+    }
+    /*
+     * Non-IEEE and simulated IEEE:
+     * Includes compare-and-trap, quantum exception, etc.
+     * Simulated IEEE are included here to match current
+     * s390x linux kernel.
+     */
+    return 0;
+}
+
 void cpu_loop(CPUS390XState *env)
 {
     CPUState *cs = env_cpu(env);
@@ -106,29 +135,14 @@ void cpu_loop(CPUS390XState *env)
 
             case PGM_DATA:
                 n = (env->fpc >> 8) & 0xff;
-                if (n == 0xff) {
-                    /* compare-and-trap */
+                if (n == 0) {
                     goto do_sigill_opn;
-                } else {
-                    /* An IEEE exception, simulated or otherwise.  */
-                    if (n & 0x80) {
-                        n = TARGET_FPE_FLTINV;
-                    } else if (n & 0x40) {
-                        n = TARGET_FPE_FLTDIV;
-                    } else if (n & 0x20) {
-                        n = TARGET_FPE_FLTOVF;
-                    } else if (n & 0x10) {
-                        n = TARGET_FPE_FLTUND;
-                    } else if (n & 0x08) {
-                        n = TARGET_FPE_FLTRES;
-                    } else {
-                        /* ??? Quantum exception; BFP, DFP error.  */
-                        goto do_sigill_opn;
-                    }
-                    sig = TARGET_SIGFPE;
-                    goto do_signal_pc;
                 }
 
+                sig = TARGET_SIGFPE;
+                n = get_pgm_data_si_code(n);
+                goto do_signal_pc;
+
             default:
                 fprintf(stderr, "Unhandled program exception: %#x\n", n);
                 cpu_dump_state(cs, stderr, 0);
Signature · 1 line
2.31.1
Reviewed-by on v3 1/2
#2 lore raw
Quoted 13 lines · Jonathan Albrecht

Currently when a compare-and-trap instruction is executed, qemu will always raise a SIGILL signal. On real hardware, a SIGFPE is raised.

Change the PGM_DATA case in cpu_loop to follow the behavior in linux kernel /arch/s390/kernel/traps.c.
* Only raise SIGILL if DXC == 0
* If DXC matches a non-simulated IEEE exception, raise SIGFPE with correct si_code
* Raise SIGFPE with si_code == 0 for everything else

When applied [email protected], this fixes crashes in the java jdk such as the linked bug.

Quoted 6 lines

Resolves:https://gitlab.com/qemu-project/qemu/-/issues/319

---
  linux-user/s390x/cpu_loop.c | 54 +++++++++++++++++++++++--------------
  1 file changed, 34 insertions(+), 20 deletions(-)

r~

#3 lore raw

PATCH v3 2/2 tests/tcg: Test that compare-and-trap raises SIGFPE

In reply to Jonathan Albrecht
---
 tests/tcg/s390x/Makefile.target |   1 +
 tests/tcg/s390x/trap.c          | 102 ++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+)
 create mode 100644 tests/tcg/s390x/trap.c
tests/tcg/s390x/Makefile.target +1 -0
index 0a5b25c156..d440ecd6f7 100644
@@ -9,6 +9,7 @@ TESTS+=exrl-trtr
 TESTS+=pack
 TESTS+=mvo
 TESTS+=mvc
+TESTS+=trap
 
 # This triggers failures on s390x hosts about 4% of the time
 run-signals: signals
tests/tcg/s390x/trap.c new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.31.1
#4 lore raw
In reply to Jonathan Albrecht

Le 09/07/2021 à 18:04, Jonathan Albrecht a écrit :

Quoted 24 lines

qemu-s390x signals with SIGILL on compare-and-trap instructions. This breaks OpenJDK which expects SIGFPE in its implementation of implicit exceptions.

This patch depends on [PATCH v6 0/2] target/s390x: Fix SIGILL and SIGFPE psw.addr reporting
https://lore.kernel.org/qemu-devel/[email protected]/

Based-on: [email protected]

v1 -> v2:
- Update to latest version of '... psw.addr reporting' patch
- Rebase to master and fix conflicts in tests/tcg/s390x/Makefile.target

v2 -> v3:
- Check for non-simulated IEEE exception DXC codes explicitly when getting si_codes
- Ensure si_code is set in all cases
- Improve comments

Jonathan Albrecht (2):
linux-user/s390x: signal with SIGFPE on compare-and-trap tests/tcg: Test that compare-and-trap raises SIGFPE

Quoted 6 lines
 linux-user/s390x/cpu_loop.c     |  54 ++++++++++-------
 tests/tcg/s390x/Makefile.target |   1 +
 tests/tcg/s390x/trap.c          | 102 ++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+), 20 deletions(-)
 create mode 100644 tests/tcg/s390x/trap.c

Series applied to my linux-user-for-6.1 branch.

Thanks,
Laurent

#5 lore raw
Quoted 9 lines · Laurent Vivier

Le 09/07/2021 à 18:04, Jonathan Albrecht a écrit :

Quoted 8 lines

qemu-s390x signals with SIGILL on compare-and-trap instructions. This breaks OpenJDK which expects SIGFPE in its implementation of implicit exceptions.

This patch depends on [PATCH v6 0/2] target/s390x: Fix SIGILL and SIGFPE psw.addr reporting
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 6 lines
Quoted 3 lines

Based-on: [email protected]

Series applied to my linux-user-for-6.1 branch.

Thanks Laurent, I see this series has been applied to https://github.com/vivier/qemu/commits/linux-user-for-6.1 but the following series that this is based on also needs to be applied:

https://lore.kernel.org/qemu-devel/[email protected]/

Did some local testing and looks like missing that series caused https://app.travis-ci.com/github/vivier/qemu/jobs/523853464 to fail.

Oh, just saw Ilya's email that the test patch has not been reviewed. Hopefully that can happen so they can both make it in.

Thanks,

Jon

#6 lore raw

Le 12/07/2021 à 23:29, jonathan.albrecht a écrit :

Quoted 21 lines · Laurent Vivier
Quoted 9 lines

Le 09/07/2021 à 18:04, Jonathan Albrecht a écrit :

Quoted 8 lines

qemu-s390x signals with SIGILL on compare-and-trap instructions. This breaks OpenJDK which expects SIGFPE in its implementation of implicit exceptions.

This patch depends on [PATCH v6 0/2] target/s390x: Fix SIGILL and SIGFPE psw.addr reporting
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 6 lines
Quoted 3 lines

Based-on: [email protected]

Series applied to my linux-user-for-6.1 branch.

Thanks Laurent, I see this series has been applied to
https://github.com/vivier/qemu/commits/linux-user-for-6.1 but the following series that this is based on also needs to be applied:

Quoted 7 lines

https://lore.kernel.org/qemu-devel/[email protected]/

Did some local testing and looks like missing that series caused
https://app.travis-ci.com/github/vivier/qemu/jobs/523853464 to fail.

Oh, just saw Ilya's email that the test patch has not been reviewed. Hopefully that can happen so they can both make it in.

I've removed these two patches from my patch queue. I'll do a new one with both series when they will be ready.

Thanks,
Laurent