Skip to contentSkip to conversation

linux-user: fix various SIGSEGV delivery bugs

PATCH In review Peter Maydell 5 participants 19 messages Nov 6, 2017 – Nov 9, 2017

lore
Share & tools
lore
#0 lore raw

cover linux-user: fix various SIGSEGV delivery bugs

This patchset fixes most of the problems with delivering SIGSEGV noted in https://bugs.launchpad.net/qemu/+bug/1705118:
* s390x is missing masking operations on the fault address passed to the guest in si_addr
* ppc is passing the PC of the offending insn, not the data address, into si_addr
* sparc fails to record the address of a data fault and so passes 0 into si_addr
* sparc isn't implementing rt_sigaction correctly (it has an extra 'restorer' argument, similar to alpha)

It doesn't actually get the sparc testcase from that bug working, though, because SPARC is entirely missing support for setup_rt_frame() and do_rt_sigreturn(), so it can't deliver RT signals.

Implementing RT signal delivery is too big a bugfix for an afternoon, so here are the simpler parts. I'll leave the signal delivery part to somebody who cares about SPARC guests...

thanks
-- PMM

Peter Maydell (4):
linux-user/s390x: Mask si_addr for SIGSEGV
linux-user/ppc: Report correct fault address for data faults linux-user/sparc: Put address for data faults where linux-user expects
it
linux-user: Handle rt_sigaction correctly for SPARC

 linux-user/main.c         |  8 ++++++--
 linux-user/syscall.c      | 27 +++++++++++++++++++++++----
 target/sparc/mmu_helper.c |  8 ++++++++
 3 files changed, 37 insertions(+), 6 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/4 linux-user/s390x: Mask si_addr for SIGSEGV

For s390x, the address passed to a signal handler in the siginfo_t si_addr field is masked (in the kernel this is done in do_sigbus() and do_sigsegv() in arch/s390/mm/fault.c). Implement this architecture-specific oddity in linux-user.

This is one of the issues described in
https://bugs.launchpad.net/qemu/+bug/1705118

---
 linux-user/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
linux-user/main.c +5 -1
index aa02f25..b6dd9ef 100644
@@ -3238,6 +3238,10 @@ void cpu_loop(CPUAlphaState *env)
 #endif /* TARGET_ALPHA */
 
 #ifdef TARGET_S390X
+
+/* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */
+#define S390X_FAIL_ADDR_MASK -4096LL
+
 void cpu_loop(CPUS390XState *env)
 {
     CPUState *cs = CPU(s390_env_get_cpu(env));
@@ -3294,7 +3298,7 @@ void cpu_loop(CPUS390XState *env)
                 sig = TARGET_SIGSEGV;
                 /* XXX: check env->error_code */
                 n = TARGET_SEGV_MAPERR;
-                addr = env->__excp_addr;
+                addr = env->__excp_addr & S390X_FAIL_ADDR_MASK;
                 goto do_signal;
             case PGM_EXECUTE:
             case PGM_SPECIFICATION:
Signature · 1 line
2.7.4
Reviewed-by on v1 1/4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] linux-user/s390x: Mask si_addr for SIGSEGV

Le 06/11/2017 à 19:33, Peter Maydell a écrit :

Quoted 30 lines

For s390x, the address passed to a signal handler in the siginfo_t si_addr field is masked (in the kernel this is done in do_sigbus() and do_sigsegv() in arch/s390/mm/fault.c). Implement this architecture-specific oddity in linux-user.

This is one of the issues described in
https://bugs.launchpad.net/qemu/+bug/1705118

---
 linux-user/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
linux-user/main.c +4 -0
index aa02f25..b6dd9ef 100644
@@ -3238,6 +3238,10 @@ void cpu_loop(CPUAlphaState *env)
 #endif /* TARGET_ALPHA */
 
 #ifdef TARGET_S390X
+
+/* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */
+#define S390X_FAIL_ADDR_MASK -4096LL
+
 void cpu_loop(CPUS390XState *env)
 {
     CPUState *cs = CPU(s390_env_get_cpu(env));

linux-user/main.c void cpu_loop(CPUS390XState *env) · line 3304

Quoted 8 lines
linux-user/main.c +1 -1
@@ -3294,7 +3298,7 @@ void cpu_loop(CPUS390XState *env)
                 sig = TARGET_SIGSEGV;
                 /* XXX: check env->error_code */
                 n = TARGET_SEGV_MAPERR;
-                addr = env->__excp_addr;
+                addr = env->__excp_addr & S390X_FAIL_ADDR_MASK;
                 goto do_signal;
             case PGM_EXECUTE:
             case PGM_SPECIFICATION:
Reviewed-by on v1 1/4
#3 lore raw

Re: [Qemu-devel] [PATCH 1/4] linux-user/s390x: Mask si_addr for SIGSEGV

In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

For s390x, the address passed to a signal handler in the siginfo_t si_addr field is masked (in the kernel this is done in do_sigbus() and do_sigsegv() in arch/s390/mm/fault.c). Implement this architecture-specific oddity in linux-user.

This is one of the issues described in
https://bugs.launchpad.net/qemu/+bug/1705118

Quoted 29 lines
---
 linux-user/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
linux-user/main.c +5 -1
index aa02f25..b6dd9ef 100644
@@ -3238,6 +3238,10 @@ void cpu_loop(CPUAlphaState *env)
 #endif /* TARGET_ALPHA */
 
 #ifdef TARGET_S390X
+
+/* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */
+#define S390X_FAIL_ADDR_MASK -4096LL
+
 void cpu_loop(CPUS390XState *env)
 {
     CPUState *cs = CPU(s390_env_get_cpu(env));
@@ -3294,7 +3298,7 @@ void cpu_loop(CPUS390XState *env)
                 sig = TARGET_SIGSEGV;
                 /* XXX: check env->error_code */
                 n = TARGET_SEGV_MAPERR;
-                addr = env->__excp_addr;
+                addr = env->__excp_addr & S390X_FAIL_ADDR_MASK;
                 goto do_signal;
             case PGM_EXECUTE:
             case PGM_SPECIFICATION:
Reviewed-by on v1 1/4
#4 lore raw

Re: [Qemu-devel] [PATCH 1/4] linux-user/s390x: Mask si_addr for SIGSEGV

In reply to Peter Maydell
Quoted 5 lines · Peter Maydell

For s390x, the address passed to a signal handler in the siginfo_t si_addr field is masked (in the kernel this is done in do_sigbus() and do_sigsegv() in arch/s390/mm/fault.c). Implement this architecture-specific oddity in linux-user.

Quoted 7 lines

This is one of the issues described in
https://bugs.launchpad.net/qemu/+bug/1705118

---
 linux-user/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Accurate, but really seems like a s390x kernel bug.

r~

#5 lore raw

Re: [Qemu-devel] [PATCH 1/4] linux-user/s390x: Mask si_addr for SIGSEGV

Quoted 9 lines · Richard Henderson
Quoted 8 lines · Peter Maydell

For s390x, the address passed to a signal handler in the siginfo_t si_addr field is masked (in the kernel this is done in do_sigbus() and do_sigsegv() in arch/s390/mm/fault.c). Implement this architecture-specific oddity in linux-user.

This is one of the issues described in
https://bugs.launchpad.net/qemu/+bug/1705118

Quoted 6 lines
Quoted 4 lines
---
 linux-user/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Accurate, but really seems like a s390x kernel bug.

The kernel code goes out of its way to do the masking, so if it's a bug presumably it's a "retain back compat with some older bug" thing...

thanks
-- PMM

#6 lore raw

PATCH 2/4 linux-user/ppc: Report correct fault address for data faults

In reply to Peter Maydell

For faults on loads and stores, ppc_cpu_handle_mmu_fault() in target/ppc/user_only_helper.c stores the offending address in env->spr[SPR_DAR]. Report this correctly to the guest in si_addr, rather than incorrectly using the address of the instruction that caused the fault.

This fixes the test case in
https://bugs.launchpad.net/qemu/+bug/1077116
for ppc, ppc64 and ppc64le.

---
 linux-user/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/main.c +1 -1
index b6dd9ef..6286661 100644
@@ -1420,7 +1420,7 @@ void cpu_loop(CPUPPCState *env)
                 info.si_code = TARGET_SEGV_MAPERR;
                 break;
             }
-            info._sifields._sigfault._addr = env->nip;
+            info._sifields._sigfault._addr = env->spr[SPR_DAR];
             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
             break;
         case POWERPC_EXCP_ISI:      /* Instruction storage exception         */
Signature · 1 line
2.7.4
Reviewed-by on v1 2/4
#7 lore raw

Re: [Qemu-devel] [PATCH 2/4] linux-user/ppc: Report correct fault address for data faults

Le 06/11/2017 à 19:33, Peter Maydell a écrit :

Quoted 21 lines

For faults on loads and stores, ppc_cpu_handle_mmu_fault() in target/ppc/user_only_helper.c stores the offending address in env->spr[SPR_DAR]. Report this correctly to the guest in si_addr, rather than incorrectly using the address of the instruction that caused the fault.

This fixes the test case in
https://bugs.launchpad.net/qemu/+bug/1077116
for ppc, ppc64 and ppc64le.

---
 linux-user/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/main.c +0 -0
index b6dd9ef..6286661 100644

linux-user/main.c void cpu_loop(CPUPPCState *env) · line 1426

Quoted 8 lines
linux-user/main.c +1 -1
@@ -1420,7 +1420,7 @@ void cpu_loop(CPUPPCState *env)
                 info.si_code = TARGET_SEGV_MAPERR;
                 break;
             }
-            info._sifields._sigfault._addr = env->nip;
+            info._sifields._sigfault._addr = env->spr[SPR_DAR];
             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
             break;
         case POWERPC_EXCP_ISI:      /* Instruction storage exception         */
Reviewed-by on v1 2/4
#8 lore raw

Re: [Qemu-devel] [PATCH 2/4] linux-user/ppc: Report correct fault address for data faults

In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

For faults on loads and stores, ppc_cpu_handle_mmu_fault() in target/ppc/user_only_helper.c stores the offending address in env->spr[SPR_DAR]. Report this correctly to the guest in si_addr, rather than incorrectly using the address of the instruction that caused the fault.

Quoted 8 lines

This fixes the test case in
https://bugs.launchpad.net/qemu/+bug/1077116
for ppc, ppc64 and ppc64le.

---
 linux-user/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

r~

#9 lore raw

PATCH 3/4 linux-user/sparc: Put address for data faults where linux-user expects it

In reply to Peter Maydell

In the user-mode-only version of sparc_cpu_handle_mmu_fault(), we must save the fault address for a data fault into the CPU state's mmu registers, because the code in linux-user/main.c expects to find it there in order to populate the si_addr field of the guest siginfo.

---
 target/sparc/mmu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/sparc/mmu_helper.c +8 -0
index 126ea5e..d5b6c1e 100644
@@ -30,10 +30,18 @@
 int sparc_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
                                int mmu_idx)
 {
+    SPARCCPU *cpu = SPARC_CPU(cs);
+    CPUSPARCState *env = &cpu->env;
+
     if (rw & 2) {
         cs->exception_index = TT_TFAULT;
     } else {
         cs->exception_index = TT_DFAULT;
+#ifdef TARGET_SPARC64
+        env->dmmu.mmuregs[4] = address;
+#else
+        env->mmuregs[4] = address;
+#endif
     }
     return 1;
 }
Signature · 1 line
2.7.4
#10 lore raw

Re: [Qemu-devel] [PATCH 3/4] linux-user/sparc: Put address for data faults where linux-user expects it

Le 06/11/2017 à 19:33, Peter Maydell a écrit :

Quoted 25 lines

In the user-mode-only version of sparc_cpu_handle_mmu_fault(), we must save the fault address for a data fault into the CPU state's mmu registers, because the code in linux-user/main.c expects to find it there in order to populate the si_addr field of the guest siginfo.

---
 target/sparc/mmu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/sparc/mmu_helper.c +3 -0
index 126ea5e..d5b6c1e 100644
@@ -30,10 +30,18 @@
 int sparc_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
                                int mmu_idx)
 {
+    SPARCCPU *cpu = SPARC_CPU(cs);
+    CPUSPARCState *env = &cpu->env;
+
     if (rw & 2) {
         cs->exception_index = TT_TFAULT;

target/sparc/mmu_helper.c line 47

Quoted 10 lines
target/sparc/mmu_helper.c +5 -0
     } else {
         cs->exception_index = TT_DFAULT;
+#ifdef TARGET_SPARC64
+        env->dmmu.mmuregs[4] = address;
+#else
+        env->mmuregs[4] = address;
+#endif
     }
     return 1;
 }

The softmmu version of sparc_cpu_handle_mmu_fault() also updates mmuregs[3]. Is it needed for this one (for ucontext)?

Thanks,
Laurent

#11 lore raw

Re: [Qemu-devel] [PATCH 3/4] linux-user/sparc: Put address for data faults where linux-user expects it

Quoted 31 lines · Laurent Vivier
Quoted 30 lines · Peter Maydell

In the user-mode-only version of sparc_cpu_handle_mmu_fault(), we must save the fault address for a data fault into the CPU state's mmu registers, because the code in linux-user/main.c expects to find it there in order to populate the si_addr field of the guest siginfo.

---
 target/sparc/mmu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/sparc/mmu_helper.c +7 -0
index 126ea5e..d5b6c1e 100644
@@ -30,10 +30,18 @@
 int sparc_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
                                int mmu_idx)
 {
+    SPARCCPU *cpu = SPARC_CPU(cs);
+    CPUSPARCState *env = &cpu->env;
+
     if (rw & 2) {
         cs->exception_index = TT_TFAULT;
     } else {
         cs->exception_index = TT_DFAULT;
+#ifdef TARGET_SPARC64
+        env->dmmu.mmuregs[4] = address;
+#else
+        env->mmuregs[4] = address;
Quoted 8 lines

target/sparc/mmu_helper.c line 47

Quoted 5 lines
target/sparc/mmu_helper.c +1 -0
+#endif
     }
     return 1;
 }

The softmmu version of sparc_cpu_handle_mmu_fault() also updates mmuregs[3]. Is it needed for this one (for ucontext)?

Nothing in linux-user/ reads mmuregs[3], so I assume not.

thanks
-- PMM

Reviewed-by on v1 3/4
#12 lore raw

Re: [Qemu-devel] [PATCH 3/4] linux-user/sparc: Put address for data faults where linux-user expects it

In reply to Peter Maydell

Le 06/11/2017 à 19:33, Peter Maydell a écrit :

Quoted 25 lines

In the user-mode-only version of sparc_cpu_handle_mmu_fault(), we must save the fault address for a data fault into the CPU state's mmu registers, because the code in linux-user/main.c expects to find it there in order to populate the si_addr field of the guest siginfo.

---
 target/sparc/mmu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/sparc/mmu_helper.c +3 -0
index 126ea5e..d5b6c1e 100644
@@ -30,10 +30,18 @@
 int sparc_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
                                int mmu_idx)
 {
+    SPARCCPU *cpu = SPARC_CPU(cs);
+    CPUSPARCState *env = &cpu->env;
+
     if (rw & 2) {
         cs->exception_index = TT_TFAULT;

target/sparc/mmu_helper.c line 47

Quoted 10 lines
target/sparc/mmu_helper.c +5 -0
     } else {
         cs->exception_index = TT_DFAULT;
+#ifdef TARGET_SPARC64
+        env->dmmu.mmuregs[4] = address;
+#else
+        env->mmuregs[4] = address;
+#endif
     }
     return 1;
 }
Reviewed-by on v1 3/4
#13 lore raw

Re: [Qemu-devel] [PATCH 3/4] linux-user/sparc: Put address for data faults where linux-user expects it

In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

In the user-mode-only version of sparc_cpu_handle_mmu_fault(), we must save the fault address for a data fault into the CPU state's mmu registers, because the code in linux-user/main.c expects to find it there in order to populate the si_addr field of the guest siginfo.

Quoted 28 lines
---
 target/sparc/mmu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/sparc/mmu_helper.c +8 -0
index 126ea5e..d5b6c1e 100644
@@ -30,10 +30,18 @@
 int sparc_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
                                int mmu_idx)
 {
+    SPARCCPU *cpu = SPARC_CPU(cs);
+    CPUSPARCState *env = &cpu->env;
+
     if (rw & 2) {
         cs->exception_index = TT_TFAULT;
     } else {
         cs->exception_index = TT_DFAULT;
+#ifdef TARGET_SPARC64
+        env->dmmu.mmuregs[4] = address;
+#else
+        env->mmuregs[4] = address;
+#endif
     }
     return 1;
 }
Reviewed-by on v1 3/4
#14 lore raw

Re: [Qemu-devel] [PATCH 3/4] linux-user/sparc: Put address for data faults where linux-user expects it

In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

In the user-mode-only version of sparc_cpu_handle_mmu_fault(), we must save the fault address for a data fault into the CPU state's mmu registers, because the code in linux-user/main.c expects to find it there in order to populate the si_addr field of the guest siginfo.

---
 target/sparc/mmu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)

r~

#15 lore raw

PATCH 4/4 linux-user: Handle rt_sigaction correctly for SPARC

In reply to Peter Maydell

SPARC is like Alpha in its handling of the rt_sigaction syscall:
it takes an extra parameter 'restorer' which needs to be copied into the sa_restorer field of the sigaction struct. The order of the arguments differs slightly between SPARC and Alpha but the implementation is otherwise the same. (Compare the rt_sigaction() functions in arch/sparc/kernel/sys_sparc_64.c and arch/alpha/kernel/signal.c.)

Note that this change is somewhat moot until SPARC acquires support for actually delivering RT signals.

---
 linux-user/syscall.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
linux-user/syscall.c +23 -4
index d4497de..8beab51 100644
@@ -8556,8 +8556,16 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_rt_sigaction:
         {
 #if defined(TARGET_ALPHA)
-            struct target_sigaction act, oact, *pact = 0;
+            /* For Alpha and SPARC this is a 5 argument syscall, with
+             * a 'restorer' parameter which must be copied into the
+             * sa_restorer field of the sigaction struct.
+             * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
+             * and arg5 is the sigsetsize.
+             * Alpha also has a separate rt_sigaction struct that it uses
+             * here; SPARC uses the usual sigaction struct.
+             */
             struct target_rt_sigaction *rt_act;
+            struct target_sigaction act, oact, *pact = 0;
 
             if (arg4 != sizeof(target_sigset_t)) {
                 ret = -TARGET_EINVAL;
@@ -8583,18 +8591,29 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 unlock_user_struct(rt_act, arg3, 1);
             }
 #else
+#ifdef TARGET_SPARC
+            target_ulong restorer = arg4;
+            target_ulong sigsetsize = arg5;
+#else
+            target_ulong sigsetsize = arg4;
+#endif
             struct target_sigaction *act;
             struct target_sigaction *oact;
 
-            if (arg4 != sizeof(target_sigset_t)) {
+            if (sigsetsize != sizeof(target_sigset_t)) {
                 ret = -TARGET_EINVAL;
                 break;
             }
             if (arg2) {
-                if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
+                if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) {
                     goto efault;
-            } else
+                }
+#ifdef TARGET_SPARC
+                act->sa_restorer = restorer;
+#endif
+            } else {
                 act = NULL;
+            }
             if (arg3) {
                 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
                     ret = -TARGET_EFAULT;
Signature · 1 line
2.7.4
Reviewed-by on v1 4/4
#16 lore raw

Re: [Qemu-devel] [PATCH 4/4] linux-user: Handle rt_sigaction correctly for SPARC

Le 06/11/2017 à 19:33, Peter Maydell a écrit :

Quoted 62 lines

SPARC is like Alpha in its handling of the rt_sigaction syscall:
it takes an extra parameter 'restorer' which needs to be copied into the sa_restorer field of the sigaction struct. The order of the arguments differs slightly between SPARC and Alpha but the implementation is otherwise the same. (Compare the rt_sigaction() functions in arch/sparc/kernel/sys_sparc_64.c and arch/alpha/kernel/signal.c.)

Note that this change is somewhat moot until SPARC acquires support for actually delivering RT signals.

---
 linux-user/syscall.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
linux-user/syscall.c +17 -4
index d4497de..8beab51 100644
@@ -8556,8 +8556,16 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_rt_sigaction:
         {
 #if defined(TARGET_ALPHA)
-            struct target_sigaction act, oact, *pact = 0;
+            /* For Alpha and SPARC this is a 5 argument syscall, with
+             * a 'restorer' parameter which must be copied into the
+             * sa_restorer field of the sigaction struct.
+             * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
+             * and arg5 is the sigsetsize.
+             * Alpha also has a separate rt_sigaction struct that it uses
+             * here; SPARC uses the usual sigaction struct.
+             */
             struct target_rt_sigaction *rt_act;
+            struct target_sigaction act, oact, *pact = 0;
 
             if (arg4 != sizeof(target_sigset_t)) {
                 ret = -TARGET_EINVAL;
@@ -8583,18 +8591,29 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 unlock_user_struct(rt_act, arg3, 1);
             }
 #else
+#ifdef TARGET_SPARC
+            target_ulong restorer = arg4;
+            target_ulong sigsetsize = arg5;
+#else
+            target_ulong sigsetsize = arg4;
+#endif
             struct target_sigaction *act;
             struct target_sigaction *oact;
 
-            if (arg4 != sizeof(target_sigset_t)) {
+            if (sigsetsize != sizeof(target_sigset_t)) {
                 ret = -TARGET_EINVAL;
                 break;
             }
             if (arg2) {
-                if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
+                if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) {
                     goto efault;
-            } else

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 8619

Quoted 10 lines
linux-user/syscall.c +6 -0
+                }
+#ifdef TARGET_SPARC
+                act->sa_restorer = restorer;
+#endif
+            } else {
                 act = NULL;
+            }
             if (arg3) {
                 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
                     ret = -TARGET_EFAULT;
Reviewed-by on v1 4/4
#17 lore raw

Re: [Qemu-devel] [PATCH 4/4] linux-user: Handle rt_sigaction correctly for SPARC

In reply to Peter Maydell

Hi Peter,

Quoted 23 lines · Peter Maydell

SPARC is like Alpha in its handling of the rt_sigaction syscall:
it takes an extra parameter 'restorer' which needs to be copied into the sa_restorer field of the sigaction struct. The order of the arguments differs slightly between SPARC and Alpha but the implementation is otherwise the same. (Compare the rt_sigaction() functions in arch/sparc/kernel/sys_sparc_64.c and arch/alpha/kernel/signal.c.)

Note that this change is somewhat moot until SPARC acquires support for actually delivering RT signals.

---
 linux-user/syscall.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
linux-user/syscall.c +0 -0
index d4497de..8beab51 100644
@@ -8556,8 +8556,16 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_rt_sigaction:
         {

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 8566

Quoted 10 lines
linux-user/syscall.c +8 -1
 
-            struct target_sigaction act, oact, *pact = 0;
+            /* For Alpha and SPARC this is a 5 argument syscall, with
+             * a 'restorer' parameter which must be copied into the
+             * sa_restorer field of the sigaction struct.
+             * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
+             * and arg5 is the sigsetsize.
+             * Alpha also has a separate rt_sigaction struct that it uses
+             * here; SPARC uses the usual sigaction struct.
+             */

I find it easier to read moving the #if after the comment. Since using restorer for sparc improve readability we can use it for alpha as well (code is a bit more consistent):

  #if defined(TARGET_ALPHA)
               target_ulong restorer = arg5;
Quoted 5 lines
linux-user/syscall.c +1 -0
             struct target_rt_sigaction *rt_act;
+            struct target_sigaction act, oact, *pact = 0;
 
             if (arg4 != sizeof(target_sigset_t)) {
                 ret = -TARGET_EINVAL;

...

                   act.sa_restorer = restorer;

Whichever way:

Quoted 34 lines
linux-user/syscall.c +14 -3
@@ -8583,18 +8591,29 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 unlock_user_struct(rt_act, arg3, 1);
             }
 #else
+#ifdef TARGET_SPARC
+            target_ulong restorer = arg4;
+            target_ulong sigsetsize = arg5;
+#else
+            target_ulong sigsetsize = arg4;
+#endif
             struct target_sigaction *act;
             struct target_sigaction *oact;
 
-            if (arg4 != sizeof(target_sigset_t)) {
+            if (sigsetsize != sizeof(target_sigset_t)) {
                 ret = -TARGET_EINVAL;
                 break;
             }
             if (arg2) {
-                if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
+                if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) {
                     goto efault;
-            } else
+                }
+#ifdef TARGET_SPARC
+                act->sa_restorer = restorer;
+#endif
+            } else {
                 act = NULL;
+            }
             if (arg3) {
                 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
                     ret = -TARGET_EFAULT;
#18 lore raw
In reply to Peter Maydell
Quoted 15 lines · Peter Maydell

This patchset fixes most of the problems with delivering SIGSEGV noted in https://bugs.launchpad.net/qemu/+bug/1705118:
* s390x is missing masking operations on the fault address passed to the guest in si_addr
* ppc is passing the PC of the offending insn, not the data address, into si_addr
* sparc fails to record the address of a data fault and so passes 0 into si_addr
* sparc isn't implementing rt_sigaction correctly (it has an extra 'restorer' argument, similar to alpha)

It doesn't actually get the sparc testcase from that bug working, though, because SPARC is entirely missing support for setup_rt_frame() and do_rt_sigreturn(), so it can't deliver RT signals.

Quoted 6 lines

Implementing RT signal delivery is too big a bugfix for an afternoon, so here are the simpler parts. I'll leave the signal delivery part to somebody who cares about SPARC guests...

thanks
-- PMM

Thanks!

series applied to linux-user tree,

Riku

Quoted 15 lines

Peter Maydell (4):
linux-user/s390x: Mask si_addr for SIGSEGV
linux-user/ppc: Report correct fault address for data faults linux-user/sparc: Put address for data faults where linux-user expects
it
linux-user: Handle rt_sigaction correctly for SPARC

 linux-user/main.c         |  8 ++++++--
 linux-user/syscall.c      | 27 +++++++++++++++++++++++----
 target/sparc/mmu_helper.c |  8 ++++++++
 3 files changed, 37 insertions(+), 6 deletions(-)
Signature · 1 line
2.7.4