]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/unix_signal.c
6cd4684f3becbc1a318940130d54fd65209f9aba
[apple/xnu.git] / bsd / dev / i386 / unix_signal.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1992 NeXT, Inc.
27 *
28 * HISTORY
29 * 13 May 1992 ? at NeXT
30 * Created.
31 */
32
33 #include <mach/mach_types.h>
34 #include <mach/exception.h>
35
36 #include <kern/thread.h>
37 #include <kern/thread_act.h>
38
39 #include <sys/param.h>
40 #include <sys/proc.h>
41 #include <sys/user.h>
42
43 #include <i386/psl.h>
44
45 #include <mach/i386/thread_status.h>
46 #include <dev/i386/sel_inline.h>
47
48
49 /*
50 * FIXME.. should be included from mach_kernel/i386/seg.h
51 */
52
53 #define USER_CS 0x17
54 #define USER_DS 0x1f
55
56 #define UDATA_SEL USER_DS
57 #define UCODE_SEL USER_CS
58
59 #define valid_user_code_selector(x) (TRUE)
60 #define valid_user_data_selector(x) (TRUE)
61 #define valid_user_stack_selector(x) (TRUE)
62
63
64 #define NULL_SEG 0
65
66 /* Signal handler flavors supported */
67 /* These defns should match the Libc implmn */
68 #define UC_TRAD 1
69
70 /*
71 * Send an interrupt to process.
72 *
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.
79 */
80
81 void
82 sendsig(p, catcher, sig, mask, code)
83 struct proc *p;
84 sig_t catcher;
85 int sig, mask;
86 u_long code;
87 {
88 struct sigframe {
89 int retaddr;
90 sig_t catcher;
91 int sigstyle;
92 int sig;
93 int code;
94 struct sigcontext * scp;
95 } frame, *fp;
96 struct sigcontext context, *scp;
97 struct sigacts *ps = p->p_sigacts;
98 int oonstack;
99 thread_t thread = current_thread();
100 thread_act_t th_act = current_act();
101 struct uthread * ut;
102 struct i386_saved_state * saved_state = get_user_regs(th_act);
103 sig_t trampact;
104
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;
111 } else
112 scp = ((struct sigcontext *)saved_state->uesp) - 1;
113 fp = ((struct sigframe *)scp) - 1;
114
115 /*
116 * Build the argument list for the signal handler.
117 */
118
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;
124 frame.sig = sig;
125
126 if (sig == SIGILL || sig == SIGFPE) {
127 frame.code = code;
128 } else
129 frame.code = 0;
130 frame.scp = scp;
131 if (copyout((caddr_t)&frame, (caddr_t)fp, sizeof (frame)))
132 goto bad;
133
134 #if PC_SUPPORT
135 {
136 PCcontext_t context = threadPCContext(thread);
137
138 if (context && context->running) {
139 oonstack |= 02;
140 context->running = FALSE;
141 }
142 }
143 #endif
144 /*
145 * Build the signal context to be used by sigreturn.
146 */
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;
166
167 saved_state->efl &= ~EFL_VM;
168 } else {
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;
173 }
174 if (copyout((caddr_t)&context, (caddr_t)scp, sizeof (context)))
175 goto bad;
176
177 saved_state->eip = (unsigned int)trampact;
178 saved_state->cs = UCODE_SEL;
179
180 saved_state->uesp = (unsigned int)fp;
181 saved_state->ss = UDATA_SEL;
182
183 saved_state->ds = UDATA_SEL;
184 saved_state->es = UDATA_SEL;
185 saved_state->fs = NULL_SEG;
186 saved_state->gs = NULL_SEG;
187 return;
188
189 bad:
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);
197 return;
198 }
199
200 /*
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
208 * a machine fault.
209 */
210 struct sigreturn_args {
211 struct sigcontext *sigcntxp;
212 };
213 /* ARGSUSED */
214 int
215 sigreturn(p, uap, retval)
216 struct proc *p;
217 struct sigreturn_args *uap;
218 int *retval;
219 {
220 struct sigcontext context;
221 thread_t thread = current_thread();
222 thread_act_t th_act = current_act();
223 int error;
224 struct i386_saved_state* saved_state = get_user_regs(th_act);
225 struct uthread * ut;
226
227
228
229 if (saved_state == NULL)
230 return EINVAL;
231
232 if (error = copyin((caddr_t)uap->sigcntxp, (caddr_t)&context,
233 sizeof (context)))
234 return(error);
235 ut = (struct uthread *)get_bsdthread_info(th_act);
236
237 if (context.sc_onstack & 01)
238 p->p_sigacts->ps_sigstk.ss_flags |= SA_ONSTACK;
239 else
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;
258
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;
268
269 saved_state->efl |= EFL_VM;
270 }
271 else {
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;
276 }
277
278 #if PC_SUPPORT
279 if (context.sc_onstack & 02) {
280 PCcontext_t context = threadPCContext(thread);
281
282 if (context)
283 context->running = TRUE;
284 }
285 #endif
286
287 return (EJUSTRETURN);
288 }
289
290 /*
291 * machine_exception() performs MD translation
292 * of a mach exception to a unix signal and code.
293 */
294
295 boolean_t
296 machine_exception(
297 int exception,
298 int code,
299 int subcode,
300 int *unix_signal,
301 int *unix_code
302 )
303 {
304
305 switch(exception) {
306
307 case EXC_BAD_INSTRUCTION:
308 *unix_signal = SIGILL;
309 *unix_code = code;
310 break;
311
312 case EXC_ARITHMETIC:
313 *unix_signal = SIGFPE;
314 *unix_code = code;
315 break;
316
317 default:
318 return(FALSE);
319 }
320
321 return(TRUE);
322 }