Test for stack alignment.
[glibc.git] / sysdeps / mach / hurd / hppa / trampoline.c
blobbbb5b960acba28a6ec231fe87c854d532257f486
1 /* Set thread_state for sighandler, and sigcontext to recover. HPPA version.
2 Copyright (C) 1995, 1997, 1998 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 <assert.h>
23 #include <errno.h>
24 #include "hurdfault.h"
27 struct mach_msg_trap_regargs
29 /* These first four arguments are in registers 26..23. */
30 mach_msg_size_t rcv_size; /* arg3 */
31 mach_msg_size_t send_size; /* arg2 */
32 mach_msg_option_t option; /* arg1 */
33 mach_msg_header_t *msg; /* arg0 */
36 struct sigcontext *
37 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
38 int signo, long int sigcode,
39 volatile int rpc_wait,
40 struct machine_thread_all_state *state)
42 __label__ trampoline, rpc_wait_trampoline;
43 void *volatile sigsp;
44 struct sigcontext *scp;
46 if (ss->context)
48 /* We have a previous sigcontext that sigreturn was about
49 to restore when another signal arrived. We will just base
50 our setup on that. */
51 if (_hurdsig_catch_fault (SIGSEGV))
52 assert (_hurdsig_fault_sigcode >= (long int) ss->context &&
53 _hurdsig_fault_sigcode < (long int) (ss->context + 1));
54 else
56 memcpy (&state->basic, &ss->context->sc_parisc_thread_state,
57 sizeof (state->basic));
58 state->set = (1 << PARISC_THREAD_STATE);
59 assert (! rpc_wait);
60 /* The intr_port slot was cleared before sigreturn sent us the
61 sig_post that made us notice this pending signal, so
62 _hurd_internal_post_signal wouldn't do interrupt_operation.
63 After we return, our caller will set SCP->sc_intr_port (in the
64 new context) from SS->intr_port and clear SS->intr_port. Now
65 that we are restoring this old context recorded by sigreturn,
66 we want to restore its intr_port too; so store it in
67 SS->intr_port now, so it will end up in SCP->sc_intr_port
68 later. */
69 ss->intr_port = ss->context->sc_intr_port;
71 /* If the sigreturn context was bogus, just ignore it. */
72 ss->context = NULL;
74 else if (! machine_get_basic_state (ss->thread, state))
75 return NULL;
77 if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
78 !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
80 sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
81 ss->sigaltstack.ss_flags |= SS_ONSTACK;
82 /* XXX need to set up base of new stack for
83 per-thread variables, cthreads. */
85 else
86 sigsp = (char *) state->basic.uesp;
88 /* Push the signal context on the stack. */
89 sigsp -= sizeof (*scp);
90 scp = sigsp;
92 if (_hurdsig_catch_fault (SIGSEGV))
94 assert (_hurdsig_fault_sigcode >= (long int) scp &&
95 _hurdsig_fault_sigcode <= (long int) (scp + 1));
96 /* We got a fault trying to write the stack frame.
97 We cannot set up the signal handler.
98 Returning NULL tells our caller, who will nuke us with a SIGILL. */
99 return NULL;
101 else
103 int ok;
105 /* Set up the sigcontext from the current state of the thread. */
107 scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
109 /* struct sigcontext is laid out so that starting at sc_regs mimics a
110 struct parisc_thread_state. */
111 memcpy (&scp->sc_parisc_thread_state,
112 &state->basic, sizeof (state->basic));
114 _hurdsig_end_catch_fault ();
116 if (! ok)
117 return NULL;
120 /* Modify the thread state to call the trampoline code on the new stack. */
121 if (rpc_wait)
123 /* The signalee thread was blocked in a mach_msg_trap system call,
124 still waiting for a reply. We will have it run the special
125 trampoline code which retries the message receive before running
126 the signal handler.
128 To do this we change the OPTION argument on its stack to enable only
129 message reception, since the request message has already been
130 sent. */
132 struct mach_msg_trap_regargs *args = (void *) &state->basic.r23;
134 if (_hurdsig_catch_fault (SIGSEGV))
136 assert (_hurdsig_fault_sigcode >= (long int) args &&
137 _hurdsig_fault_sigcode < (long int) (args + 1));
138 /* Faulted accessing ARGS. Bomb. */
139 return NULL;
142 assert (args->option & MACH_RCV_MSG);
143 /* Disable the message-send, since it has already completed. The
144 calls we retry need only wait to receive the reply message. */
145 args->option &= ~MACH_SEND_MSG;
147 /* Limit the time to receive the reply message, in case the server
148 claimed that `interrupt_operation' succeeded but in fact the RPC
149 is hung. */
150 args->option |= MACH_RCV_TIMEOUT;
151 args->timeout = _hurd_interrupted_rpc_timeout;
153 _hurdsig_end_catch_fault ();
155 MACHINE_THREAD_STATE_SET_PC (&state->basic, &&rpc_wait_trampoline);
156 /* The reply-receiving trampoline code runs initially on the original
157 user stack. We pass it the signal stack pointer in %r5. */
158 state->basic.r5 = (int) sigsp;
159 /* After doing the message receive, the trampoline code will need to
160 update the %r28 value to be restored by sigreturn. To simplify
161 the assembly code, we pass the address of its slot in SCP to the
162 trampoline code in %r4. */
163 state->basic.r4 = (unsigned int) &scp->sc_regs[27];
164 /* Set up the arguments for the handler function in callee-saved
165 registers that we will move to the argument registers after
166 mach_msg_trap returns. */
167 state->basic.r6 = signo;
168 state->basic.r7 = sigcode;
169 state->basic.r8 = (unsigned int) scp;
171 else
173 MACHINE_THREAD_STATE_SET_PC (&state->basic, &&trampoline);
174 state->basic.r20 = (unsigned int) sigsp;
175 /* Set up the arguments for the handler function. */
176 state->basic.r26 = signo;
177 state->basic.r25 = sigcode;
178 state->basic.r24 = (unsigned int) scp;
181 /* We pass the handler function to the trampoline code in %r9. */
182 state->basic.r9 = (unsigned int) handler;
183 /* For convenience, we pass the address of __sigreturn in %r10. */
184 state->basic.r10 = (unsigned int) &__sigreturn;
185 /* The extra copy of SCP for the __sigreturn arg goes in %r8. */
186 state->basic.r10 = (unsigned int) scp;
188 return scp;
190 /* The trampoline code follows. This is not actually executed as part of
191 this function, it is just convenient to write it that way. */
193 rpc_wait_trampoline:
194 /* This is the entry point when we have an RPC reply message to receive
195 before running the handler. The MACH_MSG_SEND bit has already been
196 cleared in the OPTION argument on our stack. The interrupted user
197 stack pointer has not been changed, so the system call can find its
198 arguments; the signal stack pointer is in %ebx. For our convenience,
199 %ecx points to the sc_eax member of the sigcontext. */
200 asm volatile
201 (/* Retry the interrupted mach_msg system call. */
202 "ldil L%0xC0000000,%r1\nble 4(%sr7,%r1)\n"
203 "ldi -25, %r22\n" /* mach_msg_trap */
204 /* When the sigcontext was saved, %r28 was MACH_RCV_INTERRUPTED. But
205 now the message receive has completed and the original caller of
206 the RPC (i.e. the code running when the signal arrived) needs to
207 see the final return value of the message receive in %r28. So
208 store the new %r28 value into the sc_regs[27] member of the sigcontext
209 (whose address is in %r4 to make this code simpler). */
210 "stw (%r4), %r28\n"
211 /* Switch to the signal stack. */
212 "copy %r5, %r30\n"
213 /* Copy the handler arguments to the argument registers. */
214 "copy %r6, %r26\n"
215 "copy %r7, %r25\n"
216 "copy %r8, %r24\n"
219 trampoline:
220 /* Entry point for running the handler normally. The arguments to the
221 handler function are already in the argument registers. */
222 asm volatile
223 ("bv (%r9); nop" /* Call the handler function. */
224 "bv (%r10)\n" /* Call __sigreturn (SCP); never returns. */
225 "copy %r8, %r26" /* Set up arg in delay slot. */
226 : : "i" (&__sigreturn));
228 /* NOTREACHED */
229 return NULL;