Blame sysdeps/mach/hurd/i386/sigreturn.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
register int *sp asm ("%esp");
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/signal.h>
Packit 6c4009
#include <hurd/threadvar.h>
Packit 6c4009
#include <hurd/msg.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__sigreturn (struct sigcontext *scp)
Packit 6c4009
{
Packit 6c4009
  struct hurd_sigstate *ss;
Packit 6c4009
  struct hurd_userlink *link = (void *) &scp[1];
Packit 6c4009
  mach_port_t *reply_port;
Packit 6c4009
Packit 6c4009
  if (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK))
Packit 6c4009
    {
Packit 6c4009
      errno = EINVAL;
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  ss = _hurd_self_sigstate ();
Packit 6c4009
  __spin_lock (&ss->lock);
Packit 6c4009
Packit 6c4009
  /* Remove the link on the `active resources' chain added by
Packit 6c4009
     _hurd_setup_sighandler.  Its purpose was to make sure
Packit 6c4009
     that we got called; now we have, it is done.  */
Packit 6c4009
  _hurd_userlink_unlink (link);
Packit 6c4009
Packit 6c4009
  /* Restore the set of blocked signals, and the intr_port slot.  */
Packit 6c4009
  ss->blocked = scp->sc_mask;
Packit 6c4009
  ss->intr_port = scp->sc_intr_port;
Packit 6c4009
Packit 6c4009
  /* Check for pending signals that were blocked by the old set.  */
Packit 6c4009
  if (ss->pending & ~ss->blocked)
Packit 6c4009
    {
Packit 6c4009
      /* There are pending signals that just became unblocked.  Wake up the
Packit 6c4009
	 signal thread to deliver them.  But first, squirrel away SCP where
Packit 6c4009
	 the signal thread will notice it if it runs another handler, and
Packit 6c4009
	 arrange to have us called over again in the new reality.  */
Packit 6c4009
      ss->context = scp;
Packit 6c4009
      __spin_unlock (&ss->lock);
Packit 6c4009
      __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
Packit 6c4009
      /* If a pending signal was handled, sig_post never returned.
Packit 6c4009
	 If it did return, the pending signal didn't run a handler;
Packit 6c4009
	 proceed as usual.  */
Packit 6c4009
      __spin_lock (&ss->lock);
Packit 6c4009
      ss->context = NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (scp->sc_onstack)
Packit 6c4009
    {
Packit 6c4009
      ss->sigaltstack.ss_flags &= ~SS_ONSTACK;
Packit 6c4009
      /* XXX cannot unlock until off sigstack */
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
  /* Destroy the MiG reply port used by the signal handler, and restore the
Packit 6c4009
     reply port in use by the thread when interrupted.  */
Packit 6c4009
  reply_port = &__hurd_local_reply_port;
Packit 6c4009
  if (*reply_port)
Packit 6c4009
    {
Packit 6c4009
      mach_port_t port = *reply_port;
Packit 6c4009
Packit 6c4009
      /* Assigning MACH_PORT_DEAD here tells libc's mig_get_reply_port not to
Packit 6c4009
	 get another reply port, but avoids mig_dealloc_reply_port trying to
Packit 6c4009
	 deallocate it after the receive fails (which it will, because the
Packit 6c4009
	 reply port will be bogus, whether we do this or not).  */
Packit 6c4009
      *reply_port = MACH_PORT_DEAD;
Packit 6c4009
Packit 6c4009
      __mach_port_destroy (__mach_task_self (), port);
Packit 6c4009
    }
Packit 6c4009
  *reply_port = scp->sc_reply_port;
Packit 6c4009
Packit 6c4009
  if (scp->sc_fpused)
Packit 6c4009
    /* Restore the FPU state.  Mach conveniently stores the state
Packit 6c4009
       in the format the i387 `frstor' instruction uses to restore it.  */
Packit 6c4009
    asm volatile ("frstor %0" : : "m" (scp->sc_fpsave));
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    /* There are convenient instructions to pop state off the stack, so we
Packit 6c4009
       copy the registers onto the user's stack, switch there, pop and
Packit 6c4009
       return.  */
Packit 6c4009
Packit 6c4009
    int *usp = (int *) scp->sc_uesp;
Packit 6c4009
Packit 6c4009
    *--usp = scp->sc_eip;
Packit 6c4009
    *--usp = scp->sc_efl;
Packit 6c4009
    memcpy (usp -= 12, &scp->sc_i386_thread_state, 12 * sizeof (int));
Packit 6c4009
Packit 6c4009
    sp = usp;
Packit 6c4009
Packit 6c4009
#define A(line) asm volatile (#line)
Packit 6c4009
    /* The members in the sigcontext are arranged in this order
Packit 6c4009
       so we can pop them easily.  */
Packit 6c4009
Packit 6c4009
    /* Pop the segment registers (except %cs and %ss, done last).  */
Packit 6c4009
    A (popl %gs);
Packit 6c4009
    A (popl %fs);
Packit 6c4009
    A (popl %es);
Packit 6c4009
    A (popl %ds);
Packit 6c4009
    /* Pop the general registers.  */
Packit 6c4009
    A (popa);
Packit 6c4009
    /* Pop the processor flags.  */
Packit 6c4009
    A (popf);
Packit 6c4009
    /* Return to the saved PC.  */
Packit 6c4009
    A (ret);
Packit 6c4009
Packit 6c4009
    /* Firewall.  */
Packit 6c4009
    A (hlt);
Packit 6c4009
#undef A
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* NOTREACHED */
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__sigreturn, sigreturn)