]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/unix_signal.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1992 NeXT, Inc.
32 * 13 May 1992 ? at NeXT
36 #include <mach/mach_types.h>
37 #include <mach/exception.h>
39 #include <kern/thread.h>
41 #include <sys/systm.h>
42 #include <sys/param.h>
43 #include <sys/proc_internal.h>
45 #include <sys/sysproto.h>
46 #include <sys/sysent.h>
47 #include <mach/thread_act.h> /* for thread_abort_safely */
52 #include <mach/i386/thread_status.h>
54 extern struct i386_saved_state
*get_user_regs(thread_t
);
56 extern boolean_t
valid_user_segment_selectors(uint16_t cs
,
64 extern boolean_t
machine_exception(int, int, int, int *, int *);
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.
81 sendsig(p
, catcher
, sig
, mask
, code
)
83 user_addr_t catcher
; /* sig_t */
93 struct sigcontext
* scp
;
95 struct sigcontext context
, *scp
;
96 struct sigacts
*ps
= p
->p_sigacts
;
98 thread_t thread
= current_thread();
100 struct i386_saved_state
* saved_state
= get_user_regs(thread
);
103 ut
= get_bsdthread_info(thread
);
104 oonstack
= ps
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
105 if ((ps
->ps_flags
& SAS_ALTSTACK
) && !oonstack
&&
106 (ps
->ps_sigonstack
& sigmask(sig
))) {
107 scp
= ((struct sigcontext
*)ps
->ps_sigstk
.ss_sp
) - 1;
108 ps
->ps_sigstk
.ss_flags
|= SA_ONSTACK
;
110 scp
= ((struct sigcontext
*)saved_state
->uesp
) - 1;
111 fp
= ((struct sigframe
*)scp
) - 1;
114 * Build the argument list for the signal handler.
117 trampact
= (sig_t
)ps
->ps_trampact
[sig
];
118 /* Handler should call sigreturn to get out of it */
119 frame
.retaddr
= 0xffffffff;
120 frame
.catcher
= CAST_DOWN(sig_t
,catcher
); /* XXX LP64 */
121 frame
.sigstyle
= UC_TRAD
;
124 if (sig
== SIGILL
|| sig
== SIGFPE
) {
129 if (copyout((caddr_t
)&frame
, (user_addr_t
)fp
, sizeof (frame
)))
133 * Build the signal context to be used by sigreturn.
135 context
.sc_onstack
= oonstack
;
136 context
.sc_mask
= mask
;
137 context
.sc_eax
= saved_state
->eax
;
138 context
.sc_ebx
= saved_state
->ebx
;
139 context
.sc_ecx
= saved_state
->ecx
;
140 context
.sc_edx
= saved_state
->edx
;
141 context
.sc_edi
= saved_state
->edi
;
142 context
.sc_esi
= saved_state
->esi
;
143 context
.sc_ebp
= saved_state
->ebp
;
144 context
.sc_esp
= saved_state
->uesp
;
145 context
.sc_ss
= saved_state
->ss
;
146 context
.sc_eflags
= saved_state
->efl
;
147 context
.sc_eip
= saved_state
->eip
;
148 context
.sc_cs
= saved_state
->cs
;
149 if (saved_state
->efl
& EFL_VM
) {
150 context
.sc_ds
= saved_state
->v86_segs
.v86_ds
;
151 context
.sc_es
= saved_state
->v86_segs
.v86_es
;
152 context
.sc_fs
= saved_state
->v86_segs
.v86_fs
;
153 context
.sc_gs
= saved_state
->v86_segs
.v86_gs
;
155 saved_state
->efl
&= ~EFL_VM
;
157 context
.sc_ds
= saved_state
->ds
;
158 context
.sc_es
= saved_state
->es
;
159 context
.sc_fs
= saved_state
->fs
;
160 context
.sc_gs
= saved_state
->gs
;
162 if (copyout((caddr_t
)&context
, (user_addr_t
)scp
, sizeof (context
)))
165 saved_state
->eip
= (unsigned int)trampact
;
166 saved_state
->cs
= USER_CS
;
168 saved_state
->uesp
= (unsigned int)fp
;
169 saved_state
->ss
= USER_DS
;
171 saved_state
->ds
= USER_DS
;
172 saved_state
->es
= USER_DS
;
173 saved_state
->fs
= NULL_SEG
;
174 saved_state
->gs
= USER_CTHREAD
;
178 SIGACTION(p
, SIGILL
) = SIG_DFL
;
179 sig
= sigmask(SIGILL
);
180 p
->p_sigignore
&= ~sig
;
181 p
->p_sigcatch
&= ~sig
;
182 ut
->uu_sigmask
&= ~sig
;
183 /* sendsig is called with signal lock held */
184 psignal_lock(p
, SIGILL
, 0);
189 * System call to cleanup state after a signal
190 * has been taken. Reset signal mask and
191 * stack state from context left by sendsig (above).
192 * Return to previous pc and psl as specified by
193 * context left by sendsig. Check carefully to
194 * make sure that the user has not modified the
195 * psl to gain improper priviledges or to cause
202 struct sigreturn_args
*uap
,
203 __unused
int *retval
)
205 struct sigcontext context
;
206 thread_t thread
= current_thread();
208 struct i386_saved_state
* saved_state
= (struct i386_saved_state
*)
209 get_user_regs(thread
);
214 if (saved_state
== NULL
)
217 if ((error
= copyin(CAST_USER_ADDR_T(uap
->sigcntxp
), (void *)&context
,
222 * Validate segment selectors.
223 * Bad values would result in kernel exception at context switch
224 * back to user mode. If other state is invalid an exception will
225 * occur in user context.
227 if (!valid_user_segment_selectors(context
.sc_cs
,
236 ut
= (struct uthread
*)get_bsdthread_info(thread
);
238 if (context
.sc_onstack
& 01)
239 p
->p_sigacts
->ps_sigstk
.ss_flags
|= SA_ONSTACK
;
241 p
->p_sigacts
->ps_sigstk
.ss_flags
&= ~SA_ONSTACK
;
243 ut
->uu_sigmask
= context
.sc_mask
&~ sigcantmask
;
244 if(ut
->uu_siglist
& ~ut
->uu_sigmask
)
245 signal_setast(thread
);
247 saved_state
->eax
= context
.sc_eax
;
248 saved_state
->ebx
= context
.sc_ebx
;
249 saved_state
->ecx
= context
.sc_ecx
;
250 saved_state
->edx
= context
.sc_edx
;
251 saved_state
->edi
= context
.sc_edi
;
252 saved_state
->esi
= context
.sc_esi
;
253 saved_state
->ebp
= context
.sc_ebp
;
254 saved_state
->uesp
= context
.sc_esp
;
255 saved_state
->ss
= context
.sc_ss
;
256 saved_state
->efl
= context
.sc_eflags
;
257 saved_state
->efl
&= ~EFL_USERCLR
;
258 saved_state
->efl
|= EFL_USERSET
;
259 saved_state
->eip
= context
.sc_eip
;
260 saved_state
->cs
= context
.sc_cs
;
262 if (context
.sc_eflags
& EFL_VM
) {
263 saved_state
->ds
= NULL_SEG
;
264 saved_state
->es
= NULL_SEG
;
265 saved_state
->fs
= NULL_SEG
;
266 saved_state
->gs
= NULL_SEG
;
267 saved_state
->v86_segs
.v86_ds
= context
.sc_ds
;
268 saved_state
->v86_segs
.v86_es
= context
.sc_es
;
269 saved_state
->v86_segs
.v86_fs
= context
.sc_fs
;
270 saved_state
->v86_segs
.v86_gs
= context
.sc_gs
;
272 saved_state
->efl
|= EFL_VM
;
275 saved_state
->ds
= context
.sc_ds
;
276 saved_state
->es
= context
.sc_es
;
277 saved_state
->fs
= context
.sc_fs
;
278 saved_state
->gs
= context
.sc_gs
;
281 return (EJUSTRETURN
);
285 * machine_exception() performs MD translation
286 * of a mach exception to a unix signal and code.
293 __unused
int subcode
,
301 case EXC_BAD_INSTRUCTION
:
302 *unix_signal
= SIGILL
;
307 *unix_signal
= SIGFPE
;
318 #include <sys/systm.h>
319 #include <sys/sysent.h>
321 int __pthread_cset(struct sysent
*);
322 void __pthread_creset(struct sysent
*);
325 __pthread_cset(struct sysent
*callp
)
327 unsigned int cancel_enable
;
329 struct uthread
* uthread
;
331 thread
= current_thread();
332 uthread
= get_bsdthread_info(thread
);
334 cancel_enable
= callp
->sy_cancel
;
335 if (cancel_enable
== _SYSCALL_CANCEL_NONE
) {
336 uthread
->uu_flag
|= UT_NOTCANCELPT
;
338 if((uthread
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
339 if (cancel_enable
== _SYSCALL_CANCEL_PRE
)
342 thread_abort_safely(thread
);
350 __pthread_creset(struct sysent
*callp
)
353 unsigned int cancel_enable
;
355 struct uthread
* uthread
;
357 thread
= current_thread();
358 uthread
= get_bsdthread_info(thread
);
360 cancel_enable
= callp
->sy_cancel
;
362 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;