2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / alpha / sigreturn.c
blob182d4cbd8489ae2a3d337287dd7f77d9e8e2e328
1 /* Return from signal handler in GNU C library for Hurd. Alpha version.
2 Copyright (C) 1994,95,97,98,2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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 mach_port_t *reply_port;
33 if (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK))
35 errno = EINVAL;
36 return -1;
39 ss = _hurd_self_sigstate ();
40 __spin_lock (&ss->lock);
42 /* Restore the set of blocked signals, and the intr_port slot. */
43 ss->blocked = scp->sc_mask;
44 ss->intr_port = scp->sc_intr_port;
46 /* Check for pending signals that were blocked by the old set. */
47 if (ss->pending & ~ss->blocked)
49 /* There are pending signals that just became unblocked. Wake up the
50 signal thread to deliver them. But first, squirrel away SCP where
51 the signal thread will notice it if it runs another handler, and
52 arrange to have us called over again in the new reality. */
53 ss->context = scp;
54 /* Clear the intr_port slot, since we are not in fact doing
55 an interruptible RPC right now. If SS->intr_port is not null,
56 the SCP context is doing an interruptible RPC, but the signal
57 thread will examine us while we are blocked in the sig_post RPC. */
58 ss->intr_port = MACH_PORT_NULL;
59 __spin_unlock (&ss->lock);
60 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
61 /* If a pending signal was handled, sig_post never returned. */
62 __spin_lock (&ss->lock);
65 if (scp->sc_onstack)
67 ss->sigaltstack.ss_flags &= ~SS_ONSTACK; /* XXX threadvars */
68 /* XXX cannot unlock until off sigstack */
69 abort ();
71 else
72 __spin_unlock (&ss->lock);
74 /* Destroy the MiG reply port used by the signal handler, and restore the
75 reply port in use by the thread when interrupted. */
76 reply_port =
77 (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
78 if (*reply_port)
79 __mach_port_destroy (__mach_task_self (), *reply_port);
80 *reply_port = scp->sc_reply_port;
82 if (scp->sc_used_fpa)
84 /* Restore FPU state. */
86 /* Restore the floating-point control/status register.
87 We must do this first because the compiler will need
88 a temporary FP register for the load. */
89 asm volatile ("mt_fpcr %0" : : "f" (scp->sc_fpcsr));
91 /* Restore floating-point registers. */
92 #define restore_fpr(n) \
93 asm volatile ("ldt $f" #n ",%0" : : "m" (scp->sc_fpregs[n]))
94 restore_fpr (0);
95 restore_fpr (1);
96 restore_fpr (2);
97 restore_fpr (3);
98 restore_fpr (4);
99 restore_fpr (5);
100 restore_fpr (6);
101 restore_fpr (7);
102 restore_fpr (8);
103 restore_fpr (9);
104 restore_fpr (10);
105 restore_fpr (11);
106 restore_fpr (12);
107 restore_fpr (13);
108 restore_fpr (14);
109 restore_fpr (15);
110 restore_fpr (16);
111 restore_fpr (17);
112 restore_fpr (18);
113 restore_fpr (19);
114 restore_fpr (20);
115 restore_fpr (21);
116 restore_fpr (22);
117 restore_fpr (23);
118 restore_fpr (24);
119 restore_fpr (25);
120 restore_fpr (26);
121 restore_fpr (27);
122 restore_fpr (28);
123 restore_fpr (29);
124 restore_fpr (30);
127 /* Load all the registers from the sigcontext. */
128 #define restore_gpr(n) \
129 asm volatile ("ldq $" #n ",%0" : : "m" (scpreg->sc_regs[n]))
132 /* The `rei' PAL pseudo-instruction restores registers $2..$7, the PC
133 and processor status. So we can use these few registers for our
134 working variables. Unfortunately, it finds its data on the stack
135 and merely pops the SP ($30) over the words of state restored,
136 allowing no other option for the new SP value. So we must push the
137 registers and PSW it will to restore, onto the user's stack and let
138 it pop them from there. */
139 register const struct sigcontext *const scpreg asm ("$2") = scp;
140 register integer_t *usp asm ("$3") = (integer_t *) scpreg->sc_regs[30];
141 register integer_t usp_align asm ("$4");
143 /* Push an 8-word "trap frame" onto the user stack for `rei':
144 registers $2..$7, the PC, and the PSW. */
146 register struct rei_frame
148 integer_t regs[5], pc, ps;
149 } *rei_frame asm ("$5");
151 usp -= 8;
152 /* `rei' demands that the stack be aligned to a 64 byte (8 word)
153 boundary; bits 61..56 of the PSW are OR'd back into the SP value
154 after popping the 8-word trap frame, so we store (sp % 64)
155 there and this restores the original user SP. */
156 usp_align = (integer_t) usp & 63L;
157 rei_frame = (void *) ((integer_t) usp & ~63L);
159 /* Copy the registers and PC from the sigcontext. */
160 memcpy (rei_frame->regs, &scpreg->sc_regs[2], sizeof rei_frame->regs);
161 rei_frame->pc = scpreg->sc_pc;
163 /* Compute the new PS value to be restored. `rei' adds the value at
164 bits 61..56 to the SP to compensate for the alignment above that
165 cleared the low 6 bits; bits 5..3 are the new mode/privilege level
166 (must be >= current mode; 3 == user mode); bits 2..0 are "software",
167 unused by the processor or kernel (XXX should trampoline save these?
168 How?); in user mode, `rei' demands that all other bits be zero. */
169 rei_frame->ps = (usp_align << 56) | (3 << 3); /* XXX low 3 bits??? */
171 /* Restore the other general registers: everything except $2..$7, which
172 are in the `rei' trap frame we set up above, and $30, which is the
173 SP which is popped by `rei'. */
174 restore_gpr (1);
175 restore_gpr (8);
176 restore_gpr (9);
177 restore_gpr (10);
178 restore_gpr (11);
179 restore_gpr (12);
180 restore_gpr (13);
181 restore_gpr (14);
182 restore_gpr (15);
183 restore_gpr (16);
184 restore_gpr (17);
185 restore_gpr (18);
186 restore_gpr (19);
187 restore_gpr (20);
188 restore_gpr (21);
189 restore_gpr (22);
190 restore_gpr (23);
191 restore_gpr (24);
192 restore_gpr (25);
193 restore_gpr (26);
194 restore_gpr (27);
195 restore_gpr (28);
196 restore_gpr (29);
198 /* Switch the stack pointer to the trap frame set up on
199 the user stack and do the magical `rei' PAL call. */
200 asm volatile ("mov %0, $30\n"
201 "call_pal %1"
202 : : "r" (rei_frame), "i" (63)); /* PAL_rti */
203 /* Firewall. */
204 asm volatile ("halt");
207 /* NOTREACHED */
208 return -1;
211 weak_alias (__sigreturn, sigreturn)