]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/unix_signal.c
f8081f1166c96b21b527002c568aba50da73edb2
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1992 NeXT, Inc.
27 * 13 May 1992 ? at NeXT
31 #include <mach/mach_types.h>
32 #include <mach/exception.h>
34 #include <kern/thread.h>
36 #include <sys/systm.h>
37 #include <sys/param.h>
38 #include <sys/proc_internal.h>
40 #include <sys/sysproto.h>
41 #include <sys/sysent.h>
42 #include <mach/thread_act.h> /* for thread_abort_safely */
47 #include <mach/i386/thread_status.h>
49 extern struct i386_saved_state
*get_user_regs(thread_t
);
51 extern boolean_t
valid_user_segment_selectors(uint16_t cs
,
59 extern boolean_t
machine_exception(int, int, int, int *, int *);
61 /* Signal handler flavors supported */
62 /* These defns should match the Libc implmn */
66 * Send an interrupt to process.
68 * Stack is set up to allow sigcode stored
69 * in u. to call routine, followed by chmk
70 * to sigreturn routine below. After sigreturn
71 * resets the signal mask, the stack, the frame
72 * pointer, and the argument pointer, it returns
73 * to the user specified pc, psl.
76 sendsig(p
, catcher
, sig
, mask
, code
)
78 user_addr_t catcher
; /* sig_t */
88 struct sigcontext
* scp
;
90 struct sigcontext context
, *scp
;
91 struct sigacts
*ps
= p
->p_sigacts
;
93 thread_t thread
= current_thread();
95 struct i386_saved_state
* saved_state
= get_user_regs(thread
);
98 ut
= get_bsdthread_info(thread
);
99 oonstack
= ps
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
100 if ((ps
->ps_flags
& SAS_ALTSTACK
) && !oonstack
&&
101 (ps
->ps_sigonstack
& sigmask(sig
))) {
102 scp
= ((struct sigcontext
*)ps
->ps_sigstk
.ss_sp
) - 1;
103 ps
->ps_sigstk
.ss_flags
|= SA_ONSTACK
;
105 scp
= ((struct sigcontext
*)saved_state
->uesp
) - 1;
106 fp
= ((struct sigframe
*)scp
) - 1;
109 * Build the argument list for the signal handler.
112 trampact
= (sig_t
)ps
->ps_trampact
[sig
];
113 /* Handler should call sigreturn to get out of it */
114 frame
.retaddr
= 0xffffffff;
115 frame
.catcher
= CAST_DOWN(sig_t
,catcher
); /* XXX LP64 */
116 frame
.sigstyle
= UC_TRAD
;
119 if (sig
== SIGILL
|| sig
== SIGFPE
) {
124 if (copyout((caddr_t
)&frame
, (user_addr_t
)fp
, sizeof (frame
)))
128 * Build the signal context to be used by sigreturn.
130 context
.sc_onstack
= oonstack
;
131 context
.sc_mask
= mask
;
132 context
.sc_eax
= saved_state
->eax
;
133 context
.sc_ebx
= saved_state
->ebx
;
134 context
.sc_ecx
= saved_state
->ecx
;
135 context
.sc_edx
= saved_state
->edx
;
136 context
.sc_edi
= saved_state
->edi
;
137 context
.sc_esi
= saved_state
->esi
;
138 context
.sc_ebp
= saved_state
->ebp
;
139 context
.sc_esp
= saved_state
->uesp
;
140 context
.sc_ss
= saved_state
->ss
;
141 context
.sc_eflags
= saved_state
->efl
;
142 context
.sc_eip
= saved_state
->eip
;
143 context
.sc_cs
= saved_state
->cs
;
144 if (saved_state
->efl
& EFL_VM
) {
145 context
.sc_ds
= saved_state
->v86_segs
.v86_ds
;
146 context
.sc_es
= saved_state
->v86_segs
.v86_es
;
147 context
.sc_fs
= saved_state
->v86_segs
.v86_fs
;
148 context
.sc_gs
= saved_state
->v86_segs
.v86_gs
;
150 saved_state
->efl
&= ~EFL_VM
;
152 context
.sc_ds
= saved_state
->ds
;
153 context
.sc_es
= saved_state
->es
;
154 context
.sc_fs
= saved_state
->fs
;
155 context
.sc_gs
= saved_state
->gs
;
157 if (copyout((caddr_t
)&context
, (user_addr_t
)scp
, sizeof (context
)))
160 saved_state
->eip
= (unsigned int)trampact
;
161 saved_state
->cs
= USER_CS
;
163 saved_state
->uesp
= (unsigned int)fp
;
164 saved_state
->ss
= USER_DS
;
166 saved_state
->ds
= USER_DS
;
167 saved_state
->es
= USER_DS
;
168 saved_state
->fs
= NULL_SEG
;
169 saved_state
->gs
= USER_CTHREAD
;
173 SIGACTION(p
, SIGILL
) = SIG_DFL
;
174 sig
= sigmask(SIGILL
);
175 p
->p_sigignore
&= ~sig
;
176 p
->p_sigcatch
&= ~sig
;
177 ut
->uu_sigmask
&= ~sig
;
178 /* sendsig is called with signal lock held */
179 psignal_lock(p
, SIGILL
, 0);
184 * System call to cleanup state after a signal
185 * has been taken. Reset signal mask and
186 * stack state from context left by sendsig (above).
187 * Return to previous pc and psl as specified by
188 * context left by sendsig. Check carefully to
189 * make sure that the user has not modified the
190 * psl to gain improper priviledges or to cause
197 struct sigreturn_args
*uap
,
198 __unused
int *retval
)
200 struct sigcontext context
;
201 thread_t thread
= current_thread();
203 struct i386_saved_state
* saved_state
= (struct i386_saved_state
*)
204 get_user_regs(thread
);
209 if (saved_state
== NULL
)
212 if ((error
= copyin(CAST_USER_ADDR_T(uap
->sigcntxp
), (void *)&context
,
217 * Validate segment selectors.
218 * Bad values would result in kernel exception at context switch
219 * back to user mode. If other state is invalid an exception will
220 * occur in user context.
222 if (!valid_user_segment_selectors(context
.sc_cs
,
231 ut
= (struct uthread
*)get_bsdthread_info(thread
);
233 if (context
.sc_onstack
& 01)
234 p
->p_sigacts
->ps_sigstk
.ss_flags
|= SA_ONSTACK
;
236 p
->p_sigacts
->ps_sigstk
.ss_flags
&= ~SA_ONSTACK
;
238 ut
->uu_sigmask
= context
.sc_mask
&~ sigcantmask
;
239 if(ut
->uu_siglist
& ~ut
->uu_sigmask
)
240 signal_setast(thread
);
242 saved_state
->eax
= context
.sc_eax
;
243 saved_state
->ebx
= context
.sc_ebx
;
244 saved_state
->ecx
= context
.sc_ecx
;
245 saved_state
->edx
= context
.sc_edx
;
246 saved_state
->edi
= context
.sc_edi
;
247 saved_state
->esi
= context
.sc_esi
;
248 saved_state
->ebp
= context
.sc_ebp
;
249 saved_state
->uesp
= context
.sc_esp
;
250 saved_state
->ss
= context
.sc_ss
;
251 saved_state
->efl
= context
.sc_eflags
;
252 saved_state
->efl
&= ~EFL_USERCLR
;
253 saved_state
->efl
|= EFL_USERSET
;
254 saved_state
->eip
= context
.sc_eip
;
255 saved_state
->cs
= context
.sc_cs
;
257 if (context
.sc_eflags
& EFL_VM
) {
258 saved_state
->ds
= NULL_SEG
;
259 saved_state
->es
= NULL_SEG
;
260 saved_state
->fs
= NULL_SEG
;
261 saved_state
->gs
= NULL_SEG
;
262 saved_state
->v86_segs
.v86_ds
= context
.sc_ds
;
263 saved_state
->v86_segs
.v86_es
= context
.sc_es
;
264 saved_state
->v86_segs
.v86_fs
= context
.sc_fs
;
265 saved_state
->v86_segs
.v86_gs
= context
.sc_gs
;
267 saved_state
->efl
|= EFL_VM
;
270 saved_state
->ds
= context
.sc_ds
;
271 saved_state
->es
= context
.sc_es
;
272 saved_state
->fs
= context
.sc_fs
;
273 saved_state
->gs
= context
.sc_gs
;
276 return (EJUSTRETURN
);
280 * machine_exception() performs MD translation
281 * of a mach exception to a unix signal and code.
288 __unused
int subcode
,
296 case EXC_BAD_INSTRUCTION
:
297 *unix_signal
= SIGILL
;
302 *unix_signal
= SIGFPE
;
313 #include <sys/systm.h>
314 #include <sys/sysent.h>
316 int __pthread_cset(struct sysent
*);
317 void __pthread_creset(struct sysent
*);
320 __pthread_cset(struct sysent
*callp
)
322 unsigned int cancel_enable
;
324 struct uthread
* uthread
;
326 thread
= current_thread();
327 uthread
= get_bsdthread_info(thread
);
329 cancel_enable
= callp
->sy_cancel
;
330 if (cancel_enable
== _SYSCALL_CANCEL_NONE
) {
331 uthread
->uu_flag
|= UT_NOTCANCELPT
;
333 if((uthread
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
334 if (cancel_enable
== _SYSCALL_CANCEL_PRE
)
337 thread_abort_safely(thread
);
345 __pthread_creset(struct sysent
*callp
)
348 unsigned int cancel_enable
;
350 struct uthread
* uthread
;
352 thread
= current_thread();
353 uthread
= get_bsdthread_info(thread
);
355 cancel_enable
= callp
->sy_cancel
;
357 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;