Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / i386 / sigreturn.c
blob6dba161d7cfc7a5bc0a2367fcecf48b3581106e6
1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 register int *sp asm ("%esp");
20 #include <hurd.h>
21 #include <hurd/signal.h>
22 #include <hurd/threadvar.h>
23 #include <hurd/msg.h>
24 #include <stdlib.h>
25 #include <string.h>
27 int
28 __sigreturn (struct sigcontext *scp)
30 struct hurd_sigstate *ss;
31 struct hurd_userlink *link = (void *) &scp[1];
32 mach_port_t *reply_port;
34 if (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK))
36 errno = EINVAL;
37 return -1;
40 ss = _hurd_self_sigstate ();
41 __spin_lock (&ss->lock);
43 /* Remove the link on the `active resources' chain added by
44 _hurd_setup_sighandler. Its purpose was to make sure
45 that we got called; now we have, it is done. */
46 _hurd_userlink_unlink (link);
48 /* Restore the set of blocked signals, and the intr_port slot. */
49 ss->blocked = scp->sc_mask;
50 ss->intr_port = scp->sc_intr_port;
52 /* Check for pending signals that were blocked by the old set. */
53 if (ss->pending & ~ss->blocked)
55 /* There are pending signals that just became unblocked. Wake up the
56 signal thread to deliver them. But first, squirrel away SCP where
57 the signal thread will notice it if it runs another handler, and
58 arrange to have us called over again in the new reality. */
59 ss->context = scp;
60 __spin_unlock (&ss->lock);
61 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
62 /* If a pending signal was handled, sig_post never returned.
63 If it did return, the pending signal didn't run a handler;
64 proceed as usual. */
65 __spin_lock (&ss->lock);
66 ss->context = NULL;
69 if (scp->sc_onstack)
71 ss->sigaltstack.ss_flags &= ~SS_ONSTACK; /* XXX threadvars */
72 /* XXX cannot unlock until off sigstack */
73 abort ();
75 else
76 __spin_unlock (&ss->lock);
78 /* Destroy the MiG reply port used by the signal handler, and restore the
79 reply port in use by the thread when interrupted. */
80 reply_port =
81 (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
82 if (*reply_port)
84 mach_port_t port = *reply_port;
86 /* Assigning MACH_PORT_DEAD here tells libc's mig_get_reply_port not to
87 get another reply port, but avoids mig_dealloc_reply_port trying to
88 deallocate it after the receive fails (which it will, because the
89 reply port will be bogus, whether we do this or not). */
90 *reply_port = MACH_PORT_DEAD;
92 __mach_port_destroy (__mach_task_self (), port);
94 *reply_port = scp->sc_reply_port;
96 if (scp->sc_fpused)
97 /* Restore the FPU state. Mach conveniently stores the state
98 in the format the i387 `frstor' instruction uses to restore it. */
99 asm volatile ("frstor %0" : : "m" (scp->sc_fpsave));
102 /* There are convenient instructions to pop state off the stack, so we
103 copy the registers onto the user's stack, switch there, pop and
104 return. */
106 int *usp = (int *) scp->sc_uesp;
108 *--usp = scp->sc_eip;
109 *--usp = scp->sc_efl;
110 memcpy (usp -= 12, &scp->sc_i386_thread_state, 12 * sizeof (int));
112 sp = usp;
114 #define A(line) asm volatile (#line)
115 /* The members in the sigcontext are arranged in this order
116 so we can pop them easily. */
118 /* Pop the segment registers (except %cs and %ss, done last). */
119 A (popl %gs);
120 A (popl %fs);
121 A (popl %es);
122 A (popl %ds);
123 /* Pop the general registers. */
124 A (popa);
125 /* Pop the processor flags. */
126 A (popf);
127 /* Return to the saved PC. */
128 A (ret);
130 /* Firewall. */
131 A (hlt);
132 #undef A
135 /* NOTREACHED */
136 return -1;
139 weak_alias (__sigreturn, sigreturn)