]> git.saurik.com Git - apple/xnu.git/blob - bsd/uxkern/ux_exception.c
6eb09940faa0338518e2fb7e29566e414e0d8787
[apple/xnu.git] / bsd / uxkern / ux_exception.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1987 Carnegie-Mellon University
31 * All rights reserved. The CMU software License Agreement specifies
32 * the terms and conditions for use and redistribution.
33 */
34
35 /*
36 *********************************************************************
37 * HISTORY
38 **********************************************************************
39 */
40
41 #include <sys/param.h>
42
43 #include <mach/boolean.h>
44 #include <mach/exception.h>
45 #include <mach/kern_return.h>
46 #include <mach/message.h>
47 #include <mach/port.h>
48 #include <mach/mach_port.h>
49 #include <mach/mig_errors.h>
50 #include <mach/exc_server.h>
51 #include <mach/mach_exc_server.h>
52 #include <kern/task.h>
53 #include <kern/thread.h>
54 #include <kern/sched_prim.h>
55 #include <kern/kalloc.h>
56
57 #include <sys/proc.h>
58 #include <sys/user.h>
59 #include <sys/systm.h>
60 #include <sys/ux_exception.h>
61 #include <sys/vmparam.h> /* MAXSSIZ */
62
63 #include <vm/vm_protos.h> /* get_task_ipcspace() */
64 /*
65 * XXX Things that should be retrieved from Mach headers, but aren't
66 */
67 struct ipc_object;
68 extern kern_return_t ipc_object_copyin(ipc_space_t space, mach_port_name_t name,
69 mach_msg_type_name_t msgt_name, struct ipc_object **objectp);
70 extern mach_msg_return_t mach_msg_receive(mach_msg_header_t *msg,
71 mach_msg_option_t option, mach_msg_size_t rcv_size,
72 mach_port_name_t rcv_name, mach_msg_timeout_t rcv_timeout,
73 void (*continuation)(mach_msg_return_t),
74 mach_msg_size_t slist_size);
75 extern mach_msg_return_t mach_msg_send(mach_msg_header_t *msg,
76 mach_msg_option_t option, mach_msg_size_t send_size,
77 mach_msg_timeout_t send_timeout, mach_port_name_t notify);
78 extern thread_t convert_port_to_thread(ipc_port_t port);
79 extern void ipc_port_release(ipc_port_t);
80
81
82
83
84
85 /*
86 * Unix exception handler.
87 */
88
89 static void ux_exception(int exception, mach_exception_code_t code,
90 mach_exception_subcode_t subcode,
91 int *ux_signal, mach_exception_code_t *ux_code);
92
93 mach_port_name_t ux_exception_port;
94 static task_t ux_handler_self;
95
96 static
97 void
98 ux_handler(void)
99 {
100 task_t self = current_task();
101 mach_port_name_t exc_port_name;
102 mach_port_name_t exc_set_name;
103
104 /* self->kernel_vm_space = TRUE; */
105 ux_handler_self = self;
106
107
108 /*
109 * Allocate a port set that we will receive on.
110 */
111 if (mach_port_allocate(get_task_ipcspace(ux_handler_self), MACH_PORT_RIGHT_PORT_SET, &exc_set_name) != MACH_MSG_SUCCESS)
112 panic("ux_handler: port_set_allocate failed");
113
114 /*
115 * Allocate an exception port and use object_copyin to
116 * translate it to the global name. Put it into the set.
117 */
118 if (mach_port_allocate(get_task_ipcspace(ux_handler_self), MACH_PORT_RIGHT_RECEIVE, &exc_port_name) != MACH_MSG_SUCCESS)
119 panic("ux_handler: port_allocate failed");
120 if (mach_port_move_member(get_task_ipcspace(ux_handler_self),
121 exc_port_name, exc_set_name) != MACH_MSG_SUCCESS)
122 panic("ux_handler: port_set_add failed");
123
124 if (ipc_object_copyin(get_task_ipcspace(self), exc_port_name,
125 MACH_MSG_TYPE_MAKE_SEND,
126 (void *) &ux_exception_port) != MACH_MSG_SUCCESS)
127 panic("ux_handler: object_copyin(ux_exception_port) failed");
128
129 thread_wakeup(&ux_exception_port);
130
131 /* Message handling loop. */
132
133 for (;;) {
134 struct rep_msg {
135 mach_msg_header_t Head;
136 NDR_record_t NDR;
137 kern_return_t RetCode;
138 } rep_msg;
139 struct exc_msg {
140 mach_msg_header_t Head;
141 /* start of the kernel processed data */
142 mach_msg_body_t msgh_body;
143 mach_msg_port_descriptor_t thread;
144 mach_msg_port_descriptor_t task;
145 /* end of the kernel processed data */
146 NDR_record_t NDR;
147 exception_type_t exception;
148 mach_msg_type_number_t codeCnt;
149 mach_exception_data_t code;
150 /* some times RCV_TO_LARGE probs */
151 char pad[512];
152 } exc_msg;
153 mach_port_name_t reply_port;
154 kern_return_t result;
155
156 exc_msg.Head.msgh_local_port = (mach_port_t)exc_set_name;
157 exc_msg.Head.msgh_size = sizeof (exc_msg);
158 #if 0
159 result = mach_msg_receive(&exc_msg.Head);
160 #else
161 result = mach_msg_receive(&exc_msg.Head, MACH_RCV_MSG,
162 sizeof (exc_msg), exc_set_name,
163 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL,
164 0);
165 #endif
166 if (result == MACH_MSG_SUCCESS) {
167 reply_port = (mach_port_name_t)exc_msg.Head.msgh_remote_port;
168
169 if (mach_exc_server(&exc_msg.Head, &rep_msg.Head))
170 (void) mach_msg_send(&rep_msg.Head, MACH_SEND_MSG,
171 sizeof (rep_msg),MACH_MSG_TIMEOUT_NONE,MACH_PORT_NULL);
172
173 if (reply_port != MACH_PORT_NULL)
174 (void) mach_port_deallocate(get_task_ipcspace(ux_handler_self), reply_port);
175 }
176 else if (result == MACH_RCV_TOO_LARGE)
177 /* ignore oversized messages */;
178 else
179 panic("exception_handler");
180 }
181 }
182
183 void
184 ux_handler_init(void)
185 {
186 ux_exception_port = MACH_PORT_NULL;
187 (void) kernel_thread(kernel_task, ux_handler);
188 if (ux_exception_port == MACH_PORT_NULL) {
189 assert_wait(&ux_exception_port, THREAD_UNINT);
190 thread_block(THREAD_CONTINUE_NULL);
191 }
192 }
193
194 kern_return_t
195 catch_exception_raise(
196 __unused mach_port_t exception_port,
197 mach_port_t thread,
198 mach_port_t task,
199 exception_type_t exception,
200 exception_data_t code,
201 __unused mach_msg_type_number_t codeCnt
202 )
203 {
204 mach_exception_data_type_t big_code[EXCEPTION_CODE_MAX];
205 big_code[0] = code[0];
206 big_code[1] = code[1];
207
208 return catch_mach_exception_raise(exception_port,
209 thread,
210 task,
211 exception,
212 big_code,
213 codeCnt);
214
215 }
216
217 kern_return_t
218 catch_mach_exception_raise(
219 __unused mach_port_t exception_port,
220 mach_port_t thread,
221 mach_port_t task,
222 exception_type_t exception,
223 mach_exception_data_t code,
224 __unused mach_msg_type_number_t codeCnt
225 )
226 {
227 task_t self = current_task();
228 thread_t th_act;
229 ipc_port_t thread_port;
230 struct task *sig_task;
231 struct proc *p;
232 kern_return_t result = MACH_MSG_SUCCESS;
233 int ux_signal = 0;
234 mach_exception_code_t ucode = 0;
235 struct uthread *ut;
236 mach_port_name_t thread_name = (mach_port_name_t)thread; /* XXX */
237 mach_port_name_t task_name = (mach_port_name_t)task; /* XXX */
238
239 /*
240 * Convert local thread name to global port.
241 */
242 if (MACH_PORT_VALID(thread_name) &&
243 (ipc_object_copyin(get_task_ipcspace(self), thread_name,
244 MACH_MSG_TYPE_PORT_SEND,
245 (void *) &thread_port) == MACH_MSG_SUCCESS)) {
246 if (IPC_PORT_VALID(thread_port)) {
247 th_act = convert_port_to_thread(thread_port);
248 ipc_port_release(thread_port);
249 } else {
250 th_act = THREAD_NULL;
251 }
252
253 /*
254 * Catch bogus ports
255 */
256 if (th_act != THREAD_NULL) {
257
258 /*
259 * Convert exception to unix signal and code.
260 */
261 ux_exception(exception, code[0], code[1], &ux_signal, &ucode);
262
263 ut = get_bsdthread_info(th_act);
264 sig_task = get_threadtask(th_act);
265 p = (struct proc *) get_bsdtask_info(sig_task);
266
267 /* Can't deliver a signal without a bsd process */
268 if (p == NULL) {
269 ux_signal = 0;
270 result = KERN_FAILURE;
271 }
272
273 /*
274 * Stack overflow should result in a SIGSEGV signal
275 * on the alternate stack.
276 * but we have one or more guard pages after the
277 * stack top, so we would get a KERN_PROTECTION_FAILURE
278 * exception instead of KERN_INVALID_ADDRESS, resulting in
279 * a SIGBUS signal.
280 * Detect that situation and select the correct signal.
281 */
282 if (code[0] == KERN_PROTECTION_FAILURE &&
283 ux_signal == SIGBUS) {
284 user_addr_t sp, stack_min, stack_max;
285 int mask;
286 struct sigacts *ps;
287
288 sp = code[1];
289 if (ut && (ut->uu_flag & UT_VFORK))
290 p = ut->uu_proc;
291 #if STACK_GROWTH_UP
292 stack_min = p->user_stack;
293 stack_max = p->user_stack + MAXSSIZ;
294 #else /* STACK_GROWTH_UP */
295 stack_max = p->user_stack;
296 stack_min = p->user_stack - MAXSSIZ;
297 #endif /* STACK_GROWTH_UP */
298 if (sp >= stack_min &&
299 sp < stack_max) {
300 /*
301 * This is indeed a stack overflow. Deliver a
302 * SIGSEGV signal.
303 */
304 ux_signal = SIGSEGV;
305
306 /*
307 * If the thread/process is not ready to handle
308 * SIGSEGV on an alternate stack, force-deliver
309 * SIGSEGV with a SIG_DFL handler.
310 */
311 mask = sigmask(ux_signal);
312 ps = p->p_sigacts;
313 if ((p->p_sigignore & mask) ||
314 (ut->uu_sigwait & mask) ||
315 (ut->uu_sigmask & mask) ||
316 (ps->ps_sigact[SIGSEGV] == SIG_IGN) ||
317 (! (ps->ps_sigonstack & mask))) {
318 p->p_sigignore &= ~mask;
319 p->p_sigcatch &= ~mask;
320 ps->ps_sigact[SIGSEGV] = SIG_DFL;
321 ut->uu_sigwait &= ~mask;
322 ut->uu_sigmask &= ~mask;
323 }
324 }
325 }
326 /*
327 * Send signal.
328 */
329 if (ux_signal != 0) {
330 ut->uu_exception = exception;
331 //ut->uu_code = code[0]; // filled in by threadsignal
332 ut->uu_subcode = code[1];
333 threadsignal(th_act, ux_signal, code[0]);
334 }
335
336 thread_deallocate(th_act);
337 }
338 else
339 result = KERN_INVALID_ARGUMENT;
340 }
341 else
342 result = KERN_INVALID_ARGUMENT;
343
344 /*
345 * Delete our send rights to the task and thread ports.
346 */
347 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), task_name);
348 (void)mach_port_deallocate(get_task_ipcspace(ux_handler_self), thread_name);
349
350 return (result);
351 }
352
353 kern_return_t
354 catch_exception_raise_state(
355 __unused mach_port_t exception_port,
356 __unused exception_type_t exception,
357 __unused const exception_data_t code,
358 __unused mach_msg_type_number_t codeCnt,
359 __unused int *flavor,
360 __unused const thread_state_t old_state,
361 __unused mach_msg_type_number_t old_stateCnt,
362 __unused thread_state_t new_state,
363 __unused mach_msg_type_number_t *new_stateCnt)
364 {
365 return(KERN_INVALID_ARGUMENT);
366 }
367
368 kern_return_t
369 catch_mach_exception_raise_state(
370 __unused mach_port_t exception_port,
371 __unused exception_type_t exception,
372 __unused const mach_exception_data_t code,
373 __unused mach_msg_type_number_t codeCnt,
374 __unused int *flavor,
375 __unused const thread_state_t old_state,
376 __unused mach_msg_type_number_t old_stateCnt,
377 __unused thread_state_t new_state,
378 __unused mach_msg_type_number_t *new_stateCnt)
379 {
380 return(KERN_INVALID_ARGUMENT);
381 }
382
383 kern_return_t
384 catch_exception_raise_state_identity(
385 __unused mach_port_t exception_port,
386 __unused mach_port_t thread,
387 __unused mach_port_t task,
388 __unused exception_type_t exception,
389 __unused exception_data_t code,
390 __unused mach_msg_type_number_t codeCnt,
391 __unused int *flavor,
392 __unused thread_state_t old_state,
393 __unused mach_msg_type_number_t old_stateCnt,
394 __unused thread_state_t new_state,
395 __unused mach_msg_type_number_t *new_stateCnt)
396 {
397 return(KERN_INVALID_ARGUMENT);
398 }
399
400 kern_return_t
401 catch_mach_exception_raise_state_identity(
402 __unused mach_port_t exception_port,
403 __unused mach_port_t thread,
404 __unused mach_port_t task,
405 __unused exception_type_t exception,
406 __unused mach_exception_data_t code,
407 __unused mach_msg_type_number_t codeCnt,
408 __unused int *flavor,
409 __unused thread_state_t old_state,
410 __unused mach_msg_type_number_t old_stateCnt,
411 __unused thread_state_t new_state,
412 __unused mach_msg_type_number_t *new_stateCnt)
413 {
414 return(KERN_INVALID_ARGUMENT);
415 }
416
417
418 /*
419 * ux_exception translates a mach exception, code and subcode to
420 * a signal and u.u_code. Calls machine_exception (machine dependent)
421 * to attempt translation first.
422 */
423
424 static
425 void ux_exception(
426 int exception,
427 mach_exception_code_t code,
428 mach_exception_subcode_t subcode,
429 int *ux_signal,
430 mach_exception_code_t *ux_code)
431 {
432 /*
433 * Try machine-dependent translation first.
434 */
435 if (machine_exception(exception, code, subcode, ux_signal, ux_code))
436 return;
437
438 switch(exception) {
439
440 case EXC_BAD_ACCESS:
441 if (code == KERN_INVALID_ADDRESS)
442 *ux_signal = SIGSEGV;
443 else
444 *ux_signal = SIGBUS;
445 break;
446
447 case EXC_BAD_INSTRUCTION:
448 *ux_signal = SIGILL;
449 break;
450
451 case EXC_ARITHMETIC:
452 *ux_signal = SIGFPE;
453 break;
454
455 case EXC_EMULATION:
456 *ux_signal = SIGEMT;
457 break;
458
459 case EXC_SOFTWARE:
460 switch (code) {
461
462 case EXC_UNIX_BAD_SYSCALL:
463 *ux_signal = SIGSYS;
464 break;
465 case EXC_UNIX_BAD_PIPE:
466 *ux_signal = SIGPIPE;
467 break;
468 case EXC_UNIX_ABORT:
469 *ux_signal = SIGABRT;
470 break;
471 case EXC_SOFT_SIGNAL:
472 *ux_signal = SIGKILL;
473 break;
474 }
475 break;
476
477 case EXC_BREAKPOINT:
478 *ux_signal = SIGTRAP;
479 break;
480 }
481 }