]>
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_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)
67 * Send an interrupt to process.
69 * Stack is set up to allow sigcode stored
70 * in u. to call routine, followed by chmk
71 * to sigreturn routine below. After sigreturn
72 * resets the signal mask, the stack, the frame
73 * pointer, and the argument pointer, it returns
74 * to the user specified pc, psl.
78 sendsig(p
, catcher
, sig
, mask
, code
)
90 struct sigcontext
* scp
;
92 struct sigcontext context
, *scp
;
93 struct sigacts
*ps
= p
->p_sigacts
;
95 thread_t thread
= current_thread();
96 thread_act_t th_act
= current_act();
98 struct i386_saved_state
* saved_state
= get_user_regs(th_act
);
101 ut
= get_bsdthread_info(th_act
);
102 oonstack
= ps
->ps_sigstk
.ss_flags
& SA_ONSTACK
;
103 if ((ps
->ps_flags
& SAS_ALTSTACK
) && !oonstack
&&
104 (ps
->ps_sigonstack
& sigmask(sig
))) {
105 scp
= ((struct sigcontext
*)ps
->ps_sigstk
.ss_sp
) - 1;
106 ps
->ps_sigstk
.ss_flags
|= SA_ONSTACK
;
108 scp
= ((struct sigcontext
*)saved_state
->uesp
) - 1;
109 fp
= ((struct sigframe
*)scp
) - 1;
112 * Build the argument list for the signal handler.
115 trampact
= ps
->ps_trampact
[sig
];
116 /* Handler should call sigreturn to get out of it */
117 frame
.retaddr
= 0xffffffff;
118 frame
.catcher
= catcher
;
122 if (sig
== SIGILL
|| sig
== SIGFPE
) {
127 if (copyout((caddr_t
)&frame
, (caddr_t
)fp
, sizeof (frame
)))
132 PCcontext_t context
= threadPCContext(thread
);
134 if (context
&& context
->running
) {
136 context
->running
= FALSE
;
141 * Build the signal context to be used by sigreturn.
143 context
.sc_onstack
= oonstack
;
144 context
.sc_mask
= mask
;
145 context
.sc_eax
= saved_state
->eax
;
146 context
.sc_ebx
= saved_state
->ebx
;
147 context
.sc_ecx
= saved_state
->ecx
;
148 context
.sc_edx
= saved_state
->edx
;
149 context
.sc_edi
= saved_state
->edi
;
150 context
.sc_esi
= saved_state
->esi
;
151 context
.sc_ebp
= saved_state
->ebp
;
152 context
.sc_esp
= saved_state
->uesp
;
153 context
.sc_ss
= saved_state
->ss
;
154 context
.sc_eflags
= saved_state
->efl
;
155 context
.sc_eip
= saved_state
->eip
;
156 context
.sc_cs
= saved_state
->cs
;
157 if (saved_state
->efl
& EFL_VM
) {
158 context
.sc_ds
= saved_state
->v86_segs
.v86_ds
;
159 context
.sc_es
= saved_state
->v86_segs
.v86_es
;
160 context
.sc_fs
= saved_state
->v86_segs
.v86_fs
;
161 context
.sc_gs
= saved_state
->v86_segs
.v86_gs
;
163 saved_state
->efl
&= ~EFL_VM
;
165 context
.sc_ds
= saved_state
->ds
;
166 context
.sc_es
= saved_state
->es
;
167 context
.sc_fs
= saved_state
->fs
;
168 context
.sc_gs
= saved_state
->gs
;
170 if (copyout((caddr_t
)&context
, (caddr_t
)scp
, sizeof (context
)))
173 saved_state
->eip
= (unsigned int)trampact
;
174 saved_state
->cs
= UCODE_SEL
;
176 saved_state
->uesp
= (unsigned int)fp
;
177 saved_state
->ss
= UDATA_SEL
;
179 saved_state
->ds
= UDATA_SEL
;
180 saved_state
->es
= UDATA_SEL
;
181 saved_state
->fs
= NULL_SEG
;
182 saved_state
->gs
= NULL_SEG
;
186 SIGACTION(p
, SIGILL
) = SIG_DFL
;
187 sig
= sigmask(SIGILL
);
188 p
->p_sigignore
&= ~sig
;
189 p
->p_sigcatch
&= ~sig
;
190 ut
->uu_sigmask
&= ~sig
;
191 /* sendsig is called with signal lock held */
192 psignal_lock(p
, SIGILL
, 0);
197 * System call to cleanup state after a signal
198 * has been taken. Reset signal mask and
199 * stack state from context left by sendsig (above).
200 * Return to previous pc and psl as specified by
201 * context left by sendsig. Check carefully to
202 * make sure that the user has not modified the
203 * psl to gain improper priviledges or to cause
206 struct sigreturn_args
{
207 struct sigcontext
*sigcntxp
;
211 sigreturn(p
, uap
, retval
)
213 struct sigreturn_args
*uap
;
216 struct sigcontext context
;
217 thread_t thread
= current_thread();
218 thread_act_t th_act
= current_act();
220 struct i386_saved_state
* saved_state
= get_user_regs(th_act
);
225 if (saved_state
== NULL
)
228 if (error
= copyin((caddr_t
)uap
->sigcntxp
, (caddr_t
)&context
,
231 ut
= (struct uthread
*)get_bsdthread_info(th_act
);
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
;
237 ut
->uu_sigmask
= context
.sc_mask
&~ sigcantmask
;
238 if(ut
->uu_siglist
& ~ut
->uu_sigmask
)
239 signal_setast(current_act());
240 saved_state
->eax
= context
.sc_eax
;
241 saved_state
->ebx
= context
.sc_ebx
;
242 saved_state
->ecx
= context
.sc_ecx
;
243 saved_state
->edx
= context
.sc_edx
;
244 saved_state
->edi
= context
.sc_edi
;
245 saved_state
->esi
= context
.sc_esi
;
246 saved_state
->ebp
= context
.sc_ebp
;
247 saved_state
->uesp
= context
.sc_esp
;
248 saved_state
->ss
= context
.sc_ss
;
249 saved_state
->efl
= context
.sc_eflags
;
250 saved_state
->efl
&= ~EFL_USERCLR
;
251 saved_state
->efl
|= EFL_USERSET
;
252 saved_state
->eip
= context
.sc_eip
;
253 saved_state
->cs
= context
.sc_cs
;
255 if (context
.sc_eflags
& EFL_VM
) {
256 saved_state
->ds
= NULL_SEG
;
257 saved_state
->es
= NULL_SEG
;
258 saved_state
->fs
= NULL_SEG
;
259 saved_state
->gs
= NULL_SEG
;
260 saved_state
->v86_segs
.v86_ds
= context
.sc_ds
;
261 saved_state
->v86_segs
.v86_es
= context
.sc_es
;
262 saved_state
->v86_segs
.v86_fs
= context
.sc_fs
;
263 saved_state
->v86_segs
.v86_gs
= context
.sc_gs
;
265 saved_state
->efl
|= EFL_VM
;
268 saved_state
->ds
= context
.sc_ds
;
269 saved_state
->es
= context
.sc_es
;
270 saved_state
->fs
= context
.sc_fs
;
271 saved_state
->gs
= context
.sc_gs
;
275 if (context
.sc_onstack
& 02) {
276 PCcontext_t context
= threadPCContext(thread
);
279 context
->running
= TRUE
;
283 return (EJUSTRETURN
);
287 * machine_exception() performs MD translation
288 * of a mach exception to a unix signal and code.
303 case EXC_BAD_INSTRUCTION
:
304 *unix_signal
= SIGILL
;
309 *unix_signal
= SIGFPE
;