about summary refs log tree commit diff
path: root/sysdeps/mach/hurd/i386
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>1995-02-18 01:27:10 +0000
committerRoland McGrath <roland@gnu.org>1995-02-18 01:27:10 +0000
commit28f540f45bbacd939bfd07f213bcad2bf730b1bf (patch)
tree15f07c4c43d635959c6afee96bde71fb1b3614ee /sysdeps/mach/hurd/i386
downloadglibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.tar.gz
glibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.tar.xz
glibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.zip
initial import
Diffstat (limited to 'sysdeps/mach/hurd/i386')
-rw-r--r--sysdeps/mach/hurd/i386/exc2signal.c165
-rw-r--r--sysdeps/mach/hurd/i386/init-fault.c41
-rw-r--r--sysdeps/mach/hurd/i386/longjmp-ctx.c35
-rw-r--r--sysdeps/mach/hurd/i386/longjmp-ts.c39
-rw-r--r--sysdeps/mach/hurd/i386/sigcontext.h106
-rw-r--r--sysdeps/mach/hurd/i386/sigreturn.c126
-rw-r--r--sysdeps/mach/hurd/i386/trampoline.c271
7 files changed, 783 insertions, 0 deletions
diff --git a/sysdeps/mach/hurd/i386/exc2signal.c b/sysdeps/mach/hurd/i386/exc2signal.c
new file mode 100644
index 0000000000..19f845a49e
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/exc2signal.c
@@ -0,0 +1,165 @@
+/* Translate Mach exception codes into signal numbers.  i386 version.
+Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#include <hurd.h>
+#include <hurd/signal.h>
+#include <mach/exception.h>
+
+/* Translate the Mach exception codes, as received in an `exception_raise' RPC,
+   into a signal number and signal subcode.  */
+
+void
+_hurd_exception2signal (int exception, int code, int subcode,
+			int *signo, long int *sigcode, int *error)
+{
+  *error = 0;
+
+  switch (exception)
+    {
+    default:
+      *signo = SIGIOT;
+      *sigcode = exception;
+      break;
+      
+    case EXC_BAD_ACCESS:
+      if (code == KERN_PROTECTION_FAILURE)
+	*signo = SIGSEGV;
+      else
+	*signo = SIGBUS;
+      *sigcode = subcode;
+      *error = code;
+      break;
+
+    case EXC_BAD_INSTRUCTION:
+      *signo = SIGILL;
+      if (code == EXC_I386_INVOP)
+	*sigcode = ILL_INVOPR_FAULT;
+      else if (code == EXC_I386_STKFLT)
+	*sigcode = ILL_STACK_FAULT;
+      else
+	*sigcode = 0;
+      break;
+      
+    case EXC_ARITHMETIC:
+      switch (code)
+	{
+	case EXC_I386_DIV:	/* integer divide by zero */
+	  *signo = SIGFPE;
+	  *sigcode = FPE_INTDIV_FAULT;
+	  break;
+	  
+	case EXC_I386_INTO:	/* integer overflow */
+	  *signo = SIGFPE;
+	  *sigcode = FPE_INTOVF_TRAP;
+	  break;
+
+	  /* These aren't anywhere documented or used in Mach 3.0.  */
+	case EXC_I386_NOEXT:
+	case EXC_I386_EXTOVR:
+	default:
+	  *signo = SIGFPE;
+	  *sigcode = 0;
+	  break;
+
+	case EXC_I386_EXTERR:
+	  /* Subcode is the fp_status word saved by the hardware.
+	     Give an error code corresponding to the first bit set.  */
+	  if (subcode & FPS_IE)
+	    {
+	      *signo = SIGILL;
+	      *sigcode = ILL_FPEOPR_FAULT;
+	    }
+	  else if (subcode & FPS_DE)
+	    {
+	      *signo = SIGFPE;
+	      *sigcode = FPE_FLTDNR_FAULT;
+	    }
+	  else if (subcode & FPS_ZE)
+	    {
+	      *signo = SIGFPE;
+	      *sigcode = FPE_FLTDIV_FAULT;
+	    }
+	  else if (subcode & FPS_OE)
+	    {
+	      *signo = SIGFPE;
+	      *sigcode = FPE_FLTOVF_FAULT;
+	    }
+	  else if (subcode & FPS_UE)
+	    {
+	      *signo = SIGFPE;
+	      *sigcode = FPE_FLTUND_FAULT;
+	    }
+	  else if (subcode & FPS_PE)
+	    {
+	      *signo = SIGFPE;
+	      *sigcode = FPE_FLTINX_FAULT;
+	    }
+	  else
+	    {
+	      *signo = SIGFPE;
+	      *sigcode = 0;
+	    }
+	  break;
+
+	  /* These two can only be arithmetic exceptions if we 
+	     are in V86 mode, which sounds like emulation to me.
+	     (See Mach 3.0 i386/trap.c.)  */
+	case EXC_I386_EMERR:
+	  *signo = SIGFPE;
+	  *sigcode = FPE_EMERR_FAULT;
+	  break;
+	case EXC_I386_BOUND:
+	  *signo = SIGFPE;
+	  *sigcode = FPE_EMBND_FAULT;
+	  break;
+	}
+      break;
+
+    case EXC_EMULATION:		
+      /* 3.0 doesn't give this one, why, I don't know.  */
+      *signo = SIGEMT;
+      *sigcode = 0;
+      break;
+
+    case EXC_SOFTWARE:
+      /* The only time we get this in Mach 3.0
+	 is for an out of bounds trap.  */
+      if (code == EXC_I386_BOUND)
+	{
+	  *signo = SIGFPE;
+	  *sigcode = FPE_SUBRNG_FAULT;
+	}
+      else
+	{
+	  *signo = SIGEMT;
+	  *sigcode = 0;
+	}
+      break;
+      
+    case EXC_BREAKPOINT:
+      *signo = SIGTRAP;
+      if (code == EXC_I386_SGL)
+	*sigcode = DBG_SINGLE_TRAP;
+      else if (code == EXC_I386_BPT)
+	*sigcode = DBG_BRKPNT_FAULT;
+      else
+	*sigcode = 0;
+      break;
+    }
+}
diff --git a/sysdeps/mach/hurd/i386/init-fault.c b/sysdeps/mach/hurd/i386/init-fault.c
new file mode 100644
index 0000000000..ff22814308
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/init-fault.c
@@ -0,0 +1,41 @@
+/* Set up a thread_state for proc_handle_exceptions.  i386 version.
+Copyright (C) 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#include <hurd/signal.h>
+#include <mach/thread_status.h>
+#include <string.h>
+#include <setjmp.h>
+
+extern jmp_buf _hurd_sigthread_fault_env;
+
+static char fault_stack[32];
+static volatile void
+faulted (void)
+{
+  __longjmp (_hurd_sigthread_fault_env, 1);
+}
+
+void
+_hurd_initialize_fault_recovery_state (void *state)
+{
+  struct i386_thread_state *ts = state;
+  memset (ts, 0, sizeof (*ts));
+  ts->uesp = (int) &fault_stack[sizeof (fault_stack)];
+  ts->eip = (int) &faulted;
+}
diff --git a/sysdeps/mach/hurd/i386/longjmp-ctx.c b/sysdeps/mach/hurd/i386/longjmp-ctx.c
new file mode 100644
index 0000000000..acfbee602e
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/longjmp-ctx.c
@@ -0,0 +1,35 @@
+/* Perform a `longjmp' on a `struct sigcontext'.  i386 version.
+Copyright (C) 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#include <setjmp.h>
+#include <hurd/signal.h>
+#include <string.h>
+
+void
+_hurd_longjmp_sigcontext (struct sigcontext *scp, jmp_buf env, int retval)
+{
+  memset (scp, 0, sizeof (*scp));
+  scp->sc_ebx = env[0].__bx;
+  scp->sc_esi = env[0].__si;
+  scp->sc_edi = env[0].__di;
+  scp->sc_ebp = (int) env[0].__bp;
+  scp->sc_uesp = (int) env[0].__sp;
+  scp->sc_eip = (int) env[0].__pc;
+  scp->sc_eax = retval == 0 ? 1 : retval;
+}
diff --git a/sysdeps/mach/hurd/i386/longjmp-ts.c b/sysdeps/mach/hurd/i386/longjmp-ts.c
new file mode 100644
index 0000000000..7da9be2a26
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/longjmp-ts.c
@@ -0,0 +1,39 @@
+/* Perform a `longjmp' on a Mach thread_state.  i386 version.
+Copyright (C) 1991, 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#include <hurd/signal.h>
+#include <setjmp.h>
+#include <mach/thread_status.h>
+
+
+/* Set up STATE to do the equivalent of `longjmp (ENV, VAL);'.  */
+
+void
+_hurd_longjmp_thread_state (void *state, jmp_buf env, int val)
+{
+  struct i386_thread_state *ts = state;
+
+  ts->ebx = env[0].__jmpbuf[0].__bx;
+  ts->esi = env[0].__jmpbuf[0].__si;
+  ts->edi = env[0].__jmpbuf[0].__di;
+  ts->ebp = (int) env[0].__jmpbuf[0].__bp;
+  ts->uesp = (int) env[0].__jmpbuf[0].__sp;
+  ts->eip = (int) env[0].__jmpbuf[0].__pc;
+  ts->eax = val ?: 1;
+}
diff --git a/sysdeps/mach/hurd/i386/sigcontext.h b/sysdeps/mach/hurd/i386/sigcontext.h
new file mode 100644
index 0000000000..b742326bdb
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/sigcontext.h
@@ -0,0 +1,106 @@
+/* Machine-dependent signal context structure for GNU Hurd.  i386 version.
+Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+/* Signal handlers are actually called:
+   void handler (int sig, int code, struct sigcontext *scp);  */
+
+#include <mach/machine/fp_reg.h>
+
+/* State of this thread when the signal was taken.  */
+struct sigcontext
+  {
+    /* These first members are machine-independent.  */
+
+    int sc_onstack;		/* Nonzero if running on sigstack.  */
+    __sigset_t sc_mask;		/* Blocked signals to restore.  */
+
+    /* MiG reply port this thread is using.  */
+    unsigned int sc_reply_port;
+
+    /* Port this thread is doing an interruptible RPC on.  */
+    unsigned int sc_intr_port;
+
+    /* Error code associated with this signal (interpreted as `error_t').  */
+    int sc_error;
+
+    /* All following members are machine-dependent.  The rest of this
+       structure is written to be laid out identically to:
+       {
+         struct i386_thread_state basic;
+         struct i386_float_state fpu;
+       }
+       trampoline.c knows this, so it must be changed if this changes.  */
+
+#define sc_i386_thread_state sc_gs /* Beginning of correspondence.  */
+    /* Segment registers.  */
+    int sc_gs;
+    int sc_fs;
+    int sc_es;
+    int sc_ds;
+  
+    /* "General" registers.  These members are in the order that the i386
+       `pusha' and `popa' instructions use (`popa' ignores %esp).  */
+    int sc_edi;
+    int sc_esi;
+    int sc_ebp;
+    int sc_esp;			/* Not used; sc_uesp is used instead.  */
+    int sc_ebx;
+    int sc_edx;
+    int sc_ecx;
+    int sc_eax;
+  
+    int sc_eip;			/* Instruction pointer.  */
+    int sc_cs;			/* Code segment register.  */
+  
+    int sc_efl;			/* Processor flags.  */
+
+    int sc_uesp;		/* This stack pointer is used.  */
+    int sc_ss;			/* Stack segment register.  */
+
+    /* Following mimics struct i386_float_state.  Structures and symbolic
+       values can be found in <mach/i386/fp_reg.h>.  */
+#define sc_i386_float_state sc_fpkind
+    int sc_fpkind;		/* FP_NO, FP_387, etc.  */
+    int sc_fpused;		/* If zero, ignore rest of float state.  */
+    struct i386_fp_save sc_fpsave;
+    struct i386_fp_regs sc_fpregs;
+    int sc_fpexcsr;		/* FPSR including exception bits.  */
+  };
+
+
+/* Codes for SIGFPE.  */
+#define FPE_INTOVF_TRAP		0x1 /* integer overflow */
+#define FPE_INTDIV_FAULT	0x2 /* integer divide by zero */
+#define FPE_FLTOVF_FAULT	0x3 /* floating overflow */
+#define FPE_FLTDIV_FAULT	0x4 /* floating divide by zero */
+#define FPE_FLTUND_FAULT	0x5 /* floating underflow */
+#define FPE_SUBRNG_FAULT	0x7 /* BOUNDS instruction failed */
+#define FPE_FLTDNR_FAULT	0x8 /* denormalized operand */
+#define FPE_FLTINX_FAULT	0x9 /* floating loss of precision */
+#define FPE_EMERR_FAULT		0xa /* mysterious emulation error 33 */
+#define FPE_EMBND_FAULT		0xb /* emulation BOUNDS instruction failed */
+
+/* Codes for SIGILL.  */
+#define ILL_INVOPR_FAULT	0x1 /* invalid operation */
+#define ILL_STACK_FAULT		0x2 /* fault on microkernel stack access */
+#define ILL_FPEOPR_FAULT	0x3 /* invalid floating operation */
+
+/* Codes for SIGTRAP.  */
+#define DBG_SINGLE_TRAP		0x1 /* single step */
+#define DBG_BRKPNT_FAULT	0x2 /* breakpoint instruction */
diff --git a/sysdeps/mach/hurd/i386/sigreturn.c b/sysdeps/mach/hurd/i386/sigreturn.c
new file mode 100644
index 0000000000..df8960669f
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/sigreturn.c
@@ -0,0 +1,126 @@
+/* Copyright (C) 1991, 1992, 1994, 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+register int *sp asm ("%esp");
+
+#include <hurd.h>
+#include <hurd/signal.h>
+#include <hurd/threadvar.h>
+#include <hurd/msg.h>
+#include <stdlib.h>
+
+int
+__sigreturn (struct sigcontext *scp)
+{
+  struct hurd_sigstate *ss;
+  mach_port_t *reply_port;
+
+  if (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK))
+    {
+      errno = EINVAL;
+      return -1;
+    }
+
+  ss = _hurd_self_sigstate ();
+  __spin_lock (&ss->lock);
+
+  /* Restore the set of blocked signals, and the intr_port slot.  */
+  ss->blocked = scp->sc_mask;
+  ss->intr_port = scp->sc_intr_port;
+
+  /* Check for pending signals that were blocked by the old set.  */
+  if (ss->pending & ~ss->blocked)
+    {
+      /* There are pending signals that just became unblocked.  Wake up the
+	 signal thread to deliver them.  But first, squirrel away SCP where
+	 the signal thread will notice it if it runs another handler, and
+	 arrange to have us called over again in the new reality.  */
+      ss->context = scp;
+      /* Clear the intr_port slot, since we are not in fact doing
+	 an interruptible RPC right now.  If SS->intr_port is not null,
+	 the SCP context is doing an interruptible RPC, but the signal
+	 thread will examine us while we are blocked in the sig_post RPC.  */
+      ss->intr_port = MACH_PORT_NULL;
+      __spin_unlock (&ss->lock);
+      __msg_sig_post (_hurd_msgport, 0, __mach_task_self ());
+      /* If a pending signal was handled, sig_post never returned.  */
+      __spin_lock (&ss->lock);
+    }
+
+  if (scp->sc_onstack)
+    {
+      ss->sigaltstack.ss_flags &= ~SA_ONSTACK; /* XXX threadvars */
+      /* XXX cannot unlock until off sigstack */
+      abort ();
+    }
+  else
+    __spin_unlock (&ss->lock);
+
+  /* Destroy the MiG reply port used by the signal handler, and restore the
+     reply port in use by the thread when interrupted.  */
+  reply_port =
+    (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
+  if (*reply_port)
+    __mach_port_destroy (__mach_task_self (), *reply_port);
+  *reply_port = scp->sc_reply_port;
+
+  if (scp->sc_fpused)
+    {
+  /* XXX should restore FPU state here XXX roland needs 387 manual */
+  /*    abort (); */
+    }
+
+  {
+    /* There are convenient instructions to pop state off the stack, so we
+       copy the registers onto the user's stack, switch there, pop and
+       return.  */
+
+    int *usp = (int *) scp->sc_uesp;
+
+    *--usp = scp->sc_eip;
+    *--usp = scp->sc_efl;
+    memcpy (usp -= 12, &scp->sc_i386_thread_state, 12 * sizeof (int));
+
+    sp = usp;
+
+#define A(line) asm volatile (#line)
+    /* The members in the sigcontext are arranged in this order
+       so we can pop them easily.  */
+
+    /* Pop the segment registers (except %cs and %ss, done last).  */
+    A (popl %gs);
+    A (popl %fs);
+    A (popl %es);
+    A (popl %ds);
+    /* Pop the general registers.  */
+    A (popa);
+    /* Pop the processor flags.  */
+    A (popf);
+    /* Return to the saved PC.  */
+    A (ret);
+
+    /* Firewall.  */
+    A (hlt);
+#undef A
+  }
+
+  /* NOTREACHED */
+  return -1;
+}
+
+weak_alias (__sigreturn, sigreturn)
diff --git a/sysdeps/mach/hurd/i386/trampoline.c b/sysdeps/mach/hurd/i386/trampoline.c
new file mode 100644
index 0000000000..eabf940b44
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/trampoline.c
@@ -0,0 +1,271 @@
+/* Set thread_state for sighandler, and sigcontext to recover.  i386 version.
+Copyright (C) 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#include <hurd/signal.h>
+#include "thread_state.h"
+#include <assert.h>
+#include <errno.h>
+#include "hurdfault.h"
+
+     
+struct mach_msg_trap_args
+  {
+    void *retaddr;		/* Address mach_msg_trap will return to.  */
+    /* This is the order of arguments to mach_msg_trap.  */
+    mach_msg_header_t *msg;
+    mach_msg_option_t option;
+    mach_msg_size_t send_size;
+    mach_msg_size_t rcv_size;
+    mach_port_t rcv_name;
+    mach_msg_timeout_t timeout;
+    mach_port_t notify;
+  };
+
+struct sigcontext *
+_hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
+			int signo, long int sigcode,
+			volatile int rpc_wait,
+			struct machine_thread_all_state *state)
+{
+  __label__ trampoline, rpc_wait_trampoline;
+  void *volatile sigsp;
+  struct sigcontext *scp;
+  struct 
+    {
+      int signo;
+      long int sigcode;
+      struct sigcontext *scp;	/* Points to ctx, below.  */
+      struct sigcontext *return_scp; /* Same; arg to sigreturn.  */
+      struct sigcontext ctx;
+    } *stackframe;
+
+  if (ss->context)
+    {
+      /* We have a previous sigcontext that sigreturn was about
+	 to restore when another signal arrived.  We will just base
+	 our setup on that.  */
+      if (_hurdsig_catch_fault (SIGSEGV))
+	assert (_hurdsig_fault_sigcode >= (long int) ss->context &&
+		_hurdsig_fault_sigcode < (long int) (ss->context + 1));
+      else
+	{
+	  memcpy (&state->basic, &ss->context->sc_i386_thread_state,
+		  sizeof (state->basic));
+	  memcpy (&state->fpu, &ss->context->sc_i386_float_state,
+		  sizeof (state->fpu));
+	  state->set = (1 << i386_THREAD_STATE) | (1 << i386_FLOAT_STATE);
+	  assert (! rpc_wait);
+	  /* The intr_port slot was cleared before sigreturn sent us the
+	     sig_post that made us notice this pending signal, so
+	     _hurd_internal_post_signal wouldn't do interrupt_operation.
+	     After we return, our caller will set SCP->sc_intr_port (in the
+	     new context) from SS->intr_port and clear SS->intr_port.  Now
+	     that we are restoring this old context recorded by sigreturn,
+	     we want to restore its intr_port too; so store it in
+	     SS->intr_port now, so it will end up in SCP->sc_intr_port
+	     later.  */
+	  ss->intr_port = ss->context->sc_intr_port;
+	}
+      /* If the sigreturn context was bogus, just ignore it.  */
+      ss->context = NULL;
+    }
+  else if (! machine_get_basic_state (ss->thread, state))
+    return NULL;
+
+  if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
+      !(ss->sigaltstack.ss_flags & (SA_DISABLE|SA_ONSTACK)))
+    {
+      sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
+      ss->sigaltstack.ss_flags |= SA_ONSTACK;
+      /* XXX need to set up base of new stack for
+	 per-thread variables, cthreads.  */
+    }
+  else
+    sigsp = (char *) state->basic.uesp;
+
+  /* Push the arguments to call `trampoline' on the stack.  */
+  sigsp -= sizeof (*stackframe);
+  stackframe = sigsp;
+
+  if (_hurdsig_catch_fault (SIGSEGV))
+    {
+      assert (_hurdsig_fault_sigcode >= (long int) stackframe &&
+	      _hurdsig_fault_sigcode <= (long int) (stackframe + 1));
+      /* We got a fault trying to write the stack frame.
+	 We cannot set up the signal handler.
+	 Returning NULL tells our caller, who will nuke us with a SIGILL.  */
+      return NULL;
+    }
+  else
+    {
+      int ok;
+
+      /* Set up the arguments for the signal handler.  */
+      stackframe->signo = signo;
+      stackframe->sigcode = sigcode;
+      stackframe->scp = stackframe->return_scp = scp = &stackframe->ctx;
+
+      /* Set up the sigcontext from the current state of the thread.  */
+
+      scp->sc_onstack = ss->sigaltstack.ss_flags & SA_ONSTACK ? 1 : 0;
+
+      /* struct sigcontext is laid out so that starting at sc_gs mimics a
+	 struct i386_thread_state.  */
+      memcpy (&scp->sc_i386_thread_state,
+	      &state->basic, sizeof (state->basic));
+
+      /* struct sigcontext is laid out so that starting at sc_fpkind mimics
+	 a struct i386_float_state.  */
+      ok = machine_get_state (ss->thread, state, i386_FLOAT_STATE,
+			      &state->fpu, &scp->sc_i386_float_state,
+			      sizeof (state->fpu));
+
+      _hurdsig_end_catch_fault ();
+
+      if (! ok)
+	return NULL;
+    }
+
+  /* Modify the thread state to call the trampoline code on the new stack.  */
+  if (rpc_wait)
+    {
+      /* The signalee thread was blocked in a mach_msg_trap system call,
+	 still waiting for a reply.  We will have it run the special
+	 trampoline code which retries the message receive before running
+	 the signal handler.
+	 
+	 To do this we change the OPTION argument on its stack to enable only
+	 message reception, since the request message has already been
+	 sent.  */
+
+      struct mach_msg_trap_args *args = (void *) state->basic.uesp;
+
+      if (_hurdsig_catch_fault (SIGSEGV))
+	{
+	  assert (_hurdsig_fault_sigcode >= (long int) args &&
+		  _hurdsig_fault_sigcode < (long int) (args + 1));
+	  /* Faulted accessing ARGS.  Bomb.  */
+	  return NULL;
+	}
+
+      assert (args->option & MACH_RCV_MSG);
+      /* Disable the message-send, since it has already completed.  The
+	 calls we retry need only wait to receive the reply message.  */
+      args->option &= ~MACH_SEND_MSG;
+
+      _hurdsig_end_catch_fault ();
+
+      state->basic.eip = (int) &&rpc_wait_trampoline;
+      /* The reply-receiving trampoline code runs initially on the original
+	 user stack.  We pass it the signal stack pointer in %ebx.  */
+      state->basic.ebx = (int) sigsp;
+      /* After doing the message receive, the trampoline code will need to
+	 update the %eax value to be restored by sigreturn.  To simplify
+	 the assembly code, we pass the address of its slot in SCP to the
+	 trampoline code in %ecx.  */
+      state->basic.ecx = (int) &scp->sc_eax;
+    }
+  else
+    {
+      state->basic.eip = (int) &&trampoline;
+      state->basic.uesp = (int) sigsp;
+    }
+  /* We pass the handler function to the trampoline code in %edx.  */
+  state->basic.edx = (int) handler;
+
+  return scp;
+
+  /* The trampoline code follows.  This is not actually executed as part of
+     this function, it is just convenient to write it that way.  */
+
+ rpc_wait_trampoline:
+  /* This is the entry point when we have an RPC reply message to receive
+     before running the handler.  The MACH_MSG_SEND bit has already been
+     cleared in the OPTION argument on our stack.  The interrupted user
+     stack pointer has not been changed, so the system call can find its
+     arguments; the signal stack pointer is in %ebx.  For our convenience,
+     %ecx points to the sc_eax member of the sigcontext.  */
+  asm volatile
+    (/* Retry the interrupted mach_msg system call.  */
+     "movl $-25, %eax\n"	/* mach_msg_trap */
+     "lcall $7, $0\n"
+     /* When the sigcontext was saved, %eax was MACH_RCV_INTERRUPTED.  But
+	now the message receive has completed and the original caller of
+	the RPC (i.e. the code running when the signal arrived) needs to
+	see the final return value of the message receive in %eax.  So
+	store the new %eax value into the sc_eax member of the sigcontext
+	(whose address is in %ecx to make this code simpler).  */
+     "movl %eax, (%ecx)\n"
+     /* Switch to the signal stack.  */
+     "movl %ebx, %esp\n");
+
+ trampoline:
+  /* Entry point for running the handler normally.  The arguments to the
+     handler function are already on the top of the stack:
+
+       0(%esp)	SIGNO
+       4(%esp)	SIGCODE
+       8(%esp)	SCP
+     */
+  asm volatile
+    ("call %*%%edx\n"		/* Call the handler function.  */
+     "addl $12, %%esp\n"	/* Pop its args.  */
+     "call %P0\n"		/* Call __sigreturn (SCP); never returns.  */
+     "hlt"			/* Just in case.  */
+     : : "i" (&__sigreturn));
+
+  /* NOTREACHED */
+  return NULL;
+}
+
+/* STATE describes a thread that had intr_port set (meaning it was inside
+   HURD_EINTR_RPC), after it has been thread_abort'd.  It it looks to have
+   just completed a mach_msg_trap system call that returned
+   MACH_RCV_INTERRUPTED, return nonzero and set *PORT to the receive right
+   being waited on.  */
+int
+_hurdsig_rcv_interrupted_p (struct machine_thread_all_state *state,
+			    mach_port_t *port)
+{
+  static const unsigned char syscall[] = { 0x9a, 0, 0, 0, 0, 7, 0 };
+  const unsigned char *volatile pc
+    = (void *) state->basic.eip - sizeof syscall;
+
+  if (_hurdsig_catch_fault (SIGSEGV))
+    assert (_hurdsig_fault_sigcode >= (long int) pc &&
+	    _hurdsig_fault_sigcode < (long int) pc + sizeof syscall);
+  else
+    {
+      int rcving = (state->basic.eax == MACH_RCV_INTERRUPTED &&
+		    !memcmp (pc, &syscall, sizeof syscall));
+      _hurdsig_end_catch_fault ();
+      if (rcving)
+	{
+	  /* We did just return from a mach_msg_trap system call
+	     doing a message receive that was interrupted.
+	     Examine the parameters to find the receive right.  */
+	  struct mach_msg_trap_args *args = (void *) state->basic.uesp;
+
+	  *port = args->rcv_name;
+	  return 1;
+	}
+    }
+
+  return 0;
+}