2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / powerpc / trampoline.c
blob4e1ea4b9d99ec8b084e21ff3fb4e76f65fb654cb
1 /* Set thread_state for sighandler, and sigcontext to recover. For PowerPC.
2 Copyright (C) 1994,1995,1996,1997,1998,1999,2001,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <hurd/signal.h>
22 #include <hurd/userlink.h>
23 #include <thread_state.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include "hurdfault.h"
27 #include <intr-msg.h>
29 struct sigcontext *
30 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
31 int signo, struct hurd_signal_detail *detail,
32 volatile int rpc_wait,
33 struct machine_thread_all_state *state)
35 void trampoline (void);
36 void rpc_wait_trampoline (void);
37 void *volatile sigsp;
38 struct sigcontext *scp;
40 if (ss->context)
42 /* We have a previous sigcontext that sigreturn was about
43 to restore when another signal arrived. We will just base
44 our setup on that. */
45 if (! _hurdsig_catch_memory_fault (ss->context))
47 memcpy (&state->basic, &ss->context->sc_ppc_thread_state,
48 sizeof (state->basic));
49 memcpy (&state->exc, &ss->context->sc_ppc_exc_state,
50 sizeof (state->exc));
51 memcpy (&state->fpu, &ss->context->sc_ppc_float_state,
52 sizeof (state->fpu));
53 state->set = (1 << PPC_THREAD_STATE) | (1 << PPC_EXCEPTION_STATE)
54 | (1 << PPC_FLOAT_STATE);
58 if (! machine_get_basic_state (ss->thread, state))
59 return NULL;
61 if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
62 !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
64 sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
65 ss->sigaltstack.ss_flags |= SS_ONSTACK;
66 /* XXX need to set up base of new stack for
67 per-thread variables, cthreads. */
69 else
70 sigsp = (char *) state->basic.SP;
72 /* Set up the sigcontext structure on the stack. This is all the stack
73 needs, since the args are passed in registers (below). */
74 sigsp -= sizeof (*scp);
75 scp = sigsp;
76 sigsp -= 16; /* Reserve some space for a stack frame. */
78 if (_hurdsig_catch_memory_fault (scp))
80 /* We got a fault trying to write the stack frame.
81 We cannot set up the signal handler.
82 Returning NULL tells our caller, who will nuke us with a SIGILL. */
83 return NULL;
85 else
87 int ok;
89 /* Set up the sigcontext from the current state of the thread. */
91 scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
93 /* struct sigcontext is laid out so that starting at sc_srr0
94 mimics a struct ppc_thread_state. */
95 memcpy (&scp->sc_ppc_thread_state,
96 &state->basic, sizeof (state->basic));
98 /* struct sigcontext is laid out so that starting at sc_dar
99 mimics a struct ppc_exc_state. */
100 ok = machine_get_state (ss->thread, state, PPC_EXCEPTION_STATE,
101 &state->exc, &scp->sc_ppc_exc_state,
102 sizeof (state->exc));
104 /* struct sigcontext is laid out so that starting at sc_fprs[0]
105 mimics a struct ppc_float_state. */
106 if (ok)
107 ok = machine_get_state (ss->thread, state, PPC_FLOAT_STATE,
108 &state->fpu, &scp->sc_ppc_float_state,
109 sizeof (state->fpu));
111 _hurdsig_end_catch_fault ();
113 if (!ok)
114 return NULL;
117 /* Modify the thread state to call the trampoline code on the new stack. */
118 if (rpc_wait)
120 /* The signalee thread was blocked in a mach_msg_trap system call,
121 still waiting for a reply. We will have it run the special
122 trampoline code which retries the message receive before running
123 the signal handler.
125 To do this we change the OPTION argument in its registers to
126 enable only message reception, since the request message has
127 already been sent. */
129 /* The system call arguments are stored in consecutive registers
130 starting with r3. */
131 struct mach_msg_trap_args *args = (void *) &state->basic.r3;
133 if (_hurdsig_catch_memory_fault (args))
135 /* Faulted accessing ARGS. Bomb. */
136 return NULL;
139 assert (args->option & MACH_RCV_MSG);
140 /* Disable the message-send, since it has already completed. The
141 calls we retry need only wait to receive the reply message. */
142 args->option &= ~MACH_SEND_MSG;
144 /* Limit the time to receive the reply message, in case the server
145 claimed that `interrupt_operation' succeeded but in fact the RPC
146 is hung. */
147 args->option |= MACH_RCV_TIMEOUT;
148 args->timeout = _hurd_interrupted_rpc_timeout;
150 _hurdsig_end_catch_fault ();
152 state->basic.PC = (int) rpc_wait_trampoline;
153 /* After doing the message receive, the trampoline code will need to
154 update the r3 value to be restored by sigreturn. To simplify
155 the assembly code, we pass the address of its slot in SCP to the
156 trampoline code in r10. */
157 state->basic.r10 = (long int) &scp->sc_gprs[3];
158 /* We must preserve the mach_msg_trap args in r3..r9.
159 Pass the handler args to the trampoline code in r11..r13. */
160 state->basic.r11 = signo;
161 state->basic.r12 = detail->code;
162 state->basic.r13 = (int) scp;
164 else
166 state->basic.PC = (int) trampoline;
167 state->basic.r3 = signo;
168 state->basic.r4 = detail->code;
169 state->basic.r5 = (int) scp;
172 state->basic.r1 = (int) sigsp; /* r1 is the stack pointer. */
174 /* We pass the handler function to the trampoline code in ctr. */
175 state->basic.ctr = (int) handler;
176 /* In r15, we store the address of __sigreturn itself,
177 for the trampoline code to use. */
178 state->basic.r15 = (int) &__sigreturn;
179 /* In r16, we save the SCP value to pass to __sigreturn
180 after the handler returns. */
181 state->basic.r16 = (int) scp;
183 /* In r3, we store a pointer to the registers in STATE so that the
184 trampoline code can load the registers from that. For some reason,
185 thread_set_state doesn't set all registers. */
186 state->basic.r17 = state->basic.r3; /* Store the real r3 in r17. */
187 state->basic.r3 = (int) &state->basic.r0;
189 return scp;
192 /* The trampoline code follows. This used to be located inside
193 _hurd_setup_sighandler, but was optimized away by gcc 2.95. */
195 /* This function sets some registers which the trampoline code uses
196 and which are not automatically set by thread_set_state.
197 In r3 we have a pointer to the registers in STATE. */
198 asm ("trampoline_load_registers:\n"
199 "lwz 17,68(3)\n" /* The real r3. */
200 "lwz 4,16(3)\n"
201 "lwz 5,20(3)\n"
202 "lwz 6,24(3)\n"
203 "lwz 7,28(3)\n"
204 "lwz 8,32(3)\n"
205 "lwz 9,36(3)\n"
206 "lwz 10,40(3)\n"
207 "lwz 11,44(3)\n"
208 "lwz 12,48(3)\n"
209 "lwz 13,52(3)\n"
210 "lwz 14,56(3)\n"
211 "lwz 15,60(3)\n"
212 "lwz 16,64(3)\n"
213 "mr 3,17\n"
214 "blr\n");
216 asm ("rpc_wait_trampoline:\n");
217 /* This is the entry point when we have an RPC reply message to receive
218 before running the handler. The MACH_MSG_SEND bit has already been
219 cleared in the OPTION argument in our registers. For our convenience,
220 r10 points to the sc_regs[3] member of the sigcontext (saved r3). */
222 asm (/* Retry the interrupted mach_msg system call. */
223 "bl trampoline_load_registers\n"
224 "li 0, -25\n" /* mach_msg_trap */
225 "sc\n"
226 /* When the sigcontext was saved, r3 was MACH_RCV_INTERRUPTED. But
227 now the message receive has completed and the original caller of
228 the RPC (i.e. the code running when the signal arrived) needs to
229 see the final return value of the message receive in r3. So
230 store the new r3 value into the sc_regs[3] member of the sigcontext
231 (whose address is in r10 to make this code simpler). */
232 "stw 3, 0(10)\n"
233 /* Since the argument registers needed to have the mach_msg_trap
234 arguments, we've stored the arguments to the handler function
235 in registers r11..r13 of the state structure. */
236 "mr 3,11\n"
237 "mr 4,12\n"
238 "mr 5,13\n");
240 asm ("trampoline:\n");
241 /* Entry point for running the handler normally. The arguments to the
242 handler function are already in the standard registers:
244 r3 SIGNO
245 r4 SIGCODE
246 r5 SCP
248 r16 also contains SCP; this value is callee-saved (and so should not get
249 clobbered by running the handler). We use this saved value to pass to
250 __sigreturn, so the handler can clobber the argument registers if it
251 likes. */
252 asm ("bl trampoline_load_registers\n"
253 "bctrl\n" /* Call the handler function. */
254 "mtctr 15\n" /* Copy &__sigreturn to CTR. */
255 "mr 3,16\n" /* Copy the saved SCP to r3. */
256 "bctr\n" /* Call __sigreturn (SCP). */