Back to index

glibc  2.9
sigreturn.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,94,95,96,97,98,2001 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 register int *sp asm ("%esp");
00020 
00021 #include <hurd.h>
00022 #include <hurd/signal.h>
00023 #include <hurd/threadvar.h>
00024 #include <hurd/msg.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 int
00029 __sigreturn (struct sigcontext *scp)
00030 {
00031   struct hurd_sigstate *ss;
00032   struct hurd_userlink *link = (void *) &scp[1];
00033   mach_port_t *reply_port;
00034 
00035   if (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK))
00036     {
00037       errno = EINVAL;
00038       return -1;
00039     }
00040 
00041   ss = _hurd_self_sigstate ();
00042   __spin_lock (&ss->lock);
00043 
00044   /* Remove the link on the `active resources' chain added by
00045      _hurd_setup_sighandler.  Its purpose was to make sure
00046      that we got called; now we have, it is done.  */
00047   _hurd_userlink_unlink (link);
00048 
00049   /* Restore the set of blocked signals, and the intr_port slot.  */
00050   ss->blocked = scp->sc_mask;
00051   ss->intr_port = scp->sc_intr_port;
00052 
00053   /* Check for pending signals that were blocked by the old set.  */
00054   if (ss->pending & ~ss->blocked)
00055     {
00056       /* There are pending signals that just became unblocked.  Wake up the
00057         signal thread to deliver them.  But first, squirrel away SCP where
00058         the signal thread will notice it if it runs another handler, and
00059         arrange to have us called over again in the new reality.  */
00060       ss->context = scp;
00061       __spin_unlock (&ss->lock);
00062       __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
00063       /* If a pending signal was handled, sig_post never returned.
00064         If it did return, the pending signal didn't run a handler;
00065         proceed as usual.  */
00066       __spin_lock (&ss->lock);
00067       ss->context = NULL;
00068     }
00069 
00070   if (scp->sc_onstack)
00071     {
00072       ss->sigaltstack.ss_flags &= ~SS_ONSTACK; /* XXX threadvars */
00073       /* XXX cannot unlock until off sigstack */
00074       abort ();
00075     }
00076   else
00077     __spin_unlock (&ss->lock);
00078 
00079   /* Destroy the MiG reply port used by the signal handler, and restore the
00080      reply port in use by the thread when interrupted.  */
00081   reply_port =
00082     (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
00083   if (*reply_port)
00084     {
00085       mach_port_t port = *reply_port;
00086 
00087       /* Assigning MACH_PORT_DEAD here tells libc's mig_get_reply_port not to
00088         get another reply port, but avoids mig_dealloc_reply_port trying to
00089         deallocate it after the receive fails (which it will, because the
00090         reply port will be bogus, whether we do this or not).  */
00091       *reply_port = MACH_PORT_DEAD;
00092 
00093       __mach_port_destroy (__mach_task_self (), port);
00094     }
00095   *reply_port = scp->sc_reply_port;
00096 
00097   if (scp->sc_fpused)
00098     /* Restore the FPU state.  Mach conveniently stores the state
00099        in the format the i387 `frstor' instruction uses to restore it.  */
00100     asm volatile ("frstor %0" : : "m" (scp->sc_fpsave));
00101 
00102   {
00103     /* There are convenient instructions to pop state off the stack, so we
00104        copy the registers onto the user's stack, switch there, pop and
00105        return.  */
00106 
00107     int *usp = (int *) scp->sc_uesp;
00108 
00109     *--usp = scp->sc_eip;
00110     *--usp = scp->sc_efl;
00111     memcpy (usp -= 12, &scp->sc_i386_thread_state, 12 * sizeof (int));
00112 
00113     sp = usp;
00114 
00115 #define A(line) asm volatile (#line)
00116     /* The members in the sigcontext are arranged in this order
00117        so we can pop them easily.  */
00118 
00119     /* Pop the segment registers (except %cs and %ss, done last).  */
00120     A (popl %gs);
00121     A (popl %fs);
00122     A (popl %es);
00123     A (popl %ds);
00124     /* Pop the general registers.  */
00125     A (popa);
00126     /* Pop the processor flags.  */
00127     A (popf);
00128     /* Return to the saved PC.  */
00129     A (ret);
00130 
00131     /* Firewall.  */
00132     A (hlt);
00133 #undef A
00134   }
00135 
00136   /* NOTREACHED */
00137   return -1;
00138 }
00139 
00140 weak_alias (__sigreturn, sigreturn)