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