]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/unix_signal.c
6cd4684f3becbc1a318940130d54fd65209f9aba
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1992 NeXT, Inc.
29 * 13 May 1992 ? at NeXT
33 #include <mach/mach_types.h>
34 #include <mach/exception.h>
36 #include <kern/thread.h>
37 #include <kern/thread_act.h>
39 #include <sys/param.h>
45 #include <mach/i386/thread_status.h>
46 #include <dev/i386/sel_inline.h>
50 * FIXME.. should be included from mach_kernel/i386/seg.h
56 #define UDATA_SEL USER_DS
57 #define UCODE_SEL USER_CS
59 #define valid_user_code_selector(x) (TRUE)
60 #define valid_user_data_selector(x) (TRUE)
61 #define valid_user_stack_selector(x) (TRUE)
66 /* Signal handler flavors supported */
67 /* These defns should match the Libc implmn */
71 * Send an interrupt to process.
73 * Stack is set up to allow sigcode stored
74 * in u. to call routine, followed by chmk
75 * to sigreturn routine below. After sigreturn
76 * resets the signal mask, the stack, the frame
77 * pointer, and the argument pointer, it returns
78 * to the user specified pc, psl.
82 sendsig(p
, catcher
, sig
, mask
, code
)
94 struct sigcontext
* scp
;
96 struct sigcontext context
, *scp
;
97 struct sigacts
*ps
= p
->p_sigacts
;
99 thread_t thread
= current_thread();
100 thread_act_t th_act
= current_act();
102 struct i386_saved_state
* saved_state
= get_user_regs(th_act
);
105 ut
= get_bsdthread_info(th_act
);
106 oonstack
= ps
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
107 if ((ps
->ps_flags
& SAS_ALTSTACK
) && !oonstack
&&
108 (ps
->ps_sigonstack
& sigmask(sig
))) {
109 scp
= ((struct sigcontext
*)ps
->ps_sigstk
.ss_sp
) - 1;
110 ps
->ps_sigstk
.ss_flags
|= SA_ONSTACK
;
112 scp
= ((struct sigcontext
*)saved_state
->uesp
) - 1;
113 fp
= ((struct sigframe
*)scp
) - 1;
116 * Build the argument list for the signal handler.
119 trampact
= ps
->ps_trampact
[sig
];
120 /* Handler should call sigreturn to get out of it */
121 frame
.retaddr
= 0xffffffff;
122 frame
.catcher
= catcher
;
123 frame
.sigstyle
= UC_TRAD
;
126 if (sig
== SIGILL
|| sig
== SIGFPE
) {
131 if (copyout((caddr_t
)&frame
, (caddr_t
)fp
, sizeof (frame
)))
136 PCcontext_t context
= threadPCContext(thread
);
138 if (context
&& context
->running
) {
140 context
->running
= FALSE
;
145 * Build the signal context to be used by sigreturn.
147 context
.sc_onstack
= oonstack
;
148 context
.sc_mask
= mask
;
149 context
.sc_eax
= saved_state
->eax
;
150 context
.sc_ebx
= saved_state
->ebx
;
151 context
.sc_ecx
= saved_state
->ecx
;
152 context
.sc_edx
= saved_state
->edx
;
153 context
.sc_edi
= saved_state
->edi
;
154 context
.sc_esi
= saved_state
->esi
;
155 context
.sc_ebp
= saved_state
->ebp
;
156 context
.sc_esp
= saved_state
->uesp
;
157 context
.sc_ss
= saved_state
->ss
;
158 context
.sc_eflags
= saved_state
->efl
;
159 context
.sc_eip
= saved_state
->eip
;
160 context
.sc_cs
= saved_state
->cs
;
161 if (saved_state
->efl
& EFL_VM
) {
162 context
.sc_ds
= saved_state
->v86_segs
.v86_ds
;
163 context
.sc_es
= saved_state
->v86_segs
.v86_es
;
164 context
.sc_fs
= saved_state
->v86_segs
.v86_fs
;
165 context
.sc_gs
= saved_state
->v86_segs
.v86_gs
;
167 saved_state
->efl
&= ~EFL_VM
;
169 context
.sc_ds
= saved_state
->ds
;
170 context
.sc_es
= saved_state
->es
;
171 context
.sc_fs
= saved_state
->fs
;
172 context
.sc_gs
= saved_state
->gs
;
174 if (copyout((caddr_t
)&context
, (caddr_t
)scp
, sizeof (context
)))
177 saved_state
->eip
= (unsigned int)trampact
;
178 saved_state
->cs
= UCODE_SEL
;
180 saved_state
->uesp
= (unsigned int)fp
;
181 saved_state
->ss
= UDATA_SEL
;
183 saved_state
->ds
= UDATA_SEL
;
184 saved_state
->es
= UDATA_SEL
;
185 saved_state
->fs
= NULL_SEG
;
186 saved_state
->gs
= NULL_SEG
;
190 SIGACTION(p
, SIGILL
) = SIG_DFL
;
191 sig
= sigmask(SIGILL
);
192 p
->p_sigignore
&= ~sig
;
193 p
->p_sigcatch
&= ~sig
;
194 ut
->uu_sigmask
&= ~sig
;
195 /* sendsig is called with signal lock held */
196 psignal_lock(p
, SIGILL
, 0);
201 * System call to cleanup state after a signal
202 * has been taken. Reset signal mask and
203 * stack state from context left by sendsig (above).
204 * Return to previous pc and psl as specified by
205 * context left by sendsig. Check carefully to
206 * make sure that the user has not modified the
207 * psl to gain improper priviledges or to cause
210 struct sigreturn_args
{
211 struct sigcontext
*sigcntxp
;
215 sigreturn(p
, uap
, retval
)
217 struct sigreturn_args
*uap
;
220 struct sigcontext context
;
221 thread_t thread
= current_thread();
222 thread_act_t th_act
= current_act();
224 struct i386_saved_state
* saved_state
= get_user_regs(th_act
);
229 if (saved_state
== NULL
)
232 if (error
= copyin((caddr_t
)uap
->sigcntxp
, (caddr_t
)&context
,
235 ut
= (struct uthread
*)get_bsdthread_info(th_act
);
237 if (context
.sc_onstack
& 01)
238 p
->p_sigacts
->ps_sigstk
.ss_flags
|= SA_ONSTACK
;
240 p
->p_sigacts
->ps_sigstk
.ss_flags
&= ~SA_ONSTACK
;
241 ut
->uu_sigmask
= context
.sc_mask
&~ sigcantmask
;
242 if(ut
->uu_siglist
& ~ut
->uu_sigmask
)
243 signal_setast(current_act());
244 saved_state
->eax
= context
.sc_eax
;
245 saved_state
->ebx
= context
.sc_ebx
;
246 saved_state
->ecx
= context
.sc_ecx
;
247 saved_state
->edx
= context
.sc_edx
;
248 saved_state
->edi
= context
.sc_edi
;
249 saved_state
->esi
= context
.sc_esi
;
250 saved_state
->ebp
= context
.sc_ebp
;
251 saved_state
->uesp
= context
.sc_esp
;
252 saved_state
->ss
= context
.sc_ss
;
253 saved_state
->efl
= context
.sc_eflags
;
254 saved_state
->efl
&= ~EFL_USERCLR
;
255 saved_state
->efl
|= EFL_USERSET
;
256 saved_state
->eip
= context
.sc_eip
;
257 saved_state
->cs
= context
.sc_cs
;
259 if (context
.sc_eflags
& EFL_VM
) {
260 saved_state
->ds
= NULL_SEG
;
261 saved_state
->es
= NULL_SEG
;
262 saved_state
->fs
= NULL_SEG
;
263 saved_state
->gs
= NULL_SEG
;
264 saved_state
->v86_segs
.v86_ds
= context
.sc_ds
;
265 saved_state
->v86_segs
.v86_es
= context
.sc_es
;
266 saved_state
->v86_segs
.v86_fs
= context
.sc_fs
;
267 saved_state
->v86_segs
.v86_gs
= context
.sc_gs
;
269 saved_state
->efl
|= EFL_VM
;
272 saved_state
->ds
= context
.sc_ds
;
273 saved_state
->es
= context
.sc_es
;
274 saved_state
->fs
= context
.sc_fs
;
275 saved_state
->gs
= context
.sc_gs
;
279 if (context
.sc_onstack
& 02) {
280 PCcontext_t context
= threadPCContext(thread
);
283 context
->running
= TRUE
;
287 return (EJUSTRETURN
);
291 * machine_exception() performs MD translation
292 * of a mach exception to a unix signal and code.
307 case EXC_BAD_INSTRUCTION
:
308 *unix_signal
= SIGILL
;
313 *unix_signal
= SIGFPE
;