]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/i386/trampoline.c
d4a839304bc592d01b28c847d65453475dd63ebf
[thirdparty/glibc.git] / sysdeps / mach / hurd / i386 / trampoline.c
1 /* Set thread_state for sighandler, and sigcontext to recover. i386 version.
2 Copyright (C) 1994-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <hurd/signal.h>
20 #include <hurd/userlink.h>
21 #include <thread_state.h>
22 #include <mach/machine/eflags.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include "hurdfault.h"
26 #include <intr-msg.h>
27
28
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)
34 {
35 void trampoline (void);
36 void rpc_wait_trampoline (void);
37 void firewall (void);
38 extern const void _hurd_intr_rpc_msg_in_trap;
39 extern const void _hurd_intr_rpc_msg_cx_sp;
40 extern const void _hurd_intr_rpc_msg_sp_restored;
41 void *volatile sigsp;
42 struct sigcontext *scp;
43 struct
44 {
45 int signo;
46 long int sigcode;
47 struct sigcontext *scp; /* Points to ctx, below. */
48 void *sigreturn_addr;
49 void *sigreturn_returns_here;
50 struct sigcontext *return_scp; /* Same; arg to sigreturn. */
51 struct sigcontext ctx;
52 struct hurd_userlink link;
53 } *stackframe;
54
55 if (ss->context)
56 {
57 /* We have a previous sigcontext that sigreturn was about
58 to restore when another signal arrived. We will just base
59 our setup on that. */
60 if (! _hurdsig_catch_memory_fault (ss->context))
61 {
62 memcpy (&state->basic, &ss->context->sc_i386_thread_state,
63 sizeof (state->basic));
64 memcpy (&state->fpu, &ss->context->sc_i386_float_state,
65 sizeof (state->fpu));
66 state->set |= (1 << i386_THREAD_STATE) | (1 << i386_FLOAT_STATE);
67 }
68 }
69
70 if (! machine_get_basic_state (ss->thread, state))
71 return NULL;
72
73 /* Save the original SP in the gratuitous `esp' slot.
74 We may need to reset the SP (the `uesp' slot) to avoid clobbering an
75 interrupted RPC frame. */
76 state->basic.esp = state->basic.uesp;
77
78 if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
79 !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
80 {
81 sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
82 ss->sigaltstack.ss_flags |= SS_ONSTACK;
83 /* XXX need to set up base of new stack for
84 per-thread variables, cthreads. */
85 }
86 /* This code has intimate knowledge of the special mach_msg system call
87 done in intr-msg.c; that code does (see intr-msg.h):
88 movl %esp, %ecx
89 leal ARGS, %esp
90 _hurd_intr_rpc_msg_cx_sp: movl $-25, %eax
91 _hurd_intr_rpc_msg_do_trap: lcall $7, $0
92 _hurd_intr_rpc_msg_in_trap: movl %ecx, %esp
93 _hurd_intr_rpc_msg_sp_restored:
94 We must check for the window during which %esp points at the
95 mach_msg arguments. The space below until %ecx is used by
96 the _hurd_intr_rpc_mach_msg frame, and must not be clobbered. */
97 else if (state->basic.eip >= (int) &_hurd_intr_rpc_msg_cx_sp &&
98 state->basic.eip < (int) &_hurd_intr_rpc_msg_sp_restored)
99 /* The SP now points at the mach_msg args, but there is more stack
100 space used below it. The real SP is saved in %ecx; we must push the
101 new frame below there, and restore that value as the SP on
102 sigreturn. */
103 sigsp = (char *) (state->basic.uesp = state->basic.ecx);
104 else
105 sigsp = (char *) state->basic.uesp;
106
107 /* Push the arguments to call `trampoline' on the stack. */
108 sigsp -= sizeof (*stackframe);
109 stackframe = sigsp;
110
111 if (_hurdsig_catch_memory_fault (stackframe))
112 {
113 /* We got a fault trying to write the stack frame.
114 We cannot set up the signal handler.
115 Returning NULL tells our caller, who will nuke us with a SIGILL. */
116 return NULL;
117 }
118 else
119 {
120 int ok;
121
122 extern void _hurdsig_longjmp_from_handler (void *, jmp_buf, int);
123
124 /* Add a link to the thread's active-resources list. We mark this as
125 the only user of the "resource", so the cleanup function will be
126 called by any longjmp which is unwinding past the signal frame.
127 The cleanup function (in sigunwind.c) will make sure that all the
128 appropriate cleanups done by sigreturn are taken care of. */
129 stackframe->link.cleanup = &_hurdsig_longjmp_from_handler;
130 stackframe->link.cleanup_data = &stackframe->ctx;
131 stackframe->link.resource.next = NULL;
132 stackframe->link.resource.prevp = NULL;
133 stackframe->link.thread.next = ss->active_resources;
134 stackframe->link.thread.prevp = &ss->active_resources;
135 if (stackframe->link.thread.next)
136 stackframe->link.thread.next->thread.prevp
137 = &stackframe->link.thread.next;
138 ss->active_resources = &stackframe->link;
139
140 /* Set up the arguments for the signal handler. */
141 stackframe->signo = signo;
142 stackframe->sigcode = detail->code;
143 stackframe->scp = stackframe->return_scp = scp = &stackframe->ctx;
144 stackframe->sigreturn_addr = &__sigreturn;
145 stackframe->sigreturn_returns_here = firewall; /* Crash on return. */
146
147 /* Set up the sigcontext from the current state of the thread. */
148
149 scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
150
151 /* struct sigcontext is laid out so that starting at sc_gs mimics a
152 struct i386_thread_state. */
153 memcpy (&scp->sc_i386_thread_state,
154 &state->basic, sizeof (state->basic));
155
156 /* struct sigcontext is laid out so that starting at sc_fpkind mimics
157 a struct i386_float_state. */
158 ok = machine_get_state (ss->thread, state, i386_FLOAT_STATE,
159 &state->fpu, &scp->sc_i386_float_state,
160 sizeof (state->fpu));
161
162 _hurdsig_end_catch_fault ();
163
164 if (! ok)
165 return NULL;
166 }
167
168 /* Modify the thread state to call the trampoline code on the new stack. */
169 if (rpc_wait)
170 {
171 /* The signalee thread was blocked in a mach_msg_trap system call,
172 still waiting for a reply. We will have it run the special
173 trampoline code which retries the message receive before running
174 the signal handler.
175
176 To do this we change the OPTION argument on its stack to enable only
177 message reception, since the request message has already been
178 sent. */
179
180 struct mach_msg_trap_args *args = (void *) state->basic.esp;
181
182 if (_hurdsig_catch_memory_fault (args))
183 {
184 /* Faulted accessing ARGS. Bomb. */
185 return NULL;
186 }
187
188 assert (args->option & MACH_RCV_MSG);
189 /* Disable the message-send, since it has already completed. The
190 calls we retry need only wait to receive the reply message. */
191 args->option &= ~MACH_SEND_MSG;
192
193 /* Limit the time to receive the reply message, in case the server
194 claimed that `interrupt_operation' succeeded but in fact the RPC
195 is hung. */
196 args->option |= MACH_RCV_TIMEOUT;
197 args->timeout = _hurd_interrupted_rpc_timeout;
198
199 _hurdsig_end_catch_fault ();
200
201 state->basic.eip = (int) rpc_wait_trampoline;
202 /* The reply-receiving trampoline code runs initially on the original
203 user stack. We pass it the signal stack pointer in %ebx. */
204 state->basic.uesp = state->basic.esp; /* Restore mach_msg syscall SP. */
205 state->basic.ebx = (int) sigsp;
206 /* After doing the message receive, the trampoline code will need to
207 update the %eax value to be restored by sigreturn. To simplify
208 the assembly code, we pass the address of its slot in SCP to the
209 trampoline code in %ecx. */
210 state->basic.ecx = (int) &scp->sc_eax;
211 }
212 else
213 {
214 state->basic.eip = (int) trampoline;
215 state->basic.uesp = (int) sigsp;
216 }
217 /* We pass the handler function to the trampoline code in %edx. */
218 state->basic.edx = (int) handler;
219
220 /* The x86 ABI says the DF bit is clear on entry to any function. */
221 state->basic.efl &= ~EFL_DF;
222
223 return scp;
224 }
225
226 /* The trampoline code follows. This used to be located inside
227 _hurd_setup_sighandler, but was optimized away by gcc 2.95. */
228
229 asm ("rpc_wait_trampoline:\n");
230 /* This is the entry point when we have an RPC reply message to receive
231 before running the handler. The MACH_MSG_SEND bit has already been
232 cleared in the OPTION argument on our stack. The interrupted user
233 stack pointer has not been changed, so the system call can find its
234 arguments; the signal stack pointer is in %ebx. For our convenience,
235 %ecx points to the sc_eax member of the sigcontext. */
236 asm (/* Retry the interrupted mach_msg system call. */
237 "movl $-25, %eax\n" /* mach_msg_trap */
238 "lcall $7, $0\n"
239 /* When the sigcontext was saved, %eax was MACH_RCV_INTERRUPTED. But
240 now the message receive has completed and the original caller of
241 the RPC (i.e. the code running when the signal arrived) needs to
242 see the final return value of the message receive in %eax. So
243 store the new %eax value into the sc_eax member of the sigcontext
244 (whose address is in %ecx to make this code simpler). */
245 "movl %eax, (%ecx)\n"
246 /* Switch to the signal stack. */
247 "movl %ebx, %esp\n");
248
249 asm ("trampoline:\n");
250 /* Entry point for running the handler normally. The arguments to the
251 handler function are already on the top of the stack:
252
253 0(%esp) SIGNO
254 4(%esp) SIGCODE
255 8(%esp) SCP
256 */
257 asm ("call *%edx\n" /* Call the handler function. */
258 "addl $12, %esp\n" /* Pop its args. */
259 /* The word at the top of stack is &__sigreturn; following are a dummy
260 word to fill the slot for the address for __sigreturn to return to,
261 and a copy of SCP for __sigreturn's argument. "Return" to calling
262 __sigreturn (SCP); this call never returns. */
263 "ret");
264
265 asm ("firewall:\n"
266 "hlt");