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