initial import
[glibc.git] / sysdeps / mach / hurd / alpha / sigreturn.c
blobe5dc383a3a7ac417037f8eae1e5307ab029f6926
1 /* Return from signal handler in GNU C library for Hurd. Alpha version.
2 Copyright (C) 1994, 1995 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, 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>
26 #include <mach/machine/alpha_instruction.h>
28 int
29 __sigreturn (struct sigcontext *scp)
31 struct hurd_sigstate *ss;
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 /* Restore the set of blocked signals, and the intr_port slot. */
44 ss->blocked = scp->sc_mask;
45 ss->intr_port = scp->sc_intr_port;
47 /* Check for pending signals that were blocked by the old set. */
48 if (ss->pending & ~ss->blocked)
50 /* There are pending signals that just became unblocked. Wake up the
51 signal thread to deliver them. But first, squirrel away SCP where
52 the signal thread will notice it if it runs another handler, and
53 arrange to have us called over again in the new reality. */
54 ss->context = scp;
55 /* Clear the intr_port slot, since we are not in fact doing
56 an interruptible RPC right now. If SS->intr_port is not null,
57 the SCP context is doing an interruptible RPC, but the signal
58 thread will examine us while we are blocked in the sig_post RPC. */
59 ss->intr_port = MACH_PORT_NULL;
60 __spin_unlock (&ss->lock);
61 __msg_sig_post (_hurd_msgport, 0, __mach_task_self ());
62 /* If a pending signal was handled, sig_post never returned. */
63 __spin_lock (&ss->lock);
66 if (scp->sc_onstack)
68 ss->sigaltstack.ss_flags &= ~SA_ONSTACK; /* XXX threadvars */
69 /* XXX cannot unlock until off sigstack */
70 abort ();
72 else
73 __spin_unlock (&ss->lock);
75 /* Destroy the MiG reply port used by the signal handler, and restore the
76 reply port in use by the thread when interrupted. */
77 reply_port =
78 (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
79 if (*reply_port)
80 __mach_port_destroy (__mach_task_self (), *reply_port);
81 *reply_port = scp->sc_reply_port;
83 if (scp->sc_used_fpa)
85 /* Restore FPU state. */
87 /* Restore the floating-point control/status register.
88 We must do this first because the compiler will need
89 a temporary FP register for the load. */
90 asm volatile ("mt_fpcr %0" : : "f" (scp->sc_fpcsr));
92 /* Restore floating-point registers. */
93 #define restore_fpr(n) \
94 asm volatile ("ldt $f" #n ",%0" : : "m" (scp->sc_fpregs[n]))
95 restore_fpr (0);
96 restore_fpr (1);
97 restore_fpr (2);
98 restore_fpr (3);
99 restore_fpr (4);
100 restore_fpr (5);
101 restore_fpr (6);
102 restore_fpr (7);
103 restore_fpr (8);
104 restore_fpr (9);
105 restore_fpr (10);
106 restore_fpr (11);
107 restore_fpr (12);
108 restore_fpr (13);
109 restore_fpr (14);
110 restore_fpr (15);
111 restore_fpr (16);
112 restore_fpr (17);
113 restore_fpr (18);
114 restore_fpr (19);
115 restore_fpr (20);
116 restore_fpr (21);
117 restore_fpr (22);
118 restore_fpr (23);
119 restore_fpr (24);
120 restore_fpr (25);
121 restore_fpr (26);
122 restore_fpr (27);
123 restore_fpr (28);
124 restore_fpr (29);
125 restore_fpr (30);
128 /* Load all the registers from the sigcontext. */
129 #define restore_gpr(n) \
130 asm volatile ("ldq $" #n ",%0" : : "m" (scpreg->sc_regs[n]))
133 /* The `rei' PAL pseudo-instruction restores registers $2..$7, the PC
134 and processor status. So we can use these few registers for our
135 working variables. Unfortunately, it finds its data on the stack
136 and merely pops the SP ($30) over the words of state restored,
137 allowing no other option for the new SP value. So we must push the
138 registers and PSW it will to restore, onto the user's stack and let
139 it pop them from there. */
140 register const struct sigcontext *const scpreg asm ("$2") = scp;
141 register integer_t *usp asm ("$3") = (integer_t *) scpreg->sc_regs[30];
142 register integer_t usp_align asm ("$4");
144 /* Push an 8-word "trap frame" onto the user stack for `rei':
145 registers $2..$7, the PC, and the PSW. */
147 register struct rei_frame
149 integer_t regs[5], pc, ps;
150 } *rei_frame asm ("$5");
152 usp -= 8;
153 /* `rei' demands that the stack be aligned to a 64 byte (8 word)
154 boundary; bits 61..56 of the PSW are OR'd back into the SP value
155 after popping the 8-word trap frame, so we store (sp % 64)
156 there and this restores the original user SP. */
157 usp_align = (integer_t) usp & 63L;
158 rei_frame = (void *) ((integer_t) usp & ~63L);
160 /* Copy the registers and PC from the sigcontext. */
161 memcpy (rei_frame->regs, &scpreg->sc_regs[2], sizeof rei_frame->regs);
162 rei_frame->pc = scpreg->sc_pc;
164 /* Compute the new PS value to be restored. `rei' adds the value at
165 bits 61..56 to the SP to compensate for the alignment above that
166 cleared the low 6 bits; bits 5..3 are the new mode/privilege level
167 (must be >= current mode; 3 == user mode); bits 2..0 are "software",
168 unused by the processor or kernel (XXX should trampoline save these?
169 How?); in user mode, `rei' demands that all other bits be zero. */
170 rei_frame->ps = (usp_align << 56) | (3 << 3); /* XXX low 3 bits??? */
172 /* Restore the other general registers: everything except $2..$7, which
173 are in the `rei' trap frame we set up above, and $30, which is the
174 SP which is popped by `rei'. */
175 restore_gpr (1);
176 restore_gpr (8);
177 restore_gpr (9);
178 restore_gpr (10);
179 restore_gpr (11);
180 restore_gpr (12);
181 restore_gpr (13);
182 restore_gpr (14);
183 restore_gpr (15);
184 restore_gpr (16);
185 restore_gpr (17);
186 restore_gpr (18);
187 restore_gpr (19);
188 restore_gpr (20);
189 restore_gpr (21);
190 restore_gpr (22);
191 restore_gpr (23);
192 restore_gpr (24);
193 restore_gpr (25);
194 restore_gpr (26);
195 restore_gpr (27);
196 restore_gpr (28);
197 restore_gpr (29);
199 /* Switch the stack pointer to the trap frame set up on
200 the user stack and do the magical `rei' PAL call. */
201 asm volatile ("mov %0, $30\n"
202 "call_pal %1"
203 : : "r" (rei_frame), "i" (op_rei));
204 /* Firewall. */
205 asm volatile ("call_pal %0" : : "i" (op_halt));
208 /* NOTREACHED */
209 return -1;
212 weak_alias (__sigreturn, sigreturn)