update from main archive 960910
[glibc.git] / sysdeps / mach / hurd / mips / sigreturn.c
blob7396a8bb2254d5ad172239b5c9390c3f4e454773
1 /* Copyright (C) 1991, 1992, 1994, 1995 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <hurd.h>
20 #include <hurd/signal.h>
21 #include <hurd/threadvar.h>
22 #include <stdlib.h>
24 int
25 __sigreturn (struct sigcontext *scp)
27 struct hurd_sigstate *ss;
28 mach_port_t *reply_port;
30 if (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK))
32 errno = EINVAL;
33 return -1;
36 ss = _hurd_self_sigstate ();
37 __spin_lock (&ss->lock);
39 /* Restore the set of blocked signals, and the intr_port slot. */
40 ss->blocked = scp->sc_mask;
41 ss->intr_port = scp->sc_intr_port;
43 /* Check for pending signals that were blocked by the old set. */
44 if (ss->pending & ~ss->blocked)
46 /* There are pending signals that just became unblocked. Wake up the
47 signal thread to deliver them. But first, squirrel away SCP where
48 the signal thread will notice it if it runs another handler, and
49 arrange to have us called over again in the new reality. */
50 ss->context = scp;
51 /* Clear the intr_port slot, since we are not in fact doing
52 an interruptible RPC right now. If SS->intr_port is not null,
53 the SCP context is doing an interruptible RPC, but the signal
54 thread will examine us while we are blocked in the sig_post RPC. */
55 ss->intr_port = MACH_PORT_NULL;
56 __spin_unlock (&ss->lock);
57 __msg_sig_post (_hurd_msgport, 0, __mach_task_self ());
58 /* If a pending signal was handled, sig_post never returned. */
59 __spin_lock (&ss->lock);
62 if (scp->sc_onstack)
64 ss->sigaltstack.ss_flags &= ~SA_ONSTACK; /* XXX threadvars */
65 /* XXX cannot unlock until off sigstack */
66 abort ();
68 else
69 __spin_unlock (&ss->lock);
71 /* Destroy the MiG reply port used by the signal handler, and restore the
72 reply port in use by the thread when interrupted. */
73 reply_port =
74 (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
75 if (*reply_port)
76 __mach_port_destroy (__mach_task_self (), *reply_port);
77 *reply_port = scp->sc_reply_port;
79 if (scp->sc_coproc_used & SC_COPROC_USE_FPU)
81 /* Restore FPU state. */
82 #define restore_fpr(n) \
83 asm volatile ("l.d $f" #n ",%0" : : "m" (scp->sc_fpr[n]))
85 /* Restore floating-point registers. */
86 restore_fpr (0);
87 restore_fpr (2);
88 restore_fpr (4);
89 restore_fpr (6);
90 restore_fpr (8);
91 restore_fpr (10);
92 restore_fpr (12);
93 restore_fpr (14);
94 restore_fpr (16);
95 restore_fpr (18);
96 restore_fpr (20);
97 restore_fpr (22);
98 restore_fpr (24);
99 restore_fpr (26);
100 restore_fpr (28);
101 restore_fpr (30);
103 /* Restore the floating-point control/status register ($f31). */
104 asm volatile ("ctc1 %0,$f31" : : "r" (scp->sc_fpcsr));
107 /* Load all the registers from the sigcontext. */
108 #define restore_gpr(n) \
109 asm volatile ("lw $" #n ",%0" : : "m" (scpreg->sc_gpr[n - 1]))
112 register const struct sigcontext *const scpreg asm ("$1") = scp;
113 register int *at asm ("$1");
115 /* First restore the multiplication result registers. The compiler
116 will use some temporary registers, so we do this before restoring
117 the general registers. */
118 asm volatile ("mtlo %0" : : "r" (scpreg->sc_mdlo));
119 asm volatile ("mthi %0" : : "r" (scpreg->sc_mdhi));
121 /* In the word after the saved PC, store the saved $1 value. */
122 (&scpreg->sc_pc)[1] = scpreg->sc_gpr[0];
124 asm volatile (".set noreorder; .set noat;");
126 /* Restore the normal registers. */
127 restore_gpr (2);
128 restore_gpr (3);
129 restore_gpr (4);
130 restore_gpr (5);
131 restore_gpr (6);
132 restore_gpr (7);
133 restore_gpr (8);
134 restore_gpr (9);
135 restore_gpr (10);
136 restore_gpr (11);
137 restore_gpr (12);
138 restore_gpr (13);
139 restore_gpr (14);
140 restore_gpr (15);
141 restore_gpr (16);
142 restore_gpr (17);
143 restore_gpr (18);
144 restore_gpr (19);
145 restore_gpr (20);
146 restore_gpr (21);
147 restore_gpr (22);
148 restore_gpr (23);
149 restore_gpr (24);
150 restore_gpr (25);
151 /* Registers 26-27 are kernel-only. */
152 restore_gpr (28);
153 restore_gpr (29); /* Stack pointer. */
154 restore_gpr (30); /* Frame pointer. */
155 restore_gpr (31); /* Return address. */
157 at = &scpreg->sc_pc;
158 /* This is an emulated instruction that will find at the address in $1
159 two words: the PC value to restore, and the $1 value to restore. */
160 asm volatile (".word op_sigreturn");
162 asm volatile (".set reorder; .set at;");
165 /* NOTREACHED */
166 return -1;
169 weak_alias (__sigreturn, sigreturn)