2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / alpha / trampoline.c
blob2360cbb46907d3f13b3429842a1521842fdb4b39
1 /* Set thread_state for sighandler, and sigcontext to recover. 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/signal.h>
21 #include "thread_state.h"
22 #include "hurdfault.h"
23 #include <assert.h>
25 struct mach_msg_trap_args
27 /* This is the order of arguments to mach_msg_trap. */
28 mach_msg_header_t *msg;
29 mach_msg_option_t option;
30 mach_msg_size_t send_size;
31 mach_msg_size_t rcv_size;
32 mach_port_t rcv_name;
33 mach_msg_timeout_t timeout;
34 mach_port_t notify;
38 struct sigcontext *
39 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
40 int signo, struct hurd_signal_detail *detail,
41 int rpc_wait, struct machine_thread_all_state *state)
43 __label__ trampoline, rpc_wait_trampoline;
44 void *sigsp;
45 struct sigcontext *scp;
47 if (ss->context)
49 /* We have a previous sigcontext that sigreturn was about
50 to restore when another signal arrived. We will just base
51 our setup on that. */
52 if (! _hurdsig_catch_memory_fault (ss->context))
54 memcpy (&state->basic, &ss->context->sc_alpha_thread_state,
55 sizeof (state->basic));
56 memcpy (&state->exc, &ss->context->sc_alpha_exc_state,
57 sizeof (state->exc));
58 state->set = (1 << ALPHA_THREAD_STATE) | (1 << ALPHA_EXC_STATE);
59 if (state->exc.used_fpa)
61 memcpy (&state->fpu, &ss->context->sc_alpha_float_state,
62 sizeof (state->fpu));
63 state->set |= (1 << ALPHA_FLOAT_STATE);
65 assert (! rpc_wait);
66 /* The intr_port slot was cleared before sigreturn sent us the
67 sig_post that made us notice this pending signal, so
68 _hurd_internal_post_signal wouldn't do interrupt_operation.
69 After we return, our caller will set SCP->sc_intr_port (in the
70 new context) from SS->intr_port and clear SS->intr_port. Now
71 that we are restoring this old context recorded by sigreturn,
72 we want to restore its intr_port too; so store it in
73 SS->intr_port now, so it will end up in SCP->sc_intr_port
74 later. */
75 ss->intr_port = ss->context->sc_intr_port;
77 _hurdsig_end_catch_fault ();
79 /* If the sigreturn context was bogus, just ignore it. */
80 ss->context = NULL;
82 else if (! machine_get_basic_state (ss->thread, state))
83 return NULL;
85 if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
86 !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
88 sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
89 ss->sigaltstack.ss_flags |= SS_ONSTACK;
90 /* XXX need to set up base of new stack for
91 per-thread variables, cthreads. */
93 else
94 sigsp = (char *) state->basic.SP;
96 /* Set up the sigcontext structure on the stack. This is all the stack
97 needs, since the args are passed in registers (below). */
98 sigsp -= sizeof (*scp);
99 scp = sigsp;
101 if (_hurdsig_catch_memory_fault (scp))
103 /* We got a fault trying to write the stack frame.
104 We cannot set up the signal handler.
105 Returning NULL tells our caller, who will nuke us with a SIGILL. */
106 return NULL;
108 else
110 /* Set up the sigcontext from the current state of the thread. */
112 scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
114 /* struct sigcontext is laid out so that starting at sc_regs
115 mimics a struct alpha_thread_state. */
116 memcpy (&scp->sc_alpha_thread_state,
117 &state->basic, sizeof (state->basic));
119 /* struct sigcontext is laid out so that starting at sc_badvaddr
120 mimics a struct mips_exc_state. */
121 if (! machine_get_state (ss->thread, state, ALPHA_EXC_STATE,
122 &state->exc, &scp->sc_alpha_exc_state,
123 sizeof (state->exc)))
124 return NULL;
126 if (state->exc.used_fpa &&
127 /* struct sigcontext is laid out so that starting at sc_fpregs
128 mimics a struct alpha_float_state. This state
129 is only meaningful if the coprocessor was used. */
130 ! machine_get_state (ss->thread, state, ALPHA_FLOAT_STATE,
131 &state->fpu,
132 &scp->sc_alpha_float_state,
133 sizeof (state->fpu)))
134 return NULL;
136 _hurdsig_end_catch_fault ();
139 /* Modify the thread state to call the trampoline code on the new stack. */
140 if (rpc_wait)
142 /* The signalee thread was blocked in a mach_msg_trap system call,
143 still waiting for a reply. We will have it run the special
144 trampoline code which retries the message receive before running
145 the signal handler.
147 To do this we change the OPTION argument in its registers to
148 enable only message reception, since the request message has
149 already been sent. */
151 /* The system call arguments are stored in consecutive registers
152 starting with a0 ($16). */
153 struct mach_msg_trap_args *args = (void *) &state->basic.r16;
155 assert (args->option & MACH_RCV_MSG);
156 /* Disable the message-send, since it has already completed. The
157 calls we retry need only wait to receive the reply message. */
158 args->option &= ~MACH_SEND_MSG;
160 /* Limit the time to receive the reply message, in case the server
161 claimed that `interrupt_operation' succeeded but in fact the RPC
162 is hung. */
163 args->option |= MACH_RCV_TIMEOUT;
164 args->timeout = _hurd_interrupted_rpc_timeout;
166 state->basic.pc = (long int) &&rpc_wait_trampoline;
167 /* After doing the message receive, the trampoline code will need to
168 update the v0 ($0) value to be restored by sigreturn. To simplify
169 the assembly code, we pass the address of its slot in SCP to the
170 trampoline code in at ($28). */
171 state->basic.r28 = (long int) &scp->sc_regs[0];
172 /* We must preserve the mach_msg_trap args in a0..a5 and t0
173 ($16..$21, $1). Pass the handler args to the trampoline code in
174 t8..t10 ($22.$24). */
175 state->basic.r22 = signo;
176 state->basic.r23 = detail->code;
177 state->basic.r24 = (long int) scp;
179 else
181 state->basic.pc = (long int) &&trampoline;
182 state->basic.r16 = signo;
183 state->basic.r17 = detail->code;
184 state->basic.r18 = (long int) scp;
187 state->basic.r30 = (long int) sigsp; /* $30 is the stack pointer. */
189 /* We pass the handler function to the trampoline code in ra ($26). */
190 state->basic.r26 = (long int) handler;
191 /* In the callee-saved register t12/pv ($27), we store the
192 address of __sigreturn itself, for the trampoline code to use. */
193 state->basic.r27 = (long int) &__sigreturn;
194 /* In the callee-saved register t11/ai ($25), we save the SCP value to pass
195 to __sigreturn after the handler returns. */
196 state->basic.r25 = (long int) scp;
198 return scp;
200 /* The trampoline code follows. This is not actually executed as part of
201 this function, it is just convenient to write it that way. */
203 rpc_wait_trampoline:
204 /* This is the entry point when we have an RPC reply message to receive
205 before running the handler. The MACH_MSG_SEND bit has already been
206 cleared in the OPTION argument in our registers. For our convenience,
207 at ($28) points to the sc_regs[0] member of the sigcontext (saved v0
208 ($0)). */
209 asm volatile
210 (/* Retry the interrupted mach_msg system call. */
211 "lda $0, -25($31)\n" /* mach_msg_trap */
212 "callsys\n" /* Magic system call instruction. */
213 /* When the sigcontext was saved, v0 was MACH_RCV_INTERRUPTED. But
214 now the message receive has completed and the original caller of
215 the RPC (i.e. the code running when the signal arrived) needs to
216 see the final return value of the message receive in v0. So
217 store the new v0 value into the sc_regs[0] member of the sigcontext
218 (whose address is in at to make this code simpler). */
219 "stq $0, 0($28)\n"
220 /* Since the argument registers needed to have the mach_msg_trap
221 arguments, we've stored the arguments to the handler function
222 in registers t8..t10 ($22..$24). */
223 "mov $22, $16\n"
224 "mov $23, $17\n"
225 "mov $24, $18\n");
227 trampoline:
228 /* Entry point for running the handler normally. The arguments to the
229 handler function are already in the standard registers:
231 a0 SIGNO
232 a1 SIGCODE
233 a2 SCP
235 t12 also contains SCP; this value is callee-saved (and so should not get
236 clobbered by running the handler). We use this saved value to pass to
237 __sigreturn, so the handler can clobber the argument registers if it
238 likes. */
239 /* Call the handler function, saving return address in ra ($26). */
240 asm volatile ("jsr $26, ($26)");
241 /* Reset gp ($29) from the return address (here) in ra ($26). */
242 asm volatile ("ldgp $29, 0($26)");
243 asm volatile ("mov $25, $16"); /* Move saved SCP to argument register. */
244 /* Call __sigreturn (SCP); this cannot return. */
245 asm volatile ("jmp $31, ($27)");
247 /* NOTREACHED */
248 return NULL;