]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/exception.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / osfmk / kern / 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 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 #include <mach/mach_types.h>
60 #include <mach/boolean.h>
61 #include <mach/kern_return.h>
62 #include <mach/message.h>
63 #include <mach/port.h>
64 #include <mach/mig_errors.h>
65 #include <mach/task.h>
66 #include <mach/thread_status.h>
67 #include <mach/exception_types.h>
68 #include <mach/exc.h>
69 #include <mach/mach_exc.h>
70
71 #include <ipc/port.h>
72 #include <ipc/ipc_entry.h>
73 #include <ipc/ipc_object.h>
74 #include <ipc/ipc_notify.h>
75 #include <ipc/ipc_space.h>
76 #include <ipc/ipc_pset.h>
77 #include <ipc/ipc_machdep.h>
78
79 #include <kern/counters.h>
80 #include <kern/ipc_tt.h>
81 #include <kern/task.h>
82 #include <kern/thread.h>
83 #include <kern/processor.h>
84 #include <kern/sched.h>
85 #include <kern/sched_prim.h>
86 #include <kern/host.h>
87 #include <kern/misc_protos.h>
88 #include <kern/ux_handler.h>
89
90 #include <security/mac_mach_internal.h>
91 #include <string.h>
92
93 #include <pexpert/pexpert.h>
94
95 bool panic_on_exception_triage = false;
96
97 unsigned long c_thr_exc_raise = 0;
98 unsigned long c_thr_exc_raise_state = 0;
99 unsigned long c_thr_exc_raise_state_id = 0;
100 unsigned long c_tsk_exc_raise = 0;
101 unsigned long c_tsk_exc_raise_state = 0;
102 unsigned long c_tsk_exc_raise_state_id = 0;
103
104 /* forward declarations */
105 kern_return_t exception_deliver(
106 thread_t thread,
107 exception_type_t exception,
108 mach_exception_data_t code,
109 mach_msg_type_number_t codeCnt,
110 struct exception_action *excp,
111 lck_mtx_t *mutex);
112
113 static kern_return_t
114 check_exc_receiver_dependency(
115 exception_type_t exception,
116 struct exception_action *excp,
117 lck_mtx_t *mutex);
118
119 #ifdef MACH_BSD
120 kern_return_t bsd_exception(
121 exception_type_t exception,
122 mach_exception_data_t code,
123 mach_msg_type_number_t codeCnt);
124 #endif /* MACH_BSD */
125
126 /*
127 * Routine: exception_init
128 * Purpose:
129 * Global initialization of state for exceptions.
130 * Conditions:
131 * None.
132 */
133 void
134 exception_init(void)
135 {
136 int tmp = 0;
137
138 if (PE_parse_boot_argn("-panic_on_exception_triage", &tmp, sizeof(tmp))) {
139 panic_on_exception_triage = true;
140 }
141 }
142
143 /*
144 * Routine: exception_deliver
145 * Purpose:
146 * Make an upcall to the exception server provided.
147 * Conditions:
148 * Nothing locked and no resources held.
149 * Called from an exception context, so
150 * thread_exception_return and thread_kdb_return
151 * are possible.
152 * Returns:
153 * KERN_SUCCESS if the exception was handled
154 */
155 kern_return_t
156 exception_deliver(
157 thread_t thread,
158 exception_type_t exception,
159 mach_exception_data_t code,
160 mach_msg_type_number_t codeCnt,
161 struct exception_action *excp,
162 lck_mtx_t *mutex)
163 {
164 ipc_port_t exc_port = IPC_PORT_NULL;
165 exception_data_type_t small_code[EXCEPTION_CODE_MAX];
166 int code64;
167 int behavior;
168 int flavor;
169 kern_return_t kr;
170 task_t task;
171 ipc_port_t thread_port = IPC_PORT_NULL, task_port = IPC_PORT_NULL;
172
173 /*
174 * Save work if we are terminating.
175 * Just go back to our AST handler.
176 */
177 if (!thread->active && !thread->inspection) {
178 return KERN_SUCCESS;
179 }
180
181 /*
182 * If there are no exception actions defined for this entity,
183 * we can't deliver here.
184 */
185 if (excp == NULL) {
186 return KERN_FAILURE;
187 }
188
189 assert(exception < EXC_TYPES_COUNT);
190 if (exception >= EXC_TYPES_COUNT) {
191 return KERN_FAILURE;
192 }
193
194 excp = &excp[exception];
195
196 /*
197 * Snapshot the exception action data under lock for consistency.
198 * Hold a reference to the port over the exception_raise_* calls
199 * so it can't be destroyed. This seems like overkill, but keeps
200 * the port from disappearing between now and when
201 * ipc_object_copyin_from_kernel is finally called.
202 */
203 lck_mtx_lock(mutex);
204 exc_port = excp->port;
205 if (!IP_VALID(exc_port)) {
206 lck_mtx_unlock(mutex);
207 return KERN_FAILURE;
208 }
209 ip_lock(exc_port);
210 if (!ip_active(exc_port)) {
211 ip_unlock(exc_port);
212 lck_mtx_unlock(mutex);
213 return KERN_FAILURE;
214 }
215 ip_reference(exc_port);
216 exc_port->ip_srights++;
217 ip_unlock(exc_port);
218
219 flavor = excp->flavor;
220 behavior = excp->behavior;
221 lck_mtx_unlock(mutex);
222
223 code64 = (behavior & MACH_EXCEPTION_CODES);
224 behavior &= ~MACH_EXCEPTION_MASK;
225
226 if (!code64) {
227 small_code[0] = CAST_DOWN_EXPLICIT(exception_data_type_t, code[0]);
228 small_code[1] = CAST_DOWN_EXPLICIT(exception_data_type_t, code[1]);
229 }
230
231 task = thread->task;
232
233 #if CONFIG_MACF
234 /* Now is a reasonably good time to check if the exception action is
235 * permitted for this process, because after this point we will send
236 * the message out almost certainly.
237 * As with other failures, exception_triage_thread will go on
238 * to the next level.
239 */
240
241 /* The global exception-to-signal translation port is safe to be an exception handler. */
242 if (is_ux_handler_port(exc_port) == FALSE &&
243 mac_exc_action_check_exception_send(task, excp) != 0) {
244 kr = KERN_FAILURE;
245 goto out_release_right;
246 }
247 #endif
248
249 if (behavior != EXCEPTION_STATE) {
250 task_reference(task);
251 task_port = convert_task_to_port(task);
252 /* task ref consumed */
253 thread_reference(thread);
254 thread_port = convert_thread_to_port(thread);
255 /* thread ref consumed */
256 }
257
258 switch (behavior) {
259 case EXCEPTION_STATE: {
260 mach_msg_type_number_t state_cnt;
261 thread_state_data_t state;
262
263 c_thr_exc_raise_state++;
264 state_cnt = _MachineStateCount[flavor];
265 kr = thread_getstatus_to_user(thread, flavor,
266 (thread_state_t)state,
267 &state_cnt);
268 if (kr == KERN_SUCCESS) {
269 if (code64) {
270 kr = mach_exception_raise_state(exc_port,
271 exception,
272 code,
273 codeCnt,
274 &flavor,
275 state, state_cnt,
276 state, &state_cnt);
277 } else {
278 kr = exception_raise_state(exc_port, exception,
279 small_code,
280 codeCnt,
281 &flavor,
282 state, state_cnt,
283 state, &state_cnt);
284 }
285 if (kr == KERN_SUCCESS) {
286 if (exception != EXC_CORPSE_NOTIFY) {
287 kr = thread_setstatus_from_user(thread, flavor,
288 (thread_state_t)state,
289 state_cnt);
290 }
291 goto out_release_right;
292 }
293 }
294
295 goto out_release_right;
296 }
297
298 case EXCEPTION_DEFAULT:
299 c_thr_exc_raise++;
300 if (code64) {
301 kr = mach_exception_raise(exc_port,
302 thread_port,
303 task_port,
304 exception,
305 code,
306 codeCnt);
307 } else {
308 kr = exception_raise(exc_port,
309 thread_port,
310 task_port,
311 exception,
312 small_code,
313 codeCnt);
314 }
315
316 goto out_release_right;
317
318 case EXCEPTION_STATE_IDENTITY: {
319 mach_msg_type_number_t state_cnt;
320 thread_state_data_t state;
321
322 c_thr_exc_raise_state_id++;
323 state_cnt = _MachineStateCount[flavor];
324 kr = thread_getstatus_to_user(thread, flavor,
325 (thread_state_t)state,
326 &state_cnt);
327 if (kr == KERN_SUCCESS) {
328 if (code64) {
329 kr = mach_exception_raise_state_identity(
330 exc_port,
331 thread_port,
332 task_port,
333 exception,
334 code,
335 codeCnt,
336 &flavor,
337 state, state_cnt,
338 state, &state_cnt);
339 } else {
340 kr = exception_raise_state_identity(exc_port,
341 thread_port,
342 task_port,
343 exception,
344 small_code,
345 codeCnt,
346 &flavor,
347 state, state_cnt,
348 state, &state_cnt);
349 }
350
351 if (kr == KERN_SUCCESS) {
352 if (exception != EXC_CORPSE_NOTIFY) {
353 kr = thread_setstatus_from_user(thread, flavor,
354 (thread_state_t)state,
355 state_cnt);
356 }
357 goto out_release_right;
358 }
359 }
360
361 goto out_release_right;
362 }
363
364 default:
365 panic("bad exception behavior!");
366 return KERN_FAILURE;
367 }/* switch */
368
369 out_release_right:
370
371 if (task_port) {
372 ipc_port_release_send(task_port);
373 }
374
375 if (thread_port) {
376 ipc_port_release_send(thread_port);
377 }
378
379 if (exc_port) {
380 ipc_port_release_send(exc_port);
381 }
382
383 return kr;
384 }
385
386 /*
387 * Routine: check_exc_receiver_dependency
388 * Purpose:
389 * Verify that the port destined for receiving this exception is not
390 * on the current task. This would cause hang in kernel for
391 * EXC_CRASH primarily. Note: If port is transferred
392 * between check and delivery then deadlock may happen.
393 *
394 * Conditions:
395 * Nothing locked and no resources held.
396 * Called from an exception context.
397 * Returns:
398 * KERN_SUCCESS if its ok to send exception message.
399 */
400 kern_return_t
401 check_exc_receiver_dependency(
402 exception_type_t exception,
403 struct exception_action *excp,
404 lck_mtx_t *mutex)
405 {
406 kern_return_t retval = KERN_SUCCESS;
407
408 if (excp == NULL || exception != EXC_CRASH) {
409 return retval;
410 }
411
412 task_t task = current_task();
413 lck_mtx_lock(mutex);
414 ipc_port_t xport = excp[exception].port;
415 if (IP_VALID(xport)
416 && ip_active(xport)
417 && task->itk_space == xport->ip_receiver) {
418 retval = KERN_FAILURE;
419 }
420 lck_mtx_unlock(mutex);
421 return retval;
422 }
423
424
425 /*
426 * Routine: exception_triage_thread
427 * Purpose:
428 * The thread caught an exception.
429 * We make an up-call to the thread's exception server.
430 * Conditions:
431 * Nothing locked and no resources held.
432 * Called from an exception context, so
433 * thread_exception_return and thread_kdb_return
434 * are possible.
435 * Returns:
436 * KERN_SUCCESS if exception is handled by any of the handlers.
437 */
438 kern_return_t
439 exception_triage_thread(
440 exception_type_t exception,
441 mach_exception_data_t code,
442 mach_msg_type_number_t codeCnt,
443 thread_t thread)
444 {
445 task_t task;
446 host_priv_t host_priv;
447 lck_mtx_t *mutex;
448 kern_return_t kr = KERN_FAILURE;
449
450 assert(exception != EXC_RPC_ALERT);
451
452 /*
453 * If this behavior has been requested by the the kernel
454 * (due to the boot environment), we should panic if we
455 * enter this function. This is intended as a debugging
456 * aid; it should allow us to debug why we caught an
457 * exception in environments where debugging is especially
458 * difficult.
459 */
460 if (panic_on_exception_triage) {
461 panic("called exception_triage when it was forbidden by the boot environment");
462 }
463
464 /*
465 * Try to raise the exception at the activation level.
466 */
467 mutex = &thread->mutex;
468 if (KERN_SUCCESS == check_exc_receiver_dependency(exception, thread->exc_actions, mutex)) {
469 kr = exception_deliver(thread, exception, code, codeCnt, thread->exc_actions, mutex);
470 if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
471 goto out;
472 }
473 }
474
475 /*
476 * Maybe the task level will handle it.
477 */
478 task = thread->task;
479 mutex = &task->itk_lock_data;
480 if (KERN_SUCCESS == check_exc_receiver_dependency(exception, task->exc_actions, mutex)) {
481 kr = exception_deliver(thread, exception, code, codeCnt, task->exc_actions, mutex);
482 if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
483 goto out;
484 }
485 }
486
487 /*
488 * How about at the host level?
489 */
490 host_priv = host_priv_self();
491 mutex = &host_priv->lock;
492
493 if (KERN_SUCCESS == check_exc_receiver_dependency(exception, host_priv->exc_actions, mutex)) {
494 kr = exception_deliver(thread, exception, code, codeCnt, host_priv->exc_actions, mutex);
495 if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
496 goto out;
497 }
498 }
499
500 out:
501 if ((exception != EXC_CRASH) && (exception != EXC_RESOURCE) &&
502 (exception != EXC_GUARD) && (exception != EXC_CORPSE_NOTIFY)) {
503 thread_exception_return();
504 }
505 return kr;
506 }
507
508 /*
509 * Routine: exception_triage
510 * Purpose:
511 * The current thread caught an exception.
512 * We make an up-call to the thread's exception server.
513 * Conditions:
514 * Nothing locked and no resources held.
515 * Called from an exception context, so
516 * thread_exception_return and thread_kdb_return
517 * are possible.
518 * Returns:
519 * KERN_SUCCESS if exception is handled by any of the handlers.
520 */
521 kern_return_t
522 exception_triage(
523 exception_type_t exception,
524 mach_exception_data_t code,
525 mach_msg_type_number_t codeCnt)
526 {
527 thread_t thread = current_thread();
528 return exception_triage_thread(exception, code, codeCnt, thread);
529 }
530
531 kern_return_t
532 bsd_exception(
533 exception_type_t exception,
534 mach_exception_data_t code,
535 mach_msg_type_number_t codeCnt)
536 {
537 task_t task;
538 lck_mtx_t *mutex;
539 thread_t self = current_thread();
540 kern_return_t kr;
541
542 /*
543 * Maybe the task level will handle it.
544 */
545 task = current_task();
546 mutex = &task->itk_lock_data;
547
548 kr = exception_deliver(self, exception, code, codeCnt, task->exc_actions, mutex);
549
550 if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
551 return KERN_SUCCESS;
552 }
553 return KERN_FAILURE;
554 }
555
556
557 /*
558 * Raise an exception on a task.
559 * This should tell launchd to launch Crash Reporter for this task.
560 */
561 kern_return_t
562 task_exception_notify(exception_type_t exception,
563 mach_exception_data_type_t exccode, mach_exception_data_type_t excsubcode)
564 {
565 mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
566 wait_interrupt_t wsave;
567 kern_return_t kr = KERN_SUCCESS;
568
569 code[0] = exccode;
570 code[1] = excsubcode;
571
572 wsave = thread_interrupt_level(THREAD_UNINT);
573 kr = exception_triage(exception, code, EXCEPTION_CODE_MAX);
574 (void) thread_interrupt_level(wsave);
575 return kr;
576 }
577
578
579 /*
580 * Handle interface for special performance monitoring
581 * This is a special case of the host exception handler
582 */
583 kern_return_t
584 sys_perf_notify(thread_t thread, int pid)
585 {
586 host_priv_t hostp;
587 ipc_port_t xport;
588 wait_interrupt_t wsave;
589 kern_return_t ret;
590
591 hostp = host_priv_self(); /* Get the host privileged ports */
592 mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
593 code[0] = 0xFF000001; /* Set terminate code */
594 code[1] = pid; /* Pass out the pid */
595
596 struct task *task = thread->task;
597 xport = hostp->exc_actions[EXC_RPC_ALERT].port;
598
599 /* Make sure we're not catching our own exception */
600 if (!IP_VALID(xport) ||
601 !ip_active(xport) ||
602 task->itk_space == xport->data.receiver) {
603 return KERN_FAILURE;
604 }
605
606 wsave = thread_interrupt_level(THREAD_UNINT);
607 ret = exception_deliver(
608 thread,
609 EXC_RPC_ALERT,
610 code,
611 2,
612 hostp->exc_actions,
613 &hostp->lock);
614 (void)thread_interrupt_level(wsave);
615
616 return ret;
617 }