Skip to contentSkip to conversation

linux-user: Enable sigaltstack syscall for sh4

PATCH New John Paul Adrian Glaubitz 3 participants 9 messages Nov 18, 2015 – Nov 19, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Enable sigaltstack syscall for sh4

Hello!

I'm the current maintainer for the Debian sh4 port. I maintain several buildds as well as port packages for the sh4 architecture.

Recently I discovered qemu for building packages in an emulated chroot environment with qemu-sh4-static. While setting up python, qemu issued an error message about an unimplemented syscall, 186 which is sigaltstack.

Looking up the sources, I found that linux-user/syscall.c enables sigaltstack for a limited number of architectures only. In order to fix the problem with the missing sigaltstack syscall, I added TARGET_SH4 to the list of architectures which seems to work.

The following sample C program tests the functionality of sigaltstack. Without my patch, the program segfaults on qemu-sh4. With the patch, it works as expected.

=====================================================================

#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

jmp_buf exit_jmp;

void handler(int x)
{
longjmp(exit_jmp, 1);
}

int f(void)
{
return f();
}

int main(void)
{
stack_t sigstack;
sigstack.ss_sp = malloc(1024*1024);
sigstack.ss_size = 1024*1024;
sigstack.ss_flags = 0;
sigaltstack(&sigstack, NULL);
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK;
sigaction(SIGSEGV, &sa, NULL);
if (setjmp(exit_jmp) == 0)

  {
    return f();

}
puts("recovered");
return 0;
}

=====================================================================

Any idea what else I should test to verify my changes? Otherwise, it would be great if my patch could be applied to add sigaltstack for sh4.

Thanks,

Adrian

Quoted 1 line

[1] https://wiki.debian.org/SH4/sbuildQEMU

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
#1 lore raw

PATCH 1/1 linux-user: Enable sigaltstack syscall for sh4

---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +2 -1
index 6c64ba6..4d864b2 100644
@@ -8294,7 +8294,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_sigaltstack:
 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
     defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
-    defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
+    defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC) || \
+    defined(TARGET_SH4)
         ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
         break;
 #else
Signature · 1 line
2.6.2
#2 lore raw
Quoted 9 lines · John Paul Adrian Glaubitz
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 6c64ba6..4d864b2 100644

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

Quoted 9 lines
linux-user/syscall.c +2 -1
@@ -8294,7 +8294,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_sigaltstack:
 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
     defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
-    defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
+    defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC) || \
+    defined(TARGET_SH4)
         ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
         break;
 #else

Unfortunately this isn't sufficient. You also need to add the code to the sh4-specific functions in linux-user/signal.c which honours the requested sigaltstack when taking and returning from signal handlers.

thanks
-- PMM

#3 lore raw
Quoted 4 lines · Peter Maydell

Unfortunately this isn't sufficient. You also need to add the code to the sh4-specific functions in linux-user/signal.c which honours the requested sigaltstack when taking and returning from signal handlers.

My supplied test case shows that sigaltstack works unless I am overseeing anything? Laurent Vivier (CC'ed) who has done some extensive qemu development thinks that my change should be enough.

Here's the output of my test case (CC'ing Michael Karcher who suggested the test case):

(sid-sh4-sbuild)root@jessie32:/tmp# cat stackoverflow.c

#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

jmp_buf exit_jmp;

void handler(int x)
{
longjmp(exit_jmp, 1);
}

int f(void)
{
return f();
}

int main(void)
{
stack_t sigstack;
sigstack.ss_sp = malloc(1024*1024);
sigstack.ss_size = 1024*1024;
sigstack.ss_flags = 0;
sigaltstack(&sigstack, NULL);
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK;
sigaction(SIGSEGV, &sa, NULL);
if (setjmp(exit_jmp) == 0)

    {
      return f();
    }
  puts("recovered");
  return 0;
}
(sid-sh4-sbuild)root@jessie32:/tmp# gcc stackoverflow.c -o stackoverflow
(sid-sh4-sbuild)root@jessie32:/tmp# ./stackoverflow
recovered
(sid-sh4-sbuild)root@jessie32:/tmp#

Now commenting "sigaltstack" out:

(sid-sh4-sbuild)root@jessie32:/tmp# cat stackoverflow.c

#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

jmp_buf exit_jmp;

void handler(int x)
{
longjmp(exit_jmp, 1);
}

int f(void)
{
return f();
}

int main(void)
{
stack_t sigstack;
sigstack.ss_sp = malloc(1024*1024);
sigstack.ss_size = 1024*1024;
sigstack.ss_flags = 0;
// sigaltstack(&sigstack, NULL);
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK;
sigaction(SIGSEGV, &sa, NULL);
if (setjmp(exit_jmp) == 0)

    {
      return f();
    }
  puts("recovered");
  return 0;
}
(sid-sh4-sbuild)root@jessie32:/tmp# gcc stackoverflow.c -o stackoverflow
(sid-sh4-sbuild)root@jessie32:/tmp# ./stackoverflow
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault
(sid-sh4-sbuild)root@jessie32:/tmp#

Thus, for me it seems sigaltstack behaves as expected with the patch applied.

Am I missing something obvious?

Cheers,
Adrian

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
#4 lore raw

Hi,

Quoted 5 lines · John Paul Adrian Glaubitz
Quoted 4 lines · Peter Maydell

Unfortunately this isn't sufficient. You also need to add the code to the sh4-specific functions in linux-user/signal.c which honours the requested sigaltstack when taking and returning from signal handlers.

it seems all needed functions for sh4 signal handling are already written in linux-user/signal.c, I thing about setup_frame(), setup_rt_frame(), do_sigreturn() and do_rt_sigreturn().

Do we need more ?

Quoted 104 lines

My supplied test case shows that sigaltstack works unless I am overseeing anything? Laurent Vivier (CC'ed) who has done some extensive qemu development thinks that my change should be enough.

Here's the output of my test case (CC'ing Michael Karcher who suggested the test case):

(sid-sh4-sbuild)root@jessie32:/tmp# cat stackoverflow.c

#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

jmp_buf exit_jmp;

void handler(int x)
{
longjmp(exit_jmp, 1);
}

int f(void)
{
return f();
}

int main(void)
{
stack_t sigstack;
sigstack.ss_sp = malloc(1024*1024);
sigstack.ss_size = 1024*1024;
sigstack.ss_flags = 0;
sigaltstack(&sigstack, NULL);
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK;
sigaction(SIGSEGV, &sa, NULL);
if (setjmp(exit_jmp) == 0)

    {
      return f();
    }
  puts("recovered");
  return 0;
}
(sid-sh4-sbuild)root@jessie32:/tmp# gcc stackoverflow.c -o stackoverflow
(sid-sh4-sbuild)root@jessie32:/tmp# ./stackoverflow
recovered
(sid-sh4-sbuild)root@jessie32:/tmp#

Now commenting "sigaltstack" out:

(sid-sh4-sbuild)root@jessie32:/tmp# cat stackoverflow.c

#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

jmp_buf exit_jmp;

void handler(int x)
{
longjmp(exit_jmp, 1);
}

int f(void)
{
return f();
}

int main(void)
{
stack_t sigstack;
sigstack.ss_sp = malloc(1024*1024);
sigstack.ss_size = 1024*1024;
sigstack.ss_flags = 0;
// sigaltstack(&sigstack, NULL);
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK;
sigaction(SIGSEGV, &sa, NULL);
if (setjmp(exit_jmp) == 0)

    {
      return f();
    }
  puts("recovered");
  return 0;
}
(sid-sh4-sbuild)root@jessie32:/tmp# gcc stackoverflow.c -o stackoverflow
(sid-sh4-sbuild)root@jessie32:/tmp# ./stackoverflow
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault
(sid-sh4-sbuild)root@jessie32:/tmp#

Thus, for me it seems sigaltstack behaves as expected with the patch applied.

Am I missing something obvious?

Cheers,
Adrian

#5 lore raw
Quoted 4 lines · Laurent Vivier

Hi,

Quoted 1 line · John Paul Adrian Glaubitz

On 11/19/2015 10:17 AM, Peter Maydell wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Unfortunately this isn't sufficient. You also need to add the code to the sh4-specific functions in linux-user/signal.c which honours the requested sigaltstack when taking and returning from signal handlers.

it seems all needed functions for sh4 signal handling are already written in linux-user/signal.c, I thing about setup_frame(), setup_rt_frame(), do_sigreturn() and do_rt_sigreturn().

Other architectures have calls to do_sigaltstack or other sigaltstack handling in their frame setup and sigreturn functions. SH4 doesn't, which implied to me that we were missing something beyond just enabling the call to do_sigaltstack. Maybe that's for something else?

(Incidentally the only targets other than SH4 not listed in this ifdef are CRIS, Microblaze, TileGX and Unicore32. Maybe we should just go ahead and remove the ifdef entirely, if do_sigaltstack() doesn't really have any target-specific requirements?)

thanks
-- PMM

#6 lore raw
Quoted 14 lines · Peter Maydell
Quoted 4 lines · Laurent Vivier

Hi,

Quoted 1 line · John Paul Adrian Glaubitz

On 11/19/2015 10:17 AM, Peter Maydell wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Unfortunately this isn't sufficient. You also need to add the code to the sh4-specific functions in linux-user/signal.c which honours the requested sigaltstack when taking and returning from signal handlers.

it seems all needed functions for sh4 signal handling are already written in linux-user/signal.c, I thing about setup_frame(), setup_rt_frame(), do_sigreturn() and do_rt_sigreturn().

Quoted 5 lines

Other architectures have calls to do_sigaltstack or other sigaltstack handling in their frame setup and sigreturn functions. SH4 doesn't, which implied to me that we were missing something beyond just enabling the call to do_sigaltstack. Maybe that's for something else?

Well, I didn't really check the code in these functions, but SH4 has the call in do_rt_sigreturn().

The only check I did is to compare setup_frame() with the one from kernel and it seems ok.

But I think the patch from Adrian is just fixing a forgetting in the original SH4 patch:

c3b5bc8 SH4: Signal handling for the user space emulator, by Magnus Damm.

This patch uses do_sigaltstack() without enabling sigaltstack() syscall.

Quoted 5 lines

(Incidentally the only targets other than SH4 not listed in this ifdef are CRIS, Microblaze, TileGX and Unicore32. Maybe we should just go ahead and remove the ifdef entirely, if do_sigaltstack() doesn't really have any target-specific requirements?)

I don't know if these targets have a lot of users. At least for SH4, we can enable it and see if someone is screaming (I like the "crash-test method" :) ). I think Adrian will take care of this (and I can help).

Thanks,
Laurent

#7 lore raw
Quoted 3 lines · Laurent Vivier

I don't know if these targets have a lot of users. At least for SH4, we can enable it and see if someone is screaming (I like the "crash-test method" :) ). I think Adrian will take care of this (and I can help).

Exactly :).

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
#8 lore raw
In reply to Laurent Vivier
Quoted 10 lines · Laurent Vivier
Quoted 5 lines · Peter Maydell

(Incidentally the only targets other than SH4 not listed in this ifdef are CRIS, Microblaze, TileGX and Unicore32. Maybe we should just go ahead and remove the ifdef entirely, if do_sigaltstack() doesn't really have any target-specific requirements?)

I don't know if these targets have a lot of users. At least for SH4, we can enable it and see if someone is screaming (I like the "crash-test method" :) ). I think Adrian will take care of this (and I can help).

TileGX is new and not fully implemented yet anyway; Unicore32 is pretty moribund. I don't think CRIS and MicroBlaze have a lot of use of the linux-user target; cc'ing Edgar as maintainer.

So I would say dropping the ifdef is safe, but probably best left til 2.6.

thanks
-- PMM