]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/i386/trampoline.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / mach / hurd / i386 / trampoline.c
CommitLineData
28f540f4 1/* Set thread_state for sighandler, and sigcontext to recover. i386 version.
b168057a 2 Copyright (C) 1994-2015 Free Software Foundation, Inc.
478b92f0 3 This file is part of the GNU C Library.
28f540f4 4
478b92f0 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 9
478b92f0
UD
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
41bdb6e2 13 Lesser General Public License for more details.
28f540f4 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
28f540f4
RM
18
19#include <hurd/signal.h>
e607b492 20#include <hurd/userlink.h>
8f480b4b 21#include <thread_state.h>
d2ea0b96 22#include <mach/machine/eflags.h>
28f540f4
RM
23#include <assert.h>
24#include <errno.h>
25#include "hurdfault.h"
8f480b4b 26#include <intr-msg.h>
28f540f4 27
7974fe21 28
28f540f4
RM
29struct sigcontext *
30_hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
0e3426bb 31 int signo, struct hurd_signal_detail *detail,
28f540f4
RM
32 volatile int rpc_wait,
33 struct machine_thread_all_state *state)
34{
83ddec31
RM
35 void trampoline (void);
36 void rpc_wait_trampoline (void);
37 void firewall (void);
3fe9de0d
RM
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;
28f540f4
RM
41 void *volatile sigsp;
42 struct sigcontext *scp;
7974fe21 43 struct
28f540f4
RM
44 {
45 int signo;
46 long int sigcode;
47 struct sigcontext *scp; /* Points to ctx, below. */
f0bf9cb9
RM
48 void *sigreturn_addr;
49 void *sigreturn_returns_here;
28f540f4
RM
50 struct sigcontext *return_scp; /* Same; arg to sigreturn. */
51 struct sigcontext ctx;
e607b492 52 struct hurd_userlink link;
28f540f4
RM
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. */
7974fe21 60 if (! _hurdsig_catch_memory_fault (ss->context))
28f540f4
RM
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));
421f82e5 66 state->set |= (1 << i386_THREAD_STATE) | (1 << i386_FLOAT_STATE);
28f540f4 67 }
28f540f4 68 }
421f82e5
RM
69
70 if (! machine_get_basic_state (ss->thread, state))
28f540f4
RM
71 return NULL;
72
3fe9de0d
RM
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
28f540f4 78 if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
7ce241a0 79 !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
28f540f4
RM
80 {
81 sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
7ce241a0 82 ss->sigaltstack.ss_flags |= SS_ONSTACK;
28f540f4
RM
83 /* XXX need to set up base of new stack for
84 per-thread variables, cthreads. */
85 }
3fe9de0d 86 /* This code has intimate knowledge of the special mach_msg system call
4ca84cff 87 done in intr-msg.c; that code does (see intr-msg.h):
3fe9de0d
RM
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
478b92f0 93 _hurd_intr_rpc_msg_sp_restored:
3fe9de0d
RM
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. */
7974fe21 97 else if (state->basic.eip >= (int) &_hurd_intr_rpc_msg_cx_sp &&
3fe9de0d
RM
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);
28f540f4
RM
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
7974fe21 111 if (_hurdsig_catch_memory_fault (stackframe))
28f540f4 112 {
28f540f4
RM
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
e607b492
RM
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
28f540f4
RM
140 /* Set up the arguments for the signal handler. */
141 stackframe->signo = signo;
0e3426bb 142 stackframe->sigcode = detail->code;
28f540f4 143 stackframe->scp = stackframe->return_scp = scp = &stackframe->ctx;
f0bf9cb9 144 stackframe->sigreturn_addr = &__sigreturn;
83ddec31 145 stackframe->sigreturn_returns_here = firewall; /* Crash on return. */
28f540f4
RM
146
147 /* Set up the sigcontext from the current state of the thread. */
148
7ce241a0 149 scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
28f540f4
RM
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.
7974fe21 175
28f540f4
RM
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
3fe9de0d 180 struct mach_msg_trap_args *args = (void *) state->basic.esp;
28f540f4 181
7974fe21 182 if (_hurdsig_catch_memory_fault (args))
28f540f4 183 {
28f540f4
RM
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
54da5be3
RM
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
28f540f4
RM
199 _hurdsig_end_catch_fault ();
200
83ddec31 201 state->basic.eip = (int) rpc_wait_trampoline;
28f540f4
RM
202 /* The reply-receiving trampoline code runs initially on the original
203 user stack. We pass it the signal stack pointer in %ebx. */
3fe9de0d 204 state->basic.uesp = state->basic.esp; /* Restore mach_msg syscall SP. */
28f540f4
RM
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 {
83ddec31 214 state->basic.eip = (int) trampoline;
28f540f4
RM
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
d2ea0b96
RM
220 /* The x86 ABI says the DF bit is clear on entry to any function. */
221 state->basic.efl &= ~EFL_DF;
222
28f540f4 223 return scp;
83ddec31 224}
28f540f4 225
83ddec31
RM
226/* The trampoline code follows. This used to be located inside
227 _hurd_setup_sighandler, but was optimized away by gcc 2.95. */
28f540f4 228
83ddec31 229asm ("rpc_wait_trampoline:\n");
28f540f4
RM
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. */
83ddec31 236asm (/* Retry the interrupted mach_msg system call. */
28f540f4
RM
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
83ddec31 249 asm ("trampoline:\n");
28f540f4
RM
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 */
83ddec31 257asm ("call *%edx\n" /* Call the handler function. */
f0bf9cb9
RM
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");
28f540f4 264
83ddec31
RM
265asm ("firewall:\n"
266 "hlt");