2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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>
69 #include <mach/mach_exc.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>
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>
90 #include <security/mac_mach_internal.h>
93 #include <pexpert/pexpert.h>
95 bool panic_on_exception_triage
= false;
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;
104 /* forward declarations */
105 kern_return_t
exception_deliver(
107 exception_type_t exception
,
108 mach_exception_data_t code
,
109 mach_msg_type_number_t codeCnt
,
110 struct exception_action
*excp
,
114 check_exc_receiver_dependency(
115 exception_type_t exception
,
116 struct exception_action
*excp
,
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 */
127 * Routine: exception_init
129 * Global initialization of state for exceptions.
138 if (PE_parse_boot_argn("-panic_on_exception_triage", &tmp
, sizeof(tmp
))) {
139 panic_on_exception_triage
= true;
144 * Routine: exception_deliver
146 * Make an upcall to the exception server provided.
148 * Nothing locked and no resources held.
149 * Called from an exception context, so
150 * thread_exception_return and thread_kdb_return
153 * KERN_SUCCESS if the exception was handled
158 exception_type_t exception
,
159 mach_exception_data_t code
,
160 mach_msg_type_number_t codeCnt
,
161 struct exception_action
*excp
,
164 ipc_port_t exc_port
= IPC_PORT_NULL
;
165 exception_data_type_t small_code
[EXCEPTION_CODE_MAX
];
171 ipc_port_t thread_port
= IPC_PORT_NULL
, task_port
= IPC_PORT_NULL
;
174 * Save work if we are terminating.
175 * Just go back to our AST handler.
177 if (!thread
->active
&& !thread
->inspection
) {
182 * If there are no exception actions defined for this entity,
183 * we can't deliver here.
189 assert(exception
< EXC_TYPES_COUNT
);
190 if (exception
>= EXC_TYPES_COUNT
) {
194 excp
= &excp
[exception
];
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.
204 exc_port
= excp
->port
;
205 if (!IP_VALID(exc_port
)) {
206 lck_mtx_unlock(mutex
);
210 if (!ip_active(exc_port
)) {
212 lck_mtx_unlock(mutex
);
215 ip_reference(exc_port
);
216 exc_port
->ip_srights
++;
219 flavor
= excp
->flavor
;
220 behavior
= excp
->behavior
;
221 lck_mtx_unlock(mutex
);
223 code64
= (behavior
& MACH_EXCEPTION_CODES
);
224 behavior
&= ~MACH_EXCEPTION_MASK
;
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]);
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
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) {
245 goto out_release_right
;
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 */
259 case EXCEPTION_STATE
: {
260 mach_msg_type_number_t state_cnt
;
261 thread_state_data_t state
;
263 c_thr_exc_raise_state
++;
264 state_cnt
= _MachineStateCount
[flavor
];
265 kr
= thread_getstatus_to_user(thread
, flavor
,
266 (thread_state_t
)state
,
268 if (kr
== KERN_SUCCESS
) {
270 kr
= mach_exception_raise_state(exc_port
,
278 kr
= exception_raise_state(exc_port
, exception
,
285 if (kr
== KERN_SUCCESS
) {
286 if (exception
!= EXC_CORPSE_NOTIFY
) {
287 kr
= thread_setstatus_from_user(thread
, flavor
,
288 (thread_state_t
)state
,
291 goto out_release_right
;
295 goto out_release_right
;
298 case EXCEPTION_DEFAULT
:
301 kr
= mach_exception_raise(exc_port
,
308 kr
= exception_raise(exc_port
,
316 goto out_release_right
;
318 case EXCEPTION_STATE_IDENTITY
: {
319 mach_msg_type_number_t state_cnt
;
320 thread_state_data_t state
;
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
,
327 if (kr
== KERN_SUCCESS
) {
329 kr
= mach_exception_raise_state_identity(
340 kr
= exception_raise_state_identity(exc_port
,
351 if (kr
== KERN_SUCCESS
) {
352 if (exception
!= EXC_CORPSE_NOTIFY
) {
353 kr
= thread_setstatus_from_user(thread
, flavor
,
354 (thread_state_t
)state
,
357 goto out_release_right
;
361 goto out_release_right
;
365 panic("bad exception behavior!");
372 ipc_port_release_send(task_port
);
376 ipc_port_release_send(thread_port
);
380 ipc_port_release_send(exc_port
);
387 * Routine: check_exc_receiver_dependency
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.
395 * Nothing locked and no resources held.
396 * Called from an exception context.
398 * KERN_SUCCESS if its ok to send exception message.
401 check_exc_receiver_dependency(
402 exception_type_t exception
,
403 struct exception_action
*excp
,
406 kern_return_t retval
= KERN_SUCCESS
;
408 if (excp
== NULL
|| exception
!= EXC_CRASH
) {
412 task_t task
= current_task();
414 ipc_port_t xport
= excp
[exception
].port
;
417 && task
->itk_space
== xport
->ip_receiver
) {
418 retval
= KERN_FAILURE
;
420 lck_mtx_unlock(mutex
);
426 * Routine: exception_triage_thread
428 * The thread caught an exception.
429 * We make an up-call to the thread's exception server.
431 * Nothing locked and no resources held.
432 * Called from an exception context, so
433 * thread_exception_return and thread_kdb_return
436 * KERN_SUCCESS if exception is handled by any of the handlers.
439 exception_triage_thread(
440 exception_type_t exception
,
441 mach_exception_data_t code
,
442 mach_msg_type_number_t codeCnt
,
446 host_priv_t host_priv
;
448 kern_return_t kr
= KERN_FAILURE
;
450 assert(exception
!= EXC_RPC_ALERT
);
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
460 if (panic_on_exception_triage
) {
461 panic("called exception_triage when it was forbidden by the boot environment");
465 * Try to raise the exception at the activation level.
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
) {
476 * Maybe the task level will handle it.
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
) {
488 * How about at the host level?
490 host_priv
= host_priv_self();
491 mutex
= &host_priv
->lock
;
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
) {
501 if ((exception
!= EXC_CRASH
) && (exception
!= EXC_RESOURCE
) &&
502 (exception
!= EXC_GUARD
) && (exception
!= EXC_CORPSE_NOTIFY
)) {
503 thread_exception_return();
509 * Routine: exception_triage
511 * The current thread caught an exception.
512 * We make an up-call to the thread's exception server.
514 * Nothing locked and no resources held.
515 * Called from an exception context, so
516 * thread_exception_return and thread_kdb_return
519 * KERN_SUCCESS if exception is handled by any of the handlers.
523 exception_type_t exception
,
524 mach_exception_data_t code
,
525 mach_msg_type_number_t codeCnt
)
527 thread_t thread
= current_thread();
528 return exception_triage_thread(exception
, code
, codeCnt
, thread
);
533 exception_type_t exception
,
534 mach_exception_data_t code
,
535 mach_msg_type_number_t codeCnt
)
539 thread_t self
= current_thread();
543 * Maybe the task level will handle it.
545 task
= current_task();
546 mutex
= &task
->itk_lock_data
;
548 kr
= exception_deliver(self
, exception
, code
, codeCnt
, task
->exc_actions
, mutex
);
550 if (kr
== KERN_SUCCESS
|| kr
== MACH_RCV_PORT_DIED
) {
558 * Raise an exception on a task.
559 * This should tell launchd to launch Crash Reporter for this task.
562 task_exception_notify(exception_type_t exception
,
563 mach_exception_data_type_t exccode
, mach_exception_data_type_t excsubcode
)
565 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
566 wait_interrupt_t wsave
;
567 kern_return_t kr
= KERN_SUCCESS
;
570 code
[1] = excsubcode
;
572 wsave
= thread_interrupt_level(THREAD_UNINT
);
573 kr
= exception_triage(exception
, code
, EXCEPTION_CODE_MAX
);
574 (void) thread_interrupt_level(wsave
);
580 * Handle interface for special performance monitoring
581 * This is a special case of the host exception handler
584 sys_perf_notify(thread_t thread
, int pid
)
588 wait_interrupt_t wsave
;
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 */
596 struct task
*task
= thread
->task
;
597 xport
= hostp
->exc_actions
[EXC_RPC_ALERT
].port
;
599 /* Make sure we're not catching our own exception */
600 if (!IP_VALID(xport
) ||
602 task
->itk_space
== xport
->data
.receiver
) {
606 wsave
= thread_interrupt_level(THREAD_UNINT
);
607 ret
= exception_deliver(
614 (void)thread_interrupt_level(wsave
);