2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / i386 / sigreturn.c
blob60b0d006a399ea9631e66159872a634affc850d2
1 /* Copyright (C) 1991,92,94,95,96,97,98,2001 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 register int *sp asm ("%esp");
21 #include <hurd.h>
22 #include <hurd/signal.h>
23 #include <hurd/threadvar.h>
24 #include <hurd/msg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 int
29 __sigreturn (struct sigcontext *scp)
31 struct hurd_sigstate *ss;
32 struct hurd_userlink *link = (void *) &scp[1];
33 mach_port_t *reply_port;
35 if (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK))
37 errno = EINVAL;
38 return -1;
41 ss = _hurd_self_sigstate ();
42 __spin_lock (&ss->lock);
44 /* Remove the link on the `active resources' chain added by
45 _hurd_setup_sighandler. Its purpose was to make sure
46 that we got called; now we have, it is done. */
47 _hurd_userlink_unlink (link);
49 /* Restore the set of blocked signals, and the intr_port slot. */
50 ss->blocked = scp->sc_mask;
51 ss->intr_port = scp->sc_intr_port;
53 /* Check for pending signals that were blocked by the old set. */
54 if (ss->pending & ~ss->blocked)
56 /* There are pending signals that just became unblocked. Wake up the
57 signal thread to deliver them. But first, squirrel away SCP where
58 the signal thread will notice it if it runs another handler, and
59 arrange to have us called over again in the new reality. */
60 ss->context = scp;
61 __spin_unlock (&ss->lock);
62 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
63 /* If a pending signal was handled, sig_post never returned.
64 If it did return, the pending signal didn't run a handler;
65 proceed as usual. */
66 __spin_lock (&ss->lock);
67 ss->context = NULL;
70 if (scp->sc_onstack)
72 ss->sigaltstack.ss_flags &= ~SS_ONSTACK; /* XXX threadvars */
73 /* XXX cannot unlock until off sigstack */
74 abort ();
76 else
77 __spin_unlock (&ss->lock);
79 /* Destroy the MiG reply port used by the signal handler, and restore the
80 reply port in use by the thread when interrupted. */
81 reply_port =
82 (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
83 if (*reply_port)
85 mach_port_t port = *reply_port;
87 /* Assigning MACH_PORT_DEAD here tells libc's mig_get_reply_port not to
88 get another reply port, but avoids mig_dealloc_reply_port trying to
89 deallocate it after the receive fails (which it will, because the
90 reply port will be bogus, whether we do this or not). */
91 *reply_port = MACH_PORT_DEAD;
93 __mach_port_destroy (__mach_task_self (), port);
95 *reply_port = scp->sc_reply_port;
97 if (scp->sc_fpused)
98 /* Restore the FPU state. Mach conveniently stores the state
99 in the format the i387 `frstor' instruction uses to restore it. */
100 asm volatile ("frstor %0" : : "m" (scp->sc_fpsave));
103 /* There are convenient instructions to pop state off the stack, so we
104 copy the registers onto the user's stack, switch there, pop and
105 return. */
107 int *usp = (int *) scp->sc_uesp;
109 *--usp = scp->sc_eip;
110 *--usp = scp->sc_efl;
111 memcpy (usp -= 12, &scp->sc_i386_thread_state, 12 * sizeof (int));
113 sp = usp;
115 #define A(line) asm volatile (#line)
116 /* The members in the sigcontext are arranged in this order
117 so we can pop them easily. */
119 /* Pop the segment registers (except %cs and %ss, done last). */
120 A (popl %gs);
121 A (popl %fs);
122 A (popl %es);
123 A (popl %ds);
124 /* Pop the general registers. */
125 A (popa);
126 /* Pop the processor flags. */
127 A (popf);
128 /* Return to the saved PC. */
129 A (ret);
131 /* Firewall. */
132 A (hlt);
133 #undef A
136 /* NOTREACHED */
137 return -1;
140 weak_alias (__sigreturn, sigreturn)