1 /* Set thread_state for sighandler, and sigcontext to recover. i386 version.
2 Copyright (C) 1994, 1995 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <hurd/signal.h>
21 #include <hurd/userlink.h>
22 #include "thread_state.h"
25 #include "hurdfault.h"
28 struct mach_msg_trap_args
30 void *retaddr
; /* Address mach_msg_trap will return to. */
31 /* This is the order of arguments to mach_msg_trap. */
32 mach_msg_header_t
*msg
;
33 mach_msg_option_t option
;
34 mach_msg_size_t send_size
;
35 mach_msg_size_t rcv_size
;
37 mach_msg_timeout_t timeout
;
42 _hurd_setup_sighandler (struct hurd_sigstate
*ss
, __sighandler_t handler
,
43 int signo
, long int sigcode
,
44 volatile int rpc_wait
,
45 struct machine_thread_all_state
*state
)
47 __label__ trampoline
, rpc_wait_trampoline
, firewall
;
48 extern const void _hurd_intr_rpc_msg_in_trap
;
49 extern const void _hurd_intr_rpc_msg_cx_sp
;
50 extern const void _hurd_intr_rpc_msg_sp_restored
;
52 struct sigcontext
*scp
;
57 struct sigcontext
*scp
; /* Points to ctx, below. */
59 void *sigreturn_returns_here
;
60 struct sigcontext
*return_scp
; /* Same; arg to sigreturn. */
61 struct sigcontext ctx
;
62 struct hurd_userlink link
;
67 /* We have a previous sigcontext that sigreturn was about
68 to restore when another signal arrived. We will just base
70 if (_hurdsig_catch_fault (SIGSEGV
))
71 assert (_hurdsig_fault_sigcode
>= (long int) ss
->context
&&
72 _hurdsig_fault_sigcode
< (long int) (ss
->context
+ 1));
75 memcpy (&state
->basic
, &ss
->context
->sc_i386_thread_state
,
76 sizeof (state
->basic
));
77 memcpy (&state
->fpu
, &ss
->context
->sc_i386_float_state
,
79 state
->set
|= (1 << i386_THREAD_STATE
) | (1 << i386_FLOAT_STATE
);
83 if (! machine_get_basic_state (ss
->thread
, state
))
86 /* Save the original SP in the gratuitous `esp' slot.
87 We may need to reset the SP (the `uesp' slot) to avoid clobbering an
88 interrupted RPC frame. */
89 state
->basic
.esp
= state
->basic
.uesp
;
91 if ((ss
->actions
[signo
].sa_flags
& SA_ONSTACK
) &&
92 !(ss
->sigaltstack
.ss_flags
& (SA_DISABLE
|SA_ONSTACK
)))
94 sigsp
= ss
->sigaltstack
.ss_sp
+ ss
->sigaltstack
.ss_size
;
95 ss
->sigaltstack
.ss_flags
|= SA_ONSTACK
;
96 /* XXX need to set up base of new stack for
97 per-thread variables, cthreads. */
99 /* This code has intimate knowledge of the special mach_msg system call
100 done in intr-msg.c; that code does:
103 _hurd_intr_rpc_msg_cx_sp: movl $-25, %eax
104 _hurd_intr_rpc_msg_do_trap: lcall $7, $0
105 _hurd_intr_rpc_msg_in_trap: movl %ecx, %esp
106 _hurd_intr_rpc_msg_sp_restored:
107 We must check for the window during which %esp points at the
108 mach_msg arguments. The space below until %ecx is used by
109 the _hurd_intr_rpc_mach_msg frame, and must not be clobbered. */
110 else if (state
->basic
.eip
>= (int) &_hurd_intr_rpc_msg_cx_sp
&&
111 state
->basic
.eip
< (int) &_hurd_intr_rpc_msg_sp_restored
)
112 /* The SP now points at the mach_msg args, but there is more stack
113 space used below it. The real SP is saved in %ecx; we must push the
114 new frame below there, and restore that value as the SP on
116 sigsp
= (char *) (state
->basic
.uesp
= state
->basic
.ecx
);
118 sigsp
= (char *) state
->basic
.uesp
;
120 /* Push the arguments to call `trampoline' on the stack. */
121 sigsp
-= sizeof (*stackframe
);
124 if (_hurdsig_catch_fault (SIGSEGV
))
126 assert (_hurdsig_fault_sigcode
>= (long int) stackframe
&&
127 _hurdsig_fault_sigcode
<= (long int) (stackframe
+ 1));
128 /* We got a fault trying to write the stack frame.
129 We cannot set up the signal handler.
130 Returning NULL tells our caller, who will nuke us with a SIGILL. */
137 extern void _hurdsig_longjmp_from_handler (void *, jmp_buf, int);
139 /* Add a link to the thread's active-resources list. We mark this as
140 the only user of the "resource", so the cleanup function will be
141 called by any longjmp which is unwinding past the signal frame.
142 The cleanup function (in sigunwind.c) will make sure that all the
143 appropriate cleanups done by sigreturn are taken care of. */
144 stackframe
->link
.cleanup
= &_hurdsig_longjmp_from_handler
;
145 stackframe
->link
.cleanup_data
= &stackframe
->ctx
;
146 stackframe
->link
.resource
.next
= NULL
;
147 stackframe
->link
.resource
.prevp
= NULL
;
148 stackframe
->link
.thread
.next
= ss
->active_resources
;
149 stackframe
->link
.thread
.prevp
= &ss
->active_resources
;
150 if (stackframe
->link
.thread
.next
)
151 stackframe
->link
.thread
.next
->thread
.prevp
152 = &stackframe
->link
.thread
.next
;
153 ss
->active_resources
= &stackframe
->link
;
155 /* Set up the arguments for the signal handler. */
156 stackframe
->signo
= signo
;
157 stackframe
->sigcode
= sigcode
;
158 stackframe
->scp
= stackframe
->return_scp
= scp
= &stackframe
->ctx
;
159 stackframe
->sigreturn_addr
= &__sigreturn
;
160 stackframe
->sigreturn_returns_here
= &&firewall
; /* Crash on return. */
162 /* Set up the sigcontext from the current state of the thread. */
164 scp
->sc_onstack
= ss
->sigaltstack
.ss_flags
& SA_ONSTACK
? 1 : 0;
166 /* struct sigcontext is laid out so that starting at sc_gs mimics a
167 struct i386_thread_state. */
168 memcpy (&scp
->sc_i386_thread_state
,
169 &state
->basic
, sizeof (state
->basic
));
171 /* struct sigcontext is laid out so that starting at sc_fpkind mimics
172 a struct i386_float_state. */
173 ok
= machine_get_state (ss
->thread
, state
, i386_FLOAT_STATE
,
174 &state
->fpu
, &scp
->sc_i386_float_state
,
175 sizeof (state
->fpu
));
177 _hurdsig_end_catch_fault ();
183 /* Modify the thread state to call the trampoline code on the new stack. */
186 /* The signalee thread was blocked in a mach_msg_trap system call,
187 still waiting for a reply. We will have it run the special
188 trampoline code which retries the message receive before running
191 To do this we change the OPTION argument on its stack to enable only
192 message reception, since the request message has already been
195 struct mach_msg_trap_args
*args
= (void *) state
->basic
.esp
;
197 if (_hurdsig_catch_fault (SIGSEGV
))
199 assert (_hurdsig_fault_sigcode
>= (long int) args
&&
200 _hurdsig_fault_sigcode
< (long int) (args
+ 1));
201 /* Faulted accessing ARGS. Bomb. */
205 assert (args
->option
& MACH_RCV_MSG
);
206 /* Disable the message-send, since it has already completed. The
207 calls we retry need only wait to receive the reply message. */
208 args
->option
&= ~MACH_SEND_MSG
;
210 /* Limit the time to receive the reply message, in case the server
211 claimed that `interrupt_operation' succeeded but in fact the RPC
213 args
->option
|= MACH_RCV_TIMEOUT
;
214 args
->timeout
= _hurd_interrupted_rpc_timeout
;
216 _hurdsig_end_catch_fault ();
218 state
->basic
.eip
= (int) &&rpc_wait_trampoline
;
219 /* The reply-receiving trampoline code runs initially on the original
220 user stack. We pass it the signal stack pointer in %ebx. */
221 state
->basic
.uesp
= state
->basic
.esp
; /* Restore mach_msg syscall SP. */
222 state
->basic
.ebx
= (int) sigsp
;
223 /* After doing the message receive, the trampoline code will need to
224 update the %eax value to be restored by sigreturn. To simplify
225 the assembly code, we pass the address of its slot in SCP to the
226 trampoline code in %ecx. */
227 state
->basic
.ecx
= (int) &scp
->sc_eax
;
231 state
->basic
.eip
= (int) &&trampoline
;
232 state
->basic
.uesp
= (int) sigsp
;
234 /* We pass the handler function to the trampoline code in %edx. */
235 state
->basic
.edx
= (int) handler
;
239 /* The trampoline code follows. This is not actually executed as part of
240 this function, it is just convenient to write it that way. */
243 /* This is the entry point when we have an RPC reply message to receive
244 before running the handler. The MACH_MSG_SEND bit has already been
245 cleared in the OPTION argument on our stack. The interrupted user
246 stack pointer has not been changed, so the system call can find its
247 arguments; the signal stack pointer is in %ebx. For our convenience,
248 %ecx points to the sc_eax member of the sigcontext. */
250 (/* Retry the interrupted mach_msg system call. */
251 "movl $-25, %eax\n" /* mach_msg_trap */
253 /* When the sigcontext was saved, %eax was MACH_RCV_INTERRUPTED. But
254 now the message receive has completed and the original caller of
255 the RPC (i.e. the code running when the signal arrived) needs to
256 see the final return value of the message receive in %eax. So
257 store the new %eax value into the sc_eax member of the sigcontext
258 (whose address is in %ecx to make this code simpler). */
259 "movl %eax, (%ecx)\n"
260 /* Switch to the signal stack. */
261 "movl %ebx, %esp\n");
264 /* Entry point for running the handler normally. The arguments to the
265 handler function are already on the top of the stack:
272 ("call *%edx\n" /* Call the handler function. */
273 "addl $12, %esp\n" /* Pop its args. */
274 /* The word at the top of stack is &__sigreturn; following are a dummy
275 word to fill the slot for the address for __sigreturn to return to,
276 and a copy of SCP for __sigreturn's argument. "Return" to calling
277 __sigreturn (SCP); this call never returns. */
281 asm volatile ("hlt");