1 /* Set thread_state for sighandler, and sigcontext to recover. i386 version.
2 Copyright (C) 1994,1995,1996,1997,1998,1999,2005,2008
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <hurd/signal.h>
22 #include <hurd/userlink.h>
23 #include <thread_state.h>
24 #include <mach/machine/eflags.h>
27 #include "hurdfault.h"
32 _hurd_setup_sighandler (struct hurd_sigstate
*ss
, __sighandler_t handler
,
33 int signo
, struct hurd_signal_detail
*detail
,
34 volatile int rpc_wait
,
35 struct machine_thread_all_state
*state
)
37 void trampoline (void);
38 void rpc_wait_trampoline (void);
40 extern const void _hurd_intr_rpc_msg_in_trap
;
41 extern const void _hurd_intr_rpc_msg_cx_sp
;
42 extern const void _hurd_intr_rpc_msg_sp_restored
;
44 struct sigcontext
*scp
;
49 struct sigcontext
*scp
; /* Points to ctx, below. */
51 void *sigreturn_returns_here
;
52 struct sigcontext
*return_scp
; /* Same; arg to sigreturn. */
53 struct sigcontext ctx
;
54 struct hurd_userlink link
;
59 /* We have a previous sigcontext that sigreturn was about
60 to restore when another signal arrived. We will just base
62 if (! _hurdsig_catch_memory_fault (ss
->context
))
64 memcpy (&state
->basic
, &ss
->context
->sc_i386_thread_state
,
65 sizeof (state
->basic
));
66 memcpy (&state
->fpu
, &ss
->context
->sc_i386_float_state
,
68 state
->set
|= (1 << i386_THREAD_STATE
) | (1 << i386_FLOAT_STATE
);
72 if (! machine_get_basic_state (ss
->thread
, state
))
75 /* Save the original SP in the gratuitous `esp' slot.
76 We may need to reset the SP (the `uesp' slot) to avoid clobbering an
77 interrupted RPC frame. */
78 state
->basic
.esp
= state
->basic
.uesp
;
80 if ((ss
->actions
[signo
].sa_flags
& SA_ONSTACK
) &&
81 !(ss
->sigaltstack
.ss_flags
& (SS_DISABLE
|SS_ONSTACK
)))
83 sigsp
= ss
->sigaltstack
.ss_sp
+ ss
->sigaltstack
.ss_size
;
84 ss
->sigaltstack
.ss_flags
|= SS_ONSTACK
;
85 /* XXX need to set up base of new stack for
86 per-thread variables, cthreads. */
88 /* This code has intimate knowledge of the special mach_msg system call
89 done in intr-msg.c; that code does (see intr-msg.h):
92 _hurd_intr_rpc_msg_cx_sp: movl $-25, %eax
93 _hurd_intr_rpc_msg_do_trap: lcall $7, $0
94 _hurd_intr_rpc_msg_in_trap: movl %ecx, %esp
95 _hurd_intr_rpc_msg_sp_restored:
96 We must check for the window during which %esp points at the
97 mach_msg arguments. The space below until %ecx is used by
98 the _hurd_intr_rpc_mach_msg frame, and must not be clobbered. */
99 else if (state
->basic
.eip
>= (int) &_hurd_intr_rpc_msg_cx_sp
&&
100 state
->basic
.eip
< (int) &_hurd_intr_rpc_msg_sp_restored
)
101 /* The SP now points at the mach_msg args, but there is more stack
102 space used below it. The real SP is saved in %ecx; we must push the
103 new frame below there, and restore that value as the SP on
105 sigsp
= (char *) (state
->basic
.uesp
= state
->basic
.ecx
);
107 sigsp
= (char *) state
->basic
.uesp
;
109 /* Push the arguments to call `trampoline' on the stack. */
110 sigsp
-= sizeof (*stackframe
);
113 if (_hurdsig_catch_memory_fault (stackframe
))
115 /* We got a fault trying to write the stack frame.
116 We cannot set up the signal handler.
117 Returning NULL tells our caller, who will nuke us with a SIGILL. */
124 extern void _hurdsig_longjmp_from_handler (void *, jmp_buf, int);
126 /* Add a link to the thread's active-resources list. We mark this as
127 the only user of the "resource", so the cleanup function will be
128 called by any longjmp which is unwinding past the signal frame.
129 The cleanup function (in sigunwind.c) will make sure that all the
130 appropriate cleanups done by sigreturn are taken care of. */
131 stackframe
->link
.cleanup
= &_hurdsig_longjmp_from_handler
;
132 stackframe
->link
.cleanup_data
= &stackframe
->ctx
;
133 stackframe
->link
.resource
.next
= NULL
;
134 stackframe
->link
.resource
.prevp
= NULL
;
135 stackframe
->link
.thread
.next
= ss
->active_resources
;
136 stackframe
->link
.thread
.prevp
= &ss
->active_resources
;
137 if (stackframe
->link
.thread
.next
)
138 stackframe
->link
.thread
.next
->thread
.prevp
139 = &stackframe
->link
.thread
.next
;
140 ss
->active_resources
= &stackframe
->link
;
142 /* Set up the arguments for the signal handler. */
143 stackframe
->signo
= signo
;
144 stackframe
->sigcode
= detail
->code
;
145 stackframe
->scp
= stackframe
->return_scp
= scp
= &stackframe
->ctx
;
146 stackframe
->sigreturn_addr
= &__sigreturn
;
147 stackframe
->sigreturn_returns_here
= firewall
; /* Crash on return. */
149 /* Set up the sigcontext from the current state of the thread. */
151 scp
->sc_onstack
= ss
->sigaltstack
.ss_flags
& SS_ONSTACK
? 1 : 0;
153 /* struct sigcontext is laid out so that starting at sc_gs mimics a
154 struct i386_thread_state. */
155 memcpy (&scp
->sc_i386_thread_state
,
156 &state
->basic
, sizeof (state
->basic
));
158 /* struct sigcontext is laid out so that starting at sc_fpkind mimics
159 a struct i386_float_state. */
160 ok
= machine_get_state (ss
->thread
, state
, i386_FLOAT_STATE
,
161 &state
->fpu
, &scp
->sc_i386_float_state
,
162 sizeof (state
->fpu
));
164 _hurdsig_end_catch_fault ();
170 /* Modify the thread state to call the trampoline code on the new stack. */
173 /* The signalee thread was blocked in a mach_msg_trap system call,
174 still waiting for a reply. We will have it run the special
175 trampoline code which retries the message receive before running
178 To do this we change the OPTION argument on its stack to enable only
179 message reception, since the request message has already been
182 struct mach_msg_trap_args
*args
= (void *) state
->basic
.esp
;
184 if (_hurdsig_catch_memory_fault (args
))
186 /* Faulted accessing ARGS. Bomb. */
190 assert (args
->option
& MACH_RCV_MSG
);
191 /* Disable the message-send, since it has already completed. The
192 calls we retry need only wait to receive the reply message. */
193 args
->option
&= ~MACH_SEND_MSG
;
195 /* Limit the time to receive the reply message, in case the server
196 claimed that `interrupt_operation' succeeded but in fact the RPC
198 args
->option
|= MACH_RCV_TIMEOUT
;
199 args
->timeout
= _hurd_interrupted_rpc_timeout
;
201 _hurdsig_end_catch_fault ();
203 state
->basic
.eip
= (int) rpc_wait_trampoline
;
204 /* The reply-receiving trampoline code runs initially on the original
205 user stack. We pass it the signal stack pointer in %ebx. */
206 state
->basic
.uesp
= state
->basic
.esp
; /* Restore mach_msg syscall SP. */
207 state
->basic
.ebx
= (int) sigsp
;
208 /* After doing the message receive, the trampoline code will need to
209 update the %eax value to be restored by sigreturn. To simplify
210 the assembly code, we pass the address of its slot in SCP to the
211 trampoline code in %ecx. */
212 state
->basic
.ecx
= (int) &scp
->sc_eax
;
216 state
->basic
.eip
= (int) trampoline
;
217 state
->basic
.uesp
= (int) sigsp
;
219 /* We pass the handler function to the trampoline code in %edx. */
220 state
->basic
.edx
= (int) handler
;
222 /* The x86 ABI says the DF bit is clear on entry to any function. */
223 state
->basic
.efl
&= ~EFL_DF
;
228 /* The trampoline code follows. This used to be located inside
229 _hurd_setup_sighandler, but was optimized away by gcc 2.95. */
231 asm ("rpc_wait_trampoline:\n");
232 /* This is the entry point when we have an RPC reply message to receive
233 before running the handler. The MACH_MSG_SEND bit has already been
234 cleared in the OPTION argument on our stack. The interrupted user
235 stack pointer has not been changed, so the system call can find its
236 arguments; the signal stack pointer is in %ebx. For our convenience,
237 %ecx points to the sc_eax member of the sigcontext. */
238 asm (/* Retry the interrupted mach_msg system call. */
239 "movl $-25, %eax\n" /* mach_msg_trap */
241 /* When the sigcontext was saved, %eax was MACH_RCV_INTERRUPTED. But
242 now the message receive has completed and the original caller of
243 the RPC (i.e. the code running when the signal arrived) needs to
244 see the final return value of the message receive in %eax. So
245 store the new %eax value into the sc_eax member of the sigcontext
246 (whose address is in %ecx to make this code simpler). */
247 "movl %eax, (%ecx)\n"
248 /* Switch to the signal stack. */
249 "movl %ebx, %esp\n");
251 asm ("trampoline:\n");
252 /* Entry point for running the handler normally. The arguments to the
253 handler function are already on the top of the stack:
259 asm ("call *%edx\n" /* Call the handler function. */
260 "addl $12, %esp\n" /* Pop its args. */
261 /* The word at the top of stack is &__sigreturn; following are a dummy
262 word to fill the slot for the address for __sigreturn to return to,
263 and a copy of SCP for __sigreturn's argument. "Return" to calling
264 __sigreturn (SCP); this call never returns. */