1 /* Set thread_state for sighandler, and sigcontext to recover. i386 version.
2 Copyright (C) 1994-2021 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 <https://www.gnu.org/licenses/>. */
19 #include <hurd/signal.h>
20 #include <hurd/userlink.h>
21 #include <thread_state.h>
22 #include <mach/exception.h>
23 #include <mach/machine/eflags.h>
26 #include "hurdfault.h"
28 #include <sys/ucontext.h>
31 /* Fill in a siginfo_t structure for SA_SIGINFO-enabled handlers. */
32 static void fill_siginfo (siginfo_t
*si
, int signo
,
33 const struct hurd_signal_detail
*detail
,
34 const struct machine_thread_all_state
*state
)
37 si
->si_errno
= detail
->error
;
38 si
->si_code
= detail
->code
;
40 /* XXX We would need a protocol change for sig_post to include
41 * this information. */
45 /* Address of the faulting instruction or memory access. */
46 if (detail
->exc
== EXC_BAD_ACCESS
)
47 si
->si_addr
= (void *) detail
->exc_subcode
;
49 si
->si_addr
= (void *) state
->basic
.eip
;
51 /* XXX On SIGCHLD, this should be the exit status of the child
52 * process. We would need a protocol change for the proc server
53 * to send this information along with the signal. */
56 si
->si_band
= 0; /* SIGPOLL is not supported yet. */
57 si
->si_value
.sival_int
= 0; /* sigqueue() is not supported yet. */
60 /* Fill in a ucontext_t structure SA_SIGINFO-enabled handlers. */
61 static void fill_ucontext (ucontext_t
*uc
, const struct sigcontext
*sc
)
65 uc
->uc_sigmask
= sc
->sc_mask
;
66 uc
->uc_stack
.ss_sp
= (__ptr_t
) sc
->sc_uesp
;
67 uc
->uc_stack
.ss_size
= 0;
68 uc
->uc_stack
.ss_flags
= 0;
71 memcpy (&uc
->uc_mcontext
.gregs
[REG_GS
], &sc
->sc_gs
,
72 (REG_TRAPNO
- REG_GS
) * sizeof (int));
73 uc
->uc_mcontext
.gregs
[REG_TRAPNO
] = 0;
74 uc
->uc_mcontext
.gregs
[REG_ERR
] = 0;
75 memcpy (&uc
->uc_mcontext
.gregs
[REG_EIP
], &sc
->sc_eip
,
76 (NGREG
- REG_EIP
) * sizeof (int));
79 memset (&uc
->uc_mcontext
.fpregs
, 0, sizeof (fpregset_t
));
83 _hurd_setup_sighandler (struct hurd_sigstate
*ss
, const struct sigaction
*action
,
84 __sighandler_t handler
,
85 int signo
, struct hurd_signal_detail
*detail
,
86 volatile int rpc_wait
,
87 struct machine_thread_all_state
*state
)
89 void trampoline (void);
90 void rpc_wait_trampoline (void);
92 extern const void _hurd_intr_rpc_msg_cx_sp
;
93 extern const void _hurd_intr_rpc_msg_sp_restored
;
95 struct sigcontext
*scp
;
101 /* Extra arguments for traditional signal handlers */
105 struct sigcontext
*scp
; /* Points to ctx, below. */
108 /* Extra arguments for SA_SIGINFO handlers */
111 siginfo_t
*siginfop
; /* Points to siginfo, below. */
112 ucontext_t
*uctxp
; /* Points to uctx, below. */
115 void *sigreturn_addr
;
116 void *sigreturn_returns_here
;
117 struct sigcontext
*return_scp
; /* Same; arg to sigreturn. */
119 /* NB: sigreturn assumes link is next to ctx. */
120 struct sigcontext ctx
;
121 struct hurd_userlink link
;
128 /* We have a previous sigcontext that sigreturn was about
129 to restore when another signal arrived. We will just base
130 our setup on that. */
131 if (! _hurdsig_catch_memory_fault (ss
->context
))
133 memcpy (&state
->basic
, &ss
->context
->sc_i386_thread_state
,
134 sizeof (state
->basic
));
135 memcpy (&state
->fpu
, &ss
->context
->sc_i386_float_state
,
136 sizeof (state
->fpu
));
137 state
->set
|= (1 << i386_REGS_SEGS_STATE
) | (1 << i386_FLOAT_STATE
);
141 if (! machine_get_basic_state (ss
->thread
, state
))
144 /* Save the original SP in the gratuitous `esp' slot.
145 We may need to reset the SP (the `uesp' slot) to avoid clobbering an
146 interrupted RPC frame. */
147 state
->basic
.esp
= state
->basic
.uesp
;
149 /* This code has intimate knowledge of the special mach_msg system call
150 done in intr-msg.c; that code does (see intr-msg.h):
153 _hurd_intr_rpc_msg_cx_sp: movl $-25, %eax
154 _hurd_intr_rpc_msg_do_trap: lcall $7, $0
155 _hurd_intr_rpc_msg_in_trap: movl %ecx, %esp
156 _hurd_intr_rpc_msg_sp_restored:
157 We must check for the window during which %esp points at the
158 mach_msg arguments. The space below until %ecx is used by
159 the _hurd_intr_rpc_mach_msg frame, and must not be clobbered. */
160 if (state
->basic
.eip
>= (int) &_hurd_intr_rpc_msg_cx_sp
161 && state
->basic
.eip
< (int) &_hurd_intr_rpc_msg_sp_restored
)
162 /* The SP now points at the mach_msg args, but there is more stack
163 space used below it. The real SP is saved in %ecx; we must push the
164 new frame below there (if not on the altstack), and restore that value as
165 the SP on sigreturn. */
166 state
->basic
.uesp
= state
->basic
.ecx
;
168 if ((action
->sa_flags
& SA_ONSTACK
)
169 && !(ss
->sigaltstack
.ss_flags
& (SS_DISABLE
|SS_ONSTACK
)))
171 sigsp
= ss
->sigaltstack
.ss_sp
+ ss
->sigaltstack
.ss_size
;
172 ss
->sigaltstack
.ss_flags
|= SS_ONSTACK
;
175 sigsp
= (char *) state
->basic
.uesp
;
177 /* Push the arguments to call `trampoline' on the stack. */
178 sigsp
-= sizeof (*stackframe
);
181 if (_hurdsig_catch_memory_fault (stackframe
))
183 /* We got a fault trying to write the stack frame.
184 We cannot set up the signal handler.
185 Returning NULL tells our caller, who will nuke us with a SIGILL. */
192 extern void _hurdsig_longjmp_from_handler (void *, jmp_buf, int);
194 /* Add a link to the thread's active-resources list. We mark this as
195 the only user of the "resource", so the cleanup function will be
196 called by any longjmp which is unwinding past the signal frame.
197 The cleanup function (in sigunwind.c) will make sure that all the
198 appropriate cleanups done by sigreturn are taken care of. */
199 stackframe
->link
.cleanup
= &_hurdsig_longjmp_from_handler
;
200 stackframe
->link
.cleanup_data
= &stackframe
->ctx
;
201 stackframe
->link
.resource
.next
= NULL
;
202 stackframe
->link
.resource
.prevp
= NULL
;
203 stackframe
->link
.thread
.next
= ss
->active_resources
;
204 stackframe
->link
.thread
.prevp
= &ss
->active_resources
;
205 if (stackframe
->link
.thread
.next
)
206 stackframe
->link
.thread
.next
->thread
.prevp
207 = &stackframe
->link
.thread
.next
;
208 ss
->active_resources
= &stackframe
->link
;
210 /* Set up the sigcontext from the current state of the thread. */
212 scp
= &stackframe
->ctx
;
213 scp
->sc_onstack
= ss
->sigaltstack
.ss_flags
& SS_ONSTACK
? 1 : 0;
215 /* struct sigcontext is laid out so that starting at sc_gs mimics a
216 struct i386_thread_state. */
217 memcpy (&scp
->sc_i386_thread_state
,
218 &state
->basic
, sizeof (state
->basic
));
220 /* struct sigcontext is laid out so that starting at sc_fpkind mimics
221 a struct i386_float_state. */
222 ok
= machine_get_state (ss
->thread
, state
, i386_FLOAT_STATE
,
223 &state
->fpu
, &scp
->sc_i386_float_state
,
224 sizeof (state
->fpu
));
226 /* Set up the arguments for the signal handler. */
227 stackframe
->signo
= signo
;
228 if (action
->sa_flags
& SA_SIGINFO
)
230 stackframe
->posix
.siginfop
= &stackframe
->siginfo
;
231 stackframe
->posix
.uctxp
= &stackframe
->ucontext
;
232 fill_siginfo (&stackframe
->siginfo
, signo
, detail
, state
);
233 fill_ucontext (&stackframe
->ucontext
, scp
);
240 _hurd_exception2signal_legacy (detail
, &nsigno
);
241 assert (nsigno
== signo
);
246 stackframe
->legacy
.sigcode
= detail
->code
;
247 stackframe
->legacy
.scp
= &stackframe
->ctx
;
250 /* Set up the bottom of the stack. */
251 stackframe
->sigreturn_addr
= &__sigreturn
;
252 stackframe
->sigreturn_returns_here
= firewall
; /* Crash on return. */
253 stackframe
->return_scp
= &stackframe
->ctx
;
255 _hurdsig_end_catch_fault ();
261 /* Modify the thread state to call the trampoline code on the new stack. */
264 /* The signalee thread was blocked in a mach_msg_trap system call,
265 still waiting for a reply. We will have it run the special
266 trampoline code which retries the message receive before running
269 To do this we change the OPTION argument on its stack to enable only
270 message reception, since the request message has already been
273 struct mach_msg_trap_args
*args
= (void *) state
->basic
.esp
;
275 if (_hurdsig_catch_memory_fault (args
))
277 /* Faulted accessing ARGS. Bomb. */
281 assert (args
->option
& MACH_RCV_MSG
);
282 /* Disable the message-send, since it has already completed. The
283 calls we retry need only wait to receive the reply message. */
284 args
->option
&= ~MACH_SEND_MSG
;
286 /* Limit the time to receive the reply message, in case the server
287 claimed that `interrupt_operation' succeeded but in fact the RPC
289 args
->option
|= MACH_RCV_TIMEOUT
;
290 args
->timeout
= _hurd_interrupted_rpc_timeout
;
292 _hurdsig_end_catch_fault ();
294 state
->basic
.eip
= (int) rpc_wait_trampoline
;
295 /* The reply-receiving trampoline code runs initially on the original
296 user stack. We pass it the signal stack pointer in %ebx. */
297 state
->basic
.uesp
= state
->basic
.esp
; /* Restore mach_msg syscall SP. */
298 state
->basic
.ebx
= (int) sigsp
;
299 /* After doing the message receive, the trampoline code will need to
300 update the %eax value to be restored by sigreturn. To simplify
301 the assembly code, we pass the address of its slot in SCP to the
302 trampoline code in %ecx. */
303 state
->basic
.ecx
= (int) &scp
->sc_eax
;
307 state
->basic
.eip
= (int) trampoline
;
308 state
->basic
.uesp
= (int) sigsp
;
310 /* We pass the handler function to the trampoline code in %edx. */
311 state
->basic
.edx
= (int) handler
;
313 /* The x86 ABI says the DF bit is clear on entry to any function. */
314 state
->basic
.efl
&= ~EFL_DF
;
319 /* The trampoline code follows. This used to be located inside
320 _hurd_setup_sighandler, but was optimized away by gcc 2.95.
322 If you modify this, update
323 - in gcc: libgcc/config/i386/gnu-unwind.h x86_gnu_fallback_frame_state,
324 - in gdb: gdb/i386-gnu-tdep.c gnu_sigtramp_code. */
326 asm ("rpc_wait_trampoline:\n");
327 /* This is the entry point when we have an RPC reply message to receive
328 before running the handler. The MACH_MSG_SEND bit has already been
329 cleared in the OPTION argument on our stack. The interrupted user
330 stack pointer has not been changed, so the system call can find its
331 arguments; the signal stack pointer is in %ebx. For our convenience,
332 %ecx points to the sc_eax member of the sigcontext. */
333 asm (/* Retry the interrupted mach_msg system call. */
334 "movl $-25, %eax\n" /* mach_msg_trap */
336 /* When the sigcontext was saved, %eax was MACH_RCV_INTERRUPTED. But
337 now the message receive has completed and the original caller of
338 the RPC (i.e. the code running when the signal arrived) needs to
339 see the final return value of the message receive in %eax. So
340 store the new %eax value into the sc_eax member of the sigcontext
341 (whose address is in %ecx to make this code simpler). */
342 "movl %eax, (%ecx)\n"
343 /* Switch to the signal stack. */
344 "movl %ebx, %esp\n");
346 asm ("trampoline:\n");
347 /* Entry point for running the handler normally. The arguments to the
348 handler function are already on the top of the stack:
354 asm ("call *%edx\n" /* Call the handler function. */
355 "addl $12, %esp\n" /* Pop its args. */
356 /* The word at the top of stack is &__sigreturn; following are a dummy
357 word to fill the slot for the address for __sigreturn to return to,
358 and a copy of SCP for __sigreturn's argument. "Return" to calling
359 __sigreturn (SCP); this call never returns. */