(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / mach / hurd / powerpc / trampoline.c
blob4e5d67525886dc6c6bb1c32f04d75700a5bdbf40
1 /* Set thread_state for sighandler, and sigcontext to recover. For PowerPC.
2 Copyright (C) 1994,95,96,97,98,99,2001 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 <hurd/userlink.h>
22 #include "thread_state.h"
23 #include <assert.h>
24 #include <errno.h>
25 #include "hurdfault.h"
26 #include "intr-msg.h"
28 struct sigcontext *
29 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
30 int signo, struct hurd_signal_detail *detail,
31 volatile int rpc_wait,
32 struct machine_thread_all_state *state)
34 void trampoline (void);
35 void rpc_wait_trampoline (void);
36 void *volatile sigsp;
37 struct sigcontext *scp;
39 if (ss->context)
41 /* We have a previous sigcontext that sigreturn was about
42 to restore when another signal arrived. We will just base
43 our setup on that. */
44 if (! _hurdsig_catch_memory_fault (ss->context))
46 memcpy (&state->basic, &ss->context->sc_ppc_thread_state,
47 sizeof (state->basic));
48 memcpy (&state->exc, &ss->context->sc_ppc_exc_state,
49 sizeof (state->exc));
50 memcpy (&state->fpu, &ss->context->sc_ppc_float_state,
51 sizeof (state->fpu));
52 state->set = (1 << PPC_THREAD_STATE) | (1 << PPC_EXCEPTION_STATE)
53 | (1 << PPC_FLOAT_STATE);
57 if (! machine_get_basic_state (ss->thread, state))
58 return NULL;
60 if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
61 !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
63 sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
64 ss->sigaltstack.ss_flags |= SS_ONSTACK;
65 /* XXX need to set up base of new stack for
66 per-thread variables, cthreads. */
68 else
69 sigsp = (char *) state->basic.SP;
71 /* Set up the sigcontext structure on the stack. This is all the stack
72 needs, since the args are passed in registers (below). */
73 sigsp -= sizeof (*scp);
74 scp = sigsp;
75 sigsp -= 16; /* Reserve some space for a stack frame. */
77 if (_hurdsig_catch_memory_fault (scp))
79 /* We got a fault trying to write the stack frame.
80 We cannot set up the signal handler.
81 Returning NULL tells our caller, who will nuke us with a SIGILL. */
82 return NULL;
84 else
86 int ok;
88 /* Set up the sigcontext from the current state of the thread. */
90 scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
92 /* struct sigcontext is laid out so that starting at sc_srr0
93 mimics a struct ppc_thread_state. */
94 memcpy (&scp->sc_ppc_thread_state,
95 &state->basic, sizeof (state->basic));
97 /* struct sigcontext is laid out so that starting at sc_dar
98 mimics a struct ppc_exc_state. */
99 ok = machine_get_state (ss->thread, state, PPC_EXCEPTION_STATE,
100 &state->exc, &scp->sc_ppc_exc_state,
101 sizeof (state->exc));
103 /* struct sigcontext is laid out so that starting at sc_fprs[0]
104 mimics a struct ppc_float_state. */
105 if (ok)
106 ok = machine_get_state (ss->thread, state, PPC_FLOAT_STATE,
107 &state->fpu, &scp->sc_ppc_float_state,
108 sizeof (state->fpu));
110 _hurdsig_end_catch_fault ();
112 if (!ok)
113 return NULL;
116 /* Modify the thread state to call the trampoline code on the new stack. */
117 if (rpc_wait)
119 /* The signalee thread was blocked in a mach_msg_trap system call,
120 still waiting for a reply. We will have it run the special
121 trampoline code which retries the message receive before running
122 the signal handler.
124 To do this we change the OPTION argument in its registers to
125 enable only message reception, since the request message has
126 already been sent. */
128 /* The system call arguments are stored in consecutive registers
129 starting with r3. */
130 struct mach_msg_trap_args *args = (void *) &state->basic.r3;
132 if (_hurdsig_catch_memory_fault (args))
134 /* Faulted accessing ARGS. Bomb. */
135 return NULL;
138 assert (args->option & MACH_RCV_MSG);
139 /* Disable the message-send, since it has already completed. The
140 calls we retry need only wait to receive the reply message. */
141 args->option &= ~MACH_SEND_MSG;
143 /* Limit the time to receive the reply message, in case the server
144 claimed that `interrupt_operation' succeeded but in fact the RPC
145 is hung. */
146 args->option |= MACH_RCV_TIMEOUT;
147 args->timeout = _hurd_interrupted_rpc_timeout;
149 _hurdsig_end_catch_fault ();
151 state->basic.PC = (int) rpc_wait_trampoline;
152 /* After doing the message receive, the trampoline code will need to
153 update the r3 value to be restored by sigreturn. To simplify
154 the assembly code, we pass the address of its slot in SCP to the
155 trampoline code in r10. */
156 state->basic.r10 = (long int) &scp->sc_gprs[3];
157 /* We must preserve the mach_msg_trap args in r3..r9.
158 Pass the handler args to the trampoline code in r11..r13. */
159 state->basic.r11 = signo;
160 state->basic.r12 = detail->code;
161 state->basic.r13 = (int) scp;
163 else
165 state->basic.PC = (int) trampoline;
166 state->basic.r3 = signo;
167 state->basic.r4 = detail->code;
168 state->basic.r5 = (int) scp;
171 state->basic.r1 = (int) sigsp; /* r1 is the stack pointer. */
173 /* We pass the handler function to the trampoline code in ctr. */
174 state->basic.ctr = (int) handler;
175 /* In r15, we store the address of __sigreturn itself,
176 for the trampoline code to use. */
177 state->basic.r15 = (int) &__sigreturn;
178 /* In r16, we save the SCP value to pass to __sigreturn
179 after the handler returns. */
180 state->basic.r16 = (int) scp;
182 /* In r3, we store a pointer to the registers in STATE so that the
183 trampoline code can load the registers from that. For some reason,
184 thread_set_state doesn't set all registers. */
185 state->basic.r17 = state->basic.r3; /* Store the real r3 in r17. */
186 state->basic.r3 = (int) &state->basic.r0;
188 return scp;
191 /* The trampoline code follows. This used to be located inside
192 _hurd_setup_sighandler, but was optimized away by gcc 2.95. */
194 /* This function sets some registers which the trampoline code uses
195 and which are not automatically set by thread_set_state.
196 In r3 we have a pointer to the registers in STATE. */
197 asm ("trampoline_load_registers:\n"
198 "lwz 17,68(3)\n" /* The real r3. */
199 "lwz 4,16(3)\n"
200 "lwz 5,20(3)\n"
201 "lwz 6,24(3)\n"
202 "lwz 7,28(3)\n"
203 "lwz 8,32(3)\n"
204 "lwz 9,36(3)\n"
205 "lwz 10,40(3)\n"
206 "lwz 11,44(3)\n"
207 "lwz 12,48(3)\n"
208 "lwz 13,52(3)\n"
209 "lwz 14,56(3)\n"
210 "lwz 15,60(3)\n"
211 "lwz 16,64(3)\n"
212 "mr 3,17\n"
213 "blr\n");
215 asm ("rpc_wait_trampoline:\n");
216 /* This is the entry point when we have an RPC reply message to receive
217 before running the handler. The MACH_MSG_SEND bit has already been
218 cleared in the OPTION argument in our registers. For our convenience,
219 r10 points to the sc_regs[3] member of the sigcontext (saved r3). */
221 asm (/* Retry the interrupted mach_msg system call. */
222 "bl trampoline_load_registers\n"
223 "li 0, -25\n" /* mach_msg_trap */
224 "sc\n"
225 /* When the sigcontext was saved, r3 was MACH_RCV_INTERRUPTED. But
226 now the message receive has completed and the original caller of
227 the RPC (i.e. the code running when the signal arrived) needs to
228 see the final return value of the message receive in r3. So
229 store the new r3 value into the sc_regs[3] member of the sigcontext
230 (whose address is in r10 to make this code simpler). */
231 "stw 3, 0(10)\n"
232 /* Since the argument registers needed to have the mach_msg_trap
233 arguments, we've stored the arguments to the handler function
234 in registers r11..r13 of the state structure. */
235 "mr 3,11\n"
236 "mr 4,12\n"
237 "mr 5,13\n");
239 asm ("trampoline:\n");
240 /* Entry point for running the handler normally. The arguments to the
241 handler function are already in the standard registers:
243 r3 SIGNO
244 r4 SIGCODE
245 r5 SCP
247 r16 also contains SCP; this value is callee-saved (and so should not get
248 clobbered by running the handler). We use this saved value to pass to
249 __sigreturn, so the handler can clobber the argument registers if it
250 likes. */
251 asm ("bl trampoline_load_registers\n"
252 "bctrl\n" /* Call the handler function. */
253 "mtctr 15\n" /* Copy &__sigreturn to CTR. */
254 "mr 3,16\n" /* Copy the saved SCP to r3. */
255 "bctr\n" /* Call __sigreturn (SCP). */