]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/i386/trampoline.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysdeps / mach / hurd / i386 / trampoline.c
CommitLineData
28f540f4 1/* Set thread_state for sighandler, and sigcontext to recover. i386 version.
04277e02 2 Copyright (C) 1994-2019 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 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://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));
f8baf2a2 65 state->set |= (1 << i386_REGS_SEGS_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
aa0e4663
JM
77 if ((ss->actions[signo].sa_flags & SA_ONSTACK)
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 82 }
3fe9de0d 83 /* This code has intimate knowledge of the special mach_msg system call
4ca84cff 84 done in intr-msg.c; that code does (see intr-msg.h):
3fe9de0d
RM
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
478b92f0 90 _hurd_intr_rpc_msg_sp_restored:
3fe9de0d
RM
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. */
aa0e4663
JM
94 else if (state->basic.eip >= (int) &_hurd_intr_rpc_msg_cx_sp
95 && state->basic.eip < (int) &_hurd_intr_rpc_msg_sp_restored)
3fe9de0d
RM
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);
28f540f4
RM
101 else
102 sigsp = (char *) state->basic.uesp;
103
104 /* Push the arguments to call `trampoline' on the stack. */
105 sigsp -= sizeof (*stackframe);
106 stackframe = sigsp;
107
7974fe21 108 if (_hurdsig_catch_memory_fault (stackframe))
28f540f4 109 {
28f540f4
RM
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;
114 }
115 else
116 {
117 int ok;
118
e607b492
RM
119 extern void _hurdsig_longjmp_from_handler (void *, jmp_buf, int);
120
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;
136
28f540f4
RM
137 /* Set up the arguments for the signal handler. */
138 stackframe->signo = signo;
0e3426bb 139 stackframe->sigcode = detail->code;
28f540f4 140 stackframe->scp = stackframe->return_scp = scp = &stackframe->ctx;
f0bf9cb9 141 stackframe->sigreturn_addr = &__sigreturn;
83ddec31 142 stackframe->sigreturn_returns_here = firewall; /* Crash on return. */
28f540f4
RM
143
144 /* Set up the sigcontext from the current state of the thread. */
145
7ce241a0 146 scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
28f540f4
RM
147
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));
152
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));
158
159 _hurdsig_end_catch_fault ();
160
161 if (! ok)
162 return NULL;
163 }
164
165 /* Modify the thread state to call the trampoline code on the new stack. */
166 if (rpc_wait)
167 {
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.
7974fe21 172
28f540f4
RM
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. */
176
3fe9de0d 177 struct mach_msg_trap_args *args = (void *) state->basic.esp;
28f540f4 178
7974fe21 179 if (_hurdsig_catch_memory_fault (args))
28f540f4 180 {
28f540f4
RM
181 /* Faulted accessing ARGS. Bomb. */
182 return NULL;
183 }
184
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;
189
54da5be3
RM
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;
195
28f540f4
RM
196 _hurdsig_end_catch_fault ();
197
83ddec31 198 state->basic.eip = (int) rpc_wait_trampoline;
28f540f4
RM
199 /* The reply-receiving trampoline code runs initially on the original
200 user stack. We pass it the signal stack pointer in %ebx. */
3fe9de0d 201 state->basic.uesp = state->basic.esp; /* Restore mach_msg syscall SP. */
28f540f4
RM
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;
208 }
209 else
210 {
83ddec31 211 state->basic.eip = (int) trampoline;
28f540f4
RM
212 state->basic.uesp = (int) sigsp;
213 }
214 /* We pass the handler function to the trampoline code in %edx. */
215 state->basic.edx = (int) handler;
216
d2ea0b96
RM
217 /* The x86 ABI says the DF bit is clear on entry to any function. */
218 state->basic.efl &= ~EFL_DF;
219
28f540f4 220 return scp;
83ddec31 221}
28f540f4 222
83ddec31
RM
223/* The trampoline code follows. This used to be located inside
224 _hurd_setup_sighandler, but was optimized away by gcc 2.95. */
28f540f4 225
83ddec31 226asm ("rpc_wait_trampoline:\n");
28f540f4
RM
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. */
83ddec31 233asm (/* Retry the interrupted mach_msg system call. */
28f540f4
RM
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");
245
83ddec31 246 asm ("trampoline:\n");
28f540f4
RM
247 /* Entry point for running the handler normally. The arguments to the
248 handler function are already on the top of the stack:
249
250 0(%esp) SIGNO
251 4(%esp) SIGCODE
252 8(%esp) SCP
253 */
83ddec31 254asm ("call *%edx\n" /* Call the handler function. */
f0bf9cb9
RM
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");
28f540f4 261
83ddec31
RM
262asm ("firewall:\n"
263 "hlt");